What is python
Python is a popular high level programming language. Programming languages are set of instructions written for computers to perform specific tasks. We have different kinds of programming languages like C, C++, Rust, Java, C# and of course python. Most languages are categorized as low-level, mid-level or high-level languages. Python is a high level programming language because it is more human readable and abstracts away many hardware details. On the other end, low-level languages are more closer to being easily understood by computers (or hardware) than humans.
Python is general purpose and is used for web development, system administration, scientific and numerical analysis and GUI development. The cross-field application and ease of learning has made python one of the popular languages.
Getting started with python
We are now getting closer to starting to write python code. But, before that, we need to set up our computers’ environments to write and run python. When we write code, we need an interpreter to read and execute code.
On that note, we shall begin with installation of the latest python version to our PCs. It is important to note that this installation depends on which Operating System your computer is using. Mostly, Linux distributions and mac OS comes with python pre-installed. As for windows, we can install python from the link and follow instructions on the Python installer for installation. I will be focusing on Linux, since my computer runs on Ubuntu.
We already know that python comes pre installed in linux distributtions, but it is important to know the version that is installed. Python has several versions, and the latest is version 3.x.x. Installing the right version is important because Python 3.x.x is not backward compatible, hence you can experience challenges while coding in python.
Checking Python version.
To check python version, we run the command python -V
or python --version
.
After installing the right version, we need a code editor to use in writing code. Some editors come pre-installed like Notepad, Vim which is fine.
In my opinion, using editors like Vim to write code as a beginner, you get to learn a lot on the actual syntax of python, and also learn to work from the Command Line Interface. Vim can have a steep learning curve on basic editor functionalities, and on that note we can use different editors of our preferences.
To write python, one can use VSCode, Pycharm or Sublime IDEs. To install them, you can use the links below, and remember to get the right software for your Operating System.
We now everything set to start coding in python. As anyone starts learning,python documentation is really important and contains everything needed to learn python. On top of it, there are various websites that teach well. Python for Data engineering, Here We Go.
Python basics
As for any programming language, we can start by writing ‘Hello World’ program. This is important because it enables us to learn how we run python programs.
Writing Python on IDEs
When using IDEs (Integrated Development Environments), it is easy to run a python program. All you need is to select the file you want to run and click the play button and the program executes.
Writing Python using the terminal
Learning and mastering how to use the terminal is important. Mostly when interacting with virtual machines on cloud, basically one works on a command line interface (CLI)
Step 1: Writing the script on the terminal, and create a .py
file. For the tutorial we are using vim editor. Before, navigate to a folder you want using cd
, then run Vim hello.py
To save the file, press ESC
, then type :wq
and hit enter
. Run ls
command on the directory to list all files on the directory.
Step2: Once the file is already saved, run the script using either: python hello.py
or python3 hello.py
As observed, we are using the command python
(or python3
) + file name
to run the script. Before we all get confused, the two commands are similar but subjective to the python version installed. Python command traditionally points to python 2.x.x while python3 explicitly points to python 3.x.x which is the most recent and mantained version on python.
Another method to run python on the command shell is to make it an executable. This is achieved by adding a shebang on top of the script. Note that this is only for unix-like systems eg. Ubuntu
A shebang
A shebang is the first line in a script that instructs an operating system to execute a file. Basically a shebang enables us to run a script directly like ./hello.py
unlike using python3
command.
Shebang syntax:
#!/usr/bin/python3
- Step 1: Add shebang on the first line of the script. Save and quit vim
- Step 2: Make the python script executable. Run the command
chmod u+x hello.py
. The command gives the user(you) execute permissions on the file hello.py - Step 3: Run the file directly.
./hello.py
In conclusion on these two methods of running python scripts, both of them are interchangeably. Any can be used, but I would prefer shebangs on production scripts especially used for automation. As for python3
command, it is easy and flexible to use(especially when switching between versions) when learning and development.