Publications: 52 | Followers: 1

Few More Math Operators - River Dell Regional School District

Publish on Category: Birds 0

Few More Math Operators
Just a couple of more …
Practice – the Metro Card
Write a program that asks the value of their current Metro CardIf each ride costs $3.75, compute:The number of rides they have leftThe amount of money they have “left over” after previously stated given ridesThe amount of money they need to add to round out an even number of rides
Order of Operations
Python follows the order of operations (PEMDAS)You can use parentheses inside your math expressions to group operationsExample:x = ( (5 + 10 + 20) / 60 ) * 100
Multiplication/Division, Addition/Subtraction must be done in order that it shows up, not interchangeable3 * 4 / 2 * 5(3 * 4) / (2 * 5)
Converting Math Formulas into Programming Statements
Line Continuation
Sometimes expressions can get to be very longYou 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 – 12This also works for the print ( ) function
Mixed Type Expressions
Python allows you to mix integers and floats when performing calculationsThe result of a mixed-type expression will evaluate based on the operands used in the expression
You can raise any number to a power by using the “ ** ” operatorExample:24 2 ** 4
Division Operations
Python contains two different division operatorsThe “/” operator is used to calculate the floating-point result of a division operationThe “//” 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 )# 2print ( -5 // 2 )# -3
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:110That’s 1 minute and 50 seconds!
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 …
Remainder Operator (modulo)
The modulo operator “ % ” returns the remainder portion of a division operationThis is basically the opposite of the “ // ” operatorExamples:5 / 2 # 2.55 // 2 # 25 % 2 # 1 (remainder from divisor of 2)
Practice: Time Calculations
Now extend this program to include the number of hours>> Enter seconds:12074That’s 3 hours, 21 minutes and 14 seconds!
Escape Key “\”
The backslash ( “ \ ” ) is known as an escape key in PythonIt tells Python that the character directly following the backslash will not function in it’s regular natureExample:print(“This class is “Awesome!””)#error!print(“This class is\“Awesome!\””)>> This class is “Awesome!”
Examples of the Escape Key
We can use the escape key in various ways:print(“\””) # this will print a quotation markprint(“\n”) # this will print a new lineprint(“\t”) # this will print an indented tabprint(“\\”) # this will print out a back slash
Examples of the Escape Key
print(“We saw this\nthis will print a new line”)>> We saw thisthis will print a new line
Examples of the Escape Key
print(“We saw this\tthis will print a tab”)>> We saw this this will print a tab
Examples of the Escape Key
print(“What if we want an actual backslash \\”)>> What if we want an actual back slash \
Practice: O Christmas Tree
Using a single print statement, try writing a program that prints out the image of a Christmas TreeWe want this:>> tree /\/ \/ \II
Examples of the Escape Key
print(""" /\\ \n/\\ \n/\\ \n| | """)>> tree /\/ \/ \II
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 receivetwoarguments:The number it is formatting (for now we’ll always pass floats)The instructions for formatting
format( ) Function
Instructions:format (number,“ , . 2 f ”)Examples:format (100000/7,“,.2f”)The result: 14,285.71
format( ) Function
print ( format (100000/7,“,.2f”) )>>14,285.71x = format(100000/7,“.2f ”)print(“$”+x)>>$14,285.71
I Woke Up in a New Bugatti
Compounded Interest





Make amazing presentation for free
Few More Math Operators - River Dell Regional School District