Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2019 CISC101 4/28/2019 CISC101 Reminders

Similar presentations


Presentation on theme: "Winter 2019 CISC101 4/28/2019 CISC101 Reminders"— Presentation transcript:

1 Winter 2019 CISC101 4/28/2019 CISC101 Reminders You have been assigned to two groups in onQ – the “Lab” group and the “Grader” group. You must write the quiz in the lab time as indicated by the Lab group you are in. Quiz 1 next week, starting in the Tuesday lab. More info in yesterday’s lecture. Assignment 1 due next Friday. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Today Continue Python Syntax by looking at Expressions:
Winter 2019 CISC101 4/28/2019 Today Continue Python Syntax by looking at Expressions: Python Types – we did this yesterday. Type BIFs. Variables Operators Operator Precedence Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

3 From Exercise 1 – Types, Cont.
How can you discover the type of a variable? How can you change a literal of one type to another? Winter 2019 CISC101 - Prof. McLeod

4 Aside - Using the Help Docs
Press <F1> (or use a menu option) from the Python Shell window or from IDLE. The most useful sections: The Python Tutorial. The Python Standard Library. Look for the table of Built-In Functions, for example. Winter 2019 CISC101 - Prof. McLeod

5 Variables What is a variable anyways?
CISC101 Variables What is a variable anyways? In Python, variables are created by an assignment statement (or in a function’s parameter list). A variable takes the type of the value being assigned to it when the program runs. A variable’s value can change any time, as can its type. This is a convenient way to address a location in memory. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

6 Assigning/Creating a Variable
In code: myVal = 20 Now myVal refers to some location in RAM that stores the int type value 20. We don’t have to worry about what the actual memory address is. Winter 2019 CISC101 - Prof. McLeod

7 Variable Naming Syntax Rules
Variable names are case sensitive. You can’t use a Python keyword for a variable name. No spaces! Start with a letter (use lower case, by convention), or an underscore_. The rest of the name can contain numbers, letters or the underscore (no spaces – oops I said that already!) Why no spaces, anyways? Winter 2019 CISC101 - Prof. McLeod

8 Variable Naming Style Rules
Use descriptive names. Capitalize successive words in a name using camelCase. No limit to the length of a variable name, but don’t write an essay!! Don’t use single letter variable names, except if you need a loop counter that has no intrinsic meaning, then you can use i, j or k. Winter 2019 CISC101 - Prof. McLeod

9 Aside – The Worst Variable Name!
My favourite bad variable name: l1 Winter 2019 CISC101 - Prof. McLeod

10 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 four make sense, how do the last three work? Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

11 / 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 2019 CISC101 - Prof. McLeod

12 // 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 2019 CISC101 - Prof. McLeod

13 % or “Modulo” Always gives the remainder after division.
For example, 20 % 3 is 2 Also works with floats. Result is a float. Winter 2019 CISC101 - Prof. McLeod

14 ** 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 2019 CISC101 - Prof. McLeod

15 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 2019 CISC101 - Prof. McLeod

16 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 2019 CISC101 - Prof. McLeod

17 Addition and Strings The + sign not only adds numbers but can also concatenate strings (and collections). Shall we try it out? You can concatenate a number to a string, but have to convert it to a string first using the str() BIF. Winter 2019 CISC101 - Prof. McLeod

18 Operators, Summary + - * / // % **
+ - * / // % ** All of these work with numbers on both sides. + and * will accept a string or even a collection on one side or both. If an expression has a float number in it, then the result will always be a float. Winter 2019 CISC101 - Prof. McLeod

19 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 2019 CISC101 - Prof. McLeod

20 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 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

21 Precedence Rules, Cont. Rules again: ** - (unary) * / // % + - =
All operators on the same line have equal precedence. Winter 2019 CISC101 - Prof. McLeod

22 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 2019 CISC101 - Prof. McLeod

23 Example 4 + (5 * (6 - 3)) % 4 4 + (5 * 3) % 4 4 + 15 % 4 4 + 3 7
4 + (5 * (6 - 3)) % (5 * 3) % % Winter 2019 CISC101 - Prof. McLeod

24 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 2019 CISC101 - Prof. McLeod

25 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 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

26 Unary Boolean Operator
One unary boolean operator: not Sets True to False, and False to True (just like a NOT gate!) Winter 2019 CISC101 - Prof. McLeod

27 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 2019 CISC101 - Prof. McLeod

28 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. and or a logical AND a logical OR These must have a bool value on both sides. Winter 2019 CISC101 - Prof. McLeod

29 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 2019 CISC101 - Prof. McLeod

30 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 2019 CISC101 - Prof. McLeod

31 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 2019 CISC101 - Prof. McLeod


Download ppt "Winter 2019 CISC101 4/28/2019 CISC101 Reminders"

Similar presentations


Ads by Google