avatarJim McAulay🍁 I'm nobody. Are you a nobody too?

Summary

This article introduces the process of creating a bridge hand generator in Python, focusing on the use of the random module and its shuffle function to create and shuffle a deck of cards.

Abstract

The article is the first in a series aimed at developing a program to randomly generate bridge hands using Python. It emphasizes the modularity of Python by highlighting the use of pre-written functions within modules, such as the shuffle function from the random module, to perform tasks efficiently without the need to write new code for common functionalities. The author guides readers through the initial steps of importing modules and using the help utility to explore available modules and functions, demonstrating how to shuffle a list of items as a foundational skill for the bridge hand generator. The article also provides insights into the versatility of Python for various applications beyond the scope of the current project.

Opinions

  • The author believes that Python's extensive library of modules is a significant advantage for developers, as it allows them to leverage existing code for common tasks.
  • The use of the random module, specifically the shuffle function, is presented as a practical example of how Python simplifies development by providing ready-to-use tools.
  • The article suggests that beginners in Python should familiarize themselves with the built-in help utility to better understand and utilize the language's modules and functions.
  • The author, Jim McAulay, expresses a personal message of positivity and safety to the readers, indicating a caring and supportive attitude towards the community.
  • There is an implied appreciation for the Python community and its contributions, as well as an encouragement for readers to engage with and support the author's work, as seen in the "Buy me a coffee" section.

Building A Bridge Hand Generator With Python Lesson One

Using import

Photo by Mufid Majnun on Unsplash

This is the first in a series of articles in which we develop a program to randomly generate bridge hands. We are going to take you step by step through the process of creating a useful program. This article will focus on importing modules.

Here is the first line of the program:

from random import shuffle

Python is a modular language with hundreds of modules installed when you download it. There are also third party modules available through which Python can be used web scraping, web building, data science applications and much more.

What is a module?

In an earlier article I introduced the concept of a module focusing on creating your own module.

Today we are going to look at the modules bundled with Python in general and at the random module in particular.

Our bridge hand generator will create a deck of 52 cards and shuffle them before displaying the hands.

One of the great things about Python is that functions for many of the things you need to do have already been written and are contained in modules. If you want to shuffle a deck of cards you don’t have to reinvent the wheel.

The shuffle function will shuffle the contents of a list in place.

Here is how to find modules and functions:

To get a list of available modules run the following.

help()

This will give you:

Welcome to Python 3.8’s help utility!

If this is your first time using Python, you should definitely check out the tutorial on the Internet at https://docs.python.org/3.8/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type “quit”.

To get a list of available modules, keywords, symbols, or topics, type “modules”, “keywords”, “symbols”, or “topics”. Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as “spam”, type “modules spam”.

help>

Type modules and enter. You will get a long list of available modules. One of the modules is “random” you will get help> once again. This time type in “random”. This will give you a list of functions provided by this module and show you how to use them.

One of the functions is shuffle.

from random import shuffle
lst = ["A","B","C"]
shuffle (lst)
print (lst)
will return   ["A","C","B"]

To review how to use a function from a module.

you can:

import random
lst = ["A","B","C"]
random.shuffle (lst)

or since there is only one function that you need

from random import shuffle
lst = ["A","B","C"]
shuffle (lst)

Jim McAulay🍁 says, “ To all my readers I hope you are staying positive and testing negative”

Buy me a coffee

102–102

Technology
Python
Bridge
Jim Mcaulay
Illumination
Recommended from ReadMedium