PYTHON LAMBDA FUNCTION EXPLAINED for Network Engineers

Pubblicato il: 01 febbraio 2025
sul canale di: Ferds the NetDev
193
7

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


In questa pagina del sito puoi guardare il video online PYTHON LAMBDA FUNCTION EXPLAINED for Network Engineers della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Ferds the NetDev 01 febbraio 2025, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 193 volte e gli è piaciuto 7 spettatori. Buona visione!