ํŒŒ์ด์ฌ ๋…ํ•™ 1์ผ์ฐจ

2021. 2. 26. 23:29ใ†Python

ํŒŒ์ด์ฌ์ด ๊ทธ๋ ‡๊ฒŒ ํŽธํ•˜๋‹ค๊ณ , ๋งŽ์ด๋“ค ์“ด๋‹ค๊ณ  ํ•ด์„œ ๊ฒฐ๊ตญ ๋‹ค์‹œ ๊ณต๋ถ€ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ์›๋ž˜๋Š” ์งง๊ฒŒ ์ „๊ณต์œผ๋กœ ๋ฐฐ์› ์ง€๋งŒ, ๊ทธ๋•Œ๋Š” ์ฝ”๋”ฉ์— ๊ด€์‹ฌ๋„ ์—†์—ˆ๊ณ  ํŒŒ์ด์ฌ์˜ ์†Œ์ค‘ํ•จ์— ๋Œ€ํ•ด ๋ชฐ๋ž๋‹ค.. ๊ทธ๋ž˜์„œ ๋…ํ•™ํ•˜๋ฉฐ ๋ธ”๋กœ๊ทธ์— ๊ธฐ๋กํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค. ์•„๋งˆ C๋‚˜ Swift์™€์˜ ๋น„๊ต๊ฐ€ ์ฃผ๊ฐ€ ๋˜์ง€ ์•Š์„๊นŒ ์‹ถ๋‹ค. ํŠนํžˆ C

 

1. ์ˆซ์žํ˜•

1) ์—ฐ์‚ฐ์ž ์ข…๋ฅ˜

๋‚ด๊ฐ€ ๋Š๋ผ๊ธฐ์— ๊ฐ€์žฅ ํŠน์ดํ•œ ์ ์€ ํŒŒ์ด์ฌ์˜ ์—ฐ์‚ฐ์ž ๋ถ€ํ˜ธ์˜€๋‹ค. 

+ ๋ง์…ˆ
- ๋บ„์…ˆ
/ ๋‚˜๋ˆ—์…ˆ
* ๊ณฑ์…ˆ
% ๋‚˜๋จธ์ง€
** ๊ฑฐ๋“ญ์ œ๊ณฑ

๊ฑฐ๋“ญ์ œ๊ณฑ์ด ์žˆ๋‹ค๋‹ˆ?! ๊ทธ๋ฆฌ๊ณ  ๊ทธ๊ฑธ ์ด์ œ์„œ์•ผ ์•Œ์•˜๋‹ค๋‹ˆ..? C์—์„œ๋Š” ๊ฑฐ๋“ญ์ œ๊ณฑ์„ ์“ฐ๊ธฐ ์œ„ํ•ด math.h ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํฌํ•จ์‹œ์ผœ์ฃผ๊ณ  pow(double,double) ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ์จ์•ผํ–ˆ๋Š”๋ฐ.... ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•œ๊ฒŒ ์žˆ์—ˆ๋‹ค๋‹ˆ..

 

2) ๋‚˜๋ˆ—์…ˆ์˜ ๊ฒฐ๊ณผ๋Š” ๋ฌด์กฐ๊ฑด float

6 / 3 ์ด๋ผ๋„ ๊ฒฐ๊ณผ๋Š” 2.0์œผ๋กœ ์ถœ๋ ฅ๋œ๋‹ค. ์ด ๋˜ํ•œ ํŒŒ์ด์ฌ์˜ ๋…ํŠนํ•œ ๋ฐฉ์‹ ๊ฐ™๋‹ค.

 

2. ๋ฌธ์ž์—ด

1) ๋ง์…ˆ์œผ๋กœ ๋ฌธ์ž์—ด์„ ํ•ฉ์น  ์ˆ˜ ์žˆ๋‹ค

print("Hello" + "World!")

-> ๊ฒฐ๊ณผ๋Š” Hello World!

2) ๊ณฑ์…ˆ ์—ฐ์‚ฐ์ž๋กœ ๋ฐ˜๋ณต ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค

sentence = "Hello!\n"
print(sentence * 2)

