0527
튜플
a = ('사과','감','배')
a[1] = '수박'
집합
a = [1,3,7,9,3,6,3,1,3]
a_set = set(a)
print(a_set)
a = ['사과', '감','배','수박','딸기']
b = ['배','사과','포도','참외','수박']
a_set = set(a)
b_set = set(b)
print(a_set & b_set)
print(a_set | b_set)
student_a = ['물리2','국어','수학1','음악','화학1','화학2','체육']
student_b = ['물리1','수학1','미술','화학2','체육']
a_set = set(student_a)
b_set = set(student_b)
print(a_set - b_set)
f-string
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 = s['score']
print(name + '의 점수는 ' + str(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:
try:
if person['age'] > 20:
print(person['name'])
except:
print('에러입니다')
파일 불러오기
def say_hi():
print('안녕!')
def say_hi_to(name):
print(f'{name}님 안녕하세요')
from main_func import *
say_hi()
say_hi_to('영수')
한줄의 마법
num = 3
result = ('짝수' if num % 2 == 0 else '홀수')
print(f'{num}은 {result}입니다')
a_list = [1,3,2,5,1,2]
b_list = [a*2 for a in a_list]
print(b_list)
map,filter,lamda식
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))
result = filter(lambda x:x['age'] > 20, people)
print(list(result))
클래스
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 == True:
print('살았다!')
else:
print('죽었다!')
m1 = Monster()
m1.damage(150)
m1.status_check()
m2 = Monster()
m2.damage(90)
m2.status_check()