Presentation is loading. Please wait.

Presentation is loading. Please wait.

Few More Math Operators

Similar presentations


Presentation on theme: "Few More Math Operators"— Presentation transcript:

1 Few More Math Operators
Just a couple of more …

2 Practice – the Metro Card
Write a program that asks the value of their current Metro Card If each ride costs $3.75, compute: The number of rides they have left The amount of money they have “left over” after previously stated given rides The amount of money they need to add to round out an even number of rides

3 Order of Operations Python follows the order of operations (PEMDAS)
You can use parentheses inside your math expressions to group operations Example: x = ( ( ) / 60 ) * 100

4 PEMDAS Multiplication/Division, Addition/Subtraction must be done in order that it shows up, not interchangeable 3 * 4 / 2 * 5 (3 * 4) / (2 * 5)

5 Converting Math Formulas into Programming Statements
Most math formulas need to be converted into a format that Python can understand Examples: 10 x y * x * y ( 3 ) ( 12 ) * 12 𝑦= 3𝑥 y = 3 * x / 2

6 Line Continuation Sometimes expressions can get to be very long
You can use the “ \ ” symbol to indicate to Python that you’d like to continue the expression onto another line ** Example: x = / \ + 8 – 12 This also works for the print ( ) function

7 Mixed Type Expressions
Python allows you to mix integers and floats when performing calculations The result of a mixed-type expression will evaluate based on the operands used in the expression Operand 1 Operand 2 Result int float

8 Exponents You can raise any number to a power by using the “ ** ” operator Example:  ** 4

9 Division Operations Python contains two different division operators
The “/” operator is used to calculate the floating-point result of a division operation The “//” operator is used to calculate the integer result of a division operation, it will throw away the remainder. *** This operation will always round DOWN. Examples: print ( 5 // 2 ) # 2 print ( -5 // 2 ) # -3

10 Practice: Time Calculations
Ask the user to input a number of seconds as a whole number. Then express the time value inputted as combination of minutes and seconds >> Enter seconds: 110 That’s 1 minute and 50 seconds!

11 Practice: Time Calculations
There’s actually an operator symbol in Python for what we just did. Realize, that this will happen a lot. Python has functions and commands that condense the process of a common algorithm. Let’s take a look …

12 Remainder Operator (modulo)
The modulo operator “ % ” returns the remainder portion of a division operation This is basically the opposite of the “ // ” operator Examples: 5 / # 2.5 5 // 2 # 2 5 % 2 # 1 (remainder from divisor of 2)

13 Practice: Time Calculations
Now extend this program to include the number of hours >> Enter seconds: 12074 That’s 3 hours, 21 minutes and 14 seconds!

14 Escape Key “\” The backslash ( “ \ ” ) is known as an escape key in Python It tells Python that the character directly following the backslash will not function in it’s regular nature Example: print (“This class is “Awesome!””) #error! print (“This class is \“Awesome!\””) >> This class is “Awesome!”

15 Examples of the Escape Key
We can use the escape key in various ways: print(“\””) # this will print a quotation mark print(“\n”) # this will print a new line print(“\t”) # this will print an indented tab print(“\\”) # this will print out a back slash

16 Examples of the Escape Key
print (“We saw this \n this will print a new line”) >> We saw this this will print a new line

17 Examples of the Escape Key
print (“We saw this \t this will print a tab”) >> We saw this this will print a tab

18 Examples of the Escape Key
print (“What if we want an actual backslash \\”) >> What if we want an actual back slash \

19 Practice: O Christmas Tree
Using a single print statement, try writing a program that prints out the image of a Christmas Tree We want this: >> tree /\ / \ / \ I I

20 Examples of the Escape Key
print (""" /\\ \n / \\ \n/ \\ \n | | """) >> tree /\ / \ / \ I I

21 format( ) Function This is a bit premature, but for the sake of your homework, we can use the format( ) function. This function allows us to format numbers to as many decimal places as we’d like. It also allows us to insert a comma every three digits, as there are in the real number system (i.e. 12,345,678) The format function must receive two arguments: The number it is formatting (for now we’ll always pass floats) The instructions for formatting

22 format( ) Function format ( 100000/7 , “,.2f” ) The result: 14,285.71
Instructions: format ( number , “ , . 2 f ” ) Examples: format ( /7 , “,.2f” ) The result: 14,285.71

23 format( ) Function print ( format ( /7 , “,.2f” ) ) >> 14, x = format( /7 , “.2f ” ) print( “$” + x ) >> $14,285.71

24 I Woke Up in a New Bugatti

25 Compounded Interest 𝑨= 𝑨 𝒐 𝟏+ 𝒓 𝒏 𝒏𝒕
𝑨= 𝑨 𝒐 𝟏+ 𝒓 𝒏 𝒏𝒕 𝑨=𝒇𝒊𝒏𝒂𝒍 𝒑𝒓𝒊𝒄𝒆, 𝒂𝒇𝒕𝒆𝒓 𝒂𝒄𝒄𝒓𝒖𝒆𝒅 𝒊𝒏𝒕𝒆𝒓𝒆𝒔𝒕 𝑨 𝒐 =𝒊𝒏𝒊𝒕𝒊𝒂𝒍 𝒑𝒓𝒊𝒄𝒆, 𝒃𝒆𝒇𝒐𝒓𝒆 𝒊𝒏𝒕𝒆𝒓𝒆𝒔𝒕 𝒔𝒕𝒊𝒄𝒌𝒆𝒓 𝒑𝒓𝒊𝒄𝒆 𝒓=𝒓𝒂𝒕𝒆 𝒐𝒇 𝒊𝒏𝒕𝒆𝒓𝒆𝒔𝒕, % 𝑨𝑷𝑹 (𝒎𝒖𝒔𝒕 𝒃𝒆 𝒊𝒏 𝒅𝒆𝒄𝒊𝒎𝒂𝒍 𝒇𝒐𝒓𝒎) 𝒏=𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒕𝒊𝒎𝒆𝒔 𝒄𝒐𝒎𝒑𝒐𝒖𝒏𝒅𝒆𝒅/ 𝒑𝒂𝒊𝒅 𝒑𝒆𝒓 𝒚𝒆𝒂𝒓 (𝒇𝒐𝒓 𝒐𝒖𝒓 𝒑𝒖𝒓𝒑𝒐𝒔𝒆𝒔, 𝒏 𝒘𝒊𝒍𝒍 𝒂𝒍𝒘𝒂𝒚𝒔 𝒆𝒒𝒖𝒂𝒍 𝟏𝟐) 𝒕=𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒚𝒆𝒂𝒓𝒔 𝒇𝒐𝒓 𝒕𝒉𝒆 𝒍𝒐𝒂𝒏


Download ppt "Few More Math Operators"

Similar presentations


Ads by Google