728x90
반응형
13~15. if 조건문
- 조건식에 반드시 ==
- 조건식 다음에 반드시 : 사용
- 들여쓰기 4칸 필수
>>> x = 10
>>> if x == 10:
>>> pass # if문의 처리 로직을 나중에 작성할때 pass를 사용하고 관련 메모는 주석으로 남긴다
>>> if x == 10:
>>> print('x is', x)
>>> print('위의 if 와는 상관없는 코드')
x is 10
위의 if와는 상관없는 코드
# if else
>>> if x == 10:
>>> print('x is', x)
>>> else
>>> print('else')
# input()활용
>>> y = int(input('입력: '))
>>> if y == 10:
>>> print('it\'s 10')
>>> elif y == 20:
>>> print('it\'s 20')
입력: 10(입력)
it's 10
16. for 반복문
>>> for i in range(10, 0, -1):
>>> print('hello, ', i)
hello, 10
hello, 9
hello, 8
hello, 7
hello, 6
hello, 5
hello, 4
hello, 3
hello, 2
hello, 1
>>> fruits = ('apple', 'orange', 'grape')
>>> for fruit in fruits:
>>> print(fruit)
apple
orange
grape
>>> for fruit in reversed('PYTHON'):
>>> print(fruit)
N
O
H
T
Y
P
17. while 반복문
>>> i = 1
>>> while i <= 10:
>>> print('number', i)
>>> i += 1
number 1
number 2
number 3
number 4
number 5
number 6
number 7
number 8
number 9
number 10
# random
>>> print(random.random())
0.9103325171771764
# randint(1, 6): 1, 6 사이의 난수 생성
>>> i = 0
>>> while i != 3:
>>> i = random.randint(1, 6)
>>> print('난수 : ', i)
난수 : 6
난수 : 1
난수 : 2
난수 : 1
난수 : 4
난수 : 3
# choice(list) : list, tuple, range, 문자열 값 사이에서 랜덤으로 값 선택
>>> dice = [1, 2, 3, 4, 5, 6]
>>> print('dice 사이의 랜던 값 : ', random.choice(dice))
dice 사이의 랜던 값 : 2
18. break, continue
>>> i = 0
>>> while True:
>>> print(i)
>>> i += 1
>>> if i == 20:
>>> break
0
1
2
3
4
...
17
18
19
>>> for i in range(20):
>>> if i % 2 == 0:
>>> continue
>>> print(i)
1
3
5
7
9
11
13
15
17
19
>>> for i in range(20):
>>> if i % 2 == 0:
>>> print(i)
>>> else:
>>> continue
0
2
4
6
8
10
12
14
16
18
728x90
반응형
'Language > Python' 카테고리의 다른 글
[코딩도장]20.FizzBuzz문제 (0) | 2022.03.14 |
---|---|
[코딩도장]19.계단식 별 출력 (0) | 2022.03.14 |
[코딩도장]12.딕셔너리 (0) | 2022.03.14 |
[코딩도장]11.시퀀스 자료형 활용 (0) | 2022.03.14 |
[코딩도장]10.리스트와 튜플 사용 (0) | 2022.03.14 |