Everything I know about Python...

Jeff Knupp

PYTHON PROGRAMMER


Learn to Write Pythonic Code!

Check out the book Writing Idiomatic Python!

How To Do Just About Anything With Python Lists

Python's list is one of the built-in sequence types (i.e. "it holds a sequence of things") is a wonderfully useful tool. I figured I'd try to determine what people are most often trying to do with lists (by analyzing Google's query data on the topic) and just bang out examples of "How do I do X with a list in Python?"

Reverse/Sort A List In Python

There are two ways to reverse a list in Python, and which one you use depends on what you want to do with the resulting reversed data. If you're only going iterate over the items in the reversed list (say, to print them out), use the Python built-in function reversed(seq).

Read on →


Write Better Python Functions

In Python, like most modern programming languages, the function is a primary method of abstraction and encapsulation. You've probably written hundreds of functions in your time as a developer. But not all functions are created equal. And writing "bad" functions directly affects the readability and maintainability of your code. So what, then, is a "bad" function and, more importantly, what makes a "good" function?

Read on →


Extended Absence

For those who have been keeping score, I've been on somewhat of an extended absence, especially since October of last year. Much of it was the result of events in my personal life which I'll not discuss here. Regardless, consider this my "comeback" announcement. I plan to return to writing regularly, hoping to match the pace of 2014 or so, which already seems so long ago. Heck, Writing Idiomatic Python is three months older than my older daughter and she just started Kindergarten this a few weeks ago. This blog is older than most startups, but it's been a while since I've been producing (I think) useful or interesting content at a reliable pace. I'm going to change that.

Read on →


A Common Misunderstanding About Python Generators

I received the following email a few days ago:

Jeff,

It seems that you know about iterators. Maybe you can explain some weird behavior. If you run the code below you will find that the function is treated differently just because it has a 'yield' in it somewhere, even if it's completely unreachable.

def func():
    print("> Why doesn't this line print?")
    exit() # Within this function, nothing should matter after this point.  The program should exit
    yield "> The exit line above will exit ONLY if you comment out this line."

x = func()
print(x)

When I run the code, I get the following output from the print() call: <generator object func at 0x10e968a50>.

So what's going on here? Why doesn't that line in func() print? Even if yield is completely unreachable, it seems to affect the way the function executes.

Read on →


Counting Cards With Python

Having grown up about 20 minutes from Atlantic City, I'm no stranger to casinos. When I was younger (but over 21! cough) I learned to count cards, a tool used by Blackjack players to help them gain a statistical edge over the casino and thus, in a perfect world, win money over the long term. It appealed to me mainly for the allure of beating the casino at its own game (literally). While every other game in the casino has a negative expected value over the long term, the card counter really can beat the house (I'll outline card counting in more detail below).

Something just happened in New Jersey that rekindled my interest in counting: a casino offering Blackjack with live dealers online.

Read on →