Skip to content

Latest commit

 

History

History
40 lines (28 loc) · 1.05 KB

File metadata and controls

40 lines (28 loc) · 1.05 KB

Python Cheat Sheet

Basic cheatsheet for Python mostly based on the book written by Al Sweigart, Automate the Boring Stuff with Python under the Creative Commons license and many other sources.

Read It

Comprehensions

List comprehension

a = [1, 3, 5, 7, 9, 11]
[i - 1 for i in a]

Set comprehension

b = {"abc", "def"}
{s.upper() for s in b}

Dict comprehension

c = {'name': 'Pooka', 'age': 5}
{v, k for k, v in c.items()}

A List comprehension can be generated from a dictionary:

c = {'name': 'Pooka', 'first_name': 'Oooka'}
["{}:{}".format(k.upper(), v.upper()) for k, v in c.items()]