Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Archives
Today
Total
관리 메뉴

RUBY

반복문 본문

프로그래밍 언어/Python

반복문

ruby-jieun 2023. 1. 7. 17:23

 

 

 

반복문

 

 


 

 

반복문이란?

  • 반복문이란 특정 실행을 반복하는 것
  • 반복문을 사용하면 프로그래밍이 간결하고 유지 보수가 쉽다.
  • ex) 대량 메일 또는 문자 발송, 인사말 반복, mp3 반복 재생, 구구단 출력, 팩토리얼(4! = 24), 매일 아침 기상 알람, 영어 단어 반복 학습 도구, 게임 반복 실행, 타이머

반복문 사용 이유

print('{} * {} = {}' .format(2, 1, (2 * 1)))
print('{} * {} = {}' .format(2, 2, (2 * 1)))
print('{} * {} = {}' .format(2, 3, (2 * 1)))
print('{} * {} = {}' .format(2, 4, (2 * 1)))
print('{} * {} = {}' .format(2, 5, (2 * 1)))
print('{} * {} = {}' .format(2, 6, (2 * 1)))
print('{} * {} = {}' .format(2, 7, (2 * 1)))
print('{} * {} = {}' .format(2, 8, (2 * 1)))
print('{} * {} = {}' .format(2, 8, (2 * 1)))

 → 비효율적이다.

for i in range(1, 10) :
    print('{} * {} = {}' .format(2, i, (2 * i)))

 → 효율적이다.

 

print('{}선수 한테 메일 발송' .format('박찬호'))
print('{}선수 한테 메일 발송' .format('박세리'))
print('{}선수 한테 메일 발송' .format('박지성'))
print('{}선수 한테 메일 발송' .format('김연경'))
print('{}선수 한테 메일 발송' .format('이승엽'))

 → 비효율적이다.

players = ['박찬호', '박세리', '박지성', '김연경', '이승엽']

for player in players:
    print('{}선수 한테 메일 발송' .format(player))

 → 효율적이다.

 

반복문 종류

  • 횟수에 의한 반복

for i in range(100):
    print('i → {}' .format(i))

ex) 회원 100명한테 메일 발송

 

  • 조건에 의한 반복

num = 0
while (num < 10):
    print('num → {}' .format(num))
    num += 1

ex) 실내온도 26도까지 에어컨 작동

'프로그래밍 언어 > Python' 카테고리의 다른 글

반복 범위 설정(range()함수)  (0) 2023.01.07
횟수에 의한 반복(for문)  (0) 2023.01.07
중첩 조건문  (0) 2023.01.05
다자택일 조건문 사용 시 주의할 점  (0) 2023.01.05
다자택일 조건문  (0) 2023.01.05
Comments