MapleStory Finger Point

πŸ’Ž Python/πŸ“šνŒŒμ΄μ¬ for Beginner (2판)

(κ°œλ…) ν•¨μˆ˜μ™€ λͺ¨λ“ˆ - 파이썬 for Beginner (2판)

HYEJU01 2024. 3. 30. 03:24

· ν•¨μˆ˜ --- ν•¨μˆ˜λͺ…()

λ§€κ°œλ³€μˆ˜ (Parameter) μž…λ ₯ λ°›κ³ , λ§€κ°œλ³€μˆ˜λ₯Ό 가곡-μ²˜λ¦¬ν•΄μ„œ λ°˜ν™˜κ°’μ„ λŒλ €μ€€λ‹€.

ν•¨μˆ˜ 호좜 -> ν•¨μˆ˜ μ‹€ν–‰ -> κ²°κ³Ό λ°˜ν™˜ -> λ°˜ν™˜κ°’ λŒ€μž…

def plus(v1,v2):
	result = 0 
    result = v1 + v2
    return result
   
hap = 0

hap = plus(100,200)
print("100κ³Ό 200의 plus() ν•¨μˆ˜ κ²°κ³ΌλŠ” %d" % hap)

 

 

· 지역 Local λ³€μˆ˜ , μ „μ—­ Global λ³€μˆ˜

μ§€μ—­λ³€μˆ˜λŠ” ν•¨μˆ˜ λ‚΄μ—μ„œλ§Œ μ ‘κ·Ό κ°€λŠ₯

μ „μ—­ λ³€μˆ˜λŠ” λ‹€μ–‘ν•œ ν•¨μˆ˜μ—μ„œ μ ‘κ·Ό κ°€λŠ₯ (μ„ μ–Έ μœ„μΉ˜λŠ” μœ„, μ•„λž˜ μƒκ΄€μ—†μŒ)

 

·   μš°μ„ μˆœμœ„ : μ§€μ—­λ³€μˆ˜ > μ „μ—­λ³€μˆ˜

 

·  global μ˜ˆμ•½μ–΄

ν•¨μˆ˜ μ•ˆμ—μ„œ μ‚¬μš©λ˜λŠ” λ³€μˆ˜λ₯Ό μ „μ—­λ³€μˆ˜λ‘œ μ‚¬μš©ν•˜κ³  싢을 λ•Œ

glovbal μ˜ˆμ•½μ–΄λ₯Ό 톡해 μ „μ—­λ³€μˆ˜λΌλŠ” 것을 λͺ…μ‹œν•΄μ•Όν•œλ‹€.

global a

 

 

·  ν•¨μˆ˜μ˜ λ°˜ν™˜κ°’

 

- λ°˜ν™˜κ°’ μžˆλŠ” ν•¨μˆ˜

return 'λ°˜ν™˜κ°’'

 

- λ°˜ν™˜κ°’ μ—†λŠ” ν•¨μˆ˜

retrun λ¬Έ μƒλž΅

 

- λ°˜ν™˜κ°’ μ—¬λŸ¬κ°œ ν•¨μˆ˜

λ°˜ν™˜ 값을 λ¦¬μŠ€νŠΈμ— λ‹΄μ•„ λ°˜ν™˜ν•¨.

def multi(v1,v2):
    List = []
    res1 = v1 + v2
    res2 = v1 - v2
    List.append(res1)
    List.append(res2)
    return List


myList = []
myList = multi(100,200)
hap = myList[0]
sub = myList[1]
hap, sub = multi(100,200)

μœ„ μ½”λ“œλ‘œ μž‘μ„±μ‹œ λ³„λ„λ‘œ 리슀트λ₯Ό λ§Œλ“€μ§€ μ•Šμ•„λ„ λœλ‹€.

return res1, res2

νŒŒμ΄μ¬μ€ μ—¬λŸ¬ 개의 값을 λ°˜ν™˜ν•˜λŠ” 문법을 ν—ˆμš©ν•œλ‹€.

 

 

- pass μ˜ˆμ•½μ–΄

μ΄λ¦„λ§Œ λ§Œλ“€μ–΄λ†“κ³  λ‚΄μš©μ€ pass μ˜ˆμ•½μ–΄λ₯Ό 톡해 λΉ„μ›Œλ‘˜ 수 μžˆλ‹€.

