Spex3
    

Operators in Python 🐍


    

    

Operators are symbols used to perform operations on variables and values. Python has several types of operators, including arithmetic, comparison, and logical operators.

1️⃣ Arithmetic Operators 🔢
Used for basic mathematical operations.

Operator Description Example Result
+ Addition 5 + 3 8
- Subtraction 10 - 4 6
* Multiplication 6 * 3 18
/ Division 10 / 2 5.0
// Floor Division 10 // 3 3 (Removes decimal)
% Modulus (Remainder) 10 % 3 1
** Exponentiation (Power) 2 ** 3 8


Example:


a = 10
b = 3

print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.333
print(a // b) # Output: 3
print(a % b) # Output: 1
print(a ** b) # Output: 1000


2️⃣ Comparison (Relational) Operators 🔍

Used to compare values and return True or False.

Operator Description Example Result
== 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 >= 5 True
<= Less than or equal to 3 <= 5 True


Example:

x = 10
y = 5

print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: True
print(x < y) # Output: False
print(x >= 10) # Output: True
print(y <= 5) # Output: True


3️⃣ Logical Operators 🧠

Used to combine Boolean values (True/False).

Operator Description Example Result
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 Boolean value not True False

Example:

a = True
b = False

print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False

Example with Comparisons:

x = 10
y = 5

print(x > 5 and y < 10) # Output: True
print(x < 5 or y < 10) # Output: True
print(not (x == 10)) # Output: False


    Date: 2025-03-22 00:00:00.000000