Discover how nested loops help create and shuffle a deck of 52 playing cards. Learn the logic behind dealing blackjack hands efficiently using modulus operations in Python.
Key Insights
- Demonstrates the use of nested loops in Python to systematically generate a deck of 52 playing cards by combining two separate lists—one for the 13 card kinds and another for the four suits.
- Explains how to shuffle a deck of cards using Python's built-in
random.shuffle()
function, ensuring random card order before dealing. - Illustrates modulus-based logic for alternate card distribution, clearly outlining how to deal blackjack hands efficiently between a player and dealer.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This is a lesson preview only. For the full lesson, purchase the course here.
All right, a nested loop. You can have a loop inside a loop. So given these two lists, we're going to make a deck of 52 playing cards, like “four of clubs, ” “queen of diamonds, ” and so on.
And we're going to save them—these 52 strings—to a new list called deck. We're going to make file names. We don't need to make the file names.
We're just going to make the card names—that's fine. It's just the name of the card. All right, so we've got to make two lists.
We're going to say kinds, and we're going to say suits. So you've got 13 kinds with four suits. And if you iterate over them simultaneously—for every “two, ” you could do all four suits.
For every “three, ” you could do all four suits. You would get 13 times four, or 52 iterations. Now to do that, you'll use a nested loop, which is a loop inside a loop.
We're going to say for k in kinds, let's just print k. Okay, there you go. But now what if we say for s in suits, print s. Now you get clubs, diamonds, hearts, spades—repeated multiple times. Why does that happen? Because you're running the suits loop every single time the “kinds” loop runs, which is 13 times.
Now you can put them all together with k and s. We need a deck to hold the new cards. We're going to say deck.append, and we'll use our new f-string. We'll say `f"{k} of {s}"`, append it to the deck, and print the result. There you go.
We learned how to shuffle, remember, in the last lesson, lesson three? Let's go back there for a second. Remember, lesson three had all that random content. You come on down.
So there was a random int, which we made for our headlines—news headline right now. We just did that. We did random.shuffle and random.
We did random.choice to do the random cat toy. And in random.shuffle, we could shuffle the deck, right? We could say random.shuffle and then print the deck. Let's just print the first five cards of the deck, say.
Print a hand of poker using the outer loop.
We already did that. Deal a hand of blackjack to two players. So first shuffle the deck, which we've done.
Now how would you deal to two different players? You might have a player hand and a dealer hand, where the game starts with each player and the dealer getting two cards each. So they would be stored in lists, and you'd have to know using modulus—odd and even.
The player gets a card. The dealer gets a card. The player gets a card.
The dealer gets a card. We could say for N in range(4), we're just going to go four times—boom, boom. Let’s start N at 10 and go up to 14.
Okay. So 10,11,12,13. It’s easier to check the modulus that way, right? The remainder on the larger number.
So what we're going to do is say, if N % 2 == 0, then N is even. That would be the first and third cards dealt—numbers 10 and 12. That's the first card. That's the third card.
The first and third cards go to the player. You alternate when you deal. We would say player_hand.append(deck.pop()).
We're just going to pop a card off the deck. We could actually do that in two steps. We could say card = deck.pop()—pop a card off the shuffled deck.
So the deck is already shuffled. You could shuffle it again—why not do a double shuffle?
And then we're going to loop four times, pop a card off the shuffled deck. You don't have to grab a random card because it's already been randomized. You just take the next card. That's how it works with dealing cards.
You don't go into the middle to get a random card. You shuffle all the cards to make sure they're randomized already. And then you just pick off the top—pop.
So that's your card. And then the player hand would get the first card because that would be the 10. N is 10.
And they'd get the third card. Otherwise, it goes to the dealer hand. Each time we could just print the card to know the sequence.
And when we're all done, print the player hand and print the dealer hand—see who’s got what. And we're printing each card as we loop so we can compare to make sure they went into the right hands.
So those are the four cards that got dealt because we're printing them each time. The player is getting the first and third cards, which is correct. The dealer is getting the second and fourth cards, which is correct for one player and the dealer.
New lists are holding the player and dealer hands, which are their cards—in case you don't know the terminology. If you don't know the rules of blackjack, it doesn’t really matter. Just know that in the beginning of the game, the dealer and the player (or players) each get two cards.
And you don’t just say, here’s two cards. They have to be dealt alternately, with the dealer going last.
We have a loop with if-else logic using modulus to build out a new list. And notice we're not looping the list of cards. We're just looping a range of numbers because we want to run.
The important thing about the loop is that it runs four times. Then we can just refer to the deck each time to pop off the deck, which exists as its own list at that point. We have the shuffled deck.
Now we're just going four times and grabbing one card at a time, popping it off the deck. So that's how you make the deck with a nested loop—a loop inside a loop. And this is how you deal two cards to each player alternately, using modulus to do even-odd alternation.
Building out the two hands as you go and printing every card so that we can check to make sure that, in fact, the player got the first and third cards and the dealer got the other two cards. All right. We're done with that.
And we are done, done. That is the end of the lesson. It's a lot of dense stuff, a lot of good content.
This took quite a while. So I hope you stuck with it and are enjoying the course and feel like you're learning a lot because we’re covering a lot. If you learn all this stuff and then you get into the data science, you want to just be able to focus on the data science and not have to be learning fundamentals of programming at the same time.
It's like if you're trying to write a novel—that is not the time to be trying to learn your ABCs, right? You want to get that done first. Like anything you want to get good at, you're going to put the time in. All righty.
We are done with lesson four. Thank you very much. We will see you for lesson five when you're ready.