Functions and Methods Homework
Complete the following questions:
Write a function that computes the volume of a sphere given its radius.
volume = 4/3 pi r 3
In [2]: def vol(rad):
a=4/3*31.4*rad**3
print(a)
vol(7)
14360.266666666665
Write a function that checks whether a number is in a given range (Inclusive of high and low)
In [3]: def ran_check(num,low,high):
if num<low:
print("low")
elif num>=low and num<=high:
print("Inclusive")
else:
print("high")
ran_check(7,1,10)
Inclusive
If you only wanted to return a boolean:
In [4]: def ran_bool(num,low,high):
if num<low:
print(False)
else:
print(True)
ran_bool(3,1,10)
True
Write a Python function that accepts a string and calculate 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
If you feel ambitious, explore the Collections module to solve this problem!
In [5]: def up_low(s):
# code here
count=0
count1=0
for i in range(len(s)):
if 65<=ord(s[i])<=90:
count+=1
elif 97<=ord(s[i])<=122:
count1+=1
print("Sample String: 'Hello Mr.Rogers, how are you this fine Tuesday?'")
print("Expected output")
print("No. of Upper case characters :",count)
print("No. of Lower case Characters :",count1)
up_low("Hello Mr. Rogers, how are you this fine Tuesday?'")
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
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 [6]: def unique_list(l):
ab=set(l)
ac=list(ab)
print(ac)
unique_list([1,1,1,1,2,2,3,3,3,3,4,5])
[1, 2, 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 [7]: def multiply(numbers):
sum=1
for i in numbers:
sum*=i
print(sum)
multiply([1,2,3,-4])
-24
Write a Python function that checks whether a passed 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 [8]: def palindrome(s):
# code here
ab=s
if s==ab[::-1]:
print(True)
else:
print(False)
palindrome("helleh")
True
Given a number N.Find Sum of 1 to N Using Recursion
In [18]: def summation(n):
sum=0
for i in range(n+1):
sum+=i
return sum
n = int(input())
summation(n)
55
Out[18]:
Define a function which can generate and print a list where the values are
square of numbers between 1 and 20
In [20]: def printList():
b=[]
for i in range(1,21):
b.append(i*i)
print(b)
printList()
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]
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.
In [15]: def printDict():
d={}
for i in range(1,21):
d[i]=i*i
c=d.keys()
print(c)
printDict()
dict_keys([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
Write a function that count the no of characters of the given input text
input
innomatics research labs
output
innomatics :10
research : 8
labs : 4
In [16]: def count():
a="innomatics research labs"
b=a.split(" ")
for i in b:
print(i,":",len(i))
count()
innomatics : 10
research : 8
labs : 4
Write a program which can map() to make a list whose elements are
square of elements in [1,2,3,4,5,6,7,8,9,10].
Using map() function
In [26]: li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
SquaredNumbers = map(lambda a: a*a,li)
print(list(SquaredNumbers))
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Write a program which can map() and filter() to make a list whose
elements are square of even number in [1,2,3,4,5,6,7,8,9,10].
using filter()
In [48]: def even(x):
b=map(lambda a:a%2==0,x)
d=list(b)
return d
def square(x):
b = map(lambda a: a*a,x)
c=list(filter(lambda num:num%2==0,b))
return c
li=square([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(list(li))
[4, 16, 36, 64, 100]
Write a program which can filter() to make a list whose elements are even
number between 1 and 20 (both included)
In [3]: # CODE HERE
def even(x):
#code here
evenNumbers = filter(even(range(1, 21)))
print(list(evenNumbers))
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
In [75]: def even(x):
c=filter(lambda a:a%2==0,x)
d=list(c)
return d
even(range(1,21))
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Out[75]:
f (n) = f (n − 1) + f (n − 2) if n>1
In [41]: def f(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return f(n - 1) + f(n - 2)
n = int(input("Enter n value"))
print(f(n))
13
Innomatics Research Labs
www.innomatics.in
In [ ]: