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
32
10
146
27.3K
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@PythonPr Answer: B) hello Solution: On the surface it looks trivial. s is initialized 'hello'. t is assigned s. s is modified to upper. So it 'HELLO' now t is printed. What is the value of t? t hasn't explicitly been changed, so it's going to the initial 'hello'. But you might +
English
1
0
5
2.2K
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@PythonPr have seen some bizarre stuff with lists. Where you change one variable, and the change automatically reflects in another variable. Eg. a = [] b = a b.append(1) the append to b automatically modifies `a` as well. Why doesn't that happen here? The reason is because +
English
1
0
1
237
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@PythonPr strings are immutable, ie, after creation, they cannot be modified. (unlike lists) therefore when s = 'hello' is initialized, the object 'hello' is created in memory. As this is a string, no way it's going to be changed. t = s t references the same object as s. +
English
1
0
1
201
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@PythonPr s.upper() s.upper cannot modify the existing object 'hello' because it's immutable. So, s.upper() has NO CHOICE other than create a new string object 'HELLO' and return it. The returned object is referenced to s via `=` s = s.upper() So this was a re-assignment, ie, +
English
1
0
1
103
Paylaş