Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Python Modified from Chen Lin Chen Lin Guido van Rossum Guido van Rossum Mark Hammond Mark Hammond John Zelle John Zelle.

Similar presentations


Presentation on theme: "Introduction to Python Modified from Chen Lin Chen Lin Guido van Rossum Guido van Rossum Mark Hammond Mark Hammond John Zelle John Zelle."— Presentation transcript:

1 Introduction to Python Modified from Chen Lin Chen Lin Guido van Rossum Guido van Rossum Mark Hammond Mark Hammond John Zelle John Zelle

2 What Is Python? Created in 1990 by Guido van Rossum Created in 1990 by Guido van Rossum While at CWI, Amsterdam While at CWI, Amsterdam Now hosted by centre for national research initiatives, Reston, VA, USA Now hosted by centre for national research initiatives, Reston, VA, USA Free, open source Free, open source And with an amazing community And with an amazing community Object oriented language Object oriented language “Everything is an object” “Everything is an object”

3 Why Python? Designed to be easy to learn and master Designed to be easy to learn and master Clean, clear syntax Clean, clear syntax Very few keywords Very few keywords Highly portable Highly portable Runs almost anywhere - high end servers and workstations, down to windows CE Runs almost anywhere - high end servers and workstations, down to windows CE Uses machine independent byte-codes Uses machine independent byte-codes Extensible Extensible Designed to be extensible using C/C++, allowing access to many external libraries Designed to be extensible using C/C++, allowing access to many external libraries

4 Most obvious and notorious features Clean syntax plus high-level data types Clean syntax plus high-level data types Leads to fast coding Leads to fast coding Uses white-space to delimit blocks Uses white-space to delimit blocks Humans generally do, so why not the language? Humans generally do, so why not the language? Variables do not need declaration Variables do not need declaration Although not a type-less language Although not a type-less language

5 4 Major Versions of Python “Python” or “CPython” is written in C/C++ Version 2.7 Version 3.3 “Jython” is written in Java for the JVM “IronPython” is written in C# for the.Net environment

6 Pydev with Eclipse

7 Python Interactive Shell % python Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> You can type things directly into a running Python session >>> 2+3*4 14 >>> name = "Andrew" >>> name 'Andrew' >>> print "Hello", name Hello Andrew >>>

8 Background Background Data Types/Structure Data Types/Structure Control flow Control flow File I/O File I/O Modules Modules

9 “Hello World” in Python print "hello World!"

10 Blocks Blocks are delimited by indentation Blocks are delimited by indentation Colon used to start a block Colon used to start a block Tabs or spaces may be used Tabs or spaces may be used Maxing tabs and spaces works, but is discouraged Maxing tabs and spaces works, but is discouraged >>> if 1:... print "True"... True >>> >>> if 1:... print "True"... True >>>

11 Python Build-in types Numbers 3.1415 Numbers 3.1415 Strings “Hello World” Strings “Hello World” Lists [1,2,3,4] Lists [1,2,3,4] Dictionaries {‘test’: ‘yum’} Dictionaries {‘test’: ‘yum’} Files input=open(‘file.txt’, ‘r’) Files input=open(‘file.txt’, ‘r’)

12 Variables and Types Objects always have a type Objects always have a type >>> a = 1 >>> type(a) >>> a = "Hello" >>> type(a) >>> type(1.0) >>> a = 1 >>> type(a) >>> a = "Hello" >>> type(a) >>> type(1.0)

13 Number in Action >>> a=3 #Name Created >>> b=4 >>> a+1, a-1 # (3+1), (3-1) (4,2) >>> b*3, b/2 (12,2)

14 Simple Data Types Integer objects implemented using C longs Integer objects implemented using C longs Like C, integer division returns the floor Like C, integer division returns the floor >>> 5/2 2 >>> 5/2 2 Float types implemented using C doubles Float types implemented using C doubles No point in having single precision since execution overhead is large anyway No point in having single precision since execution overhead is large anyway

