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.
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.
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
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.
- 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
Spacesto move up or down the indentation level. In the code below, a
pipe or |in line
We must not mix
Spaces and use only one of them for indentation. We must be consistent in this regard.
We say bye-bye to
bracesin Python and instead use
colons(:)to achieve similar tasks. We also do not have a compulsory
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
passwhich is very similar to an empty
;with respect to
Declaring a variable is as easy as thinking about a name and assigning it a value using
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.
- Single Line Comments can be written using
#symbol while Multi-line Comments can be written using
- 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.
- Conditional statements in
Pythonare quite similar to other languages following a
- Printing on console is as easy as using a
arraylike data-structure called
listin 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.