-> ๊ฒฐ๊ณผ

Hello!
Hello!

 

3. ํ˜• ๋ณ€ํ™˜

1) stringํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค : str()

ํ˜• ๋ณ€ํ™˜ ๋ถ€๋ถ„์€ C์—์„œ ์—†๋Š” ๋ถ€๋ถ„๋งŒ ์ž‘์„ฑํ–ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ C์—์„œ๋„ ํ˜• ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ˆ๊นŒ!

print("์ œ ๋‚˜์ด๋Š” " + str(7) + "์‚ด์ž…๋‹ˆ๋‹ค.")

์ •์ˆ˜ 7์ด ๋ฌธ์ž์—ด 7๋กœ ๋ณ€ํ™˜๋œ๋‹ค.

 

 

4. typeํ•จ์ˆ˜

์ž๋ฃŒํ˜•์„ ํ™•์ธํ•  ๋•Œ ์“ฐ๋ฉด ํŽธ๋ฆฌํ•œ typeํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ์ด ์กด์žฌํ•œ๋‹ค. 

 

์ด์šฉ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค!

print(type("True"))
print(type(True))

๊ฒฐ๊ณผ

<class 'str'>
<class 'bool'>

 

5. Floor Division( // )

ํŒŒ์ด์ฌ์—๋Š” ์ฐธ ํŠน์ดํ•œ ๊ธฐ๋Šฅ์ด ๋งŽ์€ ๊ฒƒ ๊ฐ™๋‹ค. 

Floor Divison์€ ์Šฌ๋ž˜์‰ฌ ๋‘ ๊ฐœ // ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋‚˜๋ˆ—์…ˆ ์—ฐ์‚ฐ ํ›„ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜๋‚ด๋ฆผํ•ด์ค€๋‹ค. 

 

num = 7 // 2      
print(num) 

๊ฒฐ๊ณผ

3

 

6. ๋ฐ˜์˜ฌ๋ฆผ(round)

print(round(1.421, 1))      # 1.421์„ ์†Œ์ˆ˜์  1์งธ ์ž๋ฆฌ๋กœ ๋ฐ˜์˜ฌ๋ฆผ
print(round(2.7862, 2))     # 2.7562๋ฅผ ์†Œ์ˆ˜์  2์งธ ์ž๋ฆฌ๋กœ ๋ฐ˜์˜ฌ๋ฆผ
print(round(3.141592, 4))   # 3.141592๋ฅผ ์†Œ์ˆ˜์  4์งธ ์ž๋ฆฌ๋กœ ๋ฐ˜์˜ฌ๋ฆผ
print(round(4.34))          # 4.34๋ฅผ ์†Œ์ˆ˜์  0๋ฒˆ์งธ ์ž๋ฆฌ๋กœ ๋ฐ˜์˜ฌ๋ฆผ

//์ด ๋ถ€๋ถ„์€ ๊ณต๋ถ€์— ๊ต‰์žฅํžˆ ๋„์›€๋˜๊ณ  ์žˆ๋Š” ์ฝ”๋“œ์ž‡ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.

๊ฒฐ๊ณผ

1.4
2.79
3.1416
4

 

7. ํ•จ์ˆ˜

ํŒŒ์ด์ฌ์—์„œ๋Š” ํ•จ์ˆ˜ ์ •์˜๋„ ์ข€ ๋” ๋‹จ์ˆœํ•œ ๋Š๋‚Œ์ด๋‹ค. 

def helloWorld(name):
	print("Hello, World! %s!" % (name))

helloWorld("Github")
Hello World! Github!

์ด๋•Œ def ~ ์ด ๋ถ€๋ถ„์„ ํ•จ์ˆ˜์˜ ํ—ค๋”(header), ํ•จ์ˆ˜์˜ ๋‚ด์šฉ์„ ๋ฐ”๋””(body)๋ผ๊ณ  ํ•œ๋‹ค.

๋˜ํ•œ name๊ณผ ๊ฐ™์ด ํ•จ์ˆ˜๊ฐ€ ์ž…๋ ฅ ๋ฐ›๋Š” ๊ฐ’์„ ํŒŒ๋ผ๋ฏธํ„ฐ(parameter)๋ผ๊ณ  ํ•œ๋‹ค.

ํŒŒ์ด์ฌ์—์„œ๋Š” ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ€ ๊ต‰์žฅํžˆ ์ค‘์š”ํ•˜๋‹ค. { }์™€ ๊ฐ™์€ ๊ด„ํ˜ธ๋ฅผ ์“ฐ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋“ค์—ฌ์“ฐ๊ธฐ๋กœ ํ•จ์ˆ˜์˜ ๋์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

8. Optional Parameters

def myself(name, age, nationality = "ํ•œ๊ตญ"):
    print("๋‚ด ์ด๋ฆ„์€ %s" % name)
    print("๋‚˜์ด๋Š” %d์‚ด" % age)
    print("๊ตญ์ ์€ %s" % nationality)

myself("์ฝ”๋“œ์ž‡", 1)            # ๊ธฐ๋ณธ๊ฐ’์ด ์„ค์ •๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ”๊พธ์ง€ ์•Š์„ ๋•Œ
myself("์ฝ”๋“œ์ž‡", 1, "๋ฏธ๊ตญ")     # ๊ธฐ๋ณธ๊ฐ’์ด ์„ค์ •๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ”๊พธ์—ˆ์„ ๋•Œ

** Optional Parameter๋Š” ์˜ค์ง! >>๋งจ ๋งˆ์ง€๋ง‰<<์— ์ •์˜ํ•ด์„œ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ์™ธ๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•จ

** ํ•˜์ง€๋งŒ ์—ฌ๋Ÿฌ๊ฐœ ์“ฐ๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅํ•จ! ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ๋งจ ๋งˆ์ง€๋ง‰์— ๋ชฐ์•„์„œ ์‚ฌ์šฉ

 

9. Global ๋ณ€์ˆ˜

ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ •์˜๋œ ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜ ๋ฐ–์—์„œ ์‚ฌ์šฉ ๋ชปํ•˜๋Š” ๊ฒŒ ์ •์„์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํŒŒ์ด์ฌ์—์„œ๋Š” global์ด๋ผ๋Š” ์„ ์–ธ์„ ํ†ตํ•ด ํ•จ์ˆ˜ ๋ฐ–์— ์„ ์–ธ๋˜์–ด ์žˆ๋Š” ๋ณ€์ˆ˜๋ผ๋„ ๋ณ€์ˆ˜ ๋ช…์ด ๊ฐ™์œผ๋ฉด ๊ทธ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๊ฐ€์ ธ์™€ ์ ์šฉํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด,

def add():
	global x
	x += 1

x = 1
add()
print(x)
2

add()๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์œ„์—์„œ ์ •์˜๋œ x ๊ฐ’ 1์„ ๋ฐ›์•„์™€์„œ(๊ฐ™์€ ๋ณ€์ˆ˜๋ช…์ธ๋ฐ global์ด๊ธฐ ๋•Œ๋ฌธ์—) += 1 ์—ฐ์‚ฐ์„ ์‹คํ–‰ํ•œ๋‹ค.

๊ฒฐ๊ตญ, x ๊ฐ’์€ 2๊ฐ€ ๋œ๋‹ค. ๋งŒ์•ฝ ํ•จ์ˆ˜ ์•ˆ์˜ ๊ฐ’์ด global์ด ์•„๋‹ˆ์—ˆ๋‹ค๋ฉด ๊ฒฐ๊ณผ ๊ฐ’์€ 1์ด์—ˆ์„ ๊ฒƒ์ด๋‹ค.

 

10. ์ƒ์ˆ˜ in Python

const๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” ์ƒ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.(๋‹ค๋ฅธ ์–ธ์–ด์—์„œ. ์˜ˆ๋ฅผ ๋“ค๋ฉด C)

ํ•˜์ง€๋งŒ Python์€ ๋”ฑํžˆ ๊ทธ๋Ÿฐ ์ •ํ•ด์ง„ ๊ฒƒ์ด ์—†๋‹ค. ๊ทธ๋ƒฅ ๊ทœ์น™์œผ๋กœ, ์ƒ์ˆ˜์ผ๋•Œ๋Š” ๋Œ€๋ฌธ์ž๋ฅผ  ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ ์ •๋„๊ฐ€ ์žˆ๋‹ค. 

PI = 3.14

๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์šฐ๋ฆฌ๊ฐ€ ๊ตณ์ด PI ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค๊ฑฐ๋‚˜ ํ•˜์ง€๋Š” ์•Š์ง€๋งŒ, ํŒŒ์ด์ฌ ๋‚ด์˜ ๊ทœ์น™์ด๋‹ˆ ์ƒ์ˆ˜๋กœ ์ทจ๊ธ‰ํ•˜๋ฉฐ ์ง€ํ‚ค์ž๋Š” ์ •๋„๊ฐ€ ๋˜๊ฒ ๋‹ค.

 

11. if / elif / else

elif == else if

 

12. ๋ชจ๋“ˆ(Module)

๋ชจ๋“ˆ(module)์ด๋ž€ ๋ณ€์ˆ˜, ํ•จ์ˆ˜, ํด๋ž˜์Šค ๋“ฑ์„ ๋ชจ์•„ ๋†“์€ ํŒŒ์ผ. ์ฆ‰, header ํŒŒ์ผ๊ณผ ๋น„์Šทํ•œ ๋Š๋‚Œ์ด์ง€๋งŒ ์„ ํƒํ•ด์„œ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ์ข‹๋‹ค. 

 

์‚ฌ์šฉ ์˜ˆ์‹œ

//calculate.py
//๋ชจ๋“ˆ

def sum(x, y):
	return x + y
    
def subtract(x, y):
	return x - y
    
def multiply(x,y):
	return x * y

def divide(x, y):
	return x / y
//test.py

from calculate import sum

print(sum(1,2))

-> test.py์—์„œ calculate.py์— ์ •์˜๋˜์–ด ์žˆ๋˜ sum์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ import ํ•ด ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

** ์—ฌ๋Ÿฌ ๊ฐœ ๊ฐ€์ ธ ์˜ฌ ์ˆ˜ ์žˆ์Œ

** ํ•œ ๋ฒˆ์— ๋‹ค ๊ฐ€์ ธ์˜ฌ ์ˆ˜๋„ ์žˆ์Œ( from calculate import * ) : ํ•˜์ง€๋งŒ ๋น„์ถ”!

 

1) randit

