This article is the first part of a series of articles/guides aimed at helping non-programmers learn the Python programming language. A few of my friends who read (or at least try to read) my blog requested I do something for non-programmers to learn how to program small things. To those who asked for it – this is for you!
Please be aware that this first article will involve a lot of reading – but the future ones will have less reading and a lot more coding. That’s not to say there won’t be any reading in the upcoming articles – I will be going over the theory of each topic but it will be more broken up with examples and code to try out yourself. This should hopefully give the upcoming guides a good pace that’s easy to follow.
When choosing a programming language for this beginners series I decided to go with Python as the programming language of choice. There are a lot of arguments to be made from a experienced programmer’s perspective:
- It’s Object-Oriented and Functional
- It has Dynamic Typing
- It uses Automatic Memory Management
- It provides commonly used Data Structures such as lists, dictionaries and strings
- It has powerful Standard Operators for use on those Data Structures
The only thing is when you’re just starting out programming you will have no idea what anything stated above means. Trust me – that’s all good stuff when it comes to a programming language you want to teach non-programmers – but what’s really important are the arguments for a non-programmer to use Python. Things like:
- It’s FREE
- It’s Portable – this means that you use it in many different computing environments such as Windows, Mac OS X and Linux
- It’s relatively Easy to Use – all you need to do is type your program and run it, there are no extra steps like in Java or C++
- It’s relatively Easy to Learn – the core of Python is quite simple to learn for all experience levels (if you have some programming knowledge – you’ll be making small-scale programs in no time)
My definitive argument for using Python over Java (Java was the other option I was considering due to it’s immense popularity) is the fact that most people simply find Python easier to use than Java. Python code is simpler than the equivalent code in Java and is often much smaller as well. For people just starting to program – this could be the difference between really getting into programming or just giving up on it. I want everyone who reads my articles about programming to have fun with programming – not be frustrated by it. In the end I think that Python is just more fun!
The next thing I wanted to glance over is the general theory behind Python and its concepts. I know it might look like an imposing wall of text, so feel free to skip this section if you aren’t interested in the theory and just want to get to coding. This section was written for those who want to know a little more about why things are the way they are behind the scenes and some of the concepts that surround programming.
Theory – What is Python?
To start I want to give a very basic definition of what Python is. Python is a high-level programming language used to automate tasks on your computer. Note that this definition is my own so you won’t find it in any textbook, but I really like it because it really expands to tell you what any programming language is. I feel this definition is easy to understand and doesn’t really require any extra explanation. If you do want to get technical, then the definition of a programming languages is a constructed language that allow a programmer to send instructions to a computer which the computer will then execute in order. These sets of instructions are what make up computer programs. There are many different languages to write these instructions in.
You may have noticed that I mentioned that Python is a high-level programming language in my definition above. Computer programming languages have a concept called abstraction which is how much the programming language hides extraneous details from you. A high-level programming language is one with a strong abstraction level. Remember (or learn right now) that everything inside of a computers is represented by 1’s and 0’s called binary code. Abstraction is how close you are to human language when writing your programming language. A high-level programming language is much closer to a human readable language (almost always English) than it is to binary code. It’s the ability to write something that is readable by programmers that is then converted into binary code for the computer to run. Of course we could write all code in binary, but this would be extremely hard for programmers to learn and remember – which is why we use programming languages.
Right now it might seem like high-level programming languages should be used for all programming. After all – if they are more powerful and easier to learn why not use them for everything? It’s important to remember that there is a cost to the strong abstraction in high-level programming languages. A lot of the time seemingly simple tasks in high-level languages use a lot of computing power because they are actually rather complex when they are converted to binary code. Even though they may only be a single command in your program – they are in fact many commands and this can take time. With the increase in the power of consumer computers high-level programming language performance issues are becoming less and less of an issue. Back in the day (I say this as if I was there – I was not) when computers were slower, high-level programming languages would be slower to run due to these complex operations. On today’s computers you need to write a very large program to notice these differences in speed. One more downside to high-level programming languages is that the abstraction takes away some control from the programmer. In low-level languages such as C you have control over way more aspects of the computer than you would in high-level languages. This allows you to do certain techniques that you couldn’t do in high-level programming languages because you simply don’t have access to the same things. This is why operating systems such as Linux are written in C rather than in a high level language.
Along with being a high-level language Python is also an interpreted programming language. An interpreted language is a programming language for which most of its implementations execute instructions directly, without previously compiling a program into machine-language instructions. Other programming languages such as Java, C++ and C need to be compiled in order to run. The compilation process is one where a program called the compiler converts your code into a runnable program. This needs to be run every time you make a change to your program and if you aren’t using an Integrated Development Environment (IDE) then you will have to know some command line commands to do this. You may remember from above that I stated Python was easier to use than other languages like Java. The fact it does not have to be compiled is one of the major reasons for this statement.
Python comes bundled with software called the interpreter. An interpreter is a kind of program that executes programs or code. The interpreter is like a translator between your code and computer (hence the name). The Python interpreter also comes as a runnable program called IDLE (at least on OS X) which allows the user to type in commands and then execute them. The Python interpreter can be used interactively, which makes it easy to experiment with features of the language, to write throw-away programs, or to test functions during program development.
Another great feature of Python mention above is the fact it’s portable. When a programming language is portable it means that it’s available on the major operating systems (Windows, OS X and UNIX-based). Kind of a given at for most programming languages now a days, but still worth mentioning to cover my bases.
Also just so you know, the Python programming language is named after the BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles. That’s all the theory I want to cover so I don’t end up writing my own book in this article. Let’s install Python and get started using the interpreter to run some basic commands.
The first thing you want to do is to make your way to the Python Website.
Once there, click on the “Download” link at the top of the page. This will bring you to the download page which will list a few different versions of Python you can download.
There will most likely be two different versions of Python available to download – a 3.X.X version and then a 2.X.X version. At the time of writing this tutorial they were 3.4.2 and 2.7.9. For this tutorial, download the 3.X.X version. There are still a lot of people who use the old version – but if you’re just learning it’s best to just learn the newest one instead of going backwards.
This website also has a section labelled “Documentation” that is extremely important. If you ever have any questions on what a certain function or command does – this should be the first place you go to read about it. This contains all the documentation regarding Python as well as guides, tutorials, suggested books and much more. Keep this in the back of your head and I keep it open when I’m programming so I can find what I need right away.
Note that I am doing most of this tutorial on my Mac but I did install Python on my Windows computer to take a look at what everything looked like since that’s what I assume most people will be using. When you download the installer just mash the next button until everything is installed. It open up a command prompt window during installation so don’t let that scare you. Once its installed click Finish and you are ready to rock.
You should now see a folder under your start menu called Python 3.4 (might be different version number depending on when you install) and it should contain IDLE, Python 3.4 (command line), Python 3.4 Docs Server, Python 3.4 Manuals and Uninstall Python 3.4. These will probably also contain either 32-bit or 64-bit for your system.
If you install on Mac open the installer package and then mash the continue button. You’ll have to type your password in once but that’s all you’ll really need to do. Once installed you should find a folder in your Applications called Python 3.4 which contains IDLE, Python Documentation.html, Python Launcher and Update Shell Profile.command.
Finally We Code
The first thing we want to do is test out the IDLE interpreter program. Both the Windows and Mac versions of Python come bundled with this program – so open it up and then you should see a screen like this:
It will probably look a little different on Windows and it has a little different text at the top – but everything will work the same. From this program we can start working with the Python interpreter. For example, if you type in a simple math equation like “2 + 2” and hit enter – the Python interpreter will run your line of code (and that is indeed a line of code) and give you the answer back.
There you have it – you have written exactly one line of Python code. Go celebrate! Using IDLE invokes what is called an interactive interpreter session of Python. This is not the only way you can do this however – you can also open up either your Run on Windows or your Terminal on OS X and then type “python”. This will then open up an interactive interpreter session inside of the Command Prompt (remember, you type “python” into the Run application which will then open a Command Prompt window with Python running) or Terminal respectively. You can see an example of what this looks like in my terminal below:
You can then type all of the same commands in that prompt as you would in IDLE. It’s your choice what you want to use – but since I use my terminal very frequently I do most of my work in the terminal rather than in IDLE. However, if you find that’s a little intimidating with the extra steps just keep running IDLE! It work’s perfectly fine.
Before closing out Part I here, I want to make sure you write the all mighty “Hello World!” program in Python. “Hello World!” is the first computer program you write in most languages when learning them for the first time. It has become a real staple and right of passage in the programming world so I feel it’s important to write it. Luckily in Python it’s only one line, so you can write it in IDLE or your Command Line:
This will output the phrase “Hello World!”. Definitely a simple program but a great first step!
In the next article I want to go over the different mathematical operations you can do as well as go over simple String operations. This is only the first part in a many-many part series of articles for Python (I don’t even actually know how many I want to do yet), so keep you eyes open for more!
As always thank you for reading and please share it around as much as you can! Please feel free to put any suggestions or ideas for future articles in the comments section below.