STR Coding Club logo STR Coding ClubLearn to code, together
Python and Practice

Python If Else Exercises With Six Beginner Problems

You understand what an if statement is when you read about it, but a blank editor and a problem like "print a grade from a score" still freezes you. That gap is normal.

A student at a coding club table typing on a laptop with handwritten notes about if and else branches on paper beside the keyboard

You understand what an if statement is when you read about it, but a blank editor and a problem like "print a grade from a score" still freezes you. That gap is normal. Reading about conditions builds recognition, while writing them builds recall, and only practice closes the distance. These Python if else exercises are the six we use at club sessions to move beginners from "I get it" to "I can write it".

Each exercise has a goal, a hint and the common mistake we see most. Try each one for at least ten minutes before reading the hint.

A two-minute refresher on if, elif and else

Python checks conditions from top to bottom and runs the first block whose condition is True. Everything else is skipped.

  • if starts the chain and is always required.
  • elif means "otherwise, if this", and you can have as many as you need.
  • else catches anything left over and takes no condition.

Each line ends with a colon, and the code that belongs to it is indented by four spaces. Comparison operators are "==" for equal, "!=" for not equal, and the familiar less than, greater than, at most and at least symbols you know from maths. You can combine conditions with "and", "or" and "not".

That is genuinely all the syntax you need for every exercise below.

Six Python if else exercises to try

1. Even or odd

Ask the user for a whole number with "int(input())". Print "even" if it divides by 2 with no remainder, otherwise print "odd".

Hint: the remainder operator "%" gives what is left after division, so "n % 2 == 0" is True for even numbers.

Common mistake: writing "=" instead of "==". One equals sign assigns a value, two compare.

2. Grade from a score

Take a score from 0 to 100. Print A for 90 and above, B for 80 to 89, C for 70 to 79, D for 60 to 69 and F below 60.

Hint: start from the highest band and work down with elif. Because Python stops at the first True condition, you only need "score at least 80" for B, not a range check.

Common mistake: using five separate if statements. A score of 95 then prints A, B, C and D.

3. Ticket price

A cinema charges 5 pounds for children under 12, 7 pounds for people 65 and over, and 10 pounds for everyone else. Ask for an age and print the price.

Hint: three branches, one if, one elif, one else.

Common mistake: forgetting that input returns text, so comparing the age with 12 crashes until you convert with int().

4. Leap year checker

A year is a leap year if it divides by 4, except years that divide by 100, unless they also divide by 400. So 2024 and 2000 are leap years, 1900 is not.

Hint: test the 400 rule first, then the 100 rule, then the 4 rule. Order matters more than cleverness here.

Common mistake: trying to write it all in one long line with "and" and "or" before the logic is clear. Write the three-step version first.

5. Password strength

Ask for a password. Print "weak" if it has fewer than 8 characters, "okay" if it has 8 or more but no digit, and "strong" if it has 8 or more and at least one digit.

Hint: "len(password)" gives the length, and "any(c.isdigit() for c in password)" checks for a digit. If that second line looks scary, loop through the string instead, which our Python string practice exercises cover step by step.

Common mistake: checking for the digit before checking the length, so a three-character password with a number prints "strong".

6. Rock, paper, scissors judge

Ask two players for their choice and print who wins, or "draw".

Hint: handle the draw first with one comparison. Then you only need to list the three ways player one wins. Everything else is a win for player two.

Common mistake: writing all nine combinations by hand. It works, but it takes 30 lines when 8 will do, and every extra line is a place for a typo.

How to practice these without copying answers

At club nights we follow a simple rule: plan on paper, then code, then test. For each exercise, write the branches as plain sentences first, such as "if score at least 90, say A". Then translate each sentence into one line of Python.

Testing is where most learning happens. For the grade exercise, try 100, 90, 89, 60, 59 and 0. Those boundary values catch almost every bug, because mistakes in conditions nearly always live at the edges. If a test fails and you cannot see why, our guide on getting unstuck on a practice problem without peeking walks through a calm way to find the problem.

Give yourself a time box. Twenty minutes per exercise is plenty. If you finish early, change one rule and adapt the code, for example adding a 3 pound discount on Tuesdays to the ticket price.

Why you should not rush to one-line conditionals

Many tutorials teach the shortcut "result = 'even' if n % 2 == 0 else 'odd'" early, and some beginners start writing everything that way because it looks more advanced. We push back on this in the first month.

One-line conditionals are fine for two simple outcomes. They become hard to read the moment there is a third case, and they hide the structure you are trying to learn. When a beginner writes the full if, elif and else version, they can see each branch, add a print statement inside it, and spot which one runs. That visibility is worth far more than saving two lines.

The same goes for deeply nested ifs. If you find yourself three levels deep, step back. Usually an "and" or an early elif flattens the code and makes the logic easier to test.

What to learn after conditions

Once these six exercises feel easy, combine conditions with loops. A loop that asks for numbers until the user types "stop", printing "even" or "odd" each time, uses everything above plus one new idea. Our explainer on Python loops explained with things you already do is the natural next step.

After that, wrap each exercise in a small function. Turning the grade checker into "def grade(score):" that returns a letter makes it easy to test and reuse.

Frequently asked questions

What is the difference between elif and else in Python?

An elif has its own condition and only runs if every earlier condition was False and its own condition is True. An else has no condition and runs when nothing above it matched. A chain can have many elif lines but only one else at the end.

Can you have an if without an else in Python?

Yes. An if on its own simply does nothing when the condition is False. Add an else only when you actually need to handle the leftover case.

How many if else exercises should a beginner do?

Aim for 10 to 15 small problems spread over a week rather than 30 in one evening. Spacing practice out helps the syntax stick. Move on once you can write a three-branch chain without looking anything up.

BN
Bartholomew Ng

Bartholomew taught maths for years before switching to teaching Python. He writes about the basics of the language and about teaching code without drowning learners in syntax.

More posts by Bartholomew

More in Python and Practice

01doorganico 084goad 3hot8 4465456 after5internprogram ahandfulofblessings ahmadpc ahomcybersecurity allodialresources annodex antiquesandperiodcollectibles aotc asianworldkashmir beedlectrics bloggertemplatefree bluefeatheragency bodyweightsolution campaign08blog carfixadvisor catstreetcollective cbstvsports cnfanspurchasing committeeof500yearsofdignityandresistance covertopsdjs cs-kaiyuangame cuestacomunicaciontotal cxoutlet dancebetgame deskgearhub destinyvillastay detectmagazine di33 diamondsteelbuildings donmo dyanadropship emarsysfinance emimattl escapechan eskitarih evautoplay everythingweddings-orlando farmzone feedviewerapp foxstudiosscratch gamevui123 gleasongrade greetingsday hacklabasu haskell iw-kaiyun jagoanproperti jilidskripsi joltbusinessfinance juegodepokergratis juegospucca kaeru-festival latestmehndidesigns lestanzediottaviano litemagazines localcommunityhero lustrsazan mariashebets menswheels newwebstechs1 nikkit ootheday oxigenvpn plurfitness prefetial probudgetcarpetcleaning psychicmrsmartin pyaten qvantor rejoignezlogis rexzbot riselayouts ruchin rwxqsgd5 sakaltime shibiaowang shop-bot skrtips spotlesshaven tetapatrium topselller uneplanteartificielle vipertechcr webtech1223411 wo-kygaming yamaha-mekar yeknava yoybuysheets zenunwa yamaha-mekar jilidskripsi ankertoto ankertoto ankertoto