Home Python Course #1: Python for Absolute Beginners
Post
Cancel

Python Course #1: Python for Absolute Beginners

You made the decision to start learning Python? Congratulations! In this article, you will learn how to write your first Python code and how to set up the code editor Visual Studio Code without any prior knowledge in programming.

Check if Python is installed

The first thing you have to check is if Python is installed on your computer. Figuring out if Python is installed requires you to use the command line of your operating system. The command you have to enter in the command line is the same on the three major operating systems Windows 10/11, macOS, and Linux. If you haven’t worked with a command line before, now is the best time to start using it because it will become one of your best friends for developing applications in almost every programming language.

  • To open the command line on Windows 10/11, go to the start menu, search for PowerShell, and open it. Open PowerShell
  • To open the command line on macOS, open Spotlight and search for Terminal and open it. Open macOS Terminal
  • To open the command line on Linux, you usually have to search for Terminal, but it depends highly on your Linux distribution. Open Ubuntu Terminal

After you opened the command line, enter the following command and check its output:

1
python3 -V

If a Python version 3 is installed you can now see its version number. To follow this Python course, I highly recommend using a Python version equal to or higher than 3.6.x. The provided code snippets won’t run correctly if you got a lower version of Python. An error message in your command line means Python isn’t installed or not set up correctly. Don’t panic I already released a complete guide on how to set up Python on Windows 10/11, macOS, and Linux, which you can find over here: How to Install Multiple Python Versions on your Computer and use them with VSCode

Your first Python code

After you made sure you got a suitable Python version installed on your computer it is time to start using Python. As a first step, you won’t write a Python program but rather use Python’s interactive mode. To enter Python’s interactive mode, enter the following command into your command line:

1
python3

Now you should see some text telling you which Python version you use and three > signs (>>>). When you now type something into the command line, it will appear after >>>, and when you press enter after you typed something, it will be interpreted as Python code. For example, you can use this interactive mode as a calculator by typing something like:

1
27*3

And the limit of what you can enter as a calculation is very high. For example, you can enter something like this as well:

1
13-2*21/7+51-16

Python will respect the point before line rule and evaluate 2*21/7 first and then add or subtract the other numbers. If you want to divide the result of 13-2*21 by 7+51-16 you have to use parenthesis to tell Python what it should evaluate first. Use the arrow up key on your keyboard when you are in the command line to cycle through the last statements you have entered into Python’s interactive mode. And then change the previous calculation to (hint: you have to use the arrow keys to move the cursor in the command line):

1
(13-2*21)/(7+51-16)

This will result in a completely different number than the first calculation because the terms enclosed in parenthesis will be evaluated before the division.

You can also print text in the interactive mode when entering the following:

1
print("Hello World!")

This will print Hello World! onto the command line. Make sure to enclose the text you would like to print in " or '; otherwise, it won’t work. And with that, you have written your first Python code. To leave the interactive mode, enter the following command and press enter:

1
quit()

Your first Python program

However, the use of the Python interactive mode is minimal for you at the moment. Due to this, you will now write your first Python file, which can then run as a program. To write code for any programming language, you need a plain text editor. You can’t use Word or other rich text editors to write code. Under Windows 10/11 you can use Notepad, on macOS TextEdit and set it into plain text mode by pressing CMD+Shift+T; under Ubuntu Linux, you can use gedit for example.

In the text editor, enter the following:

1
2
if __name__ == "__main__":
    print("Hello World!")

The first line tells Python that the code that is indented under it is part of the main program you would like to run. I will skip the long answer for why you should always use if __name__ == "__main__" when writing a Python program for now and will come back to it in another article. Make sure you use Tab to indent the second line. The indentation tells Python that print("Hello World!") is part of your main program.