๋‚œ์ˆ˜(์ •์ˆ˜) ๋ฐœ์ƒ์— ์‚ฌ์šฉํ•˜๋Š” ํŒŒ์ด์ฌ ์ „์šฉ ํ•จ์ˆ˜. random์ด๋ผ๋Š” ๋ชจ๋“ˆ์— ์ด๋ฏธ ์ •์˜๋˜์–ด ์žˆ์–ด์„œ ๊ฐ€์ ธ์™€์„œ ์“ฐ๊ธฐ๋งŒ ํ•˜๋ฉด ๋จ.

from random import randit

//randit(a,b)๋กœ ์‚ฌ์šฉ. a์ด์ƒ, b์ดํ•˜ ๋žœ๋คํ•œ ๊ฐ’์„ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

randomNum = randit(1,10)
print(randomNum)

2) uniform

๋‚œ์ˆ˜(์†Œ์ˆ˜) ๋ฐœ์ƒ์— ์‚ฌ์šฉ. ๋™์ผํ•˜๊ฒŒ random์— ์ •์˜๋˜์–ด ์žˆ์Œ

 

 

13. input ํ•จ์ˆ˜

name = input("์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”: ")
print("Hello " + name)

** ์ด๋•Œ ์ž…๋ ฅํ•˜๋Š” ๋ชจ๋“  ๊ฐ’์€ String ํ˜•ํƒœ์ž„. ์ฆ‰, ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ์“ฐ๊ณ  ์‹ถ๋‹ค๋ฉด ํ˜•๋ณ€ํ™˜์„ ํ•ด์ฃผ์–ด์•ผํ•จ

 

