Learn Python in 30 Days - Day 3: Operators and Expressions (with Examples)
Welcome to Day 3! Today, we’ll learn about Python operators and how to use them in expressions. Operators help you perform calculations, comparisons, and other operations in your programs. Let’s dive in!
1. What are Operators?
Operators are symbols or keywords that perform operations on variables and values.
Types of Operators in Python:
- Arithmetic Operators
- Comparison (Relational) Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
2. Arithmetic Operators
Used for mathematical operations.
Operator | Description | Example |
---|---|---|
+ |
Addition | 5 + 3 = 8 |
- |
Subtraction | 10 - 4 = 6 |
* |
Multiplication | 2 * 6 = 12 |
/ |
Division | 15 / 3 = 5.0 |
% |
Modulus (Remainder) | 7 % 3 = 1 |
** |
Exponentiation | 2 ** 3 = 8 |
// |
Floor Division | 7 // 3 = 2 |
Example:
a = 15
b = 4
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Exponentiation:", a ** b)
Output:
Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3.75
Floor Division: 3
Modulus: 3
Exponentiation: 50625
3. Comparison Operators
Used to compare values. They return True
or False
.
Operator | Description | Example |
---|---|---|
== |
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 | 5 <= 3 → False |
Example:
x = 10
y = 7
print("Equal to:", x == y)
print("Not equal to:", x != y)
print("Greater than:", x > y)
print("Less than:", x < y)
print("Greater or equal to:", x >= y)
print("Less or equal to:", x <= y)
4. Logical Operators
Used to combine conditional statements.
Operator | Description | Example |
---|---|---|
and |
Returns True if both are true |
(5 > 3) and (3 < 10) |
or |
Returns True if one is true |
(5 > 3) or (3 > 10) |
not |
Reverses the result | not(5 > 3) |
Example:
a = 5
b = 3
c = 10
print("AND:", (a > b) and (b < c)) # True
print("OR:", (a > b) or (b > c)) # True
print("NOT:", not(a > b)) # False
5. Assignment Operators
Used to assign values to variables.
Operator | Example | Equivalent To |
---|---|---|
= |
x = 5 |
x = 5 |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 3 |
x = x - 3 |
*= |
x *= 3 |
x = x * 3 |
/= |
x /= 3 |
x = x / 3 |
Example:
x = 5
x += 3 # Add 3 to x
print("x after += 3:", x)
x *= 2 # Multiply x by 2
print("x after *= 2:", x)
Output:
x after += 3: 8
x after *= 2: 16
6. Practice Tasks for Day 3
Task 1: Arithmetic Operations
Write a program to calculate and print the area of a rectangle with width = 5 and height = 10:
width = 5
height = 10
area = width * height
print("Area of the rectangle:", area)
Task 2: Comparison
Create a program to check if a number is positive or negative:
num = -5
if num > 0:
print("The number is positive.")
else:
print("The number is negative.")
Task 3: Logical Operators
Write a program to check if a number is within a range:
num = 15
if num > 10 and num < 20:
print("The number is within the range.")
else:
print("The number is outside the range.")
7. Pro Tips for Day 3
- Play around with examples to see how different operators work.
- Practice writing small programs to solidify your understanding.
- Use comments to explain the purpose of each line in your code.
🎯 Day 3 Goal: Understand and apply arithmetic, comparison, logical, and assignment operators in Python.
Let me know when you’re ready for Day 4! 🚀