Back to Python Mastery
BeginnerTeenagers
Conditional Logic
In life, we make decisions based on conditions. For example, "IF it is raining, THEN I will take an umbrella." Programming works the same way using 'if' statements.
An 'if' statement runs a block of code only if a certain condition is true.
Let's see it in action:
The code inside the 'if' block only runs because the value of 'age' is 18, which is greater than or equal to 18.
What if the condition is false? We can add an 'else' block.
In this case, since 'age' is 16, the condition is false, and the code inside the 'else' block will run. This is the foundation of making your programs smart!
An 'if' statement runs a block of code only if a certain condition is true.
Let's see it in action:
age = 18if age >= 18:print("You are eligible to vote.")The code inside the 'if' block only runs because the value of 'age' is 18, which is greater than or equal to 18.
What if the condition is false? We can add an 'else' block.
age = 16if age >= 18:print("You are eligible to vote.")else:print("You are not eligible to vote yet.")In this case, since 'age' is 16, the condition is false, and the code inside the 'else' block will run. This is the foundation of making your programs smart!
If-Else Statement
Check if the `number` is greater than 0. If it is, print "Positive". Otherwise, print "Not Positive".