0% found this document useful (0 votes)
12 views

05 - Intro To Python Part 2 - (W Exercise)

Uploaded by

Rabbani Radzi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

05 - Intro To Python Part 2 - (W Exercise)

Uploaded by

Rabbani Radzi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

INTRODUCTION TO

Python
Part 2
At the end of this lecture, you should learn:

■ Arithmetic operators
■ Comparison operators
■ Logical operators
■ Conditional operators
Introduction of expression
■ Operators are special symbols in Python that carry
out certain computation activities.
■ The value that the operator operates on is called
the operand.

a + b

Operators
Operands
Introduction of expression
■ There are 4 kinds of Python operators:

◻ Arithmetic: The math symbols we're all familiar


with as well as some less-well known ones.
◻ Comparison: These operators tell you which of
several things is bigger.
◻ Logical or Boolean: These operators test
whether things are true or false.
◻ Conditional: This operator allows you to choose
one of two values based on a logical test.
Arithmetic Operators

■ What it arithmetic operators?


◻ Mathematics basic operators

■ Why we need it?


◻ Program can perform mathematical calculation

■ Can you give example of the arithmetic operators?

Page 5
Arithmetic Operators

Operator Description Example


+ Addition - Adds values on either side of the a + b will give 30
operator
- Subtraction - Subtracts right hand operand a - b will give -10
from left hand operand
* Multiplication - Multiplies values on either a * b will give 200
side of the operator
/ Division - Divides left hand operand by b / a will give 2
right hand operand
% Modulus - Divides left hand operand by b % a will give 0
right hand operand and returns remainder
** Exponent - Performs exponential (power) a**b will give 10 to
calculation on operators the power 20
// Floor Division - The division of operands 9//2 is equal to 4 and
where the result is the quotient in which 9.0//2.0 is equal to 4.0
the digits after the decimal point are
removed.
Expressions
■ expression: A data value or set of operations to compute a
value.
Examples: 1 + 4 * 3
4 / 2
■ Arithmetic operators we will use:
◻+ - addition, subtraction/negation,
◻* / multiplication, division
◻% modulus, a.k.a. remainder
◻ ** exponentiation (10**2 =10 to the power 2)

7
Expressions

■ precedence: Order in which operations are computed.


◻ * / % ** have a higher precedence than + -

1 + 3 * 4 is 13

◻ Parentheses can be used to force a certain order of


evaluation.
(1 + 3) * 4 is 16

8
Integer division
■ When we divide integers with / , the quotient is
also an integer.
3 52
4 ) 14 27 ) 1425
12 135
2 75
54
21
◻ More examples:
■ 35 / 5 is 7
■ 84 / 10 is 8
■ 156 / 100 is 1

9
Integer division

■ The % operator computes the remainder from


a division of integers.
3 43
4 ) 14 5 ) 218
12 20
2 18
15
3

10
Exercise 1
■ Create a program that reads two integers, a and b, from the
user. Your program should compute and display:

■ The sum of a and b # +


■ The difference when b is subtracted from a # -
■ The product of a and b # *
■ The quotient when a is divided by b # /
■ The remainder when a is divided by b # %
■ The result of ab # **
Exercise 2
■ The volume of a cylinder can be computed by multiplying
the area of its circular base by its height. Write a program
that reads the radius of the cylinder, along with its height,
from the user and computes its volume. Display the result
rounded to 1 decimal place.

hint: round (a , 2)
Exercise 3
The program that you create for this exercise will begin by
reading the cost of a meal ordered at a restaurant from the
user. Then your program will compute the service tax and the
tip for the meal. (service tax = 6%). Compute the tip as 18
percent of the meal amount (without tax). The output from your
program should include the tax amount, the tip amount, and
the grand total for the meal including both tax and the tip.
Format the output so that all the values are displayed using
two decimal places.

13
Using operators with Strings
■ helloworld = "hello" + " " + "world“
■ print(helloworld)
Using operators with list
■ even_numbers = [2,4,6,8]
■ odd_numbers = [1,3,5,7]
■ all_numbers = odd_numbers + even_numbers
■ print(all_numbers)

