Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //"— Presentation transcript:

1 Math, Data Types

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

3 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 (5 + 2 + 9 + 3) >> 19

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

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

6 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)

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

8 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, 122.39 # This will cause an error!

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

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

11 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

12 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 ;

13 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”

14 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)

15 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

16 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’)

17 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 100010001000100010001000100010001000100010001000 in a year

18 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!’ ) )

19 Nesting data type conversions

20 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

21 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

22 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

23 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


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

Similar presentations


Ads by Google