KEMBAR78
Gaddis Python 4e Chapter 01 PPT - combinedForLesson1 | PDF | Computer Data Storage | Random Access Memory
0% found this document useful (0 votes)
77 views62 pages

Gaddis Python 4e Chapter 01 PPT - combinedForLesson1

The document provides an introduction to computers and programming, covering topics such as hardware and software, how computers store data, how programs work, and using Python. It discusses the central processing unit, memory, storage devices, input and output devices, and how software controls computer operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views62 pages

Gaddis Python 4e Chapter 01 PPT - combinedForLesson1

The document provides an introduction to computers and programming, covering topics such as hardware and software, how computers store data, how programs work, and using Python. It discusses the central processing unit, memory, storage devices, input and output devices, and how software controls computer operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 62

CHAPTER 1

Introduction to
Computers
and
Programming

Copyright © 2018 Pearson Education, Ltd.


Topics
• Introduction
• Hardware and Software
• How Computers Store Data
• How a Program Works
• Using Python

Copyright © 2018 Pearson Education, Ltd.


Introduction
• Computers can be programmed
• Designed to do any job that a program tells
them to
• Program: set of instructions that a
computer follows to perform a task
• Commonly referred to as Software
• Programmer: person who can design,
create, and test computer programs
• Also known as software developer
Copyright © 2018 Pearson Education, Ltd.
Computers Want to be Helpful...
• Computers are built for one What
Next?
purpose - to do things for us
• But we need to speak their
language to describe what we
want done
• Users have it easy - someone What What What
already put many different Next? Next? Next?

programs (instructions) into the What What What


Next? Next? Next?
computer and users just pick
the ones they want to use
Users vs. Programmers
• Users see computers as a set of tools - word
processor, spreadsheet, map, to-do list, etc.
• Programmers learn the computer “ways” and the
computer language
• Programmers have some tools that allow them to
build new tools
• Programmers sometimes write tools for lots of users
and sometimes programmers write little “helpers”
for themselves to automate a task
User

Computer
Programmer
Hardware + Software

Data Information .... Networks

From a software creator’s point of view, we build the


software. The end 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.
Programs for Humans...

https://www.youtube.com/watch?v=XiBYM6g8Tck
Programs for Humans...
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
Left hand to back of head
Right ham to back of head
Left hand to right hit
Right hand to left hit
Left hand on left bottom
Right hand on right bottom
Wiggle
Wiggle https://www.youtube.com/watch?v=XiBYM6g8Tck
Jump
Programs for Humans...
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
Left hand to back of head
Right ham to back of head
Left hand to right hit
Right hand to left hit
Left hand on left bottom
Right hand on right bottom
Wiggle
Wiggle https://www.youtube.com/watch?v=XiBYM6g8Tck
Jump
Programs for Humans...
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
Left hand to back of head
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 https://www.youtube.com/watch?v=XiBYM6g8Tck
Jump
Programs for Python...

the clown ran after the car and the car ran into the
tent and the tent fell down on the clown and the car
name = input('Enter file:')
handle = open(name)
python words.py
Enter file: clown.txt
counts = dict() the 7
for line in handle:
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 A short “story”
about how to count
print(bigword, bigcount) words in a file in
Python
Hardware and Software
• Hardware: The physical devices that
make up a computer
• Computer is a system composed of several
components that all work together
• Typical major components:
• Central processing unit
• Main memory
• Secondary storage devices
• Input and output devices
Copyright © 2018 Pearson Education, Ltd.
Generic
Software What
Next? Computer
Input Central
and Output Processing
Devices Unit
Secondary
Memory

Main
Memory
The CPU
• Central processing unit (CPU): the part
of the computer that actually runs
programs
• Most important component
• Without it, cannot run software
• Used to be a huge device
• Not the brains exactly - very dumb but very
very fast
• Microprocessors: CPUs located on
small chips
Copyright © 2018 Pearson Education, Ltd.
Main Memory
• Main memory: where computer stores a
program while program is running, and
data used by the program
• Known as Random Access Memory or
RAM
• CPU is able to quickly access data in RAM
• Volatile memory used for temporary storage
while program is running
• Contents are erased when computer is off

Copyright © 2018 Pearson Education, Ltd.


Secondary Storage Devices
• Secondary storage: can hold data for long
periods of time
• Programs normally stored here and loaded to
main memory when needed (lasts until deleted)
• Types of secondary memory
• Disk drive: magnetically encodes data onto a
spinning circular disk
• Solid state drive: faster than disk drive, no moving
parts, stores data in solid state memory
• Flash memory: portable, no physical disk
• Optical devices: data encoded optically

Copyright © 2018 Pearson Education, Ltd.


