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

Python Libraries PDF

The document discusses several Python libraries and concepts: 1. It introduces NumPy for scientific computing with multidimensional arrays, and provides examples of array creation and indexing. 2. It covers Matplotlib for data visualization and plotting, demonstrating simple and multiple line plots, adding labels and legends. 3. It explains common Python data types like lists, dictionaries, sets, and tuples, providing examples of creation, access, iteration and other basic operations.

Uploaded by

Pistol Costin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Python Libraries PDF

The document discusses several Python libraries and concepts: 1. It introduces NumPy for scientific computing with multidimensional arrays, and provides examples of array creation and indexing. 2. It covers Matplotlib for data visualization and plotting, demonstrating simple and multiple line plots, adding labels and legends. 3. It explains common Python data types like lists, dictionaries, sets, and tuples, providing examples of creation, access, iteration and other basic operations.

Uploaded by

Pistol Costin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Ioana Dogaru – Tehnologii de Programare Internet

PYTHON Libraries – Examples

• Numpy
• Matplotlib
• Tkinter
• More on web development

➔ Brief about Containers


Python includes several built-in container types:

lists, dictionaries, sets, and tuples.

Test the following code:

Lists

A list is the Python equivalent of an array, but is resizeable and can contain
elements of different types:

xs = [3, 1, 2] # Create a list


print(xs, xs[2]) # Prints "[3, 1, 2] 2"
print(xs[-1]) # Negative indices count from the end of the list;
prints "2"
xs[2] = 'foo' # Lists can contain elements of different types
print(xs) # Prints "[3, 1, 'foo']"
xs.append('bar') # Add a new element to the end of the list
print(xs) # Prints "[3, 1, 'foo', 'bar']"
x = xs.pop() # Remove and return the last element of the list
print(x, xs) # Prints "bar [3, 1, 'foo']"

Slicing: In addition to accessing list elements one at a time, Python provides


concise syntax to access sublists; this is known as slicing:

nums = list(range(5)) # range is a built-in function that creates a


list of integers
print(nums) # Prints "[0, 1, 2, 3, 4]"
print(nums[2:4]) # Get a slice from index 2 to 4 (exclusive);
prints "[2, 3]"
print(nums[2:]) # Get a slice from index 2 to the end; prints
"[2, 3, 4]"
print(nums[:2]) # Get a slice from the start to index 2
(exclusive); prints "[0, 1]"
Ioana Dogaru – Tehnologii de Programare Internet

print(nums[:]) # Get a slice of the whole list; prints "[0,


1, 2, 3, 4]"
print(nums[:-1]) # Slice indices can be negative; prints "[0,
1, 2, 3]"
nums[2:4] = [8, 9] # Assign a new sublist to a slice
print(nums) # Prints "[0, 1, 8, 9, 4]"

Loops: You can loop over the elements of a list like this:
animals = ['cat', 'dog', 'monkey']
for animal in animals:
print(animal)
# Prints "cat", "dog", "monkey", each on its own line.

Dictionaries
A dictionary stores (key, value) pairs, similar to a Map in Java or an object in
Javascript. You can use it like this:

d = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with


some data
print(d['cat']) # Get an entry from a dictionary; prints "cute"
print('cat' in d) # Check if a dictionary has a given key; prints
"True"
d['fish'] = 'wet' # Set an entry in a dictionary
print(d['fish']) # Prints "wet"
# print(d['monkey']) # KeyError: 'monkey' not a key of d
print(d.get('monkey', 'N/A')) # Get an element with a default; prints
"N/A"
print(d.get('fish', 'N/A')) # Get an element with a default; prints
"wet"
del d['fish'] # Remove an element from a dictionary
print(d.get('fish', 'N/A')) # "fish" is no longer a key; prints "N/A"

Loops: It is easy to iterate over the keys in a dictionary:

d = {'person': 2, 'cat': 4, 'spider': 8}


for animal in d:
legs = d[animal]
print('A %s has %d legs' % (animal, legs))
# Prints "A person has 2 legs", "A cat has 4 legs", "A spider has 8
legs"

If you want access to keys and their corresponding values, use


the items method:
Ioana Dogaru – Tehnologii de Programare Internet

d = {'person': 2, 'cat': 4, 'spider': 8}


for animal, legs in d.items():
print('A %s has %d legs' % (animal, legs))
# Prints "A person has 2 legs", "A cat has 4 legs", "A spider has 8
legs"

