-
[Python tutorial] 4. Data type - Numbers코딩/Python 2022. 12. 21. 17:11728x90
Numbers
- int
- float
- complex
Integer: int
x = 1 y = 100 z = -300
Float
x = 1.10 y = 1.0 z = -35.59 a = 35e3 # scientific numbers with an "e" b = 12E4 c = -87.7e100
Complex
x = 3+5j y = 5j z = -5j
Type Conversion
int(), float(), complex()
x = 1 # int y = 2.8 # float z = 1j # complex, 다른 타입으로 변환 불가 a = float(x) # 1.0 b = int(y) # 2 c = complex(x) # (1+0j)
Random Number: random()
import random print(random.randrange(1, 10))
Casting
x = int(1) # 1 y = int(2.8) # 2 z = int("3") # 3
x = float(1) # 1.0 y = float(2.8) # 2.8 z = float("3") # 3.0 w = float("4.2") # 4.2
x = str("s1") # 's1' y = str(2) # '2' z = str(3.0) # '3.0'
728x90'코딩 > Python' 카테고리의 다른 글
[Python tutorial] 6. Data type - Boolean (0) 2022.12.21 [Python tutorial] 5. Data type - Strings (1) 2022.12.21 [Python tutorial] 3. Data types (0) 2022.12.21 [Python tutorial] 2. Basic, Variables, Module, User input (0) 2022.12.21 [Python tutorial] 1. Introduction (0) 2022.12.21