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

Pythonlearn 01 Intro

The document discusses how computers are built to do tasks for users through programs, but programmers must understand computer languages to create programs for specific needs. It also provides examples of programs created for both humans in the form of dance instructions and programs created for computers using Python code.

Uploaded by

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

Pythonlearn 01 Intro

The document discusses how computers are built to do tasks for users through programs, but programmers must understand computer languages to create programs for specific needs. It also provides examples of programs created for both humans in the form of dance instructions and programs created for computers using Python code.

Uploaded by

Hưng Minh Phan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

10/01/21

Computers Want to be Helpful...


What
• Computers are built for one purpose - to Next?
do things for us
Why Program? • But we need to speak their language to
describe what we want done
Chapter 1
• Users have it easy - someone already put What What What
many different programs (instructions) into Next? Next? Next?
the computer and users just pick the ones
Python for Everybody they want to use What What What
www.py4e.com Next? Next? Next?

1 2

Programmers Anticipate
Users vs. Programmers
Needs
• iPhone applications are a market
• Users see computers as a set of tools - word processor, spreadsheet, map,
to-do list, etc.
• iPhone applications have over 3 billion
downloads
• Programmers learn the computer “ways” and the computer language
• Programmers have left their jobs to be
• Programmers have some tools that allow them to build new tools
full-time iPhone developers Pick Pick Pick
Me! Me! Me! • Programmers sometimes write tools for lots of users and sometimes
• Programmers know the ways of the
programmers write little “helpers” for themselves to automate a task
program Pick Pick Pay
Me! Me! Me!

3 4
10/01/21

Why be a Programmer? User

• To get some task done - we are the user and programmer Computer
Programmer
- Clean up survey data Hardware + Software

• To produce something for others to use - a programming job


Data Information .... Networks
- Fix a performance problem in the Sakai software
From a software creator’s point of view, we build the software. The end
- Add a guestbook to a web site users (stakeholders/actors) are our masters - who we want to please -
often they pay us money when they are pleased. But the data,
information, and networks are our problem to solve on their behalf.
The hardware and software are our friends and allies in this quest.

5 6

What is Code? Software? A Program? Programs for Humans...


• A sequence of stored instructions

- It is a little piece of our intelligence in the computer

- We figure something out and then we encode it and then give it to


someone else to save them the time and energy of figuring it out

• A piece of creative art - particularly when we do a good job on user


experience

https://www.youtube.com/watch?v=XiBYM6g8Tck

7 8
10/01/21

Programs for Humans... Programs for Humans...


while music is playing: while music is playing:
Left hand out and up Left hand out and up
Right hand out and up Right hand out and up
Flip Left hand Flip Left hand
Flip Right hand Flip Right hand
Left hand to right shoulder Left hand to right shoulder
Right hand to left shoulder Right hand to left shoulder
Left hand to back of head Left hand to back of head
Right ham to back of head Right ham to back of head
Left hand to right hit Left hand to right hit
Right hand to left hit Right hand to left hit
Left hand on left bottom Left hand on left bottom
Right hand on right bottom Right hand on right bottom
Wiggle Wiggle
Wiggle Wiggle
Jump Jump
https://www.youtube.com/watch?v=XiBYM6g8Tck https://www.youtube.com/watch?v=XiBYM6g8Tck

9 10

Programs for Humans... Programs for Python...


while music is playing:
Left hand out and up
Right hand out and up
Flip Left hand
Flip Right hand
Left hand to right shoulder
Right hand to left shoulder the clown ran after the car and the car ran into the tent and
Left hand to back of head the tent fell down on the clown and the car
Right hand to back of head
Left hand to right hip
Right hand to left hip
Left hand on left bottom
Right hand on right bottom
Wiggle
Wiggle
Jump
https://www.youtube.com/watch?v=XiBYM6g8Tck Image: https://www.flickr.com/photos/allan_harris/4908070612/ Attribution-NoDerivs 2.0 Generic (CC BY-ND 2.0)

11 12
10/01/21

name = input('Enter file:')


Programs for Python... handle = open(name)
python words.py
counts = dict()
for line in handle: Enter file: words.txt
words = line.split() to 16
for word in words:
counts[word] = counts.get(word,0) + 1

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

Image: https://www.flickr.com/photos/allan_harris/4908070612/ Attribution-NoDerivs 2.0 Generic (CC BY-ND 2.0) print(bigword, bigcount)

