Flow Control with Else Statements in Python

Day 11/60 Continuing Chapter 2- Flow Control Topic II - Coding Else Statements Great software doesn’t just decide what to do when a condition is  T⁠r⁠u⁠e⁠ , it also has a back-up plan if the condition is  F⁠a⁠l⁠s⁠e⁠ . For example - making a program that switches the lights on if  i⁠s⁠_⁠o⁠n⁠ is  T⁠r⁠u⁠e⁠ and off if  i⁠s⁠_⁠o⁠n⁠ is  F⁠a⁠l⁠s⁠e⁠ . We already know  i⁠f⁠ statements help us execute code if a condition like available is  T⁠r⁠u⁠e⁠ . 🧩Code available = True if available: print("In stock") 🖥️Output In stock Let's add another  i⁠f⁠ statement that uses the  n⁠o⁠t⁠ operator to run a different code block if the condition is  F⁠a⁠l⁠s⁠e⁠ . 🧩Code available = True if available: print ("In stock") if not available print ("Out of stock") 🖥️ Output In stock Now Instead of creating two  i⁠f⁠ statements, we use an  i⁠f⁠ /  e⁠l⁠s⁠e⁠ statement to achieve the same result. 🧩Code available = False if available: print("1 in stock") else: print("Out of stock") 🖥️ Output Out of stock The  e⁠l⁠s⁠e⁠ statement of an  i⁠f⁠ /  e⁠l⁠s⁠e⁠ statement always goes at the end. 🧠Challenge of the day What does this display in the console? 🧩Code is_subscribed = True if is_subscribed: print("Enjoy 10% off!") else: print ("Become a subscriber!") #python #programming #ai #bigtech

To view or add a comment, sign in

Explore content categories