# I am wendering how the outcome is 22?

#1

Q-10.What will be the output of the following code snippet?

``````fruit_list1 = ['Apple', 'Berry', 'Cherry', 'Papaya']
fruit_list2 = fruit_list1
fruit_list3 = fruit_list1[:]

fruit_list2[0] = 'Guava'
fruit_list3[1] = 'Kiwi'

sum = 0
for ls in (fruit_list1, fruit_list2, fruit_list3):
if ls[0] == 'Guava':
sum += 1
if ls[1] == 'Kiwi':
sum += 20

print (sum)
``````

A. 22
B. 21
C. 0
D. 43

#2

What do you think it should be, and why?

#3

I THINK it should be 21.
because:

fruit_list1 == [‘Apple’, ‘Berry’, ‘Cherry’, ‘Papaya’]
fruit_list2 == [‘Guava’, ‘Berry’, ‘Cherry’, ‘Papaya’]
fruit_list13== [‘Apple’, ‘kiwi’, ‘Cherry’, ‘Papaya’]

SO when the loop comes to the first “if statment” it will not change anything because their is not ‘Guava’ in fuitlist1[0], and not ‘kiwi’ in firstlist[1], means “sum” still 0.

but in the scond round sum get +1 because fruitlist2[0] == ‘Guava’
and in the last loop, fruitlist3[1] == ‘kiwi’ so sum fet +20 means that sum now is 21!

#4

When this assignment is made, it will affect both list1 and list2, since list2 is only a reference to list1. Any change in one, will be reflected in the other.

Only list3 is independent of the other two lists. When we change that list, it has no effect on the other two.

Therefore, `1 + 1 + 20 => 22`.

#5

ok, i got it.
so now: why list2 is only a reference to list1, and list3 not ?

#6
``````list2 = list1
``````

Lists are pointer objects in that they are not values, only pointers to the values in their elements. In the above assignment we are not creating a copy of list1, but creating a second reference to the list. There is only the one list in memory.

``````list3 = list1[:]
``````

Above we assign a slice of list1 to list3. A slice is a copy of the list, so list3 references a different list than list2.

#7

last quetion: list3 takes all the elements in list1 right?

#8

Correct. `list1[:]` is the equivalent of, `list1[0:len(list1)]`

#9

**thank you very much uncle ROY i appreciate it **