TechSmart Python Unit 4

Ace your homework & exams now with Quizwiz!

When the append function adds a new item to a list, where does it put the item?

At the end of the list. Append means to add on to the end. It puts the new item at the end of the list.

Using the code below, what would the list of integers that would be returned from the range() function? range(10)

0, 1, 2, 3 , 4, 5, 6, 7, 8, 9 If not given a minimum, the range() function starts at 0. It goes up to but does not include the limit argument.

Using the code below, how many items will be in the list squares after the code below is finished running? squares = [] for i in range(10): squares.append(i * i)

10 The range function here returns the values 0 - 9, for a total of 10 iterations. One item is appended to squares in each iteration.

In the list below, what value is at index position 0? my_list = [2, 4, 8, 16, 32, 64, 128, 256]

2 List indexes start counting at 0, so 0 is the position of the first item in the list.

Using the code below, how many times will the code loop? while drawing: for event in pygame.event.get(): if event.type == pygame.QUIT: for i in range(4): r = pygame.Rect(x_values[i], 0, 100, 100) pygame.draw.rect(window, colors[i], r)

4

What is the index position of the value "The Yellow Balloon"? books = ["The Hunger Games", "The Gray Bird", "The Cat in the Hat", "One Fish, Two Fish", "The Yellow Balloon"]

4 Because list indexes start counting at 0, the index of the final item is always the length of the list - 1.

Using the code below, what is the last value returned by the following range function? range(0, 50)

49 The list returned by the range function does not include the second argument, or limit. It approaches that value but stops short of it.

Looking at the code below, how many times will the for-each loop run? squares = [2, 4, 8, 16, 32] for power in squares: print("The next power of 2 is: " + str(power))

5 A for-each loop runs once for each item in the given list.

How many times will the loop below repeat? letters = ["b", "r", "j", "w", "q", "p"] word = "" for letter in letters: word += letter

6 A for-each loop will repeat once for each item in the given list.

In the list below, what is the index position of the value 256? my_list = [2, 4, 8, 16, 32, 64, 128, 256]

7 List indexes start counting at 0, so the final item in a list has an index equal to the list's length - 1.

What is the length of the list below? my_list = [2, 4, 8, 16, 32, 64, 128, 256]

8 Length is the number of values in a list.

What is the value at index position 2? test = [93, 95, 87, 79, 97, 89]

87 Index is the order of an item in a list. The count starts from the front of the list at 0.

What is the for-loop below doing? word = "treehouse" total = 0 for letter in word: if letter == "e": total += 1

A for-each loop goes through each item in a list (or string) one at a time. The conditional tests to see if each letter is "e" and, if so, increases the total by 1. So, it is counting the number of times the letter "e" is in the word.

Using the code below, what is the for-loop doing? numbers = [] for i in range(0, 50): nums.append(i * 5)

Based on the range, this will loop through the values 1 - 50. Each iteration will add i * 5 onto the end of the list nums; first 5, then 10, then 15, and so on.

Using the code below, what is the for-loop doing? nums = [5, 10, 15, 20, 25, 30, 35, 40] for i in range(len(nums)): nums[i] = nums[i] * 2

Each iteration gets the current value, multiplies it by 2, and then replaces the value at the current index with the new one.

What is true about the loop variable in a for-i-in-range loop?

It is always an integer. All versions of the range function return a list of integers, so the i variable in the loop always stands for the integers in that list.

Looking at the code below, what is the final value of the variable acronym? words = ["Random", "Acronym", "Division"] acronym = "" for current_word in words: acronym += current_word[0]

RAD Strings can be used like a list of symbols. Bracket notation [ ] for index position 0 of a string will return the first letter of that string. The += operator adds that symbol onto the end of the string in the acronym variable.

what does append mean?

The append() method in python adds a single item to the existing list.

What is a loop variable?

The loop variable changes every iteration to be the next item in the list that is being examined. In other words, it changes in each iteration of the loop.

Using the code below, why is the second for-i-in-range loop more efficient than the first? numbers = [10, 15, 20, 25, 30, 35, 40, 45, 50] for i in range(9): print(numbers[i]) for i in range(len(numbers)): print(numbers[i])

The second for-i-in-range is more efficient because if the list were to change in size (smaller or larger), then the first for -i-in-range would throw an error code.

What is a common use of a for-i-in-range loop?

To perform an action an exact number of times. A for-range loop repeats the exact number of times specified in the range part of the loop clause.

What should you name the loop variable in a for-each loop?

You can make up any variable name that makes sense based on the code.

Which of the following is an example of a list? (0, 1, 2, 3, 4, 5) [0, 1, 2, 3, 4, 5]

[0, 1, 2, 3, 4, 5] Lists must be surrounded by square brackets

Using the code below, what would the list of numbers look like after the for-i-in-range loop has run? numbers = [100, 200, 300, 400, 500] for i in range(len(numbers)): numbers[i] = int(numbers[i] / 100)

[1, 2, 3, 4, 5] In each iteration, the list value at index i is divided by 100, and then re-assigned to that same index position.

Using the code below, what would the list of integers that would be returned from the range() function? range(10,20)