15 Simple Data Types Long Integers have unlimited size Long Integers have unlimited size Limited only by available memory Limited only by available memory >>> long = 1L >> long ** 5 2135987035920910082395021706169552114602704522 3566527699470416078222197257806405500229620869 36576L >>> long = 1L >> long ** 5 2135987035920910082395021706169552114602704522 3566527699470416078222197257806405500229620869 36576L

16 High Level Data Types Tuples are similar to lists Tuples are similar to lists Sequence of items Sequence of items Key difference is they are immutable Key difference is they are immutable Often used in place of simple structures Often used in place of simple structures Automatic unpacking Automatic unpacking >>> point = 2,3 >>> x, y = point >>> x 2 >>> point = 2,3 >>> x, y = point >>> x 2

17 Reference Semantics Assignment manipulates references Assignment manipulates references x = y does not make a copy of y x = y does not make a copy of y x = y makes x reference the object y references x = y makes x reference the object y references Very useful; but beware! Very useful; but beware! Example: Example: >>> a = [1, 2, 3] >>> b = a >>> a.append(4) >>> print b [1, 2, 3, 4]

18 a 123 b a 123 b 4 a = [1, 2, 3] a.append(4) b = a a 123 Changing a Shared List

19 a 1 b a 1 b a = 1 a = a+1 b = a a 1 2 Changing an Integer old reference deleted by assignment (a=...) new int object created by add operator (1+1)

20 Strings The next major build-in type is the Python STRING --- an ordered collection of characters to store and represent text-based information The next major build-in type is the Python STRING --- an ordered collection of characters to store and represent text-based information Python strings are categorized as immutable sequences --- meaning they have a left-to-right order (sequence) and cannot be changed in place (immutable) Python strings are categorized as immutable sequences --- meaning they have a left-to-right order (sequence) and cannot be changed in place (immutable)

21 Single- and Double-Quoted Single- and Double-Quoted strings are the same Single- and Double-Quoted strings are the same >>> ‘Hello World’, “Hello World” The reason for including both is that it allows you to embed a quote character of the other inside a string The reason for including both is that it allows you to embed a quote character of the other inside a string >>> “knight’s”, ‘knight”s’

22 len() The len build-in function returns the length of strings The len build-in function returns the length of strings >>> len(‘abc’) >>> a=‘abc’ >>> len(a)

23 + Adding two string objects creates a new string object Adding two string objects creates a new string object >>> ‘abc’ + ‘def’ >>> a=‘Hello’ >>> b=‘World’ >>> a + b >>> a+ ‘ ’ +b

24 * Repetition may seem a bit obscure at first, but it comes in handy in a surprising number of contexts Repetition may seem a bit obscure at first, but it comes in handy in a surprising number of contexts For example, to print a line of 80 dashes For example, to print a line of 80 dashes >>> print ‘-’ * 80

25 25 Strings and Secret Codes In the early days of computers, each manufacturer used their own encoding of numbers for characters. In the early days of computers, each manufacturer used their own encoding of numbers for characters. ASCII system (American Standard Code for Information Interchange) uses 127 bit codes ASCII system (American Standard Code for Information Interchange) uses 127 bit codes Python supports Unicode (100,000+ characters) Python supports Unicode (100,000+ characters)

26 26 Strings and Secret Codes The ord function returns the numeric (ordinal) code of a single character. The ord function returns the numeric (ordinal) code of a single character. The chr function converts a numeric code to the corresponding character. The chr function converts a numeric code to the corresponding character. >>> ord("A") 65 >>> ord("a") 97 >>> chr(97) 'a' >>> chr(65) 'A'

27 Lists Lists are Python’s most flexible ordered collection object type Lists are Python’s most flexible ordered collection object type Lists can contain any sort of object: numbers, strings and even other lists Lists can contain any sort of object: numbers, strings and even other lists Ordered collections of arbitrary objects Ordered collections of arbitrary objects Accessed by offset Accessed by offset Variable length, heterogeneous, arbitrary nestable Variable length, heterogeneous, arbitrary nestable

