0% found this document useful (0 votes)
17 views17 pages

week2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
17 views17 pages

week2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 17

Week 2

User input, expressions and built-in functions.

1
Input from the User

Many programs we create are are interactive.


Interactive programs allow users to input information which the program then
analyzes and outputs some result.
Input from the user is done using the input() function.

Interactive
input() Program print()

2
Using input( )

To use functions we need to know 3 things:


1. What information does the function need to do its job?
Answer: a message to display to the user.
2. What will the function return?
Answer: the string value input by the user.
3. What should our program do with the value returned by the input
function?
Answer: change the value to the correct type and store it in a
variable so we can use it in our programs.

3
Example using input( )
#input birth year,store in variable #input birth year,store in variable
birth_year = input( 'Enter year: ' )
birth_year = input( 'Enter year:' )

#change type to number (int)


#calculate age of user birth_year = int( birth_year )
age = 2024 - birth_year
#calculate the age of the user
#display result age = 2024 - birth_year
print(‘your age is:’, age)
#display result
print(‘your age is:’, age)

OUTPUT: OUTPUT (assume user input 2004):


TypeError: unsupported operand your age is: 20
type(s) for -: 'int' and 'str' 4
Expressions

An expression is a statement that produces a result.

There are several types of expressions in Python:

1. Arithmetic Expression: calculates result of an expression containing


arithmetic operators and numeric values.
2. Relational Expression: compares two values using comparison
operators (>,<,==).
3. Logical Expression: combines values using and, or, not.
Here we will focus on arithmetic expressions.
5
Arithmetic Expressions - Operators

6
Examples of Arithmetic Expressions
5 -> value is 5, type is int
7 + 5 -> value is 12, type is int
3.5 * 2 -> value is 7.0, type is float (why?)
x * 2 -> value is 12, type is int
10 // 2.0 -> what is the type and value?

7
Quick Check

What are the results of the following expressions?


12 / 5 = 2.4
12 // 5 = 2
10 / 4.0 = 2.5
10 // 4.0 = 2.0
4 / 10 = 0.4
4 // 10 = 0
12 % 3 = 0
10 % 3 = 1
3 % 10 = 3
8
Operator Precedence and Associativity
Precedence: when an expression contains two or more different operators, which should be applied first?
Consider the expression 2 + 3 * 4
Should it be interpreted as (2 + 3) * 4 with the value 20?
Should it be interpreted as 2 + (3 * 4) with the value 14?
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

Associativity: when an expression contains two operators with the same precedence the operators are
applied from left to right.

To change the order of operations, you may use parentheses which have the highest precedence.
9
Operator Precedence

Arity Operators Associativity


Binary ** Right
Unary +, -
Binary *, /, //, % Left
Binary +, - Left
Binary = Right

* Parentheses have the highest priority and are evaluated from the innermost set to the outermost set.

10
Quick Check

In what order are the operators evaluated in the


following expressions?
a + b + c + d + e a + b * c - d / e
1 2 3 4 3 1 4 2

a / (b + c) - d % e
2 1 4 3

a / (b * (c + (d - e)))
4 3 2 1

11
Example using Arithmetic Operators
Problem Description:
You are the owner of a bakery that sells muffins in containers of 3. Each container
costs 200TL.
Muffins left at the end of the day are packaged in bags containing 5 muffins and all
bags are sold to a local after school kids organization for 100TL.
Muffins that do not make it into a bag are set outside to feed neighborhood squirrels.
Write a program that does the following:
1. Input the number of muffins baked by the bakery.
2. Input the number of containers sold at full price by the bakery.
3. Calculate and display the number of muffins not sold.
4. Calculate and display the number of bags made and sold at the discounted price.
5. Calculate and display the number of muffins left out for the squirrels.
6. Calculate and display the money earned by the bakery.
12
Examples of Arithmetic Expressions
1. Write a script that inputs a temperature in fahrenheit from the user, converts it to
celsius and displays the result.
See: convert_temp.py

2. Write a script that inputs the user’s height in meters and weight in kilograms. The
script should calculate and display the user’s bmi.
See: calculate_bmi.py

13
Built-in Functions
Function: a small program that has a name, takes some input, and does something for us.
Built-in functions: functions defined by Python that we can use in our programs; they give us
functionality. Examples: print( ), input( )
Functions have:
• a name,
• input values (data required by the function),
• return value (result returned by the function).

Useful functions include:


• round( ): takes float value and returns rounded value.
• abs(): takes a float/int value and returns the absolute value.

14
Built-in Functions (Math module)
Functions are often grouped according to their functionality. The groups are called packages or
modules.
Modules can contain functions as well as constant values.
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.

15
Math Functions
The math module contains common mathematical functions and constants, which
include:
• ceil() : Returns the smallest integer greater than or equal to x.
• floor() : Returns the largest integer less than or equal to x.
• sqrt() : Returns the square root of x.
• pi: Mathematical constant, 3.1415…

16
Examples with Math functions
1. Write a python script (math_exercise.py) that does the following:
○ Input the area of a square as an integer.
– Calculate and display the side length of the square.
– Round the side length and display.
○ Input a side length of a square as floating point number.
– Calculate and display the area of the square.
– Round the area up to the next integer value and display.
– Round the area down to the next integer value and display.
– Round the area to two decimal places and display.

2. Write a python script (circle_exercise.py) that inputs the radius of a circle and
calculates and displays its area and perimeter.
17

You might also like