Week 4
Week 4
Week 4
1
Expressions
An expression is a statement which can be evaluated, and has a value, and a type.
Expressions may contain string literals, numeric constants, variables and operators.
Some examples of expressions:
5 -> value is 5, type is int
‘abc’ ->value is ‘abc’, type is str
7 + 5 -> value is 12, type is int
3.5 * 2 -> value is 7.0, type is float (why?)
x = 6 -> value is 6, type is int
x * 2 -> value is 12, type is int
2
Arithmetic Expressions - Operators
3
Quick Check
6
Concatenating Strings in print()
Instead of using the comma, we can output multiple strings with the print() statement by
concatenating.
Look at the examples below, what is the difference between the comma and the +
usage?
7
Concatenating Strings in print()
When concatenating within the print statement, remember to convert all operands to
strings first. This is not necessary when joining using the comma.
8
Operator Precedence and Associativity
When different operators appear in the same expression, the normal rules of arithmetic apply.
All Python operators have a precedence and associativity:
• Precedence—when an expression contains two different kinds of operators, which should be applied first?
• Associativity—when an expression contains two operators with the same precedence, which should be
applied first?
To see how precedence works, consider the expression 2 + 3 * 4
• Should it be interpreted as (2 + 3) * 4
• (that is, 20), or rather is 2 + (3 * 4)
According to the rules of precedence, the multiplication operator has a higher precedence than the addition
operator, so the first operation will be multiplication, followed by addition.
To change the order of operations, you may use parentheses. Parentheses have the highest precedence.
9
Operator Precedence
* Parentheses have the highest priority and are evaluated from the innermost set to the outermost set.
10
Quick Check
a + b + c + d + e a + b * c - d / e
a / (b + c) - d % e
a / (b * (c + (d - e)))
11
Quick Check
a / (b + c) - d % e
2 1 4 3
a / (b * (c + (d - e)))
4 3 2 1
12
Examples of Arithmetic Expressions
1. Write a script that inputs a temperature in Fahrenheit from the user and converts to Celsius.
See: convert_fahrenheit.py
2. Write a script that inputs a user’s height (in meters) and weight in kilograms and calculates and displays their
BMI.
𝐰𝐞𝐢𝐠𝐡𝐭 (𝐤𝐠)
𝐁𝐌𝐈 =
𝐡𝐞𝐢𝐠𝐡𝐭 𝟐 (𝐦𝟐 )
13
See: calculate_bmi.py
Assignment Operators
14
Assignment Operators
Output:
15
Built-in Functions
A function is a small program that has a name, takes some input, and does something for us. Sometimes the
function will return the result of a calculation.
Python defines a number of functions that we can use in our programs, that give us functionality. These are called
built-in functions.
We have already used some built-in functions in Python. The print() and input() statements are examples of built
in functions.
Functions have: a name, data required by the function (input arguments) and the result returned by the function
(return value).
One such module is the math module contains common mathematical functions and constants.
To use the functions and constants defined in a module, we must first import the module that
contains the functionality we want to use.
Once we have imported the module, we can access its functionality using the
module_name.function syntax.
17
Math Functions
The math module contains common mathematical functions and constants, which include:
18
Examples with Math functions
1. Write a python script (math_exercise.py) that inputs two values and calculates and display the following:
• The square root of first number divided by the square root of the second number, rounded up.
• The second number divided by the first number, rounded down (floor vs //).
2. Write a python script (circle_exercise.py) that inputs the radius of a circle and calculates its area and perimeter.
19
Structured Types: str
Strings are structured because you can use indexing to extract individual characters from
a string and slicing to extract substrings.
String objects are immutable, meaning that once the string is created, it cannot be
modified.
20
Updating Strings
name1 “125"
Before:
name2 “CS"
name1 “125"
After: “CS"
name2
“CS125"
21
String Functions
Some data types have their own built-in functionality, a good example of this is the string (str) type.
Below are some common functions that we can use with strings. Because strings are immutable, the
functions below that change a string will return a new string.
See: string_functions.py 23
Examples with String Functions
1. Write a python script (string_exercise.py) that inputs a string containing a country name, a population
and a life expectancy. Assume each value is separated by a single space. Complete the following:
2. Count the number of 0s in the population. Display the population increased by 10%.
24