Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Python Standard Library: Essential Modules for Everyday Tasks | Mastering Python Modules and Imports
Python Advanced Concepts

book
Python Standard Library: Essential Modules for Everyday Tasks

Python's standard (built-in) libraries are a set of modules included with every Python installation. They provide a range of functionality that allows you to add features to your programs without installing additional modules. Let's explore a few essential libraries that you'll find yourself using often.

The math Library

The math library includes functions for mathematical operations beyond basic arithmetic. It provides access to the mathematical functions defined by the C standard.

Example uses:

import math

# Calculating powers
print(math.pow(2, 3)) # Output: 8.0

# Finding square roots
print(math.sqrt(16)) # Output: 4.0
1234567
import math # Calculating powers print(math.pow(2, 3)) # Output: 8.0 # Finding square roots print(math.sqrt(16)) # Output: 4.0
copy

Here’s a list of some of the most useful functions within the math library:

FunctionDescriptionExample
sqrt(x)Computes the square root of xsqrt(3) == 9
pow(x, y)Returns x raised to the power of ypow(2, 3) == 8
ceil(x)Returns the smallest integer greater than or equal to xceil(9.2) == 10
floor(x)Returns the largest integer less than or equal to xfloor(9.2) == 9
exp(x)Calculates e raised to the power of x, where e is the base of natural logarithmsexp(1) == 2.72
sin(x), cos(x), tan(x)These functions return the sine, cosine, and tangent of x, which is in radianscos(pi) == -1.0
radians(x)Converts degrees to radiansradians(pi) == 0.0548
degrees(x)Converts radians to degreesdegrees(0.0548) == 3.14

The datetime Library

When you need to work with dates and times, the datetime library is your go-to solution. It can handle date transformations, time zones, and more.

import datetime

# Getting today's date
today = datetime.date.today()
print(today) # Output: YYYY-MM-DD

# Calculating a future date
future = today + datetime.timedelta(days=10)
print(future) # Output: YYYY-MM-DD + 10 days
123456789
import datetime # Getting today's date today = datetime.date.today() print(today) # Output: YYYY-MM-DD # Calculating a future date future = today + datetime.timedelta(days=10) print(future) # Output: YYYY-MM-DD + 10 days
copy

Other Notable Libraries

  • os: provides a way of using operating system dependent functionality like reading or writing to files;
  • sys: provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter;
  • json: for parsing JSON data into Python objects, and vice versa.
import os

print(os.getcwd()) # Outputs the current working directory.
123
import os print(os.getcwd()) # Outputs the current working directory.
copy
Tarefa

Swipe to start coding

Complete the Python code to perform specific mathematical calculations using the appropriate functions from the math library.

  1. Import math library;
  2. Calculate the natural logarithm of 10 and print the result;
  3. Calculate the factorial of 5 and print the result;
  4. Find the sine, cosine, and tangent of 30 degrees (after converting degrees to radians) and print the results;
  5. Use both ceil and floor functions on the number 9.2 and print the results.

Solução

import math

# Calculating the natural logarithm of 10
log_value = math.log(10)
print("The natural logarithm of 10 is:", log_value)

# Calculating the factorial of 5
factorial_value = math.factorial(5)
print("The factorial of 5 is:", factorial_value)

# Finding the sine, cosine, and tangent of 30 degrees
# First convert 30 degrees to radians
radians = math.radians(30)
sin_value = math.sin(radians)
cos_value = math.cos(radians)
tan_value = math.tan(radians)
print("The sine of 30 degrees is:", sin_value)
print("The cosine of 30 degrees is:", cos_value)
print("The tangent of 30 degrees is:", tan_value)

# Using ceil and floor on 9.2
ceil_value = math.ceil(9.2)
floor_value = math.floor(9.2)
print("The ceiling value of 9.2 is:", ceil_value)
print("The floor value of 9.2 is:", floor_value)

Congratulations! 🎉 You’ve just harnessed the power of multiple standard libraries to create a useful tool. In our next chapter, we’ll explore advanced importing techniques that will further enhance your Python prowess. Stay curious and keep coding! 🚀

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 5
___ ___

# Calculating the natural logarithm of 10
log_value = math.___(10)
print("The natural logarithm of 10 is:", log_value)

# Calculating the factorial of 5
factorial_value = ___.___(5)
print("The factorial of 5 is:", factorial_value)

# Finding the sine, cosine, and tangent of 30 degrees
# First convert 30 degrees to radians
radians = ___.radians(30)
sin_value = ___.___(radians)
cos_value = math.___(radians)
tan_value = math.___(radians)
print("The sine of 30 degrees is:", sin_value)
print("The cosine of 30 degrees is:", cos_value)
print("The tangent of 30 degrees is:", tan_value)

# Using ceil and floor on 9.2
ceil_value = ___.___(9.2)
floor_value = math.___(9.2)
print("The ceiling value of 9.2 is:", ceil_value)
print("The floor value of 9.2 is:", floor_value)
toggle bottom row
some-alt