Category Archives: Python

Python Notes-2

This is continuation from Python Notes-1. We were learning about python list
li = []
I really like the way python never need the variable to be declared. It is automatically assigned the data type based on it’s last initialization. e.g.

a = 9
a = 'q'

>>> a = 9
>>> type(a)

>>> a = 'q'
>>> type(a)

>>> a = []
>>> type(a)

As we know from last post that python string are immutable similarly we have another data structure in python called tuple which is similar to list but it is immutable.

var = ()
var = ('abc',3,4.8)

You can’t do var[0] = 3 also if there is only one element in the tuple it doesn’t make sense to write it like var = (2) or var = (x) as this will just mean var = 2 or var = x. So you have to do it like var = (2,) or var = (x,). One item tuple will need a trailing comma.

len() will give you length of a variable like
var = "12345" then len(var) will be 5.
Similarly var = [1,2,3,4,5] len(var)

Negative index This is very handy in accessing elements from last so you can say:

var = [1,2,3,4,5]
This will give you the last element of the list. This can be applied similarly to list or tuple
-2 will refer to second last element.


var = ['apple', 'ball', 'cat', 'dog']
>>> spam[:2]
['apple', 'ball']
>>> spam[1:]
['ball', 'cat', 'dog']

Reading from standard input

var = input()

var will be of str type. Also note that Python doesn’t have char data type.

var = int(input())
will try to convert what is read to int.

One important list method is append()
used extensibly in a loop to keep on adding things.

insert() method to insert at a particular index first argument is index second argument is what to insert. insert(indx,val)

li = [1, 2]
>>> li.append(4)
>>> li.insert(2,3)
>>> li
[1, 2, 3, 4]
>>> li.insert(2,3)
>>> li
[1, 2, 3, 3, 4]
>>> del(li[2])
>>> li
[1, 2, 3, 4]

A sample code

To be continued …


Python Notes – 1

I think python is best for any personal use (small scale development) prototyping ideas etc.

Some examples:
Like say I want to parse a document for some specific keywords and format the extracted data.
I want to get update when something on some website changes.
I want to teach a lesson to a spammer by sending him lots and lots of email.
For investing you want to make sense out of some stocks historic data. Using numpy and pandas. Using pandas reading data in meaningful way from a web resource is as easy as reading it from local file.
You want to download all the news paper resources (having a particular date format) from an internet location

All above are my actual use cases to give you an idea.Apart from this I think Python is made for interviews it is very simple to write algorithms or create a data structure in Python. (I guess)

That said now let us get to work and learn some python you will find many online resources really good resources or you can join open/free courses from coursera or similar sites. If you don’t want all this you can right away jump into coding and google(basically stack overflow) your requirements as it arises. (given that you have prior experience with programming and at least one language). This may be problamatic as it will not teach you to code in the Pythonic way. But you can cover that later by browsing some existing good resources/libraries.

Lots of talking now let us get back to work the really boring stuff in language learning syntax.

Note: The notes are all personal it is not meant to cover everything in the language. I am biased towards whatever interested me more or whatever was new for me.

** exponent
Example: 2**3 = 8 = 2 ^ 3

// Integer division/floored quotient

5//2 = 2 % gives you remainder as usual

List in python are object same as string although string are immutable similar to java string.

I don’t know why I assumed that python list should be like C++ STL vector. This was a shock to me when I got to know that python list are object just like any array in Java or C++. So if you pass it in a function which is modifying it it will be modified in caller also.
I was thinking may be in Python as long as possible things will be copied and passed around.

To be continued…