SlideShare a Scribd company logo
Python Programming
Dr.A.Rajeswari, M.Tech, Ph.D
Assistant Professor II
Department of Computer Science
and Engineering
Velammal Engineering College
Operators and Operands
2
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC
• Operators are symbols that instruct the
computer to perform a task
• Operands are expressions or values on which
an operator operates or works
• Example: a+b
a,b are operands
+ is the operator
Based on Number of Operands
• Unary Operator- one operand
Example: -a
• Binary Operator- Two operand
Example: a+5
• Ternary Operator- Three operand
conditional operator (?:)
Example: a if a>b else b
3
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC
Types of Operator
4
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC
Python language supports the following types of operators.
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Arithmetic Operators
Arithmetic operators take numerical values (either literals or variables) as their
operands and return a single numerical value. Assume variable a holds 30(a=30)
and variable b holds 18(b=18) then
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 5
Example Code
a,b=30,18
print('a=',a)
print('b=',b)
print('na+b=',a+b)
print('a-b=',a-b)
print('a*b=',a*b)
print('a/b=',a/b)
print('a%b=',a%b)
print('a**b=',a**b)
print('a//b=',a//b)
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 6
Comparison Operators
Also called a relational operator is a
programming language construct or operator
that tests or defines some kind of relation
between two entities. relational operators
return the integers 0 or 1, where 0 stands for
false and 1 stands for true
Assume variable a holds 30 and variable b holds
18,
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 7
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 8
Example Code
a,b=30,18
print('a=',a)
print('b=',b)
print('na>b is ',a>b)
print('a<b is ',a<b)
print('a==b is ',a==b)
print('a!=b is ',a!=b)
print('na<=b is ',a<=b)
print('na>=b is ',a>=b)
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 9
Assignment Operators
Assume variable a holds 30 and variable b holds 18, and c is a variable
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 10
a,b=30,18
print('a=',a)
print('b=',b)
c=a+b
print('c=a+b=',c)
c+=a #c=c+a c=48+30=78 now c is 78
print('c=c+a=',c)
c-=a #c=c-a c=78-30=48 now c is 48
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 11
Cont..
print('c=c-a=',c)
c*=a #c=c*a c=48*30=1440 now c is 1440
print('c=c*a=',c)
c/=a #c=c/a c=1440/30=48 now c is 48
print('c=c/a=',c)
c%=a #c=c%a c=48%30=18 now c is 18
print('c=c%a=',c)
c**=a #c=c**a c=18^30=4.551715960790334e+37 now
c is 4.551715960790334e+37
print('c=c^a=',c)
c//=a #c=c//a
c=4.551715960790334e+37//30=1.517238653596778e+3 6 now
c is 1.517238653596778e+36
print('c=c//a',c)
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 12
Bitwise Operators
Bitwise operator works on bits and performs bit
by bit operation
and gate
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 13
OR and NOT Gate
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 14
X-OR Gate
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 15
Assume if a = 60; and b = 13; Now in binary
format they will be as follows
a=0011 1100 b=0000 1101
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 16
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 17
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 18
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 19
<< Binary Left Shift
• a << 2 Returns a with the bits shifted to the
left by 2 places (and new bits on the right-
hand-side are zeros).
• a<<2 0 0 1 1 1 1 0 0<<2 1 1 1 1 0 0 0 0 240
>> Binary Right Shift
• a >> 2 Returns a with the bits shifted to the
right by 2 places (and new bits on the left-
hand-side are zeros).
• a>>2 0 0 1 1 1 1 0 0>>2 0 0 0 0 1 1 1 1 15
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 20
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 21
Example Code
a=0b00111100
b=0b00001101
print('a=',bin(a),'b=',bin(b))
print('a or b is=',bin(a|b))
print('a and b is=',bin(a&b))
print('a xor b is=',bin(a^b))
print('Ones Complement of a=',bin(~a))
print('a Left Shift by 2 is',bin(a<<2))
print('a Right Shift by 2 is',bin(a>>2))
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 22
Logical Operators
here are following logical operators supported by Python language. Assume
variable a holds 10 and variable b holds 20 then
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 23
Example Code
a,b=10,20
c=c=(a>11)and (b>10) # 0 and 1 so result is false
print(c)
c=(a>11)or(b>10) # 0 or 1 so result is true print(c)
a,b=10,20
c=not((a>11)and (b>10)) # not(0 and 1)=not(false) so
result is true
print(c)
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 24
Membership Operators
Python’s membership operators test for membership in a sequence, such as
strings, lists, or tuples. There are two membership operators as explained
below
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 25
Example Code
a=[1,2,3,4,5,6,7,8,9,10] # a is a list
print(3 in a) # 3 in list a so result is true
print(20 in a) # 20 not in list a so result is false
print(3 not in a) # 3 not in list a ,(but 3 in list a) so
result is false
print(20 not in a) # 20 not in list a ,(but 20 not
in list a ) so result is true
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 26
Identity Operators
Identity operators compare the memory locations of two objects. There are two
Identity operators as explained below
Example Code
a,b=10,100
print(a is b)
print(a is not b)
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 27
ALL Python Operators Precedence
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 28
Cont..
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 29
Quiz
https://realpython.com/quizzes/python-
operators-expressions/
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 30
Control Statement
Flow of control through any given program is
implemented with three basic types of control
structures: Sequential, Selection and Repetition.
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 31
Decision Making Statement
Decision making statements in programming languages
decides the direction of flow of program execution.
Decision making statements available in python are:
• if statement
• if..else statements
• nested if statements
• if-elif ladder
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 32
if Statement
Syntax:
if condition:
# Statements to
execute if
# condition is true
if condition:
statement1
statement2
# Here if the condition
is true, if block
# will consider only
statement1 to be
inside
# its block.
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 33
Example Code
i = 10
if (i > 15):
print ("10 is less than 15")
print ("I am Not in if")
Output:
I am Not in if
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 34
if- else Statement
Syntax:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 35
Example Code
i = 20
if (i < 15):
print ("i is smaller than 15")
print ("i'm in if Block")
else:
print ("i is greater than 15")
print ("i'm in else Block")
print ("i'm not in if and not in else
Block")
Output
i is greater than 15
i'm in else Block
i'm not in if and not in else
Block
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 36
Nested-if Statement
Syntax:
if (condition1):
# Executes when condition1 is
true
if (condition2):
# Executes when
condition2 is true
# if Block is end here
# if Block is end here
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 37
Example Code
i = 10
if (i == 10):
# First if statement
if (i < 15):
print ("i is smaller than 15")
# Nested - if statement
# Will only be executed if
statement above
# it is true
if (i < 12):
print ("i is smaller than 12 too")
else:
print ("i is greater than 15")
Output:
i is smaller than 15
i is smaller than 12 too
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 38
If-elif-else ladder
Syntax
if (condition):
statement
elif (condition):
statement
.
.
else:
statement
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 39
Example Code
i = 20
if (i == 10):
print ("i is 10")
elif (i == 15):
print ("i is 15")
elif (i == 20):
print ("i is 20")
else:
print ("i is not present")
Output
i is 20
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 40
Example Programs
• Odd or Even Number
• Positive or Negative Number
• Leap year or not
• Greatest of two numbers and three numbers
• Eligibility for voting
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 41
Quiz
https://realpython.com/quizzes/python-
conditional-statements/
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 42
Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 43

More Related Content

What's hot (20)

PPTX
Operators in python
deepalishinkar1
 
PPTX
Prim's algorithm
Pankaj Thakur
 
PPTX
sum of subset problem using Backtracking
Abhishek Singh
 
PPTX
6-Python-Recursion PPT.pptx
Venkateswara Babu Ravipati
 
PPTX
Chapter 03 python libraries
Praveen M Jigajinni
 
PPT
Time complexity.ppt
YekoyeTigabuYeko
 
PPT
Bucket sort
Hossain Md Shakhawat
 
PPTX
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
muthukrishnavinayaga
 
PPTX
stack & queue
manju rani
 
PPTX
Python dictionary
Mohammed Sikander
 
PPT
Python GUI Programming
RTS Tech
 
PPTX
Python: Modules and Packages
Damian T. Gordon
 
PPTX
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
PPTX
Python-Functions.pptx
Karudaiyar Ganapathy
 
PPTX
Python Data Structures and Algorithms.pptx
ShreyasLawand
 
PPT
Binary Search
kunj desai
 
PDF
Operators in python
Prabhakaran V M
 
PPTX
asymptotic notation
SangeethaSasi1
 
PDF
Datatypes in python
eShikshak
 
PPTX
Basic data types in python
sunilchute1
 
Operators in python
deepalishinkar1
 
Prim's algorithm
Pankaj Thakur
 
sum of subset problem using Backtracking
Abhishek Singh
 
6-Python-Recursion PPT.pptx
Venkateswara Babu Ravipati
 
Chapter 03 python libraries
Praveen M Jigajinni
 
Time complexity.ppt
YekoyeTigabuYeko
 
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
muthukrishnavinayaga
 
stack & queue
manju rani
 
Python dictionary
Mohammed Sikander
 
Python GUI Programming
RTS Tech
 
Python: Modules and Packages
Damian T. Gordon
 
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
Python-Functions.pptx
Karudaiyar Ganapathy
 
Python Data Structures and Algorithms.pptx
ShreyasLawand
 
Binary Search
kunj desai
 
Operators in python
Prabhakaran V M
 
asymptotic notation
SangeethaSasi1
 
Datatypes in python
eShikshak
 
Basic data types in python
sunilchute1
 

Similar to Operators and Control Statements in Python (20)

PPT
Py-Slides-2 (1).ppt
KalaiVani395886
 
PPT
Py-Slides-2.ppt
TejaValmiki
 
PPT
Py-Slides-2.ppt
AllanGuevarra1
 
PPT
hlukj6;lukm,t.mnjhgjukryopkiu;lyk y2.ppt
PraveenaFppt
 
PPTX
python statement, expressions and operators.pptx
richumt
 
PPT
python operators.ppt
ErnieAcuna
 
PDF
ESIT135 Problem Solving Using Python Notes of Unit-1 and Unit-2
prasadmutkule1
 
PPTX
Python programming language introduction unit
michaelaaron25322
 
PDF
23CSC101T PSPP python programming - UNIT 3.pdf
RajeshThanikachalam
 
PDF
23CSC101T PSPP python program - UNIT 3.pdf
RajeshThanikachalam
 
PDF
Python : basic operators
S.M. Salaquzzaman
 
PPTX
Python Programming | JNTUK | UNIT 1 | Lecture 5
FabMinds
 
PPTX
Operators in Python
Anusuya123
 
PPTX
Strings in python are surrounded by eith
Orin18
 
PPTX
Python Lec-6 Operatorguijjjjuugggggs.pptx
ks812227
 
PPTX
Python tutorials for beginners | IQ Online Training
Rahul Tandale
 
PPTX
Operators Concept in Python-N.Kavitha.pptx
Kavitha713564
 
PPTX
OPERATOR IN PYTHON-PART2
vikram mahendra
 
PPTX
python operators.pptx
irsatanoli
 
PPTX
Python notes for students to develop and learn
kavithaadhilakshmi
 
Py-Slides-2 (1).ppt
KalaiVani395886
 
Py-Slides-2.ppt
TejaValmiki
 
Py-Slides-2.ppt
AllanGuevarra1
 
hlukj6;lukm,t.mnjhgjukryopkiu;lyk y2.ppt
PraveenaFppt
 
python statement, expressions and operators.pptx
richumt
 
python operators.ppt
ErnieAcuna
 
ESIT135 Problem Solving Using Python Notes of Unit-1 and Unit-2
prasadmutkule1
 
Python programming language introduction unit
michaelaaron25322
 
23CSC101T PSPP python programming - UNIT 3.pdf
RajeshThanikachalam
 
23CSC101T PSPP python program - UNIT 3.pdf
RajeshThanikachalam
 
Python : basic operators
S.M. Salaquzzaman
 
Python Programming | JNTUK | UNIT 1 | Lecture 5
FabMinds
 
Operators in Python
Anusuya123
 
Strings in python are surrounded by eith
Orin18
 
Python Lec-6 Operatorguijjjjuugggggs.pptx
ks812227
 
Python tutorials for beginners | IQ Online Training
Rahul Tandale
 
Operators Concept in Python-N.Kavitha.pptx
Kavitha713564
 
OPERATOR IN PYTHON-PART2
vikram mahendra
 
python operators.pptx
irsatanoli
 
Python notes for students to develop and learn
kavithaadhilakshmi
 
Ad

Recently uploaded (20)

PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
Design Thinking basics for Engineers.pdf
CMR University
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Ad

Operators and Control Statements in Python

  • 1. Python Programming Dr.A.Rajeswari, M.Tech, Ph.D Assistant Professor II Department of Computer Science and Engineering Velammal Engineering College
  • 2. Operators and Operands 2 Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC • Operators are symbols that instruct the computer to perform a task • Operands are expressions or values on which an operator operates or works • Example: a+b a,b are operands + is the operator
  • 3. Based on Number of Operands • Unary Operator- one operand Example: -a • Binary Operator- Two operand Example: a+5 • Ternary Operator- Three operand conditional operator (?:) Example: a if a>b else b 3 Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC
  • 4. Types of Operator 4 Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC Python language supports the following types of operators. • Arithmetic Operators • Comparison (Relational) Operators • Assignment Operators • Logical Operators • Bitwise Operators • Membership Operators • Identity Operators
  • 5. Arithmetic Operators Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. Assume variable a holds 30(a=30) and variable b holds 18(b=18) then Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 5
  • 7. Comparison Operators Also called a relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. relational operators return the integers 0 or 1, where 0 stands for false and 1 stands for true Assume variable a holds 30 and variable b holds 18, Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 7
  • 8. Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 8
  • 9. Example Code a,b=30,18 print('a=',a) print('b=',b) print('na>b is ',a>b) print('a<b is ',a<b) print('a==b is ',a==b) print('a!=b is ',a!=b) print('na<=b is ',a<=b) print('na>=b is ',a>=b) Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 9
  • 10. Assignment Operators Assume variable a holds 30 and variable b holds 18, and c is a variable Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 10
  • 11. a,b=30,18 print('a=',a) print('b=',b) c=a+b print('c=a+b=',c) c+=a #c=c+a c=48+30=78 now c is 78 print('c=c+a=',c) c-=a #c=c-a c=78-30=48 now c is 48 Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 11
  • 12. Cont.. print('c=c-a=',c) c*=a #c=c*a c=48*30=1440 now c is 1440 print('c=c*a=',c) c/=a #c=c/a c=1440/30=48 now c is 48 print('c=c/a=',c) c%=a #c=c%a c=48%30=18 now c is 18 print('c=c%a=',c) c**=a #c=c**a c=18^30=4.551715960790334e+37 now c is 4.551715960790334e+37 print('c=c^a=',c) c//=a #c=c//a c=4.551715960790334e+37//30=1.517238653596778e+3 6 now c is 1.517238653596778e+36 print('c=c//a',c) Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 12
  • 13. Bitwise Operators Bitwise operator works on bits and performs bit by bit operation and gate Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 13
  • 14. OR and NOT Gate Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 14
  • 15. X-OR Gate Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 15
  • 16. Assume if a = 60; and b = 13; Now in binary format they will be as follows a=0011 1100 b=0000 1101 Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 16
  • 17. Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 17
  • 18. Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 18
  • 19. Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 19
  • 20. << Binary Left Shift • a << 2 Returns a with the bits shifted to the left by 2 places (and new bits on the right- hand-side are zeros). • a<<2 0 0 1 1 1 1 0 0<<2 1 1 1 1 0 0 0 0 240 >> Binary Right Shift • a >> 2 Returns a with the bits shifted to the right by 2 places (and new bits on the left- hand-side are zeros). • a>>2 0 0 1 1 1 1 0 0>>2 0 0 0 0 1 1 1 1 15 Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 20
  • 21. Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 21
  • 22. Example Code a=0b00111100 b=0b00001101 print('a=',bin(a),'b=',bin(b)) print('a or b is=',bin(a|b)) print('a and b is=',bin(a&b)) print('a xor b is=',bin(a^b)) print('Ones Complement of a=',bin(~a)) print('a Left Shift by 2 is',bin(a<<2)) print('a Right Shift by 2 is',bin(a>>2)) Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 22
  • 23. Logical Operators here are following logical operators supported by Python language. Assume variable a holds 10 and variable b holds 20 then Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 23
  • 24. Example Code a,b=10,20 c=c=(a>11)and (b>10) # 0 and 1 so result is false print(c) c=(a>11)or(b>10) # 0 or 1 so result is true print(c) a,b=10,20 c=not((a>11)and (b>10)) # not(0 and 1)=not(false) so result is true print(c) Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 24
  • 25. Membership Operators Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 25
  • 26. Example Code a=[1,2,3,4,5,6,7,8,9,10] # a is a list print(3 in a) # 3 in list a so result is true print(20 in a) # 20 not in list a so result is false print(3 not in a) # 3 not in list a ,(but 3 in list a) so result is false print(20 not in a) # 20 not in list a ,(but 20 not in list a ) so result is true Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 26
  • 27. Identity Operators Identity operators compare the memory locations of two objects. There are two Identity operators as explained below Example Code a,b=10,100 print(a is b) print(a is not b) Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 27
  • 28. ALL Python Operators Precedence Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 28
  • 29. Cont.. Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 29
  • 31. Control Statement Flow of control through any given program is implemented with three basic types of control structures: Sequential, Selection and Repetition. Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 31
  • 32. Decision Making Statement Decision making statements in programming languages decides the direction of flow of program execution. Decision making statements available in python are: • if statement • if..else statements • nested if statements • if-elif ladder Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 32
  • 33. if Statement Syntax: if condition: # Statements to execute if # condition is true if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block. Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 33
  • 34. Example Code i = 10 if (i > 15): print ("10 is less than 15") print ("I am Not in if") Output: I am Not in if Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 34
  • 35. if- else Statement Syntax: if (condition): # Executes this block if # condition is true else: # Executes this block if # condition is false Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 35
  • 36. Example Code i = 20 if (i < 15): print ("i is smaller than 15") print ("i'm in if Block") else: print ("i is greater than 15") print ("i'm in else Block") print ("i'm not in if and not in else Block") Output i is greater than 15 i'm in else Block i'm not in if and not in else Block Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 36
  • 37. Nested-if Statement Syntax: if (condition1): # Executes when condition1 is true if (condition2): # Executes when condition2 is true # if Block is end here # if Block is end here Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 37
  • 38. Example Code i = 10 if (i == 10): # First if statement if (i < 15): print ("i is smaller than 15") # Nested - if statement # Will only be executed if statement above # it is true if (i < 12): print ("i is smaller than 12 too") else: print ("i is greater than 15") Output: i is smaller than 15 i is smaller than 12 too Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 38
  • 39. If-elif-else ladder Syntax if (condition): statement elif (condition): statement . . else: statement Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 39
  • 40. Example Code i = 20 if (i == 10): print ("i is 10") elif (i == 15): print ("i is 15") elif (i == 20): print ("i is 20") else: print ("i is not present") Output i is 20 Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 40
  • 41. Example Programs • Odd or Even Number • Positive or Negative Number • Leap year or not • Greatest of two numbers and three numbers • Eligibility for voting Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 41
  • 43. Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 43