■ What is the output?


Exercise 4
■ What will be the output of the following program:

print([1,2,3] * 3)

■ What will be the output of the following program:

lotsofhellos = "hello" * 10
print(lotsofhellos)
Comparison Operators

■ What are comparison operators?


◻ Operators use in decision making.
■ Why we need it?
◻ For logical structure (e.g. If, if-else etc)
■ Condition in if structures can be formed by using
comparison operators.
■ Can you give example of comparison operators?

Page 17
Comparison/Relational Operators
Operator Description Example
== Checks if the value of two operands are equal or not, (a == b) is not true.
if yes then condition becomes true.
!= Checks if the value of two operands are equal or not, (a != b) is true.
if values are not equal then condition becomes true.
<> Checks if the value of two operands are equal or not, (a <> b) is true. This is
if values are not equal then condition becomes true. similar to != operator.
> Checks if the value of left operand is greater than (a > b) is not true.
the value of right operand, if yes then condition
becomes true.
< Checks if the value of left operand is less than the (a < b) is true.
value of right operand, if yes then condition becomes
true.
>= Checks if the value of left operand is greater than or (a >= b) is not true.
equal to the value of right operand, if yes then
condition becomes true.
<= Checks if the value of left operand is less than or (a <= b) is true.
equal to the value of right operand, if yes then
condition becomes true.

Page 18
Comparison Operators
print ("************************")
print ("Comparison operator ")
print ("************************")
a=input("Masukkan nilai a : ")
b=input("Masukkan nilai b : ")
print ("************************")
print("a > b : ", a>b)
print("a < b : ", a<b)
print("a == b : ", a==b)
print("a != b : ", a!=b)
print("a >= b : ", a>=b)
print("a <= b : ", a<=b)

Page 19
Exercise 5
■ Write a program that reads integer by user and the program
has to evaluate whether the integer is zero. Display result of
the evaluation.
■ Write a program that reads an integer from the user. Then
your program should display a message indicating whether
the integer is even or odd.
Logical Operators

■ What are logical operators?


◻ Are they also use in decision making
■ Can you give example of logical operators?

Page 21
Logical Operators
Operator Description Example
and Called Logical AND operator. If both the (a and b) is true.
operands are true then then condition
becomes true.
or Called Logical OR Operator. If any of the (a or b) is true.
two operands are non zero then then
condition becomes true.
not Called Logical NOT Operator. Use to not(a and b) is false.
reverses the logical state of its operand.
If a condition is true then Logical NOT
operator will make false.

Truth Table

Page 22
Logical Operators

print ("************************")
print (“Logical operator ")
print ("************************")
a=input("Masukkan nilai a : ")
b=input("Masukkan nilai b : ")
c=input("Masukkan nilai c : ")
print ("************************")
print("a > b AND a > c: ", a>b and a>c)
print("a > b OR a > c: ", a>b or a>c)
print("a < b AND a < c: ", a<b and a<c)
print("a < b OR a < c: ", a<b or a<c)
print("not(a > b) AND a > c: ", not(a>b) and a>c)

Page 23
Conditional Operators
■ Operators that evaluate something based on a condition
being true or false
■ Allows to test a condition in a single line replacing the
multiline if-else making the code compact.

# Program to demonstrate conditional operator


a, b = 10, 20
# Copy value of a in min if a < b else copy b
min = a if a < b else b
print(min)
Example
# Python program to demonstrate nested ternary
operator
a, b = 10, 20

if a != b:
if a > b:
print("a is greater than b")
else:
print("b is greater than a")
else:
print("Both a and b are equal")
Homework
■ Write a program that begins by reading a temperature from
the user in degrees Celcius. Then your program should
display the equivalent temperature in degrees Fahrenheit
and degrees Kelvin. The calculations needed to convert
between different units of temperature can be found on the
Internet.

note:
Degrees Fahrenheit = (Degrees Celcius × 9/5) + 32

You might also like