write the Python coding to print all the odd numbers from 10 to 50 using for loop
Answers
Odd Numbers in a Range - Python
We are given a lower limit (10) and an upper limit (50). We need the Odd Numbers in this Range.
We will use the range(start, stop, step) function. We can use the range() function in three ways:
- range(n) will give all integers from 0 to n (n exclusive)
- range(m, n) will give all integers from m to n (m inclusive, n exclusive)
- range(m, n, s) will give all integers from m to n (m inclusive, n exclusive) with a step size of s
Here, we run a loop simply with range(10, 50). We use the for loop over this range.
Next, we need to check if a number is odd or not. A number is odd if it is not divisible by 2. In other words, dividing an odd number by 2 will not yield a remainder 0.
So, a number i is odd if i%2 != 0
This becomes the condition to check for odd numbers. We print all such numbers.
m = 10 # Lower Limit
n = 50 # Upper Limit
print(f"Odd numbers from {m} to {n} are:")
# We will now run a loop from m to n
# We use the for loop with the range() function
for i in range(m,n):
if(i%2!=0): # Condition for odd numbers
print(i) # Printing Odd Numbers
from for loop
for i in range ( 11, 50 , 2 ) :
______ print ( i )
working of range :-
- ( start, stop, step )
- start value = 11
- stop = 50
- step = 2
Program way of execution
It will start from 11 and end at 49 less one from stop index and take step of 2 one by one value of i will change first it will come 11 and it will print 11 then comes 13 in i and it will print 13 and than so on.
programme will run till the value 49 because after that, the value will be 51 which is not in range
note :- I am using --> ( _____ ) these underscore line imagine it invisible