Type Conversion(Typecasting) Flashcards

1
Q

Convert from one type to another with the:

A

methods:

  • int()
  • float()
  • complex()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

convert from int to float:

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

a = float(x)

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

convert from float to int:

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

b = int(y)

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

convert from int to complex:

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

c = complex(x)

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

convert 1 to a float

A

float(1)

1.0

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

convert 1.3 to an int

A

int(1.3)

1

Rember int will not round up

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

Everything in Python can be truend into what?

A

Booleon

bool

>>> bool(1)

True

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

bool of 0 will return:

A

false

>>> bool(0)

False

>>>>bool(0.0)

False

If there is 0 lenth string

bool(‘ ‘)

false

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