728x90
๋ฐ˜์‘ํ˜•

1) while ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ

 

# 1๋ถ€ํ„ฐ 100๊นŒ์ง€ ์ถœ๋ ฅํ•˜๊ธฐ
num = 1
while num <= 100:
    print(num)
    num = num + 1

# ;(์„ธ๋ฏธ์ฝœ๋ก )์„ ์•ˆ์“ด๋‹ค๋Š” ์ ๊ณผ ์กฐ๊ฑด๋ฌธ๋’ค์— :(์ฝœ๋ก )์„ ๋ถ™์ธ๋‹ค๋Š”๊ฒŒ ์ •๋ง ์ƒˆ๋กญ๋„ค์—ฌ...
  • ๋ณ€์ˆซ๊ฐ’ ์ฆ๊ฐ€ 
num = num + 1  (num +=1)
  • num์ด 100์ด ๋  ๋•Œ๊นŒ์ง€ print(num)๊ณผ num = num +1์„ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ !!
  • while๋ฌธ ๋งˆ์ง€๋ง‰์—๋Š” ์ฝœ๋ก (:)์ด ๊ผญ ๋“ค์–ด๊ฐ€์•ผ ํ•œ๋‹ค!!

 

++) ์—ฐ์Šต๋ฌธ์ œ

# ์ •์ˆ˜๋ฅผ ํ•œ ๊ฐœ ์ž…๋ ฅ๋ฐ›์•„, 1๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜๊นŒ์ง€ ๊ฐ๊ฐ์— ๋Œ€ํ•ด ์ œ๊ณฑ์„ ๊ตฌํ•ด ํ”„๋ฆฐํŠธ
num = int(input())

i = 1
while (i < num):
    print (i, i ** 2)
    i += 1

 

++) round ( )  : ๋ฐ˜์˜ฌ๋ฆผ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ ๐Ÿ‘

  • round(1.23456, 2) # 1.23456์„ ์†Œ์ˆ˜์  ๋‘˜์งธ ์ž๋ฆฌ๋กœ(์…‹์งธ ์ž๋ฆฌ์—์„œ) ๋ฐ˜์˜ฌ๋ฆผ => 1.2
  • round(1.23456, 3) # 1.23456์„ ์†Œ์ˆ˜์  ์…‹์งธ ์ž๋ฆฌ๋กœ(๋„ท์งธ ์ž๋ฆฌ์—์„œ) ๋ฐ˜์˜ฌ๋ฆผ => 1.235
  • ์ฐธ๊ณ ๋กœ, round(2.675, 2)๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๊ฒฐ๊ณผ๊ฐ€ 2.68์ด ์•„๋‹Œ 2.67๋กœ ๋‚˜์˜ค๋Š”๋ฐ, ์ด๊ฒƒ์€ ๋ฒ„๊ทธ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ถ€๋™ ์†Œ์ˆ˜์ (floating point) ์—ฐ์‚ฐ์˜ ํ•œ๊ณ„์ด๋‹ค

 


 

2) ์กฐ๊ฑด๋ฌธ (if - elif - else)

 

  • ํŒŒ์ด์ฌ์˜ if ์™€ else
a = 1234 * 4
b = 13456 / 2
# ๋งŒ์•ฝ a๊ฐ€ b๋ณด๋‹ค ํฌ๋ฉด
if a > b:
    # 'a'๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค
    print('a')
# ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด
else: 
    # 'b'๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค
    print('b')
    
# ๊ด„ํ˜ธ๋ฅผ ์•ˆ์“ฐ๋Š”๊ฑฐ๋ž‘ ์„ธ๋ฏธ์ฝœ๋ก  ์•ˆ ๋ถ™์ด๋Š”๊ฑฐ ์•„์ง์€ ์ต์ˆ™ํ•˜์ง€ ์•Š๋„ค์š”...

 

  • elif

 

c = 15 * 5
d = 15 + 15 + 15 + 15 + 15
if c > d:
    print('c is greater than d')
elif  c == d:
    print('c is equal to d')
else:
    print('c is less than d')

elif ๋ผ๋Š” ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•ด์„œ ๊ทธ ์ค‘์—์„œ ๋ง˜์— ๋“œ๋Š” ๊ฒƒ์„ ๊ณ ๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž๋ฐ”์˜ else if ๋ž‘ ๋น„์Šทํ•˜๋„ค์š” ๐Ÿค”

 

  • == ์—ฐ์‚ฐ์ž 
# c์™€ d์˜ ๊ฐ’์ด ๊ฐ™๋‹ค
c == d
# d์˜ ๊ฐ’์„ c์— ๋„ฃ๋Š”๋‹ค
c = d
  • ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฐ˜๋ณต๋ฌธ ์ค‘๋‹จํ•˜๊ธฐ
