1 / 25

Few More Math Operators

Few More Math Operators. Just a couple of more …. 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

ernestiner
Télécharger la présentation

Few More Math Operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 = ( (5 + 10 + 20) / 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 10 * x * y ( 3 ) ( 12 ) 3 * 12 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 = 5 + 2 / 7 \ + 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

  8. Exponents • You can raise any number to a power by using the “ ** ” operator • Example: 24 2 ** 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 # 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 Instructions: format ( number , “ , . 2 f ” ) Examples: format ( 100000/7 , “,.2f” ) The result: 14,285.71

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

  24. I Woke Up in a New Bugatti

  25. Compounded Interest

More Related