KnowledgeBoat Logo
|

Informatics Practices

What will be the output of the following code?

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

Python List Manipulation

3 Likes

Answer

['USA', 'China', 'Russia']
['Australia', 'USA', 'China']

Working

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

Answered By

3 Likes


Related Questions