Please enable JavaScript.
Coggle requires JavaScript to display documents.
Python Basic Modules - Coggle Diagram
Python Basic Modules
os (operating system) module provides a portable way of using operating system-dependent functionality. The main advantage of using this module is that it offers many functions for working with the operating system. Their behavior does not depend on the operating system, so the written program remains portable.
os.path module -
implements some useful methods for working with paths.
The os.path module possibilities are beneficial because the paths to directories or files in different operating systems have a different formats.
-
sys.argv,
which returns a list of command line arguments passed to a Python script. The first value in the argument list will always be the name of the current Python script.
-
The math module has a set of methods and constants. Let's consider the most popular:
math.ceil(x) — rounds x up to the nearest integer;
math.sqrt(x) — returns the square root of x;
math.pow(x, y) — returns the value of x to the power of y;
math.pi — the PI number (3.14...);
math.nan — a NaN value;
math.inf — a positive infinity.
-
several functions from this module:
random.randint() — returns a random number between the given range;
random.choice() — returns a random element from the given sequence;
random.choices() — returns a list with a random selection from the given sequence;
random.shuffle() — returns the given sequence in random order;
random.seed() - It initializes the random number generator and is used when the same code is restarted so the random numbers do not change.
This module is similar to the previous one, but it implements the ability to work with both time and date.
several methods:
The datetime.datetime.now() function returns the current date and time. You can get a piece of specified information from the returned value, for example, day, minute, etc.:
from datetime import datetime
today = datetime.now()
print(today) # 2022-09-28 14:04:34.464598
print(today.date()) # 2022-09-28
print(today.time()) # 14:04:34.464598
print(today.day) # 28
print(today.month) # 9
print(today.year) # 2022
print(today.hour) # 14
print(today.minute) # 4
print(today.second) # 34
To calculate some amount of time (days, hours, years) from one date, use the .timedelta() method and specify which unit of time you want to calculate and its value:
import datetime
current_date = datetime.datetime.now()
print(current_date) # 2022-09-28 14:12:16.805400
print(current_date+datetime.timedelta(days=1)) # 2022-09-29 14:12:16.805400
print(current_date-datetime.timedelta(days=1)) # 2022-09-27 14:12:16.805400
print(current_date+datetime.timedelta(hours=5)) # 2022-09-28 19:12:16.805400
print(current_date-datetime.timedelta(minutes=3)) # 2022-09-28 14:09:16.805400
if you want to change a string appearance, use the datetime.strftime() function
It has several methods:
time.time() — returns the number of seconds that have passed since the epoch (January 1, 1970, 00:00:00 (UTC));
time.ctime() — returns a time string that accepts seconds as an argument and computes time till mentioned. If no argument is passed, time is calculated till the present;
time.sleep() — is used to halt the program execution for the time specified in the arguments.
datetime.strptime() function that, on the contrary, makes an object from a string
Serialization
pickle.dump() — writes the serialized object to a file;
pickle.dumps() — returns a serialized object;
pickle.load() — loads an object from a file;
pickle.loads() — loads an object from a byte stream.
The pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled to be saved on disk. The pickle serializes the object before writing it to a file. Unlike the json module, the pickle is faster and works with many more Python types right out of the box, including custom-defined objects.
How can you write Python classes into a JSON file?
-Use the pickle.dump() method from the pickle module.
json.load() — reads the JSON document from the file;
json.loads() — converts the JSON string into the Python dictionary.
If you have a Python object, you can convert it into a JSON string by using the json.dump() and json.dumps() methods.
The difference is that json.dump() method is used to write Python serialized objects as JSON formatted data into a file, and json.dumps() method is used to encode any Python object into JSON formatted string.
The json (JavaScript Object Notation) module allows you to encode and decode data in a convenient format.
-
-
The decimal module supports fast and correctly rounded decimal floating point arithmetic. The decimal is a floating decimal point type with more precision and a smaller range than the float.
The dataclasses module provides a decorator and functions for automatically adding generated special methods such as init() and repr() to user-defined classes.
-
-
-