CBSE Class 11 Informatics Practices Question 23 of 40

Practice Paper — Question 5

Back to all questions
5
Question

Question 23

What will be the output of the following code?

Country=["India", "Australia", "USA", "China", "Russia", "Ukraine" ]  
print(Country[2:5])  
print(Country[-5:-2])  
Answer
Output
['USA', 'China', 'Russia']
['Australia', 'USA', 'China']
Explanation

In the given Python code, Country[2:5] returns ["USA", "China", "Russia"], slicing from index 2 up to index 4. Then, Country[-5:-2] retrieves a sublist starting from index -5 up to index -3. This results in ["Australia", "USA", "China"].

The indexing table is:

ElementPositive IndexNegative Index
"India"0-6
"Australia"1-5
"USA"2-4
"China"3-3
"Russia"4-2
"Ukraine"5-1