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

Slides:



Advertisements
Similar presentations
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
Introduction to C Programming
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Introduction to C Programming
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
A First Program Using C#
Introduction to Python
Comments are for people Header comments supply basic information about the artifact.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
The Java Programming Language
Input, Output, and Processing
Week 1 Algorithmization and Programming Languages.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Winter 2006CISC121 - Prof. McLeod1 Last Time Wrapper classes JFileChooser (along with JOptionPane, and JColorChooser !) Text File Output.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
Python Let’s get started!.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
C++ First Steps.
Last Time Unary operators: Other Assignment operators
Chapter 2 Introduction to C++ Programming
Chapter 2, Part I Introduction to C Programming
Variables, Expressions, and IO
Introduction to Scripting
Winter 2018 CISC101 11/9/2018 CISC101 Reminders
CISC101 Reminders Assn 3 due Friday, this week.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to C++ Programming
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Winter 2018 CISC101 11/27/2018 CISC101 Reminders
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
T. Jumana Abu Shmais – AOU - Riyadh
Introduction to C++ Programming
Topics Introduction to Functions Defining and Calling a Void Function
CISC124 Labs start this week in JEFF 155. Fall 2018
Winter 2019 CISC101 4/8/2019 CISC101 Reminders
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CISC101 4/14/2019 CISC101 Reminders
12th Computer Science – Unit 5
CISC101 Reminders All assignments are now posted.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due today.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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

2 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 2016

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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