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
60
9
185
60.2K
Sergei Kotov
Sergei Kotov@kotov_dev·
@PythonPr Answer: B) [1, 2, 3, 1, 2, 3] List multiplication repeats the entire list! › [1, 2, 3] * 2 creates [1, 2, 3, 1, 2, 3] › Like string repetition: "ab" * 2 = "abab" › Not element-wise multiplication For beginners: Want [2, 4, 6]? Use [x*2 for x in a] for element-wise math!
English
0
0
15
1.7K
Developer Help Zone
Developer Help Zone@DevHelpZone·
@PythonPr In Python, the list * operator repeats the list instead of multiplying the elements. For example, when you type [1, 2, 3] * 2, you get [1, 2, 3, 1, 2, 3] as a result. So, the list is kind of copied.
English
0
0
10
5.2K
Aliu Ogundare (DHAN)
Aliu Ogundare (DHAN)@AliuOgunda44574·
@PythonPr In Python, multiplying a list by a number (e.g., a * 2) repeats the list that many times. So, the list a = [1, 2, 3] when multiplied by 2 becomes [1, 2, 3, 1, 2, 3]. The elements are repeated, not doubled in value. So the answer is B
English
0
0
7
3K
へーーお
へーーお@Techforx·
@PythonPr In python list items multiplication repeats the list instead of multiplying individual elements
English
0
0
2
1.9K
Othieno Michael Edwards
Othieno Michael Edwards@EdwardsOthieno·
@PythonPr 1. The * operator in python when dealing with list data types, it replicates the elements inset n times provided. It doesn't perform element-wise multiplication as in other languages 2. a*2 concatenates the list object [1,2,3] itself hence a new list [1,2,3,1,2,3] in memory
English
0
0
2
615
nekohei
nekohei@nekoheics·
@PythonPr a = [1,2,3] print([x * 2 for x in a]) # output: [2,4,6]
English
0
0
1
313
Amit | SQL • JS • Python
Amit | SQL • JS • Python@AmitKumarDev_·
@PythonPr B — [1, 2, 3, 1, 2, 3] Because list multiplication repeats the list, it doesn’t multiply individual elements.
English
0
0
1
2.2K
Ehshanulla
Ehshanulla@Ehshanulla·
@PythonPr a*2 does not multiply 2 elements it repeats the list 2 times output= [1, 2, 3, 1, 2, 3]
English
0
0
1
1.1K
laso
laso@lasitolas·
@PythonPr B..multiply the list /duplicate
English
0
0
1
1.8K
شیئر