Python modules: Exercises, Practice, Solution
Python built-in Modules [31 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
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.
Module - random
1. 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. 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. 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. 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. 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. Write a Python program to shuffle the elements of a given list. Use random.shuffle()
Click me to see the sample solution
7. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Write a Python program to create a shallow copy of a given list. Use copy.copy
Click me to see the sample solution
2. Write a Python program to create a deep copy of a given list. Use copy.copy
Click me to see the sample solution
3. Write a Python program to create a shallow copy of a given dictionary. Use copy.copy
Click me to see the sample solution
4. 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. 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. 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. 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. 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. Write a Python program to skip the headers of a given CSV file. Use csv.reader
Click me to see the sample solution
6. 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 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
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/modules/index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics