Many Python teaching materials for statement chapters
n = 10
for i in range(n):
print(i)
When you write, it starts from the place where "0 to 9 can be output!".
Next, when faced with the question "How can I output"Hello, World!"
Character by character?"
hoge = "Hello, World!"
for i in range(len(hoge)):
print(hoge[i])
I feel that many people write.
If you previously knew that you can retrieve the ʻi th by specifying the string as
hoge [i] `", the two knowledges "0-9 can be output!" The code is complete.
However, I
hoge = "Hello, World!"
for i in hoge:
print(i)
I want to strongly recommend how to write!
To be honest, it's just "** because it looks neat **" (in other words, range (len (hoge))
looks messy). However, since each person feels differently, I tried to forcibly twist the reason that is a little more convincing.
-** Fast ** -** Variable names have meaning ** -** Variables can be separated ** -** Code consistency occurs **
If you measure the speed with jupyter with hoge =" Hello, World! "
,
# In[1]:
%%timeit
for i in range(len(hoge)):
hoge[i] #No output
# Out[1]:
# 976 ns ± 34.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
# In[2]:
%%timeit
for i in hoge:
i
# Out[2]:
# 294 ns ± 3.83 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
It is about 3 times faster. However, since the unit is ns, I don't really feel it.
For for i in range (len (hoge)):
, ʻi is just an index no matter what the
hogeis. However, if you write
for i in hoge:, ʻi
will contain each element of hoge
.
For example, there is a two-dimensional array of hoge = schools
, and the code that outputs all the elements is written in the following two ways.
#Sample 1
for i in range(len(schools)):
for j in range(len(schools[i])):
print(schools[i][j])
#Sample 2
for students in schools:
for student in students:
print(student)
In sample 1, even if you say schools [i] [j]
, you don't know what will come out, and you have to go back to the definition of schools
. In sample 2, schools
contains a student list called students
, and it can be predicted that one student student
from each school will be output.
hoge = [("H "," h "), ("E "," e "), ("L "," l "), ("L "," l "), ("O "," If you want to use uppercase and lowercase letters separately in the for statement as o ")]
for upper, lower in hoge:
print(upper)
print(lower)
Can be divided as follows. For the same reason as in the chapter above, it is more readable than hoge [i] [0], hoge [i] [1]
.
If hoge
is an iterator, you can't use len because you get an error likeTypeError: object of type'generator' has no len ()
. So you have to write for i in hoge:
. Here, even for lists and strs that are not iterators, writing for i in hoge:
will make the code consistent.
In the first place, regardless of whether the Python for statement is an iterator or not, the iterator object placed after in with ʻiter (hoge)is internally converted to iterator, and
next (hoge)` is displayed. The operation of repeating is exactly the same. Therefore, it feels more natural to implement lists and strs as if they were iterators.
Recommended Posts