Docsity

Python Cheat Sheet: Functions and Tricks, Cheat Sheet of Advanced Computer Programming

Toggle sidebar
bg1
Python Cheat Sheet: Functions and Tricks๎˜„
Description๎˜„
Example๎˜„
Result๎˜„
A
D
V
A
N
C
E
D
F
U
N
C
T
I
O
N
S
map(func, iter)๎˜„
Executes the function on all elements of๎˜„
the iterable๎˜„
list(map(๎˜lambda๎˜ x: x[๎˜0๎˜], [๎˜'red'๎˜,
'green'๎˜, ๎˜'blue'๎˜]))
[๎˜'r'๎˜, ๎˜'g'๎˜, ๎˜'b'๎˜]
map(func, i1, ...,
ik)๎˜„
Executes the function on all k elements of๎˜„
the k iterables๎˜„
list(map(๎˜lambda๎˜ x, y: str(x) + ๎˜' '๎˜ +
y + ๎˜'s'๎˜ , [๎˜0๎˜, ๎˜2๎˜, ๎˜2๎˜], [๎˜'apple'๎˜,
'orange'๎˜, ๎˜'banana'๎˜]))
[๎˜'0 apples'๎˜, ๎˜'2
oranges'๎˜, ๎˜'2
bananas'๎˜]
string.join(iter)๎˜„
Concatenates iterable elements๎˜„
separated by ๎˜string๎˜„
' marries '๎˜.join(list([๎˜'Alice'๎˜,
'Bob'๎˜]))
'Alice marries Bob'
filter(func,
iterable)๎˜„
Filters out elements in iterable for which๎˜„
function returns ๎˜False๎˜ ๎˜(or 0)๎˜„
list(filter(๎˜lambda๎˜ x: ๎˜True๎˜ ๎˜if๎˜ x>๎˜17
else๎˜ ๎˜False๎˜, [๎˜1๎˜, ๎˜15๎˜, ๎˜17๎˜, ๎˜18๎˜]))
[๎˜18๎˜]
string.strip()๎˜„
Removes leading and trailing๎˜„
whitespaces of string๎˜„
print(๎˜" \n \t 42 \t "๎˜.strip())
42
sorted(iter)๎˜„
Sorts iterable in ascending order๎˜„
sorted([๎˜8๎˜, ๎˜3๎˜, ๎˜2๎˜, ๎˜42๎˜, ๎˜5๎˜])
[๎˜2๎˜, ๎˜3๎˜, ๎˜5๎˜, ๎˜8๎˜, ๎˜42๎˜]
sorted(iter,
key=key)๎˜„
Sorts according to the key function in๎˜„
ascending order๎˜„
sorted([๎˜8๎˜, ๎˜3๎˜, ๎˜2๎˜, ๎˜42๎˜, ๎˜5๎˜], key=๎˜lambda
x: ๎˜0๎˜ ๎˜if๎˜ x==๎˜42๎˜ ๎˜else๎˜ x)
[๎˜42๎˜, ๎˜2๎˜, ๎˜3๎˜, ๎˜5๎˜, ๎˜8๎˜]
help(func)๎˜„
Returns documentation of ๎˜func๎˜„
help(str.upper())
'... to uppercase.'
zip(i1, i2, ...)๎˜„
Groups the i-th elements of iterators ๎˜i1,
i2, ...๎˜ together๎˜„
list(zip([๎˜'Alice'๎˜, ๎˜'Anna'๎˜], [๎˜'Bob'๎˜,
'Jon'๎˜, ๎˜'Frank'๎˜]))
[(๎˜'Alice'๎˜, ๎˜'Bob'๎˜),
(๎˜'Anna'๎˜, ๎˜'Jon'๎˜)]
Unzip๎˜„
Equal to: 1) unpack the zipped list, 2) zip๎˜„
the result๎˜„
list(zip(*[(๎˜'Alice'๎˜, ๎˜'Bob'๎˜),
(๎˜'Anna'๎˜, ๎˜'Jon'๎˜)]))
[(๎˜'Alice'๎˜, ๎˜'Anna'๎˜),
(๎˜'Bob'๎˜, ๎˜'Jon'๎˜)]
enumerate(iter)๎˜„
Assigns a counter value to each element๎˜„
of the iterable๎˜„
list(enumerate([๎˜'Alice'๎˜, ๎˜'Bob'๎˜,
'Jon'๎˜]))
[(๎˜0๎˜, ๎˜'Alice'๎˜), (๎˜1๎˜,
'Bob'๎˜), (๎˜2๎˜, ๎˜'Jon'๎˜)]
T
R
I
C
K
S
python -m http.server๎˜„
<P>๎˜„
Want to share files between PC and phone? Run this command in PCโ€™s shell. <P> is any port number 0โ€“65535. Type <๎˜„
IP address of PC>:<P> in the phoneโ€™s browser. You can now browse the files in the PC directory.๎˜„
Read comic๎˜„
import๎˜ antigravity
Open the comic series xkcd in your web browser
Zen of Python๎˜„
import๎˜ this๎˜„
'...Beautiful is better than ugly. Explicit is ...'
Swapping numbers๎˜„
Swapping variables is a breeze in Python.๎˜„
No offense, Java!๎˜„
a, b = ๎˜'Jane'๎˜, ๎˜'Alice'
a, b = b, a
a = ๎˜'Alice'
b = ๎˜'Jane'
Unpacking arguments๎˜„
Use a sequence as function arguments๎˜„
via asterisk operator *. Use a dictionary๎˜„
(key, value) via double asterisk operator **๎˜„
def๎˜ ๎˜f๎˜(x, y, z)๎˜:๎˜ return๎˜ x + y * z
f(*[๎˜1๎˜, ๎˜3๎˜, ๎˜4๎˜])
f(**{๎˜'z'๎˜ : ๎˜4๎˜, ๎˜'x'๎˜ : ๎˜1๎˜, ๎˜'y'๎˜ : ๎˜3๎˜})
13
13
Extended Unpacking๎˜„
Use unpacking for multiple assignment๎˜„
feature in Python๎˜„
a, *b = [๎˜1๎˜, ๎˜2๎˜, ๎˜3๎˜, ๎˜4๎˜, ๎˜5๎˜]
a = ๎˜1
b = [๎˜2๎˜, ๎˜3๎˜, ๎˜4, 5๎˜]
Merge two dictionaries๎˜„
Use unpacking to merge two dictionaries๎˜„
into a single one๎˜„
x={๎˜'Alice'๎˜ : ๎˜18๎˜}
y={๎˜'Bob'๎˜ : ๎˜27๎˜, ๎˜'Ann'๎˜ : ๎˜22๎˜}
z = {**x,**y}
z = {๎˜'Alice'๎˜: ๎˜18๎˜,
'Bob'๎˜: ๎˜27๎˜, ๎˜'Ann'๎˜: ๎˜22๎˜}

