** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
list_for
num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in num_list:
print(i)
result
1
2
3
4
5
6
7
8
9
It's annoying to write a list.
range
for i in range(10):
print(i)
result
0
1
2
3
4
5
6
7
8
9
At this rate, it starts from 0, so I'll start from 1.
range
for i in range(1, 10):
print(i)
result
1
2
3
4
5
6
7
8
9
The first argument specifies the beginning and the second argument specifies the end.
range
for i in range(1, 10, 2):
print(i)
result
1
3
5
7
9
With the third argument, you can specify "how many skips". If you enter 2, the values are processed by 2 (skipping 1).
_
range
for i in range(10):
print(i, 'hello')
result
0 hello
1 hello
2 hello
3 hello
4 hello
5 hello
6 hello
7 hello
8 hello
9 hello
At this time, ʻiis also printed, If you just print
hello 10 times without using ʻi
,
range
for i in range(10):
print('hello')
If you write, when other people read it
"What is this ʻi?" Is likely to arise. In that case,
_` is often used.
range
for _ in range(10):
print('hello')
result
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
If you use _
,
"I don't really use indexes in this loop."
Is transmitted.
Recommended Posts