def myFunc() :
	pass

 

 

 

· ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜ 전달

- 값에 μ˜ν•œ 전달

κ°’ 자체 전달, 값이 λ³΅μ‚¬λ˜λŠ” 방식

 

- 참쑰에 μ˜ν•œ 전달

리슀트,νŠœν”Œ,λ”•μ…”λ„ˆλ¦¬,μ„ΈνŠΈ = μ£Όμ†Œ 곡유

 

- λ§€κ°œλ³€μˆ˜ 개수λ₯Ό 지정해 전달

λ§€κ°œλ³€μˆ˜ κ°œμˆ˜μ— λ§žμΆ”μ–΄ ν˜ΈμΆœν•΄μ•Όν•¨

def func(v1,v2):
...
hap = func(1,2)

 

-λ§€κ°œλ³€μˆ˜μ— κΈ°λ³Έ 값을 μ„€μ •ν•΄ 놓고 전달

λ§€κ°œλ³€μˆ˜μ— κΈ°λ³Έ κ°’ μ„€μ •

def func(v1,v2,v3=0) :

 

- λ§€κ°œλ³€μˆ˜μ˜ 개수λ₯Ό μ§€μ •ν•˜μ§€ μ•Šκ³  전달 --- κ°€λ³€ λ§€κ°œλ³€μˆ˜ Arbitrary Argument List

λ§€κ°œλ³€μˆ˜ λͺ… μ•žμ— * 을 뢙이면 됨.

λ§€κ°œλ³€μˆ˜κ°€ νŠœν”Œ ν˜•μ‹μœΌλ‘œ λ„˜μ–΄μ™€ νŠœν”Œμ„ μ²˜λ¦¬ν•˜λŠ” λ°©μ‹μœΌλ‘œ ν•¨μˆ˜ μ•ˆμ—μ„œ μ‚¬μš©κ°€λŠ₯

def func( *para ):
	return = 0
    for num in para :
    	result = result + para
        
    return result

func(1,2,3,4)
func(1,2,3)

 

**의 κ²½μš°μ—λŠ” λ”•μ…”λ„ˆλ¦¬ν˜•μ‹μœΌλ‘œ 처리되며 , λ§€κ°œλ³€μˆ˜λ₯Ό ν‚€=κ°’μœΌλ‘œ μ‚¬μš©ν•΄μ•Όν•¨

def func(**para) :
	for k in para.keys() : 
    	print(k,para[k])

func(μ•ˆλ…•=1,ν•˜μ„Έμš”=2)

 

·  λͺ¨λ“ˆ

ν•¨μˆ˜μ˜ 집합

import λͺ¨λ“ˆμ΄λ¦„

문을 ν†΅ν•΄μ„œ ν•¨μˆ˜ ν˜ΈμΆœμ„ ν•  수 μžˆλ‹€.

 

· λͺ¨λ“ˆ 생성, μ‚¬μš©

λͺ¨λ“ˆ 파일, 호좜 νŒŒμΌμ€ λͺ¨λ‘ 같은 폴더 에 μžˆμ–΄μ•Ό ν•œλ‹€.

import Module1
Module1.func1()
from λͺ¨λ“ˆλͺ… import ν•¨μˆ˜1,ν•¨μˆ˜2,ν•¨μˆ˜3
from λͺ¨λ“ˆλͺ… import *

func1()

λͺ¨λ“ˆλͺ….ν•¨μˆ˜() κ°€ μ•„λ‹Œ ν•¨μˆ˜()만 μ“°κ³  싢을 λ•Œ

 

· λͺ¨λ“ˆ μ’…λ₯˜

- ν‘œμ€€λ¬˜λ“ˆ : νŒŒμ΄μ¬μ—μ„œ μ œκ³΅ν•˜λŠ” λͺ¨λ“ˆ 

- μ‚¬μš©μž μ •μ˜ λͺ¨λ“ˆ : 직접 λ§Œλ“€μ–΄μ„œ μ‚¬μš©ν•˜λŠ” λͺ¨λ“ˆ

- μ„œλ“œ νŒŒν‹° λͺ¨λ“ˆ 3rd party : μ™ΈλΆ€ νšŒμ‚¬, λ‹¨μ²΄μ—μ„œ μ œκ³΅ν•˜λŠ” λͺ¨λ“ˆ

import sys
print(sys.builtin_module_names)

