Python For GCSE
If Statements

Simple If

Study the following example. The line with the if statement ends with a colon. The line(s) of code that are executed if the condition is met are all indented.

name = input("Enter your name: ")
if name=="John":
    print("Hello, John")
    print("Nice to meet you")
print("This will be displayed whatever is entered.")

If..else

The keyword else is used to describe everything that does not meet the other conditions in the if.

n = int(input("Enter a number: "))

if n>99:
    print("Your number has 3 or more digits.")
else:
    print("Your number is less than 99.")

If..elif..else

The keyword elif is used when you want to check more than one conditon in the if structure.

a = int(input("Enter a number: "))
b = int(input("Enter a number: "))

if a>b:
    print("The first number is larger than the second.")
elif a<b:
    print("The second number is larger than the first.")
else:
    print("The numbers are equal.")

And

Use the and keyword when two conditions both need to be met.

a = int(input("Enter a number: "))
b = int(input("Enter a number: "))

if a % 2==0 and b % 2==0:
    print("Both numbers are even.")
else:
    print("At least one of the numbers is odd.")

Or

a = int(input("Enter a number: "))
b = int(input("Enter a number: "))

if a<0 or b<0:
    print("At least one of the numbers is negative.")

Not

There are times when the not keyword makes the code read more nicely. This is probably not one of them.

name = input("Enter your name: ")
if not name == "John":
    print("You're not John.")

Nested Ifs

You can put if statements inside other if statements as long as you use indentation correctly.
name = input("Enter your name: ")
if name=="John":
    name2 = input("Tall or short? [t/s]: ")
    if name2=="t":
        print("Big John")
    elif name2=="s":
        print("Little John")
else:
    print("You are not John")