์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ)
<์ด์ ๊ธ>
https://silvercoding.tistory.com/37
[python ๊ธฐ์ด] 5. ํ์ด์ฌ ํ๋ฆ์ ์ด (์ ์ด๋ฌธ) - ์กฐ๊ฑด๋ฌธ if
์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ) <์ด์ ๊ธ> https://silvercoding.tistory.com/36 https://silvercoding.tistory.com/35 https://silvercoding.tistory.com/34 https://silvercoding.tistory.com/33..
silvercoding.tistory.com
(1) while ๋ฌธ
- ์์ 1
v1 = 1
while v1 < 11:
print("v1 is : ", v1)
v1 += 1
v1 is : 1
v1 is : 2
v1 is : 3
v1 is : 4
v1 is : 5
v1 is : 6
v1 is : 7
v1 is : 8
v1 is : 9
v1 is : 10
while๋ฌธ์ ์กฐ๊ฑด์ด ์ฐธ์ผ ๋์ ๊ณ์ ๋ฐ๋ณตํ๋ค. ์ด ์์ ์์๋ 11๋ณด๋ค ์์ ๋, ์ฆ 10๊น์ง ๋ฐ๋ณตํ๋ค.
- ์์ 2
sum1 = 0
cnt1 = 1
while cnt1 <= 100:
sum1 += cnt1
cnt1 += 1
print('1 ~ 100: ', sum1)
1 ~ 100: 5050
cnt1์ด 100 ์ดํ์ผ ๋์ ๋ฐ๋ณต๋ฌธ์ ์คํํ๊ฒ ๋๋ค. sum1์ 1์ฉ ์ฆ๊ฐํ๋ cnt1์ ๊ณ์ ๋ํด์ค์ผ๋ก์จ 1๋ถํฐ 100๊น์ง์ ํฉ์ ๊ตฌํ๋ ์ฝ๋์ด๋ค.
* ์ฐธ๊ณ - ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋ผ ์ ์๋ ํจ์ sum(), range()์ด์ฉ
print('1 ~ 100: ', sum(range(1, 101)))
print('1 ~ 100: ', sum(range(1, 101, 2)))
1 ~ 100: 5050
1 ~ 100: 2500
๋ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ผ๋ก ํฉ์ ๊ตฌํด์ค ์ ์๋ค. 1๋ถํฐ 100๊น์ง ํ์์ ํฉ๋ง์ ๊ตฌํ๋ ค๋ฉด range(1, 101, 2) ๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
(2) for ๋ฌธ
* ์ํ์ค (์์๊ฐ ์๋) ์๋ฃํ ๋ฐ๋ณต : ๋ฌธ์์ด, ๋ฆฌ์คํธ, ํํ, ์งํฉ, ์ฌ์
* iterable ๋ฆฌํด ํจ์ : range. reversed, enumerate, filter, map, zip
- ์์ 1 - range()
for v3 in range(1, 11):
print('v3 is : ', v3)
v3 is : 1
v3 is : 2
v3 is : 3
v3 is : 4
v3 is : 5
v3 is : 6
v3 is : 7
v3 is : 8
v3 is : 9
v3 is : 10
์ํ์ค ์๋ฃํ์ ๋ฐ๋ณตํ๋ค. ์์ ๊ฐ์ ๊ฒฝ์ฐ์์๋ 1๋ถํฐ 10๊น์ง ์ฐจ๋ก๋๋ก v3์ ๋ค์ด๊ฐ๋ ๊ฒ์ ๋ฐ๋ณตํ๋ฉฐ ์ถ๋ ฅํด ์ค๋ค.
- ์์ 2 - ๋ฆฌ์คํธ (ilst)
names = ['Kim', 'Park', 'Cho', 'Choi', 'Yoo']
for v in names:
print('You are: ', v)
You are: Kim
You are: Park
You are: Cho
You are: Choi
You are: Yoo
๋ฆฌ์คํธ ์๋ฃํ์ ๋ฃ์ด์ฃผ์ด๋ ๋ฐ๋ณต์ด ๊ฐ๋ฅํ๋ค!
- ์์ 3 - ๋ฌธ์์ด (str)
word = "dreams"
for s in word:
print("Word: ", s)
Word: d
Word: r
Word: e
Word: a
Word: m
Word: s
๋ฌธ์์ด๋ ๋ฐ๋ณต์ด ๊ฐ๋ฅํ๋ค.
- ์์ 4 - ๋์ ๋๋ฆฌ (dictionary)
my_info = {
'name' : 'kim',
'age' : 33,
'city' : 'Seoul'
}
์ฐ์ ๋์ ๋๋ฆฌ ์ ์ธํ๊ธฐ
(1) ๊ธฐ๋ณธ : key
for key in my_info:
print('my_info', key)
my_info name
my_info age
my_info city
๋์
๋๋ฆฌ ์์ฒด๋ฅผ ๋ฐ๋ณต๋ฌธ์ ๋ฃ์ผ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ํค๊ฐ ๋ฐ๋ณต๋๋ค.
(2) values
for key in my_info.values():
print('my_info', key)
my_info kim
my_info 33
my_info Seoul
(3) keys
for key in my_info.keys():
print('my_info', key)
my_info name
my_info age
my_info city
(4) items
for key, value in my_info.items():
print('my_info', key, value)
my_info name kim
my_info age 33
my_info city Seoul
- ์์ 5 : ๋ฐ๋ณต๋ฌธ + ์กฐ๊ฑด๋ฌธ
name = 'KennRY'
name2 = ""
KennRY ์์ ๋๋ฌธ์๋ ์๋ฌธ์๋ก, ์๋ฌธ์๋ ๋๋ฌธ์๋ก ๋ฐ๊พธ๊ณ , ์ต์ข ์ ์ผ๋ก name2์ ๋ฐ๋ ๋ฌธ์์ด์ ์ ์ฅํ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ค.
for n in name:
if n.isupper():
print(n.lower())
name2+=n.lower()
else:
print(n.upper())
name2+=n.upper()
k
E
N
N
r
y
print(name2)
kENNry
๋ฌธ์์ด์ ๋ฐ๋ณตํ์ฌ, ๊ธ์๊ฐ ๋๋ฌธ์๋ฉด ์๋ฌธ์๋ก ๋ฐ๊พธ๊ณ name2์ ๊ธ์๋ฅผ ๋ํด์ค๋ค. ๊ทธ ๋ฐ๋์ ๊ฒฝ์ฐ๋ ๋์ผํ๊ฒ ํด์ค๋ค. ๊ทธ๋ผ ์ด์ฒ๋ผ ์ํ๋ ๊ฒฐ๊ณผ๊ฐ ๋์ค๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
- ์์ 6 : break
numbers = [14, 3, 4, 7, 10, 24, 17, 2, 33, 35, 36, 38]
numbers2 = [14, 3, 4, 7, 10, 24, 17, 2, 37, 35, 36, 38]
for num in numbers:
if num == 33:
print('found : 33 !')
break
else:
print('not found : 33!')
not found : 33!
not found : 33!
not found : 33!
not found : 33!
not found : 33!
not found : 33!
not found : 33!
not found : 33!
found : 33 !
33์ ๋ฐ๊ฒฌํ๋ฉด break๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ๋ณต๋ฌธ์ ํ์ถํ๋ ์ฝ๋์ด๋ค. ์ค์ ๋ก 33 ์ฐจ๋ก๊ฐ ์์ ๋, found : 33 ! ์ด ์ถ๋ ฅ๋๊ณ , ๊ทธ ์ดํ๋ก๋ ์๋ฌด๊ฒ๋ ์ถ๋ ฅ๋์ง ์๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
- ์์ 7 : for-else ๊ตฌ๋ฌธ
: ๋ฐ๋ณต๋ฌธ์ด ์ ์์ ์ผ๋ก ์ํ(๋ชจ๋ ๊ฐ ์ํ ) ๋ ๊ฒฝ์ฐ else ๋ธ๋ญ ์ํ
for num in numbers2:
if num == 33:
print('found : 33 !')
break
else:
print('not found : 33!')
else:
print('Not found 33 ...........')
not found : 33!
not found : 33!
not found : 33!
not found : 33!
not found : 33!
not found : 33!
not found : 33!
not found : 33!
not found : 33!
not found : 33!
not found : 33!
not found : 33!
Not found 33 ...........
break๋ฌธ ๋ฑ์ผ๋ก ์ธํ์ฌ ๋ฐ๋ณต๋ฌธ์ด ์ ์์ ์ผ๋ก ๋ชจ๋ ๊ฐ์ ์ํํ์ง ๋ชปํ ๊ฒฝ์ฐ else๋ธ๋ญ์ ์ํ๋์ง ์๋๋ค. ์์ 7๊ณผ ๊ฐ์ ๊ฒฝ์ฐ์๋ 33์ด ๋ฆฌ์คํธ์ ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ์ ์ ์์ ์ผ๋ก ๋ชจ๋ ๊ฐ์ ์ํํ์ฌ else๋ธ๋ญ์ด ์ํ๋์๋ค.
- ์์ 8 : continue
lt = ['1', 2, 5, True, 4.3, complex(4)]
for v in lt:
if type(v) is float:
continue
print('ํ์
: ', type(v))
ํ์
: <class 'str'>
ํ์
: <class 'int'>
ํ์
: <class 'int'>
ํ์
: <class 'bool'>
ํ์
: <class 'complex'>
continue๋ฅผ ๋ง๋๋ฉด ๊ทธ ๋ค์ ์ฝ๋๋ฅผ ์คํํ์ง ์๊ณ ๊ณง๋ฐ๋ก for๋ฌธ์ ์ฒซ๋ฒ์งธ๋ก ๋๋์ ๊ฐ ๋ฐ๋ณต์ ๊ทธ๋๋ก ์ํํ๋ค. ์์ 8์์๋ floatํ์ ์ด๋ฉด continue๋ฅผ ์คํํ๋ค. float ํ์ ์ธ 4.3์ ์ค๊ฐ์ ๋ฃ์ด๋์๋๋ ๊ทธ ํ์ print๋ฌธ์ ์คํ๋์ง ์๊ณ complexํ์ ์ ์ ์์ ์ผ๋ก ์ถ๋ ฅ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
- ์์ 9 : reversed ํจ์
name = 'Niceman'
print(reversed(name))
print(list(reversed(name)))
print(tuple(reversed(name)))
<reversed object at 0x000001C1CC70D898>
['n', 'a', 'm', 'e', 'c', 'i', 'N']
('n', 'a', 'm', 'e', 'c', 'i', 'N')
reversed ํจ์๋ ๊ทธ๋๋ก ์ถ๋ ฅํด์ ๋ณด๊ธฐ ์ํด list๋ tuple๊ฐ์ฒด๋ก ๋ณํํด์ฃผ์ด์ผ ํ๋ค.
for i in reversed(name):
print(i)
n
a
m
e
c
i
N
ํ์ง๋ง iterable์ ๋ฐํํด์ฃผ๋ ํจ์์ด๊ธฐ ๋๋ฌธ์ for๋ฌธ์์๋ ๊ณง๋ฐ๋ก ์ฌ์ฉํ ์ ์๋ค.
'ํ๋ก๊ทธ๋๋ฐ ์ธ์ด > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python ๊ธฐ์ด] 8. ํด๋์ค , ์์ , ๋ค์ค์์ (0) | 2021.08.01 |
---|---|
[python ๊ธฐ์ด] 7. ํจ์ ๋ฐ ๋๋ค (lambda) (0) | 2021.07.31 |
[python ๊ธฐ์ด] 5. ํ์ด์ฌ ํ๋ฆ์ ์ด (์ ์ด๋ฌธ) - ์กฐ๊ฑด๋ฌธ if (0) | 2021.07.31 |
[python ๊ธฐ์ด] 4. ๋ฐ์ดํฐ ํ์ (2) (์๋ฃํ) - dictionary, set (0) | 2021.07.31 |
[python ๊ธฐ์ด] 3. ๋ฐ์ดํฐ ํ์ (1) (์๋ฃํ) - list, tuple (0) | 2021.07.30 |