Python 기초
- 주석 달기
- '#' 뒤에 작성한 내용은 인식되지 않음, 메모로 활용
- """내용"""
while
from random import randint
playing = True
while playing:
user_choice = int(input("Choose number."))
pc_choice = randint(1, 50)
if user_choice == pc_choice:
print("You won!")
elif user_choice > pc_choice:
print("Lower!")
elif user_choice < pc_choice:
print("Higher!")
method
- method - 데이터에 결합해서 사용하는 함수 - .를 붙여 사용
- list, [] 사용, 복수형 사용
days_of_week = ["Mon", "Tue", "Wed", "Thur", "Fri"]
print(days_of_week.count("Wed"))
days_of_week.reverse()
print(days_of_week)
- 위치를 0 부터 인식하기 때문에 4번째 값을 호출하기 위해서는 3을 입력
print(days_of_week[3])
# Tue
tuples () 사용
dictionary, {} 사용
- key - value 의 조합으로 이루어짐
- 내부에 리스트를 넣어 사용 가능
- 수정 가능
player = {
'name' : 'user',
'age' : 20,
'alive' : True,
'fav_food' : ["pizza", "hamburger"]
}
player['fav_food'].append("noodle")
print(player.get('fav_food'))
print(player['fav_food'])
['pizza', 'hamburger', 'noodle']
for loop 순차 실행
from requests import get
websites = [
"google.com",
"airbnb.com",
"x.com",
"facebook.com",
"tiktok.com"
]
results ={}
for website in websites:
if not website.startswith("https://"):
website = f"https://{website}"
response = get(website)
if response.status_code >= 200 and response.status_code < 300:
results[website] = "OK"
elif response.status_code >= 300 and response.status_code < 400:
results[website] = "REDIRECT"
elif response.status_code >= 400 and response.status_code < 500:
results[website] = "ERROR"
else:
results[website] = "FAILED"
print(results)