"파이썬 입문"의 두 판 사이의 차이
DB CAFE
(→기본 문법) |
(→파이썬 제어문) |
||
192번째 줄: | 192번째 줄: | ||
== 파이썬 제어문 == | == 파이썬 제어문 == | ||
− | [ | + | === 조건별 분기 === |
+ | <source lang=python> | ||
+ | if ending_price > 10000: | ||
+ | print(“sell”) | ||
+ | elif ending_price < 8000: | ||
+ | print(“buy”) | ||
+ | else: | ||
+ | print(“hold”) | ||
+ | </source> | ||
+ | === 반복 처리 === | ||
+ | * Loop – For | ||
+ | <source lang=python> | ||
+ | >>> for i in range(0, 5): | ||
+ | print(i) | ||
+ | |||
+ | 0 | ||
+ | 1 | ||
+ | 2 | ||
+ | 3 | ||
+ | 4 | ||
+ | </source> | ||
+ | <source lang=python> | ||
+ | >>> for i in range(0, 5): | ||
+ | if i % 2 == 0: | ||
+ | print(i, end=‘ ’) | ||
+ | 0, 2, 4 | ||
+ | >>> buy_list = [‘000660’, ‘039490’] | ||
+ | >>> for code in buy_list: | ||
+ | print(“buy”, code) | ||
+ | |||
+ | buy 000660 | ||
+ | buy 039490 | ||
+ | >>> hold_list = {‘naver’: 10, ‘samsung’: 20} | ||
+ | >>> for company, num in hold_list.items(): | ||
+ | print(company, num) | ||
+ | |||
+ | naver 10 | ||
+ | samsung 20 | ||
+ | </source> | ||
+ | |||
+ | === Loop - While === | ||
+ | <source lang=python> | ||
+ | >>> i = 0 | ||
+ | >>> while i < 5: | ||
+ | print(i) | ||
+ | i += 1 | ||
+ | |||
+ | 0 | ||
+ | 1 | ||
+ | 2 | ||
+ | 3 | ||
+ | 4 | ||
+ | >>> i = 0 | ||
+ | >>> while i < 5: | ||
+ | if i % 2 == 0: | ||
+ | print(i) | ||
+ | i += 1 | ||
+ | |||
+ | 0 2 4 | ||
+ | </source> | ||
== 파이썬 모듈 임포트 방법 == | == 파이썬 모듈 임포트 방법 == |
2020년 4월 10일 (금) 13:54 판
thumb_up 추천메뉴 바로가기
- DBA { Oracle DBA 명령어 > DBA 초급 과정 > DBA 고급 과정 }
- 튜닝 { 오라클 튜닝 목록 }
- 모델링 { 데이터 모델링 가이드 }
1 데이터 타입[편집]
# int
>>> a = 3
>>> type(a)
int
- float
- str
- bool
- complex
>>> c = 3 + 4j
>>> type(c)
complex
2 데이터 구조체[편집]
2.1 리스트[편집]
2.1.1 생성[편집]
>>> names = [ ]
>>> names = [‘a’, ‘b’, ‘c’, ‘d, ‘e’]
2.1.2 len(s)[편집]
>>> names = [‘a’, ‘b’, ‘c’, ‘d, ‘e’]
>>> len(names)
5
2.1.3 indexing[편집]
>>> names = [‘a’, ‘b’, ‘c’, ‘d, ‘e’]
>>> names[0]
‘a’
>>> names[1]
‘b’
2.1.4 append[편집]
>>> names = [‘a’, ‘b’,’c’]
>>> names.append(‘d’)
>>> names
[‘a’, ‘b’, ‘c’, ‘d’]
2.1.5 insert[편집]
>>> names = [‘a’, ‘b’, ‘c’]
>>> names.insert(1, ‘e’)
>>> names
[‘a’, ‘e’, ‘b’, ‘c’]
2.1.6 comprehension[편집]
- 컴프리핸션: 리스트를 한줄의 코드로 쉽게 만들때 사용
>>> data = [3, 4, 5]
>>> float_data = [float(d) for d in data]
2.2 튜플[편집]
2.2.1 생성[편집]
>>> names = (‘a’, ‘b’, ‘c’, ‘d, ‘e’)
2.2.2 len(s)[편집]
>>> names = (‘a’, ‘b’, ‘c’, ‘d, ‘e’)
>>> len(names)
5
2.2.3 indexing[편집]
>>> names = (‘a’, ‘b’, ‘c’, ‘d, ‘e’)
>>> names[0]
‘a’
>>> names[1]
‘b’
2.3 딕셔너리[편집]
2.3.1 생성[편집]
>>> cur_price = { }
2.3.2 insert[편집]
>>> cur_price[‘samsung’] = 10000
>>> cur_price
{‘samsung’: 10000}
2.3.3 indexing[편집]
>>> cur_price[‘samsung’]
>>> 10000
2.3.4 delete[편집]
>>> del cur_price[‘samsung’]
>>> cur_price{}
2.3.5 key, value[편집]
>>> cur_price.keys()
dict_keys([‘samsung’])
>>> cur_price.values()
dict_values([10000])
3 문자열 조작 함수[편집]
3.2 indexing[편집]
>>> mystring[0]
‘h’
3.3 slicing[편집]
>>> mystring[0:5]
‘hello’
>>> mystring[6:]
‘world’
3.4 문자열.split(S)[편집]
>>> companies = “yahoo google”
>>> companies.split(‘ ’)
[‘yahoo’, ‘google’]
3.5 in[편집]
>>> ‘google’ in companies
True
3.6 combining[편집]
>>> s1 = “hello”
>>> s2 = “world”
>>> s3 = s1 + ‘ ’ + s2
>>> s3
“hello world”
3.7 replace[편집]
>>> a = “yahoo;google”
>>> new_a = a.replace(‘;’, ‘-’)
>>> new_a
“yahoo-google”
3.8 index[편집]
>>> s = “yahoo google”
>>> s.index(“google”)
6
3.9 문자열.find(x)[편집]
>>> s = “yahoo google”
>>> s.find(“google”)
6
3.10 stripping[편집]
>>> a = “ yahoo ”
>>> new_a = a.strip()
>>> new_a
“yahoo”
4 파이썬 제어문[편집]
4.1 조건별 분기[편집]
if ending_price > 10000:
print(“sell”)
elif ending_price < 8000:
print(“buy”)
else:
print(“hold”)
4.2 반복 처리[편집]
- Loop – For
>>> for i in range(0, 5):
print(i)
0
1
2
3
4
>>> for i in range(0, 5):
if i % 2 == 0:
print(i, end=‘ ’)
0, 2, 4
>>> buy_list = [‘000660’, ‘039490’]
>>> for code in buy_list:
print(“buy”, code)
buy 000660
buy 039490
>>> hold_list = {‘naver’: 10, ‘samsung’: 20}
>>> for company, num in hold_list.items():
print(company, num)
naver 10
samsung 20
4.3 Loop - While[편집]
>>> i = 0
>>> while i < 5:
print(i)
i += 1
0
1
2
3
4
>>> i = 0
>>> while i < 5:
if i % 2 == 0:
print(i)
i += 1
0 2 4
5 파이썬 모듈 임포트 방법[편집]
- import os
: 일반적인 import 방식
- from os import xxx
: xxx 함수만 import 하는 방식
- from os import *
: 모든 함수를 import 하는 방식