Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
Overig

Libraries and Modules in Python: A Comprehensive Guide with Examples

Beoordeling
-
Verkocht
-
Pagina's
6
Geüpload op
24-01-2025
Geschreven in
2024/2025

This document introduces libraries and modules in Python, explaining how to use built-in and external libraries. Learn about Python’s standard library, importing modules, and popular libraries like NumPy, pandas, and Matplotlib with practical examples. Perfect for first-year Computer Science students.

Meer zien Lees minder
Instelling
Vak

Voorbeeld van de inhoud

Libraries and Modules in Python
Libraries and modules are essential to Python, enabling developers to reuse code,
reduce redundancy, and leverage powerful prebuilt functionalities. Python comes
with a rich standard library, and you can also install third-party libraries to extend
its capabilities.



1. What are Modules and Libraries?
 A module is a file containing Python code (functions, classes, variables) that
can be imported and reused.
 A library is a collection of modules packaged together for specific tasks.

Example:

A single .py file is a module, whereas libraries like NumPy or Pandas contain
multiple modules.



2. Importing Modules
Python provides the import statement to use modules in your program.

Importing a Module:

import math
print(math.sqrt(16)) # Output: 4.0

Importing Specific Functions or Variables:

from math import sqrt, pi
print(sqrt(25)) # Output: 5.0
print(pi) # Output: 3.141592653589793

Using Aliases:

, import math as m
print(m.pow(2, 3)) # Output: 8.0

Importing All Functions (Not Recommended):

from math import *
print(sin(pi / 2)) # Output: 1.0


3. Built-in Libraries
Python's standard library includes modules for common tasks. Here are some
popular ones:

1. os: Operating System Operations

import os
print(os.getcwd()) # Get the current working directory
os.mkdir("test_folder") # Create a new folder
os.rmdir("test_folder") # Remove the folder

2. sys: System-Specific Parameters and Functions

import sys
print(sys.version) # Python version
print(sys.argv) # Command-line arguments

3. datetime: Date and Time Operations

from datetime import datetime
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S")) # Output: e.g., 2024-12-24
10:30:45




4. random: Generating Random Numbers

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
24 januari 2025
Aantal pagina's
6
Geschreven in
2024/2025
Type
OVERIG
Persoon
Onbekend

Onderwerpen

€6,62
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kun je een ander document kiezen. Je kunt het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper
Seller avatar
rileyclover179

Maak kennis met de verkoper

Seller avatar
rileyclover179 US
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
-
Lid sinds
1 jaar
Aantal volgers
0
Documenten
252
Laatst verkocht
-

0,0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Populaire documenten

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Veelgestelde vragen