Python for Beginners

My first program in Python

Last updated on March 18, 2019 by Michael Lossagk , 5 min  • 
My first program in Python

1. Introduction


I am often asked if I can start a series where I could introduce programming with Python step by step. That’s exactly what I want to do with this post. I will write a simple program in Python and present the steps. To understand this, no programming skills are necessary. What is needed is familiarity with the basic functioning of computers. This means that you should be able to install programs and start commands on the command line.

2. Installation of Python


To be able to program with Python we first have to install the Python library together with the Python interpreter. Python can be downloaded here or can be installed as follows, depending on your system:

# macOS
brew install python # if homebrew is installed

# Linux
# Debian
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get install python3.7

# Red Hat Enterprise Linux (RHEL), CentOS, Fedora
sudo yum install -y https://centos7.iuscommunity.org/ius-release.rpm
sudo yum update
sudo yum install -y python36u python36u-libs python36u-devel python36u-pip

# Windows
Go to python.org/downloads and download the latest Python version

After the installation we should be able to execute the command python on the command line:

# Python Interpreter
python

# The output should look something like this:
[email protected] ~/Sourcecode/ $ python
Python 3.7.1 (default, Aug 17 2018, 19:45:59)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
...

3. My first program in Python


Once the installation is complete, we can start writing our first program. For this we first create a text file with the file extension .py. The file can have any name:

touch helloworld.py

We have now created an empty file called helloworld.py. As you can see, a file in Python can be create very easily. All we need is a file without content to have a valid file for Python. Since the file is currently empty, our program will not produce any output when executed, so we still need a function that generates an output on the screen. This is in Python e.g. the print function. We supplement our file with the following content:

print("Hello World!")

And thats it in Python. We do not need a main function, no modifier, no return value, no curly {}, no semicolon at the end of a command, just the above command. That’s simple, isn’t it? At least much simpler than the analogous example in Java.

4. Interpret and execute


Our program is now finished and can be started. Again, we do not need to convert the code to machine-readable code, as in the example with Java, but we can just write the following:

python helloworld.py

Python interprets (not compiled!) the file and produces the following output on the screen:

Hello World!

If you have made it this far, then congratulations! You have just written and started your first program in Python successfully.

Test it yourself online (if it does not work with Chrome, then allow third party cookies or use a different browser):

5. Questions


You have now learned how to write a program in Python and should be able to answer the following questions:

  • Why is there no need to compile Python code?
  • What exactly is an interpreter?
  • What is the difference to a compiler?
  • Why is there no main function?
  • Do we need a semicolon ;? Why not?
  • Do we need curly braces {}?

I did not go into detail about some points. As a programmer, however, it is essential that you acquire many things yourself and this includes researching concepts and deepening technologies. A good starting point for Python is the official documentation. Without the will to acquire things independently you will have a hard time becoming a good programmer, so I can only advise you to start with it and browse the documentation.

6. Summary


In this tutorial, I showed how to write a simple program in Python. Here I described how easy it is to create and run a program in Python (as opposed to Java for example).

The appropriate code examples can be found in my GitLab repository. If you want to learn more about Python and programming I can recommend you this book.


Michael Lossagk
Michael Lossagk
Coding Enthusiast
Founder @ TechDiffuse

You may also like


Share this: