250 likes | 262 Views
Create Python program to calculate remaining rides, money left, and required top-up for Metro Card users. Understand operators, order of operations, line continuation, types of expressions, exponents, division, and modulo operators. Practice time calculations and use escape key in Python. Extend program to include hours. Explore formatting function for number precision and styles. Practice making a Christmas tree image using a single print statement.
E N D
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 • The 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 operations • Example: x = ( (5 + 10 + 20) / 60 ) * 100
PEMDAS • Multiplication/Division, Addition/Subtraction must be done in order that it shows up, not interchangeable 3 * 4 / 2 * 5 (3 * 4) / (2 * 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
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
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
Exponents • You can raise any number to a power by using the “ ** ” operator • Example: 24 2 ** 4
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
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!
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 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)
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!
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!”
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
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
Examples of the Escape Key print (“We saw this \t this 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 Tree • We want this: >> tree /\ / \ / \ I I
Examples of the Escape Key print (""" /\\ \n / \\ \n/ \\ \n | | """) >> tree /\ / \ / \ I I
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
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.71 x = format( 100000/7 , “.2f ” ) print( “$” + x ) >> $14,285.71