Operators are symbols or special keywords used to perform operations on values and variables.
For example, we can use operators to add two numbers, compare values, check conditions, and perform many other operations.
Python has six main categories of operators:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Special Operators
1) Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, and division.
- Addition
- Subtraction
- Multiplication / Division // Floor Division % Modulus Operator ** Exponent Operator Or Power Operator
Example:
no1 = 10
no2 = 2
print(no1 + no2) # 12
print(no1 - no2) # 8
print(no1 * no2) # 20
print(no1 / no2) # 5.0
print(no1 // no2) # 5
print(no1 % no2) # 0
print(no1 ** no2) # 100
Multiple variable assignment:
We can assign values to multiple variables in a single line.
no1=10, no2=3 // SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
no1, no2 = 10, 3
print(no1 + no2) // 13
no1=10
no2=3
print(no1 + no2) // 13
Operators with strings:
The + operator can join two strings, and the * operator can repeat a string.
name = "Prem "
print(name * 3) # Prem Prem Prem
print(name + "Kumar") # Prem Kumar
But we cannot directly add a string and an integer.
name = "Prem"
print(name + 3) # TypeError
We also cannot use the exponent operator with strings.
print(name ** 3) # TypeError
Division by zero:
Dividing a number by zero raises a ZeroDivisionError.
no1, no2 = 10, 0
print(no1/no2) //ZeroDivisionError:division by zero
print(no1//no2) //ZeroDivisionError:division by zero
Relational Operators
Relational operators are used to compare two values. The result will always be either True or False.
,<, >=, <=,==,!=
Example:
no1, no2 = 10, 20
print(no1 > no2) # False
print(no1 < no2) # True
print(no1 >= no2) # False
print(no1 <= no2) # True
print(no1 == no2) # False
print(no1 != no2) # True
Comparing strings:
Python compares strings using lexicographical order (based on Unicode values).
name1 = 'ajith'
name2 = 'vijay'
print(name1 > name2) //False
print(name1 < name2) //True
Python is case-sensitive. Uppercase and lowercase letters have different Unicode values.
For example:
name1 = 'Ajith'
name2 = 'ajith'
print(name1 > name2) //False
The Unicode value of A is 65, while the Unicode value of a is 97.
name1 = 'ajith'
name2 = 'ajithkumar'
print(name1 > name2) //False
Comparing Boolean values:
In Python, True behaves like 1 and False behaves like 0 in numeric comparisons.
print(True > True) //False
print(False > True) //False
print(False < True) //True
True -> Non-zero value it will be 1 or 1.3 or 0.005
False -> Zero
Chained comparisons:
Python allows us to combine multiple comparisons in one expression.
no1, no2, no3 = 100, 200, 300
print(no3 > no2 < no1) //False
print(no3 > no2 > no1) //False
Python evaluates these as two comparisons joined by and.
name1 = 'sanjay'
name2 = 'sanjay'
print(name1 == name2) #Equality Operator //True
print(name1 != name2) //False
Logical Operators
Logical operators are used to combine conditions or reverse a condition.
Python has three logical operators: and,or,not
AND operator
The and operator evaluates to True only when both conditions are true.
Example:
print(True and True) # True
print(True and False) # False
OR Operator
The or operator evaluates to True when at least one condition is true.
Example:
print(True or True) # True
print(False or True) # True
print(False or False) # False
NOT Operator
The not operator reverses the Boolean result.
print(not True) # False
print(not False) # True
Important: Logical operators can also work with numbers and strings.
In Python, 0, None, and empty values such as "" are considered falsy. Most other values are truthy.
Also, and and or return one of their operands, not necessarily a Boolean value.
no1,no2=100,200;
print(no1 and no2) //200
print(100 and 200) //200
print(True and True) //True
no1,no2=0,200;
print(no1 and no2) //0
print(0 and 200) //0
print(False and True) //False
no1,no2=100,200;
print(no1 or no2) //100
print(100 or 200) //100
print(True or True) //True
no1,no2=0,200;
print(no1 or no2) //200
print(0 or 200) // 200
print(False or True) //True
print(not 5) // False
print(not 0) //True
print(not 'Vishnu') //False
print(not '') //True
Bitwise Operators
Bitwise operators perform operations on the binary (bit) representation of integers.
& | ~ >> <<
Bitwise AND (&)
The result is 1 only when both corresponding bits are 1. Otherwise, the result is 0.
print(4 & 5) # 4
Binary calculation:
4 = 100
5 = 101
-----------
100 = 4
Bitwise OR (|)
The result is 1 when at least one of the corresponding bits is 1.
print(4 | 5) # 5
Binary calculation:
4 = 100
5 = 101
-----------
101 = 5
Top comments (0)