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







<< PreviousNext >>

Listing Odd Numbers in Python - Fun Coding Tutorial for Kids



Listing Odd Numbers Using Python

This tutorial explains how to list odd numbers using Python in a simple and beginner-friendly way. It is designed for students and learners who are new to programming and want to understand how odd numbers can be generated using basic Python logic.

By the end of this lesson, you will know how to generate odd numbers in Python, use loops effectively, and apply simple conditions to filter numbers.

What Are Odd Numbers? | Maths Explanation for Python Kids

Odd numbers are whole numbers that are not divisible by 2. Examples include 1, 3, 5, 7, and 9. In mathematics, an odd number always leaves a remainder of 1 when divided by 2.

Understanding odd numbers is an important part of primary mathematics, and Python provides a practical way to explore this concept using code.


How to Generate Odd Numbers in Python

To generate odd numbers in Python, we use a loop to go through a range of numbers and a condition to check whether each number is odd.

A number is considered odd if:

number % 2 !== 0

This condition checks whether the remainder after dividing by 2 is not zero.

Python Odd Number Program (Beginner Example)

The following example shows a simple Python odd number program. It lists odd numbers within a given range and displays them on the page.

This type of example is commonly used in Python beginner tutorials and helps students learn both maths concepts and programming basics at the same time.

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



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.

Using a Loop to Display Odd Numbers in Python

A loop allows Python to repeat an action multiple times. In this case, the loop checks each number in a range and displays only the odd ones.

This approach demonstrates:

  • How to use a loop in Python
  • How to apply conditions
  • How to list odd numbers with Python clearly and efficiently

It is an excellent example for learners studying Python maths code for the first time.


We have used a function here.
A function is a chunk of code that is executed when it is called upon, such as when an event occurs.



Why Learn Odd Numbers with Python?

Learning how to work with odd numbers in Python helps students to:

  • Understand number patterns
  • Practice logical thinking
  • Learn basic programming structures
  • Combine primary maths with coding skills

This makes Python a useful tool for teaching and reinforcing mathematical concepts in an interactive way.

Who Is This Python Lesson For?

This Python lesson is suitable for:

  • Primary school students
  • Beginners learning Python
  • Teachers looking for simple coding examples
  • Anyone learning how to generate odd numbers in Python

No prior Python programming experience is required.


Key Takeaways from Listing Odd Numbers Using Python

  • Odd numbers are numbers not divisible by 2
  • Python can be used to generate and display odd numbers
  • Loops and conditions are essential programming tools
  • This tutorial provides a clear Python odd numbers tutorial for beginners

Summary: Listing Odd Numbers Using Python

Learning how to generate odd numbers in Python is a fun and practical way to combine math and coding. Whether you're a teacher, parent, or student, these Python tutorials for kids make programming approachable and engaging.

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 >>