[013] Introduction To Python Basics: Conditional Logic

Koay Yong Cett
4 min readJul 18, 2020
Photo by Reproductive Health Supplies Coalition on Unsplash

Conditional Logic is an important concept all over the programming. We learn about boolean (True/False) and you might be wondering why are they useful. When it comes to conditional logic, boolean are really important.

  • Let’s say we create one variable for driver and if you are not old enough the engine won’t be starting and allow you to drive. Another variable is license and if you don’t have licensed then you cannot start the engine.
  • The two variable can change accordingly depending on the user and driver.
  • Here we want to talk about the condition whether it is true or not. For instance the is old and is licensed variable we mentioned before.
  • Here, we added a semicolon(:) and the indentation is different. As a matter of fact when we press enter after the semicolon, it automatically gives me a space.
  • This tell the Python interpreter that we will be performing a conditional operation after the if statement. And if statement is true, then everything after the indentation will print out.
  • Let’s say we are performing something without indentation, then this line will be a completely new line.
  • Here, there is a code block where everything that are indented is belong to it.
  • If the is old is false, then everything in the code block will not be printed/ignore and then go to next line. This is because it will only print out that line if is old is true.
  • Well, we have something called else. Guess what will be printed out in this line?
  • We get the check in the result since the is old is false. This means that the else statement will run whenever the if statement is false.
  • The if statement is True, then it will run whatever inside the code block and ignore the else statement.
  • Now we are controlling the flow of our coding instead of going through line by line. For instance, we will be able to do some sort of check and based on that skip the lines of coding.
  • The elif statement is used in combination with the if statement. Here, we use another condition is licensed in the elif statement.
  • Here, we get true for the first conditional block and the line is printed out. Then, the elif statement and else statement will be ignored.
  • The is old is false, then the elif statement conditional block will be printed out and ignore the else statement.
  • If both the if statement and elif statement is False, then the else statement will be printed out. This means that else statement is kinda backup.
  • The “and” can be used to combine two condition together. Both the condition must be evaluate true before printed out the line in the code block.
  • Here, we noticed that only one statement is true then it will print out the else statement.
  • Now only when both statement is true, then only you are suit to drive a car.

Thanks for reading my stories 😃 I just write for fun based on what I understand. Please refer to other sources for details.

--

--

Koay Yong Cett

A Bachelor CS student with major in Network Security (UniSZa). Every stories I shared is based on my personal opinion. Thanks you. Having my Internship now.