Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

What Is the Output of the below Code? Justify Your Answer.

Satinder Rawal answered on April 24, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers


What Is the Output of the below Code? Justify Your Answer.

0

>>def addToList(val, list=[]):

>> list.append(val)

>> return list

>>list1 = addToList(1)

>>list2 = addToList(123,[])

>>list3 = addToList('a’)

>>print ("list1 = %s" % list1)

>>print ("list2 = %s" % list2)

>>print ("list3 = %s" % list3)

Output:

list1 = [1,’a’]

list2 = [123]

lilst3 = [1,’a’]

Note that list1 and list3 are equal. When we passed the information to the addToList, we did it without a second value. If we don't have an empty list as the second value, it will start off with an empty list, which we then append. For list2, we appended the value to an empty list, so its value becomes [123].

For list3, we're adding ‘a’ to the list. Because we didn't designate the list, it is a shared value. It means the list doesn’t reset and we get its value as [1, ‘a’].

Remember that a default list is created only once during the function and not during its call number. 

Popularity 1/10 Helpfulness 1/10 Language whatever
Source: Grepper
Link to this answer
Share Copy Link
Contributed on Apr 24 2023
Satinder Rawal
0 Answers  Avg Quality 2/10


X

Continue with Google

By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
X
Grepper Account Login Required

Oops, You will need to install Grepper and log-in to perform this action.