Open In App

Output of python program | Set 15 (Modules)

Last Updated : 26 Apr, 2022
Like Article

Prerequisite: Regular Expressions Note: Output of all these programs is tested on Python3 

1) Which of the options below could possibly be the output of the following program? 


from random import randrange
L = list()
for x in range(5):
    L.append(randrange(0, 100, 2)-10)
# Choose which of outputs below are valid for this code.   

a) [-8, 88, 8, 58, 0] b) [-8, 81, 18, 46, 0] c) [-7, 88, 8, 58, 0] d) [-8, 88, 94, 58, 0] 

Ans. (a) Explanation: The for loop will result in appending 5 elements to list L. Range of the elements lies from [0, 98] – 10 = [-10, 88], which rules out option (d). The upper range is 98 because the step size is 2, thus option (c) and (b) are invalid. Also note that each time you may not get the same output or the one in the options as the function is random. 

2) What is the output of the following program? 


from math import *
a = 2.13
b = 3.7777
c = -3.12
print(int(a), floor(b), ceil(c), fabs(c))

a) 2 3 -4 3 b) 2 3 -3 3.12 c) 2 4 -3 3 d) 2 3 -4 3.12 

Ans. (b) Explanation: int() returns the integer value of a number, int(2.13) = 2. floor() returns the largest integer lesser or equal to the number, floor(3.777) = 3. ceil() returns smallest integer greater or equal to the number, ceil(-3.12) = -3. fabs() return the modulus of the number, thus fabs(-3.12) = 3.12. 

3) What is the output of the following program? 


import re
p = re.compile('\d+')
print(p.findall("I met him once at 11 A.M. on 4th July 1886"), end = " ")
p = re.compile('\d')
print(p.findall("I went to him at 11 A.M."))

a) [’11’, ‘4’, ‘1886’, ’11’] b) [‘1141886’] [‘1’, ‘1’] c) [’11’, ‘4’, ‘1886’] [’11’] d) [’11’, ‘4’, ‘1886’] [‘1’, ‘1’] 

Ans. (d) Explanation: \d is equivalent to [0-9] and \d+ will match a group on [0-9], group of one or greater size. In first statement, group of digits are 11, 4, 1886. In the second statement, \d will treat each digit as different entity, thus 1, 1. 

4) What is the output of the following program? 


import re
print(re.sub('ge', '**', 'Geeksforgeeks', flags = re.IGNORECASE), end = " ")
print(re.sub('ge', '**', 'Geeksforgeeks'))

a) **eksfor**eks **eksfor**eks b) **eksfor**eks Geeksfor**eks c) **Geeksfor**geeks Geeksfor**geeks d) TypeError: ‘str’ object does not support item assignment 

Ans. (b) Explanation: In the first print statement, all ‘ge’ will be replaced ‘**’, and case is ignored. Case is not ignored in 2nd statement, thus ‘ge’ will be replaced but not ‘Ge’. 

5) Which of the options below could possibly be the output of the following program? 


import math
import random
L = [1, 2, 30000000000000]
for x in range(3):
    L[x] = math.sqrt(L[x])
# random.choices() is available on Python 3.6.1 only.
string = random.choices(["apple", "carrot", "pineapple"], L, k = 1)

a) [‘pineapple’] b) [‘apple’] c) ‘pineapple’ d) both a and b 

Ans. (d) Explanation: Two modules math and random are used, L after the for loop will be [1.0, 1.4142135623730951, 5477225.575051662]. choices() has choice as 1st parameter and their weights as second parameter, k is the number valued needed from choice. The answer will come out to ‘pineapple’ almost always due to it’s weight but ‘apple’ and ‘carrot’ may turn out to be the output at times. 

Previous Article
Next Article

Similar Reads

Reloading modules in Python
The reload() is a previously imported module. If you've altered the module source file using an outside editor and want to test the updated version without leaving the Python interpreter, this is helpful. The module object is the return value. Reloading modules in Python2.xreload(module)For above 2. x and <=Python3.3import imp imp.reload(module)
1 min read
Python | Opaque Pointers in C extension modules
Let's discuss an extension module that needs to handle a pointer to a C data structure, without exposing any internal details of the structure to Python. Opaque data structures can be easily handled by wrapping them inside capsule objects as shown in the code snippet below. Code #1 : typedef struct Point { double x, y; } Point; extern double distan
2 min read
10 Interesting modules in Python to play with
Python is a high level, interpreted and general-purpose dynamic programming language that focuses on code readability. It is used in many organizations as it supports multiple programming paradigms. It also performs automatic memory management. It is one of the world’s most popular, in-demand programming languages. This is for many reasons: It’s ea
7 min read
Create and Import modules in Python
In Python, a module is a self-contained Python file that contains Python statements and definitions, like a file named, can be considered as a module named GFG which can be imported with the help of import statement. However, one might get confused about the difference between modules and packages. A package is a collection of modules in dir
3 min read
How to Dynamically Load Modules or Classes in Python
Python provides a feature to create and store classes and methods and store them for further use. The file containing these sets of methods and classes is called a module. A module can have other modules inside it. Note: For more information, refer to Python Modules Example: A simple example of importing a module is shown below in which, there are
3 min read
Basics Of Python Modules
A library refers to a collection of modules that together cater to a specific type of needs or application. Module is a file(.py file) containing variables, class definitions statements, and functions related to a particular task. Python modules that come preloaded with Python are called standard library modules. Creating our module We will be crea
3 min read
Modules available for Serialization and Deserialization in Python
Python provides three different modules which allow us to serialize and deserialize objects : Marshal ModulePickle ModuleJSON Module 1. Marshal Module: It is the oldest module among these three. It is mainly used to read and write the compiled byte code of Python modules. Even we can use marshal to serialize Python objects, but it is not recommende
3 min read
Best Python Modules for Automation
Automation is an addition of technology that performs tasks with reduced human assistance to processes that facilitate feedback loops between operations and development teams so that iterative updates can be deployed faster to applications in production. There are different types of automation library in Python: GUI AutomationFull Stack AutomationT
3 min read
Import Modules From Another Folder in Python
In this article, we are going to see how to import a module from another folder, While working on big projects we may confront a situation where we want to import a module from a different directory, here we will see the different ways to import a module form different folder. It can be done in two ways: Using sys.pathUsing PythonPath. Create a mod
2 min read
How to create modules in Python 3 ?
Modules are simply python code having functions, classes, variables. Any python file with .py extension can be referenced as a module. Although there are some modules available through the python standard library which are installed through python installation, Other modules can be installed using the pip installer, We can also create our own pytho
4 min read
Article Tags :
Practice Tags :