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
Nesta página do site você pode assistir ao vídeo on-line PYTHON LAMBDA FUNCTION EXPLAINED for Network Engineers duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Ferds the NetDev 01 Fevereiro 2025, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 193 vezes e gostou 7 espectadores. Boa visualização!