14. ๋ฆฌ์ŠคํŠธ

๋‚˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐฐ์—ด๊ณผ ๋™์ผํ•˜๊ฒŒ ์ดํ•ดํ–ˆ๋‹ค. ๋ฐฐ์—ด์ฒ˜๋Ÿผ index ๊ฐœ๋…์„ ์‚ฌ์šฉํ•˜๊ณ , 0๋ฒˆ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. 

1) ์Šฌ๋ผ์ด์‹ฑ

numbers = [1, 2, 3, 4, 5, 6]

# ์Šฌ๋ผ์ด์‹ฑ์„ ํ†ตํ•ด ์›์†Œ ์ถœ๋ ฅํ•˜๊ธฐ
print(numbers[0:4])
print(numbers[0:-3])
print(numbers[2:])
print(numbers[:3])
[1, 2, 3, 4]
[1, 2, 3]
[3, 4, 5, 6]
[1, 2, 3]

 

2) ํ•จ์ˆ˜ ์ข…๋ฅ˜ (์ด ๋ถ€๋ถ„์€ Swift์™€ ์œ ์‚ฌํ•˜๋‹ค๊ณ  ๋Š๊ผˆ์Œ)

- len : ์›์†Œ count

 

- insert & append : insert๋Š” ์ •ํ™•ํžˆ ์–ด๋–ค ์ž๋ฆฌ์— ๋ฌด์—‡์„ ๋„ฃ๋Š”์ง€ ๋ช…๋ น๊ฐ€๋Šฅ. append๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์‚ฝ์ž….

 

