a = 7
b = 2
a+b # 9
a-b # 5
a*b # 14
a/b # 3.5
a+3*b # 13 (์ฌ๋ฌ ์ฐ์ฐ์ ํ ์ค์ ํ ๊ฒฝ์ฐ ์ฌ์น์ฐ์ฐ์ ์์๋๋ก!)
(a+3)*b # 20 (์๊ดํธ๋ฅผ ์ด์ฉํด์ ๋จผ์ ๊ณ์ฐํ ๋ถ๋ถ์ ํ์ํด์ค ์ ์์)
a//b # 3 (๋ชซ)
a%b # 1 (๋๋จธ์ง)
a**b # 49 (๊ฑฐ๋ญ์ ๊ณฑ)
x = True # ์ฐธ
y = False # ๊ฑฐ์ง
# ์๋ฌธ์๋ก ์ฐ๋ฉด ์๋ฃํ์ผ๋ก ์ธ์ํ์ง ์๊ณ ๋ณ์๋ช
์ด๋ผ ์๊ฐํด ์๋ฌ๊ฐ ๋จ!
z = true # name 'true' is not defined
True = 1 # True/False๋ ๋ณ์๋ช
์ผ๋ก ์ธ ์ ์์ด์!
4 > 2 # True ํฌ๋ค
5 < 1 # False ์๋ค
6 >= 5 # True ํฌ๊ฑฐ๋ ๊ฐ๋ค
4 <= 4 # True ์๊ฑฐ๋ ๊ฐ๋ค
3 == 5 # False ๊ฐ๋ค
4 != 7 # True ๊ฐ์ง ์๋ค
-> ๋ ผ๋ฆฌ ์ฐ์ฐ์ ์ฌ์ฉ๊ฐ๋ฅ
a = 4 > 2 # True
not a # False NOT ์ฐ์ฐ์๋ก ์ฐธ์ ๊ฑฐ์ง์ผ๋ก, ๊ฑฐ์ง์ ์ฐธ์ผ๋ก ๋ฐ๊ฟ์ค๋ค.
a and b # False AND ์ฐ์ฐ์๋ก ๋ชจ๋ ์ฐธ์ด์ด์ผ ์ฐธ์ ๋ฐํํ๋ค.
a or b # True OR ์ฐ์ฐ์๋ก ๋ ์ค ํ๋๋ง ์ฐธ์ด๋ฉด ์ฐธ์ด๋ค.
# ์์ ๋ฐ์ดํ ๋๋ ํฐ ๋ฐ์ดํ. ๋ ๋ค ๊ฐ์์!
a = "aa"
b = 'aa'
first_name = "Harry"
last_name = "Potter"
first_name + last_name # HarryPotter
first_name + " " + last_name # Harry Potter
a = "3"
b = "5"
a + b # 35
// ๋ฌธ์์ด๊ณผ ์ ์๋ฅผ ๋ํ๋ฉด ์๋ฌ๋๋ค!!!
a = "3"
a + 5 # ๋ฌธ์์ด๊ณผ ์ซ์ํ์ ๋ํ ์ ์์ด์ ์๋ฌ!!!
print(len("abcde")) # 5
print(len("Hello, Sparta!")) # 14
print(len("์๋
ํ์ธ์.")) # 6
sentence = 'Python is FUN!'
sentence.upper() # PYTHON IS FUN!
sentence.lower() # python is fun!
# ์ด๋ฉ์ผ ์ฃผ์์์ ๋๋ฉ์ธ 'gmail'๋ง ์ถ์ถํ๊ธฐ
myemail = 'test@gmail.com'
result = myemail.split('@') # ['test','gmail.com'] (๋ค์ ๋ฐฐ์ธ '๋ฆฌ์คํธ'๋ผ๋ ์๋ฃํ์ด์์ :))
result[0] # test (๋ฆฌ์คํธ์ ์ฒซ๋ฒ์งธ ์์)
result[1] # gmail.com (๋ฆฌ์คํธ์ ๋ ๋ฒ์งธ ์์
result2 = result[1].split('.') # ['gmail','com']
result2[0] # gmail -> ์ฐ๋ฆฌ๊ฐ ์๊ณ ์ถ์๋ ๊ฒ
result2[1] # com
# ํ ์ค๋ก ํ ๋ฒ์!
myemail.split('@')[1].split('.')[0]
txt = '์์ธ์-๋งํฌ๊ตฌ-๋ง์๋'
print(txt.replace('-', '>')) # '์์ธ์>๋งํฌ๊ตฌ>๋ง์๋'
๐ธ ์ธ๋ฑ์ค 0๋ถํฐ ์์!
๋ค์ ์๋ ์ซ์ ์ ๊น์ง ์ถ๋ ฅํ๋ค๊ณ ์๊ฐํ์! ๐ธ
f="abcdefghijklmnopqrstuvwxyz"
f[1] # b ํ์ด์ฌ์ ์ซ์๋ฅผ 0๋ถํฐ ์
๋๋ค
f[4:15] # efghijklmno f[4]๋ถํฐ f[14]๊น์ง, ์ด 14-4+1=11๊ฐ!
f[8:] # ijklmnopqrstuvwxyz f[8]๋ถํฐ ๋๊น์ง, ์์ 8๊ฐ ๋นผ๊ณ !
f[:7] # abcdefg ์์๋ถํฐ f[6]๊น์ง, ์์ 7๊ฐ!
f[:] # abcdefghijklmnopqrstuvwxyz ์ฒ์๋ถํฐ ๋๊น์ง
a = [1, 3, 2, 4]
print(a[3]) # 4
print(a[1:3]) # [3, 2]
print(a[-1]) # 4 (๋งจ ๋ง์ง๋ง ๊ฒ)
๐๐ป -1 ์ ๋งจ ๋ง์ง๋ง 1๊ฐ๋ผ๋๊ฑฐ ๊ธฐ์ตํ๊ธฐ
a = [1, 2, [2, 3], 0]
print(a[2]) # [2, 3]
print(a[2][0]) # 2
person = {"name":"Bob", "age": 21}
print(person["name"])
a = {"one":1, "two":2}
# ๋น ๋์
๋๋ฆฌ ๋ง๋ค๊ธฐ
a = {}
a = dict()
โ ๋์ ๋๋ฆฌ๋ ์์๊ฐ ์๊ธฐ ๋๋ฌธ์ ์ธ๋ฑ์ฑ ์ฌ์ฉ ๋ถ๊ฐ!! ๋ฆฌ์คํธ๋ ํท๊ฐ๋ฆฌ์ง ๋ง์ โ
person = {"name":"Bob", "age": 21}
person["name"] = "Robert"
print(person) # {'name': 'Robert', 'age': 21}
person["height"] = 174.8
print(person) # {'name': 'Robert', 'age': 21, 'height': 174.8}
person = {"name":"Alice", "age": 16, "scores": {"math": 81, "science": 92, "Korean": 84}}
print(person["scores"]) # {'math': 81, 'science': 92, 'Korean': 84}
print(person["scores"]["science"]) # 92
person = {"name":"Bob", "age": 21}
print("name" in person) # True
print("email" in person) # False
print("phone" not in person) # True
people = [{'name': 'bob', 'age': 20}, {'name': 'carry', 'age': 38}]
# people[0]['name']์ ๊ฐ์? 'bob'
# people[1]['name']์ ๊ฐ์? 'carry'
person = {'name': 'john', 'age': 7}
people.append(person)
# people์ ๊ฐ์? [{'name':'bob','age':20}, {'name':'carry','age':38}, {'name':'john','age':7}]
# people[2]['name']์ ๊ฐ์? 'john'
money = 5000
if money > 3800:
print("ํ์ ํ์!")
money = 2000
if money > 3800:
print("ํ์ ํ์!")
else:
print("๊ฑธ์ด๊ฐ์...")
age = 27
if age < 20:
print("์ฒญ์๋
์
๋๋ค.")
elif age < 65:
print("์ฑ์ธ์
๋๋ค.")
else:
print("๋ฌด๋ฃ๋ก ์ด์ฉํ์ธ์!")
<๋ฆฌ์คํธ ์์ ์๋๊ฑฐ ์ถ๋ ฅ>
fruits = ['์ฌ๊ณผ', '๋ฐฐ', '๊ฐ', '๊ทค']
for fruit in fruits:
print(fruit)
<20์ธ ์ด๊ณผ ์ฌ๋ ์ถ๋ ฅ>
people = [
{'name': 'bob', 'age': 20},
{'name': 'carry', 'age': 38},
{'name': 'john', 'age': 7},
{'name': 'smith', 'age': 17},
{'name': 'ben', 'age': 27},
{'name': 'bobby', 'age': 57},
{'name': 'red', 'age': 32},
{'name': 'queen', 'age': 25}
]
for person in people:
if person['age'] > 20:
print(person['name'])
for i, fruit in enumerate(fruits):
print(i, fruit)
if i == 4:
break // i = 4๋ฉด ๋น ์ ธ๋์ด
def bus_rate(age):
if age > 65:
print("๋ฌด๋ฃ๋ก ์ด์ฉํ์ธ์")
elif age > 20:
print("์ฑ์ธ์
๋๋ค.")
else:
print("์ฒญ์๋
์
๋๋ค")
bus_rate(27)
bus_rate(10)
bus_rate(72)
a = (1,2,3)
print(a[0])
-> ๋์ ๋๋ฆฌ ๋์ ๋ฆฌ์คํธ์ ํํ๋ก ๋์ ๋๋ฆฌ '๋น์ทํ๊ฒ' ๋ง๋ค์ด ์ฌ์ฉํด์ผ ํ ๋ ๋ง์ด ์ฐ์
โ๐ป ๋ถ๋ณ์ด๊ธฐ ๋๋ฌธ์, ์๋์ ๊ฐ์ ์ถ๊ฐ ์์ ๋ถ๊ฐ!!!!
a = (1,2,3)
a[0] = 99
a = [1,2,3,4,5,3,4,2,1,2,4,2,3,1,4,1,5,1]
a_set = set(a)
print(a_set) // 1,2,3,4,5 ๋ง ์ถ๋ ฅ
๐ซต๐ป '์งํฉ'์ด๋๊น ๊ต์งํฉ, ํฉ์งํฉ, ์ฐจ์งํฉ ๊ตฌํ ๊ฐ๋ฅ
a = ['์ฌ๊ณผ','๊ฐ','์๋ฐ','์ฐธ์ธ','๋ธ๊ธฐ']
b = ['์ฌ๊ณผ','๋ฉ๋ก ','์ฒญํฌ๋','ํ ๋งํ ','์ฐธ์ธ']
a_set = set(a)
b_set = set(b)
print(a_set & b_set) # ๊ต์งํฉ
print(a_set | b_set) # ํฉ์งํฉ
print(a_set - b_set) # ์ฐจ์งํฉ
scores = [
{'name':'์์','score':70},
{'name':'์ํฌ','score':65},
{'name':'๊ธฐ์ฐฌ','score':75},
{'name':'ํฌ์','score':23},
{'name':'์๊ฒฝ','score':99},
{'name':'๋ฏธ์ฃผ','score':100},
{'name':'๋ณํ','score':32}
]
for s in scores:
name = s['name']
score = str(s['score'])
print(f'{name}์ {score}์ ์
๋๋ค')
people = [
{'name': 'bob', 'age': 20},
{'name': 'carry', 'age': 38},
{'name': 'john', 'age': 7},
{'name': 'smith', 'age': 17},
{'name': 'ben', 'age': 27},
{'name': 'bobby'},
{'name': 'red', 'age': 32},
{'name': 'queen', 'age': 25}
]
for person in people:
if person['age'] > 20:
print (person['name'])
๐ ๋์ด๊ฐ 20 ์ด๊ณผ์ธ ์ฌ๋์ ๋ฝ์๋ ค๊ณ ํ์ผ๋, bobby ์๊ฒ ๋์ด์ ๋ํ ์ ๋ณด๊ฐ ์์ -> ์ค๋ฅ ๋จ๊ณ ์งํ ๋ถ๊ฐ -> try - except ์ด์ฉ
<ํด๊ฒฐ์ฑ >
for person in people:
try:
if person['age'] > 20:
print (person['name'])
except:
name = person['name']
print(f'{name} - ์๋ฌ์
๋๋ค')
// main_func ๋ถ๋ฌ์ค๊ธฐ
from main_func import *
say_hi()
// main_func ํ์ผ
def say_hi():
print('์๋
!')
(์ฐธ์ผ ๋ ๊ฐ) if (์กฐ๊ฑด) else (๊ฑฐ์ง์ผ ๋ ๊ฐ)
num = 3
result = "์ง์" if num % 2 == 0 else "ํ์"
print(f"{num}์ {result}์
๋๋ค.")
a_list = [1, 3, 2, 5, 1, 2]
b_list = []
for a in a_list:
b_list.append(a*2)
print(b_list)
โฌ๏ธ
a_list = [1, 3, 2, 5, 1, 2]
b_list = [a*2 for a in a_list]
print(b_list)
people = [
{'name': 'bob', 'age': 20},
{'name': 'carry', 'age': 38},
{'name': 'john', 'age': 7},
{'name': 'smith', 'age': 17},
{'name': 'ben', 'age': 27},
{'name': 'bobby', 'age': 57},
{'name': 'red', 'age': 32},
{'name': 'queen', 'age': 25}
]
โฌ๏ธ
def check_adult(person):
if person['age'] > 20:
return '์ฑ์ธ'
else:
return '์ฒญ์๋
'
result = map(check_adult, people)
print(list(result))
โฌ๏ธ
def check_adult(person):
return '์ฑ์ธ' if person['age'] > 20 else '์ฒญ์๋
'
result = map(check_adult, people)
print(list(result))
โฌ๏ธ
result = map(lambda x: ('์ฑ์ธ' if x['age'] > 20 else '์ฒญ์๋
'), people)
print(list(result))
result = filter(lambda x: x['age'] > 20, people)
print(list(result))
def cal(a, b):
return a + 2 * b
print(cal(3, 5))
print(cal(5, 3))
print(cal(a=3, b=5))
print(cal(b=5, a=3))
def cal2(a, b=3):
return a + 2 * b
print(cal2(4))
print(cal2(4, 2))
print(cal2(a=6))
print(cal2(a=1, b=7))
def call_names(*args):
for name in args:
print(f'{name}์ผ ๋ฐฅ๋จน์ด๋ผ~')
call_names('์ฒ ์','์์','ํฌ์ฌ')
def get_kwargs(**kwargs):
print(kwargs)
get_kwargs(name='bob')
get_kwargs(name='john', age='27')
class Monster():
hp = 100
alive = True
def damage(self, attack):
self.hp = self.hp - attack
if self.hp < 0:
self.alive = False
def status_check(self):
if self.alive:
print('์ด์์๋ค')
else:
print('์ฃฝ์๋ค')
m = Monster()
m.damage(120)
m2 = Monster()
m2.damage(90)
m.status_check()
m2.status_check()