Numbers and Arithmetic

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Python November 14, Unit 7. Python Hello world, in class.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
Python Programming Using Variables and input. Objectives We’re learning to use basic knowledge of variables combined with user input. Outcomes Continue.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
ITEC 109 Lecture 7 Operations. Review Variables / Methods Functions Assignments –Purpose? –What provides the data? –What stores the data? –What type of.
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.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
ORDER OF OPERATIONS.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
1 + 2 × 3 – 4 Division / Multiplication × 3 – 4 Division / Multiplication Addition / Subtraction.
GCSE Computing: Programming GCSE Programming Remembering Python.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Input, Output and Variables GCSE Computer Science – Python.
Getting Started 27 June 2016 Week 1, Day 1. Who am I? Name: Christopher Little Town: Cambridge Eats: Chips.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Numbers and arithmetic
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Lesson 03: Variables and Types
Chapter 2 Variables.
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
Chapter 2 Basic Computation
CMSC201 Computer Science I for Majors Lecture 04 – Expressions
Intro to CS Nov 2, 2015.
Data Types and Conversions, Input from the Keyboard
Design & Technology Grade 7 Python
Variables, Expressions, and IO
Computer Science 3 Hobart College
Introduction to C++ October 2, 2017.
To write a Python program, you first need to open Pyscripter
Introduction to Programming
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Chapter 2 Basic Computation
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
G7 programing language Teacher / Shamsa Hassan Alhassouni.
And now for something completely different . . .
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Learning Outcomes –Lesson 4
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.
Margaret Derrington KCL Easter 2014
Variables and Expressions
Feedback Pace Different ideas for delivery Debugging strategies
CMSC201 Computer Science I for Majors Lecture 04 – Expressions
Lesson 03: Variables and Types
GCSE Computing Lesson 6.
Core Objects, Variables, Input, and Output
Introduction to Programming with Python
Variables In today’s lesson we will look at: what a variable is
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Basic Lessons 5 & 6 Mr. Kalmes.
Building Java Programs
Unit 3: Variables in Java
Chapter 2 Variables.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
ORDER OF OPERATIONS.
Text Copyright (c) 2017 by Dr. E. Horvath
COMPUTING.
H446 Computer Science.
Data Types and Expressions
Introduction to Python
Python Creating a calculator.
Getting Started in Python
Presentation transcript:

Numbers and Arithmetic Introduction to Python

Learning Objectives All to understand how to use assignment statements correctly Most to use the int, float and round functions Some to write a program involving input, calculation and output

What does this program do? Look at the code below and describe what each line does #Animal animal = input("Enter the name of an animal: ") description = input("How would you describe a " + animal + "? ") print ("A",animal,"is",description) print("You will find it under",animal[0],"in the dictionary")

Data Types String holds alphanumeric data as text Integer holds whole numbers Float holds numbers with a decimal point Boolean holds either ‘True’ or ‘False’

Defining Variable Data Types Python automatically assigns a data type to a variable You can override this to manually define or change the data type using: str() str(animal_name) int() int(goals_scored) float() float(share_price) This is called Casting

Choosing Data Types Data Example Data Type Code Age 14 Integer int(age) Shoe Size 6.5 Float Price 14.99 Postcode Height Quantity in Stock Telephone Number

The Importance of Data Types Create a variable called mynumber and allow the user to input a value to assign to it mynumber = input("Enter a number") print (mynumber + mynumber) What do you think happens? Try it! Convert the data type: mynumber = int(mynumber) Where would you put this line?

Assignment Statements Assignment statements assign a value to a variable. If the variable doesn’t already exist, it’s created. Examples: name = "Jo" + " Smith" number = 10 number = input("Enter a number")

BIDMAS The order of arithmetic operations matters. Brackets , Indices, Division and Multiplication, Addition and Subtraction

Arithmetic Operators Arithmetic Operator Example Output Addition + 2 + 10 12 Subtraction - 9 – 6 3 Multiplication * 5 * 4 20 Division / 5 / 2 2.5 Floor Division // 7 // 2 Remainder % 7 % 3 1

Sleep Calculator Create a program to calculate how many hours per week you sleep Try this code and find the problem! hourspernight = input("How many hours per night do you sleep? ") hoursperweek = hourspernight * 7 print ("You sleep",hoursperweek,"hours per week")

Sleep Calculator Add to the problem to find the total number of hours spent sleeping in a month Assume an average of 4.35 weeks per month hourspernight = input("How many hours per night do you sleep? ") hoursperweek = int(hourspernight) * 7 print ("You sleep",hoursperweek,"hours per week") hourspermonth = float(hoursperweek) * 4.35 print ("You sleep",hourspermonth,"hours per month")

Sleep Calculator Now find the equivalent number of days per month you sleep for!

Rounding hourspermonth = 228.37499999999997 round(hourspermonth) = 228 hourspernight = input("How many hours per night do you sleep? ") hoursperweek = int(hourspernight) * 7 hourspermonth = round(hourspermonth) print ("You sleep",hoursperweek,"hours per week") hourspermonth = float(hoursperweek) * 4.35 print (“\nYou sleep",hourspermonth,"hours per month")

***** Hello! ***** Finishing off Display a title for your program when it first runs Calculate the number of days per year spent asleep Include the user’s name to make it more user friendly ***** Hello! *****

Calculating Mobile Phone Costs Create code for an app to do the following: Accept the user’s name and mobile phone network Ask for the number of minutes they have used this month and multiply that by 0.10 per minute Ask for the number of texts that they have sent in a month and multiply that by 0.05 pence per text Display a total bill amount for the month Displays a new total including VAT of 20%

Finally… Complete the plenary questions to show me what you have learnt today. Plenary questions should be completed and are available on the iTeach website