HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →
Live examsInternational live exams are comingTell us which exam you want first. Timed practice papers are open now.See what is coming →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Space Science and Astronomy
  • Earth and Environmental Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Python Programming
  2. /Python Basics
  3. /Conditionals: if, elif, else

Conditionals: if, elif, else

Python Programming · Middle Stage (6–8) · 2 courses, 26 worked problems, 52 practice questions

A program often has to choose. A heater with a thermostat turns on when the room is cold and off when it is warm. In Python, the if statement runs a block of code only when a condition is True. You add elif to test another condition and else for every other case. The code inside each branch is indented. Conditions use comparisons such as > and ==, and you can join them with and and or. With these, a program can give a grade from a mark or decide whether a number is even.

With practice you will be able to

  • Write an if, elif and else statement with indented blocks
  • Combine conditions with and, or and not
  • Turn a rule such as a grading scale into a chain of conditions

On the syllabus of

  • CBSE / State Class 12
  • Python Certification
  • AP
  • IB
  • A-Level
  • IGCSE

Lessons

  • If, Elif, Else: Making DecisionsWhy does if x = 5: give an error? Where do mistakes in a decision hide?Middle Stage (6–8)
  • And, Or, Not: Combining ConditionsIs every year divisible by 4 a leap year? Who gets into the club without a ticket?Middle Stage (6–8)

Practice questions

Choose an answer, then check it. Each one shows a full explanation.

An if statement has another if inside it, and the first test compares two strings. word = "Loop" if word < "loop": if len(word) > 3: print("A") else: print("B") elif word == "loop": print("C") else: print("D") What does the program print?

A program checks a basket, which is a list, before it adds a fruit. basket = ["apple", "pear"] fruit = "plum" if fruit in basket: print("have it") elif len(basket) < 3: basket.append(fruit) print("added", len(basket)) else: print("full") What does the program print?

A function picks a word for a temperature with if, elif and a final return. def label(t): if t > 30: return "hot" elif t > 20: return "warm" elif t > 10: return "mild" return "cold" print(label(30), label(10), label(21)) What does the program print?

Worked problems

Each one has a hint, the key idea and a full worked solution.

  • Say what each comparison gives, True or False.Easy
  • This program should give A for 90 and more, B for 75 to 89, C for 50 to 74, and D for the rest…Medium
  • The ticket price rules are: under 5 free, under 13 pays 50, under 60 pays 100, and the rest pay 70…Medium
  • This program should say a vowel only for the letters a, e, i, o and u…Medium
  • At a club door the guard asks the age first…Medium
  • Write a simpler condition, without a not in front of a bracket, that is the same as each of these…

Python labs

  • True or False?
  • Can you vote?
  • The grade
  • Positive, negative or zero
  • Boundary test
  • Project: the ticket office

Related topics

  • Python Basics
  • Getting Started with Python
  • Variables and Data Types
  • Operators and Expressions
  • Input and Output
  • Strings
  • Loops
  • Lists
  • Functions
  • Mini Projects

A function sorts values by whether they count as true or false in an if test. def describe(x): if x: return "full" elif x == 0: return "zero" else: return "empty" print(describe([]), describe(0.0), describe(" "), describe(None)) What does the program print?

A program reads an operator and a number from the keyboard, and passes both to a function. The user types + on the first line and 3 on the second. def apply(op, a, b): if op == "+": return a + b elif op == "*": return a * b return "?" op = input() x = input() print(apply(op, x, x), apply("-", 1, 2)) What does the program print?

Each number of a list should give one letter: A, B or C. The test in the if line is left out, and ____ stands in its place. for n in [-2, 0, 3]: if ____: print("A", end="") elif n: print("B", end="") else: print("C", end="") print() Which condition fills the gap so that the output is exactly BCA?

A function decides with not, or and and in one condition, and has no brackets to show the order. def pick(x, y): if not x or y and x > y: return "first" elif x == y or not y: return "second" return "third" print(pick(0, 5), pick(4, 2), pick(3, 3), pick(2, 9)) What does the program print?

A program uses or and and on values that are not Boolean. a = 0 b = "" c = "go" print(a or b or c, c and 5, a and c) What does the program print?

A function prints its argument and then answers a question about it. def check(n): print(n, end=" ") return n > 1 if check(1) and check(2) or check(3): print("yes") What does the program print?

A program uses chained comparisons and a mixed condition. x = 5 print(1 < x < 10, 10 > x > 7, x == 5 != 4, not x > 3 or x) What does the program print?

All 52 practice questions on Conditionals: if, elif, else →

Medium
  • What does each line show? print(5 > 3), print(4 == 4.0), print(7!= 7), print("apple" < "banana").Easy
  • Write a program that reads a whole number and says if it is even or odd…Easy
  • Write a program that reads an age. If the age is 18 or more, it shows You can vote…Easy
  • Read two numbers and show which one is bigger. If they are equal, say so.Medium
  • Read a number and say if it is positive, negative or zero.Medium
  • A grade is A for 90 or more, B for 75 or more, C for 50 or more and D for less than 50…Medium
  • All 26 worked problems on Conditionals: if, elif, else →