28 List A compound data type: [0] [2.3, 4.5] [5, "Hello", "there", 9.8] [] Use len() to get the length of a list >>> names = [“Ben", “Chen", “Yaqin"] >>> len(names) 3

29 Use [ ] to index items in the list >>> names[0] ‘Ben' >>> names[1] ‘Chen' >>> names[2] ‘Yaqin' >>> names[3] Traceback (most recent call last): File " ", line 1, in File " ", line 1, in IndexError: list index out of range >>> names[-1] ‘Yaqin' >>> names[-2] ‘Chen' >>> names[-3] ‘Ben' [0] is the first item. [1] is the second item... Out of range values raise an exception Negative values go backwards from the last element.

30 Strings share many features with lists >>> smiles = "C(=N)(N)N.C(=O)(O)O" >>> smiles[0] 'C' >>> smiles[1] '(' >>> smiles[-1] 'O' >>> smiles[1:5] '(=N)' >>> smiles[10:-4] 'C(=O)' Use “slice” notation to get a substring

31 String Methods: find, split smiles = "C(=N)(N)N.C(=O)(O)O" >>> smiles.find("(O)") 15 >>> smiles.find(".") 9 >>> smiles.find(".", 10) >>> smiles.split(".") ['C(=N)(N)N', 'C(=O)(O)O'] >>> Use “find” to find the start of a substring. Start looking at position 10. Find returns -1 if it couldn’t find a match. Split the string into parts with “.” as the delimiter

32 String operators: in, not in if "Br" in “Brother”: print "contains brother“ email_address = “clin” if "@" not in email_address: email_address += "@brandeis.edu“

33 String Method: “strip”, “rstrip”, “lstrip” are ways to remove whitespace or selected characters >>> line = " # This is a comment line \n" >>> line.strip() '# This is a comment line' >>> line.rstrip() ' # This is a comment line' >>> line.rstrip("\n") ' # This is a comment line ' >>>

34 More String methods email.startswith(“c") endswith(“u”) True/False >>> "%s@brandeis.edu" % "clin" 'clin@brandeis.edu' >>> names = [“Ben", “Chen", “Yaqin"] >>> ", ".join(names) ‘Ben, Chen, Yaqin‘ >>> “chen".upper() ‘CHEN'

35 Unexpected things about strings >>> s = "andrew" >>> s[0] = "A" Traceback (most recent call last): File " ", line 1, in File " ", line 1, in TypeError: 'str' object does not support item assignment >>> s = "A" + s[1:] >>> s 'Andrew‘ Strings are read only

36 “\” is for special characters \n -> newline \t -> tab \\ -> backslash... But Windows uses backslash for directories! filename = "M:\nickel_project\reactive.smi" # DANGER! filename = "M:\\nickel_project\\reactive.smi" # Better! filename = "M:/nickel_project/reactive.smi" # Usually works

37 Lists are mutable - some useful methods >>> ids = ["9pti", "2plv", "1crn"] >>> ids.append("1alm") >>> ids ['9pti', '2plv', '1crn', '1alm'] >>>ids.extend(L) Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L. Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L. >>> del ids[0] >>> ids ['2plv', '1crn', '1alm'] >>> ids.sort() >>> ids ['1alm', '1crn', '2plv'] >>> ids.reverse() >>> ids ['2plv', '1crn', '1alm'] >>> ids.insert(0, "9pti") >>> ids ['9pti', '2plv', '1crn', '1alm'] append an element remove an element sort by default order reverse the elements in a list insert an element at some specified position. (Slower than.append())

38 Tuples: Tuples: sort of an immutable list >>> yellow = (255, 255, 0) # r, g, b >>> one = (1,) >>> yellow[0] >>> yellow[1:] (255, 0) >>> yellow[0] = 0 Traceback (most recent call last): File " ", line 1, in File " ", line 1, in TypeError: 'tuple' object does not support item assignment Very common in string interpolation: >>> "%s lives in %s at latitude %.1f" % ("Andrew", "Sweden", 57.7056) 'Andrew lives in Sweden at latitude 57.7'

39 zipping lists together >>> names ['ben', 'chen', 'yaqin'] >>> gender = [0, 0, 1] >>> zip(names, gender) [('ben', 0), ('chen', 0), ('yaqin', 1)]

40 High Level Data Types Dictionaries hold key-value pairs Dictionaries hold key-value pairs Often called maps or hashes. Implemented using hash-tables Often called maps or hashes. Implemented using hash-tables Keys may be any immutable object, values may be any object Keys may be any immutable object, values may be any object Declared using braces Declared using braces >>> d={} >>> d[0] = "Hi there" >>> d["foo"] = 1 >>> d={} >>> d[0] = "Hi there" >>> d["foo"] = 1

41 Dictionaries Dictionaries are lookup tables. They map from a “key” to a “value”. symbol_to_name = { "H": "hydrogen", "He": "helium", "Li": "lithium", "C": "carbon", "O": "oxygen", "N": "nitrogen" } Duplicate keys are not allowed Duplicate values are just fine

42 Keys can be any immutable value numbers, strings, tuples, frozenset, not list, dictionary, set,... atomic_number_to_name = { 1: "hydrogen" 6: "carbon", 7: "nitrogen" 8: "oxygen", } nobel_prize_winners = { (1979, "physics"): ["Glashow", "Salam", "Weinberg"], (1962, "chemistry"): ["Hodgkin"], (1984, "biology"): ["McClintock"], } A set is an unordered collection with no duplicate elements.

43 Dictionary >>> symbol_to_name["C"] 'carbon' >>> "O" in symbol_to_name, "U" in symbol_to_name (True, False) >>> "oxygen" in symbol_to_name False >>> symbol_to_name["P"] Traceback (most recent call last): File " ", line 1, in File " ", line 1, in KeyError: 'P' >>> symbol_to_name.get("P", "unknown") 'unknown' >>> symbol_to_name.get("C", "unknown") 'carbon' Get the value for a given key Test if the key exists (“in” only checks the keys, not the values.) [] lookup failures raise an exception. Use “.get()” if you want to return a default value.

44 Some useful dictionary methods >>> symbol_to_name.keys() ['C', 'H', 'O', 'N', 'Li', 'He'] >>> symbol_to_name.values() ['carbon', 'hydrogen', 'oxygen', 'nitrogen', 'lithium', 'helium'] >>> symbol_to_name.update( {"P": "phosphorous", "S": "sulfur"} ) >>> symbol_to_name.items() [('C', 'carbon'), ('H', 'hydrogen'), ('O', 'oxygen'), ('N', 'nitrogen'), ('P', 'phosphorous'), ('S', 'sulfur'), ('Li', 'lithium'), ('He', 'helium')] >>> del symbol_to_name['C'] >>> symbol_to_name {'H': 'hydrogen', 'O': 'oxygen', 'N': 'nitrogen', 'Li': 'lithium', 'He': 'helium'}

45 Background Background Data Types/Structure Data Types/Structure list, string, tuple, dictionary Control flow Control flow File I/O File I/O Modules Modules

46 Control Flow Things that are False The boolean value False The numbers 0 (integer), 0.0 (float) and 0j (complex). The empty string "". The empty list [], empty dictionary {} and empty set set(). Things that are True The boolean value True The boolean value True All non-zero numbers. All non-zero numbers. Any string containing at least one character. Any string containing at least one character. A non-empty data structure. A non-empty data structure.

47 If >>> smiles = "BrC1=CC=C(C=C1)NN.Cl" >>> bool(smiles) True >>> not bool(smiles) False >>> if not smiles:... print "The SMILES string is empty"... The “else” case is always optional

48 Use “elif” to chain subsequent tests >>> mode = "absolute" >>> if mode == "canonical":... smiles = "canonical"... elif mode == "isomeric":... smiles = "isomeric”... elif mode == "absolute":... smiles = "absolute"... else:... raise TypeError("unknown mode")... >>> smiles ' absolute ' >>> “raise” is the Python way to raise exceptions

49 Boolean logic Python expressions can have “and”s and “or”s: if (ben = 10 or chen == 500 and ben != 5): print “Ben and Chen“

50 Range Test if (3 <= Time <= 5): print “Office Hour"

51 Looping The for statement loops over sequences The for statement loops over sequences >>> for ch in "Hello":... print ch... H e l l o >>> >>> for ch in "Hello":... print ch... H e l l o >>>

52 For >>> names = [“Ben", “Chen", “Yaqin"] >>> for name in names:... print smiles...BenChenYaqin

53 Tuple assignment in for loops data = [ ("C20H20O3", 308.371), ("C22H20O2", 316.393), ("C24H40N4O2", 416.6), ("C14H25N5O3", 311.38), ("C15H20O2", 232.3181)] for (formula, mw) in data: print "The molecular weight of %s is %s" % (formula, mw) The molecular weight of C20H20O3 is 308.371 The molecular weight of C22H20O2 is 316.393 The molecular weight of C24H40N4O2 is 416.6 The molecular weight of C14H25N5O3 is 311.38 The molecular weight of C15H20O2 is 232.3181

54 Break, continue >>> for value in [3, 1, 4, 1, 5, 9, 2]:... print "Checking", value... if value > 8:... print "Exiting for loop"... break... elif value < 3:... print "Ignoring"... continue... print "The square is", value**2... Use “break” to stop the for loop Use “continue” to stop processing the current item Checking 3 The square is 9 Checking 1 Ignoring Checking 4 The square is 16 Checking 1 Ignoring Checking 5 The square is 25 Checking 9 Exiting for loop >>>

55 Range() “range” creates a list of numbers in a specified range “range” creates a list of numbers in a specified range range([start,] stop[, step]) -> list of integers range([start,] stop[, step]) -> list of integers When step is given, it specifies the increment (or decrement). When step is given, it specifies the increment (or decrement). >>> range(5) [0, 1, 2, 3, 4] >>> range(5, 10) [5, 6, 7, 8, 9] >>> range(0, 10, 2) [0, 2, 4, 6, 8] How to get every second element in a list? for i in range(0, len(data), 2): print data[i]

56 Functions Functions are defined with the def statement: Functions are defined with the def statement: >>> def foo(bar):... return bar >>> >>> def foo(bar):... return bar >>> This defines a trivial function named foo that takes a single parameter bar This defines a trivial function named foo that takes a single parameter bar

57 Functions A function definition simply places a function object in the namespace A function definition simply places a function object in the namespace >>> foo >>> >>> foo >>> And the function object can obviously be called: And the function object can obviously be called: >>> foo(3) 3 >>> >>> foo(3) 3 >>>

58 Functions, Procedures def name(arg1, arg2,...): """documentation"""# optional doc string """documentation"""# optional doc string statements statements return# from procedure return expression# from function

59 Example Function def gcd(a, b): "greatest common divisor" "greatest common divisor" while a != 0: while a != 0: a, b = b%a, a # parallel assignment a, b = b%a, a # parallel assignment return b return b >>> gcd.__doc__ 'greatest common divisor' >>> gcd(12, 20) 4

60 Classes Classes are defined using the class statement Classes are defined using the class statement >>> class Foo:... def __init__(self):... self.member = 1... def GetMember(self):... return self.member... >>> >>> class Foo:... def __init__(self):... self.member = 1... def GetMember(self):... return self.member... >>>

61 Classes A few things are worth pointing out in the previous example: A few things are worth pointing out in the previous example: The constructor has a special name __init__, while a destructor (not shown) uses __del__ The constructor has a special name __init__, while a destructor (not shown) uses __del__ The self parameter is the instance (ie, the this in C++). In Python, the self parameter is explicit (c.f. C++, where it is implicit) The self parameter is the instance (ie, the this in C++). In Python, the self parameter is explicit (c.f. C++, where it is implicit) The name self is not required - simply a convention The name self is not required - simply a convention

62 Classes Like functions, a class statement simply adds a class object to the namespace Like functions, a class statement simply adds a class object to the namespace >>> Foo >>> >>> Foo >>> Classes are instantiated using call syntax Classes are instantiated using call syntax >>> f=Foo() >>> f.GetMember() 1 >>> f=Foo() >>> f.GetMember() 1

63 Example class class Stack: "A well-known data structure…" "A well-known data structure…" def __init__(self):# constructor def __init__(self):# constructor self.items = [] self.items = [] def push(self, x): def push(self, x): self.items.append(x)# the sky is the limit self.items.append(x)# the sky is the limit def pop(self): def pop(self): x = self.items[-1]# what happens if it’s empty? x = self.items[-1]# what happens if it’s empty? del self.items[-1] del self.items[-1] return x return x def empty(self): def empty(self): return len(self.items) == 0# Boolean result return len(self.items) == 0# Boolean result

64 Using classes To create an instance, simply call the class object: To create an instance, simply call the class object: x = Stack()# no 'new' operator! To use methods of the instance, call using dot notation: To use methods of the instance, call using dot notation: x.empty()# -> 1 x.push(1)# [1] x.empty()# -> 0 x.push("hello")# [1, "hello"] x.pop()# -> "hello"# [1] To inspect instance variables, use dot notation: To inspect instance variables, use dot notation: x.items# -> [1]

65 Subclassing class FancyStack(Stack): "stack with added ability to inspect inferior stack items" "stack with added ability to inspect inferior stack items" def peek(self, n): def peek(self, n): "peek(0) returns top; peek(-1) returns item below that; etc." "peek(0) returns top; peek(-1) returns item below that; etc." size = len(self.items) size = len(self.items) assert 0 <= n < size# test precondition assert 0 <= n < size# test precondition return self.items[size-1-n] return self.items[size-1-n]

66 Subclassing (2) class LimitedStack(FancyStack): "fancy stack with limit on stack size" "fancy stack with limit on stack size" def __init__(self, limit): def __init__(self, limit): self.limit = limit self.limit = limit FancyStack.__init__(self)# base class constructor FancyStack.__init__(self)# base class constructor def push(self, x): def push(self, x): assert len(self.items) < self.limit assert len(self.items) < self.limit FancyStack.push(self, x)# "super" method call FancyStack.push(self, x)# "super" method call

67 Class & instance variables class Connection: verbose = 0# class variable verbose = 0# class variable def __init__(self, host): def __init__(self, host): self.host = host# instance variable self.host = host# instance variable def debug(self, v): def debug(self, v): self.verbose = v# make instance variable! self.verbose = v# make instance variable! def connect(self): def connect(self): if self.verbose:# class or instance variable? if self.verbose:# class or instance variable? print "connecting to", self.host print "connecting to", self.host

68 Instance variable rules On use via instance (self.x), search order: On use via instance (self.x), search order: (1) instance, (2) class, (3) base classes (1) instance, (2) class, (3) base classes this also works for method lookup this also works for method lookup On assigment via instance (self.x =...): On assigment via instance (self.x =...): always makes an instance variable always makes an instance variable Class variables "default" for instance variables Class variables "default" for instance variables But...! But...! mutable class variable: one copy shared by all mutable class variable: one copy shared by all mutable instance variable: each instance its own mutable instance variable: each instance its own

69 Exceptions Python uses exceptions for errors Python uses exceptions for errors try / except block can handle exceptions try / except block can handle exceptions >>> try:... 1/0... except ZeroDivisionError:... print "Eeek"... Eeek >>> >>> try:... 1/0... except ZeroDivisionError:... print "Eeek"... Eeek >>>

70 Exceptions try / finally block can guarantee execute of code even in the face of exceptions try / finally block can guarantee execute of code even in the face of exceptions >>> try:... 1/0... finally:... print "Doing this anyway"... Doing this anyway Traceback (innermost last): File " ", line 2, in ? ZeroDivisionError: integer division or modulo >>> >>> try:... 1/0... finally:... print "Doing this anyway"... Doing this anyway Traceback (innermost last): File " ", line 2, in ? ZeroDivisionError: integer division or modulo >>>

71 More on exceptions User-defined exceptions User-defined exceptions subclass Exception or any other standard exception subclass Exception or any other standard exception Old Python: exceptions can be strings Old Python: exceptions can be strings WATCH OUT: compared by object identity, not == WATCH OUT: compared by object identity, not == Last caught exception info: Last caught exception info: sys.exc_info() == (exc_type, exc_value, exc_traceback) sys.exc_info() == (exc_type, exc_value, exc_traceback) Last uncaught exception (traceback printed): Last uncaught exception (traceback printed): sys.last_type, sys.last_value, sys.last_traceback sys.last_type, sys.last_value, sys.last_traceback Printing exceptions: traceback module Printing exceptions: traceback module

72 Background Background Data Types/Structure Data Types/Structure Control flow Control flow File I/O File I/O Modules Modules

73 A file is a sequence of data that is stored in secondary memory (disk drive). A file is a sequence of data that is stored in secondary memory (disk drive). Files can contain any data type, but the easiest to work with are text. Files can contain any data type, but the easiest to work with are text. A file usually contains more than one line of text. A file usually contains more than one line of text. Python uses the standard newline character (\n) to mark line breaks. Python uses the standard newline character (\n) to mark line breaks. Files: Multi-line Strings

74 File Processing Reading a file into a word processor Reading a file into a word processor File opened File opened Contents read into RAM Contents read into RAM File closed File closed Changes to the file are made to the copy stored in memory, not on the disk Changes to the file are made to the copy stored in memory, not on the disk It has to be flushed It has to be flushed

75 File Objects f = open(filename[, mode[, buffersize]) f = open(filename[, mode[, buffersize]) mode can be "r", "w", "a" (like C stdio); default "r" mode can be "r", "w", "a" (like C stdio); default "r" append "b" for text translation mode append "b" for text translation mode append "+" for read/write open append "+" for read/write open buffersize: 0=unbuffered; 1=line-buffered; buffered buffersize: 0=unbuffered; 1=line-buffered; buffered methods: methods: read([nbytes]), readline(), readlines() read([nbytes]), readline(), readlines() write(string), writelines(list) write(string), writelines(list) seek(pos[, how]), tell() seek(pos[, how]), tell() flush(), close() flush(), close() fileno() fileno()

76 Reading files >>> f = open(“names.txt") >>> f.readline() 'Yaqin\n'

77 File Processing readline can be used to read the next line from a file, including the trailing newline character readline can be used to read the next line from a file, including the trailing newline character infile = open(someFile, " r " ) for i in range(5): line = infile.readline() print line[:-1] infile = open(someFile, " r " ) for i in range(5): line = infile.readline() print line[:-1] This reads the first 5 lines of a file This reads the first 5 lines of a file Slicing is used to strip out the newline characters at the ends of the lines Slicing is used to strip out the newline characters at the ends of the lines

78 File Processing Another way to loop through the contents of a file is to read it in with readlines and then loop through the resulting list. Another way to loop through the contents of a file is to read it in with readlines and then loop through the resulting list. infile = open(someFile, " r " ) for line in infile.readlines(): # Line processing here infile.close() infile = open(someFile, " r " ) for line in infile.readlines(): # Line processing here infile.close()

79 File Processing Python treats the file itself as a sequence of lines! Python treats the file itself as a sequence of lines! Infile = open(someFile, " r " ) for line in infile: # process the line here infile.close() Infile = open(someFile, " r " ) for line in infile: # process the line here infile.close()

80 Quick Way >>> lst= [ x for x in open("text.txt","r").readlines() ] >>> lst ['Chen Lin\n', 'clin@brandeis.edu\n', 'Volen 110\n', 'Office Hour: Thurs. 3-5\n', '\n', 'Yaqin Yang\n', 'yaqin@brandeis.edu\n', 'Volen 110\n', 'Offiche Hour: Tues. 3-5\n'] Ignore the header? for (i,line) in enumerate(open(‘text.txt’,"r").readlines()): if i == 0: continue if i == 0: continue print line print line

81 Using dictionaries to count occurrences >>> for line in open('names.txt'):... name = line.strip()... name_count[name] = name_count.get(name,0)+ 1... >>> for (name, count) in name_count.items():... print name, count... Chen 3 Ben 3 Yaqin 3

82 File Processing Opening a file for writing prepares the file to receive data Opening a file for writing prepares the file to receive data If you open an existing file for writing, you wipe out the file ’ s contents. If the named file does not exist, a new one is created. If you open an existing file for writing, you wipe out the file ’ s contents. If the named file does not exist, a new one is created. Outfile = open( " mydata.out ", " w " ) Outfile = open( " mydata.out ", " w " ) print(, file=Outfile) print(, file=Outfile)

83 File Output input_file = open(“in.txt") output_file = open(“out.txt", "w") for line in input_file: output_file.write(line) “w” = “write mode” “a” = “append mode” “wb” = “write in binary” “r” = “read mode” (default) “rb” = “read in binary” “U” = “read files with Unix or Windows line endings”

84 Background Background Data Types/Structure Data Types/Structure Control flow Control flow File I/O File I/O Modules Modules

85 Modules When a Python program starts it only has access to a basic functions and classes. (“int”, “dict”, “len”, “sum”, “range”,...) “Modules” contain additional functionality. Modules can be implemented either in Python, or in C/C++ Modules can be implemented either in Python, or in C/C++ Use “import” to tell Python to load a module. >>> import math

86 import the math module >>> import math >>> math.pi 3.1415926535897931 >>> math.cos(0) 1.0 >>> math.cos(math.pi) >>> dir(math) ['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'] >>> help(math) >>> help(math.cos)

87 “import” and “from... import...” >>> import math math.cos >>> from math import cos, pi cos >>> from math import *

88 Standard Library Python comes standard with a set of modules, known as the “standard library” Python comes standard with a set of modules, known as the “standard library” Rich and diverse functionality available from the standard library Rich and diverse functionality available from the standard library All common internet protocols, sockets, CGI, OS services, GUI services (via Tcl/Tk), database, Berkeley style databases, calendar, Python parser, file globbing/searching, debugger, profiler, threading and synchronisation, persistency, etc All common internet protocols, sockets, CGI, OS services, GUI services (via Tcl/Tk), database, Berkeley style databases, calendar, Python parser, file globbing/searching, debugger, profiler, threading and synchronisation, persistency, etc

89 External library Many modules are available externally covering almost every piece of functionality you could ever desire Many modules are available externally covering almost every piece of functionality you could ever desire Imaging, numerical analysis, OS specific functionality, SQL databases, Fortran interfaces, XML, Corba, COM, Win32 API, etc Imaging, numerical analysis, OS specific functionality, SQL databases, Fortran interfaces, XML, Corba, COM, Win32 API, etc

90 For More Information? http://python.org/ - documentation, tutorials, beginners guide, core distribution,... Books include: Learning Python by Mark Lutz Python Essential Reference by David Beazley Python Cookbook, ed. by Martelli, Ravenscroft and Ascher (online at http://code.activestate.com/recipes/langs/python/) http://wiki.python.org/moin/PythonBooks

91 Python Videos http://showmedo.com/videotutorials/python “5 Minute Overview (What Does Python Look Like?)” “Introducing the PyDev IDE for Eclipse” “Linear Algebra with Numpy” And many more


Download ppt "Introduction to Python Modified from Chen Lin Chen Lin Guido van Rossum Guido van Rossum Mark Hammond Mark Hammond John Zelle John Zelle."

Similar presentations


Ads by Google