1.
Write a python program to generate a function to double a
specified number.
The output is:
Specified number: 27.2 Expected Answer:
The double number of 27.2 = 54.4
Sample Answer:
def double_number(n):
return lambda x : x * n
result = double_number(2)
print("The double number of 27.2 =", result(27.2))
2. Write a python program to generate a function to get the power
of a specified number.
The output is:
Power of: 5, 6, 7
Specified number: 3 Expected Answer:
The power of five for number 3 = 15
The power of six for number 3 = 18
The power of seven for number 3 = 21
Sample Answer:
def double_number(n):
return lambda x : x * n
result = double_number(5)
print("The power of five for number 3 =", result(3))
result = double_number(6)
print("The power of six for number 3 =", result(3))
result = double_number(7)
print("The power of seven for number 3 =", result(3))
1.
3. Write a python program to sort the order of programming based
on its alphabet sequence with list of dictionaries, programmer is
provided. The expected answer is in the following output for better
understanding.
The output is:
programmer =
[{'Name':'Mark', 'Programming':'Python', 'Year of Experience': 3},
{'Name':'Nicole', 'Programming':'C', 'Year of Experience': 1},
{'Name':'Jason', 'Programming':'R', 'Year of Experience': 10}]
Expected Answer:
The initial list of dictionaries :
[{'Name': 'Mark', 'Programming': 'Python', 'Year of Experience': 3},
{'Name': 'Nicole', 'Programming': 'C', 'Year of Experience': 1},
{'Name': 'Jason', 'Programming': 'R', 'Year of Experience': 10}]
After sorting the list of dictionaries :
[{'Name': 'Nicole', 'Programming': 'C', 'Year of Experience': 1},
{'Name': 'Mark', 'Programming': 'Python', 'Year of Experience': 3},
{'Name': 'Jason', 'Programming': 'R', 'Year of Experience': 10}]
Sample Answer:
programmer = [{'Name':'Mark', 'Programming':'Python', 'Year
of Experience': 3},
{'Name':'Nicole', 'Programming':'C', 'Year of
Experience': 1},
{'Name':'Jason', 'Programming':'R', 'Year of
Experience': 10}]
print("The initial list of dictionaries :")
print(programmer)
sorted_programmer = sorted(programmer, key = lambda x:
x['Programming'])
print("\nAfter sorting the list of dictionaries :")
print(sorted_programmer)
4. Write a python program to sort the programmer user as provided
in the output below in increasing order.
The output is:
n_programmer = [('Python', 198), ('C', 112), ('R', 67)]
Expected Answer:
The initial list of tuples:
[('Python', 198), ('C', 112), ('R', 67)]
After sorting the list of tuples:
[('R', 67), ('C', 112), ('Python', 198)]
Sample Answer:
n_programmer = [('Python', 198), ('C', 112), ('R', 67)]
print("The initial list of tuples:")
print(n_programmer)
n_programmer.sort(key = lambda x: x[1])
print("\nAfter sorting the list of tuples:")
print(n_programmer)
1.
5. Write a python program to generate a lambda function that able
to add a number of 20.
The output is:
Input number: 10
Expected Answer:
After adding 10: 30
Sample Answer:
input_number = lambda a : a + 20
print("After adding 10:", input_number(10))
6. Write a python program to generate a lambda function that able
to multiply two input number.
The output is:
Two numbers: 3.2, 7.6
Expected Answer:
Multiplication of both input number: 24.32
Sample Answer:
input_number = lambda a, b : a * b
print("Multiplication of both input
number:",input_number(3.2, 7.6))
7. Write a python program to generate a lambda function to
separate the odd and even number from a list of numbers, list_a as
provided in the following output.
The output is:
list_a = [22, 56, 77, 99, 45, 36, 87, 93, 5, 78, 2, 66]
Expected Answer:
The initial list of integers: [22, 56, 77, 99, 45, 36, 87, 93, 5, 78, 2, 66]
Even numbers from the said list: [22, 56, 36, 78, 2, 66]
Odd numbers from the said list: [77, 99, 45, 87, 93, 5]
Sample Answer:
list_a = [22, 56, 77, 99, 45, 36, 87, 93, 5, 78, 2, 66]
print("The initial list of integers:", list_a)
even_list_a = list(filter(lambda x: x%2 == 0, list_a))
print("\nEven numbers from the said list:", even_list_a)
odd_list_a = list(filter(lambda x: x%2 != 0, list_a))
print("\nOdd numbers from the said list:", odd_list_a )
8. Write a python program to generate a lambda function to get the
power of two on a list of numbers, list_a as provided in the following
output.
The output is:
list_a = [22, 56, 77, 99, 45, 36, 87, 93, 5, 78, 2, 66]
Expected Answer:
The initial list of numbers: [3, 22, 36, 45, 56, 77, 87, 93]
The power of two for all elements in list: [9, 484, 1296, 2025, 3136,
5929, 7569, 8649]
Sample Answer:
list_a = [3, 22, 36, 45, 56, 77, 87, 93]
print("The initial list of numbers:", list_a)
power2_list_a = list(map(lambda x: x ** 2, list_a))
print("\nThe power of two for all elements in list:",
power2_list_a)
1.
9. Write a python program to generate a lambda function to check
whether the specified first letter "C" match with the input values
randomly key in by user. (True or False)
The output is:
First letter: C
Expected Answer:
Python starts with C letter: False
C Programming starts with C letter: True
Sample Answer:
starts_with = lambda x: True if x.startswith('C') else False
print("Python starts with C letter:", starts_with('Python'))
starts_with = lambda x: True if x.startswith('C') else False
print("C Programming starts with C letter:",
starts_with('C_Programming'))
1.
10. Write a python program to generate a lambda function to check
whether a given string is a number or not. (True or False)
The output is:
Example of Expected Answer:
Is the given 3687 a number: True
Is the given Python a number: False
Sample Answer:
is_num = lambda q: q.replace('.','',1).isdigit()
print("Is the given 3687 a number:", is_num('3687'))
print("Is the given Python a number:", is_num('Python'))
*Write a python program to generate a function to accept an input
string therefore count the number of lowercase and uppercase
respectively.
The output is:
input string = 'Welcome to learn Python on Freelearningpoints
website'
Expected Answer:
The initial string is: Welcome to learn Python on Freelearningpoints
website
The number of uppercase characters is: 3
The number of lowercase Characters is: 44
Sample Answer:
def check_string(string):
result={"UPPER_CASE":0, "LOWER_CASE":0}
for i in string:
if i.isupper():
result["UPPER_CASE"]+=1
elif i.islower():
result["LOWER_CASE"]+=1
else:
pass
print ("The initial string is: ", string)
print ("The number of uppercase characters is: ",
result["UPPER_CASE"])
print ("The number of lowercase Characters is: ",
result["LOWER_CASE"])
check_string('Welcome to learn Python on Freelearningpoints
website')
11 .Write a python program to generate a function to check whether
the input alphabet is vowel or not.
The output is:
Input: letter "a" and "z"
Expected Answer:
Is letter 'a' a vowel: True
Is letter 'z' a vowel: False
Sample Answer:
1. def check_vowel(letter):
2. vowels = 'aeiou'
3. return letter in vowels
4. print("Is letter 'a' a vowel:", check_vowel('a'))
5. print("Is letter 'z' a vowel:", check_vowel('z'))
6.
12.Write a python function to count the negative number in the list
a and list b.
The output is:
list_a = [6, 2, 7, -8, -5]
list_b = [3, 2, 7, 9, -5, 1]
Expected Answer:
Count of negative number: [2]
Count of negative number: [1]
Sample Answer:
1. def count_neg(nums):
2. if not nums: return []
3. return [len([n for n in nums if n < 0])]
4.
5. list_a = [6, 2, 7, -8, -5]
6. list_b = [3, 2, 7, 9, -5, 1]
7.
8. print("Count of negative number:", count_neg(list_a))
9. print("Count of negative number:", count_neg(list_b))
example 13 :-
filter_nums = lambda s: ''.join([ch for ch in s if not
ch.isdigit()])
print("filter_nums():", filter_nums("Geeks101"))
do_exclaim = lambda s: s + '!'
print("do_exclaim():", do_exclaim("I am tired"))
find_sum = lambda n: sum([int(x) for x in str(n)])
print("find_sum():", find_sum(101))
Example 14.
l = ["1", "2", "9", "0", "-1", "-2"]
# sort list[str] numerically using sorted()
# and custom sorting key using lambda
print("Sorted numerically:",
sorted(l, key=lambda x: int(x)))
# filter positive even numbers
# using filter() and lambda function
print("Filtered positive even numbers:",
list(filter(lambda x: not (int(x) % 2 == 0 and int(x)
> 0), l)))
# added 10 to each item after type and
# casting to int, then convert items to string again
print("Operation on each item using lambda and map()",
list(map(lambda x: str(int(x) + 10), l)))
example 15:
# Example list
my_list = [1, 2, 3, 4, 5]
# Use lambda to filter out even numbers from the list
new_list = list(filter(lambda x: x % 2 != 0, my_list))
# Print the new list
print(new_list)