Skip to content
English

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

  • bu is not a parameter. Methods do not list self; bu is always there. The constructor is the method yarat, and ota.usul(...) calls the parent's method.
  • global also does the work of nonlocal. 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): ifoda is the lambda. It uses the same keyword as a named function.
  • Anything can be thrown. tashla accepts any value, and ushla xato catches every error; there are no exception classes and no finally. Built-in errors arrive as text.
  • birlashtir belongs to the list: ["a", "b"].birlashtir(", "), where Python writes ", ".join(...).
  • indeks and top return -1 when nothing is found, instead of raising an error.
  • raqamla, juftla, qo'lla and filtrla return lists, not lazy objects, and their pairs are two-item lists.
  • yaxlitla rounds halves away from zero: yaxlitla(2.5) is 3.
  • 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 funksiya stands for @property, and statik funksiya for @staticmethod.
  • Modules are imported whole: there is import nom, but no from … import and no as.
  • 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, *args and **kwargs
  • Chained comparisons (a < b < c); write a < 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
  • classmethod and property setters
  • Changeable bytes (bytearray) and b"..." 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