Chapter 1. Introduction to Python

1.1. Pros and Cons of Python
1.2. Who uses Python?
1.3. Prerequisite
1.4. Tools of the Trade
1.5. Zen of Python
1.6. How to Write and Run Programs in Python
1.7. Invoking REPL of Python

If you are reading this book then you probably know what Python is and you know the usage of Python. However, in case you don't then I will let you explore the book to see if it is useful to you. Python is a general purpose, object-oriented scripting language which was initially developed by Guido van Rossum. Structured programming is also fully supported in Python. Many features of Python support functional programming(map, filter, reduce and lambdas are part of it) and aspect-oriented programming. Python also supports metaprogramming and metaobjects. Its design was inspired by a number of other languages, including C, Modula-3, and particularly the educational language ABC. The first question is why Python is so popular? What drives its popularity? Let us go through pros and cons of Python.

Figure 1.1. Guido von Rossum(image countesy Wikipedia)

Guido von Rossum(image countesy Wikipedia)

>

1.1. Pros and Cons of Python

1.1.1. Easy to Write, Easy to Read

Python is probably the easiest language out there. Being a dynamically-typed language helps but there are other dynamic languages which are not so popular. The reason is the syntax and elements of Python are well-planned and makes user feel right at home. Python code is very close to readable English or pseudo code. Also, because Python uses whitespace for indentation all programs appear the same if you follow PEP-8(Python guidelines are documented as PEP which is short for Python Enhancement Proposals). High readability means Python code is maintainable and can be reused. Obeject orieinted programming facilities of Python further increase code reuse.

However, there is one disadvantage is that since it is a dynamically-typed langauge you might have runtime surprises if you do not test your code carefully or your code coverage is low. This comes at the price of edit-compile-run cycle of statically typed language. Some people prefer statically typed language and some people prefer dynamically typed language. This problem is not an issue if you fully test your code. There is a saying in software engineering that untested code is broken code. People who write software in statically typed language also test their code further increasing time taken to develop software.

1.1.2. Software Quality

Python programs are shorter than their counterparts in other programming languages due to syntax and dynamic typing. Less code means less problems. Less code means less time spent on debugging and fixing bugs. Python is dynamically typed does not mean it does not have types. Evem a basic integer has a class type int. Since code size is smaller bugs are fewer and even if they are present Python programs can be fixed with much less effort than those in compiled languages or other dynamic languages. The ease of writing and reading helps with software quality as well.

1.1.3. Batteries Included

Python comes with many built-in data structures and libraries. You get array, lists, dictionaries, sets, queues, heapq etc. It has libraries for disc IO, network, XML, internet data, testing frameworks and so on. Since there are so many libraries in Python's stanndard library you get fast implementation because a lot of such libraries are written in C. Also, most of AI/ML libraries are written in C/C++ and then Python bindings are exposed for end programmer. If you ever need a library for any task it is almost guaranteed that there will be a Python library. For example, all SQL and NoSQL databases have Python libraries for them.

1.1.4. Low on Human Resources

Python boosts developers' productivity because it does not have static types, code is less verbose, memory management is automatic, and there is no compile-link-run cycle. Speed of development of programs in Python is about 3x-5x compared to C/C++/Java programs. It is easy to buy hardware time but developer time is very expensive. Also, since less time is spent in debugging(because of high readability and less code) and fixing bugs that also amounts to savings of time, money and other resources.

1.1.5. Portability

A Python program can be run on any operating system which has a Python interpreter. Also, if there is no interpreter highly portable C code of Python interpreter can be compiled to get one. Right now 3.10.4 is available for Windows, Mac OS and Linux. Other platforms include AIX, IBM i series, iOS and iPadOS, OS/390, z/OS, RISC OS, Solaris, VMS and HP-UX. That covers most of the software and hardware series in use right now. A Python program does not need to be compiled on every OS or hardware. Copying is enough in most cases to run the code on any target platform unless OS specific code or hardware speicifc code is present. Since Python is portable you do not need conditional code most of the time.

