1. Introduction
In the previous tutorial, we covered Python variables and data types. In this tutorial, we’ll dive into the basics of operators, conditional statements, and loops in Python. These concepts are essential for making decisions and repeating tasks in your programs.
2. Operators in Python
Operators are symbols or keywords used to perform operations on variables and values. Below are the different types of operators in Python:
2.1 Arithmetic Operators
Arithmetic operators are used for mathematical operations.
2.2 Comparison Operators
x = 10
y = 3
print(x + y) # Addition
print(x - y) # Subtraction
print(x * y) # Multiplication
print(x / y) # Division
print(x % y) # Modulus (remainder)
print(x // y) # Floor Division (quotient without remainder)
print(x ** y) # Exponentiation (x raised to the power y)
Comparison operators compare two values and return a Boolean result (True or False).
print(x == y) # Equal to
print(x != y) # Not equal to
print(x > y) # Greater than
print(x < y) # Less than
print(x >= y) # Greater than or equal to
print(x <= y) # Less than or equal to
2.3 Logical Operators
Logical operators combine conditional statements.
a = True
b = False
print(a and b) # True if both are True
print(a or b) # True if at least one is True
print(not a) # Negates the value of a
2.4 Assignment Operators
Assignment operators are used to assign values to variables.
x = 5 # Assign
x += 3 # Add and assign
x -= 2 # Subtract and assign
x *= 4 # Multiply and assign
x /= 2 # Divide and assign
2.5 Membership Operators
Membership operators test whether a value is in a sequence (like a list or string).
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # True
print("grape" not in fruits) # True
2.6 Identity Operators
Identity operators compare memory locations of two objects.
x = [1, 2, 3]
y = [1, 2, 3]
z = x
print(x is z) # True (same object)
print(x is y) # False (different objects with same content)
print(x is not y) # True
3. Conditional Statements
Conditional statements allow your program to make decisions based on conditions. Python uses if
, elif
, and else
statements.
age = 20
has_id = True
if age >= 18 and has_id:
print("You can enter the club.")
else:
print("Access denied.")
4. Loops
Loops are used to repeat a block of code multiple times.
4.1 for
Loop
The for
loop is used to iterate over sequences such as lists or ranges.
for i in range(1, 6):
print(i * i) # Prints squares of numbers from 1 to 5
4.2 while
Loop
The while
loop runs as long as a condition is true.
count = 0
while count < 5:
print(f"Count: {count}")
count += 1
4.3 Using Operators in Loops
You can combine loops with operators to perform calculations or conditions.
numbers = [1, 2, 3, 4, 5]
total = 0
for num in numbers:
total += num # Adding numbers using the += operator
print(f"Total: {total}")
5. Controlling Loops
Python allows controlling loops using break
and continue
.
for i in range(1, 11):
if i % 2 == 0: # Skip even numbers
continue
print(i)
6. Common Mistakes
- Misusing comparison and logical operators.
- Forgetting to update variables in
while
loops. - Overcomplicating conditions.