You are on page 1of 5

CCF AVG

Name: ____________________________________ Course and Year: ___________________________ Date: ________

Exercise No. 2

MODULAR ARITHMETIC, CONDITIONALS, CHECKING EQUALITY

OBJECTIVES (at the end of the activity the students will be able to)

1. Use modulus operator in python to determine whether the number is even or odd.
2. Create a program that will prompt the user to input a number an print out an appropriate message to the user.
3. Apply conditional statements in the python programing.
4. Check for equality or inequality of the equation.

EQUIPMENT

Quantity
1 PC with Wing IDE or Anaconda or any open source IDE platform for Python
1 PC per student

DISCUSION

MODULAR ARITHMETIC (THE MODULUS OPERATOR)

We have been doing arithmetic (addition, subtraction, multiplication, division) since elementary school, and often it is useful for
us to find not the answer to a division problem but the remainder when we do a division operation. This operation is called the “modulus
operation.” For example, when I divide 5 by 3, the remainder is 2, and the sentence reads like this: “5 modulo 3 is 2.”

In the Python shell:

>>> 5 % 3
2
>>> 6 % 3
0
>>> 7 % 3
1
The % sign is exactly the modulus operator.

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:

Write this code in your WING IDE editor and run.

if age > 17:


print("can see a rated R movie")
elif age < 17 and age > 12:
print("can see a rated PG-13 movie")
else:
print("can only see rated PG movies")

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.

Laboratory Experiment Page 6


CCF AVG

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.

Conditional operators, also known as relational operators, are used to compare the relationship between two operands.
Expressions that can only result in one of two answers are known as Boolean expression.

Programming Structures

CHECKING FOR EQUALITY (AND COMPARATORS IN GENERAL)

A fundamental thing you want to do with your program is check whether some number is equal to another. Say the user tells you
how many questions they answered incorrectly on a practice exam, and depending on the number of correctly-answered questions, you
can suggest a specific course of action. For integers, strings, floats, and many other variable types, this is done with a simple syntax: ==.
To explicitly check inequality, use !=.

if a == 3:
print("the variable has the value 3")
elif a != 3:
print("the variable does not have the value 3")

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:

Laboratory Experiment Page 7


CCF AVG

if a == 3:
print("the variable has the value 3")
else:
print("the variable does not have the value 3")

The same equality / inequality comparisons work for strings.

QUESTIONS

1. What symbol that is use for modulus operator? Explain in your own words what is modulus operator?

2. Explain conditional statement?

3. Can you site some application that you can use conditional statement in programming?

4. Explain how the conditional operators are used when the operands are strings.

5. Execute the two following python programs by copying and pasting into your editor window of WING IDE.

5.a What was the output for each program when examScore variable is set to 85?

5.b Change the examScore values to 95 in each program and run again. What is the output for each program now?

5.c What can you say about the importance of the indentation in a python program using an if statement in Python?

Laboratory Experiment Page 8


CCF AVG

6. Enter and execute the Python program below.

a. Test the program at least three times. List the three test data points you used and the corresponding output. Be
sure you test each part of the condition. Explain why the data you chose were the best data to use to thoroughly
test for the program.

b. Now add print statements to the Python program above so that it prints "It’s turning to steam!" when the water is
100 degrees or hotter, and says "Done!" when the program finishes regardless of what temperature the water is.
Rewrite the if/else below with this statement included.

7. Sometimes you want to include more than one condition to determine which code segment is executed. You can use the
following logical operators to create compound conditions. Examine each operator and a sample of its use and provide an
explanation of how the operator works.

TEST YOUR SKILLS


Create a program that will ask the user input a number. Depending on whether the number is even or odd, print out an
appropriate message to the user. Hint: how does an even / odd number react differently when divided by 2?
Extras:
If the number is a multiple of 4, print out a different message.
Ask the user for two numbers: one number to check (call it num) and one number to divide by (check). If check divides evenly
into num, tell that to the user. If not, print a different appropriate message.

Individual Homework Activity:


Write a Python program that prompts the user for the cost of two items to be purchased. Then prompt the user for their payment.
If they enter an amount that is less than the total cost of the two items, print a message that tells them how much they still owe. Otherwise,
print a message that thanks them for their payment and tells them how much change they will receive. Thoroughly test your code for all
possible input.

Laboratory Experiment Page 9


CCF AVG

Laboratory Experiment Page 10

You might also like