Skip to content
English

Values and variables

A program works with values. This page describes the simple kinds of value and what can be done with them.

Variables

A variable is a name for a value. It is created the first time something is assigned to it with =, and it can later be given another value, even of another kind.

son = 42
narx = 12500.5
ism = "Ali"
faol = rost
hech = bo'sh

chiqar(tur(son), tur(narx), tur(ism), tur(faol), tur(hech))
Output
butun kasr matn mantiqiy bo'sh

The function tur returns the name of a value's type as text. The simple types are:

Type Meaning Examples
butun whole number (64-bit) 42, -7, 1_000_000
kasr real number 3.14, 1e3, -0.5
matn text "Salom", 'Ali'
mantiqiy true or false rost, yolg'on
bo'sh no value bo'sh

A name may contain letters, digits, _ and the apostrophe, and must not begin with a digit. Upper and lower case are different, so Son and son are two names. The keywords of the language cannot be used as names.

Numbers and arithmetic

chiqar(7 + 2, 7 - 2, 7 * 2)
chiqar(7 / 2, 7 // 2, 7 % 2, 2 ** 10)
chiqar(7 > 2, 7 == 2, 7 != 2)
chiqar(rost va yolg'on, rost yoki yolg'on, emas rost)

x = 10
x += 5
x *= 2
chiqar(x)
Output
9 5 14
3.5 3 1 1024
rost yolg'on rost
yolg'on rost yolg'on
30
Operator Meaning
+ - * addition, subtraction, multiplication
/ division; the result is always a real number
// whole division, rounded down
% remainder of whole division
** power
+= -= *= /= change a variable: x += 5 means x = x + 5

Whole numbers and real numbers can be mixed freely; the result is real as soon as one side is real. A whole number has 64 bits; if a result would not fit, Algorizm stops with an error instead of giving a wrong answer. Dividing by zero is an error too.

Comparing and combining

The comparisons == (equal), != (not equal), <, <=, > and >= give rost or yolg'on. Conditions are combined with the words va (and), yoki (or) and emas (not).

Comparisons cannot be chained: instead of a < b < c write a < b va b < c.

Where a condition is expected, every value counts as true or false. yolg'on, bo'sh, 0, 0.0, empty text and empty collections are false; everything else is true.

Text

Text is written in double or single quotation marks; the two forms mean the same. Inside text, \n is a new line, \t a tab, \" a quotation mark and \\ a backslash.

ism = "Algorizm"
chiqar(uzunlik(ism), ism[0], ism[-1], ism[:5])
chiqar(ism.katta(), ism.kichik(), ism + "!", "-" * 12)
chiqar("gor" ichida ism, ism.almashtir("z", "th"))

soz = "Ўзбекистон"
chiqar(uzunlik(soz), soz[0], 'bir "ikki" uch', "bir\tikki")
Output
8 A m Algor
ALGORIZM algorizm Algorizm! ------------
rost Algorithm
10 Ў bir "ikki" uch bir	ikki
  • + joins two texts, and * repeats a text.
  • uzunlik gives the number of characters.
  • [i] takes one character. Counting starts at 0, and negative numbers count from the end.
  • [a:b] takes a part, from position a up to but not including b.
  • ichida tests whether one text occurs inside another.

Text is counted in characters, not bytes, so Cyrillic and every other script work as expected. Text cannot be changed in place; methods such as katta() and almashtir() return a new text. All text methods are listed on the page Methods.

Values inside text: f-strings

An f before the opening quotation mark turns text into a template. Every { } inside it may hold any expression, and its value is put into the text.

ism = "Guli"
yosh = 19
chiqar(f"{ism} {yosh} yoshda, kelasi yili {yosh + 1} ga kiradi")
chiqar(f"Narxi: {formatla(1250000, ',')} so'm")
chiqar(f"{{qavslar}} shunday yoziladi")
Output
Guli 19 yoshda, kelasi yili 20 ga kiradi
Narxi: 1,250,000 so'm
{qavslar} shunday yoziladi

To write a real brace, double it: {{ and }}. Inside { }, use the other kind of quotation mark for text, as in {formatla(narx, ',')}. Numbers are formatted with the function formatla.

Converting between types

Text and numbers are not mixed automatically: "5" + 5 is an error. Convert explicitly with butun, kasr, matn and mantiqiy.

chiqar(butun("42") + 1, kasr("3.5") * 2, matn(42) + "!")
chiqar(butun(3.99), butun("ff", 16), yaxlitla(3.14159, 2))
chiqar(mantiqiy(0), mantiqiy("salom"), mantiqiy([]))
Output
43 7.0 42!
3 255 3.14
yolg'on rost yolg'on

Printing and reading

chiqar prints its arguments separated by spaces and then starts a new line. yoz does the same but stays on the line. kirit shows a question, waits for the user to type a line, and returns it as text.

ism = kirit("Ismingiz nima? ")
yosh = butun(kirit("Yoshingiz nechada? "))
chiqar(f"Salom, {ism}! Kelasi yili {yosh + 1} yoshga kirasiz.")

kirit always returns text, so a number must be converted with butun or kasr. At the end of input it returns bo'sh.

Next

The next page, Conditions and loops, shows how a program makes decisions and repeats work.