Write a Python program: list1= ["pythoon", [10,20,40,50,30], 3.4, "Java"] i. To display the elements 10 and 50 from list1
Answers
Answered by
4
In the program above, we use a for loop to traverse through the elements in the list. We use list1[1] since the elements 10 and 50 are in a nested list and are in the 1st index position in list1. We use a conditional statement to check the equality of the traversing variable and 10 and 50. If they're equivalent to 10 or 50, they get printed.
We could also simply use statement(s) to print the elements.
10
50
Elements can be accessed using index values. Index values start from 0. In list1,
- "Python" = 0
- [10, 20, 40, 50, 30] = 1
- 3.4 = 2
- "Java" = 3
In the nested list [10, 20, 40, 50, 30],
- 10 = 0
- 20 = 1
- 40 = 2
- 50 = 3
- 30 = 4
Nested lists are lists present inside another existing list.
Similar questions