Sets
A set is an unordered collection of distinct elements. As a simple example,
consider the following:

animals = {'cat', 'dog'}


print('cat' in animals) # Check if an element is in a set; prints
"True"
print('fish' in animals) # prints "False"
animals.add('fish') # Add an element to a set
print('fish' in animals) # Prints "True"
print(len(animals)) # Number of elements in a set; prints "3"
animals.add('cat') # Adding an element that is already in the
set does nothing
print(len(animals)) # Prints "3"
animals.remove('cat') # Remove an element from a set
print(len(animals)) # Prints "2"

Loops: Iterating over a set has the same syntax as iterating over a list; however
since sets are unordered, you cannot make assumptions about the order in which
you visit the elements of the set:

animals = {'cat', 'dog', 'fish'}


for idx, animal in enumerate(animals):
print('#%d: %s' % (idx + 1, animal))
# Prints "#1: fish", "#2: dog", "#3: cat"

Tuples
A tuple is an (immutable) ordered list of values. A tuple is in many ways similar to
a list; one of the most important differences is that tuples can be used as keys in
dictionaries and as elements of sets, while lists cannot. Here is a trivial example:

d = {(x, x + 1): x for x in range(10)} # Create a dictionary with


tuple keys
t = (5, 6) # Create a tuple
print(type(t)) # Prints "<class 'tuple'>"
Ioana Dogaru – Tehnologii de Programare Internet

print(d[t]) # Prints "5"


print(d[(1, 2)]) # Prints "1"

NumPy

Numpy is the core library for scientific computing in Python. It provides a high-
performance multidimensional array object, and tools for working with these
arrays. https://numpy.org/devdocs/user/quickstart.html

If you use pip, you can install it with:

pip install numpy

Arrays
A numpy array is a grid of values, all of the same type, and is indexed by a tuple
of nonnegative integers.
The number of dimensions is the rank of the array; the shape of an array is a tuple
of integers giving the size of the array along each dimension.

We can initialize numpy arrays from nested Python lists, and access elements
using square brackets:

import numpy as np

a = np.array([1, 2, 3]) # Create a rank 1 array


print(type(a)) # Prints "<class 'numpy.ndarray'>"
print(a.shape) # Prints "(3,)"
print(a[0], a[1], a[2]) # Prints "1 2 3"
a[0] = 5 # Change an element of the array
print(a) # Prints "[5, 2, 3]"

b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array


print(b.shape) # Prints "(2, 3)"
print(b[0, 0], b[0, 1], b[1, 0]) # Prints "1 2 4"

Numpy also provides many functions to create arrays:

import numpy as np

a = np.zeros((2,2)) # Create an array of all zeros


print(a) # Prints "[[ 0. 0.]
# [ 0. 0.]]"
Ioana Dogaru – Tehnologii de Programare Internet

b = np.ones((1,2)) # Create an array of all ones


print(b) # Prints "[[ 1. 1.]]"

c = np.full((2,2), 7) # Create a constant array


print(c) # Prints "[[ 7. 7.]
# [ 7. 7.]]"

d = np.eye(2) # Create a 2x2 identity matrix


print(d) # Prints "[[ 1. 0.]
# [ 0. 1.]]"

e = np.random.random((2,2)) # Create an array with random values


print(e)
# Might print "[[ 0.91940167 0.08143941]

# [ 0.68744134 0.87236687]]"

Array indexing

Numpy offers several ways to index into arrays.

Slicing: Similar to Python lists, numpy arrays can be sliced. Since arrays may be
multidimensional, you must specify a slice for each dimension of the array:
import numpy as np

# Create the following rank 2 array with shape (3, 4)


# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

# Use slicing to pull out the subarray consisting of the first 2 rows
# and columns 1 and 2; b is the following array of shape (2, 2):
# [[2 3]
# [6 7]]
b = a[:2, 1:3]

# A slice of an array is a view into the same data, so modifying it


# will modify the original array.
print(a[0, 1]) # Prints "2"
b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1]
print(a[0, 1]) # Prints "77"

Exercise:
1)Write a NumPy program to test whether any of the elements of a given array is
non-zero.
2) Write a NumPy program to create an array with the values 1, 7, 13, 105 and
determine the size of the memory occupied by the array.
3) Write a NumPy program to create an array of the integers from 30 to70
Ioana Dogaru – Tehnologii de Programare Internet

