CS 110X Jan 16 2014
Lecture Path: 01
Next
Expected reading: pages 1-11 Course Structure and Details.
Expected interaction:
Programming
intro
Interactive
interpreter
Proposition 15: Until you work it out for yourself, two times two makes four only because the teacher says so. Mathsemantics: Making Numbers Talk Sense, Edward Macneal
1 A Journey Of A Thousand Miles Begins With A Single Step
Find the area of a ring whose outer radius is outer=5 meters and inner radius inner=3 meters.
The sketch in Figure 1 shows you how to solve the problem using basic knowledge of mathematics.
Let’s solve this problem now.
1.1 Manual
The first question to address is what value to use for pi. I’m sure you must recall someone telling you that pi is a transcendental number and cannot be expressed as a fixed decimal value. Should you use 3? If so, then you would compute the disk are to be 48. Should you use the popular fractional approximation of 22/7? if so, then you have 50.29 as your answer. Perhaps you feel like using 3.14, which gives you a result of 50.24. Do you think you would be foolish using 3.1415926535897932384626433832795 as pi to state your answer of 50.265482457436691815402294132472?
British mathematician William Shanks (1873) took 15 years to calculate pi to 707 digits, but made a mistake in the 528th digit meaning the final 125 digits were incorrect. Wikipedia
As we will learn throughout this course, you need to know your requirements to be able to answer the questions given to you. You need to know the precision (and type) of your input data as well as the expected precision (and types of your output. And you need to learn the role that computation plays in programming.
The second question to address is whether this is a one-time computation or whether you have twenty such computations to perform. Or two thousand? How would you proceed? Well, prior to the invention of the Spreadsheet, most people would have broken out a calculator and started working on the problem. Such a repetitive task would be annoying, but could certainly be done.
1.2 Spreadsheet
The invention and widespread use of spreadsheets were game changing, enabling everyone to automate routine tasks. The spreadsheet in Figure 2 would be a reasonable solution to the tedious task of computing a number of "area of disk" computations.
While you might not have noticed it, when using Excel you are actually carrying out a "programming task" using a declarative style of programming. You choose where you want to place your input values (in this case, in columns A and B). The output for computations are found in column C which contain the single-line expressions that use Excel expressions to compute the desired values.
Programming lets you take computations "out of the spreadsheet" and you then have total control of (and full responsibility for!) the computation.
Computer Science is the study of all things computational. In this course we will be limiting our attention to developing the skills needed to write programs to solve meaningful tasks. In fact you will gain the necessary intuition to be able to meaningfully respond when asked "Can you write a program to do task X?"
Refuse to accept practical limitations. Learn to program!
1.3 Programming
So let’s get started by using Python. I have provided extra material showing you exactly what you need to install on your computer (PC, Mac, or Linux). Throughout this course, I will use Python 2.7.3 as distributed by Enthought.
Facts are hilighted in orange. Pay attention to these!
A computer program is a detailed, step-by-step set of instructions telling a computer exactly what to do. (p2)
You can begin by executing idle on your computer, which can be launched right from the command line (if you have properly installed the python distribution available from Enthought). Alternatively you can locate the directory on disk where you installed Python and find the "Scripts" sub-directory within it. Double-click on the "idle.exe" icon to launch idle.
You are going to be running idle in "interactive mode" where you can type mathematical expressions and see their computations. This will make idle look similar to a calculator. When you launch idle you will receive the following prompt (get used to this; you’ll be seeing this countless times over the next few weeks). Make sure that the version of Python is 2.7.3 (or 2.7.4) so the following code examples will work properly.
Python 2.7.3 |EPD_free 7.3-2 (32-bit)| (default, Apr 12 2012, 14:30:37) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>>
The >>> is prompting you to enter in a Python command. Now let’s put Python to work to solve the "Area of disk problem" stated at the beginning of this lab. Type in the following exactly as it appears
3.14159 * ( 5 ** 2 - 3 ** 2)
And you will see the following output: >>> 3.14159 * ( 5 ** 2 - 3 ** 2) 50.26544
Fin.
1.4 Explanation
Any computer program is composed of statements. The above is an example of a mathematical expression in Python. In Excel, you would write the same computation as =3.14159*(POWER(5,2) - POWER(3,2)).
So how do you go about interpreting the Python statement? Let’s start at the top. Because you are typing this statement at the ">>>" prompt, you are working in "interactive mode", much like a calculator. If you type a valid mathematical expression, the computed output will appear on a line by itself. Try typing the following sequence of python mathematical expressions to see how Python can operate like a calculator.
18 2 + 4 3 - 9 9 * 8 15 / 3
The above statements involve the basic mathematical operators addition (+), subtraction (-), multiplication (*), and division (/) and exponentiation (**).
These statements are also composed of literals, such as 2 and 4, which represent the integer values for 2 and 4, respectively. Now what about the computed value of the following statement?
12 * 9 + 7 * 2
If you think about it for a moment, it could either be (12*9+7)*2=230 or 12*(9+7)*2=384 or (12*9)+(7*2)=122.
What answer do you get when you type in the statement? Can you explain why Python computes the result as it did?
An interpreter is a program that simulates a computer that understands a high-level language (such as Python).
All Python programs you write in this class are executed by a Python interpreter.
On a final note, the above statements had differing amounts of whitespace. In these one-line statements, you could have used spaces between the integer literals and the operators, but it is truly optional. Thus, 2*4 is the same as 2 * 4.
We shall see later in this course that whitespace indentation plays an important role in Python programs that contain more than a single statement.
Programming also develops valuable problem-solving skills, especially the ability to analyze complex systems by reducing them to interactions of understandable subsystems (p3).
1.5 Print statement
Now that you have used Python as a calculator, it is time to introduce two python functions. You can tell you are invoking a function because of the parentheses that you must use.
You can also use exit()
I know this doesn’t seem like much, but this function exits the python interpreter, much like turning off a calculator. To execute a function, you must know its name – and there are lots of them! We will introduce a number of useful functions over the first few days of this course.
One very useful function is print which allows you to print values to the screen. Here are some examples using print.
Note how the parentheses always have to balance from left to right, including nesting
When you run Python IDLE as a calculator, the value of a computation is immediately printed to the screen, so you don’t need to invoke the print function. When you write programs, as you will begin to do so tomorrow, you will need to use this print function.
1.6 Debugging Challenge
Most of your time in this course will be spent on debugging, or fixing, programs that you are writing. As you write more and more programs, you will develop this skill. Each lecture will have a debug challenge to test your knowledge of the material.
There are twelve different lights that can each be individually turned on and off. How many unique configurations of on/off patterns are possible?
You are reviewing the following answer.
12 * 11 * 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2
Does the above compute the correct answer? If not, what is the appropriate computation?
1.7 Skills
The entire course is focused on learning specific programming skills
Facts – Demonstrated through stating definitions, giving examples, and using constructs properly in practice. Validated by quizzes and exams.
Skills – Demonstrated through writing programs and their associated artifacts. Validated by homeworks and exams.
Abilities – Demonstrated in decisions reflected in code and explanations/justifications of these decisions. Validated by homeworks and quizzes.
You will have multiple opportunities to demonstrate your facts, skills and abilities.
In this lecture, you exercised the following skills:
IO-1: Know how to print information to the console window
DS-2. Know basic mathematical operators {-, +, *, /, **}
PS-11. Understand operator precedence and use of parentheses for clarity
1.8 Self Assessment
To make sure you know the material covered in this lecture, consider the following question, which could appear on a quiz or an exam:
Compute the area of a cylinder with height=2 meters and has radius=4 meters.
You should be able to write the Python program to compute the correct answer. Each lecture will close with a self assessment, and I expect you to complete these. Should you have problems or questions after attempting these self assessments, be sure to ask for help!
Once you get Python installed on your computer, you should immediately run the IDLE editor and try to write the Python code for this problem.
1.9 Three Things To Do Today!
Complete the pre-class Survey in my.wpi.edu. Find it in Course Materials | Pre-class Assessment
Install Python via enthought (need help? See video in my.wpi.edu)
Get started on forming project-partner pairs. All confirmed partner pairs should be emailed to cs110x-staff. If you would like to try to find a partner, you can use the discussion board "Programming Partner Connection". Partners will be finalized by Jan 21 2014.
Obtain access to online Zyante Python Text Book. See Announcement in my.wpi.edu web site
Get started on your individual homework HW1 (Assignment Rubric Template ) due on Jan 21 2014 (OK, so that’s five...)
1.10 Version : 2014/01/22
(c) 2014, George Heineman