Start free

GCSE · 14 exercises

Python conditions

An if statement runs its indented lines only when the condition is true; elif tries another condition; else catches everything left.

Compare with == (equal), != (not equal), <, <=, >, >=, and join conditions with and, or, not.

n = int(input())
if n > 0:
    print("positive")
elif n < 0:
    print("negative")
else:
    print("zero")

Watch for: = gives a value, == compares. And the order of elif matters: the first true one wins, so test the narrow case first.

Exercises

  1. Even or oddRead a whole number and say whether it is even or odd.Beginner
  2. Positive, negative or zeroRead a number and say whether it is positive, negative or zero.Beginner
  3. The larger numberRead two different whole numbers and print the larger one, without using max().Beginner
  4. Largest of threeRead three whole numbers and print the largest, without using max().Beginner
  5. In orderRead two whole numbers and print them from the smaller to the larger, on one line.Beginner
  6. Grade from a scoreTurn a test score into a grade: 90 or more is A, 80 or more is B, 70 or more is C,Beginner
  7. Leap yearA year is a leap year if it divides by 4, except years that divide by 100, which areBeginner
  8. Ticket priceA cinema ticket costs 5 for children under 12, 7 for people aged 65 or more, and 10Beginner
  9. Secret passwordThe password is python123.Beginner
  10. Divisible?Read two whole numbers a and b (b is not 0).Beginner
  11. Can it be a triangle?Three lengths make a triangle when each one is smaller than the sum of the otherBeginner
  12. Kind of triangleRead the three sides of a triangle and name its kind: equilateral (all sides equal),Beginner
  13. Which quadrant?Read a point (x, y) where neither coordinate is 0, and print its quadrant: 1 forBeginner
  14. Day of the weekRead a number from 1 to 7 and print the day: 1 is Monday, 7 is Sunday.Beginner
Start with “Even or odd”