Python Operator Precedence PDF
Document Details
Uploaded by HelpfulWaterfall2594
Tags
Summary
This document provides an explanation of operator precedence in Python. The document includes examples, and a table demonstrating the order of operations for different operators. It also explains the difference in operator precedence between programming languages.
Full Transcript
Cont. of Operators Operator Precedence Outline Recall Operator Precedence Exercises Notes on SQ2 Algorithmic Thinking For us to better understand the concepts of definiteness and sequential, let’s talk about how Python evaluates expressions This also is a continuation of what w...
Cont. of Operators Operator Precedence Outline Recall Operator Precedence Exercises Notes on SQ2 Algorithmic Thinking For us to better understand the concepts of definiteness and sequential, let’s talk about how Python evaluates expressions This also is a continuation of what was discussed last meeting Data types Operators Recall: Operators We discussed common Operator Symbol arithmetic operators Negation - in Python and how Addition + they are used Subtraction - Multiplication * Division / Integer Division (Floor Division) // Modulo (Remainder) % Quick Tip for Modulo For those who still aren’t 100% sure about modulo, think of the operation as being asked the following… If it were 3 o’clock and you were tasked to add 11 hours, what would the current time be? Quick Tip for Modulo For those who still aren’t 100% sure about modulo, think of the operation as being asked the following… If it were 3 o’clock and you were tasked to add 12 hours, what would the current time be? Quick Tip for Modulo For those who still aren’t 100% sure about modulo, think of the operation as being asked the following… If it were 3 o’clock and you were tasked to add 13 hours, what would the current time be? Manually computing for the following and then run them in Python to see if you got them correct a) 3 + -4 a) 7 b) 2 * 4.0 b) 8.0 c) 5 / 2 c) 2.5 d) 10 // 3 d) 3 e) 16 % 3 e) 1 Operators in Python We tackled simple Operator Symbol expressions Negation - Valid combinations of any number of Addition + operators and operands Subtraction - But we’re probably not Multiplication * going to just deal with Division / 2 operands at a time… Integer Division (Floor Division) // Modulo (Remainder) % Operator Precedence in Python As we’re going to deal Operator Symbol with more complex Negation - expressions, its Addition + important to know Subtraction - how Python Multiplication * processes expressions Division / step-by-step Integer Division (Floor Division) // Modulo (Remainder) % Operator Precedence in Python Let’s talk about MDAS (or PEMDAS) A classic acronym mnemonic that helps with remembering operator precedence … so how does this work again? What’s the answer here? 16? 1? At least according to Python, the answer is 16 The answers here aren’t wrong – they just follow different algorithms Technically speaking, the answer here should be 9 because we should be treating multiplication and division equally. Image source: https://i.imgur.com/vNUWVZM.jpg%20 Finding the error is easy because we show each step of our solution [state of the algorithm] Left Calculator Right Calculator 6 / 2 * (2 + 1) 6 / 2 * (2 + 1) 6 / [2 * 3] [6 / 2] * 3 [6 / 6] [3 * 3] 1 9 According to Python rules, error was here! Operator Precedence The best way to view MDAS is to view the operators in tiers * / Multiplication and Division occupy the same tier + - Addition and Subtraction meanwhile occupy the same lower tier Operator Precedence If there are multiple operators in an expression that belong in the same tier, * / they are performed left to right + - In which tier does modulo and integer division belong? Operator Precedence If there are multiple operators in an expression that belong in the same tier, % * / // they are performed left to right + - Being variants of division, in the same tier as multiplication and division! Operator Precedence If there are multiple operators in an expression that belong in the same tier, % * / // they are performed left to right + - How about positive and negative? Operator Precedence If there are multiple operators in an Perform first + - expression that belong in the same tier, % * / // they are performed left to right Perform last + - Unary operators always take the highest If in same tier, precedence! perform left to right Let’s try evaluating a couple of expressions 2 + 5 // 2 2 + (5 // 2) (2 + 2) 4 8 % 6 + 4 * 2 (8 % 6) + 4 * 2 2 + (4 * 2) (2 + 8) 10 5 % 2 // 6 * 1.0 (5 % 2) // 6 * 1.0 (1 // 6) * 1.0 (0 * 1.0) 0.0 [float] Solve a couple more… Expression Answer 3 - 17 % 6 18 + 3 * 5 - 6 3 % 5 + 19 // 4 For the succeeding items, let x be 7 and y be 3. y * (3 + 4) - x 18 // y + x % y Answers! Expression Answer 3 - 17 % 6 -2 18 + 3 * 5 - 6 27 3 % 5 + 19 // 4 7 For the succeeding items, let x be 7 and y be 3. y * (3 + 4) - x 14 18 // y + x % y 7 Post-processing Its important to remember how operators work in Python as we’re dealing with learning Python for this course However, if you jump over to another prog. language, the rules might be different Right now, we’re just learning the domain knowledge Let’s move over to Canvas Exercises SQ2 on Variables, Data Types, and Operators Converted to an asynch task 8 items, 10 points focused on the topics above Has a 15-minute time limit Deadline is right before our next session 12:00 pm Oct 17 2022 (M) Thanks and see you all next meeting!