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
Sur cette page du site, vous pouvez voir la vidéo en ligne Master PYTHON MEMBERSHIP OPERATORS: Simplify Your Code! durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Ferds the NetDev 14 janvier 2025, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 59 fois et il a aimé 1 téléspectateurs. Bon visionnage!