0% found this document useful (0 votes)
3 views14 pages

S02 Operators

The document outlines a seminar on Python operators, covering mathematical, comparison, and logical operators, as well as debugging techniques. It explains the use of modules, decision-making structures like if statements, and the importance of understanding errors and exceptions in coding. Key learning objectives include manipulating numbers, using shortcut operators, and managing exceptions effectively.

Uploaded by

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

S02 Operators

The document outlines a seminar on Python operators, covering mathematical, comparison, and logical operators, as well as debugging techniques. It explains the use of modules, decision-making structures like if statements, and the importance of understanding errors and exceptions in coding. Key learning objectives include manipulating numbers, using shortcut operators, and managing exceptions effectively.

Uploaded by

esthereqwong
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Seminar 2:

Operators in Python

Learning Objectives
In this lecture, you will learn
● Mathematical operators
● Comparison operators
● Logical operators
● Debugging

1
Maths Operators

1. Addition +
2. Subtraction -
3. Multiplication *
4. To the power of **
5. Float division /
6. Integer division // Which
operator to
7. Modulus/Remainder % run first????

Augmented assignment
shortcuts
1. Increment +=
2. Decrement -=
3. Multiplying *=
4. Dividing /=

2
Module
Module
● A python program file.
● Eg. [Link] is a module.
Import a module
● Re-use proven program by including the module in another
program.
● General Syntax:

OR
import module_name from math import * # import everything
module_name.some_constant some_constant
module_name.some_function() some_function()

Importing math module


● Contains some useful maths functions and constants.
● Can be combined with other data for maths operations.

3
Decision making
● Sequential program: ● Indentation and Branching:

Decision making
● Sequential program: ● Indentation and Branching:

4
Decision -> if
• Decision involves comparison.
• Compare numbers, compare texts.
• Compare using comparison operators to
results in Boolean values (True / False).
• Decision can be reached by using if, for, and
while loop.
• Loop will be executed when the comparison
results in True.
9

if condition
• Test the comparison condition then execute body if it is True.
• Structure:
if condition:
body
elif condition:
body
else:
body

10

10

5
If condition

Error Checking

Criteria Checking

11

11

Comparison Operators
● Compare the expression on both sides
● To give two possible Boolean outcomes: True False
● Provide control through comparison, used with if.
● Operators: < , > , <= , >= , == , !=

12

12

6
Assignment versus equality
● Assignment operator assigns right value to the left
○ =
● Equality operator compares and results in Boolean value
○ ==

13

13

Logical Operators
● Combine multiple expressions
● To give two possible Boolean outcomes: True False
● Operators:
○ and (True only when both operands are true).

14

14

7
Logical Operators

● Operators:
○ or (True if any of the operands True)

15

15

Logical Operators
● Operators:
○ not

16

16

8
Special Operators

● Operators:
○ is
○ is not
○ in
○ not in

17

17

Orders of operators
PEMDAS Rule

Highest Operator Description

precedence ** Raise to the power of

~+‐ Complement, unary plus and negate

* / % // Multiply, divide, modulo and floor division

+‐ Addition and subtraction

>> << Right and left bitwise shift

<= < > >= Comparison operators

<> == != Equality operators

= %= /= //= ‐= += *= **= Assignment operators

is, is not Identity operators

in, not in Membership operators

not, or, and Logical operators

Lowest
precedence

18

18

9
Debugging
● When errors occur, check for the type of error and correct it

19

19

Debugging
● When errors occur, check for the position of error and correct it

20

10
Errors in Python

Syntax Error
Runtime Error
Logical Error

21

21

Debugging

22

22

11
Errors

ValueError: A type of exception or error raised when the wrong type of arguments are passed.

To fix this: Check the line with error, confirm the type of input required for each function call.

23

23

Types of Exceptions

● AttributeError
● ImportError
● IndexError
● KeyError
● IndentationError
● TypeError
● ValueError
● ZeroDivisionError
● IOError

24

24

12
Exception due to User Input

Exception is thrown when user’s input is invalid.

25

25

Fixing Exception Raised

26

26

13
Exceptions and Errors
● Exceptions and errors are part and parcel of coding.
● Exceptions and errors due to syntax errors should be fixed during
code development.
● Errors due to user entry or other manageable factors should be
mitigated through the use of try and except.

27

27

You have learnt...


1. To use mathematical operators to manipulate numbers in python.
2. The shortcut operators, augmented operators.
3. Different types of comparison operators to construct if condition
4. Compound or Boolean operators to combine multiple expressions
5. Debugging techniques
6. Errors and Exception
7. The way to manage exception

28

28

14

You might also like