Chapter 1
Hardware and Software
Hardware
1. Central Processing Unit (CPU)
a. The part of the computer that actually runs programs/software
b. “Most important component”
c. made of small chips - microprocessors
2. Main Memory (RAM)
a. Random Access Memory
b. Volatile type of memory – temporary storage while the program is running
c. RAM is stored in chips
3. Secondary Storage
a. Can hold data for long periods of time
b. most important type is a disk drive
c. increasingly popular is a solid state drive
4. Output Devices
a. ex. reports, video display, printer
5. Input Devices
a. ex keyboard, mouse
b. disk drives and optional drives can also be considered input devices
Software
1. Application Software
a. Ex. Excel/Word
2. System Software
Programs that control the basic operations of a computer
a. Operating System
b. Utility Programs
i. Perform a specialized task that enhances the computers operation or
safeguards data. ex. virus scanner, file compression, data backup
c. Software Development Tools
i. programs used to create, modify or test software. ex. assemblers,
compilers, interpreters
How Computers Store Data
Computer memory is divided into storage locations – bytes -> 8 bits in a byte
a. 1 byte = letter of the alphabet
b. bit – tiny electrical component that can store positive or negative charge
c.
d. By adding all the 1 you get the number. In this case 156. Max. 255 per byte
ASCII
coding scheme to represent characters (only 128)
Unicode
developed after ASCI to represent many other language (compatible with ASCII)
Two-Complement
A way for computers to store negative numbers
Floating Point Notation
a way for computers to store real numbers
Digital Data
Data stored in binary format
Other forms of data:
pixel – picture
samples – music
How a Program Works
Program
A list of instructions that cause the CPU to perform operations
CPU Instructions
Entire set of instructions that a CPU can execute
Fetch-Decode-Execute-Cycle
When a CPU executes instructions in a program
disk drive -> main memory (RAM) -> CPU
Low Level Languages
Machine Language
language understood by CPU
Assembly Language
a. Created as an alternative to machine language
b. uses short words knows and mnemonics
c. Assemblers are used to translate an assembly language into machine language
High Level Languages
need compilers and interpreters to be understood bz the CPU
ex. C, C++, c#, java, python…
Syntax
Keywords
Operators
= Statements
Interpreter
compiles & executes
compiler
only compiles/transltaes into machine language
Chapter 2
Program Development
Programs must be carefully designed before they are written. During the design process,
programmers use tools such as pseudocode and flowcharts to create models of programs.
Program Development Cycle
Design the Program
1. understand the task that the program is to perform
2. Determine the steps that must be taken to perform the task
a. Algorithm: set of well-defined logical steps that must be taken to perform a task
b. Flowcharts all are connected by arrows:
i. Oval = terminal symbols: mark the end or start of a program
ii. Parallelogram = Input and output Symbols
iii. Rectanges = processing symbol
Write the Code
Begins writing code in a high-level language
Correct Syntax Errors
Test the Program
Correct Logic Errors
logic errors are mistakes that do not prevent the program from running but cause it to produce
incorrect results. (Mathematical mistakes are common causes=
Displaying Output
Calling the function
when the programmers execute a function
String
Sequences of characters that are used as data
String Literal
When a string appears in the actual code of a program – must be enclosed in quote marks
Variables
Variable is a name that represents a value stored in the computer’s memory. Equal sign in in the
variable is called an assignment operator. Variables are case sensitive
Numeric Data Types and Literals
You can use type() to show what data type the value is
Int
Integers (7, 10, 12…)
Float
real number (1.5, 2.14159…)
Strings
Text
Numeric Literal
A number that is written into a programs code
Reading Input from the Keyboard
variable = input (prompt) – you can change the input (always a string) into floats or int
Nested Function Calls
pay_rate = float(input(‘What is zour hourly rate? ‘))
Performing Calculations
Operands
Values around the operations for example numbers in a sum formulas
There is an exception to the left-to-right rule. When two ** operators share an operand, the
operators execute right-to-left. For example, the expression 2**3**4 is evaluated as 2**(3**4).
end=’’
makes sure that the print function prints everything in the same line with a space afterwards
instead of a line break
sep=’’
Removes spaces between print items if you add anything between the ‘ that is what the sparation
will be for example *
Escape Characters
A special character preceded by \ -> ex
\n (new line)
\t (output skips over the next horizontal tab)
\’ single quote mark
\”
\\
Format Function
Format specifier: a string that contains special characters specifying how the numeric value
should be formatted
Float Numbers:
ex -> format(12345.6789, '.2f')
.2 = round number to two decimals
f= float
ex -> 12345.6789, ',.2f' -> 12,345.68
ex -> 12345.6789, '12,.2f' -> 12 = field width
Integers:
print(format(123456, ',d'))
123,456
Named Constants
Name that represents a value that cannot be changed during the program’s execution
Magic number: unexplained value that appears in the programs code
To distinguish named costants from regular valiables we write them in all uppercase ex:
INTEREST_RATE
Turtule Graphics
first you need to import the turtle graphics system into Python by stating – import turtle
The turtle (cursor) is initialy displayed in the middle of the screen. Zou can show I t vy using
[Link]()
The turtuel is initially facing 0 degrees = east
Turtle Commands
Import Turtle
[Link]()
[Link](angle)
[Link](angle)
[Link](pixels)
[Link](angle
[Link]()
[Link]()
[Link](radius)
[Link]()
[Link](width)
[Link](color)
[Link](color)
[Link]()
[Link]()
[Link]()
[Link](width, height)
[Link](x, y)
[Link]()
[Link](text)
turtle.begin_fill()
turtle.end_fill()
Chapter 3
The IF Statement
Used to create a decision structure with more than one path. Only executes when a
Boolean expression is true
If statements mainly used in single alternative decision structures with relational
operators >, <, ==
Control Structure
logical design that controls the order in which a set of statements executes
Sequence Structure
Set of statements that execute in the order in which they appear
Decision Structure/Selection Structure
executes a set of statements only under certain circumstances
The IF-ELSE Statement
will execute one block of statements if the condition is true or another if it is false.
Mainly used with dual alternative decision statements
Nested Decision Structures and IF-ELIF-ELSE Statement
Mainly used to test more than one condition
You can write manz if else, if else, or just use elif
Logic Operators
AND, OR, NOT
AND and OR operators perform short-circuit evaluations – meaning that if the operation on the
left is false it skips reading the operation on the right
NOT is a unary operation
Boolean Variables
Can either be True or False
Used as flags to indicate whether a specific condition exist
There is a bool data tzpe
ex -> Hungry = True
Chapter 4
Condition-Controlled loop: The WHILE loop
While a condition works, do some tasks.
AKA: Pretest Loop
1. A condition that is tested for true or false value
2. A statements or set of statements that is repeated as long as the
condition is true
While condition: (While Clause + Boolean condition)
statement
statement
etc.
if there is nothing to make the condition false it will be an infinite loop
Count-Controlled loop: The FOR loop
for variable in [value1, value2,…]: [list]
statement
statement
etc…
variable usually referred to as the target variable
using range -> function creates an object called an iterable (similar to a list).
for num in range (1, 10, 2) = print starting at 1 ending in 10 every 2 numbers (2 could also be a
negative number)
Calculating a Running Total
variable used to keep a running total = accumulator
ex. total = total + number OR total += number
Sentinels
a special value that marks the end of a sequence of values
Input Validation Loops
you can use a while loop to validate before performing anything else
sometimes called an error trap or an error handler
Nested Loops
1. inner loops go through all of its iterations for evey single iteration of an outer loop
2. Inner loops complete their iterations faster than outer loops
3. To et the total number of iterations of a nested loop multiply the number of iterations
of all the loops
Chapter 5
Function: a group of statements that exist within a program for a purpose of performing a
specific task. Usually used to break down one complex task into smaller ones.
Modulized program: a program that has been written with each task in its own function
Simpler code and easier to understand
Code can be reused
Better testing
Faster development
Easier teamwork
Void Functions
executes the statement that it contains then terminates
you must name a function usually programmers use verbs
To create a function you write its definition
def function_name():
statement
statement
we would call this function with function_name()
programs usually call for a main function and then call for other functions as they go.
main function contains the mainline logic
functions in flow charts are usually displayed as a rectangle with one vertical line on
each end and the function name in the middle with ().
Hierarchy charts are used to plan and draw out function relationships
Local Variables
local variables are created inside a function and cannot be accessed by statements that are outside
the function. Different functions can have local variables with the same names because the
functions cannot see each other local variables
Arguments in Functions
In order for a function to receive arguments you must equip the function with one or more
parameter variables
parameter variable: special variable that is assigned the value of an argument when a
function is called ex:
o def show_double(number):
result = number *2
It is possible to mix positional arguments and keyword arguments in a function call but the
positional arguments must come first
Global variables
Accessible to all the function in a program file. You should restrict using global varialbles:
debugging is difficult since any statement in the file can change the value
functions that use them are dependent on them
makes a program hard to understand
Global Constant
global name that references a value that cannot be changed
Constants should be written in all uppercase letters
Value-Returning Functions
executes the statement that it contains, then returns a value back to the statement that
called for it
In order to call a function that is stored in a module you have to use the import module.
ex
o import random
o number = [Link](1,100)
[Link](0,101,10)
returns a random value within the specified range (integ)
[Link]()
Returns a value between 0.o and 1.0 (float)
[Link](1.0,10.0)
returns a value between a specified range (float)
numbers in a random module are not truly random. They are pseudorandom numbers
that are calculated by a formula. This formula has to be initialized with a seed value. The
seed value imported from the computer from the internal clock. it would always give
the same response if executed at the same time of the hundredth second
You can always use [Link](10)
Return statement
Main difference for a value returning function is the return expression at the end of the block
IPO charts
input, processing and output – usually showin in columns
Storing Modules
As you write more functions you can store them into a program called a module.
the file name should end in .py
you can import them with an import statement and must be saved in the same folder as
the program
Menu Driven program
displays a “menu” that the user can pick from