Input Devices
• Input: data the computer collects from
people and other devices
• Input device: component that collects
the data
• Examples: keyboard, mouse, touchscreen,
scanner, camera
• Disk drives can be considered input devices
because they load programs into the main
memory

Copyright © 2018 Pearson Education, Ltd.


Output Devices
• Output: data produced by the computer
for other people or devices
• Can be text, image, audio, or bit stream
• Output device: formats and presents
output
• Examples: video display, printer
• Disk drives and USB drives can be
considered output devices because data is
sent to them to be saved

Copyright © 2018 Pearson Education, Ltd.


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

Main
Memory
Generic
Software What
Next? Computer
Input Central
and Output Processing
Devices Unit
01001001 Secondary
00111001 Memory

Main
Memory
Machine
Language
Totally Hot CPU

What
Next?

http://www.youtube.com/watch?v=y39D4529FM4
Hard Disk in Action

http://www.youtube.com/watch?v=9eMWG3fwiEU
Software
• Everything the computer does is
controlled by software
• General categories:
• Application software
• System software
• Application software: programs that
make computer useful for every day
tasks
• Examples: word processing, email, games,
and Web browsers
Copyright © 2018 Pearson Education, Ltd.
Software (cont’d.)
• System software: programs that control
and manage basic operations of a
computer
• Operating system: controls operations of
hardware components
• Utility Program: performs specific task to
enhance computer operation or safeguard
data
• Software development tools: used to create,
modify, and test software programs
Copyright © 2018 Pearson Education, Ltd.
How Computers Store Data
• All data in a computer is stored in
sequences of 0s and 1s
• Byte: just enough memory to store
letter or small number
• Divided into eight bits
• Bit: electrical component that can hold
positive or negative charge, like on/off switch
• The on/off pattern of bits in a byte represents
data stored in the byte

Copyright © 2018 Pearson Education, Ltd.


Storing Numbers
• Bit represents two values, 0 and 1
• Computers use binary numbering
system
• Position of digit j is assigned the value 2j-1
• To determine value of binary number sum
position values of the 1s
• Byte size limits are 0 and 255
• 0 = all bits off; 255 = all bits on
• To store larger number, use several bytes

Copyright © 2018 Pearson Education, Ltd.


Storing Characters
• Data stored in computer must be stored
as binary number
• Characters are converted to numeric
code, numeric code stored in memory
• Most important coding scheme is ASCII
• ASCII (American Standard Code for Information
Interchange) is limited: defines codes for only 128
characters
• Unicode coding scheme becoming standard
• Compatible with ASCII
• Can represent characters for other languages
Copyright © 2018 Pearson Education, Ltd.
Advanced Number Storage
• To store negative numbers and real
numbers, computers use binary
numbering and encoding schemes

• Negative numbers encoded using two’s


complement

• Real numbers encoded using floating-point


notation

Copyright © 2018 Pearson Education, Ltd.


Other Types of Data
• Digital: describes any device that
stores data as binary numbers
• Digital images are composed of pixels
• To store images, each pixel is converted to a
binary number representing the pixel’s color
• Digital music is composed of sections
called samples
• To store music, each sample is converted to a
binary number

Copyright © 2018 Pearson Education, Ltd.


How a Program Works
• CPU designed to perform simple
operations on pieces of data
• Examples: reading data, adding, subtracting,
multiplying, and dividing numbers
• Understands instructions written in machine
language and included in its instruction set
• Each brand of CPU has its own instruction set
• To carry out meaningful calculation,
CPU must perform many operations

Copyright © 2018 Pearson Education, Ltd.


How a Program Works
(cont’d.)
• Program must be copied from
secondary memory to RAM each time
CPU executes it
• CPU executes program in cycle:
• Fetch: read the next instruction from memory
into CPU
• Decode: CPU decodes fetched instruction to
determine which operation to perform
• Execute: perform the operation
Copyright © 2018 Pearson Education, Ltd.
How a Program Works
(cont’d.)

Figure 1-16 The fetch-decode-execute cycle

Copyright © 2018 Pearson Education, Ltd.


From Machine Language to
Assembly Language
• Impractical for people to write in
machine language
• Assembly language: uses short words
(mnemonics) for instructions instead of
binary numbers
• Easier for programmers to work with
• Assembler: translates assembly
language to machine language for
execution by CPU
Copyright © 2018 Pearson Education, Ltd.
High-Level Languages
• Low-level language: close in nature to
machine language
• Example: assembly language
• High-Level language: allows simple
creation of powerful and complex
programs
• No need to know how CPU works or write
large number of instructions
• More intuitive to understand

Copyright © 2018 Pearson Education, Ltd.


