unit 1 - weeblymswardecke.weebly.com/uploads/1/7/1/6/17167688/unit_1... · 2019-01-21 · 4...
TRANSCRIPT
UNIT 1Intro to Python
Graphics
Computer Hardware
DAY 1Python Introduction
I can…..
• Identify the 4 functions of a computer.
• Explain what at programming language, why it is used,
and its three types.
• Explain the difference between a compiler and interpreter.
• Write my first python program.
• Use the turtle graphics to create a basic drawing in
Python.
Functions of Computer
• Think – Pair – Share
Brainstorm the following question:
What are the main functions of all computers?
4 Functions of a Computers
1.Input
2.Process
3.Store
4.Output
4 Functions of a Computer
1. Input
• Computers take in information from its users
• Examples:
o Keyboard
o Mouse
o Audio
o Touch
2. Process
• Computers “do something” what that information
• Examples
o Calculators perform the inputted mathematical equation
o Phone opens an app when you touch it
4 Functions of a Computer
3. Store
• Computers store information in memory
• Examples:
o Save a document in Microsoft word
o Cell Phones stores your contacts
o Calculator stores “previous answer”
4. Output
• Computers output information for the user to see
• Examples:
• Audio stounds
• Visually displaying a document on the screen
What is a computer program?
•A program is a planned sequence
of instructions that tell a computer
what steps to perform and when to
perform them
•A program is also known as computers
software .
Think-Pair-Share
Brainstorm the following:
If writing a program is simply giving a computer
instructions, why can’t we use English? Why do we need
to learn computer languages?
Programming Languages
• Computers follow instructions EXACTLY they
cannot interpret meaning like humans can
• English language is confusing !
• Some words have multiple meanings
• Ex: lead the action and lead the metal
• Some words sounds the same, but spelled differently with different
meanings
• Ex: To, two, too
• Ex: Their, they’re, there
• Each word in a computer programming language has
ONE meaning as a result the computer will know
exactly what to do as it reads each instruction
Programming Languages
• What programming languages have you heard of?!?
Types of Computer Languages
1.High Level Languages
2.Assembly Language
3.Machine Language
High Level Languages
• Easier for programmer to use – similar to every
language and symbols
• Portable (can be used on different types of computers
without modification)
• Must be translated into something the computer
can understand
• Example Languages:
Pascal C++ Python
FORTRAN C Logo
BASIC C# Ava
COBOL Java
Assembly Language
• Commonly considered a mid -level or low -level language
• While still uses every day language, operations must be given more explicitly .
Example Code snippets:Assembly Language High Level Language
LOAD A C = A + B
ADD B if(x < 5)
STORE C C := A + B
• Actual words and symbols are translated into machine language
Machine Language
• Computer’s native language – the only type of program
a computer understands
• Higher level programs must be translated into machine
language
• Consists of a binary string ( 0 and 1s )
• Examples: 0100 0011 0011 1010
0011 1101 0100 0001
0010 1011 0100 0010
Translating Higher Level Machine
• High-level Language must be translated into machine
language before it can be executed
• A compiler is a program that translates the entire
program from the higher level language to machine
language
• Examples: C++, Pascal, Java
• An interpreter is a program that translates a program
from the higher level language into machine language
one line at a time
• Examples: BASIC, Visual Basic, Python
Python Programming Language
• Created by Guido van Rossum – released in 1991
• Interpreter language
• Focuses on easy readability
Fun Facts:
• Where did the name come from??
• Author had a love for the British comedy group Monty Python
• IDLE – Integrated DeveLopment Environment
• Eric Idle is one of Monty Python’s founding members
Where Can I find Python??
Two “Modes” of Python
• Interactive Mode - Shell
• Where our code will run/display to
the screen
• Where we can “interact” with our
program
• Will see “shell” across the top
• Script Mode .
• Where we write longer programs
– mostly programming text
• Will see a file path across the top
Interactive Mode - Python Shell
• Open up the Python IDLE Shell
• Shell is where your program will appear when it is run
• Where we can “interact” with our program
• Can Type Single Lines of Instruction
Examples:
>>> print(“Hello computer”) **press enter**
Hello computer
Script Mode - First Python Program
• Where we type programs longer than 1 line
To Create New Program:
File New File
File Save As
name: FirstProgram.py
(**all program names end in py)
Now may type instructions!
First Python Program
Type the following in your program:
print(“Hello Ms. Wardecke”)
print(“This is my first program”)
print(“It was a success!”)
To Run Program in Shell:
1. Save your program
2. Run Run Module
3. Your program will run in the shell! You should see 3
lines of code!
Commenting
• Commenting is an important part of the programming
process
• Benefits
• Others can read and comprehend the purpose of your code
• Finding errors .
• Remind you of the purpose of each set of code so in a day, month,
year, etc you will still remember the purpose of each line of code!
Requirement:
• You WILL BE required to comment your code!!!
Commenting in Python
# single line comment (hashtag)
“”” multiple line comment (3 double quotes
needed at start and end)
Examples:
“”” Project Name: First Program
Author: Sarah Wardecke
Date: 6/12/17”””
print(“Hello”) # prints hello message to the screen
Where do comments typically go?
• Beginning to introduce a program
• Next to a single line (or a few times) of code to summarize
its purpose
• The end to conclude a program
Let’s go add comments into your first program!
Organizing Files into Folders
• Right Click on your desk top – create a folder entitled
“Computer Science” – open this folder
• Right Click – create a folder entitled “Unit 1” – open this
folder
• Right Click – create a folder entitled “In Class Practice”
Create Unit 1 – Lesson 1 Program
• Open up the Python IDLE
• File New File
• File Save As
• Open up the Folder Path:
Computer Science/Unit 1/In Class Practice
Name: U1L1Graphics
Beginning Code
• Add the following to your program:
**All programs should begin with commenting discussing the
purpose of the program**
# Unit 1 Lesson 1 – Graphics Introduction
# <insert your name here>
Python Shell
• Dimensions: 700 (width) x 660 (height)
• Middle is point (0,0)
• Works like a coordinate plane in mathematics
(0,0)
Import Turtle Graphics
Type the following into the first line of your program:
import turtle #import turtle graphics
**this line of code imports the turtle graphics into your program.
Without this line of code, you cannot use graphics**
Create a Turtle Pen in your program
Add the following to your program:
tia = turtle.Turtle() # creates a turtle to draw with
# called “tia” you can create a
# turtle of any color your choose
tia.shape(“turtle”) # changes tia to a turtle shape
# rather than an arrow
Turtle Class Functions
• Turtle “pens” have many functions or “actions” – the python language has many built in functions that allows us to mimic a real pen. A few are listed below.
<turtle name>.forward(<#>)
<turtle name>.backward(<#>)
<turtle name>.left(<#>)
<turtle name>.right(<#>)
<turtle name>.width(<#>)
<turtle name>.goto(<x coordinate>, <ycoordinate>)
<turtle name>.pendown()
<turtle name>.penup()
Note: anything with < > symbols around them will be replaced with values unique to the program
Python Turtle Function Exploration Lab
Follow the Exploration Activity – once everyone is
completed, we will recap as a class!
Python Turtle Pen Functions
1. <turtle name>.forward(<#>)
• Moves the specified pen forward the numbers of steps (pixels)
specified.
• NOTE: forward = direction the pen is currently facing, NOT a
specific direction
2. <turtle name>.backward(<#>)
• Moves the specified pen backward the number of steps (pixels)
specified
• NOTE: backward = opposite direction the pen is currently facing
Python Turtle Pen Functions
3. <turtle name>.left(<#>)
• Rotates the specified pen the specified number of degrees left
(counterclockwise)
4. <turtle name>.right(<#>)
• Rotates the specified pen the specified number of degrees left
(counterclockwise)
Remember how degrees work:
360° = full circle 180° = half circle
90° = one four circle/right angle
Python Turtle Pen Functions
5. <turtle name>.width(<#>)
• Changes the width of the specified pen – the large the number, the
thicker the pen
6. <turtle name>.goto(<xcoord>, <ycoord>)
• Will send the pen to that specified location (remember the
background is an x-y plane)
• Will draw continue to draw a straight line with the pen on the way to
that location!
Python Turtle Pen Functions
7. <turtle name>.penup()
• Mimics picking the pen up off the paper – pen will not draw again
until placed back down
• You will still see the “turtle tracer” showing you the location of your
pen even when you are not drawing
8. <turtle name>.pendown()
• Puts the pen back down – the pen will now write!
DAY 2Computer Hardware (processing devices)
Python Graphics
I can…
• Describe the key processing components of a computer.
• Use the turtle graphics to create a basic drawing in
Python.
The Hardware Parts of a Computer
Input devices
Central Process-ing Unit
Main Memory
Auxiliary Memory
Output devices
Bus / Interface (includes motherboard)
Input Processing Storage Output
Processing Devices
Central Processing Unit
• Also called the CPU .
• The brain of the computer system
• Most complex component of the computer
Our computers:
• Single silicon chip called a microprocessor .
Main Components of the CPU
1. Arithmetic-Login Unit (ALU) :
• Performs:
• Arithmetic calculations (+, -, x, ÷)
• Logical comparisons of two numbers (=, <, >)
• Logical functions (and, not, & or)
Main Components of the CPU
2. Control Unit :
• Controls the workings of the computer through a
sequential “fetch-execute” cycle
• Information is “fetched” from memory and sent to
the ALU
• As the ALU “executes” the command or processes
data, the control unit fetches more data
Main Components of the CPU
3. Registers :
• On-chip memory for temporary storage of data or
instructions
4. Floating Point Processing Unit (FPU) :
• Specialized, optional unit to perform arithmetic
calculations (+, -, x, ÷) on floating point (non-integer)
numbers
Main Components of the CPU
5. Clock :
• Drives operation of the CPU by electrical impulses
originatin from a quartz crystal
• Speed is measured in one of two ways:
• Millions of impulses per section (megahertz or MHz)
• Billions per section (gigahertz or GHz)
Turtle Pen Graphics - Continued
• Finish Exploration 1
• Exercises 1-5
Graphics Exploration 2
Create Unit 1 – Lesson 2 Program
• Open up the Python IDLE
• File New File
• File Save As
• Open up the Folder Path:
Computer Science/Unit 1/In Class Practice
Name: U1L2GraphicsExtended
Beginning Code
• Add the following to your program:
**All programs should begin with commenting discussing the
purpose of the program**
# Unit 1 Lesson 2 – Graphics Extended
# <insert your name here>
import turtle # import graphics
<turtle name> = turtle.Turtle() # create turtle Pen
More Pen Class Functions
• Now that we’ve learned the basics – let’s extend our
knowledge of the pen class further!
<turtle name>.color(“<color>“) **string
<turtle name>.color(<red#>, <green#>, <blue#>) # = intensity between 0 and 1
<turtle name>.begin_fill()
<turtle name>.end_fill()
<turtle name>.fillcolor(“<color>”)
<turtle name>.circle(<radius>)
<turtle name>.write(“<words>”)
Note: anything with < > symbols around them will be replaced with
values unique to the program
Python Pen Function Exploration Lab 2
Similar to yesterday - Follow the Exploration Activity – once
everyone is completed, we will recap as a class!
Python Pen Functions
1. <turtle name>. color(“<color>“)
• Changes the pen color to the specified color in the quotations
• NOTE: some colors may not be recognize by python
2. <turtle name>. color(<#>, <#>, <#>)
• Changes the pen color to the specified color combination
• Each number must be the range of 0 and 1 and represents:
red intensity, green intensity, and blue intensity
(order matters!)
Python Pen Functions
3. <turtle name>. begin_fill()
• Notes the beginning of an object to be filled in
4. <turtle name>. end_fill()
• Notes the end of an object to be filled in with color
• The shape starts with “begin fill”’ and ends with “end fill”
Python Pen Functions
5. <turtle name>. fillcolor(“<color>”)
• Changes the “fill shape” color
• NOTE: not all colors will be recognized by python
• Like the pen, can also use the 3 number combination
6. <turtle name>. circle(<radius>)
• Creates a circle where the pen is the bottom of the circle is with a
radius with the specified value
• Remember – radius means distance from the center of the circle to
the outside of the circle
Python Pen Functions
7. <turtle name>. write(“<words>”)
• Will write the specified words to the screen
DAY 3Computer Hardware - Storage
Python Graphics – continued!
I can…
• Describe the key storage components of a computer.
• Use the turtle graphics to create a basic drawing in
Python.
The Hardware Parts of a Computer
Input devices
Central Process-ing Unit
Main Memory
Auxiliary Memory
Output devices
Bus / Interface (includes motherboard)
Input Processing Storage Output
Storage Devices! – TWO TYPES
1. Main Memory , also known as
Internal/Primary Storage .
2. Auxiliary Memory , also known as
External/Secondary Storage .
Main Memory
1. Main Memory , also known as
Internal/Primary Storage :
• Computer chips called microchips which
can hold a certain amount of information
• Each location in memory has an address ,
and can be thought of as a similar to a
mailbox
Types of Main Memory
A.RAM
B.ROM
Types of Main Memory
A. Random Access Memory or RAM .
• Computer’s temporary or working memory
• Stores data in binary ( machine language ) –
electrical impulses like switches that are “on” (1) or
“off” (0)
• Each “switch” is called one bit and it takes eight
bites to form a byte .
• Information in RAM is stored electronically and
therefore is lost when the computer is turned
off.
Types of Main Memory
B. Read Only Memory or ROM .
• Information/programs permanently stores on
certain chips chips can only be read (not
changed like RAM)
• Information stays even after computer turned off
• Contain machine language to control….
• Start-up routines (tell the computer what to do
when turned on)
• Routines to control input/output devices
• Programs to interpret information and carry out
commands
Auxiliary Memory
2. Auxiliary Memory , also known as
External/Seconday Storage :
• Large, permanent files of information stored
outside of computer
Types of Auxiliary Memory
A. Magnetic Tape
B. Magnetic Disk
C. Hard Drive
D. Compact Disc (CD)
E. Flash Memory
Types of Auxiliary Memory
A. Magnetic Tape
• Similar to the recording tape found in reel to reel
recorder or cassette tape
• Data stores sequentially (one after another)
• Very slow to read
• Holds very large quantities of data
Types of Auxiliary Memory
B. Magnetic Disk
• Plastic, coated with metallic oxide
• Information is recorded magnetically .
• Disk drive spins disk, read/write heads record and
play back information
• Disk is formatted into tracks (concentric circles)
and sectors (section of track) for store information
• Data on disk does not need to be store sequentially
• Information retrieved be sending the read/write head
to the location indicated by the disk’s directory
Types of Auxiliary Memory
C. Hard Drive
• Similar to floppy disk, but usually internal to the
computer
• Several disk surfaces and read/write heads
Types of Auxiliary Memory
D. Compact Disks (CD)
• Information is stored optically through a
series of “pits” and flat surfaces read my
lasers
• Information on…
• CD-ROM = permanent
• CD-R = recordable
• CD-RW = recordable and rewriteable
• Very fast and reliable due to lasers (and no
mechanical parts!)
Types of Auxiliary Memory
E. Flash Memory
• Used in memory cards and USB flash drives
• Non-volatile: does not need power to retain
information
• Stores information in an array of floating gate
transistors called “cells”
• Can be accesses much faster than CDs and hard
drives
Memory Storage Devices
Turtle Pen Graphics - Continued
• Finish Exploration 2
• Exercises 6-9
DAY 4Computer Hardware - Input
Python Graphics – continued!
I can…
• Describe the key input components of a computer.
• Describe the key output components of a computer.
• Use the turtle graphics to create a basic drawing in
Python.
The Hardware Parts of a Computer
Input devices
Central Process-ing Unit
Main Memory
Auxiliary Memory
Output devices
Bus / Interface (includes motherboard)
Input Processing Storage Output
Input Devices - TPS
With a partner, discuss the following:
Name as many input devices for the
computer as you can think of!
Input Devices
a. Keyboard
b. Mouse
c. Microphone
d. Touch Screen
e. Scanner
f. Digital Camera
g. Video Camera
h. Modem
i. Microphone
j. Network Card
k. Controller
l. USB
Output Devices - TPS
With a partner, discuss the following:
Name as many output devices for the
computer as you can think of!
Output Devices
a. Monitor
b. Printer
c. Audio Speakers
d. Modem
e. USB
f. Touch Screen
g. Network Card
h. Controller
(vibrate)
**note – some devices are
input AND output**
Input-Output TerminologyNOTE: Often, input and output devices are discussed
together, and are sometimes called
I/O devices .
Other Functions Functions
• Extra Functions
<screen name> = Screen()
<screen name>.bgcolor(“<color>”)
<pen name>.circle(<radius>, <degrees>)
<pen name>.dot(<diameter> , <color>)
<pen name>.speed(<integer 0 to 10>)
Note: anything with < > symbols around them will be replaced with
values unique to the program
**Note – you can research more. These are simply the functions we
are covering in our class**
Python Pen Function Exploration Lab 3
Similar to yesterday - Follow the Exploration Activity – once
everyone is completed, we will recap as a class!
Python Extra Functions
1. <screen name> = turtle.Screen()
• Creates an object that allows you to control characteristics of the
screen
1. <screen name>.bgcolor(“<color>”)
• Changes the “background” color of the screen
• NOTE: not all colors will be recognized by python
• Like the pen, can also use the 3 number combination
Python Extra Functions
3. <turtle name>.circle(<radius>, <degrees>)• Creates a part of a circle
• The first number represents the radius of the circle
• The number of degrees discuss how much of the circle to draw
• 360 degrees = full circle
• 180 degrees = half circle
• Can do partial circles
• Starts where and in the direction the pen is currently facing
4. <turtle name>.dot(<diameter> , “<color>”)• Creates a filled in “dot” on the screen
• The first number represents the diameter of the dot
• The second color selects the color of the dot
• Pen in the CENTER of the dot
Python Extra Functions
5. <turtle name>.speed(<integer 0 to 10>)
• Changes the speed at which the pen will draw the instructions you
have given the computer
6. What happens when you create two pens? Will they
draw at the same time? What happens?
DAY 5Computer Hardware – Output
Output in python: print() function
I can…
• Display text output to the screen using Python.
Open a New Python Program
• File New File
• File Save As
• Name: U1L4Output
Print() function – 3 options
1. print(“<string>”)
• Prints the exact message in the quotations to the screen and
places an “enter” after the string
2. print(“<string>”, “<string>”, ……, “<string>”)
• Prints the messages one after another, the comma “connects” the
strings together
• A space is put in place of every common as it “concatenates” the
strings together
3. print(“”” <string> “””)
• Prints EXACTLY what is inside the triple double quotes, enters and
spaces included
Code the following
Type the following into Python’s script window
print(“Hello, Computer!”)
Code the following
Add the following line of code – note the small difference
print(“Hello” , “Computer!”)
How is this line of printed code slightly different than the
code on the previous slide?
Quotations and commas
Pay close attention to where the quotations are in relation
to the commas
print(“Hello, Computer!”)
**comma inside of quotations = a comma printed to screen**
**comma outside of quotations = “concatenate and add a space”**
print(“Hello” , “Computer!”)
Code the following
Add the following line of code
print(“Hello
Computer!”)
How is this line of printed code slightly different than the
code on the previous slide?
Code the following
Whitespace means something in Python! The code on the
previous slide throws an error! However we can fix this!
print(“”“Hello
Computer!”””)
The triple double quotations tell the computer to print the
message EXACTLY – enters and spaces included!
Code the following
If you want things to line up
print(“”“
Hello
Computer!”””)
Make sure the all lines of text start and end at the same
location!
Code the following
Type the following
print(“”“
__________
\ \
\__________\
“””)
Code the following
If you want things to line up
print(“”“
__________ .
\ \ .
\__________\ .
“””)
Make sure you start and end each line in the same spot
(use period to mark the end of the line)
Practice Drawing In the Shell!
• Using the print() function, create a drawing on the screen
that meets the following specifications
• Has a “Title” at the top naming the drawing
• Creates a drawing of your choice – I would suggest
choosing a basic shape to start (square, rectangle,
hexagon, etc)
• Your drawing must contain at least 7 rows of text to be
displayed on the screen
Open a New Python Program
• File New File
• File Save As
• Name: U1L5OutputExtras
Code the following
Type the following into Python’s script window
print(“My name is: \t Sarah”)
**Note: this is a forward slash: \t (NOT “/t”)**
Predict: What do you think is going to happen??
\t
In the print statement – the symbols “\t” communicate
something to the computer
print(“My name is: \t Sarah”)
\t = TAB
Code the following
Type the following into Python’s script window
print(“My name is: \n Sarah”)
**Note: this is a forward slash: \n (NOT “/n”)**
Predict: What do you think is going to happen??
\n
In the print statement – the symbols “\n” communicate
something to the computer
print(“My name is: \n Sarah”)
\n = NEW LINE
Coding Practice
• Write the code that will make the following print to the
screen using ONLY ONE PRINT STATEMENT.
0 1 1 2 3
5 8 13 21 34
55 89 144 233 377
Numbers in the Print Statement
Type the following into Python’s script window
print(“7 + 2”)
Predict: What do you think is going to happen??
Numbers in the Print Statement
Take away the quotations!
print(7 + 2)
Predict: What do you think is going to happen??
Numbers in the Print Statement
• Numbers are different than strings in the print statement!
• Quotations = print characters exactly as written, like a
string
print(“7 + 2”) 7 + 2
• No quotations = computer the math, then print the result
print(7 + 2) 9
Practice
• Write down what the following print statements will print to
the screen – if there will be an error, note “error”
1. print(“7*3 + 4 = “ , 5*5)
2. print(“7*3 + 4 = “ 5*5)
3. print(3*5 + 6)
4. print(hello)
5. print(“my result: “ , 8*6/4)
6. print(14-6 , 15*10 , 16//3)
Practice - Solutions
• Write down what the following print statements will print to
the screen – if there will be an error, note “error”
1. print(“7*3 + 4 = “ , 5*5) 7*3 + 4 = 25
2. print(“7*3 + 4 = “ , 5*5) ERROR – need comma
3. print(3*5 + 6) 21
4. print(“hello”) ERROR – need quotes around string
5. print(“my result: “ , 8*6/4) my result: 12
6. print(14-6 , 15*10 , 16/4) 8 150 4
Introduce Projects
• Project 1A: Initials
• Project 1B: Smiley Face
• Project 1C: Trapezoid in print statements
• Project 1D: Dream House
• Project 1E: Flags Project