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
2 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:
| Element | Positive Index | Negative Index |
|---|---|---|
| "India" | 0 | -6 |
| "Australia" | 1 | -5 |
| "USA" | 2 | -4 |
| "China" | 3 | -3 |
| "Russia" | 4 | -2 |
| "Ukraine" | 5 | -1 |
Answered By
1 Like
Related Questions
Differentiate between Cloud Computing and Grid Computing with suitable examples.
Write a Python program to calculate and display the square and cube of an inputted number.
Define Relation between IOT and WOT?
Create a list containing 10 marks the scored by the students—
(i) Write a command to insert the marks of the student at index position 4.
(ii) Write a command to know the length of the list.