TIL-10

정진우·2021년 5월 27일
0

TIL

목록 보기
8/54
post-thumbnail
post-custom-banner

0527

튜플

#불변형
a = ('사과','감','배')

a[1] = '수박'

#TypeError: 'tuple' object does not support item assignment

집합

a = [1,3,7,9,3,6,3,1,3]

a_set = set(a)
print(a_set)    # {1, 3, 6, 7, 9}  중복 제거

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) #{'음악', '국어', '물리2', '화학1'}   차집합

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('에러입니다')

파일 불러오기

#main_func.py
def say_hi():
    print('안녕!')

def say_hi_to(name):
    print(f'{name}님 안녕하세요')
#main_test.py
from main_func import *

say_hi()
say_hi_to('영수')

한줄의 마법

num = 3

#if num % 2 == 0:
#    result = '짝수'
#else:
#    result = '홀수'

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)

b_list = [a*2 for a in a_list]

print(b_list)

map,filter,lamda식

#map  lambda
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)

#result = map(lambda person: ('성인' if person['age'] > 20 else '청소년'), people)
print(list(result))
###
#filter
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, m2를 인스턴스라고 부른다
m1.damage(150)
m1.status_check()

m2 = Monster()
m2.damage(90)
m2.status_check()
profile
프론트엔드 개발자를 꿈꾸는
post-custom-banner

0개의 댓글