0% found this document useful (0 votes)
54 views15 pages

Python Operators Explained: Types & Usage

The document provides an overview of various Python operators, including arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. It explains the functionality of each operator with examples and discusses operator precedence. This serves as a comprehensive guide for understanding how to perform operations on variables and values in Python.

Uploaded by

abhishekabix08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views15 pages

Python Operators Explained: Types & Usage

The document provides an overview of various Python operators, including arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. It explains the functionality of each operator with examples and discusses operator precedence. This serves as a comprehensive guide for understanding how to perform operations on variables and values in Python.

Uploaded by

abhishekabix08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Python Operators

• Operators are used to perform operations on variables


and values
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Arithmetic Operators

•+ Addition x+y
•- Subtraction x - y
•* Multiplication x*y
•/ Division x/y
•% Modulus x%y
• ** Exponentiation x ** y
• // Floor division x // y
• x=5
• y=3
• print(x + y)
• print(x -y)
• print(x * y)
• print(x / y)
• print(x ** y)
• print(x // y)
• print(x % y)
• x = 5.5
• y=3
• print(x + y)
• print(x -y)
• print(x * y)
• print(x / y)
• print(x ** y)
• print(x // y)
• print(x % y)
Python Assignment Operators

•= x=5 x=5 print(x)


• += x += 3 x = x + 3 print(x)
• -= x -= 3 x = x - 3 print(x)
• *= x *= 3 x = x * 3 print(x)
Python Comparison Operators
== Equal
x=5
y=3
print(x == y)

!= Not equal
x=5
y=3
print(x != y)
•> Greater than
•< Less than
• >= Greater than or equal to
• x=5
• y=3

• print(x >= y)
• <= Less than or equal to
Python Logical Operators

• and Returns True if both statements are true x < 5 and x < 10
• or Returns True if one of the statements is true x < 5 or x < 4
• not Reverse the result, returns False if the result is true not(x
< 5 and x < 10)

• x = 15
• print(x > 3 and x < 10)
• x = 50
• print(not(x > 3 and x < 100))
Identity operators
• They are used to compare the objects, not if they are
equal, but if they are actually the same object, with the
same memory location:
• 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
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x
print(x is z)
# returns True because z is the same object as x
print(x is y)
# returns False because x is not the same object as y, even if they have the same
content
print(x == y)
# to demonstrate the difference between "is" and "==": this comparison returns True
because x is equal to y
Python Membership Operators

• Membership operators are used to test if a sequence is


presented in an object:

in
• Returns True if a sequence with the specified value is present in the object
• x in y
• not in
• Returns True if a sequence with the specified value is not present in the
object
• x not in y
• x = ["apple", "banana"]

• print("banana" in x)
Bitwise Operators

& AND Sets each bit to 1 if both bits are 1 x&y


a=2
b=3
print(a & b)
| OR Sets each bit to 1 if one of two bits is 1 x|y
a=2
b=3
print(a | b)
^ XOR Sets each bit to 1 if only one of two bits is 1 x ^ y
a=2
b=3
print(a ^ b)
~ NOT Inverts all the bits ~x
x=3
print(~x)
<< Zero fill left shift Shift left by pushing zeros in from the right
and let the leftmost bits fall off x << 2
print(x << 2)
>> Signed right shift Shift right by pushing copies of the leftmost
bit in from the left, and let the rightmost bits fall off x >> 2
print(x >> 2)
Operator Precedence
1. () Parentheses
2. ** Exponentiation
3. +x -x ~x Unary plus, unary minus, and bitwise NOT
4. * / // % Multiplication, division, floor division, and modulus
5. + - Addition and subtraction
6. << >> Bitwise left and right shifts
7. & Bitwise AND
8. ^ Bitwise XOR
9. | Bitwise OR
10. == != > >= < <= is is not in not in Comparisons, identity, and membership operators
11. not Logical NOT
12. and AND
13. or OR
# 1 has highest precedence

You might also like