Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m