if
for
while
continue,break
한줄 for문
weather = input("how about weather? today?") #micro dust, sunny, rain
if weather == "rain" or weather == "snow":
print("get your umbrella")
elif weather == "micro dust":
print("get yout mask")
else:
print("negative needs")
출력결과
how about weather? today?#입력값rain
get your umbrella
how about weather? today?#입력값snow
get your umbrell
for 변수 in 리스트(또는 튜플, 문자열):
수행할 문장1
for wating_no in range(1, 6): #1,2,3,4,5
print("wating no : {0}" .format(wating_no))
starbucks = ["ironman","thor","grut"]
for customer in starbucks:
print("{0}, ready to coffe." .format(customer))
출력결과
wating no : 1
wating no : 2
wating no : 3
wating no : 4
wating no : 5
ironman, ready to coffe.
thor, ready to coffe.
grut, ready to coffe
for index, 변수 in enumerate(리스트(또는 튜플, 문자열)):
수행할 문장1
while <조건문>:
<수행할 문장1>
<수행할 문장2>
absent = [2,5]
no_book = [7]
for student in range(1,11):
if student in absent:
continue
elif student in no_book:
print("class is over, {0} follow the my room" .format(student))
break
print("{0}, read the book" .format(student))
출력결과
1, read the book
3, read the book
4, read the book
6, read the book
class is over, 7 follow the my room
말 그대로 한줄로 표현한 for문이다. if문이 추가 될 수도있고, 중복 for문도 가능하다.
students = ["ironman","thor","groot"]
students = [i.upper() for i in students]
for i in range(2):
if i==3:
print(i)
else :
print("No")
[i if i==3 else "No" for i in range(2)]
출력결과
['IRONMAN', 'THOR', 'GROOT']
No
No
No
3
['No', 'No', 'No', 3]