** * 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. ** **
t = (1, 2, 3, 4, 5)
r = []
for i in t:
r.append(i)
print(r)
result
[1, 2, 3, 4, 5]
I'm making a tuple and taking the elements of that tuple one by one and adding them to an empty list.
t = (1, 2, 3, 4, 5)
r = [i for i in t]
print(r)
result
[1, 2, 3, 4, 5]
You can add it directly to the list by writing the above in []
.
t = (1, 2, 3, 4, 5)
r = []
for i in t:
if i % 2 == 0:
r.append(i)
print(r)
result
[2, 4]
t = (1, 2, 3, 4, 5)
r = [i for i in t if i % 2 == 0]
print(r)
result
[2, 4]
The if statement can also be added by adding it to the end. You can add more and more like this, but if you add too much, it will be difficult to read, so use it appropriately.
Recommended Posts