#!/usr/bin/env python # coding: utf-8 # *** # *** # # 3. 내장 자료형의 기초 # *** # *** # *** # ## 1 수치 자료형 # *** # ### 1-1 정수형 상수 # In[2]: a = 23 # 10진 정수 # b = 023 b = 0o23 # 8진 정수 c = 0x23 # 16진 정수 print(type(a), type(b), type(c)) print(a, b, c) # In[1]: import sys # print sys.maxint <-- python2 에서만 동작 print(sys.maxsize) # 최대 정수 값 확인 # https://docs.python.org/3/library/sys.html#sys.maxsize # In[2]: a = 9223372036854775808 print(a) print(type(a)) # ### 1-2 실수형 상수 # In[13]: a = 1.2 b = 3.5e3 c = -0.2e-4 print(type(a), type(b), type(c)) print() print(a) print(b) print(c) print() print("{:5.3f}".format(a)) print("{:5.3f}".format(b)) print("{:10.8f}".format(c)) # ### 1-3 롱형 상수 # - Python3부터는 더 이상 존재하지 않음
# # Python 2 had separate int and long types for non-floating-point numbers. An int could not be any larger than sys.maxint, which varied by platform. Longs were defined by appending an L to the end of the number, and they could be, well, longer than ints. In Python 3, there is only one integer type, called int, which mostly behaves like the long type in Python 2. Since there are no longer two types, there is no need for special syntax to distinguish them.
#
# 참고: http://www.diveintopython3.net/porting-code-to-python-3-with-2to3.html # In[15]: h1 = 123456789012345678901234567890 # Python2에서는 마지막에 L을 붙여서 명시적으로 long 형이라고 알려줬다. print(type(h1)) print(h1 * h1) print() h2 = 123456789012345678901234567890 # Python2에서는 L을 붙이지 않아도 int형이 담을 수 있는 수치(sys.maxint)를 초과하면 자동으로 long형이 됐다. print(type(h2)) print(h2 * h2) print() h3 = 123 print(type(h3)) print() h4 = 123 print(type(h4)) # ### 1-4 복소수형 상수 # In[17]: a = 10 + 20j print(a) print(type(a)) print() b = 10 + 5j print(a + b) # ### 1-5 수치 자료형의 치환 # - 아래 예에서는 x가 지니고 있는 1의 값이 변경되는 것이 아니라 새로운 객체 2로 레퍼런스를 변경하는 것임 # In[17]: x = 1 x = 2 # ![image1](../images/referenceChangeNumerical.png) # ### 1-6 수치 연산과 관련된 내장 함수 # In[3]: print(abs(-3)) print(int(3.141592)) print(int(-3.1415)) # print long(3) # In Python 3, the old long() function no longer exists print(int(3)) print(float(5)) print(complex(3.4, 5)) print(complex(6)) # In[18]: print(divmod(5, 2)) print() print(pow(2, 3)) print(pow(2.3, 3.5)) # ### 1-7 math 모듈의 수치 연산 함수 # In[19]: import math print(math.pi) print(math.e) print(math.sin(1.0)) # 1.0 라디안에 대한 사인 값 print(math.sqrt(2)) # 제곱근 # 3.14159265359 # 2.71828182846 # 0.841470984808 # 1.41421356237 # In[20]: 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 # *** # ## 2 문자열 # *** # ### 2-1 문자열 형식 # - 한 줄 문자열 형식 # - 단일 따옴표 # - 이중 따옴표 # In[7]: print('Hello World!') print("Hello World!") # - 여러 줄 문자열 형식 # - 연속된 단일 따옴표 세 개 # - 연속된 이중 따옴표 세 개 # In[8]: multiline = ''' To be, or not to be that is the question ''' print(multiline) multiline2 = """ To be, or not to be that is the question """ print(multiline2) # ### 2-2 인덱싱(Indexing)과 슬라이싱(Slicing) # - Indexing # In[9]: s = "Hello world!" print(s[0]) print(s[1]) print(s[-1]) print(s[-2]) # - Slicing # - 형식: [start(included) : stop(excluded) : step] # - 기본값: start - 0, stop - 자료형의 크기, step - 1 # In[10]: s = "Hello world!" print(s[1:3]) print(s[0:5]) # In[13]: s = 'Hello' print(s[1:]) print(s[:3]) print(s[:]) # In[14]: s = 'abcd' print(s[::2]) print(s[::-1]) # - 문자열 자료형은 변경되지 않는다. # In[16]: s = 'Hello World' s[0] = 'h' # - 문자열을 변경하려면 Slicing 및 연결 연산 (+)을 주로 이용한다. # In[17]: s = 'Hello World' s = 'h' + s[1:] s # ### 2-3 문자열 연산 # - +: 연결 # - *: 반복 # In[21]: print('Hello' + ' ' + 'World') print('Hello' * 3) print('-' * 60) # ### 2-4 문자열의 길이 # - len(): 문자열의 길이를 반환하는 내장함수 # - Return the number of items in a container. # In[19]: s = 'Hello World' len(s) # ### 2-5 문자열내 포함 관계 여부 # - in, not in: 문자열내에 일부 문자열이 포함되어 있는지를 파악하는 키워드 # In[20]: s = 'Hello World' print('World' in s) print('World' not in s) #

참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일