PYTHON LAMBDA FUNCTION EXPLAINED for Network Engineers

Published: 01 February 2025
on channel: 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


On this page of the site you can watch the video online PYTHON LAMBDA FUNCTION EXPLAINED for Network Engineers with a duration of hours minute second in good quality, which was uploaded by the user Ferds the NetDev 01 February 2025, share the link with friends and acquaintances, this video has already been watched 193 times on youtube and it was liked by 7 viewers. Enjoy your viewing!