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 names, 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 want, we're going to make file names. We don't need to make the file names.
We're just going to make, that's fine. It's just the name of the name of the card. All right, so we've got, we need 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 could iterate over them simultaneously, so like 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, clubs, diamonds, hearts, spades, clubs, diamonds, hearts, spades. Why are you getting that? Because you're running this loop of clubs, diamonds, hearts, spades, every single time k runs, which is 13 times, every time kinds runs.
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. We'll say k of s, print deck, or p print for that matter. There you go.
We learned how to shuffle, remember, in the last lesson, lesson three? Let's go back there for a second. Remember, 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. There. 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 player one. Well, if it's blackjack, you might have player hand and dealer hand, where the game starts with the player, each player and the dealer getting two cards each. So they would be stored in lists, but you'd have to know using modulus, odd, even.
So the player go 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 four, we're just going to go four times, right? Boom, boom. We could say, if N mod, let's start with 10 and go to 14.
Okay. So 10,11,12,13. Easier to check the modulus, right? The remainder on the larger number.
So what we're going to do is say, if N mod two equals one, if no, actually if it's zero, if N is even, that would be first and third cards dealt, right? In other words, 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 the player hand dot append is going to get, we're going to say deck dot pop.
We're just going to pop a card off the deck. We could actually do that in two steps. We could say card equals deck, right? Pop a card off the shuffled deck.
So the deck is already shuffled. We could actually shuffle it again. Why not? Double shuffle.
And then we're going to loop four times, pop a card off the shuffle deck. You don't have to go grab a random card because it's already been randomized. You just take the next card, right? 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 you'd get the third card. And then else would be the dealer hand. And then every time we could just print the card just 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. And the player is getting the first card and the third card, which is correct. And the dealer is getting the second card and the last card, which is correct to one player and dealer.
New lists or 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. But 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. 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 of the loop is that it runs four times. Then we can just refer to the deck every time this 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 alternate alternately, right? Alternating 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 odd, the first and third card 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, 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. So it's like anything you want to get good at it, 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.