Sonlar bilan ishlashning tashqi funksiya va metodlari
Sonlar bian ishlash uchun quyidagi tashqi funksiyalar mo’ljallangan:
int ([[, ]]) – obyektni butun sonaga o’tkazish. Ikkinchi parametr sanoq sistemasini anglatib, ko’rsatish majburiy emas (odatiy holda 10 lik deb qabul qilinadi). Masalan:
>>> int(7. 5), int ("71", 10) , int ("0o71", 8) , int ("0xA", 16)
(7, 71, 57, 10)
>>> int(), int ("0b11111111", 2)
(0, 255 )
float ([]) - son yoki satrni haqiyqiy songa o’tkazish. Masalan:
>>> float (7), float ("7.1") , float ("12.")
(7 .0, 7.1, 12.0)
>>>float ("inf") , float ("-Infinity" ), float ("nan" )
(inf, -inf, nan)
>>> float ()
0.0
bin ( ) – o’nlik sonni ikkilikka o’tkazish. Sonlarni satr ko’rinishida taqdim qilinadi. Masalan:
>>> bin(255), bin(l) , bin(- 45)
('0b1111111' , '0b1' , '-0b101101')
oct ( ) - o’nlik sonni sakkizlik songa o’tkazish. Sonlarni satr ko’rinishida taqdim qilinadi. Masalan:
>>> oct(7), oct(8), oct(64)
('0o7 ' , '0ol0 ' , '0ol00' )
hex ( ) - o’nlik sonni o’n oltilik songa o’tkazish. Sonlarni satr ko’rinishida taqdim qilinadi. Masalan:
>>> hex (l0) , hex(l6) , hex (255)
('0ха' , '0xl0' , '0xff ')
round ( [, ]) – kasr qismi 0.5 dan kichik bo’lsa sonni o’zi, katta bo’lsa eng yaqin katta songa aylantiriladi. Agar kasr qismi 0.5 ga teng bo’lsa, eng yaqin juft songa aylantiriladi. Masalan:
>>> round (0.49), round (0.50), round(0.51)
(0, 0, 1)
>>> round (1.49), round(l.50), round (l.51)
(1, 2, 2)
>>> round (2.49), round(2.50), round(2.51)
(2, 2, 3)
>>> round(3.49), round(3.50), round(3.51)
(3, 4, 4)
Ikkinchi parametrverguldan keying raqamlar sonini olish uchun ko’rsatiladi. Biroq majburiy emas, ko’rsatilmasa 0 deb qabul qilinadi (son butunga yahlitlanadi):
>>> round (l.524, 2), round (l.525, 2), round (l.5555, 3)
(1.52, 1.52, 1.556)
abs () - sonning absolyut qiymatini qaytaradi:
>>> abs (-10) , abs (l0) , abs (-12.5 )
(1 0, 10, 12 . 5)
pow (, [, ]) - ni ga ko’tarish:
>>> pow (l0, 2), 10**2, pow (3, 3), 3**3
(100, 100, 27, 27 )
Agar vuchinchi parametr ko’rsatilsa, olingan natijani shu parametrga bo’lishda qoldiq qismi qaytariladi:
>>> pow(l0, 2, 2) , (10 ** 2)%2, pow(3, 3, 2), (3**3) % 2
(0, 0, 1, 1)
max ( ) - ro’yxatdagi sonlarning eng kattasini aniqlash:
>>> max (l, 2, 3) , max (3, 2, 3, 1), max (l, 1.0 ) , max (l.0, 1)
(3, 3, 1, 1.0 )
min ( ) - ro’yxatdagi sonlarning eng kichigini aniqlash:
>>> min(l, 2, 3) , min(3, 2, 3, 1) , min (l, 1.0) , min (l.0, 1)
(1, 1, 1, 1.0 )
sum ( [, ]) - ketma-ketlik (masalan: ro’yxat, kortej) elementlari yig’indisini aniqlash plyus . Agar ikkinchi parametr ko’rsatilmasa, u 0 ga teng deb qabul qilinadi. Agar ketma-ketlik bo’sh bo’lsa, ikkinchi parametr qiymati olinadi. Masalan:
>>> sum ((10, 20, 30, 40 )), sum ([10, 20, 30, 40])
(100 , 100)
>>> sum ([10, 20, 30 , 40] , 2) , sum ([ ], 2)
(102 , 2)
divmod (х, у) - ikki qiymatli (birinchi qiymat –bo’linma, ikkinchi qiymat–qoldiq (х//у, х%у)) kortej qaytaradi:
>>> divmod (l3, 2) # 13 == 6 * 2 + 1
(6, 1)
>>> 13//2, 13%2
(6, 1)
>>> divmod (13.5, 2.0) # 13 .5 == 6.0 * 2.0 + 1.5
(6 .0, 1.5 )
>>> 13.5 // 2.0, 13 .5%2.0
(6.0 , 1.5 )
Do'stlaringiz bilan baham: |