Python Labs : Say 'Hi!' with Python

Madhawa Perera
7 min readMay 1, 2019

--

In this article, I'm hoping to cover how to write your first Python program.

This an article for a beginner. If you are here to write your first program with Python then this article is for you. Well, the good news is, keep in your mind that Python is a programming language which is very easy to learn and use compared to many other programming languages out there. So it has nearly a very small learning curve. Also, according to PYPL ("PopularitY of Programming Language Index is created by analyzing how often language tutorials are searched on Google"), Python is the most popular language so far in 2019 as well. Nowadays, you will see Python is used in web applications, network servers, machine learning, desktop apps, data analysis, and more. So I'd say you made a good choice :).

I'd recommend you to go through my "Let’s start with Python — Setting up the programming environment" which would be helpful but not mandatory. You can read it while you are going through this article (pause this article here) or later or never ;). Following are the 'prerequisites' (There are alternatives tools out there which I have mentioned in the article I mentioned above) to start writing your first Python program.

Prerequisites

Download Python 3.7.3 directly from https://www.python.org/downloads/ (Make sure to pick the correct OS version — Note that Python 3.7.3 cannot be used on Windows XP or earlier). For Windows use the executable installer which is easier.

Download and install PyCharm Community version. https://www.jetbrains.com/pycharm/download (Again make sure to pick the correct OS version)

I'm using a mac for this tutorial. If you are also using a mac please note that macOS already have Python 2 installed in it. But that is not gonna do any harm. Your Python3 installation is not gonna affect that as well. You can keep both.

Once you download Python 3.7.3 the installation has made pretty straight forward unlike the old days. Just follow the installer GUI instructions (As in Figure 01) and then you are all set.

Figure 01: Python installation window

Once done, on your mac go to the terminal and type python3.7then you will see the Python console. Type exit() and exit the console. (Refer to Figure 02).

Figure 02: Check Python 3.7 installation

If everything goes well up to this point next is to create your first Python project. Open the PyCharm editor on your mac. Go to + Create New Project. You will see the following window(Figure 03). There, click on Project interpreter. Now you should be in the following screen(Figure 03).

Figure 03: Setting up the project interpreter

In here, firstly, it is asking for a location to save your project and its contents. Next, the most important section is the project interpreter. This is where you specify which tool you are going to use to create your virtual environment and what Python version you are going to use in your project. Keep in mind that you can change your Python version later by changing the base interpreter even after you created the project. No pressure. :)

Under the interpreter, you have two options. First is to create a new environment using a tool on your preference (a tool which will be used to create the virtual environment for your project, Some tools you can pick are Conda, Pipenv Virtualenv etc.). Second is to use the existing interpreter. If you select existing interpreter your project's development environment will be the system default. The difference is, all your project related packages and libraries will be saved system-wide. Usually, even on macOS Mojave you will get Python 2.7(not Python 3) by default. This will be there under the existing interpreter by default. If you select this, then your libraries and packages will be installed in /System/Library/Frameworks/Python.framework/Versions/2.7/bin. Otherwise, if you have downloaded and install Python 3 from the official website, you can add that as existing interpreter then your packages will be saved in/Library/Frameworks/Python.framework/Versions/3.7/bin. However, it’s always a good practice to not install your project related packages and libraries systemwide. Always try to create and keep a separate virtual environment for each project. Therefore, select the first option and create a new environment. Here, I'm using Virtualenv which will be there by default if you download and install Python from the official website as I mentioned above. This will create a "venv" directory inside your project. Click create.

Please note that based on which method(e.g. You could use “Homebrew” to install Python) you used to install Python, the aforementioned locations will be different.

Once you click 'Create', you will see a window is prompting to indicate you that it is creating the virtual environment for you. Once it's done and the project is created you will see a project structure as shown below(Figure 04). There you will see the 'venv' directory. Inside this directory, it will store all the packages that you are going to use in your program. This is where you should install the Python packages that are necessary for your development. This is an isolated environment from other projects.

Figure 04: Project structure

For your further understanding, go to PyCharm -> Preferences -> Project: Hi_World(your project name here) -> Project Interpreter. Here you will see a similar window to Figure 05 which shows all the currently available pacakges in your virtual environment. If you are struggling to understand what are these virtual environemnts and packages are refer to my article “Let’s start with Python — Setting up the programming environment”.

Figure 05: Packages installed in your virtual environment

Click Cancel, and you are all set to write your first piece of code with Python. Right click on the project name and go to New then Python File . Give the file a name. I have named it 'app'. Refer to Figure 06 below.

Figure 06: Project structure with app.py

The first thing we are going to do here is to print the sentence "Hi World". (This is usally the first and most conventional activity you would do when learning any programming langauage. Printing "Hello World" statement. For a change let's print "Hi World" :D ). As I mentioned at the beginning of this article, Python has almost zero learning curve. Just type the following line of code in your newly created Python file.

Make sure to add a blank line at the end of the file. (Well this is a best practice now. Quoted: "Many older tools misbehave if the last line of data in a text file is not terminated with a newline or carriage return / new line combination. They ignore that line as it is terminated with ^Z (eof) instead".)

That's your first Python program. Let's run it and see. On mac press control + option + R or go to the Run menu and then select Run.... There will be a prompt to select the file name. (In my case it's 'app'). Click on the file that you want to run. You will get the following output.

Figure 07: Output

Well, that's it. :D Pretty easy ha! Now the rest is up to you. You can write various codes here and run them. Practice makes you perfect. Search on the internet and try some sample codes, write your own and have fun coding. In my next article, I will cover the basic concepts of programming with Python.

Small exercise…

Try to do the following exercise with Python. I know this will be too easy for you. :D Write a program to get the output shown in Figure 08. Note that you have to let the user of your program enter their name first and then to use that name thereafter.

Tip: Search online on how to take user input in python 3.

Figure 08: Expected output

Pretty easy ha! Congratulations!! You wrote your first Python program and complete an exercise too. If you find this article helpful, please leave feedback.

If you like this article and the content that I create, you can buy me a virtual coffee ️😊;

Cheers!

--

--

Madhawa Perera
Madhawa Perera

Written by Madhawa Perera

Research Engineer, HCI Researcher, and Sessional Academic at ANU. Passionate about crafting human-centered AI experiences through Mixed Reality (AR/VR)

No responses yet