Monthly Archives: December 2016

Python Notes-3

In continuation from Python Notes-2
Python Notes-1

We will learn about python string as you know string in python are immutable all function which applies to string will eventually not modify the string but will return a str e.g.

var = 'ab'
can’t change str but returns another string

sometime I just forget this and assume that my original variable is changed to all upper, silly mistake.

var = var.lower()

Count number of times a value is appearing in str or tuple or list
Index of a value

To include special characters in a string you can escape using \ for or just use another for quotation then you can use one inside without escaping.
If you want to be more specific you can place r before the beginning quotation mark of a string to make it a raw string. A raw string completely ignores all escape characters and prints any backslash that appears in the string. e.g.

>>> print(r'This!! This is me \' " cat.')
This!! This is me \' " cat.

Multiline Strings with Triple Quotes

print('''Dear Apple,

You are so sweet.


Dear Apple,

You are so sweet.


After reading about str, list and tuple I wanted to know why use tuple? Why another data structure?

Seems like python uses tuple behind our back when a function has multiple return type. Even when it is not specifically mentioned.
Also since tuple data structure is restrictive it is much faster compared to list. There is one more benefit which relates to code understanding if you use tuple data type in your code for variables which doesn’t need to be changed. It will be known to reader without you telling specifically.

Just like string concatenation list and tuple can also be concatenated. All three cal be multiplied/replicated

t1 = (1,2)
t2 = (3,4)
t3 = t1+t2


t4 = t1*4

To be continued …


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…

Reverse engineering android code

I never guessed reverse engineering will be so easy. Is it true for all java code or just android?
For android apps decompilation overall is so simple. Although don’t bank on understanding the code as the app providers make their code cryptic most probably due to this reason(the process is called obfuscation).

That being said you can decompile almost every app on android. It is just vagueness of code which is going to save you(for the app developers) and make life tough for the hackers.

So basically .apk is nothing but zipped version of all your resources and compiled classes. You can extract all this from .apk the use tool like
or even simpler use some online website which does so.

java decompilation is also so easy I guess till Java 1.5? Stack Overflow link.

Finding all prime numbers

Till now I just knew only one way of finding all primes i.e. Sieve of Eratosthenes or space optimized version of the same called segmented sieve or time optimized version called Euler’s Sieve.

All of above work on same fundamentals striking out composite numbers from the beginning and what is remaining are the primes.

Recently I came across another way of finding primes the process is called Sieve of Sundaram.

  • You start with all the integers from 2 to n
  • From the above list remove all numbers which are of the form i + j + 2 * i*j

    1<= i <= j

    i + j + 2 *i * j <= N
  • What is remaining is doubled and incremented by 1 to give all the primes except 2. So basically all odd primes.

Example working:

Let us start with (n = 7) i.e. 1, 2, 3, 4, 5, 6, 7
In first step let us keep i = 1 and j = 1, 2, .. so cross out 1 + 1 + 2*1*1 (=4), 1 + 2 + 2 * 2* 1 (=6) take i = 2 then all are greater than 7 so not needed.
Remaining numbers are 1, 2, 3, 5, 7 So from Sieve of Sundaram all odd primes are:
1*2+1, 2*2+1, 3*2+1 = 3, 5, 7 add 2 and you have your result(all the primes in 1 to n).