Get started with python

> What is python and how can you learn it?

What is python??

Python is a easy to learn and a very powerful interpreted, high-level and general-purpose language.

What does this even mean?

Some of the features that make python a unique choice are:-

There are tons of features python provide but there are some cons as well (you’ll learn and understand the reasons behind them once you are dived in.)

How can we write and run python??

There are several ways to write and run your code. But the 2 broad and general ways are either to use an interactive interpreter or you can go with the python files and run them.

All you’ll need is a python interpreter installed on your system before you can do anything. You can confirm this by checking python --version or python3 --version. If this gives something like Python 2.x.x then it means you have python2 installed and if it gives you Python 3.x.x this shows the python3 is installed. you can have both of them installed as well.

If this does not gives you any output then you’ll have to install it yourself from here. I’ll use python3 and I recommended you to use it instead of python2 (It’s going to die soon)

Let’s see how you can use python3 interpreter to run your code. Obviously you can’t write code using interpreter — wait!! — You can write your code using interpreter but you can’t save it permanently. It means the code will reside in the volatile memory(RAM) instead of the the permanent one (Harddisk). So if you want to write code in permanent memory (Harddisk storage), you’ll need a text editor to create a source code (Check in a simple wiki).

(I am using a debian based linux distro — So few things might be different from yours)

python-version

This confirms that I have python3 installed in my system and I can invoke it by python3 command. For you it can be just python. It depends on how it is installed either by you or your OS/distro.

python-interpreter

Just type python3 in terminal and you should be shown with something similar to the image above. This is called interactive python REPL (Read, Evaluate, Print, Loop). This will read whatever you type... then understand and evaluate it using python language grammar... and print the result after evaluation (either the output or an error)... Loop again.

(Note: >>> - This thing is called a prompt, This is the indicator of that python REPL shell is available to take commands and REPL it.)

Let’s start by initializing some variables.

variable-init

We can use these variables to perform some basic mathematical calculations (also we can perform complex mathematical calculations as well — check this)

maths-vars

As we can see python supports negative values and float (decimal point) values without any problems. But that opens the door to another type of variable types. There are few data types supported by python.

The below example shows how to work with Strings in python.

python-string

To print a string on console (terminal screen), we need to use an in-built function and pass the string to it which is to be printed on screen.

print-function

Congratulations!! you have made your first ever python code. But all your hard work will be forgotten once you close this interactive shell/REPL. For that you need to write your code in a file.

To exit the REPL, type — exit()

Now open any text editor of your choice and create a file with it. Name it hello_world.py (you can name it anything). Also open a terminal in the same folder. Type the below code in the file. Save it.

## Creating a hello world string and assinging it to a variable
x = "hello world"

## passing variable to print function
print(x)

Finally, run your code using python interpreter.

hello-world-file

Now you know how to write a python code and execute it. You can look up for more tutorials and get your way around python.

congratulations

Is python a programming language??

Umm… Yes and No.

What we usually read everywhere is that python is a programming language implimented in C language — also known as CPython. But Python is implimented using many other languages apart from C (check all implimentations of python).

So you can consider Python as an Idea that can be implimented using multiple languages as per the need.

This thing won’t change much for you. Basically we’ll be using Cpython everywhere normally, it’s just a good thing to know that there are other implimentations of python as well.

Some good resources?

Below is the list of the resources that can be your guide to the rabbit hole.

Connecting the dots and rest is magic. https://ayedaemon.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.