0
0
Pythonprogramming~10 mins

Type checking using type() in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Type checking using type()
Start
Have a variable
Call type(variable)
Get type result
Compare or print type
Use type info as needed
End
This flow shows how Python checks the type of a variable using the type() function and then uses that information.
Execution Sample
Python
x = 10
print(type(x))
y = 'hello'
print(type(y))
This code checks and prints the types of two variables: an integer and a string.
Execution Table
StepActionVariabletype(variable)Output
1Assign 10 to xx=10int
2Call type(x)x=10int<class 'int'>
3Print type(x)x=10int<class 'int'>
4Assign 'hello' to yy='hello'str
5Call type(y)y='hello'str<class 'str'>
6Print type(y)y='hello'str<class 'str'>
💡 All variables checked and their types printed.
Variable Tracker
VariableStartAfter Step 1After Step 4Final
xundefined101010
yundefinedundefined'hello''hello'
Key Moments - 2 Insights
Why does type(x) return <class 'int'> instead of just 'int'?
type() returns the type object, which prints as <class 'int'>. This shows the variable's type clearly. See execution_table step 2.
Can type() tell the difference between strings and numbers?
Yes, type() returns different type objects like <class 'int'> for numbers and <class 'str'> for strings, as shown in steps 2 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the type of variable y?
A<class 'int'>
B<class 'str'>
C<class 'float'>
D<class 'list'>
💡 Hint
Check the 'type(variable)' column at step 5 in the execution_table.
At which step is the variable x assigned a value?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Action' and 'Variable' columns in the execution_table for when x gets a value.
If we change x to 3.14, what would type(x) return?
A<class 'int'>
B<class 'str'>
C<class 'float'>
D<class 'bool'>
💡 Hint
Recall that 3.14 is a decimal number, which Python treats as float.
Concept Snapshot
type(variable) returns the type of the variable.
It shows the class like <class 'int'> or <class 'str'>.
Use it to check what kind of data a variable holds.
Useful for debugging or conditional checks.
Example: type(x) == int checks if x is an integer.
Full Transcript
This lesson shows how Python's type() function checks the type of a variable. We assign values to variables x and y, then call type() on them. The output shows <class 'int'> for x when it holds 10, and <class 'str'> for y when it holds 'hello'. This helps us understand what kind of data each variable contains. Beginners often wonder why the output shows <class 'int'> instead of just int; this is Python's way of showing the type object clearly. The execution table traces each step, showing variable assignments and type checks. The variable tracker shows how x and y change from undefined to their assigned values. Quizzes test understanding of variable types and when assignments happen. Remember, type() is a simple way to see what data type a variable has in Python.