Control Flow with if-else and loop statements in Python
Rama Krishna Vankam
Digital Marketing Strategist | Expert in SEO, PPC, and Content Marketing | Driving 30%+ Organic Traffic Growth for Global Brands
?
### 1. Control Flow with if-else Statements
Here’s an explanation along with examples for Control Flow (if-else statements, loops) in Python under the Basic Syntax and Data Types section:
?
The if-else statement allows you to execute certain code based on conditions. It checks whether a condition is True or False and executes the appropriate block of code.
?
#### Syntax:
```python
if condition:
??? # code to execute if condition is True
elif another_condition:
??? # code to execute if another_condition is True
else:
??? # code to execute if all conditions are False
```
?
#### Example:
age = 20
if age >= 18:
print("You are an adult.")
elif age >= 13:
print("You are a teenager.")
else:
print("You are a child.")
?
#### Explanation:
- In this example, the program checks if age is 18 or more. If so, it prints "You are an adult."
- If age is not 18 or more but at least 13, it prints "You are a teenager."
- Otherwise, it prints "You are a child."
?
### 2. Loops in Python
Loops allow you to repeat a block of code multiple times.
?
#### a) for Loop
A for loop is typically used to iterate over a sequence (such as a list, tuple, string, or range).
?
#### Syntax:
```python
for item in sequence:
??? # code to execute for each item
```
?
#### Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
?
#### Explanation:
- The loop iterates over each element in the fruits list.
领英推荐
- In each iteration, the fruit variable holds the current element of the list, and it is printed.
?
#### b) while Loop
A while loop is used when you want to repeat a block of code as long as a condition is True.
?
#### Syntax:
```python
while condition:
??? # code to execute while condition is True
```
?
#### Example:
count = 0
while count < 5:
print("Count is:", count)
count += 1
?
#### Explanation:
- The loop continues to execute as long as count is less than 5.
- In each iteration, the value of count is printed, and then count is incremented by 1 (`count += 1`).
?
### 3. Breaking Out of Loops
Sometimes, you may want to exit a loop before it finishes. You can do this using the break statement.
?
#### Example:
for number in range(10):
if number == 5:
break
print(number)
?
#### Explanation:
- The loop will print numbers from 0 to 4.
- When number reaches 5, the break statement exits the loop, and no further numbers are printed.
?
### 4. Skipping Iterations with continue
The continue statement skips the current iteration of a loop and continues with the next one.
?
#### Example:
for number in range(5):
if number == 2:
continue
print(number)
?
#### Explanation:
- The loop prints numbers from 0 to 4, except 2.
- When number equals 2, the continue statement skips that iteration and moves to the next one.
?
---
?
This explanation covers the basics of control flow in Python, including conditional statements and loops.
Digital Marketing Strategist | Expert in SEO, PPC, and Content Marketing | Driving 30%+ Organic Traffic Growth for Global Brands
5 个月