Today’s Topics ...
• Intro to Computer Science (cont.)
• Python exercise Reading Assignment
Computation (cont.)
What “parts” do recipes usually have?
• A description (the requirements)
• An ingredient list (the input)
• A set of ordered steps to perform (a procedure)
A recipe is an example of procedural (imperative) knowledge
• Knowledge of how to perform some task (or process)
• A recipe gives a procedure to create (“compute”) an output from input
• Declarative knowledge only tells us about the output, not how to produce it Computation is a process for performing certain tasks
• Like the process of making your favorite dessert
• We typically think of computation as a process that manipulates data
• That is, computing (deriving) new data from existing data Computation is directed by a series of instructions
• Often called procedures, methods, or algorithms
• We write these down using a programming language
• A procedure written in a programming language is called a program
At the 10,000 foot level, as programmers
• We figure out what the instructions should be (the algorithm)
• We write them down as a program (in a programming language)
• We give the program to a computer with input data
• The computer runs the program on input data (the computation process)
• And supplies the output
A computer is a general-purpose machine (aka “universal machine”)
• Instead of being able to execute only one or a few types of processes – e.g., like a toaster or a car wash
• A computer can perform any computation – According to the Church-Turing Thesis
– Everything computable is computable by a “Turing Machine”
Another example ...
Consider the following two statements 1. √x is y such that y2 = x for y ≥ 0
2. To find an approximation of √x, make a guess g, improve the guess by averaging g and x/g, and continue until g is good enough
What type of knowledge do each of these represent?
Another way to think of computation ... (from Abelson & Sussman)
• Computation is like a sorcerer’s idea of a magical spirit – Cannot be seen or touched, is not composed of matter
• The programs we use to conjure computations are like a sorcerer’s spells – From symbolic expressions in arcane, esoteric programming langauges – Used to prescribe the tasks we want our computations to perform
• We’ll conjure our (computational) spirits in a magical language called Python
• Learning to program is considerably less dangerous than learning sorcery – Computational “spirits” are conveniently contained within a computer In case you aren’t convinced ...
def square_root(x, d):
g = 1 # not a great guess for i in range(1, d):
Getting started in the CS Lab
The CS lab guide
• http://www.cs.gonzaga.edu/startup_guide.html
The computers run the Linux (CentOS) operating system
• Similar to MS Windows and MacOS, but different
Login using GU userid and password
• If you can’t login, send email to Jason Schnagl (schnagl@gonzaga.edu, PC 216, 313-3534)
Double-click on the IDLE 3.2 desktop icon
• Starts a basic Python Shell
• Shows a prompt “>>>” called a “chevron”
• You can start typing statements at the prompt (e.g., 3+4) IDLE includes a simple code editor
• From the Python Shell window click File > New Window
• In the new window, note there is no prompt
• You can enter statements into the window
After you enter statements, you can “run” the module
• You need to save the module file first
• You might create a cpsc121 folder and save the file, e.g., as lab1.py
• Run the module by clicking Run > Run Modulein the editor (not the shell)
• You can also hit the F5 shortcut key
• After you run the module, you’ll see “RESTART” in the Python Shell
• This is where you wold see the “output” of your program Print statements
• The Python Shell prints the result of every statement entered
• E.g., >>> 3 + 4 prints 7
• In a module, you have to explicitly tell Python to print the result of a statement
• E.g., print(3 + 4) prints 7 in the Python Shell
Basic file management
• Double-click on the “Home” folder icon on the desktop
• For me, this is “bowers’s Home” Printing
• Select File > Print Window from within Python Shell or the editor and select the Default Printer
Don’t forget to logout!
• Select System > Log Out from the menu bar on the desktop
Exercise: The “Hello World!” Program
By convention, the first program written when learning a new langauge >>> print("Hello World!")
Hello World! >>>
• In Python, this is a super-easy program to write
Exercise: 1. Start IDLE
2. Using the Python Shell, use print to display “Hello World!” 3. Try print with other strings
4. Send your session to the printer
5. Repeat the above, but use the editor instead of the Python Shell 6. Put your name on both hardcopies and turn them in
Notes:
• print is a function
• We call the print function
• We pass arguments to function calls