Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Exploring Standard Libraries | Modules and Imports
Python Advanced Concepts
course content

Conteúdo do Curso

Python Advanced Concepts

Python Advanced Concepts

1. Modules and Imports
2. Error Handling
3. File Handling
4. Pytest Framework
5. Unittest Framework
6. Iterators and Generators

book
Exploring Standard Libraries

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:

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.

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.
123
import os print(os.getcwd()) # Outputs the current working directory.
copy
Tarefa
test

Swipe to show code editor

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.

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! 🚀

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 5
toggle bottom row

book
Exploring Standard Libraries

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:

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.

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.
123
import os print(os.getcwd()) # Outputs the current working directory.
copy
Tarefa
test

Swipe to show code editor

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.

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! 🚀

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 5
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt