Skip to main content
EDU-MMCS
You are currently using guest access (Log in)

Programming Fundamentals (Python)

  1. Home
  2. Courses
  3. Осенний семестр
  4. Python Eng
  5. Module 1: Basics and Conditions
  6. Module 1.2 – Conditional Statements

Module 1.2 – Conditional Statements

Completion requirements

Theory link: https://labs-org.ru/python-eng-2-theory/

 Python Programming: Module 1 – Conditional Statements

For English‑speaking students  |  if, elif, else, and logical operators

📑 Table of Contents

  • 🔹 Lab 3 – Password Checker
  • 🔹 Lab 4 – Even or Odd
  • 🔹 Lab 5 – Age Group Classifier
  • 🔹 Lab 6 –
  • 🔹 Lab 7 –
  • 🔹 Lab 8 –
  • 🔹 Lab 9 –
  • 🔹 Practice Exercises
  • 🔸 Answer Key

📘 Laboratory Works 

Each lab assignment includes a step‑by‑step algorithm, solution code, and expected output. Max score: 0.1 per lab.

Lab 3 – Password Checker

Score: 0.1

Task: Write a program that compares a password and its confirmation. If they match, print "Password accepted", otherwise print "Password not accepted".

📌 Algorithm

  1. Read the password from the user.
  2. Read the confirmation from the user.
  3. Compare the two strings using ==.
  4. If they match, print "Password accepted".
  5. Otherwise, print "Password not accepted".

💻 Solution Code


password = input("Enter password: ")
confirm = input("Confirm password: ")

if password == confirm:
    print("Password accepted")
else:
    print("Password not accepted")
                

📤 Expected Output (examples)

Enter password: qwerty
Confirm password: qwerty
Password accepted
Enter password: qwerty
Confirm password: Qwerty
Password not accepted
📖 Step‑by‑Step Explanation
  1. input() reads the password and confirmation as strings.
  2. The == operator compares the two strings exactly.
  3. If they are equal, the if block executes; otherwise, the else block executes.
  4. Each block prints the corresponding message.

Lab 4 – Even or Odd

Score: 0.1

Task: Write a program that determines if a number is even or odd.

📌 Algorithm

  1. Read the number as an integer.
  2. Check if the remainder when divided by 2 is 0 using %.
  3. If remainder is 0 → print "Even".
  4. Otherwise → print "Odd".

💻 Solution Code


number = int(input("Enter a number: "))

if number % 2 == 0:
    print("Even")
else:
    print("Odd")
                

📤 Expected Output (examples)

Enter a number: 4
Even
Enter a number: 7
Odd
📖 Step‑by‑Step Explanation
  1. Read the number as an integer.
  2. Use the modulo operator % to get the remainder of division by 2.
  3. If remainder is 0, the number is even; otherwise, odd.
  4. Print the appropriate message.

Lab 5 – Age Group Classifier

Score: 0.1

Task: Write a program that determines a person's age group based on their age:

  • 0–13: childhood
  • 14–24: youth
  • 25–59: maturity
  • 60+: old age

📌 Algorithm

  1. Read age as an integer.
  2. Check each age range in order using if/elif.
  3. The first matching range determines the output.
  4. Ages 60+ fall into the else category.

💻 Solution Code


age = int(input("Enter age: "))

if age <= 13:
    print("childhood")
elif age <= 24:
    print("youth")
elif age <= 59:
    print("maturity")
else:
    print("old age")
                

📤 Expected Output (examples)

Enter age: 11
childhood
Enter age: 27
maturity
📖 Step‑by‑Step Explanation
  1. Read the age as an integer.
  2. Check conditions from smallest to largest range.
  3. The first if/elif that evaluates to True prints the corresponding group.
  4. If none match, the else block handles ages 60+.

Lab 6 – Three-Digit Number Checker

Score: 0.1

Task: Write a program that checks if a number is three‑digit.

📌 Algorithm

  1. Read the number as an integer.
  2. Check if it is between 100 and 999 (inclusive).
  3. Use chained comparison 100 <= num <= 999.
  4. Print the corresponding message.

💻 Solution Code

num = int(input("Enter a number: "))

if 100 <= num <= 999:
    print("The number is three-digit")
