Coding in Python 14 – If Statements

In video number 14 of the Coding in Python series, you’ll be introduced to branching.

YouTube player

Commands Used in this Video

Simple if statement

#!/usr/bin/env python3
  
number = 5

if number < 10:
    print("The number is less than 10.")

print("This will always print.")

If statement with user input

#!/usr/bin/env python3
  
number = input("Please enter a number between 1 and 10: ")

if int(number) <= 10:
    print("A valid number was entered")

print("This will always print.")

If statement with else

#!/usr/bin/env python3
  
number = input("Please enter a number between 1 and 10: ")

if int(number) <= 10:
    print("A valid number was entered")
else:
    print("An invalid number was entered.")

print("This will always print.")

If statement with else end elif

#!/usr/bin/env python3
  
age = input("Please enter your age: ")

if int(number) >= 50:
    print("You are 50 or older")
elif int(age) >= 18:
    print("You are an adult.")
else:
    print("You are not even an adult.")

print("This will always print.")

If statement with multiple elif

#!/usr/bin/env python3
  
age = input("Please enter your age: ")

if int(number) >= 50:
    print("You are 50 or older")
elif int(age) >= 25:
    print("You are 25 or older.")
elif int(age) >= 18:
    print("You are an adult.")
else:
    print("You are not even an adult.")

print("This will always print.")

Brand-New Course!

Check out Jay’s new course on Ansible and start automating today!

Discount Vouchers


Receive 5% off an LPI exam voucher!

Exclusive Member Features

Support the channel and receive exclusive perks!