python logo

If, Elif, Else

With branching using If, Elif, or Else statements I can control the execution of the code. The code block will only be executed if the condition is True. if condition: run this code elif other_condition_occured: run this code over here not the one above else: run this code for anything else that don't meet the conditions above Example: option = input("Choose 1 to Multiply numbers, Choose 2 to Divide: ") num1 = int(input("Enter the first number --> ")) num2 = int(input("Enter the second number --> ")) if option == "1": print(f"{num1}multiplied by {num2}is {num1 * num2}") elif option == "2": print(f"{num1}divided by {num2}is {num1 / num2}") else: print("That's an invalid selection") The usage of if, elif, and else in the code above works fine if the user choose either option 1 or 2, but it will still prompt the user to enter the first number even when the user entered something else other than 1 or 2 To fix this I can use Nested If Statements...

February 19, 2022 · 2 min · Kei
python logo

Booleans

Booleans evaluate to True or False based on conditions: Greater than > Greater than or equal to >= Less than < Less than or equal to <= Equal to == Not equal to != A Truth table is used to carry out logical operations

February 12, 2022 · 1 min · Kei