Introduction to Python


What is Python? #


Python is a general-purpose programming language created by Guido Van Rossum. Python is most praised for its elegant syntax and readable code, if you are just beginning your programming career Python suits you best. Here are some of the cool things you can do with Python

  1. GUI development
  2. Web development
  3. System administration
  4. Financial calculation
  5. Data analysis
  6. Visualization
  7. Machine learning

A little history lesson in Computing #


Things start off with the machine language.

Machine Language #


Machine language refers to the native language of the computer, that is, the CPU. Machine language consists of strings of 0s and 1s. For example, instruction to add two numbers in binary would look something like this:

0101010100001

A CPU usually consists of hundreds of instructions, which are collectively called the instruction set. CPUs from different vendors have different instruction set. This means that, instruction set for the Intel family of CPUs is different from the AMD family.

As you can imagine, writing programs in machine language is quite tedious and time consuming. Furthermore, they are difficult to read, modify and debug (art of finding and resolving errors). Consequently, this led to the development of assembly language.

Assembly language #


Rather than replying on the binary pattern, assembly language uses short descriptive words, often called mnemonics to represent each machine instruction. For example, instruction to add two numbers in assembly language would looks like this:

add a, b

In contrast to the machine language, this was quite an improvement. Even people with no background in Computer science can intuitively understand what this instruction does.

This is all fine and dandy, but the CPU doesn't understand the assembly language, it is still stuck to the good old pattern of binary numbers! This is where the assembler comes in to the play. An assembler is a program whose job is to translate a program written in assembly language to the machine language.

[]

The machine language or machine code generated by the assembler can then be executed by the CPU. After every modification you will have to pass the program through the assembler again to generate the new machine code.

Assembly language made the life of programmer somewhat easier. In comparison to the machine code, the programs written in assembly language was easier to read, modify and debug. But there was still room for the improvement.

Here are some of the shortcomings of the assembly language:

  1. Writing large scale program in assembly language is quite difficult. Even trivial programs require several instructions.
  2. To use assembly language to the fullest one needs to have a through understanding of CPU architecture and instruction set.
  3. No portability. Programs written for one CPU, may not work on other.
  4. Given the complex nature (illustrated in 1-3), it takes lot of time to write programs in assembly language.

Apart from the readability all of these shortcomings also exists in the machine language.

Since, both machine and assembly language require you to have a working knowledge of low-level details of the computer's hardware, they are referred to as the low-level languages. To address the shortcomings of low-level language, high-level language was invented, which brings us to the next topic.

The dawn of the High-level languages #


A high-level language is a new generation of language that is focused on solving problems rather than bogging down into the details of how the computers work. High-level languages are closer to human languages. Thus, they are easier to learn. The instruction written in a high-level language is called a statement. For example, the following statement calculates the area of a rectangle:

area = 10 * 20

Here are some advantages of the High-level language:

  1. You don't need to spend years learning about various CPU architectures or instruction sets to write programs. All you need is the knowledge of the industry/domain you are working for.
  2. Programs written in High-level language are portable. That is, programs are not tied to a particular CPU.
  3. In early, days of computing, programmers were mathematicians or computer scientists. High-level languages have lowered the entry barrier so much that the people who are relatively new to computing can learn to program in a few weeks.

The programs written in high-level languages are called source code. As with the assembly language, the CPU doesn't have clue as to what the high-level language is. To run the program written in a high-level language, you still have to translate it to the machine code.

There are two types of programs available to do the translation:

  1. Compiler
  2. Interpreter

Compiler #


A compiler translates the high-level language to machine code, in one go. The machine code can then be executed by the CPU.

The following figure illustrates the working of a compiler.

[]

Once the machine code is generated, you can run it as many times as you want. After every modification, you will have to re-compile the program to generate the new machine code.

High-level Languages that use compiler to generate the machine code are called Compiled languages. The following are some examples of the compiled languages.

  1. C
  2. C++
  3. Java
  4. C#
  5. Go

Interpreter #


Unlike a compiler, an interpreter reads statements one by one, converts it to the machine code and executes it immediately. This process continues until the end of the file is reached.

