





















Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!






















A List is a Kind of Collection
print([1, 24, 76]) [1, 24, 76] print(['red', 'yellow', 'blue']) ['red', 'yellow', 'blue'] print(['red', 24, 98.6]) ['red', 24, 98.6] print([ 1, [5, 6], 7]) [1, [5, 6], 7] print([]) []
Lists and Definite Loops - Best Pals friends = ['Joseph', 'Glenn', 'Sally'] for friend in friends : print('Happy New Year:', friend) print('Done!')
z = ['Joseph', 'Glenn', 'Sally'] for x in z: print('Happy New Year:', x) print('Done!')
Just like strings, we can get at any single element in a list using an index specified in square brackets 0 Joseph
friends = [ 'Joseph', 'Glenn', 'Sally' ] print(friends[ 1 ]) Glenn 1 >>> Glenn 2 Sally
We can create a new list by adding two existing lists together
a = [1, 2, 3] b = [4, 5, 6] c = a + b
t = [9, 41, 12, 3, 74, 15] t[1: 3 ] [41,12] t[: 4 ] [9, 41, 12, 3] t[3:] [3, 74, 15] t[:] [9, 41, 12, 3, 74, 15] Remember: Just like in strings, the second number is “up to but not including”
some = [1, 9, 21, 10, 16] 9 in some True 15 in some False 20 not in some True
numlist = list() while True : inp = input('Enter a number: ') if inp == 'done' : break value = float(inp) numlist.append(value) average = sum(numlist) / len(numlist) print('Average:', average) total = 0 count = 0 while True : inp = input('Enter a number: ') if inp == 'done' : break value = float(inp) total = total + value count = count + 1 average = total / count print('Average:', average) Enter a number: 3 Enter a number: 9 Enter a number: 5 Enter a number: done Average: 5.