Python's Traffic Lights


π¦ We've all been there, stuck in soul-crushing traffic! π© You're constantly eyeing that traffic light, desperately hoping for a flicker of green, maybe even a hesitant yellow. But all too often, it's a stubborn, unyielding red, turning minutes into what feels like hours. β³
But have you ever stopped to think about it? π€ When that traffic light blazes red, an unspoken agreement takes over. Every single vehicle in that jam comes to a complete halt. π When it shifts to yellow, a sense of cautious anticipation fills the air, and we inch forward, a little more slowly. π And when that glorious green appears? π’ A collective sigh of relief! We accelerate, perhaps a little too eagerly, knowing (or at least hoping!) that cross-traffic is at a standstill.
The fascinating thing is this: the color of that traffic light dictates the flow of movement. π₯ Each change brings a different outcome. And what happens when there are no traffic lights? We become hyper-aware, proceeding with utmost caution. β οΈ
Can you see the pattern? There's a set of rules that every vehicle instinctively follows. There's a default behavior β the desire to keep moving β but the traffic light's colors act as conditions, each with a specific behavior attached: stop at red, proceed cautiously at yellow, and move freely at green.
Just like these traffic lights control the flow of vehicles, Python has control flow statements that allow us to manage the flow of data within our programs. These include if statements, elif statements, and if-else statements. Their names often give us a clue about their function. While elif might sound a tad mysterious, if-else is something we encounter in our daily language.
Think about it: the English definition of "if" is "used in sentences in which one thing only happens or is true when another thing happens or is true." And "else" means "another, different person, thing, or place." In simple terms:
Imagine Tushar walks into a coffee shop and wants an americano. β (This is our if statement: "Tushar wants an americano." This desire for one specific coffee can only happen once. He's not going to order two americanos if he only wants one!) But alas, the coffee shop is out of americanos. π So, they offer Tushar another option, and he chooses tea. π΅ (This is our else statement: "Tushar wanted an americano, but the cafe didn't have it, so he chooses a different drink." Think about the definition: if the cafe had an americano (our if condition was met), Tushar wouldn't have opted for tea. Once the if condition is true, we don't need to consider the else.)
With Tushar, it was straightforward: americano or tea. But what if another person, Yash, hadn't decided on a backup plan? If there's no americano, what will he choose? He'd probably have to look at the entire menu. π Now, imagine describing an if-else condition for every single item on that menu! That would make our code incredibly long and repetitive, violating core programming principles like DRY (Don't Repeat Yourself) and KISS (Keep It Simple, Stupid). π€¦ββοΈ
This is where our savior, the elif statement, comes to the rescue! π¦Έ It acts like a bridge between the if and else. Instead of writing else repeatedly for each alternative, we use elif to describe another condition without closing off the possibilities. The elsestatement comes at the very end, acting as the default action when none of the preceding if or elif conditions are met.
So, the structure is: it starts with an if statement, followed by any number of elif statements to describe various other conditions, and finally, an optional else statement to define the default behavior β what's bound to happen if nothing else fits.
Phew! That was quite a journey through the traffic of analogies. Hopefully, this makes the concept of if, elif, and else in Python a little clearer! π
SYNTAX:
We all know that the if statement includes a condition. In Python, we adhere to a specific format when writing an if statement, and there's a defined block of code associated with it. Within this block, we describe the behavior to be executed when the if statement's condition is met. The syntax is:
Here you can see that we define our condition inside the paranthesis which follows the βifβ keyword and at the end we use a semicolon β:β. And if you observe that we left a space before writing our block, that space is know as indentation which defines the block of the statement anything written with indentation will be considered a part of our if statement, if we donβt use this then python compiler wonβt able to treat the statement as part of the if statement and throw an error. You can understand by this code:
As you see in the above code, that python throws βIndentationErrorβ as we have not given indentation then the code will not be able to excute, thatβs why Indentation is a important aspect in writing our code.
Now, we will add our else statement in our if statement, and it is very simple to do so. We will use the βelseβ keyword adding below the if-block without giving indentation followed by semicolon(:) at the end, and we write our else-block by using indentation, which is:
Adding the elif statement is same as else statement the only difference will be that the condition in paranthesis will be added to it as it is treated as the if statement.
Now, you have understand all the control statement which is offered by python. Happy Coding!
#chaicode
Subscribe to my newsletter
Read articles from Tushar Nebhnani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
