LESSON 03 – LISTS

 


WORK

PROGRAMS

 

PROGRAM 1 – LIST PROGRAM ANALISYS

 

Consider the following code.  First, try to predict what each of the following print statements will output.  Write your answers down.  Then execute the code to see if you are correct.

 

list = [5,4,8]

print(list)             #A

 

list.append(2)

print(list)             #B

 

list.insert(1,-2)

print(list)             #C

 

list[0] = 1

print(list)             #D

 

PROGRAM 2 – TEAM NAME GENERATOR

 

Create a list with at least five attributes such as “Angry”, “Vengeful”, “Mega” and “Awesome”.  Create another list with nouns such as “Wolves”, “Storm Troopers”, “Warriors” and “Otters”.

 

Now write the code to randomly generate a Team Name such as “Angry Otters” or “Mega Warriors”.

 

PROGRAM 3 – LAST, ALPHABETICALLY SPEAKING

 

Get the user to enter five words.  Output the word that appears last in the dictionary.

 

Hints:

-Add the five words to a list using the append method.  (See note)

-Sort the list using:  listname.sort()

-Output the fifth element in the list using its index number.  (See note)

EXPLORATION PROBLEMS

 

Try the following programs out to see what is going on.

 

a)

What does the third line do?

 

listA = [1,2,3]

listB = [4,5,6]

listC = listA + listB

print(listC)

 

b)

What does the first line do?

 

myList = ["Yo"] * 5

print(myList)

 

c)

What does that if statement check for exactly?  Run the program and try changing the number 3 to other values.

 

myNumbers = [4,9,34,12,19]

 

if 3 in myNumbers:

    print ("It's in the list")

else:

    print("It's not in the list")

d)

What does the second line do?  Try changing the function min to the function max.

 

list = [9,2,10,18,7]

value = min(list)

print(value)

 

e)

What does line 2 do?  Perhaps you need to change the 1 to other values to figure this out.

 

list=[1,2,5,3,1,4,5,9,4,2]

occ = list.count(1)

print(occ)

 

f)

What does the choice function from random do? 

 

import random

medals = ["gold", "silver", "bronze"]

print(random.choice(medals))

g)

What does this do?  Try to figure it out before running the program.

 

prefix = "Never gonna"

sendings = ["make you cry", "say goodbye", "let you down", "give you up"]

lendings = ["run around and desert you", "tell a lie and hurt you"]

print(prefix, sendings[3])

print(prefix, sendings[2])

print(prefix, lendings[0])

print(prefix, sendings[0])

print(prefix, sendings[1])

print(prefix, lendings[1])