1.1.6. Cross-Language Operations

Python can be extended using C/C++ and Python can call code written using C/C++. If you use Jython then you can use all Java libraries and there is Iron Python for .Net. For Javascript you have Brython with which you can replace Javascript code with Python. It can interface over network using RPC, SOAP and CORBA.

1.1.7. Python is Slow

While it is true that Python is slow the slow pieces can be optimized by using libraries like numpy, scipy, pypy, numba, cython etc. If all else fails you can write that critical part in C and call that using Python. Thus, this argument can be worked with.

Also, modern hardware is so powerful and cheap that developer time is far more expensive than hardware cost. Thus, from a business perspective it is better to invest more in hardware rather than more developers.

1.1.8. Python has GIL

It is true that Python has GIL(global interpreter lock which fundamentally results in one thread running) and muti-threading is hard but it has been established that multiple processes is a better idea than multithreading because of complexity of multithreading. Also, multithreading is hard to debug. This thought is further estanslished by frameworks like Seastar and Glommio which employ per core thread architecture without sharing data between threads. Rather they use inter-thread message passing. Thus, this argument is not valid either.

1.1.9. Python is Memory Hog

It is true that Python eats a lot of memory but it is justified with the facilities it provieds. If memory is a constraint like on low resource devices they Python may not be a suitable language to program in. However, if you are on even on a mobile then working in Python is justified.

To justify even further consider the case of integers. In Python integers are a class and it can be as large as memory allows it to be. Compare that with integers of C/C++/Rust/Golang/Javascript which have fixed length for integers. You will need an arbitrary precision library to do what integers of Python do. So, managing this integer will obviously be slower and consume more memory.

Another case is of lists in Python. Lists in Python are heavily used and they can accomodate all kinds of data i.e. they can contain heterogeneous elements. Compare this with STL of C++ which cannot do this. So this kind of feature comes at a price.

1.1.10. Time to Market

This is a management term which implies that how fast you can ship features. Now that software industry is widespread there is lot of competition. Thus if you cannot deliver features faster and more features before your competition you will lose your edge and market share. This has direct impact of revenue, user base and profit margins. Thus, Python is an excellent language to deliver features fast to users. You can do lot more work in short amount of time in Python leading to higher economic returns and happier users.

1.1.11. First Language for AI and Data Science

Tensorflow, PyTorch, MxNet, Keras, GPT-2/3 are most prominent AI/DS frameworks having Python bindings. While the lower level implementation of these libraries are in C/C++ the users who write models using these frameworks/libraries make use of Python bindings exposed by them. Python is the most used language for AI/DS development. Thus, if you want to leverage latest and greatest features offered by these frameworks then you got to use Python. This also proves the earlier point that critical performance related features can be written in C/C++ and other logic can be written in Python. There are great tools like Jupyter for further speeding up development.

There are great visualization libraries like Matplotlib or Plotly to generate nice looking graphics for AI/DS models. Also, Python has great documentation framework in Sphinx which is one of the most advanced tool for software documentation.

1.1.12. Great for Web Development

There are lots of web development frameworks for Python making web development a breeze. One of the earliest frameworks Django/DRF is very popular for web development. It comes with great admin panel which is auto-generated. Then we have Pyramid, Pylons, Flask and Falcon which cater to other needs for web development. For async support we have FastAPI, Sanic and Blacksheep which compete with the speed of Node and Golang frameworks.

1.2. Who uses Python?

