type casting and operators
type casting and operators
Example:
x = 5 # Integer
y = 2.5 # Float
Example:
Example:
print(bool(0)) # False
print(bool(1)) # True
print(bool("")) # False
print(bool("Hi")) # True
print(bool([])) # False
print(bool([0])) # True (Non-empty list)
Arithmetic Operators
Comparison (Relational) Operators
Logical Operators
Assignment Operators
Membership Operators
Identity Operators
Bitwise Operators
1. Arithmetic Operators
These operators perform mathematical operations.
Example:
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.3333
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000
Example:
x = 10
y = 5
print(x == y) # False
print(x != y) # True
print(x > y) # True
print(x < y) # False
print(x >= y) # True
print(x <= y) # False
3. Logical Operators
These operators are used to combine conditional statements.
Example:
x = 10
y = 5
z = 8
print(x > y and y < z) # True
print(x > y or y > z) # True
print(not(x > y)) # False
4. Assignment Operators
These operators assign values to variables.
Example:
x = 10
x += 5 # x = x + 5
print(x) # 15
x *= 2 # x = x * 2
print(x) # 30
x //= 4 # x = x // 4
print(x) # 7
5. Membership Operators
These operators check if a value is in a sequence (like a list, string, or tuple).
Example:
fruits = ["apple", "banana", "cherry"]
6. Identity Operators
These operators check if two objects have the same memory location.
Example:
a = [1, 2, 3]
b = a # Both refer to the same object
c = [1, 2, 3] # Different object with the same values
print(a is b) # True
print(a is c) # False
print(a is not c) # True
7. Bitwise Operators
These operators work at the binary level.
Example:
x = 5 # 101 in binary
y = 3 # 011 in binary