Practice Problems: Data Types

Now it’s time to test your understanding of Python’s data types, indexing, and type conversion. Work through these problems to solidify your knowledge.

Problem 1: The User Profile

Task: Create a user profile using a dictionary. The profile should contain the user’s name (string), age (integer), a list of their favorite hobbies (list of strings), and whether they are a premium member (boolean). Finally, print the type of each value in the dictionary.

Pyground

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:

Problem 2: String Manipulation and Slicing

Task: You are given a string: "Learning Python is fun!". Perform the following operations:

  1. Extract the word “Python” from the string using slicing.
  2. Reverse the entire string.
  3. Extract every second character from the string.

Pyground

Perform slicing operations on the given string to extract specific parts, reverse it, and step through it.

Expected Output:


Extracted word: 'Python'
Reversed string: '!nuf si nohtyP gninraeL'
Every second character: 'Lann yhnisfn'

Output:

Problem 3: Type Conversion Challenge

Task: You receive a list of strings representing different prices: ['99.99', '15.50', '100', '75.25']. Calculate the total sum of these prices. To do this, you will need to convert each string price to a number (float) before you can add them together.

Pyground

Calculate the sum of prices stored as strings by first converting them to floats.

Expected Output:

The total sum of prices is: 290.74

Output:

Problem 4: Finding Unique Items

Task: You are given a list of numbers with many duplicates: [10, 20, 30, 20, 10, 50, 60, 40, 80, 50, 40]. Your goal is to find all the unique numbers from this list and then convert the result back into a sorted list.

Hint: The set data type is perfect for automatically removing duplicates!

Pyground

Use a set to find the unique numbers from a list, then convert it back to a sorted 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]

Output: