Python Lab 3 lecture slides

Download Report

Transcript Python Lab 3 lecture slides

Introduction to Programming
Python Lab 3:
Arithmetic
PythonLab3 lecture slides.ppt
22 January 2016
Ping Brennan ([email protected])
1
Getting Started
• Create a new folder in your disk space with the name PythonLab3
• Launch the Python Integrated Development Environment (IDLE) begin with the Start icon in the lower left corner of the screen.
• If you are in a DCSIS lab, select the options in the order shown:
Start -> All Programs -> Python 3.4 -> IDLE (Python GUI)
A window with the title Python 3.4.1 Shell should appear. This
window is the Shell.
2
Getting Started (2)
• If you are in one of the ITS labs (MAL 109 or MAL 457), select the
options in the order shown:
Start -> All Programs -> Departmental Software -> Computer
Science -> Python 3.4 -> IDLE (Python 3.4 GUI – 64 bit)
A window with the title Python 3.4.4rc1 Shell should appear. This
window is the Shell.
If the window does not appear then click on Start and then in the
box Search programs and files write IDLE. A list will appear.
Click on IDLE(Python GUI). A window with the title Python
2.7.8 should appear. This window is the Shell.
• In the Shell click on File. A drop down menu will appear.
Click on New File. A window with the title Python 3.4.1:Untitled
(DCSIS) or Untitled (ITS) or Python 2.7.8: Untitled (ITS) should
appear. This window is the Editor.
3
Getting Started (3)
• In the Editor, click on File, and then in the drop down menu click
on Save As… .
A window showing a list of folders should appear.
– To search any folder on the list, double click on the folder.
– Find the folder PythonLab3 and double click on it.
– In the box File name at the bottom of the window type
PseudoCode.py, and then click on the button Save in the lower right
corner of the window.
The title of the Editor should change to show the location of the file
PseudoCode.py.
4
Program 1: calculate cost of a car
• Problem statement
Calculate the cost of owning a car for 10 years. Assume a
purchase price of £10,000, a price of £4 per gallon of petrol, a
usage of 15,000 miles per year and a fuel efficiency of 20 miles
per gallon.
•
Objectives
– Using arithmetic operators:
+
-
*
/
– Understanding arithmetic expressions
– Using variables to store values of type int or type float
5
Program 1: calculate cost of a car (2)
• Problem solving
1.
Declare variables in a Python program to store the number of years,
purchase price, price per gallon, annual miles driven, and fuel
efficiency. Choose descriptive variable names, e.g.,
annualMilesDriven = 15000
fuelEfficiency = 20
2. Use the below pseudo code for the calculation and convert it into a
sequence of assignment statements in the program. Create variables
(e.g. annualFuelConsumed) to hold the results of the arithmetic
expressions.
annual fuel consumed = annual miles driven / fuel efficiency
annual fuel cost = price per gallon * annual fuel consumed
operating cost = number of years * annual fuel cost
total cost = purchase price + operating cost
3.
Print out the string "Total cost: " followed by the numerical value
of the total cost.
6
Program 1: calculate cost of a car (3)
•
Provide a comment at the beginning of the program to explain
the purpose of the program, along with your name and the
date.
•
Save the program to the file PseudoCode.py
•
Run your program.
7
Program 2: do integer calculations
• Create a new Editor for a new file called
IntegerCalculations.py
• Problem statement
Write a program that assigns integer values to two variables x
and y, and then prints out the following values:
–
–
–
–
–
–
–
The
The
The
The
The
The
The
sum
difference
product is the multiplication of the values in the two variables.
average
absolute value of the difference
maximum
minimum
8
Program 2: do integer calculations (2)
• Problem solving – convert the below steps into a sequence of
Python statements.
1.
Declare two variables x and y and assign values to them.
2.
Calculate and print the sum, the difference, the product and the
average of the values in the above variables. Break this task down into
4 separate print statements.
3.
Use the below built-in functions in your program to calculate and print
the absolute value of the difference, the maximum and the minimum of
the values in the variables.
Function
Returns
Example
abs(x)
The absolute value of x.
abs(-5) returns the
value 5
max(x1, x2, .., xn) The largest value from
among the arguments.
max(1, 2) returns the
value 2.
min(x1, x2, .., xn)
min(1, 2) returns the
value 1.
The smallest value from
among the arguments.
9
Program 2: do integer calculations (3)
• The printed output should be in the following style
The sum of the two integers is 8.
• Provide a comment at the beginning of the program to explain
the purpose of the program, along with your name and the date.
• Save your program to the file IntegerCalculations.py
• Run your program.
10
Program 3: print properties of Rectangles
• Create a new Editor for a new file called Rectangle.py
• Problem statement
Write a program that assigns integer values to two variables a
and b and then prints out the area of the rectangle with sides a,
b, the perimeter of the rectangle and the length of a diagonal of
the rectangle.
• Problem solving – convert the below steps into a sequence of
Python statements.
1.
Declare two variables a and b and assign values to them.
2.
Calculate and print the area which is the product of the lengths of the
sides, such as, area = a * b
3.
Calculate and print the perimeter which is the sum of the lengths of the
sides.
4.
Calculate and print the square of the diagonal which is equal to the sum
of the squares of the sides. To use the square root function place the
following code at the beginning of your file.
from math import sqrt
11
Program 3: print properties of Rectangles
(2)
• Include in the print out a short description of each number that
is printed.
• Provide a comment at the beginning of the program to explain
the purpose of the program, along with your name and the date.
• Save your program to the file Rectangle.py
• Run your program.
12
Program 4: prints separate digits
• Create a new Editor for a new file called SeparateDigits.py
• Problem statement
Write a program that assigns a five digit positive integer to a
variable x and prints out the individual digits, separated by
spaces. For example, if x has the value 16348 then the print
out is
16348
• Objective
– Understand the arithmetic operators % (modulus) and //
Note that if x and n are integers then:
x//n is the quotient on dividing x by n , e.g. 6//4 returns
the value 1
x%n is the remainder on dividing x by n, e.g. 6%4 returns the
value 2.
13
Program 4: prints separate digits (2)
• Problem solving – write a sequence of Python statements as
follows.
# assign a five digit integer to a variable x
x = 16348
# extract the ten thousandth digit from x
tenThousand = x // 10000
# extract the thousandth digit
thousand = (x % 10000) // 1000
# extract the hundredth digit
hundred = (x % 1000) // 100
# next extract the tenth digit using a similar
# arithmetic calculation as above.
# finally extract the unit digit using a similar
# arithmetic calculation as above.
# add a print statement to output the individual
# digits separated by spaces.
14
Program 4: prints separate digits (3)
• Provide a comment at the beginning of the program to explain
the purpose of the program, along with your name and the date.
• Save your program to the file SeparateDigits.py
• Run your program.
15
Appendix A Variable Names
(Python For Everyone, Section 2.1.3)
A few simple rules:
•
Names must start with a letter or the underscore (_) character.
The remaining characters must be letters, numbers, or
underscores.
•
Other symbols such as ? or % cannot be used, and spaces are
not permitted inside names.
•
Names are case sensitive.
•
Python reserved words cannot be used.
Recommended practice:
•
•
•
Use descriptive names for variables.
Begin names of variables with a lowercase letter.
Name of a variable that is all uppercase indicates a constant,
that is the value of the variable does not change.
16