Python Tutorial: Learn Python in one Day

Learn to code Python in this Python coding tutorial in just one day! In this tutorial, you will get your hands dirty and learn the basic principles of Python. Let’s get started!

The Python Programming Language

The object oriented Python programming language was invented back in 1991 by Guido van Rossum and could therefore be considered as a Dutch programming language. Its core philosophy is summarized in PEP 20, the Zen of Python:

  • Beautiful is better than ugly
  • Explicit is better than implicit
  • Simple is better than complex
  • Complex is better than complicated
  • Readability counts

In my opinion, all of these statements still hold today and contribute to the success of Python. For beginners, it is not hard to learn the basic concepts of Python, since the low-level features of a programming language (such as resource allocation and garbage collection) happen under the hood.

Python Download

Before you can start programming in Python, you will need to download it. It is also possible to use an interactive Python interpreter and some exotic variants such as Jupyter, but we will just used the basic Python IDE which you can download from here. This tutorial is intended for Python 3, so make sure you choose the right Python version.

Now just start IDLE. You will now see something similar to the following screen:

Believe me, you will use this Python shell only in your first few days of writing Python code. After a while, you definitely want to use a different IDE. Here you will find an overview of different Python IDEs.

Your first ‘Hello World’

We will start with a ‘Hello World’ application. A ‘Hello World’ application is the most basic application written in a programming language which show the famous words ‘Hello World’. For this, you will need to know two things. ‘Hello World’ is called a string (which is a data type). A string is a sequence of characters. Other data types are lists, integers (whole numbers) and floats. There are many different data types, but we will start with just the string. We need a way to print the string to the screen. This is done by using the ‘print’ function. A function takes in data and does something to it. Here, the print function simply prints the input data to your screen. This results into the following code:

You can enter it in the Python shell and press the enter key to execute the code:

Well done! You executed your first Python code.

Scripts

When I started programming in Python, I was confused how I could ‘save’ my code such that I could rerun it later. The Python shell is just a way such that you can enter Python code in an interactive way. You can make a Python script (ending on the .py) extension just from the Python shell by going to “File” and then “New File”. You can then run your script by choosing “Run” and then “Run Module”.

Functions

Lets start a new Python script. Now we will create a function which takes in two numbers and computes its sum. A function is defined by using the “def” keyword. Lets define this function:

1
2
3
def add_numbers(a, b):
 # Adds up two numbers a and b and returns the result a + b
 return a + b

There are a few remarks to make here. First, after the function definition line (def add_numbers(a, b):) a newline appears followed by four spaces. The spaces indicate that these lines belong to the line without the spaces. So you can see clearly that “return a + b” belongs to “def add_numbers(a, b):”. There is a comment on the second line. This comment starts with a hash symbol “#” and is not executed by the Python programming language. It is important that you add comments to your code, especially when you are working together with other human beings or when you’d like to read back your code in a while. Make sure to make a habit out of this!

Now we will try out our function by a few numbers:

1
2
3
4
5
6
def add_numbers(a, b):
 # Adds up two numbers a and b and returns the result a + b
 return a + b

# Add up 3 and 4 and print the result
print(add_numbers(3, 4))

Now run your script (“Run” and then “Run Module”). You will be probably asked to give your script a name. Name it anything you like by just using lower case characters and underscores. I named it “my_script”. Now you will hopefully see 7 in the Python shell, well done! If you have any questions, feel free to ask below.

Conclusion (TL;DR)

In this Python programming tutorial you learned how to print out results and how to create a function. In the next blog post on this topic, we will gradually build up your Python knowledge. If you are interested in more advanced topics, definitely read this blog post on Python list comprehensions. If you would really like to read more, I recommend one of the following books:

If you have any questions, feel free to ask them below!