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
39
10
132
21.9K
The root of infinity
The root of infinity@parth_hirpara05·
Ans---- 10 The code initializes a variable score to the value 10. Then, a new variable saved_score is assigned the current value of score, which is 10. The score variable is then updated to 15, but this change does not affect the value already stored in saved_score. The print function outputs the value of saved_score.
English
0
0
4
823
Hiba El-Amin
Hiba El-Amin@hiba_el_amin·
@PythonPr 15 because they both refer to the same address
English
1
0
1
1.7K
Earnest Codes
Earnest Codes@Earnesto037·
@PythonPr Answer: (a) 10 The code initializes the variable score to 10. The line saved_score = score assigns the current value of score (which is 10) to saved_score. Subsequent changes to the score variable (like adding 5 to it) do not affect the value already stored in
English
1
0
1
883
DocsAllOver
DocsAllOver@docsallover·
@PythonPr 10 — assignment copies the value, not a reference. Updating score later doesn’t affect savedscore.
English
0
0
1
378
Raunak
Raunak@RJ00030·
@PythonPr 10, as integers are immutable in python
English
0
0
0
665
Aaron Powell
Aaron Powell@PowellOnPoint·
@PythonPr Non-AI answer: Score is a namespace that points to an object. Saved_score points to the same object when compiled. Since Python is line-by-line execution, saved_score now just references the same object as score. The score namespace is then reassigned to a new object.
English
0
0
0
138
Ranjan
Ranjan@Ranjanrgdev·
@PythonPr 15 Because they are referring to the same variable
English
1
0
0
1.6K
Ron Sperber
Ron Sperber@MathProf·
@PythonPr 10. Integers don't get passed by reference because they are immutable. A list is mutable and doing b=a when a is mutable, then modifying a will modify b.
English
1
0
0
490
Tanmay Newatia
Tanmay Newatia@thetanmaydoes·
@PythonPr 10 when assigning the integer value to another variable, it is passed by value rather than reference, hence the addition operation doesn't affect the previous assigned value.
English
0
0
6
1.3K
Tanmay Newatia
Tanmay Newatia@thetanmaydoes·
if at the same time, if the questions would have been: score=[1] saved_score=score score+=[2] print(saved_score) here the answer would have been: [1,2] as due to the list being passed as a reference. this is just a layer of abstraction in python, as in c++ we distinguish when to pass by value and reference.
English
0
0
3
1.3K
Paylaş