0% found this document useful (0 votes)
2 views11 pages

Lecture 4 - Libraries +

The document explains the concept of libraries and modules in Python, detailing how to import and use them in programs. It covers command-line arguments, the sys module, packages, and the requests library for API interactions. Additionally, it discusses creating custom libraries and the significance of the __name__ variable in Python scripts.

Uploaded by

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

Lecture 4 - Libraries +

The document explains the concept of libraries and modules in Python, detailing how to import and use them in programs. It covers command-line arguments, the sys module, packages, and the requests library for API interactions. Additionally, it discusses creating custom libraries and the significance of the __name__ variable in Python scripts.

Uploaded by

RunningMan Haha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Libraries

 Libraries are generally files of code that other people have written that you can use
your own program,

 or a library code that you have written that you can use in your own program

1. Modules +

 Module in Python is just a library that typically has one or more functions or other
features built into it

 When we install python, we got whole bunch of module as well

 There is a file called random.py that is written by someone, and we can access to the
functions inside the random.py file

2. Import +

 Import keyword allows us to import the contents of functions from some module in
Python
3. From +
 From allows us to import functions from a module, but it is more specific

 “Import random” means that we are accessing all of the functions in random library

 A downside is that we have to type random.choice, random.that because all of


the functions we are calling have to be associated with that module

 It loads the function choice into namespace

4. Statistics (Built-in)
5. Command-line Arguments, sys

 We can provide arguments in the command line

Sys

 Sys.argv, it stands for argument vector, it means the variables in the command-line
interface

 What is sys.argv[0]? It is the name of the program


 Adding “ “ will allows the terminal understand that is one argument
6. Sys.exit

 It is going to exit my program

7. Slice
Problem
8. Packages +

 It is the third-party library as well


 It is a module implemented in a folder .
 Packages are third-party libraries that we can install in PC or cloud server

PyPI

 We can get the package here, Python Package Index

 To use the package, we need to figure it out how to download, unzip and put it
somewhere

 But in Python, we use package manager, pip, pip is a program that generally comes
with Python, that allows us to install packages on our PC

9. Cowsay
10. API, requests, JSON

 We can pretend a browser and connect to that third-party API on server, and download
some data that you can incorporate in the program

 Requests library allows us to make web request, browser requests

 It is JSON, it is a language agnostic format for exchanging data between computers

 Request library converts the JSON to dictionary


JSON library
11. Custom Libraries +

 We can build our libraries, this is our library

 This is our program

 When Python has read the file from top to bottom, the last line of code is to call
“main”. Main is called no mater what
 __name__, it is a special variable set by Python to “main” when you run a file from
command line

 When we run the program in command line, Python will automatically set __name__
to “__main__”

 But when we import the file “__name__” will not be “__main__”

You might also like