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.
Arithmetic and assignment
| Operator | Meaning | Network example |
|---|---|---|
+ - * | Add, subtract, multiply | Total ports across stack members |
/ | Division, always gives a float | 750 / 1000 is 0.75 of a gigabit |
// | Floor division, whole number | 500 // 48 is 10 full 48-port switches |
% | Remainder (modulo) | 500 % 48 is 20 ports left over |
** | Power | 2 ** 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,orandnotcombine conditions. Python stops early: ina and b, ifais false,bis never checked.- Comparing text is case-sensitive:
"Up" == "up"is False, so normalise withlower()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?
Why should you write if result is None rather than if result == None?
Which bitwise operator finds the network address from an IP address and a 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.
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.