[Solved] Why is the output of the following code [4,3]? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 4

[Solved] Why is the output of the following code [4,3]?

a, b, *c, d = [1,4,3,4,3] print(c[::-1])

29th Sep 2020, 3:07 AM
Rahul Hemdev
Rahul Hemdev - avatar
2 Respuestas
+ 6
AFAIK: This code assigns following values to variables: a = 1 b = 4 c = [3, 4] d = 3 There are two numbers in "c" because it has a "*" symbol, so it receives everything that remained unused. And then we print c[::-1]. [::-1] is a list slice. It accepts three parameters: from, to and step. Here from and to are omitted, so the whole list is used. And step is -1, so it will print all elements from last to first. Output: [4, 3]
29th Sep 2020, 4:53 AM
Artem 🇺🇦
Artem 🇺🇦 - avatar
+ 4
Artem Really great explanation..... Thank you. My main doubt was in '*c' part, which is now clear. Thanks again!
29th Sep 2020, 6:16 AM
Rahul Hemdev
Rahul Hemdev - avatar