Python Functions Exercise and Solutions


Question 1 : How to create a function with variable length of arguments? And print each value in the function.

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

def func(*args):
    for i in args:
        print(i)
        
print("First Call")
func(40,50)

print("\nSecond Call")
func(10,20,30)

Output:

First Call
40
50

Second Call
10
20
30

[/bg_collapse]


Question 2 : Return multiple values from a function(return addition, subtraction, multiplication and division)

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

def calc(x, y):
    addition = x+y
    subtraction = x-y
    multiplication = x*y
    division = x/y
    
    return addition, subtraction, multiplication, division

addition, subtraction, multiplication, division = calc(4,2)

print("Addition =", addition)
print("Subtraction =", subtraction)
print("Multiplication =", multiplication)
print("Division =", division)

Output:

Addition = 6
Subtraction = 2
Multiplication = 8
Division = 2.0

[/bg_collapse]


Question 3 : Create a function with the inner function to return the addition and subtraction of the values passed as arguments.

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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

[/bg_collapse]


Question 4 : Create a function named “calculation” to print the addition of two numbers. Call the same function again using the name “addition”.

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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

[/bg_collapse]


Question 5 : Write a function in Python to check whether the string is pangram or not?

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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

[/bg_collapse]


Question 6 : Write a function in Python to sort the words of the sentence in the alphabetical order.

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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'

[/bg_collapse]


Question 7 : Write a function in python to check whether the string is the Palindrome or not.

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

def is_palindrome(str):
    return str == str[::-1]

print(is_palindrome('radar'))
print(is_palindrome('racecar'))
print(is_palindrome('racecars'))

Output:

True
True
False

[/bg_collapse]


Question 8 : Write a function in Python to calculate the alphabets and digits from the string.

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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}

[/bg_collapse]


Question 9 : Write a function in Python to calculate the frequency of each word in the string.

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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))

Output:

{'This': 1, 'the': 5, 'just': 1, 'frequency': 1, 'test': 2, 'of': 1, 'string': 1}

[/bg_collapse]



dinamobet - dinamobet giriş - dinamobet güncel giriş
dinamobet
dinamobet - dinamobet giriş - dinamobet güncel giriş
dinamobet
dinamobet
dinamobet
Dinamobet
betasus
meritking
dinamobet
dinamobet
dinamobet
dinamobet
dinamobet
dinamobet
dinamobet
Dinamobet
Dinamobet
Dinamobet
Dinamobet
Dinamobet
Dinamobet
Kulisbet
Kulisbet
Dinamobet
Dinamobet
Kulisbet
Kulisbet
Kulisbet giriş
Kulisbet güncel giriş