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

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

10 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 can not use separators or symbols when storing numeric data  num_3 = $ 5, 122.39 # This will cause an error!

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

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

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 Concatenation  We are able to use math operators on more than numeric data types. We can “add” and also “multiply” strings.  When we add two strings together, we call this concatenation.  Example: print(“53” + “2”) >> 532

16 Concatenation  This will help us with a problem we ran into previously.  Remember that our print function by nature will add a space between arguments, whenever it recognizes a comma. print (“Average Scores for \“”, class, “\“”) >> Average Scores for “ Intro to Programming ”

17 Concatenation  Now, with the “+” operator, we can literally attach two strings together without any additional spaces. print (“Average Scores for \“” + class + “\“”) >> Average Scores for “Intro to Programming”

18 String Repetition  A similar function is applied when we “multiply” strings with a numeric data type.  The * operator will print out a string the number of times it is being “multiplied by” but in a literal sense. print (“Hello” * 3) >> HelloHelloHello

19 If we do not convert data types … # ask user for monthly salary monthly_salary = input (‘how much do you make a month?’) # calculate annual salary annual_salary = monthly_salary * 12 # print result print ( ‘that means you make’, annual_salary, ‘in a year’) >> how much do you make a month? 100 that means you make 100100100100100100100100100100100100 in a year

20 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

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

22 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: my_num = float ( input ( ‘give me a number!’ ) )

23 Nesting data type conversions

24 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


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

Similar presentations


Ads by Google