Data Types & Conversion — Python Questions with Answers
Start with the study notes on Data Types & Conversion (definitions, examples and a quick revision), then work through 30 practice questions, each with the correct answer and a clear explanation of why it is right. When you are ready, take this topic as an interactive quiz to test yourself.
13 easy · 12 medium · 5 hard · Part of Python: Master Python from basics to advanced - 20 topics.
Topic overview
Data Types and Conversion – what kind of value is it?
1. The basic data types
Every value has a type, which decides what you can do with it. type() tells you a value's type.
int: whole numbers, e.g. 5, -12.
float: numbers with a decimal point, e.g. 3.14.
str: text, e.g. "hello".
bool: True or False (capital letters).
NoneType: the single value None, meaning “no value”.
In a condition, every value counts as true or false. Falsy values are 0, 0.0, "" (empty string), empty containers ([], {}, ()), None and False. Everything else is truthy.
isinstance(x, int) is the preferred check. It also accepts subclasses and a tuple of types.
print(isinstance(5, int))
print(isinstance(5.0, (int, float)))
x = None
print(x is None)
OutputTrue
True
True
8. Common misconceptions
Misconception
Correct understanding
bool("False") is False.
Any non-empty string is truthy, so bool("False") is True.
int(3.9) rounds to 4.
int() cuts off the decimal part: int(3.9) is 3.
0.1 + 0.2 == 0.3 is True.
Floats are approximate; use math.isclose().
None is the same as 0 or "".
None is its own value meaning “nothing”, though it is falsy.
"5" + 5 gives 10.
It is a TypeError; Python never mixes text and numbers automatically.
9. Quick revision
int
Whole number.
float
Number with a decimal point.
str
Text.
bool
True or False.
None
The “no value” value.
Falsy
0, empty string, empty containers, None, False.
type()
Tells you a value's type.
isinstance()
Checks a type, including subclasses.
10. Test your understanding
What is the type of 5.0?
Show answer
float
What does int("12") + 3 give?
Show answer
15
What does str(5) + "5" give?
Show answer
"55"
What does bool("") give?
Show answer
False
What does True + True give?
Show answer
2
Why is 0.1 + 0.2 == 0.3 False?
Show answer
Floats are stored approximately in binary.
What error does int("3.5") raise?
Show answer
ValueError.
Which is better: type(x) == int or isinstance(x, int)?
Show answer
isinstance(x, int): it also accepts subclasses.
Key takeaway: Know each value's type, convert explicitly with int(), float() and str(), remember which values are falsy, and never compare floats with ==.
Notes written by Velnov. Every code example with an output was run in Python 3 to check that the output shown is exactly what Python prints.
Python refuses to mix a string and a number with + . You must convert one side first: int("5") + 5 = 10, or "5" + str(5) = "55". (JavaScript would silently give "55" - Python is stricter.)
float() converts a value to a floating-point number: float("7") is 7.0. It accepts integer strings, decimal strings, and numbers.
float()Type conversion
Q24.easy
Converting a value from one type to another (e.g. int("5")) is commonly called:
A Mutation
B Type casting / type conversion
C Inheritance
D Compilation
Answer: B — Type casting / type conversion
Changing a value's type - int("5"), str(3.14), float(2) - is called type conversion or type casting. Python requires it to be explicit; it won't silently convert strings to numbers.
int()str()float()
Q25.easy
Which of these creates a string?
a = 'hello' # str
b = "hello" # also str - same thing
A x = 5
B x = 'hello'
C x = True
D x = 5.5
Answer: B — x = 'hello'
Text wrapped in quotes (single OR double - both work identically) is a string. 5 is int, True is bool, 5.5 is float.
Answer: D — ValueError - int() can't parse a decimal string
int() can convert "3" but NOT "3.5" - the decimal point makes it invalid. Convert in two steps: int(float("3.5")) gives 3.
ValueErrorType conversion
Q16.medium
What is the value and type of x?
x = None
x = None
print(type(x)) # <class 'NoneType'>
print(x is None) # True - the proper check
A 0, int
B Empty string, str
C None, NoneType - it represents 'no value'
D False, bool
Answer: C — None, NoneType - it represents 'no value'
None is Python's special 'nothing here' value, with its own type NoneType. It is NOT zero, not an empty string, and not False - though it is falsy in conditions.
Noneis operator
Q18.medium
What is the value of: 1e3
print(1e3) # 1000.0
print(1e-2) # 0.01
A 13
B 1000.0
C 103
D Error
Answer: B — 1000.0
1e3 is scientific notation: 1 x 10^3 = 1000.0 (always a float). Similarly 2.5e2 = 250.0 and 1e-2 = 0.01.
Scientific notationFloats
Q21.medium
Which is the better way to check if x is a number, and why?
A) type(x) == int B) isinstance(x, int)
B B - isinstance() also accepts subclasses and is the Python convention
C Both are identical
D Neither works
Answer: B — B - isinstance() also accepts subclasses and is the Python convention
isinstance(x, int) is preferred: it returns True for int AND its subclasses (like bool), works with a tuple of types, and reads better. type(x) == int does strict exact-type matching only.
isinstance()type()
Q22.medium
Python has a built-in type for complex numbers. Which literal creates one?
str(3.0) converts the float to the text "3.0", and print shows it without quotes: 3.0. The .0 is kept because the float's standard representation includes it.
str()String representation
Q27.medium
What is the output of: print(type(10) == type(10.0))
type(10) is int and type(10.0) is float - two different types, so the comparison is False. Note that 10 == 10.0 (comparing VALUES) is True!
type()Value vs type equality
Q30.medium
Which core types will we meet later that hold COLLECTIONS of values?
nums = [1, 2, 3] # list
point = (4, 5) # tuple
ages = {"Ana": 21} # dict
ids = {101, 102, 103} # set
A list, tuple, dict, set
B int, float, bool, str
C array, vector, map, table
D group, bundle, pack, batch
Answer: A — list, tuple, dict, set
Python's four main collection types: list (ordered, changeable), tuple (ordered, unchangeable), dict (key-value pairs), set (unique values). Each gets its own topic later in this course.
Floats are stored in binary, and 0.1, 0.2, 0.3 can't be represented exactly - 0.1 + 0.2 is actually 0.30000000000000004. Never compare floats with ==; use math.isclose() or round first.
This is true in nearly every language, not just Python.
bool is actually a subclass of int: True behaves as 1 and False as 0 in arithmetic. So True + True = 2. Useful trick: sum(list_of_bools) counts the Trues.
bool as intsum()
Q20.hard
What is the output of: print(isinstance(True, int))
Surprising but true: bool is a subclass of int in Python, so True IS an instance of int. This is why True + 1 equals 2.
isinstance()Inheritance
Q28.hard
Which statement about int in Python 3 is TRUE?
print(2 ** 100)
# 1267650600228229401496703205376
A It overflows above 2 billion like in C
B It has unlimited precision - it can hold arbitrarily large numbers
C It maxes out at 64 bits
D Big numbers must use the long type
Answer: B — It has unlimited precision - it can hold arbitrarily large numbers
Python 3 integers grow as large as memory allows - 2**1000 works fine. There's no overflow and no separate long type (that was Python 2). This is unusual: C, Java and JavaScript all have fixed-size integers.