- del

# ์ธ๋ฑ์Šค 3์— ์žˆ๋Š” ๊ฐ’ ์‚ญ์ œ
del numbers[3]

# ์ธ๋ฑ์Šค 4๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๊ฐ’๊นŒ์ง€ ์‚ญ์ œ
del numbers[4:]

 

- sorted : ๋ฆฌ์ŠคํŠธ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ

numbers = [8, 6, 2, 4, 5, 7, 1, 3]
numbers = sorted(numbers)

print(numbers)

 ++ .sort() ๋„ ์œ ์‚ฌ ๊ธฐ๋Šฅ.

numbers = [5, 3, 7, 1]
numbers.sort()
print(numbers)

 

 

- + : ๋ฆฌ์ŠคํŠธ ์—ฐ๊ฒฐ ๊ฐ€๋Šฅ

alphabet1 = ["a", "b", "c"]
alphabet2 = ["d", "e", "f"]
alphabet = alphabet1 + alphabet2

 

3) in ์‚ฌ์šฉ์œผ๋กœ ๋ฆฌ์ŠคํŠธ ๊ฐ’ ์ฒดํฌํ•˜๊ธฐ

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23]
print(7 in primes)
print(12 in primes)
True
False

 

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23]
print(7 not in primes)
print(12 not in primes)
False
True

 

4) nested list == 2์ฐจ์› ๋ฐฐ์—ด

5) .reverse() : ๋ฐฐ์—ด ๋’ค์ง‘์–ด์คŒ

numbers = [5, 3, 7, 1]
numbers.reverse()
print(numbers)

6) .index(์ฐพ๊ณ  ์‹ถ์€ ๊ฐ’) : ์ฐพ๊ณ  ์‹ถ์€ ๊ฐ’์˜ ์ธ๋ฑ์Šค ๋ฆฌํ„ดํ•ด์คŒ

members = ["์˜ํ›ˆ", "์œค์ˆ˜", "ํƒœํ˜ธ", "ํ˜œ๋ฆฐ"]
print(members.index("์œค์ˆ˜"))
print(members.index("ํƒœํ˜ธ"))
1
2

 

7) .remove(์‚ญ์ œ ํ•˜๊ณ  ์‹ถ์€ ๊ฐ’)

fruits = ["๋”ธ๊ธฐ", "๋‹น๊ทผ", "ํŒŒ์ธ์• ํ”Œ", "์ˆ˜๋ฐ•", "์ฐธ์™ธ", "๋ฉ”๋ก "]
fruits.remove("ํŒŒ์ธ์• ํ”Œ")
print(fruits)