Download as pdf or txt
Download as pdf or txt
You are on page 1of 74

LAB MODULE

Week 6
Operator in Python: arithmetic,
comparison, and logical operators.
Introduction to Operators

Arithmetic operators
Outline Practice I

Comparison operators
Practice II

Logical operators

Practice III

Submission
Introduction to Operator
What is an In Computer Science, Operator is a
symbol to operate values.
Operator?
For example, just as in arithmetic, the +
(plus) sign is the operator which is able to
add two numbers, giving the result of the
addition.
Operators in Python
Now, we're going to show you a completely new side of the print() function. You already know that the
function is able to show you the values of the literals passed to it by arguments. In fact, it can do
something more. Take a look at the snippet:

Retype the code in the editor and run it. Can you guess the output? You should see the number five. Here,
+is the operator that performs addition. 2 and 3 are the operands and 5 is the output of the operation.

Feel free to experiment with other operators and operands.


Types of Operator in Python
Python divides the operators in the following groups:

● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators
● Identity operators
● Membership operators
● Bitwise operators

In this module, we only focus on Arithmetic, Comparison, and Logical operators.


Python Arithmetic Operators
Arithmetic operator
Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication, etc. There are 7 arithmetic operators in Python:

● Addition ( +)
● Subtraction ( - )
● Multiplication ( *)
● Division ( / )
● Integer division ( // )
● Exponentiation ( **)
● Modulo ( %)
Addition ( +)
The addition operator is the +(plus) sign, which is fully in line with mathematical standards that is to add
two operands.

Remember: It's possible to formulate the following rules based on this result:
● when both **arguments are integers, the result is an integer, too;
● when at least one **argument is a float, the result is a float, too.
This is an important distinction to remember.
Try it!
Try to run the following code, see if the result is as you expected:

What is the datatype of the result of each code?


S ubtraction ( - )
The subtraction operator is an operator used to subtract two operands.

Remember: It's possible to formulate the following rules based on this result:
● when both **arguments are integers, the result is an integer, too;
● when at least one **argument is a float, the result is a float, too. This
is an important distinction to remember.
Try it!
Try to run the following code, see if the result is as you expected:

What is the datatype of the result of each code?


Multiplication ( * )
An *(asterisk) sign is a multiplication operator. The function of this operator is to multiply two operands.

Remember: It's possible to formulate the following rules based on this result:
● when both **arguments are integers, the result is an integer, too;
● when at least one **argument is a float, the result is a float, too.
This is an important distinction to remember.
Try it!
Try to run the following code, see if the result is as you expected:

What is the datatype of the result of each code?


Division ( / )
A / (slash) sign is a divisional operator. The value in front of the slash is a dividend, the value behind the
slash, a divisor.

The result produced by the division operator is always a float, regardless of whether or not the result
seems to be a float at first glance: 1 / 2, or if it looks like a pure integer: 2 / 1.

Is this a problem? Yes, it is. It happens sometimes that you really need a division that provides an integer
value, not a float.

Fortunately, Python can help you with that.


Try it!
Try to run the following code, see if the result is as you expected:

What is the datatype of the result of each code?


Integer division ( / )
A // (double slash) sign is an Integer division or floor division operator. It differs from the standard /
operator in two details:
● its result lacks the fractional part - it's absent (for integers), or is always equal to zero (for floats);
this means that the results are always rounded;
● it conforms to the integer vs. float rule.

The result of integer division is always rounded to the nearest integer value that is less than the real (not
rounded) result.

This is very important: rounding always goes to the lesser integer.


Try it!
Try to run the following code, see if the result is as you expected:

What is the datatype of the result of each code?


Exponentiation ( ** )
A **(double asterisk) sign is an exponentiation (power) operator. Its left argument is the base, its right,
the exponent.

Classical mathematics prefers notation with superscripts, just like this: 23. Pure text editors don't accept
that, so Python uses **instead, e.g., 2 **3.

Remember: It's possible to formulate the following rules based on this result:
● when both **arguments are integers, the result is an integer, too;
● when at least one **argument is a float, the result is a float, too. This
is an important distinction to remember.
Try it!
Try to run the following code, see if the result is as you expected:

What is the datatype of the result of each code?


M odulo ( % )
The result of the operator is a remainder left after the integer division. In other words, it's the value left
over after dividing one value by another to produce an integer quotient.

Remember: The return value of modulo operation is always integer.


Try it!
Try to run the following code, see if the result is as you expected:

What is the datatype of the result of each code?


Example
Question
Create a program that let the user input user birth year. From that input, calculate the user age of the
user (Assume that every user born at 1st January).

For example:
Input: 2000
Output: “Your age: 22”
Steps to solve (Algorithm)
1. Define the current year and store it into a variable.
2. Get the user input and store it into a variable (don’t forget to convert the input to integer).
3. Calculate the age (current year - birth year) and store it into a variable.
4. Display the user age.
Result
Practice I
For student whose last digit of student id is odd

Please recreate the previous program, but the current year is 2018

Take the screenshot of your program (code and terminal) and put it into the docx file!
For student whose last digit of student id is
even

Please recreate the previous program, but the current year is 2031

Take the screenshot of your program (code and terminal) and put it into the docx file!
Python Comparison Operators
Comparison operator
Comparison operators are used to compare values. It returns either True or False according to the
condition. There are 6 comparison operators in Python:

● Equal ( ==)
● Not equal ( != )
● Greater than ( >)
● Less than ( <)
● Greater than or equal to ( >=)
● Less than or equal to ( <=)
Equal ( ==)
This operand used to check if both operands are equal. If they are equal, this operator will return True.
Try it!
Try to run the following code, see if the result is as you expected:

What is the result of each code, is it True or False?


Not equal ( != )
This operand used to check if both operands are not equal. If they are not equal, this operator will return
True.
Try it!
Try to run the following code, see if the result is as you expected:

What is the result of each code, is it True or False?


Greater than ( > )
This operator will return True if left operand is greater than the right.
Try it!
Try to run the following code, see if the result is as you expected:

What is the result of each code, is it True or False?


Less than ( < )
This operator will return True if left operand is lesser than the right.
Try it!
Try to run the following code, see if the result is as you expected:

What is the result of each code, is it True or False?


Greater than or equal to ( >= )
This operator will return True if left operand is greater than or equal to the right.
Try it!
Try to run the following code, see if the result is as you expected:

What is the result of each code, is it True or False?


Less than or equal to ( <= )
This operator will return True if left operand is lesser than or equal to the right.
Try it!
Try to run the following code, see if the result is as you expected:

What is the result of each code, is it True or False?


Basic Introduction to Conditional
statement
The conditional statement is used to control the program flow in a Python program. This makes it possible
to decide at runtime whether certain program parts should be executed or not.

The simplest form of an if statement in a Python program looks like this:


Conditional Statements in Real Life
We can actually convert a conditional statement in real life into the a Python code. Take a look on the
following example:
“If today is raining, I will use an umbrella. Else, I will not use an umbrella.”

We can convert the above statement in to a python code:


Try it!
Try to run the following code, see if the result is as you expected:

What is the result of each code?


Example
Question
You are an employee from civil registry office. You had been given a task to create a program to
determine if the person is able to create a national id or not based on their age. The rule that only a
person above 16 years old that able to create a national id. Please create a program that able to do so if
the input is only the person age.

For example:
Input: 20
Output: “You able to create a national id”
Steps to solve (Algorithm)
1. Take the input from the user and store it into a variable.
2. Determine if the input (age) is greater than 16 or not.
3. If the input (age) is greater than 16, then print the message that they are able to create a national
id.
4. Else, print the message that they are not able to create a national id.
Result
Practice II
For student whose last digit of student id is odd

Please recreate the previous program, but only a person above 18 years old that able to create national id

Take the screenshot of your program (code and terminal) and put it into the docx file!
For student whose last digit of student id is
even

Please recreate the previous program, but only a person above 20 years old that able to create national id

Take the screenshot of your program (code and terminal) and put it into the docx file!
Python Logical Operators
Logical operator
There are 3 logical operators in Python:

● And ( and )
● Or ( or )
● Not ( not )
And ( and )
This operator will return True if both the operands are true
Try it!
Try to run the following code, see if the result is as you expected:

What is the result of each code, is it True or False?


Or ( or )
This operator will return True if either of the operands is true
Try it!
Try to run the following code, see if the result is as you expected:

What is the result of each code, is it True or False?


Not ( not )
This operator will return True if operand is False (complementsthe operand)
Try it!
Try to run the following code, see if the result is as you expected:

What is the result of each code, is it True or False?


Example
Question
There are several new regulations from the government regarding procedures for using vehicles. People
who are allowed to drive are people who are over 17 years old and already have a driving license. You are
tasked with creating a program to determine whether someone can drive a vehicle or not. Your program
requires 2 inputs, namely age and confirmation of whether the person has a driving license.

For example:
Input:
● 18
● Yes

Output: “You are able to drive this vehicle”


Steps to solve (Algorithm)
1. Get the age input from the user and store it into a variable.
2. Get the input from the user of they have a driving license (lowercase yes if they have, lowercase no
if they don't have), and store the input into a variable.
3. Check if the user is above 17 years old and have a driving license.
4. If the user satisfies all requirements, then print that they are able to drive the vehicle.
5. Else, print that they are not able to drive the vehicle.
Result
Practice III
For student whose last digit of student id is odd

Please recreate the previous program, but the age the they are able to drive is 19 years old.

Take the screenshot of your program (code and terminal) and put it into the docx file!
For student whose last digit of student id is
even

Please recreate the previous program, but the age the they are able to drive is 21 years old.

Take the screenshot of your program (code and terminal) and put it into the docx file!
Additional Practice
(Optional)
Optional Exercise 1
Optional Exercise
Create a prompt for inputting your year of birth. If it’s an odd year, print “Odd” to the terminal, otherwise, print
“Even”.

Which should result in:


Submission
Screenshot & Upload to eCampus
Screenshot your codes and terminals (which you previously did) and put them into a docx or pdf file.
Don’t forget to put your name and student id in the top of the file.

Upload the file to Ecampus on Practice Week 6.

Practice I -> Screenshot your program code and output depending on your student id last digit
Practice II -> Screenshot your program code and output depending on your student id last digit
Practice III -> Screenshot your program code and output depending on your student id last digit
Questions?
References
https://www.programiz.com/python-programming/operators
https://python-reference.readthedocs.io/en/latest/docs/operators/

https://www.w3schools.com/python/python_operators.asp

https://www.python-course.eu/python3_conditional_statements.php

You might also like