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

Python Operators and Functions

Here is a program to calculate daily expenses that prompts the user to enter 5 expenses and prints the total: # Program to calculate daily expenses # Prompt the user to enter the first expense expense1 = float(input("Enter the price of the first item: ")) # Prompt the user to enter the second expense expense2 = float(input("Enter the price of the second item: ")) # Prompt the user to enter the third expense expense3 = float(input("Enter the price of the third item: ")) # Prompt the user to enter the fourth expense expense4 = float(input("Enter the price of the fourth item: ")) # Pro

Uploaded by

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

Python Operators and Functions

Here is a program to calculate daily expenses that prompts the user to enter 5 expenses and prints the total: # Program to calculate daily expenses # Prompt the user to enter the first expense expense1 = float(input("Enter the price of the first item: ")) # Prompt the user to enter the second expense expense2 = float(input("Enter the price of the second item: ")) # Prompt the user to enter the third expense expense3 = float(input("Enter the price of the third item: ")) # Prompt the user to enter the fourth expense expense4 = float(input("Enter the price of the fourth item: ")) # Pro

Uploaded by

upneetkaurrud
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Intro to Python

Walid Belal
Winter 2024
PROG12583
• Apply arithmetic operations
• Explore numeric data types

Objectives • Explore math functions


• Determine programming errors
• Apply programming logic to solve
simple problems
Arithmetic Operators
Arithmetic Expression
• Arithmetic expression is an expression that combines
Example of
operand operands (numbers), operators, and brackets ()

Example of
operator

Urban & Murach, 2021 p 39


Step 1
The Order of Precedence

Step 2
The order of applying operators

** Start from left of the expression


Step 3 * / // % Start from left of the expression
+ - Start from left of the expression
Step 1

The Order of Precedence

Step 2 Parentheses () are used to override the


order or precedence
• Start from the inside bracket and make your way
outwards

Step 3
This statement is
equivalent to Compound Assignment Operators
val = val + (expression)

Compounds (add) a value or the


This statement is
result of expression to the existing
equivalent to value of a variable
val = val - (expression) Subtracts a value or the result of
expression to the existing value of a
variable
Multiply a value or the result of
expression to the existing value of a
This statement is variable
equivalent to
val = val * (expression)
Activity
• Let's debug this code. What is the value of a, b,
and c at lines 2,4,6,8, 10 and 12. fill in the
below table with the values

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.

Counter is a variable that is used in a


program to keep track on how many times
something happened.

This is another way how


to implement a counter Counters are very useful im programming,
using compound
assignment operators
so you better get a good grasp of them
Activity

Let's relate compound operators to real life problems

Think of a real-life example where compound operators


can be used to solve it

Think of a real-life example where a counter can be used


to solve it
More on Numeric Data
Types
int
int takes negative integer numbers, 0 , and positive integer numbers

There is no maximum limit to the number of digits of an integer in python 3

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

• A floating-point number is an approximate


This is scientific notation value that can yield unexpected results
= 12.3434 *
known as floating-point errors
Floats-point errors
• Floating numbers are approximations, so
they present an approximate result if used in
a mathematical operation

• This can result in calculation errors (floating-


point errors)
Equation in line 7 is derived from the equation
on line 3 but the result is different
Rounding floats
• Floating numbers are approximations, so
they present an approximate result if used in
a mathematical operation

• This can result in calculation errors (floating-


point errors)
math module
Math module

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

• Easy to find and fix. IDEs will highlight the error


• Most common errors:

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

What are the syntax errors in this code


and explain what caused the error
The syntax is correct but
will get an error when the
program will run and
evaluate the numbers

Runtime Errors
• The syntax is correct, but errors
happens when the program is
running

The syntax is correct but will get an error if a user doe


• The error causes the program to stop
sont enter a string that can be converted to integer
running
• Mostly happens when there are
errors related to inputs
• To the left are two examples of
runtime error
Example of logic error.
Try to find the error and Logic Errors
fix it
• No syntax or logic errors
• The error in the business functionality
rather than the code
• Most difficult error to find
• Programmer must ensure that the
code does not have logic errors
Activities
Activity

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

You need to use comments in your code.

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)

Your program should calculate:


-40 -30 -20 -10 0 10 20 30 40
1- The mean of the measurement
2- The standard deviation

Print the mean and standard deviation of your


measurements to screen
References

Urban, M., & Murach, J. (2021). Murach’s Python programming : beginner to


pro (2nd edition.). Mike Murach & Associates, Inc.

You might also like