Python Operators
Python divides the operators in the following groups:
•Arithmetic operators
•Assignment operators
•Comparison operators
•Logical operators
•Identity operators
•Membership operators
•Bitwise operators
•Ternary operator
Python Identity Operators
Identity operators are used to compare the objects, not if they are
equal, but if they are actually the same object, with the same
memory location:
operators Description Example
is Returns True if both variables are the
same object
x is y
is not Returns True if both variables are not
the same object
x is not y
Python Membership Operators
Membership operators are used to test if a sequence is presented in an
object:
x
Python Bitwise Operators
Bitwise operators are used to compare (binary) numbers:
Ternary Operator in Python
The Python ternary operator determines if a condition is true or false and then returns the appropriate
value in accordance with the result. The ternary operator is useful in cases where we need to assign a
value to a variable based on a simple condition, and we want to keep our code more concise — all in
just one line of code. It’s particularly handy when you want to avoid writing multiple lines for a
simple if-else situation.
Syntax of Ternary Operator in Python
Syntax: [on_true] if [expression] else [on_false]
# Program to demonstrate conditional
operator
a, b = 10, 20
# Copy value of a in min if a < b else
copy b
min = a if a < b else b
print(min)
Operator precedence describes the order in which operations are
performed.
Operator Precedence
1. print((6 + 3) - (6 + 3))
2. print(100 - 3 ** 3)
3. print(100 + ~3)
4. print (100 + 5 * 3)
5. print (8 >> 4 - 2)
6. print (6 & 2 + 1)
7. print (6 ^ 2 + 1)
8. print (6 | 2 + 1)
9. print (5 == 4 + 1)
10. print(not 5 == 5)
0
73
96
115
2
2
5
7
True
False

Python operators (1).pptx

  • 1.
  • 2.
    Python divides theoperators in the following groups: •Arithmetic operators •Assignment operators •Comparison operators •Logical operators •Identity operators •Membership operators •Bitwise operators •Ternary operator
  • 3.
    Python Identity Operators Identityoperators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location: operators Description Example is Returns True if both variables are the same object x is y is not Returns True if both variables are not the same object x is not y
  • 6.
    Python Membership Operators Membershipoperators are used to test if a sequence is presented in an object:
  • 7.
  • 9.
    Python Bitwise Operators Bitwiseoperators are used to compare (binary) numbers:
  • 16.
    Ternary Operator inPython The Python ternary operator determines if a condition is true or false and then returns the appropriate value in accordance with the result. The ternary operator is useful in cases where we need to assign a value to a variable based on a simple condition, and we want to keep our code more concise — all in just one line of code. It’s particularly handy when you want to avoid writing multiple lines for a simple if-else situation. Syntax of Ternary Operator in Python Syntax: [on_true] if [expression] else [on_false]
  • 17.
    # Program todemonstrate conditional operator a, b = 10, 20 # Copy value of a in min if a < b else copy b min = a if a < b else b print(min)
  • 18.
    Operator precedence describesthe order in which operations are performed. Operator Precedence
  • 20.
    1. print((6 +3) - (6 + 3)) 2. print(100 - 3 ** 3) 3. print(100 + ~3) 4. print (100 + 5 * 3) 5. print (8 >> 4 - 2) 6. print (6 & 2 + 1) 7. print (6 ^ 2 + 1) 8. print (6 | 2 + 1) 9. print (5 == 4 + 1) 10. print(not 5 == 5)
  • 21.