Python modules: Exercises, Practice, Solution
This resource offers a total of 155 Python built-in Modules problems for practice. It includes 31 main exercises, each accompanied by solutions, detailed explanations, and four related problems.
Python comes with a library of standard modules. Some modules are built into the interpreter; these provide access to operations that are not part of the core of the language but are nevertheless built in, either for efficiency or to provide access to operating system primitives such as system calls.
Following exercises based on important methods of useful Python built in modules.
[An Editor is available at the bottom of the page to write and execute the scripts.]
Module - random
1. Random Value Generation
Write a Python program to generate a random color hex, a random alphabetical string, random value between two integers (inclusive) and a random multiple of 7 between 0 and 70. Use random.randint()
Click me to see the sample solution
2. Random Element Selection
Write a Python program to select a random element from a list, set, dictionary-value, and file from a directory. Use random.choice()
Click me to see the sample solution
3. Random Alphabetical Generation
Write a Python program that generates random alphabetical characters, alphabetical strings, and alphabetical strings of a fixed length. Use random.choice()
Click me to see the sample solution
4. Seeded Random Float Generation
Write a Python program to construct a seeded random number generator, also generate a float between 0 and 1, excluding 1. Use random.random()
Click me to see the sample solution
5. Random Integer and Date with randrange
Write a Python program to generate a random integer between 0 and 6 - excluding 6, random integer between 5 and 10 - excluding 10, random integer between 0 and 10, with a step of 3 and random date between two dates. Use random.randrange()
Click me to see the sample solution
6. List Shuffle
Write a Python program to shuffle the elements of a given list. Use random.shuffle()
Click me to see the sample solution
7. Uniform Random Float Generation
Write a Python program to generate a float between 0 and 1, inclusive and generate a random float within a specific range. Use random.uniform()
Click me to see the sample solution
8. Random Sample from List
Write a Python program to create a list of random integers and randomly select multiple items from the said list. Use random.sample()
Click me to see the sample solution
9. Set Seed and Random Float
Write a Python program to set a random seed and get a random number between 0 and 1. Use random.random.
Click me to see the sample solution
Module - types
1. User-Defined Function Checker
Write a Python program to check if a function is a user-defined function or not. Use types.FunctionType, types.LambdaType()
Click me to see the sample solution
2. User-Defined Method Checker
Write a Python program to check if a given value is a method of a user-defined class. Use types.MethodType()
Click me to see the sample solution
3. Generator Function Checker
Write a Python program to check if a given function is a generator or not. Use types.GeneratorType()
Click me to see the sample solution
4. Compiled Code and Module Checker
Write a Python program to check if a given value is compiled code or not. Also check if a given value is a module or not. Use types.CodeType, types.ModuleType()
Click me to see the sample solution
Module - decimal
1. Decimal Construction
Write a Python program to construct a Decimal from a float and a Decimal from a string. Also represent the decimal value as a tuple. Use decimal.Decimal
Click me to see the sample solution
2. Rounding Up/Down Configuration
Write a Python program to configure rounding to round up and round down a given decimal value. Use decimal.Decimal
Click me to see the sample solution
3. Round to Nearest 0.10 (with 0.05 Exception)
Write a Python program to round a decimal value to the nearest multiple of 0.10, unless already an exact multiple of 0.05. Use decimal.Decimal
Click me to see the sample solution
4. Floor and Ceiling Rounding
Write a Python program to configure the rounding to round to the floor, ceiling. Use decimal.ROUND_FLOOR, decimal.ROUND_CEILING
Click me to see the sample solution
5. Rounding with Tie-Breaking Rules
Write a Python program that can be configured to round to the nearest - with ties going towards 0 and ties going away from 0. Use decimal.ROUND_HALF_DOWN, decimal.ROUND_HALF_UP
Click me to see the sample solution
6. Round to Nearest Even (Bankers' Rounding)
Write a Python program to configure rounding to round to the nearest integer, with ties going to the nearest even integer. Use decimal.ROUND_HALF_EVEN
Click me to see the sample solution
7. Scientific Notation Display
Write a Python program to display a given decimal value in scientific notation. Use decimal.Decimal
Click me to see the sample solution
Module - copy
1. Shallow Copy of List
Write a Python program to create a shallow copy of a given list. Use copy.copy
Click me to see the sample solution
2. Deep Copy of List
Write a Python program to create a deep copy of a given list. Use copy.copy
Click me to see the sample solution
3. Shallow Copy of Dictionary
Write a Python program to create a shallow copy of a given dictionary. Use copy.copy
Click me to see the sample solution
4. Deep Copy of Dictionary
Write a Python program to create a deep copy of a given dictionary. Use copy.copy
Click me to see the sample solution
Module - csv
1. CSV Content Reader
Write a Python program to read and display the content of a given CSV file. Use csv.reader
Click me to see the sample solution
2. CSV Line Counter
Write a Python program to count the number of lines in a given CSV file. Use csv.reader
Click me to see the sample solution
3. CSV String Parser
Write a Python program to parse a given CSV string and get a list of lists of string values. Use csv.reader
Click me to see the sample solution
4. Current Line Reader from CSV
Write a Python program to read the current line from a given CSV file. Use csv.reader
Click me to see the sample solution
5. CSV Header Skipper
Write a Python program to skip the headers of a given CSV file. Use csv.reader
Click me to see the sample solution
6. CSV Writer with Custom Delimiter
Write a Python program to write (without writing separate lines between rows) and read a CSV file with a specified delimiter. Use csv.reader
Click me to see the sample solution
7. Write CSV from Dictionaries
Write a Python program to write dictionaries and a list of dictionaries to a given CSV file. Use csv.reader
Click me to see the sample solution
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