Day 4: Control Flow (if statements, loops) in Python
Table of contents
In this blog, I'll delve into my understanding of Control Flow in Python—a crucial aspect that enhances program interactivity. Python offers a variety of Control Flow statements to achieve this. Today, on my fourth day of learning Python, I aim to share insights into these statements.
Let's optimize the paragraph further:
In this blog, I'll explore Control Flow in Python—a vital feature enhancing program interactivity. Python offers diverse Control Flow statements. Today, on my fourth day of learning Python, I'll share insights into these statements.
Control Flow
Python processes programs sequentially, reading from left to right, line by line, and from top to bottom, much like a human. Control flow statements empower us to direct and alter the program's flow as needed. These statements include if, elif, else, and loops.
if statement:
if
statement is used to check some condition and if that condition is true then perform some action.age = 18 if age > 18: print("you can drive the car")
In the given example, there's an 'age' variable assigned with a value. If this value evaluates to true within the if condition, the subsequent print statement executes; otherwise, it's skipped.
elif and else statements: The 'elif' statement is employed after the 'if' condition evaluates to false. Python then assesses whether the 'elif' condition holds; if not, it proceeds to the next 'elif' condition. Multiple 'elif' statements can be utilized in our code for this purpose.
Another conditional statement to note is 'else.' If all 'if' and 'elif' conditions are evaluated to be false, then the code within the 'else' block will execute.
age = 18 if age > 18: print("You can drive the car") elif age == 18: print("Next year you can drive the car") else: print("You can't drive the car")
Loop statements
Loops in Python facilitate the repetition of actions multiple times. Python supports various types of loops, such as 'while' and 'for' loops.
While Loop: In a 'while' loop, a condition is provided, and the loop iterates as long as the condition remains true.
a = 1 while a < 10: a += 1; print("value of a : ",a)
The loop will continue executing until the value of variable 'a' equals 10.
For Loop: In Python, the 'for' loop iterates over a sequence or any iterable object, executing a block of code for each item.
list = [ "apple","Banan","mango" ] for fruit in list: # this loop will return fruit name one by one print("fruit : ",fruit)
Program:
# this program will print numbers between 0 to 9 for a in range(10): print(a)
Conclusion
Control flow statements are essential in Python programming as they allow developers to manage the flow of their code effectively. In this blog, we explored various control flow statements such as 'if', 'elif', 'else', and loops like 'while' and 'for'.
The 'if' statement enables conditional execution based on whether a given condition is true or false.
'Elif' and 'else' statements provide additional conditions and fallback options if the initial condition in the 'if' statement is not met.
Loops, including 'while' and 'for', enable repetitive execution of code blocks, allowing for efficient iteration over sequences or iterable objects.
Understanding and mastering these control flow statements is crucial for writing robust and efficient Python programs. With practice and experimentation, developers can leverage these constructs to build more complex and interactive applications.
Subscribe to my newsletter
Read articles from Himanshu Chauhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Himanshu Chauhan
Himanshu Chauhan
A coder how works for excellence...