# int
year = 2014
month = 10
print("year, month:", year, month)
# 8, 16, 2 진수 표현
print("0o10, 0x10, 0b10:", 0o10, 0x10, 0b10)
# 10진수를 8, 16, 2진수로 변환
print("oct(10):", oct(10))
print("hex(10):", hex(10))
print("bin(10):", bin(10))
# 정수형 표현 int
print("type(2**31):", type(2**31))
# 실수형 표현 float
print("type(3.14):", type(3.14))
print("type(314e-2):", type(314e-2))
# 복소수, 켤레복소수
x = 3 - 4j
print("type(x):", type(x))
# 원의 넓이
r=2
circle_area = 3.14*(r**2)
print("circle_area:", circle_area)
# 삼각형의 넓이
x=3
y=4
triangle_area = x*y/2
print("triangle_area:", triangle_area)
# 나누기, 정수 나누기
print("3/2:", 3/2)
print("3//2:", 3//2)
# 문자열
print(type("sting"))
print(type('string'))
print(type("""string"""))
print(type('''string'''))
print("""
테스트 스트링
줄바꿈도 저장되고
탭도 저장되고
""")
print("\t탭\n다음줄")
print(r"r은 raw문자열 \t탭\n다음줄")
print('hello.' 'eunmi')
print('hello.'+'eunmi')
print('hi!'*3)
# 인덱싱, 배열처럼 0부터 시작, 치환은 안됨
a="python"
print("a:", a)
print("a[0]:", a[0])
print("a[5]:", a[5])
# error
# a[0] = "a"
# TypeError: 'str' object does not support item assignment
# a[0] = "a"
# print(a[0])
# 슬라이싱
print("a[0:1]=", a[0:1])
print("a[1:4]=", a[1:4])
print("a[:2]=", a[:2])
print("a[-2:]=", a[-2:])
print("a[:]=", a[:])
print("a[::2]=", a[::2])
# 생성자
print("str(3.14)=", str(3.14))
print("int(3)=", int(3))
print("float(3.14)=", float(3.14))
# 유니코드 (기본적으로 모두 유니코드, 인코딩이 있는 문자는 byte로 표현
print("type('가'):", type('가'))
print("'가'.encode('utf_8'):", '가'.encode('utf_8'))
print("type('가'.encode('utf_8')):", type('가'.encode('utf_8')))
print("ord('s'):", ord('s'))
print("chr(115):", chr(115))
# 리스트, 순서 있음
colors = ['red', 'green', 'blue']
print("colors:", colors)
print("type(colors):", type(colors))
colors.append('gold')
print("colors:", colors)
colors.insert(1, 'yellow')
print("colors:", colors)
colors.extend(['white', 'black', 'gray'])
print("colors:", colors)
colors += ['red']
print("colors:", colors)
colors += 'red'
print("colors:", colors)
print("colors.index('red'):", colors.index('red'))
print("colors.index('red', 1):", colors.index('red', 1))
# print(colors.index('red', 1, 5))
# Traceback (most recent call last):
# File "/Users/eunguru/Source/Python/Type_Operator/src/Type_Operator.py", line 108, in <module>
# print(colors.index('red', 1, 5))
# ValueError: 'red' is not in list
print("colors.count('red'):", colors.count('red'))
colors.pop()
colors.pop()
colors.pop()
print("colors:", colors)
colors.pop(1)
print("colors:", colors)
colors.remove('red')
print("colors:", colors)
colors.sort()
print("colors:", colors)
colors.reverse()
print("colors:", colors)
def mysort(x): return x[-1]
colors.sort(key=mysort)
print("colors:", colors)
colors.sort(key=mysort, reverse=True)
print("colors:", colors)
# 세트, 순서 없음
a = {1, 2, 3}
b = {3, 4, 5}
print("a:", a)
print("b:", b)
print("type(a):", type(a))
print("a.union(b):", a.union(b))
print("a|b:", a|b)
print("a.intersection(b):", a.intersection(b))
print("a&b:", a&b)
print("a-b:", a-b)
# 튜플, 읽기 전용
t = (1, 2, 3)
print("t:", t)
print("type(t):", type(t))
print("t.count(1):", t.count(1))
print("t.index(2):", t.index(2))
a, b = 1, 2
print("a, b:", a, b)
(a, b) = (1, 2)
print("a, b:", a, b)
a, b = b, a
print("a, b:", a, b)
# list(), set(), tuple()
a = (1, 2, 3)
print("a:", a, "type(a):", type(a))
b = list(a)
print("b:", b, "type(b):", type(b))
c = set(b)
print("c:", c, "type(c):", type(c))
d = tuple(c)
print("d:", d, "type(d):", type(d))
# in 연산자
print("1 in a:", (1 in a))
print("2 in b:", (2 in b))
print("3 in c:", (3 in c))
print("4 in c:", (4 in c))
# 사전(dictionary), key:value pair
d = dict(a=1, b=3, c=5)
print("d:", d)
print("type(d):", type(d))
color = {"apple":"red", "banana":"yellow"}
print("color:", color)
print("color[\"apple\"]:", color["apple"])
# print("color[0]:", color[0])
#Traceback (most recent call last):
#File "/Users/eunguru/Source/Python/Type_Operator/src/Type_Operator.py", line 179, in <module>
#print("color[0]:", color[0])
#KeyError: 0
color["cherry"] = "red"
print("color:", color)
color["apple"] = "green"
print("color:", color)
for c in color.items():
print(c)
for k, v in color.items():
print(k, v)
for k in color.keys():
print(k)
for v in color.values():
print(v)
print("type(color.keys()):", type(color.keys()))
print("type(color.values()):", type(color.values()))
print("type(color.items()):", type(color.items()))
print(color)
print("list(color.keys()):", list(color.keys()))
del color["cherry"]
print("color:", color)
color.clear()
print("color:", color)
s = {'age':40.5, 'job':[1,2,3], 'name':{'kim':2, 'Cho':1}, 'num':(3,4,5), 'set':{1,2,3}}
print("s:", s)
print("type(s):", type(s))
# 부울
isRight = False
print("isRight:", isRight, "type(isRight):", type(isRight))
print("1 < 2:", 1<2)
print("1 != 2:", 1!=2)
print("1 is not 2:", 1 is not 2)
print("1 == 2:", 1==2)
print("1 is 2:", 1 is 2)
print("True and True:", True and True)
print("True & True:", True & True)
print("True & False:", True & False)
print("True or False:", True or False)
print("True | False:", True | False)
print("not False:", not False)
print("bool(0):", bool(0))
print("bool(-1):", bool(-1))
print("bool('test'):", bool('test'))
print("bool(None):", bool(None))
# 얕은 복사
a = [1,2,3]
b = a
a[0] = 38
print("a:", a, "b:", b)
print("id(a):", id(a), "id(b):", id(b))
# 깊은 복사
c = [1,2,3]
d = c[:]
print("c:", c, "d:", d)
print("id(c):", id(c), "id(d):", id(d))
c[0] = 38
print("c:", c, "d:", d)
year, month: 2014 10
0o10, 0x10, 0b10: 8 16 2
oct(10): 0o12
hex(10): 0xa
bin(10): 0b1010
type(2**31): <class 'int'>
type(3.14): <class 'float'>
type(314e-2): <class 'float'>
type(x): <class 'complex'>
circle_area: 12.56
triangle_area: 6.0
3/2: 1.5
3//2: 1
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
테스트 스트링
줄바꿈도 저장되고
탭도 저장되고
탭
다음줄
r은 raw문자열 \t탭\n다음줄
hello.eunmi
hello.eunmi
hi!hi!hi!
a: python
a[0]: p
a[5]: n
a[0:1]= p
a[1:4]= yth
a[:2]= py
a[-2:]= on
a[:]= python
a[::2]= pto
str(3.14)= 3.14
int(3)= 3
float(3.14)= 3.14
type('가'): <class 'str'>
'가'.encode('utf_8'): b'\xea\xb0\x80'
type('가'.encode('utf_8')): <class 'bytes'>
ord('s'): 115
chr(115): s
colors: ['red', 'green', 'blue']
type(colors): <class 'list'>
colors: ['red', 'green', 'blue', 'gold']
colors: ['red', 'yellow', 'green', 'blue', 'gold']
colors: ['red', 'yellow', 'green', 'blue', 'gold', 'white', 'black', 'gray']
colors: ['red', 'yellow', 'green', 'blue', 'gold', 'white', 'black', 'gray', 'red']
colors: ['red', 'yellow', 'green', 'blue', 'gold', 'white', 'black', 'gray', 'red', 'r', 'e', 'd']
colors.index('red'): 0
colors.index('red', 1): 8
colors.count('red'): 2
colors: ['red', 'yellow', 'green', 'blue', 'gold', 'white', 'black', 'gray', 'red']
colors: ['red', 'green', 'blue', 'gold', 'white', 'black', 'gray', 'red']
colors: ['green', 'blue', 'gold', 'white', 'black', 'gray', 'red']
colors: ['black', 'blue', 'gold', 'gray', 'green', 'red', 'white']
colors: ['white', 'red', 'green', 'gray', 'gold', 'blue', 'black']
colors: ['red', 'gold', 'white', 'blue', 'black', 'green', 'gray']
colors: ['gray', 'green', 'black', 'white', 'blue', 'red', 'gold']
a: {1, 2, 3}
b: {3, 4, 5}
type(a): <class 'set'>
a.union(b): {1, 2, 3, 4, 5}
a|b: {1, 2, 3, 4, 5}
a.intersection(b): {3}
a&b: {3}
a-b: {1, 2}
t: (1, 2, 3)
type(t): <class 'tuple'>
t.count(1): 1
t.index(2): 1
a, b: 1 2
a, b: 1 2
a, b: 2 1
a: (1, 2, 3) type(a): <class 'tuple'>
b: [1, 2, 3] type(b): <class 'list'>
c: {1, 2, 3} type(c): <class 'set'>
d: (1, 2, 3) type(d): <class 'tuple'>
1 in a: True
2 in b: True
3 in c: True
4 in c: False
d: {'c': 5, 'a': 1, 'b': 3}
type(d): <class 'dict'>
color: {'apple': 'red', 'banana': 'yellow'}
color["apple"]: red
color: {'apple': 'red', 'cherry': 'red', 'banana': 'yellow'}
color: {'apple': 'green', 'cherry': 'red', 'banana': 'yellow'}
('apple', 'green')
('cherry', 'red')
('banana', 'yellow')
apple green
cherry red
banana yellow
apple
cherry
banana
green
red
yellow
type(color.keys()): <class 'dict_keys'>
type(color.values()): <class 'dict_values'>
type(color.items()): <class 'dict_items'>
{'apple': 'green', 'cherry': 'red', 'banana': 'yellow'}
list(color.keys()): ['apple', 'cherry', 'banana']
color: {'apple': 'green', 'banana': 'yellow'}
color: {}
s: {'num': (3, 4, 5), 'age': 40.5, 'job': [1, 2, 3], 'name': {'Cho': 1, 'kim': 2}, 'set': {1, 2, 3}}
type(s): <class 'dict'>
isRight: False type(isRight): <class 'bool'>
1 < 2: True
1 != 2: True
1 is not 2: True
1 == 2: False
1 is 2: False
True and True: True
True & True: True
True & False: False
True or False: True
True | False: True
not False: True
bool(0): False
bool(-1): True
bool('test'): True
bool(None): False
a: [38, 2, 3] b: [38, 2, 3]
id(a): 4302873992 id(b): 4302873992
c: [1, 2, 3] d: [1, 2, 3]
id(c): 4302937288 id(d): 4302874632
c: [38, 2, 3] d: [1, 2, 3]
'컴&프로그래밍 > Python' 카테고리의 다른 글
5. 클래스 (0) | 2014.12.20 |
---|---|
4. 제어 (0) | 2014.12.19 |
Google Python Tutorial - Basic Python Exercises #2 List (0) | 2014.12.11 |
Google Python Tutorial - Basic Python Exercises #1 String (0) | 2014.12.05 |
Python 2.x에서 3.x로 변경 (0) | 2014.12.03 |
튜플 자료형 특이사항 (0) | 2014.10.19 |
Hello Python (0) | 2014.10.15 |
Mac에서 python 3. x 버전 사용 하기 (3) | 2014.10.12 |
Upgrading Python 2.7 to 3.3 on MAC OS X (0) | 2013.12.03 |