Question: The following program creates a list of odd integers, whose range depends on user's choice: print( This program creates a list of odd numbers in
The following program creates a list of odd integers, whose range depends on user's choice:
print("This program creates a list of odd numbers in the range of your choice.") start_num = int(input("Enter starting number: ")) end_num = int(input("Enter ending number: ")) my_list = [] for i in range(start_num, end_num+1): if i % 2 == 1: my_list.append(i) print("odd numbers in the range:", my_list)
The program above creates an empty list first and then uses a loop to append odd integers to the list. We can shorten the program by using generator expression. Use one single statement to generate the whole list of odd integers. You must use generator expression in that statement. No loop is allowed in the program.
The following is sample test run.
This program creates a list of odd numbers in the range of your choice.
Enter starting number: 11
Enter ending number: 21
Odd numbers in the range: [11, 13, 15, 17, 19, 21]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
