In [5]:
import math
def vol(rad):
return (4.0/3.0) * math.pi * rad**3
l_vol = lambda rad: (4.0/3.0) * math.pi * rad**3
In [6]:
l_vol(3)
Out[6]:
Write a function that checks whether a number is in a given range (Inclusive of high and low)
In [7]:
def ran_check(num,low,high):
return low <= num <= high
In [9]:
ran_check(4, 0, 10)
Out[9]:
In [10]:
ran_check(-20, 0, 100)
Out[10]:
In [11]:
ran_check(1, -2, -10)
Out[11]:
If you only wanted to return a boolean:
In [8]:
def ran_bool(num,low,high):
pass
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 [20]:
import collections
import string
def up_low(s):
c = collections.Counter(s)
lower = 0
upper = 0
for key, count in c.items():
if key in list(string.ascii_lowercase):
lower += count
elif key in list(string.ascii_uppercase):
upper += count
print("No. of Uppercase: {up}\nNo. of Lowercase: {low}".format(up=upper, low=lower))
In [21]:
up_low('Hello Mr. Rogers, how are you this fine Tuesday?')
In [19]:
import string
type(string.ascii_lowercase.split())
Out[19]:
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 [24]:
def unique_list(l):
return list(set(l))
In [25]:
unique_list([1,1,1,1,2,2,3,3,3,3,4,5])
Out[25]:
Write a Python function to multiply all the numbers in a list.
Sample List : [1, 2, 3, -4]
Expected Output : -24
In [33]:
def multiply(numbers):
total = 1
for n in numbers:
total *= n
return total
multi_lamb = reduce(lambda x, y: x*y, [1, 2, 3, -4])
def mult_mix(num):
return reduce(lambda x, y: x*y, num)
In [34]:
#multiply([1,2,3,-4])
# multi_lamb
mult_mix([1, 3, 4, 100])
Out[34]:
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 [37]:
def palindrome(s):
return s == s[::-1]
In [38]:
palindrome('helleh')
Out[38]:
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 [48]:
import string
import collections
def ispangram(str1, alphabet=string.ascii_lowercase):
strSet = list(set(str1))
for alpha in alphabet:
if strSet.count(alpha) >= 1:
continue
else:
return False
return True
def alt_ispanagram(str1, alphabet=string.ascii_lowercase):
alphaSet = set(alphabet)
strSet = set(str1)
return True if c.issubset(alphaSet) else False
In [47]:
#ispangram("The quick brown fox jumps over the lazy dog")
alt_ispanagram("The quick brown fox jumps over the lazy dog")
Out[47]:
In [23]:
string.ascii_lowercase
Out[23]: