lecture 9 - dsa - python data structures
DESCRIPTION
TRANSCRIPT
Data Structures in PythonCrash Course
Dr.Haitham A. El-Ghareeb
Information Systems DepartmentFaculty of Computers and Information Sciences
Mansoura University
December 9, 2012
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 1 / 61
Agenda
• Whe*ng Your Appe0te • Using Python Interpreter • Informal Introduc0on to Python • More Control Flows • Data Structures • Modules
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 2 / 61
Python
• Easy to learn, powerful programming language.
• Has efficient high-‐level data structures and a simple but effec0ve approach to object-‐oriented programming. – elegant syntax – dynamic typing – interpreted nature
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 3 / 61
Python
• Simpler to use • Available on Windows, Mac OS X, and Unix • Help you get the job done more quickly • Split your program into modules that can be reused
• Python is an interpreted language: – Save you considerable 0me during program development because no compila0on and linking is necessary.
– Interpreter can be used interac0vely
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 4 / 61
Python
• Python is extensible: if you know how to program in C it is easy to add a new built-‐in func0on or module to the interpreter,
• Named aSer the BBC show “Monty Python’s Flying Circus” and has nothing to do with rep0les.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 5 / 61
Using the Python Interpreter
• Python interpreter is usually installed as /usr/local/bin/python
• Invoking the Interpreter – A second way of star0ng the interpreter is python -‐c command [arg] ..., which executes the statement(s) in command
– Some Python modules are also useful as scripts. These can be invoked using python -‐m module [arg] ..., which executes the source file for module
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 6 / 61
Using Python Interpreter
• Argument Passing • Interac0ve Mode
• Error Handling
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 7 / 61
Executable Python Scripts
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 8 / 61
Source Code Encoding
• It is possible to use encodings different than ASCII in Python source files.
• The best way to do it is to put one more special comment line right aSer the #! line to define the source file encoding:
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 9 / 61
Informal Introduc0on
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 10 / 61
Using Python as a Calculator
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 11 / 61
Numbers
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 12 / 61
Strings
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 13 / 61
Strings
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 14 / 61
String Concatena0on
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 15 / 61
String Indexing
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 16 / 61
String Slicing
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 17 / 61
Strings are Immutable!
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 18 / 61
Indices May be Nega0ve
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 19 / 61
How Come?!
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 20 / 61
Lists
• compound data type • used to group together other values • The most versa0le • can be wricen as a list of comma-‐separated values (items) between square brackets.
• List items need not all have the same type.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 21 / 61
Shallow Copy
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 22 / 61
First Steps toward Programming
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 23 / 61
More Control Flow Tools
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 24 / 61
If Statement
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 25 / 61
For Statement
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 26 / 61
For Statement with Shallow Copy
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 27 / 61
Range Func0on
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 28 / 61
Break, Con0nue, Else
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 29 / 61
Pass
• Does Nothing!
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 30 / 61
Func0ons
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 31 / 61
Return Statement
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 32 / 61
Default Argument Value
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 33 / 61
Documenta0on String
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 34 / 61
Coding Style -‐ PEP8
• Use 4-‐space indenta0on, and no tabs. • 4 spaces are a good compromise between small indenta0on (allows greater nes0ng depth) and large indenta0on (easier to read). Tabs introduce confusion, and are best leS out.
• Wrap lines so that they don’t exceed 79 characters. • This helps users with small displays and makes it possible to have several code files side-‐by-‐side on larger displays.
• Use blank lines to separate func0ons and classes, and larger blocks of code inside func0ons.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 35 / 61
Coding Style -‐ PEP8 • When possible, put comments on a line of their own. • Use docstrings. • Use spaces around operators and aSer commas, but not
directly inside bracke0ng constructs: a = f(1, 2) + g(3, 4). • Name your classes and func0ons consistently; the
conven0on is to use CamelCase for classes and lower_case_with_underscores for func0ons and methods. Always use self as the name for the first method argument.
• Don’t use fancy encodings if your code is meant to be used in interna0onal environments. Plain ASCII works best in any case.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 36 / 61
Data Structures -‐ Lists • list.append(x) Add an item to the end of the list. • list.extend(L) Extend the list by appending all the items in the given list.
• list.insert(i, x) Insert an item at a given posi0on. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
• list.remove(x) Remove the first item from the list whose value is x. It is an error if there is no such item.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 37 / 61
Data Structures -‐ Lists
• list.pop([i]) Remove the item at the given posi0on in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list.
• list.index(x) Return the index in the list of the first item whose value is x. It is an error if there is no such item.
• list.count(x) Return the number of 0mes x appears in the list.
• list.sort() Sort the items of the list, in place. • list.reverse() Reverse the elements of the list, in place.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 38 / 61
Lists Example
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 39 / 61
Using Lists as Stack
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 40 / 61
Using List as a Queue
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 41 / 61
Func0onal Programming Tools
• There are three built-‐in func0ons that are very useful when used with lists: filter(), map(), and reduce().
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 42 / 61
Filter()
• filter(func0on, sequence) returns a sequence consis0ng of those items from the sequence for which func0on(item) is true.
• If sequence is a string or tuple, the result will be of the same type; otherwise, it is always a list.
• For example, to compute a sequence of numbers not divisible by 2 and 3:
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 43 / 61
Filter()
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 44 / 61
Map()
• map(func0on, sequence) calls func0on(item) for each of the sequence’s items and returns a list of the return values.
• For example, to compute some cubes:
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 45 / 61
Map()
• More than one sequence may be passed; the func0on must then have as many arguments as there are sequences and is called with the corresponding item from each sequence (or None if some sequence is shorter than another). For example:
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 46 / 61
Reduce()
• reduce(func0on, sequence) returns a single value constructed by calling the binary func0on func2on on the first two items of the sequence, then on the result and the next item, and so on.
• For example, to compute the sum of the numbers 1 through 10:
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 47 / 61
Tuples and Sequences
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 48 / 61
Comma at the end!
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 49 / 61
Sets
• A set is an unordered collec0on with no duplicate elements.
• Basic uses include membership tes0ng and elimina0ng duplicate entries.
• Set objects also support mathema0cal opera0ons like union, intersec0on, difference, and symmetric difference.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 50 / 61
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 51 / 61
Dic0onaries • Dic0onaries are some0mes found in other languages as “associa0ve memories” or “associa0ve arrays”.
• Unlike sequences, which are indexed by a range of numbers, dic0onaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys.
• Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 52 / 61
Dic0onaries
• You can’t use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like append() and extend().
• It is best to think of a dic0onary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dic0onary).
• A pair of braces creates an empty dic0onary: {}. Placing a comma-‐separated list of key:value pairs within the braces adds ini0al key:value pairs to the dic0onary; this is also the way dic0onaries are wricen on output.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 53 / 61
Dic0onaries • The main opera0ons on a dic0onary are storing a value with some key and extrac0ng the value given the key.
• It is also possible to delete a key:value pair with del. If you store using a key that is already in use, the old value associated with that key is forgocen. It is an error to extract a value using a non-‐existent key.
• The keys() method of a dic0onary object returns a list of all the keys used in the dic0onary, in arbitrary order (if you want it sorted, just apply the sorted() func0on to it).
• To check whether a single key is in the dic0onary, use the in keyword.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 54 / 61
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 55 / 61
Dict() Constructor
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 56 / 61
Looping Techniques
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 57 / 61
Looping over Two Sequences
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 58 / 61
Loop in Reverse
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 59 / 61
More on Condi0ons
• Include and, or
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 60 / 61
Comparing Objects of Different Types
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 61 / 61