Getting Started with Python The Python Interpreter A piece of - - PowerPoint PPT Presentation

getting started with python the python interpreter
SMART_READER_LITE
LIVE PREVIEW

Getting Started with Python The Python Interpreter A piece of - - PowerPoint PPT Presentation

Object-Oriented Programming in Python Goldwasser and Letscher Chapter 2 Getting Started with Python The Python Interpreter A piece of software that executes commands for the Python language Start the interpreter by typing python at a


slide-1
SLIDE 1

Getting Started with Python

Object-Oriented Programming in Python Goldwasser and Letscher Chapter 2

slide-2
SLIDE 2

Object-Oriented Programming in Python 2-2

The Python Interpreter

  • A piece of software that executes

commands for the Python language

  • Start the interpreter by typing python at a

command prompt

  • Many developers use an Integrated

Development Environment for Python known as IDLE

slide-3
SLIDE 3

Object-Oriented Programming in Python 2-3

The Python Prompt

>>>

  • This lets us know that the interpreter awaits
  • ur next command
slide-4
SLIDE 4

Object-Oriented Programming in Python 2-4

Our First Example

>>> >>> groceries = list()

  • We see a new Python prompt, so the

command has completed.

  • But what did it do?
slide-5
SLIDE 5

Object-Oriented Programming in Python 2-5

Instantiation

>>> groceries = list() >>> Constructs a new instance from the list class (notice the parentheses in the syntax)

list

slide-6
SLIDE 6

Object-Oriented Programming in Python 2-6

Assignment Statement

>>> groceries = list() >>> groceries serves as an identifier for the newly constructed object (like a “sticky label”)

list groceries

slide-7
SLIDE 7

Object-Oriented Programming in Python 2-7

Calling a Method

>>> groceries = list() >>> groceries.append('bread') >>>

list 'bread' groceries

slide-8
SLIDE 8

Object-Oriented Programming in Python 2-8

Displaying Internals

>>> groceries = list() >>> groceries.append('bread') >>> When working in the interpreter, we do not directly "see" the internal picture. But we can request a textual representation. groceries ['bread'] interpreter's response >>>

slide-9
SLIDE 9

Object-Oriented Programming in Python 2-9

Method Calling Syntax

groceries.append('bread')

  • bject
  • There may be many objects to choose from

method

  • The given object may support many methods

parameters

  • Use of parameters depends upon the method
slide-10
SLIDE 10

Object-Oriented Programming in Python 2-10

Traceback (most recent call last): File "<stdin>", line 1, in -toplevel- NameError: name 'bread' is not defined

Common Errors

Traceback (most recent call last): File "<stdin>", line 1, in -toplevel- TypeError: append() takes exactly one argument (0 given) >>> groceries.append(bread) What's the mistake? >>> groceries.append() What's the mistake?

slide-11
SLIDE 11

Object-Oriented Programming in Python 2-11

The append Method

