tos9@Freenode
Strongly typed
2 + "2"
Traceback (most recent call last):
File "input", line 1, in module
TypeError: unsupported operand type(s) for +: 'int' and 'str'
That funny language with semantic whitespace
>>> 12 + 2
14
>>> "hello" + ' ' + "world"
"hello world"
>>> cats = 12.3
>>> lives = 9j
>>> print cats * lives
110.7j
favorite_things = ["dogs", "Filbert", "Jackie Chan"]
row = "Enrico", True, 1, "Narcoleptic", ["Wallet", "Key"]
vehicles = {"Thomas", "Lightning McQueen"}
board = {
"A2" : ("White", "Pawn"),
"B1" : ("White", "Rook"),
"B8" : ("Black", "Rook"),
"F5" : ("Black", "Knight"),
"G7" : ("White", "King"),
"H7" : ("Black", "King"),
}
def print_long_things(things, longer_than=3, finish_msg="Done!"):
for thing in things:
if len(thing) > longer_than:
print thing
print finish_msg
def print_long_things(things, longer_than=3, finish_msg="Done!"):
for thing in things:
if len(thing) > longer_than:
print thing
print finish_msg
>>> print_long_things(["the", "quick", "brown", "fox"])
quick
brown
Done!
def print_long_things(things, longer_than=3, finish_msg="Done!"):
for thing in things:
if len(thing) > longer_than:
print thing
print finish_msg
>>> print_long_things(things=["cat", "walrus"], finish_msg="OK!")
walrus
OK!
numbers = range(10)
[1, 2, 3]
[number ** 2 for number in numbers]
{1, 2, 3}
{float(number) for number in numbers if number % 2 == 0}
{1 : 2, 3 : 4}
{number : number ** 2 for number in numbers if number % 2 == 0}
(number ** 2 for number in numbers if number % 2 == 0)
def make_numbers(numbers):
for number in numbers:
if number % 2:
yield number
>>> movements = [1, -3, 5, 2, 3, -7, 4, 0, 2, -2]
>>> for movement in sorted(movements, key=abs):
... print movement
0
1
2
2
-2
-3
3
4
5
-7
Python is like the NSA
Nothing is private.
"If it quacks like a duck, walks like a duck..."
"If it quacks like a duck, walks like a duck..."
Types
Great for compilers
Not so great for humans
class File {
...
}
class Parser {
new(File inputFile) {
List buffer = []
while (String line = inputFile.readline() != EOF) {
buffer.append(parse(line))
}
}
}
class File extends Enumerable {
...
}
class Parser {
new(Enumerable input) {
List buffer = []
while (String line = input.next() != EOI) {
buffer.append(parse(line))
}
}
}
>>> sorted(["the", "cat", "in", "the", "hat"])
['cat', 'hat', 'in', 'the', 'the']
sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
with open("shakespeare.txt") as input_file:
print sorted(input_file)
itertools
subprocess
import subprocess
processes = subprocess.check_output(["ps", "aux"])
os
os.path
sys
glob
import os
import glob
import sys
for path in glob.iglob("css/*.css"):
sys.stdout.write(os.path.abspath(path) + " ")
argparse
json
sqlite3
decimal, math, fractions
Tkinter
gettext
difflib
bisect
logging