Using python please answers questions R6.4 & R6.7 (6.4 goes onto the next page)
ID: 3687963 • Letter: U
Question
Using python please answers questions R6.4 & R6.7 (6.4 goes onto the next page)HW16 Review Questions 327 e.1 4 9 16 9 74 9 11 g.0 1 23 4 01 2 3 a [1, 2, 3, 4, 5, 4, 3, 2, 1, 0 R6.2 Consider the following list: What is the value of total after each of the following loops complete? a. total-0 for i in range (10) total- total ati] b.total0 for i in range (O, 10, 2): total total ati] c.total-0 for i in range (1, 10, 2): total total a[i] d. total 0 for i in range (2, 11): total total+ a[i] e. total while i
Explanation / Answer
Q6.4
Range(I,j,k) in python will give you list of values starting from i and ends with j-1 and every time it advance with step k
So range(1,5,1) will give [1,2,3,4]
This is the basic which is used in every loop below.
A.
a=[1,2,3,4,5,4,3,2,1,0]
for i in range(1,10):
a[i]=a[i-1]
print (a)
Output: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Explanation:
Here range will give [1,2,3,4,5,6,7,8,9]
Then for every value of i
Example for i=1
A[1]=a[1-1]à a[1]=a[0] Hence a[1] is 2 before after that it will become 1.
So finaly we got
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
B. a=[1,2,3,4,5,4,3,2,1,0]
for i in range(9,0,-1):
a[i]=a[i-1]
print (a)
Output: [1, 1, 2, 3, 4, 5, 4, 3, 2, 1]
Explanation:
Here range will have negative value as 3rd parameter. So it will advance in negative direction starting from 9 and move downwards 1 step So will give [9,8,7,6,5,4,3,2,1]
Then for every value of i
Example for i=9
A[9]=a[9-1]à a[9]=a[8] Hence a[9] is 0 before after that it will become 1.
So finaly we got
: [1, 1, 2, 3, 4, 5, 4, 3, 2, 1]
C:
a=[1,2,3,4,5,4,3,2,1,0]
for i in range(9):
a[i]=a[i+1]
print (a)
Output: [2, 3, 4, 5, 4, 3, 2, 1, 0, 0]
Explanation:
hen for every value of I from[0-8]
Example for i=0
A[0]=a[0+1]à a[0]=a[1] Hence a[0] is 1 before after that it will become 2.
So finaly we got
[2, 3, 4, 5, 4, 3, 2, 1, 0, 0]
D.
a=[1,2,3,4,5,4,3,2,1,0]
for i in range(8,-8,-1):
a[i]=a[i+1]
print (a)
Output: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Explanation:
One thing to note in this is If we give negative index in list like
A[-1] then it will point list starting from Last with indexing start from 1.
So a[-2] will give you 1.
Consider this rule Here
E.
a=[1,2,3,4,5,4,3,2,1,0]
for i in range(1,10):
a[i]=a[i]+a[i-1]
print (a)
Output: [1, 3, 6, 10, 15, 19, 22, 24, 25, 25]
F:
a=[1,2,3,4,5,4,3,2,1,0]
for i in range(1,10,2): # advance by 2
a[i]=0
print (a)
output: [1, 0, 3, 0, 5, 0, 3, 0, 1, 0]
G:
a=[1,2,3,4,5,4,3,2,1,0]
for i in range(5):
a[i+5]=a[i]
print (a)
Output:
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
H:
a=[1,2,3,4,5,4,3,2,1,0]
for i in range(1,5):
a[i]=a[9-i]
print (a)
Output:
[1, 1, 2, 3, 4, 4, 3, 2, 1, 0]
Q6.7
Q 6.7
A:
values=[1,2,3,4,5,6,7,8,9,10]
for i in range(1,11):
values[i]=i*i
print (values)
Explanation:
Here error is Index out of Range
Because in range function range(1,11) will give [1,2,3,4,5,6,7,8,9,10]
For every i you are doing i*I and assigning it to values[i].
So when i=10
Values[10]=10*10 but value[10] not exist. Index in values are from 0-9 only.
So this is wrong.
B. values=[]
for i in range(len(values)):
values[i]=i*i
print (values)
Explanation:
Here List is empty so in range become range(0) which will give nothing and in output you will get empty List again
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.