w3resource

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.]

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()
Go to the editor
Click me to see the sample solution

2. Write a Python program to select a random element from a list, set, dictionary (value) and a file from a directory. Use random.choice()
Go to the editor
Click me to see the sample solution

3. Write a Python program to generate a random alphabetical character, alphabetical string and alphabetical string of a fixed length. Use random.choice()
Go to the editor
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()
Go to the editor
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()
Go to the editor
Click me to see the sample solution

6. Write a Python program to shuffle the elements of a given list. Use random.shuffle()
Go to the editor
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()
Go to the editor
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()
Go to the editor
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. Go to the editor
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()
Go to the editor
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()
Go to the editor
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()
Go to the editor
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()
Go to the editor
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
Go to the editor
Click me to see the sample solution

2. Write a Python program to configure the rounding to round up and round down a given decimal value. Use decimal.Decimal
Go to the editor
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
Go to the editor
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
Go to the editor
Click me to see the sample solution

5. Write a Python program to configure the rounding to round to the nearest - with ties going towards 0, with ties going away from 0. Use decimal.ROUND_HALF_DOWN, decimal.ROUND_HALF_UP
Go to the editor
Click me to see the sample solution

6. Write a Python program to configure the rounding to round to the nearest, with ties going to the nearest even integer. Use decimal.ROUND_HALF_EVEN
Go to the editor
Click me to see the sample solution

7. Write a Python program to display a given decimal value in scientific notation. Use decimal.Decimal
Go to the editor
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
Go to the editor
Click me to see the sample solution

2. Write a Python program to create a deep copy of a given list. Use copy.copy
Go to the editor
Click me to see the sample solution

3. Write a Python program to create a shallow copy of a given dictionary. Use copy.copy
Go to the editor
Click me to see the sample solution

4. Write a Python program to create a deep copy of a given dictionary. Use copy.copy
Go to the editor
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
Go to the editor
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
Go to the editor
Click me to see the sample solution

3. Write a Python program to parse a given CSV string and get the list of lists of string values. Use csv.reader
Go to the editor
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
Go to the editor
Click me to see the sample solution

5. Write a Python program to skip the headers of a given CSV file. Use csv.reader
Go to the editor
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 specified delimiter. Use csv.reader
Go to the editor
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
Go to the editor
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



Python: Tips of the Day

Creating a sequence of numbers (zero to ten with skips):

>>> range(0,10,2)
[0, 2, 4, 6, 8]