데이터 타입, 형 변환, 수치 연산

gyomni·2021년 12월 16일
0

Python

목록 보기
5/12
post-thumbnail

#데이터 타입

v_str1 = "Hi"
v_bool = True
v_str2 = "Bye"
v_float = 10.3
v_int = 7
v_dict = {
    "name" : "Kim",
    "age" : 25
} # key:value 
v_list = [3,5,7]
v_tuple = (3, 5, 7)
v_set = {7, 8, 9} # set=집합

print(type(v_tuple)) # <class 'tuple'>  
print(type(v_set)) # <class 'set'>
print(type(v_str1)) # <class 'str'>
print(type(v_bool)) # <class 'bool'>
print(type(v_float)) # <class 'float'>
print(type(v_int)) # <class 'int'>
print(type(v_dict)) # <class 'dict'>

# type( ) -> ( )안의 데이터 자료형을 알려줌

i1 = 39
i2 = 939
big_int1 = 999999999999999999
big_int2 = 777777777777777777
f1 = 1.234
f2 = 3.784
f3 = .5
f4 = 10.

print(i1 *i2) # 36621
print(big_int1*big_int2) # 777777777777777776222222222222222223
print(f1**f2)  # 2.2158306445574567
print(f3+i2) # 939.5 , 자동 형변환 일어나서 출력. (casting) 

result = f3 +i2
print(result, type(result)) # 939.5 <class 'float'>

a = 5.
b = 4
c = 10

print(type(a), type(b)) # <class 'float'> <class 'int'>
result2 = a+b
print(result2) # 9.0

# 형변환

- int, float, complex(복소수)

print(int(result2)) # 9
print(float(c)) # 10.0
print(complex(3)) # (3+0j)
print(int(True)) # 1
print(int(False)) # 0
print(int('3')) # 3 -> 문자를 숫자로 형변환
print(complex(False)) # 0j


y = 100
y += 100
print(y) # 200

# 수치 연산 함수

print(abs(-7)) # 7 -> 절대값
n, m = divmod(100,8) # 100을 8로 나눠서 몫은 n, 나머지는 m에 들어감.
print(n,m) # 12 4

import math # 맨 위에 적기.
print(math.ceil(5.1)) # 6 -> 5.1 이상에서 가장 작은 정수
print(math.floor(3.891)) # 3 -> 3.891 보다 작은 것 중에 가장 가까운 정수

https://docs.python.org/3/library/math.html

profile
Front-end developer 👩‍💻✍

0개의 댓글