_won_
wonprogrammer
_won_
전체 방문자
오늘
어제
  • 분류 전체보기
    • woncoding
      • TIL
      • WIL
    • source Code
      • Python
      • Programmers
      • BAEKJOON

블로그 메뉴

  • 방명록

티스토리

Github · Wonprogrammer
hELLO · Designed By 정상우.
_won_

wonprogrammer

source Code/Python

Python | py_basic_week1

2022. 9. 2. 11:26

- py_basic_week1 [ 튜플 / 집합 / f-string / 예외처리 / 파일분리 / map,lambda,filter / class ] 

 

1. [set.py]

student_a = ['물리2','국어','수학1','음악','화학1','화학2','체육']
student_b = ['물리1','수학1','미술','화학2','체육']

set_a = set(student_a)
set_b = set(student_b)

print(set_a - set_b)

 

 

2. [f_string.py]

scores = [
    {'name':'영수','score':70},
    {'name':'영희','score':65},
    {'name':'기찬','score':75},
    {'name':'희수','score':23},
    {'name':'서경','score':99},
    {'name':'미주','score':100},
    {'name':'병태','score':32}
]

for i in scores :
    name = i['name']
    score = i['score']
    print(name + '의 점수는' + str(score) + '점 입니다.')

    print(f'{name}의 점수는 {score}점!')

 

 

 

 

3. [try_except.py]

people = [
    {'name': 'bob', 'age': 20},
    {'name': 'carry', 'age': 38},
    {'name': 'john', 'age': 7},
    {'name': 'smith', 'age': 17},
    {'name': 'ben'},
    {'name': 'bobby', 'age': 57},
    {'name': 'red', 'age': 32},
    {'name': 'queen', 'age': 25}
]

for i in people :
    try :
        if i['age'] > 20 :
            print(i['name'])
    except:
        print(i['name'] + ' : 에러입니다.')

 

 

 

 

4. [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('영수')

 

 

 

 

 

5. [short_if.py]

num = 3

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

short = ('짝수' if num % 2 == 0 else '홀수')

print(f'{num}은 {result} 입니다')

print(f'{num}은 {short} 입니다')

 

[short_for]

a = [1,2,3,4,5,6]
a_list = [1,2,3,4,5,6]

b = []
c = []

for i in a :
    b.append(i*2)

print(b)

c = [i*2 for i in a_list]

print(c)

 

 

 

 

 

6. [map_lambda_filter.py]

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 '청소년'

# map
result = map(check_adult, people)  
print(list(result))



# filter
result_filter = filter(check_adult, people)  👉 if/else 모두 True 값이기 때문에 모든 딕셔너리가 출력된다 
print(list(result_filter))  👉 '성인'도 '청소년'도 모두 유효한 값



------------------------------------------------------------------------------------

👉 정의된 def 함수가 아닌 lambda를 이용해 조건 처리


# lambda
result_lambda = map(lambda person : ('성인' if person['age']>20 else '청소년'), people)
print(list(result_lambda))

result_lambda_x = map(lambda x : x['age']>20 , people)    👉 통상적으로 x 가 쓰인다.
print(list(result_lambda_x))




# filter
result_filter = filter(lambda person : person['age']>20 , people)
print(list(result_filter))

 

 

 

 

 

 

7. [class.py]

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('죽었다')



m1 = Monster()
m1.damage(150)
m1.status_check()

m2 = Monster()
m2.damage(80)
m2.status_check()

'source Code > Python' 카테고리의 다른 글

Python | py_basic_week1  (0) 2022.09.01
Python | poo_game.py  (0) 2022.09.01
Python | memory_game.py  (0) 2022.08.26
Python | weapon_game.py  (0) 2022.08.25
Python | calculator.py  (0) 2022.08.18
    'source Code/Python' 카테고리의 다른 글
    • Python | py_basic_week1
    • Python | poo_game.py
    • Python | memory_game.py
    • Python | weapon_game.py
    _won_
    _won_
    Coding Practice blog

    티스토리툴바