Sitemap
Paola Canale

Paola Canale

Born as a Java programmer, I'm converting to Python, with some small traumas, because I want to try to make machines reason (having failed with people)

3 ready-to-use Python tips

4 min readDec 9, 2025

--

Coming from Java, jumping into Python was traumatic. At the beginning I literally replicated the verbose syntax of Java (which I love anyway) in Python, never was a choice so unfortunate.
Then, I gradually embraced the new Zen. What I’m showing you are some little tricks that I would have liked to have known in my first work experience to avoid comments on the readability of the code that started with encouraging “But what…?”

import this

>The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

And by the way: yes, Python’s Zen really exists.

1. List comprehensions

A list comprehension is a compact, single-line syntax that builds a new list by evaluating an expression on each item of an iterable.

It is the first but effective way of understanding what is meant by readable, simple and, why not, beautiful.

# ❌ NON pythonic
my_list = list(range(1,11))
c=0
for i in my_list:
my_list[c] = i*10
c+=1
my_list

# ✅ Pythonic (one-liner)
my_list = [i*10 for i in list(range(1,11))]
print(my_list) # [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

You can add an If-clause…

# ❌ NON pythonic
my_even_list = []
for i in list(range(1,11)):
if i % 2 == 0:
my_even_list.append(i)

# ✅ Pythonic (one-liner)
my_even_list = [i for i in list(range(1,11)) if i %2 ==0]

print(my_even_list) # [2, 4, 6, 8, 10]

Or an if-else clause:

# ❌ NON pythonic
my_list = list(range(1,11))
c =0
for i in my_list:
if i % 2 != 0:
my_list[c] = i*10
else:
my_list[c]= int(i/2)
c+=1

# ✅ Pythonic (one-liner)
my_list = [i * 10 if i %2 != 0 else int(i/2) for i in list(range(1,11))]

print(my_list) # [10, 1, 30, 2, 50, 3, 70, 4, 90, 5]

You can also leverage iterable unpacking and *-operators to flatten nested loops:

nested = [(1, 2), (3, 4), (5, 6)]
# ❌ NON pythonic
flat = []
for t in nested:
for x in t:
flat.append(x)

# ✅ Pythonic (one-liner)
flat = [x for *t, in nested for x in t]
print(flat) # [1, 2, 3, 4, 5, 6]

2. Lambda function

A lambda is an anonymous, inline function expressed as a single expression, usable wherever a normal function object is required.

Get Paola Canale’s stories in your inbox

Join Medium for free to get updates from this writer.

You can pass one parameter

square = lambda x : x**2
square(2) #4

You can pass two parameters (and three and so on)

areaT = lambda b, h: b * h / 2
areaT(2,3) #3

or you can pass two parameters and the second can be a function of the first

areaT = lambda b, h: b * h(b) / 2
areaT(2, lambda x: x/2) #1.0

3. OOP

@dataclass auto-writes __init__, __repr__, __eq__, etc. from type-annoted fields, turning a 40-line class into 4 readable lines while keeping full Pythonic clarity and still letting you add custom methods when you need them.

from dataclasses import dataclass
from typing import List

# ❌ old style
class OldProduct:
def __init__(self, sku: str, price: float, tags: List[str]):
self.sku = sku
self.price = price
self.tags = tags

# ✅ dataclass: equal functionality, 1/10 of the rows
@dataclass
class Product:
sku: str
price: float
tags: List[str]

p = Product("PY-Book", 29.9, ["python", "best-seller"])
print(p) # Product(sku='PY-Book', price=29.9, tags=['python', 'best-seller'])

You can also implement abstract class

from dataclasses import dataclass
from abc import ABC, abstractmethod
import math

@dataclass
class Shape(ABC):
"""Abstract dataclass: common anchor for every concrete shape."""
@abstractmethod
def area(self) -> float: ...

# ---------- concrete shapes ----------
@dataclass
class Rectangle(Shape):
width: float
height: float

def area(self) -> float:
return self.width * self.height

@dataclass
class Triangle(Shape):
base: float
height: float

def area(self) -> float:
return 0.5 * self.base * self.height

@dataclass
class Circle(Shape):
radius: float

def area(self) -> float:
return math.pi * self.radius ** 2

# ---------- quick demo ----------
if __name__ == "__main__":
shapes = [Rectangle(3, 4), Triangle(5, 2), Circle(3)]
for s in shapes:
print(f"{s.__class__.__name__}: area = {s.area():.2f}")

# Rectangle: area = 12.00
# Triangle: area = 5.00
# Circle: area = 28.27

Conclusions

In short, coding Pythonic isn’t just virtuous — it’s lazy in the best possible way: you write fewer lines, the interpreter does the heavy lifting, and your future self can still read the script without needing coffee, therapy, or a time machine. Thank you for reading this far, and to indulge like Truman in “The Truman Show”: “Good morning, and in case I don’t see you, good afternoon, good evening, and good night!”

--

--

Paola Canale
Paola Canale

Written by Paola Canale

Born as a Java programmer, I'm converting to Python, with some small traumas, because I want to try to make machines reason (having failed with people)