Friday, July 18, 2014

django - Overview of Python

Python is an open source and object oriented language for developing web applications fast.  It is powerful, friendly and easy to learn.  It is platform independent and runs anywhere and in any browser.

Python comes with an command-line interpreter where you can play around and check many of its features.  I will give few examples here about how you can use this interpreter.

Open the DOS window and at the command prompt type 'Python' to activate the interpreter.  Then let us try the following simple mathematical things shown in this screen:



Note that we have tried simple things like addition, multiplication, subtraction and division.  The '#' symbol above indicates Python's comment.  Which means that, anything after '#' is ignored by the interpreter.

If you observe closely, the interpreter displays the values immediately after each command.  And when we tried to divide 15 by 6, technically the answer should be 2.5.  But the division operator returned only 2.

It means that, Python considers any number as a whole integer unless specified by the decimals.  Thus it returns the answer in integers only.  So if any number is having a decimal value, then it is returned as an actual division.

In other words, using any of the following returns the correct value 2.5:

>>> 15.0 / 6 
2.5
>>> 15 / 6.0
2.5
>>> 15.0 / 6.0 
2.5

Now, if you want to know what the remainder value is when you do a division, you can use the '%' operator as follows:

>>> 15 % 6
3
>>> 6 % 3
0

Note that when 15 is divided by 6, we get 3 as the remainder and while we divide 6 by 3, we get 0.

Like in any other language, variables can be defined and assigned values in Python too.  But, unlike those languages, you need not specifically declare a data type for the variables.  Python considers the data type based on the value assigned to the variable.  Also, Python is case-sensitive.  It considers the variables 'a' and 'A' as different.

For example, if you assign a integer value to the variable:

>>> a = 10

Then, Python assumes that 'a' is of data type integer.

Else if,

>>> a = "django"

Then, Python assumes that 'a' is of string data type.

Note that, for string values or at other places, you can use either single quotes (') or double quotes (") but not both.  That means, if you start with a single quote then end with the single quote and vice versa:

>>> a = "django" # correct
>>> a = 'django' # correct
>>> a = "django' # incorrect

The following is termed as an integer array:

>>> a = [1, 2, 3, 4, 5]

And the following is termed as a string array:

>>> a = ["django", "python", "pip"]

Like with arrays in other programming languages, the index of an array starts with 0.  But Python has another distinct feature.  The right-most value in the array is represented by negative index value and moving up along to the left-most value.

For example, if we have 

>>> a = [1, 2, 3, 4, 5]

Then

>>> a[0]
1
>>> a[1]
2
>>> a[2]
3

And

>>> a[-1]
5
>>> a[-2]
4
>>> a[-3]
3

Now, you must be having an idea of what this means and how it works.

Python has an in-built method 'len' that returns the length of the string variable or the number of items in an array depending on which variable we used.

So if 

>>> a = "django"

Then

>>> len(a)
6

And if 

>>> a = [1, 2, 3, 4, 5]

Then 

>>> len(a)
5

If you want to add any item/ value at the end of the list, you can use the 'append' method.  

>>> a.append(6)

>>> a
[1, 2, 3, 4, 5, 6]

But if you want to add any item/ value in the middle of the list, you use the 'insert' method where you can specify the index value of position you want the new item to be.

>>> a.insert(3, 7) # index position is 3 and value is 7

>>> a
[1, 2, 3, 7, 4, 5, 6]

In order to remove any item from the list, you can use the 'pop' method.  By default, it removes the last item in the list.  But if you specify the index value of position for any item, that item will be removed.

>>> a.pop()
6

>>> a
[1, 2, 3, 7, 4, 5]

>>> a.pop(3)
7

>>> a
[1, 2, 3, 4, 5]

Next, Python has a method called 'print' to print out any values to the console.

>>> print("Hello, world!")
'Hello, world!'

Note that, Python always displays the string values in single quotes (')

There is a statement 'for...in' which you can use to loop through the values in an array variable or characters in a string variable.

Python is different from other languages such that there are no curly brackets ({..}) like in Java or C# to indicate begin and end of the block or statements.  It does not have semi-colons (;) to indicate the end of the statement etc.

So, how does Python keep track of the blocks of the code?  

Python uses indentation (spaces) to separate sections of the block and keeps track of the code that way.  And it uses colon (:) to declare the classes or methods or even for the statements like 'for...in'.

The following screen shot shows how this is used:

 

Notice that, when you type in ':' and press Enter key, the prompt is changed from '>>>' to '...'.  This means that the interpreter is waiting for you to continue with the code.  Now, the 'print' command is followed after few spaces to define the indentation for Python that 'print' belongs to 'for...in' block.  The number of spaces can be anything like 2, 3, 4, 5 etc. but, all the subsequent code statements that belong to 'for...in' block should have the same indentation or number of spaces.

When you are done with the code, press Enter again twice until the results are displayed and '>>>' prompt comes back.

Same with using an array, you can display the contents of the string as shown below:



Now you are getting a grasp of how Python interpreter works and what methods or functions are available, we will cover more in the next article.

No comments:

Post a Comment