Poker With Python. If you have some experience playing…
By: Diego Salinas
Tags: Machine Learning, Data Science, Python, Poker Online, Poker

Poker With Python

How to Score All Hands in Texas Hold’em

If you have some experience playing Poker you are probably aware that there are 2,598,960 different hands which can be built by doing 5 card combinations from a 52 cards deck.

Calculating a score for such a high number of combinations might look like a daunting task, but don’t let yourself be intimidated, I am going to show you how it can be done with a simple Python script.

First of all, we need to build our deck with 52 cards. That is as simple as creating two lists (one with 4 suits and another with numbers from 2 to 14 included) and combining them just like this:

Now let’s build a function that returns all possible 5 cards combinations taking into account that order doesn’t matter in Poker. I will do it by using the itertools library together with Numpy fromiter which allows making an array from our itertools combinations list. As you probably know, Numpy arrays are faster than Python lists:

OK, so far that was pretty easy. We have created all possible hands but now we need to implement a system to score these hands following all Poker rules.

I am sure there are many ways to do this, so I just came up with my own: I give scores from 0 to 135. This is so because there are 10 different types of hands in Poker and 14 different numbers in every suit, so the scores go like this:

Hand Type → Score

High card → 0 to 14

Pair → 15 to 29

Two Pair → 30 to 44

3 of a Kind → 45 to 59

Straight → 60 to 74

Flush → 75 to 89

Full House → 90 to 104

Four of a Kind → 105 to 1

Straight Flush →120 to 134

Royal Flush →135

OK, so now we have a scoring system to implement. Going into detail about poker rules is out of scope here, but feel free to try running examples with my functions with different hands and check the scores follow the logic of poker rules. I created some small functions to score particular hands and then a bigger function to wrap all cases.

These are my small functions:

Great, so with these functions we can already create our final scoring function.

It will first check if the hand has 5 suited cards, if this is true, it proceeds to determine what type of hand exactly we are dealing with and then give it a score.

If the hand doesn’t contain 5 suited cards, the function looks at the cards’ numbers. It checks how many times each number is repeated, determine the type of hand, and then again, gives a score for the hand.

The function is this one:

Cool, so now we can score any possible hand in Poker.

Let’s run the code we have written so far and check if it works with a few hands:

image by author

Great, there can see how we have created all hands combinations in an array and called it combi. Then we scored a few different types of hands and it looks good.

At this point, it only makes sense to go ALL IN! and score all possible hands in Poker

Let’s put together the functions we have created so far and add one more function that creates a list with all hands and their scores values sorted so that it looks nicer :

And Voila! We finally got all our hands’ scores calculated and sorted in a beautiful Pandas DataFrame:

If you want to download the full code of my script, you can find my GitHub repo here.

This data is very insightful for a poker player as we can now see the frequencies of every hand type.

See below the odds of getting each hand are:

royal flush = 0.000154%

straight flush = 0.00139%

four of a kind = 0.024%

full house = 0.1441%

flush = 0.1965%

straight = 0.3925%

three of a kind = 2.1118%

two pair = 4.7539%

pair = 42.2569%

high card= 50.1177%

In other words, the odds of getting a pair or a hight card are 92.3746% and the odds of getting a straight or more are only 0.7597%.

This was nice so far.

Now the most exciting thing about that is we can use our scores, to create a Python Expected Value Calculator.

In my following article, I show you exactly how to do that.

We will just need to type in our cards, the pot size, number of players, and the price of betting. Then it will decide if we should call, raise, or fold and also determine the size of the bet.

Hope you enjoyed it. Happy coding!