Intermediate Python

tips and tricks for more pythonic fun

Unpacking

>>> l = [1,2,3]
>>> p,q,r = l
>>> p
1
>>> person = ["Philo", "van Kemenade", (5,4,1988)]
>>> first, last, dateOfBirth = person
>>> first
'Philo'
>>> dateOfBirth
(5, 4, 1988)
>>> first, *middle, last = [0,1,2,3,4,5,6,7,8,9]
>>> first
0
>>> middle
[1, 2, 3, 4, 5, 6, 7, 8]
>>> last
9
>>> person = ["Philo", "van Kemenade", (5,4,1988), 'python', 'music', 'climbing', 'hiking']
>>> first, last, dateOfBirth, *hobbies = person
>>> hobbies
['python', 'music', 'climbing', 'hiking']

Unpacking arbitrary length

using *star expressions

Iterators

Generators

List Comprehensions

Enumerate

4 Types of Quotes

Yield

>>> 1 + 1
2
>>> _
2

Interactive _

in the interactive interpreter

_ stores the last printed result

>>> hobbies = ['python', 'music', 'climbing', 'hiking']
>>> 'python' in hobbies
True

Checking membership

>>> hobbies = ['python', 'music', 'climbing', 'hiking']
>>> 'python' in hobbies
True

works for any iterable

Intermediate Python [WIP]

By Philo van Kemenade

Intermediate Python [WIP]

  • 1,975