Python Log¡c

Checks a Condition 

Decision making based on conditions

Conditional statements gives us the ability to check conditions and change the behavior of the program accordingly.

 

Conditional Statements:

Python Comparison Operators 

>         greater than

<         less than

>=       greater than or equal to

<=       less than or equal to

==       equality

!=        inequality

If Statement 

Checks for only one condition

Checks Condition

True/False

Executes Statement

True

False

If Statement 

  • The boolean expression after the if statement is called the condition
  • The colon is required after the condition 
  • The lines (statements) after the colon must be indented
  • If condition is true, the statements will get executed
  • If condition if false, nothing happens

If Else Statement 

Checks for two conditions

Checks Condition

True/False

Statement 2

False

True

Statement 1

If Else Statement 

  • When you are checking when a condition is true and when it is false
  • If the first condition passes, the first statement will be executed, if it doesn't the else block will be executed instead

Chained Conditionals

Checks for more than two conditions

Checks Condition

True/False

Statement 2

False

True

Statement 1

Next Condition

True/False

True

Statement 3

False

Chained Conditionals

if-elif-else chain

  • When a condition passes, the code following the test is executed and the rest of the test will be skipped
  • You can use an many elif block as you like
  • Python does not require an else block at the end of an if-elif chain

Practice!

Made with Slides.com