a = 23 # 10진 정수
# b = 023
b = 0o23 # 8진 정수
c = 0x23 # 16진 정수
print(type(a), type(b), type(c))
print(a, b, c)
<class 'int'> <class 'int'> <class 'int'> 23 19 35
import sys
# print sys.maxint <-- python2 에서만 동작
print(sys.maxsize) # 최대 정수 값 확인
# https://docs.python.org/3/library/sys.html#sys.maxsize
9223372036854775807
a = 9223372036854775808
print(a)
print(type(a))
9223372036854775808 <class 'int'>
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))
<class 'float'> <class 'float'> <class 'float'> 1.2 3500.0 -2e-05 1.200 3500.000 -0.00002000
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.
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))
<class 'int'> 15241578753238836750495351562536198787501905199875019052100 <class 'int'> 15241578753238836750495351562536198787501905199875019052100 <class 'int'> <class 'int'>
a = 10 + 20j
print(a)
print(type(a))
print()
b = 10 + 5j
print(a + b)
(10+20j) <class 'complex'> (20+25j)
x = 1
x = 2
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))
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.45216910555504
math
모듈의 수치 연산 함수¶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
3.141592653589793 2.718281828459045 0.8414709848078965 1.4142135623730951
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.8660254037844386 0.5000000000000001 1.7320508075688767
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
[start(included) : stop(excluded) : step]
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-16-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)
Hello World HelloHelloHello ------------------------------------------------------------
len()
: 문자열의 길이를 반환하는 내장함수s = 'Hello World'
len(s)
11
in
, not in
: 문자열내에 일부 문자열이 포함되어 있는지를 파악하는 키워드s = 'Hello World'
print('World' in s)
print('World' not in s)
True False
참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일