Type checking using type() in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how long it takes to check the type of a value using the type() function in Python.
The question is: How does the time to check a type change when we use type() on different inputs?
Analyze the time complexity of the following code snippet.
values = [123, 'hello', 3.14, [1, 2, 3], {'a': 1}]
for val in values:
if type(val) == int:
print(f"{val} is an integer")
This code checks the type of each item in a list and prints a message if the item is an integer.
- Primary operation: Looping through each item in the list and calling
type()on it. - How many times: Once for each item in the list (n times, where n is the list size).
Each item requires one type check, so the total work grows directly with the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 type checks |
| 100 | 100 type checks |
| 1000 | 1000 type checks |
Pattern observation: The time grows in a straight line as the list gets bigger.
Time Complexity: O(n)
This means the time to check types grows directly with the number of items you check.
[X] Wrong: "Checking the type with type() takes the same time no matter how many items there are, so it is constant time overall."
[OK] Correct: While each type() call is fast, you do it once per item, so total time adds up as the list grows.
Understanding how simple operations like type checking scale helps you explain code efficiency clearly and confidently in interviews.
"What if we used isinstance() instead of type()? How would the time complexity change?"
Practice
type() function do in Python?Solution
Step 1: Understand the purpose of
Thetype()type()function returns the data type of the given variable or value.Step 2: Compare options with the function's purpose
Only It tells you the kind of data stored in a variable. correctly states thattype()tells the kind of data stored in a variable.Final Answer:
It tells you the kind of data stored in a variable. -> Option AQuick Check:
type() returns data type [OK]
- Thinking type() changes the variable's type
- Confusing type() with print()
- Assuming type() deletes variables
x?Solution
Step 1: Recall the syntax of the
The correct syntax uses parentheses around the variable:type()functiontype(x).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.Final Answer:
type(x) -> Option CQuick Check:
Use parentheses with type() [OK]
- Using square brackets instead of parentheses
- Omitting parentheses
- Using non-existent functions like check_type()
value = 3.14 print(type(value))
Solution
Step 1: Identify the data type of
The variablevaluevalueis assigned 3.14, which is a decimal number, so its type isfloat.Step 2: Understand what
Theprint(type(value))outputstype()function returns<class 'float'>for a float value, which is printed.Final Answer:
<class 'float'> -> Option AQuick Check:
3.14 is float type [OK]
- Confusing float with int
- Expecting string output
- Thinking it causes an error
my_var = 'hello'
if type(my_var) == str:
print('It is a string')Solution
Step 1: Check the if statement syntax
The if statement has a colon and uses type() correctly, so no error there.Step 2: Check indentation of the print statement
In Python, the code inside if must be indented. Here,printis not indented, causing an indentation error.Final Answer:
Indentation error in print statement -> Option BQuick Check:
Indent inside if blocks [OK]
- Forgetting to indent after if
- Misusing type() comparison
- Missing colon after if
True if the input is an integer or a float, and False otherwise. Which code correctly uses type() to do this?Solution
Step 1: Understand the goal
The function should return True ifxis eitherintorfloat.Step 2: Analyze each option's correctness
def check_num(x): return type(x) == int or float usesor floatincorrectly, always returning truthy. def check_num(x): return type(x) in [int, float] usesinto 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 usesandincorrectly, only detecting ints.Final Answer:
def check_num(x): return type(x) in [int, float] -> Option DQuick Check:
Useinto check multiple types [OK]
- Comparing type to string names
- Using 'and' instead of 'or' or 'in'
- Using incomplete logic like '== int or float'
