Skip to content

Commit 96b7399

Browse files
authored
Merge branch 'master' into python-join-strings
2 parents 33aec44 + 4de2690 commit 96b7399

24 files changed

+214
-0
lines changed

python-for-loop/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python for Loops: The Pythonic Way
2+
3+
This folder provides the code examples for the Real Python tutorial [Python for Loops: The Pythonic Way](https://realpython.com/python-for-loop/).

python-for-loop/async_range.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import asyncio
2+
3+
4+
class AsyncRange:
5+
def __init__(self, start, end):
6+
self.data = range(start, end)
7+
8+
async def __aiter__(self):
9+
for index in self.data:
10+
await asyncio.sleep(0.5)
11+
yield index
12+
13+
14+
async def main():
15+
async for index in AsyncRange(0, 5):
16+
print(index)
17+
18+
19+
asyncio.run(main())

python-for-loop/break_continue.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
numbers = [1, 3, 5, 7, 9]
2+
target = 5
3+
for number in numbers:
4+
print(f"Processing {number}...")
5+
if number == target:
6+
print(f"Target found {target}!")
7+
break
8+
9+
numbers = [1, 2, 3, 4, 5, 6]
10+
for number in numbers:
11+
print(f"{number = }")
12+
if number % 2 != 0:
13+
continue
14+
print(f"{number} is even!")

python-for-loop/built_in_types.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
numbers = [1, 2, 3, 4]
2+
for number in numbers:
3+
print(number)
4+
5+
person = ("Jane", 25, "Python Dev", "Canada")
6+
for field in person:
7+
print(field)
8+
9+
text = "abcde"
10+
for character in text:
11+
print(character)
12+
13+
for index in range(5):
14+
print(index)

python-for-loop/chained_lists.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from itertools import chain
2+
3+
first = [7, 6, 1]
4+
second = [4, 1]
5+
third = [8, 0, 6]
6+
7+
for value in chain(first, second, third):
8+
print(value**2)

python-for-loop/colors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
colors = ["red", "green", "blue", "yellow"]
2+
3+
for color in colors:
4+
print(color)

python-for-loop/cubes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cubes = []
2+
for number in range(10):
3+
cubes.append(number**3)
4+
print(cubes)
5+
6+
cubes = [number**3 for number in range(10)]
7+
print(cubes)

python-for-loop/dictionaries.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
students = {
2+
"Alice": 89.5,
3+
"Bob": 76.0,
4+
"Charlie": 92.3,
5+
"Diana": 84.7,
6+
"Ethan": 88.9,
7+
}
8+
for student in students:
9+
print(student)
10+
11+
for student in students:
12+
print(student, "->", students[student])
13+
14+
for student in students.keys():
15+
print(student)
16+
17+
teams = {
18+
"Colorado": "Rockies",
19+
"Chicago": "White Sox",
20+
"Boston": "Red Sox",
21+
"Minnesota": "Twins",
22+
"Milwaukee": "Brewers",
23+
"Seattle": "Mariners",
24+
}
25+
for team in teams.values():
26+
print(team)
27+
28+
for place, team in teams.items():
29+
print(place, "->", team)

python-for-loop/else_clause.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
numbers = [1, 3, 5, 7, 9]
2+
target = 42
3+
4+
for number in numbers:
5+
print(f"Processing {number}...")
6+
if number == target:
7+
print(f"Target found {target}!")
8+
break
9+
else:
10+
print(f"Target no found {target}")

python-for-loop/exceptions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
files = ["file1.txt", "file2.txt", "file3.txt"]
2+
3+
for file in files:
4+
with open(file, "r") as f:
5+
print(f"Contents of {file}:")
6+
print(f.read())
7+
8+
for file in files:
9+
try:
10+
with open(file, "r") as f:
11+
print(f"Contents of {file}:")
12+
print(f.read())
13+
except FileNotFoundError:
14+
print(f"Error: {file} not found. Skipping.")

0 commit comments

Comments
 (0)