Overview of Programming

From OpenCircuits
Jump to navigation Jump to search

This overview of programming applies to Python and to pretty much all current programming languages. It does not emphasize what is different about Python, but what it shares with most others. This is presented to those who are not familiar with programming with the hope that an overview will help make the process of learning Python easier. There are some fine technical distinctions that we are just going to gloss over, you can learn them and the details involved in this page later. We often refer to the lines in a program ( most programming environment will number the lines to help you refer to them ) or we will refer to the parts of a program as statements.

A Recipe[edit]

A program is a recipe in that it is a set of directions in order telling the computer ( like the cook ) what to do. Generally it is all text, the directions are not graphic. The text is very exact, almost everything matters no extra punctuation, no misspellings, no "you know what I mean". The computer will not do what you meant unless you say exactly what you meant. ( Some white space, spaces, tabs, blank line, and comments are exceptions to this )An IDE may make it harder to make mistakes. Error messages sometimes find your mistakes, sometimes just tell you something is wrong, but can be cryptic about what.


A variable is a name that refers to some item of data, a name, a number, a picture.... It is called a variable because what item of data it refers to can be changed by statements in the program. A bit like a variable is a literal, 5 for example is a literal. It cannot be changed.


Operators take one or more values and change it. Mathematical operators may be easiest to understand, if a and b are numbers the operator + can be used to add them together. If a variable represents an image an operator might change the appearance of it. Operators are fairly closely related to functions which also take "things" and do something to them. You could have a function called "add()" and numbers could be added by writing add( a, b ).


In your first Python program you use a function: print(). A function takes something, like "Hello World" and does something with it, print() prints what it is given. Python has hundreds of functions, and you can write your own. Functions are used to avoid writing the same, or almost the same code over and over, instead you write a bunch of code, give it a name, the function's name, and then "call it by name". The terms Functions, Methods, and Subroutines all are examples of this idea.


Programming frequently involves working through list: for example if the program is sending emails, the program may work through a list of email addresses. This is closely related to the idea of looping. Looping is a little like driving around a rotary or traffic circle. Here the program goes through the same instructions over and over a loop. Typically looping is used to work through a list. Each time through the loop a different element in the loop is processed. In Python there are several different data structures that are like lists. In many languages the data structure that works like a list is called an array. Looping is done with the for, while, or until constructs.


Sometimes a program will want to do one thing, other times another. This is called branching, the program follows one branch or another. In Python this is typically done with an "if" statement. If one thing is true the program does "thing A" otherwise it does "thing B".


Often programs may want some information from you like an email address, and will then send the email to someone. Taking the information from you is input, sending the email is output. The function print() is probably the simplest method of output. Other methods involve websites, files, sockets.....

Import External Code[edit]

You often want to add code others have written to your project, why write stuff over if others have already written it and will let you use the code. Several different terms are used around this idea, packages, libraries, modules.... ( some with a bit of tecnical difference ). Usually using this code involves 2 steps. Adding the code to your computing environment ( although it may have come with the code in place making this step unnecessary ) and a line in your code to use the "library". In Python this is one or more of the import statements.