choice() – funksiyasi roʻyxatdan bitta tasodifiy elementni qaytaradi.
import random
sonlar = [1, 2, 3, 4, 5, 6, 7, 8]
random.shuffle(sonlar)
print(sonlar)
random_son = random.choice(sonlar)
print(random_son)
Dastur natijasi:
math() moduli Pythonning o'rnatilgan math moduli matematik, trigonometrik va logarifmik amallarni bajarish uchun funktsiyalar to'plamini taqdim etadi. Modulning asosiy xususiyatlaridan ba'zilari:
pow(son, daraja): sonni darajaga oshirish
sqrt(son): sonning kvadrat ildizi
ceil(son): Raqamni eng yaqin butun songa yaxlitlash
floor(son): sonni eng yaqin eng kichik butun songa yaxlitlash
factorial(son): sonning faktoriali
degrees (rad): radiandan gradusga o'tkazish
radian (grad): gradusdan radianga o'tkazish
cos(rad): burchakning radiandagi kosinusi
sin(rad): burchakning radiandagi sinusi
tan(rad): burchakning radiandagi tangensi
acos(rad): radiandagi burchakning yoy kosinasi
asin(rad): burchakning radianlarda arksinus
atan(rad): radiandagi burchakning yoy tangensi
log(n, asos): n ning ixtiyoriy asosli logarifmi
log10(n): n ning 10 lik logarifmi
Ba'zi funktsiyalardan foydalanishga misol:
import math
n1 = math.pow(2, 3)
print(n1)
print(math.sqrt(9))
print(math.ceil(4.56))
print(math.floor(4.56))
print(math.degrees(3.14159))
print(math.radians(180))
print(math.cos(math.radians(60)))
print(math.sin(math.radians(90)))
print(math.tan(math.radians(0)))
print(math.log(8,2))
print(math.log10(100))
Matematik modul shuningdek, PI va E kabi bir qator o'rnatilgan konstantalarni taqdim etadi:
import math
radius = 30
print(“pi=“,math.pi)
# radiusi 30 bo'lgan doira yuzi
S = math.pi * math.pow(radius, 2)
print(S)
# 10 sonining natural logarifmi
print(“e=“,math.e)
son = math.log(10, math.e)
print(son)
Dastur natijasi: