a = 23 # 10진 정수 b = 023 # 8진 정수 c = 0x23 # 16진 정수 print type(a), type(b), type(c) print a, b, c import sys sys.maxint # 최대 정수 값 확인 a = 1.2 b = 3.5e3 c = -0.2e-4 print type(a), type(b), type(c) print a, b, c e, f, g = 3.14, 2.16e-9, 3E220 print e, f, g h1 = 123456789012345678901234567890L # 마지막에 L을 붙여서 명시적으로 long 형이라고 알려도 되고 print type(h1) print h1 * h1 print h2 = 123456789012345678901234567890 # L을 붙이지 않아도 int형이 담을 수 있는 수치를 초과하면 자동으로 long형이 된다. print type(h2) print h2 * h2 print h3 = 123L print type(h3) print h4 = 123 print type(h4) octal = 01234L # 8진법 long형 print octal octal hexa = 0x123456789L # 16진법 long형 hexa 123456789012345678890 # 자동 long형 변환 2 ** 3 ** 4 # print 없이 출력할 때에는 L를 함께 표기함 print 2 ** 3 ** 4 # print와 함께 출력할 때에는 L 표기 하지 않음 1.1 from decimal import * Decimal(1234) Decimal('1234') from decimal import * Decimal(1.1) Decimal('1.1') from decimal import * Decimal('Infinity') # 양의 무한대 표현 Decimal('-Infinity') # 음의 무한대 표현 Decimal("NaN") # Not a number 표현 e = 0.0 # 기본 자료형인 float형 변수 e 생성 및 0.0 값으로 초기화 for k in range(10000): # 0부터 9999 까지 만번 반복 e += 0.0001 # 0.0001을 누적 print e # print로는 1.0이 출력됨 e # 하지만, 실제 저장되어 있는 값에는 오차 존재 from decimal import * e2 = Decimal('0.0') # 기본 자료형인 float형 변수 e 생성 및 0.0 값으로 초기화 delta = Decimal('0.0001') # 누적하려고 하는 0.0001 값을 Decimal 객체인 delta 변수로 생성 for k in range(10000): # 0부터 9999 까지 만번 반복 e2 += delta # delta를 누적 print e2 # print로는 1.0이 출력됨 e2 # 실제 저장되어 있는 것은 오차 없이 1.0 값을 지닌 Decimal 객체 - Decimal 객체에 대해 각종 연산자들이 모두 수행됨 - 오차가 거의 없는 연산이 가능함 print a ** 2 a ** b a = Decimal("1.2") print a + 2 print 2 + a a = Decimal("1.2") a + 1.5 import math, cmath d = Decimal('123.456789012345') print math.sqrt(d) print cmath.sqrt(-d) print d.sqrt() # Decimal 객체 자체가 sqrt() 함수 지니고 있음 print d.quantize(Decimal('.01'), rounding = ROUND_DOWN) # 해당 자리수 미만 내림 (제거) a1 = Decimal((1, (1, 4, 7, 5), 0)) a2 = Decimal((1, (1, 4, 7, 5), -1)) a3 = Decimal((1, (1, 4, 7, 5), -2)) a4 = Decimal((1, (1, 4, 7, 5), -3)) print a1 print a2 print a3 print a4 print b1 = Decimal((0, (1, 4, 7, 5), 0)) b2 = Decimal((0, (1, 4, 7, 5), -1)) b3 = Decimal((0, (1, 4, 7, 5), -2)) b4 = Decimal((0, (1, 4, 7, 5), -3)) print b1 print b2 print b3 print b4 getcontext() Decimal(1) / Decimal(7) getcontext().prec = 9 Decimal(1) / Decimal(7) ExtendedContext BasicContext DefaultContext setcontext(ExtendedContext) Decimal(1)/ Decimal(7) setcontext(DefaultContext) Decimal(1)/ Decimal(7) print 2 ** 3 print 2 ** 3 ** 2 print (2 ** 3) ** 2 print 5 % 2 print -5 % 2 5 ** -2.0 print 3+5 print 3+5.0 # 정수 + 실수의 결과는 실수 print 5 / 2.0 # 정수 / 실수의 결과는 실수 print 5 / 2 a = 5 / 3 b = 5 % 3 print a, b print divmod(5,3) print 5 % 3 print 5 % -3 print -5 % 3 print -5 % -3 print 5 / 3 print 5 / -3 print -5 / 3 print -5 / -3 print 3 // 4 print 3.0 // 4.0 print -3 // 4 print -3 // 4.0 print 3 / 4 print 3.0 / 4.0 print -3 / 4 print -3 / 4.0 print -7/4 #단항 연산자(-)의 우선순위가 이항 연산자(/)의 우선순위보다 높다 (-7을 4로 나눈다). print -(7/4) #7/4의 결과에 단항 연산을 수행함 4 * -5 print 2 + 3 * 4 print (2 + 3) * 4 print ++3 print --3 print -+3 print +-3 print 4 / 2 * 2 print 2 ** 3 ** 4 # ** 연산자의 결합순서는 오른쪽에서 왼쪽으로! print (2 ** 3) ** 4 print 6 == 9 print 6 != 9 print 1 > 3 print 4 <= 5 a = 5 b = 10 print a < b 0 < a < b 0 < a and a < b print 'abcd' > 'abd' # 문자열, 튜플, 리스트의 관계 연산 비교는 일반 사전 순서. 사전에서 앞에 나오는 값이 작은 값으로 평가됨 print (1, 2, 4) < (2, 1, 0) print [1, 3, 2] == [1, 2, 3] print 9999999999999999999999L < 'abc' print {3:2} < [1,2,3] < (1,2,3) L = [1,2,3, 'abc', 'a', 'z', (1,2,3), [1,2,3], {1:2}, ['abc']] L.sort() L x = [1,2,3] y = [1,2,3] z = y print x == y print x == z print x is y print x is z print y is z Y[1] = 100 print X print Y print Z a = 20 b = 30 a > 10 and b < 50 print True + 1 print False + 1 print False * 75 print True * 75 print bool(0) # 정수 0은 거짓 print bool(1) print bool(100) print bool(-100) print print bool(0.0) # 실수 0.0은 거짓 print bool(0.1) print bool('abc') print bool('') print print bool([]) # 공 리스트는 거짓 print bool([1,2,3]) print print bool(()) # 공 튜플은 거짓 print bool((1,2,3)) print print bool({}) # 공 사전은 거짓 print bool({1:2}) print print bool(None) # None 객체는 거짓 print 1 and 1 print 1 and 0 print 0 or 0 print 1 or 0 print print [] or 1 # [] 거짓 print [] or () # [], () 거짓 print [] and 1 # [] 거짓이므로 1은 참조할 필요 없음 print 1 and 2 print 1 or 2 print print [[]] or 1 # [[]] 참으로 간주 print [{}] or 1 # [)] 참으로 간주 print '' or 1 # 빈 문자열('')은 거짓 print not(True) print not(1 and 2) print not('' or 1) print ~5 #"0000 0101" -> "1111 1010" print ~-1 #"1111 1111" -> "0000 0000" print ~-128 #"1000 0000" -> "0111 1111" a = 3 # "0000 0011" print a << 2 # "0000 1100", 오른쪽에는 0으로 채워짐 (a * 2 ** 2 와 동일) print 1 << 128 # 롱형으로 자동변환 (1 * 2 ** 128 과 동일) a = 4 # "0000 0100" print a >> 1 # "0000 0010", 왼쪽에도 0으로 채워짐 a = -4 # "1111 1100" print a >> 1 # "1111 1110", 음수의 경우 왼쪽에는 1로 채워짐 a = 3 print a & 2 #0000 0011 bit_and 0000 0010 print a | 8 #0000 0011 bit_or 0000 1000 print 0x0f ^ 0x06 #0000 1111 exclusive_or 0000 0110 print abs(-3) print int(3.141592) print int(-3.1415) print long(3) print float(5) print complex(3.4, 5) print complex(6) print divmod(5, 2) print print pow (2, 3) print pow(2.3, 3.5) import math print math.pi print math.e print math.sin(1.0) # 1.0 라디안에 대한 사인 값 print math.sqrt(2) # 제곱근 r = 5.0 # 반지름 a = math.pi * r * r # 면적 degree = 60.0 rad = math.pi * degree / 180.0 # 각도를 라디안으로 변환 print math.sin(rad), math.cos(rad), math.tan(rad) #sin, cos, tan import math print math.pi print math.sin(0.1)