728x90
반응형

파이썬공부 36

[파이썬] 기초 다지기 클래스(class)

class 정의하기 객체 사용하기 (shell에서 실행하기) class Student: def __init__(inst,name,age,gender): inst.name=name inst.age=age inst.gender=gender def info(inst): return inst.name,inst.age,inst.gender student_1=Student('일지매',25,'남자') student_1.info() 클래스 활용하기 1 (shell에서 실행하기) class RCCar: def __init__(inst): inst.dir='stop' inst.spd=0 def go_forward(inst): inst.dir='forward' def go_backward(inst): inst.dir='ba..

[파이썬] 기초 다지기 함수 2

함수와 for 문 2 : zip(리스트 항목별 묶기, 일회용) (shell에서 실행하기) L1=['americano','caffe latte','espresso'] L2=[1500,2500,1600] z=zip(L1,L2) for coffee,price in z: coffee,price for coffee,price in z: coffee,price 무명 함수 : lambda(한줄짜리 함수) (shell에서 실행하기) f=lambda a,b:a*b f(10,20) f2=lambda a:a**2 f2(3) f3=lambda a:a>2 f3(3) lambda, map, for~in 문의 활용(map은 일회용) (shell에서 실행하기) f=lambda a:a**2 L=[1,2,3,4] for m in map..

[파이썬] 기초 다지기 함수 1

둘 이상의 함수 반환 값 (shell에서 실행하기) def k(a,b): q=a//b r=a%b return q,r a,b=19,5 q,r=k(a,b) q,r 위치 기반 인자, 키워드 기반 인자 (shell에서 실행하기) def func(a,b): a,b func(10,20) func('hello','world') func(a=3000,b=100) func(b=100,a=3000) 함수와 커피 자판기 (shell에서 실행하기) menu={'americano':1500, 'caffe latte':2500, 'espresso':1500 } totalMoney=0 def coffeeMachine(moneyIn,coffeeIn): change,coffeeOut=moneyIn,None if coffeeIn in ..

[파이썬] 기초 다지기 dict(딕셔너리 함수)

dict 생성하기 (shell에서 실행하기) shopping={} shopping['apple']=2 shopping['pear']=3 shopping['hanrabong']=2 shopping['pine apple']=1 shopping dict 항목 읽기 (shell에서 실행하기) shopping['apple'] shopping['pear'] shopping['mango'] shopping.get('mango','does not exist') shopping.get('hanrabong','does not exist') dict 주요함수 (shell에서 실행하기) shopping={'apple':2, 'pear':3, 'hanrabong':2, 'pine apple':1} shopping.keys() ..

[파이썬] 기초 다지기 tuple, dict, set와 packing operator

tuple, dict, set(집합, 중복된 것을 정리) 소개 (shell에서 실행하기) t={1,2,3,4} d={'a':1,'b':2,'c':3,'d':4} s={1,2,2,3,3,3,4,4,4,4} t d s 튜플, 사전, 집합 : 내포와 생성자 (shell에서 실행하기) t=(i for i in range(5)) d={('key'+str(i)):i for i in range(5)} s={i for i in range(5)} t d s type(t) type(d) type(s) 튜플의 기본적인 활용(콤마(,)로 분리된 항목들 / 병렬대입 / 암시적인 대입(함수를 사용한 인자) (shell에서 실행하기) a=(1,2,3,4) a type(a) b=(1) b type(b) c=(1,) type(c) 튜..

[파이썬] 기초 다지기 리스트(list), 임의 문자열 생성

부분 항목 접근 : slicing (shell에서 실행하기) [0,1,2,3,4][0:1] [0,1,2,3,4][0:2] [0,1,2,3,4][1:3] [0,1,2,3,4][:3] [0,1,2,3,4][3:] [0,1,2,3,4][:] [0,1,2,3,4][1:4:2] [0,1,2,3,4][::2] 리스트 주요 함수 : len, sort, reverse, random, shuffle (shell에서 실행하기) import numpy as np np.random.seed(42) iList=[*np.random.randint(0,100,5)] iList len(iList) iList.sort() iList import random random.shuffle(iList) iList 임의 숫자 생성 : rand..

[파이썬] 기초 다지기 if 문

if 문 냉장고에서 사과 찾기 1 (shell에서 실행하기) fridge=["apple","pear","mandarine","banana"] where=fridge.index('apple') #index where apple=fridge.pop(where) #pop apple 냉장고에서 사과 찾기 2 (shell에서 실행하기) fridge=["apple","pear","mandarine","banana"] if 'apple' in fridge: where=fridge.index('apple') apple=fridge.pop(where) 'eat'+apple BMI 계산기 height=float(input('Enter your height in centimeters:')) weight=float(input..

728x90
반응형