In [2]:
import math
def vol(rad):
return 4/3*math.pi*rad**4
vol(5)
Out[2]:
In [5]:
l_vol = lambda rad: 4/3*math.pi*rad**4
l_vol(5)
Out[5]:
Write a function that checks whether a number is in a given range (Inclusive of high and low)
In [8]:
def ran_check(num,low,high):
if num>low and num<high:
print("it's in range!")
else:
print('it\'s out of range!')
ran_check(11,10,20)
If you only wanted to return a boolean:
In [11]:
def ran_bool(num,low,high):
if num>low and num<high:
return True
else:
return False
ran_bool(3,1,10)
Out[11]:
In [9]:
ran_bool(3,1,10)
Out[9]:
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 [14]:
def up_low(s):
upper = 0
lower = 0
for i in s:
if i.isupper() == True:
upper += 1
elif i.islower() == True:
lower += 1
else:
continue
print(upper)
print(lower)
up_low('Hello Mr. Rogers, how are you this fine Tuesday?')
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 [30]:
def unique_list(l):
u = []
for i in l:
if i not in u:
u.append(i)
print(u)
In [31]:
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 [34]:
def multiply(numbers):
quot = 0
quot = numbers[0]
for i in numbers:
quot = i*quot
return quot
In [35]:
multiply([1,2,3,-4])
Out[35]:
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 [49]:
def palindrome(s):
s.replace(' ','')
return s == s[::-1]
In [54]:
def palindrome(s):
x = 0
y = s[::-1]
for i in s:
if s[x] != y[x]:
return False
x += 1
return True
In [55]:
palindrome('hel leh')
Out[55]:
In [57]:
palindrome('this is cool')
Out[57]:
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 [63]:
import string
def ispangram(str1, alphabet=string.ascii_lowercase):
for i in alphabet:
if i not in str1:
return False
return True
In [64]:
ispangram("The quick brown fox jumps over the lazy dog")
Out[64]:
In [66]:
ispangram('i still dont know')
Out[66]:
In [23]:
string.ascii_lowercase
Out[23]:
In [62]:
b = {'a','b','c','c'}
b
Out[62]: