Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we're going to learn how to check whether a number is even or odd using a simple Python program. First, let's understand what the modulo operator does. Can anyone tell me what the modulo operator is?
Isn't the modulo operator used to find the remainder of a division?
Exactly, Student_1! For example, when we divide 5 by 2, the remainder is 1. So, we can use % to check if a number is even or odd. If num % 2 equals 0, the number is even. Otherwise, it's odd. Can someone provide an example of what that looks like in Python?
Could you use 4 as an example? If we do 4 % 2, it equals 0, so it's even!
Great illustration, Student_2! It’s a simple but powerful way to check conditions in programming.
Now, let’s write our program together. First, we need to get the user's input. What function do we use to take input in Python?
We can use the input() function!
That's correct, Student_3! Let's write the first line: num = int(input('Enter a number: ')). This will store the input as an integer in the variable num. What should we do next?
Now we need to add the if condition to check if this number is even or odd.
Right, Student_4! So we write: if num % 2 == 0: followed by the code to print 'Even'. In the else part, we'll print 'Odd'.
So the complete program prints whether the entered number is even or odd?
Exactly! Let’s summarize what we learned: We used input to capture a number, applied the modulo operator to check evenness, and created an if-else structure for decision-making.
Now that we’ve written our program, let’s test it out! What types of numbers should we test?
We should try both even and odd numbers to see if the program works correctly!
Good thinking! If we test with 8, what do you expect the output to be?
The output should say 'Even'!
Correct! And if we input 7, what should happen?
It should say 'Odd'!
Nice job! Testing different inputs helps us ensure our program functions as expected. Remember, always test extensively!
The program begins by requesting input from the user.
It uses an if-else construct to make decisions based on the remainder of the number when divided by 2.
Finally, it provides output to the user indicating whether the given number is even or odd.
This practical example helps reinforce fundamental programming concepts such as variables, control structures, and input/output operations.
To find if a number's neat (even) or odd, divide by two with a nod!
Once there was a number named 'Seven'. It wanted to know if it was cool. So, it went to a wise old teacher named Python, who said, 'If you divide me by 2 and the remainder's not seen, you’re even like a queen!'
E.O. (Even - Odd) can help you remember: Even means 0 remainder, Odd means 1 remainder.
EOP (Even or Odd Program): E for Even, O for Odd, P for Program helps us recall how to check.
Term
What is the output if 3 is input?
Click to flip
Term
How do you check if a number is even in Python?
Click to flip
An integer that is divisible by 2 with no remainder.
An integer that is not divisible by 2 without a remainder.
A mathematical operator that returns the remainder of a division operation.
A programming construct that allows execution of certain code based on whether a condition evaluates to true or false.
Data that is received by a program from the user.
The data that is sent from a program to the user.
This section provides a practical example of a simple program in Python that checks if a number is even or odd.
The section illustrates how to use the modulo operator to determine whether an input number is even or odd through a straightforward Python program, emphasizing the use of conditional statements for decision-making in programming.
In this section, we explore an example of a simple program written in Python that checks whether a given number is even or odd. The program utilizes the modulo operator (%), which gives the remainder of a division. When a number is divided by 2, if the remainder is 0, the number is classified as even; otherwise, it's odd.
if-else construct to make decisions based on the remainder of the number when divided by 2.This practical example helps reinforce fundamental programming concepts such as variables, control structures, and input/output operations.