Python- Day 32- CLASSES-The Python Standard Library
The Python Standard Library is a collection of tools that come with Python.
These tools are ready to use as soon as you install Python.
Now that you understand the basics of functions and classes, you can start using these pre-made tools, known as modules, which other programmers have created.
To use a module, just write an import statement at the top of your code.
Let’s look at one of these modules: random
.
This module helps simulate real-life randomness.
Using Functions in the Random Module
One useful function in random
is randint()
.
This function picks a random whole number between two numbers you give it.
For example, to get a random number between 1 and 6:
from random import randint
randint(1, 6)
# Example output: 3
Another helpful function is choice()
.
It picks a random item from a list of options.
For example, if you have a list of players and want to choose one at random:
from random import choice
players = ['charles', 'martina', 'michael', 'florence', 'eli']
first_up = choice(players)
# Example output: 'florence'
The random
module isn’t suitable for secure applications, but it’s great for fun projects or games.
Using Other Modules
Besides the built-in modules, you can also download more modules from outside sources.
In the next sections, we’ll use some of these extra modules to build various projects.
Styling Classes in Python
When writing classes in Python, here are some simple style guidelines to keep your code clean and readable:
- Class Names: Use CamelCase for class names, which means capitalizing the first letter of each word, with no underscores. For example,
MyClass
orCustomerAccount
. - Instance and Module Names: Use lowercase_with_underscores for names of instances (objects created from classes) and module files.
- Docstrings: Every class should have a short description (called a docstring) right after the class definition. This should briefly explain what the class does. Each module should also have a docstring explaining what classes it contains.
- Using Blank Lines: Use blank lines to separate parts of your code, but don’t overdo it. Within a class, add one blank line between methods. When you have multiple classes in a module, separate them with two blank lines.
- Organizing Import Statements: If you’re importing modules, list standard library imports first, followed by a blank line, and then your own module imports. This makes it easy to see where each module is coming from.
Summary
You’ve learned:
- How to create and use classes, store information with attributes, and give classes behaviors with methods.
- How to use the
__init__()
method to set up instances with specific attributes. - How to modify instance attributes both directly and through methods.
- How inheritance allows you to create related classes easily.
- How to use instances of one class as attributes in another, making each class simpler.
You also saw that organizing classes into modules and importing them helps keep your projects neat. Following Python style guidelines keeps your code readable and organized.
Here end’s section CLASSES, Next we will learn about FILES AND EXCEPTIONS
If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!