0% found this document useful (0 votes)
19 views9 pages

Python Libraries and Random Module Guide

The document provides an overview of Python libraries and modules, emphasizing the use of the built-in 'random' and 'statistics' libraries for various functions like generating random choices and calculating averages. It also highlights the importance of third-party packages available through PyPI, such as Numpy and Pandas, which enhance Python's functionality. The document serves as a practical guide for utilizing these libraries in programming.

Uploaded by

emonahmed.kl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

Python Libraries and Random Module Guide

The document provides an overview of Python libraries and modules, emphasizing the use of the built-in 'random' and 'statistics' libraries for various functions like generating random choices and calculating averages. It also highlights the importance of third-party packages available through PyPI, such as Numpy and Pandas, which enhance Python's functionality. The document serves as a practical guide for utilizing these libraries in programming.

Uploaded by

emonahmed.kl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Practical-08

Libraries
Generally, libraries are bits of code written by you or
others which you can use in your program.

Python allows you to share functions or features with


others as “modules”.

If you copy and paste code from an old project, chances


are you can create such a module or library that you
could bring into your new project.

2
Random
random is a library that comes with Python that you
could import into your own project.
It’s easier as a coder to stand on the shoulders of prior
coders.
So, how do you load a module into your own program?
You can use the word import in your program.
Inside the random module, there is a built-in function
called [Link](seq). random is the module you
are importing. Inside that module, there is the choice
function. That function takes into it a seq or sequence
that is a list.

3
Random
In your text editor, code as follows:

import random

coin = [Link](["heads", "tails"])


print(coin)

Notice that the list within choice has square braces,


quotes, and a comma. Since you have passed in two
items, Python does the math and gives a 50% chance for
heads and tails. Running your code, you will notice that
this code, indeed, does function well!

4
Random
Consider the function [Link](a, b). This function
will generate a random number between a and b. Modify
your code as follows:

import random

number = [Link](1, 10)


print(number)

Notice that our code will randomly generate a number


between 1 and 10.

5
Random
We can introduce into our card [Link](x) where
it will shuffle a list into a random order.

import random

cards = ["jack", "queen", "king"]


[Link](cards)
for card in cards:
print(card)

Notice that [Link] will shuffle the cards in place.


Unlike other functions, it will not return a value. Instead,
it will take the cards list and shuffle them inside that list.
Run your code a few times to see the code functioning.
6
Statistics
Python comes with a built-in statistics library. How might we
use this module?
average is a function of this library that is quite useful. In your
terminal window, type code [Link]. In the text editor
window, modify your code as follows:

import statistics

print([Link]([100, 90]))

Notice that we imported a different library called statistics. The


mean function takes a list of values. This will print the average
of these values.

7
Packages
One of the reasons Python is so popular is that there are
numerous powerful third-party libraries that add functionality.
We call these third-party libraries, implemented as a folder,
“packages”.
PyPI is a repository or directory of all available third-party
packages currently available.
Some of the most important packages are:
1. Numpy
2. Pandas
3. Matpotlib/Seaborn
4. Openpyxl/xlrd
5. Pyfolio
6. yfinance
7. Numpy-financial

8
That’s All
For Today!
9

You might also like