Output of python program | Set 15 (Modules)
Last Updated :
06 Sep, 2024
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?
PYTHON
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.
print(L)
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?
PYTHON
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?
PYTHON
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?
PYTHON
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?
PYTHON
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)
print(string)
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.
Similar Reads
Output of python program | Set 2
Difficulty level : IntermediatePredict the output of following Python Programs. Program 1: C/C++ Code class Acc: def __init__(self, id): self.id = id id = 555 acc = Acc(111) print (acc.id) Output: 111 Explanation: Instantiation of the class "Acc" automatically calls the method __init__ and passes th
2 min read
Output of Python Program | Set 3
Difficulty level : Intermediate Predict the output of following Python Programs. Program 1: class Geeks: def __init__(self, id): self.id = id manager = Geeks(100) manager.__dict__['life'] = 49 print (manager.life + len(manager.__dict__)) Output:51 Explanation : In the above program we are creating a
2 min read
Output of Python Program | Set 4
Difficulty level : Intermediate Predict the output of the following Python Programs. Program 1: nameList = ['Harsh', 'Pratik', 'Bob', 'Dhruv'] print nameList[1][-1] Output: k Explanation: The index position -1 represents either the last element in a list or the last character in a String. In the abo
2 min read
Output of Python program | Set 5
Predict the output of the following programs: Program 1: [GFGTABS] Python def gfgFunction(): "Geeksforgeeks is cool website for boosting up technical skills" return 1 print (gfgFunction.__doc__[17:21]) [/GFGTABS]Output: coolExplanation: There is a docstring defined for this method,
3 min read
Output of Python program | Set 6 (Lists)
Prerequisite - Lists in Python Predict the output of the following Python programs. These question set will make you conversant with List Concepts in Python programming language. Program 1 Python Code list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print "list1[0
3 min read
Output of Python programs | Set 7
Prerequisite - Strings in Python Predict the output of the following Python programs. These question set will make you conversant with String Concepts in Python programming language. Program 1[GFGTABS] Python var1 = 'Hello Geeks!' var2 = "GeeksforGeeks" print "var1[0]: ",
3 min read
Output of Python programs | Set 8
Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 [GFGTABS] Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) [/GFGTABS]Output: 6Explanation: The beauty of python list datatype is that within
3 min read
Output of Python programs | Set 9 (Dictionary)
Prerequisite: Dictionary 1) What is the output of the following program? [GFGTABS] Python dictionary = {'GFG' : 'geeksforgeeks.org', 'google' : 'google.com', 'facebook' : 'facebook.com' } del dictionary['google']; for key, values in dictionary.
3 min read
Output of Python programs | Set 10 (Exception Handling)
Pre-requisite: Exception Handling in PythonNote: All the programs run on python version 3 and above. 1) What is the output of the following program? [GFGTABS] Python data = 50 try: data = data/0 except ZeroDivisionError: print('Cannot divide by 0 ', end = '') else: print('Divisio
3 min read
Output of python program | Set 11(Lists)
Pre-requisite: List in python 1) What is the output of the following program? Python Code data = [2, 3, 9] temp = [[x for x in[data]] for x in range(3)] print (temp) a) [[[2, 3, 9]], [[2, 3, 9]], [[2, 3, 9]]] b) [[2, 3, 9], [2, 3, 9], [2, 3, 9]] c) [[[2, 3, 9]], [[2, 3, 9]]] d) None of these Ans. (a
3 min read