Python 3 Standard Library: Overview of Useful Modules and Functions, Study notes of Statistics

An overview of the Python 3 standard library, highlighting a few modules and their common usage. It covers topics such as command line arguments, standard streams, pathname manipulations, mathematical functions, and more. Python's ecosystem is also discussed, with a mention of the Python Package Index (PyPI) and its vast collection of third-party packages.

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

avni
avni 🇺🇸

4.7

(3)

229 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python 3 Standard Library
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Python 3 Standard Library: Overview of Useful Modules and Functions and more Study notes Statistics in PDF only on Docsity!

Python 3 Standard Library

Batteries Included

● Python 3 includes a large standard library of modules

Together providing a huge number of useful attributes, functions and classes

● Next slides are a survey of a few modules and common usage

We’re only scratching the surface, See https://docs.python.org/3/library/index.html

sys module

● sys.argv

list containing command line

arguments

import sys

for num, arg in enumerate(sys.argv): print('arg {} is {!r}'.format(num, arg))

$ python3 printargs.py arg 0 is 'printargs.py'

$ python3 printargs.py hello world 1. arg 0 is 'printargs.py' arg 1 is 'hello' arg 2 is 'world' arg 3 is '1.222'

sys module

● sys.stdin

sys.stdout

sys.stderr

The standard streams

import sys

sys.stdout.writelines(sorted(sys.stdin.readlines()))

$ cat cities.txt New York Los Angeles Chicago Philadelphia Phoenix Chicago New York

$ python3 sortstdin.py < cities.txt Chicago Chicago Los Angeles New York New York Philadelphia Phoenix

os.path module

Pathname manipulations

● os.path.join()

Join pathname components using native

separator

● os.path.exists()

True if a pathname exists

● os.path.isfile()

os.path.isdir()

os.path.islink()

>>>> import os.path >>>> os.path.join('workdir','subdir','data') 'workdir/subdir/data'

On Windows

>>>> import os.path >>>> os.path.join(‘c:’,'workdir','subdir','data') 'workdir\subdir\data'

glob module

Pathname pattern expansion

cmath module

math with complex number support

● Extend quadroot.py program to

support complex roots

>>> math.sqrt(-29) Traceback (most recent call last): File "", line 1, in ValueError: math domain error

>>> cmath.sqrt(-29) 5.385164807134504j

>>> type(cmath.sqrt(-29))

>>> cmath.polar(3+7j) (7.615773105863909, 1.1659045405098132)

random module

Generate pseudo-random numbers

● random.randrange()

Random integer from a range

● random.shuffle()

Random integer from a range

● random.uniform()

random.triangular()

random.gauss()

>>> random.randrange(-10,10) 8 >>> random.randrange(-10,10) 6 >>> random.randrange(-10,10)

>>> random.randrange(-10,10)

>>> random.randrange(-10,10) …

>>> a = [1,2,3,4,5,6,7,8,9,10,11,12] >>> random.shuffle(a) >>> print(a) [10, 9, 5, 8, 4, 7, 2, 12, 11, 1, 3, 6]

>>> random.choice('abcdefghijklm') 'h' >>> random.choice('abcdefghijklm') 'a' >>> random.choice('abcdefghijklm') 'c'

datetime module

Date and time calculations

>>>> import datetime as dt >>>> t1 = dt.datetime(year=2015, month=2, day=28, hour=1, minute=10, second=0 ) >>>> t2 = dt.datetime(year=2016, month=10, day=4, hour=14, minute=00, second=0 )

>>>> print(t2-t1) 584 days, 12:50:

itertools module

Iterator building blocks

>>>> import itertools >>>> for i in itertools.product('abc','1234'): .... print(i) .... ('a', '1') ('a', '2') ('a', '3') ('a', '4') ('b', '1') ('b', '2') ('b', '3') ('b', '4') ('c', '1') ('c', '2') ('c', '3') ('c', '4')

>>>> for i in itertools.permutations([1,2,3]): .... print(i) .... (1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1)

urllib.request module

● Open URLs

subprocess module

● Subprocess management

>>> import subprocess as sp >>> st1 = sp.Popen(['cat','cities.txt'], stdout=sp.PIPE) >>> st2 = sp.Popen(['sort'], stdin=st1.stdout, stdout=sp.PIPE)

>>> out, err = st2.communicate() >>> print(out) b'Chicago\nChicago\nLos Angeles\nNew York\nNew York\nPhiladelphia\nPhoenix\n'