Functions
1. Write a function that computes the volume of a sphere given its radius.
The volume of a sphere is given as $$ \frac{4}{3} πr^3 $$
Answer
def vol(rad):
volume = (4/3)*3.14*rad*rad*rad
print(volume)
vol(2)
2. Write a function that checks whether a number is in a given range (inclusive of high and low)
Answer
def num_check(num,low,high):
if num in range(low,high+1):
print(f'{num} is in the range between {low} and {high}')
else:
print(f'{num} is out of range')
num_check(5,2,7)
To return only boolean value
def num_bool(num,low,high):
return num in range(low,high+1)
num_bool(5,2,7)
3. 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]
4. Write a Python function to multiply all the numbers in a list.
Sample List : [1, 2, 3, -4]
Expected Output : -24
5. 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()