Sitemap
Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Writing Pretty Pythonic Code

3 min readNov 3, 2025

--

How many of us remember The Zen of Python? Its existence, I mean.

It is a set of principles defining to the maintainers and developers of the Python language (and libraries!), how the Python language should look and function like. Code adhering to these principles is called “Pythonic”.

Get Udit’s stories in your inbox

Join Medium for free to get updates from this writer.

Basically, the idea behind pythonic code is to utilize python-only syntaxes and best practices to make really, truly readable and optimized code. Fortunately, this relies not on different ways of thinking than simply replacing operators and keywords used; usually from language-agnostic to Python specific.

  1. &&, || is un-pythonic.
    and, or is pythonic.
  2. x==2 || x == 5 && x == 6 is un-pythonic.
    x in (5,6) or x is 2 is pythonic.
  3. x > 0 and x < 5 is un-pythonic.
    0 < x < 5 is pythonic.
x = 5

if (x in (5, 6) or x == 2) and (0 < x < 5):
print("Valid number")
else:
print("Invalid number")

4. Avoid ternaries. Python does not provide a true ternary feature. There is one, but it will cause errors for Falsey values of true (e.g. value_if_true is 0, False etc). Conditional statements are more reliable. If you’d still like however, you can value_if_true if condition else value_if_false.

5. Instead of if-elifing switch cases, utilize dicts. Make a dict with the case and corresponding value; and then define a default with dict.get() or dict.setdefault(); depending on whether you want to modify the dict (use setdefault) or not (use get).

def handle_option_a():
return "You selected Option A!"

def handle_option_b():
return "You selected Option B!"

def handle_default():
return "Invalid option selected."

def process_choice(choice):
# Define a dictionary where keys are the choices and values are the functions to execute
option_handlers = {
'A': handle_option_a,
'B': handle_option_b,
}

# Get the function associated with the choice, or the default handler if not found
handler = option_handlers.get(choice, handle_default)

# Execute the retrieved function
return handler()

# Example usage:
print(process_choice('A'))
print(process_choice('B'))
print(process_choice('C'))

6. One Liner Lists:

#Creating a list by appending values of n square
squares = [n * n for n in range(10)]

#Filtering a list to only show values that are squares
evens = [n for n in squares if n%2==0]

#Checking membership
target = [n for range(10) if 5 < n < 15]
foundFirstMethod = target in squares #alternatively
foundSecondMethod = any(n in perfect_squares for n in target)

#Printing the lists, with different delimiters
print(f"{", ".join(foundFirstMethod)}")
print(f"{"; ".join(foundSecondMethod)}")

There are many more. But this is enough to get you started. The way I’d recommend doing it is, singlefile this blog onto your PC, and just start your day writing any dummy code, trying to incorporate as many of these as you can. Of course there’ll be many compiler errors at first, but try not to spend more than 15 minutes a day on it.

You’ll be writing pretty pythonic code within 2 weeks! :D

--

--

Udit
Udit

Written by Udit

Here I teach others what I wish someone had taught me earlier.

Python in Plain English
Python in Plain English

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.