w1coding.comAll tutorials

Python Control

Python lessons
  1. Environment Setup
  2. Introduction
  3. Control
  4. Control
  5. Loops
  6. Functions
  7. Lists
  8. Dictionary
  9. Classes
  10. Exception
  11. File
  12. Libray
  13. Date
  14. Regula
  15. JSON
  16. Web
  17. Zweb
  18. Apis
  19. Unity
  20. Llampda
  21. Iterators
  22. Generators
  23. Files
  24. Context
  25. Ilogging
  26. Multithreading
  27. Networking
  28. SQLite

Python Control Flow (If Statements)

Control flow structures allow your program to make decisions based on conditions. The if statement is used for this purpose.

If Statements

The if statement is used to test conditions. If the condition is True, the code inside the block runs:

      
        age = 18
        if age >= 18:
            print("You are an adult.")
        else:
            print("You are a minor.")
      
    

Activity

Try It Yourself!

Write a program that checks whether a number is even or odd.

Quick Quiz

Quick Quiz

  1. What is the purpose of the if statement in Python?
  2. What happens when the condition in an if statement is False?

Answers: The if statement checks a condition and runs code if the condition is True. If the condition is False, the code inside the else block will run (if defined).