Now the goal of this exercise is
to write a program using what you've learned about if
and else conditional statements to figure out
whether if an input number is odd or even.
So what differentiates odd and even numbers?
Well, even numbers, by definition, can be divided
by two with no remainder.
That means that the final result has no decimal places.
It's a whole number and the division is clean.
Whereas if you took an odd number,
like three, and divided it by two,
the result is 1.5, and it does have a decimal place.
It's not a whole number.
In Python and in many other programming languages,
there's a way to check
if a division occurs with no remainder,
and that is through something called the modulo.
Now, you can use the modulo by writing a percentage sign %
and that will give you the remainder after a division.
For example, if you divide six by two, that equals three
with no remainder.
So if 6 % 2, the result would be 0.
But if you try to divide five by two, then two can go
into five two times, but there is one remainder.
So five modulo two is equal to one (5 % 2 = 1).
You can take a look in the description pane to
see this written out and understand how the modulo works
and you can get more details on the modulo by Googling
for it as well.
In our case, we want our code to be able to check the input
number and it's ready coming in, and we're converting it
into a whole number.
And we're going to check to see if it's odd, or even.
If it's odd, we're going to print into the output,
"This is an odd number."
And if it's even, we're going to print,
"This is an even number."
That is the task at hand, and now it's for you
to start struggling and trying to figure out
how to solve this code exercise.
The first thing we're going to check using an
if statement is if the value that's stored
in our variable number is divisible by two
and has no remainder, and we're going to do this using
the modulo, so number % 2, and then we
use the double equal (==) sign to check to see if it equals 0.
And this is equivalent to saying if number divided
by 2 has no decimal places, well then in that case
it must be an even number.
So that's why on the next line, Line 6,
we have an indented print statement.
This print statement is going to be carried
out if the Line 5 condition is true.
If the input was indeed an even number,
say it was Number 4, 4/ 2,
2 goes into 4 twice, and there is no remainder.
So therefore
the modulo operator gives us a result of 0.
So if that condition is true, it's going to fall
into the indented print statement and printout,
"This is an even number."
Alternatively, or in this case, in the else condition,
so if Line 5 condition was false, it did not work
and there was a remainder, then we're going to print out,
"This is an odd number."
This is a way that we can manipulate numbers
and check them and use what we learned
about conditionals to check if a condition is true
and behave in a certain way, or if that condition was false,
then our program should behave in a different way.
If you need to make any changes to your code
feel free to go back
to the previous slide and make those changes.
Don't feel like you've failed
or you're cheating or whatever.
This is all a part of the learning process.
It's okay to look
at the solution and learn from the solution too.
You don't have to figure everything out the first time.