Computer Science, asked by FangirlF4F, 3 months ago

Create a program in python to print the odd elements of the given list using the "for" loop: List=[1,2,6,9,15,18]

Answers

Answered by Equestriadash
73

We have List = [1, 2, 6, 9, 15, 18].

List = [1, 2, 6, 9, 15, 18]

print(List, "is the list.")

print()

print("The odd elements are: ")

for i in List:

   if i%2 != 0:

       print(i)

A for-loop is an iteration statement used to perform repeated checking.

Here, we use 'i' as the changing variable and the given List as the range. 'i' traverses through all the elements in the list, taking each value, and testing the proceeding statements, i.e., testing if its an odd number, and printing it. Once it has traversed through all the elements, the loop stops.


Equestriadash: Thanks for the Brainliest! ^_^"
Answered by anindyaadhikari13
63

Required Answer:-

Question:

  • Create a program in Python to print the odd elements of the given list using for loop.
  • List =[1,2,6,9,15,18]

Solution:

Here comes the Python program.

List =[1,2,6,9,15,18]

print("Given List: ", List)

print("Odd elements of the list are as follows:")

for i in List:

   if i%2==1:

       print(i, end=" ")

One liner c∅de:

print("Odd numbers are:"," ".join(str(i) for i in [1,2,6,9,15,18] if i%2==1))

Explanation:

  • Line 1: Initialises the list.
  • Line 2: Displays the list.
  • Line 3: Print the message that - "Odd elements of the list are as follows:  "
  • Line 4: Iterate the loop through the list.
  • Line 5: Check if the list item is even or odd.
  • Line 6: If true, displays the list item.

Refer to the attachment.

•••♪

Attachments:
Similar questions