Download presentation
Presentation is loading. Please wait.
Published byΤιτάνια Ελευθερίου Modified over 6 years ago
1
Winter 2018 CISC101 12/1/2018 CISC101 Reminders The Prof will mark Quiz 1. Marks will be released and you can view your graded quiz once they are all marked. Assn 1 now due Monday, Feb. 5, next week. Exercise 3 is fair game now – conditionals. Videos are all caught up now. All assignments are posted from the course web site. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
2
Today Error in Jan. 25 Slides on Precedence.
Aside on Comparing Strings More Style – Naming “Things” Back to Building Expressions: Invoking Functions. (sneak in a bit of a discussion of Console I/O) Punctuation Keywords (next time…) Winter 2018 CISC101 - Prof. McLeod
3
Precedence Rule Error in Jan. 25 Slides
(My thanks to the student that pointed this out!) Slide 25 says that unary operators (not and negation) come before **, which contradicts what it says on slide 14, which has ** before unary negation. Slide 14 is correct, slide 25 is in error. You can confirm this by evaluating an expression like: -5 ** 2 at the Python prompt. The answer will be -25, which means that the negation takes place after the **. The rest of slide 25 is OK. Winter 2018 CISC101 - Prof. McLeod
4
Aside - Comparing Strings
Binary Boolean operators work with either numbers on both sides or strings on both sides. But, how are strings compared? Winter 2018 CISC101 - Prof. McLeod
5
Comparing Strings, Cont.
Winter 2013 CISC101 Comparing Strings, Cont. Some examples: "abc" == "abC" gives False "abc" < "abcd" gives True "A" < "a" gives True "a" < "b" gives True "aaa" < "aaaa" gives True These comparisons are based on the ASCII code values of the characters compared. “American Standard Code for Information Interchange” Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
6
ASCII Table (Lower Half)
Winter 2018 CISC101 - Prof. McLeod
7
Comparing Strings, Summary
So, you are still actually comparing numeric values, character by character. A shorter string is less than an otherwise identical longer string. Capitals are less than lower case letters. This is important when sorting or analyzing textual information. Winter 2018 CISC101 - Prof. McLeod
8
Variable and Function Names
Follow Python restrictions on names: Use only letters, numeric digits (0 to 9) and the “_” character. Cannot start name with a number. Python is case sensitive! Variables and function names usually start with a lower case character. Class names start with an upper case character. Constants are all in upper case. The use of one or two underscores and the beginning and/or the end of a variable name has a special meaning in Python… Variables are usually nouns. Functions are verbs or verbs and nouns. Winter 2018 CISC101 - Prof. McLeod
9
Variable and Function Names, Cont.
Be descriptive, but not excessive! Examples: numStudents setPassingGrade ( parameter list ) Somewhat too long…: flagThatIsSetToTrueIfAProblemArisesWhenThereIsAFullMoonOverMyHouseInTheWinterWhileMyProgramIsRunning Winter 2018 CISC101 - Prof. McLeod
10
Variable and Function Names, Cont.
Use camelCase for variable names (Google Python Style Guide says use underscores – I don’t like that style in Python, personally). Note that Python keywords are in all lower case. You will get an error message if you attempt to use a keyword as a variable name. It is very tacky to use a keyword as a variable name, just by changing the capitalization! Winter 2018 CISC101 - Prof. McLeod
11
Naming Variables Also applies to naming parameters, functions, methods and classes. The name should reveal the intention of what it is you are naming. You should not need to add a comment to a variable declaration to provide further explanation. A comment is OK if you want to record the units of the variable or if you need to explain the initial value. Winter 2018 CISC101 - Prof. McLeod
12
Naming Variables, Cont. Avoid Disinformation
Avoid using words that might have multiple meanings. Make Meaningful Distinctions Don’t use artificial means of distinguishing similar names. Examples: account0 or account1, accountNum or accountNums Winter 2018 CISC101 - Prof. McLeod
13
Naming Variables, Cont. Use Pronounceable Names
It is easier for our brains to remember a variable name if it is pronounceable. Use Searchable Names For example, single or even two-letter variable names will be difficult to locate using a text search. If a loop counter has no intrinsic meaning then it is OK to use i, j and k (but not l !!!) as loop counters. Winter 2018 CISC101 - Prof. McLeod
14
Naming Variables, Cont. Avoid Encodings
Older naming conventions might use a prefix or suffix to indicate the type or membership of a variable – this is no longer necessary. Use One Word per Concept For example don’t use all of the terms “manager”, “controller” and “driver” – what is the difference? Winter 2018 CISC101 - Prof. McLeod
15
Coding Style - Above All Else:
Be Consistent! Winter 2018 CISC101 - Prof. McLeod
16
Aside - Functions and Methods
A reminder: A method belongs to an object and must be invoked from an instance of that object (see the .format() method in Exercise 2, for example). A function stands alone and does not belong to any object. We can define our own functions or invoke functions that have already been defined for us. Winter 2018 CISC101 - Prof. McLeod
17
Functions What is a “Function”?
This is a collection of lines of code that are contained in a function definition. A function should perform a single specific task. You can run these lines of code by invoking the function. We have been adding code to a function called “main”. Winter 2018 CISC101 - Prof. McLeod
18
Functions, Cont. A function is characterized by: Its name.
The use of round brackets () after the name. Its parameter list inside the round brackets (none, one or many parameters!) Its return value (none or one thing, but that one thing could be a collection). Winter 2018 CISC101 - Prof. McLeod
19
Aside - Invoking Functions
How do you invoke a function? You name the function and then must supply a set of brackets ( ). The brackets could be empty or might contain one or more arguments. Arguments are mapped to parameters inside the function. Parameters act like variables inside the function. Functions we have already seen and used: print(), input(), int(), bin(), hex(), type(). Winter 2018 CISC101 - Prof. McLeod
20
Using BIFs and Methods, Ex: Console I/O
Or “Input/Output” How you interact with the “user” of your program. Use two BIFs – print() for Output and input() for Input. Control output format using escape sequences, the .format() method or “formatted” strings that have the “f” in front of them. Winter 2018 CISC101 - Prof. McLeod
21
Console (or Screen) Input
CISC101 Console (or Screen) Input Use the input() function, which returns a string (even if you type in a number). Supply a prompting string to the input() function as a parameter. The function returns what the user has typed in. (How do you get a number from the input() function’s returned string?) Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
22
Using the print() BIF You can supply any number of parameters to the function, including no parameters at all, by supplying a comma-separated list of parameters inside the brackets. Parameters can be variables or literal values (or values supplied by some other function). Comma separated values are printed together on the same line, separated by a space, by default. Winter 2018 CISC101 - Prof. McLeod
23
Escape Characters Can be used to control how strings are displayed when using the print() function. See the Python Help docs, as well: \n - linefeed \t – tab character \’ – single quote \” – double quote \\ - backslash (You can also use triple quotes to create a multi-line string without using escape characters…) Winter 2018 CISC101 - Prof. McLeod
24
Modifying How print() Works
There are two keyword parameters that affect: what goes between multiple arguments, and how the printed line is terminated. The first is sep=, where the default is sep=" " The second is end=, where the default is end="\n" For example: print("Hello", "world", sep="***", end="!\n") Displays: Hello***world! Winter 2018 CISC101 - Prof. McLeod
25
More Output Formatting
The str.format() method gives you complete control over how information is displayed in the console window. Here is an example from Exercise 2: >>> print("The variable a is: {0}, b is: {1} and c is: {2}.".format(a, b, c)) The variable a is: 123, b is: and c is: 21. Get more info from Exercise 2. Winter 2018 CISC101 - Prof. McLeod
26
Aside – .format() Method
.format() is like a function, but it is invoked differently. .format() is a member function of the string (or str) object. So, you must have a string object in order to invoke this method or “member function”. Don’t worry too much about this, for now… Winter 2018 CISC101 - Prof. McLeod
27
Formatted Strings in New Python Versions
This is even easier: print(f"The variable a is: {a}, b is: {b} and c is: {c}.") You can use formatting codes for each variable if you put them after a colon (:) inside the { }. Winter 2018 CISC101 - Prof. McLeod
28
Aside – the round BIF You can use the round BIF to round numbers to a certain number of digits before you display them. For example: >>> aNum = >>> print("Rounded is", str(round(aNum, 2))) Rounded is 15.46 The first argument to round() is the number to be rounded, the second argument is the number of digits you want to have after the decimal place. Winter 2018 CISC101 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.