First Python Application Tutorial

In this tutorial we learn how to create a simple console application in Python. We also do a detailed breakdown to help you understand the structure and syntax of a basic Python application.

Here's a table of contents of what you'll learn in this lesson:
(click on a link to skip to its section)

Let's jump right in.

First steps

Because we’re using PyCharm in this course, we’ll go through the steps of working with it. If you want to follow along with the free community version of PyCharm, please refer to our previous environment setup lessonOpens up in a new page .

If you don’t have PyCharm as your IDE, please still follow along but create, modify and run the files as your IDE requires.

For this tutorial series, we will be creating console applications. Console applications are great when learning a language because we don’t have to worry about other complexities, such as a graphical user interface.

PyCharm

Most of the time, we’ll only use 10% of what an IDE has on offer. For now all we need to know about is the following.

  • Explorer pane. This is where our projects, files, libraries etc. is shown in a list view. By default it is the pane on the left-hand side of PyCharm.
  • File and Code Editor. This is where our individual source code files are shown and where we’ll be typing all our code. By default it is the pane on the right-hand side of PyCharm.
  • Run pane. This is where our console application will output what we tell it to. By default it doesn’t show in PyCharm, but once an application runs it will expand from the bottom.

We also need to know about a few buttons or shortcuts. These buttons are shown in the top right-hand corner of PyCharm.

  • Run. This button will tell the interpreter to work its magic on our application or document. It looks like a little green Play icon and its keyboard shortcut is Shift + F10
  • Debug. This button will tell the interpreter that we think there’s a bug in our code and it needs to try and help us find it. It looks like a little green Bug icon and its keyboard shortcut is Shift + F9
  • Stop. This button will tell the interpreter that we want to stop running the application. It looks like a little grey block icon when the application isn’t running, but will turn red when the application is running. Its shortcut is Ctrl + F2 on Windows and Linux or Cmd + F2 on Mac.

Create a new file

Go to File > New > Python File and name the file HelloWorld. PyCharm automatically adds the .py file extension for us that identifies the document as a python file.

When you create a new file, it should automatically open in the Code Editor window. If it doesn’t, simply double-click on the file name in the Explorer Pane to open it in the Code Editor window.

If you’re working in an IDE that doesn’t automatically add the extension, please ensure that you name your file HelloWorld.py

Edit the new file

Type the following code into the new HelloWorld file and save it. Don’t worry too much about what it means right now, it will become clearer as we progress through the tutorial series.

We strongly recommend that you type and/or run the code alongside the lessons. It will help you learn faster and retain the information easier.

Code:
# My first Python application
print("Hello World")

That’s it, that’s all we need to write our first Python application.

Run the HelloWorld application

In PyCharm, press Shift + F10 or click on the Run button to run the application.

You should see the Run pane expand from the bottom with our output. Its output would look something like this.

Output:
C:\Users\Username\PyProjects\Scripts\python.exe C:/Users/Username/PycharmProjects/PyProjects/HelloWorld.py
Hello World

Process finished with exit code 0

Let’s break it down step by step:

  1. The first line is the file that is currently running,HelloWorld.py
  2. The second line is where our custom output starts. The words we instructed Python to print: Hello World.
  3. The fourth line tells us the process finished with exit code 0, which means there were no errors.

Breakdown of our first Python application

Let’s break down our application to understand a little more about what’s happening.

Line 1:
 # My first Python application

This line is a comment. Comments are a way for us to document our code to help ourselves, or our colleagues, and is completely ignored by the interpreter.

A single line comment, like the one above, starts with the octothorp # symbol. Everything that follows it, on that line, will be a comment.

Line 2:
 print("Hello World")

This line is a function call. The function in this case is print() and is responsible for outputting our text to the console. Between the parenthesis it takes a message to print out, in this case the words Hello World.

Because the message we print is a string of characters, they need to be wrapped in quotes.

If you’re coming from another language, like C++ or Javascript, note that statements are not terminated with a semicolon. We cover this in more detail in the Basic Syntax lesson.

Errors in our Python application

To demonstrate quickly what an error would look like, let’s modify our code a little. Remove the quotes around the words Hello World so that it looks like this:

Code:
# My first Python application
print(Hello World)

Now run the application again. The output will show us an error and an exit code of 1.

Output:
File "C:/Users/Username/PycharmProjects/PyProjects/HelloWorld.py", line 2
  print(Hello World)
                  ^
SyntaxError: invalid syntax

Process finished with exit code 1

Let’s break it down again:

  1. Firstly, it tells us in which source file the error is located, our HelloWorld.py file. It also tells us the line number on which the error was found, in this case line 2.
  2. Then it shows us the offending code and even attempts to point out the exact position of the error.
  3. Then, a new line is shown that explains that we have a Syntax Error with invalid syntax. This means that we’ve written some of the code incorrectly.
  4. Lastly, it tells us that the process finished with an exit code of 1, which means there was a problem.

If we wrap the words in quotes again, the error will go away.

Summary: Points to remember

  • Python files have the extension .py
  • When an error occurs, the interpreter will try to show us what type of error has been raised, and where.