-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathfor_loop_basic_demo.py
More file actions
executable file
·99 lines (71 loc) · 2.12 KB
/
for_loop_basic_demo.py
File metadata and controls
executable file
·99 lines (71 loc) · 2.12 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
"""
This file is part of eRCaGuy_hello_world:
https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world
GS
7 March 2022
Demonstrate a basic range-based for loop in Python, as well as the 3 main ways to do formatted
printing, in order of newest first to oldest last:
1. [newest] formatted string literals (f-strings)
2. `str.format()` method
3. [oldest] C-like "printf"-style `%` operator
Status: done!
keywords: for loop; print strings; formatted strings; format strings
Check this script with `pylint` v2.0.0 or later. See "eRCaGuy_hello_world/python/README.md" for
installation instructions to install the latest version from GitHub.
For a list of all error codes, such as `C0301`, `C0116`, `W0105`, etc., see here:
https://pylint.pycqa.org/en/latest/messages/messages_list.html
```bash
pylint for_loop_basic_demo.py
```
Run command:
```bash
./for_loop_basic_demo.py
# OR
python3 for_loop_basic_demo.py
```
References:
1. [MY ANSWER with this code] How to type "line1", "line2", "line3".... using a for loop in python:
https://stackoverflow.com/a/71376781/4561887
1. f-strings / "format strings": https://realpython.com/python-f-strings/
"""
END_NUM = 7
for i in range(1, END_NUM + 1):
# 3 techniques to print:
# 1. newest technique: formatted string literals; AKA: "f-strings" or "format strings"; see:
# https://realpython.com/python-f-strings/
print(f"line{i}")
# 2. newer technique: `str.format()` method
# pylint: disable-next=consider-using-f-string
print("line{}".format(i))
# 3. oldest, C-like "printf"-style `%` operator print method
# (sometimes is still the best method, however, as it provides lots of versatility and control!)
# pylint: disable-next=consider-using-f-string
print("line%i" % i)
print() # newline
# pylint: disable-next=pointless-string-statement
"""
SAMPLE OUTPUT:
eRCaGuy_hello_world/python$ ./for_loop_basic_demo.py
line1
line1
line1
line2
line2
line2
line3
line3
line3
line4
line4
line4
line5
line5
line5
line6
line6
line6
line7
line7
line7
"""