- Introduction to Operators Operators in Python are special symbols or keywords used to perform operations on variables and values.
Python has several types of operators:
Arithmetic Operators
Relational (Comparison) Operators
Logical Operators
Assignment Operators
Bitwise Operators
- Arithmetic Operators Arithmetic operators are used to perform mathematical calculations.
Let us take two variables:
no1 = 10
no2 = 3
Operator Name Example Output
- Addition no1 + no2 13
- Subtraction no1 - no2 7
- Multiplication no1 * no2 30 / Division no1 / no2 3.3333... // Floor Division no1 // no2 3 % Modulus no1 % no2 1 ** Exponentiation (Power) no1 ** no2 1000 Example:
no1 = 10
no2 = 3
print(no1 + no2)
print(no1 - no2)
print(no1 * no2)
print(no1 / no2)
print(no1 // no2)
print(no1 % no2)
print(no1 ** no2)
Important: Division by zero causes a ZeroDivisionError.
no1 = 10
no2 = 0
print(no1 / no2) # ZeroDivisionError
print(no1 // no2) # ZeroDivisionError
- Variable Assignment and Multiple Variables Python allows us to assign values to variables in different ways.
Separate assignment:
no1 = 10
no2 = 20
Multiple assignment:
no1, no2 = 10, 20
print(no1)
print(no2)
Output:
10
20
The statement no1 = 10, no2 = 20 is invalid Python syntax because two assignments cannot be separated by a comma in this way.
Python also supports assigning the same value to multiple variables:
no1 = no2 = 10
- Single and Double Quotes in Python Python supports both single quotes (' ') and double quotes (" ") for strings.
name1 = 'Sanjay'
name2 = "Vishnu"
print(name1)
print(name2)
Both quotation styles work in the same way for ordinary strings.
- Relational (Comparison) Operators Comparison operators compare two values and return either True or False.
Operator Meaning Example Output
Greater than 10 > 5 True
< Less than 10 < 5 False
= Greater than or equal to 10 >= 10 True
<= Less than or equal to 5 <= 10 True
== Equal to 10 == 10 True
!= Not equal to 10 != 5 True
Example:
no1 = 100
no2 = 200
no3 = 300
print(no3 > no2 > no1)
Output:
True
This is called operator chaining. Python evaluates it as:
no3 > no2 and no2 > no1
Both comparisons are true.
String Comparison
name1 = 'sanjay'
name2 = 'sanjay'
print(name1 == name2)
Output:
True
The == operator checks whether two values are equal.
The = operator is an assignment operator, not a comparison operator. For example, name1 = name2 assigns the value of name2 to name1.
Python also compares strings lexicographically using Unicode code points. For example, 'A' has code point 65 and 'a' has code point 97, so:
print('A' < 'a')
Output:
True
- Logical Operators Logical operators are used to combine or reverse conditions.
Python has three logical operators:
and
or
not
A. AND Operator
The and operator returns a truthy result only when both operands are truthy. If either operand is falsy, it returns a falsy result.
no1 = 10
no2 = 20
print(no1 and no2)
Output:
20
Important: Python's and operator does not always return the Boolean value True or False. It can return one of its operands.
print(0 and 20)
Output:
0
Zero is a falsy value.
B. OR Operator
The or operator returns the first truthy operand. If all operands are falsy, it returns the last operand.
print(0 or 20)
Output:
20
C. NOT Operator
The not operator reverses the truth value and always returns a Boolean value.
print(not 10)
print(not 0)
print(not 'Vishnu')
print(not '')
Output:
False
True
False
True
A non-empty string is truthy, whereas an empty string is falsy.
Remember: not ' ' returns False because a string containing a space is not empty.
- Assignment Operators Assignment operators assign values to variables and provide shortcuts for updating them.
Example:
no1 = 5
no1 = no1 + 10
print(no1)
Output:
15
The shortcut version is:
no1 = 5
no1 += 10
print(no1)
Output:
15
Other arithmetic assignment operators include:
Operator Equivalent expression
+= no1 = no1 + 10
-= no1 = no1 - 10
= no1 = no1 * 10
/= no1 = no1 / 10
//= no1 = no1 // 10
%= no1 = no1 % 10
*= no1 = no1 ** 10
These operators make code shorter and easier to read.
- Bitwise Operators Bitwise operators perform operations on the binary representation of integers.
Operator Name Purpose
& Bitwise AND Sets a bit to 1 when both corresponding bits are 1
| Bitwise OR Sets a bit to 1 when either corresponding bit is 1
^ Bitwise XOR Sets a bit to 1 when the corresponding bits differ
~ Bitwise NOT Inverts the integer's bits
<< Left shift Shifts bits to the left
Right shift Shifts bits to the right
Example:
no1 = 5
no2 = 3
print(no1 & no2)
print(no1 | no2)
print(no1 ^ no2)
print(~no1)
print(no1 << 1)
print(no1 >> 1)
Output:
1
7
6
-6
10
2
For understanding bitwise operators, convert the numbers to binary.
5 = 101
3 = 011
For example, 5 & 3 gives 001, which equals 1 in decimal.
- Conclusion Today, I learned about Python operators and how they work with variables and values. I practised arithmetic calculations, comparisons, logical operations, assignment shortcuts, and bitwise operations.
Understanding these operators is an important foundation for learning conditional statements, loops, functions, and more advanced Python programming.
Top comments (0)