http://www.skillbrew.com
/SkillbrewTalent brewed by the industry itself
Operators
Pavan Verma
@YinYangPavan
Python Programming Essentials
1
© SkillBrew http://skillbrew.com
Comparison operators
2
>>> 200 > 199
True
>>> 'foo' > 'bar'
True
>>> 100 < 90
False
>>> 'Foobar' <= 'Foobar'
True
Operator Description
a > b Greater than
a < b Less than
a >= b Greater than or equal
to
a <= b Less than or equal to
a == b Check if value of two
operands is equal
a != b Check if value of two
operands is not equal
© SkillBrew http://skillbrew.com
Comparison operators (2)
3
>>> 101 >= 100.0
True
>>> 100.01 == 100.01
True
>>> 'game' != 'game'
False
Operator Description
a > b Greater than
a < b Less than
a >= b Greater than or equal
to
a <= b Less than or equal to
a == b Check if value of two
operands is equal
a != b Check if value of two
operands is not equal
© SkillBrew http://skillbrew.com
Logical operators
4
>>> a, b, c = 10, 20, 30
>>> (a > b) and (b < c)
False
>>> (a < b) and (b < c)
True
>>> (a > b) or (b < c)
True
Operator Description
a and b Logical AND
If both operands are
True than it returns
True
a or b Logical OR
If one of the operands
is True then it returns
True
not Logical NOT
© SkillBrew http://skillbrew.com
Logical operators
5
>>> a, b, c = 10, 20, 30
>>> not a
False
>>> not (a > b)
True
>>> a > b
False
Operator Description
a and b Logical AND
If both operands are
True than it returns
True
a or b Logical OR
If one of the operands
is True then it returns
True
not Logical NOT
© SkillBrew http://skillbrew.com
Shorthand operators
6
>>> x = 5
>>> x +=1 # x = x + 1
>>> x
6
>>> x -=2 # x = x - 2
>>> x
4
>>> x *=3 # x = x * 3
>>> x
12
© SkillBrew http://skillbrew.com
Shorthand operators (2)
7
>>> x /=4
>>> x
3
>>> x >>=1
>>> x
1
>>> x <<=2
>>> x
4
>>> x **=3
>>> x
64
© SkillBrew http://skillbrew.com
Increment and decrement
the ++ and -- operators were left out for consistency and
simplicity
>>> x = 5
>>> x += 1
>>> x
6
>>> x -= 2
>>> x
4
8
Use shorthand
operators to
perform increment
and decrement
9
Operator Precedence
© SkillBrew http://skillbrew.com
Why is operator precedence required?
10
This one Python statement
could mean many things
hence we need operator
precedence to determine
the order in which evaluation
takes place
>>> 10 + 20 * 30 / 10
70
>>> (10 + 20) * 30 / 10
90
>>> 10 + (20 * 30) / 10
70
>>> (10 + 20 * 30) / 10
61
>>> 10 + 20 * (30 / 10)
70
© SkillBrew http://skillbrew.com
Operator precedence (highest to lowest)
Operator Description
** exponentiation (raise to the power)
~ Complement
*, /, %, // multiply, divide, modulo and floor
division
+, - addition and subtraction
>>, << right and left bitwise shift
& bitwise 'AND'
11
© SkillBrew http://skillbrew.com
Operator precedence (highest to lowest) (2)
Operator Description
^, | Bitwise exclusive `OR' and regular
`OR'
<=, >= Comparison operators
==, != Equality operators
%=, /=, -=, +=, *=, **= Shorthand operators
is, is not Identity operators
in, not in Membership operators
not , or, and Logical operators
12
© SkillBrew http://skillbrew.com
Operator precedence (3)
13
>>> 10 + 20 * 30 / 10
70
>>> (10 + 20 * 30) / 10
61
>>> (10 + 20) * 30 / 10
90
>>> 2 + 2 == 4
True
>>> 2 + (2 == 4)
2
© SkillBrew http://skillbrew.com
Operator precedence (3)
14
When in doubt always use round brackets ()
© SkillBrew http://skillbrew.com
Summary
 Comparison operators
 Logical operators
 Bitwise operators
 Short-hand operators
 Increment and decrement
 Operator precedence
15
© SkillBrew http://skillbrew.com
Resources
 Increment and decrement behavior
http://stackoverflow.com/questions/1485841/behaviour-of-
increment-and-decrement-operators-in-python
 Tutorial on numbers
http://www.tutorialspoint.com/python/python_numbers.htm
16
17

