Even Numbers Explained with Python
Even numbers are an important concept in mathematics, and they're also a great way to introduce coding to beginners. In this tutorial, we'll show you how to list even numbers in Python using simple loops and conditional statements. This activity is designed for primary students, teachers, and parents who want to combine math learning with fun programming exercises.
What Are Even Numbers? | Maths Explanation for Python Kids
Hello pupils, what are even numbers?
Even numbers are integers that can be divided by 2 without leaving a remainder. Examples include 2, 4, 6, 8, and 10. Understanding even numbers helps students build a strong foundation in math and prepares them for more advanced coding challenges.
How to List Even Numbers in Python
Considering how simple and straight-forward even numbers are, writing a Python code for even numbers serves as a great way to introduce our Mathematics educational activities for young learners.
Let's write a simple Python program that prints even numbers between 1 and 100. Bear in mind, we use only a loop and a simple conditional statement for the Python code. This is a perfect math coding activity for kids and beginners.
Creating Python Files
Create a new Python Class File;
call it Arithmetic.py.
Create a new Python Module File;
call it evenNumbers.py.
Type out the adjoining Python code that lists even numbers.
How to run Python Codes
Run the code from the cmd shell with the command:
cd ~/Documents/usingMaths -- to navigate to the folder where
you saved your files;
python Arithmetic.pl
How the Python Even Numbers Code Works
In this code, we use a Python loop to go through numbers from 1 to 100.
The conditional statement i % 2 === 0 checks if a number is divisible by 2. If it is, the number is printed as an even number.
- The variable `start` starts at 2, which is the first even number.
- The `while` loop continues running as long as the number is less than or equal to 100.
- Each even number is displayed on the screen.
This Python even number loop is suitable for primary school students and beginners learning to code.
Why Learn Even Numbers with Python?
Learning even numbers with Python helps students connect maths with coding. Instead of writing numbers by hand, students can use programming to generate number patterns quickly and accurately.
This primary school Python maths lesson:
- Reinforces understanding of even numbers
- Introduces basic programming logic
- Builds confidence in using Python
- Encourages problem-solving and logical thinking
Summary: Even Numbers and Python Programming for Beginners
Learning how to list even numbers in Python is a simple yet powerful way to combine math and coding. Whether you're a teacher, parent, or student, these math coding activities make programming fun and educational.
Using Python to list even numbers is a great way to introduce young learners to programming. It shows how computers follow instructions step by step and how maths concepts can be applied in real coding examples.
This lesson forms part of a wider approach to teaching maths and coding for primary learners through practical, hands-on examples.
So! Python Fun Practice Exercise - List Even Numbers
Teachers can use this Python even numbers tutorial to make math lessons more engaging. Students not only learn about even numbers but also practice basic programming skills.
As a fun practice exercise, try extending the activity by asking students to try out their own boundary values, and see how the Python code lists the even numbers between those boundary values.
Python Code for Even Numbers - Module File.
# define a class
class EvenNumerals:
###
# 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 even numbers
def prepResult(self):
# Loop from start to stop and rip out even numbers;
self.i = 0
while self.start <= self.stop:
if self.start % 2 == 0: # modulo(%) is explained later
self.result.append(self.start)
self.i += 1
self.start = self.start + 1 # increase start by 1
return self.result
Python Code for Even Numbers - Main Class.
from EvenNumbers import EvenNumerals
# Use the even number module/class
lower_boundary = 1
upper_boundary = 100
even_list = EvenNumerals(lower_boundary, upper_boundary)
answer = even_list.prepResult()
print("Even numbers between", lower_boundary, "and", upper_boundary, "are:\n", answer)
print("\n\n")