To run this program, you have to save it first. To make things easier, give the file a name without spaces and make sure to give it the file extension .py, which will identify this file as a Python file. For this tutorial, enter hello.py. Then save it on your computer in your user directory. Now open the command line again and navigate to the directory in which you have stored the file. To navigate to a directory in the command line, use the cd command followed by the path you want to navigate to. For Example if your file is under C:\Users\yourname\Docments or /home/yourname/Documents on macOS or Linux enter:

1
cd C:\Users\yourname\Docments

on Windows 10/11 or

1
cd /home/yourname/Documents

on macOS or Linux. To check if your Python hello.py file is actually in the directory enter ls, which will list all visible files in the directory you are currently in. If you can see hello.py among the listed files, you are in the correct directory. And to finally run your first Python program, enter:

1
python3 hello.py

And as you probably already expected, it will print out Hello World! onto the command line. You can extend your first Python program with the calculations you entered in the Python interactive mode before. However, you have to enclose them in a print(...) statement to see them on the command line in contrast to the interactive mode, which prints out the evaluated calculation after you press enter. Your program could look like this now:

1
2
3
if __name__ == "__main__":
    print("Hello World!")
    print((13-2*21)/(7+51-16))

In Python each expression of your program goes into a seperate line and the program runs line by line from the top the the bottom. So if you want to print the result of the calculation first you have to switch the position of the two print(...) statements. You can also merge the Hello World! print and the calculation into one print(...) statement when you separate them with a comma ,:

1
2
if __name__ == "__main__":
    print("Hello World!", (13-2*21)/(7+51-16))

Using a plain text editor for programming is not ideal. You usually don’t have line numbers that are super important when something goes wrong, and Python tells you where to look. A text editor doesn’t have syntax highlighting, which shows your code in different colors for better readability, and it can’t call Python to run your program. But there are a lot of free programs for writing, executing, and debugging code. So, for now, save your hello.py and close the text editor.

Setting up Visual Studio Code for Python

Visual Studio Code (short VSCode) is one of the most popular code editors at the moment, and it is available on Windows 10/11, macOS, and Linux. Installing it is pretty straightforward. Head over to https://code.visualstudio.com/Download and download the latest version for your operating system.

After installing VSCode open it and go to Menu -> File -> Open File … and open the hello.py file you created. You will then see your code in colors and with line numbers. VSCode hello.py

To use the full potential of VSCode, you have to install the Python extension for it. To do so, click on the gear icon in the bottom left corner and choose Extensions. VSCode go to extensions menu

In the extension menu enter Python as a search term and click on Install next to the Python (IntelliSense (Pylance), Linting …) entry. VSCode go to extensions menu

After installing the Python extension, you can leave the extension menu by double-clicking on the explorer icon on the top left of the icon bar. Now you can easily edit your Python code in VSCode. For example, if you want to add another print(...) statement to your current program, press enter after the first print(...) statement, and your cursor will automatically be indented by one tab. Then enter p in this new line, and a menu will pop up showing you all possible statements you can enter. Use the arrow keys to select print from the list and press enter to insert it into your program. This might look like a small gimmick at the moment but will be a huge time saver when you work on big projects. If you accidentally closed the suggestion menu, you can always bring it up by pressing Ctrl+Space on Windows 10/11 and Linux or Cmd+Space on macOS.

To run your program now, you don’t have to go into the operating system’s command line. You can run your program directly from VSCode. To do so, go to Menu -> Run -> Run Without Debugging. VSCode run

This will bring up a command line terminal at the bottom of your editor window in which you can see the output of your program. You can also use the shortcut Ctrl+F5 to run your program in VSCode. VSCode run

This concludes this article on how to write your first Python program. You have seen how to use Python’s interactive mode, do basic math calculations with Python, use print statements, and use VSCode to edit and run Python files. I encourage you to play around with the things you learned because trying things out yourself is one of the best ways of learning. You can find part two of this Python course over here: Python Course #2: Variables and Primitive Datatypes for Absolute Beginners.

If you have any questions about this article, feel free to join our Discord community to ask them over there.

This post is licensed under CC BY 4.0 by the author.