>>> waitlist = list() >>> waitlist.append('Kim') >>> waitlist.append('Eric') >>> waitlist.append('Nell') New item added to the end of the list (much like a restaurant's waitlist) >>> waitlist ['Kim', 'Eric', 'Nell']

slide-12
SLIDE 12

Object-Oriented Programming in Python 2-12

waitlist ['Kim', 'Donald', 'Eric', 'Nell']

The insert Method

waitlist.insert(1, 'Donald') >>>

Can insert an item in an arbitrary place using a numeric index to describe the position. An element's index is the number of items before it.

>>> waitlist ['Kim', 'Eric', 'Nell'] >>>

slide-13
SLIDE 13

Object-Oriented Programming in Python 2-13

Zero-Indexing

By this definition,

  • the first element of the list has index 0
  • the second element has index 1
  • the last element has index (length - 1)

We call this convention zero-indexing. (this is a common point of confusion)

slide-14
SLIDE 14

Object-Oriented Programming in Python 2-14

waitlist ['Kim', 'Donald', 'Nell'] >>> >>> waitlist ['Kim', 'Donald', 'Eric', 'Nell'] >>>

The remove Method

What if Eric gets tired of waiting?

waitlist.remove('Eric') >>>

slide-15
SLIDE 15

Object-Oriented Programming in Python 2-15

  • Notice that we didn't have to identify where

the item is; the list will find it.

  • If it doesn't exist, a ValueError occurs
  • With duplicates, the earliest is removed

>>> groceries ['milk', 'bread', 'cheese', 'bread'] >>> groceries.remove('bread') >>> groceries ['milk', 'cheese', 'bread']

The remove Method

slide-16
SLIDE 16

Object-Oriented Programming in Python 2-16

  • Thus far, all of the methods we have seen

have an effect on the list, but none return any direct information to us.

  • Many other methods provide an explicit

return value.

  • As our first example: the count method

Return values

slide-17
SLIDE 17

Object-Oriented Programming in Python 2-17

>>> groceries ['milk', 'bread', 'cheese', 'bread'] >>>

The count method

2 response from the interpreter >>> groceries.count('bread') groceries.count('milk') 1 >>> groceries.count('apple') >>>

slide-18
SLIDE 18

Object-Oriented Programming in Python 2-18

>>> groceries ['milk', 'bread', 'cheese', 'bread'] >>>

Saving a Return Value

  • We can assign an identifier to the returned object

numLoaves = groceries.count('bread') >>>

  • Notice that it is no longer displayed by interpreter

numLoaves >>> 2

  • Yet we can use it in subsequent commands
slide-19
SLIDE 19

Object-Oriented Programming in Python 2-19

Operators

  • Most behaviors are invoked with the typical

"method calling" syntax of object.method( )

  • But Python uses shorthand syntax for many of the

most common behaviors (programmers don't like extra typing)

  • For example, the length of a list can be queried as

len(groceries) although this is really shorthand for a call groceries.__len__( )

slide-20
SLIDE 20

Object-Oriented Programming in Python 2-20

>>> waitlist ['Kim', 'Donald', 'Eric', 'Nell'] >>>

Accessing a list element

'Donald' >>> waitlist[1] waitlist[3] 'Nell' >>> waitlist[4] Traceback (most recent call last): File "<stdin>", line 1, in ? IndexError: list index out of range

slide-21
SLIDE 21

Object-Oriented Programming in Python 2-21

>>> waitlist ['Kim', 'Donald', 'Eric', 'Nell'] >>>

Negative Indices

'Nell' >>> waitlist[-1] waitlist[-3] 'Donald' >>> waitlist[-4] 'Kim' >>>

slide-22
SLIDE 22

Object-Oriented Programming in Python 2-22

List Literals

  • We originally used the syntax list( ) to create a

new empty list. For convenience, there is a shorthand syntax known as a list literal. groceries = [ ] (experienced programmers like to type less!)

  • List literals can also be used to create non-empty

lists, using a syntax similar to the one the interpreter uses when displaying a list. groceries = ['cheese', 'bread', 'milk']

slide-23
SLIDE 23

Object-Oriented Programming in Python 2-23

favoriteColors ['red', 'green', 'purple', 'blue'] >>>

Copying Lists

  • The list( ) constructor is useful for making a

new list modeled upon an existing sequence

>>> favoriteColors = ['red', 'green', 'purple', 'blue'] >>> primaryColors ['red', 'green', 'blue'] >>> primaryColors.remove('purple') >>> primaryColors = list(favoriteColors) a copy >>>

slide-24
SLIDE 24

Object-Oriented Programming in Python 2-24

The range function

  • Lists of integers are commonly needed.

Python supports a built-in function named range to easily construct such lists.

  • There are three basic forms:

range(stop) goes from zero up to but not including stop

>>> range(5) [0, 1, 2, 3, 4]

slide-25
SLIDE 25

Object-Oriented Programming in Python 2-25

The range function

range(start, stop) begins with start rather than zero

>>> range(23, 28) [23, 24, 25, 26, 27]

range(start, stop, step) uses the given step size

>>> range(23, 35, 4) [23, 27, 31] >>> range(8, 3, -1) [8, 7, 6, 5, 4]

slide-26
SLIDE 26

Object-Oriented Programming in Python 2-26

Many useful behaviors

These will become familiar with more practice.

  • groceries.pop( )

remove last element

  • groceries.pop(i)

remove ith element

  • groceries.reverse( )

reverse the list

  • groceries.sort( )

sort the list

  • 'milk' in groceries

does list contain?

  • groceries.index('cereal')

find leftmost match

slide-27
SLIDE 27

Object-Oriented Programming in Python 2-27

Documentation

  • See Section 2.2.6 of the book for more

details and a table summarizing the most commonly used list behaviors.

  • You may also type help(list) from within

the Python interpreter for documentation, or for a specific method as help(list.insert)