Example: marketing

Introduction to Python Programming Course Notes

Introduction to Python ProgrammingCourse NotesPhil SpectorDepartment of Statistics, University of California BerkeleyMarch 16, 20052 Contents1 What is Python ? .. The very Basics of Python .. Invoking Python .. Basic Principles of Python .. Basic Core Language .. Modules .. Object Oriented Programming .. Namespaces and Variable Scoping .. Exception Handling .. 152 String String Constants .. Special Characters and Raw Strings .. Unicode Strings .. String Operations .. Concatenation .. Repetition .. Indexing and Slicing .. Functions and Methods for Character Strings .. 233 Numeric Types of Numeric Data .. Hexadecimal and Octal Constants .. Numeric Operators .. Functions for Numeric Data .. Conversion of Scalar Types.

python is an excellent choice as a first programming language without sacri- ficing the power and advanced capabilities that users will eventually need. Although pictures of snakes often appear on python books and websites,

Tags:

  Introduction, Programming, Python, Course, Introduction to python programming course

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Introduction to Python Programming Course Notes

1 Introduction to Python ProgrammingCourse NotesPhil SpectorDepartment of Statistics, University of California BerkeleyMarch 16, 20052 Contents1 What is Python ? .. The very Basics of Python .. Invoking Python .. Basic Principles of Python .. Basic Core Language .. Modules .. Object Oriented Programming .. Namespaces and Variable Scoping .. Exception Handling .. 152 String String Constants .. Special Characters and Raw Strings .. Unicode Strings .. String Operations .. Concatenation .. Repetition .. Indexing and Slicing .. Functions and Methods for Character Strings .. 233 Numeric Types of Numeric Data .. Hexadecimal and Octal Constants .. Numeric Operators .. Functions for Numeric Data .. Conversion of Scalar Types.

2 3334 CONTENTS4 Lists, Tuples and List Data .. List Indexing and Slicing .. List Operators .. Concatenation .. Repetition .. Theinoperator .. Functions and Methods for Lists .. Tuple Objects .. Operators and Indexing for Tuples .. Functions and Methods for Tuples .. Dictionaries .. Functions and Methods for Dictionaries .. 525 Input and Theprintcommand .. Formatting Strings .. Using Names in Format Strings .. File Objects .. Methods for Reading .. Methods for Writing .. Printing to a File .. Other Methods .. File Object Variables .. Standard Input and Output Streams .. Pipes .. 636 Assignments .. Indentation .. Truth, Falsehood and Logical Operators .. and therangefunction.

3 Control in Loops:breakandcontinue.. List Comprehensions .. 84 CONTENTS57 Introduction .. Scoping: How Python finds your variables .. Function Basics .. Named Arguments and Default Values .. Variable Number of Arguments .. Functional Programming , and anonymous functions .. 988 Using Introduction .. Namespaces .. Functions for working with modules .. Thestringmodule .. String Constants .. Functions in thestringmodule .. Theremodule: Regular Expressions .. Introduction to Regular Expressions .. Constructing Regular Expressions .. Compiling Regular Expressions .. Finding Regular Expression Matches .. Tagging in Regular Expressions .. Using Named Groups for Tagging .. Greediness of Regular Expressions .. Multiple Matches.

4 Substitutions .. Operating System Services:osandshutilmodules .. Expansion of Filename wildcards - theglobmodule .. Information about your Python session - thesysmodule .. Copying: thecopymodule .. Object Persistence: thepickle/cPickleandshelvemodules Pickling .. Theshelvemodule .. CGI (Common Gateway Interface): thecgimodule .. Introduction to CGI .. Security Concerns .. CGI Environmental Variables .. Accessing Documents on the Web: theurllibmodule .. 1356 CONTENTS9 Introduction .. Tracebacks .. Dealing with Multiple Exceptions .. The Exception Hierarchy .. Raising Exceptions .. 14210 Writing Introduction .. An Example .. Test Programs for Modules .. Classes and Object Oriented Programming .. Operator Overloading .. Private Attributes.

