For Python programmers
Algorizm follows Python closely. If you know Python, you can read Algorizm at once, and this page tells you the rest.
The same program in both
In Algorizm:
sinf Hayvon:
funksiya yarat(ism, ovoz):
bu.ism = ism
bu.ovoz = ovoz
funksiya gapir():
chiqar(f"{bu.ism}: {bu.ovoz}!")
sinf Mushuk(Hayvon):
funksiya yarat(ism):
ota.yarat(ism, "Miyov")
har hayvon ichida [Hayvon("Olapar", "Vov"), Mushuk("Momiq")]:
hayvon.gapir()Output
Olapar: Vov! Momiq: Miyov!
In Python:
class Hayvon:
def __init__(self, ism, ovoz):
self.ism = ism
self.ovoz = ovoz
def gapir(self):
print(f"{self.ism}: {self.ovoz}!")
class Mushuk(Hayvon):
def __init__(self, ism):
super().__init__(ism, "Miyov")
for hayvon in [Hayvon("Olapar", "Vov"), Mushuk("Momiq")]:
hayvon.gapir()
What is the same
- Blocks are marked by a colon and indentation.
- Variables need no declaration, and values carry their own types.
- Lists, dictionaries, sets and tuples are written with the same brackets, and indexing and slices work the same way.
- f-strings,
+=, unpacking (a, b = b, a), default and named arguments, closures and generators. /always gives a real number,//rounds down, and%follows the sign of the divisor.- Scope: a name assigned in a function is local to it.
- Dictionaries keep the order of insertion.
Names
Keywords are listed on the page Keywords and operators, and every built-in function has its Python equivalent next to it on the page Built-in functions. The most common ones:
| Python | Algorizm | Python | Algorizm |
|---|---|---|---|
print |
chiqar |
len |
uzunlik |
input |
kirit |
range |
oraliq |
int |
butun |
float |
kasr |
str |
matn |
bool |
mantiqiy |
list |
ro'yxat |
dict |
lug'at |
set |
to'plam |
tuple |
kortej |
sorted |
saralangan |
enumerate |
raqamla |
zip |
juftla |
map |
qo'lla |
max |
eng_katta |
min |
eng_kichik |
sum |
yig'indi |
type |
tur |
.append |
.qo'sh |
.pop |
.o'chir |
.upper |
.katta |
.lower |
.kichik |
.split |
.ajrat |
.join |
.birlashtir |
.strip |
.qirq |
.replace |
.almashtir |
.keys |
.kalitlar |
.items |
.juftlar |
What is different
buis not a parameter. Methods do not listself;buis always there. The constructor is the methodyarat, andota.usul(...)calls the parent's method.globalalso does the work ofnonlocal. It declares a variable of the module or of an enclosing function.- Default values are worked out at every call, not once when the function is defined. A default such as
r=[]is therefore a fresh list each time, and a default may use earlier parameters. funksiya(x): ifodais the lambda. It uses the same keyword as a named function.- Anything can be thrown.
tashlaaccepts any value, andushla xatocatches every error; there are no exception classes and nofinally. Built-in errors arrive as text. birlashtirbelongs to the list:["a", "b"].birlashtir(", "), where Python writes", ".join(...).indeksandtopreturn-1when nothing is found, instead of raising an error.raqamla,juftla,qo'llaandfiltrlareturn lists, not lazy objects, and their pairs are two-item lists.yaxlitlarounds halves away from zero:yaxlitla(2.5)is3.- Whole numbers have 64 bits. A result that does not fit is an error, not a big number.
- A class body holds methods only.
xossa funksiyastands for@property, andstatik funksiyafor@staticmethod. - Modules are imported whole: there is
import nom, but nofrom … importand noas. - Apostrophes may be part of a name:
yo'l,qo'sh.
Not in version 0.1
- List comprehensions and generator expressions
- Nameless functions longer than one expression
- Named arguments for built-in functions,
*argsand**kwargs - Chained comparisons (
a < b < c); writea < b va b < c - Format rules inside f-strings (
{x:.2f}); write{formatla(x, '.2f')} - Slices with a step (
r[::2]) with,finally,match, decorators,async- Operator overloading and a custom text form for objects
classmethodand property setters- Changeable bytes (
bytearray) andb"..."literals - Complex numbers, big whole numbers, hexadecimal literals
- Reading a large file piece by piece, and network access
- Upper and lower case outside Latin, Latin-1 and Cyrillic letters