Computer Science, asked by ragavaragav124, 1 month ago

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 Equestriadash
4

\tt list1\ =\ ["Python",\ [10,\ 20,\ 40,\ 50,\ 30],\ 3.4,\ "Java"]\\for\ i\ in\ list1[1]:\\{\ \ \ \ \ }if\ i\ ==\ 10\ or\ i\ ==\ 50:\\{\ \ \ \ \ }{\ \ \ \ \ }print(i)

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.

>>>\ \tt list1\ =\ ["Python",\ [10,\ 20,\ 40,\ 50,\ 30],\ 3.4,\ "Java"]\\>>>\ print(list1[1][0])

10

>>>\ \tt print(list1[1][3])

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