How do you represent odd numbers in python?

A number is even if it is perfectly divisible by 2. When the number is divided by 2, we use the remainder operator % to compute the remainder. If the remainder is not zero, the number is odd.

Source Code

# Python program to check if the input number is odd or even.
# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.

num = int(input("Enter a number: "))
if (num % 2) == 0:
   print("{0} is Even".format(num))
else:
   print("{0} is Odd".format(num))

Output 1

Enter a number: 43
43 is Odd

Output 2

Enter a number: 18
18 is Even

In this program, we ask the user for the input and check if the number is odd or even. Please note that { } is a replacement field for num.

Python Program to Check if a Number is Odd or Even

Odd and Even numbers:

If you divide a number by 2 and it gives a remainder of 0 then it is known as even number, otherwise an odd number.

Even number examples: 2, 4, 6, 8, 10, etc.

Odd number examples:1, 3, 5, 7, 9 etc.

See this example:

Output:

How do you represent odd numbers in python?


How do you represent odd numbers in python?
For Videos Join Our Youtube Channel: Join Now


Feedback

  • Send your Feedback to [email protected]

Help Others, Please Share

How do you represent odd numbers in python?
How do you represent odd numbers in python?
How do you represent odd numbers in python?





View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given starting and endpoints, write a Python program to print all odd numbers in that given range. 

    Example:

    Input: start = 4, end = 15
    Output: 5, 7, 9, 11, 13, 15
    
    Input: start = 3, end = 11
    Output: 3, 5, 7, 9, 11

    Example #1: Print all odd numbers from the given list using for loop 

    1. Define the start and end limit of the range.
    2. Iterate from start till the range in the list using for loop and 
    3. check if num % 2 != 0. 
    4. If the condition satisfies, then only print the number. 

    Python3

    start, end = 4, 19

    for num in range(start, end + 1):

        if num % 2 != 0:

            print(num, end = " ")

    Output:

    5 7 9 11 13 15 17 19 

      Example #2: Taking range limit from user input 

    Python3

    start = int(input("Enter the start of range:"))

    end = int(input("Enter the end of range:"))

    for num in range(start, end + 1):

        if num % 2 != 0:

            print(num)

    Output:

    Enter the start of range: 3
    Enter the end of range: 7
    3
    5
    7

    Example #3: Taking range limit from user input or with static inputs to reduce code execution time and to increase code performance.

    Python3

    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=" ")

    Output

    5 7 9 11 13 15 17 19 

    Example #4: Taking range limit from user input 

    Python3

    start = int(input("Enter the start of range: "))

    end = int(input("Enter the end of range: "))

    even_list = range(start, end + 1)[start%2::2]

    for num in even_list:

        print(num, end = " ")

    Enter the start of range: 3
    Enter the end of range: 11
    3 5 7 9 11 

    Method: Using the lambda function

    Python3

    a=3;b=11

    li=[]

    for i in range(a,b+1):

        li.append(i)

    odd_num = list(filter(lambda x: (x%2!=0),li)) 

    print(odd_num)

    Method: Using recursion 

    Python3

    def odd(num1,num2):

        if num1>num2:

            return

        print(num1+1,end=" ")

        return odd(num1+2,num2)

    num1=4;num2=15

    odd(num1,num2)

    Method: Using list comprehension

    Python3

    x = [i for i in range(4,15+1) if i%2!=0]

    print(*x)

    Method: Using the enumerate function 

    Python3

    a=4;b=15;l=[]

    for i in range(a,b+1):

      l.append(i)

    print([a for j,a in enumerate(l) if a%2!=0])

    Output

    [5, 7, 9, 11, 13, 15]

    Method: Using pass 

    Python3

    a=4;b=15

    for i in range(a,b+1):

      if i%2==0:

        pass

      else:

        print(i,end=" ")

    Method: Using filter method:

    Python3

    a=4;

    b=15;

    l= filter(lambda a : a%2 , range(a, b+1))

    print(*l)

    Output:

    5 7 9 11 13 15

    How do you write even and odd numbers in Python?

    Check Even / Odd without using modulus or bitwise operator:.
    #Even Odd Program using Modulus Operator..
    number=int(input("Please Enter a Number : "));.
    x=int(number/2)*2;.
    if(x==number):.
    print("This Number is Even").
    print("This Number is Odd").

    How do you code an odd number?

    If a number is evenly divisible by 2 with no remainder, then it is even. You can calculate the remainder with the modulo operator % like this num % 2 == 0 . If a number divided by 2 leaves a remainder of 1, then the number is odd. You can check for this using num % 2 == 1 .

    What is odd Python?

    A number is even if it is perfectly divisible by 2. When the number is divided by 2, we use the remainder operator % to compute the remainder. If the remainder is not zero, the number is odd.

    How do you print odd elements in Python?

    Using for loop : Iterate each element in the list using for loop and check if num % 2 != 0. If the condition satisfies, then only print the number.