AOIT Introduction to Programming Unit 2, Lesson 6 Arithmetic Operators and Operator Precedence...
-
Upload
evelyn-booker -
Category
Documents
-
view
213 -
download
0
description
Transcript of AOIT Introduction to Programming Unit 2, Lesson 6 Arithmetic Operators and Operator Precedence...
![Page 1: AOIT Introduction to Programming Unit 2, Lesson 6 Arithmetic Operators and Operator Precedence Copyright © 2009–2012 National Academy Foundation. All rights.](https://reader036.fdocuments.us/reader036/viewer/2022081901/5a4d1b667f8b9ab0599b0b0b/html5/thumbnails/1.jpg)
AOITIntroduction to Programming
Unit 2, Lesson 6
Arithmetic Operators and Operator Precedence
Copyright © 2009–2012 National Academy Foundation. All rights reserved.
![Page 2: AOIT Introduction to Programming Unit 2, Lesson 6 Arithmetic Operators and Operator Precedence Copyright © 2009–2012 National Academy Foundation. All rights.](https://reader036.fdocuments.us/reader036/viewer/2022081901/5a4d1b667f8b9ab0599b0b0b/html5/thumbnails/2.jpg)
Python uses common arithmetic operators with integers
Operator Symbol Example Evaluates To...Addition + 3+2 5Subtraction - 3-2Multiplication * 3*2Division / 2/3Exponentiation ** 3**2Remainder(modulo)
% 27%5
What do you think each example evaluates to?
Note: This last example calculates the remainder left after 27 is divided by 5.
![Page 3: AOIT Introduction to Programming Unit 2, Lesson 6 Arithmetic Operators and Operator Precedence Copyright © 2009–2012 National Academy Foundation. All rights.](https://reader036.fdocuments.us/reader036/viewer/2022081901/5a4d1b667f8b9ab0599b0b0b/html5/thumbnails/3.jpg)
Python uses the same operators with floating point
Operator Symbol Example Evaluates To...Addition + 3.0+2.0 5.0Subtraction - 3.0-2.0Multiplication * 3.0*2.0Division / 2.0/3.0Exponentiation ** 3.0**2.0Remainder(modulo)
% 27.0%5.0
What do you think each example evaluates to?
![Page 4: AOIT Introduction to Programming Unit 2, Lesson 6 Arithmetic Operators and Operator Precedence Copyright © 2009–2012 National Academy Foundation. All rights.](https://reader036.fdocuments.us/reader036/viewer/2022081901/5a4d1b667f8b9ab0599b0b0b/html5/thumbnails/4.jpg)
Special notation is used for large floating-point numbers
If a floating-point calculation gets large enough, the result is returned in exponential notation:
2.0**100=1.26765060002282294e+30
In scientific notation this would be: 1.26765060002282294x10^30
![Page 5: AOIT Introduction to Programming Unit 2, Lesson 6 Arithmetic Operators and Operator Precedence Copyright © 2009–2012 National Academy Foundation. All rights.](https://reader036.fdocuments.us/reader036/viewer/2022081901/5a4d1b667f8b9ab0599b0b0b/html5/thumbnails/5.jpg)
Python observes operator precedence rules
By default Python evaluates operations in the same order used in arithmetic: 1. Parentheses (used to group operations in expressions) 2. Exponentiation3. Multiplication4. Division (and Remainder)5. Addition6. Subtraction
This is sometimes called the PEMDAS rule. Do you see why?
(evaluated at the same level, left to right)
(evaluated at the same level, left to right)
(multiple instances are evaluated right to left)
![Page 6: AOIT Introduction to Programming Unit 2, Lesson 6 Arithmetic Operators and Operator Precedence Copyright © 2009–2012 National Academy Foundation. All rights.](https://reader036.fdocuments.us/reader036/viewer/2022081901/5a4d1b667f8b9ab0599b0b0b/html5/thumbnails/6.jpg)
Here are some PEMDAS examples
1.0 + 2.0 / 3.0 = 1.6666666666666665
2 ** 3 + 10 = 18
2 ** (3 + 10) = 8192
Which operation was done first?
Which operation was done first?
Which operation was done first?
![Page 7: AOIT Introduction to Programming Unit 2, Lesson 6 Arithmetic Operators and Operator Precedence Copyright © 2009–2012 National Academy Foundation. All rights.](https://reader036.fdocuments.us/reader036/viewer/2022081901/5a4d1b667f8b9ab0599b0b0b/html5/thumbnails/7.jpg)
In expressions with multiple operations, it’s better to use parentheses to ensure the result you want
2 * 6 - 4 / 2 = ???
(2 * (6 - 4)) / 2 = ???
What is the answer?
What is the answer?
![Page 8: AOIT Introduction to Programming Unit 2, Lesson 6 Arithmetic Operators and Operator Precedence Copyright © 2009–2012 National Academy Foundation. All rights.](https://reader036.fdocuments.us/reader036/viewer/2022081901/5a4d1b667f8b9ab0599b0b0b/html5/thumbnails/8.jpg)
Operators are the building blocks of Python expressions
• Operators are used in virtually all numeric expressions.
• The PEMDAS rule tells you how Python will evaluate arithmetic expressions by default.
• To be sure Python evaluates your expressions in the way you intend, use parentheses to isolate operations.