Conditions and loops
Blocks and indentation
A line that ends with a colon opens a block, and the lines of the block are indented, by four spaces as a rule. The block ends where the indentation goes back. All lines of one block must be indented by the same amount.
A block of a single statement may stay on the same line, after the colon: agar x > 5: chiqar("katta").
agar, yoki agar, aks holda
agar runs its block when the condition is true. Any number of yoki agar branches may follow, each with its own condition, and a final aks holda runs when none of the conditions was true. Only the first branch that fits is run.
ball = 78
agar ball >= 86:
chiqar("a'lo")
yoki agar ball >= 71:
chiqar("yaxshi")
yoki agar ball >= 56:
chiqar("qoniqarli")
aks holda:
chiqar("qoniqarsiz")
agar ball > 50 va ball % 2 == 0: chiqar("juft va 50 dan katta")yaxshi juft va 50 dan katta
| Algorizm | Meaning |
|---|---|
agar shart: |
if the condition is true |
yoki agar shart: |
otherwise, if this condition is true |
aks holda: |
otherwise |
toki
toki repeats its block for as long as the condition stays true. The condition is checked before every round, so the block may run zero times.
son = 1
toki son < 100:
son *= 2
chiqar(son)128
toki rost: loops forever; such a loop is left with to'xta.
har … ichida
har goes through the items of a sequence one after another: the items of a list, the characters of a text, the keys of a dictionary, the numbers of a range, and so on.
har i ichida oraliq(5):
yoz(i, "")
chiqar()
har i ichida oraliq(10, 0, -3):
yoz(i, "")
chiqar()
har meva ichida ["olma", "anor", "uzum"]:
chiqar(meva)
har harf ichida "abc":
yoz(harf.katta())
chiqar()0 1 2 3 4 10 7 4 1 olma anor uzum ABC
oraliq produces whole numbers without storing them all:
| Call | Numbers |
|---|---|
oraliq(5) |
0, 1, 2, 3, 4 |
oraliq(2, 6) |
2, 3, 4, 5 |
oraliq(10, 0, -3) |
10, 7, 4, 1 |
The end is never included. The third argument is the step, which may be negative.
Several loop variables
When every item is itself a pair, har can take it apart into several names. raqamla numbers the items of a sequence, juftla walks through several sequences side by side, and the dictionary method juftlar() gives keys together with their values.
mevalar = ["olma", "anor", "uzum"]
har i, meva ichida raqamla(mevalar, 1):
chiqar(f"{i}. {meva}")
har ism, yosh ichida juftla(["Ali", "Guli"], [17, 19]):
chiqar(ism, yosh)1. olma 2. anor 3. uzum Ali 17 Guli 19
to'xta and davom
to'xta leaves the innermost loop at once. davom skips the rest of the current round and goes on with the next one. Both work in toki and in har.
har son ichida oraliq(1, 20):
agar son % 2 == 0:
davom
agar son > 9:
to'xta
chiqar(son)1 3 5 7 9
mayli
A block cannot be empty. Where nothing should happen, write mayli, which does nothing. It is useful for a function or class that will be filled in later.
funksiya keyinroq_yoziladi():
mayli
har i ichida oraliq(3):
mayli
chiqar("tayyor")tayyor
Next
Lists, dictionaries and other collections describes the values that hold many other values.