Contents
This notebook is based on "Think Python, 2Ed" by Allen B. Downey
https://greenteapress.com/wp/think-python-2e/
In [1]:
[10, 20, 30, 40]
['dog', 'fish', 'bird']
Out[1]:
In [2]:
['bob', 3.14, 42, ['sam', 55]]
Out[2]:
[]
In [3]:
cheeses = ['Chedder', 'Pepper Jack', 'Queso Fresca']
grades = [99, 84, 91]
empty = []
In [4]:
cheeses = ['Chedder', 'Pepper Jack', 'Queso Fresca']
cheeses[0] = 'Gouda'
print( cheeses )
in
operator
In [5]:
for cheese in cheeses:
print( cheese )
range
and len
functions
In [6]:
numbers = [1, 2, 3]
for i in range( len( numbers ) ):
numbers[i] = numbers[i] * 2
print( numbers )
for
loop over an empty list never executes the body of the loop
In [7]:
nested_list = ['spam', 1, [ 'Brie', 'Roquefort' , 'Pol le Veq'], [1, 2, 3] ]
len( nested_list )
Out[7]:
In [8]:
a = [ 1, 2, 3 ]
b = [ 4, 5, 6 ]
c = a + b
print( c )
*
operator repeats a list a specified number of times
In [9]:
d = [ 0 ] * 4
print( d )
e = [ 1, 2, 3 ] * 3
print( e )
In [10]:
f = [ 'a', 'b', 'c', 'd', 'e', 'f' ]
print( f[1:3] )
print( f[:4] )
print( f[3:] )
In [11]:
f[1:3] = [ 'x', 'y' ]
print( f )
In [12]:
list1 = [ 'a', 'b', 'c' ]
list1.append( 'd' )
print( list1 )
extend
takes a list as an argument and appends all of the elements
In [13]:
list2 = [ 'a', 'b', 'c' ]
list3 = [ 'd', 'e', ]
list2.extend( list3 )
print( list2 )
print( list3 )
list3
is unmodifiedsort
arranges the elements of a list from low to high
In [14]:
list4 = [ 'd', 'c', 'e', 'a', 'b' ]
list4.sort()
print( list4 )
void
None
In [15]:
def add_all( a_list ):
total = 0
for value in a_list:
total += value
return total
+=
operator updates a variabletotal
is used in the loop as an accumulatorsum
function to sum the elements of a list
In [16]:
list_of_numbers = [ 1, 2, 3 ]
sum( list_of_numbers )
Out[16]:
reduce
function
In [17]:
def capitalize_all( a_list ):
result = []
for a_string in a_list:
result.append( a_string.capitalize() )
return result
words = [ 'life', 'the', 'universe', 'and', 'everything' ]
capitalized_words = capitalize_all( words )
print( capitalized_words )
map
function since it "maps" a function onto each of the elements in a sequence
In [18]:
def only_upper( a_list ):
result = []
for a_string in a_list:
if( a_string.isupper() ):
result.append( a_string )
return result
words = [ 'LIFE', 'The', 'uNiverse', 'AND', 'everything' ]
some_words = only_upper( words )
print( some_words )
In [19]:
a_list = [ 'a', 'b', 'c' ]
value = a_list.pop( 1 )
print( a_list )
print( value )
del
operator
In [20]:
a_list = [ 'a', 'b', 'c' ]
del a_list[1]
print( a_list )
del
can also be used with a slice index to remove more than one element
In [21]:
a_list = [ 'a', 'b', 'c', 'd', 'e', 'f' ]
del a_list[1:5]
print( a_list )
remove
operator
In [22]:
a_list = [ 'a', 'b', 'c', 'a', 'b', 'c' ]
a_list.remove( 'b' )
print( a_list )
In [23]:
a_string = 'spam'
a_list = list( a_string )
print( a_list )
list
as a variable namel
either since it looks like a 1
a_list
or something more descriptive detailing a value in your algorithmsplit
In [24]:
a_string = 'pining for the fjords'
a_list = a_string.split()
print( a_list )
In [25]:
a_string = 'spam-spam-spam'
delimiter = '-'
a_string.split( delimiter )
Out[25]:
join
is the inverse of split
In [26]:
a_list = [ 'pining', 'for', 'the', 'fjords' ]
delimiter = ' '
delimiter.join( a_list )
Out[26]:
In [27]:
a = 'banana'
b = 'banana'
a is b
Out[27]:
In [28]:
a = [ 1, 2, 3 ]
b = [ 1, 2, 3 ]
a is b
Out[28]:
In [29]:
a = [ 1, 2, 3 ]
b = a
b[0] = 17
print( a )
In [30]:
def delete_head( a_list )
del a_list[0]
letters = [ 'a', 'b', 'c' ]
delete_head( letters )
print( letters )
In [31]:
list1 = [ 1, 2 ]
list2 = list1.append( 3 )
print( list1 )
print( list2 )
In [32]:
def cumulative_sum( a_list ):
# INSERT YOUR CODE HERE
return []
numbers = [ 1, 2, 3 ]
result = cumulative_sum( numbers )
# The answer should be:
# [ 1, 3, 6 ]
chop
that takes a list and modifies it by removing the first and last element. The function should return None
.
In [33]:
def chop( a_list ):
# INSERT YOUR CODE HERE
print( 'Remove this line' ) # Jupyter needs a statement to compile
numbers = [ 1, 2, 3, 4 ]
chop( numbers )
print( numbers )
# Should print: [ 2, 3 ]
is_anagram
that takes two strings and returns True
if they are anagrams.