+ 2
List operarions
num=(10, 9,8,7,6,5) num[0]=num[1]-5 If 4 in nums: print(num[3]) else: print(num[4]) How's the ans is 7....i cant understand anyone can help me?
11 ответов
+ 1
num[0]= num[1] - 5 #num[1] is 9
num[0]= 9 - 5
num[0]= 4
+ 6
In the beginning:
num = [10, 9, 8, 7, 6, 5]
because:
num[0] = num[1] - 5
and num[1] is 9,
and num[0] = 4
thus now:
num = [4, 9, 8, 7, 6, 5]
Because 4 in [4, 9, 8, 7, 6, 5], num[3], which is 7, is printed.
+ 2
Num=(10,9,8,7,6,5)
Num[0]=num[1]-5
If in nums:
Print(nums[3])
Else:
Print(nums[4])
It is the answer because they said output nums number 3
And remember when you start counting you start from zero
Now the new will be
Num=(4,9,8,7, 6, 5)
So the third one we will be 7
And if you still don't understand go back to the beginning where the lesson just started it's no big deal
+ 1
Because list first item is the 0, the second item is 1...
In this example num[0] is 10, num[1] is 9...
+ 1
I'll try and break it down line by line:
Line #1
num=(10,9,8,7,6,5)
# The list and its contents are provided and assigned to the variable num
Line #2
num[0]=num[1]-5
# num[0] is reassigning the value of the 1st element in the list (currently 10) to the difference of the value of the 2nd element (currently 9) minus 5. 9-5=4. The 1st element now has a value of 4. Now, num=(4,9,8,7,6,5)
Line #3
If 4 in num:
# This is testing if the value of 4 exists in the list assigned to the num variable. If it is True (i.e. the value is found), move on to the next line. If False, move on to Line #6
Line #4
print(num[3])
# This is saying to print the value of the 4th Element in list assigned to the num variable (currently 7)
Line #6
print(num[4])
# This is saying to print the 5th element of the list assigned to the variable num (currently 6) if the test in Line 3 were False.
0
num[0] = num[1] -5#num[1] is 9
num[0]=9-5
num[0]=4
0
nums = [3, 2, 8]
print(not 4 in nums)
print(4 not in nums)
print(not 3 in nums)
print(3 not in nums)
0
List starts from [0,1,2,3,4..........]
num = [10,9,8,7,6,5]
0 is 10
1 is 9
2 is 8
3 is 7
4 is 6
5 is 5
- 1
Given is num,condition is num[0]=num[1]-5,so we assign num[1]=9-5=4
Go to loop
If 4 is print num[3]
Result is 4 so we print the 3 num.3 num is 7.so 7 is answer
- 1
Coming to 2nd row
num[0] which is 10 is asigned ir replaced with num[1]-5 or 9-5 or 4
Now in 3rd row.... 4 in nums is true hence num[3] means 4th numbers will be printed which is 7
- 2
List operarions
num=(10, 9,8,7,6,5)
num[0]=num[1]-5
#num[0] = 10 and num[1]-5 = 9-5 = 4.
#we take the number four and we affect to num[0] which is equal to 10,that mean num[0] = 4 now.
If 4 in nums:
#from what is above we know that the number 4 exists in the list, so the condition is checked.
print(num[3])
#like that our condition is checked, so the first print () is executed.which gives us the result as 7, because do you remember that:num[0]=4,num[1]=9,num[2]=8, and num[3]= 7, ...etc
else:
print(num[4])