Hello, Python!

Inroduction

Python is a high level, general purpose and object oriented programming language which is known for its simplicity, code readability and easy to learn syntaxes.

Python, at this moment is available in two versions, Python 2 and Python 3. Both of these versions are different and a program/script/application written in Python 3 is not guaranteed to run using Python 2. If you have just started, you must learn Python 3 is the latest and support for Python 2 will be stopped in the coming years.

Code Examples

Without wasting our time in talking about its history, features and, comparing and contrasting it with different programming languages, let us see some simple code examples.

Do not try to understand each and every line of all the examples given here. Try to read the code and click on the corresponding output tab to see the expected output. Skip those parts which you don't understand. We will revisit them all.

If you want to run these programs and do not have Python 3 installed in your system, go to ideone.com, select Python 3 as your language. Copy the code written in the examples and paste in the box provided. Supply input in the input box, if any, and then click Run.

1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
      

Sum of all the numbers in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is 55
     

3
7
11
     

2
1.0
4
6
8
3.0
     

Key Takeaways


Below are a few important points we can extract out of the examples. While you read them, do not hesitate to look back and forth in the examples.

Do not try to remember these points. You can always come back here and refer the article. By the way, why remember if you can refer.


  1. Python is an indentation based language. If we consider the leftmost point as 1st level of indentation, the level increases as we move towards right. We usually use Tabs or Spaces to move up or down the indentation level. In the code below, a pipe or | in line x shows x-th indentation-level.

We must not mix Tabs or Spaces and use only one of them for indentation. We must be consistent in this regard.

  1. We say bye-bye to semi-colons and braces in Python and instead use new lines, indentations and colons(:) to achieve similar tasks. We also do not have a compulsory main method.

  2. If we don't know or we have no-idea or we just don't want to write anything inside a block, we can simply use pass which is very similar to an empty ; with respect to C++ like languages.

  3. Declaring a variable is as easy as thinking about a name and assigning it a value using = sign.

We do not need to specify the type of a variable. The same is taken care of by Python. This does not mean that variables do not have a type. This simply means that we do not have to mention it and Python is able to detect them automatically.

  1. Single Line Comments can be written using # symbol while Multi-line Comments can be written using """comments""" or '''comments''' syntaxes.
  1. Iterating through a container or data-structure is easy as shown below.

With respect to C++/Java like languages, we can think of it as a variable named dummy_variable being assigned a value contained at current index of container during iteration.

  1. Conditional statements in Python are quite similar to other languages following a if-elif-else pattern.
  1. Printing on console is as easy as using a print function and passing a few arguments to it.
  1. An array like data-structure called list in Python can be created using square backets []. These list can contain any type of data structure, variables or numbers. Each item of a list are separated are comma (,). You can also see an extra (optional) comma after the last list item and the same does not result in any error.

A list can be iterated using for loops as shown in code examples.

Author image
New Delhi
I am a passionate programmer based in New Delhi, India. I mostly work with Python. Apart from work, I find myself doing poetry and learning about various languages. I prefer chai over coffee.