Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC101 Reminders All assignments are now posted.

Similar presentations


Presentation on theme: "CISC101 Reminders All assignments are now posted."— Presentation transcript:

1 CISC101 Reminders All assignments are now posted.
Winter 2019 CISC101 4/27/2019 CISC101 Reminders All assignments are now posted. Assignment 1 due this Friday. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Today Comparing Strings.
Winter 2019 CISC101 4/27/2019 Today Comparing Strings. Continue Coding Style (also applicable to your Assignment 1 code). What is a Function? What is a Method? Start a review of Input/Output functions and methods. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

3 Aside - Comparing Strings
Binary Boolean operators work with either numbers on both sides or strings on both sides. But, how are strings compared? Winter 2019 CISC101 - Prof. McLeod

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

5 ASCII Table (Lower Half)
Winter 2019 CISC101 - Prof. McLeod

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

7 Coding Style, So Far… Yesterday’s video discussed: Commenting Code.
Documentation Strings. Being Careful with Indentation. Use of Whitespace – blank lines and spaces. Winter 2019 CISC101 - Prof. McLeod

8 Coding Style: Misc. No code after a : when it is used with a def, if or while loop. Define main at the top of your program or at the end - not in the middle! One import statement per line. Put import statements before globals but after block comments and module-level doc strings. (There is a big set of additional rules for doc strings themselves that I won’t get into here…) Winter 2019 CISC101 - Prof. McLeod

9 Do # This program is used to demonstrate better style.
# Version 1, by Alan McLeod, 27 Oct. 2011 def product(num1, num2) : '''This is a useless little function that does not do much''' print('Hello') return num1 * num2 def main() : '''main invokes product and then waves goodbye!''' print(product(3, 4)) print('Goodbye!') main() Winter 2019 CISC101 - Prof. McLeod

10 Don’t! This works! How many things are wrong with this code?
def m(ll1,l1):print('Hello');return(ll1*l1); def main():print(m(3,4));print("Goodbye!") main () This works! How many things are wrong with this code? Winter 2019 CISC101 - Prof. McLeod

11 Python Style Guides From Google:
Lots of detail – more than you need, right now. Originally intended for internal use (Google uses Python a lot – or at least, they used to…). At python.org, “PEP 8”: Winter 2019 CISC101 - Prof. McLeod

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

13 Variable and Function Names, Cont.
Be descriptive, but not excessive! Examples: numStudents setPassingGrade ( parameter list ) Somewhat too long…: flagThatIsSetToTrueIfAProblemArisesWhenThereIsAFullMoonOverMyHouseInTheWinterWhileMyProgramIsRunning Winter 2019 CISC101 - Prof. McLeod

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

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

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

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

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

19 Coding Style - Above All Else:
Be Consistent! Winter 2019 CISC101 - Prof. McLeod

20 Functions and Methods What’s the difference?
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 2019 CISC101 - Prof. McLeod

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

22 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). A function has a limited and defined interface to a program that uses the function. Winter 2019 CISC101 - Prof. McLeod

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

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

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

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

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

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

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

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

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

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


Download ppt "CISC101 Reminders All assignments are now posted."

Similar presentations


Ads by Google