In this video, we will learn about lambda functions in Python.
Below is the code I used in the video:
A lambda function in Python is an anonymous, one-line function using the lambda keyword.
It can take multiple parameters but must contain only one expression.
Lambda functions are commonly used with functions like map(), filter(), and sorted().
Syntax: lambda parameters: expression
Example 1: Simple Addition using Lambda
add = lambda x, y: x + y
print(add(5, 3))
Regular function for addition
def add(x, y):
return x + y
print(add(5, 3))
Example 2: Extracting the Last Octet of an IP Address
This lambda function splits the IP address by '.' and returns the last octet.
get_last_octet = lambda ip: ip.split(".")[-1]
print(get_last_octet("192.168.1.100"))
print(get_last_octet("10.0.0.5"))
Example 3: Checking If an IP is in a Private Range
This lambda function returns True if the IP belongs to a private range.
is_private_ip = lambda ip: ip.startswith(("192.168.", "10.", "172.16."))
print(is_private_ip("192.168.1.1"))
print(is_private_ip("8.8.8.8"))
Example 4: Checking If a Port is Open
This lambda function returns 'Open' if the port number is in the allowed list, otherwise 'Closed'.
is_port_open = lambda port: "Open" if port in [22, 80, 443] else "Closed"
print(is_port_open(22))
print(is_port_open(8080))
#python #networkautomation #coding
Auf dieser Seite können Sie das Online-Video PYTHON LAMBDA FUNCTION EXPLAINED for Network Engineers mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Ferds the NetDev 01 Februar 2025 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 193 Mal angesehen und es wurde von 7 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!