As of this writing Python comes bundled with GNU/Linux distributions and Mac OS. Based on download rates and developer surveys we can say that there are more than 1 million users of Python; though it is hard to say that accurately because Python is free(free as in freedom not free beer) software. Since Python comes bundled with GNU/Linux and Mac OS the actual no. is developers is certainly more than 1 million. Since Python first appeared 30 years ago it is quite mature and stable so there are many companies which used it. Youtube, Instagram, Disqus are some famous websites which have been built with Python. Other than individual users many companies use Python for real money-making businesses.

  • Google uses Python in various applications inclduing search engine. Python is one of three official languages in Google. The other two are C++ and Java.
  • Youtube is mostly Python.
  • Instagram and Disqus are written using Python and Django(a web framework in Python which we will study later).
  • Bit-torrent P2P file sharing system is a Python program.
  • NSA uses Python for cryptography and other intelligence analysis.
  • NASA uses Python for its space missions. Fermilab, Los Alamos and JPL use Python for scientific computing.
  • Maya, GIMP and Blender have Python scripting API.
  • Intel, Cisco, HP, Qualcomm, Seagate, and IBM use Python for their hardware testing.

There are many more famous companies making use of Python for their day-to-day work. You can find more success stories at https://www.python.org/success-stories/

There will be hardly any significant company which does not make use of Python in prominent way.

1.3. Prerequisite

1.3.1. Python Version

Right now Python 3.10.4 is available for all platforms so we will make use of that. Python version 2 will not be discussed in this book at all. It has reached EOL(end-of-life) quite some time ago and is not receommended at all for new development. If you have legacy code it is best to patch that to support latest version of Python as a lot of new exciting features are added with each release.

1.3.2. Installation

All GNU/Linux systems come installed with Python and you can use that to work with most examples and problems. If you want latest Python then depending on your distribution you can use a backports repository for latest Python installation. It is trivial to install Python on Windows, just remember to add Python to the PATH variable of the Windows to make it avaliable on command prompt of Windows.

1.3.2.1. Installation from Source Code

Installation of Python from source code is somewhat complicated and not recommended as it requires quite a few dependencies for complete compilation. It is out of scope of this book to give details of source code compilation of Python for so many operating systems.

1.4. Tools of the Trade

You will need Python interpreter which we have already discussed. You will also need a text editor or IDE. There are many choices. I will list some of them. You can use VI/VIM, Emacs, Visual Studio Code, PyCharm, Sublime Text and so on. Personally I use Visual Studio Code when working on Python. It is easy too use and comes with many extensions. You can choose any text editor which you are comfortable with. PyCharm is a bit heavy and for large projects you will need a good computer for intellisense to respond reasonably fast. Also, if you have an open source repository which is older than three months you can get professional version of PyCharm absolutely free from Jetbrains. You can also use VI/VIM or Emacs which are super lightweight and can be configured to perform like an IDE. neoVIM is also quite popular among VI/VIM users and you can try that as well. New users find VI/VIM or Emacs a bit complicated to use. If you are on Windows you can use Visual Studio community edition for free and it has very good Python support. It is one of the best IDEs on Windows hands down.

1.5. Zen of Python

Zen of Python or philosophy of Python is an interesting one. To get this you can type python on command prompt and then in Python shell you can type import this and then hit enter to see it, which is given below:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
    

This summarizes the philosophy of Python very well. While this philosophy imposes some restrictions on what Python can do it is great for productivity. There is no doubt that there are superior languages in theory like Scheme but in practical terms they did not achieve much.

1.6. How to Write and Run Programs in Python

Hello world is a classic first program. In Python it looks like below:

print("Hello world!")

You can save this code as a file on disk. Say you give it a name hello_world.py then you can run it like python hello_world.py and you will see Hello world! on screen. It is possible to run this print program on Python REPL(real-eval-print-loop) but for larger programs REPL does not work well. So we will stick to file based program.

1.7. Invoking REPL of Python

You can run python without any arguments to get REPL shell of Python. It has >> prompt. As an alternatve way to write previous program we can type print("Hello world!") and hit enter to see the same output on your terminal or command prompt.


© 2022 Shiv S. Dayal. www.ashtavakra.org. GNU FDL license v1.3 or later is applicable where not stated.