Question 3 : Create a function with the inner function to return the addition and subtraction of the values passed as arguments.
def calculations(x, y):
def addition(a, b):
return a + b
def subtraction(a, b):
return a - b
return addition(x, y), subtraction(x, y)
add, sub = calculations(4, 6)
print("Addition =", add)
print("Subtraction =", sub)
Output:
Addition = 10
Subtraction = -2
Question 4 : Create a function named “calculation” to print the addition of two numbers. Call the same function again using the name “addition”.
def calculations(x, y):
def addition(a, b):
return a + b
def subtraction(a, b):
return a - b
return addition(x, y), subtraction(x, y)
add, sub = calculations(4, 6)
print("Addition =", add)
print("Subtraction =", sub)
Output:
Addition = 10
Subtraction = -2
Question 5 : Write a function in Python to check whether the string is pangram or not?
def is_pangram(str):
alphabets = set('abcdefghijklmnopqrstuvwxyz')
return alphabets <= set(str.lower())
print(is_pangram('The quick brown fox jumps over the lazy dog'))
print(is_pangram('The quick brown fox jumps over the lazy'))
Output:
True
False
Question 6 : Write a function in Python to sort the words of the sentence in the alphabetical order.
def sort_string(str):
items = [s for s in str.split()]
items.sort()
sorted_str = ' '.join(items)
return(sorted_str)
srt = 'abcd az acd bo ba'
sort_string(srt)
Output:
'abcd acd az ba bo'
Question 7 : Write a function in python to check whether the string is the Palindrome or not.
Question 8 : Write a function in Python to calculate the alphabets and digits from the string.
def count_alpha_digit(str):
dict = {'alphabet' : 0, 'digit' : 0}
for s in str:
if s.isalpha():
dict['alphabet'] += 1
elif s.isdigit():
dict['digit'] += 1
else:
pass
return dict
print(count_alpha_digit('Logical Python1 0101'))
Output:
{'alphabet': 13, 'digit': 5}
Question 9 : Write a function in Python to calculate the frequency of each word in the string.
def count_frequency(str):
frequency = {}
for s in str.split():
frequency[s] = frequency.get(s, 0) + 1
return frequency
str = "This the just the the the frequency test test of the string"
print(count_frequency(str))