Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Python Libraries Final

The document provides an overview of importing modules in Python, specifically focusing on how to import entire modules or selected objects from them. It also explains the use of the random module for generating random numbers, detailing various functions such as random(), randint(), and uniform(). Additionally, the document includes examples and exercises to illustrate the concepts of module importing and random number generation.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Libraries Final

The document provides an overview of importing modules in Python, specifically focusing on how to import entire modules or selected objects from them. It also explains the use of the random module for generating random numbers, detailing various functions such as random(), randint(), and uniform(). Additionally, the document includes examples and exercises to illustrate the concepts of module importing and random number generation.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Importing Modules

in a
Python Program
CLASS-XI

SUBJECT : (Computer Science-XII)


CHAPTER NUMBER: 04
CHAPTER NAME : Using Python Libraries
Importing Module in a python program:

• If you want to use the definitions inside a module, you need to first
import the module in your program.

• use import statement to import the module in your program

• import statement can be used in two ways:

• to import entire module


• to import selected objects from a module
Importing Entire Module:

To import entire module, the import statement can be used as:


import module1 [, module2 [,. . . . module ] ]
import time
To import two modules namely decimals and fractions, we will write,
import decimals, fractions
After importing module we can use access the members from the module through dot
notation:
<module_name>.<function_name>()
Importing Selected Objects from a Module:

If you want to import some selected items, not all from a module, then
you can use from <module> import used as:
from <module> import <objectname> [, <objectname> [,. . . . ] ] |*
To import single object:
To import constant pi from math module we can write:
from math import pi
To import multiple object:
To import two functions sqrt() and pow() from math module we can write:
from math import sqrt, pow
To import all objects of a module:
from <module name> import *
Using random module:

● Python has a module named random that provides random number generators.

● A random number means- a number generated by chance, randomly.

● To use random number generators in the program, first we need to import the

random module command as:

import random
Random number generator functions:

random() it returns a random floating point number N in the range


[0.0 to 1.0], i.e, 0.0 < N < 1.0. Notice that the number
generated with random() will always be less than 1.0.(only
lower limit is inclusive)

randint(a, b) it returns a random integer N in the range(a, b),


i.e, a <= N <=b( both range limits are inclusive).

random.uniform(a, b) it returns a random floating point number such that


a <= N <=b for a <= b and b <= N <=a for b < a

random.randrange(stop) it returns a randomly selected element from 0 to stop-1


random.randrange(start, stop[, step])
Examples on generating random number:

1. To generate a random floating point number between 0.0 to 1.0.


>> import random
>> print(random.random())
0.022353193431 -> generated between range 0.0 to 1.0

2. To generate a random floating point number between range lower to upper.


