Introduction to Programming

So, you wanna be a programmer, eh?

The main challenge of being a programmer is you need to be (or be able to learn how to be) ruthlessly logical. You must be able to mercilessly self-evaluate and criticize yourself, be open to the idea of mining your own works for faults, contradictions and stupid mistakes.

You program a computer by writing a 'program'. This is typically a very carefully written and revised text file telling your computer what to do. Programs can be written in different 'languages', which are just different ways of telling the computer to do something. Computers are fundamentally stupid; they'll do exactly what you tell them.

You also need familiarity with basic mathematics (again, can be learned). More is better, but Algebra is a fair minimum. I rarely need more than Calculus, but I find Trigonometry and Geometry to be very helpful all the time.

Often neglected, it is also very important to be creative. Programming is about solving problems, and if you don't like doing that, you're going to fail. Most of the problems you'll run into (especially if you get good) are problems no one has ever encountered before, and you are the one that needs to solve them with little to no help from outside. So, to be a good programmer, you must use logic and reason in new ways.


Variables

A key concept in most programming languages is the notion of a 'variable'. A variable can vary.

Code will be presented in the language Python, because it's very easy to read and understand intuitively. A good way to learn is to play with a 'shell', which some languages provide to let you make code interactively. Below is a Python Shell session. I type a command after the >>> and the computer tells me its output on the lines starting with a blank. Sometimes the computer doesn't have an output, in which case, it just gives me another >>> to type after.

>>> x = 5
>>> print(x)
5
>>> print(x*x*x)
125
>>> y = 16.4
>>> print( y / 7.6 )
2.1578947368421053
>>>

In the example above, x and y were variables. The print is a command that prints out the value of whatever you give to it (inside the parentheses). The +, -, * and / all do (mostly) what you'd expect, so all this should be fairly straightforward. A key concept is that I can change values of a variable:

>>> c = 4
>>> print(c)
4
>>> c = 7
>>> print(c)
7
>>> c = c + 200
>>> print(c)
207
>>>

Notice the final assignment statement (c = c + 200). This basically tells the computer to add 200 to c and then put that value back into c. It is very important to be able to understand and recognize forms like this.


Arrays

Another key concept is that of an array. For now, you can think of an array as a list of numbers or letters. In Python, you define an array like:

>>> my_array = [3,1,4,1,5,9,2,6,5]

You can access 'elements' of an array by 'indexing' them with [ and ]. For reasons covered in the planned C 101 tutorial, array indexing starts at 0. You can also set elements of an array with =, as you might expect. So:

>>> my_array = [3,1,4,1,5,9,2,6,5]
>>> print( my_array[0] )
3
>>> print( my_array[5] )
9
>>> my_array[2] = 487
>>> print( my_array )
[3, 1, 487, 1, 5, 9, 2, 6, 5]
>>>

Strings

Another key concept is that of so-called 'strings'. A string is a sequence of characters, kindof like an array except you aren't allowed to modify it. In Python, one way to make a string is to put quotes around a piece of text (this is called a 'literal'). A string is otherwise just like any other variable:

>>> text = "Hello there!  My name is Agatha!"
>>> print(text)
Hello there!  My name is Agatha!
>>>

Notice that most mathematical operations on strings don't make sense. Like division:

>>> a = "hi"
>>> print( a / 6 )
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    print( a / 6 )
TypeError: unsupported operand type(s) for /: 'str' and 'int'
>>>

However, adding two strings is defined. It puts them together and returns a new string:

>>> h = "hello"
>>> w = "world"
>>> print( h + w )
helloworld
>>>

Control Flow

Another key concept in most languages is 'control flow', which basically means jumping around in the source code. The most basic question you can ask is the if-else block:

>>> var = 6
>>> if var == 6:
...     print( "var is 6." )
... else:
...     print( "var is something else!" )
...
...
var is 6.
>>>

There are two 'blocks' above, corresponding to what to do in the two different cases. If var is 6, then the first print statement will execute, and the second one will not. Otherwise (else) the second print statement will execute, and the first one will not.

Notice that I have to check for equality with the == operator. This syntax is required because if I had used =, it would try to set the first thing equal to the second instead—and we want to compare, not set.

There are other comparison operators that can be used, too:

>>> a = 1
>>> b = 2
>>> c = 3
>>> d = 2
>>> print( a == b )
False
>>> print( b == c )
False
>>> print( a < c )
True
>>> print( b == d )
True
>>> print( b is d )
True
>>> print( a != b )
True
>>> print( a is not b )
True
>>>

The only one that's a little tricky is !=, which can be read as "not equal to".

The final key element of programming is the 'loop'. Loops let you do things over and over again without writing them so many times. For example, suppose I want to print the numbers 1 through 5:

>>> print(1)
1
>>> print(2)
2
>>> print(3)
3
>>> print(4)
4
>>> print(5)
5
>>>

This sucks. What if I wanted to do the numbers 1 through 100? It would take forever, and there's a good chance I'd make a mistake. A better way to do this sort of thing is with a loop:

>>> x = 1
>>> while x < 6:
...     print(x)
...     x = x + 1
...
...
1
2
3
4
5
>>>

This is called a 'while loop'. It does whatever is beneath it while its 'condition' is true. When it starts, x is 1. It then asks if 1 < 6, which it is, so it prints x and then adds 1 to it. Now, x is 2. So, it asks if 2 < 6, which it is, so it prints x and then adds 1 to it. This continues until x is 5. Because 5 < 6, we print 5 and then add 1 to x, making it 6. The next time around, though, 6 is not < 6, so the loops 'terminates'. We have now printed out the numbers we want!


Misc. and Conclusion

A final helpful bit: If you find yourself wanting to make notes to yourself in your program, you can make a 'comment', which will be completely ignored by the computer. In Python, any line starting with # is a comment:

>>> #This line does absolutely nothing!
>>>

I didn't cover a few more basic forms (like elif, for loops, functions, recursion, or classes and inheritance). These are also very important tools. However, for now, you should have a pretty solid understanding of how to make some basic programs. If I make a subsequent tutorial, it may cover these topics.

Remember, the best way to improve is to practice! Get out there and code!