Coding

반복문(while), 한줄 for문(for문을 한줄로)

Kim Da Beeen 2022. 6. 8. 10:14
반응형

●반복문

다섯번불렀는데 안오면 커피 폐기처분하기
customer = "아이언맨"
index = 5
while index >=1 :
print("{0}, 커피가 준비되었습니다.{1} 번남났어요".format(customer,index)
index -= 1
if index == 0
print("커피는 폐기처분되었습니다")

continue break
반복문장에서
다음문장 신경안쓰고 계속이어감
지금상황에서 바로 반복문 종료하고 끝냄

 

●한줄for문

##출석번호앞에 100을 붙이고싶을때
students = [1,2,3,4,5]
students = [i+100 for i in students]
print(students) # 101,102,103,104,105

##학생이름을 길이로 변환
students = ["김","김답","김다빈"]
students = [len(i) for i in students]
print(students) # 1,2,3

##학생이름 대문자로 변환하기
students = ["dabin", "tabin"]
students = [i.upper() for i in students]
print(students) # DABIN TABIN

 

 

 

 

 

 

 

 

 

 

 

반응형