COMP 144 Programming Language Concepts Felix Hernandez-Campos 1 Lecture 7: Python’s Built- Lecture 7: Python’s Built- in in Types and Basic Statements Types and Basic Statements COMP 144 Programming Language COMP 144 Programming Language Concepts Concepts Spring 2002 Spring 2002 Felix Hernandez-Campos Felix Hernandez-Campos Jan 25 Jan 25 The University of North Carolina at The University of North Carolina at Chapel Hill Chapel Hill
36
Embed
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 7: Python’s Built-in Types and Basic Statements COMP 144 Programming Language Concepts.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
11
Lecture 7: Python’s Built-in Lecture 7: Python’s Built-in Types and Basic StatementsTypes and Basic Statements
COMP 144 Programming Language ConceptsCOMP 144 Programming Language Concepts
Spring 2002Spring 2002
Felix Hernandez-CamposFelix Hernandez-Campos
Jan 25Jan 25
The University of North Carolina at Chapel HillThe University of North Carolina at Chapel Hill
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
22
Built-in Data Structures: ListsBuilt-in Data Structures: Lists
• A list is an A list is an ordered collection of objectsordered collection of objects
• Lists can contain Lists can contain anyany type of object type of object
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
33
Lists: Accessing ItemsLists: Accessing Items
• Syntax:Syntax: list[index]list[index]– Indexing from the left starts at 0Indexing from the left starts at 0– E.g.E.g.>>> >>> l = [1, ["2", 4], 3.0]l = [1, ["2", 4], 3.0]>>> >>> l[0]l[0]11>>> >>> l[2]l[2]3.03.0>>> >>> l[1]l[1]['2', 4]['2', 4]>>> >>> l[3] = 4l[3] = 4Traceback (most recent call last):Traceback (most recent call last): File "<pyshell#17>", line 1, in ?File "<pyshell#17>", line 1, in ? l[3] = 4l[3] = 4IndexError: list assignment IndexError: list assignment index out of rangeindex out of range
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
44
Lists: Accessing ItemsLists: Accessing Items
• Syntax:Syntax: list[-index]list[-index]– Indexing from the right starts at -1Indexing from the right starts at -1– E.g.E.g.>>> >>> l = [1, ["2", 4], 3.0]l = [1, ["2", 4], 3.0]>>> >>> l[-1]l[-1]3.03.0>>> >>> l[-3]l[-3]11>>> >>> l[-4]l[-4]Traceback (most recent call last):Traceback (most recent call last): File "<pyshell#29>", line 1, in ?File "<pyshell#29>", line 1, in ? l[-4]l[-4]IndexError: list IndexError: list index out of rangeindex out of range
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
55
Lists: Deleting ItemsLists: Deleting Items
• Syntax: Syntax: del list[index]del list[index]– E.g.E.g.
>>> >>> l = [1, ["2", 4], 3.0]l = [1, ["2", 4], 3.0]>>> >>> del l[2]del l[2]>>> >>> ll[1, ['2', 4]][1, ['2', 4]]>>> >>> del l[2]del l[2]Traceback (most recent call last):Traceback (most recent call last): File "<pyshell#16>", line 1, in ?File "<pyshell#16>", line 1, in ? del l[2]del l[2]IndexError: list assignment IndexError: list assignment index out of rangeindex out of range
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1010
Lists: MethodsLists: Methods
• Inserting an item at a given positionInserting an item at a given position– Syntax: Syntax: list.insert[index, item]list.insert[index, item]– E.g.E.g.>>> >>> l = [1, ["2", 4], 3.0]l = [1, ["2", 4], 3.0]>>> >>> l.insert(0, 8.3)l.insert(0, 8.3)>>> >>> ll[8.3, 1, ['2', 4], 3.0][8.3, 1, ['2', 4], 3.0]
• Adding an item at the end of the listAdding an item at the end of the list– Syntax: Syntax: list.append[item]list.append[item]– E.g.E.g.>>> >>> l.append(“end”)l.append(“end”)>>> >>> ll[8.3, 1, ['2', 4], 3.0, “end”][8.3, 1, ['2', 4], 3.0, “end”]
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
>>> >>> d[item]d[item]Traceback (most recent call last):Traceback (most recent call last): File "<pyshell#88>", line 1, in ?File "<pyshell#88>", line 1, in ? d[item]d[item]NameError: name 'item' is NameError: name 'item' is not definednot defined>>> >>> str = 'item'str = 'item'>>> >>> d[str]d[str]'tire''tire'
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
2020
Built-in Data Structures: TuplesBuilt-in Data Structures: Tuples
• CommasCommas are used to define tuples are used to define tuples– Parentheses around tuples are optionalParentheses around tuples are optional– E.g.E.g.>>> >>> 1,('2',2.0)1,('2',2.0)(1, ('2', 2.0))(1, ('2', 2.0))
– The one-element list The one-element list requiresrequires a trailing comma a trailing comma>>> 1,>>> 1,(1,)(1,)>>> (1)>>> (1) This is not a tuple but a numberThis is not a tuple but a number11
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
2121
Tuples: Accessing ItemsTuples: Accessing Items
• Syntax:Syntax: tuple[index]tuple[index]– E.g.E.g.>>> >>> t = (1, 2, (3, 4, 5))t = (1, 2, (3, 4, 5))>>> >>> t[1]t[1]22>>> >>> t[-1]t[-1](3, 4, 5)(3, 4, 5)>>> >>> t[-1][1]t[-1][1]44>>> >>> t[3]t[3]Traceback (most recent call last):Traceback (most recent call last): File "<pyshell#110>", line 1, in ?File "<pyshell#110>", line 1, in ? t[3]t[3]IndexError: tuple IndexError: tuple index out of rangeindex out of range
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
2222
Tuples: No Deletion and LengthTuples: No Deletion and Length
• No deletion!No deletion!– Tuples are immutableTuples are immutable
• Length:Length:– Syntax: Syntax: len(tuple)len(tuple)– E.g.E.g.>>> >>> t = (1,2,(3,4,5))t = (1,2,(3,4,5))>>> >>> len(t)len(t)33>>> >>> len(t[1])len(t[1])Traceback (most recent call last):Traceback (most recent call last): File "<pyshell#117>", line 1, in ?File "<pyshell#117>", line 1, in ? len(t[1])len(t[1])TypeError: len() of TypeError: len() of unsized objectunsized object>>> >>> len(t[2])len(t[2])33
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
>>> >>> def incr(x):def incr(x):return x + 1return x + 1
>>> >>> incr(3)incr(3)44
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
3333
FunctionsFunctions
• Default valuesDefault values– E.g.E.g.
def ask_ok(prompt, retries=4, complaint='Yes or no!'):def ask_ok(prompt, retries=4, complaint='Yes or no!'): while 1:while 1: ok = raw_input(prompt)ok = raw_input(prompt) if ok in ('y', 'ye', 'yes'): return 1if ok in ('y', 'ye', 'yes'): return 1 if ok in ('n', 'no', 'nop', 'nope'):if ok in ('n', 'no', 'nop', 'nope'):
return 0return 0 retries = retries - 1retries = retries - 1 if retries < 0: if retries < 0:
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
3434
FunctionsFunctions
• Parameter passing by position and by nameParameter passing by position and by name– E.g.E.g.def parrot(voltage, state='a stiff', action='voom', def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):type='Norwegian Blue'):
print "-- This parrot wouldn't", action,print "-- This parrot wouldn't", action, print "if you put", voltage, "Volts through print "if you put", voltage, "Volts through it."it."
>>> parrot(1000)>>> parrot(1000)>>> parrot(action = 'VOOOOOM', voltage = 1000000)>>> parrot(action = 'VOOOOOM', voltage = 1000000)>>> parrot('a thousand', state = 'pushing up the >>> parrot('a thousand', state = 'pushing up the daisies')daisies')
>>> parrot('a million', 'bereft of life', 'jump')>>> parrot('a million', 'bereft of life', 'jump')
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
3535
FunctionsFunctions
• Functions can also have an arbitrary number of Functions can also have an arbitrary number of parametersparameters
– Passed as a dictionary or as list of Passed as a dictionary or as list of remainingremaining parameters parameters– See documentationSee documentation
• We will talk about lambda forms and other We will talk about lambda forms and other functional programming techniquesfunctional programming techniques
– After the Haskell lecturesAfter the Haskell lectures
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
3636
Reading AssignmentReading Assignment
• Guido van Rossum and Fred L. Drake, Jr. (ed.), Guido van Rossum and Fred L. Drake, Jr. (ed.), Python tutorialPython tutorial, PythonLabs, 2001., PythonLabs, 2001.
– Read chapters 3 to 5Read chapters 3 to 5– http://www.python.org/doc/current/tut/tut.htmlhttp://www.python.org/doc/current/tut/tut.html– Write some simple programsWrite some simple programs
• Eric S. Raymond, Eric S. Raymond, Why Python?Why Python?– http://www.linuxjournal.com/article.php?sid=3882http://www.linuxjournal.com/article.php?sid=3882