+ 1

Python - Multiplying an array for - 1

This empties array. Why? i=[1,2,3,4]*(-1) print(i)

13th Sep 2019, 7:48 AM
Paolo De Nictolis
Paolo De Nictolis - avatar
2 Réponses
+ 6
When you multiply an integer n by a sequence python documentation says: "Values of n less than 0 are treated as 0" Hence you'll get an empty sequence of the same type. In your case an empty array.
13th Sep 2019, 8:07 AM
Amr Saeed
Amr Saeed - avatar
+ 4
My guess would be that there's a condition in the loop that looks like: if multiplicator > 0: .... else: return [] [1]*2 -> [1, 1] [1]*1 -> [1] [1]*0 -> [] [1]*-1 -> ... still []
13th Sep 2019, 8:08 AM
HonFu
HonFu - avatar