티스토리 뷰
INTRO
python 기본 구성요소 숫자, 문자열, 리시트(list), 튜플(tuple), 딕셔너리(dictionary)
1. int(), float(), str(), bool()
2. list(), tuple(), dic()
1. Data-type : int(), float(), str(), bool()
- 숫자(Number) : 정수형/실수형, int()/float()
- 문자열(String) : str(), + 연산 or join() 함수 등 문자열을 합칠 수 있음
- 불(bool or boolean) : True/False, bool(), 제어문에서 활용
숫자 0은 False 나머지는 True, 빈 문자열 False 나머지는 True
python code : test3.py
#### int(), float()
a = 10 # int 정수
b = 2.63 # float 실수
c = int(10) # 정수화
d = int(a) # 정수화
e = int(b) # 정수화 float -> int
f = float(a) # 실수화 int -> float
g = float(b) # 실수화
print('''
a = {}\n\tb = {}\n\tc = {}\n\td = {}\n\te = {}\n\tf = {}\n\tg = {}
'''.format(a,b,c,d,e,f,g))
print('''
Data-type of a is {}
Data-type of b is {}
Data-type of c is {}
Data-type of d is {}
Data-type of e is {}
Data-type of f is {}
Data-type of g is {}
'''.format(type(a),type(b),type(c), \
type(d),type(e),type(f),type(g))) # \ 긴 code 행바꿈
#### str(), bool()
x = 0
y = 1
z = 2
X = ''
Y = '0'
Z = 'a'
print('''
x = {}\n\ty = {}\n\tz = {}\n\tX = {}\n\tY = {}\n\tZ = {}
Z + Y = {}
str(x) = {}
'''.format(x,y,z,X,Y,Z,Z+Y,str(x))) # Z+Y 문자열 합치기, str(x) int -> str
print('''
Data-type of x is {}
Data-type of y is {}
Data-type of z is {}
Data-type of X is {}
Data-type of Y is {}
Data-type of Z is {}
Data-type of str(x) is {}
'''.format(type(x),type(y),type(z), \
type(X),type(Y),type(Z),type(str(x)))) # str(x) int -> str 변환
print('''
boolean value of x is {}
boolean value of y is {}
boolean value of z is {}
boolean value of X is {}
boolean value of Y is {}
boolean value of Z is {}
boolean value of str(x) is {}
'''.format(bool(x),bool(y),bool(z), \
bool(X),bool(Y),bool(Z),bool(str(x)))) # str(x) False -> True
---- SublimeText python Run test3.py ----
a = 10
b = 2.63
c = 10
d = 10
e = 2
f = 10.0
g = 2.63
Data-type of a is <class 'int'>
Data-type of b is <class 'float'>
Data-type of c is <class 'int'>
Data-type of d is <class 'int'>
Data-type of e is <class 'int'>
Data-type of f is <class 'float'>
Data-type of g is <class 'float'>
x = 0
y = 1
z = 2
X =
Y = 0
Z = a
Z + Y = a0
str(x) = 0
Data-type of x is <class 'int'>
Data-type of y is <class 'int'>
Data-type of z is <class 'int'>
Data-type of X is <class 'str'>
Data-type of Y is <class 'str'>
Data-type of Z is <class 'str'>
Data-type of str(x) is <class 'str'>
boolean value of x is False
boolean value of y is True
boolean value of z is True
boolean value of X is False
boolean value of Y is True
boolean value of Z is True
boolean value of str(x) is True
***Repl Closed***
2. Data-type collection(자료형 집합) : list(), tuple(), dic()
- 리스트(list) : 숫자, 문자 등의 자료형이나 객체들의 순서가 있는 집합
- 튜플(tuple) : 리스트와 유사하나 변경할 수 없는 자료형
- 딕셔너리(dictionary) : 고유식별자(key, 키)와 일대일(1:1) 쌍을 이루는 정보(value, 값)로 구성된 리스트
python code : test4.py
#### list(), tuple(), dic()
a = [0,1,2,3,str(0),bool(0)]
b = [0,1,2,3]
c = list(range(4)) # 리스트화, range(4)는 0~3
d = list(range(0,10,2)) # range(4)는 0~10 interval 2
e = ['a','b','c'] # 문자열
f = [a,b,[c]] # 변수(객체) a를 가져옴
g = (0,1,2,3)
h = ('a',c,d) # 문자열, 객체 c,d 가져옴
i = tuple(range(4)) # 튜플화
j = tuple(a) # list -> tuple
k = {'file_name':'test', 'tail_name':4, 'extension':'.py'}
l = {'user1':('id1','pass1','name1'),'user2':('id2','pass2','name2')}
m = k.keys()
n = k.values()
o = k.items()
p = l.get('user1') # key에 해당하는 value 얻기
print('''
a = {}\n\tb = {}\n\tc = {}\n\td = {}\n\te = {}\n\tf = {}\n
g = {}\n\th = {}\n\ti = {}\n\tj = {}\n\n\tk = {}\n\tl = {}
'''.format(a,b,c,d,e,f,g,h,i,j,k,l))
print('''
Data-type of a is {}
Data-type of b is {}
Data-type of c is {}
Data-type of d is {}
Data-type of e is {}
Data-type of f is {}\n
Data-type of g is {}
Data-type of h is {}
Data-type of i is {}
Data-type of j is {}\n
Data-type of k is {}
Data-type of l is {}
'''.format(type(a),type(b),type(c),type(d),type(e),type(f), \
type(g),type(h),type(i),type(j),type(k),type(l))) # \ 긴 code 행바꿈
#### dic() handle
print('dic k info. \n{}\n{}\n{}\n\n'.format(m,n,o))
print('Data-types of dic k info. \nkeys = {}\nvalues = {}\nitems = {}\n\n'.format(type(m),type(n),type(o)))
print('user1 info. {}'.format(p))
print('Data-types of user1 info. {}'.format(type(p)))
---- SublimeText python Run test4.py ----
a = [0, 1, 2, 3, '0', False]
b = [0, 1, 2, 3]
c = [0, 1, 2, 3]
d = [0, 2, 4, 6, 8]
e = ['a', 'b', 'c']
f = [[0, 1, 2, 3, '0', False], [0, 1, 2, 3], [[0, 1, 2, 3]]]
g = (0, 1, 2, 3)
h = ('a', [0, 1, 2, 3], [0, 2, 4, 6, 8])
i = (0, 1, 2, 3)
j = (0, 1, 2, 3, '0', False)
k = {'file_name': 'test', 'tail_name': 4, 'extension': '.py'}
l = {'user1': ('id1', 'pass1', 'name1'), 'user2': ('id2', 'pass2', 'name2')}
Data-type of a is <class 'list'>
Data-type of b is <class 'list'>
Data-type of c is <class 'list'>
Data-type of d is <class 'list'>
Data-type of e is <class 'list'>
Data-type of f is <class 'list'>
Data-type of g is <class 'tuple'>
Data-type of h is <class 'tuple'>
Data-type of i is <class 'tuple'>
Data-type of j is <class 'tuple'>
Data-type of k is <class 'dict'>
Data-type of l is <class 'dict'>
dic k info.
dict_keys(['file_name', 'tail_name', 'extension'])
dict_values(['test', 4, '.py'])
dict_items([('file_name', 'test'), ('tail_name', 4), ('extension', '.py')])
Data-types of dic k info.
keys = <class 'dict_keys'>
values = <class 'dict_values'>
items = <class 'dict_items'>
user1 info. ('id1', 'pass1', 'name1')
Data-types of user1 info. <class 'tuple'>
***Repl Closed***
반응형
'python > Lecture' 카테고리의 다른 글
python 기초 #4 : Code Block(제어문 : 조건문/반복문) (0) | 2021.02.25 |
---|---|
python 기초 #3 : Data-type Handling(자료형 다루기) (0) | 2021.02.24 |
python 기초 #1 : 기본 문법 (0) | 2021.02.22 |
개발 환경 구축 : macOS/python, SublimeText Editor (0) | 2021.02.22 |
INTRO. (0) | 2021.02.22 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- ERP
- Python
- DS18B20
- Pandas
- pyserial
- Raspberry Pi
- SSH
- 라즈베리파이
- MacOS
- 확진
- r
- server
- 자가격리
- Regression
- Model
- git
- vscode
- arduino
- Templates
- DAQ
- 코로나
- 코로나19
- github
- analysis
- CSV
- COVID-19
- raspberrypi
- Django
- template
- sublime text
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함