-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sample code for the article on for loops #620
Open
lpozo
wants to merge
5
commits into
master
Choose a base branch
from
python-for-loop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e028ac1
Sample code for the article on for loops
lpozo eceb1b4
Liter issues
lpozo f9e63d5
Merge branch 'master' into python-for-loop
martin-martin eda34e8
Merge branch 'master' into python-for-loop
martin-martin 310798e
TR updates, first round
lpozo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Python "for" Loops: The Pythonic Way | ||
|
||
This folder provides the code examples for the Real Python tutorial [Python "for" Loops: The Pythonic Way](https://realpython.com/python-for-loop-update/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import asyncio | ||
|
||
|
||
class AsyncRange: | ||
def __init__(self, start, end): | ||
self.data = range(start, end) | ||
|
||
async def __aiter__(self): | ||
for index in self.data: | ||
await asyncio.sleep(0.5) | ||
yield index | ||
|
||
|
||
async def main(): | ||
async for index in AsyncRange(0, 5): | ||
print(index) | ||
|
||
|
||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
numbers = [1, 3, 5, 7, 9] | ||
target = 5 | ||
for number in numbers: | ||
print(f"Processing {number}...") | ||
if number == target: | ||
print(f"Target found {target}!") | ||
break | ||
|
||
numbers = [1, 2, 3, 4, 5, 6] | ||
for number in numbers: | ||
print(f"{number = }") | ||
if number % 2 != 0: | ||
continue | ||
print(f"{number} is even!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
numbers = [1, 2, 3, 4] | ||
for number in numbers: | ||
print(number) | ||
|
||
person = ("Jane", 25, "Python Dev", "Canada") | ||
for field in person: | ||
print(field) | ||
|
||
text = "abcde" | ||
for character in text: | ||
print(character) | ||
|
||
for index in range(5): | ||
print(index) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
colors = ["red", "green", "blue", "yellow"] | ||
|
||
for color in colors: | ||
print(color) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cubes = [] | ||
for number in range(10): | ||
cubes.append(number**3) | ||
print(cubes) | ||
|
||
cubes = [number**3 for number in range(10)] | ||
print(cubes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
students = { | ||
"Alice": 89.5, | ||
"Bob": 76.0, | ||
"Charlie": 92.3, | ||
"Diana": 84.7, | ||
"Ethan": 88.9, | ||
} | ||
for student in students: | ||
print(student) | ||
|
||
for student in students: | ||
print(student, "->", students[student]) | ||
|
||
for student in students.keys(): | ||
print(student) | ||
|
||
teams = { | ||
"Colorado": "Rockies", | ||
"Chicago": "White Sox", | ||
"Boston": "Red Sox", | ||
"Minnesota": "Twins", | ||
"Milwaukee": "Brewers", | ||
"Seattle": "Mariners", | ||
} | ||
for team in teams.values(): | ||
print(team) | ||
|
||
for city, team in teams.items(): | ||
print(city, "->", team) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
numbers = [1, 3, 5, 7, 9] | ||
target = 42 | ||
|
||
for number in numbers: | ||
print(f"Processing {number}...") | ||
if number == target: | ||
print(f"Target found {target}!") | ||
break | ||
else: | ||
print(f"Target no found {target}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
files = ["file1.txt", "file2.txt", "file3.txt"] | ||
|
||
for file in files: | ||
with open(file, "r") as f: | ||
print(f"Contents of {file}:") | ||
print(f.read()) | ||
|
||
for file in files: | ||
try: | ||
with open(file, "r") as f: | ||
print(f"Contents of {file}:") | ||
print(f.read()) | ||
except FileNotFoundError: | ||
print(f"Error: {file} not found. Skipping.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fruits = ["orange", "apple", "mango", "lemon"] | ||
|
||
for index in range(len(fruits)): | ||
fruit = fruits[index] | ||
print(index, fruit) | ||
|
||
for index, fruit in enumerate(fruits): | ||
print(index, fruit) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from itertools import chain | ||
|
||
matrix = [ | ||
[9, 3, 8], | ||
[4, 5, 2], | ||
[6, 4, 3], | ||
] | ||
|
||
for value in chain(*matrix): | ||
print(value**2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def display_menu(options): | ||
print("Main Menu:") | ||
for pos, option in enumerate(options, start=1): | ||
print(f"{pos}. {option}") | ||
|
||
|
||
display_menu(["Open", "Save", "Settings", "Quit"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
for number in range(1, 11): | ||
for product in range(number, number * 11, number): | ||
print(f"{product:>4d}", end="") | ||
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
numbers = [1, 2, 3] | ||
letters = ["a", "b", "c"] | ||
|
||
for number, letter in zip(numbers, letters): | ||
print(number, "->", letter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
for _ in range(3): | ||
print("Knock, knock, knock") | ||
print("Penny!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
actions = ["Type text", "Select text", "Cut text", "Paste text"] | ||
|
||
for action in reversed(actions): | ||
print(f"Undo: {action}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
tools = {"Django", "Flask", "pandas", "NumPy"} | ||
|
||
for tool in tools: | ||
print(tool) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
students = { | ||
"Alice": 89.5, | ||
"Bob": 76.0, | ||
"Charlie": 92.3, | ||
"Diana": 84.7, | ||
"Ethan": 88.9, | ||
"Fiona": 95.6, | ||
"George": 73.4, | ||
"Hannah": 81.2, | ||
} | ||
|
||
sorted_students = sorted( | ||
students.items(), key=lambda item: item[1], reverse=True | ||
) | ||
|
||
for name, grade in sorted_students: | ||
print(f"{name}'s average grade: {grade:->{20-len(name)}.1f}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
points = [(1, 4), (3, 6), (7, 3)] | ||
|
||
for x, y in points: | ||
print(f"{x = } and {y = }") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flake8 compains and I was also wondering about it, I've generally only seen this used without whitespace:
pycodestyle
, which implements the check, seems to be stubborn about keeping it this way: PyCQA/pycodestyle#1201$ flake8 break_continue.py break_continue.py:11:20: E251 unexpected spaces around keyword / parameter equals break_continue.py:11:22: E202 whitespace before '}' break_continue.py:11:22: E251 unexpected spaces around keyword / parameter equals
But I know you're using space around the equals sign also in the f-string tutorial.
So IDK, I guess the linters don't fail on it?
If you decide to change it, you'd also have to change it in the written tutorial.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I remember correctly, in one of my previous articles I used this feature without spaces and I guess it was Geir Arne who suggested adding the spaces for readability. However, I don't think this is a written rule.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, yeah I'm fine with leaving it considering it passes our linter checks. @gahjelle should we note this somewhere, that we'll ignore E251 and E202 for these cases?