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