Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys.
Use dict[key]=value pattern to put entry into a dictionary.Use ** operator to get power of a number.Use range() for loops.
Solutions:
def printDict():
dict = {i: i ** 2 for i in range(1, 21)} # Using comprehension method and
print(dict)
printDict()
Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only.
Use dict[key]=value pattern to put entry into a dictionary.Use ** operator to get power of a number.Use range() for loops.Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.
Solutions:
def printDict():
dict = {i: i ** 2 for i in range(1, 21)}
print(dict.keys()) # print keys of a dictionary
printDict()
def printList():
lst = [i ** 2 for i in range(1, 21)]
print(lst)
printList()
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list.
Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use [n1:n2] to slice a list
Solutions:
def printList():
lst = [i ** 2 for i in range(1, 21)]
for i in range(5):
print(lst[i])
printList()
"""Solution by: yuan1z && edited by: apurvmishra99"""
func = lambda: ([i ** 2 for i in range(1, 21)][:5])
print(*(func()), sep="\n")
1 4 9 16 25
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list.
Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use [n1:n2] to slice a list
Solutions:
def printList():
lst = [i ** 2 for i in range(1, 21)]
for i in range(19, 14, -1):
print(lst[i])
printList()
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print all values except the first 5 elements in the list.
Hints: Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use [n1:n2] to slice a list
Solutions:
def printList():
lst = [i ** 2 for i in range(1, 21)]
for i in range(5, 20):
print(lst[i])
printList()
Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included).
Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use tuple() to get a tuple from a list.
Solutions:
def printTupple():
lst = [i ** 2 for i in range(1, 21)]
print(tuple(lst))
printTupple()
"""
Solution by: Seawolf159
"""
def square_of_numbers():
return tuple(i ** 2 for i in range(1, 21))
print(square_of_numbers())
Problems of this section is very much easy and all of those are of a modification of same type problem which mainly focused on using some commonly used function works with list,dictionary, tupple.In my entire solutions, I havn't tried to solve problems in efficient way.Rather I tried to solve in a different way that I can.This will help a beginner to know how simplest problems can be solved in different ways.