-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-Functions and Methods Homework.py
182 lines (103 loc) · 2.85 KB
/
08-Functions and Methods Homework.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# coding: utf-8
# # Functions and Methods Homework
#
# Complete the following questions:
# ____
# **Write a function that computes the volume of a sphere given its radius.**
# <p>The volume of a sphere is given as $$\frac{4}{3} πr^3$$</p>
# In[21]:
import math
def vol(rad):
volume=1.3*(math.pi)*(3**rad)
return volume
# In[22]:
# Check
vol(2)
# ___
# **Write a function that checks whether a number is in a given range (inclusive of high and low)**
# In[42]:
def ran_check(num,low,high):
if num>low and num<high:
print("{} is in the range between {} and {}".format(num,low,high) )
# In[43]:
# Check
ran_check(5,2,7)
# If you only wanted to return a boolean:
# In[45]:
def ran_bool(num,low,high):
if num >low and num<high :
return True
else:
return False
pass
# In[47]:
ran_bool(7,1,10)
# ____
# **Write a Python function that accepts a string and calculates the number of upper case letters and lower case letters.**
#
# Sample String : 'Hello Mr. Rogers, how are you this fine Tuesday?'
# Expected Output :
# No. of Upper case characters : 4
# No. of Lower case Characters : 33
#
# HINT: Two string methods that might prove useful: **.isupper()** and **.islower()**
#
# If you feel ambitious, explore the Collections module to solve this problem!
# In[52]:
def up_low(s):
num=0
for letters in s:
if letters[0].isupper():
num=num+1
print(num)
pass
# In[53]:
s = 'Hello Mr. Rogers, how are you this fine Tuesday?'
up_low(s)
# ____
# **Write a Python function that takes a list and returns a new list with unique elements of the first list.**
#
# Sample List : [1,1,1,1,2,2,3,3,3,3,4,5]
# Unique List : [1, 2, 3, 4, 5]
# In[9]:
def unique_list(lst):
pass
# In[10]:
unique_list([1,1,1,1,2,2,3,3,3,3,4,5])
# ____
# **Write a Python function to multiply all the numbers in a list.**
#
# Sample List : [1, 2, 3, -4]
# Expected Output : -24
# In[11]:
def multiply(numbers):
pass
# In[12]:
multiply([1,2,3,-4])
# ____
# **Write a Python function that checks whether a passed in string is palindrome or not.**
#
# Note: A palindrome is word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.
# In[13]:
def palindrome(s):
pass
# In[14]:
palindrome('helleh')
# ____
# #### Hard:
#
# **Write a Python function to check whether a string is pangram or not.**
#
# Note : Pangrams are words or sentences containing every letter of the alphabet at least once.
# For example : "The quick brown fox jumps over the lazy dog"
#
# Hint: Look at the string module
# In[15]:
import string
def ispangram(str1, alphabet=string.ascii_lowercase):
pass
# In[16]:
ispangram("The quick brown fox jumps over the lazy dog")
# In[17]:
string.ascii_lowercase
# #### Great Job!