Practice Problems: Data Types
Now it is time to test your understanding of Python’s data types, indexing, and type conversion. Work through these hands-on challenges to solidify your coding skills!
We have pre-loaded starter templates for you in each interactive editor. Read the task instructions, write your solution directly inside the editor, and click Run Code to check your results. If you get stuck, you can click Show Solution to view the answer!
Challenge 1: The User Profile
Your Task: Create a user profile using a Python dictionary. The dictionary must store the user’s name (text string), their age (integer), a list of their favorite hobbies (list of strings), and whether they are a premium member (boolean). Finally, print the value and type of each key in the dictionary.
PygroundTry It Out
Create a dictionary for a user profile and print the type of each of its values.
Expected Output:
Name: Alex, Type: <class 'str'> Age: 28, Type: <class 'int'> Hobbies: ['reading', 'hiking', 'coding'], Type: <class 'list'> Premium Status: False, Type: <class 'bool'>
Output:
Challenge 2: Slicing and Reversing Strings
Your Task: You are given the string "Learning Python is fun!". Perform the following operations:
- Extract the word
"Python"from the string using positive slicing. - Reverse the entire string.
- Extract every second character from the string.
PygroundTry It Out
Slice the string to extract 'Python', reverse it, and grab every second character.
Expected Output:
Extracted word: 'Python' Reversed string: '!nuf si nohtyP gninraeL' Every second character: 'Lann yhni u!'
Output:
Challenge 3: Type Casting Challenge
Your Task: You receive a list of prices represented as text strings: ['99.99', '15.50', '100', '75.25']. Write a loop to calculate the total sum of these prices. You will need to explicitly convert each string price to a decimal float before adding them together!
PygroundTry It Out
Calculate the total sum of prices by casting the strings to float numbers.
Expected Output:
The total sum of prices is: 290.74
Output:
Challenge 4: Finding Unique Sorted Items
Your Task: You are given a list of numbers containing several duplicates: [10, 20, 30, 20, 10, 50, 60, 40, 80, 50, 40]. Write a program to extract all unique numbers from this list, and convert the result back into a sorted list.
Hint: The set data type is designed to store only unique elements. It removes all duplicates automatically!
PygroundTry It Out
Use a set to extract unique numbers, then sort the list.
Expected Output:
Unique items (as a set): {40, 10, 80, 50, 20, 60, 30}
Sorted unique list: [10, 20, 30, 40, 50, 60, 80]