import math 
dir(math) ##λͺ¨λ“ˆλ³„λ‘œ μ œκ³΅λ˜λŠ” ν•¨μˆ˜ 확인 ##

 

 

·  νŒ¨ν‚€μ§€

μ—¬λŸ¬ λͺ¨λ“ˆμ„ λͺ¨μ•„ 놓은 것 (폴더 ν˜•νƒœ)

import νŒ¨ν‚€μ§€λͺ….λͺ¨λ“ˆλͺ… import ν•¨μˆ˜λͺ…
import pakage.Module1 import *

 

·  λ‚΄λΆ€ν•¨μˆ˜

ν•¨μˆ˜ μ•ˆμ— ν•¨μˆ˜κ°€ μžˆλŠ” ν˜•νƒœ

def outFunc(v1,v2):
	def inFunc(num1,num2):
    	return num1+num2
	return inFunc(v1,v2)
print(outFunc(10,20))

κ²°κ³Ό => 30

λ‚΄λΆ€ν•¨μˆ˜λŠ” μ§€μ—­λ³€μˆ˜μ™€ λΉ„μŠ·ν•¨. ν•΄λ‹Ήν•¨μˆ˜ μ•ˆμ—μ„œλ§Œ μ‚¬μš© κ°€λŠ₯

단독 ν˜ΈμΆœν•˜λ©΄ 였λ₯˜ λ°œμƒ.

 

 

·  λžŒλ‹€ν•¨μˆ˜  + map() ν•¨μˆ˜

ν•¨μˆ˜λ₯Ό ν•œ μ€„λ‘œ κ°„λ‹¨ν•˜κ²Œ λ§Œλ“€μ–΄μ€Œ

map (ν•¨μˆ˜λͺ…,리슀트) ν•¨μˆ˜ : λ¦¬μŠ€νŠΈμ— ν•¨μˆ˜ μˆ˜μ‹μ„ ν•œκΊΌλ²ˆμ— 적용 μ‹œν‚¨λ‹€.

def hap(num1,num2):
    	res = num1 + num2
    	return res
print(hap(10,20))
hap2 = lambda num1, num2 : num1 + num2
print(hap2(10,20))

##λ§€κ°œλ³€μˆ˜ κΈ°λ³Έκ°’##
hap3 = lambda num1=10, num2= 20  : num1 + num2 
print(hap3())
print(hap3(100,200)) ## κΈ°λ³Έκ°’ λ¬΄μ‹œ ##

 

 

 

 

 

·  μž¬κ·€ν•¨μˆ˜  --- νŒ©ν† λ¦¬μ–Ό

자기 μžμ‹ μ„ ν˜ΈμΆœν•œλ‹€.

def selfCall() :
	print('ν•˜', end='')
    selfCall()
selfCall()

 

λ¬΄ν•œλ£¨ν”„ 방지λ₯Ό μœ„ν•΄ 쀑지 쑰건을 κΌ­ λ„£μ–΄μ•Όν•œλ‹€.

def factorial(num) :
	if num <= 1 :
    	return num
    else : 
    	retrun num * factorical(num-1)
print(factorial(4))
print(factorial(10))

=24
=3628800

νŒ©ν† λ¦¬μ–Ό κ°’ κ΅¬ν•˜λŠ” ν•¨μˆ˜

10! = 10x(10-1)! 와 κ°™λ‹€λŠ” μ„±μ§ˆμ„ 이용.

 

 

 

·  yield ν•¨μˆ˜ ---- μ œλ„ˆλ ˆμ΄ν„° Generator μƒμ„±μž ν•¨μˆ˜

ν•¨μˆ˜λ₯Ό μ’…κ²°ν•˜μ§€ μ•Šκ³  값을 계속 λ°˜ν™˜ν•˜κ³  μ‹ΆμœΌλ©΄ yield λ¬Έ μ‚¬μš©

def genFunc():
    yield 1
    yield 2
    yield 3
    
print(list(genFunc())

=> 123

μ œλ„ˆλ ˆμ΄ν„° ν•¨μˆ˜ (yield ν•¨μˆ˜κ°€ ν¬ν•¨λœ ν•¨μˆ˜)

μ œλ„ˆλ ˆμ΄ν„°λŠ” yield문으둜 값을 λ°˜ν™˜ν•œ ν›„ 계속 진행함