w3resource

Python pprint: Exercise, Practice, Solution


Python pprint [6 exercises with solution]


You may read our Python list tutorial before solving the following exercises.

[An editor is available at the bottom of the page to write and execute the scripts.  Go to the editor]

The pprint module provides a capability to "pretty-print" arbitrary Python data structures in a form which can be used as input to the interpreter. The formatted representation keeps objects on a single line if it can, and breaks them onto multiple lines if they don't fit within the allowed width. Construct PrettyPrinter objects explicitly if you need to adjust the width constraint.

1. Write a Python program to print a dictionary, nested dictionary, list of dictionaries of dictionaries using the pprint module.
Click me to see the sample solution

2. Write a Python program to sort the keys of a dictionary before printing it using the pprint module.
Click me to see the sample solution

3. Write a Python program that specifies the width of the output while printing a list, dictionary using the pprint module.
"width" (default 80) specifies the desired maximum number of characters per line in the output. If a structure cannot be formatted within the width constraint, a best effort will be made.
Click me to see the sample solution

4. Write a Python program that specifies the indentation while printing a nested dictionary using the pprint module.
"indent" (default 1) specifies the amount of indentation added for each nesting level.
Click me to see the sample solution

5. Write a Python program to fetch information about a project (from PyPI) using pprint() function.
Click me to see the sample solution

6. Write a Python program to fetch information about a project (i.e. PyPI) using pprint() function. Limit the result to a certain level and specify the width of the content.
Click me to see the sample solution

List: Cheat Sheet

Making a list:

colors = ['Red', 'Blue', 'Green', 'Black', 'White']

Accessing elements:

# Getting the first element
first_col = colors[0]
# Getting the second element
second_col = colors[1]
# Getting the last element
newest_col = colors[-1]

Modifying individual items:

# Changing an element
colors[0] = 'Yellow'
colors[-2] = 'Red'

Adding elements:

# Adding an element to the end of the list
colors.append('Orange')
# Starting with an empty list
colors = []
colors.append('Red')
colors.append('Blue')
colors.append('Green')
# Inserting elements at a particular position
colors.insert(0, 'Violet')
colors.insert(2, 'Purple')

Removing elements:

# Deleting an element by its position
del colors[-1]
# Removing an item by its value
colors.remove('Green')

Popping elements:

# Pop the last item from a list
most_recent_col = colors.pop()
print(most_recent_col)
# Pop the first item in a list
first_col = colors.pop(0)
print(first_col)

List length:

# Find the length of a list
num_colors = len(colors)
print("We have " + str(num_colors) + " colors.")

Sorting a list:

# Sorting a list permanently
colors.sort()
# Sorting a list permanently in reverse alphabetical order
colors.sort(reverse=True)
# Sorting a list temporarily
print(sorted(colors))
print(sorted(colors, reverse=True))
# Reversing the order of a list
colors.reverse()

Looping through a list:

# Printing all items in a list
for col in colors:
 print(col)
# Printing a message for each item, and a separate message afterwards
for col in colors:
 print("Welcome, " + col + "!")
print("Welcome, we're glad to see you all!")

The range() function:

# Printing the numbers 0 to 2000
for num in range(2001):
 print(num)
# Printing the numbers 1 to 2000
for num in range(1, 2001):
 print(num)
# Making a list of numbers from 1 to a million
nums = list(range(1, 1000001))

Simple statistics:

# Finding the minimum value in a list
nums = [23, 22, 44, 17, 77, 55, 1, 65, 82, 2]
num_min = min(nums)
# Finding the maximum value
nums = [23, 22, 44, 17, 77, 55, 1, 65, 82, 2]
num_max = max(nums)
# Finding the sum of all numbers
nums = [23, 22, 44, 17, 77, 55, 1, 65, 82, 2]
total_num = sum(nums)

Slicing a list:

# Getting the first three items
colors = ['Red', 'Blue', 'Green', 'Black', 'White']
first_three = colors [:3]
# Getting the middle three items
middle_three = colors[1:4]
# Getting the last three items
last_three = colors[-3:]

Copying a list:

# Making a copy of a list
colors = ['Red', 'Blue', 'Green', 'Black', 'White']
copy_of_colors = colors[:]

List of Comprehensions:

# Using a loop to generate a list of square numbers
squr = []
for x in range(1, 11):
 sq = x**2
 squr.append(sq)
# Using a comprehension to generate a list of square numbers
squr = [x**2 for x in range(1, 11)]
# Using a loop to convert a list of names to upper case
colors = ['Red', 'Blue', 'Green', 'Black', 'White']
upper_cols = []
for cols in colors:
 upper_cols.append(cols.upper())
# Using a comprehension to convert a list of names to upper case
colors = ['Red', 'Blue', 'Green', 'Black', 'White']
upper_cols = [cols.upper() for cols in colors]

Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.