Posts

Showing posts from 2019

Dictionary in Python

# how to create blank dictionary dictionary = {} print(dictionary) # declare dictionary with key and value dictionary = {"fname": "avni","mname":"singh","lname":"rathor"} print(dictionary) # accessing key and value from dictionary for key in dictionary:     print(key)     print(dictionary[key]) # accessing key and value with items method for key,value in dictionary.items():     print(key,value) #  value can also be fetched from get method print(dictionary["fname"]) print(dictionary.get("fname")) # now will add key and value in dictionary . dictionary["age"] = "31" print(dictionary) # now removing item fname  from dictionary dictionary.pop("fname") print(dictionary) # del keyword can also be used del del dictionary["lname"] print(dictionary) # can clear dictionary with clear function dictionary.clear() print(dictionary) # dict constructior

list in python

# declaring a list list_t = [ "my" , "name" , "is" , "abhishek" ] print (list_t) # accessing first list item print (list_t[ 0 ]) # accessing all item of list for x in list_t: print (x) # inserting item at position in list . list_t.insert( 0 , "hello" ) print (list_t) # extending a list lastname = [ "rathor" ] list_t.extend(lastname) print (list_t) # removing item from list list_t.pop( 0 ) print (list_t) list_t.remove( "rathor" ) print (list_t) # index of item in list print (list_t.index( 'name' )) # sorting an index list_t.sort() print (list_t) # reverse is also possible in index list_t.reverse() print (list_t) # constructing a list with constructor list2 = list (( "abhishek" , "pratap" , "singh" , "rathor" )) print (list2) # tuple and list both behave same except tuple are unchangeable and ordered collection # so inserting and removing