Presentation is loading. Please wait.

Presentation is loading. Please wait.

Math operations 9/19/16.

Similar presentations


Presentation on theme: "Math operations 9/19/16."— Presentation transcript:

1 Math operations 9/19/16

2 Math operations You can add, subtract, multiply, divide numbers like this: addition = subtraction = 108 – 204 multiplication = 108 * 1.5 division = 108 / 9 You try it: Set the variable count_to equal to the sum of two big numbers Then print count_to

3 Exponentiation All that math can be done on a calculator, so why use Python? Because you can combine math with other data types (e.g. variables, strings, and booleans…which we will get to) and commands to create useful programs. Calculators just stick to numbers. Now let’s work with exponents. eight = 2 ** 3 In the above example, we create a new variable called eight, and set it to 8, or the result of 2 to the power to 3 (2^3) Notice that we use ** instead of * or the multiplication operator. You try it: Create a new variable and use exponents to set that variable equation to Try raising 10 to the power of 2.

4 Modulo Our final operator is modulo. Modulo returns the remainder from a division. So, if you type 3 % 2, it will return 1, because 2 goes into 3 evenly once, with 1 left over. You try it: use modulo to set a variable equal to 1. You can use any two numbers that will leave a remainder of 1 to do this.

5 Useful math operators Operator Description Example Evaluates to +
Addition 7+3 10 - Subtraction 7-3 4 * Multiplication 7*3 21 / Division (True) 7/3 // Division (Integer) 7//3 2 % Modulus 7%3 1

6 More about variables Recap: a variable is a way to label and access information. Instead of having to know exactly where in the computers memory something is store, you use a variable to get at it. name = “Larry” This line is called an assignment statement. Technically, an assignment statement stores the value on the right side of the equal sign into your computers memory while the variable on the left side only refers to the value

7 Using Variables Once the variable has been created, it refers to some value. The convenience of the variable is that it can be used just like the value to which it refers. print (name) Prints the string “Larry” just like the statement print (“Larry”) does. And the line print (“Hi, “, name) Prints the string “Hi” followed by a space, followed by “Larry”

8 You try it Write a single-line comment on line 1. It can be anything! (Make sure it starts with a #) Set a variable python equal to 1.234 Set a second variable to monty_python equal to python squared Print monty_python

9 The Meal Now let's apply the concepts from the previous section to a real world example. You've finished eating at a restaurant, and received this bill: Cost of meal: $44.50 Restaurant tax: 6.75% Tip: 15% You'll apply the tip to the overall cost of the meal (including tax).

10 Instructions First lets declare the variable meal and assign it to 44.50

11 The Tax Now let's create a variable for the tax percentage.
The tax on your receipt is 6.75%. You'll have to divide 6.75 by 100 in order to get the decimal form of the percentage.

12 Instructions Create the variable tax and set it equal to the decimal value of 6.75%

13 The Tip You received good service, so you'd like to leave a 15% tip on top of the cost of the meal, including tax. Before we compute the tip for your bill, let's set a variable for the tip. Again, we need to get the decimal form of the tip, so we divide by 100.

14 Instructions On the next line, set the variable tip to the decimal value of 15%

15 Reassign in a Single Line
We've got the three variables we need to perform our calculation, and we know some arithmetic operators that can help us out. We saw in last week’s lesson that we can reassign variables. For example, we could say spam = 7 and then later change out minds and say spam = 3

16 Instructions On the next line, reassign meal to the value of itself + itself * tax. And yes, your allowed to reassign a variable in terms of itself. We’re only calculating the cost of the meal and tax here. We will get to the tip next.

17 The Total Instructions
Now that meal has the cost of the food plus tax, let’s introduce a new variable on the next line, total, equal to meal +meal *tip Instructions Assign the variable total to the sum of meal + meal * tip on the next line. Now you have the total cost of the meal. Then add: print("%.2f" % total) The code on the end formats and prints to the console the value of total with exactly two numbers after the decimal. (We'll learn about string formatting, the console, and print in the next unit!)


Download ppt "Math operations 9/19/16."

Similar presentations


Ads by Google