Introduction to Programming

Slides:



Advertisements
Similar presentations
Introduction to Programming Java Lab 3: Variables and Number types 25 January JavaLab3.ppt Ping Brennan
Advertisements

Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Recitation 1 Programming for Engineers in Python.
Python Programming Fundamentals
Introduction to Python
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
Computer Science 101 Introduction to Programming.
Lesson 01: Introduction to Database Software. At the end of this lesson, students should be able to: State the usage of database software. Start a database.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Spreadsheets What is Excel?. Objectives 1. Identify the parts of the Excel Screen 2. Identify the functions of a spreadsheet 3. Identify how spreadsheets.
22 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 1: My First Program 8 January PythonLab1 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 5: Strings and Output 05 February PythonLab5 lecture slides.ppt Ping Brennan
Introduction to Programming
Introduction to Programming Python Lab 2: Variables 15 January PythonLab2 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 6: Relational Operators and Boolean Variables 12 February PythonLab6 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 8: Loops 26 February PythonLab8 lecture slides.ppt Ping Brennan
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
2.5 Another Java Application: Adding Integers
Introduction to Programming
Introduction to Programming
Introduction to Programming
Computer Fundamentals
Introduction to Programming
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Introduction to Programming
Introduction to Programming
Creating a Workbook Part 2
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Programming Funamental slides
Introduction to Programming
Introduction to Programming
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
CS 100: Roadmap to Computing
Introduction to Programming
Introduction to Programming
Microsoft Excel 101.
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Introduction to Programming
Introduction to Programming
Introduction to Programming
Intro to Excel CSCI-150.
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
CS 100: Roadmap to Computing
Presentation transcript:

Introduction to Programming Python Lab 3: Arithmetic PythonLab3 lecture slides.ppt Ping Brennan (p.brennan@bbk.ac.uk) 1 February 2019

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 laboratory, search using the keyword Python and click on IDLE (Python 3.6 64-bit) A window with the title Python 3.6.2 Shell should appear. This window is the Shell.

Getting Started (2) If you are in the ITS laboratory MAL 109, 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, click on Start and then in the box Search programs and files write IDLE. A list will appear. Click on IDLE(Python 3.4 GUI-64 bit). A window with the title Python 3.4.4rc1 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 Untitled should appear. This window is the Editor.

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 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.

Program PseudoCode.py calculates cost of a car Learning objectives Use Python arithmetic operators: Understand arithmetic expressions Use variables to store values of type int or type float Question 2: 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. + - * /

Program PseudoCode.py calculates cost of a car (2) Problem solving Create and initialise five 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, for example, annualMilesDriven = 15000 # stores the annual miles driven fuelEfficiency = 20 # stores the fuel efficiency Convert the pseudo code below into a sequence of Python assignment statements in order to calculate the total cost of a car. Create four variables (e.g. annualFuelConsumed, annualFuelCost, etc.) to hold the resulting values of the arithmetic expressions in the Python statements. 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

Program PseudoCode.py calculates cost of a car (3) Problem solving (continued) Print out the string "Total cost: " followed by the numerical value of the total cost. Provide a comment at the beginning of the program to explain the purpose of the program, together with your name and the date. Save the program to the file PseudoCode.py Run your program.

Program IntegerCalculations.py shows integer calculations Create a new Editor for a new file called IntegerCalculations.py Question 3: Problem statement Write a program that assigns integer values to two variables x and y, and then prints out the following values. The sum The difference The product (that is, the multiplication of the values in the two variables). The average The absolute value of the difference The maximum The minimum

Program IntegerCalculations.py shows integer calculations (2) Problem solving – convert the steps below into a sequence of Python statements. Create two variables x and y, and assign integer values to both. For example, x = 3 Calculate and print the sum, the difference, the product and the average of the values in the two variables. Break down the task for step 2 into four separate print statements. For example, print("The sum of the two integers is", x+y)

Program IntegerCalculations.py shows integer calculations (3) Problem solving (continued) Use the built-in functions in the table below in your program to calculate and print the absolute value of the difference, the maximum and the minimum of the values in the two variables. For example, print("The absolute value of the difference is", abs(x-y)) The Python built-in functions that you can use in your program is listed in the table below. Function Explanation Example abs(x) Returns the absolute value of x. abs(-5) #returns the value 5 max(x, y, z, …) Returns the largest value of the arguments. max(1, 2) #returns the value 2 min(x, y, z, …) Returns the smallest value of the arguments. min(1, 2) #returns the value 1

Program IntegerCalculations.py shows integer calculations (4) The printed output should be in the following style (assuming variable x holds an integer value 3 and variable y holds an integer value 5) The sum of the two integers is 8. Provide a comment at the beginning of the program to explain the purpose of the program, together with your name and the date. Save your program to the file IntegerCalculations.py Run your program.

Program Rectangle.py prints properties of Rectangles Create a new Editor for a new file called Rectangle.py Question 4: 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.

Program Rectangle.py prints properties of Rectangles (2) Problem solving – convert the steps below into a sequence of Python statements. Create two variables a and b, and assign integer values to both. For example, a = 4 Calculate and print the area which is the product of the lengths of the sides. For example, print("The area of the rectangle is", a*b) Calculate and print the perimeter of the rectangle which is the sum of the lengths of the sides.

Program Rectangle.py prints properties of Rectangles (3) Problem solving (continued) Calculate and print the length of a diagonal of the rectangle. First find the square of the diagonal which is equal to the sum of the squares of the sides, and then work out the square root of the resulting value. To use the square root function sqrt(x), place the following import statement at the top of your program code. from math import sqrt The function sqrt is called with the argument x which can be an arithmetic expression, such as a*a + b*b. For example, print(sqrt(a*a + b*b))

Program Rectangle.py prints properties of Rectangles (4) Include in the print out a short description of each number that is printed. An example is shown in step 2 of the ‘Problem solving’ section on page 13. Provide a comment at the beginning of the program to explain the purpose of the program, together with your name and the date. Save your program to the file Rectangle.py Run your program.

Program SeparateDigits.py prints separate digits Create a new Editor for a new file called SeparateDigits.py Question 5: 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 1 6 3 4 8 Learning objective Understand the use of the remainder operator % and the floor division operator // Note that if x and n are integers then: Expression and explanation Example x//n is the quotient on dividing x by n 6//4 #returns the value 1 x%n is the remainder on dividing x by n 5%4 # returns the value 1

Program SeparateDigits.py 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 # find the unit digit from the value stored in x d1 = x % 10 # assigns the integer 8 to d1 # find the tenth digit d2 = (x // 10) % 10 # assigns the integer 4 to d2 # find the hundredth digit d3 = (x // 100) % 10 # assigns the integer 3 to d3 # add a statement below to find the thousandth digit # by using a similar arithmetic expression as above. … Code to be added here by you … # add a statement below to find the ten thousandth digit # add a print statement below to output the individual # digits separated by spaces. … Code to be added here by you…

Program SeparateDigits.py prints separate digits (3) Provide a comment at the beginning of the program to explain the purpose of the program, together with your name and the date. Save your program to the file SeparateDigits.py Run your program.

Supplementary Questions for Private Study The laboratory worksheet contains supplementary questions 6.1, 6.2 and 6.3 for private study. You are encouraged to complete the supplementary questions at home, or in the laboratory if you have time after completing questions 2 to 5.