🎓 Velnov

🐍 Python · Topic 3 of 20

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.

print(type(5))
print(type(5.0))
print(type("5"))
print(type(True))
print(type(None))
Output<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'NoneType'>

2. Converting between types

Type conversion (casting) changes a value into another type with int(), float(), str() or bool(). Python never mixes strings and numbers on its own.

print(int("12") + 3)
print(str(5) + "5")
print(int(3.9))        # cuts off the decimal, does not round
print(float("7"))
print(int(float("3.5")))
Output15
55
3
7.0
3

Remember: "5" + 5 is a TypeError, and int("3.5") is a ValueError. Convert in two steps: int(float("3.5")).

Mixing int and float

When an int and a float meet in arithmetic, the result is a float.

print(2 + 3.0)
print(10 == 10.0, type(10) == type(10.0))
Output5.0
True False

3. Truthy and falsy values

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.

print(bool(""), bool("hi"))
print(bool("False"))     # a non-empty string!
print(bool(0), bool(-1))
print(bool([]), bool([0]))
OutputFalse True
True
False True
False True

4. bool is a kind of int

In Python, True behaves like 1 and False like 0. This makes sum() a quick way to count True values.

print(True + True)
print(isinstance(True, int))
passed = [True, False, True]
print(sum(passed))
Output2
True
2

5. Floats are approximate

Floats are stored in binary, so many decimals cannot be stored exactly. Never compare floats with ==; use math.isclose().

import math
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
print(math.isclose(0.1 + 0.2, 0.3))
Output0.30000000000000004
False
True

6. Integers have no size limit

Python's int can grow as large as memory allows, so there is no overflow.

print(2 ** 100)
print(int("ff", 16), int("1010", 2))   # hex and binary
Output1267650600228229401496703205376
255 10

7. Checking types: isinstance()

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

MisconceptionCorrect 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

  1. What is the type of 5.0?

    Show answer

    float

  2. What does int("12") + 3 give?

    Show answer

    15

  3. What does str(5) + "5" give?

    Show answer

    "55"

  4. What does bool("") give?

    Show answer

    False

  5. What does True + True give?

    Show answer

    2

  6. Why is 0.1 + 0.2 == 0.3 False?

    Show answer

    Floats are stored approximately in binary.

  7. What error does int("3.5") raise?

    Show answer

    ValueError.

  8. 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.

Key concepts in this topic

type()Numeric typesBooleansComparison resultsType conversionint()str()String concatenationTypeErrorround()TruncationDivisionFloor divisionisinstance()IntrospectionTruthinessbool()Common gotchasType promotionMixed arithmeticValueErrorfloat()Noneis operator

Jump to: Easy · Medium · Hard

Easy questions (13)

Q1. easy

What is the type of the value 5?

print(type(5))     # <class 'int'>
print(type(5.0))   # <class 'float'>
print(type("5"))   # <class 'str'>
  1. A float
  2. B int
  3. C str
  4. D number

Answer: B — int

Whole numbers without a decimal point are int (integers). 5.0 would be a float, and "5" (in quotes) would be a str.

type()Numeric types

Q2. easy

What is the type of True?

print(type(True))   # <class 'bool'>
  1. A str
  2. B int
  3. C bool
  4. D boolean

Answer: C — bool

True and False are the two values of the bool type. Note the capital letters - true and false (lowercase) are NameErrors in Python.

  • The type is spelled bool, not boolean.

BooleansComparison results

Q3. easy

What is the output of: print(int("12") + 3)

print(int("12") + 3)   # 15
# print("12" + 3)      # TypeError!
  1. A 123
  2. B 15
  3. C "123"
  4. D Error

Answer: B — 15

int("12") converts the string to the number 12, then 12 + 3 = 15. Without the conversion, "12" + 3 would be a TypeError.

Type conversionint()

Q4. easy

What is the output of: print(str(5) + "5")

print(str(5) + "5")   # 55  (text joining)
print(5 + 5)           # 10  (math)
  1. A 10
  2. B 55
  3. C Error
  4. D 5 5

Answer: B — 55

