Learn 90% of Python in 90 Minutes
- 2. About Me
●
●
●
12+ years Python
Worked in Data Analysis, HA, Search,
Open Source, BI, and Storage
Author of multiple Python Books
- 10. Why Python?
Python is a powerful, multi-paradigm,
interpreted language popular with
start-ups and large Co’s
- 11. Python 2 or 3?
For beginners there is no real difference
between Python 2 & 3. The basics are the
same (except for print)
- 20. Python 3 hello world
print is no longer a statement, but a
function
print("hello world")
- 25. Mutable
>>> b = []
>>> id(b)
140675605442000
>>> b.append(3)
>>> b
[3]
>>> id(b)
140675605442000
# SAME!
- 26. Immutable
>>> a = 4
>>> id(a)
6406896
>>> a = a + 1
>>> id(a)
6406872
# DIFFERENT!
- 32. Careful with integer division
>>> 3/4
0
>>> 3/4.
0.75
(In Python 3 // is integer division
operator)
- 35. Long (2)
>>> import sys
>>> sys.maxint
9223372036854775807
>>> sys.maxint + 1
9223372036854775808L
- 38. How do I print?
He said, “I’m sorry”
- 39. String escaping
Escape with
>>> print 'He said, "I'm sorry"'
He said, "I'm sorry"
>>> print '''He said, "I'm sorry"'''
He said, "I'm sorry"
>>> print """He said, "I'm sorry""""
He said, "I'm sorry"
- 40. String escaping (2)
Escape Sequence
Output
Backslash
'
Single quote
"
Double quote
b
ASCII Backspace
n
Newline
t
Tab
u12af
Unicode 16 bit
U12af89bc
Unicode 32 bit
o84
Octal character
xFF
Hex character
- 43. dir
Lists attributes and methods:
>>> dir("a string")
['__add__', '__class__', ... 'startswith',
'swapcase', 'title', 'translate', 'upper',
'strip',
'zfill']
- 46. help
>>> help("a string".startswith)
Help on built-in function startswith:
startswith(...)
S.startswith(prefix[, start[, end]]) -> bool
Return True if S starts with the specified prefix, False
otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
prefix can also be a tuple of strings to try.
- 57. lists
>>> dir([])
['__add__', '__class__', '__contains__',...
'__iter__',... '__len__',... , 'append', 'count',
'extend', 'index', 'insert', 'pop', 'remove',
'reverse', 'sort']
- 64. dictionaries
Also called hashmap or associative array
elsewhere
>>>
>>>
>>>
>>>
>>>
10
age = {}
age['george'] = 10
age['fred'] = 12
age['henry'] = 10
print age['george']
- 89. range note
Python tends to follow half-open interval
([start,end)) with range and slices.
●
end - start = length
●
easy to concat ranges w/o overlap
- 90. iteration (2)
Java/C-esque style of object in array
access (BAD):
animals = ["cat", "dog", "bird"]
for index in range(len(animals)):
print index, animals[index]
- 91. iteration (3)
If you need indices, use enumerate
animals = ["cat", "dog", "bird"]
for index, value in enumerate(animals):
print index, value
- 92. iteration (4)
Can break out of nearest loop
for item in sequence:
# process until first negative
if item < 0:
break
# process item
- 94. iteration (6)
Can loop over lists, strings, iterators,
dictionaries... sequence like things:
my_dict = { "name": "matt", "cash": 5.45}
for key in my_dict.keys():
# process key
for value in my_dict.values():
# process value
for key, value in my_dict.items():
# process items
- 95. pass
pass is a null operation
for i in
# do
pass
range(10):
nothing 10 times
- 98. Slicing
Sequences (lists, tuples, strings, etc) can
be sliced to pull out a single item
my_pets = ["dog", "cat", "bird"]
favorite = my_pets[0]
bird = my_pets[-1]
- 100. Slicing (2)
Slices can take an end index, to pull out a
list of items
my_pets = ["dog", "cat", "bird"]
# a list
cat_and_dog = my_pets[0:2]
cat_and_dog2 = my_pets[:2]
cat_and_bird = my_pets[1:3]
cat_and_bird2 = my_pets[1:]
- 101. Slicing (3)
Slices can take a stride
my_pets = ["dog", "cat", "bird"]
# a list
dog_and_bird = [0:3:2]
zero_three_etc = range(0,10)
[::3]
- 102. Slicing (4)
Just to beat it in
veg = "tomatoe"
correct = veg[:-1]
tmte = veg[::2]
eotamot = veg[::-1]
- 104. File Input
Open a file to read from it (old style):
fin = open("foo.txt")
for line in fin:
# manipulate line
fin.close()
- 105. File Output
Open a file using 'w' to write to a file:
fout = open("bar.txt", "w")
fout.write("hello world")
fout.close()
- 118. pdb commands
●
h - help
●
s - step into
●
n - next
●
c - continue
●
w - where am I (in stack)?
●
l - list code around me