5 Python Operators That Look Weird but Are Actually Brilliant

They may confuse you at first glance, but once you understand them, these quirky Python operators can make your code cleaner, faster, and…

5 Python Operators That Look Weird but Are Actually Brilliant
Photo by Usman Yousaf on Unsplash

Looks weird. Works wonders.

5 Python Operators That Look Weird but Are Actually Brilliant

They may confuse you at first glance, but once you understand them, these quirky Python operators can make your code cleaner, faster, and more expressive.

Python Has Some Weird Tricks — And They’re Brilliant Once You Get Them

Let’s be honest: Python’s syntax is famously clean and beginner-friendly. But every once in a while, you stumble upon an operator that looks like a typo or some secret hacker glyph.

Maybe it’s a lone asterisk doing something strange. Or two ampersands that remind you of C. Or a walrus, of all things.

At first glance, these operators can seem weird or unnecessary. But look deeper and you’ll find they’re actually some of Python’s most powerful and expressive tools — once you know when and how to use them.

In this article, I’ll walk you through 5 Python operators that look odd but are incredibly useful, with real-world examples to show why they’re worth mastering.


1. The Walrus Operator (:=)

“Assignment inside expressions? Is this legal?”

Introduced in Python 3.8, the walrus operator allows you to assign a value and use it in the same expression. This can reduce repetition and make your code cleaner — but it looks…strange.

Example

# Without walrus 
value = input("Enter something: ") 
if value != "": 
    print(f"You entered: {value}") 
 
# With walrus 
if (value := input("Enter something: ")) != "": 
    print(f"You entered: {value}")
Saves you from repeating code.
Especially useful in while loops or if conditions where you want to use the result immediately.

Use it sparingly and with clear intent — overuse can hurt readability.

2. The Single Asterisk (*) in Function Parameters

“What does this lonely star even do?”

A single * in a function definition acts as a separator between positional and keyword-only arguments. It doesn’t collect arguments like *args — instead, it forces everything after it to be specified by keyword.

Example

def greet(name, *, punctuation="!"): 
    print(f"Hello, {name}{punctuation}") 
 
greet("Alice", punctuation="?")  # Valid 
greet("Bob", "?")               # TypeError
Makes your function interfaces clearer.
Prevents accidental misplacement of arguments.
Encourages self-documenting code by requiring explicit keyword arguments.

3. The Double Asterisk (**) in Function Calls

“Okay, what’s up with this star obsession?”

When you see ** in a function call, it's unpacking a dictionary into keyword arguments. This allows for dynamic and flexible code, especially when dealing with configs, APIs, or wrappers.

Example

def connect(host, port, secure=False): 
    print(f"Connecting to {host}:{port}, secure={secure}") 
 
params = { 
    "host": "localhost", 
    "port": 8080, 
    "secure": True 
} 
 
connect(**params)
Makes your code modular and dynamic.
Works beautifully with config files, decorators, and wrappers.
Enables generic functions that are still clean and readable.

4. The Bitwise NOT Operator (~)

“Is this even Python or did I switch to assembly?”

The tilde ~ is Python’s bitwise NOT operator. At first glance, it seems obscure. But it shines in certain contexts — particularly when working with sets, flags, or data science libraries like pandas.

Example: In pandas

import pandas as pd 
 
df = pd.DataFrame({"name": ["Alice", "Bob"], "active": [True, False]}) 
 
# Get all inactive users 
inactive = df[~df["active"]]
In pandas, ~ is often used as a logical NOT on boolean series.
In low-level code, it lets you flip bits (useful for flag management).
Looks odd, but often simpler and faster than alternatives.

5. The Ellipsis Operator (...)

“Dot dot dot? Is Python just trailing off?”

Yes, ... (three dots) is a real thing in Python — it’s called the ellipsis operator.

Most people encounter it in slicing, especially with multidimensional arrays like those in NumPy.

Example: In NumPy

import numpy as np 
 
arr = np.random.rand(3, 4, 5) 
 
# Access all elements in the first dimension 
print(arr[..., 0].shape)  # (3, 4)

Other Use Cases

  • As a placeholder in incomplete functions or stubs:
def future_feature(): 
    ...
  • As a marker for “I’ll finish this later” — more expressive than pass.
Adds clarity in complex slicing.
Acts as a semantic placeholder — better than comments.
Makes your code cleaner when using frameworks that expect it (e.g., FastAPI, NumPy, etc.).

Why These “Weird” Operators Deserve Love

Many Python developers never use these operators — not because they’re bad, but because they’re unfamiliar. They look strange at first glance, and Python’s forgiving nature means you can go years without needing them.

But that also means you’re leaving power on the table.

Mastering these lesser-known operators can help you:

  • Write more elegant, expressive code
  • Avoid repetition and boilerplate
  • Work more effectively with advanced libraries and patterns

You don’t need to memorize them all today — just knowing they exist gives you an edge. And the next time you spot a walrus, a tilde, or an ellipsis in the wild, you’ll nod and say, “Ah, that’s actually brilliant.”


Final Takeaway

The beauty of Python lies not just in its simplicity, but in its depth masked by that simplicity. Weird-looking operators like :=, *, ~, and ... may not be beginner-friendly — but they’re senior-friendly, offering compact, powerful tools for writing expressive, professional-grade code.