str(5) converts the number to the text "5", and + on two strings joins them: "5" + "5" = "55".

str()String concatenation

Q5. easy

What happens when you run: print("5" + 5)

# print("5" + 5)      # TypeError
print(int("5") + 5)   # 10
print("5" + str(5))   # 55
  1. A Prints 10
  2. B Prints 55
  3. C TypeError - can't add str and int
  4. D Prints 5 5

Answer: C — TypeError - can't add str and int

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.)

TypeErrorType conversion

Q6. easy

What is the output of: print(int(3.9))

print(int(3.9))     # 3  (cut, not rounded)
print(round(3.9))   # 4
print(int(-3.9))    # -3 (cuts toward zero)
  1. A 4
  2. B 3
  3. C 3.9
  4. D Error

Answer: B — 3

int() TRUNCATES a float - it simply cuts off the decimal part, it does not round. int(3.9) is 3. Use round(3.9) if you want 4.

int()round()Truncation

Q8. easy

Which function tells you the type of a value?

print(type(42))       # <class 'int'>
print(type([1, 2]))   # <class 'list'>
  1. A typeof()
  2. B type()
  3. C gettype()
  4. D kind()

Answer: B — type()

type(x) returns the type of x, e.g. type(3.14) is <class 'float'>. (typeof is JavaScript, gettype is PHP.)

isinstance()Introspection

Q11. easy

What is the output of: print(bool(0), bool(-1))

print(bool(0))     # False
print(bool(-1))    # True
print(bool(0.0))   # False
  1. A False False
  2. B False True
  3. C True True
  4. D True False

Answer: B — False True

Only zero is falsy among numbers. ANY non-zero number - including negatives - is truthy, so bool(-1) is True.

Truthiness

Q12. easy

Which data type would you use to store 3.14?

pi = 3.14
print(type(pi))   # <class 'float'>
  1. A int
  2. B float
  3. C str
  4. D bool

Answer: B — float

Numbers with a decimal point are floats (floating-point numbers). Integers (int) hold only whole numbers.

Numeric types

Q15. easy

What does float("7") return?

print(float("7"))     # 7.0
print(float("3.14"))  # 3.14
print(float(5))        # 5.0
  1. A 7
  2. B 7.0
  3. C "7.0"
  4. D Error

Answer: B — 7.0

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:

  1. A Mutation
  2. B Type casting / type conversion
  3. C Inheritance
  4. 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
  1. A x = 5
  2. B x = 'hello'
  3. C x = True
  4. 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.

StringsQuotes

Q29. easy

What is the output of: print(len("hello"))

print(len("hello"))    # 5
print(len(""))          # 0
print(len([1, 2, 3]))   # 3
  1. A 4
  2. B 5
  3. C 6
  4. D Error

Answer: B — 5

len() returns the number of characters in a string: "hello" has 5 letters. len() also works on lists, tuples, dicts and sets.

len()Strings

Medium questions (12)

Q7. medium

What is the result type of: 3 / 2 in Python 3?

print(3 / 2)         # 1.5
print(3 // 2)        # 1
print(type(3 / 2))   # <class 'float'>
  1. A int
  2. B float
  3. C decimal
  4. D Error

Answer: B — float

The / operator always returns a float in Python 3, even for integers: 3 / 2 = 1.5. Use // for integer (floor) division.

  • Key difference from Python 2, where 3 / 2 gave 1.

DivisionFloor division

Q9. medium

What is the output of: print(bool(""))

print(bool(""))      # False
print(bool("hi"))    # True
print(bool(0))        # False
print(bool([]))       # False
  1. A True
  2. B False
  3. C Error
  4. D None

Answer: B — False

An EMPTY string is falsy - bool("") is False. Empty containers ([], {}, ()), the number 0, and None are all falsy. Everything else is truthy.

Truthinessbool()

Q13. medium

What is the type of the result: 2 + 3.0

print(2 + 3.0)        # 5.0
print(type(2 + 3.0))  # <class 'float'>
  1. A int
  2. B float
  3. C str
  4. D Error

Answer: B — float

When int and float mix in arithmetic, Python converts the int to float automatically: 2 + 3.0 = 5.0 (a float). The 'wider' type wins.

Type promotionMixed arithmetic

Q14. medium

What happens with: int("3.5")

# int("3.5")          # ValueError!
print(float("3.5"))    # 3.5
print(int(float("3.5")))  # 3
  1. A Returns 3
  2. B Returns 3.5
  3. C Returns 4
  4. D ValueError - int() can't parse a decimal string

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
  1. A 0, int
  2. B Empty string, str
  3. C None, NoneType - it represents 'no value'
  4. 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
  1. A 13
  2. B 1000.0
  3. C 103
  4. 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)

