Download presentation
Presentation is loading. Please wait.
Published byJohnathan Harvey Modified over 8 years ago
1
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1
2
Operators So Far, a Summary + - * / // % ** All of these work with numbers on both sides. Some will accept a string or even a collection on one side. If an expression has a float number in it, then the result will always be a float. CISC101 - Prof. McLeod2Winter 2016
3
Assignment Operator We’ve used this one already: = Used to assign a value to a variable and to create that variable, if necessary. Note that you can combine the other arithmetic operators with = as long as the variable already exists. For example +=. Assuming aVar is 3, then aVar += 5 stores 8 in aVar This is the same as writing: aVar = aVar + 5 Winter 2016CISC101 - Prof. McLeod3
4
4 Precedence Rules Suppose I have an expression like: a = 5 * b + 27 / c How do we know the order of operations? These rules are built-in to the interpreter. = is always last (why?) ** first, then - (as unary negation), then *, /, // and %, then + and -, and then = Winter 2016
5
Precedence Rules, Cont. Rules again: ** - (unary) * / // % + - = CISC101 - Prof. McLeod5 All operators on the same line have equal precedence. Winter 2016
6
CISC101 - Prof. McLeod6 Precedence Rules, Cont. How can you take over control of the order of operations? Use the round brackets: For example (4 + 5) // 3 is 3, but 4 + 5 // 3 is 5 If you have a series of operators that have equal precedence and no brackets to control things, then the expression is evaluated from left to right. Winter 2016
7
Example 4 + (5 * (6 - 3)) % 4 4 + (5 * 3) % 4 4 + 15 % 4 4 + 3 7 Winter 2016CISC101 - Prof. McLeod7
8
Another Example aNum = 3 ((7 - 12) // 2) + 7 / 2 - aNum + 2 * 3 - (7 % 2) ((7 - 12) // 2) + 7 / 2 - 3 + 2 * 3 - (7 % 2) (-5 // 2) + 7 / 2 - 3 + 2 * 3 - (7 % 2) -3 + 7 / 2 - 3 + 2 * 3 - (7 % 2) -3 + 7 / 2 - 3 + 2 * 3 – 1 -3 + 3.5 - 3 + 2 * 3 – 1 -3 + 3.5 - 3 + 6 – 1 0.5 - 3 + 6 – 1 -2.5 + 6 – 1 3.5 – 1 2.5 Winter 2016CISC101 - Prof. McLeod8
9
Boolean Operators Also have a bunch of binary operators (and one unary operator) that yield a Boolean or a bool type result – a True or False. CISC101 - Prof. McLeod9Winter 2016
10
CISC101 - Prof. McLeod10 Unary Boolean Operator One unary boolean operator: not –Sets True to False, and False to True (just like a NOT gate!) Winter 2016
11
CISC101 - Prof. McLeod11 Binary Boolean Operators greater than less than greater than or equal to less than or equal to equals not equals > < >= <= == != and or a logical AND a logical OR All of these result in a True or False bool value. Winter 2016
12
CISC101 - Prof. McLeod12 Binary Boolean Operators, Cont. greater than less than greater than or equal to less than or equal to equals not equals > < >= <= == != and or a logical AND a logical OR All of these must have a number on both sides or a string on both sides. These must have a bool value on both sides. Winter 2016
13
Binary Boolean Operators, Cont. The interpreter is going to want both sides to be either numeric or both sides to be strings. You will get an error if you try to compare a string to a number. CISC101 - Prof. McLeod13Winter 2016
14
Example How can you test a variable, aVal, to see if it is between two limits, inclusive? Call them low and high : Check: low <= aVal and aVal <= high Unfortunately, in Python, this also works sometimes: low <= aVal <= high This is bad form and will not work in most other programming languages! Winter 2016CISC101 - Prof. McLeod14
15
Example, Cont. If aVal is between the limits the expression on the previous slide will evaluate to a True. Suppose you want to see a True if aVal is outside the limits. How? Two ways: not( low <= aVal and aVal <= high ) Or: low > aVal or aVal > high Which way do you like better? Winter 2016CISC101 - Prof. McLeod15
16
CISC101 - Prof. McLeod16 Expanded Precedence Rules Unary operations ( not and negation) first. Math operators in usual order: ** * / // % + - Binary boolean comparison operators: > >= < <= == != Logical operator: and Logical operator: or Assignment operator: = Winter 2016
17
Expanded Precedence Rules, Cont. Round brackets are still used to control precedence. And, function invocations and variable substitution take place before all of the operators listed on the previous slide. Can brackets force the assignment operator to go first? CISC101 - Prof. McLeod17Winter 2016
18
CISC101 - Prof. McLeod18 Boolean Expression Examples 5 > 2 4 1 7 != 8 6 + 2 > 9 or 4 == 3 + 1 7 == 7 and 5 > 2 and 6 != 3 5 * 5 >= 5 ** 2 128 % 2 == 0 Winter 2016
19
CISC101 - Prof. McLeod19 Another Example – Evaluate: not(5 * 4 > 3) or 2 + 3 <= 5 and 6 == 2 not(20 > 3) or 2 + 3 <= 5 and 6 == 2 not True or 2 + 3 <= 5 and 6 == 2 False or 2 + 3 <= 5 and 6 == 2 False or 5 <= 5 and 6 == 2 False or True and 6 == 2 False or True and False False or False False Winter 2016
20
CISC101 - Prof. McLeod20 So Far… Without conditionals programs are linear: etc. Obtain data from user Calculations… Display results Winter 2016
21
CISC101 - Prof. McLeod21 Branching Algorithms If a program could test a condition, then it would have a choice as to its path of execution: etc. if trueif false Winter 2016
22
Python has if, if-else and chained if ( if- elif-else ) statements. if statement syntax : if boolean_expression : statement1_when_true statement2_when_true statement3_when_true … Example: if capacitance < 0 : print("Illegal capacitance") CISC101 - Prof. McLeod22 Conditionals or “Selection Statements” Winter 2016
23
CISC101 - Prof. McLeod23 if-else Statement Syntax of if-else statement: if boolean_expression : statement(s)_when_true else : statement(s)_when_false Example: if stress > maxStress / 1.5 : result = "failure" else : result = "pass" Winter 2016
24
CISC101 - Prof. McLeod24 if Statement, Cont. You will often have to nest if statements: etc. Winter 2016
25
CISC101 - Prof. McLeod25 Nested if Statements For example, if you have three numbers, a, b, & c and you want to print them to the screen in order: a < b TF b < c TF TF a < c TF TF abc acbcabbacbca cba See SortThree.py Winter 2016
26
CISC101 - Prof. McLeod26 if-elif Statements In the code in SortThree.py, you might have noticed this construct: else : if a < c : This kind of thing occurs so often that it can be shortened to: elif a < c : Winter 2016
27
This leads to a common code structure often called a “chained if” construct: if condition1 : statement(s) elif condition2 : statement(s) elif condition3 : statement(s) else : statement(s) CISC101 - Prof. McLeod27 if-elif Statements, Cont. You can have as many elif s as you want. The else part is optional. Winter 2016
28
CISC101 - Prof. McLeod28 if-elif Statements, Cont. There is nothing in this construct that you could not make with normal if-else statements. For some kinds of conditionals, the if-elif might be easier to put together. See Part B of assignment 1, for example. Winter 2016
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.