How to Start with Python Language for Beginners

From training machine learning models to creating a small-scaled Raspberry Pi project, Python programming language has a vast range of functionality. Due to its simple implementation and easy availability of libraries, Python is the preferred programming language where direct application of an idea is concerned. The reason for this lies behind the availability of plenty of resources that save a lot of time and let you focus on the central part of the problem.

In this comprehensive guide on python language for beginners, we will focus on the basic steps to get started with Python. We won’t go much into detail like the advantages and disadvantages of Python and the. You can read more about that in this article. In this Python tutorial, we will cover the following points:

[Note: It is assumed that you are working on Linux OS as we will use sudo commands. If you are on a Windows machine, you might want to consider installing a Virtual Machine. Have a look at this video to download, install, and configure one].

Linux Prerequisites

Before we get started, there are a few Linux commands that are important to understand:

  • cd: This command lets you change directories.
  • mkdir: This command is used to create a new folder/directory.
  • touch: touch is used to generate an empty file.
  • python3: Any Python script (file with extension .py), can be run using python3 command.

Python programming language is already installed on every Linux distribution. You can check the version by typing the following command:

python3 --version

Installing pip for Python

pip is a tool that is used to install python packages. One can install it by running the following:

sudo apt update
sudo apt install python3-pip

Installing pip will also install other modules necessary for Python. After the installation is complete, you can check its version as follows:

pip3 --version

Why and how to create a virtual environment in Python?

A virtual environment, as the name suggests, is an environment that is isolated from the other modules in your system. Isolating the modules you need for a certain project prevents the compatibility issues that may arise between modules. We will start by creating an environment called first-venv.

python3 -m venv first-venv

If you wish to name your virtual environment differently, you can just replace first-venv with the name of your choice. The rest of the code remains the same.

Installing necessary dependencies in the virtual environment

As mentioned earlier, we want to isolate the necessary dependencies and make them available only for this project. For this, we need to activate the virtual environment we just created.

cd first-venv
source bin/activate

If the environment is activated, its name will appear in parentheses in the terminal.

python language for beginners
Figure 1: Activating a virtual environment

Whatever dependencies we install now will be installed in the environment. Let us start by installing numpy, one of the most popular module for mathematical operations. We will do this using pip that we installed earlier at the beginning of this tutorial.

pip3 install numpy

Create your first project in Python

Now that we have our virtual environment for the project, we can start by writing the source code. For this, we will first create an src folder for our source code and create our Python script in that folder.

mkdir src
cd src
touch main.py

A Python script will have a .py extension. We will write a simple program that will print out a statement: “This is my first Python script”.

print("This is my first Python script.")

After writing the code, you can run the script using python3 code in your linux terminal.

python3 main.py

If everything runs correctly, which it should, you will see the following output:

python language for beginners
Figure 2: Output

Do It Yourself

Summary

In this short tutorial on Python language for beginners, we created a virtual environment and understood its importance. We saw how to use the print statement to display an output of the elements present inside that function. Go ahead and try to display different sentences in Python using the online editor above.

What’s next? Now that you know what virtual environment is and how to use the print command, proceed to understand what lists are and how to use them in Python. Also, are you interested in small Python/machine learning tips regularly? Then check out my Instagram page where I publish posts with interesting tips and information of Python and Machine Learning topics. Apart from that, please do support me on my other social media accounts:

If you enjoyed this blog, you wouldn’t want to miss another post on some other interesting topic, would you? Then subscribe to my free newsletter where you will get reminders and updates on the monthly posts that you might have missed:

Leave a Reply