Built-in functions
Built-in functions are always available; nothing has to be imported. Arguments in square brackets are optional. Built-in functions take positional arguments only.
Printing and reading#
Function
Python
Meaning
chiqar(...)
print
prints the values separated by spaces, then a new line
yoz(...)
print(end="")
like chiqar, but stays on the same line
kirit([savol])
input
shows the question, reads one line and returns it as text; bo'sh at the end of input
chiqar ("Salom" , 42 , [1 , 2 ])
yoz ("bir " )
yoz ("qatorda" )
chiqar ()
chiqar ("oxiri" )Output
Salom 42 [1, 2]
bir qatorda
oxiri
Types and conversion#
Function
Python
Meaning
tur(x)
type
the name of the type as text: "butun", "matn", "ro'yxat" …; for an object, the name of its class
butun(x[, asos])
int
whole number from a real number (the fraction is cut off) or from text; asos is the base of the text, from 2 to 36
kasr(x)
float
real number from a whole number or text
matn(x)
str
text form of any value
mantiqiy(x)
bool
rost or yolg'on
ro'yxat([x])
list
empty list, or a list of the items of a sequence
lug'at([x])
dict
empty dictionary, or a copy of a dictionary
to'plam([x])
set
empty set, or the distinct items of a sequence
kortej([x])
tuple
empty tuple, or a tuple of the items of a sequence
baytlar([x])
bytes
bytes from text (UTF-8) or from a list of numbers 0 to 255
turdanmi(x, tur)
isinstance
is x of this type; tur is a class, a type function such as butun or matn, or a list of these
chiqar (tur (1 ), tur (1.5 ), tur ("a" ), tur (rost ), tur (bo'sh ))
chiqar (tur ([]), tur ({}), tur ({1 }), tur ((1 ,)), tur (oraliq (3 )), tur (baytlar ("a" )))
chiqar (turdanmi (5 , butun), turdanmi ("a" , [butun, matn]), chaqiriladimi (chiqar))Output
butun kasr matn mantiqiy bo'sh
ro'yxat lug'at to'plam kortej oraliq baytlar
rost rost rost
Numbers#
Function
Python
Meaning
mutlaq(x)
abs
absolute value
yaxlitla(x[, xona])
round
rounds to the nearest whole number, or to xona decimal places; halves are rounded away from zero
daraja(x, y)
pow
x to the power y, the same as x ** y
bo'linma_qoldiq(a, b)
divmod
the list [a // b, a % b]
eng_katta(...)
max
the largest of several values, or of the items of one sequence; with a function as second argument, compares by f(x)
eng_kichik(...)
min
the smallest, in the same way
yig'indi(r)
sum
the sum of the items of a sequence
oraliq(n), oraliq(a, b[, qadam])
range
the whole numbers from 0 (or a) up to, but not including, n (or b)
chiqar (mutlaq (-7 ), yaxlitla (3.14159 , 2 ), yaxlitla (2.5 ), daraja (2 , 8 ))
chiqar (eng_katta (3 , 9 , 4 ), eng_kichik ([3 , 9 , 4 ]), yig'indi ([1 , 2 , 3 ]))
chiqar (bo'linma_qoldiq (17 , 5 ))
chiqar (ikkilik (5 ), sakkizlik (15 ), o'n_oltilik (255 ), butun ("ff" , 16 ))
chiqar (belgi (65 ), kod ("A" ), ko'rinish ("matn" ))Output
7 3.14 3 256
9 3 6
[3, 2]
0b101 0o17 0xff 255
A 65 "matn"
Text and characters#
Function
Python
Meaning
uzunlik(x)
len
number of characters of a text, or of items in a collection or range
belgi(kod)
chr
the character with this code: belgi(65) is "A"
kod(belgi)
ord
the code of a one-character text
ikkilik(n)
bin
text in base 2: "0b101"
sakkizlik(n)
oct
text in base 8: "0o17"
o'n_oltilik(n)
hex
text in base 16: "0xff"
ko'rinish(x)
repr
the value as it is written in a program; text keeps its quotation marks
formatla(x, andoza)
format
formatted text, described below
formatla(x, andoza) turns a number or text into text of a chosen shape. The pattern has the form [[fill]align][sign][0][width][, or _][.precision][type]; every part is optional.
chiqar (formatla (3.14159 , ".2f" ))
chiqar (formatla (1234567 , "," ))
chiqar (formatla (42 , "6" ) + "|" )
chiqar (formatla ("ali" , "*^7" ))
chiqar (formatla (7 , "03" ))
chiqar (formatla (0.256 , ".1%" ))
chiqar (formatla (255 , "x" ), formatla (5 , "b" ))Output
3.14
1,234,567
42|
**ali**
007
25.6%
ff 101
Part
Meaning
width
the least number of characters; numbers go to the right, text to the left
< > ^
align left, right or centre; a character before it is used to fill
0
fill a number with zeros
, or _
separator between thousands
.N
number of decimal places, or the greatest length of a text
d
whole number
f
real number with a fixed number of decimal places
e
real number with an exponent
%
percentage
x X b o
whole number in base 16, 2 or 8
s
text
Inside an f-string, write {formatla(x, '.2f')}.
Sequences#
These functions accept any sequence: a list, tuple, text, set, dictionary (its keys), range or generator. Each leaves its argument unchanged.
Function
Python
Meaning
saralangan(r[, f])
sorted
sorted list; with f, sorted by f(x)
teskarisi(r)
reversed
list in reverse order
raqamla(r[, boshi])
enumerate
list of [number, item] pairs, numbered from 0 or from boshi
juftla(a, b, ...)
zip
list of [a[i], b[i], ...] groups, as long as the shortest argument
qo'lla(f, r)
map
list of f(x) for every item
filtrla(f, r)
filter
list of the items for which f(x) is true
hammasi(r)
all
rost if every item is true
birortasi(r)
any
rost if at least one item is true
takrorlagich(r)
iter
an iterator that hands out the items one at a time
keyingi(t[, standart])
next
the next item of an iterator or generator; at the end an error, or standart if given
sonlar = [3 , 1 , 2 ]
chiqar (saralangan (sonlar), teskarisi (sonlar), sonlar)
chiqar (hammasi ([rost , 1 , "a" ]), birortasi ([0 , "" , bo'sh ]))
chiqar (raqamla (["a" , "b" ]), juftla ([1 , 2 ], ["bir" , "ikki" ]))
chiqar (qo'lla (matn, [1 , 2 ]), filtrla (funksiya (x): x > 1 , sonlar))
chiqar (ro'yxat ("abc" ), ro'yxat (oraliq (3 )), kortej ([1 , 2 ]), to'plam ([1 , 1 , 2 ]))Output
[1, 2, 3] [2, 1, 3] [3, 1, 2]
rost yolg'on
[[0, "a"], [1, "b"]] [[1, "bir"], [2, "ikki"]]
["1", "2"] [3, 2]
["a", "b", "c"] [0, 1, 2] (1, 2) {1, 2}
Objects#
Function
Python
Meaning
avlodmi(sinf, ota_sinf)
issubclass
is the class the same as, or derived from, the other class
chaqiriladimi(x)
callable
can x be called: a function, method or class
xossa_bormi(o, nom)
hasattr
does the object have this field or method
xossa_ol(o, nom[, standart])
getattr
reads a field or method by name
xossa_qo'y(o, nom, qiymat)
setattr
sets a field by name
xossa_o'chir(o, nom)
delattr
removes a field
xossalar(x)
dir
sorted list of the names of fields and methods
xesh(x)
hash
the hash number of a value that cannot change
kimlik(x)
id
a number that identifies an object while the program runs
Running text as code, and help#
Function
Python
Meaning
hisobla(matn)
eval
works out one expression given as text and returns its value
bajar(matn)
exec
runs statements given as text; names they create stay in the module
yordam([x])
help
prints a short description of a function, method, module, class or value; without an argument, lists everything built in
chiqar (hisobla ("2 + 3 * 4" ))
bajar ("x = 5\nchiqar(x * 2)" )
chiqar (x)
yordam (uzunlik)Output
14
10
5
uzunlik(x)
Matn, ro'yxat, lug'at, to'plam yoki oraliqdagi elementlar sonini qaytaradi.
The text given to hisobla and bajar sees the names of the module, but not the local variables of the function that called it. Never pass text that came from someone else to these functions: it can do anything a program can do.
Previous Keywords and operators
Next Methods