Matplotlib
Matplotlib is a plotting library. In this section give a brief introduction to
the matplotlib.pyplot module, which provides a plotting system similar to that of
MATLAB.

Plotting

The most important function in matplotlib is plot , which allows you to plot 2D
data. Here is a simple example:
import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on a sine curve


x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)

# Plot the points using matplotlib


plt.plot(x, y)
plt.show() # You must call plt.show() to make graphics
appear.

Running this code produces the following plot:

With just a little bit of extra work we can easily plot multiple lines at once, and add
a title, legend, and axis labels:
Ioana Dogaru – Tehnologii de Programare Internet

import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on sine and


cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)

# Plot the points using matplotlib


plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine'])
plt.show()

Subplots
You can plot different things in the same figure using the subplot function. Here
is an example:
import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on sine and


cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
Ioana Dogaru – Tehnologii de Programare Internet

y_sin = np.sin(x)
y_cos = np.cos(x)

# Set up a subplot grid that has height 2 and width 1,


# and set the first such subplot as active.
plt.subplot(2, 1, 1)

# Make the first plot


plt.plot(x, y_sin)
plt.title('Sine')

# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')

# Show the figure.


plt.show()

Exercise: Write a Python program to draw a line with suitable label in the x axis,
y axis and a title.
Ioana Dogaru – Tehnologii de Programare Internet

Display a bar chart - example

Sample data:
Programming languages: Java, Python, PHP, JavaScript, C#, C++
Popularity: 22.2, 17.6, 8.8, 8, 7.7, 6.7

import matplotlib.pyplot as plt


