0% found this document useful (0 votes)
23 views18 pages

ITE3711 Lecture1.3 Operators 20230906

Uploaded by

zainoff445
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views18 pages

ITE3711 Lecture1.3 Operators 20230906

Uploaded by

zainoff445
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Lecture 1

Part 3 :
Operators

ITE3711 - Programming Concept and Applications


Lecture 1 – Part 3

Python operators’ groups

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

2
Lecture 1 – Part 3

Arithmetic operators

Arithmetic operators are used with numeric values to perform common mathematical operations

Operator Name Example


** Priority 1
+ Addition a+b 5 + 5  10
* / // % Priority 2
- Minus, Subtraction a-b 5-50
+- Priority 3
* Multiplication a*b 5 * 5  25
/ Division a/b 5/51
** Superscripts, a ** b 5 ** 2  25
Exponentiation
// Integer division a // b 5 // 2  2.5  2
% Remainder, Modulus a%b 5%21 3
Lecture 1 – Part 3

Arithmetic operators

Example of + addition and - minus


Example Result
print(5 + 5) 10
print(5.0 + 5.0) 10.0
print(5 - 5) 0
print(5.0 - 5.0) 0.0
print(-5 + 5) 0
print(-5.0 + 5.0) 0.0

4
Lecture 1 – Part 3

Arithmetic operators

Example of * multiplication,
/ division, Example Result

** superscripts, print(5 * 5) 25
print(5.0 * 5.0) 25.0
// integer division,
print(5 ** 4) 625
and % remainder
print(5 / 5) 1.0
print(5.0 / 5.0) 1.0
print(5 / 4) 1.25
print(5 // 4) 1
print(4 % 4) 0
5
print(5 % 4) 1
Lecture 1 – Part 3

Arithmetic operators

Writing a formula using arithmetic


operators Example Result
(Program code format) (5 * (4 - 3)) / 6
(7 + a) / ((2 * a) * 1) - (2 * c) / 3
(a ** 2 + b ** 2) ** 0.5

6
Lecture 1 – Part 3

Assignment operators

Assignment operators are used to assign


values to variables Operator Example Equal to (Same as)
(Shortened way of writing operations) = x=5 x=5
+= x += 5 x=x+5
-= x -= 5 x=x-5
*= x *= 5 x=x*5
/= x /= 5 x=x/5
%= x %= 5 x=x%5

7
Lecture 1 – Part 3

Assignment operators

Example of =, += and -=
Operator Example Result
= x=5 5
print(x)

+= x=5 10
x += 5
print(x)
-= x=5 0
x -= 5
print(x)

8
Lecture 1 – Part 3

Assignment operators

Example of *=, /= and %=


Operator Example Result
*= x=5 25
x *= 5
print(x)
/= x=5 1.0
x /= 5
print(x)
%= x=5 0
x %= 5
print(x)

9
Lecture 1 – Part 3

Comparison operators

Comparison operators are used to


compare two values Operator Name Example
Assume a = 5, b = 10, c = 5
(Conditional statement)
== Equal a == b a == b  False
!= Not Equal a != b a != b  True
> Greater than a>b a > b  False
< Less than a<b a < b  True
>= Greater than or equal to a >= b a >= b  False
a >= c  True
<= Less than or equal to a <= b a <= b  True
10
a <= c  True
Lecture 1 – Part 3

Logical operators

Logical operators are used to combine


conditional statements Operator Description Example
Assume a = 5, b = 10
and Returns TRUE if all the statements are a <= b and a >= b
true  False
or Returns TRUE if one of the statement is a <= b or a >= b
true  True
not Reverse the result, not(a > b)
True  False  True
False  True

11
Lecture 1 – Part 3

Logical operators

Truth table
Condition 1 Condition 2 and or
result result
True True True True
True False False True
False True False True
False False False False

Condition not
result
True False
12
False True
Lecture 1 – Part 3

Example of using operators

number1 = 5
number2 = 10
sum = number1 + number2

print(sum) Output:
print("The sum of two numbers: " + str(sum)) 15
The sum of two numbers: 15

Using + operator to calculate the sum


sum = number1 + number2

Using + operator to combine string and numeric value

13
Lecture 1 – Part 3

Example of using operators


Output:
15
The sum of two numbers: 15
number1 = 5 35
number2 = 10 The sum of two numbers: 35
sum = number1 + number2

print(sum)
print("The sum of two numbers: " + str(sum))

number1 = 15
Update the value of variable – number1 and number2
number2 = 20
sum = number1 + number2

print(sum)
print("The sum of two numbers: " + str(sum))
14
Lecture 1 – Part 3

Example of using operators


Output:
15
The sum of 5 and 10 is 15
number1 = 5 35
number2 = 10 The sum of 15 and 20 is 35
sum = number1 + number2

print(sum)
print("The sum of " + str(number1) + " and " + str(number2) + " is " + str(sum))

number1 = 15 Using + operator to combine strings and numbers


number2 = 20
sum = number1 + number2 Update the value of variable – number1 and number2

print(sum)
print("The sum of " + str(number1) + " and " + str(number2) + " is " + str(sum))
15
Lecture 1 – Part 3

Example of using operators


Output:
15
The sum of 5 and 10 is 15
number1 = 5 35
number2 = 10 The sum of 15 and 20 is 35
sum = number1 + number2

print(sum)
print("The sum of " + str(number1) + " and " + str(number2) + " is " + str(sum))

number1 += 10 Using += operator to update the value of variable


number2 += 10 number1 += 10 is the shortened way of
sum = number1 + number2 number1 = number1 + 10

print(sum)
print("The sum of " + str(number1) + " and " + str(number2) + " is " + str(sum))
16
Lecture 1 – Part 3

Example of using operators


Output:
Peter can play the roller coaster? True

name = "Peter" John can play the roller coaster? False


height = 173

print(name + " can play the roller coaster? " + str(height>145))

print() Using > operator to get True / False

print() is used to print a new line


name = "John"
height = 182 Using and operator to combine conditions

print(name + " can play the roller coaster? " + str(height>145 and height<180))

17
Lecture 1 – Part 3

Example of using operators


Output:
The result of c is: 11.180339887498949
The result of c is: 11.18
a = 5 The result of c is: 11.2
b = 10
c = (a ** 2 + b ** 2) ** 0.5

print("The result of c is: " + str(c))


print("The result of c is: " + str(round(c,2))) Using ** operator
print("The result of c is: " + str(round(c,1)))
round() is used to set the float number rounded to how many
decimal place(es)

round(variable, decimal place(es))

18

You might also like