Key Words, Operators, and
Syntax: an Overview
• Key words: predefined words used to
write program in high-level language
• Each key word has specific meaning
• Operators: perform operations on data
• Example: math operators to perform arithmetic
• Syntax: set of rules to be followed when
writing program
• Statement: individual instruction used in
high-level language
Copyright © 2018 Pearson Education, Ltd.
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
Compilers and Interpreters
• Programs written in high-level
languages must be translated into
machine language to be executed
• Compiler: translates high-level
language program into separate
machine language program
• Machine language program can be executed
at any time

Copyright © 2018 Pearson Education, Ltd.


Compilers and Interpreters
(cont’d.)
• Interpreter: translates and executes
instructions in high-level language
program
• Used by Python language
• Interprets one instruction at a time
• No separate machine language program
• Source code: statements written by
programmer
• Syntax error: prevents code from being
translated
Copyright © 2018 Pearson Education, Ltd.
Compilers and Interpreters
(cont’d.)

Figure 1-19 Executing a high-level program with an interpreter

Copyright © 2018 Pearson Education, Ltd.


COMPILER INTERPRETER

It translates the full source .1 It translates one line of code at a .1


.code at a time .time
It translates one line of code .2 .Comparatively slower .2
.at a time
It uses more memory to .3 The interpreter uses less memory .3
.perform .than the compiler to perform
Error detection is difficult for .4 Error detection is easier for the .4
.the compiler .interpreter
It shows error alert after .5 Whenever it finds any error it stops .5
.scanning the full program .there
Before execution of a .6 Compilation and execution for a .6
program, the compilation is .program are done simultaneously
.done
Copyright © 2018 Pearson Education, Ltd.
Python as a Language
Parseltongue is the language of
serpents and those who can
converse with them. An individual
who can speak Parseltongue is
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
Python is the language of the Python
Interpreter and those who can
converse with it. An individual who can
speak Python is known as a
Pythonista. It is a very uncommon skill,
and may be hereditary. Nearly all
known Pythonistas use software
initially developed by Guido van
Rossum.
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...
Using Python
• Python must be installed and
configured prior to use
• One of the items installed is the Python
interpreter
• Python interpreter can be used in two
modes:
• Interactive mode: enter statements on
keyboard
• Script mode: save statements in Python script

Copyright © 2018 Pearson Education, Ltd.


Interactive Mode
• When you start Python in interactive
mode, you will see a prompt
• Indicates the interpreter is waiting for a
Python statement to be typed
• Prompt reappears after previous statement is
executed
• Error message displayed If you incorrectly
type a statement
• Good way to learn new parts of Python
Copyright © 2018 Pearson Education, Ltd.
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
>>> print(x) This is a good test to make sure that you
2 have Python correctly installed. Note that
>>> exit() quit() also works to end the interactive
session.
Sentences or Lines

x=2 Assignment statement


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

Variable Operator Constant Function


Writing Python Programs and
Running Them in Script Mode
• Statements entered in interactive mode
are not saved as a program
• To have a program use script mode
• Save a set of Python statements in a file
• The filename should have the .py extension
• To run the file, or script, type
python filename
at the operating system command line

Copyright © 2018 Pearson Education, Ltd.


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
The IDLE Programming
Environment
• IDLE (Integrated Development
Program): single program that provides
tools to write, execute and test a
program
• Automatically installed when Python language
is installed
• Runs in interactive mode
• Has built-in text editor with features designed
to help write Python programs

Copyright © 2018 Pearson Education, Ltd.


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.
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 ?

No
print('Smaller') Program: 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 :
3
print(n)
n = n -1 n=n–1 2
print('Blastoff!') 1
Blastoff!

print('Blastoff')
Loops (repeated steps) have iteration
variables that change each time through a
loop.
name = input('Enter file:')
handle = open(name, 'r')
Sequential
counts = dict()
for line in handle: Repeated
words = line.split()
for word in words: Conditional
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
handle = open(name, 'r')
“Story” about how to
counts = dict() count 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
A sentence about
bigcount = None
bigword = None updating one of the
for word,count in counts.items(): many counts
if bigcount is None or count > bigcount:
bigword = word A paragraph about
bigcount = count how to find the
largest item in a list
print(bigword, bigcount)
Summary
• This chapter covered:
• Main hardware components of the computer
• Types of software
• How data is stored in a computer
• Basic CPU operations and machine language
• Fetch-decode-execute cycle
• Complex languages and their translation to
machine code
• Installing Python and the Python interpreter
modes
Copyright © 2018 Pearson Education, Ltd.
Acknowledgements / Contributions

These slides are Copyright 2010- Charles R. Continue…


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

You might also like