Ch 7 and 8 test two
77
Output: temps = [65, 67, 72, 75] temps.append(77) print(temps[-1])
sorted()
built-in function provides the same sorting functionality as the list.sort() method, however it creates and returns a new list instead of modifying an existing list
list nesting
embedding of a list inside another list
inferred positional
empty {} assumes ordering of replacement fields is {0}, {1,}, etc
conversion type
indicate how to convert the value to a string ex: the 'd' in %d
positional
integer describes the position of value
{}
my_dict = {'Bob': 1, 'Jane': 42} my_dict.clear() print(my_dict)
{'Jane': 42}
my_dict = {'Bob': 1, 'Jane': 42} val = my_dict.pop('Bob') print(my_dict)
[1, 2, 9]
my_list = [1, 2, 3] my_list [2] = 9 print(my_list)
[1, 2, 3, 9]
my_list = [1, 2, 3] my_list[len(my_list):] = [9] print(my_list)
2
my_list = [1, 2, 3] print(my_list[1])
[5, 8, 14]
my_list = [14, 5, 8] my_list.sort()
[5, 40]
my_list = [5, 10, 20, 40, 80] print(my_list[0:5:3])
[20, 40, 80]
my_list = [5, 10, 20, 40, 80] print(my_list[2:])
[5, 10, 20, 40]
my_list = [5, 10, 20, 40, 80] print(my_list[:4])
[5, 14]
my_list = [5, 8, 14] my_list.remove(8)
[5, 8, 16]
my_list = [5, 8] my_list.append(16)
['1', '2', '3']
my_list = list('123') print(my_list)
[1, 3]
my_list= [1, 2, 3,] del my_list[1] print(my_list)
dict.keys()
num_calories = dict(Coke=90, Coke_zero=0, Pepsi=94) for soda in num_calories.keys(): print(soda) Coke Coke_zero Pepsi
dict.items()
num_calories = dict(Coke=90, Coke_zero=0, Pepsi=94) for soda, calories in num_calories.items(): print('%s: %d' % (soda, calories)) Coke: 90 Coke_zero: 0 Pepsi: 94
[3, 5, 8]
nums = [1, 1, 2, 3, 5, 8, 13] What is the result of nums [3:-1]
[8, 13]
nums = [1, 1, 2, 3, 5, 8, 13] What is the result of nums[5:10]
list
one of the most important and often used type in Python program
sort()
performs an in-place rearranging of the list elements, sorting the elements from lowest to highest
positional
'The {1} in the {0}'.format('hat','cat') The cat in the hat
Named
'The {animal} in the {headwear}'.format(animal='cat', headwear='hat') The cat in the hat
inferred positional
'The {} in the {}'.format('cat', 'hat') The cat in the hat
IndexError
Accessing an index that is out of range causes the program to automatically abort execution
my_dict[key] = value
Adds an entry if the entry does not exist, else modifies the existing entry
Agt2t
Agt2t3afc2kjMhagrds! What is the result of the expression my_str[0:5:1]
cnt_neg+=1
Count how many negative numbers (cnt_neg) there are. cnt_neg = 0 for i in num: if i < 0: SOLUTION
del my_dict[key]
Deletes the key entry from a dict
cat
Determine output: my_str = 'The cat in the hat' print(my_str[3:7])
/r/python
Determine output: my_str = 'http://reddit.com/r/python' my_str = my_str[17:]
Today is April 22
Determine the output of the following code snippets month = 'April' day = 22 print('Today is {month} {0}'.format(day, month=month))
Hi {Bilbo}!
Determine the output of the following code snippets print('Hi {{{0}}}!'.format('Bilbo'))
99
Given the list nums = [[10, 20, 30], [98, 99]], what does nums[1][1] evaluate to?
reddit.com/r/python
Output: my_str = 'http://reddit.com/r/python' protocol = 'http://' my_str = my_str[len(protocol):]
my_dict.keys()
Print each key in the dictionary my_dict for key in SOLUTION: print(key)
Positional Inferred positional Named
Replacement field...
False
T\F The output of the following is [13, 7, 5] primes = [5, 13, 7] primes.sort() print(primes)
True
T\F The sort() method modifies a list in-place
conversion specifier
placeholder for a value in a string literal ex:%d, %s, %f
in-place modification
the growing and shrinking of a list
enumerate()
variables pos and token being assigned the current loop iteration element's index and value
replacement field
{}
slice notation
creates a new string whose value mirrors the characters of string from positions start to end ex: my_str[start:end]
stride
determines how much to increment the index after reading each element
nested dictionaries
dict contains another dictionary as a value
dict()
dict(Bobby='805-555-2232', Johnny='951-555-0055')
format()
format text
conversion operator
inserts values from the tuple on the right into conversion specifiers on the left ex:%
index
integer corresponding to a specific position in the list's sequence of elements
list comprehension
iterates over a list, modifies each element, and returns a new list consisting of the modified elements
1. An expression component to evaluate for each element in the iterable object. 2. A loop variable component to bind to the current iteration element. 3. An iterable object component to iterate over (list, string, tuple, enumerate, etc)
list comprehension components...
dict.values()
num_calories = dict(Coke=90, Coke_zero=0, Pepsi=94) for soda in num_calories.values(): print(soda) 90 0 94
container
object that groups related objects together
named
a name matching a keyword argument
multi-dimensional data structure
scores = [ [75, 100, 82, 76], [85, 98, 89, 99], [75, 82, 85, 5] ]
scores[0][1]
scores = [ [75, 100, 82, 76], [85, 98, 89, 99], [75, 82, 85, 5] ] Write an indexing expression that gets the element from scores whose value is 100