Post

Python Programming
Python Programming@PythonPr·
Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!
Python Programming tweet media
English
20
15
79
7.1K
Theoden
Theoden@J_Theoden·
@PythonPr 2/x Example: print(func(1)) print(func(2, [987]) the output would be: [1] [987, 2] because the default b=[] in the function definition would be overwritten by the [987] and then have 2 append to it, rather than the original b=[].
English
0
0
0
226
Jimmy Fikes
Jimmy Fikes@akajim·
D, but not really. The options we are shown here do not show what is really happening. When the first function call is made by print(func(1)), [1] is printed (and we are not presented with this result in the options shown) - D is not the full answer. That first function call sends only one argument to func() and a gets a value of 1. b acts like a default paramenter (not requiring that we ever send an argument for b). In the body of the function, that first 1 is appended to b (so the parameter in the function signature is now b=[1]). The second function call print(func(2)) sends 2, positionally, to a which is then appended to b, and the b parameter now contains [1,2]. If we added a third function call func(3), b could become [1,2,3]. However, all of this would change if we sent a 3rd function call that included print(func(3,['a','b','c'])). Now, the values in b ([1,2]) would be overwritten by the new argument for b and the 3 would be appended to the new list, giving us ['a', 'b', 'c', 3]. The tricky part of this snippet is that as long we are using any of the mutable collection data types as a default parameter (lists, sets, or dictionaries), we can have a parameter that retains new values with each call of the function.
English
0
0
1
101
Theoden
Theoden@J_Theoden·
Actually the answer is A and D. The first func(1) print produces an output of [1]. The second func(2) print appends 2 to the original list so its output is [1, 2]. As an exercise, if you supplied a different list in either print 1 or print 2, then the original list would be overwritten. 1/x
English
0
0
1
225
TIME
TIME@TIME·
At Nvidia's annual GTC conference, CEO Jensen Huang showcased the brands Vera Rubin system, its superchip platform.
English
142
338
3.1K
8.2M
Starlink
Starlink@Starlink·
Stream. Scroll. Surf. Get speeds up to 400+ Mbps to connect with friends and family without delays.
English
255
718
4.7K
16.7M
Mohamed Ibrahim
Mohamed Ibrahim@MahmmedIbr88346·
@PythonPr The answer will be D because the function put an empty list and a variable named a. On the next line, it told him to add the variable a inside the list. On the next line, it told him that after the operation is completed, return or count back to list b, and then the print command
English
0
0
2
54
Paylaş