# 10๋ณด๋‹ค ํฐ ์ˆซ์ž๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ๋ฉˆ์ถ”๋Š” ๋ฐ˜๋ณต๋ฌธ
max = 10
while True:
    num = int(intput())
    if num > max:
        print(num, 'is too big!')
        break

์กฐ๊ฑด์— ํ•ด๋‹นํ•˜์ง€ ์•Š๋Š” ๊ฐ’์ด ์˜ค๋ฉด break ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜๋ณต๋ฌธ์„ ์ค‘๋‹จ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค !!

 

 

++) ์—ฐ์Šต๋ฌธ์ œ

# ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์ •์ˆ˜๋ฅผ ๊ณ„์† ๋”ํ•ด๋‚˜๊ฐ€๋‹ค๊ฐ€, ์Œ์ˆ˜๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ์ค‘๋‹จํ•˜๊ณ  ๊ทธ ์ „๊นŒ์ง€ ๊ณ„์‚ฐํ•œ ๊ฐ’์„ ์ถœ๋ ฅ
sum = 0
while True:
    num = int(input())
    if num < 0:
        break
    else:
       sum += num

print(sum)

 

  •  and / or ์—ฐ์‚ฐ์ž
๋”๋ณด๊ธฐ

and ์—ฐ์‚ฐ์ž : '๊ทธ๋ฆฌ๊ณ ' ์— ํ•ด๋‹นํ•˜๋Š” ์˜๋ฏธ, ์ค‘์ฒฉ๋˜์—ˆ๋˜ if ๋ฌธ์„ ํ•œ๋ฒˆ์œผ๋กœ ์ค„์ธ๋‹ค (์ž๋ฐ”์—์„  &&)

or ์—ฐ์‚ฐ์ž : '๋˜๋Š”' ์— ํ•ด๋‹นํ•˜๋Š” ์˜๋ฏธ (์ž๋ฐ”์—์„  ||)

 

++) ๋ฌธ์ž์—ด์˜ lower () ๋ฉ”์„œ๋“œ : ์˜์–ด ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๊ฐ’์„ ๋Œ๋ ค์ค๋‹ˆ๋‹ค.

๋”๋ณด๊ธฐ

'Yes'. lower( ) => 'yes'

 

 

3) for ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ

 

  • for ๋ฌธ 
# ๊ฐ€์กฑ๋“ค์˜ ์ด๋ฆ„๊ณผ ๋ฌธ์ž์—ด ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ
family = ['father', 'mother', 'brother', 'sister']
# family์˜ ๊ฐ ํ•ญ๋ชฉ x์— ๋Œ€ํ•˜์—ฌ
for x in family:
# x์™€ x์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•˜๋ผ
    print(x, len(x))

 

  • range ( )
# for ๋ฌธ์—์„œ range () ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค 
# range (x, y) = [x, x+1, x+2 ... y-1]
for i in range(4, 8):
    print(i)

 

++) ์—ฐ์Šต๋ฌธ์ œ

# ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, ๊ทธ ์ˆซ์ž ํฌ๊ธฐ๋งŒํผ ๋ฐ˜๋ณตํ•ด์„œ ์ถœ๋ ฅํ•˜์‹œ์˜ค
num = int(input())
# i = 1

for i in range (num):
    print('', num)
    # print(num)

์ ์–ด๋„ ํ‹€๋ฆฌ์ง€๋Š” ์•Š๋˜๋ฐ

์™œ i ์ดˆ๊ธฐ๊ฐ’์„ ์ ์–ด์ฃผ์ง€ ์•Š๋Š” ๊ฑฐ๋ž‘ , print ๋’ค์— ' ' ์„ ๋ถ™์—ฌ์ค˜์•ผ ํ•˜๋Š”์ง€๋ฅผ ๋ชจ๋ฅด๊ฒ ๋„ค์š”.ใ…œ

 

++) spilt ( )  ๋ฉ”์„œ๋“œ

# ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
'0, 100'.split()
# spilt()์œผ๋กœ ์–ป์€ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”๋กœ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
freezing_point, boiling_point = '0 100.spilt()'
freezing_point # 0
boiling_point # 100

 


 

4) match - case ๋ฌธ (java์˜ switch-case ๋ฌธ๊ณผ ์œ ์‚ฌ)

 

  • ํ™€์ˆ˜, ์ง์ˆ˜ ํŒ๋ณ„
for i in range(1,11):
    match i % 2:
        case 0:
            print(f"{i} is even.")
        case 1:
            print(f"{i} is odd.")

 

  • case(0. _)  => _ ๋Š” ์•„๋ฌด ๊ฐ’์ด๋‚˜ ์ƒ๊ด€ ์—†๋‹ค๋Š” ๋œป

 

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts