Sunday, October 22, 2017

set in python

#set doesnt have index , it contains unique values

>>> var
set([3, 1, 2, 'john'])

use update method to add multiple item to set
>>> var.update({5,6})
>>> var
set([1, 2, 3, 5, 6, 'john'])

use add to add single item to set
>>> var.add(9)
>>> var
set([1, 2, 3, 5, 6, 7, 9, 'john'])

remove item with 9 but gives error if 9 doesnt exist
>>> var.remove(9)
>>> var
set([1, 2, 3, 5, 6, 7, 'john'])

Use discard to delete 8 from set and it wont spit out error if it doesnt exist
var.discard(8)


# gives union of two sets
set1 | set 2
OR
set1.union(set2)

# gives difference
set1 - set2
OR
set1.difference(set2)

# things that is uniqe to a set
set1 ^ set2
or
set1.symmetric_difference(set2)
example:

>>> cd1={1,2,3,4}
>>> cd2={2,3,5,6}
>>> cd1 ^ cd2
set([1, 4, 5, 6])


# gives intersection
set1 & set2
set1.intersection(set2)

Enumerate in python

Enumerate function takes ordered iterable like string,list,tuple and gives items and its index

 list(enumerate("oranges"))
>>> [(0, 'o'), (1, 'r'), (2, 'a'), (3, 'n'), (4, 'g'), (5, 'e'), (6, 's')]

#!/usr/bin/python3
def main():

    for i,j in list(enumerate("oranges")):
        print(i,j)

if __name__ == "__main__":
    main()

#output
0 o
1 r
2 a
3 n
4 g
5 e
6 s




Tuples

>>> tup=(1,"apple",["jim","sammy"])
>>> tup
(1, 'apple', ['jim', 'sammy'])

# Tuples is immutable / unchangeable
>>> tup[0]=1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

#The list inside the tuple can be changed though!!

>>> tup[2][1]="tommy"
>>> tup
(1, 'apple', ['jim', 'tommy'])




   

python list and dictionary

#tutorial blog after tihar oct 22/kartik 5

To convert string to a list use split() method
"this is string".split()
>>> "this-is-string".split("-")
['this', 'is', 'string']


join the strings in a list
>>> "".join(["sdf","asdf"])
'sdfasdf'


>>> mylist
['urgen', 'sherpa', 'is', 'persons', 'name', 'w', 'a', 't']
>>> mylist[-3:]=["".join(mylist[-3:])]
>>> mylist
['urgen', 'sherpa', 'is', 'persons', 'name', 'wat']



mydict={"fname":"john","lname":"doe"}
To change keyvalue for key "lname" to "Foe"
and add extra key value - "age"=21

mydict.update({"lname":"Foe","age":21})

result: >>> {'fname': 'john', 'age': 21, 'lname': 'Foe'}

Packing and Unpacking the dictionaries

def main():

    def packer(**kwargs):
        print(kwargs)
     
    def unpacker(fname=None, lname=None):
        if fname and lname:
            print("{} {}".format(fname,lname))
        else:
            print("missing fname or lname")
         
         
    packer(name="urgen",age=22)
    unpacker(**{"fname":"kenneth","lname":"love"})
 
if __name__ ==  "__main__":
    main()
   

def main():

    var={"fname":"urgne","lname":"sherpa","address":"ktm"}
    print("#keys using method 1")
    for k in var:
        #returns the keys
        print(k)
    #keys
    print("#keys using method 2")  

    for k in var.keys():
        #return keys
        print(k)
    #values
    print("#values")
    for k in var.values():
        #returns the key-value individual tuple item
        print(k)
    print("#key and values")
    for i in var.items():
        print(i)
   
if __name__ ==  "__main__":
    main()