“Introduction to Python Programming” Learning Path
Operators
Operators are special symbols used to perform operations on variables and values in Python. They help us perform calculations, comparisons, and logical decisions in a program.
Arithmetic Operators
These operators are used to perform mathematical operations.
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
** | Exponent (power) | a ** b |
// | Floor division | a // b |
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.33
print(a % b) # 1
print(a ** b) # 1000
print(a // b) # 3
Comparison Operators
These operators compare two values and return True or False.
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Example:
a = 10
b = 5
print(a == b) # False
print(a != b) # True
print(a > b) # True
print(a < b) # False
Logical Operators
Logical operators are used to combine multiple conditions.
| Operator | Meaning |
|---|---|
and | True if both conditions are true |
or | True if at least one condition is true |
not | Reverses the result |
Example:
a = 10
print(a > 5 and a < 20) # True
print(a > 15 or a < 20) # True
print(not(a > 5)) # False
Assignment Operators
These operators are used to assign values to variables.
| Operator | Example | Meaning |
|---|---|---|
= | x = 5 | Assign value |
+= | x += 3 | x = x + 3 |
-= | x -= 2 | x = x – 2 |
*= | x *= 2 | x = x * 2 |
/= | x /= 2 | x = x / 2 |
Example:
x = 10
x += 5
print(x) # 15
x *= 2
print(x) # 30