Python Numbers Flashcards

1
Q

What are the three numeric types in Python:

A

int
float
complex

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

To verify the type of any object in Python, use the ? function:

A

type() function:

x = 1
y = 2.8
z = 1j

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

is a whole number, positive or negative, without decimals, of unlimited length.

A

(int)

x = 1
y = 35656222554887711
z = -3255522

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.

A

Float

x = 1.10
y = 1.0
z = -35.59

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

? numbers are written with a “j” as the imaginary part:

A

complex

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

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you convert from one type to another with the int(), float(), and complex() methods?

A

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))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Python has a built-in module called ? that can be used to make ? numbers:

A

import random

print(random.randrange(1, 10))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you bring in a random variable?

A

import random
print (random.randrange(1,10))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly