[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18]
If x
's value is 18, what is the value of each of the
following Python expressions?
a. 3 + x * 2
b. x % 7
c. x // 5
a. 3 + x * 2
is 39
b. x % 7
is 4
c. x // 5
is 3
If k
's value is 5, what is the value of each of the
following Python expressions?
a. 10 + 2 * k ** 2
b. 127 % k
c. 52 // k * k
a. 10 + 2 * k ** 2
is 60
b. 127 % k
is 2
c. 52 // k * k
is 50
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 |
If name
refers to the string Arkansas, what is
name[3:6]
?
What are the values referenced by a
, b
, and
c
following the execution of the below fragment?
a = 'x'
b = 'y'
c = 'z'
a = b
b = c
c = a + 'b'
a : |
'y' |
b : |
'z' |
c : |
'yb' |
The following program will not work, even for a simple input such as 16. Explain what is wrong, and show how to repair the program to work as intended.
x = float(input())
print('Square root is ' + (x ** 0.5))
The value of x ** 0.5
is a floating-point number,
and Python does not allow adding a string and a number.
The solution is to use the str
function to compute
the string correspoding to x ** 0.5
; that string can be
added to another string:
print('Square root is ' + str(x ** 0.5))
Complete the below program so that it reads a string from the user and displays the string where the first letter has been moved to the end and followed by ay. For example, the input nix should produce the output ixnay, or road should produce oadray.
word = input()
word = input()
first = word[0]
rest = word[1:len(str)]
print(rest + first + 'ay')
Write a program that reads two integers from the user and displays the two numbers in increasing order, separated by a comma. For example, if the user enters 4 and then 8, it should display “4, 8”; the same output should result if the user enters 8 and then 4.
a = int(input())
b = int(input())
smaller = min(a, b)
larger = max(a, b)
print(str(smaller) + ', ' + str(smaller))
Tabulate all values taken on by the variables i
, j
,
and j
as the program fragment below executes.
j = 3
k = 4
for i in range(1, 4):
j = k - j
k = k + i
i : |
1, | 2, | 3 | |
j : |
3, | 1, | 4, | 3 |
k : |
4, | 5, | 7, | 10 |
This question removed to Hydra.
This question removed to Hydra.
The harmonic number for an integer n is the sum of the reciprocals of the integers from 1 to n. For example, the harmonic number for 3 is 1/1 + 1/2 + 1/3 = 1 5/6. Complete the below program so that it computes and displays the harmonic number corresponding to the integer given by the user. If the user enters 2, the program should display 1.5; for 3, it should display 1.83333333333.
print('Find harmonic number for: ')
n = int(input())
An answer | Alternative answer |
total = 0 | total = 0 |
This question removed to Hydra.
This question removed to Hydra.
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.
word = input()
out = ''
# your solution here
print('Result is: ' + out)
An answer | Alternative answer |
for i in range(0, len(word), 2): | for i in range(len(word) // 2): |
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
Suppose vals
refers to the list of integers
[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]
This question removed to Hydra.
This question removed to Hydra.
Complete the below program fragment so that, given
a list vals
of integers, it creates another list prefix
containing the sum of the entries of vals
up to that entry.
For example, if vals
is [3, 4, 5] before your fragment,
then the fragment will create prefix
as [3, 7, 12];
the first entry is the same as in vals
,
the second entry is the sum of the first two entries of vals
(3 + 4),
and the third entry is the sum of the first three entries of vals
(3 + 4 + 5).
prefix = len(vals) * [0] # creates a list the same length as vals, all 0's
An answer | Alternative answer |
for i in range(len(vals)): | total = 0 |