4.1 KiB
4.1 KiB
09 – Python Standard Library
This document introduces some of the most commonly used Python standard library modules:
datetimemathrandomdecimal
These modules come bundled with Python and require no external installation.
1. Date and Time with datetime
The datetime module provides classes for working with dates and times.
Working with Dates
Code
import datetime
date_1 = datetime.date(2026, 1, 1)
print(date_1.year)
print(date_1.month)
print(date_1.day)
print(date_1.weekday)
print(date_1.ctime)
Explanation
datetime.date(year, month, day)creates a date object..year,.month,.dayaccess individual components.
Important Note
date_1.weekday()
-
Returns the day of the week as an integer:
- Monday = 0
- Sunday = 6
date_1.ctime()
- Returns a human-readable string representation of the date.
Working with Time
Code
time_1 = datetime.time(12, 12)
print(time_1.hour)
print(time_1.min)
Explanation
datetime.time(hour, minute)creates a time object..hourreturns the hour..minutereturns the minute.
Working with Date and Time Together
Code
abbas_birth = datetime.datetime(2026, 1, 1, 12, 12)
today = datetime.date.today()
now = datetime.datetime.now()
diff_time = now - abbas_birth
Explanation
datetime.datetimeincludes both date and time.date.today()returns today’s date.datetime.now()returns the current date and time.- Subtracting two
datetimeobjects returns atimedelta.
2. Mathematical Operations with math
The math module provides advanced mathematical functions and constants.
Mathematical Constants
import math
print(math.pi)
print(math.e)
print(math.inf)
math.pi: π constantmath.e: Euler’s numbermath.inf: infinity
Power and Rounding
print(math.pow(2, 3))
print(round(4.2))
print(round(4.8))
math.pow(a, b)returnsaraised to the power ofb.round()rounds to the nearest integer.
Floor and Ceil
print(math.floor(4.2))
print(math.floor(4.9))
print(math.ceil(4.2))
print(math.ceil(4.9))
floor: rounds downceil: rounds up
Logarithms
print(math.log(100, 10))
- Returns the logarithm of 100 with base 10.
3. Random Values with random
The random module is used to generate pseudo-random values.
Random Numbers
import random
print(random.randint(1, 6))
print(random.random())
randint(a, b): random integer betweenaandb(inclusive)random(): random float between0and1
Random Selection
number_list = list(range(15))
print(random.choice(number_list))
char_list = ['a', 'm', 's']
print(random.choice(char_list))
choice()selects a random element from a sequence.
Shuffling
random.shuffle(number_list)
print(number_list)
shuffle()randomly rearranges the list in place.
4. Decimal Precision with decimal
The decimal module provides precise decimal arithmetic, avoiding floating-point errors.
Decimal Context
import decimal
print(decimal.getcontext())
- Shows current precision and rounding settings.
Float vs Decimal
print(decimal.Decimal(0.1))
print(decimal.Decimal('0.1'))
- Passing a float carries floating-point error.
- Passing a string preserves exact value.
Precision Comparison
print(0.1 + 0.2 == 0.3)
Returns False due to floating-point precision issues.
print(decimal.Decimal(0.1) + decimal.Decimal(0.2) == decimal.Decimal(0.3))
Still False because the floats are imprecise.
print(decimal.Decimal('0.1') + decimal.Decimal('0.2') == decimal.Decimal('0.3'))
Returns True because strings preserve precision.
Summary
datetimehandles dates and timesmathprovides mathematical constants and functionsrandomgenerates pseudo-random valuesdecimalsolves floating-point precision problems- Always use strings when creating
Decimalvalues