Lists and Loops

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Suppose you have a list defined in the following way: myList = [[9, 8], [7, 6], [5, 4], [3, 2]] What are the correct dimensions of this list?

4 x 2

Code flow can be tricky to follow. What is the function call sequence (excluding returns) and output for the following program: def delta(num): if num > 1: num = num - 2 else: num = num + 2 return num def gamma(num): if num > 1: num = num / 2 else: num = num / -2 return num def beta(num): if num > 1: num = num * 2 num = gamma(num) else: num = num * -2 num = delta(num) return num def alpha(num): if num > 0: num += 1 num = beta(gamma(num)) else: num -=1 num = beta(delta(num)) return num print('output = ', alpha(-5))

alpha, delta, beta, delta with an output of 6. recommended online code compiler for other coding questions if you wish to do so!

What data collection type is calc_dB after the following code segment is executed: import math def dB(P1, P2): return [10 * math.log10(P1/P2), P1, P2] calc_dB = dB(10,5)

list

What data collection type is calc_dB after the following code segment is executed: import math def dB(P1, P2): return {10 * math.log10(P1/P2), P1, P2} calc_dB = dB(10,5)

set

What is the output of the following program: def outer_math(x): y = x + 1 def inner_math(): z = x + y return z return inner_math() z = outer_math(2) print("z = ", z)

z = 5

Which term defines repeating a block of code for a specific number of times?

definite iteration

Which term defines repeating a block as long as a condition is met?

indefinite iteration

After this code segment is executed, what will be the dimensions of the list multiples: multiples = [] for outer in range(1,3): multiples.append([]) for inner in range(1,3): multiples[outer-1].append(inner) print(multiples)

2 x 2

Suppose you have a list defined in the following way: myList = [[9, 8, 7, 6], [5, 4, 3, 2]] What are the correct dimensions of this list?

2 x 4

What is the result of this code? while myNum > 0: myNum = myNum - 1 print(myNum) print("Blastoff")

An error because myNum has not been defined.

A user that inputs 1 and 3 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 since the index is out of range.

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 * 3 my_third_list.extend('h') print (my_third_list[5:])

['c', 'a', 'b', 'c', 'h']

In the update to the tic-tac-toe game, why was a sentinel value used?

It creates a variable that can be set to true to exit the loop.

myType = [1, 2, "car", True] What collection type is created in the code snippet above?

List

Which of the following options is a property of Python lists?

List elements can be accessed by an index value.

What is the error within the following code segment? while Num > 0: Num = Num - 1 print(Num)

Num has not been initialized.

What is the result of this code? for num in range(-1,1): if num == 0: break print("Num is set to: ",num) print("Num is set to: ",num)

Num is set to: -1 Num is set to: 0

Functions can be used to replace multiple code statements that perform essentially the same task with different values. This occurs in the Tic-Tac-Toe game program within the tutorial. See the printBoard function created there as an example. What other code within the Tic-Tac-Toe program would be a good set of statements to turn into a function?

Players selecting a column and row: 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.") while (row < 1 or row > 3): row = int(input(playerTurn + " player, select a row 1-3: ")) if (row < 1 o)r row > 3): print("The row must be between 1 and 3.")

Functions can be used to replace multiple code statements that perform essentially the same task with different values. This occurs in the Tic-Tac-Toe game program within the tutorial. See the printBoard function created there as an example. Functions often need to switch between doing something based on their input, such as offering different queries to a user. What string method could be used to switch between printing "select a column" and "select a row"?

The .format() method; for example, col_or_row = 'column' print("The {} must be between 1 and 3.".format(col_or_row))

What is wrong with the following code segment? for count in range(3,2): print("Count is set to:",count)

The values for range will not enable the print statement to be reached.

What is wrong with the following code segment? Num = 1 while Num > 0: Num = Num + 1 print(Num)

The while loop is an infinite loop.

Functions can be used to replace multiple code statements that perform essentially the same task with different values. This occurs in the Tic-Tac-Toe game program within the tutorial. See the printBoard function created there as an example. If using a function to implement the players selecting a column and row portion of the Tic-Tac-Toe program, what input arguments would it need in its definition?

Whether selecting a column or row, and the player; for example def turn(col_or_row, player):

What should the result of this code be? my_list = ['pear','orange','pineapple','apple','peach','kiwi','banana'] my_other_list = my_list my_other_list.sort() print(my_other_list)

['apple','banana','kiwi','orange','peach','pear','pineapple']

What should the result of this code be? my_list = ['pear','orange','pineapple','apple','peach','kiwi','banana'] my_other_list = my_list my_other_list.sort() print(my_list) print(my_other_list)

['apple','banana','kiwi','orange','peach','pear','pineapple'] ['apple','banana','kiwi','orange','peach','pear','pineapple']

A programmer writes a function to __________.

contain a section of code that is later called and executed


Kaugnay na mga set ng pag-aaral

Business Law Exam 1 WS (Topics 2 thru 12)

View Set

Chapter 10 - Infancy and Childhood Pretest

View Set

Microsoft Access Proctored Exam 2 Study Guide

View Set

Economic Public Policy Questions

View Set

Unit 3: Structuring the Business Firm

View Set