Tingimuslause süntaks

Tingimuslauses kasutatakse koolonit ja kogu sisu peale tingimust on eristatud taandega. PyCharm ja teised IDE-d lisavad taanded automaatselt, kuid käsitsi saab taanet lisada Tab abil ja seda eemaldada Shift+Tab, kui kursor on rea alguses.

Taane on osa Pythoni süntaksist ja seda ei tohi ära jätta. Kogu koodist ei pea siin aru saama, kuid eesmärk on jälgida taanet. Võtmesõnu if-elif-else tutvustatakse allpool.

number = 4
if number < 10:  # The if statement is executed because 4 is smaller than 10.
    print("The number is too small, it should be increased.")
    if number < 5:  # The if statement is executed because 4 is smaller than 5.
        number += 2  # number = number + 2
        print("The number was increased by two.")
    else:
        number += 1
        print("The number was increased by one")
else:
    print("The number is big enough.")
print(f"The number is {number} now.")

# Output:
# The number is too small, it should be increased.
# The number was increased by two.
# The number is 6 now.

Võtmesõna if

Kõige lihtsam tingimuslause on moodustatud if tingimusest. Koodiplokk käivitub siis, kui if tingimuses on tõeväärtus True. Kui tõeväärtuseks tuleb False, siis jääb koodiplokk vahele.

Koodiplokk käivitub, sest tingimus on True.

# no pec # You can also edit the code name = "Alice" if name == "Alice": # The if statement is executed because (variable) name equals to "Alice". print("Hi", name) # --> "Hi Alice"

Koodiplokk ei käivitu, sest tingimus on False.

# no pec animal = "cat" if animal == "zebra": # The if statement is not executed because (variable) animal does not equal to "zebra". print("Hello")

if voodiagrammina:

../_images/if_flowchart.png

Võtmesõna else

Võtmesõna else on valikuline tingimus, mis lisatakse if järele ja sellele järgnev plokk käivitatakse vaid siis, kui if ja kõik järgnevad (kui on) elif tingimuslausete tõeväärtused on False.

name = "Tim"
if name == "Tim":  # The if statement is executed because the condition is True.
    print("Hello", name)
else:
    print("Hello, Stranger")

# Output:
# Hello Tim
# no pec age = 62 if age < 20: # Condition is False. print("You are too young.") elif 20 <= age <= 60: # Condition is False. print("You may proceed.") else: # The else statement is executed because previous conditions were False. print("You are too old.") # Output: # You are too old.

Võtmesõna elif

Kui soovitakse, et üks mitmest tingimustest käivituks, siis on selleks sobiv kasutatada võtmesõna elif. See on lühike vorm koodist else: if. Kui if ei käivitu, siis kontrollitakse järgmist elif tingimust ja nii kuni üks neist on True. Käivituda saab ainult üks plokk.

number = 2
if number > 3:
    print("The number is larger than 3")
elif number <= 3:  # The elif statement is executed.
    print("The number is smaller or equal to 3")

# Output:
# The number is smaller or equal to 3

Kui üks tingimustest on True, siis edasi ei vaadata.

number = 2
if number > 3:
    print("The number is larger than 3")
elif number <= 3:
    print("The number is smaller or equal to 3")
elif number == 2:
    print("The number is 2")
elif number >= 0:
    print("The number is positive")

# Output:
# The number is smaller or equal to 3

Võrdluseks: kasutades ainult if tingimusi.

number = 2
if number > 3:
    print("The number is larger than 3")
if number <= 3:
    print("The number is smaller or equal to 3")
if number == 2:
    print("The number is 2")
if number >= 0:
    print("The number is positive")

# Output:
# The number is larger than 3
# The number is 2
# The number is positive

if-elif-else

Võtmesõna pass on vajalik, kui süntaks nõuab käsku, kuid sisu on tühi.

condition_0, condition_1, condition_2 = False, False, False

if condition_0:
    pass
    # Executed if condition_0 is True.
elif condition_1:
    pass
    # Executed if condition_0 is False and condition_1 is True.
elif condition_2:
    pass
    # Executed if condition_0 is False and condition_1 is False and condition_2 is True.
else:
    pass
    # Executed if all previous statements are False.