-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day_12.py
108 lines (82 loc) · 1.64 KB
/
Day_12.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# coding: utf-8
# # Question 44
#
# ### **Question:**
#
# > **_Write a program which can map() to make a list whose elements are square of numbers between 1 and 20 (both included)._**
#
# ---
#
# ### Hints:
#
# > **_Use map() to generate a list. Use lambda to define anonymous functions._**
#
# ---
#
#
#
# **Solutions:**
# In[1]:
def sqr(x):
return x * x
squaredNumbers = list(map(sqr, range(1, 21)))
print(squaredNumbers)
# ---
#
# # Question 45
#
# ### **Question:**
#
# > **_Define a class named American which has a static method called printNationality._**
#
# ---
#
# ### Hints:
#
# > **_Use @staticmethod decorator to define class static method.There are also two more methods.To know more, go to this [link](https://realpython.com/blog/python/instance-class-and-static-methods-demystified/)._**
#
# ---
#
#
#
# **Solutions:**
# In[2]:
class American:
@staticmethod
def printNationality():
print("I am American")
american = American()
american.printNationality() # this will not run if @staticmethod does not decorates the function.
# Because the class has no instance.
American.printNationality() # this will run even though the @staticmethod
# does not decorate printNationality()
# ---
#
# # Question 46
#
# ### **Question:**
#
# > **_Define a class named American and its subclass NewYorker._**
#
# ---
#
# ### Hints:
#
# > **Use class Subclass(ParentClass) to define a subclass.\***
#
# ---
#
#
#
# **Solutions:**
# In[3]:
class American:
pass
class NewYorker(American):
pass
american = American()
newyorker = NewYorker()
print(american)
print(newyorker)
# ---