Introduction
Have you ever thought about learning to program? Maybe you've thought about how it would be nice to know how. Maybe you've thought it would be nice to make websites that are more than just a simple page with a few graphics, but actually have functionality, like a custom blog, a forum, or even a social network! What about making a game or an application that you think would be useful? It's a lot easier than you might think. It just takes the right teacher, or the dedication to look through the resources, practice programming, and persevere through frustration and the many bugs you will encounter. A lot of programming is actually just research, and either implementing what someone tells you, or copying techniques and bringing them together to make them your own!
I'm not gonna claim to be the right teacher for you. Many people learn in their own ways. What I am going to do is try to do my best to introduce you to a programming language, Python, and hopefully make it easy to follow, as well as informative. I have hopes that after reading this series of posts, and following along, you'll be able to write useful programs of your own, perhaps with a little extra research.
So, why Python? Well, Python is a very easy language to learn. It's also very powerful. It is one of the most common programming languages used today. All this means that there are a lot of modules that have been built to make programming in Python even easier. It's also pretty fast, as well as being quicker to build programs in. This means that you can learn Python, be programming faster, and get programs done that are very useful, quickly.
If you already know how to program, then this post, and the series, might go a little slow for you. I hope you still enjoy it however. If you already know Python, you might want to skip this one, and stick around for future posts to learn about Flask and other programming tasks. If you do decide to read through though, I'd love your input, and your upvote if you think I did a good job.
What this series aims to do:
- Provide a basic introduction to the Python programming language.
- Introduce you to how to make web apps and website back-ends using Python and Flask.
- Hopefully use code that will run in either Python 2.7 or 3.x.
What this series will not do:
- Teach you anywhere near everything you need to know to program. You will likely need to do extensive research after you read this. Programming takes a lot of research. Almost every program you write requires you to research new modules and techniques, or look up things that you've forgotten, or aren't sure about.
- Teach you how to install Python. If you have a *nix based OS like Linux or Mac OS X, then you probably already have Python installed. If you don't, I'm sorry. You should probably install Linux. :P Or just google how to install Python. It's a little bit of a pain on Windows, but you can do it.
- Teach you how to make Android or iOS apps. While you can technically use Python to make Android apps, I would highly suggest that you use languages with greater adoption on those platforms.
- Teach you to be a 1337 haxor. Programming is not hacking. If you are interested in cyber security though, programming is a useful tool to have in your belt. Python in particular is used often in InfoSec, and can be highly useful. I do not plan on covering this in this particular series, but I may cover topics useful to sys admins and infosec in the future.
The Print Function: Hello World
Almost every tutorial on almost every programming language starts with an introduction using the now famous "Hello World!" This is meant to show you the bare basics of the language. It also introduces you to the "standard output". The standard output is usually set to print to the terminal. In web development, it's also often used to print to the browser.
#!/usr/bin/env python
print("Hello World!")
If you don't have any prior experience with programming languages, this might look a little foreign to you. If you have programmed before, even in another language, this probably looks all too familiar.
Lets break it down.
#!/usr/bin/env python
This is what is known as a shebang, or hash-bang, among other names. This tells *nix based operating systems to send the code to an interpreter.
If you have multiple versions of Python installed, your interpreter may default to Python 2.7, and you may have to specify Python 3 for some programs.
#!/usr/bin/env python3
Specifying Python 3 isn't required for the programs covered in this post though, and I'll try my best to ensure every program in this series runs on both Python 2.7 and Python 3.x. Specifically I'm testing them with Python 2.7.13 and Python 3.5.3, and maybe Python 3.6.1 if I feel like it, for the more advanced stuff later on.
print("Hello World!")
This is the part that does the actual dirty work. This passes the string "Hello World!" to the print function. The print function then sends it to standard out, which is usually the terminal, along with a newline character. The newline character is what is put in text documents when you press the return key.
Functions are sections of code, that are usually named, so you can call them from elsewhere in your code. Functions are often use for commonly used tasks, or occasionally for complex code that you want to keep separate from your main code. Any time you do something more than once, it's good to put it into a function.
You can read more about the built in function in Python at docs.python.org.
Note: You don't need to use the parenthesis in version earlier than Python 3, because it used to be a statement rather than a function. It's good practice to do so now that it is though, so it works in both Python 2.7 and 3. Adding parenthesis to print commands is likely one of the first things you'll do when modifying code to be able to run on Python 3.
Running Your Code
The Python Interpreter
You can run the program in a few different ways. The first way I will show you is the Python interpreter. The Python interpreter is extremely useful. I only wish I had been introduced to it when I first started using Python. I likely would have learned a lot faster, and gotten more used to it.
Simply type in "python" in the terminal to start the python interpreter.
$ python
Python 3.5.3
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
It should look something like the above.
In the Python interpreter you don't have to use the shebang. You simply type the code directly in, and it instantly runs it.
>>> print("Hello World!")
Hello World!
When you're done, you can use the quit of exit functions to exit the interpreter.
>>> quit()
OR
>>> exit()
The Command Line
Running Python programs directly from the command line can be useful occasionally. It may come particularly in handy for sys admins and users that use the Linux terminal a lot.
$ python -c 'print ("Hello World!")'
Hello World!
Note: This probably doesn't work on Windows.
Calling the Python Interpreter with the File
Likely the most common way to run Python scripts is to save them to a file, and then run that file. One of the ways to do that is to feed it into the Python interpreter.
$ python helloworld.py
Hello World!
This tells Python to execute the script "helloworld.py". You can then see the output directly after.
Executing the Script Directly
There's one last thing you need to do though to be able to just double click to run your script, or run it by typing it's name into the terminal. You need to give it executable permissions! On a *nix based system, such as Linux or Mac OS X you can issue the following command.
chmod +x helloworld.py
This just tells the operating system that you're giving it permission to execute the file. This is a security measure so you don't execute something you don't want to.
If you're putting the file on a webserver, the much more common method is to do the following.
chmod 755 helloworld.py
This gives your account read, write, and execute permissions (the 7), the group your account belongs to read and execute permissions (the second 5), and the entire server read and execute permissions (the last 5).
You can read the manual for chmod on *nix based systems by issuing the following.
man chmod
Or just read it on linux.die.net.
In most operating systems you can also right click it, go to get info, and add executable permissions in one of the tabs of the window that pops up.

Image copyright Michael McConville
Used under Creative Commons Attribution 4.0 International
(source)
I'm gonna stop here for now. I had planned on covering a lot more in my initial post on programming, but I feel as though certain subjects, such as variables and loops, deserve their own posts. I have the majority of the Variables post already written though, so expect that out soon.
Hope you enjoyed this rather simple introduction. We're only just getting started.