[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
What is displayed by the below sequence of Python statements?
k = 31
print 64 - k * 2
print k % 8
print 8 * (k / 8)
2 displayed for 64 - k * 2 7 displayed for k % 8 24 displayed for 8 * (k / 8)
What is displayed by the below sequence of Python statements?
k = 18
x = 3.5
print k + x
print 22/5 * x
print k % 7
print 1 + x * 2
21.5 displayed for k + x 14.0 displayed for 22/5 * x 4 displayed for k % 7 8.0 displayed for 1 + x * 2
List the values of a, b, and c
following the execution of the below fragment.
a = 10
b = 20
c = 40
a = b
a = c
a: | 40 |
b: | 20 |
c: | 40 |
List the values of a, b, and c
following the execution of the below fragment.
a = 10
b = 20
c = 40
a = b
b = c
c = a
a: | 20 |
b: | 40 |
c: | 20 |
Suppose we wish to enter the below if statement's
body if the square of x isn't between 99 and 101. What
condition belongs in the parentheses?
if :
x = (x + 1.0 / x) / 2.0
x * x < 99 or x * x > 101
Suppose we have a vertical line segment from
(lx,ly0) to
(lx,ly1) (where
ly0 < ly1).
What should go into an if statement's condition
to test whether a point
(px,py) lies on that line?
if :
print 'point on line'
px == lx and py >= ly0 and py <= ly1
Tabulate the values taken on by each variable as the program fragment below executes.
n = 32
k = n + 1
for i in range(2, 7):
if n % i == 0:
k = k + i + (n / i)
n |
32 | ||||
k |
33, | 51, | 63 | ||
i |
2, | 3, | 4, | 5, | 6 |
The below program reads 100 numbers from the user. Modify the program so that after the loop is complete, the program displays the sum of all numbers the user typed.
for index in range(100):
number = input('Number? ')
total = 0
for index in range(100):
number = input('Number? ')
total = total + number
print total
What are the first and last lines displayed by the below program fragment?
for i in range(1234, 4321):
s = str(i)
if s[1] == '5':
print s
The first is 1500
and the last is 3599
.
Tabulate the values taken on by the variables
i, j, k, and res as the
program fragment below executes.
base = 'barber'
res = 'a'
for i in range(len(base)):
k = -1
for j in range(i):
if base[j] == base[i]:
k = j
if k < 0:
res = res + base[i]
print res
i |
0, | 1, | 2, | 3, | 4, | 5 | |||||||||||||||
j |
0, | 0, | 1, | 0, | 1, | 2, | 0, | 1, | 2, | 3, | 0, | 1, | 2, | 3, | 4 | ||||||
k |
−1, | −1, | −1, | −1, | 0, | −1, | −1, | 2 | |||||||||||||
res |
ab, | aba, | abar, | abare |
The program displays abare
.
Complete the following program so that it displays the number of lower-case r's in the string entered by the user.
line = raw_input('Line of text: ')
count = 0
for i in range(len(line)):
if line[i] == 'r':
count = count + 1
print count
Complete the program below so that it computes another string containing every other letter from the string typed by the user. For example, if the user enters blackberry, the program should compute baker, and if the user enters steadfastness, the program should compute sedates.
Note that your program should build up its result in the
out variable rather than display letters as it goes
through the string.
in = raw_input('Give me a line: ')
out = ''
# your solution here
print 'Result is:', out
for i in range(0, len(in), 2):
out = out + in[i]
An alternative solution:
for i in range(len(in)):
if i % 2 == 0:
out = out + in[i]