Basic Programming Concepts
OER name : Basic Programming Concepts
Category: Computer Science
Objective: The main objective of this course is to teach basic programming skills to students who have never learned programming before. Python was used as the language to teach because it is considered an excellent language for teaching programming to beginners.
Target group: A/L students
Licensing information: CC BY
Readers are free to copy and distribute the text; they are also free to modify it, which allows them to adapt the book to different needs, and to help develop new material.
Copyright notice: This course is based on How to Think Like a Computer Scientist: Learning with Python by Allen Downey et al. (2002), which is avaible under GNU Free Documentation License
The goal of course is to teach you to think like a computer scientist. This way of thinking combines some of the best features of mathematics, engineering, and natural science. Like mathematicians, computer scientists use formal languages to denote ideas (specifically computations). Like engineers, they design things, assembling components into systems and evaluating tradeoffs among alternatives. Like scientists, they observe the behavior of complex systems, form hypotheses, and test predictions.
The single most important skill for a computer scientist is problem solving. Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately. As it turns out, the process of learning to program is an excellent opportunity to practice problem-solving skills. That's why this chapter is called, "The way of the program."
On one level, you will be learning to program, a useful skill by itself. On another level, you will use programming as a means to an end. As we go along, that end will become clearer.
Lession 01. What is a program?
A program is a sequence of instructions that specifies how to perform a computation. The computation might be something mathematical, such as solving a system of equations or finding the roots of a polynomial, but it can also be a symbolic computation, such as searching and replacing text in a document or (strangely enough) compiling a program.
The details look different in different languages, but a few basic instructions appear in just about every language:
Get data from the keyboard, a file, or some other device.
Display data on the screen or send data to a file or other device.
Perform basic mathematical operations like addition and multiplication.
Check for certain conditions and execute the appropriate sequence of statements.
Perform some action repeatedly, usually with some variation.
Believe it or not, that's pretty much all there is to it. Every program you've ever used, no matter how complicated, is made up of instructions that look more or less like these. Thus, we can describe programming as the process of breaking a large, complex task into smaller and smaller subtasks until the subtasks are simple enough to be performed with one of these basic instructions.
That may be a little vague, but we will come back to this topic later when we talk about algorithms.
Before next lesson: Installing Python
In order to go through the following lessons, you will need to install Python in your computer. The video below shows you how to do this.
The first program
Traditionally, the first program written in a new language is called "Hello, World!" because all it does is display the words, "Hello, World!" In Python, it looks like this:
print "Hello, World!"
This is an example of a print statement, which doesn't actually print anything on paper. It displays a value on the screen. In this case, the result is the words
The quotation marks in the program mark the beginning and end of the value; they don't appear in the result.
Some people judge the quality of a programming language by the simplicity of the "Hello, World!" program. By this standard, Python does about as well as possible.
Lession 02: Variables, expressions and statements
One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value.
The assignment statement creates new variables and gives them values:
>>> message = "What's up, Doc?" >>> n = 17 >>> pi = 3.14159
This example makes three assignments. The first assigns the string "What's up, Doc?" to a new variable named message. The second gives the integer 17 to n, and the third gives the floating-point number 3.14159 to pi.
An expression is a combination of values, variables, and operators. If you type an expression on the command line, the interpreter evaluates it and displays the result:
>>> 1 + 1 2
Although expressions contain values, variables, and operators, not every expression contains all of these elements. A value all by itself is considered an expression, and so is a variable.
>>> 17 17 >>> x 2
Confusingly, evaluating an expression is not quite the same thing as printing a value.
>>> message = 'Hello, World!' >>> message 'Hello, World!' >>> print message Hello, World!
When the Python interpreter displays the value of an expression, it uses the same format you would use to enter a value. In the case of strings, that means that it includes the quotation marks. But if you use a print statement, Python displays the contents of the string without the quotation marks.
In a script, an expression all by itself is a legal statement, but it doesn't do anything. The script
17 3.2 'Hello, World!' 1 + 1
produces no output at all. How would you change the script to display the values of these four expressions?
A statement is an instruction that the Python interpreter can execute. We have seen two kinds of statements: print and assignment.
When you type a statement on the command line, Python executes it and displays the result, if there is one. The result of a print statement is a value. Assignment statements don't produce a result.
A script usually contains a sequence of statements. If there is more than one statement, the results appear one at a time as the statements execute.
For example, the script
print 1 x = 2 print x
produces the output
Again, the assignment statement produces no output.
Lession 03. Functions
In mathematics, you have probably seen functions like sin and log, and you have learned to evaluate expressions like sin(pi/2) and log(1/x). First, you evaluate the expression in parentheses (the argument). For example, pi/2 is approximately 1.571, and 1/x is 0.1 (if x happens to be 10.0).
Then, you evaluate the function itself, either by looking it up in a table or by performing various computations. The sin of 1.571 is 1, and the log of 0.1 is -1 (assuming that log indicates the logarithm base 10).
This process can be applied repeatedly to evaluate more complicated expressions like log(1/sin(pi/2)). First, you evaluate the argument of the innermost function, then evaluate the function, and so on.
Python has a math module that provides most of the familiar mathematical functions. A module is a file that contains a collection of related functions grouped together.
Before we can use the functions from a module, we have to import them:
>>> import math
To call one of the functions, we have to specify the name of the module and the name of the function, separated by a dot, also known as a period. This format is called dot notation.
>>> decibel = math.log10 (17.0) >>> angle = 1.5 >>> height = math.sin(angle)
The first statement sets decibel to the logarithm of 17, base 10. There is also a function called log that takes logarithm base e.
The third statement finds the sine of the value of the variable angle. sin and the other trigonometric functions (cos, tan, etc.) take arguments in radians. To convert from degrees to radians, divide by 360 and multiply by 2*pi. For example, to find the sine of 45 degrees, first calculate the angle in radians and then take the sine:
>>> degrees = 45 >>> angle = degrees * 2 * math.pi / 360.0 >>> math.sin(angle) 0.707106781187
The constant pi is also part of the math module. If you know your geometry, you can check the previous result by comparing it to the square root of two divided by two:
>>> math.sqrt(2) / 2.0 0.707106781187