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
25
13
74
12.3K
Earnest Codes
Earnest Codes@Earnesto037·
@PythonPr Answer: A) ['a', 'b', 'c'] In Python, the += operator, when used with a list, acts like the list.extend() method. The extend() method iterates over its argument and adds each individual item to the list in-place. Initial list: x is assigned the list ['a', 'b'].
English
1
0
6
352
MHH
MHH@HuwaidiMH·
@PythonPr None. All the outputs are missing a single quote.
English
1
0
4
162
Kamal Gurjar
Kamal Gurjar@KamalGurjar8·
@PythonPr Answer: A → ['a', 'b', 'c'] += on a list uses extend, and a string is iterable. So 'c' is added as a single element, not nested or concatenated as a string.
English
0
0
2
519
Omo Yewa
Omo Yewa@AceKelm·
@PythonPr The answer is A. ["a", "b", "c"]. The trick here is on the second line x += 'c'. Which further means x = x + 'c'. Our x was already defined to be a list. So this second line is just saying add 'c' to variable x. Hence the answer.
English
0
0
2
528
Sergei Kotov
Sergei Kotov@kotov_dev·
Answer: A/C/D) ['a', 'b', 'c'] (all identical!) How += works with lists: › x += 'c' iterates over 'c' (a string) › Strings are iterable, so it takes each char › 'c' has one char so it appends 'c' to list › Result: ['a', 'b', 'c'] For beginners: x += 'c' equals x.extend('c'). Want nested? Use x.append('c')
English
0
0
1
570
Jay
Jay@learnaiwthme·
@PythonPr += on a list with a string iterates over the string. So 'c' is added as a single character, and the result becomes: ['a', 'b', 'c'] Correct answer: A.
English
0
0
1
336
Siddartha DevOps
Siddartha DevOps@SiddarthaDevops·
@PythonPr There is typo error in your answer ['a', 'b', 'c'] instead of ['a', 'b' , 'c] A) ['a', 'b', 'c'] x is a list of two elements. += operator extends the list with whatever comes on right side. like equivalent to x.extend(....).
English
0
0
1
773
Satheesh Paskanti
Satheesh Paskanti@SatheeshPaskan1·
@PythonPr None of the above is correct.. Correct answer is ['a', 'b', 'c']
English
0
0
1
167
Jim Hamilton
Jim Hamilton@Chaosity8·
@PythonPr A, B, C, and D all have syntax errors ('c instead of 'c'). None of them can be the right answer.
English
0
0
0
461
フリーザンス
フリーザンス@otakunoojisan12·
@PythonPr cにシングルクオートないからこの中に答えはないな
日本語
0
0
0
65
laso
laso@lasitolas·
@PythonPr A.. the x += is python short for summation or addition at the end over an iteration
English
0
0
0
55
SY/TAN
SY/TAN@MUTHHJI·
@PythonPr A x += 'c' with lists = extending the list with string elements
English
0
0
0
110
شیئر