2D Lists
List Comprehensions
Packing and Unpacking
Dictionaries
What is the output?
100

How many times does this code print the word "Nathan"?

my_grid = []

for i in range(2):

    my_grid.append(["Nathan"] * 3)

print(my_grid)

6 times

100

what does this output?

nums = [i for i in range(1, 6)]

print(nums)

[1, 2, 3, 4, 5]

100

What does packing means in coding terms?

To put variables in a data structure

100

my_dictionary = {}

my_dictionary["Jonathan"] = 100

What role does the string "Jonathan" play?

It is a key for the dictionary

100

my_grid = []


for i in range(2):

    my_grid.append(["David"] * 3)


print(my_grid)

[

["David", "David", "David"],

["David", "David", "David"]

]

200

OLD TOPIC !!!!!!

How do you contain a list () or []

[]

200

What does this output?

nums = [i * 2 for i in range(1, 6)]

print(nums)

[2, 4, 6, 8, 10]

200

What is this called?

x, y, z, =  my_list

Unpacking

200

my_dictionary = {}

my_dictionary["Jonathan"] = 100

What part of the definition is 100?

The value

200

What is the Output:

my_list = []

my_list.append([1, 2, 3])

my_list.append([4, 5, 6])

print(my_list[1][2])

6

300

What does this code output?

my_grid = [[2, 5, 7, 10], [-3, "hello", 9, "hi"], ["hello", "hi", "hello","hi"],         [True, False, True, False]]
print(my_grid[:2])

[[2, 5, 7, 10], [-3, "hello", 9, "hi"]]

300

What does this output?

nums = [i % 2 == 0 for i in range(1, 6)]

print(nums)

[False, True, False, True, False]

300

What does this command do?

function_name(*list_name)

Assigns items in a list to parameters in a function

300

my_dictionary = {

    "a": 1,

    "b": 2

    "x": 5

    "g": 100

}


print("g" in my_dictionary)

What is printed?

True

300

my_list = []

for i in range(3):

    my_list.append([i * 1, i * 2, i * 4])


print(my_list)

[

[0, 0, 0],

[1, 2 , 4],

[2, 4, 8]

]

400

What does this command do?
list_name = ([element] * number)

It creates a list of specific amounts of list elements

400

What does this command do?

[operation for element in range(number)]

Alters each element in a list with the specified operation

400

What is this an example of?

x = 1

y = 2

z = 3

a = z + 2

b = y + 1

c = x - 1

my_list = [x, y, z]


print(my_list)

Packing

400


my_dictionary = {

    "a": 1,

    "b": 2

    "x": 5

    "g": 100

}

for key in my_dictionary:

    print("key: " + str(key))

    print("value: " + str(my_dictionary[key]))

What does the ninth line print?

Trick question: There is no ninth line!

400

my_list = []

for i in range(3):

    my_list.append([i * 1, i * 2, i * 4])


print(my_list[0][0:2])

[0, 0]

500

What game does this code create?

def get_valid_index(prompt):

    while True:

        try:

            index = int(input(prompt))

            if index >= 0 and index <= 2:

                return index

            print("Must be 0 - 2 inclusive!")

        except ValueError:

            print("Must be an integer!")

def game_is_over(board):

    for i in range(3):

        if board[i][0] == board[i][1] == board[i][2] \

            and board[i][0] != " ":

            print_board(board)

            print(board[i][0] + " wins!")

            return True

        if board[0][i] == board[1][i] == board[2][i] \

            and board[0][i] != " ":

            print_board(board)

            print(board[0][i] + " wins!")

            return True

    if board[0][0] == board[1][1] == board[2][2] \

        and board[0][0] != " ":

        print_board(board)

        print(board[0][0] + " wins!")

        return True

    if board[2][0] == board[1][1] == board[0][2] \

        and board[2][0] != " ":

        print_board(board)

        print(board[2][0] + " wins!")

        return True

    if " " not in board[0] and " " not in board[1] \

        and " " not in board[2]:

        print_board(board)

        print("Tie game!")

        return True

    return False

def print_board(board):

    print("")

board = []

for i in range(3):

    board.append([" "] * 3)

turn = "x"

while not game_is_over(board):

    print_board(board)

    print("It's " + turn + "'s turn.")

    row = get_valid_index("Row: ")

    col = get_valid_index("Col: ")

    if board[row][col] == " ":

        board[row][col] = turn

        print(board)

        if turn == "x":

            turn = "o"

        else:

            turn = "x"

    else:

        print("That space is taken")

Tic - Tac - Toe

500

What does this code do?

names = [

    "Maya Angelou",

    "Chimamanda Ngozi Adichie",

    "Tobias Wolff",

    "Sherman Alexie",

    "Aziz Ansari"

]

last_names = [name.split()[-1] for name in names]

print(last_names)

It Prints the Last names of the people in the list

500

What is this an example of?

def sum_three_numbers(x, y, z):

    return x + y + z

my_list = [1, 2, 3]

sum = sum_three_numbers(*my_list)

print(sum)

Unpacking

500

my_dictionary = {

    "a": 1,

    "b": 2,

    "x": 5,

    "g": 100

}

my_name = "David Agosu"

for letter in my_name:

    for character in my_dictionary:

        if letter == character:

            print(character)

What is printed?

a

g

500

my_phonebook = {}

while True:

    name = input("Enter your full name: ")

    if name == "":

        break

    elif name in my_phonebook:

        print(my_phonebook[name])

    else:

        number = input("Enter your phone number: ")

        my_phonebook[name] = number

print(my_phonebook)

Describe the output of this code.

It checks for names in a dictionary. If the name is there, it prints the phone number attached. If not, it asks the unknown user for a phone number, adds the name as a key and the phone number as a value, and prints the final dictionary.
M
e
n
u