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
what does this output?
nums = [i for i in range(1, 6)]
print(nums)
[1, 2, 3, 4, 5]
What does packing means in coding terms?
To put variables in a data structure
my_dictionary = {}
my_dictionary["Jonathan"] = 100
What role does the string "Jonathan" play?
It is a key for the dictionary
my_grid = []
for i in range(2):
my_grid.append(["David"] * 3)
print(my_grid)
[
["David", "David", "David"],
["David", "David", "David"]
]
OLD TOPIC !!!!!!
How do you contain a list () or []
[]
What does this output?
nums = [i * 2 for i in range(1, 6)]
print(nums)
[2, 4, 6, 8, 10]
What is this called?
x, y, z, = my_list
Unpacking
my_dictionary = {}
my_dictionary["Jonathan"] = 100
What part of the definition is 100?
The value
What is the Output:
my_list = []
my_list.append([1, 2, 3])
my_list.append([4, 5, 6])
print(my_list[1][2])
6
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"]]
What does this output?
nums = [i % 2 == 0 for i in range(1, 6)]
print(nums)
[False, True, False, True, False]
What does this command do?
function_name(*list_name)
Assigns items in a list to parameters in a function
my_dictionary = {
"a": 1,
"b": 2
"x": 5
"g": 100
}
print("g" in my_dictionary)
What is printed?
True
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]
]
What does this command do?
list_name = ([element] * number)
It creates a list of specific amounts of list elements
What does this command do?
[operation for element in range(number)]
Alters each element in a list with the specified operation
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
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!
my_list = []
for i in range(3):
my_list.append([i * 1, i * 2, i * 4])
print(my_list[0][0:2])
[0, 0]
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
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
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
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
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.