In this video, we will learn about the Python membership operators. Below is the code I used in the video:
Membership operators in Python are `in` and `not in`.
They are used to check if a value exists or does not exist in a sequence like strings, lists, tuples, or dictionaries.
`in`: Checks if a value exists in a sequence.
`not in`: Checks if a value does not exist in a sequence.
Syntax and Basic Example
Using `in` operator with a string
print("a" in "apple")
print("z" not in "apple")
Practical Examples
Example 1: Using `in` with a list
devices = ["switch", "router", "firewall"]
print("router" in devices)
Example 2: Using `not in` with a list
print("server" not in devices)
Example 3: Using `in` with a tuple
vlans = (10, 20, 30)
print(20 in vlans)
Example 4: Using `not in` with a tuple
print(40 not in vlans)
Example 5: Using `in` with a dictionary (checks keys)
device_info = {"hostname": "Switch1", "ip": "192.168.1.1"}
print("hostname" in device_info)
Example 6: Using `not in` with a dictionary (checks keys)
print("location" not in device_info)
Example 7: Membership operators are case-sensitive:
devices = ["switch", "router", "firewall"]
print("Switch" in devices)
#python #pythonprogramming #networkautomation
Auf dieser Seite können Sie das Online-Video Master PYTHON MEMBERSHIP OPERATORS: Simplify Your Code! mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Ferds the NetDev 14 Januar 2025 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 59 Mal angesehen und es wurde von 1 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!