0
0
Pythonprogramming~10 mins

Type conversion (int, float, string) in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Type conversion (int, float, string)
Start with a value
Choose conversion type
int()
Converted value of chosen type
Use value
Start with a value, pick a conversion function (int, float, or str), convert the value, then use the new type.
Execution Sample
Python
x = '123'
y = int(x)
z = float(y)
s = str(z)
Convert string '123' to int, then to float, then back to string.
Execution Table
StepVariableValue BeforeConversion FunctionValue AfterType After
1xNoneAssign '123''123'str
2y'123'int(x)123int
3z123float(y)123.0float
4s123.0str(z)'123.0'str
5----End of conversions
💡 All conversions done, final value s is string '123.0'
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
xNone'123''123''123''123'
yNoneNone123123123
zNoneNoneNone123.0123.0
sNoneNoneNoneNone'123.0'
Key Moments - 3 Insights
Why does int('123') work but int('123.0') causes an error?
int() expects a string that looks like a whole number. '123' works (see step 2), but '123.0' is a decimal string and causes an error.
Does converting int to float change the value?
No, converting int 123 to float 123.0 keeps the number the same but changes its type (see step 3).
Why does str() add quotes around the value?
str() creates a string representation. The quotes show it's a string type (see step 4). The actual value is '123.0' without quotes in memory.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the type of variable y after step 2?
Astr
Bfloat
Cint
DNone
💡 Hint
Check the 'Type After' column in row for step 2.
At which step does the variable z get assigned a float value?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Conversion Function' and 'Type After' columns for variable z.
If we tried int('123.0') instead of int('123'), what would happen?
AIt raises an error
BIt converts successfully to 123
CIt converts to 123.0 as float
DIt converts to string '123.0'
💡 Hint
Recall the key moment about int() conversion from string with decimal.
Concept Snapshot
Type conversion changes a value's type using int(), float(), or str().
int() converts to whole numbers, float() to decimals, str() to text.
Conversions must be valid (e.g., int('123') works, int('123.0') errors).
Use conversions to change data types for calculations or display.
Full Transcript
This lesson shows how Python converts values between int, float, and string types. We start with a string '123', convert it to an integer 123, then to a float 123.0, and finally back to a string '123.0'. Each step changes the variable's type and value representation. We see that int() requires a string without decimals, float() can convert integers to decimals, and str() turns numbers into text. Understanding these conversions helps when you need to do math or show numbers as text.