0
0
Pythonprogramming~5 mins

Type conversion (int, float, string) in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does int() do in Python?

int() converts a value to an integer type. For example, it can turn a string like "123" or a float like 12.7 into an integer by removing the decimal part.

Click to reveal answer
beginner
How do you convert a number to a string in Python?

You use the str() function. For example, str(10) becomes "10" as a string.

Click to reveal answer
intermediate
What happens if you convert a string with decimals to int()?

It causes an error because int() expects a whole number string. You should convert it first to float() then to int() if needed.

Click to reveal answer
beginner
What does float() do?

float() converts a value to a floating-point number (a number with decimals). For example, float("3.14") becomes 3.14.

Click to reveal answer
beginner
Why is type conversion useful in programming?

Because sometimes you need to change data from one type to another to do calculations, display text, or store data correctly. For example, adding numbers needs numeric types, but showing them needs strings.

Click to reveal answer
What will int("45") return?
A45
B"45"
C45.0
DError
What does str(3.14) return?
A"3.14"
B3.14
C3
DError
Which function converts a number with decimals to an integer by removing decimals?
Astr()
Bint()
Cfloat()
Dbool()
What happens if you try int("3.5") directly?
AReturns 3
BReturns 3.5
CReturns "3.5"
DRaises an error
How to convert a string "7.8" to an integer 7?
Aint("7.8")
Bstr(7.8)
Cfloat("7.8") then int()
Dint(str(7.8))
Explain how to convert between int, float, and string types in Python with examples.
Think about how to change numbers to text and text to numbers.
You got /4 concepts.
    Describe a situation where type conversion is necessary in a program.
    Imagine adding numbers entered as text.
    You got /3 concepts.