Python Basics
Python Basics
Python Basics
Tuple
tup = 4, 5, 6
tup
(4, 5, 6)
nested_tup = (4, 5, 6), (7, 8)
nested_tup
tuple([4, 0, 2])
tup = tuple('string')
tup
tup[0]
's'
tup = tuple(['foo', [1, 2], True])
#tup[2] = False
tup[1].append(3)
tup
(4, None, 'foo') + (6, 0) + ('bar',)
('foo', 'bar') * 4
a, b, c = tup
tup = 4, 5, (6, 7)
a, b, (c, d) = tup
tmp = a
a = b
b = tmp
a, b = 1, 2
print(a)
print(b)
b, a = a, b
print(a)
print(b)
seq = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
for a, b, c in seq:
print('a={0}, b={1}, c={2}'.format(a, b, c))
values = 1, 2, 3, 4, 5
a, b, *rest = values
a, b
rest
[3, 4, 5]
a, b, *_ = values
Tuple methods
a = (1, 2, 2, 2, 3, 4, 2)
a.count(2)
List
a_list = [2, 3, 7, None]
tup = ('foo', 'bar', 'baz')
b_list = list(tup)
print(b_list)
b_list[1] = 'peekaboo'
print(b_list)
gen = range(10)
gen
list(gen)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b_list.append('dwarf')
b_list
b_list.insert(1, 'red')
b_list
b_list.pop(2)
b_list
b_list.append('foo')
print(b_list)
b_list.remove('foo')
print(b_list)
'dwarf' in b_list
True
'dwarf' not in b_list
False
[4, None, 'foo'] + [7, 8, (2, 3)]
x = [4, None, 'foo']
x.extend([7, 8, (2, 3)])
everything = []
for chunk in list_of_lists:
everything.extend(chunk)
everything = []
for chunk in list_of_lists:
everything = everything + chunk
Sorting
a = [7, 2, 5, 1, 3]
a.sort()
[1, 2, 3, 5, 7]
b = ['saw', 'small', 'He', 'foxes', 'six']
b.sort(key=len)
Slicing
seq = [7, 2, 3, 7, 5, 6, 0, 1]
seq[1:5]
[2, 3, 7, 5]
seq[3:4] = [6, 3]
seq
[7, 2, 3, 6, 3, 5, 6, 0, 1]
seq[:5]
seq[3:]
[6, 3, 5, 6, 0, 1]
seq[-4:]
seq[-6:-2]
[6, 3, 5, 6]
seq[::2]
[7, 3, 3, 6, 1]
seq[::-1]
[1, 0, 6, 5, 3, 6, 3, 2, 7]
enumerate
i = 0
for value in collection:
some_list = ['foo', 'bar', 'baz']
mapping = {}
for i, v in enumerate(some_list):
mapping[v] = i
mapping
sorted
print(sorted([7, 1, 2, 6, 0, 3, 2]))
print(sorted('horse race'))
[0, 1, 2, 2, 3, 6, 7]
[' ', 'a', 'c', 'e', 'e', 'h', 'o', 'r', 'r', 's']
zip
seq1 = ['foo', 'bar', 'baz']
seq2 = ['one', 'two', 'three']
zipped = zip(seq1, seq2)
list(zipped)
seq3 = [False, True]
list(zip(seq1, seq2, seq3))
for i, (a, b) in enumerate(zip(seq1, seq2)):
print('{0}: {1}, {2}'.format(i, a, b))
0: foo, one
1: bar, two
2: baz, three
pitchers = [('Nolan', 'Ryan'), ('Roger', 'Clemens'),
('Curt', 'Schilling')]
first_names, last_names = zip(*pitchers)
print(first_names)
print(last_names)
reversed
list(reversed(range(10)))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
dict
empty_dict = {}
d1 = {'a' : 'some value', 'b' : [1, 2, 3, 4]}
d1
d1[7] = 'an integer'
print(d1)
print(d1['b'])
[1, 2, 3, 4]
'b' in d1
True
d1[5] = 'some value'
print(d1)
d1['dummy'] = 'another value'
print(d1)
del d1[5]
print(d1)
ret = d1.pop('dummy')
print(ret)
print(d1)
{'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer', 5: 'some value'}
{'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer', 5: 'some value', 'dummy': 'an
{'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer', 'dummy': 'another value'}
another value
print(list(d1.keys()))
print(list(d1.values()))
['a', 'b', 7]
d1.update({'b' : 'foo', 'c' : 12})
d1
{7: 'an integer', 'a': 'some value', 'b': 'foo', 'c': 12}
mapping = {}
for key, value in zip(key_list, value_list):
mapping[key] = value
mapping = dict(zip(range(5), reversed(range(5))))
mapping
{0: 4, 1: 3, 2: 2, 3: 1, 4: 0}
Default values
if key in some_dict:
value = some_dict[key]
else:
value = default_value
words = ['apple', 'bat', 'bar', 'atom', 'book']
by_letter = {}
for word in words:
letter = word[0]
if letter not in by_letter:
by_letter[letter] = [word]
else:
by_letter[letter].append(word)
by_letter
#hash('string') # gives error
hash((1, 2, (2, 3)))
#hash((1, 2, [2, 3])) # fails because lists are mutable
1097636502276347782
d = {}
d[tuple([1, 2, 3])] = 5
{(1, 2, 3): 5}
set
set([2, 2, 2, 1, 3, 3])
{2, 2, 2, 1, 3, 3}
{1, 2, 3}
a = {1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7, 8}
a.union(b)
a | b
{1, 2, 3, 4, 5, 6, 7, 8}
a.intersection(b)
a & b
{3, 4, 5}
c = a.copy()
c |= b
print(c)
d = a.copy()
d &= b
print(d)
{1, 2, 3, 4, 5, 6, 7, 8}
{3, 4, 5}
my_data = [1, 2, 3, 4]
my_set = {tuple(my_data)}
print(my_set)
print(len(my_set))
{(1, 2, 3, 4)}
a_set = {1, 2, 3, 4, 5}
{1, 2, 3}.issubset(a_set)
a_set.issuperset({1, 2, 3})
True
{1, 2, 3} == {3, 2, 1}
True
result = []
for val in collection:
if
strings = ['a', 'as', 'bat', 'car', 'dove', 'python']
[x.upper() for x in strings if len(x) > 2]
dict_comp = {
set_comp = {
unique_lengths = {len(x) for x in strings}
unique_lengths
{1, 2, 3, 4, 6}
set(map(len, strings))
{1, 2, 3, 4, 6}
loc_mapping = {val : index for index, val in enumerate(strings)}
loc_mapping
all_data = [['John', 'Emily', 'Michael', 'Mary', 'Steven'],
['Maria', 'Juan', 'Javier', 'Natalia', 'Pilar']]
names_of_interest = []
for names in all_data:
enough_es = [name for name in names if
name.count('e') >= 2]
names_of_interest.extend(enough_es)
result = [name for names in all_data for name in names
if name.count('e') >= 2]
result
['Steven']
some_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
flattened = [x for tup in some_tuples for x in tup]
flattened
[1, 2, 3, 4, 5, 6, 7, 8, 9]
flattened = []
Functions
my_function(5, 6, z=0.7)
my_function(3.14, 7, 3.5)
my_function(10, 20)
def func():
a = []
for i in range(5):
a.append(i)
a = []
def func():
for i in range(5):
a.append(i)
a = None
def bind_a_variable():
global a
a = []
bind_a_variable()
print(a)
[]
def f():
a = 5
b = 6
c = 7
return a, b, c
a, b, c = f()
return_value = f()
def f():
a = 5
b = 6
c = 7
return {'a' : a, 'b' : b, 'c' : c}
states = [' Alabama ', 'Georgia!', 'Georgia', 'georgia', 'FlOrIda',
'south carolina##', 'West virginia?']
import re
def clean_strings(strings):
result = []
for value in strings:
value = value.strip()
value = re.sub('[!#?]', '', value)
value = value.title()
result.append(value)
return result
clean_strings(states)
['Alabama',
'Georgia',
'Georgia',
'Georgia',
'Florida',
'South Carolina',
'West Virginia']
def remove_punctuation(value):
return re.sub('[!#?]', '', value)
clean_ops = [str.strip, remove_punctuation, str.title]
def clean_strings(strings, ops):
result = []
for value in strings:
for function in ops:
value = function(value)
result.append(value)
return result
clean_strings(states, clean_ops)
['Alabama',
'Georgia',
'Georgia',
'Georgia',
'Florida',
'South Carolina',
'West Virginia']
for x in map(remove_punctuation, states):
print(x)
Alabama
Georgia
Georgia
georgia
FlOrIda
south carolina
West virginia
def short_function(x):
return x * 2
equiv_anon = lambda x: x * 2
ints = [4, 0, 1, 5, 6]
apply_to_list(ints, lambda x: x * 2)
strings = ['foo', 'card', 'bar', 'aaaa', 'abab']
strings.sort(key=lambda x: len(set(x)))
strings
Generators
some_dict = {'a': 1, 'b': 2, 'c': 3}
for key in some_dict:
print(key)
dict_iterator = iter(some_dict)
dict_iterator
<dict_keyiterator at 0x7f5575e1b290>
list(dict_iterator)
def squares(n=10):
print('Generating squares from 1 to {0}'.format(n ** 2))
for i in range(1, n + 1):
yield i ** 2
gen = squares()
gen
for x in gen:
print(x, end=' ')
1 4 9 16 25 36 49 64 81 100
Generator expresssions
gen = (x ** 2 for x in range(100))
gen
def _make_gen():
for x in range(100):
yield x ** 2
gen = _make_gen()
sum(x ** 2 for x in range(100))
dict((i, i **2) for i in range(5))
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
itertools module
import itertools
first_letter = lambda x: x[0]
names = ['Alan', 'Adam', 'Wes', 'Will', 'Albert', 'Steven']
for letter, names in itertools.groupby(names, first_letter):
print(letter, list(names)) # names is a generator
A ['Alan', 'Adam']
W ['Wes', 'Will']
A ['Albert']
S ['Steven']
float('1.2345')
#float('something') # ERROR
1.2345
def attempt_float(x):
try:
return float(x)
except:
return x
print(attempt_float('1.2345'))
print(attempt_float('something'))
1.2345
something
#float((1, 2)) # ERROR
def attempt_float(x):
try:
return float(x)
except ValueError:
return x
#attempt_float((1, 2)) #ERROR
def attempt_float(x):
try:
return float(x)
except (TypeError, ValueError):
return x
f = open(path, 'w')
try:
write_to_file(f)
finally:
f.close()
f = open(path, 'w')
try:
write_to_file(f)
except:
print('Failed')
else:
print('Succeeded')
finally:
f.close()
Exceptions in IPython