Introduction to Programming

Similar documents
Introduction to Programming

Introduction to Programming

Introduction to Programming

Introduction to Programming

Introduction to Programming

Introduction to Programming

Introduction to Programming

Arithmetic. 2.2.l Basic Arithmetic Operations. 2.2 Arithmetic 37

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Chapter 4 Section 2 Operations on Decimals

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

6th Grade Arithmetic (with QuickTables)

Math Glossary Numbers and Arithmetic

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

CT 229 Java Syntax Continued

Programming for Engineers in Python. Autumn

CS1110 Lab 6 (Mar 17-18, 2015)

1.- DECIMAL PLACE VALUE: tenths, hundredths, thousandths. 1.1 Ordering decimals. 1.2 Rounding CALCULATIONS. 2.- ADDITION AND SUBTRACTION OF DECIMALS

Introduction to Programming

MATLAB INTRODUCTION. Risk analysis lab Ceffer Attila. PhD student BUTE Department Of Networked Systems and Services

Introduction to FORTRAN

Fundamentals: Expressions and Assignment

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Python Numbers. Learning Outcomes 9/19/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17

Introduction to Engineering gii

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Chapter 7 Arithmetic

FUNDAMENTAL ARITHMETIC

Excel Functions INTRODUCTION COUNT AND COUNTIF FUNCTIONS

Chapter 1 Introduction to MATLAB

Methods CSC 121 Fall 2014 Howard Rosenthal

Unit 1: Numeration I Can Statements

Introduction to Matlab

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Scott Foresman Investigations in Number, Data, and Space Content Scope & Sequence Correlated to Academic Language Notebooks The Language of Math

>>> * *(25**0.16) *10*(25**0.16)

Number Theory and Proof Methods

HOW TO DIVIDE: MCC6.NS.2 Fluently divide multi-digit numbers using the standard algorithm. WORD DEFINITION IN YOUR WORDS EXAMPLE

A Walkthrough of the Learning Aids

Programming for Engineers in Python. Recitation 1

The Hyderabad Public School, Begumpet, Hyderabad, A.P

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Math 085 Final Exam Review

VBScript: Math Functions

Software and Programming 1

Week One: Introduction A SHORT INTRODUCTION TO HARDWARE, SOFTWARE, AND ALGORITHM DEVELOPMENT

Chapter 5 DECIMAL NOTATION

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Downloaded from Chapter 2. Functions

MATH WIZARD USER GUIDE. Page 1 of 15. Created by Landorocks 2007 Teacher Created Resources, Inc. TCR1234 Math Wizard

Florida Math 0018 Correlation of the ALEKS course Florida Math 0018 to the Florida Mathematics Competencies - Lower

Introduction to MATLAB

UNIT 6 OPERATIONS WITH DECIMALS

Chapter Procedural Abstraction and Functions That Return a Value. Overview. Top-Down Design. Benefits of Top Down Design.

Chapter 4. Procedural Abstraction and Functions That Return a Value

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

CHAPTER 4: DECIMALS. Image from Microsoft Office Clip Art CHAPTER 4 CONTENTS

Variable and Data Type I

Introduction to Python, Cplex and Gurobi

Summer Review for Students entering Algebra

Volumes 1 and 2. Grade 5. Academic Standards in Mathematics. Minnesota. Grade 5. Number & Operation

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

MATLAB NOTES. Matlab designed for numerical computing. Strongly oriented towards use of arrays, one and two dimensional.

Lesson 1: THE DECIMAL SYSTEM

Functions. Systems Programming Concepts

CS177 Python Programming. Recitation 2 - Computing with Numbers

Introduction to Functional Programming

Methods CSC 121 Spring 2017 Howard Rosenthal

ABE Math TABE Modules 1-10

Built-in Types of Data

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

CSCI 121: Anatomy of a Python Script

Variable and Data Type 2

50 MATHCOUNTS LECTURES (6) OPERATIONS WITH DECIMALS

Introduction to the course and basic programming concepts

Lab Instructor : Jean Lai

C++ Programming: From Problem Analysis to Program Design, Third Edition

Unit. 8 Days : August 24 September 2

Computer Programming in MATLAB

Methods CSC 121 Fall 2016 Howard Rosenthal

MS RtI Tier 3. Curriculum (107 topics + 91 additional topics)

CS 1803 Individual Homework 1 Python Practice Due: Wednesday, January 26th, before 6 PM Out of 100 points

Advanced features of the Calculate signal tool

Product Price Formula extension for Magento2. User Guide

Year 5 PROMPT sheet. Negative numbers 4 7 = -3. l l l l l l l l l Place value in numbers to 1million = 4

Number and Operation Standard #1. Divide multi- digit numbers; solve real- world and mathematical problems using arithmetic.

CIS133J. Working with Numbers in Java

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

List Comprehensions and Simulations

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Visual Basic for Applications

Excel Lesson 3 USING FORMULAS & FUNCTIONS

Supporting our children to aim high!

Transcription:

Introduction to Programming Python Lab 3: Arithmetic PythonLab3 lecture slides.ppt 26 January 2018 Ping Brennan (p.brennan@bbk.ac.uk) 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 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. 2

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. 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 1. Type PseudoCode.py 2. 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 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. 5

Program PseudoCode.py calculates cost of a car (2) Problem solving 1. Declare 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, such as annualmilesdriven = 15000 fuelefficiency = 20 2. Convert the pseudo code below into a sequence of Python assignment statements in order to calculate the total cost. Create four variables (e.g. annualfuelconsumed, annualfuelcost, etc.) to hold the resulting values of the arithmetic expressions in the 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 3. Print out the string "Total cost: " followed by the numerical value of the total cost. 6

Program PseudoCode.py calculates cost of a car (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 the program to the file PseudoCode.py Run your program. 7

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. i. The sum ii. The difference iii. iv. The product (that is, the multiplication of the values in the two variables). The average v. The absolute value of the difference vi. The maximum vii. The minimum 8

Program IntegerCalculations.py shows integer calculations (2) Problem solving convert the steps below into a sequence of Python statements. 1. Create two variables x and y, and assign integer values to both. 2. Calculate and print the sum, the difference, the product and the average of the values in the two variables. Break this task down into four separate print statements. For example, print("the sum of the two integers is", x+y) 3. 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)) 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 9

Program IntegerCalculations.py shows integer calculations (3) 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. 10

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

Program Rectangle.py prints properties of Rectangles (2) Problem solving convert the steps below into a sequence of Python statements. 1. Create two variables a and b, and assign integer values to both. For example, a = 4 2. 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) 3. Calculate and print the perimeter of the rectangle which is the sum of the lengths of the sides. 4. Calculate and print the length of a diagonal of the rectangle. First find the square of the diagonal which is given by 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 code at the beginning of your program file. 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. 12

Program Rectangle.py prints properties of Rectangles (3) 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 slide 12. 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. 13

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 14

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

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

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