Partial preview of the text

Download Python Cheat Sheet: Functions and Tricks and more Cheat Sheet Advanced Computer Programming in PDF only on Docsity!

Python Cheat Sheet: Functions and Tricks

Description Example Result A D V A N C E D F U N C T I O N S map(func, iter) Executes the function on all elements of the iterable list(map(lambda x: x[ 0 ], ['red', 'green', 'blue'])) ['r', 'g', 'b'] map(func, i1, ..., ik) Executes the function on all k elements of the k iterables list(map(lambda x, y: str(x) + ' ' + y + 's' , [ 0 , 2 , 2 ], ['apple', 'orange', 'banana'])) ['0 apples', ' oranges', ' bananas'] string.join(iter) Concatenates iterable elements separated by string ' marries '.join(list(['Alice', 'Bob'])) 'Alice marries Bob' filter(func, iterable) Filters out elements in iterable for which function returns False (or 0) list(filter(lambda x: True if x> 17 else False, [ 1 , 15 , 17 , 18 ]))

[ 18 ]

string.strip() Removes leading and trailing whitespaces of string print(" \n \t 42 \t ".strip()) 42 sorted(iter) Sorts iterable in ascending order sorted([ 8 , 3 , 2 , 42 , 5 ]) [ 2 , 3 , 5 , 8 , 42 ] sorted(iter, key=key) Sorts according to the key function in ascending order sorted([ 8 , 3 , 2 , 42 , 5 ], key=lambda x: 0 if x== 42 else x)

[ 42 , 2 , 3 , 5 , 8 ]

help(func) Returns documentation of func help(str.upper()) '... to uppercase.' zip(i1, i2, ...) Groups the i-th elements of iterators i1, i2, ... together list(zip(['Alice', 'Anna'], ['Bob', 'Jon', 'Frank'])) [('Alice', 'Bob'), ('Anna', 'Jon')] Unzip Equal to: 1) unpack the zipped list, 2) zip the result list(zip(*[('Alice', 'Bob'), ('Anna', 'Jon')])) [('Alice', 'Anna'), ('Bob', 'Jon')] enumerate(iter) Assigns a counter value to each element of the iterable list(enumerate(['Alice', 'Bob', 'Jon'])) [( 0 , 'Alice'), ( 1 , 'Bob'), ( 2 , 'Jon')] T R I C K S python -m http.server

Want to share files between PC and phone? Run this command in PCโ€™s shell.

is any port number 0โ€“65535. Type < IP address of PC>:

in the phoneโ€™s browser. You can now browse the files in the PC directory. Read comic import antigravity Open the comic series xkcd in your web browser Zen of Python import this '...Beautiful is better than ugly. Explicit is ...' Swapping numbers Swapping variables is a breeze in Python. No offense, Java! a, b = 'Jane', 'Alice' a, b = b, a a = 'Alice' b = 'Jane' Unpacking arguments Use a sequence as function arguments via asterisk operator *. Use a dictionary (key, value) via double asterisk operator ** def f(x, y, z): return x + y * z f(*[ 1 , 3 , 4 ]) f(**{'z' : 4 , 'x' : 1 , 'y' : 3 })

Extended Unpacking Use unpacking for multiple assignment feature in Python a, b = [ 1 , 2 , 3 , 4 , 5 ] a = 1 b = [ 2 , 3 , 4, 5] Merge two dictionaries Use unpacking to merge two dictionaries into a single one x={'Alice' : 18 } y={'Bob' : 27 , 'Ann' : 22 } z = {x,*y} z = {'Alice': 18 , 'Bob': 27 , 'Ann': 22 }