[10, 11, 12, 13, 14, 15, 16, 17, 18, 19] f specified with two arguments, the first argument to range is the minimum, where the list begins. The final argument is the limit, which the list gets up to but does not include.

Using the code below, what is the loop variable that is used inside the loop block? while drawing: for event in pygame.event.get(): if event.type == pygame.QUIT: for i in range(4): r = pygame.Rect([i], 0, 100, 100) pygame.draw.rect(window, colors, r)

[i] In for-range loops, the loop variable is almost always i.

Using the code below, what do the arguments (10, 0, -1) represent? for i in range(10, 0, -1): countdown = 1 * i print(str(countdown) + " more seconds until the New Year!") pygame.time.wait(1000)

arguments (10, 0, -1) 10= START - The first argument is the number where we start - in other words, the highest number 0 = END - one less than the lowest number where we want to finish -1 = INCREMENT - will be negative, so that i decreases. (For normal counting down, the increment will be -1.)

Look at the code below, use square bracket [ ] notation to print out the first letter of the string word. book = "One Fish, Two Fish"

book = "One Fish, Two Fish" print(book[0])

How could you change the following code to use the len() function to get the length of book_length? books = ["The Hunger Games", "The Gray Bird", "The Cat in the Hat", "One Fish, Two Fish", "The Yellow Balloon"] book_length = 5

book_length = len(books) The len() function returns the length of the collection that is passed to it as an argument

What will the following list look like after the remove command has been executed? books = ["The Hunger Games", "The Gray Bird", "The Cat in the Hat", "One Fish, Two Fish", "The Yellow Balloon"] books.remove("The Cat in the Hat") books.remove("The Yellow Balloon")

books = ["The Hunger Games", "The Gray Bird", "One Fish, Two Fish"] remove() gets rid of an item from a list,

What will the following list look like after the list is appended? books = ["The Hunger Games", "The Gray Bird", "The Cat in the Hat", "One Fish, Two Fish", "The Yellow Balloon"] books.append("The Blue Gill")

books = ["The Hunger Games", "The Gray Bird", "The Cat in the Hat", "One Fish, Two Fish", "The Yellow Balloon", "The Blue Gill"] append() adds a new item on to the end of the list.

What syntax (using square brackets) will give you the item at index position 7 in the list books?

books[7] The syntax for bracket notation is the name of the list you are searching, followed by square brackets around the index position you are searching for.

What data type does the operator return?

boolean

Looking at the code below, what is the for-each clause? ints = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print("Here are the squares for each integer 1 - 10: ") for base in ints: square = base * base print(str(base) + " squared is " + str(square))

for base in ints: The for-each clause is the line that starts a for-each loop. It begins with the word for and ends with a colon (:).

How could you simplify the code below using a for-i-in-range loop? i = 10 while i < 20: print(i) i += 1

for i in range( 10, 20): print (i) Since i starts as 10, 10 should be the minimum for the range. The loop continues until i gets up to (but is not equal to) 20, so that is the range limit.

Using the code below, which line of code is the loop block? while drawing: for event in pygame.event.get(): if event.type == pygame.QUIT: for i in range(4): r = pygame.Rect([i], 0, 100, 100) pygame.draw.rect(window, colors, r)

for i in range(4):

In the code below, which line of code replaces the last item in the list with a new value. groceries = ["cheese", "OJ", "peanut butter", "milk"] healthy = "low-fat milk" first = groceries[0] groceries[3] = healthy

groceries[3] = healthy Bracket notation [ ] gets a specific position in a list. Use it to get an item at that position, or to replace an item at that position.

Using the code below, what string will print after this loop finishes? word = "football" new_word = "" for i in range(len(word) - 1, -1, -1): new_word += word[i] print(new_word) hint*** counting down and len(word)

llabtoof The for-loop starts at the end of the string and goes backwards, adding each letter onto the new string. The result is the first string in reverse. -1, -1, -1 are your arguments

Using the code below, how could you add a print statement to the block of this for-i-in-range loop so that it prints out every item in the list colors. colors = ["red", "orange", "yellow", "green", "blue", "violet"] for i in range(len(colors)):

print(colors[i]) The variable i starts at 0 increases by 1 in each loop iteration. Therefore it can be used as indexes for a list.

Using the code below, how could you add a print statement that prints the loop variable in every iteration of the loop. alph = ["a", "b", "c", "d", "e"] for letter in alph:

print(letter) The loop variable is the word after the for in the for-each clause. It has a new value in each iteration of the loop.

Using the code below, what is the loop-block that uses the loop variable? for i in range(50): r = int(i * .5) pygame.draw.circle(w, black, (0, 0), r) pygame.flip() ​

r = int(i * .5) In most for-range loops, the loop variable is the letter i. This variable can be used for anything that needs to increase in every iteration of the loop, including indexes.

The range function counts up from 1 to 10. What would code look like if you were counting down from 10 to 1 and including 1.

range (10, 0, -1) With three arguments, range starts from the first value (including it), counts towards the second value (not including it), and counts by (increments by) the third value each iteration.


Related study sets

Bio 151 - McGraw SB (Ch 2.2-2.5)

View Set

CHAPTER 2 - BUSINESS ETHICS AND SOCIAL RESPONSIBILITY

View Set