Bird
Raised Fist0
Pythonprogramming~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the type() function do in Python?
easy
A. It tells you the kind of data stored in a variable.
B. It changes the data type of a variable.
C. It prints the value of a variable.
D. It deletes a variable from memory.

Solution

  1. Step 1: Understand the purpose of type()

    The type() function returns the data type of the given variable or value.
  2. Step 2: Compare options with the function's purpose

    Only It tells you the kind of data stored in a variable. correctly states that type() tells the kind of data stored in a variable.
  3. Final Answer:

    It tells you the kind of data stored in a variable. -> Option A
  4. Quick Check:

    type() returns data type [OK]
Hint: Remember: type() shows data kind, not value or change [OK]
Common Mistakes:
  • Thinking type() changes the variable's type
  • Confusing type() with print()
  • Assuming type() deletes variables
2. Which of the following is the correct syntax to check the type of variable x?
easy
A. type x
B. type[x]
C. type(x)
D. check_type(x)

Solution

  1. Step 1: Recall the syntax of the type() function

    The correct syntax uses parentheses around the variable: type(x).
  2. Step 2: Evaluate each option

    type(x) matches the correct syntax. Options A and B use wrong brackets or missing parentheses. check_type(x) is not a valid Python function.
  3. Final Answer:

    type(x) -> Option C
  4. Quick Check:

    Use parentheses with type() [OK]
Hint: Use parentheses with functions like type() [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Omitting parentheses
  • Using non-existent functions like check_type()
3. What will be the output of this code?
value = 3.14
print(type(value))
medium
A. <class 'float'>
B. <class 'int'>
C. <class 'str'>
D. Error

Solution

  1. Step 1: Identify the data type of value

    The variable value is assigned 3.14, which is a decimal number, so its type is float.
  2. Step 2: Understand what print(type(value)) outputs

    The type() function returns <class 'float'> for a float value, which is printed.
  3. Final Answer:

    <class 'float'> -> Option A
  4. Quick Check:

    3.14 is float type [OK]
Hint: Decimals are float type in Python [OK]
Common Mistakes:
  • Confusing float with int
  • Expecting string output
  • Thinking it causes an error
4. Find the error in this code snippet:
my_var = 'hello'
if type(my_var) == str:
print('It is a string')
medium
A. Missing colon after if statement
B. Indentation error in print statement
C. Variable name is invalid
D. Incorrect use of type() function

Solution

  1. Step 1: Check the if statement syntax

    The if statement has a colon and uses type() correctly, so no error there.
  2. Step 2: Check indentation of the print statement

    In Python, the code inside if must be indented. Here, print is not indented, causing an indentation error.
  3. Final Answer:

    Indentation error in print statement -> Option B
  4. Quick Check:

    Indent inside if blocks [OK]
Hint: Indent code inside if statements [OK]
Common Mistakes:
  • Forgetting to indent after if
  • Misusing type() comparison
  • Missing colon after if
5. You want to write a function that returns True if the input is an integer or a float, and False otherwise. Which code correctly uses type() to do this?
hard
A. def check_num(x): return type(x) == int and float
B. def check_num(x): return type(x) == int or float
C. def check_num(x): return type(x) == 'int' or 'float'
D. def check_num(x): return type(x) in [int, float]

Solution

  1. Step 1: Understand the goal

    The function should return True if x is either int or float.
  2. Step 2: Analyze each option's correctness

    def check_num(x): return type(x) == int or float uses or float incorrectly, always returning truthy. def check_num(x): return type(x) in [int, float] uses in to check membership in a list of types, which is concise and correct. def check_num(x): return type(x) == 'int' or 'float' compares type to strings, which is wrong. def check_num(x): return type(x) == int and float uses and incorrectly, only detecting ints.
  3. Final Answer:

    def check_num(x): return type(x) in [int, float] -> Option D
  4. Quick Check:

    Use in to check multiple types [OK]
Hint: Use 'type(x) in [int, float]' for multiple types [OK]
Common Mistakes:
  • Comparing type to string names
  • Using 'and' instead of 'or' or 'in'
  • Using incomplete logic like '== int or float'