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)

Programming skills
Programming skills
COMMON Europe
 
Variables & Data Types In Python | Edureka
Variables & Data Types In Python | Edureka
Edureka!
 
Scope rules : local and global variables
Scope rules : local and global variables
sangrampatil81
 
What are variables and keywords in c++
What are variables and keywords in c++
Abdul Hafeez
 
Break and continue
Break and continue
Frijo Francis
 
Call by value
Call by value
Dharani G
 
Functions in c
Functions in c
kalavathisugan
 
6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
Venkateswara Babu Ravipati
 
Looping statement in python
Looping statement in python
RaginiJain21
 
JVM
JVM
baabtra.com - No. 1 supplier of quality freshers
 
Operator in c programming
Operator in c programming
Manoj Tyagi
 
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
Venugopalavarma Raja
 
Operators in python
Operators in python
deepalishinkar1
 
Datastructures in python
Datastructures in python
hydpy
 
Picture box control
Picture box control
chauhankapil
 
Strings in python
Strings in python
Prabhakaran V M
 
programming with python ppt
programming with python ppt
Priyanka Pradhan
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_c
ForwardBlog Enewzletter
 
Functions in python
Functions in python
Ilian Iliev
 
Python-01| Fundamentals
Python-01| Fundamentals
Mohd Sajjad
 

Similar to Operators and Control Statements in Python (20)

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

Recently uploaded (20)

NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...
ijait
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Flow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docx
rifka575530
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
Water demand - Types , variations and WDS
Water demand - Types , variations and WDS
dhanashree78
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
ravicivil
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...
ijait
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Flow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docx
rifka575530
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
Water demand - Types , variations and WDS
Water demand - Types , variations and WDS
dhanashree78
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
ravicivil
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
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
  • 30. Quiz https://realpython.com/quizzes/python- operators-expressions/ Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 30
  • 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
  • 42. Quiz https://realpython.com/quizzes/python- conditional-statements/ Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 42
  • 43. Python Programming - Operators and Conditional Statements by Dr.A.Rajeswari,AP-II/CSE,VEC 43