ppt on python code example

Example: pprint python

# pprint (aka prettyprint) is a function that allow to print objects in a clearer way
# Fist you need to import the pprint module
import pprint
# then you can create a printer whith whatever arguments you need
my_printer = pprint.PrettyPrinter(width=20)
# default arguments are : indent=1, width=80, depth=None, stream=None, *, compact=False, sort_dicts=True

# let's take this list as an exemple:
list1 = [[0,1,2,3,4],[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7]]

print(list1) # will print [[0,1,2,3,4],[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7]]
my_printer.pprint(list1) # will print:
# [[0, 1, 2, 3, 4],
#  [1, 2, 3, 4, 5],
#  [2, 3, 4, 5, 6],
#  [3, 4, 5, 6, 7]]

# You can also formant text whith pprint
output = my_printer.pformat(list1)
# output will be:
"""[[0, 1, 2, 3, 4],
 [1, 2, 3, 4, 5],
 [2, 3, 4, 5, 6],
 [3, 4, 5, 6, 7]]"""

Tags:

Misc Example