else:
    print("The number is not three-digit")

📤 Expected Output

Enter a number: 123
The number is three-digit
Enter a number: 45
The number is not three-digit

Lab 7 – All Digits Different

Score: 0.5

Task: Write a program that checks if all three digits of a three‑digit number are different.

📌 Algorithm

  1. Read the number as integer.
  2. Extract each digit using // and %.
  3. Compare all three digits: d1 != d2 and d1 != d3 and d2 != d3.
  4. Print appropriate message.

💻 Solution Code

num = int(input("Enter a number: "))
d1 = num // 100
d2 = (num // 10) % 10
d3 = num % 10

if d1 != d2 and d1 != d3 and d2 != d3:
    print("All digits are different")
else:
    print("Digits are not all different")

📤 Expected Output

Enter a number: 123
All digits are different
Enter a number: 122
Digits are not all different

Lab 8 – Coordinate Quadrant

Score: 0.5

Task: Determine the quadrant of a point (x, y) not on the axes.

📌 Algorithm

  1. Read x and y coordinates as integers.
  2. Check the sign of both coordinates.
  3. Each quadrant has a unique combination of signs.
  4. Use and to combine conditions.

💻 Solution Code

x = int(input("Enter x: "))
y = int(input("Enter y: "))

if x > 0 and y > 0:
    print("Quadrant 1")
elif x < 0 and y > 0:
    print("Quadrant 2")
elif x < 0 and y < 0:
    print("Quadrant 3")
elif x > 0 and y < 0:
    print("Quadrant 4")

📤 Expected Output

Enter x: 3
Enter y: 4
Quadrant 1
Enter x: -2
Enter y: 5
Quadrant 2

Lab 9 – Rook Move (Chess)

Score: 0.5

Task: Given two different chess squares, determine if a rook can move from the first to the second in one move.

Rook Rule: A rook can move any number of squares horizontally or vertically.

📌 Algorithm

  1. Read the coordinates of both squares (row, col).
  2. A rook moves if either row or column is the same.
  3. If same row OR same column → valid move.
  4. Otherwise → invalid.

💻 Solution Code

row1 = int(input("Enter row1: "))
col1 = int(input("Enter col1: "))
row2 = int(input("Enter row2: "))
col2 = int(input("Enter col2: "))

if row1 == row2 or col1 == col2:
    print("YES")
else:
    print("NO")

📤 Expected Output

Enter row1: 4
Enter col1: 4
Enter row2: 4
Enter col2: 2
YES

✏️ Practice Exercises (Independent Work)

Try these on your own. Each practice includes a hint. 

Practice 1 – True or false?

Score: 0.2

Task: Two integers are given. Check if the following statement is True: the first number is greater than the second (the program must return True if it is true, and False otherwise).

Enter two integers
>>> 23
>>> 1
23 is greater than 1 is True 

Practice 2 – True or false?

Score: 0.2

Task: Two integers are entered. Check the truth of the statement: the first number is not equal to the second (the program must return True if it is true and False otherwise).

Enter two integers
>>>5 
>>>5
5 is not equal to 5 is False

Practice 3 – True or false?

Score: 0.3

Task: Three integers are given: the values of variables A, B, C. Check the truth of the double inequality A < B < C. Make sure that your program is correct with at least two input data sets.

Enter three integers
>>>3 >>>6 >>>2
3 is less than 6 less than 2 is False

Enter three integers >>>2 >>>5 >>>7 2 is less than 5 less than 7 is True

Practice 4 – True or false?

Score: 0.5

Task: Three-digit integer is given. Check the truth: the first digit (left digit) of the number is less than the second (middle) and third (right).

Enter a three digit number
>>> 854
8 is less than 5 and 4 is : False

Practice 5 – True or false?

Score: 0.4

Task: Two integers are entered. Check the truth of the statement: at least one of these numbers is odd.

Enter two integer numbers
>>> 9 >>> 2
Either 9 or 2 is an odd number. This is : True

Check the following:

-5,  8 => True
 12, 0 => False
 6, -1 => True
 11, 7 => True

Practice 6 – If statement

Score: 0.2

Task: An integer is entered. If this is positive number, you should add 1 to it. Output the result.

Enter an integer number
>>> -77
The result is -77

Check the following:

-3 => -3
 0 =>  0
 1 =>  2
 5 =>  6

Practice 7 – If statement

Score: 0.2

Task: An integer is given. If this is even number then multiply it by 10. Output the result.

Enter an integer number
>>> 8
The result is 80

Check the following:

-3 => -3
 2 => 20
 1 =>  1
 -10 =>  -100

Practice 8 – If - Else

Score: 0.3

Task: Determine if a user has access: if age ≥ 18 → access granted, otherwise access denied.

Enter age: 16
Access denied

Hint: Simple if/else.

📖 Algorithm
  1. Read age.
  2. If age >= 18, grant access; else deny.

Practice 9 – If - Else

Score: 0.3

Task: An integer is given. If this is an even number, then multiply it by 3, if it is not even, then divide it by 3. Output result.

Enter an integer number
>>> 12
The result is 36

Check the following:

4 => 12
9 =>  3
-10 => -30

Practice 10 – If - Else

Score: 0.3

Task: An integer is given. If this is a positive number then add 1 to it; otherwise subtract it by 2. Output result.

Enter an integer number
>>> 48
The result is 49

Check the following:

-3 => -5
 0 =>  1
 1 =>  2
 5 =>  6

Practice 11 – Triangle Validity

Score: 0.3

Task: Check if three sides can form a triangle (sum of any two sides > third side).

Enter side a: 3
Enter side b: 4
Enter side c: 5
Triangle exists

Hint: Check all three conditions.

📖 Algorithm
  1. Read three sides.
  2. Check if a+b>c, a+c>b, b+c>a.
  3. If all true, triangle exists; else not.

Practice 12 – Access with Parental Consent

Score: 0.5

Task: Determine access:

  • If age ≥ 18 → access granted
  • If age ≥ 14 AND parental consent → access granted
  • Otherwise → access denied
Enter age: 16
Parental consent (yes/no): yes
Access granted

Hint: Combine conditions with and.

📖 Algorithm
  1. Read age and consent.
  2. Check age >= 18 or (age >=14 and consent).

Practice 13 – Nested If statements (Elif)

Score: 0.4

Task: An integer is given (age of the person). If it is greater than or equal to 18, then output "you can watch this movie"; if the number is less than 10, then output "you should watch the cartoon". Otherwise, print "you can take a walk".

how old are you?
>>> 8
you should watch the cartoon
how old are you?
>>> 15
you can take a walk

Practice 14 – Nested If statements (Elif)

Score: 0.4

Task: The program must request the time of a day in hours (from 1 to 24). Depending on the time entered, display a message indicating what time of a day the entered hour belongs to (midnight (24), night (1-4), morning (5-11), day (12-16), evening (17-23)).

what's time of the day?
>>> 2
"night"
what's time of the day?
>>> 24
"midnight"

Practice 15 – Nested If statements (Elif)

Score: 0.4

Task: The student received a grade. If it is 2 points, then the program should output "it's very bad"; if it is a 3 - program should print "it's bad"; if it is 4 - "it's good", in case of 5 - "it's excellent", otherwise - "there are no such marks".

what's your grade?
>>> 2
"it's very bad"
what's your grade?
>>> 2
"it's very bad"

Practice 16 – Nested If statements (Triangle Type)

Score: 0.5

Task: Extend the previous program. If triangle exists, determine its type: equilateral, isosceles, or scalene.

Enter side a: 3
Enter side b: 3
Enter side c: 3
Equilateral triangle

Hint: Use nested conditions after validity check.

📖 Algorithm
  1. Check validity first.
  2. If valid, compare sides to classify.

Practice 17 – In operator (Vowel Checker)

Score: 0.4

Task: Check if a single character is a vowel (a, e, i, o, u) using the in operator.

Enter a character: a
YES

Hint: Use char in 'aeiouAEIOU'.

📖 Algorithm
  1. Read a character.
  2. Check if it's in the set of vowels.
  3. Print YES or NO.

Practice 18 – Elif (Discount Calculator)

Score: 0.5

Task: Calculate final price based on total and promo code:

  • If total ≥ 10000 and has promo code → 10% discount
  • If total ≥ 10000 and no promo code → 5% discount
  • If total < 10000 → no discount
Enter total: 12000
Enter promo code: PROMO18
Discount: 10%
Final price: 10800.0

Hint: Use nested if or elif.

📖 Algorithm
  1. Read total and promo code.
  2. Determine discount rate based on conditions.
  3. Compute final price.

Practice 19 – Max and Min (Maximum of Two Numbers)

Score: 0.3

Task: Write a program that reads two numbers and prints the larger one.

Enter first number: 7
Enter second number: 17
17

Hint: Use if/else to compare.

📖 Algorithm
  1. Read two numbers.
  2. Compare them; print the greater one.
  3. If equal, print either.

Practice 20 – Three Number Maximum

Score: 0.4

Task: Read three numbers and print the maximum.

Enter first number: 5
Enter second number: 12
Enter third number: 8
Maximum: 12

Hint: Compare using nested if or chained comparisons.

📖 Algorithm
  1. Read three numbers.
  2. Compare and find the largest using if/elif.

Practice 21 – Maximum and Minimum

Score: 0.4

Task: A two-digit number is given. Find the minimum and the maximum among its digits and swap the digits in the number.

Enter two-digit number
>>> 74    
max = 7, min = 4, swapped = 47
Enter two-digit number
>>> 7    
Incorrect input! should be two-digit number

Practice 22 – Match statement (Time of Day)

Score: 0.5

Task: Output the time of day based on hour:

  • 0–5: night
  • 6–11: morning
  • 12–17: afternoon
  • 18–23: evening
Enter hour: 14
Afternoon

Hint: Use match with ranges.

📖 Algorithm
  1. Read hour.
  2. Check which range it belongs to and print.

Practice 23 – Match statement (Simple Calculator)

Score: 0.5

Task: Perform addition, subtraction, multiplication, or division using the match statement.

Enter first number: 10
Enter second number: 5
Enter operation (ADD, SUB, MUL, DIV): MUL
Result: 50

Hint: Use match with string cases.

📖 Algorithm
  1. Read two numbers and operation.
  2. Use match to select operation.
  3. Handle division by zero.

Practice 24 – If and Equations

Score: 0.5

Task: For a given real x find the value of the following function f.

Enter a real number
>>> -4
The result of function f is 4
Enter a real number
>>> 1.5
The result of function f is 2.25

🔑 Answer Key – Practice Exercises

Solutions for the independent practice exercises. Check your work after attempting them.

Practice 1 – Solution

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if a > b:
    print(a)
elif b > a:
    print(b)
else:
    print(a)  # equal

Practice 2 – Solution

age = int(input("Enter age: "))

if age >= 18:
    print("Access granted")
else:
    print("Access denied")

Practice 3 – Solution

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b and a >= c:
    maximum = a
elif b >= a and b >= c:
    maximum = b
else:
    maximum = c

print("Maximum:", maximum)

Practice 4 – Solution

a = float(input("Enter side a: "))
b = float(input("Enter side b: "))
c = float(input("Enter side c: "))

if a + b > c and a + c > b and b + c > a:
    print("Triangle exists")
else:
    print("Triangle does not exist")

Practice 5 – Solution

a = float(input("Enter side a: "))
b = float(input("Enter side b: "))
c = float(input("Enter side c: "))

if a + b > c and a + c > b and b + c > a:
    if a == b == c:
        print("Equilateral triangle")
    elif a == b or a == c or b == c:
        print("Isosceles triangle")
    else:
        print("Scalene triangle")
else:
    print("Triangle does not exist")

Practice 6 – Solution

char = input("Enter a character: ")

if len(char) == 1 and char in 'aeiouAEIOU':
    print("YES")
else:
    print("NO")

Practice 7 – Solution

total = float(input("Enter total: "))
promo = input("Enter promo code: ")

if total >= 10000 and promo == "PROMO18":
    discount = 0.10
elif total >= 10000:
    discount = 0.05
else:
    discount = 0

final_price = total * (1 - discount)
print("Discount:", discount * 100, "%")
print("Final price:", final_price)

Practice 8 – Solution

hour = int(input("Enter hour: "))

if 0 <= hour <= 5:
    print("Night")
elif 6 <= hour <= 11:
    print("Morning")
elif 12 <= hour <= 17:
    print("Afternoon")
elif 18 <= hour <= 23:
    print("Evening")
else:
    print("Invalid hour")

Practice 9 – Solution

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Enter operation (ADD, SUB, MUL, DIV): ")

match op:
    case "ADD":
        print("Result:", a + b)
    case "SUB":
        print("Result:", a - b)
    case "MUL":
        print("Result:", a * b)
    case "DIV":
        if b != 0:
            print("Result:", a / b)
        else:
            print("Error: Division by zero")
    case _:
        print("Unknown operation")

Practice 10 – Solution

age = int(input("Enter age: "))
consent = input("Parental consent (yes/no): ")

if age >= 18:
    print("Access granted")
elif age >= 14 and consent.lower() == "yes":
    print("Access granted")
else:
    print("Access denied")

📋 Quick Reference – Conditional Statements

if Statement

if condition:
    # code block

if/else

if condition:
    # code if True
else:
    # code if False

if/elif/else

if cond1: # ...
elif cond2: # ...
else: # ...

Comparison Operators

  • == equal
  • != not equal
  • > greater
  • < less
  • >= ≥
  • <= ≤

Logical Operators

  • and – both true
  • or – at least one true
  • not – reverses truth

Precedence: not > and > or

Chained Comparisons

if 3 <= age <= 6:
    print("Child")

© 2026 Python Module 1 – Conditional Statements  |  Keep coding!

◄ Module 1.1 – Fundamentals

Blocks

Skip Navigation

Navigation

  • Home

    • Site pages

      • My courses

      • Tags

    • My courses

    • Courses

      • Осенний семестр

        • Прикладная математика и информатика

        • Фундаментальная информатика и ИТ

        • Математика, механика

        • Педагогическое образование

        • Магистратура

          • Разработка мобильных приложений и компьютерных игр

        • Аспирантура

        • Вечернее отделение

        • Другое

        • ОНС-Н_1

        • Прогр-ММ-2

        • Прогр-ММ-1

        • АБиМ-ИИБ26

        • МСиТВ_АБМ_2026

        • Python Eng

          • General

          • Module 1: Basics and Conditions

            • AssignmentModule 1.1 – Fundamentals

            • AssignmentModule 1.2 – Conditional Statements

          • Module 2: LOOPs

          • Module 3: Strings and Slices

          • Topic 3

          • Topic 4

          • Topic 5

          • Topic 6

          • Topic 7

          • Topic 8

          • Topic 9

          • Topic 10

        • Экзамен ИКТ

        • ТестИИ

        • Информатика-Осень-ПМИ-2

        • Информатика-осень-ПМИ-1

        • ИММвс

      • Весенний семестр

        • Прикладная математика и информатика

        • Фундаментальная информатика и ИТ

        • Математика, механика

        • Педагогическое образование

        • Магистратура

          • Разработка мобильных приложений и компьютерных игр

        • Аспирантура

        • Вечернее отделение

        • Другое

      • Воскресная компьютерная школа

        • Пользователь компьютера плюс

        • Пользователь прикладных программ

        • Программирование I ступень

        • Программирование II ступень

        • Программирование III ступень

        • Архив

      • Воскресная математическая школа

        • Открытое тестирование РНОМЦ и мехмата ЮФУ

          • Открытое тестирование РНОМЦ и мехмата ЮФУ - 2026

          • Открытое тестирование РНОМЦ и мехмата ЮФУ - 2025

        • Олимпиадная математическая школа

        • Повышение квалификации

        • Доступная математика

        • Лаборатория математического онлайн-образования мех...

        • Осенняя универсиада

        • Научно-практическая конференция

        • ВМШ

          • ВМШ -2025

        • Летняя олимпиадная математическая школа РНОМЦ и ме...

      • Государственная итоговая аттестация

      • Дополнительное образование

      • Олимпиады

      • Видеолекции

      • Разное

      • Архив курсов

      • Заочная школа мехмата ЮФУ

Supplementary blocks

You are currently using guest access (Log in)
Python Eng
Data retention summary
Get the mobile app
Яндекс.Метрика