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

1.4 Writing Paragraphs of Code

This document serves as an introduction to Python programming, covering basic concepts such as variables, syntax, and program structure. It explains the difference between interactive Python and script-based programming, as well as how to count words in a file using a Python script. The document emphasizes the importance of understanding program flow, including sequential, conditional, and repeated steps.

Uploaded by

daday daihi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

1.4 Writing Paragraphs of Code

This document serves as an introduction to Python programming, covering basic concepts such as variables, syntax, and program structure. It explains the difference between interactive Python and script-based programming, as well as how to count words in a file using a Python script. The document emphasizes the importance of understanding program flow, including sequential, conditional, and repeated steps.

Uploaded by

daday daihi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

PYTHON FOR

In t rod u ct ion – Pa rt 4 EVERYBODY

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

name = input('Enter file:')


handle = open(name)
A short “story” about
counts = dict()
for line in handle: how to count words
words = line.split() in a file in Python
for word in words:
counts[word] = counts.get(word,0) + 1

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)
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

Variable Operator Constant Function


(e.g., x) (e.g., = +) (e.g., 2) (e.g., print () )
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY

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.

• 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.
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY

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
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY

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).
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY

Sequential Steps
x=2 Program Output
x = 2
print(x)
print(x) 2
x=x+2 x = x + 2

print(x) print(x) 4

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


As programmers, we set up “paths” for the program to follow.
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY

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

n=5 Repeated Steps


Program Output
No Yes
n>0? n = 5
while n > 0: 5
print(n)
print(n) 4
n = n - 1 3
n = n -1
2
1
print(‘Blastoff!') Blastoff!
print('Blastoff') Loops (repeated steps) have iteration variables that
change each time through a loop.
PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY

Sequential name = input('Enter file:')


handle = open(name)

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

Sequential bigcount = None


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

Sequential print(bigword, bigcount)


PYTHON FOR
In t rod u ct ion – Pa rt 4 EVERYBODY

A short Python “Story” about


how to count words in a file
name = input('Enter file:') A word used to read data from a
user
handle = open(name, 'r')

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.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors and Translators here

You might also like