Wednesday, December 19, 2018

Python Ninja Bootcamp 38-Q&A 6th


Learn Python like a Professional! Take You from 0 to Hero






In this Python lecture, we will learn about the Q&A 6th.


Answer the following Question-

    Q. Write one line of code to return the prime numbers between 0 to 100 by using map function and filter function.

     

    print(*list(filter(lambda num:num ,list(map(lambda num:num if all(num%i!=0 for i in range(2,num)) else None,range(2,101))))),sep=',')
    
    >>2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97


    Q. Write a code to aggregate each word and its length in the given sentence-'Welcome to Python Ninja Bootcamp' by using map function and zip function.


    l='Welcome to Python Ninja Bootcamp'.split(' ')
    print(tuple(zip(l,list(map(len,l)))))
    
    
    
    >>(('Welcome', 7), ('to', 2), ('Python', 6), ('Ninja', 5), ('Bootcamp', 8))


    Q. Write code to tag counter (1-26) to the alphabets(a-z) by using enumerate function


    Ist Method
    import string 
    d={}
    for counter,value in enumerate(list(string.ascii_lowercase)):
         d[counter +1]=value
            
    print(d)
    
    >>{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
    
    

    IInd method
    import string 
    
    print(dict(enumerate(string.ascii_lowercase,1)))
    
    >>{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
    
    

    EDITS ARE WELCOMED!!

    In the next Blog, we will discuss Python Module.  

    https://sngurukuls247.blogspot.com/2018/12/python-ninja-bootcamp-39-counter.html

    ......................................................................................................................................

    Follow the link below to access Free Python Lectures-
    https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnw

    Instagram-
    https://www.instagram.com/python.india/


    View the Jupyter Notebook for this lecture

    Download the Jupyter Notebook for this lecture 




    Feel free contact me on-


    No comments:

    Post a Comment