Day 3 of #100DaysOfCode: Dive into Control Structures

Elshad Karimov
3 min readSep 6, 2023

Welcome to Day 3 of #100DaysOfCode in Python! Today, we are going to explore control structures in Python, specifically conditional statements and loops. These are the building blocks of decision-making and repetition in code, and are fundamental concepts that you will use in almost every program you write.

Conditional Statements

Conditional statements are used to execute a block of code only if a certain condition is met. In Python, the if statement is used for conditional execution.

The if Statement

The if statement is used to execute a block of code only if a specified condition is true.

x = 10

if x > 5:
print("x is greater than 5")

In this example, the condition x > 5 is true, so the print statement will be executed.

The elif Statement

The elif (else if) statement is used to specify a new condition if the previous condition is false.

x = 10

if x > 20:
print("x is greater than 20")
elif x > 5:
print("x is greater than 5")

In this example, the first condition x > 20 is false, so Python will check the next condition x > 5, which is true, so the second print statement will be executed.

The else Statement

The else statement is used to specify a block of code to be executed if all the conditions are false.

x = 10

if x > 20:
print("x is greater than 20")
elif x > 15:
print("x is greater than 15")
else:
print("x is not greater than 15 or 20")

In this example, all the conditions are false, so the else statement will be executed.

Loops

Loops are used to repeat a block of code multiple times. Python has two types of loops: for loops and while loops.

The for Loop

The for loop is used to iterate over a sequence (e.g., a list, tuple, string, or range).

for i in range(5):
print(i)

In this example, the for loop will iterate over the sequence range(5), which generates a sequence of numbers from 0 to 4, and the print statement will be executed for each number in the sequence.

The while Loop

The while loop is used to repeat a block of code as long as a specified condition is true.

x = 0

while x < 5:
print(x)
x += 1

In this example, the while loop will repeat as long as the condition x < 5 is true. The print statement will be executed, and the value of x will be incremented by 1 in each iteration.

Keep Practicing!

Now that you have a solid understanding of conditional statements and loops, it’s time to practice! Here are some exercises to get you started:

  1. Write a program that asks the user for a number and then prints whether the number is even or odd.
  2. Write a program that prints the numbers from 1 to 100, but for multiples of three, print “Fizz” instead of the number, and for multiples of five, print “Buzz”. For numbers that are multiples of both three and five, print “FizzBuzz”.
  3. Write a program that asks the user for a number and then prints the factorial of that number.

Remember, practice is key to mastering any skill. Keep practicing, stay curious, and don’t hesitate to seek help or feedback. Happy coding!

--

--

Elshad Karimov
Elshad Karimov

Written by Elshad Karimov

Software Engineer, Udemy Instructor and Book Author, Founder at AppMillers

No responses yet