2. What is a Programming Language?¶
What is a programming language? This wouldn’t be much of a programming course if you left without even knowing what a programming language was! So let’s get that out of the way.
Computers have a chip called the Central Processing Unit (CPU) that functions as the main “brain” of the computer. For example, right now you might have an Intel i7 or an AMD-FX CPU in your computer.
The CPU gets its instructions by reading a set of numbers. For example the number “04” might be an instruction to add two other numbers together.
Everything stored on the computer is in the form of numbers. Some numbers computers store are for data (text, photos, movies), and some are computer instructions.
2.1. Machine Code¶
In the early days of computing, programmers punched in numbers that represented commands for the CPU. Then the programmers punched in the data.
We call these numbers that are instructions machine code. Note that not all numbers on the computer are machine code (they might just be data), but all machine code is made of numbers. Machine code is also called a First Generation Language (1GL).
Below is an image of the Altair 8800, the first personal computer that regular people could buy. Notice that it is missing a monitor and a keyboard! The first computers loaded the instructions by flipping switches. A pattern of switches represented a machine instruction. So you’d flip lots of switches, then flip the “Run” switch. And the lights would blink.
While this may not seem very useful (and quite frankly, it wasn’t) it was very popular in the hobbyist community. Those people saw the potential.
Computers still run on machine code. You can still code by punching in numbers if you want. But you’d be crazy because hand-coding these numbers is so tedious. There’s something better. Assembly Language.
2.2. Assembly Language¶
Don’t worry! We aren’t coding in assembly language for this class.
Assembly language allows a programmer to edit a file and type in codes like
stands for “Load Accumulator Immediate.” The programmer types these commands
into a source file. We call the commands source code. The computer
can’t run the source code as-is. The programmer runs a compiler that
simply translates the computer commands like
LDA into the corresponding
number of the machine language instruction.
After I compile the source code into compiled code, I can run the compiled code. I can give the compiled code to someone else and they can run it. They do not need the source code or the compiler.
Assembly language is an improvement over machine language. But it isn’t that much of an improvement. Why? Assembly language instructions are very low-level. There are no commands like “draw a building here.” Or even “print hi.” There are only mind-numbingly simple commands that move bits from one spot to another, add them, and shift them.
2.3. Third Generation Languages¶
Third Generation Languages (3GL) started with Grace Hopper creating the language COBOL. There are many, many different third generation languages now. These languages often specialize at certain tasks. For example, the language C is great at creating small, fast programs that can run on minimal hardware. PHP is an easy-to-use language that can build websites.
Third generation languages usually fall into one of three categories.
- Compiled: The computer takes the original source code, and uses a compiler to translate it to machine code. The user then run the machine code. The original source code is not needed to run the program. “C” is an example of a language that works this way. So is the 2GL assembly language we just talked about.
- Interpreted: The computer looks at the source code and translates/runs it line-by-line. The compile step is not needed, but the user needs both the source code and an interpreter to run the program. Python is an example of an interpreted language.
- Runtime Environment: Languages such as Java and C# take source code, and compile the source code to a machine language. But not the language of your actual machine, they compile to a virtual machine. This is a separate program that acts as a layer between the real machine and the compiled code. This allows for better security, portability, and memory management.
Working with a compiled language is like taking a book in Spanish and translating it to English. You no longer need the Spanish book, and you don’t need the translator. However, if you want to edit or change the book you have to re-translate everything.
Working with an interpreted language is like working with a interpreter. You can communicate back and forth with a person that knows both English and Spanish. You need the original Spanish, the English, and the interpreter. It is easier to make ad-hoc changes and carry out a dialog. Interpreters often help prevent computers from running commands that will cause major crashes or common security issues. Kind of like having a human interpreter that says, “You don’t really want to say that.”
Using a runtime environment is hard to explain in human terms. It is a hybrid of the two system. You need source code. You need a compiler. Instead of the compiler making machine code, it makes for for a virtual machine.
2.4. What is so great about Python?¶
Python is a great language to start programming in. Python is a Top-5 language in popularity according to the TIOBE Index. While may be less popular than Java, it is easier to read and learn. Less work is required to do graphics. And everything you learn in Python you can also apply when you learn C# or Java.
Python a great language for people interested in doing data processing and automating boring things.
2.5. Python 2.7 vs. Python 3.6¶
There are two main versions of Python. When Python moved to version 3, there were changes that didn’t work with all the currently written Python 2 programs. So both Python 2 and Python 3 were being developed simultaneously. Some people don’t want to move to Python 3 at all.
We use Python 3. Why are you going to care?
- If you search up examples you will find both Python 2 and Python 3 examples.
- Systems such as the Mac and Linux have Python 2 installed by default.
If you see a Python example on the web that has a print statement that looks like:
# A "print" statement with Python Version 2.x print "Hi"
# A "print" statement with Python Version 3.x print("Hi")
Then you have a Python 2 example and it won’t run with what we install and use in this class.
In the case of the Mac and Linux, it will be important to use Python 3 and not Python 2. Since Python 2 is installed by default, it can be a bit of a hassle to make sure they use Python 3.