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
course content

Course Content

Python Advanced Concepts

Exploring Standard LibrariesExploring 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:

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.

To gain more practice, here's a project on Python Basics: Date and Time Mastery

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.

Task

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

Everything was clear?

Section 1. Chapter 5
toggle bottom row
course content

Course Content

Python Advanced Concepts

Exploring Standard LibrariesExploring 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:

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.

To gain more practice, here's a project on Python Basics: Date and Time Mastery

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.

Task

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

Everything was clear?

Section 1. Chapter 5
toggle bottom row
some-alt