Skip to main content
Informatik.uz
Chapter 1. Python Basics

The if and else Statements

A program needs the conditional statement to make decisions. We study comparison operators, if, else, elif and logical operators, and write programs that work with age and scores.

Topic 3 of 112 min readUpdated October 27, 2026
All topics (3 of 11)

Programs often have to behave differently depending on the situation. The conditional statement if is used for this: if the condition is true, certain commands are executed. A condition is the result of a comparison, and it takes the value True or False.

The comparison operators are == (equal), != (not equal), <, >, <= and >=. Notice that a single = is used to assign a value, while a double == is used to check equality. Several conditions can be combined with the words and, or and not.

First we enter the age: age = int(input("Yoshingiz: ")). Then the first line is if age >= 18: and the second line, indented by 4 spaces, is print("Siz kattasiz") (Uzbek for “You are an adult”). The indentation after the colon is very important in Python: it shows which commands belong to the condition.

If the condition is false, the indented lines are skipped. The action for a false condition uses else: you write the line else: and under it, again indented, for example print("Siz hali kichiksiz"). If there are several cases, elif (“else if”) is added: a grading program has the parts if, elif and else one after another. Python checks the conditions from top to bottom, runs the first true part and skips the rest.

Key takeaways

  • if runs commands when the condition is true, and else gives the path for a false condition.
  • A condition is a comparison that gives True or False.
  • = assigns a value, while == checks equality.
  • Commands that belong to a condition are written with an indent (4 spaces).
  • elif checks several cases in turn; and, or and not combine conditions.

Practice tasks

  1. Write a program that asks for the age and prints “Siz kattasiz” if it is 18 or more, and “Siz hali kichiksiz” otherwise.
  2. Write a program that decides whether an entered number is even or odd: the condition n % 2 == 0 means the number is even.
  3. A score (from 0 to 100) is entered: using if, elif and else, write a program that prints “excellent” for 90 and above, “good” for 70 and above, and “satisfactory” otherwise.

Review questions

Test yourself: think first, then reveal the answer.

  1. 1How do the signs = and == differ?
    = assigns a value to a variable, while == checks whether two values are equal.
  2. 2What is elif for?
    To check several conditions in turn: the first true part is executed.
  3. 3What shows which commands belong to a condition in Python?
    The indentation (4 spaces) of the lines after the colon.

0 of 11 topics completed

Sign in to mark
Informatika 9-sinf darsligi muqovasi
In the textbookInformatics — Grade 9