Python打卡Day10

Question 31

Question:

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.


Hints:

1Use dict[key]=value pattern to put entry into a dictionary.Use ** operator to get power of a number.Use range() for loops.

Main Author's Solution: Python 2

1def printDict():
2	d=dict()
3	for i in range(1,21):
4		d[i]=i**2
5	print d
6
7printDict()

My Solution: Python 3

1def printDict():
2    dict={i:i**2 for i in range(1,21)}   # Using comprehension method and
3    print(dict)
4
5printDict()

Question 32

Question:

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.


Hints:

1Use 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.

Main Author's Solution: Python 2

1def printDict():
2	d=dict()
3	for i in range(1,21):
4		d[i]=i**2
5	for k in d.keys():
6		print k
7printDict()

My Solution: Python 3

1def printDict():
2    dict = {i: i**2 for i in range(1, 21)}
3    print(dict.keys())      # print keys of a dictionary
4
5printDict()

Question 33

Question:

Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included).


Hints:

1Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.

Main Author's Solution: Python 2

1def printList():
2	li=list()
3	for i in range(1,21):
4		li.append(i**2)
5	print li
6
7printList()

My Solution: Python 3

1def printList():
2    lst = [i ** 2 for i in range(1, 21)]
3    print(lst)
4
5printList()

Question 34

Question:

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.


Hints:

1Use ** 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

Main Author's Solution: Python 2

1def printList():
2	li=list()
3	for i in range(1,21):
4		li.append(i**2)
5	print li[:5]
6
7printList()

My Solution: Python 3

1def printList():
2    lst = [i ** 2 for i in range(1, 21)]
3
4    for i in range(5):
5        print(lst[i])
6
7printList()

1'''Solution by: popomaticbubble
2'''
3def squares(n):
4    squares_list = [i**2 for i in range(1,n+1)]
5    print(squares_list[0:5])
6squares(20)

1'''Solution by: yuan1z'''
2func = lambda :print([i**2 for i in range(1,21)][:5])

Question 35

Question:

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.


Hints:

1Use ** 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

Main Author's Solution: Python 2

1def printList():
2	li=list()
3	for i in range(1,21):
4		li.append(i**2)
5	print li[-5:]
6
7printList()

My Solution: Python 3

1def printList():
2    lst = [i ** 2 for i in range(1, 21)]
3    for i in range(19,14,-1):
4        print(lst[i])
5
6printList()

1'''Solution by: popomaticbubble
2'''
3def squares(n):
4    squares_list = [i**2 for i in range(1,n+1)]
5    print(squares_list[-5:])
6squares(20)

Question 36

Question:

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.


1Hints: 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

Main Author's Solution: Python 2

1def printList():
2	li=list()
3	for i in range(1,21):
4		li.append(i**2)
5	print li[5:]
6
7printList()

My Solution: Python 3

1def printList():
2    lst = [i ** 2 for i in range(1, 21)]
3    for i in range(5,20):
4        print(lst[i])
5
6printList()

Question 37

Question:

Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included).


Hints:

1Use ** 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.

Main Author's Solution: Python 2

1def printTuple():
2	li=list()
3	for i in range(1,21):
4		li.append(i**2)
5	print tuple(li)
6
7printTuple()

My Solution: Python 3

1def printTupple():
2    lst = [i ** 2 for i in range(1, 21)]
3    print(tuple(lst))
4
5printTupple()

1'''
2Solution by: Seawolf159
3'''
4def square_of_numbers():
5    return tuple(i ** 2 for i in range(1, 21))
6
7print(square_of_numbers())

Comment

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.

go to previous day

go to next day

Discussion