Python Standard Library: Math, Random, Datetime, OS

Python ya zo tare da adadin daidaitattun ɗakunan karatu masu amfani don taimakawa tare da ayyuka gama gari a cikin shirye-shirye. Anan akwai gabatarwa ga shahararrun ɗakunan karatu kamar math, random, datetime da os:

math Laburare

Laburaren math yana ba da ayyuka na lissafi da ayyuka. Yana ba ku damar yin ƙididdige ƙididdiga masu rikitarwa kamar lambobin zagaya, lissafin logarithms, ƙididdige abubuwa, da ƙari.

Misali:

import math  
  
print(math.sqrt(25))   # Output: 5.0  
print(math.factorial(5))   # Output: 120  

 

random Laburare

Laburaren random yana ba da kayan aiki don aiki tare da lambobin bazuwar. Kuna iya ƙirƙirar lambobi bazuwar, zaɓi wani bazuwar kashi daga lissafin, ko yin ayyuka daban-daban masu alaƙa da bazuwar.

Misali:

import random  
  
print(random.random())   # Output: a random float between 0 and 1  
print(random.randint(1, 10))   # Output: a random integer between 1 and 10  

 

datetime Laburare

Laburaren datetime yana ba da kayan aikin aiki tare da kwanan wata da lokuta. Yana ba ku damar samun kwanan wata na yanzu, lokacin tsari, da lissafin bambanci tsakanin kwanakin biyu.

Misali:

import datetime  
  
current_date = datetime.date.today()  
print(current_date)   # Output: current date in the format 'YYYY-MM-DD'  
  
current_time = datetime.datetime.now()  
print(current_time)   # Output: current date and time in the format 'YYYY-MM-DD HH:MM:SS'  

 

os Laburare

Laburaren os yana ba da kayan aiki don hulɗa tare da tsarin aiki. Kuna iya yin ayyuka kamar ƙirƙira da share kundayen adireshi, samun jerin fayiloli a cikin kundin adireshi, canza kundin adireshin aiki na yanzu, da ƙari.

Misali:

import os  
  
current_dir = os.getcwd()  
print(current_dir)   # Output: current working directory  
  
os.mkdir("new_folder")   # create a new folder named "new_folder"  

 

Waɗannan ɗakunan karatu a Python suna sauƙaƙe da inganci don yin ayyuka na gama gari. Bugu da ƙari, Python yana da sauran ɗakunan karatu da yawa don gudanar da ayyuka daban-daban a cikin shirye-shirye.