usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

How to Generate Odd Numbers in Python for Kids



What are Odd Numbers? | Maths Explanation for Python Kids

Odd numbers are numbers that are not divisible by 2.

They include:

1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, ...

In this beginner-friendly Maths Python tutorial for kids, we'll explore how to list odd numbers using Python loops and conditions - perfect for primary school students learning to code.
To generate odd numbers in Python, we use a 'while' loop combined with a conditional statement. This simple Python exercise will help you understand number patterns and logic.

Create a new Python module file; File, New File.
Call it oddNumbers.py.
Type out the adjoining Python algorithm that lists odd numbers.



Note: You can comment out the evenNumbers Python object code in the main class from the previous lesson or simply continue from where it stopped.


Code for Odd Number List with User Input in Python

For a little more flexibility, let's add an input mechanism to our Python code for odd numbers.

All we need is a way to ask the user for input to limit the range of odd numbers.
For this purpose, we'll use the input() function in Python.


So! Python Fun Practice Exercise - List Odd Numbers

As a fun practice exercise, feel free to try out your own boundary values, and see how the Python code lists the odd numbers between those boundary values.







Python Code for Odd Numbers - Module File.

# define a class
class OddNumerals:

    ###
     # Our constructor.
     # @param alpha - for the start value
     # @param omega - for the end value
     ##
    def __init__(self, alpha, omega):
        self.start = alpha
        self.stop = omega
        self.result = [] # list to hold our answers


    # Returns an list of the desired set of odd numbers
    def prepResult(self):
        # Loop from start to stop and rip out odd numbers;
        self.i = 0
        while self.start <= self.stop:
            if self.start % 2 != 0:
                self.result.append(self.start)
                self.i += 1
                
            self.start = self.start + 1 # increase start by 1
            
        return self.result

Python Code for Odd Numbers - Main class.

#!/usr/bin/python
from OddNumbers import OddNumerals

# Use the even number module/class
lower_boundary = 1
upper_boundary = 100
odd_list = OddNumerals(lower_boundary, upper_boundary)
answer = odd_list.prepResult()
print("Even numbers between", lower_boundary, "and", upper_boundary, "are:\n", answer)


print("\n\n")

Python Code for Odd Numbers - Main class: Collecting Input.

from OddNumbers import OddNumerals

# Collect Input
print("\n\nEnter the range for your odd numbers.\n")

lower_boundary = int(input("Enter Start Number: "))
upper_boundary = int(input("Enter Stop Number: "))

odd_list = OddNumerals(lower_boundary, upper_boundary)
answer = odd_list.prepResult()
print("Even numbers between", lower_boundary, "and", upper_boundary, "are:\n", answer)


print("\n\n");



<< PreviousNext >>