python ํ์ฉ
์๋ฃํ ํ์ฉํ๊ธฐ
๋ํ๊ธฐ | x + y | |
๋นผ๊ธฐ | x - y | |
๊ณฑํ๊ธฐ | x * y | |
์ ๊ณฑ | x ** y | 2**16 = 65536 |
๋๋๊ธฐ | x / y | int์ int๋ฅผ ๋๋๋๋ผ๋ ์ฐ์ฐ๊ฒฐ๊ณผ๋ ํญ์ float ex) 4 / 2 = 2.0, 5 / 2 = 2.5 |
๋๋จธ์ง์๋ ๋๋๊ธฐ | x // y | ๋ ์ ์๋ก ๋จ์ด์ง๋ค ex) 4 / 2 = 2, 5 / 2 = 2 |
๋๋จธ์ง | x % y | 5 % 2 = 1 |
dictionary
key:value๋ก ๊ตฌ์ฑ๋๋ฉฐ key๋ฅผ ์ฌ์ฉํด value๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค.
products = {
"bread": 1000,
"milk": 3000,
"egg": 6000,
}
print(products["bread"])
print(products["milk"])
print(products["egg"])
# result print
"""
1000
3000
6000
"""
# ๋ง์ฝ ์กด์ฌํ์ง ์๋ key๋ก value๋ฅผ ๊ฐ์ ธ์ค๋ ค ์๋ํ ๋์๋ ์๋ฌ ๋ฐ์
print(products["drink"])
"""
Traceback (most recent call last):
File "sample.py", line 18, in <module>
print(products["drink"])
KeyError: 'drink'
"""
# ์ด ๊ฒฝ์ฐ, .get์ ์ฌ์ฉํด ํด๋น key๊ฐ ์์ ๋ ์ฌ์ฉ๋ ๊ฐ์ ์ง์ ํ ์ ์๋ค
print(products.get("egg", 0)) # egg key๊ฐ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ ํด๋น value ์ถ๋ ฅํ์ง๋ง
print(products.get("drink", 0)) # drink key๊ฐ ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ์ 0 ์ถ๋ ฅํ๋ค
# result print
"""
6000
0
"""
dictionary ์๋ฃํ์ ์์ ๋กญ๊ฒ ๊ฐ์ ์ถ๊ฐ, ์์ , ์ญ์ ํ ์ ์๋ค.
products = {
"bread": 1000,
"milk": 3000,
"egg": 6000,
}
products["bread"] = 1500 # bread key์ value๋ฅผ 1500์ผ๋ก ๋ณ๊ฒฝ
products["drink"] = 2000 # drink key๋ฅผ ๊ฐ์ง value ์ถ๊ฐ
'''
{
"bread": 1500,
"milk": 3000,
"egg": 6000,
"drink": 2000
}
'''
del(products["milk"]) # milk key๋ฅผ ๊ฐ์ง key: value ์ ์ญ์
'''
{
"bread": 1500,
"egg": 6000,
"drink": 2000
}
'''
์ฐ์ฐ ์ถ์ฝ ๋ฌธ๋ฒ
sum_numbers = sum_numbers + i
=> sum_numbers += i
ex)
number = 10
number += 5
print(number)
# 15
string
๋ฌธ์์ด์ +์ *๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
๋ฌธ์ + ๋ฌธ์ = ๋ฌธ์!
"hello " + "world" = hello world
๊ณฑํ๊ธฐ๋ ๋ง์ฐฌ๊ฐ์ง!!
๋ฐฐ์ด๋ for ๋ก ๋๋ฆด ์ ์๋ค!.
sample_string = "hello world!!"
for i in sample_string:
print(i)
'''
h
e
l
l
o
w
o
r
l
d
!
!
'''
fstring
string์ ๋ณ์๋ฅผ ๋ด๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ค.
n1 = 5
n2 = 10
# ์๋ ์ธ๊ฐ์ง ๋ฐฉ๋ฒ ๋ชจ๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฌผ์ด ์ถ๋ ฅ๋ฉ๋๋ค.
print("n1 : %s, n2 : %s, sum : %s" % (n1, n2, n1+n2)) # old Style
print("n1 : {}, n2 : {}, sum : {}".format(n1, n2, n1+n2)) # ~ python < 3.6
print(f"n1 : {n1}, n2 : {n2}, sum : {n1+n2}") # ~ python >= 3.6
# result print
"""
n1 : 5, n2 : 10, sum : 15
n1 : 5, n2 : 10, sum : 15
n1 : 5, n2 : 10, sum : 15
"""
์์ ๋ฒ์ ์ ์ฐ๋ ํ์ฌ๋ ์๊ธฐ ๋๋ฌธ์ ๋ค ์์๋๋๊ฒ์ด ์ข๋ค!
# float์ ์์์ ์ ์ํ๋ ์๋ฆฌ๊น์ง ์ถ๋ ฅํ ์๋ ์์ต๋๋ค.
PIE = 3.14159265358979
print(f"pie : {PIE}") # pie : 3.14159265358979
print(f"pie : {PIE:.2f}") # pie : 3.14
list [ ]
๊ฐ์ ์ํ๋๋๋ก ์ถ๊ฐ, ์์ , ์ญ์ ๊ฐ๋ฅํ๋ค.
numbers = [1, 2, 3, 4, 5]
numbers.append(6) # numbers์ ๋ง์ง๋ง ์๋ฆฌ์ 6 ์ถ๊ฐ
print(numbers)
# = [1, 2, 3, 4, 5, 6]
print(numbers[5])
# 6
numbers.remove(1) # numbers์์ 1์ด๋ผ๋ ๊ฐ์ ๊ฐ์ง ์์ ์ญ์
print(numbers)
# = [2, 3, 4, 5, 6]
numbers[-1] = 7 # numbers์ ๋ง์ง๋ง ์์(6)์ ๊ฐ์ 7๋ก ๋ณ๊ฒฝ
print(numbers)
#= [2, 3, 4, 5, 7]
last_num = numbers.pop() # list์์ ๋ง์ง๋ง ์์ ์ญ์ ํ ๋ณ์์ ํ ๋น
print(numbers)
# last_num = 7
# = [2, 3, 4, 5]
์ซ์, ๋ฌธ์ ์ธ์๋ ๋ค์ํ ์๋ฃํ ์ฌ์ฉ ๊ฐ๋ฅ
indexing, slicing์ ์ฌ์ฉํด์ ์ํ๋ ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์๋ค.
len()์ ์ด์ฉํด์ list์ ๊ธธ์ด๋ ๊ตฌํ ์ ์๋ค!! print(len(numbers))
alphabets = ["a", "b", "c", "d", "e", "f"]
# indexing์ ํ์ฉํด ์ํ๋ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
print(alphabets[0]) # alphabets์ ์ฒซ ๋ฒ์งธ ์์๋ฅผ ์ถ๋ ฅ
print(alphabets[2]) # alphabets์ ์ธ ๋ฒ์งธ ์์๋ฅผ ์ถ๋ ฅ
print(alphabets[-1]) # alphabets์ ๋ง์ง๋ง ์์๋ฅผ ์ถ๋ ฅ
# index slicing ๊ธฐ๋ฅ์ ํ์ฉํด ์ํ๋ ๊ฐ๋ค ๊ฐ์ ธ์ค๊ธฐ
print(alphabets[0:4]) # alphabets์ index๊ฐ 0๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ 4๋ณด๋ค ์์ ์์ ์ถ๋ ฅ
# result print
"""
a
c
f
['a', 'b', 'c', 'd'] # alphabets ์ 0, 1, 2, 3 index๊ฐ ์ถ๋ ฅ๋จ
"""
tuple ( )
๋ฆฌ์คํธ๋ก ๊ฑฐ์ ๋ค ํ ์ ์๊ธฐ๋ ํด์ ๋ณดํต ์ ์์ฐ์ธ๋ค!
๋ฆฌ์คํธ์ฒ๋ผ indexing ๊ฐ๋ฅํ๊ณ , ์์์ ๋ค์ํ ์๋ฃํ์ ์ฌ์ฉํ ์ ์๋ค.
ํํ์์๋ ์์์ ๊ฐ์ ์์ ํ๊ฑฐ๋ ์ญ์ ํ ์ ์๋ค. ์ถ๊ฐ๋ง ๊ฐ๋ฅํ๋ค
len() ํจ์๋ฅผ ์ฌ์ฉํด tuple์ ๊ธธ์ด๋ฅผ ๊ตฌํ ์ ์๋ค.
numbers = (1, 2, 3, 4, 5)
numbers += (6, 7) # numbers์ ๋ง์ง๋ง ์๋ฆฌ์ 6, 7 ์ถ๊ฐ
print(numbers)
# (1, 2, 3, 4, 5, 6, 7)
numbers[-1] = 8
# tuple ์๋ฃํ์ ๊ฐ์ ๋ณ๊ฒฝํ๋ ค๊ณ ์๋ํ ๊ฒฝ์ฐ ์๋ฌ ๋ฐ์
"""
Traceback (most recent call last):
File "sample.py", line 7, in <module>
numbers[-1] = 8
TypeError: 'tuple' object does not support item assignment
"""
set { }
์ด๊ฒ๋ ๋ฆฌ์คํธ๋ ๋น์ทํ์ง๋ง indexing๊ณผ slicing์ ์ง์ํ์ง ์๋๋ค
์ค๋ณต๋ ๊ฐ๋ ํฌํจํ์ง ์๋๋ค!
len() ํจ์๋ง ์ฌ์ฉํด์ ๊ธธ์ด๋ฅผ ๊ตฌํ ์ ์๋ค.
numbers = {1, 2, 3, 4, 5, 5, 3}
print(numbers)
# numbers = {1, 2, 3, 4, 5} / ์ค๋ณต๋ ๊ฐ์ ํฌํจํ์ง ์๊ธฐ ๋๋ฌธ์ 5๋ ํ๋๋ง ์ ์ฅ
print(numbers[0]) # set ์๋ฃํ์ index๋ฅผ ์ฌ์ฉํด ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์์
"""
Traceback (most recent call last):
File "sample.py", line 4, in <module>
print(numbers[0])
TypeError: 'set' object is not subscriptable
"""
dictionary ์ค์ํ๋ค!!
Key : Value๋ก ๊ตฌ์ฑ๋๋ฉฐ key๋ฅผ ์ฌ์ฉํด์ value๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค!
: ๋์ ,์ ์ฌ์ฉํ๋ฉด set์ด ๋๋ค!
products = {
"bread": 1000,
"milk": 3000,
"egg": 6000,
}
print(products["bread"])
print(products["milk"])
print(products["egg"])
# result print
"""
1000
3000
6000
"""
get.()
products = {
"bread": 1000,
"milk": 3000,
"egg": 6000,
}
print(products["bread"])
print(products["milk"])
print(products["egg"])
# result print
"""
1000
3000
6000
"""
# ๋ง์ฝ ์กด์ฌํ์ง ์๋ key๋ก value๋ฅผ ๊ฐ์ ธ์ค๋ ค ์๋ํ ๋์๋ ์๋ฌ ๋ฐ์
print(products["drink"])
"""
Traceback (most recent call last):
File "sample.py", line 18, in <module>
print(products["drink"])
KeyError: 'drink'
"""
# ์ด ๊ฒฝ์ฐ, .get์ ์ฌ์ฉํด ํด๋น key๊ฐ ์์ ๋ ์ฌ์ฉ๋ ๊ฐ์ ์ง์ ํ ์ ์๋ค
print(products.get("egg", 0)) # egg key๊ฐ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ ํด๋น value ์ถ๋ ฅํ์ง๋ง
print(products.get("drink", 0)) # drink key๊ฐ ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ์ 0 ์ถ๋ ฅํ๋ค
# result print
"""
6000
0
"""
์์ key๊ฐ์ ๊ฐ์ ธ์ค๋ ค๊ณ ํ๋ฉด error๊ฐ ๋ฐ์ํ๋๋ฐ
์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๊ฒ ํ๊ธฐ ์ํด์๋ .get()์ ์ด์ฉํ๋ฉด ๋๋ค!
get ์์ ์๋ฌ๊ฐ ๋ฌ์๋ ์ถ๋ ฅํ ๊ฐ์ ๋ฃ์ด์ฃผ๋ฉด ์๋ฌ๊ฐ ๋์ง ์๊ณ , ์ถ๋ ฅ๊ฐ์ ์ถ๋ ฅํ๋ค!
dictionary๋ ์ถ๊ฐ, ์์ , ์ญ์ ๊ฐ๋ฅํ๋ค!
products = {
"bread": 1000,
"milk": 3000,
"egg": 6000,
}
products["bread"] = 1500 # bread key์ value๋ฅผ 1500์ผ๋ก ๋ณ๊ฒฝ
products["drink"] = 2000 # drink key๋ฅผ ๊ฐ์ง value ์ถ๊ฐ
print(products)
'''
{
"bread": 1500,
"milk": 3000,
"egg": 6000,
"drink": 2000
}
'''
del(products["milk"]) # milk key๋ฅผ ๊ฐ์ง key: value ์ ์ญ์
print(products)
'''
{
"bread": 1500,
"egg": 6000,
"drink": 2000
}
'''
์๋ฃํ ๋ณํ
string -> int
string_number = "10"
integer_number = int(string_number)
# type() ํจ์๋ฅผ ์ฌ์ฉํด ํน์ ๊ฐ์ ์๋ฃํ์ ํ์ธํ ์ ์์ต๋๋ค.
print(string_number)
print(integer_number)
print(type(string_number))
print(type(integer_number))
# result print
"""
10
10
<class 'str'>
<class 'int'>
"""
# ๋๊ฐ์ ๊ฐ์ด๋ผ๋ type์ด ๋ค๋ฅผ ์ ์๊ธฐ ๋๋ฌธ์ ์๋ฃํ์ ํญ์ ์ ์ํด์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
# ์๋ฃํ ๋ณํ ์กฐ๊ฑด์ด ๋ง์ง ์์ ๊ฒฝ์ฐ, ์๋ฌ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค.
string_number = "number"
integer_number = int(string_number)
"""
Traceback (most recent call last):
File "sample.py", line 21, in <module>
integer_number = int(string_number)
ValueError: invalid literal for int() with base 10: 'number'
"""
๋๊ฐ์ ๊ฐ์ด๋ผ๋ type์ด ๋ค๋ฅด๋ฉด ๊ฐ์ง ์๊ธฐ ๋๋ฌธ์ ์ ์ํด์ผํ๋ค!
list -> tuple -> set
sample_list = [1, 2, 3, 4, 5]
sample_tuple = tuple(sample_list)
sample_set = set(sample_tuple)
print(sample_list)
print(sample_tuple)
print(sample_set)
# result print
"""
[1, 2, 3, 4, 5]
(1, 2, 3, 4, 5)
{1, 2, 3, 4, 5}
"""
set์ list๋ก ๋ฐ๊พธ๋๊ฒ๋ ๊ฐ๋ฅ!!
any -> string
number = 10
numbers = [1, 2, 3, 4, 5]
sample_dict = {"key": "value"}
flag = True
print(str(number))
print(str(numbers))
print(str(sample_dict))
print(str(flag))
# result print
"""
10
[1, 2, 3, 4, 5]
{'key': 'value'}
True
"""
# ์ถ๋ ฅ ๋ ๊ฐ๋ค์ ๋ชจ๋ string ์๋ฃํ
ํจ์
ํจ์์ ์ธ
def print_hello_world():
print("hello world!!")
# ์ ์ธํ ํจ์๋ฅผ ํธ์ถํ๋ฉด ํจ์ ์์ ์์ฑ ๋ ์ฝ๋๊ฐ ์คํ๋ฉ๋๋ค.
print_hello_world() # hello world!!
๋ฆฌํด๋ฐ๊ธฐ
def multiply(a, b):
return a * b
num1 = 5
num2 = 10
result = multiply(num1, num2)
# result = 50
importํด์ ๋ค๋ฅธํ์ผ ์ฝ๋ ์ฌ์ฉํ๊ธฐ
a.py, main.py, folder๊ฐ ์๊ณ ,
folder ์์ b.py, c.py ๊ฐ ์๋ค๊ณ ํ์
main.py ์์ a.py๋ฅผ ๊ฐ์ ธ์์ ์ฌ์ฉํ๊ธฐ
1.
# a.py
def a_funtion():
print("execute a")
# main.py
import a # a ํ์ผ์ import
a.a_funtion() # a ํ์ผ์ a_funtion() ์ฌ์ฉ
2.
# a.py
def a_funtion():
print("execute a")
# main.py
from a import a_funtion # a ํ์ผ์ ์๋ a_funtion์ import
# ์ด ๊ฒฝ์ฐ ์ ์์ ์ ๋ค๋ฅด๊ฒ, a.a_funtion๊ฐ ์๋ a_funtion์ผ๋ก ์ฌ์ฉํ๋ค.
a_funtion() # execute a
3.
# a.py
def a_funtion():
print("execute a")
# main.py
from a import * # a ํ์ผ์ ์๋ ๋ชจ๋ ํจ์๋ฅผ import
a_funtion() # execute a
import ์์ * ์ง์ํ๋ผ๊ณ ํ๋ค. ์๋ํ๋ฉด ์ฝ๋๊ฐ ๋ง์์ก์ ๋
์ด๋์ import๋ฅผ ํด์จ๊ฑด์ง ์ถ์ฒ๋ฅผ ์๊ธฐ๊ฐ ์ด๋ ต๊ธฐ ๋๋ฌธ์ด๋ค.
๋ค๋ฅธ ํด๋์ ์๋ ํ์ผ importํ๊ธฐ
# folder/b.py
def b_funtion():
print("execute b")
# folder/c.py
def c_funtion1():
print("execute c1")
def c_funtion2():
print("execute c2")
# main.py
from folder import b
from folder.c import *
b.b_funtion() # execute b
c_funtion1() # execute c1
c_funtion2() # execute c2
๋ณ์๋ ๋์ผํ๊ฒ import ํด์์ ์ฌ์ฉํ๋ค
# ๋ค๋ฅธ ํ์ผ์ ๋ณ์ ๋ํ ํจ์์ ๋์ผํ ๋ฐฉ๋ฒ์ผ๋ก from / import๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
# folder/b.py
PIE = 3.14
HELLO = "world"
# main.py / case 1
from folder.b import * # *๊ถ์ฅX* ์กด์ฌํ๋ ๋ชจ๋ ๋ณ์ import
print(PIE) # 3.14
print(HELLO) # "world"
# main.py / case 2
from folder.b import PIE, HELLO # ์ฌ์ฉ ํ ๋ณ์๋ฅผ ๊ฐ๊ฐ import
print(PIE) # 3.14
print(HELLO) # "world"
# main.py / case 3
from folder import b # *๊ถ์ฅO* b ํ์ผ import
print(b.PIE) # 3.14
print(b.HELLO) # world
'''
python์์ ๋ค๋ฅธ ํ์ผ์ ์๋ ์ฝ๋๋ฅผ ์ฌ์ฉํ ๋์๋ ์ด๋์(from) ์ด๋ค(import) ๊ฒ์
๊ฐ์ ธ์์ ์ฌ์ฉํ ์ง ์ง์ ํด ์ค์ผ ํฉ๋๋ค.
'''
# main.py ํ์ผ์ ๊ธฐ์ค์ผ๋ก, ์ ์ธ๋ ํจ์์ ๋ณ์์ ๊ฒฝ๋ก๋ ์๋์ ๊ฐ์ต๋๋ค.
a.a_funtion
folder.b.b_funtion
folder.b.PIE
folder.b.HELLO
folder.c.c_funtion1
folder.c.c_funtion2
# ์ฌ๊ธฐ์ ๋ค์ํ ๋ฐฉ์์ผ๋ก from๊ณผ import๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
from a import a_funtion
from a import *
import a
from folder import b
from folder.b import *
from folder.c import c_funtion1, c_funtion2
๊ฐ ๋น๊ตํ๊ธฐ
'''== : ๊ฐ์ด ์ผ์นํ๋์ง ๋น๊ต'''
"a" == "a" # True
"a" == "b" # False
1 == "1" # False, ๊ฐ์ ๋์ผํ์ง๋ง ์๋ฃํ์ด ๋ค๋ฅด๊ธฐ ๋๋ฌธ
'''!= : ๊ฐ์ด ์ผ์นํ์ง ์๋์ง ๋น๊ต'''
0 != 1 # True
0 != 0 # False
'''>, < : ๊ฐ์ด ํฐ์ง ์์์ง ๋น๊ต'''
5 > 2 # True
1 < 0 # False
1 > 1 # False
'''>=, <= : ๊ฐ์ด ํฌ๊ฑฐ๋ ๊ฐ์์ง, ์๊ฑฐ๋ ๊ฐ์์ง ๋น๊ต'''
1 >= 1 # True
'''in : ํน์ ๊ฐ์ด list / tuple / set์ ํฌํจ๋์ด ์๋์ง ํ์ธ'''
4 in [1, 2, 3] # False
1 in (1, 2, 3) # True
# ๋ชจ๋ ๋น๊ต ์ฐ์ฐ์์ ๊ฒฐ๊ณผ๋ print()๋ก ํ์ธํ ์ ์์ต๋๋ค.
print(1 == 1) # True
์กฐ๊ฑด๋ฌธ
ํน์ ๊ฐ์ด True ํน์ False์ผ ๊ฒฝ์ฐ ์คํ๋ ๋ก์ง์ ์ ์ํ๋ค.
if condition: # ์กฐ๊ฑด์ด True์ผ ๊ฒฝ์ฐ
# some code
# not ํค์๋๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ ์กฐ๊ฑด์ด False์ผ ๋ ์คํ๋ฉ๋๋ค.
elif not condition: # ์กฐ๊ฑด์ด False์ผ ๊ฒฝ์ฐ
# some code
else: # ์ ์กฐ๊ฑด๋ค ์ค ๋ง์กฑํ๋๊ฒ ์์ ๊ฒฝ์ฐ
# some code
and, or ์ ์ฌ์ฉํด 2๊ฐ ์ด์์ ์กฐ๊ฑด์ ๋ณตํฉ์ ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค
๋น์ด์๋ string, list ๋ฑ์ ๋ถ๊ธฐ๋ฌธ์์ False๋ก ํ๋จํ๋ค
ํน์ ๊ฐ์ด True์ธ์ง False์ธ์ง๋ bool()ํจ์๋ฅผ ํตํด ํ์ธ ๊ฐ๋ฅํ๋ค.
print(bool(""))
print(bool(0))
print(bool([]))
print(bool("sample"))
print(bool([1, 2]))
print(bool(1))
print(bool(-1)) # 0์ด ์๋ ์ซ์๋ True๋ก ํ๋จ
# sample result
"""
False
False
False
True
True
True
True
"""
'๐๐ช > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Python ํน๊ฐ (3) (0) | 2022.09.07 |
---|---|
22. 09. 06 ๊ณ์ฐ๊ธฐ ๋ง๋ค๊ธฐ (0) | 2022.09.06 |
Python ํน๊ฐ (1) (0) | 2022.09.05 |
ํ์ด์ฌ ๋ฌธ๋ฒ ๊ธฐ์ด 1์ฃผ์ฐจ(์ฌํ) (0) | 2022.09.01 |
ํ์ด์ฌ ๋ฌธ๋ฒ ๊ธฐ์ด 1์ฃผ์ฐจ ๊ฐ๋ฐ์ผ์ง (0) | 2022.09.01 |
๋๊ธ