sports = ["football", "soccer", "baseball", "basketball"]

# change the value "soccer" to "hockey"
sports.remove("soccer")
sports.insert(1, "hockey")
print (sports)
['football', 'hockey', 'baseball', 'basketball']
sports = ["football", "soccer", "baseball", "basketball"]
sports.insert(2, "golf")
# add "golf" as the 3rd element in the list
print (sports)
['football', 'soccer', 'golf', 'baseball', 'basketball']

HW Iterations

words = ["alfa", "beta", "charlie", "delta", "echo", "foxtrot", "golf", "hotel", "india", "julitt", "kilo", "lima", "mike", "november", "oscar", "papa", "quebec", "romeo", "sierra", "tango", "uniform", "victor", "wiskey", "xray", "yankee", "zulu"]


# Get ASCII value of char 'a'

ascii_a = ord('a');


# Get input from terminal

print("Please type in a word (alphabets ONLY) ...")

in_string = input()



# Convert the input string into all lower case

in_string = in_string.lower()

print(in_string + " ->" + "\n")


# Find ASCII value of each "char" in the "string"

# The difference of ASCII value between these characters and "a"

# will become the index to the list of words


for k in range(len(in_string)) :

     in_ascii = ord(in_string[k]) - ascii_a

     print(words[in_ascii])
Please type in a word (alphabets ONLY) ...
toby ->

tango
oscar
beta
yankee

2D Array

  • Find another way to print the matrix
keypad =   [[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9],
            [" ", 0, " "]]

def print_matrix3(matrix):
    for i in matrix:
        print(*i)
            
print_matrix3(keypad)
1 2 3
4 5 6
7 8 9
  0  

Hacks

  • Print what month you were born and how old you are by iterating through the keyboard (don't just write a string).
def lookup_keyboard(msg) :



# Create a virtual keyboard by 2-D array

#   - It is actually a list, with 2-dimenstion 

#   - In total it has 4 rows, each row has different number of list elements 



     keyboard = [["`", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, "-", "="],

                 ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]"],

                 ["A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'"],

                 ["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"] ]



     for k in range(len(msg)) :  # Loop each letter in mybirthday

          bb = msg[k]   # Get the k-th letter in mybirthday

          if bb.isnumeric() :  # Convert it to a number if it is numeric

               bb = int(bb)

          for i in range(4) :   # Loop through each row of keyboard array

               if keyboard[i].count(bb) : # To check if an element is in the list or NOT

                    idx = keyboard[i].index(bb)  # If it is in, then find its index

                    print(keyboard[i][idx])  # Print out the letter in myname by index







myname = "Max"

myname = myname.upper()

print("My name is : ")

lookup_keyboard(myname)

print("\n")





mybirthday= "2006.1.25"

mybirthday= mybirthday.upper()

print("My birthday is : ")

lookup_keyboard(mybirthday)

print("\n")



mymonth = "January"

mymonth= mymonth.upper()

print("The month of my birthday is : ")

lookup_keyboard(mymonth)

print("\n")



myage = "16"

myage = myage.upper()

print("My age is : ")

lookup_keyboard(myage)

print("\n")
My name is : 
M
A
X


My birthday is : 
2
0
0
6
.
1
.
2
5


The month of my birthday is : 
J
A
N
U
A
R
Y


My age is : 
1
6