728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

1) ํ•จ์ˆ˜

 

  • ์šฐ๋ฆฌ๊ฐ€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ข€ ๋” ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค
  • ์ฒ˜์Œ๋ถ€ํ„ฐ ํŒŒ์ด์ฌ์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ๊ฑฐ์†“ ์žˆ๊ณ , ์šฐ๋ฆฌ๊ฐ€ ํ•„์š”๋กœ ํ•˜๋Š” ๊ฒƒ์„ ์ง์ ‘ ๋งŒ๋“ค์–ด ์“ธ ์ˆ˜ ์žˆ๋‹ค
# len () : ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜ -> ํŒŒ์ด์ฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ
a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
len(a_list)  # 10

# ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ๊ทธ ์ˆ˜๊ฐ€ ๋ช‡ ์ž๋ฆฌ ์ˆซ์ž์ธ์ง€ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜ numOfDigits () ๋งŒ๋“ค๊ธฐ
def numOfDigits(num):
    print(len(str(num)))

 

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

 

# ๋‚ด ํ’€์ด
for i in range (2, 10):
    for j in range (1,10):
        # print (f"{i} * {j} = {i * j}"
        print(i, "*", j, "=", (i*j))
        
 # ๋ชจ๋ฒ” ๋‹ต์•ˆ
 def multi(m):
        for n in range(1, 10):
            print(f'{m} * {n} = {m*n:2d}')


    if __name__ == '__main__':
        for i in range(2, 10):
            multi(i)
            print()

 

++) map ํ•จ์ˆ˜ 

  • ํ•จ์ˆ˜์™€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ธ์ž๋กœ ๋ฐ›๊ณ 
  • ๋ฆฌ์ŠคํŠธ๋กœ๋ถ€ํ„ฐ ์›์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์„œ ํ•จ์ˆ˜๋ฅผ ์ ์šฉ์‹œํ‚จ ๋‹ค์Œ
  • ๊ฒฐ๊ณผ๋ฅผ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ์— ๋‹ด์•„์ค๋‹ˆ๋‹ค
map(ํ•จ์ˆ˜, ๋ฆฌ์ŠคํŠธ)

list(map(lambda x : x ** 2, range(5)))
[0, 1, 4, 9, 16]

 

 

++) reduce ํ•จ์ˆ˜

  • ์‹œํ€€์Šค(๋ฌธ์ž์—ด, ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ) ์˜ ์›์†Œ๋“ค์„ ๋ˆ„์ ํ•ด์„œ ํ•จ์ˆ˜์— ์ ์šฉ์‹œํ‚ต๋‹ˆ๋‹ค
reduce(ํ•จ์ˆ˜, ์‹œํ€€์Šค)

from functools import reduce
reduce(lambda x, y: x + y, [0, 1, 2, 3, 4])   #10

 

++) filter ํ•จ์ˆ˜

  • ์›์†Œ๋“ค์„ ํ•จ์ˆ˜์— ์ ์šฉ์‹œ์ผœ์„œ ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ธ ๊ฐ’๋“ค๋กœ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค
filter(ํ•จ์ˆ˜, ๋ฆฌ์ŠคํŠธ)

list(filter(lambda x: x < 5, range(10)))
[0, 1, 2, 3, 4]

 

 

2) ๋ฐ˜ํ™˜ (return) ๋ฌธ

 

  • ํ•จ์ˆ˜์˜ ๊ฐ’์„ ๋Œ๋ ค์ฃผ๊ธฐ ์œ„ํ•ด return์ด ์‚ฌ์šฉ๋œ๋‹ค
# ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ์ •์ˆ˜๋ฅผ ํ•œ๊ตญ์–ด๋กœ ํ‘œ๊ธฐํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ korean_number() ๋ฅผ ์ •์˜
# ๋‹จ, ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” 1์ด์ƒ 10์ดํ•˜์˜ ์ •์ˆ˜

def korean_number(num):
    if num == 1:
        return '์ผ'
    elif num == 2:
        return '์ด'
    elif num == 3:
        return '์‚ผ'
    elif num == 4:
        return '์‚ฌ'
    elif num == 5:
        return '์˜ค'
    elif num == 6:
        return '์œก'
    elif num == 7:
        return '์น '
    elif num == 8:
        return 'ํŒ”'
    elif num == 9:
        return '๊ตฌ'
    elif num == 10:
        return '์‹ญ'

 

 

3) ์ง€์—ญ๋ณ€์ˆ˜, ์ „์—ญ๋ณ€์ˆ˜

 

  • ์ง€์—ญ๋ณ€์ˆ˜ : ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋งŒ๋“ค์–ด์ง„ ๋ณ€์ˆ˜, ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ๋งŒ๋“ค์–ด์ ธ์„œ, ํ•จ์ˆ˜์˜ ์‹คํ–‰์ด ๋๋‚  ๋–„ ํ•จ๊ป˜ ์—†์–ด์ง„๋‹ค
  • ์ „์—ญ๋ณ€์ˆ˜ : ํ•จ์ˆ˜ ๋ฐ–์—์„œ ๋งŒ๋“ค์–ด์ง„ ๋ณ€์ˆ˜ , ํ•จ์ˆ˜์™€ ๊ด€๊ณ„์—†์ด ํ•ญ์ƒ ์œ ์ง€๋œ๋‹ค

=> ๊ทธ๋Ÿผ ํ•ญ์ƒ ์œ ์ง€๋˜๋Š” ์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ์“ฐ์ง€ ๋ญํ•˜๋Ÿฌ ์ง€์—ญ ๋ณ€์ˆ˜๋ฅผ ์“ฐ๋ƒ??

๋”๋ณด๊ธฐ

    ์ „์—ญ๋ณ€์ˆ˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ๋ณต์žกํ•ด์งˆ์ˆ˜๋ก ๋‹ค๋ฅธ ํ•จ์ˆ˜๋“ค ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๋ฐ”๋€Œ์–ด๋ฒ„๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ์žˆ์–ด์„œ  ํ•„์š”์— ๋”ฐ๋ผ ์ง€์—ญ๋ณ€์ˆ˜์™€ ์ „์—ญ๋ณ€์ˆ˜๋ฅผ ๊ณจ๋ผ ์“ฐ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. 

  • ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ „์—ญ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค : ์ „์—ญ๋ณ€์ˆ˜(global)๋กœ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๊ณ  ๋ช…์‹œ
def e_is_10():
    global e   # ์ „์—ญ๋ณ€์ˆ˜
    e = 10
    print('e ๊ฐ’์€ ', e, '์ž…๋‹ˆ๋‹ค')

 

4) ๋žŒ๋‹ค

 

  • ๋žŒ๋‹ค ํ˜•์‹์€ ์ธ๊ณต์ง€๋Šฅ ๋ถ„์•ผ๋‚˜ AutoCAD ๋ผ๋Š” ์„ค๊ณ„ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์“ฐ์ด๋Š” Lisp ์–ธ์–ด์—์„œ ๋ฌผ๋ ค๋ฐ›์•˜๋‹ค
  • ํ•จ์ˆ˜๋ฅผ ๋”ฑ ํ•œ์ค„๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค
lambda ๋งค๊ฒจ๋ณ€์ˆ˜ : ํ‘œํ˜„์‹

(lambda x, y : x+y) (10, 20)   # 30

 

++) str.strip์œผ๋กœ ๋ฌธ์ž์—ด ์•ž๋’ค์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค

>>> str.strip(" I am a boy. ")
'I am a boy.'

 

728x90
๋ฐ˜์‘ํ˜•
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