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
33
5
136
22.7K
Jenny
Jenny@JennyTheDev·
@PythonPr A) [2, 3] nums[-3:-1] starts at index -3 (which is 2) and stops before -1 (which is 4). Negative slicing: confusing everyone since Python 1.0
English
0
0
9
702
Jay
Jay@learnaiwthme·
@PythonPr Answer: A) [2, 3] Why (short): -3 starts at index 1 → value 2 -1 stops before the last element Slice includes 2 and 3 only
English
1
0
6
2K
Kamal Gurjar
Kamal Gurjar@KamalGurjar8·
@PythonPr Output A [2, 3] -3 starts at value 2, and -1 is the stop index (exclusive), so it stops before the last element (4)
English
0
0
5
1.6K
Mr. B
Mr. B@bodelizm·
The "Only 1% Get This Right" claim is finally debunked. 🐍 The answer is A [2, 3]. Here’s why most people fail: 1️⃣ Negative indices count from the right (-3 is the value 2). 2️⃣ Python slices are exclusive of the stop index. It stops before -1. Master your fundamentals and you're already in the top 1%. 🚀 #Python #Coding #ProgrammingTips
Mr. B tweet media
English
0
0
3
454
clovis
clovis@cloclodma·
@PythonPr The answer is A : [2, 3] nums[-3:-1] means: -3 : index of value 2 -1 : stop before the last element (4) Python slicing is start inclusive, end exclusive
English
0
0
3
525
The root of infinity
The root of infinity@parth_hirpara05·
Ans---- (a) The code uses list slicing with negative indices in Python. In Python, negative indices count from the end of the list. nums[-3] refers to the third element from the end, which is 2. nums[-1] refers to the last element, which is 4. The slice nums[-3:-1] includes elements starting from the index of nums[-3] up to, but not including, the index of nums[-1]. Therefore, the slice includes nums[-3] and nums[-2] (which is 3), resulting in the list [2, 3].
English
0
0
2
367
Sergei Kotov
Sergei Kotov@kotov_dev·
Answer: A) [2, 3] Negative indices count from the end: 1 2 3 4 -4 -3 -2 -1 nums[-3:-1]: › Start: -3 = 2 › Stop: -1 = 4 (exclusive!) › Takes: -3(2), -2(3) › Result: [2, 3] For beginners: Stop index is always exclusive. -1 is the last item but not included! To include the last element omit the index entirely nums[-3:]
English
0
0
2
759
Python Tech
Python Tech@PythonTech43716·
@PythonPr A) [2,3] is the right answer Here we use concept of list slicing and negative indexing concept [start: end: stepsize].
English
0
0
2
550
Ranjan
Ranjan@Ranjanrgdev·
@PythonPr A. [2, 3] 4(-1), 3(-2), 2(-3), 1(-4) So, -3 is 2 (start) -1 is 4 but ending index will not be included
English
0
0
2
1.1K
JA ŘÕ
JA ŘÕ@JaroMohamed·
@PythonPr A) [2,3] the function trim item 0 and item 3 from array
English
0
0
0
24
Terrence
Terrence@terrenceeleven·
@PythonPr A [ 2 , 3 ] Indexing from -3(4) to -1(2)
English
0
0
0
73
Droid | Code
Droid | Code@DriodXL·
@PythonPr [2, 3] Negative slicing: start = -3 →mean it need to count from backwards [4,3,2,1] when to count 3,it ends at [2], same with -1, count backwards, [3] =[2,3]
English
0
0
0
40
Israel
Israel@IOjehonmon31052·
@PythonPr A this is just like basic maths
English
0
0
0
40
Tanmay Newatia
Tanmay Newatia@thetanmaydoes·
@PythonPr A: [2, 3] explaination: nums=[1,2,3,4] negativeIndex=[-4,-3,-2,-1] nums[-3:-1] logically becomes: nums[-3:-2] hence the answer!
English
0
0
2
32
Поделиться