Unlike a compiler, an interpreter reads one statement from the source code, translate it to the machine code and executes it immediately. In this way, an interpreter continues to read, translate and execute the statements one by one until the end of the file is reached.

The following figure illustrates the working of an interpreter.

[]

High-level Languages that use an interpreter to generate the machine code are called Interpreted languages. The following are some examples of compiled languages.

  1. Python
  2. PHP
  3. JavaScript
  4. Ruby
  5. Perl

Python is an interpreted language #


Yes, Python is interpreted language, when you run a Python program an interpreter will translate and execute the source code on line by line basis, as compared to compiled languages like C or C++, where compiler translates all statements at once.

Now you might ask, so what's the difference?

The difference is that the interpreted languages are little bit slow as compared to compiled languages. Yes, you will definitely get some performance benefits if you write your code in compiled languages like C or C++, but writing codes in such languages is a daunting task for a beginner. Furthermore, in such languages, you need to write even most basic functions like calculating the length of the array, splitting the string, finding substring in a string and so on. For more advanced tasks sometimes you need to roll out your own data structures (referred to the way data is organized in the system) to encapsulate data in the program. Hence, in C/C++ before you actually start solving your business problem you need to take care of all the gritty details. This is where Python steps in. In Python, you don't need to define any data structure, no need to define small utility functions because Python has everything to get you started.

Moreover, Python has hundreds of libraries available at https://pypi.python.org/ which you can use in your project without reinventing the wheel.

Python is Dynamically Typed #


Python doesn't require you to define variable data type ahead of time. Python automatically infers the type of the variable based on the type of value it contains. For example:

myvar = "Hello Python"

The above line of code assigns string "Hello Python" to the variable myvar, so the type of myvar is string.

Note that unlike languages like C, C++ and Java, in Python you do not need to end a statement with a semicolon (;).

Suppose, a little bit later in the program we assign variable myvar a value of 1, that is:

myvar = 1

Now, the myvar variable is of type int.

We discuss all about variables in chapter Variables in Python.

Python is strongly typed #


If you have programmed in languages like PHP or JavaScript. You may have noticed that they both convert data of one type to another automatically. Such languages are called weakly typed language.

For example, consider the following JavaScript code:

1 + "2"

Expected Output:

'12'

Here before the addition (+) is carried out, integer 1 will be converted to a string "1" and concatenated to "2", which results in a string containing "12". However, In Python, such automatic conversions are not allowed, so the following code will produce an error.

1 + "2"

Try it out:

# run this code to see the error
1 + "2"


Write less code and do more #


Programs written in Python are usually 1/3 or 1/5 of the Java code. It means, we can write less code in Python to achieve the same thing as in Java.

To read a file in Python you only need 2 lines of code:

1
2
with open("myfile.txt") as f:
   print(f.read())

Try it out:

# these two lines create a file "myfile.txt" with data "Learning Python"
with open("myfile.txt", "w") as f:
   f.write("Learning Python")


# these two lines read data from myfile.txt
with open("myfile.txt") as f:
   print(f.read())


Don't pay much attention to the commands being used to read and write the file. We will learn all about that in the upcoming chapters. In case, you are dying to know how to read and write files, consider jumping to chapter Reading and writing files in Python.

Should I learn Python 2 or Python 3? #


If you are just starting out you might have heard that there are two major versions of Python: Python 2 and Python 3. You might also be confused whether you should learn Python 2 or 3.

Just to clear things up, Python 2 is a thing of the past and Python 3 is the future. Python 3 was introduced to address the shortcomings of Python 2 and now all the development is happening in Python 3.

If you have some background with Python 2 then you already know Python 3 to some extent. Also keep in mind that Python 3 is not backward compatible with Python 2. In other words, you can't run a Python 3 program using a Python 2 interpreter.

This tutorial is all about Python 3, but we will point out major differences wherever necessary.

Who uses Python? #


Python is used by many large organizations like Google, NASA, Quora, HortonWorks and many others.

In the next post, we will learn how to install Python.


Other Tutorials (Sponsors)