0% found this document useful (0 votes)
0 views

Python Control Flow- Conditionals and Loops

This document explains how to control the flow of execution in Python using conditional statements and loops. It covers the use of `if`, `elif`, and `else` for conditionals, as well as `for` and `while` loops for repetition, including examples for each. Additionally, it discusses loop control statements like `break` and `continue` to manage loop execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python Control Flow- Conditionals and Loops

This document explains how to control the flow of execution in Python using conditional statements and loops. It covers the use of `if`, `elif`, and `else` for conditionals, as well as `for` and `while` loops for repetition, including examples for each. Additionally, it discusses loop control statements like `break` and `continue` to manage loop execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

# Python Control Flow: Conditionals and Loops

This file introduces how to control the flow of execution in Python using conditional
statements and loops.

1. Conditional Statements (`if`, `elif`, `else`):


- Allow you to execute different blocks of code based on whether certain conditions are true
or false.
- `if condition:`: Executes the indented block of code if the condition is true.
- `elif condition:`: (else if) Checks another condition if the preceding `if` or `elif` condition
was false. You can have multiple `elif` blocks.
- `else:`: Executes the indented block of code if none of the preceding `if` or `elif` conditions
were true.
- Example:
```python
score = 75
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "D"
print(f"Your grade is: {grade}")
```

2. Loops:
- Allow you to repeatedly execute a block of code.
- **`for` loop:** Iterates over a sequence (like a list, tuple, string, or the result of `range()`)
or other iterable objects.
```python
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

for i in range(5): # Generates a sequence of numbers from 0 to 4


print(i)
```
- **`while` loop:** Executes a block of code as long as a specified condition is true.
```python
count = 0
while count < 5:
print(f"Count is: {count}")
count += 1
```

3. Loop Control Statements:


- `break`: Immediately terminates the loop and execution continues with the statement after
the loop.
```python
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num == 3:
break
print(num) # Output: 1, 2
```
- `continue`: Skips the rest of the current iteration of the loop and proceeds to the next
iteration.
```python
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num == 3:
continue
print(num) # Output: 1, 2, 4, 5
```

Control flow structures are essential for creating programs that can make decisions and
perform repetitive tasks, making your code dynamic and powerful.

You might also like