x = ['Java', 'Python', 'PHP', 'JavaScript', 'C#', 'C++']
popularity = [22.2, 17.6, 8.8, 8, 7.7, 6.7]
x_pos = [i for i, _ in enumerate(x)]
plt.bar(x_pos, popularity, color='blue')
plt.xlabel("Languages")
plt.ylabel("Popularity")
plt.title("PopularitY of Programming Language\n" + "Worldwide, Oct 2020
compared to a year ago")
plt.xticks(x_pos, x)
# Turn on the grid
plt.minorticks_on()
plt.grid(which='major', linestyle='-', linewidth='0.5', color='red')
# Customize the minor grid
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
plt.show()

Exercise: Modify the first example in order to display the values in a


horizontal bar. Also, change the colors for the bar chart.
Ioana Dogaru – Tehnologii de Programare Internet

Tkinter

Python has a lot of GUI frameworks, but Tkinter is the only framework that’s
built into the Python standard library. Tkinter has several strengths. It’s cross-
platform, so the same code works on Windows, macOS, and Linux. Visual
elements are rendered using native operating system elements, so
applications built with Tkinter look like they belong on the platform where
they’re run.

→import the Python GUI Tkinter module:

import tkinter as tk

→ A window is an instance of Tkinter’s Tk class. Go ahead and create a new


window and assign it to the variable window:
>>>
window = tk.Tk()

a new window pops up on your screen. How it looks depends on your


operating system:

Now that you have a window, you can add a widget. Use the tk.Label class to add
some text to a window. Create a Label widget with the text "Hello, Tkinter" and
assign it to a variable called greeting. There are several ways to add widgets to a
window. Right now, you can use the Label widget’s .pack() method

import tkinter as tk
window = tk.Tk()
greeting = tk.Label(text="Hello, Tkinter")
Ioana Dogaru – Tehnologii de Programare Internet

greeting.pack()
window.mainloop()

window.mainloop() tells Python to run the Tkinter event loop.

Widget
Class Description
Label A widget used to display text on the screen
Button A button that can contain text and can perform an action when clicked
Entry A text entry widget that allows only a single line of text
Text A text entry widget that allows multiline text entry
Frame A rectangular region used to group related widgets or provide padding between
widgets

Change the label :

import tkinter as tk
window = tk.Tk()
greeting = tk.Label(
text="Hello, Tkinter",
foreground="white", # Set the text color to white
background="black" # Set the background color to black
)
greeting.pack()
window.mainloop()

There are numerous valid color names, including:

• "red"
• "orange"
• "yellow"
• "green"
• "blue"
• "purple"
• label = tk.Label(text="Hello, Tkinter", fg="white", bg="black")
Ioana Dogaru – Tehnologii de Programare Internet

import tkinter as tk
window = tk.Tk()
greeting = tk.Label(text="Hello, Tkinter", fg="red", bg="blue")
greeting.pack()
window.mainloop()

Set label font size


You can set the label font so you can make it bigger and maybe bold. You can
also change the font style.

lbl = Label(window, text="Hello", font=("Arial Bold", 50))

Tkinter Buttons

The Button widget is a standard Tkinter widget, which is used for various kinds of
buttons. A button is a widget which is designed for the user to interact with, i.e. if
the button is pressed by mouse click some action might be started. They can also
contain text and images like labels. While labels can display text in various fonts, a
button can only display text in a single font. The text of a button can span more than
one line.
A Python function or method can be associated with a button. This function or
method will be executed, if the button is pressed in some way.

Example for the Button Class

The following script defines two buttons: one to quit the application and another one
for the action, i.e. printing the text "Tkinter is easy to use!" on the terminal.

import tkinter as tk

def write_slogan():
print("Tkinter is easy to use!")

root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
button = tk.Button(frame,
Ioana Dogaru – Tehnologii de Programare Internet

text="QUIT",
fg="red",
command=quit)
button.pack(side=tk.LEFT)
slogan = tk.Button(frame,
text="Hello",
command=write_slogan)
slogan.pack(side=tk.LEFT)
root.mainloop()

Dynamical Content in a Label

The following script shows an example, where a label is dynamically incremented by


1 until a stop button is pressed:

import tkinter as tk
Ioana Dogaru – Tehnologii de Programare Internet

counter = 0
def counter_label(label):
counter = 0
def count():
global counter
counter += 1
label.config(text=str(counter))
label.after(1000, count)
count()
root = tk.Tk()
root.title("Counting Seconds")
label = tk.Label(root, fg="dark green")
label.pack()
counter_label(label)
button = tk.Button(root, text='Stop', width=25, command=root.destroy)
button.pack()
root.mainloop()
Ioana Dogaru – Tehnologii de Programare Internet

Slider

A slider is a Tkinter object with which a user can set a value by moving an indicator.
Sliders can be vertically or horizontally arranged. A slider is created with the Scale
method().

Using the Scale widget creates a graphical object, which allows the user to select a
numerical value by moving a knob along a scale of a range of values. The minimum
and maximum values can be set as parameters, as well as the resolution. We can also
determine if we want the slider vertically or horizontally positioned. A Scale widget is
a good alternative to an Entry widget, if the user is supposed to put in a number from
a finite range, i.e. a bounded numerical value.

import tkinter as tk

window = tk.Tk()
window.title('My Window')
window.geometry('500x300')

l = tk.Label(window, bg='white', fg='black', width=20,


text='empty')
l.pack()

def print_selection(v):
l.config(text='you have selected ' + v)

s = tk.Scale(window, label='try me', from_=0, to=10,


orient=tk.HORIZONTAL, length=200,
showvalue=0,tickinterval=2, resolution=0.01,
command=print_selection)
s.pack()

window.mainloop()
Ioana Dogaru – Tehnologii de Programare Internet

Exercise:
1) Write a Python GUI program to create a Listbox bar widgets using tkinter
module like in the following picture: (Use listbox.insert)

2) Write a Python GUI program to create three single line text-box to accept a
value from the user using tkinter module.
Ioana Dogaru – Tehnologii de Programare Internet

Python and Networked programs


The network protocol that powers the web is actually quite simple and there is
built-in support in Python called socket which makes it very easy to make network
connections and retrieve data over those sockets in a Python program.

BeautifulSoup → A Python library for parsing HTML documents and extracting


data from HTML documents that compensates for most of the imperfections
in the HTML that browsers generally ignore. You can download the BeautifulSoup
code from www.crummy.com.

port → A number that generally indicates which application you are contacting
when you make a socket connection to a server. As an example, web traffic usually
uses port 80 while email traffic uses port 25.

socket → A network connection between two applications where the applications


can send and receive data in either direction.
Sockets and the socket API are used to send messages across a network.

The world’s simplest web browser


Perhaps the easiest way to show how the HTTP protocol works is to write a very
simple Python program that makes a connection to a web server and follows
the rules of the HTTP protocol to request a document and display what the
server sends back.

The code is:

import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)
while True:
data = mysock.recv(512)
Ioana Dogaru – Tehnologii de Programare Internet

