Python Operators and Functions
Python Operators and Functions
Walid Belal
Winter 2024
PROG12583
• Apply arithmetic operations
• Explore numeric data types
Example of
operator
Step 2
The order of applying operators
Step 3
This statement is
equivalent to Compound Assignment Operators
val = val + (expression)
Line a b c
2
4
6
8
10
12
This is one way how to
implement a counter Counter
Compound assignments can be used to
generate a counter.
Examples of int
float
• Floating numbers are negative or positive
numbers with decimals.
• float type is double-precision floating-point
number (usually occupy 64 bits in computer
memory).
• Can use scientific notations
Examples of useful math functions • Modules are files that contain code (for
example functions)
ceil(num) floor(num) • Python provides multiple built in modules
that can asset you with your programming
effort
fabs(num) pow(num,pow)
• math is one of those modules and it contains
mathematical functions
sqrt(num) • Here is a link to the math module functions:
https://docs.python.org/3/library/math.html
You first need to imports the
module (library) to your program
In order to use the functions
inside your program
Math module
m is an alias (nick name)
so now you will type
m.cei() instead of • Run this code and write down what will each
math.ceil(). Don’t need
to use it if you don’t want
to. It is there for your
print statement will print
convenience
Line Result
Print statement at line 5
Print statement at line 7
Print statement at line 11
Print statement at line 15
Print statement at line 17
Type of Errors
Three
Syntax errors (Compile-time error):
• The compiler will not understand the
statement and the program will not
Types of compile
Runtime errors:
Errors • The program compiles but the error
happens when the program is running
Logic errors:
• The program runs but it produces a
wrong result
Syntax Error
Writing variable
Forget to declare Forget to close
or part of it in
a variable brackets
different case
forget to close or
Forget Misspelled
open quotes for
indentation keywork
string literals
Activity
Runtime Errors
• The syntax is correct, but errors
happens when the program is
running
The format of your result should Ask the user to enter a number
be like this
that represents minutes (for
example 170). The program
will format the time to
hour:minutes (for example
Hours Minutes
(2:50)
Activity
Make a program that will calculate daily expenses. Ask a user to enter
5 expenses (the price of 5 things that they bought today). The program
should calculate the expenses (add the numbers together and print the
result to screen
Make sure that there is an appropriate prompt for the input and the
output is formatted properly.
Activity
Create a program that asks a user to input 4
temperature values (could be negative or positive)