1. What is a dictionary in Python? A python dictionary is something I have never seen in other languages like C++ or Java programming. It holds key-value pairs. >>> roots={25:5,16:4,9:3,4:2,1:1} >>> type(roots) - Output: <class ‘dict’> >>> roots[9] - Output: 3 A dictionary is mutable, and we can also use a comprehension to create it. >>> roots={x**2 for x in range(5,0,-1)} >>> roots {25: 5, 16: 4, 9: 3, 4: 2, 1: 1} 2. Explain the //, %, and ** operators in Python. The // operator performs floor division. It will return the integer part of the result on division. >>> 7//2 - Output: 3 Normal division would return 3.5 here. Similarly, ** performs exponentiation. a**b returns the value of a raised to the power b. >>> 2**10 - Output: 1024 Finally, % is for modulus. This gives us the value left after the highest achievable division. >>> 13%7 - Output: 6 >...
Comments
Post a Comment