Download presentation
Presentation is loading. Please wait.
Published byChristiana Gardner Modified over 5 years ago
1
Winter 2019 CISC101 4/14/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. “Grader” is the TA who will be grading your assignments. Quiz 1 next week, starting in the Tuesday lab. More info coming up… Exercise 2 is now fair game – this would be good preparation for the coding part of the quiz. Assignment 1 due next Friday. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
2
Quiz 1 Topics Everything up to and including this Friday’s lecture. But not Conditionals. Exercises 1 and 2 are fair game. Emphasis on: Computing background history tidbits. How a computer works. Digital systems logic. Controlling the CPU. Python history and features. Python types, variables, expressions and console I/O. Python coding – simple linear programs. Winter 2019 CISC101 - Prof. McLeod
3
Quiz 1 Restrictions The quiz will start out showing that it is open only for the Tuesday lab between 11:30 and 1:30pm. When this lab is over the quiz will be re-opened for the Thursday labs. You must write the quiz in the lab time you chose in SOLUS – as indicated by the Lab group you have been assigned to in onQ. If you cannot do so, the prof with a good reason. The quiz can only be opened in JEFF155 – it is IP address restricted to only the lab machines. Winter 2019 CISC101 - Prof. McLeod
4
Quiz 1 Format Electronic – in onQ. No aids. About 45 minutes.
T/F and short answer. Short answer could be a code segment or possibly a complete program. Code writing will be towards the end of the quiz – leave yourself enough time!!! You will need to be able to write a simple Python program. Winter 2019 CISC101 - Prof. McLeod
5
Quiz 1 Format, Cont. When typing code directly into a multi-line answer box avoid using the <tab> key. This key will not give you a tabbed space but will just move the cursor out of the box, which is really annoying. Use just the spacebar for spacing, say 3 or 4 spaces per indent. Style is not critical, but nice looking code is easier to read and grade for an old guy like me… Winter 2019 CISC101 - Prof. McLeod
6
Quiz 1 Rules You will use onQ, but no other programs can be used on any device. No other aids. The TA must be able to see your screen. No talking to other students when writing. If you have written, please keep the noise down until all other students have finished writing the quiz. Winter 2019 CISC101 - Prof. McLeod
7
Quiz 1 Rules, Cont. Do not discuss the quiz with students who have not written it. Do not provide quiz answers to students who have not written it. The TAs will note the names of any “violators” and their quizzes will become zero! The rest of the lab time is available for assignment help. Winter 2019 CISC101 - Prof. McLeod
8
Preparing for Quiz 1 A “sample” version of this quiz is linked at the bottom of the “Grading” page. It will give you an idea of the type and quantity of questions on the quiz, including samples of coding questions. Do exercises 1 and 2. Start working on Assignment 1. Go over lecture notes, and videos if you need to. Winter 2019 CISC101 - Prof. McLeod
9
Today From last time: The Python Prompt and IDLE.
Continue Python Syntax by looking at Expressions: Python Types Variables Operators (if we have time) Winter 2019 CISC101 - Prof. McLeod
10
Aside - The Python Prompt
An open “pipeline” to the Python interpreter. You can always experiment using this prompt by writing small pieces of code. The prompt is never far away when you are using IDLE to build a Python program. Winter 2019 CISC101 - Prof. McLeod
11
The Python Prompt, Cont. <Cntrl>n will open up an empty editing window in IDLE. You first save, and then run any program you have written in IDLE. (Shortcut – press the <F5> function key when in IDLE to run your program – you will be prompted to save if you have not done so.) Anything stored in memory from a previous program that you have run is first deleted. (The “RESTART…” thing.) Then your program it is fed directly to the same Python Prompt, one line at a time. You can run your program again from the prompt by typing in main() followed by an <enter>. Winter 2019 CISC101 - Prof. McLeod
12
Python Expressions A program is a series of expressions, one per line.
An expression is built from one or many of the following pieces: Literal values Variables Operators Keywords Function and method calls Punctuation Syntax supplies the rules about how these pieces go together so the interpreter can understand our commands. Winter 2019 CISC101 - Prof. McLeod
13
Numeric Types in Python
CISC101 Numeric Types in Python In Exercise 1 you (have or will?) discovered that literal values can be of different types. What numeric types did you find? int float complex Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
14
Numeric Types, Cont. Each type’s literal value is characterized by the way it is typed into a program. Do you remember the characteristics of each numeric type? How does the interpreter tell them apart? Winter 2019 CISC101 - Prof. McLeod
15
Numeric Types, Cont. The int type is an integer (no decimal or exponent) and there is no limit to its size. The float type is characterized by a decimal place and/or an exponent. It is limited to about 17 digits. (We won’t use the complex type much!) Winter 2019 CISC101 - Prof. McLeod
16
float Type For example to code the real number:
You would write: 2.43E-4 or 2.43e-4 exponent decimal Winter 2019 CISC101 - Prof. McLeod
17
Other Bases Normally we view numbers in base 10, or in a “radix” of 10. That’s the default in Python. How can you create literal numbers in base 2, 8 or 16? Use the prefixes: 0b, 0o or 0x on literals. Use the BIFs: bin(), oct() and hex() to display a value in another base. Winter 2019 CISC101 - Prof. McLeod
18
Aside – What’s a “BIF”? (Not a Canadian Singer!) “Built-In Function”.
Winter 2019 CISC101 - Prof. McLeod
19
Other Types What other types did you find? bool str
How about the collections? bool str list tuple dict set We’ll talk more about these later… Winter 2019 CISC101 - Prof. McLeod
20
String Literals Characters enclosed in quotes. You can use: ' "
""" or ''' The opening quote must match the closing quote. Examples: 'hello' "hello" """hello""" Winter 2019 CISC101 - Prof. McLeod
21
String Literals, Cont. Triple quoted strings can span multiple lines. (Can make for some really long strings - often used for documentation.) Winter 2019 CISC101 - Prof. McLeod
22
Python Types, Cont. Python determines the type of a literal value by examination. When the value is assigned to a variable, the variable is typed to match the type of the value. Python is a dynamically typed language. Which means that a variable can change types. Winter 2019 CISC101 - Prof. McLeod
23
From Exercise 1 – Literal Types
What are the types of the following literals? 45.237 3.4e-7 45e10 123 0b100101 0x45cde 'Hello!' "Again" """line1 Line2""" True [4, 6, 7.9, "Dingdong"] (4, 5, True) {'first':'Alan', 'last':'McLeod'} {3, 4, 7, 10} "H" 3.4E10 Winter 2019 CISC101 - Prof. McLeod
24
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
25
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
26
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
27
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
28
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
29
Aside – The Worst Variable Name!
My favourite bad variable name: l1 Winter 2019 CISC101 - Prof. McLeod
30
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
31
/ 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
32
// 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
33
% 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
34
** 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
35
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
36
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
37
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
38
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 2019 CISC101 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.