Back to Python Course

Python Course

Operators are special symbols used to perform operations on variables and values. Python supports various types of operators such as arithmetic, comparison, assignment, logical, and membership.

Arithmetic Operators

Used for basic mathematical operations.

Operator Meaning Example + Addition a + b - Subtraction a - b * Multiplication a * b / Division a / b // Floor Division a // b % Modulus a % b ** Exponentiation a ** b

Let's see some examples,

a = 10 b = 3 print(a + b) # 13 print(a / b) # 3.333... print(a // b) # 3

Comparison Operators

Used to compare two values. Always return a boolean (True or False).

Operator Meaning Example == Equal to a == b != Not equal to a != b > Greater than a > b < Less than a < b >= Greater or equal a >= b <= Less or equal a <= b

Let's see some examples,

a = 5 b = 7 print(a == b) # False print(a < b) # True

Assignment Operators

Used to assign values to variables and update them in place.

Operator Meaning Example = Assign a = 10 += Add and assign a += 5 -= Subtract and assign a -= 3 *= Multiply and assign a *= 2 /= Divide and assign a /= 4

Let's see some examples,

x = 5 x += 2 # x is now 7

Logical Operators

Used to combine multiple boolean conditions.

Operator Meaning Example and True if both are true a > 5 and b < 10 or True if at least one is true a > 5 or b < 10 not Inverts the boolean value not(a > 5)

Let's see some examples,

a = 6 b = 8 print(a > 5 and b < 10) # True print(not a > 5) # False

Membership Operators

Used to test for membership in sequences like strings, lists, etc.

Operator Meaning Example in True if value in sequence "a" in "praveen" not in True if value not in sequence "z" not in "praveen"

Let's see some examples,

print("a" in "praveen") # True print("z" not in "praveen") # True