0
0
Pythonprogramming~5 mins

Type checking using type() in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the type() function do in Python?
The type() function tells you the type of a value or variable, like whether it is a number, text, list, or something else.
Click to reveal answer
beginner
How do you check if a variable x is an integer using type()?
You compare type(x) to int like this: type(x) == int. If it is true, x is an integer.
Click to reveal answer
beginner
What will type('hello') return?
It will return <class 'str'> because 'hello' is a string (text).
Click to reveal answer
beginner
Can type() be used to check the type of a list?
Yes, for example type([1, 2, 3]) == list will be True because the value is a list.
Click to reveal answer
intermediate
Is type() useful for making decisions in code?
Yes, you can use type() in if statements to run different code depending on the type of a variable.
Click to reveal answer
What does type(3.14) return?
A&lt;class 'float'&gt;
B&lt;class 'int'&gt;
C&lt;class 'str'&gt;
D&lt;class 'bool'&gt;
How do you check if variable y is a string?
Atype(y) == bool
Btype(y) == int
Ctype(y) == list
Dtype(y) == str
What will type(True) return?
A&lt;class 'int'&gt;
B&lt;class 'bool'&gt;
C&lt;class 'str'&gt;
D&lt;class 'float'&gt;
Which of these is NOT a valid use of type()?
AChanging the type of a variable
BChecking if a variable is a list
CChecking if a variable is an integer
DFinding the type of a string
What does type([1, 2, 3]) == list evaluate to?
AFalse
BError
CTrue
DNone
Explain how to use type() to check if a variable is a string or an integer.
Think about comparing the result of type() to the type name.
You got /3 concepts.
    Describe a simple real-life example where checking the type of a variable with type() might be useful.
    Imagine you get different kinds of data and want to treat them differently.
    You got /3 concepts.