1.4 Writing Paragraphs of Code
1.4 Writing Paragraphs of Code
Talking to Python
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY
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?
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY
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()
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY
What Do We Say?
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY
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
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY
print(bigword, bigcount)
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY
Reserved Words
• You cannot use reserved words as variable names / identifiers
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY
Sentences or Lines
x = 2 Assignment statement
x = x + 2 Assignment with expression
print(x) Print function
Programming Paragraphs
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY
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.
Sequential Steps
x=2 Program Output
x = 2
print(x)
print(x) 2
x=x+2 x = x + 2
print(x) print(x) 4
x=5
Conditional Steps
Yes
x < 10 ?
Program Output
No
print('Smaller') x = 5
if x < 10: Smaller
print('Smaller')
Yes
x > 20 ? if x > 20:
No print('Bigger')
print('Bigger')
print('Finis') Finis
print('Finis')
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY
counts = dict()
Repeated for line in handle:
words = line.split()
for word in words:
counts[word] = counts.get(word,0) + 1
counts = dict()
for line in handle:
words = line.split()
for word in words:
counts[word] = counts.get(word,0) + 1 A sentence about updating one
of the many counts
bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count A paragraph about how to find
the largest item in a list
print(bigword, bigcount)
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY
Summary
• This is a quick overview of Chapter 1
• We will revisit these concepts throughout the course
• Focus on the big picture
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY
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.