Tuesday, March 21, 2017

5 things I find very useful and new in Python programming

1.  List - (a list of values. Each one of them is numbered, starting from zero )

I find it very handy in Python programming language , because looping through list and reading each values are very easy in List.


num = [3,4,5,6]

items = ['desk','pen','pencil']

you can read through List very easily as below -























2. split function


You can split a string with any seperator and put into a list very easily with this function.











3.Map function


Map applies a function to all the items in an input_list.



Output:

{1,4,9,16]






4. Reduce Function:

Reduce is a really useful function for performing some computation on a list and returning the result.

Below example we are passing the 'sum' function to inbuild 'reduce' function and getting the total sum for the list of numbers.




Output:
14





5.Some thing you find very strange in Python ( Mutable and Immutable objects)

I thought it was bug, but its not ..

eg:
First Lets me create a list as below.

a = [1,2]


then simply create another list 'b' and assign  'a' + [3]

b= a;
b+=[3]

what you will expect on 'b' .. obviously .. [1,2,3]

But what you expect on 'a'... I thought that i didn't change anything in 'a' , so it should be same, but what actually 'print a'  become [1,2,3]

read below link for more details - http://book.pythontips.com/en/latest/mutation.html

https://codehabitude.com/2013/12/24/python-objects-mutable-vs-immutable/

 










No comments: