728x90
반응형

파이썬 73

[파이썬] 기초 다지기 함수 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..

[파이썬] 기초 다지기 예제 2

자료형 살펴보기 3 : isinstance >>> isinstance(3,int) isinstance(3.14,float) isinstance('3.14',str) isinstance(True,bool) isinstance([],list) 한 문자열 접근 : indexing (거꾸로(-) 접근)) >>> 'hello'[-0] 'hello'[-1] 'hello'[-2] 'hello'[-5] 'hello'[-6] 부분 문자열 접근 : slicing >>> 'hello'[0:1] 'hello'[0:2] 'hello'[1:3] 'hello'[:3] 'hello'[3:] 'hello'[:] 'hello'[1:4:2] 'hello'[::2] 문자열 중요 함수 : len(문자열 갯수), join(문자열 합치기), s..

[파이썬] turtle을 활용하여 피젯스피너 만들기

turtle을 활용하여 피젯스피너 만들기 from turtle import* state={'turn':0} def draw_wing(color): forward(100) dot(120,color) back(100) right(120) def draw_spinner(): clear() angle=state['turn']/10 right(angle) draw_wing('red') draw_wing('green') draw_wing('blue') update() def animate(): if state['turn']>0: state['turn']-=1 draw_spinner() ontimer(animate,20) # 20ms 있다가 애니메이트 호출해줘 def flick(): state['turn']+=10 ..

[파이썬] 기초 다지기 예제 1

파이썬 맛보기 : turtle from turtle import* color('red','yellow') begin_fill() while True: forward(200) left(170) if abs(pos())=19: 'adult' 파이썬에서 판단하기 1 : if~else (Shell에서 실행) >>> birth_year=2004 if 1981=10: break 작은 동작을 묶어 대표 동작 만들기 : 함수 (Shell에서 실행) >>> coffee_price=2500 def coffee_machine(coffee_in, money_in): coffee_out=coffee_in change=money_in-coffee_price return coffee_out,change money=5000 coff..

카테고리 없음 2023.10.26
728x90
반응형