Building A Bridge Hand Generator With Python Lesson One
Using import
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 shufflePython 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”

102–102






