write python code to display all numbers in range 50 to 60 using a for loop . exclude 50 and 65 in output
Answers
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