Operators & Expressions
Operators & Expressions in Python
Operators are special symbols that perform operations on values or variables.
Example: +, -, *, /, ==, and, etc.
An expression is a combination of variables, values, and operators that produces a result.
Example: x + y * 2
1. Arithmetic Operators
Definition:
Used to perform mathematical calculations between numbers.
Syntax:
result = operand1 operator operand2
Operators:
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division (float)
// Floor Division
% Modulus (remainder)
** Exponentiation
Example 1:
a = 10
b=3
print(a + b)
print(a ** b)
Output:
13
1000
Example 2:
x = 15
y=4
print(x / y)
print(x % y)
Output:
3.75
2. Comparison (Relational) Operators
Definition:
Used to compare two values and return True or False.
Syntax:
result = operand1 operator operand2
Operators:
Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Example 1:
a = 10
b = 20
print(a < b)
print(a == b)
Output:
True
False
Example 2:
x=5
y=5
print(x >= y)
print(x != 10)
Output:
True
True
3. Logical Operators
Definition:
Used to combine conditional statements (returns True or False).
Syntax:
result = expression1 operator expression2
Operators:
Operator Meaning
and True if both conditions are true
or True if any one condition is true
not Reverses the result
Example 1:
a=5
b = 10
print(a > 0 and b > 0)
print(a > 10 or b > 0)
Output:
True
True
Example 2:
x = False
print(not x)
print(True and not x)
Output:
True
True
4. Assignment Operators
Definition:
Used to assign values to variables.
Syntax:
variable operator= value
Operators:
Operator Example Same As
= x=5 assign value
+= x += 3 x=x+3
-= x -= 2 x=x-2
*= x *= 4 x=x*4
/= x /= 5 x=x/5
%= x %= 2 x=x%2
Example 1:
x = 10
x += 5
print(x)
Output:
15
Example 2:
a = 20
a //= 3
print(a)
Output:
5. Bitwise Operators
Definition:[O1]
Used to perform bit-level operations (on binary numbers).
Syntax:
result = operand1 operator operand2
Operators:
Operator Meaning Example
& AND 5&3=1
` ` OR
^ XOR 5^3=6
~ NOT ~5 = -6
<< Left shift 5 << 1 = 10
>> Right shift 5 >> 1 = 2
Example 1:
a = 5 # 0101
b = 3 # 0011
print(a & b)
print(a | b)
Output:
Example 2:
x=8
print(x << 1)
print(x >> 2)
Output:
16
6. Operator Precedence
Definition:
Operator precedence determines which operation is performed first in an expression.
Example Order (Highest → Lowest):
** → * / // % → + - → < > == != → and → or
Example 1:
result = 10 + 2 * 3
print(result)
Output:
16 # because 2*3 is done first
Example 2:
result = (10 + 2) * 3
print(result)
Output:
36 # parentheses change the order
7. Expressions & Short-Circuit Evaluation
Definition:
Short-circuit means — in and and or expressions,
Python stops evaluating as soon as the final result is known.
Example 1 (and):
x=0
print(x and 5)
Output:
0 # stops at first False value
Example 2 (or):
y=0
print(y or 5)
Output:
5 # stops at first True value
Practice Programs
1. Temperature Conversion (Celsius → Fahrenheit)
Example
c = float(input("Enter Celsius: "))
f = (c * 9/5) + 32
print("Fahrenheit:", f)
Output:
Enter Celsius: 37
Fahrenheit: 98.6
2. Even or Odd Check
Example:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
Output:
Enter a number: 11
Odd number