PYTHON
for문, while문
yuurimingg
2023. 12. 23. 00:48
In [1]:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity="all"
import warnings
warnings.filterwarnings('ignore')
for문¶
for문과 range 이용¶
In [6]:
for i in range(10):
print("Hello world!")
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
In [4]:
for i in range(10):
print("hello, world", i, sep = "")
hello, world0
hello, world1
hello, world2
hello, world3
hello, world4
hello, world5
hello, world6
hello, world7
hello, world8
hello, world9
for문과 range 응용¶
- for 변수 in range(start, end, step)
In [7]:
for i in range(0, 10, 2):
print(i, "번째", "hello world!")
0 번째 hello world!
2 번째 hello world!
4 번째 hello world!
6 번째 hello world!
8 번째 hello world!
In [8]:
for i in range(10, 0, -2):
print(i,"번째", "hello world!")
10 번째 hello world!
8 번째 hello world!
6 번째 hello world!
4 번째 hello world!
2 번째 hello world!
감소하는 방법 : reversed
- 숫자의 순서를 뒤집음
- for 변수 in reversed(range(start, end, step))
In [9]:
for i in reversed(range(0, 10, 2)):
print(i, "번째", "hello world")
8 번째 hello world
6 번째 hello world
4 번째 hello world
2 번째 hello world
0 번째 hello world
In [10]:
for i in reversed(range(10, 0, -2)):
print(i, "번째", "hello world")
2 번째 hello world
4 번째 hello world
6 번째 hello world
8 번째 hello world
10 번째 hello world
입력 받아 for문 수행
In [11]:
count = int(input("반복할 횟수를 입력하세요 : "))
for i in range(count):
print(i, "번째", "hello world!")
반복할 횟수를 입력하세요 : 4
0 번째 hello world!
1 번째 hello world!
2 번째 hello world!
3 번째 hello world!
시퀀스 객체로 반복하기¶
리스트
In [12]:
lst = [10, 20, 30, 40, 50]
for i in lst:
print(i)
10
20
30
40
50
튜플
In [13]:
tup = (10, 20, 30, 40, 50)
for i in tup:
print(i)
10
20
30
40
50
문자열
In [14]:
for i in 'python':
print(i, end = ' ')
p y t h o n
문자열 reversed
In [15]:
for i in reversed('python'):
print(i, end = ' ')
n o h t y p
연습¶
16.6 심사 문제 : 구구단 출력하기¶
표준 입력으로 정수가 입력됩니다. 입력된 정수의 구구단을 출력하는 프로그램을 만드세요(input에서 안내 문자열은 출력하지 않아야 합니다). 출력 형식은 숫자 숫자 = 숫자처럼 만들고 숫자와 , = 사이는 공백은 한 칸 띄웁니다.
In [16]:
num = int(input())
for i in range(1, 10):
print(num, "*", i, "=", num * i)
4
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
로또 만들기¶
In [17]:
import random
for i in range(5):
for i in range(6):
r = random.randint(1, 45)
print(r, end = ' ')
print()
9 39 41 42 38 20
24 11 31 9 32 8
10 35 27 1 2 21
37 20 3 8 10 13
36 44 35 18 43 23
while문¶
In [18]:
i = 0
while i < 5:
print("hello, world!")
i += 1
hello, world!
hello, world!
hello, world!
hello, world!
hello, world!
In [ ]:
i = 0
while True: # True는 계속 참이라 무한 반복
print("hello, world!")
i += 1
In [19]:
i = 0
while False: # False는 계속 거짓이라 실행X
print("hello, world!")
i += 1
증가
In [20]:
i = 1
while i <= 5:
print(i, 'hello world')
i += 1
1 hello world
2 hello world
3 hello world
4 hello world
5 hello world
감소
In [21]:
i = 5
while i > 0 :
print(i, 'hello world')
i -= 1
5 hello world
4 hello world
3 hello world
2 hello world
1 hello world
입력받아 while문 수행
In [22]:
count = int(input())
i = 0
while i <= count:
print(i, 'hello world')
i += 1
6
0 hello world
1 hello world
2 hello world
3 hello world
4 hello world
5 hello world
6 hello world
반복 횟수가 정해지지 않은 경우¶
- 난수 사용
- 난수 : 특정 주기로 반복되지 않으며 규칙 없이 무작위로 나열되는 숫자
- random을 이용 -> import 모듈
- 모듈 이름. 모듈 메소드
In [24]:
import random
print(random.random())
print(random.random())
print(random.random())
0.8337291756358066
0.7119296115422973
0.6603259689943262
random.randint(a, b) : a와 b 사이의 난수 생성(1과 6도 나올 수 있다)
In [25]:
print(random.randint(1, 6))
print(random.randint(1, 6))
print(random.randint(1, 6))
6
6
3
3이 나올 때까지 주사위를 계속 던지는 것과 같다
In [27]:
i = 0
while i != 3: # 3이 나오면 멈춰라
i = random.randint(1,6)
print(i)
5
4
6
4
4
2
1
3
random.choice : 시퀀스 객체에서 요소를 무작위로 선택
- 리스트, 튜플, range, 문자열로 가능
In [28]:
lst = [1, 2, 3, 4, 5]
print(random.choice(lst))
print(random.choice(lst))
print(random.choice(lst))
3
3
1
In [29]:
r = range(10)
print(random.choice(r))
print(random.choice(r))
print(random.choice(r))
6
9
8
In [30]:
s = 'python'
print(random.choice(s))
print(random.choice(s))
print(random.choice(s))
n
n
n