Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A beginning Python programmer was asked to predict what value would be returned

ID: 3663793 • Letter: A

Question

A beginning Python programmer was asked to predict what value would be returned by each of the following commands. In each case, you'll be given the command and an incorrect prediction. Give the correct answer, and say why the given prediction was wrong. So for example, if the statement was 'foo'+'bar' and the prediction was 'foo bar', you would say something like "The prediction is wrong because concatenating strings with + doesn't insert a space. The correct value is'foobar'" For each problem, assume the definition pangram='Waltz, bad nymph, for quick jigs vex!'.

a) Command: pangram[4]
Prediction: 't'

b) Command: pangram[37]
Prediction: 'W'

c) Command: pangram[-2]
Prediction: an error occurs

d) Command: pangram[2:9]
Prediction: 'ltz, bad'

e) Command: pangram[5:1]
Prediction: 'ztla'

f) Command: pangram[3:3]
Prediction: 't'

g) Command: pangram[30:]
Prediction: an error occurs

h) Command: pangram[30:40]
Prediction: an error occurs

Explanation / Answer

pangram='Waltz, bad nymph, for quick jigs vex!'.

a) Wrong prediction
Answer is z, because index of array starts from 0, 4 means 5th element

b) Wrong prediction
It gives index out of range because there are total 37 characters in the string
but index range = [0,36], as index starts from 0

c) Wrong prediction
It gives answer as x,
in python, negative index gives character from last of string [total charactes + negative inde]
i.e -2 index means = 37-2=35 th element

d) Wrong prediction
Correct Prediction: 'ltz, ba'
list[start:end] gives items start through end-1
Here till index 9-1 = 8, but d is at index = 9

e) Wrong prediction
It give empyt string, By defualt python assumes index range is in ascending order

f) Wrong predictionram[3:3]
In python[start,end] means items start through end-1, so end = 3-1 = 2, not in ascending order so
gives empty string

g) Wrong prediction.
No error occurs, it displays substring starting from index 30 to till end

h) Wrong prediction
It just display till end of string i.e index-36 which is less than 40

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote