livepython.net
CHAPTER 08If Statements
8.5  PRACTICE

Python if Statement Practice Questions for Beginners

Chapter 8 Recap and Practice

Your programs can now make decisions. Here is the chapter in one table:

You writeIt does
if age >= 18:runs the indented block only when the condition is True
4 spacesthe indentation that marks a block; it must line up exactly
else:the other path, when the condition is False
elif marks >= 75:another condition; only the first True one runs
a and bTrue only when both are True
a or bTrue when at least one is True; write both comparisons in full
not aflips True and False
(a and b) or cbrackets show which parts go together
an if inside an ifa nested if: a second question after a yes
if b != 0:check input before you use it

And the checklist when an if misbehaves: == not =, the colon, the indentation, numbers converted with int(), and the order of your elifs.

Practice questions

Use input() with no prompt text. The tests try several inputs, including the edges (like exactly 18, or exactly 100), so test your program with those too.

The if statement

  1. 59.Python Program to Check If a Number Is PositiveEasySolve
  2. 60.Python Program to Apply a Discount on Orders Over 1000EasySolve

else and elif

  1. 61.Python Program to Check If a Number Is Even or OddEasySolve
  2. 62.Python Program to Find the Larger of Two NumbersEasySolve
  3. 63.Python Program to Calculate a Grade from MarksMediumSolve

and, or, not

  1. 64.Python Program to Check If a Number Is Between 1 and 100EasySolve
  2. 65.Python Program to Check If a Day Is a WeekendEasySolve
  3. 66.Python Program to Check If a Year Is a Leap YearMediumSolve

Nested if and checking input

  1. 67.Python Program to Check a Username and PasswordMediumSolve
  2. 68.Python Program to Divide Two Numbers SafelyEasySolve
  3. 69.Python Program to Make a Simple ATMHardSolve

Now your programs can choose. Next, they'll learn to remember many values at once: lists.