Download presentation
Presentation is loading. Please wait.
Published byHendri Kusumo Modified over 6 years ago
1
Winter 2018 CISC101 11/22/2018 CISC101 Reminders Prof’s office hours set to Thursdays 1:45 to 2:30 in GOO 550. Quiz 1 next week. More info in last lecture. Assn 1 due Friday, next week. Exercise 2 is fair game now – work on console input and output techniques. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
2
Today Continue Python Expressions. Operators and Precedence.
Winter 2018 CISC101 - Prof. McLeod
3
Arithmetic Operators As listed in Exercise 1: + addition
Winter 2013 CISC101 Arithmetic Operators As listed in Exercise 1: + addition - subtraction (and unary negation) * multiplication / division // "floor" division % modulo or "remainder" ** exponentiation or "to the power of" The first three make sense, how do the last four work? Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
4
/ or Division For example, what is the value of 1 / 3 ?
In previous versions of Python the result of int divided by int would be an int. Not any more! Now the result is a float. BTW, good thing there is a limit to the size of a float! Winter 2018 CISC101 - Prof. McLeod
5
// or Integer or “Floor” Division
What is the value of 1 // 3? The result is always truncated, not rounded. So, 99 // 100 is also zero. You still get the truncated value, even if one or the other numbers are floats. So, 1.0 // 3.0 is still 0.0 Winter 2018 CISC101 - Prof. McLeod
6
% or “Modulo” Always gives the remainder after division.
For example 20 % 3 is 2 Also works with floats. Result is a float. Winter 2018 CISC101 - Prof. McLeod
7
** or “To the Power Of” Example 5 ** 2 is 25
If both numbers are ints, the result is an int. You can generate some pretty large int values this way! For example 5 ** 100 is: If at least one number is a float, the answer is a float. So, 5.0 ** 100 is: e+69 Winter 2018 CISC101 - Prof. McLeod
8
Mixed Numeric Types If you have an expression like: aVal = 4 + 5 – 2.0
the result will always be a float in this case. So, if you get any float values in an expression that has integers the result will always be a float. So something like / 5 gives 6.0 Winter 2018 CISC101 - Prof. McLeod
9
5 * 'abc' is 'abcabcabcabcabc'
Strings and Numbers! Multiplication: 5 * 'abc' is 'abcabcabcabcabc' The + operator is the only other numeric operator that works with strings. Winter 2018 CISC101 - Prof. McLeod
10
Addition and Strings The + sign not only adds numbers but can also concatenate strings (and collections). You can concatenate a number to a string, but have to convert it to a string first using the str() BIF. Winter 2018 CISC101 - Prof. McLeod
11
Arithmetic Operators, 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. Winter 2018 CISC101 - Prof. McLeod
12
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 2018 CISC101 - Prof. McLeod
13
Precedence Rules Suppose I have an expression like: a = 5 * b + 27 / c
CISC101 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 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
14
Precedence Rules, Cont. Rules again: ** - (unary) * / // % + - =
All operators on the same line have equal precedence. Winter 2018 CISC101 - Prof. McLeod
15
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 // 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 2018 CISC101 - Prof. McLeod
16
Example 4 + (5 * (6 - 3)) % 4 4 + (5 * 3) % 4 4 + 15 % 4 4 + 3 7
4 + (5 * (6 - 3)) % (5 * 3) % % Winter 2018 CISC101 - Prof. McLeod
17
Another Example aNum = 3 ((7 - 12) // 2) + 7 / 2 - aNum + 2 * 3 - (7 % 2) ((7 - 12) // 2) + 7 / * 3 - (7 % 2) (-5 // 2) + 7 / * 3 - (7 % 2) / * 3 - (7 % 2) / * 3 – * 3 – – – – – 1 2.5 Winter 2018 CISC101 - Prof. McLeod
18
CISC101 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. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
19
Unary Boolean Operator
One unary boolean operator: not Sets True to False, and False to True (just like a NOT gate!) Winter 2018 CISC101 - Prof. McLeod
20
Binary Boolean Operators
> < >= <= == != greater than less than greater than or equal to less than or equal to equals not equals All of these result in a True or False bool value. and or a logical AND a logical OR Winter 2018 CISC101 - Prof. McLeod
21
Binary Boolean Operators, Cont.
> < >= <= == != greater than less than greater than or equal to less than or equal to equals not equals 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. and or a logical AND a logical OR Winter 2018 CISC101 - Prof. McLeod
22
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. Winter 2018 CISC101 - Prof. McLeod
23
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 2018 CISC101 - Prof. McLeod
24
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 2018 CISC101 - Prof. McLeod
25
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 2018 CISC101 - Prof. McLeod
26
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? Winter 2018 CISC101 - Prof. McLeod
27
Boolean Expression Examples
5 > 2 4 < 3 or 7 > 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 2018 CISC101 - Prof. McLeod
28
Another Example – Evaluate:
not(5 * 4 > 3) or <= 5 and 6 == 2 not(20 > 3) or <= 5 and 6 == 2 not True or <= 5 and 6 == 2 False or <= 5 and 6 == 2 False or <= 5 and 6 == 2 False or True and 6 == 2 False or True and False False or False False Winter 2018 CISC101 - Prof. McLeod
29
Expressions, Cont. We’ve discussed literals, variables and operators.
We still have function/method calls, keywords and punctuation to discuss. I’ll talk about conditionals first – so you can work on the assignment and Exercise 3. Winter 2018 CISC101 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.