Introduction to Python

advertisement
Introducing Python
CS 4320, SPRING 2015
Resources
We will be following the Python tutorial
These notes will cover the following sections and subsections:
◦
◦
◦
◦
◦
◦
◦
Section 1: Whetting your Appetite
Section 3: An Informal Introduction to Python
Subsections 4.1-4.3, 4.5 and 4.6: More Control Flow
Subsubsections 4.7.1-4.7.3: More on defining functions
Subsubsections 5.1.1-5.1.2: More on Lists
Subsection 5.3: Tuples
Section 6, 6.1, 6.1.1, 6.4: Modules
You are encouraged to read more than the minimum and, of course, to try out the examples
presented in the tutorial
1/7/2015
CS 4320, SPRING 2015
2
Sources of Information
The most important source about Python is the documentation for the Python Standard Library
◦ As the Python site says: ‘keep this under your pillow’
The Python documentation page points to other good sources
1/7/2015
CS 4320, SPRING 2015
3
History
Developed by Guido van Rossum in the late 1980’s
Now developed to version 3.x
Python is multi-paradigm: imperative and OO and some functional
1/7/2015
CS 4320, SPRING 2015
4
Lexical Structure
Two aspects of Python syntax may be challenging to Java programmers
Indenting
◦ Indenting is syntactically important
◦ Blocks of code, as inside an ‘if’, are indicated by indenting
◦ Code that is sequential must not be indented
Statement continuation
◦ Statements normally finish at the end of a line
◦ A statement will continue to further lines if there are parentheses or braces still not matched
◦ A single backslash, \, at the end of a line will also continue the statement
◦ Note: no characters, including spaces, are allowed after \ in this case
Summary: In Python, white-space is important
1/7/2015
CS 4320, SPRING 2015
5
Global Level
Python allows definitions and code at the global level
That is, not everything has to be inside a class definition
The famous ‘hello world’ program can be one line:
◦ print(‘hello world!’)
This also means that variables can have a global scope: seen by every part of the code. Be
careful, global variables can make very difficult code
Many important pre-defined functions are free (Built-in Functions Reference)
1/7/2015
CS 4320, SPRING 2015
6
First Program
Creating a project
Creating a file
Running from the IDE
Running a script from the command line
◦ The interpreter definition line #!
◦ Change the mode of the file to executable
Running the Python interpreter interactively
1/7/2015
CS 4320, SPRING 2015
7
Printing
The print function is used to print data to standard output
Arguments must be enclosed in parentheses (a major difference from Python 2)
Arguments are separated by a space when printed
◦ So, print(“x is”, x) will leave a space between the label and the value
The print function inserts a new line after printing the arguments
◦ You can change this by using the keyword argument end=…
1/7/2015
CS 4320, SPRING 2015
8
Basic Data Types
Numeric
Strings
Lists
Tuples
Dictionaries
1/7/2015
CS 4320, SPRING 2015
9
Numeric Data
Float and Integer are subsumed under a single type
Integer arithmetic is arbitrary precision
When doing division
◦ / gives a float result
◦ // gives an integer result
1/7/2015
CS 4320, SPRING 2015
10
Strings
Strings are, as in Java, immutable sequences of characters
◦ The global len function will return the length of a string
String literals can be delimited by single or double quotes, these must be on one line
Strings can be delimited by ‘triple quotes’ (either ‘’’ or “””)
◦ This form can be multi-line
1/7/2015
CS 4320, SPRING 2015
11
Lists
Lists are ordered, indexed sequences of values
Lists are not typed, so pretty much any values can be mixed into a list
Square brackets are used to delimit a literal list in code
Indexing
◦
◦
◦
◦
◦
◦
Indexes start at 0
Negative indexes count from the end of the list, so s[-1] is the last element in list s
s[i] is the item at index I
s[k:j] is the sublist of items from k to j (not including j)
s[k:] is the sublist from k to the end
s[i:j:k] is the sublist from I to j in steps of k
1/7/2015
CS 4320, SPRING 2015
12
List Functions
len(s) is the length of list s
s + t is the concatenation of lists s and t
s.append(x) adds x to the end of list s
s.pop(i) removes and returns the element at index I
◦ s.pop() is the same as s.pop(-1)
1/7/2015
CS 4320, SPRING 2015
13
Tuples
Tuples are immutable lists
As literals, tuples are given by a comma separated list of values surrounded by parentheses
◦ (1,2,3)
Beware!
◦ (1) is not a tuple, it is simply the integer 1
◦ (1,) is a tuple with one element, the integer 1
Tuples turn up in some places, especially as the return values from some functions.
◦ Just be aware that tuples cannot be modified
1/7/2015
CS 4320, SPRING 2015
14
Dictionaries
Also known as ‘hash maps’, ‘associative memory’, and ‘maps’
A dictionary associates keys to values
A literal dictionary uses curly braces with pairs ‘key:value’
1/7/2015
CS 4320, SPRING 2015
15
Selection
The semantics of the if statement are familiar, however the syntax is different from Java
The body of each part of the ‘if’ must be indented. Four spaces is pretty standard.
◦ The indentation must be consistent throughout the body
If x < 3:
print(‘small’)
elif x < 5:
print(‘medium’)
else:
print(‘large’)
1/7/2015
CS 4320, SPRING 2015
16
Iteration
The ‘for’ loop is good for stepping through lists
The code below will print each element in the list s
for x in s:
print(x)
1/7/2015
CS 4320, SPRING 2015
17
Ranges
For stepping through ranges of integers, the range function is available
range(10) is the numbers from 0 to 9, inclusive
range(5,10) is the numbers from 5 to 9, inclusive
range(5,10,2) is the numbers 5, 7 and 9
1/7/2015
CS 4320, SPRING 2015
18
Download