30-Day Coding Challenge Extras
Makeup Days 1, 2, & 3 — Wait, It isn’t Over?!
Countdown to 100 Stories: ⏱️(2 stories left)
Today will mark the last makeup day of my coding challenge. Finally, I can see the finish line in sight. But yeah, my reports' consistency has only worsened since last week.
Due to that, I’ll be reporting my activities for makeup days 1, 2, & 3 here. Then my report for makeup day 4 (today) will come out tomorrow.
So, diving straight into the major highlights of makeup days 1 & 2 — For Loops!
Makeup Day 1 & 2 Top Highlight: Nested For Loops
For loops
was one of the first things I learned in Python along with while loop
. But unlike the while loop, it works only over an iterable (a collection of data) like lists, tuples, etc.
During my makeup days, I discovered the possibility of using multiple for loops
in one statement i.e nested for loops
. Here’s how it can work:
# The outer loop goes through each list inside the main list
for digit in [[5, 6, 7], [8, 9, 10]]:
# The inner loop goes through each number in the sublists
for digits in digit:
# Print each number one by one
print(digits)
'''
OUTPUT:
5
6
7
8
9
10
'''
These past few days, I’ve discovered I can do much more with loops and conditional statements than I thought. And the one that beats everything else is the ternary operator.
Makeup Day 3: Ternary Operator
Basically, this is a different and simpler way of using the if statement
in one line of code:
x = 1
color = 'blue' if x < 5 else 'red'
print(color)
# OUTPUT: blue
The logic behind this is pretty simple and can even be read like a normal English sentence:
Color is “Blue” if x is less than five, else color is “Red”
The downside of this method of writing if statements? It simply can’t be used for complex statements, or at least I don’t see any way it can.
The last report of my current challenge approaches…