Python Programming Essentials - M11 - Comparison and Logical Operators

  • 1.
    http://www.skillbrew.com /SkillbrewTalent brewed bythe industry itself Operators Pavan Verma @YinYangPavan Python Programming Essentials 1
  • 2.
    © SkillBrew http://skillbrew.com Comparisonoperators 2 >>> 200 > 199 True >>> 'foo' > 'bar' True >>> 100 < 90 False >>> 'Foobar' <= 'Foobar' True Operator Description a > b Greater than a < b Less than a >= b Greater than or equal to a <= b Less than or equal to a == b Check if value of two operands is equal a != b Check if value of two operands is not equal
  • 3.
    © SkillBrew http://skillbrew.com Comparisonoperators (2) 3 >>> 101 >= 100.0 True >>> 100.01 == 100.01 True >>> 'game' != 'game' False Operator Description a > b Greater than a < b Less than a >= b Greater than or equal to a <= b Less than or equal to a == b Check if value of two operands is equal a != b Check if value of two operands is not equal
  • 4.
    © SkillBrew http://skillbrew.com Logicaloperators 4 >>> a, b, c = 10, 20, 30 >>> (a > b) and (b < c) False >>> (a < b) and (b < c) True >>> (a > b) or (b < c) True Operator Description a and b Logical AND If both operands are True than it returns True a or b Logical OR If one of the operands is True then it returns True not Logical NOT
  • 5.
    © SkillBrew http://skillbrew.com Logicaloperators 5 >>> a, b, c = 10, 20, 30 >>> not a False >>> not (a > b) True >>> a > b False Operator Description a and b Logical AND If both operands are True than it returns True a or b Logical OR If one of the operands is True then it returns True not Logical NOT
  • 6.
    © SkillBrew http://skillbrew.com Shorthandoperators 6 >>> x = 5 >>> x +=1 # x = x + 1 >>> x 6 >>> x -=2 # x = x - 2 >>> x 4 >>> x *=3 # x = x * 3 >>> x 12
  • 7.
    © SkillBrew http://skillbrew.com Shorthandoperators (2) 7 >>> x /=4 >>> x 3 >>> x >>=1 >>> x 1 >>> x <<=2 >>> x 4 >>> x **=3 >>> x 64
  • 8.
    © SkillBrew http://skillbrew.com Incrementand decrement the ++ and -- operators were left out for consistency and simplicity >>> x = 5 >>> x += 1 >>> x 6 >>> x -= 2 >>> x 4 8 Use shorthand operators to perform increment and decrement
  • 9.
  • 10.
    © SkillBrew http://skillbrew.com Whyis operator precedence required? 10 This one Python statement could mean many things hence we need operator precedence to determine the order in which evaluation takes place >>> 10 + 20 * 30 / 10 70 >>> (10 + 20) * 30 / 10 90 >>> 10 + (20 * 30) / 10 70 >>> (10 + 20 * 30) / 10 61 >>> 10 + 20 * (30 / 10) 70
  • 11.
    © SkillBrew http://skillbrew.com Operatorprecedence (highest to lowest) Operator Description ** exponentiation (raise to the power) ~ Complement *, /, %, // multiply, divide, modulo and floor division +, - addition and subtraction >>, << right and left bitwise shift & bitwise 'AND' 11
  • 12.
    © SkillBrew http://skillbrew.com Operatorprecedence (highest to lowest) (2) Operator Description ^, | Bitwise exclusive `OR' and regular `OR' <=, >= Comparison operators ==, != Equality operators %=, /=, -=, +=, *=, **= Shorthand operators is, is not Identity operators in, not in Membership operators not , or, and Logical operators 12
  • 13.
    © SkillBrew http://skillbrew.com Operatorprecedence (3) 13 >>> 10 + 20 * 30 / 10 70 >>> (10 + 20 * 30) / 10 61 >>> (10 + 20) * 30 / 10 90 >>> 2 + 2 == 4 True >>> 2 + (2 == 4) 2
  • 14.
    © SkillBrew http://skillbrew.com Operatorprecedence (3) 14 When in doubt always use round brackets ()
  • 15.
    © SkillBrew http://skillbrew.com Summary Comparison operators  Logical operators  Bitwise operators  Short-hand operators  Increment and decrement  Operator precedence 15
  • 16.
    © SkillBrew http://skillbrew.com Resources Increment and decrement behavior http://stackoverflow.com/questions/1485841/behaviour-of- increment-and-decrement-operators-in-python  Tutorial on numbers http://www.tutorialspoint.com/python/python_numbers.htm 16
  • 17.

Editor's Notes

  • #3 Comparison operators allow you to compare two values.
  • #4 Comparison operators allow you to compare two values.
  • #5 Logical operators compare Boolean expressions and return a Boolean result. A boolean expression is an expression that results in a boolean value, that is, in a value of either true or false. More complex boolean expressions can be built out of simpler expressions, using the following boolean operators.
  • #6 Logical operators compare Boolean expressions and return a Boolean result. A boolean expression is an expression that results in a boolean value, that is, in a value of either true or false. More complex boolean expressions can be built out of simpler expressions, using the following boolean operators.
  • #7 A shorthand operator is a shorter way to express something that is already available in the programming language Shorthand operations do not add any feature to the programming language The intension here is to just walk through shorthand operators one by one
  • #8 The intension here is to just walk through shorthand operators one by one