[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
What is displayed by the below sequence of Python statements?
data = [2, 3, 5, 7, 11]
print data[2]
data[3] = data[1]
print data[3]
data[1] = 5
print data[3]
5 3 3
What does the below fragment display if next is
a list of integers containing
[3, 4, 5, −1, 2, 6, −2].
loc = 1
while loc >= 0:
print loc
loc = next[loc]
1 4 2 5 6
Suppose nums were a list of integers containing
[1, 2, 2, 3, 5, 5, 5, 8].
What would be in nums after executing the
below fragment?
j = 0
for i in range(1, len(nums)):
if nums[i] != nums[i - 1]:
nums[j] = num[i];
j = j + 1
[2, 3, 5, 8, 5, 5, 5, 8]
Suppose we have a list of integers referenced by the variable
vals containing
[8, 7, 6, 5, 4].
What will be in this list after executing the following
fragment?
for i in range(1, len(vals)):
vals[i] = vals[i - 1] + 1
[8, 9, 10, 11, 12]
Suppose nums refers to a list of numbers.
Write a Python fragment that displays the maximum
value within the list.
max = nums[0]
for i in range(1, len(nums)):
if nums[i] > max:
max = nums[i]
print max
Write a Python fragment that creates a list named
powers containing the first 100 powers of 2:
1, 2, 4, 8,….
powers = 100 * [0]
for i in range(100):
powers[i] = 2 ** i
Write a Python fragment that displays the longest
string in a list of words named names. For example, if names
refers to the list
[Martin, Galloway, Couch, Raney],
then your program should display Galloway.
longest = names[0]
for i in range(1, len(names)):
if len(names[i]) > len(longest):
longest = names[i]
print longest
Suppose we have two lists, an lists of strings called
candidates which enumerates the names of candidates
running in an election, and a list of integers (of the same
length) called votes which has the number of votes tallied
for each candidate. For example, votes[0] contains the
number of votes garnered by the candidate whose name is in
candidates[0].
Write a program fragment that prints the name of the candidate who received the most votes in the election.
max_pos = 0
for i in range(1, len(candidates)):
if votes[i] > votes[max_pos]:
max_pos = i
print candidates[max_pos]
Both functions and methods allow names to be associated with
more complex computations. For example, with strings,
you can use the len function and the count method.
What distinguishes them?
A function is initiated using just its name, and any
information it needs is supplied in the parameters, but a method
must be invoked by preceding the method name with the value
it's operating on
. Thus, if we have a string variable
called name, you'd invoke the count method by
writing
, but you'd invoke the
name.count('e')len function by writing
.len(name)
Using the graphics module, complete the below program fragment
so that it shows three circles stacked atop each other.
from graphics import *
window = GraphWin('Snowman')
center_low = Point(100, 160)
center_mid = Point(100, 90)
center_high = Point(100, 40)
ball_low = Circle(center_low, 40)
ball_mid = Circle(center_low, 30)
ball_high = Circle(center_low, 20)
ball_low.draw(window)
ball_mid.draw(window)
ball_high.draw(window)
Complete the below program so that the ball switches from red to green every second.
from graphics import *
import time
window = GraphWin('Flashing Ball')
center = Point(100, 100)
ball = Circle(center, 50)
ball.draw(window)
while not window.isClosed():
time.sleep(1)
from graphics import *
import time
window = GraphWin('Flashing Ball')
center = Point(100, 100)
color = 'red'
while not window.isClosed():
if color == 'red':
color = 'green'
else:
color = 'red'
ball.setFill(color)
time.sleep(1)