+ 1

Can somebody please explain what is happening here?

t=10,20,30 for i in range (len(t)): print(t[i]) #output is 10,20,30 #But if I type print(t[i-1]) #output is 30,10,20 print(t[i-2]) # output 20,30,10 #if I type print(t[i in range(0,1)] #output is 20,10,10

13th Feb 2022, 9:29 AM
మాధురి
మాధురి - avatar
3 Réponses
+ 2
Check out again.. Are you missed anything in question? t=10,20,30 #makes a tuple for i in range (len(t)): print(t[i]) #output is 10,20,30 print(t[i-1]) #output is 20 , here temparary variable i has remains and retaining its value i=2, so i-1 index refers value 20 in t. print(t[i-2]) # output 10, similar way, i-2=0, t[0]=10 print(t[i in range(0,1)]) #output 10, in range(0,1) returns only 0 so t[0] is printed 10
13th Feb 2022, 9:47 AM
Jayakrishna 🇮🇳
+ 2
t = 10, 20, 30 # basic slicing print(t[0]) # 10 print(t[1]) # 20 print(t[2]) # 30 print() # negative slicing print(t[-1]) # 30 print(t[-2]) # 20 print(t[-3]) # 10 ''' range(len(t)) is the same as range(3) range(3) will give the iterations of 0, 1, 2 So t[i-1] is the same as saying t[-1] # 0 -1= -1 t[0] # 1 -1 = 0 t[1] # 2 -1 = 1 '''
13th Feb 2022, 9:57 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Thank you all for your help
15th Feb 2022, 7:54 AM
మాధురి
మాధురి - avatar