if len(data) < 1:
break
print(data.decode(),end='')
mysock.close()

The program produces the following result:

HTTP/1.1 200 OK
Date: Fri, 02 Oct 2020 07:13:02 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Sat, 13 May 2017 11:22:22 GMT
Accept-Ranges: bytes
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Connection: close
Content-Type: text/plain
Content-Length: 167

But soft what light through yonder window breaks


It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief

First the program makes a connection to port 80 on the server www.py4e.com.


Since our program is playing the role of the “web browser”, the HTTP protocol
says we must send the GET command followed by a blank line. \r\n signifies
an EOL (end of line), so \r\n\r\n signifies nothing between two EOL sequences.
That is the equivalent of a blank line.

Once we send that blank line, we write a loop that receives data in 512-character
chunks from the socket and prints the data out until there is no more data to read
(i.e., the recv() returns an empty string).
Ioana Dogaru – Tehnologii de Programare Internet

Sockets can be used to communicate with a web server or with a mail server or
many other kinds of servers. All that is needed is to find the document which
describes the protocol and write the code to send and receive the data according
to the protocol.

One of the requirements for using the HTTP protocol is the need to send and
receive data as bytes objects, instead of strings. In the preceding example, the
encode() and decode() methods convert strings into bytes objects and back again.

Retrieving an image over HTTP

We can use a similar program to retrieve an image across using HTTP. Instead
of copying the data to the screen as the program runs, we accumulate the data in
a string, trim off the headers, and then save the image data to a file as follows:

Save the following code in a file named for example socket.py

# Code: http://www.py4e.com/code3/urljpeg.py

import socket
HOST = 'data.pr4e.org'
PORT = 80
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((HOST, PORT))
mysock.sendall(b'GET http://data.pr4e.org/cover3.jpg HTTP/1.0\r\n\r\n')
count = 0
picture = b""
while True:
data = mysock.recv(5120)
if len(data) < 1: break
#time.sleep(0.25)
count = count + len(data)
print(len(data), count)
picture = picture + data
mysock.close()
# Look for the end of the header (2 CRLF)
pos = picture.find(b"\r\n\r\n")
print('Header length', pos)
print(picture[:pos].decode())
# Skip past the header and save the picture data
picture = picture[pos+4:]
fhand = open("stuff.jpg", "wb")
fhand.write(picture)
Ioana Dogaru – Tehnologii de Programare Internet

fhand.close()

Save the read content in Suff.jpg file.

You can see that for this url, the Content-Type header indicates that body of the
document is an image (image/jpeg). Once the program completes, you can view
the image data by opening the file stuff.jpg in an image viewer.
Ioana Dogaru – Tehnologii de Programare Internet

Retrieving web pages with urllib

While we can manually send and receive data over HTTP using the socket library,
there is a much simpler way to perform this common task in Python by using the
urllib library.

Using urllib, you can treat a web page much like a file. You simply indicate which
web page you would like to retrieve and urllib handles all of the HTTP protocol and
header details.

The equivalent code to read the romeo.txt file from the web using urllib is as
follows:

import urllib.request
fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')

for line in fhand:


print(line.decode().strip())

# Code: http://www.py4e.com/code3/urllib1.py

Once the web page has been opened with urllib.urlopen, we can treat it like a file
and read through it using a for loop.

When the program runs, we only see the output of the contents of the file.
The headers are still sent, but the urllib code consumes the headers and only
returns the data to us.

As an example, we can write a program to retrieve the data for romeo.txt and
compute the frequency of each word in the file as follows:
Again, once we have opened the web page, we can read it like a local file.

import urllib.request, urllib.parse, urllib.error


fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')
counts = dict()
Ioana Dogaru – Tehnologii de Programare Internet

for line in fhand:


words = line.decode().split()
for word in words:
counts[word] = counts.get(word, 0) + 1
print(counts)

# Code: http://www.py4e.com/code3/urlwords.py

Exercise: Change the socket program to prompt the user for the URL so it can
read any web page. You can use split('/') to break the URL into its component parts
so you can extract the host name for the socket connect call. Add error checking
using try and except to handle the condition where the user enters an improperly
formatted or non-existent URL.

References:
https://cs231n.github.io/python-numpy-tutorial/
https://www.py4e.com/
https://realpython.com/python-sockets/

You might also like