Introduction to python programming Milestone 2
Iteration is performed using a(n) ______ which enables a program to repeat a block of code.
loop
Suppose you have a list defined in the following way: myList = [[12, 11, 10, 9], [8, 7, 6, 5], [4, 3, 2, 1]] What are the correct dimensions of this list?
3 x 4
What is the result of this code? my_list = ['1','2','3'] print(sum(my_list))
An error because the sum function is only valid on a list when the list elements are numbers
A user that inputs 1 and 2 into the following code will result in what output? board = {"-", "-", "-", "-", "-", "-", "-", "-", "-"} col = int(input("X player, select a column: ")) row = int(input("X player, select a row: ")) board[row][col] = "X" print(board[0]) print(board[1]) print(board[2])
An error when board[row][col] = "X" is executed.
What is the result of this code? my_first_list = ['a', 'b', 'c'] my_second_list = ['d', 'e', 'f', 'g'] my_third_list = my_first_list * 2 my_third_list.upper() print (my_third_list)
An error, because upper is not a method available for lists.
What do sets and tuples have in common?
Both are immutable.
What is the return result of the dB2 function? import math def dB2(param1, param2): dB_calc = 20 * math.log10(param1/param2) if dB_calc > 6: return "High SNR" elif dB_calc > 0: return "Low SNR" return "Noise" print(dB2(10,5))
High SNR
Which of the following options is a property of Python lists?
Lists can be nested (list within a list).
In the update to the tic-tac-toe game, why was there such a reduction in program size?
Repetitive statements were programmed using loops.
When would you use a break statement in a while loop?
To exit the loop.
Which code segment has the correct syntax and format to execute its for loop?
Y = ['1','2','3'] for X in Y: print(X)
What is the output of the print statement for the following code: multiples = [] for threeD in range(1,3): multiples.append([]) for twoD in range(1,3): multiples[threeD-1].append([]) for oneD in range(1,3): multiples[threeD-1][twoD-1].append(twoD) print(multiples)
[[[1,1], [2,2]], [[1,1], [2,2]]]
A recursive formula can be implemented in Python and then used to provide the nth term in a series. Which of the following recursive programs implements the recursive formula below and prints out the value of its 5th term (11): base: a(1) = 3 recursion: a(n) = a(n -1) + 2
def recursive_1(num): if num == 1: return 3 return recursive_1(num - 1) + 2 print('rec_1 ',recursive_1(5))
Functions can be used to replace multiple code statements. This occurs in the Tic-Tac-Toe game program within the tutorial 2.3.5 Finishing the Tic-Tac-Toe Program; see the printBoard function created there as an example. What if we wanted to replace the following lines in the Tic-Tac-Toe game: while (col < 1 or col > 3): col = int(input(playerTurn + " player, select a column 1-3: ")) if (col < 1 or col > 3): print("The column must be between 1 and 3.") with a function called turn. For the call to this function, we could use: col = turn("column", playerTurn) where we input into the function that we want to select a column ("column") and which player is making this selection (playerTurn). Which code segment properly implements this function?
def turn(col_row, player): good_selection = False while (good_selection == False): col_row_picked = int(input(playerTurn + " player, select a {} 1-3: ".format(col_row))) if (col_row_picked < 1 or col_row_picked > 3): print("The {} must be between 1 and 3.".format(col_row)) else: good_selection = True return col_row_picked
Where is the optimal place to insert a print statement in the following code to debug it? 1 multiples = [] 2 3 for outer in range(1,3): 4 5 multiples.append([]) 6 7 for inner in range(1,3): 8 9 multiples[outer-1].append(inner) 10
line 10
Scope defines where variables can be accessed. What variables are within the scope of the inner2 function? def outer1(i): j = i + 1 def outer2(): k = j + 1 def inner1(): m = k + 1 def inner2(): n = q + m + 1 return n m = inner2() return m k = inner1() return k j = outer2() return j print ("outer1(1) = ", outer1(1))
n, m, k, j, i, q
For a user-defined function, arguments are assigned to __________ and used within a function.
parameters