05 - Intro To Python Part 2 - (W Exercise)
05 - Intro To Python Part 2 - (W Exercise)
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:
Page 5
Arithmetic Operators
7
Expressions
1 + 3 * 4 is 13
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
10
Exercise 1
■ Create a program that reads two integers, a and b, from the
user. Your program should compute and display:
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)
print([1,2,3] * 3)
lotsofhellos = "hello" * 10
print(lotsofhellos)
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
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.
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