










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 18
This page cannot be seen from the preview
Don't miss anything!











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'
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
>>>> import os.path >>>> os.path.join('workdir','subdir','data') 'workdir/subdir/data'
>>>> import os.path >>>> os.path.join(‘c:’,'workdir','subdir','data') 'workdir\subdir\data'
>>> 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.randrange(-10,10) 8 >>> random.randrange(-10,10) 6 >>> 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'
>>>> 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:
>>>> 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)
>>> 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'