Practice: Python Data Types

« Read the chapter

Q1. Print the type of a value

BeginnerCoding

Print the type of the value 3.5.

Goal: Use type().

Q2. Convert text to number and add

BeginnerCoding

Given s = "40", convert it to an int, add 2 and print the result.

Goal: Convert str to int.

Q3. Remove duplicates

IntermediateCoding

From data = [4, 2, 4, 1, 2], print the sorted unique values as a list.

Goal: Use a set for uniqueness.

Q4. Dictionary lookup

BeginnerCoding

Create a dict with keys name and age for a person (Mia, 30). Print the age.

Goal: Use a dict.

Q5. Check with isinstance

BeginnerCoding

Print True if the value 10 is an int, using isinstance.

Goal: Use isinstance().

Q6. Single-item tuple

IntermediateCoding

Create a tuple with the single item 9 and print its type and its length.

Goal: Create a one-item tuple.

Q7. Count types in a list

IntermediateCoding

Given items = [1, "a", 2.5, 3, "b"], print how many are strings.

Goal: Filter values by type.

Q8. Safe bool of text

IntermediateCoding

Print the Boolean value of an empty string and of the string 0.

Goal: Understand truthiness.

Q9. Type of a division

BeginnerOutput

What does this print?

Code
print(type(10 / 2))

Goal: Know division types.

Q10. Floor division type

BeginnerOutput

What does this print?

Code
print(10 // 2)
print(type(10 // 2))

Goal: Know floor division.

Q11. Shared list

IntermediateOutput

What does this print?

Code
a = [1, 2]
b = a
b.append(3)
print(a)

Goal: Understand mutability.

Q12. Tuple concatenation

IntermediateOutput

What does this print?

Code
x = (1, 2)
y = x + (3,)
print(x)
print(y)

Goal: Understand immutability.

Q13. Bool as number

IntermediateOutput

What does this print?

Code
print(True + True)
print(isinstance(True, int))

Goal: Know bool is an int.

Q14. Empty braces

BeginnerOutput

What does this print?

Code
a = {}
b = set()
print(type(a))
print(type(b))

Goal: Remember the empty-set rule.

Q15. Truthiness of values

IntermediateOutput

What does this print?

Code
print(bool(0), bool(1), bool([]), bool([0]), bool(None))

Goal: Understand truthiness.

Q16. Fix the TypeError

BeginnerDebugging

This program should print 15 but raises a TypeError. Fix it.

Code
total = "10" + 5
print(total)

Goal: Fix a type mismatch.

Q17. Fix the item assignment

IntermediateDebugging

This code tries to change the first letter of a string and fails. Make word become Cat.

Code
word = "cat"
word[0] = "C"
print(word)

Goal: Work with immutable strings.

Q18. Fix the empty set

IntermediateDebugging

The code should print an empty set's length 0 and add item 5, giving 1. Currently it fails. Fix it.

Code
s = {}
s.add(5)
print(len(s))

Goal: Fix the empty-set trap.

Q19. Mutable or immutable?

BeginnerConceptual

List the built-in types list, str, tuple, dict, int and set under the headings mutable and immutable.

Goal: Classify types.

Q20. Choose a data type

IntermediateApplication

Which type would you use to store the days of a week that never change? Which for a shopping list that changes? Explain briefly.

Goal: Choose a suitable type.