Skip to content

Latest commit

ย 

History

History
827 lines (570 loc) ยท 14.7 KB

File metadata and controls

827 lines (570 loc) ยท 14.7 KB

python

ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์ž๋ฃŒํ˜•

์ •์ˆ˜, ์†Œ์ˆ˜, ๋ฌธ์ž์—ด, ๋ถˆ๋ฆฐ

์ถ”์ƒํ™”

๋ณต์žกํ•œ ๋‚ด์šฉ์€ ์ˆจ๊ธฐ๊ณ  ์ฃผ์š” ๊ธฐ๋Šฅ์—๋งŒ ์‹ ๊ฒฝ ์“ฐ์ž

ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ์˜ ์ถ”์ƒํ™”

  • ๋ณ€์ˆ˜

๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ

  • ํ•จ์ˆ˜

๋ช…๋ น์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ

def hello(): # ํ•จ์ˆ˜์˜ ํ—ค๋”
    print("Hello!")
    print("Welcome to Suwon")

hello()

ํŒŒ๋ผ๋ฏธํ„ฐ : ํ•จ์ˆ˜์— ๋„˜๊ฒจ ์ฃผ๋Š” ๊ฐ’

def hello(name):
    print("Hello!")
    print(name)
    print("Welcome to Suwon")

hello("Chris")

์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ

def print_sum(a, b, c):
    print(a + b + c)

print_sum(7, 3, 2)

return : ๋Œ๋ ค์ค€๋‹ค

  • ๊ฐ์ฒด

์ˆซ์žํ˜•

์ •์ˆ˜ํ˜•๊ณผ ์ •์ˆ˜ํ˜•๊ฐ„์˜ ์—ฐ์‚ฐ์€ ๋ชจ๋‘ ์ •์ˆ˜ํ˜•์œผ๋กœ ๋‚˜์˜จ๋‹ค.

# ๋‚˜๋จธ์ง€
print(7 % 3) # 1

# ๊ฑฐ๋“ญ์ œ๊ณฑ
print(2**3) # 2์˜ 3์ œ๊ณฑ -> 8

์†Œ์ˆ˜ํ˜•๊ณผ ์†Œ์ˆ˜ํ˜•๊ฐ„์˜ ์—ฐ์‚ฐ์€ ๋ชจ๋‘ ์†Œ์ˆ˜ํ˜•์œผ๋กœ ๋‚˜์˜จ๋‹ค.

# ๋‚˜๋จธ์ง€
print(7.0 % 3.0) # 1.0

# ๊ฑฐ๋“ญ์ œ๊ณฑ
print(2.0**3.0) # 8.0

์†Œ์ˆ˜ํ˜•๊ณผ ์ •์ˆ˜ํ˜•๊ฐ„์˜ ์—ฐ์‚ฐ์€ ๋ชจ๋‘ ์†Œ์ˆ˜ํ˜•์œผ๋กœ ๋‚˜์˜จ๋‹ค.

# ๋‚˜๋จธ์ง€
print(7 % 3.0) # 1.0

# ๊ฑฐ๋“ญ์ œ๊ณฑ
print(2.0**3) # 8.0

๋‚˜๋ˆ—์…ˆ

๋‚˜๋ˆ—์…ˆ์€ ์ •์ˆ˜ํ˜• ์ •์ˆ˜ํ˜•์ด์–ด๋„ ํ•ญ์ƒ ์†Œ์ˆ˜ํ˜•์œผ๋กœ ๋‚˜์˜จ๋‹ค.

print(7 / 2) # 3.5
print(6 / 2) # 3.0
print(7.0 / 2) # 3.5
print(6.0 / 2.0) # 3.0

floor division (๋ฒ„๋ฆผ ๋‚˜๋ˆ—์…ˆ)

๋ฒ„๋ฆผ ๋‚˜๋ˆ—์…ˆ์„ ํ•  ๋•Œ ํ•˜๋‚˜๋ผ๋„ ์†Œ์ˆ˜ํ˜•์ด๋ฉด ๊ฒฐ๊ณผ๊ฐ’์€ ์†Œ์ˆ˜ํ˜•์œผ๋กœ ๋‚˜์˜จ๋‹ค. -> ์›๋ž˜ ๊ฒฐ๊ณผ ๊ฐ’์— .0๋งŒ ๋ถ™๋Š”๋‹ค.

