Saturday, 10 February 2018

Python Dictionaries part1 - Day 7


  • Dictionaries store elements in key value pair,we can not append values to dictionary through any function or method ,we can assign using the key, functions like get,clear,del works with dictionary
  •   
    #dictionary can not be accessed vy index,but through key value
    films = {"muthu":"one of the movie in which rajini comes in double role",
             "basha":"Don of a don movie,a cult hit",
             "enthiran":"rajini's scifi movie",
             "murattu kalai":"movie taken in paganeri"}
    print(films)
    print(films["murattu kalai"])
    #to add a new value to dictionay we dont have a method or function,but we can assign like the below one
    films["kaala"]="more like a basha 2,april 27th - 2018 release"
    print(films)
    #if we use the same key and assign it will update instead of creating new element
    films["kaala"]="probably thalaivars gonna be biggest hit"
    print(films)
    #similarly it will take the last set of key,value combination if there is any duplicate
    films = {"muthu":"one of the movie in which rajini comes in double role",
             "basha":"Don of a don movie,a cult hit",
             "enthiran":"rajini's scifi movie",
             "murattu kalai":"movie taken in paganeri",
             "muthu":"one of the thalaivar & sarath babu combination movie"}
    print (films["muthu"])
    
    bike = {"make":"royal enfiled","model":"himalayan","cc":400,"review":"mostly avg or bad"}
    print (bike["model"])
    print (bike["cc"])
    #delete a element or delete a entire dictonary or cleare the elements in the dictionary
    del(bike["review"])
    # del(bike)
    # bike.clear()
    print (bike)
    #getting the unpresent key value will result in error,in that case we can use get function
    # print (bike["color"])
    print(bike.get("color"))
    #while True is used to make the user keep qasking the questions,unless he types quit
    # while True:
    #     x = input("enter any thalaivar's film name: ")
    #     if x == "quit":
    #         break
    #     description = films.get(x)
    #     print(description)
    #     #the below piece of code will return error if we enter any unknown key value
    #     print(films[x])
    
    #same code is rewritten to print the custom message if the value doesnot exists in dictory
    
    while True:
        x = input("enter any thalaivar's film name: ")
        if x == "quit":
            break
        if x in films:
            description = films.get(x)
            print(description)
            print(films[x])
        else:
            print ("{} - doesnot exists in dictionaryy".format(x))
     
    

    Python Handling Binary ,Hex and octal Numbers - Day 6(posted next day)


  • its better to have a basic idea about binary and hexadecimal nos,since the computers deal with them, here are the basics
  •   
    # #decimal numbers in binary
    # for i in range(10):
    #     print("{0:>2} in binary is {0:>8b}".format(i))
    #
    # print(0b1011)
    # #hex decimal
    # for i in range(257):
    #     print("{0:>2} in hex is {0:>02x}".format(i))
    #
    # #hex multipliation
    # x = 0x20
    # y = 0x0a
    # print(x*y)
    # #operations like or,and,xor,add,subtract refreshed
    
    #converting decimal to binary through program
    # print(10//2)
    # print(10%3)
    # powers = []
    # for power in range(15,-1,-1):
    #     powers.append(2**power)
    #     # print(powers)
    # print(powers)
    # x = int(input("enter any number less than 65535 to convert to binary \n"))
    # for i in powers:
    #     # print(i)
    #     print(x//i,end ='')
    #     x%=i
    #the above program will work ,but to avoid the trialing 0's can use the below code
    powers = []
    for power in range(15,-1,-1):
        powers.append(2**power)
        # print(powers)
    print(powers)
    printing = False
    x = int(input("enter any number less than 65535 to convert to binary \n"))
    for i in powers:
        # print(i)
        bit = x//i
        if bit!=0 or i ==1:
            printing =True
        if printing==True:
            print(bit,end ='')
        x%=i