Python Operators
Python Operators
Operators in Python facilitate various computations, comparisons, and logical operations on data. Operators in Python enable computations, comparisons, and logical evaluations within a program. Python provides different operator categories, each designed for specific types of computations and manipulations.
1. Arithmetic Operators
Arithmetic operators are used for mathematical operations.
Operator | Description | Example |
---|---|---|
+ |
Addition | 5 + 3 = 8 |
- |
Subtraction | 5 - 3 = 2 |
* |
Multiplication | 5 * 3 = 15 |
/ |
Division | 5 / 2 = 2.5 |
// |
Floor Division | 5 // 2 = 2 |
% |
Modulus (remainder) | 5 % 2 = 1 |
** |
Exponentiation | 5 ** 2 = 25 |
Example:
a = 10 b = 3 print(a + b) # Output: 13 print(a ** b) # Output: 1000
2. Comparison (Relational) Operators
Comparison operators evaluate relationships between values and yield a Boolean result.
Operator | Description | Example |
---|---|---|
== |
Equal to | 5 == 5 = True |
!= |
Not equal to | 5 != 3 = True |
> |
Greater than | 5 > 3 = True |
< |
Less than | 5 < 3 = False |
>= |
Greater than or equal to | 5 >= 3 = True |
<= |
Less than or equal to | 5 <= 3 = False |
Example:
x = 7 y = 10 print(x > y) # Output: False print(x != y) # Output: True
3. Logical Operators
Logical operators help in evaluating multiple conditions simultaneously.
Operator | Description | Example |
---|---|---|
and |
Returns True if both conditions are True |
True and False = False |
or |
Returns True if at least one condition is True |
True or False = True |
not |
Reverses the logical state | not True = False |
Example:
a = 5 b = 10 print(a > 3 and b < 20) # Output: True print(not (a > b)) # Output: True
4. Assignment Operators
Assignment operators store and update values in variables efficiently.
Operator | Description | Example |
---|---|---|
= |
Assigns value | x = 5 |
+= |
Adds and assigns | x += 3 (x = x + 3) |
-= |
Subtracts and assigns | x -= 3 (x = x - 3) |
*= |
Multiplies and assigns | x *= 3 |
/= |
Divides and assigns | x /= 3 |
//= |
Floor divides and assigns | x //= 3 |
%= |
Modulus and assigns | x %= 3 |
**= |
Exponentiation and assigns | x **= 3 |
Example:
x = 10 x += 5 print(x) # Output: 15
5. Bitwise Operators
Bitwise operators operate on binary numbers.
Operator | Description | Example |
---|---|---|
& |
AND | 5 & 3 = 1 |
| |
OR | 5 | 3 = 7 |
^ |
XOR | 5 ^ 3 = 6 |
~ |
Complement | ~5 = -6 |
<< |
Left Shift | 5 << 1 = 10 |
>> |
Right Shift | 5 >> 1 = 2 |
Example:
a = 5 # 101 in binary b = 3 # 011 in binary print(a & b) # Output: 1 print(a | b) # Output: 7
6. Membership Operators
These operators determine whether a value exists within a given sequence.(like strings, lists, tuples).
Operator | Description | Example |
---|---|---|
in |
Returns True if value exists in sequence |
'a' in 'apple' = True |
not in |
Returns True if value does not exist in sequence |
'b' not in 'apple' = True |
Example:
fruits = ["apple", "banana", "cherry"] print("banana" in fruits) # Output: True print("grape" not in fruits) # Output: True
7. Identity Operators
give me unique word this details is repeted please give me unique line.
Operator | Description | Example |
---|---|---|
is |
Returns True if objects are identical (same memory location) |
a is b |
is not |
Returns True if objects are not identical |
a is not b |
Example:
x = [1, 2, 3] y = x z = [1, 2, 3] print(x is y) # Output: True print(x is z) # Output: False
8. Special Operators
Ternary Operator (Conditional Expression)
Python supports concise conditional expressions using the ternary operator for quick decision-making.
Example:
a, b = 5, 10 result = "A is greater" if a > b else "B is greater" print(result) # Output: B is greater
Summary:
Python operators are used to perform calculations, comparisons, and logical evaluations on variables and values. They help in writing efficient and concise code for various programming tasks.