# floor division (๋ฒ„๋ฆผ ๋‚˜๋ˆ—์…ˆ)
print(7 // 2) # 3
print(8 // 3) # 2
print(8.0 // 3) # 2.0
print(8 // 3.0) # 2.0
print(8.0 // 3.0) # 2.0

# round (๋ฐ˜์˜ฌ๋ฆผ)
print(round(3.1415926535)) # 3
print(round(3.1415926535, 2)) # 3.14
print(round(3.1415926535, 4)) # 3.1416

๋ฌธ์ž์—ด

ํ‚ค๋ณด๋“œ๋กœ ์“ธ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž๋“ค์„ ํ‘œํ˜„ํ•˜๋Š” ์ž๋ฃŒํ˜•

'์Šฌํฌ'

์—ญ์Šฌ๋ž˜์‰ฌ๋ฅผ ์จ์„œ ๋ฌธ์ž์—ด์•ˆ์— ๋”ฐ์˜ดํ‘œ๋ฅผ ์˜๋„ํ–ˆ์Œ์„ ์•Œ๋ ค์ค€๋‹ค.

print("I\'m\"excited\" to learn Python!")

ํ˜• ๋ณ€ํ™˜ (Type Conversion)

๊ฐ’์„ ํ•œ ์ž๋ฃŒํ˜•์—์„œ ๋‹ค๋ฅธ ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ

print(int(3.8)) # 3
print(float(3)) # 3.0
print(int("2") + int("5")) # 7
print(float("1.1") + float("2.5")) # 3.6

print(str(2) + str(5)) # 25
age = 23
print("์ œ ๋‚˜์ด๋Š”" + str(age) + "์‚ด์ž…๋‹ˆ๋‹ค.")
print(int("Hello World!")) -> ์˜ค๋ฅ˜ (๋…ผ๋ฆฌ์ ์œผ๋กœ ๋ง์ด ์•ˆ ๋จ)

๋ฌธ์ž์—ด ํฌ๋งคํŒ…

year = 2023
month = 8
day = 1

print("์˜ค๋Š˜์€ " + str(year) + "๋…„ " + str(month) + "์›” " + str(day) + "์ผ์ž…๋‹ˆ๋‹ค.")

print("์˜ค๋Š˜์€ {}๋…„ {}์›” {}์ผ์ž…๋‹ˆ๋‹ค.".format(year, month, day))

date_string = "์˜ค๋Š˜์€ {}๋…„ {}์›” {}์ผ์ž…๋‹ˆ๋‹ค."
print(date_string.format(year, month, day))

format ๋‹ค๋ฃจ๊ธฐ

num_1 = 1
num_2 = 2

print("{0} ๋‚˜๋ˆ„๊ธฐ {1}์€ {2}์ž…๋‹ˆ๋‹ค.".format(num_1, num_2, num_1 / num_2)) # 1 ๋‚˜๋ˆ„๊ธฐ 2์€ 0.5์ž…๋‹ˆ๋‹ค.

# f๋Š” float์˜ f
# :.2f -> ์†Œ์ˆ˜์  ๋‘˜์งธ์ž๋ฆฌ๋กœ ๋ฐ˜์˜ฌ๋ฆผ ํ•ด๋ผ
# ์ •์ˆ˜๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค๋ฉด :.0f
print("{0} ๋‚˜๋ˆ„๊ธฐ {1}์€ {2:.2f}์ž…๋‹ˆ๋‹ค.".format(num_1, num_2, num_1 / num_2)) # 1 ๋‚˜๋ˆ„๊ธฐ 2์€ 0.50์ž…๋‹ˆ๋‹ค.

f-string

ํŒŒ์ด์ฌ ๋ฒ„์ „ 3.6๋ถ€ํ„ฐ ์ƒˆ๋กญ๊ฒŒ ๋‚˜์˜จ ๋ฐฉ์‹

name = "์Šฌํฌ"
age = 23

print(f"์ œ ์ด๋ฆ„์€ {name}์ด๊ณ  {age}์‚ด์ž…๋‹ˆ๋‹ค.") # ์ œ ์ด๋ฆ„์€ ์Šฌํฌ์ด๊ณ  23์‚ด์ž…๋‹ˆ๋‹ค.
wage = 5  # ์‹œ๊ธ‰ (1์‹œ๊ฐ„์— 5๋‹ฌ๋Ÿฌ)
exchange_rate = 1142.16  # ํ™˜์œจ (1๋‹ฌ๋Ÿฌ์— 1142.16์›)

# "1์‹œ๊ฐ„์— 5๋‹ฌ๋Ÿฌ ๋ฒŒ์—ˆ์Šต๋‹ˆ๋‹ค."
print("{}์‹œ๊ฐ„์— {}{} ๋ฒŒ์—ˆ์Šต๋‹ˆ๋‹ค.".format(1, wage * 1, "๋‹ฌ๋Ÿฌ"))

# "5์‹œ๊ฐ„์— 25๋‹ฌ๋Ÿฌ ๋ฒŒ์—ˆ์Šต๋‹ˆ๋‹ค."
print("{}์‹œ๊ฐ„์— {}{} ๋ฒŒ์—ˆ์Šต๋‹ˆ๋‹ค.".format(wage, wage**2,"๋‹ฌ๋Ÿฌ"))

# "1์‹œ๊ฐ„์— 5710.8์› ๋ฒŒ์—ˆ์Šต๋‹ˆ๋‹ค."
print("{}์‹œ๊ฐ„์— {:.1f}{} ๋ฒŒ์—ˆ์Šต๋‹ˆ๋‹ค.".format(1, exchange_rate*wage, "์›"))

# "5์‹œ๊ฐ„์— 28554.0์› ๋ฒŒ์—ˆ์Šต๋‹ˆ๋‹ค."
print("{}์‹œ๊ฐ„์— {:.1f}{} ๋ฒŒ์—ˆ์Šต๋‹ˆ๋‹ค.".format(wage, wage*(exchange_rate*wage), "์›"))

๋ถˆ ๋Œ€์ˆ˜

์ง„๋ฆฌ๊ฐ’ true, false

๋ถˆ ๋Œ€์ˆ˜์˜ ์—ฐ์‚ฐ

AND, OR, NOT

๋ช…์ œ

์ฐธ ๋˜๋Š” ๊ฑฐ์ง“์ด ํ™•์‹คํ•œ ๋ฌธ์žฅ

AND ์—ฐ์‚ฐ

x์™€ y๊ฐ€ ๋ชจ๋‘ ์ฐธ์ผ ๋•Œ๋งŒ x AND y๊ฐ€ ์ฐธ์ด๋‹ค.

๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ์ˆ˜๋„๋Š” ์„œ์šธ์ด๋‹ค. AND 2๋Š” 1๋ณด๋‹ค ํฌ๋‹ค.

-> ๋‘˜๋‹ค TRUE์ด๋ฏ€๋กœ TRUE

OR ์—ฐ์‚ฐ

x์™€ y ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ฐธ์ด๋ฉด x OR y๊ฐ€ ์ฐธ์ด๋‹ค.

๋‘˜ ๋‹ค ๊ฑฐ์ง“์ด์–ด์•ผ FALSE๋‹ค.

๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ์ˆ˜๋„๋Š” ์„œ์šธ์ด๋‹ค. OR ๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ์ˆ˜๋„๋Š” ์ œ์ฃผ๋„๋‹ค.

-> ํ•˜๋‚˜๋ผ๋„ TRUE์ด๋ฏ€๋กœ TRUE

NOT ์—ฐ์‚ฐ

TRUE๋ฉด FALSE, FALSE๋ฉด TRUE๋กœ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

NOT ๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ์ˆ˜๋„๋Š” ์„œ์šธ์ด๋‹ค.

-> ๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ์ˆ˜๋„๋Š” ์„œ์šธ์ด ์•„๋‹ˆ๋‹ค. -> FALSE

NOT 2๋Š” 1๋ณด๋‹ค ์ž‘๋‹ค.

-> 2๋Š” 1๋ณด๋‹ค ์ž‘์ง€ ์•Š๋‹ค. -> TRUE

๋ถˆ๋ฆฐํ˜•

์ฐธ๊ณผ ๊ฑฐ์ง“ (True, False)

print(not not True) # not False -> True

print(not not False) # not True -> False
print(7 == 7 or (4 < 3 and 12 > 10)) # True
x = 3

print(x > 4 or not (x < 2 or x == 3)) # False

ํƒ€์ž…

print(type(4 / 2))

# 4 / 2๋Š” 2.0์ด๋‹ค. ๋”ฐ๋ผ์„œ <class 'int'>๊ฐ€ ์•„๋‹ˆ๋ผ <class 'float'>๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

# ๋‚˜๋ˆ—์…ˆ์„ ํ•  ๊ฒฝ์šฐ ํ•ญ์ƒ ์‹ค์ˆ˜ ๊ฐ’์œผ๋กœ ๋Œ๋ ค์ค€๋‹ค.
def hello():
  print("Hello world!")

print(type(hello)) # <class 'function'>
print(type(print)) # <class 'builtin_function_or_method'>

# ํ•จ์ˆ˜๋„ ํ•˜๋‚˜์˜ ์ž๋ฃŒํ˜•์ด๋‹ค.
# builtin_function -> ๋‚ด์žฅ๋˜์–ด ์žˆ๋Š” ํ•จ์ˆ˜

๋ณ€์ˆ˜

๋“ฑํ˜ธ : ์ง€์ •์—ฐ์‚ฐ์ž

return ๋ฌธ

  • ๊ฐ’ ๋Œ๋ ค์ฃผ๊ธฐ
  • ํ•จ์ˆ˜ ์ฆ‰์‹œ ์ข…๋ฃŒํ•˜๊ธฐ
def square(x):
    print("ํ•จ์ˆ˜ ์‹œ์ž‘")
    return x * x
    print("ํ•จ์ˆ˜ ๋") # Dead Code (์˜๋ฏธ ์—†๋Š” ์ฝ”๋“œ)

print(square(3))
print("Hello world")

# ํ•จ์ˆ˜ ์‹œ์ž‘
# 9
# Hello world

return VS print

return ํ•˜๋Š” ๊ฒŒ ์—†์œผ๋ฉด None

def print_square(x):
    print(x * x)

def get_square(x):
    return (x * x)

get_square(3) # ๊ทธ๋ƒฅ ์•”๊ฒƒ๋„ ์—†์Œ
print(get_square(3)) # 9

print(print_square(3))
# 9
# None

์˜ต์…”๋„ ํŒŒ๋ผ๋ฏธํ„ฐ

ํŒŒ๋ผ๋ฏธํ„ฐ์— '๊ธฐ๋ณธ๊ฐ’(default value)'์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ '์˜ต์…”๋„ ํŒŒ๋ผ๋ฏธํ„ฐ(optional parameter)'๋ผ๊ณ  ํ•œ๋‹ค.

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

myself("์Šฌํฌ", 23, "๋ฏธ๊ตญ")  # ์˜ต์…”๋„ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒฝ์šฐ

# ๋‚ด ์ด๋ฆ„์€ ์Šฌํฌ
# ๋‚˜์ด๋Š” 23์‚ด
# ๊ตญ์ ์€ ๋ฏธ๊ตญ

print()
myself("์Šฌํฌ", 23)  # ์˜ต์…”๋„ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
# ๋‚ด ์ด๋ฆ„์€ ์Šฌํฌ
# ๋‚˜์ด๋Š” 23์‚ด
# ๊ตญ์ ์€ ํ•œ๊ตญ

์˜ต์…”๋„ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋ชจ๋‘ ๋งˆ์ง€๋ง‰์— ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์•„๋ž˜์ฒ˜๋Ÿผ ์˜ต์…”๋„ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ค‘๊ฐ„์— ๋„ฃ์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

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


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

# File "myself.py", line 1
     def myself(name, nationality = "ํ•œ๊ตญ", age):
               ^
SyntaxError: non-default argument follows default argument

Syntactic Sugar

Syntactic Sugar ์ด๋ž€ ์ž์ฃผ ์“ฐ์ด๋Š” ํ‘œํ˜„์„ ๋” ๊ฐ„๋žตํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋ฌธ๋ฒ•์ด๋‹ค.

x = x * 2
x *= 2

scope

๋ณ€์ˆ˜๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฒ”์œ„

๋กœ์ปฌ ๋ณ€์ˆ˜, ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜

x = 2 # ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜

def my_function():
    print(x)

my_function()
print(x)

# 2
# 2
x = 2 # ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜

def my_function():
    x = 3 # ๋กœ์ปฌ ๋ณ€์ˆ˜
    print(x)

my_function()
print(x)

# 3
# 2

ํŒŒ๋ผ๋ฏธํ„ฐ๋„ ๋กœ์ปฌ ๋ณ€์ˆ˜๋‹ค.

์ƒ์ˆ˜ (constant)

์ ˆ๋Œ€๋กœ ๋ฐ”๋€Œ์ง€ ์•Š๋Š” ๊ฒƒ

ํ•œ๋ˆˆ์— ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ฆ„์€ ๋ฌด์กฐ๊ฑด ๋Œ€๋ฌธ์ž๋กœ ํ•˜์ž

๋ชจ๋“  ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜ ์ด๋ฆ„์€ ์†Œ๋ฌธ์ž๋กœ ์“ฐ๊ณ , ์—ฌ๋Ÿฌ ๋‹จ์–ด์ผ ๊ฒฝ์šฐ _ (์–ธ๋”๋ฐ”)๋กœ ๋‚˜๋ˆˆ๋‹ค. ๋ชจ๋“  ์ƒ์ˆ˜ ์ด๋ฆ„์€ ๋Œ€๋ฌธ์ž๋กœ ์“ฐ๊ณ , ์—ฌ๋Ÿฌ ๋‹จ์–ด์ผ ๊ฒฝ์šฐ _ (์–ธ๋”๋ฐ”)๋กœ ๋‚˜๋ˆˆ๋‹ค.

๋“ค์—ฌ์“ฐ๊ธฐ๋Š” ๋ฌด์กฐ๊ฑด ์ŠคํŽ˜์ด์Šค 4๊ฐœ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

ํ•จ์ˆ˜ ์ •์˜ ์œ„์•„๋ž˜๋กœ ๋นˆ ์ค„์ด ๋‘ ๊ฐœ์”ฉ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

๊ด„ํ˜ธ ๋ฐ”๋กœ ์•ˆ์—๋Š” ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ•˜์ง€ ๋ง์•„์•ผ ํ•œ๋‹ค. ์‰ผํ‘œ ์•ž์—๋Š” ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ•˜์ง€ ๋ง์•„์•ผ ํ•œ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ์—ฐ์‚ฐ์ž ์•ž๋’ค๋กœ ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ•˜๋‚˜์”ฉ ํ•œ๋‹ค.

while๋ฌธ

i = 1
while i <= 9:
    j = 1
    while j <= 9:
        print("{} * {} = {}".format(i, j, i * j))
        j += 1
    i += 1

if๋ฌธ

elif ๋ฌธ

i = 1
while i <= 100:
    if i % 8 == 0 and i % 12 != 0:
        print(i)
    i += 1
i = 1
total = 0

while i < 1000:
    if i % 2 == 0 or i % 3 == 0:
        total += i
    i += 1

print(total)

๋ฆฌ์ŠคํŠธ ์Šฌ๋ผ์ด์‹ฑ

numbers[2:]

-> index 2๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰๊นŒ์ง€

numbers[:3]

-> ์ฒ˜์Œ๋ถ€ํ„ฐ index 2๊นŒ์ง€

numbers[0:3]

-> 0๋ถ€ํ„ฐ index 2๊นŒ์ง€

๋ฆฌ์ŠคํŠธ ํ•จ์ˆ˜

numbers = []
numbers.append(5) # [5]
numbers.append(8) # [5, 8]
len(numbers) # 2

del -> ์‚ญ์ œ

numbers = [2, 3, 5, 7]
del numbers[3]
print(numbers) # [2, 3, 5]

insert -> ์‚ฝ์ž… (์›ํ•˜๋Š” ์œ„์น˜์— ์‚ฝ์ž… ๊ฐ€๋Šฅ)

numbers = [2, 3, 5, 7]
numbers.insert(4, 37)
print(numbers) # [2, 3, 5, 7, 37]

๋ฆฌ์ŠคํŠธ ์ •๋ ฌ

sorted : ๊ธฐ์กด ๋ฆฌ์ŠคํŠธ๋Š” ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ณ  ์ •๋ ฌ๋œ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ด

numbers = [2, 5, 3, 7]
new_list = sorted(numbers)
print(new_list) # [2, 3, 5, 7]

๋‚ด๋ฆผ์ฐจ์ˆœ

numbers = [2, 5, 3, 7]
new_list = sorted(numbers, reverse=True)
print(new_list) # [7, 5, 3, 2]

sort : ์•„๋ฌด๊ฒƒ๋„ ๋ฆฌํ„ดํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌ

numbers = [2, 5, 3, 7]
print(numbers.sort()) # None

numbers = [2, 5, 3, 7]
numbers.sort()
print(numbers) # [2, 3, 5, 7]
numbers = [2, 5, 3, 7]
numbers.sort(reverse=True)
print(numbers) # [7, 5, 3, 2]

๋ฆฌ์ŠคํŠธ ์•ˆ์˜ ๋ฆฌ์ŠคํŠธ (Nested List)

grades = [[62, 75, 77], [78, 81, 86], [85, 91, 89]]

# ์ฒซ ๋ฒˆ์งธ ํ•™์ƒ์˜ ์„ฑ์ 
print(grades[0])

# ์„ธ ๋ฒˆ์งธ ํ•™์ƒ์˜ ์„ฑ์ 
print(grades[2])

# ์ฒซ ๋ฒˆ์งธ ํ•™์ƒ์˜ ์ฒซ ๋ฒˆ์งธ ์‹œํ—˜ ์„ฑ์ 
print(grades[0][0])

index ๋ฉ”์†Œ๋“œ

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

for๋ฌธ

range

๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ good

for i in range(start, stop):
    print(i)
# start ๋ถ€ํ„ฐ stop - 1๊นŒ์ง€์˜ ๋ฒ”์œ„
for i in range(10):
    print(i)
# 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ๋ฒ”์œ„
for i in range(start, stop, step):
    print(i)
# start ๋ถ€ํ„ฐ stop - 1๊นŒ์ง€์˜ ๋ฒ”์œ„, ๊ฐ„๊ฒฉ step

์‚ฌ์ „ (dictionary)

key-value ์Œ

์ˆœ์„œ๋ผ๋Š” ๊ฐœ๋…์ด ์—†์Œ, ์‚ฌ์ „์˜ key๋Š” ๋ฌธ์ž์—ด, ์ •์ˆ˜ํ˜• ๋‹ค ๊ฐ€๋Šฅ

my_dictionary = {
    5: 25,
    2: 4,
    3: 9
}

print(type(my_dictionary)) # dict

print(my_dictionary[3]) # 9

my_dictionary[9] = 81
print(my_dictionary) # { 5: 25, 2: 4, 3: 9, 9: 81 }

values

my_dictionary = {
    5: 25,
    2: 4,
    3: 9
}

print(my_dictionary.values()) # dict_values([25, 4, 9])

print(25 in my_dictionary.values()) # True
print(20 in my_dictionary.values()) # False

for value in my_dictionary.values():
    print(value)
    # 25
    # 4
    # 9

for key in my_dictionary.keys():
    print(key)
    # 5
    # 2
    # 3

items

for key in my_dictionary.keys():
    value = my_dictionary[key]
    print(key, value)
    # 5 25
    # 2 4
    # 3 9

for key, value in my_dictionary.items():
    print(key, value)
    # 5 25
    # 2 4
    # 3 9

Aliasing

x = [2, 3, 5, 7, 11]
y = list(x) # ๋ณต์ œ ํ•จ์œผ๋กœ์จ x์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š๊ฒŒ ํ•จ
y[2] = 4

print(x) # [2, 3, 5, 7, 11]
print(y) # [2, 3, 4, 7, 11]

๋ฆฌ์ŠคํŠธ์™€ ๋ฌธ์ž์—ด

๋ฆฌ์ŠคํŠธ๋Š” ์ˆ˜์ • ๊ฐ€๋Šฅ, ๋ฌธ์ž์—ด์€ ์ˆ˜์ • ๋ถˆ๊ฐ€

๋ชจ๋“ˆ

๊ธฐ๋Šฅ๋“ค์„ ์ •๋ฆฌํ•ด ๋†“์€ ๊ฒƒ

์Šคํƒ ๋‹ค๋“œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

import math
import os
import random

randint()

randint(a, b) -> a, b ๋‘ ์ˆ˜ ์‚ฌ์ด์˜ ์–ด๋–ค ๋žœ๋คํ•œ ์ •์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜

import random

print(random.randint(1, 20)) # 1๋ถ€ํ„ฐ 20๊นŒ์ง€ ์•„๋ฌด ์ˆซ์ž 1๊ฐœ

uniform()

๋‘ ์ˆ˜ ์‚ฌ์ด์˜ ๋žœ๋คํ•œ ์†Œ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜

uniform(a, b)๋ฅผ ํ•˜๋ฉด, a โ‰ค N โ‰ค b๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์–ด๋–ค ๋žœ๋คํ•œ ์†Œ์ˆ˜ N์„ ๋ฆฌํ„ดํ•œ๋‹ค.

datetime

pi_day = datetime.datetime(2020, 3, 14) # 2020-03-14 00:00:00

pi_day = datetime.datetime(2020, 3, 14, 13, 6, 15) # 2020-03-14 13:06:15

today = datetime.datetime.now() # 2023-08-04 17:49:12.360266

today = datetime.datetime.now()

print(today)
print(today.year)  # ์—ฐ๋„
print(today.month)  # ์›”
print(today.day)  # ์ผ
print(today.hour)  # ์‹œ
print(today.minute)  # ๋ถ„
print(today.second)  # ์ดˆ

ํŒŒ์ผ ์ฝ๊ธฐ

with open("file.txt", "r") as f: # r์€ read, w์€ write, a๋Š” append
    for line in f:
        print(line)

strip

print ๋ฌธ์€ ์ถœ๋ ฅ๋  ๋•Œ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•œ ์ค„ ์—”ํ„ฐ๊ฐ€ ๋Œ

strip์€ ํ™”์ดํŠธ ์ŠคํŽ˜์ด์Šค (๊ณต๋ฐฑ)์„ ์—†์• ์ค€๋‹ค.

print("      abc   def   ".strip()) # abc   def

with open("file.txt", "r") as f:
    for line in f:
        print(line.strip()) # ์ค„ ๊ฑด๋„ˆ๋œ€ ์‚ฌ๋ผ์ง

split

string = "1. 2. 3. 4. 5. 6"

print(string.split(".")) # ['1', ' 2', ' 3', ' 4', ' 5', ' 6']

print(string.split(". ")) # ['1', '2', '3', '4', '5', '6']

name = "Han, Seulhee"
print(name.split(", ")) # ['Han', 'Seulhee']
with open('data/chicken.txt', 'r') as f:
    total_revenue = 0
    total_days = 0

    for line in f:
        data = line.strip().split(": ")
        revenue = int(data[1])  # ๊ทธ๋‚ ์˜ ๋งค์ถœ

        total_revenue += revenue
        total_days += 1

    print(total_revenue / total_days)

ํŒŒ์ผ ์“ฐ๊ธฐ

with open("new_file.txt", "w") as f:
    f.write("์•ˆ๋…•")
    f.write("๋‚œ ์Šฌํฌ์•ผ")

# ์•ˆ๋…•๋‚œ ์Šฌํฌ์•ผ

with open("new_file.txt", "w") as f:
    f.write("์•ˆ๋…•\n")
    f.write("๋‚œ ์Šฌํฌ์•ผ\n")

# ์•ˆ๋…•
# ๋‚œ ์Šฌํฌ์•ผ

with open("new_file.txt", "a") as f: # a ๋Š” append (ํŒŒ์ผ์ด ์—†์–ด๋„ a๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.)
    f.write("์•ˆ๋…•\n")
    f.write("๋‚œ ์Šฌํฌ์•ผ\n")

# ์•ˆ๋…•
# ๋‚œ ์Šฌํฌ์•ผ
# ์•ˆ๋…•
# ๋‚œ ์Šฌํฌ์•ผ
with open('vocabulary.txt', 'r') as f:
    for line in f:
        data = line.strip().split(": ")
        english_word, korean_word = data[0], data[1]

        # ์œ ์ € ์ž…๋ ฅ๊ฐ’ ๋ฐ›๊ธฐ
        guess = input("{}: ".format(korean_word))

        # ์ •๋‹ต ํ™•์ธํ•˜๊ธฐ
        if guess == english_word:
            print("๋งž์•˜์Šต๋‹ˆ๋‹ค!\n")
        else:
            print("์•„์‰ฝ์Šต๋‹ˆ๋‹ค. ์ •๋‹ต์€ {}์ž…๋‹ˆ๋‹ค.\n".format(english_word))