Professional Documents
Culture Documents
Week 3 - Operators in Python
Week 3 - Operators in Python
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.
● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators
● Identity operators
● Membership operators
● Bitwise operators
● 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:
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:
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:
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.
The result of integer division is always rounded to the nearest integer value that is less than the real (not
rounded) result.
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:
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:
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:
For example:
Input:
● 18
● Yes
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”.
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