Practice the Metro Card Write a program that asks the value of their current Metro Card If each ride costs 375 compute The number of rides they have left The amount of money they have left over after previously stated given rides ID: 745572
Download Presentation The PPT/PDF document "Few More Math Operators Just a couple of..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Few More Math Operators
Just a couple of more … Slide2
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 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 Slide3
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 ) * 100Slide4
PEMDAS
Multiplication/Division, Addition/Subtraction must be done in order that it shows up, not interchangeable
3 * 4 / 2 * 5
(3 * 4) / (2 * 5) Slide5
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
Slide6
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 Slide7
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
int
int
float
float
float
float
int
float Slide8
Exponents
You can raise any number to a power by using the “ ** ” operator
Example:
24
2 ** 4Slide9
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 Slide10
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! Slide11
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 … Slide12
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) Slide13
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! Slide14
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!” Slide15
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 Slide16
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 Slide17
Examples of the Escape Key
print
(“We saw this
\t this will print a tab”)>> We saw this this will print a tab Slide18
Examples of the Escape Key
print
(“What if we want an actual backslash \\”)
>> What if we want an actual back slash \ Slide19
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
Slide20
Examples of the Escape Key
print
(""" /
\\ \n / \\ \n
/
\\ \n
| | """)
>> tree /\
/ \
/ \
I
I Slide21
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 Slide22
format( ) Function
Instructions:
format (
number , “ , . 2 f ” )
Examples:
format (
100000/7
,
“,.2f”
)
The result: 14,285.71 Slide23
format( ) Function
print ( format (
100000/7
, “,.2f” ) )>>
14,285.71
x = format(
100000/7
,
“.2f ”
)
print( “$” + x )
>>
$14,285.71 Slide24
I Woke Up in a New BugattiSlide25
Compounded Interest