Python Numbers Flashcards
What are the three numeric types in Python:
int
float
complex
To verify the type of any object in Python, use the ? function:
type() function:
x = 1
y = 2.8
z = 1j
print(type(x))
print(type(y))
print(type(z))
is a whole number, positive or negative, without decimals, of unlimited length.
(int)
x = 1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
is a number, positive or negative, containing one or more decimals, Float can also be scientific numbers with an “e” to indicate the power of 10.
Float
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))
? numbers are written with a “j” as the imaginary part:
complex
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
How do you convert from one type to another with the int(), float(), and complex() methods?
convert from int to float:
x = 1 # int
y = 2.8 # float
z = 1j # complex
a = float(x)
b = int(y)
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
Python has a built-in module called ? that can be used to make ? numbers:
import random
print(random.randrange(1, 10))
How do you bring in a random variable?
import random
print (random.randrange(1,10))