5 A First Example of Classes .. Inheritance .. Adding Methods to the Basic Types .. 164 Chapter What is Python ? Python is a high-level scripting language which can be used for a wide varietyof text processing, system administration and internet-related tasks. Unlikemany similar languages, it s core language is very small and easy to mas-ter, while allowing the addition of modules to perform a virtually limitlessvariety of tasks. Python is a true object-oriented language, and is availableon a wide variety of platforms. There s even a Python interpreter writtenentirely in Java, further enhancing Python s position as an excellent solutionfor internet-based was developed in the early 1990 s by Guido van Rossum, thenat CWI in Amsterdam, and currently at CNRI in Virginia. In some ways, Python grew out of a project to design a computer language which would beeasy for beginners to learn, yet would be powerful enough for even advancedusers.

6 This heritage is reflected in Python s small, clean syntax and the thor-oughness of the implementation of ideas like object-oriented Programming ,without eliminating the ability to program in a more traditional style. Sopython is an excellent choice as a first Programming language without sacri-ficing the power and advanced capabilities that users will eventually pictures of snakes often appear on Python books and websites,the name is derived from Guido van Rossum s favorite TV show, MontyPython s Flying Circus . For this reason, lots of online and print documen-tation for the language has a light and humorous touch. Interestingly, manyexperienced programmers report that Python has brought back a lot of the78 CHAPTER 1. INTRODUCTIONfun they used to have Programming , so van Rossum s inspiration may be wellexpressed in the language The very Basics of PythonThere are a few features of Python which are different than other program-ming languages, and which should be mentioned early on so that subsequentexamples don t seem confusing.

7 Further information on all of these featureswill be provided later, when the topics are covered in statements do not need to end with a special character thepython interpreter knows that you are done with an individual statementby the presence of a newline, which will be generated when you press the Return key of your keyboard. If a statement spans more than one line, thesafest Course of action is to use a backslash (\) at the end of the line to letpython know that you are going to continue the statement on the next line;you can continue using backslashes on additional continuation lines. (Thereare situations where the backslashes are not needed which will be discussedlater.) Python provides you with a certain level of freedom when composing aprogram, but there are some rules which must always be obeyed.

8 One ofthese rules, which some people find very surprising, is that Python uses in-dentation (that is, the amount of white space before the statement itself) toindicate the presence of loops, instead of using delimiters like curly braces({}) or keywords (like begin and end ) as in many other languages. Theamount of indentation you use is not important, but it must be consistentwithin a given depth of a loop, and statements which are not indented mustbegin in the first column. Most Python programmers prefer to use an edi-tor likeemacs, which automatically provides consistent indentation; you willprobably find it easier to maintain your programs if you use consistent in-dentation in every loop, at all depths, and an intelligent editor is very usefulin achieving Invoking PythonThere are three ways to invoke Python , each with its own uses.

9 The firstway is to type Python at the shell command prompt. This brings up INVOKING PYTHON9python interpreter with a message similar to this one: Python (#2, Aug 27 2002, 09:01:47)[GCC 20011002 (Debian prerelease)] on linux2 Type "help", "copyright", "credits" or "license" for more three greater-than signs (>>>) represent Python s prompt; you type yourcommands after the prompt, and hit return for Python to execute them. Ifyou ve typed an executable statement, Python will execute it immediatelyand display the results of the statement on the screen. For example, if I usepython sprintstatement to print the famous Hello, world greeting, I llimmediately see a response:>>> print hello,world hello,worldThe print statement automatically adds a newline at the end of the printedstring. This is true regardless of how Python is invoked.

10 (You can suppressthe newline by following the string to be printed with a comma.)When using the Python interpreter this way, it executes statements im-mediately, and, unless the value of an expression is assigned to a variable(See Section ), Python will display the value of that expression as soon asit s typed. This makes Python a very handy calculator:>>> cost = >>> taxrate = .075>>> cost * >>> 16 + 25 + 92 * 3317 When you use Python interactively and wish to use a loop, you must,as always, indent the body of the loop consistently when you type yourstatements. Python can t execute your statements until the completion ofthe loop, and as a reminder, it changes its prompt from greater-than signs toperiods. Here s a trivial loop that prints each letter of a word on a separateline notice the change in the prompt, and that Python doesn t responduntil you enter a completely blank 1.


Related search queries