Institute of Computer Science
  1. Courses
  2. 2025/26 fall
  3. Introduction to Programming (MTAT.03.236)
ET
Log in

Introduction to Programming 2025/26 fall

  • Home Page
  • Introduction
  • Expressions
  • Conditional Execution
  • Functions
  • Iterations
  • Strings
  • Files
  • Lists
  • Graphics?
< previousTable of Contentsnext >

2.6 LIBRARIES AND PACKAGES

Libraries are collections of pre-written code that developers can use in writing their programs. This makes software composable and means you don't have to reinvent the wheel every time. A library you will interact with throughout the course and have seen plenty of already during this chapter is the Python Standard Library, which includes the essential built-in functions and types and mathematical modules, like math and random.

Each library has documentation explaining how to use the library's contents. Here's a link to the documentation to the Python Standard Library Mathematical functions.


# We have to specify which library we want to use through the import command
import math

# Accessing the pow() function within the random library to raise x to the power of y
power_of = math.pow(3, 4) 
print(power_of) 

# We can also specify the specific function that we want to use at the time of importing
from math import pow

# Accessing the pow() function within the random library to raise x to the power of y
power_of = pow(5, 2)
print(power_of)

Note that the pow function converts both arguments to type float. This differs from the exponentiation operator ** where such a conversion does not occur.

Commonly, you would only use specific imports if you need only a few functions from a library. If those functions are used frequently, importing them directly can be more straightforward and efficient. If you're using many different library parts, import the whole library. However, in the context of this course, feel free to use it whichever way you feel more comfortable.

KEY THINGS TO REMEMBER

  • Libraries are collections of pre-written code that you can utilize in your projects.
  • Use the import command to import the entire library
  • Use the from library import function command to import a specific function from a library.
  • Specific imports are best when only a few functions are used and needed frequently.
  • Full imports are ideal when multiple functions from a library are used.
< previousTable of Contentsnext >

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment