0% found this document useful (0 votes)
20 views

Slide Pythonlearn

The document discusses key concepts in Python including code, syntax, output, compilation vs interpretation, and provides an overview of the Python language including its design philosophy and use of an interpreter. It also covers topics such as variables, expressions, statements, data types, operators, and control flow structures in Python.

Uploaded by

Hồng Ngọc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Slide Pythonlearn

The document discusses key concepts in Python including code, syntax, output, compilation vs interpretation, and provides an overview of the Python language including its design philosophy and use of an interpreter. It also covers topics such as variables, expressions, statements, data types, operators, and control flow structures in Python.

Uploaded by

Hồng Ngọc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 350

Lecture 1: Overview of

Python Language
Programming Basics
v Code or source code: The sequence of instructions in a
program.

v Syntax: The set of legal structures and commands that


can be used in a particular programming language.

v Output: The messages printed to the user by a program.

v Console: The text box onto which output is printed.


Compilation vs Interpretation
v Compilation translates your program into a form that the
machine understands (machine language).

vInterpretation: interpreted into machine instructions.


Python overview
v Conceived in the late 1980s by Guido van Rossum at
Centrum Wiskunde & Informatica (CWI, Netherlands)

v Python is a high-level, general-purpose programming


language. It supports multiple programming paradigms:
v structured programming

v object-oriented programming

v functional programming
Python overview
v Core design philosophy [The Zen of Python (PEP 20)]
Ø Beautiful is better than ugly.

Ø Explicit is better than implicit.

Ø Simple is better than complex.

Ø Complex is better than complicated.

Ø Readability counts.
Python interpreter
v Python is an interpreted
language

v The interpreter provides an


interactive environment to play
with the language

v Results of expressions are


printed on the screen
Early Learner: Syntax Errors
• We need to learn the Python language so we can communicate our instructions to
Python. In the beginning we will make lots of mistakes and speak gibberish like
small children.

• When you make a mistake, the computer does not think you are “cute”. It says
“syntax error” - given that it knows the language and you are just learning it. It
seems like Python is cruel and unfeeling.

• You must remember that you are intelligent and can learn. The computer is
simple and very fast, but cannot learn. So it is easier for you to learn Python than
for the computer to learn English...
Talking to Python
csev$ python3
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType
"help", "copyright", "credits" or "license" for more information.
>>>
What
next?
csev$ python3
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType
"help", "copyright", "credits" or "license" for more information.
>>> x = 1
>>> print(x)
1
>>> x = x + 1 This is a good test to make sure that you have
>>> print(x) Python correctly installed. Note that quit() also
2 works to end the interactive session.
>>> exit()
What Do We Say?
Elements of Python
• Vocabulary / Words - Variables and Reserved words (Chapter 2)
• Sentence structure - valid syntax patterns (Chapters 3-5)
• Story structure - constructing a program for a purpose
name = input('Enter file:')
handle = open(name)
A short “story”
counts = dict()
for line in handle:
about how to count
words = line.split() words in a file in
for word in words:
counts[word] = counts.get(word,0) + 1
Python
bigcount = None python words.py
bigword = None
for word,count in counts.items():
Enter file: words.txt
if bigcount is None or count > bigcount: to 16
bigword = word
bigcount = count

print(bigword, bigcount)
Reserved Words
You cannot use reserved words as variable names / identifiers

False class return is finally


None if for lambda continue
True def from while nonlocal
and del global not with
as elif try or yield
assert else import pass
break except in raise
Sentences or Lines

x = 2 Assignment statement
x = x + 2 Assignment with expression
print(x) Print statement

Variable Operator Constant Function


Programming Paragraphs
Python Scripts
• Interactive Python is good for experiments and programs of 3-4 lines
long.

• Most programs are much longer, so we type them into a file and tell
Python to run the commands in the file.

• In a sense, we are “giving Python a script”.

• As a convention, we add “.py” as the suffix on the end of these files to


indicate they contain Python.
Interactive versus Script
• Interactive
- You type directly to Python one line at a time and it responds

• Script
- You enter a sequence of statements (lines) into a file using a text
editor and tell Python to execute the statements in the file
Program Steps or Program Flow
• Like a recipe or installation instructions, a program is a sequence of
steps to be done in order.

• Some steps are conditional - they may be skipped.


• Sometimes a step or group of steps is to be repeated.
• Sometimes we store a set of steps to be used over and over as
needed several places throughout the program (Chapter 4).
Sequential Steps
x=2 Program:
Output:
print(x) x = 2
print(x) 2
x=x+2 x = x + 2 4
print(x)
print(x)

When a program is running, it flows from one step to the next. As


programmers, we set up “paths” for the program to follow.
x=5
Conditional Steps
Yes
x < 10 ?

print('Smaller') Program:
No Output:
x = 5
Yes if x < 10: Smaller
x > 20 ? print('Smaller') Finis
if x > 20:
print('Bigger') print('Bigger')
No
print('Finis')

print('Finis')
n=5 Repeated Steps
No Yes Output:
n>0? Program:
5
print(n) n = 5 4
while n > 0 :
print(n)
3
n = n -1 n = n – 1 2
print('Blastoff!') 1
Blastoff!
Loops (repeated steps) have iteration variables that
print('Blastoff')
change each time through a loop.
name = input('Enter file:')
handle = open(name, 'r') Sequential
Repeated
counts = dict()
for line in handle: Conditional
words = line.split()
for word in words:
counts[word] = counts.get(word,0) + 1

bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count

print(bigword, bigcount)
name = input('Enter file:') A short Python “Story”
handle = open(name, 'r') about how to count
counts = dict()
words in a file
for line in handle:
words = line.split() A word used to read
for word in words: data from a user
counts[word] = counts.get(word,0) + 1

bigcount = None A sentence about


bigword = None updating one of the
for word,count in counts.items(): many counts
if bigcount is None or count > bigcount:
bigword = word
bigcount = count A paragraph about how
to find the largest item
print(bigword, bigcount) in a list
Summary

• We will revisit these concepts throughout the course


• Focus on the big picture
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance
Continue…
(www.dr-chuck.com) of the University of Michigan School of
Information and made available under a Creative Commons
Attribution 4.0 License. Please maintain this last slide in all
copies of the document to comply with the attribution
requirements of the license. If you make a change, feel free to
add your name and organization to the list of contributors on this
page as you republish the materials.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors and Translators here


Variables, Expressions, and
Statements
Constants
• Fixed values such as numbers, letters, and strings, are called
“constants” because their value does not change
• Numeric constants are as you expect
>>> print(123)
• String constants use single quotes (') 123
or double quotes (") >>> print(98.6)
98.6
>>> print('Hello world')
Hello world
Reserved Words
You cannot use reserved words as variable names / identifiers

False class return is finally


None if for lambda continue
True def from while nonlocal
and del global not with
as elif try or yield
assert else import pass
break except in raise
Variables
• A variable is a named place in the memory where a programmer can store
data and later retrieve the data using the variable “name”

• Programmers get to choose the names of the variables

• You can change the contents of a variable in a later statement

x = 12.2 x 12.2
y = 14
y 14
Variables
• A variable is a named place in the memory where a programmer can store
data and later retrieve the data using the variable “name”

• Programmers get to choose the names of the variables

• You can change the contents of a variable in a later statement

x = 12.2 x 12.2 100


y = 14
x = 100 y 14
Python Variable Name Rules
• Must start with a letter or underscore _

• Must consist of letters, numbers, and underscores

• Case Sensitive

Good: spam eggs spam23 _speed


Bad: 23spam #sign var.12
Different: spam Spam SPAM
Mnemonic Variable Names
• Since we programmers are given a choice in how we choose our
variable names, there is a bit of “best practice”
• We name variables to help us remember what we intend to store
in them (“mnemonic” = “memory aid”)
• This can confuse beginning students because well-named
variables often “sound” so good that they must be keywords

http://en.wikipedia.org/wiki/Mnemonic
x1q3z9ocd = 35.0
x1q3z9afd = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd
print(x1q3p9afd)

What is this bit of


code doing?
x1q3z9ocd = 35.0 a = 35.0
x1q3z9afd = 12.50 b = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd c = a * b
print(x1q3p9afd) print(c)

What are these bits


of code doing?
x1q3z9ocd = 35.0 a = 35.0
x1q3z9afd = 12.50 b = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd c = a * b
print(x1q3p9afd) print(c)

hours = 35.0
What are these bits rate = 12.50
of code doing? pay = hours * rate
print(pay)
Sentences or Lines

x = 2 Assignment statement
x = x + 2 Assignment with expression
print(x) Print statement

Variable Operator Constant Function


Assignment Statements
• We assign a value to a variable using the assignment statement (=)

• An assignment statement consists of an expression on the


right-hand side and a variable to store the result

x = 3.9 * x * ( 1 - x )
A variable is a memory location x 0.6
used to store a value (0.6)

0.6 0.6
x = 3.9 * x * ( 1 - x )

0.4

The right side is an expression.


0.936
Once the expression is evaluated, the
result is placed in (assigned to) x.
A variable is a memory location used to
store a value. The value stored in a x 0.6 0.936
variable can be updated by replacing the
old value (0.6) with a new value (0.936).
0.6 0.6
x = 3.9 * x * ( 1 - x )

0.4
The right side is an expression. Once the
expression is evaluated, the result is
placed in (assigned to) the variable on the
0.936
left side (i.e., x).
Expressions…
Numeric Expressions
Operator Operation
• Because of the lack of mathematical
symbols on computer keyboards - we + Addition
use “computer-speak” to express the - Subtraction
classic math operations
* Multiplication
• Asterisk is multiplication / Division

• Exponentiation (raise to a power) looks ** Power


different than in math % Remainder
Numeric Expressions
>>> xx = 2 >>> jj = 23
>>> xx = xx + 2 >>> kk = jj % 5 Operator Operation
>>> print(xx) >>> print(kk)
+ Addition
4 3
>>> yy = 440 * 12 >>> print(4 ** 3) - Subtraction
>>> print(yy) 64 * Multiplication
5280
>>> zz = yy / 1000 4R3 / Division

>>> print(zz) 5 23 ** Power


5.28 20 % Remainder

3
Order of Evaluation
• When we string operators together - Python must know which one
to do first

• This is called “operator precedence”

• Which operator “takes precedence” over the others?

x = 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence Rules
Highest precedence rule to lowest precedence rule:

• Parentheses are always respected Parenthesis


Power
• Exponentiation (raise to a power) Multiplication
Addition
• Multiplication, Division, and Remainder
Left to Right
• Addition and Subtraction

• Left to right
1 + 2 ** 3 / 4 * 5
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print(x)
11.0 1 + 8 / 4 * 5
>>>
1 + 2 * 5
Parenthesis
Power
Multiplication 1 + 10
Addition
Left to Right 11
Operator Precedence Parenthesis
Power
• Remember the rules top to bottom Multiplication
Addition
• When writing code - use parentheses Left to Right

• When writing code - keep mathematical expressions simple enough


that they are easy to understand

• Break long series of mathematical operations up to make them


more clear
What Does “Type” Mean?
• In Python variables, literals, and
constants have a “type” >>> ddd = 1 + 4
>>> print(ddd)
• Python knows the difference between 5
an integer number and a string >>> eee = 'hello ' + 'there'
>>> print(eee)
hello there
• For example “+” means “addition” if
something is a number and
“concatenate” if something is a string
concatenate = put together
Type Matters
• Python knows what “type” >>> eee = 'hello ' + 'there'
everything is >>> eee = eee + 1
Traceback (most recent call last):
File "<stdin>", line 1, in
• Some operations are <module>TypeError: Can't convert
prohibited 'int' object to str implicitly
>>> type(eee)
• You cannot “add 1” to a string <class'str'>
>>> type('hello')
<class'str'>
• We can ask Python what type >>> type(1)
something is by using the <class'int'>
type() function >>>
Several Types of Numbers
>>> xx = 1
• Numbers have two main types >>> type (xx)
<class 'int'>
- Integers are whole numbers:
>>> temp = 98.6
-14, -2, 0, 1, 100, 401233 >>> type(temp)
<class'float'>
- Floating Point Numbers have
>>> type(1)
decimal parts: -2.5 , 0.0, 98.6, 14.0 <class 'int'>
>>> type(1.0)
• There are other number types - they
<class'float'>
are variations on float and integer
>>>
Type Conversions
>>> print(float(99) + 100)

• When you put an integer and


199.0
>>> i = 42
floating point in an >>> type(i)
expression, the integer is <class'int'>
implicitly converted to a float >>> f = float(i)
>>> print(f)
• You can control this with the 42.0
>>> type(f)
built-in functions int() and
<class'float'>
float()
>>>
Integer Division
>>> print(10 / 2)
5.0
>>> print(9 / 2)
Integer division produces a floating 4.5
point result >>> print(99 / 100)
0.99
>>> print(10.0 / 2.0)
5.0
>>> print(99.0 / 100.0)
0.99
This was different in Python 2.x
String
>>> sval = '123'
>>> type(sval)
<class 'str'>

Conversions
>>> print(sval + 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object

• You can also use int() and to str implicitly


>>> ival = int(sval)
float() to convert between >>> type(ival)
<class 'int'>
strings and integers >>> print(ival + 1)

• You will get an error if the string


124
>>> nsv = 'hello bob'
>>> niv = int(nsv)
does not contain numeric Traceback (most recent call last):
characters File "<stdin>", line 1, in <module>
ValueError: invalid literal for int()
with base 10: 'x'
User Input
• We can instruct Python to
nam = input('Who are you? ')
pause and read data from print('Welcome', nam)
the user using the input()
function
• The input() function
returns a string Who are you? Chuck
Welcome Chuck
Converting User Input
• If we want to read a number
from the user, we must inp = input('Europe floor?')
convert it from a string to a usf = int(inp) + 1
number using a type print('US floor', usf)
conversion function
• Later we will deal with bad
Europe floor? 0
input data
US floor 1
Comments in Python
• Anything after a # is ignored by Python

• Why comment?

- Describe what is going to happen in a sequence of code

- Document who wrote the code or other ancillary information

- Turn off a line of code - perhaps temporarily


# Get the name of the file and open it
name = input('Enter file:')
handle = open(name, 'r')

# Count word frequency


counts = dict()
for line in handle:
words = line.split()
for word in words:
counts[word] = counts.get(word,0) + 1

# Find the most common word


bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count

# All done
print(bigword, bigcount)
Summary
• Type • Integer Division

• Reserved words • Conversion between types

• Variables (mnemonic) • User input

• Operators • Comments (#)

• Operator precedence
Exercise

Write a program to prompt the user for hours


and rate per hour to compute gross pay.

Enter Hours: 35
Enter Rate: 2.75

Pay: 96.25
Acknowledgements / Contributions

These slides are Copyright 2010- Charles R. Severance


(www.dr-chuck.com) of the University of Michigan School of ...
Information and made available under a Creative Commons
Attribution 4.0 License. Please maintain this last slide in all
copies of the document to comply with the attribution
requirements of the license. If you make a change, feel free to
add your name and organization to the list of contributors on this
page as you republish the materials.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors and Translators here


Conditional Execution
x=5
Conditional Steps
Yes
x < 10 ?

No print('Smaller') Program:
Output:
x = 5
Yes if x < 10: Smaller
x > 20 ? print('Smaller') Finis
if x > 20:
No print('Bigger') print('Bigger')

print('Finis')

print('Finis')
Comparison Operators
• Boolean expressions ask a Python Meaning
question and produce a Yes or No < Less than
result which we use to control
program flow <= Less than or Equal to
== Equal to
• Boolean expressions using >= Greater than or Equal to
comparison operators evaluate to
> Greater than
True / False or Yes / No
!= Not equal
• Comparison operators look at
variables but do not change the Remember: “=” is used for assignment.
variables
http://en.wikipedia.org/wiki/George_Boole
Comparison Operators
x = 5
if x == 5 :
print('Equals 5') Equals 5
if x > 4 :
print('Greater than 4') Greater than 4
if x >= 5 :
Greater than or Equals 5
print('Greater than or Equals 5')
if x < 6 : print('Less than 6') Less than 6
if x <= 5 :
print('Less than or Equals 5') Less than or Equals 5
if x != 6 :
print('Not equal 6') Not equal 6
One-Way Decisions
x = 5 Yes
print('Before 5') Before 5 x == 5 ?
if x == 5 :
print('Is 5') Is 5 No print('Is 5’)
print('Is Still 5') Is Still 5
print('Third 5')
print('Afterwards 5')
Third 5 print('Still 5')
print('Before 6') Afterwards 5
if x == 6 : Before 6 print('Third 5')
print('Is 6')
print('Is Still 6')
print('Third 6')
print('Afterwards 6') Afterwards 6
Indentation
• Increase indent indent after an if statement or for statement (after : )

• Maintain indent to indicate the scope of the block (which lines are affected
by the if/for)

• Reduce indent back to the level of the if statement or for statement to


indicate the end of the block

• Blank lines are ignored - they do not affect indentation

• Comments on a line by themselves are ignored with regard to indentation


Warning: Turn Off Tabs!!
• Atom automatically uses spaces for files with ".py" extension (nice!)

• Most text editors can turn tabs into spaces - make sure to enable this
feature

- NotePad++: Settings -> Preferences -> Language Menu/Tab Settings

- TextWrangler: TextWrangler -> Preferences -> Editor Defaults

• Python cares a *lot* about how far a line is indented. If you mix tabs and
spaces, you may get “indentation errors” even if everything looks fine
This will save you
much unnecessary
pain.
increase / maintain after if or for
decrease to indicate end of block
x = 5
if x > 2 :
print('Bigger than 2')
print('Still bigger')
print('Done with 2')

for i in range(5) :
print(i)
if i > 2 :
print('Bigger than 2')
print('Done with i', i)
print('All Done')
Think About begin/end Blocks
x = 5
if x > 2 :
print('Bigger than 2')
print('Still bigger')
print('Done with 2')

for i in range(5) :
print(i)
if i > 2 :
print('Bigger than 2')
print('Done with i', i)
print('All Done')
Nested x>1
yes

Decisions no print('More than one’)

x = 42
if x > 1 : yes
print('More than one') x < 100
if x < 100 :
no
print('Less than 100') print('Less than 100')
print('All done')

print('All Done')
Two-way Decisions
x=4

• Sometimes we want to
do one thing if a logical no yes
x>2
expression is true and
something else if the
expression is false print('Not bigger') print('Bigger')

• It is like a fork in the


road - we must choose
one or the other path but print('All Done')
not both
Two-way Decisions
x=4
with else:
no yes
x = 4 x>2

if x > 2 :
print('Bigger') print('Not bigger') print('Bigger')
else :
print('Smaller')

print('All done')
print('All Done')
Visualize Blocks x=4

no yes
x = 4 x>2

if x > 2 :
print('Bigger') print('Not bigger') print('Bigger')
else :
print('Smaller')

print('All done')
print('All Done')
More Conditional Structures…
Multi-way
yes
x<2 print('small')
no
if x < 2 :
yes
print('small')
elif x < 10 :
x < 10 print('Medium')
print('Medium') no
else :
print('LARGE') print('LARGE')
print('All done')

print('All Done')
x=0
Multi-way
yes
x<2 print('small')
x = 0
no
if x < 2 :
yes
print('small')
elif x < 10 :
x < 10 print('Medium')
print('Medium') no
else :
print('LARGE') print('LARGE')
print('All done')

print('All Done')
x=5
Multi-way
yes
x<2 print('small')
x = 5
no
if x < 2 :
yes
print('small')
elif x < 10 :
x < 10 print('Medium')
print('Medium') no
else :
print('LARGE') print('LARGE')
print('All done')

print('All Done')
x = 20
Multi-way
yes
x<2 print('small')
x = 20
no
if x < 2 :
yes
print('small')
x < 10 print('Medium')
elif x < 10 :
print('Medium') no
else :
print('LARGE') print('LARGE')
print('All done')

print('All Done')
Multi-way if x < 2 :
print('Small')
elif x < 10 :
# No Else print('Medium')
x = 5 elif x < 20 :
if x < 2 : print('Big')
print('Small') elif x < 40 :
elif x < 10 : print('Large')
print('Medium') elif x < 100:
print('Huge')
print('All done') else :
print('Ginormous')
Multi-way Puzzles
Which will never print
regardless of the value for x?
if x < 2 :
print('Below 2')
if x < 2 : elif x < 20 :
print('Below 2') print('Below 20')
elif x >= 2 : elif x < 10 :
print('Two or more') print('Below 10')
else : else :
print('Something else') print('Something else')
The try / except Structure

• You surround a dangerous section of code with try and except


• If the code in the try works - the except is skipped
• If the code in the try fails - it jumps to the except section
$ python3 notry.py
Traceback (most recent call last):
File "notry.py", line 2, in <module>
istr = int(astr)ValueError: invalid literal
for int() with base 10: 'Hello Bob'
$ cat notry.py
astr = 'Hello Bob' All
istr = int(astr) Done
print('First', istr)
astr = '123'
istr = int(astr)
print('Second', istr)
$ python3 notry.py
Traceback (most recent call last):
File "notry.py", line 2, in <module>
The istr = int(astr)ValueError: invalid literal
program for int() with base 10: 'Hello Bob'
stops $ cat notry.py
here astr = 'Hello Bob' All
istr = int(astr) Done
print('First', istr)
astr = '123'
istr = int(astr)
print('Second', istr)
Generic
Software
Computer
Input
Central
Devices
Processing
Unit
Secondary
Memory

Output Main
Devices Memory
Generic
Software
Computer
Input
Central
Devices
Processing
Unit
Secondary
Memory

Output Main
Devices Memory
astr = 'Hello Bob' When the first conversion fails - it
try: just drops into the except: clause
istr = int(astr) and the program continues.
except:
istr = -1
$ python tryexcept.py
print('First', istr) First -1
Second 123
astr = '123'
try:
istr = int(astr)
except:
istr = -1 When the second conversion
succeeds - it just skips the except:
print('Second', istr) clause and the program continues.
try / except astr = 'Bob'

print('Hello')
astr = 'Bob'
try:
print('Hello') istr = int(astr)
istr = int(astr)
print('There')
except: print('There')
istr = -1
istr = -1
print('Done', istr)

print('Done', istr) Safety net


Sample try / except
rawstr = input('Enter a number:')
try:
ival = int(rawstr) $ python3 trynum.py
except: Enter a number:42
ival = -1 Nice work
$ python3 trynum.py
if ival > 0 : Enter a number:forty-two
print('Nice work') Not a number
else: $
print('Not a number')
Summary
• Comparison operators • Nested Decisions
== <= >= > < !=
• Multi-way decisions using elif
• Indentation
• try / except to compensate for
• One-way Decisions errors
• Two-way decisions:
if: and else:
Exercise

Rewrite your pay computation to give the


employee 1.5 times the hourly rate for hours
worked above 40 hours.

Enter Hours: 45
Enter Rate: 10

Pay: 475.0
475 = 40 * 10 + 5 * 15
Exercise

Rewrite your pay program using try and except so


that your program handles non-numeric input
gracefully.

Enter Hours: 20
Enter Rate: nine
Error, please enter numeric input

Enter Hours: forty


Error, please enter numeric input
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance
(www.dr-chuck.com) of the University of Michigan School of ...
Information and made available under a Creative Commons
Attribution 4.0 License. Please maintain this last slide in all
copies of the document to comply with the attribution
requirements of the license. If you make a change, feel free to
add your name and organization to the list of contributors on this
page as you republish the materials.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors and Translators here


Functions
Chapter 4

Python for Everybody


www.py4e.com
Stored (and reused) Steps
def
thing(): Program:
print('Hello') Output:
def thing():
print('Fun') print('Hello')
print('Fun') Hello
thing() Fun
thing()
print('Zip') Zip
print('Zip') thing()
Hello
Fun
thing()
We call these reusable pieces of code “functions”
Python Functions
• There are two kinds of functions in Python.

- Built-in functions that are provided as part of Python - print(),


input(), type(), float(), int() ...

- Functions that we define ourselves and then use

• We treat function names as “new” reserved words


(i.e., we avoid them as variable names)
Function Definition
• In Python a function is some reusable code that takes
arguments(s) as input, does some computation, and then returns
a result or results

• We define a function using the def reserved word

• We call/invoke the function by using the function name,


parentheses, and arguments in an expression
Argument

big = max('Hello world')


Assignment
'w'

Result
>>> big = max('Hello world')
>>> print(big)
w
>>> tiny = min('Hello world')
>>> print(tiny)

>>>
Max Function
A function is some
>>> big = max('Hello world') stored code that we
>>> print(big) use. A function takes
w
some input and
produces an output.

'Hello world' max() 'w'


(a string) function (a string)

Guido wrote this code


Max Function
A function is some
>>> big = max('Hello world') stored code that we
>>> print(big) use. A function takes
w
some input and
def max(inp):
produces an output.
blah
'Hello world' blah 'w'
(a string) for x in inp:
blah
(a string)
blah

Guido wrote this code


Type Conversions
>>> print(float(99) / 100)
• When you put an integer 0.99
>>> i = 42
and floating point in an >>> type(i)
expression, the integer <class 'int'>
is implicitly converted to >>> f = float(i)
>>> print(f)
a float 42.0
>>> type(f)
• You can control this with <class 'float'>
the built-in functions int() >>> print(1 + 2 * float(3) / 4 – 5)
and float() -2.5
>>>
String >>> sval = '123'
>>> type(sval)

Conversions <class 'str'>


>>> print(sval + 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
• You can also use int() TypeError: cannot concatenate 'str'
and 'int'
and float() to convert >>> ival = int(sval)
between strings and >>> type(ival)
integers <class 'int'>
>>> print(ival + 1)
124
• You will get an error if the >>> nsv = 'hello bob'
string does not contain >>> niv = int(nsv)
Traceback (most recent call last):
numeric characters File "<stdin>", line 1, in <module>
ValueError: invalid literal for int()
Functions of Our Own…
Building our Own Functions
• We create a new function using the def keyword followed by
optional parameters in parentheses

• We indent the body of the function

• This defines the function but does not execute the body of the
function

def print_lyrics():
print("I'm a lumberjack, and I'm okay.")
print('I sleep all night and I work all day.')
print("I'm a lumberjack, and I'm okay.")
print_lyrics(): print('I sleep all night and I work all day.')

x = 5
print('Hello')

def print_lyrics():
print("I'm a lumberjack, and I'm okay.") Hello
print('I sleep all night and I work all day.')
Yo
print('Yo') 7
x = x + 2
print(x)
Definitions and Uses
• Once we have defined a function, we can call (or invoke) it
as many times as we like

• This is the store and reuse pattern


x = 5
print('Hello')

def print_lyrics():
print("I'm a lumberjack, and I'm okay.")
print('I sleep all night and I work all day.')

print('Yo')
print_lyrics()
x = x + 2
Hello
print(x) Yo
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
7
Arguments
• An argument is a value we pass into the function as its input
when we call the function

• We use arguments so we can direct the function to do different


kinds of work when we call it at different times

• We put the arguments in parentheses after the name of the


function
big = max('Hello world')
Argument
Parameters
>>> def greet(lang):
... if lang == 'es':
A parameter is a variable which ... print('Hola')
... elif lang == 'fr':
we use in the function definition. ... print('Bonjour')
It is a “handle” that allows the ... else:
... print('Hello')
code in the function to access ...
>>> greet('en')
the arguments for a particular Hello
function invocation. >>> greet('es')
Hola
>>> greet('fr')
Bonjour
>>>
Return Values
Often a function will take its arguments, do some computation, and
return a value to be used as the value of the function call in the
calling expression. The return keyword is used for this.

def greet():
return "Hello" Hello Glenn
Hello Sally
print(greet(), "Glenn")
print(greet(), "Sally")
Return Value
>>> def greet(lang):
... if lang == 'es':
• A “fruitful” function is one ... return 'Hola'
... elif lang == 'fr':
that produces a result (or ... return 'Bonjour'
return value) ... else:
... return 'Hello'
...
• The return statement ends >>> print(greet('en'),'Glenn')
the function execution and Hello Glenn
“sends back” the result of >>> print(greet('es'),'Sally')
Hola Sally
the function >>> print(greet('fr'),'Michael')
Bonjour Michael
>>>
Arguments, Parameters, and
Results
>>> big = max('Hello world') Parameter
>>> print(big)
w
def max(inp):
blah
blah
'Hello world' for x in inp: 'w'
blah
blah
Argument return 'w'
Result
Multiple Parameters / Arguments
• We can define more than one
parameter in the function def addtwo(a, b):
definition added = a + b
return added
• We simply add more arguments
x = addtwo(3, 5)
when we call the function print(x)

• We match the number and order 8


of arguments and parameters
Void (non-fruitful) Functions

• When a function does not return a value, we call it a “void”


function

• Functions that return values are “fruitful” functions


• Void functions are “not fruitful”
To function or not to function...
• Organize your code into “paragraphs” - capture a complete
thought and “name it”

• Don’t repeat yourself - make it work once and then reuse it

• If something gets too long or complex, break it up into logical


chunks and put those chunks in functions

• Make a library of common stuff that you do over and over -


perhaps share this with your friends...
Summary
• Functions • Arguments
• Built-In Functions • Results (fruitful functions)
• Type conversion (int, float) • Void (non-fruitful) functions
• String conversions • Why use functions?
• Parameters
Exercise

Rewrite your pay computation with time-and-a-


half for overtime and create a function called
computepay which takes two parameters ( hours
and rate).

Enter Hours: 45
Enter Rate: 10

Pay: 475.0
475 = 40 * 10 + 5 * 15
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance ...
(www.dr-chuck.com) of the University of Michigan School of
Information and open.umich.edu and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change,
feel free to add your name and organization to the list of
contributors on this page as you republish the materials.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors and Translators here


Loops and Iteration
Chapter 5

Python for Everybody


www.py4e.com
n=5 Repeated Steps
Output:
No Yes Program:
n>0? 5
n = 5 4
print(n) while n > 0 :
3
print(n)
n = n – 1 2
n = n -1 print('Blastoff!') 1
print(n) Blastoff!
0
Loops (repeated steps) have iteration variables that
print('Blastoff') change each time through a loop. Often these iteration
variables go through a sequence of numbers.
n=5 An Infinite Loop
No Yes
n>0?
n = 5
while n > 0 :
print('Lather')
print('Lather')
print('Rinse')
print('Rinse') print('Dry off!')

print('Dry off!') What is wrong with this loop?


n=0 Another Loop
No Yes
n>0?
n = 0
while n > 0 :
print('Lather')
print('Lather')
print('Rinse')
print('Rinse') print('Dry off!')

print('Dry off!') What is this loop doing?


Breaking Out of a Loop
• The break statement ends the current loop and jumps to the
statement immediately following the loop

• It is like a loop test that can happen anywhere in the body of the
loop
while True: > hello there
line = input('> ') hello there
if line == 'done' : > finished
break finished
print(line) > done
print('Done!') Done!
Breaking Out of a Loop
• The break statement ends the current loop and jumps to the
statement immediately following the loop

• It is like a loop test that can happen anywhere in the body of the
loop
while True: > hello there
line = input('> ') hello there
if line == 'done' : > finished
break finished
print(line) > done
print('Done!') Done!
No Yes
while True: True ?
line = input('> ')
if line == 'done' :
....
break
print(line)
print('Done!')
break

...

http://en.wikipedia.org/wiki/Transporter_(Star_Trek)
print('Done')
Finishing an Iteration with
continue
The continue statement ends the current iteration and jumps to the
top of the loop and starts the next iteration

while True:
> hello there
line = input('> ')
if line[0] == '#' : hello there
continue > # don't print this
if line == 'done' : > print this!
break print this!
print(line) > done
print('Done!') Done!
Finishing an Iteration with
continue
The continue statement ends the current iteration and jumps to the
top of the loop and starts the next iteration

while True:
> hello there
line = input('> ')
if line[0] == '#' : hello there
continue > # don't print this
if line == 'done' : > print this!
break print this!
print(line) > done
print('Done!') Done!
No
True ? Yes
while True:
line = raw_input('> ') ....
if line[0] == '#' :
continue
if line == 'done' : continue
break
print(line)
...
print('Done!')

print('Done')
Indefinite Loops

• While loops are called “indefinite loops” because they keep


going until a logical condition becomes False

• The loops we have seen so far are pretty easy to examine to see
if they will terminate or if they will be “infinite loops”

• Sometimes it is a little harder to be sure if a loop will terminate


Definite Loops
Iterating over a set of items…
Definite Loops
• Quite often we have a list of items of the lines in a file -
effectively a finite set of things

• We can write a loop to run the loop once for each of the items in
a set using the Python for construct

• These loops are called “definite loops” because they execute an


exact number of times

• We say that “definite loops iterate through the members of a set”


A Simple Definite Loop
5
for i in [5, 4, 3, 2, 1] :
print(i)
4
print('Blastoff!') 3
2
1
Blastoff!
A Definite Loop with Strings

Happy New Year: Joseph


friends = ['Joseph', 'Glenn', 'Sally'] Happy New Year: Glenn
for friend in friends : Happy New Year: Sally
print('Happy New Year:', friend)
print('Done!')
Done!
A Simple Definite Loop
Yes No
Done? Move i ahead 5
for i in [5, 4, 3, 2, 1] : 4
print(i) 3
print(i) print('Blastoff!')
2
1
Blastoff!

Definite loops (for loops) have explicit iteration variables


print('Blast off!') that change each time through a loop. These iteration
variables move through the sequence or set.
Looking at in...
• The iteration variable
“iterates” through the Five-element
sequence (ordered set) sequence
Iteration variable
• The block (body) of code is
executed once for each
for i in [5, 4, 3, 2, 1] :
value in the sequence print(i)

• The iteration variable moves


through all of the values in
the sequence
No • The iteration variable “iterates”
Yes
Done? Move i ahead through the sequence (ordered
set)
print(i)
• The block (body) of code is
executed once for each value in
the sequence

• The iteration variable moves


through all of the values in the
for i in [5, 4, 3, 2, 1] :
print(i)
sequence
i=5
No print(i)
Yes
Done? Move i ahead i=4
print(i)
print(i)
i=3
print(i)

i=2

for i in [5, 4, 3, 2, 1] : print(i)


print(i)
i=1
print(i)
Loop Idioms:
What We Do in Loops

Note: Even though these examples are simple,


the patterns apply to all kinds of loops
Making “smart” loops
Set some variables to
initial values
for thing in data:
The trick is “knowing” something
Look for something or
about the whole loop when you
do something to each
are stuck writing code that only
entry separately,
sees one entry at a time updating a variable

Look at the variables


Looping Through a Set
$ python basicloop.py
print('Before')
Before
for thing in [9, 41, 12, 3, 74, 15] : 9
print(thing) 41
print('After')
12
3
74
15
After
What is the Largest Number?
What is the Largest Number?

3
What is the Largest Number?

41
What is the Largest Number?

12
What is the Largest Number?

9
What is the Largest Number?

74
What is the Largest Number?

15
What is the Largest Number?
What is the Largest Number?

3 41 12 9 74 15
What is the Largest Number?

largest_so_far -1
What is the Largest Number?

largest_so_far 3
What is the Largest Number?

41

largest_so_far 41
What is the Largest Number?

12

largest_so_far 41
What is the Largest Number?

largest_so_far 41
What is the Largest Number?

74

largest_so_far 74
What is the Largest Number?

15

74
What is the Largest Number?

3 41 12 9 74 15

74
Finding the Largest Value
$ python largest.py
largest_so_far = -1 Before -1
print('Before', largest_so_far)
for the_num in [9, 41, 12, 3, 74, 15] :
9 9
if the_num > largest_so_far : 41 41
largest_so_far = the_num 41 12
print(largest_so_far, the_num) 41 3
74 74
print('After', largest_so_far) 74 15
After 74

We make a variable that contains the largest value we have seen so far. If the current
number we are looking at is larger, it is the new largest value we have seen so far.
More Loop Patterns…
Counting in a Loop
$ python countloop.py
zork = 0 Before 0
print('Before', zork) 19
for thing in [9, 41, 12, 3, 74, 15] :
2 41
zork = zork + 1
print(zork, thing) 3 12
print('After', zork) 43
5 74
6 15
After 6

To count how many times we execute a loop, we introduce a counter variable


that starts at 0 and we add one to it each time through the loop.
Summing in a Loop
$ python countloop.py
zork = 0 Before 0
print('Before', zork) 99
for thing in [9, 41, 12, 3, 74, 15] :
50 41
zork = zork + thing
print(zork, thing) 62 12
print('After', zork) 65 3
139 74
154 15
After 154

To add up a value we encounter in a loop, we introduce a sum variable that


starts at 0 and we add the value to the sum each time through the loop.
Finding the Average in a Loop
$ python averageloop.py
count = 0 Before 0 0
sum = 0
199
print('Before', count, sum)
for value in [9, 41, 12, 3, 74, 15] : 2 50 41
count = count + 1 3 62 12
sum = sum + value 4 65 3
print(count, sum, value) 5 139 74
print('After', count, sum, sum / count) 6 154 15
After 6 154 25.666

An average just combines the counting and sum patterns and


divides when the loop is done.
Filtering in a Loop
print('Before') $ python search1.py
for value in [9, 41, 12, 3, 74, 15] : Before
if value > 20: Large number 41
print('Large number',value)
print('After')
Large number 74
After

We use an if statement in the loop to catch / filter the


values we are looking for.
Search Using a Boolean Variable
$ python search1.py
Before False
found = False
print('Before', found) False 9
for value in [9, 41, 12, 3, 74, 15] : False 41
if value == 3 : False 12
found = True True 3
print(found, value) True 74
print('After', found)
True 15
After True

If we just want to search and know if a value was found, we use a variable that
starts at False and is set to True as soon as we find what we are looking for.
How to Find the Smallest Value
$ python largest.py
largest_so_far = -1 Before -1
print('Before', largest_so_far)
for the_num in [9, 41, 12, 3, 74, 15] :
9 9
if the_num > largest_so_far : 41 41
largest_so_far = the_num 41 12
print(largest_so_far, the_num) 41 3
74 74
print('After', largest_so_far) 74 15
After 74

How would we change this to make it find the smallest value in the list?
Finding the Smallest Value
smallest_so_far = -1
print('Before', smallest_so_far)
for the_num in [9, 41, 12, 3, 74, 15] :
if the_num < smallest_so_far :
smallest_so_far = the_num
print(smallest_so_far, the_num)

print('After', smallest_so_far)

We switched the variable name to smallest_so_far and switched the > to <
Finding the Smallest Value
$ python smallbad.py
smallest_so_far = -1 Before -1
print('Before', smallest_so_far)
for the_num in [9, 41, 12, 3, 74, 15] :
-1 9
if the_num < smallest_so_far : -1 41
smallest_so_far = the_num -1 12
print(smallest_so_far, the_num) -1 3
-1 74
print('After', smallest_so_far) -1 15
After -1

We switched the variable name to smallest_so_far and switched the > to <
Finding the Smallest Value
smallest = None $ python smallest.py
print('Before') Before
for value in [9, 41, 12, 3, 74, 15] : 99
if smallest is None :
9 41
smallest = value
elif value < smallest :
9 12
smallest = value 33
print(smallest, value) 3 74
print('After', smallest) 3 15
After 3

We still have a variable that is the smallest so far. The first time through the loop
smallest is None, so we take the first value to be the smallest.
The is and is not Operators
• Python has an is operator
smallest = None that can be used in logical
print('Before') expressions
for value in [3, 41, 12, 9, 74, 15] :
if smallest is None :
• Implies “is the same as”
smallest = value
elif value < smallest :
smallest = value • Similar to, but stronger than
print(smallest, value) ==
print('After', smallest)
• is not also is a logical
operator
Summary
• While loops (indefinite) • For loops (definite)
• Infinite loops • Iteration variables
• Using break • Loop idioms
• Using continue • Largest or smallest
• None constants and variables
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance ...
(www.dr-chuck.com) of the University of Michigan School of
Information and open.umich.edu and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change,
feel free to add your name and organization to the list of
contributors on this page as you republish the materials.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors and Translators here


Strings
Chapter 6

Python for Everybody


www.py4e.com
String Data Type
>>> str1 = "Hello"
>>> str2 = 'there'
>>> bob = str1 + str2
>>> print(bob)
• A string is a sequence of characters Hellothere
>>> str3 = '123'
• A string literal uses quotes >>> str3 = str3 + 1
'Hello' or "Hello" Traceback (most recent call
last): File "<stdin>", line 1,
• For strings, + means “concatenate” in <module>
TypeError: cannot concatenate
• When a string contains numbers, it is 'str' and 'int' objects
still a string >>> x = int(str3) + 1
>>> print(x)
• We can convert numbers in a string 124
>>>
into a number using int()
Reading and >>> name = input('Enter:')
Enter:Chuck
Converting >>> print(name)
Chuck
>>> apple = input('Enter:')
• We prefer to read data in using
Enter:100
strings and then parse and
>>> x = apple – 10
convert the data as we need
Traceback (most recent call
last): File "<stdin>", line 1,
• This gives us more control over
in <module>
error situations and/or bad user
TypeError: unsupported operand
input
type(s) for -: 'str' and 'int'
>>> x = int(apple) – 10
• Input numbers must be
>>> print(x)
converted from strings
90
Looking Inside Strings
• We can get at any single character in a b a n a n a
string using an index specified in 0 1 2 3 4 5
square brackets
>>> fruit = 'banana'
• The index value must be an integer >>>
>>>
letter = fruit[1]
print(letter)
and starts at zero a

• The index value can be an expression


>>> x = 3
>>> w = fruit[x - 1]
that is computed >>> print(w)
n
A Character Too Far
• You will get a python error >>> zot = 'abc'
>>> print(zot[5])
if you attempt to index Traceback (most recent call
beyond the end of a string last): File "<stdin>", line

• So be careful when
1, in <module>
IndexError: string index out
constructing index values of range
and slices >>>
Strings Have Length

b a n a n a
The built-in function len gives 0 1 2 3 4 5
us the length of a string
>>> fruit = 'banana'
>>> print(len(fruit))
6
len Function
>>> fruit = 'banana' A function is some stored
>>> x = len(fruit) code that we use. A
>>> print(x) function takes some
6 input and produces an
output.

'banana' len() 6
(a number)
(a string) function
len Function
>>> fruit = 'banana' A function is some stored
>>> x = len(fruit) code that we use. A
>>> print(x) function takes some
6 input and produces an
output.
def len(inp):
blah
'banana' blah 6
(a string) for x in y:
blah
(a number)
blah
Looping Through Strings

Using a while statement, fruit = 'banana' 0b


an iteration variable, and index = 0 1a
the len function, we can while index < len(fruit): 2n
construct a loop to look at letter = fruit[index] 3a
print(index, letter)
each of the letters in a 4n
index = index + 1
string individually 5a
Looping Through Strings

• A definite loop using a b


for statement is much a
fruit = 'banana'
more elegant n
for letter in fruit:
• The iteration variable is print(letter) a
n
completely taken care of a
by the for loop
Looping Through Strings

• A definite loop using a fruit = 'banana'


for letter in fruit :
b
for statement is much a
print(letter)
more elegant n
• The iteration variable is index = 0
a
n
completely taken care of while index < len(fruit) :
a
by the for loop letter = fruit[index]
print(letter)
index = index + 1
Looping and Counting
word = 'banana'
This is a simple loop that count = 0
loops through each letter in a for letter in word :
string and counts the number if letter == 'a' :
of times the loop encounters count = count + 1
the 'a' character print(count)
Looking Deeper into in
• The iteration variable “iterates”
through the sequence Iteration Six-character
(ordered set) variable string
• The block (body) of code is
executed once for each value for letter in 'banana' :
in the sequence print(letter)
• The iteration variable moves
through all of the values in the
sequence
Yes No b a n a n a
Done? Advance letter

print(letter)

for letter in 'banana' :


print(letter)

The iteration variable “iterates” through the string and the block (body)
of code is executed once for each value in the sequence
More String Operations
Slicing Strings M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11
• We can also look at any
continuous section of a string
using a colon operator >>> s = 'Monty Python'
>>> print(s[0:4])
• The second number is one Mont
beyond the end of the slice - >>> print(s[6:7])
“up to but not including” P
>>> print(s[6:20])
• If the second number is
Python
beyond the end of the string,
it stops at the end
Slicing Strings M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11

>>> s = 'Monty Python'


If we leave off the first number >>> print(s[:2])
or the last number of the slice, Mo
it is assumed to be the >>> print(s[8:])
beginning or end of the string thon
respectively >>> print(s[:])
Monty Python
String Concatenation
>>> a = 'Hello'
>>> b = a + 'There'
When the + operator is >>> print(b)
applied to strings, it means HelloThere
“concatenation” >>> c = a + ' ' + 'There'
>>> print(c)
Hello There
>>>
Using in as a Logical Operator
• The in keyword can also be
>>> fruit = 'banana'
>>> 'n' in fruit
used to check to see if one True
string is “in” another string >>> 'm' in fruit
False
• The in expression is a >>> 'nan' in fruit
True
logical expression that >>> if 'a' in fruit :
returns True or False and ... print('Found it!')
can be used in an if ...
statement Found it!
>>>
String Comparison
if word == 'banana':
print('All right, bananas.')

if word < 'banana':


print('Your word,' + word + ', comes before banana.')
elif word > 'banana':
print('Your word,' + word + ', comes after banana.')
else:
print('All right, bananas.')
• Python has a number of string String Library
functions which are in the
string library
>>> greet = 'Hello Bob'
• These functions are already >>> zap = greet.lower()
built into every string - we >>> print(zap)
invoke them by appending the hello bob
function to the string variable >>> print(greet)
Hello Bob
• These functions do not modify >>> print('Hi There'.lower())
the original string, instead they hi there
return a new string that has >>>
been altered
>>> stuff = 'Hello world'
>>> type(stuff)
<class 'str'>
>>> dir(stuff)
['capitalize', 'casefold', 'center', 'count', 'encode',
'endswith', 'expandtabs', 'find', 'format', 'format_map',
'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit',
'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust',
'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']

https://docs.python.org/3/library/stdtypes.html#string-methods
String Library
str.capitalize() str.replace(old, new[, count])
str.center(width[, fillchar]) str.lower()
str.endswith(suffix[, start[, end]]) str.rstrip([chars])
str.find(sub[, start[, end]]) str.strip([chars])
str.lstrip([chars]) str.upper()
Searching a String
b a n a n a
• We use the find() function to search
for a substring within another string
0 1 2 3 4 5

• find() finds the first occurrence of the >>> fruit = 'banana'


substring >>> pos = fruit.find('na')
>>> print(pos)
• If the substring is not found, find() 2
returns -1 >>> aa = fruit.find('z')
>>> print(aa)
• Remember that string position starts -1
at zero
Making everything UPPER CASE
• You can make a copy of a >>> greet = 'Hello Bob'
string in lower case or upper >>> nnn = greet.upper()
case >>> print(nnn)
• Often when we are searching HELLO BOB
>>> www = greet.lower()
for a string using find() we first
>>> print(www)
convert the string to lower case
hello bob
so we can search a string
>>>
regardless of case
Search and Replace
• The replace() function
is like a “search and >>> greet = 'Hello Bob'
replace” operation in a >>> nstr = greet.replace('Bob','Jane')
>>> print(nstr)
word processor Hello Jane

• It replaces all >>> nstr = greet.replace('o','X')


>>> print(nstr)
occurrences of the HellX BXb
search string with the >>>
replacement string
Stripping Whitespace
• Sometimes we want to take
a string and remove
whitespace at the beginning >>> greet = ' Hello Bob '
>>> greet.lstrip()
and/or end
'Hello Bob '
• lstrip() and rstrip() remove >>> greet.rstrip()
' Hello Bob'
whitespace at the left or right >>> greet.strip()

• strip() removes both


'Hello Bob'
>>>
beginning and ending
whitespace
Prefixes
>>> line = 'Please have a nice day'
>>> line.startswith('Please')
True
>>> line.startswith('p')
False
Parsing and
21 31 Extracting
From [email protected] Sat Jan 5 09:14:16 2008

>>> data = 'From [email protected] Sat Jan 5 09:14:16 2008'


>>> atpos = data.find('@')
>>> print(atpos)
21
>>> sppos = data.find(' ',atpos)
>>> print(sppos)
31
>>> host = data[atpos+1 : sppos]
>>> print(host)
uct.ac.za
Two Kinds of Strings
Python 2.7.10 Python 3.5.1
>>> x = '이광춘' >>> x = '이광춘'
>>> type(x) >>> type(x)
<type 'str'> <class 'str'>
>>> x = u'이광춘' >>> x = u'이광춘'
>>> type(x) >>> type(x)
<type 'unicode'> <class 'str'>
>>> >>>

In Python 3, all strings are Unicode


Summary
• String type • String operations
• Read/Convert • String library
• Indexing strings [] • String comparisons
• Slicing strings [2:4] • Searching in strings
• Looping through strings • Replacing text
with for and while • Stripping white space
• Concatenating strings with +
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance ...
(www.dr-chuck.com) of the University of Michigan School of
Information and open.umich.edu and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change,
feel free to add your name and organization to the list of
contributors on this page as you republish the materials.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors and Translators here


Reading Files
Chapter 7

Python for Everybody


www.py4e.com
Software What
It is time to go find some
Next? Data to mess with!
Input Central
and Output Processing Files R
Devices Unit Us

Secondary
if x < 3: print Memory

Main From [email protected] Sat Jan 5 09:14:16 2008


Memory Return-Path: <[email protected]>
Date: Sat, 5 Jan 2008 09:12:18 -0500To:
[email protected]:
[email protected]: [sakai] svn commit: r39772 -
content/branches/Details:
http://source.sakaiproject.org/viewsvn/?view=rev&rev=39772
...
File Processing
A text file can be thought of as a sequence of lines
From [email protected] Sat Jan 5 09:14:16 2008
Return-Path: <[email protected]>
Date: Sat, 5 Jan 2008 09:12:18 -0500
To: [email protected]
From: [email protected]
Subject: [sakai] svn commit: r39772 - content/branches/

Details: http://source.sakaiproject.org/viewsvn/?view=rev&rev=39772

http://www.py4e.com/code/mbox-short.txt
Opening a File
• Before we can read the contents of the file, we must tell Python
which file we are going to work with and what we will be doing
with the file

• This is done with the open() function

• open() returns a “file handle” - a variable used to perform


operations on the file

• Similar to “File -> Open” in a Word Processor


Using open()
fhand = open('mbox.txt', 'r')
• handle = open(filename, mode)

• returns a handle use to manipulate the file

• filename is a string

• mode is optional and should be 'r' if we are planning to


read the file and 'w' if we are going to write to the file
What is a Handle?
>>> fhand = open('mbox.txt')
>>> print(fhand)
<_io.TextIOWrapper name='mbox.txt' mode='r' encoding='UTF-8'>
When Files are Missing
>>> fhand = open('stuff.txt')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or
directory: 'stuff.txt'
The newline Character
>>> stuff = 'Hello\nWorld!'
>>> stuff
• We use a special character 'Hello\nWorld!'
called the “newline” to indicate >>> print(stuff)
when a line ends Hello
World!
• We represent it as \n in strings >>> stuff = 'X\nY'
>>> print(stuff)
X
• Newline is still one character - Y
not two >>> len(stuff)
3
File Processing
A text file can be thought of as a sequence of lines

From [email protected] Sat Jan 5 09:14:16 2008


Return-Path: <[email protected]>
Date: Sat, 5 Jan 2008 09:12:18 -0500
To: [email protected]
From: [email protected]
Subject: [sakai] svn commit: r39772 - content/branches/

Details: http://source.sakaiproject.org/viewsvn/?view=rev&rev=39772
File Processing
A text file has newlines at the end of each line

From [email protected] Sat Jan 5 09:14:16 2008\n


Return-Path: <[email protected]>\n
Date: Sat, 5 Jan 2008 09:12:18 -0500\n
To: [email protected]\n
From: [email protected]\n
Subject: [sakai] svn commit: r39772 - content/branches/\n
\n
Details: http://source.sakaiproject.org/viewsvn/?view=rev&rev=39772\n
Reading Files in Python
File Handle as a Sequence
• A file handle open for read can
be treated as a sequence of
strings where each line in the xfile = open('mbox.txt')
file is a string in the sequence for cheese in xfile:
print(cheese)
• We can use the for statement
to iterate through a sequence

• Remember - a sequence is an
ordered set
Counting Lines in a File
fhand = open('mbox.txt')
• Open a file read-only count = 0
for line in fhand:
• Use a for loop to read each line count = count + 1
print('Line Count:', count)
• Count the lines and print out
the number of lines
$ python open.py
Line Count: 132045
Reading the *Whole* File
>>> fhand = open('mbox-short.txt')
We can read the whole >>> inp = fhand.read()
file (newlines and all) >>> print(len(inp))
94626
into a single string
>>> print(inp[:20])
From stephen.marquar
Searching Through a File

We can put an if statement in fhand = open('mbox-short.txt')


for line in fhand:
our for loop to only print lines if line.startswith('From:') :
that meet some criteria print(line)
OOPS!
From: [email protected]
What are all these blank
lines doing here? From: [email protected]

From: [email protected]

From: [email protected]
...
OOPS!
What are all these blank From: [email protected]\n
lines doing here? \n
From: [email protected]\n
• Each line from the file \n
has a newline at the end From: [email protected]\n
\n
• The print statement adds From: [email protected]\n
a newline to each line \n
...
Searching Through a File (fixed)
fhand = open('mbox-short.txt')
• We can strip the whitespace for line in fhand:
from the right-hand side of line = line.rstrip()
if line.startswith('From:') :
the string using rstrip() from print(line)
the string library
From: [email protected]
• The newline is considered
From: [email protected]
“white space” and is From: [email protected]
stripped From: [email protected]
....
Skipping with continue
fhand = open('mbox-short.txt')
We can conveniently for line in fhand:
skip a line by using the line = line.rstrip()
if not line.startswith('From:') :
continue statement continue
print(line)
Using in to Select Lines
fhand = open('mbox-short.txt')
We can look for a string for line in fhand:
anywhere in a line as our line = line.rstrip()
if not '@uct.ac.za' in line :
selection criteria continue
print(line)

From [email protected] Sat Jan 5 09:14:16 2008


X-Authentication-Warning: set sender to [email protected] using –f
From: [email protected]
Author: [email protected]
From [email protected] Fri Jan 4 07:02:32 2008
X-Authentication-Warning: set sender to [email protected] using -f...
fname = input('Enter the file name: ')
fhand = open(fname)
count = 0
Prompt for
for line in fhand:
if line.startswith('Subject:') :
count = count + 1
File Name
print('There were', count, 'subject lines in', fname)

Enter the file name: mbox.txt


There were 1797 subject lines in mbox.txt

Enter the file name: mbox-short.txt


There were 27 subject lines in mbox-short.txt
fname = input('Enter the file name: ')

Bad File
try:
fhand = open(fname)
except:
print('File cannot be opened:', fname)

Names quit()

count = 0
for line in fhand:
if line.startswith('Subject:') :
count = count + 1
print('There were', count, 'subject lines in', fname)

Enter the file name: mbox.txt


There were 1797 subject lines in mbox.txt

Enter the file name: na na boo boo


File cannot be opened: na na boo boo
Summary
• Secondary storage • Searching for lines

• Opening a file - file handle • Reading file names

• File structure - newline character • Dealing with bad files

• Reading a file line by line with a


for loop
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance ...
(www.dr-chuck.com) of the University of Michigan School of
Information and open.umich.edu and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change,
feel free to add your name and organization to the list of
contributors on this page as you republish the materials.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors and Translators here


Python Lists
Chapter 8

Python for Everybody


www.py4e.com
Programming
• Algorithm
- A set of rules or steps used to solve a problem

• Data Structure
- A particular way of organizing data in a computer

https://en.wikipedia.org/wiki/Algorithm
https://en.wikipedia.org/wiki/Data_structure
What is Not a “Collection”?
Most of our variables have one value in them - when we put a new
value in the variable, the old value is overwritten

$ python
>>> x = 2
>>> x = 4
>>> print(x)
4
A List is a Kind of
Collection
• A collection allows us to put many values in a single “variable”

• A collection is nice because we can carry all many values


around in one convenient package.

friends = [ 'Joseph', 'Glenn', 'Sally' ]

carryon = [ 'socks', 'shirt', 'perfume' ]


List Constants
• List constants are surrounded by >>> print([1, 24, 76])
square brackets and the elements [1, 24, 76]
>>> print(['red', 'yellow',
in the list are separated by 'blue'])
commas ['red', 'yellow', 'blue']
>>> print(['red', 24, 98.6])
• A list element can be any Python ['red', 24, 98.6]
>>> print([ 1, [5, 6], 7])
object - even another list [1, [5, 6], 7]
>>> print([])
• A list can be empty []
We Already Use Lists!
5
for i in [5, 4, 3, 2, 1] :
print(i) 4
print('Blastoff!') 3
2
1
Blastoff!
Lists and Definite Loops - Best Pals

friends = ['Joseph', 'Glenn', 'Sally']


for friend in friends : Happy New Year: Joseph
print('Happy New Year:', friend)
print('Done!') Happy New Year: Glenn
Happy New Year: Sally
Done!
z = ['Joseph', 'Glenn', 'Sally']
for x in z:
print('Happy New Year:', x)
print('Done!')
Looking Inside Lists

Just like strings, we can get at any single element in a list using an
index specified in square brackets

>>> friends = [ 'Joseph', 'Glenn', 'Sally' ]


Joseph Glenn Sally >>> print(friends[1])
Glenn
0 1 2 >>>
Lists are Mutable
>>> fruit = 'Banana'
>>> fruit[0] = 'b'
• Strings are “immutable” - we Traceback
cannot change the contents of a TypeError: 'str' object does not
string - we must make a new string support item assignment
>>> x = fruit.lower()
to make any change >>> print(x)
banana
>>> lotto = [2, 14, 26, 41, 63]
• Lists are “mutable” - we can >>> print(lotto)
change an element of a list using [2, 14, 26, 41, 63]
>>> lotto[2] = 28
the index operator >>> print(lotto)
[2, 14, 28, 41, 63]
How Long is a List?

• The len() function takes a list as a >>> greet = 'Hello Bob'


parameter and returns the number >>> print(len(greet))
of elements in the list 9
>>> x = [ 1, 2, 'joe', 99]
>>> print(len(x))
• Actually len() tells us the number of 4
elements of any set or sequence >>>
(such as a string...)
Using the range Function
• The range function returns
>>> print(range(4))
a list of numbers that range [0, 1, 2, 3]
from zero to one less than >>> friends = ['Joseph', 'Glenn', 'Sally']
>>> print(len(friends))
the parameter 3
>>> print(range(len(friends)))
• We can construct an index [0,
>>>
1, 2]

loop using for and an


integer iterator
A Tale of Two Loops...
>>> friends = ['Joseph', 'Glenn', 'Sally']
friends = ['Joseph', 'Glenn', 'Sally'] >>> print(len(friends))
3
for friend in friends : >>> print(range(len(friends)))
print('Happy New Year:', friend) [0, 1, 2]
>>>
for i in range(len(friends)) :
friend = friends[i]
print('Happy New Year:', friend) Happy New Year: Joseph
Happy New Year: Glenn
Happy New Year: Sally
Concatenating Lists Using +
>>> a = [1, 2, 3]
We can create a new list >>> b = [4, 5, 6]
by adding two existing >>> c = a + b
lists together >>> print(c)
[1, 2, 3, 4, 5, 6]
>>> print(a)
[1, 2, 3]
Lists Can Be Sliced Using :
>>> t = [9, 41, 12, 3, 74, 15]
>>> t[1:3]
[41,12] Remember: Just like in
>>> t[:4] strings, the second
[9, 41, 12, 3] number is “up to but not
>>> t[3:] including”
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]
List Methods
>>> x = list()
>>> type(x)
<type 'list'>
>>> dir(x)
['append', 'count', 'extend', 'index', 'insert',
'pop', 'remove', 'reverse', 'sort']
>>>

http://docs.python.org/tutorial/datastructures.html
Building a List from Scratch
>>> stuff = list()
• We can create an empty list >>> stuff.append('book')
and then add elements using >>> stuff.append(99)
the append method >>> print(stuff)
['book', 99]
• The list stays in order and >>> stuff.append('cookie')
>>> print(stuff)
new elements are added at
['book', 99, 'cookie']
the end of the list
Is Something in a List?
• Python provides two operators >>> some = [1, 9, 21, 10, 16]
that let you check if an item is >>> 9 in some
True
in a list
>>> 15 in some
False
• These are logical operators >>> 20 not in some
that return True or False True
>>>
• They do not modify the list
Lists are in Order
• A list can hold many
items and keeps
those items in the
order until we do >>> friends = [ 'Joseph', 'Glenn', 'Sally' ]
>>> friends.sort()
something to change >>> print(friends)
the order ['Glenn', 'Joseph', 'Sally']
>>> print(friends[1])
• A list can be sorted Joseph
(i.e., change its order) >>>

• The sort method


(unlike in strings)
means “sort yourself”
Built-in Functions and Lists
>>> nums = [3, 41, 12, 9, 74, 15]
• There are a number of >>> print(len(nums))
functions built into Python 6
that take lists as >>> print(max(nums))
parameters 74
>>> print(min(nums))
3
• Remember the loops we
>>> print(sum(nums))
built? These are much 154
simpler. >>> print(sum(nums)/len(nums))
25.6
total = 0 Enter a number: 3
count = 0
while True :
Enter a number: 9
inp = input('Enter a number: ') Enter a number: 5
if inp == 'done' : break
value = float(inp) Enter a number: done
total = total + value Average: 5.66666666667
count = count + 1

average = total / count


numlist = list()
print('Average:', average)
while True :
inp = input('Enter a number: ')
if inp == 'done' : break
value = float(inp)
numlist.append(value)

average = sum(numlist) / len(numlist)


print('Average:', average)
Best Friends: Strings and Lists
>>> abc = 'With three words' >>> print(stuff)
>>> stuff = abc.split() ['With', 'three', 'words']
>>> print(stuff) >>> for w in stuff :
['With', 'three', 'words'] ... print(w)
>>> print(len(stuff)) ...
3 With
>>> print(stuff[0]) Three
With Words
>>>

Split breaks a string into parts and produces a list of strings. We think of these
as words. We can access a particular word or loop through all the words.
>>> line = 'A lot of spaces'
>>> etc = line.split()
>>> print(etc)
['A', 'lot', 'of', 'spaces'] ● When you do not specify a
>>>
>>> line = 'first;second;third' delimiter, multiple spaces are
>>> thing = line.split()
>>> print(thing) treated like one delimiter
['first;second;third']
>>> print(len(thing))
1 ● You can specify what delimiter
>>> thing = line.split(';')
>>> print(thing) character to use in the splitting
['first', 'second', 'third']
>>> print(len(thing))
3
>>>
From [email protected] Sat Jan 5 09:14:16 2008

fhand = open('mbox-short.txt') Sat


for line in fhand:
line = line.rstrip() Fri
if not line.startswith('From ') : continue Fri
words = line.split()
print(words[2])
Fri
...

>>> line = 'From [email protected] Sat Jan 5 09:14:16 2008'


>>> words = line.split()
>>> print(words)
['From', '[email protected]', 'Sat', 'Jan', '5', '09:14:16', '2008']
>>>
The Double Split Pattern
Sometimes we split a line one way, and then grab one of the pieces
of the line and split that piece again

From [email protected] Sat Jan 5 09:14:16 2008

words = line.split()
email = words[1]
print pieces[1]
The Double Split Pattern

From [email protected] Sat Jan 5 09:14:16 2008

words = line.split()
email = words[1] [email protected]
print pieces[1]
The Double Split Pattern

From [email protected] Sat Jan 5 09:14:16 2008

words = line.split()
email = words[1] [email protected]
pieces = email.split('@') ['stephen.marquard', 'uct.ac.za']
print pieces[1]
The Double Split Pattern

From [email protected] Sat Jan 5 09:14:16 2008

words = line.split()
email = words[1] [email protected]
pieces = email.split('@') ['stephen.marquard', 'uct.ac.za']
print(pieces[1]) 'uct.ac.za'
List Summary
• Concept of a collection • Slicing lists

• Lists and definite loops • List methods: append, remove

• Indexing and lookup • Sorting lists

• List mutability • Splitting strings into lists of words

• Functions: len, min, max, sum • Using split to parse strings


Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance
...
(www.dr-chuck.com) of the University of Michigan School of
Information and open.umich.edu and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change,
feel free to add your name and organization to the list of
contributors on this page as you republish the materials.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors and Translators here


Python Dictionaries
Chapter 9

Python for Everybody


www.py4e.com
What is a Collection?
• A collection is nice because we can put more than one value in it
and carry them all around in one convenient package

• We have a bunch of values in a single “variable”

• We do this by having more than one place “in” the variable

• We have ways of finding the different places in the variable


What is Not a “Collection”?
Most of our variables have one value in them - when we put a new
value in the variable - the old value is overwritten

$ python
>>> x = 2
>>> x = 4
>>> print(x)
4
A Story of Two Collections..
• List

- A linear collection of values that stay in order

• Dictionary

- A “bag” of values, each with its own label


Dictionaries
tissue
calculator

perfume
money
candy

http://en.wikipedia.org/wiki/Associative_array
Dictionaries
• Dictionaries are Python’s most powerful data collection

• Dictionaries allow us to do fast database-like operations in Python

• Dictionaries have different names in different languages

- Associative Arrays - Perl / PHP

- Properties or Map or HashMap - Java

- Property Bag - C# / .Net


Dictionaries
• Lists index their entries >>> purse = dict()
based on the position in the >>> purse['money'] = 12
>>> purse['candy'] = 3
list >>> purse['tissues'] = 75
>>> print(purse)
• Dictionaries are like bags - {'money': 12, 'tissues': 75, 'candy': 3}
>>> print(purse['candy'])
no order 3
>>> purse['candy'] = purse['candy'] + 2
>>> print(purse)
• So we index the things we {'money': 12, 'tissues': 75, 'candy': 5}
put in the dictionary with a
“lookup tag”
Comparing Lists and Dictionaries
Dictionaries are like lists except that they use keys instead of
numbers to look up values

>>> lst = list() >>> ddd = dict()


>>> lst.append(21) >>> ddd['age'] = 21
>>> lst.append(183) >>> ddd['course'] = 182
>>> print(lst) >>> print(ddd)
[21, 183] {'course': 182, 'age': 21}
>>> lst[0] = 23 >>> ddd['age'] = 23
>>> print(lst) >>> print(ddd)
[23, 183] {'course': 182, 'age': 23}
>>> lst = list() List
>>> lst.append(21)
>>> lst.append(183) Key Value
>>> print(lst)
[0] 21
[21, 183] lst
>>> lst[0] = 23 [1] 183
>>> print(lst)
[23, 183]

>>> ddd = dict()


Dictionary
>>> ddd['age'] = 21
>>> ddd['course'] = 182 Key Value
>>> print(ddd)
{'course': 182, 'age': 21} ['course'] 182
>>> ddd['age'] = 23 ddd
>>> print(ddd) ['age'] 21
{'course': 182, 'age': 23}
Dictionary Literals (Constants)
• Dictionary literals use curly braces and have a list of key : value pairs

• You can make an empty dictionary using empty curly braces

>>> jjj = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}


>>> print(jjj)
{'jan': 100, 'chuck': 1, 'fred': 42}
>>> ooo = { }
>>> print(ooo)
{}
>>>
Most Common Name?
Most Common Name?
marquard cwen cwen
zhen marquard zhen
csev
csev zhen
marquard
zhen csev zhen
Most Common Name?
marquard cwen cwen
zhen marquard zhen
csev
csev zhen
marquard
zhen csev zhen
Many Counters with a Dictionary
Key Value
One common use of dictionaries is
counting how often we “see” something
>>> ccc = dict()
>>> ccc['csev'] = 1
>>> ccc['cwen'] = 1
>>> print(ccc)
{'csev': 1, 'cwen': 1}
>>> ccc['cwen'] = ccc['cwen'] + 1
>>> print(ccc)
{'csev': 1, 'cwen': 2}
Dictionary Tracebacks
• It is an error to reference a key which is not in the dictionary

• We can use the in operator to see if a key is in the dictionary

>>> ccc = dict()


>>> print(ccc['csev'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'csev'
>>> 'csev' in ccc
False
When We See a New Name
When we encounter a new name, we need to add a new entry in the
dictionary and if this the second or later time we have seen the name,
we simply add one to the count in the dictionary under that name

counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
for name in names :
if name not in counts: {'csev': 2, 'zqian': 1, 'cwen': 2}
counts[name] = 1
else :
counts[name] = counts[name] + 1
print(counts)
The get Method for Dictionaries
The pattern of checking to see if a if name in counts:
key is already in a dictionary and x = counts[name]
assuming a default value if the key else :
is not there is so common that there x = 0
is a method called get() that does
this for us
x = counts.get(name, 0)

Default value if key does not exist


(and no Traceback). {'csev': 2, 'zqian': 1, 'cwen': 2}
Simplified Counting with get()
We can use get() and provide a default value of zero when the key
is not yet in the dictionary - and then just add one

counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
for name in names :
counts[name] = counts.get(name, 0) + 1
print(counts)

Default {'csev': 2, 'zqian': 1, 'cwen': 2}


Simplified Counting with get()

counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
for name in names :
counts[name] = counts.get(name, 0) + 1
print(counts)

http://www.youtube.com/watch?v=EHJ9uYx5L58
Counting Words in Text
Writing programs (or programming) is a very creative and rewarding activity. You can write
programs for many reasons ranging from making your living to solving a difficult data analysis
problem to having fun to helping someone else solve a problem. This book assumes that
everyone needs to know how to program and that once you know how to program, you will figure
out what you want to do with your newfound skills.

We are surrounded in our daily lives with computers ranging from laptops to cell phones. We
can think of these computers as our “personal assistants” who can take care of many things on
our behalf. The hardware in our current-day computers is essentially built to continuously ask us
the question, “What would you like me to do next?”

Our computers are fast and have vast amounts of memory and could be very helpful to us if we
only knew the language to speak to explain to the computer what we would like it to do next. If
we knew this language we could tell the computer to do tasks on our behalf that were repetitive.
Interestingly, the kinds of things computers can do best are often the kinds of things that we
humans find boring and mind-numbing.
Counting Pattern
counts = dict()
print('Enter a line of text:')
The general pattern to count the
line = input('')
words in a line of text is to split
words = line.split() the line into words, then loop
through the words and use a
print('Words:', words) dictionary to track the count of
print('Counting...')
each word independently.
for word in words:
counts[word] = counts.get(word,0) + 1
print('Counts', counts)
python wordcount.py
Enter a line of text:
the clown ran after the car and the car ran into the tent
and the tent fell down on the clown and the car

Words: ['the', 'clown', 'ran', 'after', 'the', 'car',


'and', 'the', 'car', 'ran', 'into', 'the', 'tent', 'and',
'the', 'tent', 'fell', 'down', 'on', 'the', 'clown',
'and', 'the', 'car']
Counting…

Counts {'and': 3, 'on': 1, 'ran': 2, 'car': 3, 'into': 1,


'after': 1, 'clown': 2, 'down': 1, 'fell': 1, 'the': 7,
'tent': 2}

http://www.flickr.com/photos/71502646@N00/2526007974/
python wordcount.py
counts = dict() Enter a line of text:
line = input('Enter a line of text:') the clown ran after the car and the car ran
words = line.split()
into the tent and the tent fell down on the
print('Words:', words) clown and the car
print('Counting...’)
Words: ['the', 'clown', 'ran', 'after', 'the', 'car',
for word in words: 'and', 'the', 'car', 'ran', 'into', 'the', 'tent', 'and',
counts[word] = counts.get(word,0) + 1 'the', 'tent', 'fell', 'down', 'on', 'the', 'clown',
print('Counts', counts)
'and', 'the', 'car']
Counting...

Counts {'and': 3, 'on': 1, 'ran': 2, 'car': 3,


'into': 1, 'after': 1, 'clown': 2, 'down': 1, 'fell':
1, 'the': 7, 'tent': 2}
Definite Loops and Dictionaries
Even though dictionaries are not stored in order, we can write a for
loop that goes through all the entries in a dictionary - actually it
goes through all of the keys in the dictionary and looks up the
values
>>> counts = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}
>>> for key in counts:
... print(key, counts[key])
...
jan 100
chuck 1
fred 42
>>>
Retrieving Lists of Keys and Values
>>> jjj = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}
You can get a list >>> print(list(jjj))
['jan', 'chuck', 'fred']
of keys, values, or >>> print(list(jjj.keys()))
items (both) from ['jan', 'chuck', 'fred']
a dictionary >>> print(list(jjj.values()))
[100, 1, 42]
>>> print(list(jjj.items()))
[('jan', 100), ('chuck', 1), ('fred', 42)]
>>>

What is a “tuple”? - coming soon...


Bonus: Two Iteration Variables!
• We loop through the
key-value pairs in a jjj = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}
for aaa,bbb in jjj.items() :
dictionary using *two* print(aaa, bbb)
iteration variables aaa bbb

• Each iteration, the first jan 100 [jan] 100


chuck 1
variable is the key and fred 42 [chuck] 1
the second variable is
the corresponding [fred] 42
value for the key
name = input('Enter file:')
handle = open(name)
python words.py
counts = dict() Enter file: words.txt
for line in handle:
words = line.split()
to 16
for word in words:
counts[word] = counts.get(word,0) + 1

bigcount = None
bigword = None
python words.py
for word,count in counts.items(): Enter file: clown.txt
if bigcount is None or count > bigcount: the 7
bigword = word
bigcount = count

print(bigword, bigcount)
Using two nested loops
Summary
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance
...
(www.dr-chuck.com) of the University of Michigan School of
Information and open.umich.edu and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change,
feel free to add your name and organization to the list of
contributors on this page as you republish the materials.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors or translation credits here


Tuples
Chapter 10

Python for Everybody


www.py4e.com
Tuples Are Like Lists
Tuples are another kind of sequence that functions much like a list
- they have elements which are indexed starting at 0
>>> x = ('Glenn', 'Sally', 'Joseph') >>> for iter in y:
>>> print(x[2]) ... print(iter)
Joseph ...
>>> y = ( 1, 9, 2 ) 1
>>> print(y) 9
(1, 9, 2) 2
>>> print(max(y)) >>>
9
but... Tuples are “immutable”
Unlike a list, once you create a tuple, you cannot alter its
contents - similar to a string

>>> x = [9, 8, 7] >>> y = 'ABC' >>> z = (5, 4, 3)


>>> x[2] = 6 >>> y[2] = 'D' >>> z[2] = 0
>>> print(x) Traceback:'str' Traceback:'tuple'
>>>[9, 8, 6] object does object does
>>> not support item not support item
Assignment Assignment
>>> >>>
Things not to do With Tuples
>>> x = (3, 2, 1)
>>> x.sort()
Traceback:
AttributeError: 'tuple' object has no attribute 'sort'
>>> x.append(5)
Traceback:
AttributeError: 'tuple' object has no attribute 'append'
>>> x.reverse()
Traceback:
AttributeError: 'tuple' object has no attribute 'reverse'
>>>
A Tale of Two Sequences
>>> l = list()
>>> dir(l)
['append', 'count', 'extend', 'index', 'insert', 'pop',
'remove', 'reverse', 'sort']

>>> t = tuple()
>>> dir(t)
['count', 'index']
Tuples are More Efficient
• Since Python does not have to build tuple structures to be
modifiable, they are simpler and more efficient in terms of
memory use and performance than lists
• So in our program when we are making “temporary variables”
we prefer tuples over lists
Tuples and Assignment
• We can also put a tuple on the left-hand side of an assignment
statement
• We can even omit the parentheses

>>> (x, y) = (4, 'fred')


>>> print(y)
fred
>>> (a, b) = (99, 98)
>>> print(a)
99
Tuples and Dictionaries
>>> d = dict()
>>> d['csev'] = 2
>>> d['cwen'] = 4
The items() method >>> for (k,v) in d.items():
in dictionaries ... print(k, v)
returns a list of (key, ...
csev 2
value) tuples
cwen 4
>>> tups = d.items()
>>> print(tups)
dict_items([('csev', 2), ('cwen', 4)])
Tuples are Comparable
The comparison operators work with tuples and other
sequences. If the first item is equal, Python goes on to the next
element, and so on, until it finds elements that differ.
>>> (0, 1, 2) < (5, 1, 2)
True
>>> (0, 1, 2000000) < (0, 3, 4)
True
>>> ( 'Jones', 'Sally' ) < ('Jones', 'Sam')
True
>>> ( 'Jones', 'Sally') > ('Adams', 'Sam')
True
Sorting Lists of Tuples
• We can take advantage of the ability to sort a list of tuples to
get a sorted version of a dictionary
• First we sort the dictionary by the key using the items() method
and sorted() function

>>> d = {'a':10, 'b':1, 'c':22}


>>> d.items()
dict_items([('a', 10), ('c', 22), ('b', 1)])
>>> sorted(d.items())
[('a', 10), ('b', 1), ('c', 22)]
Using sorted()
>>> d = {'a':10, 'b':1, 'c':22}
We can do this even >>> t = sorted(d.items())
more directly using the >>> t
built-in function sorted [('a', 10), ('b', 1), ('c', 22)]
that takes a sequence >>> for k, v in sorted(d.items()):
as a parameter and ... print(k, v)
returns a sorted ...
a 10
sequence
b 1
c 22
Sort by Values Instead of Key
• If we could construct a >>> c = {'a':10, 'b':1, 'c':22}
>>> tmp = list()
list of tuples of the
>>> for k, v in c.items() :
form (value, key) we ... tmp.append( (v, k) )
could sort by value ...
>>> print(tmp)
• We do this with a for [(10, 'a'), (22, 'c'), (1, 'b')]
loop that creates a list >>> tmp = sorted(tmp, reverse=True)
of tuples >>> print(tmp)
[(22, 'c'), (10, 'a'), (1, 'b')]
fhand = open('romeo.txt') The top 10 most
counts = {}
for line in fhand:
common words
words = line.split()
for word in words:
counts[word] = counts.get(word, 0 ) + 1

lst = []
for key, val in counts.items():
newtup = (val, key)
lst.append(newtup)

lst = sorted(lst, reverse=True)

for val, key in lst[:10] :


print(key, val)
Even Shorter Version
>>> c = {'a':10, 'b':1, 'c':22}

>>> print( sorted( [ (v,k) for k,v in c.items() ] ) )

[(1, 'b'), (10, 'a'), (22, 'c')]

List comprehension creates a dynamic list. In this case, we


make a list of reversed tuples and then sort it.
http://wiki.python.org/moin/HowTo/Sorting
Summary
• Tuple syntax • Tuples in assignment
statements
• Immutability
• Sorting dictionaries by
• Comparability
either key or value
• Sorting
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance
...
(www.dr-chuck.com) of the University of Michigan School of
Information and open.umich.edu and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change,
feel free to add your name and organization to the list of
contributors on this page as you republish the materials.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors and Translators here


Python Objects
Charles Severance

Python for Everybody


www.py4e.com
Warning
• This lecture is very much about definitions and
mechanics for objects
• This lecture is a lot more about “how it works” and less
about “how you use it”
• You won’t get the entire picture until this is all looked at
in the context of a real problem
• So please suspend disbelief and learn technique for the
next 40 or so slides…
https://docs.python.org/3/tutorial/datastructures.html
https://docs.python.org/3/library/sqlite3.html
Lets Start with Programs
Europe floor? 0
inp = input('Europe floor?') US floor 1
usf = int(inp) + 1
print('US floor', usf)

Input Process Output


Object Oriented
• A program is made up of many cooperating objects
• Instead of being the “whole program” - each object is a
little “island” within the program and cooperatively
working with other objects
• A program is made up of one or more objects working
together - objects make use of each other’s capabilities
Object
• An Object is a bit of self-contained Code and Data
• A key aspect of the Object approach is to break the
problem into smaller understandable parts (divide and
conquer)
• Objects have boundaries that allow us to ignore un-needed
detail
• We have been using objects all along: String Objects,
Integer Objects, Dictionary Objects, List Objects...
Input
Dictionary
Object

Object
String
Objects get
created and
used Output
Input
Code/Data
Code/Data

Code/Data
Code/Data
Objects are
bits of code
and data Output
Input
Code/Data
Code/Data

Code/Data
Code/Data
Objects hide detail
- they allow us to
ignore the detail of
the “rest of the Output
program”.
Input
Code/Data
Code/Data

Code/Data
Code/Data
Objects hide detail -
they allow the “rest
of the program” to
ignore the detail Output
about “us”.
Definitions
• Class - a template
• Method or Message - A defined capability of a class
• Field or attribute- A bit of data in a class
• Object or Instance - A particular instance of a class
Terminology: Class
Defines the abstract characteristics of a thing (object), including the
thing's characteristics (its attributes, fields or properties) and the
thing's behaviors (the things it can do, or methods, operations or
features). One might say that a class is a blueprint or factory that
describes the nature of something. For example, the class Dog would
consist of traits shared by all dogs, such as breed and fur color
(characteristics), and the ability to bark and sit (behaviors).

http://en.wikipedia.org/wiki/Object-oriented_programming
Terminology: Instance
One can have an instance of a class or a particular object.
The instance is the actual object created at runtime. In
programmer jargon, the Lassie object is an instance of the
Dog class. The set of values of the attributes of a particular
object is called its state. The object consists of state and the
behavior that's defined in the object's class.
Object and Instance are often used interchangeably.

http://en.wikipedia.org/wiki/Object-oriented_programming
Terminology: Method
An object's abilities. In language, methods are verbs. Lassie, being a
Dog, has the ability to bark. So bark() is one of Lassie's methods. She
may have other methods as well, for example sit() or eat() or walk() or
save_timmy(). Within the program, using a method usually affects
only one particular object; all Dogs can bark, but you need only one
particular dog to do the barking

Method and Message are often used interchangeably.

http://en.wikipedia.org/wiki/Object-oriented_programming
Some Python Objects
>>> dir(x)
>>> x = 'abc' [ … 'capitalize', 'casefold', 'center', 'count',
>>> type(x) 'encode', 'endswith', 'expandtabs', 'find',
<class 'str'> 'format', … 'lower', 'lstrip', 'maketrans',
>>> type(2.5) 'partition', 'replace', 'rfind', 'rindex', 'rjust',
<class 'float'> 'rpartition', 'rsplit', 'rstrip', 'split',
>>> type(2) 'splitlines', 'startswith', 'strip', 'swapcase',
<class 'int'> 'title', 'translate', 'upper', 'zfill']
>>> y = list() >>> dir(y)
>>> type(y) [… 'append', 'clear', 'copy', 'count', 'extend',
<class 'list'> 'index', 'insert', 'pop', 'remove', 'reverse',
>>> z = dict() 'sort']
>>> type(z) >>> dir(z)
<class 'dict'> […, 'clear', 'copy', 'fromkeys', 'get', 'items',
'keys', 'pop', 'popitem', 'setdefault', 'update',
'values']
A Sample Class
This is the template
class is a reserved
class PartyAnimal: for making
word
x=0 PartyAnimal objects

def party(self) : Each PartyAnimal


Each PartyAnimal
self.x = self.x + 1 object has a bit of
object has a bit of
print("So far",self.x) data
code
Construct a
an = PartyAnimal() PartyAnimal object
and store in an
an.party()
Tell the an object an.party() PartyAnimal.party(an)
to run the party() an.party()
code within it
class PartyAnimal: $ python party1.py
x=0

def party(self) :
self.x = self.x + 1
print("So far",self.x)

an = PartyAnimal()

an.party()
an.party()
an.party()
class PartyAnimal:
$ python party1.py
x=0

def party(self) :
self.x = self.x + 1
print("So far",self.x)
an
an = PartyAnimal()
x 0
party()
an.party()
an.party()
an.party()
class PartyAnimal: $ python party1.py
x=0 So far 1
So far 2
def party(self) : So far 3
self.x = self.x + 1
print("So far",self.x)
an
self x
an = PartyAnimal()
party()
an.party()
an.party()
an.party() PartyAnimal.party(an)
Playing with dir() and type()
A Nerdy Way to Find Capabilities
>>> y = list()
• The dir() command lists >>> type(y)
capabilities <class 'list'>
>>> dir(y)
• Ignore the ones with underscores ['__add__', '__class__',
- these are used by Python itself '__contains__', '__delattr__',
'__delitem__', '__delslice__',
'__doc__', … '__setitem__',
• The rest are real operations that '__setslice__', '__str__',
the object can perform 'append', 'clear', 'copy',
'count', 'extend', 'index',
• It is like type() - it tells us 'insert', 'pop', 'remove',
'reverse', 'sort']
something *about* a variable >>>
class PartyAnimal:
x = 0
We can use dir() to find
the “capabilities” of our
def party(self) : newly created class.
self.x = self.x + 1
print("So far",self.x)

an = PartyAnimal()
$ python party3.py
print("Type", type(an)) Type <class '__main__.PartyAnimal'>
print("Dir ", dir(an)) Dir ['__class__', ... 'party', 'x']
Try dir() with a String
>>> x = 'Hello there'
>>> dir(x)
['__add__', '__class__', '__contains__', '__delattr__',
'__doc__', '__eq__', '__ge__', '__getattribute__',
'__getitem__', '__getnewargs__', '__getslice__', '__gt__',
'__hash__', '__init__', '__le__', '__len__', '__lt__',
'__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__',
'capitalize', 'center', 'count', 'decode', 'encode', 'endswith',
'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit',
'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust',
'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex',
'rjust', 'rpartition', 'rsplit', 'rstrip', 'split',
'splitlines', 'startswith', 'strip', 'swapcase', 'title',
'translate', 'upper', 'zfill']
Object Lifecycle
http://en.wikipedia.org/wiki/Constructor_(computer_science)
Object Lifecycle
• Objects are created, used, and discarded
• We have special blocks of code (methods) that get called
- At the moment of creation (constructor)
- At the moment of destruction (destructor)
• Constructors are used a lot
• Destructors are seldom used
Constructor
The primary purpose of the constructor is to set up some
instance variables to have the proper initial values when
the object is created
class PartyAnimal:
x = 0
$ python party4.py
def __init__(self):
I am constructed
print('I am constructed')
So far 1
def party(self) : So far 2
self.x = self.x + 1 I am destructed 2
print('So far',self.x) an contains 42

def __del__(self):
print('I am destructed', self.x)

an = PartyAnimal() The constructor and destructor are


an.party() optional. The constructor is
an.party() typically used to set up variables.
an = 42 The destructor is seldom used.
print('an contains',an)
Constructor
In object oriented programming, a constructor in a class
is a special block of statements called when an object is
created

http://en.wikipedia.org/wiki/Constructor_(computer_science)
Many Instances
• We can create lots of objects - the class is the template
for the object
• We can store each distinct object in its own variable

• We call this having multiple instances of the same class


• Each instance has its own copy of the instance variables
class PartyAnimal:
Constructors can have
x = 0 additional parameters.
name = "" These can be used to set up
def __init__(self, z): instance variables for the
self.name = z
print(self.name,"constructed")
particular instance of the
class (i.e., for the particular
def party(self) : object).
self.x = self.x + 1
print(self.name,"party count",self.x)

s = PartyAnimal("Sally")
j = PartyAnimal("Jim")

s.party()
j.party()
s.party() party5.py
class PartyAnimal:
x = 0
name = ""
def __init__(self, z):
self.name = z
print(self.name,"constructed")

def party(self) :
self.x = self.x + 1
print(self.name,"party count",self.x)

s = PartyAnimal("Sally")
j = PartyAnimal("Jim")

s.party()
j.party()
s.party()
class PartyAnimal:
x = 0
name = "" s
def __init__(self, z): x: 0
self.name = z
print(self.name,"constructed")
name:
def party(self) :
self.x = self.x + 1
print(self.name,"party count",self.x)

s = PartyAnimal("Sally")
j = PartyAnimal("Jim")

s.party()
j.party()
s.party()
class PartyAnimal:
x = 0
name = "" s
def __init__(self, z): x: 0
self.name = z
print(self.name,"constructed")
name: Sally
def party(self) :
self.x = self.x + 1
print(self.name,"party count",self.x)

s = PartyAnimal("Sally") j
j = PartyAnimal("Jim") x: 0
We have two
s.party()
j.party()
independent name: Jim
s.party() instances
class PartyAnimal:
x = 0
name = "" Sally constructed
def __init__(self, z): Jim constructed
self.name = z Sally party count 1
Jim party count 1
print(self.name,"constructed")
Sally party count 2

def party(self) :
self.x = self.x + 1
print(self.name,"party count",self.x)

s = PartyAnimal("Sally")
j = PartyAnimal("Jim")

s.party()
j.party()
s.party()
Inheritance
http://www.ibiblio.org/g2swap/byteofpython/read/inheritance.html
Inheritance
• When we make a new class - we can reuse an existing
class and inherit all the capabilities of an existing class
and then add our own little bit to make our new class
• Another form of store and reuse
• Write once - reuse many times
• The new class (child) has all the capabilities of the old
class (parent) - and then some more
Terminology: Inheritance

‘Subclasses’ are more specialized versions of a class, which


inherit attributes and behaviors from their parent classes, and
can introduce their own.

http://en.wikipedia.org/wiki/Object-oriented_programming
class PartyAnimal:
x = 0 s = PartyAnimal("Sally")
name = "" s.party()
def __init__(self, nam):
self.name = nam j = FootballFan("Jim")
print(self.name,"constructed") j.party()
j.touchdown()
def party(self) :
self.x = self.x + 1
print(self.name,"party count",self.x)
FootballFan is a class which
class FootballFan(PartyAnimal):
extends PartyAnimal. It has all
points = 0
def touchdown(self): the capabilities of PartyAnimal
self.points = self.points + 7 and more.
self.party()
print(self.name,"points",self.points)
class PartyAnimal:
x = 0 s = PartyAnimal("Sally")
name = "" s.party()
def __init__(self, nam):
self.name = nam j = FootballFan("Jim")
print(self.name,"constructed") j.party()
j.touchdown()
def party(self) :
self.x = self.x + 1
print(self.name,"party count",self.x) s
x:
class FootballFan(PartyAnimal):
points = 0
def touchdown(self):
name: Sally
self.points = self.points + 7
self.party()
print(self.name,"points",self.points)
class PartyAnimal:
x = 0 s = PartyAnimal("Sally")
name = "" s.party()
def __init__(self, nam):
self.name = nam j = FootballFan("Jim")
print(self.name,"constructed") j.party()
j.touchdown()
def party(self) :
self.x = self.x + 1
print(self.name,"party count",self.x) j
x:
class FootballFan(PartyAnimal):
points = 0
def touchdown(self): name: Jim
self.points = self.points + 7
self.party() points:
print(self.name,"points",self.points)
Definitions
• Class - a template
• Attribute – A variable within a class
• Method - A function within a class
• Object - A particular instance of a class
• Constructor – Code that runs when an object is created
• Inheritance - The ability to extend a class to make a new class.
Summary
• Object Oriented programming is a very structured
approach to code reuse

• We can group data and functionality together and create


many independent instances of a class
Acknowledgements / Contributions
Thes slide are Copyright 2010- Charles R. Severance (www.dr-
...
chuck.com) of the University of Michigan School of Information
and made available under a Creative Commons Attribution 4.0
License. Please maintain this last slide in all copies of the
document to comply with the attribution requirements of the
license. If you make a change, feel free to add your name and
organization to the list of contributors on this page as you
republish the materials.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors here


Additional Source Information
• Snowman Cookie Cutter" by Didriks is licensed under CC BY
https://www.flickr.com/photos/dinnerseries/23570475099

• Photo from the television program Lassie. Lassie watches as Jeff (Tommy Rettig) works on his bike is Public
Domain
https://en.wikipedia.org/wiki/Lassie#/media/File:Lassie_and_Tommy_Rettig_1956.JPG

You might also like