0% found this document useful (0 votes)
12 views

Lambda Assignement - Part 1

The document provides a series of Python programming tasks focused on using lambda functions, including creating functions for addition, multiplication, filtering lists, and checking string properties. It includes example code snippets for each task, demonstrating how to implement the required functionality. The tasks cover a range of topics such as list manipulation, string processing, and mathematical operations.

Uploaded by

KINJAL PARMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lambda Assignement - Part 1

The document provides a series of Python programming tasks focused on using lambda functions, including creating functions for addition, multiplication, filtering lists, and checking string properties. It includes example code snippets for each task, demonstrating how to implement the required functionality. The tasks cover a range of topics such as list manipulation, string processing, and mathematical operations.

Uploaded by

KINJAL PARMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

4/24/24, 11:48 PM Lambda Assignement

Write a Python program to create a lambda


function that adds 15 to a given number
passed in as an argument
In [5]: s = lambda x: x+15
print("Answer method 1 - ", s(7))
#Or you can also write
print("Answer method 2 - ",(lambda x: x+15)(7))

Answer method 1 - 22
Answer method 2 - 22

Create a lambda function that multiplies


argument x with argument y and prints the
result.
In [6]: s=lambda x,y:x*y
print("Answer method 1 - ", s(2,7))
#Or you can also write
print("Answer method 1 - ", (lambda x,y:x*y)(2,7))

Answer method 1 - 14
Answer method 1 - 14

Write a Python program to create a


function that takes one argument, and that
argument will be multiplied with an
unknown given number.
In [7]: unknownNumer = int(input("Enter any unknown number - "))
s = lambda x:x*unknownNumer
print(s(10))

20

Write a Python program to filter a list of


integers using Lambda.
Original list of integers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Even numbers from the said list: [2, 4, 6, 8,
10] Odd numbers from the said list: [1, 3, 5, 7, 9]

file:///C:/Users/HP 840-G5 i5 8th/Downloads/Lambda Assignement.html 1/8


4/24/24, 11:48 PM Lambda Assignement

In [10]: #method 1 to take list from user


list1 = eval(input("Enter list 1 - "))

#method 2 to take list from user


#list2 = []
#n = int(input("Enter list length - "))
#for i in range(n):
# temp = int(input("Enter number -"))
# list2.append(temp)

s_even = lambda x : x%2==0


s_odd = lambda x : x%2==1
print("Even list is - ", list(filter(s_even,list1)))
print("Odd list is - ", list(filter(s_odd,list1)))

Even list is - [10, 2, 2, 4, 4]


Odd list is - [3, 5, 3]

Write a Python program to square and


cube every number in a given list of
integers using Lambda.
Original list of integers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Square every number of the said list: [1, 4,
9, 16, 25, 36, 49, 64, 81, 100] Cube every number of the said list: [1, 8, 27, 64, 125, 216, 343,
512, 729, 1000]

In [15]: original_List = eval(input("Enter list - "))


s_Square = lambda x : x*x
s_Cube = lambda x : x**3
print("Original list of integers: ", original_List)
print("Square every number of the said list: ", list(map(s_Square,original_List)))
#or you can also write
print("Or you can also write - Square every number of the said list: ", list(map(la
print("Cube every number of the said list: ", list(map(s_Cube,original_List)))
#or you can also write
print("Or you can also write - Cube every number of the said list: ", list(map(lamb

Original list of integers: [1, 2, 3, 4, 5]


Square every number of the said list: [1, 4, 9, 16, 25]
Or you can also write - Square every number of the said list: [1, 4, 9, 16, 25]
Cube every number of the said list: [1, 8, 27, 64, 125]
Or you can also write - Cube every number of the said list: [1, 8, 27, 64, 125]

Write a Python program to find if a given


string starts with a given character using
Lambda.

file:///C:/Users/HP 840-G5 i5 8th/Downloads/Lambda Assignement.html 2/8


4/24/24, 11:48 PM Lambda Assignement

In [16]: s = lambda s,c:s[0]==c

string = input("Enter String - ")


char = input("Enter character - ")
print(s(string,char))

True

Write a Python program to check whether


a given string is a number or not using
Lambda.
In [19]: s = lambda s:s.isdigit()
string = input("Enter String - ")
if(s(string)):
print(string, " is number")
else:
print(string, " is not a number")

12 is number

Write a Python program to find the


intersection of two given arrays using
Lambda.
Original arrays: [1, 2, 3, 5, 7, 8, 9, 10] [1, 2, 4, 8, 9] Intersection of the said arrays: [1, 2, 8, 9]

In [22]: list1 = eval(input("Enter List 1 - "))


list2 = eval(input("Enter List 2 - "))
print("Original arrays:")
print(list1)
print(list2)
print("Intersection of the said arrays: ", list(filter(lambda x:x in list1,list2)))

Original arrays:
[1, 2, 3, 4, 5, 6, 7]
[4, 5, 6]
Intersection of the said arrays: [4, 5, 6]

Write a Python program to filter a given


list to determine if the values in the list
have a length of 6 using Lambda.
In [23]: l = eval(input("Enter List of values - "))
s = lambda x:len(str(x))==6
print("Requried filtered list with only values of length 6 is - ", list(filter(s,l)

file:///C:/Users/HP 840-G5 i5 8th/Downloads/Lambda Assignement.html 3/8


4/24/24, 11:48 PM Lambda Assignement

Requried filtered list with only values of length 6 is - ['abcdeg']

Write a Python program to add two given


lists using map and lambda.
Original list: [1, 2, 3] [4, 5, 6] Result: after adding two list [5, 7, 9]

In [25]: list1 = eval(input("Enter List 1 - "))


list2 = eval(input("Enter List 2 - "))
#also check the result by entering the lists of different sizes
print("Original arrays:")
print(list1)
print(list2)
print("Result: after adding two list ", list(map(lambda x,y:x+y,list1,list2)))

Original arrays:
[1, 2, 3]
[1, 1, 1]
Result: after adding two list [2, 3, 4]

Write a Python program to find numbers


divisible by nineteen or thirteen from a list
of numbers using Lambda.
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by
nineteen or thirteen: [19, 65, 57, 39, 152, 190]

In [26]: list1 = eval(input("Enter List - "))


print("Orginal list: ", list1)
s = lambda x : x%13==0 or x %19==0
print("Numbers of the above list divisible by nineteen or thirteen: ", list(filter(

Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190]
Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152,
190]

Write a Python program to find


palindromes in a given list of strings using
Lambda.
Orginal list of strings: ['php', 'w3r', 'Python', 'abcd', 'Java', 'aaa'] List of palindromes: ['php',
'aaa']

In [27]: list1 = eval(input("Enter List - "))


print("Orginal list of strings: ", list1)

file:///C:/Users/HP 840-G5 i5 8th/Downloads/Lambda Assignement.html 4/8


4/24/24, 11:48 PM Lambda Assignement

s = lambda string:string==string[::-1]
print("List of palindromes: ", list(filter(s,list1)))

Orginal list of strings: ['php', 'w3r', 'Python', 'abcd', 'Java', 'aaa']


List of palindromes: ['php', 'aaa']

Write a Python program that multiplies


each number in a list with a given number
using lambda functions. Print the results.
Original list: [2, 4, 6, 9, 11] Given number: 2 Result: [4, 8, 12, 18, 22]

In [29]: list1 = eval(input("Enter List - "))


n = int(input("Enter a number to multiply with list: "))
print("Orginal list: ", list1)
s = lambda x : x*n
print("Result: ", list(map(s,list1)))

Orginal list: [2, 4, 6, 9, 11]


Result: [4, 8, 12, 18, 22]

Write a Python program that sums the


length of a list of names after removing
those that start with lowercase letters. Use
the lambda function.
In [34]: list1 = eval(input("Enter List - "))
print("Orginal list: ", list1)
s = lambda string:not(string[0].islower())
new_list = list(filter(s,list1))
print("new_list is: ",new_list)
print("Length of sum of all strings in new_list is: ", len(''.join(new_list)))

Orginal list: ['sally', 'Dylan', 'rebecca', 'Diana', 'Joanne', 'keith']


new_list is: ['Dylan', 'Diana', 'Joanne']
Length of sum of all strings in new_list is: 16

Write a Python program to calculate the


sum of the positive and negative numbers
of a given list of numbers using the lambda
function.
Original list: [2, 4, -6, -9, 11, -12, 14, -5, 17] Sum of the negative numbers: 48 Sum of the
positive numbers: -32

file:///C:/Users/HP 840-G5 i5 8th/Downloads/Lambda Assignement.html 5/8


4/24/24, 11:48 PM Lambda Assignement

In [37]: from functools import *


list1 = eval(input("Enter List - "))
print("Orginal list: ", list1)
list_p = list(filter(lambda x:x>0,list1))
list_n = list(filter(lambda x:x<0,list1))
print("Sum of the positive numbers: ", reduce(lambda x,y:x+y, list_p))
print("Sum of the negative numbers: ", reduce(lambda x,y:x+y, list_n))

Orginal list: [2, 4, -6, -9, 11, -12, 14, -5, 17]
Sum of the positive numbers: 48
Sum of the negative numbers: -32

Write a Python program to remove specific


words from a given list using lambda.
Original list: ['orange', 'red', 'green', 'blue', 'white', 'black'] Remove words: ['orange', 'black']
After removing the specified words from the said list: ['red', 'green', 'blue', 'white']

In [39]: list1 = eval(input("Enter List - "))


remove_words = eval(input("Enter List for removed words - "))
print("Orginal list: ", list1)
print("Remove words: ", remove_words)
final_list = list(filter(lambda x: x not in remove_words,list1))
print("After removing the specified words from the said list: ",final_list)

Orginal list: ['orange', 'red', 'green', 'blue', 'white', 'black']


Remove words: ['orange', 'black']
After removing the specified words from the said list: ['red', 'green', 'blue', 'wh
ite']

Write a Python program to multiply all the


numbers in a given list using lambda.
Original list: [4, 3, 2, 2, -1, 18] Multiply all the numbers of the said list: -864 Original list:[2, 4,
8, 8, 3, 2, 9] Multiply all the numbers of the said list: 27648

In [40]: from functools import *


list1 = eval(input("Enter List - "))
print("Orginal list: ", list1)
print("Multiply all the numbers of the said list: ", reduce(lambda x,y:x*y,list1))

Orginal list: [4, 3, 2, 2, -1, 18]


Multiply all the numbers of the said list: -864

Write a Python program to reverse strings


in a given list of string values using
lambda.
file:///C:/Users/HP 840-G5 i5 8th/Downloads/Lambda Assignement.html 6/8
4/24/24, 11:48 PM Lambda Assignement

Original lists: ['Red', 'Green', 'Blue', 'White', 'Black'] Reverse strings of the said given list:
['deR', 'neerG', 'eulB', 'etihW', 'kcalB']

In [41]: list1 = eval(input("Enter List - "))


print("Orginal list: ", list1)
print("Reverse strings of the said given list: ", list(map(lambda string:string[::-

Orginal list: ['Red', 'Green', 'Blue', 'White', 'Black']


Reverse strings of the said given list: ['deR', 'neerG', 'eulB', 'etihW', 'kcalB']

Write a Python program to check whether


a specified list is sorted or not using
lambda.
Original list: [1, 2, 4, 6, 8, 10, 12, 14, 16, 17] Is the said list is sorted! - True Original list: [1, 2, 4,
6, 8, 10, 12, 14, 16, 17] Is the said list is sorted! - False

In [51]: from functools import *


list1 = eval(input("Enter List - "))
print("Orginal list: ", list1)
is_sorted = lambda lst: all(lst[i] <= lst[i + 1] for i in range(len(lst) - 1))
print("Is the said list is sorted!",is_sorted(list1))
#In Python, the all() function is a built-in function that returns True if all elem

Orginal list: [1, 2, 4, 6, 8, 10, 12, 14, 6, 17]


Is the said list is sorted! False

Write a Python program to check whether


a given string contains a capital letter, a
lower case letter, a number and a minimum
length using lambda.
Input the string: W3resource output: Valid string

In [57]: inputString = input("Enter string: ")


is_valid1 = lambda string:(any(string[i].islower() for i in range(len(string))))
is_valid2 = lambda string:(any(string[i].isalpha() for i in range(len(string))))
is_valid3 = lambda string:(any(string[i].isdigit() for i in range(len(string))))
is_valid4 = lambda string:len(string) >= 8 #where 8 is minimum length
if(is_valid1(inputString) and is_valid2(inputString) and is_valid3(inputString)and
print("Valid string")
else:
print("Not Valid string")

Valid string

file:///C:/Users/HP 840-G5 i5 8th/Downloads/Lambda Assignement.html 7/8


4/24/24, 11:48 PM Lambda Assignement

Write a Python program to count float


values in a mixed list using lambda.
Original list: [1, 'abcd', 3.12, 1.2, 4, 'xyz', 5, 'pqr', 7, -5, -12.22] Number of floats in the said
mixed list: 3 Hint - The isinstance() function returns True if the specified object is of the
specified type, otherwise False.

In [59]: list1 = eval(input("Enter List - "))


print("Orginal list: ", list1)
final = list(filter(lambda i: isinstance(i, float), list1))
print("Number of floats in the said mixed list: ",len(final))

Orginal list: [1, 'abcd', 3.12, 1.2, 4, 'xyz', 5, 'pqr', 7, -5, -12.22]
Number of floats in the said mixed list: 3

Write a Python program to extract a


specified size of strings from a given list of
string values using lambda.
Original list: [' Python', 'list', 'exercises', 'practice', 'solution'] length of the string to extract: 8
After extracting strings of specified length from the said list: ['practice', 'solution']

In [61]: list1 = eval(input("Enter List - "))


n = int(input("length of the string to extract: "))
print("Orginal list: ", list1)
final = list(filter(lambda string:len(string)==8,list1))
print("After extracting strings of specified length from the said list: ",final)

Orginal list: [' Python', 'list', 'exercises', 'practice', 'solution']


After extracting strings of specified length from the said list: ['practice', 'solu
tion']

file:///C:/Users/HP 840-G5 i5 8th/Downloads/Lambda Assignement.html 8/8

You might also like