13 14

Hardware Architecture

http://upload.wikimedia.org/wikipedia/commons/3/3d/RaspberryPi.jpg

15 16
10/01/21

Software What
Next?
Generic
Computer
Definitions
Input Central • Central Processing Unit: Runs the Program - The CPU is What
and Output Processing always wondering “what to do next”. Not the brains Next?
Devices Unit exactly - very dumb but very very fast
Secondary • Input Devices: Keyboard, Mouse, Touch Screen
Memory
• Output Devices: Screen, Speakers, Printer, DVD Burner
Main • Main Memory: Fast small temporary storage - lost on reboot - aka RAM
Memory
• Secondary Memory: Slower large permanent storage - lasts until deleted - disk
drive / memory stick

17 18

Generic Generic
Software What Software What
Next? Computer Next? Computer
Input Central Input Central
and Output Processing and Output Processing
Devices Unit Devices Unit
Secondary 01001001 Secondary
if x< 3: print Memory 00111001 Memory

Main Main
Memory Memory
Machine
Language

19 20
10/01/21

Totally Hot CPU Hard Disk in Action

What
Next?

http://www.youtube.com/watch?v=9eMWG3fwiEU
http://www.youtube.com/watch?v=y39D4529FM4

21 22

Parseltongue is the language of serpents


and those who can converse with them. An
individual who can speak Parseltongue is
Python as a Language known as a Parselmouth. It is a very
uncommon skill, and may be hereditary.
Nearly all known Parselmouths are
descended from Salazar Slytherin.

http://harrypotter.wikia.com/wiki/Parseltongue

23 24
10/01/21

Python is the language of the Python Early Learner: Syntax Errors


Interpreter and those who can converse with
it. An individual who can speak Python is • 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
known as a Pythonista. It is a very uncommon small children.
skill, and may be hereditary. Nearly all known
Pythonistas use software initially developed • 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
by Guido van Rossum. 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...

25 26

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.
Talking to Python >>>
What
next?

27 28
10/01/21

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)
What Do We Say?
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()

29 30

name = input('Enter file:')


Elements of Python handle = open(name)
A short “story”
counts = dict()
for line in handle:
about how to count
• Vocabulary / Words - Variables and Reserved words (Chapter 2) words = line.split()
for word in words:
words in a file in
Python
• Sentence structure - valid syntax patterns (Chapters 3-5) counts[word] = counts.get(word,0) + 1

• Story structure - constructing a program for a purpose python words.py


bigcount = None
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)

31 32
10/01/21

Reserved Words Sentences or Lines


You cannot use reserved words as variable names / identifiers

False class return is finally x = 2 Assignment statement


None
True
if
def
for
from
lambda
while
continue
nonlocal
x = x + 2 Assignment with expression
and del global not with print(x) Print statement
as elif try or yield
assert else import pass
break except in raise
Variable Operator Constant Function

33 34

Python Scripts
• Interactive Python is good for experiments and programs of 3-4 lines
long.
Programming Paragraphs • 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.

35 36
10/01/21

Interactive versus Script Program Steps or Program Flow


• Interactive • Like a recipe or installation instructions, a program is a sequence of
steps to be done in order.
- You type directly to Python one line at a time and it responds
• Some steps are conditional - they may be skipped.
• Script • Sometimes a step or group of steps is to be repeated.
- You enter a sequence of statements (lines) into a file using a text
editor and tell Python to execute the statements in the file • Sometimes we store a set of steps to be used over and over as
needed several places throughout the program (Chapter 4).

37 38

Sequential Steps x=5


Conditional Steps
Yes
x < 10 ?
x=2 Program:
Output: print('Smaller') Program:
No Output:
print(x) x = 2
print(x) 2 x = 5
Yes if x < 10: Smaller
x=x+2 x = x + 2 4
x > 20 ? print('Smaller') Finis
print(x) if x > 20:
print(x) print('Bigger') print('Bigger')
No
print('Finis')
When a program is running, it flows from one step to the next. As
programmers, we set up “paths” for the program to follow. print('Finis')

39 40
10/01/21

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

41 42

Summary
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:
counts[word] = counts.get(word,0) + 1
data from a user • This is a quick overview of Chapter 1
bigcount = None A sentence about • We will revisit these concepts throughout the course
bigword = None
for word,count in counts.items():
updating one of the
many counts • Focus on the big picture
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

43 44
10/01/21

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

45

You might also like