Python Basic _ 3-1. Type(숫자형)

WONY_yoon·2025년 10월 3일
post-thumbnail

Python 숫자형 알아보기

# chapter03-1
# 숫자형

# 파이썬 지원 자료형
'''
int : 정수
float : 실수
complex : 복소수
bool : 불린
str : 문자열(시퀀스)
list : 리스트(시퀀스)
tuple : 튜플(시퀀스)
set : 집합
dict : 사전
'''

# 데이터 타입
str1 = "python"
bool = True
str2 = 'Anaconda'
float1 = 10.0      # 10 == 10.0 ?
int1 = 7
list1 = [str1, str2]
dict1 = {
    "name" : "Machine Learning",
    "version" : 2.0
}
tuple1 = (7, 8, 9)
set1 = {7, 8, 9}

# 데이터 타입 출력
print(type(str1))    # <class 'str'>
print(type(bool))    # <class 'bool'>
print(type(str2))    # <class 'str'>
print(type(float))   # <class 'type'>
print(type(int))     # <class 'type'>
print(type(dict))    # <class 'type'>
print(type(tuple))   # <class 'type'>
print(type(set))     # <class 'type'>

# 숫자형 연산자
"""
+
-
*
/
// : 몫
% : 나머지
abs(x) : 절대값
pow(x, y) = x**y
"""

# 정수 선언
i = 77
i2 = -14
big_int = 777777779999999797979777799

# 정수 출력
print(i)        # 77
print(i2)       # -14
print(big_int)  # 777777779999999797979777799

print()

# 실수 선언
f = 0.9999
f2 = 3.141592
f3 = -3.9
f4 = 3 / 9

# 실수 출력
print(f)    # 0.9999
print(f2)   # 3.141592
print(f3)   # -3.9
print(f4)   # 0.3333333333333333

print()

# 연산 실습
i1 = 39
i2 = 939
big_int1 = 783729837498137894798427
big_int2 = 38435345678909876543
f1 = 1.234
f2 = 3.939

# +
print(">>>>>+")
print("i1 + i2 : ", i1 + i2)                     # 978
print("f1 + f2 : ", f1 + f2)                     # 5.173
print("big_int1 + big_int2 : ", big_int1 + big_int2)
# 783729837536573240277370

# *
print(">>>>>*")
print("i1 * i2 : ", i1 * i2)                     # 36621
print("f1 * f2 : ", f1 * f2)                     # 4.857226
print("big_int1 * big_int2 : ", big_int1 * big_int2)
# (아주 큰 수 출력)

# -
print(">>>>>-")
print("i1 - i2 : ", i1 - i2)                     # -900
print("f1 - f2 : ", f1 - f2)                     # -2.705
print("big_int1 - big_int2 : ", big_int1 - big_int2)
# 783729837459702549319484

print()

# 형 변환 실습
a = 3.     # 실수
b = 6      # 정수
c = .7     # 실수
d = 12.7   # 실수

# 타입 출력
print(type(a), type(b), type(c), type(d))
# <class 'float'> <class 'int'> <class 'float'> <class 'float'>

# 형 변환
print(float(b))      # 6.0
print(int(c))        # 0
print(int(d))        # 12
print(int(True))     # 1
print(float(False))  # 0.0
print(complex(3))    # (3+0j)
print(complex('3'))  # (3+0j)
print(complex(False))# 0j

# 수치 변환 함수
print(abs(-7))       # 7
x, y = divmod(100, 8)
print(x, y)          # 12 4
print(pow(5, 3), 5 ** 3)  # 125 125

# 외부 모듈
import math
print(math.pi)       # 3.141592653589793
print(math.ceil(5.1))# 6

0개의 댓글