Python programming beginner tutorial understanding the basics

Some important things to understand about the python programming language

Unlike other programming languages python is one of the few which determines open and closing programming blocks based on indentation of code. For example the following code will not run :

Example of python indentation error
Example of python indentation error

The correct way to do this is to introduce a tab in the code which runs under a construct.

For example:

Python beginners tutorial showing how we indent code in python
Python beginners tutorial showing how we indent code in python

Making the programming experience easier

In a previous tutorial we made use of the python command line interpreter. Which can become quite tedious if you have large blocks of python code. For this tutorial and going forward we will start making use of some nice text editors to format our code as we write it. Also they provide nice highlighting of your code which just makes things a lot neater.

Recommended code editors

We would like to recommend the following code editors.

  • Sublime Text 2 or Sublime Text 3
  • Atom code editor
  • Notepad++

Or you could make use of some advanced IDE’s. However these can have quite a bit of a learning curve and is not recommended for someone just learning. Also the code completion portion they provide defeats the purpose of learning the actual python programming language as the IDE will spoon feed you.

Lets write a small python program to show off some python constructs

For this python program we want to do a few basic things. Such as read in command line input then display different messages based on the input. What we will be teaching is the conditional statements which python provides so that we can build conditional code snippets.

name = input("Please enter your name ")
print("Your name is : ", name)
if name == 'jack':
    print('Hi you are my creator')
    print("Hi Welcome to I'm Alley")

Add your code to a filename of your choice saved with an extension of .py.

Let’s run our program

To run your python program type python in your command line and hit enter. Try the following two scenarios for your name. Where your name is ‘jack’ and where your name is otherwise.

The python code explained

First of all we create a variable called name. Hang on though what is a variable. A variable is a container where you can store information in your computer’s memory. More specifically a variable will point to a address in your memory where you information is stored.

We then say that the name is equal to the input. The input function basically will wait for user input and a enter key. Our program will then store what we typed in, in the name variable. The next block of code basically prints “Your name is:” and then what ever you typed in on the command line when you python program asked you.  We then have what we call a if else construct. The if will check if your name is equal to jack, this is done with a double =. If you are jack we will print that you are the creator. Then the else will run for any scenario where you are not jack and then printing : Hi Welcome to I’m Alley.


Leave a Reply

Your email address will not be published. Required fields are marked *