-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_loops.py
More file actions
49 lines (33 loc) · 1.25 KB
/
03_loops.py
File metadata and controls
49 lines (33 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Short examples demonstrating Python loops (for / while) and iteration patterns.
This module includes:
- count_up: a simple for-loop counting from 1 to a target number.
- countdown: a while-loop that decrements until reaching zero.
- iterate_habits: iterating through a list with indexes and friendly output.
"""
from typing import List
def count_up(to_number: int) -> List[int]:
"""Return a list counting upward from 1 through to_number."""
return [i for i in range(1, to_number + 1)]
def countdown(start: int) -> List[int]:
"""Return a list counting down from `start` to 1 using a while-loop."""
numbers = []
current = start
while current > 0:
numbers.append(current)
current -= 1
return numbers
def iterate_habits(habits: List[str]) -> List[str]:
"""Return a formatted list describing habits with their index positions."""
return [f"{index + 1}. {habit}" for index, habit in enumerate(habits)]
def main() -> None:
print("Counting up to 5:")
print(count_up(5))
print("\nCountdown from 5:")
print(countdown(5))
print("\nHabits list:")
habits = ["Workout", "Code", "Read", "Stretch"]
for line in iterate_habits(habits):
print(line)
if __name__ == "__main__":
main()