(a)multiply random() with the difference of upper limit with lower limit,
(upper - lower)
(b)add it to lower limit
To generate between 15 to 35 using random() (35 not included), we ,may write
>> import random
>> print(random.random()*(35 - 15) + 15
28.3071872734 -> generated between range 15 to 35
Examples on generating random number:

3. To generate random number in range 15 to 35 using randint(), we may write:


>> print(random.randint(15, 35))
16 -> output generated is integer between range 15 to 35
4. To generate a random floating point number in the range 11 .. 55 or 111 … 55
>> random.uniform(11, 55)
41.34518981317353
>> random.uniform(111, 55)
60.03906551659219
5. To generate a random integer in the range 23 .. 47 with a step 3 or 0 .. 235
>> random.randrange(23, 47, 3)
38
>> random.randrange(235)
126
Examples:

1. Given the following python code, which is repeated four times. What could be the
possible set of outputs out of given four sets (dddd represent any combination of
digits) ?
>> import random
>> print(15 + random.random() * 5)

(i) 17.dddd, 19.dddd, 20.dddd, 15.dddd


(ii) 15.dddd, 17.dddd, 19.dddd, 18.dddd
(iii) 14.dddd, 16.dddd, 18.dddd, 20.dddd
(iv) 15.dddd, 15.dddd, 15.dddd, 15.dddd
Explanation:

Option (ii) & (iv) are the correct possible output because:

(a) random() generates number N between range 0.0 < N < 1.0
(b) when it is multiplied with 5, the range becomes 0.0 to < 5
(c) when 15 is added to it, the range becomes 15 to < 20

only option (ii) & (iv) fulfill the condition of range 15 to < 20
Examples:

2. What could be the minimum and maximum numbers by the following code?
>> import random
>> print(random.randint(3, 10) - 3)
Explanation:

minimum possible number = 0


maximum possible number = 7
Because,
• randint(3, 10) would generate a random integer in the range 3 to 10
• subtracting 3 from it would change the range to 0 to 7
• (because if randint(3,10) generates 10 then -3 would make it 7;
similarly, for lowest generated value 3, it will make it 0)
Examples:

3. In a school fest, three randomly chosen students out of 100 students(having roll
number 1-100) have to represent bouquets to the guests. Help the school
authorities choose three students randomly.
Solution:
import random
s1 = random.randint(1, 100)
s2 = random.randint(1, 100)
s3 = random.randint(1, 100)
print(s1, s2, s3)

for i in range(3):
print(random.randint(1, 100))
1. Which of the following is not a function/method of the random module in Python

(a) randfloat() (b) randint() (c) random() (d) randrange()

2. Identify the correct possible output for the following Python code:
import random
for N in range(2,5,2) :
print(random.randrange(1,N),end="#")

(a) 1#3#5# (b) 2#3# (c)1#4# (d) 1#3#


import random
print(int( 100 + random.randint(5, 10) , end=” “ )
print(int( 100 + random.randint(5, 10) , end=” “ )
print(int( 100 + random.randint(5, 10) , end=” “ )
print(int( 100 + random.randint(5, 10) ) )

What could be the possible outputs out of the given four choices? Also, write the
minimum & maximum value that can be generated.

(i) 102 105 104 105 (ii) 110 103 104 105
(iii) 105 107 105 110 (iv) 110 105 105 110
import math
import random
print(str( int(math.pow(random.randint(2,4), 2) ) ), end = ‘ ‘ ‘ )
print(str( int( math.pow( random.randint(2, 4), 2) ) ), end = ‘ ‘ )
print(str( int( math.pow( random.randint(2,4), 2) ) )

What could be the possible outputs out of the given choices?

(i) 2 3 4 (ii) 9 4 4 (iii) 16 16 16 (iv) 2 4 9 (v) 4 9 4 (vi) 4 4 4


import random
X=3
N=random.randint(1,X)
for i in range(N):
print(i, '#' ,i+1)

(a) What is the minimum and maximum number of times the loop will execute?
(b) Find out, which line of output(s) out of (i) to (iv) will not be expected from the
program?

i. 0#1 ii.1#2 iii. 2#3 iv. 3#4


Area=["NORTH","SOUTH","EAST","WEST"]
for I in range (3) :
ToGo=random.randrange(0,2)+1
print(Area[ToGo],end=":")

(a) What is the minimum and maximum value assigned to the variable ToGo?

(b) Find out, which line of output(s) out of (i) to (iv) will not be expected from
the program?

i. SOUTH:EAST :SOUTH: ii. NORTH :SOUTH:EAST :


iii. SOUTH:EAST :WEST : iv. SOUTH:EAST :EAST :
city = ["DEL", "CHN", "KOL","BOM","BNG"]
for i in range(1,4):
Fly=random.randrange(0,2)+1
print(city[Fly],end=":")

(a) What is the minimum and maximum value assigned to variable FLY?
(b) Find out, which line of output(s) out of (i) to (iv) will not be expected from the
program?

i. DEL:CHN :KOL: ii. CHN:KOL:CHN : iii. KOL:BOM:BNG: iv. KOL:CHN :KOL:


What will be the output of the following code?
import random
List = [ "Delhi","Mumbai","Chennai","Kolkata" ]
for y in range(4):
x = random.randint(1,3)
print( List [x] , end="#" )

a. Delhi#Mumbai#Chennai#Kolkata#
b. Mumbai#Chennai#Kolkata#Mumbai#
c. Mumbai# Mumbai #Mumbai # Delhi#
d. Mumbai# Mumbai #Chennai # Mumbai
What possible outputs(s) are expected to be displayed on screen at the time
of execution of the program from the following code?
Also specify the maximum values that can be assigned to each of the
variables Lower and Upper.

import random
AR=[20,30,40,50,60,70]
Lower=random.randint(1,3)
Upper =random.randint(2,4)
for K in range(Lower, Upper +1):
print (AR[K],end=”#“)

(i) 10#40#70# (ii) 30#40#50# (iii)50#60#70# (iv)40#50#70


Observe the following program and answer the questions that follow:
low=25
point=5
for i in range (1,5):
Number=low+ random.randrange(0,point)
print(Number,end=":")
point -= 1

(a) What is the minimum and maximum value assigned to the variable Number?
(b) Find out, which line of output(s) out of (i) to (iv) will not be expected from the
program?

#Output Options:
i. 29:26:25 :28: ii. 24: 28:25 :26 : iii.29:26:24 :28: iv. 29:26:25 :26 :
What possible output(s) are expected to be displayed on screen at the time of
execution of the program from the following code? Also specify the minimum
and maximum values that can be assigned to the variable End.

import random
Colours=["VIOLET","INDIGO","BLUE","GREEN","YELLOW","ORANGE","RED"]
End = randrange(2)+3
Begin = randrange(End)+1
for i in range(Begin,End):
print(Colours[i],end="&")

(i) INDIGO&BLUE&GREEN& (ii) VIOLET&INDIGO&BLUE&

(iii) BLUE&GREEN&YELLOW& (iv) GREEN&YELLOW&ORANGE&


Home Work:
1. What are the possible output(s) of the following code? Also specify the
maximum and minimum values that can be assigned to variable PICK.
import random
PICK = random.randint(0,3)
CITY= [“Durgapur”, “Asansol”, “Bokaro”,”Dhanbad”]
for x in CITY:
for y in range(1, PICK):
print (x, end = “ “)
print()

i) Durgapur Durgapur (ii) Durgapur (iii) Durgapur Durgapur (iv) Durgpur


Asansol Asansol Asansol Asansol Asansol Asansol
Bokaro Bokaro Bokaro Durgapur Asansol Bokaro Bokaro
Dhanbad Dhanbad Dhanbad Bokaro Durgapur Bokaro
Asansol Bokaro Dhanbad
3. What are the possible outcome(s) executed from the following code? Also
specify the maximum and minimum? values that can be assigned to variable
NUMBER.

STRING = "CBSEONLINE"
NUMBER = random.randint(0, 3)
N=9
while STRING [N] != 'L':
print(STRING[N] + STRING[NUMBER] + '#',end = ' ')
NUMBER = NUMBER 1
N=N-1

(i) ES#NE#IO# (ii) LE#NO#ON# (iii) NS#IE#LO# (iv) EC#NB#IS#


5. Consider the following code:
import random
print(int( 20 + random.random() * 5 ) , end=” “ )
print(int( 20 + random.random() * 5 ) , end=” “ )
print(int( 20 + random.random() * 5 ) , end=” “ )
print(int( 20 + random.random() * 5 ) )

What could be the possible outputs out of the given four choices? Also, write the
minimum & maximum value that can be generated.
(i) 20 22 24 25 (ii) 22 23 24 25 (iii) 23 24 23 24 (iv) 21 21 21 21
What are the possible output(s) of the following code? Also specify the maximum and
minimum values that can be assigned to variable PICK.

import random
PICK = random.randint(0,3)
CITY= [“DELHI”, “MUMBAI”, “CHENNAI”,”KOLKATA”]
for I in CITY:
for J in range(1, PICK):
print (I, end = “ “)
print()

I. DELHIDELHI II. DELHI III. DELHI IV. DELHI


MUMBAIMUMBAI DELHIMUMBAI MUMBAI MUMBAIMUMBAI
CHENNAICHENNAI DELHIMUMBAICHENNAI CHENNAI KOLKATAKOLKATAKOLKATA
Observe the following program and answer the questions that follow:
import random
myNumber = random.randint(0, 3)
COLOR = ["YELLOW","WHITE","BLACK","RED"]
for I in COLOR:
for J in range(1, myNumber):
print( I , end = "*" )
print()
(a) What is the minimum and maximum number of times the loop will execute?
(b) Find out, which line of output(s) out of (i) to (iv) will not be expected from the
program?
I. RED* II. YELLOW* III. WHITE*WHITE* IV. YELLOW*
WHITE*WHITE* YELLOW*YELLOW* WHITE*WHITE*
BLACK*BLACK* BLACK*BLACK* BLACK*BLACK*BLACK*
RED*RED* RED*RED* RED*RED*RED*RED*RED*
What possible outputs(s) are expected to be displayed on screen at the
time of execution of the program from the following code?
Also specify the maximum values that can be assigned to each of the
variables Lower and Upper.

import random
AR = [ 20,30,40,50,60,70 ]
Lower = random.randint(1,3)
Upper = random.randint(2,4)
for K in range(Lower, Upper +1):
print ( AR[K] , end = ”#“ )

(i) 10#40#70# (ii) 30#40#50# (iii) 50#60#70# (iv) 40#50#70#


THANKING YOU
ODM EDUCATIONAL GROUP

You might also like