a = 23 # 10진 정수
b = 023 # 8진 정수
c = 0x23 # 16진 정수
print type(a), type(b), type(c)
print a, b, c
<type 'int'> <type 'int'> <type 'int'> 23 19 35
import sys
print sys.maxint # 최대 정수 값 확인
9223372036854775807
a = 1.2
b = 3.5e3
c = -0.2e-4
print type(a), type(b), type(c)
print a, b, c
<type 'float'> <type 'float'> <type 'float'> 1.2 3500.0 -2e-05
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)
<type 'long'> 15241578753238836750495351562536198787501905199875019052100 <type 'long'> 15241578753238836750495351562536198787501905199875019052100 <type 'long'> <type 'int'>
123456789012345678890 # 자동 long형 변환
123456789012345678890L
a = 10 + 20j
print a
b = 10 + 5j
print a + b
(10+20j) (20+25j)
x = 1
x = 2
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)
3 3 -3 3 5.0 (3.4+5j) (6+0j)
print divmod(5, 2)
print
print pow(2, 3)
print pow(2.3, 3.5)
(2, 1) 8 18.4521691056
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
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
0.866025403784 0.5 1.73205080757
print 'Hello World!'
print "Hello World!"
Hello World! Hello World!
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
To be, or not to be that is the question To be, or not to be that is the question
s = "Hello world!"
print s[0]
print s[1]
print s[-1]
print s[-2]
H e ! d
s = "Hello world!"
print s[1:3]
print s[0:5]
el Hello
s = 'Hello'
print s[1:]
print s[:3]
print s[:]
ello Hel Hello
s = 'abcd'
print s[::2]
print s[::-1]
ac dcba
s = 'Hello World'
s[0] = 'h'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-88-28020f3d59a5> in <module>() 1 s = 'Hello World' ----> 2 s[0] = 'h' TypeError: 'str' object does not support item assignment
s = 'Hello World'
s = 'h' + s[1:]
s
'hello World'
print 'Hello' + '' + 'World'
print 'Hello' * 3
print '-' * 60
HelloWorld HelloHelloHello ------------------------------------------------------------
s = 'Hello World'
len(s)
11
s = 'Hello World'
print 'World' in s
print 'World' not in s
True False
참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일