Programa de Python para imprimir todos los números impares en un rango

Dados los puntos inicial y final, escriba un programa Python para imprimir todos los números impares en ese rango dado. 

Ejemplo:

Python3

# Python program to print odd Numbers in given range
 
start, end = 4, 19
 
# iterating each number in list
for num in range(start, end + 1):
     
    # checking condition
    if num % 2 != 0:
        print(num, end = " ")

Python3

# Python program to print Even Numbers in given range
 
start = int(input(& quot
                   Enter the start of range: & quot
                   ))
end = int(input(& quot
                 Enter the end of range: & quot
                 ))
 
# iterating each number in list
for num in range(start, end + 1):
 
    # checking condition
    if num % 2 != 0:
        print(num, end=& quot
              & quot
              )

Python3

# Uncomment the below two lines for taking the User Inputs
#start = int(input("Enter the start of range: "))
#end = int(input("Enter the end of range: "))
 
# Range declaration
start = 5
end = 20
 
if start % 2 != 0:
 
    for num in range(start, end + 1, 2):
        print(num, end=" ")
else:
 
    for num in range(start+1, end + 1, 2):
        print(num, end=" ")

Python3

# Python program to print Even Numbers in given range
 
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
 
#create a list that contains only Even numbers in given range
even_list = range(start, end + 1)[start%2::2]
 
for num in even_list:
    print(num, end = " ")

Python3

# Python code To print all odd numbers
# in a given range using the lambda function
a=3;b=11
li=[]
for i in range(a,b+1):
    li.append(i)
# printing odd numbers using the lambda function
odd_num = list(filter(lambda x: (x%2!=0),li)) 
print(odd_num)

Publicación traducida automáticamente

Artículo escrito por Shivam_k y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *