week2
week2
1
Input from the User
Interactive
input() Program print()
2
Using input( )
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:' )
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
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
* Parentheses have the highest priority and are evaluated from the innermost set to the outermost set.
10
Quick Check
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).
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