Python is interpreted high-level programming language for general purpose programming.
Not at all anything general about the explanation right? Thought so.
The basic idea behind python is that it is a scripting language and we write scripts in a certain manner and then tell the python interpreter to interpret it and give us the output. That’s all.
Let’s dig in some history of this language, shall we?
Python was first developed by Guido van Rossum
Python was conceived in the late 1980s and came into effect before the popular language Java in 1991. With its first release, python was not able to capture the market as its subsequent competitor Java did after it’s release in 1996. This must have been because of many reasons.
Sooner or later Python came with tons of new features and a revamped Python 2.0 on October 16, 2000, with features like cycle-detecting garbage collector, support of Unicode and many other features like list comprehension borrowed from Haskel. Before Python 2.0, there were versions extending from Python 1 to Python 1.6.1.
Python 2.0 was pretty good and extended till version Python 2.7 with features like backward compatibility, which means scripts written in Python 1 and other versions can be interpreted.
After Python 2, Python 3 was released on December 3, 2008. Python 3 is the Python as we know it today. With Python 3, the Python Software Foundation broke its backward compatibility feature and started fresh with new features and rectified the fundamental design flaw of the language. The guiding principle of Python 3 was: “reduce feature duplication by removing old ways of doing things”. Python 3 nonetheless was still a multi-paradigm language where the coder can choose among the options of OOP, Structured programming, functional programming and many other.
Some major changes in Python were:
input. Thus, taking every input as a string.
5 / 2is
2. In Python 3,
5 / 2is
5 // 2is
These were some great features which were incorporated in the new Python 3.
Enough of the history right? It is boring as it is. Now let’s jump to using python.
Using Python is very easy and anybody who wants a hands-on in programming can do it also.
For Windows user, you have to download the installer, depending on your PC if it is 64-bit or 32-bit. Run the executable file and don’t forget to select the option at the beginning screen to add python interpreter to your PATH. It is a very important step or you won’t be able to use python. Give some time to install it and after the installation is complete, open command prompt(Win+R -> Type cmd -> Enter). Write this below command
This gives the version of the python. If the output is anything other than the version that is Python is not a known command or similar. Then you must have done something wrong or python has not been added into your PATH variables. Then you have to explicitly add the directory in your PATH variables.
For Linux and MacOS users, two different versions of python come inbuilt. For many users,
Gives the output as 2.7.x, as python invokes Python 2.7.x in your system, and
Gives the output as 3.6.x as python3 invokes 3.x.x.
If you want to change this, I would tell you not to because this can break your system permanently and you may have to install the OS again, so let it be.
And if you so eager to change it such that python invokes Python 3.x.x. You can just use
alias python = 'python3'
Hopefully, this will do it.
I’ll be working with Linux users as obviously it is much handy and easy to use and I would advise every programmer to shift to Linux. Why? I’ll write another blog about it. Stay tuned.
So let’s start, shall we?
We will be working with Python 3 and at the end of the blog, I’ll also give you links for those who want to study deeper about Python.
First, let’s just start python:
This will go in your terminals for Linux and macOS users, and for Windows users, you can do it in Command Prompt, but you will have to write just python.
The above command will show you somewhat like this:
This tells us that the python command line interpreter has started and you can start writing code snippets.
Next, you can write, the following things one by one as I have written and below will be the output after every code.
>>> 12+56 68 >>> 26-20 6 >>> -56+45 -11 >>> 25-50 -25 >>> 25/2 12.5 >>> 25//2 #Gives only integer part 12 >>> 5*2 10 >>> 5**2 #5 raise to the power 2 25 >>> var_1 = 100 >>> var_2 = 200 >>> var_1 + var_2 300 >>> print(var_1) 100 >>> print(var_1 + var_2) 300 >>> value = input('Enter the number to be squared: ') Enter the number to be squared: 12 >>> value = value**2 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int' >>> #Right way to input an integer >>> value = int(input('Enter the number to be squared: ')) Enter the number to be squared: 12 >>> value = value**2 >>> print(value) 144
The above error in bold is because in Python we don’t need to declare any datatype hence, by default it takes input and stores the value as a string. Hence, I have to explicitly tell Python that I am the boss here, :P. Kidding, that I am inputting an integer, by int( ) method here.
This was the command line hence, whenever you hit enter it will execute the statement.
So, how do we write codes and execute it as a whole? Well, I got your back.
For Windows users, there is an application called IDLE which comes when you install Python on your system. Just search IDLE and tell Cortana to do some work. She’ll show you the application and once you open it, and hit CTRL + N, you’ll be able to write and run .py scripts.
For Linux users, you can download MU editor by using the below command on the terminal as explained by Kushal Das in his book for python for everyone.
python3 -m pip install -U mu-editor --user
It is around 150 MBs and may take some time to download and install. When it is downloaded then to run it you can by the command
python3 -m mu
When you want to execute the code you can simply click RUN on the editor.
Saving the scripts is also simple in its Graphical User Interface(GUI), just click SAVE and give the script a name without any space, ex. my_script.py.
.py is the file extension to let the interpreter know it is a python script. Every python scripts you’ll find will have this .py extension. Now you know when to shriek and declare a python script.
So as now you know to write basic python and also where to write the scripts. You can go on and read about python more.
Python is the most versatile language. By python alone, we can work on web development, data science, make new programs and software, develop games, develop search engines, etc. Some very famous python libraries and frameworks which you can use as a developer are stated below:
Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.
Flask is easy to use small framework written in Python used for web development. It was released on January 1, 2010, and is developed by Armin Ronacher
Django is a free and opensource framework which follows the model-view template(MVT) architectural pattern. Django’s primary goal is to ease the creation of complex, database-driven websites. Django emphasizes reusability and “pluggability” of components, less code, low coupling, rapid development, and the principle of don’t repeat yourself. Python is used throughout, even for settings files and data models. Django also provides an optional administrative create, read, update and delete interface that is generated dynamically through introspection and configured via admin models. It is fast and also very easy with HTML data binding feature.
You can learn about the latest stable version, Django 2.0 in its own documentation as it is the best resource on the internet.
These three very famous libraries are used for Data analytics and the field of Data Science. If you want to be a Data Scientist or Data Analytics, these three libraries are essential to work upon.
NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. NumPy was first released in 1995 as Numeric and then in 2006, it changed to NumPy. Please refer to the documentation for further information and how to use it.
SciPy is a free and open-source Python library used for scientific computing and technical computing. To try and know more about SciPy try their documentation.
Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in Python scripts, the Python and IPython shells, the Jupyter notebook, web application servers, and four graphical user interface toolkits. The latest stable release of matplotlib 2.2.2 can be read about in its dedicated documentation.
Source of the info: https://www.scipy.org/index.html
Pandas is a software library written for the Python programming language for data manipulation and analysis. In particular, it offers data structures and operations for manipulating numerical tables and time series.
It is also a very famous library and has the below salient features(compiled by Wikipedia):
There are many libraries and frameworks of python, these are just a teaser, if you go on research on the language and dig deeper you’ll get to know its true abilities, and at the end, you’ll see yourself falling in love with this language. Python is actually a never-ending resource which you’ll find at every step of your programming life.
Some examples of Python in daily life, the biggest example is Google, the search engine, search engines are written in python, Political parties gathering votes or analyzing their own supporters is the work of Data Analytics and Machine Learning. Hence, its crucial that as a coder we learn and study more about python.
As promised, some resources to learn about Python:
For me, Python is one of the best softwares that I have worked upon. It has a huge number of uses and at present, it is hovering high above every language. Today for a programmer, knowing Python is a necessity. Hence, I personally advise you to research on this language and get familiar with this language.
This is thegeekbong signing off until next time. Do share and follow for more and please leave your feedback in the comments, it is the most important thing to me.