02 Odd or Even
02 Odd or Even
PRACTICE PYTHON
Beginner Python exercises Follow @practice_python
Home
Why Practice Python?
Why Chilis?
Resources for learners
Exercises
Blog
About
05 FEBRUARY 2014
Odd Or Even
input
if
types
int
equality
comparison
numbers
mod
Again, the exercise comes first (with a few extras if you want the extra challenge or want to spend
more time), followed by a discussion. Enjoy!
Extras:
Discussion
Concepts for this week:
>>> 5 % 3
>>> 6 % 3
https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 1/14
9/22/21, 11:53 PM 02 Odd Or Even
>>> 7 % 3
Conditionals
When a computer (or a program) needs to decide something, it checks whether some condition is
satisfied, which is where the term conditional comes from. Conditionals are a fancy way of saying “if
statements”. If Michele was born in New York, she has an American passport. That statement is a
conditional (if statement) that in this case is true. In Python this works the same way:
else:
When the program gets to the if statement, it will check the value of the variable called age against all
of the conditions, in order, and will print something to the screen accordingly. Note that elif is a
portmanteau of “else” and “if”. So if the variable age holds the value 15, the statement "can see a rated
PG-13 movie" will be printed to the screen.
Note how the statement elif age < 17 and age > 12 has the statement and - you can use or and not in
the same way. Understanding a bit about logic and how it works, or being able to rationally think
about logic will help you get the conditions right - oh, and a lot of practice.
if a == 3:
elif a != 3:
https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 2/14
9/22/21, 11:53 PM 02 Odd Or Even
Notice how in this example, the condition is redundant. In the first condition we are checking whether
the variable a has the value 3 and in the second, we are checking whether a does NOT have the value
3. However, if the first condition is not true (a is in fact not 3), then the second condition is by
definition true. So a more efficient way to write the above conditional is like this:
if a == 3:
else:
Happy coding!
Forgot how to submit exercises?
https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 3/14
9/22/21, 11:53 PM 02 Odd Or Even
ALSO ON PRACTICEPYTHON
https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 4/14
9/22/21, 11:53 PM 02 Odd Or Even
Sponsored
Beirut: Online Jobs in the USA May Pay More Than You Think
Jobs in the USA | Search Ads
Tiger Wood's Ex-wife Turns 40, Hold Your Heart Before You See Her
WTFacts
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
if(number % 2 == 0):
if(number % 4 ==0 ):
else:
print("number is even")
else:
https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 5/14
9/22/21, 11:53 PM 02 Odd Or Even
print("number is odd")
print("")
print("")
else:
elif number%4 == 0:
else:
if number % 4 == 0:
something
if number % 2 == 0:
something
{}".format(number1,number2))
△ ▽ • Reply • Share ›
check it..
code:
if number % 2 == 0:
if number % 4 ==0 :
else:
print("number is even")
else:
print("number is odd")
print("")
print("")
if number1 % number2 == 0:
else:
nType = "odd"
msg = ""
if inp % 4 == 0:
nType = "even"
nType = "even"
if num % check == 0:
else:
R l Sh
https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 7/14
9/22/21, 11:53 PM 02 Odd Or Even
1△ ▽ • Reply • Share ›
https://gist.github.com/vka...
△ ▽ • Reply • Share ›
https://github.com/Webbarrr/
if ( b % d b % )
https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 9/14
9/22/21, 11:53 PM 02 Odd Or Even
if (number % 2 == 0 and number % 4 == 0):
print (output)
elif number % 2 == 0:
print (output)
else:
print (output)
if num % check == 0:
print (output)
else:
like, the
https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 10/14
9/22/21, 11:53 PM 02 Odd Or Even
if num%2==0:
else:
********************************************************
num=int(input("Enter any number: "))
if num%4==0:
else:
********************************************************
num1=int(input("Enter any number: "))
if num1%num2==0:
else:
import time
print("even!")
else:
print("odd!")
△ ▽ • Reply • Share ›
if n1%4 == 0:
elif n1%2 == 0:
else:
if n1%n2 == 0:
else:
Extra 2:
if int(numb) % 2 == 0:
else:
if answer.lower() == 'y':
guessing()
else:
quit()
guessing()
1△ ▽ • Reply • Share ›
print('Type a number')
num = int(input('>>>'))
def div_by_4():
number = num % 4
if number == 0:
def odd():
number = num % 2
if number > 0:
print('Odd')
see more
△ ▽ • Reply • Share ›
def prt1():
if number%2 == 0:
if number%4 ==0:
else:
else:
print('invalid input')
l i i (' ld lik l i
see more
△ ▽ • Reply • Share ›
num1=int(num1)
if num1%2 == 0:
print("This is even")
else:
print("This is odd")
Sponsored
Put a sponge in the refrigerator - the reason will surprise you! What a great trick!
5 Min Tricks
Finding a Job in the USA from Lebanon Might be Easier Than You Think
Job USA | Search Ads
https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 14/14