Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Python November 14, Unit 7. Python Hello world, in class.
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Introduction to Python
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #001 (January 9, 2015)
Chapter 3.  Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Input, Output, and Processing
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
CS 177 Week 4 Recitation Slides Variables, Files and Functions.
Java Data Types Assignment and Simple Arithmetic.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏ Sec 9-7 Web Design.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
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.
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
Debugging, Escape Command, More Math. It’s your birthday!  Write a program that asks the user for their name and their age.  Figure out what their birth.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
Formatting Output. Line Endings By now, you’ve noticed that the print( ) function will automatically print out a new line after passing all of the arguments.
Python Let’s get started!.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
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,
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Thinking about programming Intro to Computer Science CS1510 Dr. Sarah Diesburg.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Input, Output and Variables GCSE Computer Science – Python.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Few More Math Operators
Math operations 9/19/16.
Numbers and arithmetic
Lesson 03: Variables and Types
Numbers and Arithmetic
Chapter 2 Basic Computation
Python Let’s get started!.
Lecture 4: Expressions and Variables
Data Types and Conversions, Input from the Keyboard
Formatting Output.
Introduction to Computer Science / Procedural – 67130
Variables, Expressions, and IO
Formatting Output.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Chapter 2 Basic Computation
Few More Math Operators
Math and Data Types Practice Problems
Learning Outcomes –Lesson 4
T. Jumana Abu Shmais – AOU - Riyadh
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
Variables, Data Types & Math
Lecture 4: Expressions and 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.
Presentation transcript:

Math, Data Types

Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //

Expressions  We use operators along with numeric data to create “math expressions” that Python can evaluate on our behalf  However, unless you ask python to output the value of the expression, it will not do so  Example: -writing “5 + 2” in python will not output anything, although the calculation will be done -instead, you must write something like: print ( ) >> 19

Storing results of an expression  You can also store the result of your expression into a variable  Example: answer = print ( ‘the answer to is‘, answer ) >> the answer to is 7

Using variables in math expressions  Math expressions don’t need to be done on only numeric literals  Example: price = sales_tax = 0.07 total = price + price * sales_tax

Data Types  Python needs to know how to set aside memory in your computer based on what kind of information you want to store  There are three basic types of data types that we will be working with:  Strings (character-based data)  Number (floats/integers)  Boolean variables (True/False)

Numeric Data Types  Integers:  Whole numbers that do not contain a decimal point  Abbreviated as “int” in Python  Example: 5, - 5, 100,  Floating Point Numbers:  Numbers that contain a decimal point  Abbreviated as “float” in Python  Example: 5.0, - 5.0, ,

Numeric Data Types  You can store numeric data inside variables and Python will automatically assign it a type according to how it is created  num_1 = 5 # Python recognizes this as an int  num_2 = 4.99 # Python recognizes this a float  Keep in mind that you do can not use separators or symbols when storing numeric data  num_3 = $ 5, # This will cause an error!

Practice  5  5.5  “Hello”  “5.5”   2.0  “$2.99”

Practice  5 # int  5.5 # float  “Hello” # string  “5.5” # string  # float  2.0 #float  “$2.99” # string

Strictly Typed Languages  Python is not a strictly typed language, which means that you don’t need to pre-declare what kind of data your variables will be holding, it will automatically assign it for you Loosely TypedStrictly Typed PythonC PHPC++ JavaScriptJava PerlActionScript

Strictly Typed Languages ActionScriptJava var name:String = “Donald”;string name = “Donald” ; var top_speed:Number = 50;int top_speed = 50 ; var gravity:Number = 9.5;float gravity = 9.5 ;

Input and Math expressions  Recall that the input function always returns a string average = input (“what was the average?”) what was the average? 53 # this will be inputted into the variable as a string, “53”

Input and Math expressions average = input (“what was the average?”) new_average = average + 2 # this will cause an error because you are trying to add a string to some numeric data  So, how can we convert our inputted data into a numeric data type that Python supports? (an integer, or a float)

The float( ) and int ( ) functions  The float ( ) and int ( ) functions are data type conversion functions.  They take the passed argument and convert that into a specific data type

The float( ) and int ( ) functions # ask user for monthly salary monthly_salary = input (‘how much do you make a month?’) # convert salary into float monthly_salary_float = float(monthly_salary) # calculate annual salary annual_salary = monthly_salary_float * 12 # print result print ( ‘that means you make’, annual_salary, ‘in a year’)

The float( ) and int ( ) functions # ask user for monthly salary monthly_salary = input (‘how much do you make a month?’) # calculate annual salary annual_salary = monthly_salary_float * 12 # print result print ( ‘that means you make’, annual_salary, ‘in a year’) >> how much do you make a month? 1000 that means you make in a year

Nesting data type conversions  It took us two steps to convert our data, but we can do it in one!  We use a technique called “nesting”  Example: mynum = float ( input ( ‘give me a number!’ ) )

Nesting data type conversions

Practice  Ask the user for two numbers. You can assume they will input floating point numbers.  Compute the following and print it out to the user:  The sum of the numbers  The difference between the numbers  The product of the numbers  The quotient of the first number divided by the second

Practice – the Metro Card  Write a program that asks the value of their current Metro Card  Compute how many rides they have left on their card. Only provide whole number results (you cannot have 3.5 rides left)  HINT: you may have to look up the standard rate for a ride

Practice – The solution to all our problems  Build a program that will allow the user to input the amount that their specific meal cost, and calculate and print out the final amount that they will need to pay  HINT: You will need to ask the user for a few things:  The price of their own meal  The total amount of the check (before tax)  The percent tip they want to leave  The amount of tax for the table  The number of people at the table

Practice – The solution to all our problems Step by step:  You will need to take the total amount of the check and multiply it by the percent tip they want to leave  Then you will need to add that tip amount to the tax (for the whole table)  You will take that number and divide it by the number of people who ate, this will be the amount people need to put on top of their meal for “tip and tax”  Add this value to the amount of the user’s specific meal