146k views
5 votes
Write a for loop that uses a variable named i as well as the range and print function to efficiently display only the odd integers from 3 to 9

1 Answer

3 votes

Final answer:

To display only the odd integers from 3 to 9 using a for loop and the range and print function, use a for loop with the range function and a conditional statement to check if each number is odd. If the number is odd, print it.

Step-by-step explanation:

To display only the odd integers from 3 to 9 using a for loop and the range and print function, you can set the starting value of the range to 3 and the ending value to 10 (exclusive). Then, iterate over the range and check if each number is odd using the modulo operator (%). If the number is odd, print it.



for i in range(3, 10):
if i % 2 != 0:
print(i)



In this example, the variable i is used as the loop iterator. The range(3, 10) creates a sequence of numbers from 3 to 9. The condition i % 2 != 0 checks if the number is not divisible by 2, meaning it is odd. If the condition is true, the number is printed.

User Binh Tran
by
7.1k points