튜플에 있는 웹사이트가 동작중인지 내려갔는지 확인하기
websites = (
"google.com",
"airbnb.com",
"twitter.com",
"facebook.com"
)
리스트가 얼마나 긴지 신경쓰지 않고 list의 item을 활용해서 자동으로 코드를 실행하는 방법
: for문
for each in websites:
print("hello")
>
hello
hello
hello
hello
------------------------------------------------------------------
websites = (
"google.com",
"airbnb.com",
"twitter.com",
"facebook.com",
"tittok.com"
)
for each in websites:
print("hello")
>
hello
hello
hello
hello
hello
여기에서 알 수 있듯 tuple(websites)의 각각의 item에 코드를 hello로 실행시켰다.
이건 list에도 작동한다.
websites = [
"google.com",
"airbnb.com",
"twitter.com",
"facebook.com",
"tittok.com"
]
>
hello
hello
hello
hello
hello
--------------------------------------------
현재 처리중인 item이 무엇인지 알 수 있는가?
예를 들면 airbnb나 facebook 등 어디 사이트를 타고 있을까?
그럴 때 사용하는게 each이다.
for는 각각의 item이 실행 될 때 placeholder를 만드는 것을 허락해준다.
placeholder의 이름은 마음대로 정할 수 있다.
for potato in websites:
print("potato is equals to", potato)
>
potato is equals to google.com
potato is equals to airbnb.com
potato is equals to twitter.com
potato is equals to facebook.com
potato is equals to tittok.com
-----------------------------------------------
* tip :
흔한 관습으로, tuple이나 list를 만들 때 복수형을 사용한다.
ex) potatos, websites, users ...
그러나 for in에서는 website, movie, user, potato 단수를 사용 한다.
ex) for website in websites:
>>이제, for loop에서 현재 실행중인 item에 접근할 수 있다!
websites = [
"google.com",
"airbnb.com",
"twitter.com",
"facebook.com",
"tittok.com"
]
for website in websites:
print(website)
>
google.com
airbnb.com
twitter.com
facebook.com
tittok.com
잘한 점: 이렇게 한개 씩 하다보니까 강의도 더 집중되고, 그냥 사용하던 부분(상식)에 대해서도 알게 되어서 재밌다.
아쉬운 점: 시간이 촉박하게 쫓겨 하는 것 같다. 좀 더 효율적으로 해야겠다..
'개발 > python' 카테고리의 다른 글
#4.7 Requests (0) | 2022.09.21 |
---|---|
#4.6 URL Formmatting (0) | 2022.09.20 |
#4.4 recap (0) | 2022.09.18 |
#4.3 Dics (0) | 2022.09.17 |
#4.2 Tuples (0) | 2022.09.16 |