Get started with python
What is python??
Python is a easy to learn and a very powerful
What does this even mean?
Interpreted- Ahh... Did I say "Interpreted"? Actually python does both. It first compile the code into "byte-code" and then Interprets the byte-code to perform what it was programmed for. (Read this stackoverflow answer)
High-level- This simply means you are abstracted from very core working of the computer system. (Read more at wikipedia)
General-Purpose- This means you can't debate that python is going to replace Java or C++ or any other programming languages. Just because that python has a tons of libraries, easy to read and write syntax, high-level programming features, and many more things which just makes it a WOW... it does not mean that it can replace something as evil as Java. I understand your hatred towards Java but still it is a NO NO. (Read what is general purpose language - Wikipedia)
Some of the features that make python a unique choice are:-
- code readability
- dynamically typed
- tons of libraries
- runs on different platforms (windows, linux, mac, etc)
- etc, etc, etc…
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.
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)
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.
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.
>>> - 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.
We can use these variables to perform some basic mathematical calculations (also we can perform complex mathematical calculations as well — check this)
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.
- float (Decimal point)
- Strings (text)
- Binary, Hexadecimal, Octal
- and tons others (Thanks to packages in python)
The below example shows how to work with Strings in python.
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.
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 —
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
Finally, run your code using python interpreter.
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.
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.