python ch7 lists and tuples
fill a list with values
num = [0] * 5 # all 5 elements are set 0 index = 0 while index < len(num): num[index] = 99 # all 5 elements are set 99 index += 1
negative number slices
num = [1, 2, 3, 4 , 5, 6, 7, 8, 9, 10] print(num[-5:]) returns [ 6, 7, 8, 9, 10]
increment slice
num = [1, 2, 3, 4 , 5, 6, 7, 8, 9, 10] print(num[1: 8: 2]) returns [ 2, 4, 6, 8]
total values in list
num = [2, 4, 6, 8, 10] total = 0 for value in num: total += value print(total)
indexing
access individual elements in a list Example: my_list = [10,20,30] print(my_list[0], my_list[1], my_list[2])
append(item)
adds item to end of list. name_list = [ ] name = input('enter name: ') name_list.append(name)
len
my_list = [ 10, 20, 30] size = len( my_list ) returns value of 3 which is the number of elements in the list
list()
a python built-in function that can convert certain types of objects to lists. EXAMPLE: numbers = list( range(5)) output: [0, 1, 2, 3, 4]
two separate but identical lists
list1 = [1, 2, 3, 4] list2 = [ ] + list1
copy a list
list1 = [1, 2, 3, 4] list2 = list1
concatenate lists
list1 = [1, 2, 3] list2 = [4, 5, 6] list3 = list1 + list2 or list1 += list2
nested lists or two-dimensional lists
lists of lists. rows and columns.
element
each item in a list is called an element
use len to avoid IndexError
my_list = [ 10, 20, 30] index = 0 while index < len( my_list ): print(my_list[index]) index += 1
DEL statement
my_list = [1, 2, 3, 4, 5] print('before deletion: ', my_list) del my_list[2] print('after deletion: ', my_list) returns [1, 2, 4, 5]
negative indexes (prints list in reverse)
my_list = [10,20,30] print(my_list[-1], my_list[-2], my_list[-3]) output: 30 20 10
lists are mutable
num = [ 1, 2, 3, 4, 5] print(num) returns [1, 2, 3, 4, 5] num[0] = 99 print(num) returns [99, 2, 3, 4, 5]
iterating over a list with FOR loop
num = [ 1, 2, 3] for n in num: print (n) output: 1, 2, 3
insert (index, item)
inserts item in list at the specified index.
IndexError exception
invalid index
lists (is mutable which means you can chg its contents) even_num = [2,4,6,8,10] or diff types info = ['Tom', 27, 15.50]
is a dynamic data structure, meaning items may be added to them or removed from them. You can index, slice and other methods to work with the list.
tuple
is a sequence, very much like a list but it is not changeable (immutable).
sequence (two types: lists and tuples)
is an object that holds multiple items of data, stored one after the other. You can perform operations on a sequence to examine and manipulate the items stored in it.
remove(item)
removes the first occurrence of the item from the list. A ValueError occurs if the item is not found.
index (item) item_index = food.index(item)
returns the index of the first element whose value is equal to item. A ValueError exception is raised if the item is not found in the list.
min or max function
returns the item that has the lowest (min) value or highest (max). my_list = [1, 2, 3, 4, 5] print('the lowest value is ', min(my_list)) returns a 1
reverse()
reverses the order in the items of the list
averaging a list
scores = [2.5, 7.3, 6.5, 4.0, 5.2] total = 0.0 for value in scores: total += value avg = total/len(scores) print(avg)
item in list
search for an item in the list. prod_nums = [ 'v5', 'f9, 'q8'] search = input('enter product number: ') if search in pro_nums: or not in print(search, ' was found.')
slice list_name[ start: end] example: num = [1, 2, 3, 4, 5] print(num[ 1:3 ] returns [2, 3]
selects a range of elements from a sequence. the expression returns a list containing a copy of the elements from START up to (but not including) END. START defaults to zero. print(num[:3]). if you leave off the END index python default to the length. if you leave off START and END print(num[:]), you will get the entire copy of the list.
iterable
similar to a list, it contains a sequence of values that can be iterated over in a loop. EXAMPLE: for num in range(5): print(num) this works the same: for num in [0,1,2,3,4]: print(num) for num in range(1,5): print(num) output: 1, 2, 3, 4 for num in range(1,10, 2): output: 1-9 step by 2
sort() my_list = [ 'b', 'a', 'd', 'g'] my_list.sort() print('sorted order: ', my_list) returns [ 'a', 'b', 'd', 'g']
sorts the item in the list so they appear in ascending order (from lowest to highest)
repetition operator (*) list * n EXAMPLE: num = [0] * 5 print(num) output: [0,0,0,0,0] num = [1,2,3] * 2 print(num) output: [1,2,3,1,2,3]
the * symbol multiplies to numbers. however, when the operand is on the left side of the * symbol is a sequence (such as a list) and the operand on the right side is an integer, it becomes the repetition operator. "List" is the list and "n" is the number of copies to make.
saving a list to a file
the drawback to the writelines method is that it does not automatically write a newline ('\n') at the end of each item. So it is one string.