Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.

Similar presentations


Presentation on theme: "Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1."— Presentation transcript:

1 Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1

2 2 Python Style Guides From Google: http://google-styleguide.googlecode.com/svn/trunk/pyguide.html 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”: http://legacy.python.org/dev/peps/pep-0008/ Winter 2016

3 CISC101 - Prof. McLeod3 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 2016

4 CISC101 - Prof. McLeod4 Variable and Function Names, Cont. Be descriptive, but not excessive! Examples: –numStudents –setPassingGrade ( parameter list ) Somewhat too long…: –flagThatIsSetToTrueIfAProblemArises WhenThereIsAFullMoonOverMyHouseInTh eWinterWhileMyProgramIsRunning Winter 2016

5 CISC101 - Prof. McLeod5 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 2016

6 CISC101 - Prof. McLeod6 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 2016

7 CISC101 - Prof. McLeod7 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 2016

8 CISC101 - Prof. McLeod8 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 2016

9 CISC101 - Prof. McLeod9 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 2016

10 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. CISC101 - Prof. McLeod10Winter 2016

11 BIFs So, what is a “BIF” anyways? A “Built-In-Function”. These are functions that already exist – the interpreter already knows what to do when you invoke one. See section 2 “Built-in Functions” in the “Python Standard Library” help document. CISC101 - Prof. McLeod11Winter 2016

12 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”. CISC101 - Prof. McLeod12Winter 2016

13 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). CISC101 - Prof. McLeod13Winter 2016

14 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(). CISC101 - Prof. McLeod14Winter 2016

15 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 and the.format() method. CISC101 - Prof. McLeod15Winter 2016

16 CISC101 - Prof. McLeod16 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 2016

17 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. CISC101 - Prof. McLeod17Winter 2016

18 CISC101 - Prof. McLeod18 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 2016

19 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 2016CISC101 - Prof. McLeod19

20 CISC101 - Prof. McLeod20 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: 45678 and c is: 21. Get more info from Exercise 2. Winter 2016

21 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… CISC101 - Prof. McLeod21Winter 2016

22 Punctuation All those “things” that are not operators, keywords, literals or variables. Some you see and some you don’t! Many symbols have a different meaning depending on context. CISC101 - Prof. McLeod22Winter 2016

23 Whitespace Punctuation The ones you don’t see (ie. non-printing): –Spaces –Tabs –Carriage return –Line feed Spaces are used as delimiters in expressions and to improve readability. CR/LF is known as a “Newline”. Used at the end of lines and to put empty lines in a program (for better readability, again). CISC101 - Prof. McLeod23Winter 2016

24 CISC101 - Prof. McLeod24 Tabs and Indentation Press the key to get an indent in your code, if you need one. Use the key to get out of an indentation. Don’t type spaces for indents! Indents are very important in Python, they are not just “whitespace”! IDLE starts indentation automatically, especially after you type a : Winter 2016

25 Indentation, Cont. Indentation indicates “containment” of blocks of code. A block can be one or many lines of code. For example, code could be contained: –In a function definition –In an if statement –In a loop As soon as you “de-dent” a line of code, the subsequent code will no longer be contained in whatever block you were defining. CISC101 - Prof. McLeod25Winter 2016

26 CISC101 - Prof. McLeod26 Punctuation You Can See ' " # \ ' and " for string literals. # used to start a comment. \ is used to continue a long line of code onto the next line. Winter 2016

27 More Punctuation You Can See ( ) [ ] { }, :. ; @ These are all multi-use symbols and their meaning depends on context. Probably better to learn about them in that context! CISC101 - Prof. McLeod27Winter 2016


Download ppt "Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1."

Similar presentations


Ads by Google