print(isinstance(5, int))          # True
print(isinstance(5.0, (int, float)))  # True - multiple types
  1. A A - type() is faster
  2. B B - isinstance() also accepts subclasses and is the Python convention
  3. C Both are identical
  4. 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?

z = 2 + 3j
print(z.real)   # 2.0
print(z.imag)   # 3.0
print(type(z))  # <class 'complex'>
  1. A 2 + 3i
  2. B complex only via import
  3. C 2 + 3j
  4. D (2, 3)

Answer: C — 2 + 3j

Python uses j (from engineering) for the imaginary unit: 2 + 3j is a complex number with real part 2 and imaginary part 3. No import needed.

Numeric typescomplex()

Q23. medium

What is the output of: print(int("ff", 16))

print(int("ff", 16))    # 255 (hex)
print(int("1010", 2))   # 10 (binary)
print(int("77", 8))     # 63 (octal)
  1. A Error
  2. B 255
  3. C ff
  4. D 1616

Answer: B — 255

int() takes an optional base: int("ff", 16) reads "ff" as hexadecimal, which is 255 in decimal. Base 2 works too: int("1010", 2) = 10.

Number baseshex()bin()

Q26. medium

What is the output of: print(str(3.0))

print(str(3.0))    # 3.0
print(str(True))   # True
print(str(None))   # None
  1. A 3
  2. B 3.0
  3. C '3.0'
  4. D Error

Answer: B — 3.0

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))

print(10 == 10.0)              # True - equal values
print(type(10) == type(10.0))  # False - different types
  1. A True
  2. B False
  3. C Error
  4. D 10

Answer: B — False

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
  1. A list, tuple, dict, set
  2. B int, float, bool, str
  3. C array, vector, map, table
  4. 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.

ListsTuplesDictionariesSets

Hard questions (5)

Q10. hard

What is the output of: print(bool("False"))

print(bool("False"))  # True (non-empty text!)
print(bool("0"))       # True
print(bool(""))        # False
  1. A False
  2. B True
  3. C Error
  4. D None

Answer: B — True

Tricky! "False" is a NON-EMPTY string, and any non-empty string is truthy - regardless of what it says. Only the empty string "" is falsy.

  • This matters when reading 'True'/'False' from files or input - compare the text instead.

TruthinessCommon gotchas

Q17. hard

What is the output of: print(0.1 + 0.2 == 0.3)

print(0.1 + 0.2)            # 0.30000000000000004
import math
print(math.isclose(0.1 + 0.2, 0.3))  # True
  1. A True
  2. B False
  3. C Error
  4. D 0.3

Answer: B — False

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.

Floating point precisionmath.isclose()

Q19. hard

What is the output of: print(True + True)

print(True + True)   # 2
print(True * 10)     # 10
marks = [True, False, True]
print(sum(marks))    # 2 - counts passes
  1. A TrueTrue
  2. B 2
  3. C True
  4. D Error

Answer: B — 2

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))

print(isinstance(True, bool))  # True
print(isinstance(True, int))   # True (subclass!)
  1. A False
  2. B True
  3. C Error
  4. D None

Answer: B — True

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
  1. A It overflows above 2 billion like in C
  2. B It has unlimited precision - it can hold arbitrarily large numbers
  3. C It maxes out at 64 bits
  4. 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.

Arbitrary precisionPython 2 vs 3

Practice “Data Types & Conversion” as a quiz