Free tech learning for everyone, from basics to pro

Network AutomationNetworking SecurityBeginner

Python operators for network engineers: arithmetic, comparison, logic, membership and bitwise

In short: Operators are the symbols Python uses to calculate, compare and combine values. Arithmetic operators handle sizes and rates, comparison and logical operators drive if statements, membership tests check whether an item is in a list, identity checks whether two names point to the same object, and bitwise operators work on the individual bits that make up IP addresses and masks.

Why operators matter

Every decision a script makes, such as "is this interface over 80 percent?" or "is this VLAN in the allowed list?", is built from operators. They are simple on their own, but a few behave in ways that surprise beginners, and the bitwise ones explain exactly how subnetting works inside a router.

Python operator familiesArithmetic+ - * / // % **Assignment= += -=Comparison== != > <Logicaland or notMembershipin, not inIdentityis, is notBitwise& | ^ ~ << >>
Figure 1. Seven families of operators. Bitwise operators are the ones subnet calculations use.

Arithmetic and assignment

OperatorMeaningNetwork example
+ - *Add, subtract, multiplyTotal ports across stack members
/Division, always gives a float750 / 1000 is 0.75 of a gigabit
//Floor division, whole number500 // 48 is 10 full 48-port switches
%Remainder (modulo)500 % 48 is 20 ports left over
**Power2 ** 8 - 2 is 254 usable hosts in a /24

Assignment operators update a variable: count += 1 is short for count = count + 1. You will use it constantly in loops that count devices, errors or changes.

Comparison and logical operators

util = 86
status = "up"
if util >= 80 and status == "up":
    print("Busy link")
if not status == "up" or util == 0:
    print("Check this link")
  • Comparison operators (== != < > <= >=) return True or False. Remember that = assigns and == compares.
  • and, or and not combine conditions. Python stops early: in a and b, if a is false, b is never checked.
  • Comparing text is case-sensitive: "Up" == "up" is False, so normalise with lower() first.

Membership and identity

allowed_vlans = [10, 20, 30, 99]
if 40 not in allowed_vlans:
    print("VLAN 40 is not allowed on this trunk")
if "Gi" in "Gi1/0/5":
    print("Gigabit interface")

result = None
if result is None:
    print("No reply from device")

in checks whether an item is in a list, a key is in a dictionary or text is inside a string. is checks whether two names refer to the very same object. Use is only for None, True and False; for comparing values, use ==.

Bitwise operators and subnet masks

Bitwise operators work on the binary digits of whole numbers. & (AND) gives 1 only where both bits are 1, which is exactly what a router does with an address and a mask to find the network.

ip   = (192 << 24) | (168 << 16) | (20 << 8) | 77   # 192.168.20.77 as one number
mask = (0xFFFFFFFF << (32 - 26)) & 0xFFFFFFFF        # a /26 mask
net  = ip & mask
print(net >> 24, (net >> 16) & 255, (net >> 8) & 255, net & 255)   # 192 168 20 64
  • << and >> shift bits left and right; shifting by 8 moves a value one octet.
  • | (OR) combines octets into one 32-bit number.
  • ~ (NOT) flips bits, and ^ (XOR) gives 1 where bits differ, which is how wildcard masks relate to subnet masks.

Operators also follow an order of precedence, just as in school maths: multiplication happens before addition, comparisons before and, and and before or. When a condition mixes several, add brackets. They cost nothing, make your intention obvious to the next reader, and prevent the subtle bugs that appear when Python groups the expression differently from the way you read it.

This is great for understanding; in real scripts, the ipaddress module does it for you safely. Practise the maths with the CIDR calculator.

Real-world example: planning switch ports for a new floor

An engineer at a fictional insurance office had 310 wall points to patch on a new floor and 48-port access switches to buy. A short script used 310 // 48 to get 6 full switches and 310 % 48 to show 22 leftover points, so one more switch was needed: 7 in total. It added 20 percent spare with * and //, checked each requested VLAN with in against the approved list, and flagged any uplink plan where expected load was >= 70 percent of 10 Gbit/s. The numbers went straight into the purchase request.

Practice

What are 500 // 48 and 500 % 48, and what do they mean for switch planning?
10 and 20: ten full 48-port switches, with 20 ports still needed, so you need an eleventh switch.
Why should you write if result is None rather than if result == None?
None is a single object, and is checks identity directly. It is the recommended style and cannot be fooled by objects that redefine ==.
Which bitwise operator finds the network address from an IP address and a mask?
AND (&): the network address is the IP address ANDed with the mask.

Key takeaways

  • Arithmetic: // gives whole units and % gives the remainder, handy for capacity planning.
  • == compares, = assigns; and, or and not combine conditions.
  • in tests membership in lists, dictionaries and strings.
  • Use is only for None, True and False.
  • Bitwise AND of an address and mask gives the network; use ipaddress in real scripts.
Tracks your learning on this device only. See your progress on the learning paths.

About the author

Mohamed Fayas

Founder of FM TecHub. CCNA certified, with a BTEC HND in Network Engineering, and BEng (Hons) in Computer Networking and Cloud Security. He writes practical, plain-English guides on networking, security and cloud.

More about the author

Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.

Stay up to date by email

New notes, tools and security news from FM TecHub, explained simply. Free.

We only use your email to send these updates. See our privacy policy.