Writing your first Python program

5 min read ⋅ 52 views

If you’re new to programming, choosing your first language can be a daunting prospect. Every language has it’s pros and cons and many newcomers spend ages trying to figure out where to start.

They think things like, “I plan to work as an app developer so I better start with Java” or “I want to do web development so I need to look at PHP and JavaScript”. Now if you can be sure you will work in a specific industry for the rest of your life, then maybe you can get away with this approach. The reality though, is that this will never be the case. The technology industry as a whole moves faster than any other. If you want to be a successful programmer you need to go about it in the right way. You need to be willing to constantly expand your knowledge and expertise and one of the best places to start is with a language called Python.

Python was created in the late 1980’s by Guido Van Rossum at Centrum Wiskande & Informatica and it is the successor to the ABC language. The name ‘Python’ comes from “Monty Python’s High Flying Circus”, a 70’s BBC comedy. Guido was a huge fan of the show and decided to use the word “Python” as he wanted a name that was short and succinct.

Why is Python good for newcomers?

There are many reasons to start your programming journey with Python. The first being that it is considered the easiest programming language to learn.

Python has incredibly simple syntax. One of the problems a lot of newcomers have is focusing too much on the syntax of a language. Programming isn’t just learning the language, programming is problem solving. In order to solve problems with programming you need to have a good understanding of the concepts of programming. The languages are just tools. They are what you use to build the solution to the problem.

An engineer isn’t someone who can use a drill, an engineer is someone who builds solutions to a problem with that drill. The same applies with programming.

Python’s simple syntax allows beginners to focus on grasping the concepts of programming instead. This is far more beneficial as it means the transition to other languages will be a lot smoother for them. It also means they will be better programmers in general as they have focused their attention on the right things.

Below I have written a simple “Hello World” program in both Python and Java to show the difference in syntax. As you will see the simplicity is quite clear.

Python:

print(“Hello World”)


Java:

Public class HelloWorld {

        Public static void main(String[] args) {

                system.out.println(“Hello World”)

        }

}


The reason for this brings us to point number two. Python is good for beginners because it is dynamically typed. This means it does not compile. Some will say this is both a pro and a con but for a beginner, it is important to focus on writing programs and understanding the concepts involved. The more a language helps you to focus on that the better and Python is the best option for that.

Writing your first program

Before you can write anything you need to install Python on your computer. To do this, go to https://www.python.org/ and hit the download button. Then simply follow the installation steps to get Python up and running on your computer.

After that you are going to need to install a program to actually write your code with. You can choose whatever you want but Pyzo is a good option if you want something simple for now. To install Pyzo go to http://www.pyzo.org/ and again, download and follow the installation instructions.

Once it is installed you should open it and find an environment like the one below.

The large white area is where we will write our code and the smaller area on the right is the shell where our code will be output. Before we write our code we need to save our file on the system. Right-click the file tab and go to “Save As”, then save it as whatever you like.

Now that’s done we are ready to write our programs. Let’s start simple with the Hello World program from before. Enter the python code from before into the editing area on the left. 

Next right-click the file tab again and choose “Run file as script”. You should see you “Hello World appear in the shell on the right. It should look something like this. 

Well done! You just wrote your first Python program.

Now we will write one more program, this time involving a bit of arithmetic. You can create a new file for this or simply continue using the one you already have.

In programming, numbers are referred to in a few different ways depending on the type of number they are. For this example we will use integers. In Python integers are whole numbers that can be either positive or negative. Most of the time they are referred to as "ints".

Here is a very simple program that prints the answer to 2 multiplied by 3.

In this example “x” is a variable that has been assigned the value of 2 * 3. Notice that we still need a print statement to show the output in the shell. Without this statement, the program will run but nothing will show in the shell. The difference with this print statement is that before we were simply printing a string whereas now we are printing the value of the variable. In this case, the output is 6.

It’s important to point out that this is about as basic as it gets when it comes to programming. The point of this article is to get you up and running and show you that once you get started it really isn’t as hard as some people make it out to be.

What’s next?

Now that you have written your first Python programs you can begin exploring the language further. YouTube is a great place to find free programming tutorials, but there is also plenty of other content on here that you can look through to learn more.

If you aren’t sure what to do next, a good place to start is by learning about the different data types in Python and then moving onto functions. The main thing though, is that you experiment with the code yourself and you enjoy it. Programming takes a long time to get good at so don’t get too discouraged if you get stuck. It’s all part of the process of becoming an amazing problem solver!





MORE ARTICLES

How To Structure a Python Project

Writing programs isn’t all about the code. Sure when it comes down to it, the code is what makes the functioning program...

6635 views

Pipenv - First Impressions

Pipenv is an experimental tool with the purpose of bringing together different packaging tools in one place. It has been...

4003 views

How To Write Tests For Python

An important part of development in any Python project is testing. Testing is used to help define the intent of your cod...

2998 views