컴퓨터

Python Numbers 파이썬 수치형

sayyesdoit 2019. 9. 6. 20:41

1. 수치형 Numbers

  파이썬 수치형은 세 가지가 있다.

- int 정수형
- float 실수형
- complex 복소수형

x = 1    # int
y = 2.8  # float
z = 1j   # complex

  실수형은 익스포넨셜을 이용해 나타낼 수도 있다.

x = 35e3
y = 12E4
z = -87.7e100

print(type(x))
print(type(y))
print(type(z))

  복소수형은 실수부와 허수부를 나뉘어 허수부에는 'j'를 붙여서 표시한다.

x = 3+5j
y = 5j
z = -5j

print(type(x))
print(type(y))
print(type(z))

2. 형 변환 Type Conversion

  int(), float(), complex() 메서드를 이용해 형 변환을 할 수 있다.

x = 1 # int
y = 2.8 # float
z = 1j # complex

#convert from int to float:
a = float(x)

#convert from float to int:
b = int(y)

#convert from int to complex:
c = complex(x)

print(a)
print(b)
print(c)

print(type(a))
print(type(b))
print(type(c))

3. 난수 Random Number

  파이썬은 난수를 생성하는 random() 함수는 없지만 random 모듈이 있다.

import random

print(random.randrange(1,10))