You are on page 1of 7

PYTHON PROGRAMMING

Lecture# 11

Lecture Content
1. Introduction to Module, Package, and Library
2. Introduction to Random module
Introduction to Module, Package, and Library:
If you want your code to be well organized, it’s a good idea to start by grouping
related code.

Module Package Library


 It is basically a  Python packages are  It is often assumed
bunch of related basically a directory that while a package
code saved in a file of a collection of is a collection of
with the modules. modules, a library is
extension .py.  e.g., Numpy, a collection of
 You use the import pandas. packages
statement to import  e.g., pygame, turtle.
a particular module
in your program.
 e.g., random, re,
datetime

Python modules can get access to code from another module by importing the
file/function using import keyword.

Introduction to Random Module:


Python have a built in module called Random which is used to generate random
numbers or items. It is most helpful when we are working on making different
games. Below are the different functions by random.
1. Random()
The random() method returns a random floating number between 0 and 1.
Syntax
random.random()
Example:
2. Uniform()
The uniform() method returns a random floating number between the two
specified numbers (both included).

Syntax
random.uniform(a, b)
a Required. A number specifying the lowest possible outcome
b Required. A number specifying the highest possible outcome

Example:

3. Randrange()
The randrange() method returns a randomly selected element from the specified
range.

Syntax
random. randrange(start, stop, step)

start Optional. An integer specifying at which position to start.


Default 0
stop Required. An integer specifying at which position to end.
step Optional. An integer specifying the incrementation.
Default 1

Example:
4. Randint()
The randint() method returns an integer number selected element from the
specified range.

Syntax
random.randint(start, stop)

start Optional. An integer specifying at which position to start.


Default 0
stop Required. An integer specifying at which position to end.

Example:

5. Choice()
The choice() method returns a randomly selected element from the specified
sequence.
The sequence can be a string, a range, a list, a tuple or any other kind of
sequence.

Syntax
random.choice(sequence)

Example:
6. Shuffle()
The shuffle() method takes a sequence, like a list, and reorganize the order of
the items.

Syntax
random.shuffle(sequence)

Example:

7. Sample()
The sample() method returns a list with a randomly selection of a specified
number of items from a sequnce.
Note: This method does not change the original sequence.

Syntax
random.sample(sequence, k) { The size of the returned list }

Example:
EXERCISE:

1. Write a Python program that simulates rolling a six-sided dice. Each time
the program is run, it should generate a random number between 1 and 6 and
display the result.
2. Create a password generator that generates a random password of a specified
length. Ask the user for the desired password length and include a mix of
uppercase letters, lowercase letters, numbers, and special characters in the
generated password.
3. Make list of items given by user then shuffle the items randomly and print
the shuffled list.
4. Create a program that stores a collection of inspirational quotes in a list.
Each time the program is run, it should display a random quote from the list.

You might also like