Control Flow

👋 Hi there, I am Nirmal. I am a former software engineer with a keen interest in data science and analytics domains. Besides, I love to contribute to open source and help others to understand various tech stuffs.
Search for a command to run...

👋 Hi there, I am Nirmal. I am a former software engineer with a keen interest in data science and analytics domains. Besides, I love to contribute to open source and help others to understand various tech stuffs.
No comments yet. Be the first to comment.
This series will provides the core concepts of python programming starting from variables, data types, control flow, and functions to some problem solving.
Functions are a fundamental building block in Python programming, allowing you to organize code, promote reusability, and simplify complex tasks. It takes input arguments (optional) and returns a result (optional). Using function Functions are group...
Bias in Machine Learning Bias refers to the simplifying assumptions made by a model to make the target function easier to learn. High bias can lead to underfitting Represents the error introduced by approximating a real-world problem Example: A li...
When it comes to developing software applications, choosing the appropriate architectural style is a critical decision. Among the popular options are microservices and monolithic architecture. By understanding the trade-offs, readers can make well-i...

In a world where information is everywhere, businesses have tons of data at their fingertips. This data is like a goldmine waiting to be used for smart decisions. That's where Big Data analytics and Data Science come in. They're like the experts who ...

Python is a powerful tool for data professionals, largely due to its extensive collection of open-source libraries and packages. In this blog, we'll explore the fundamentals of pandas, with a special focus on its core data structures: Series and Data...

NumPy is a powerful library for numerical computing in Python, offering robust support for large, multi-dimensional arrays and an extensive collection of mathematical functions. Creating Arrays To begin using NumPy, import the library and create arra...

Control statements are vital in programming as they allow you to control the flow of execution in your code. In Python, several control statements help you make decisions, loop through code blocks, and handle exceptions.
Conditional statements allow you to execute different blocks of code based on specific conditions.
The most common conditional statement in Python is the if statement.
age = 25
if age < 18:
print("You are underage.")
elif age >= 18 and age < 60:
print("You are an adult.")
else:
print("You are a senior citizen.")
In the above example, the program checks the value of the age variable and prints the corresponding message based on the condition.
From version python 3.10+, Python has implemented a switch case feature called “structural pattern matching”.
number = int(input("Enter day ::"))
match number:
case 1:
return "Sunday"
case 2:
return "Monday"
case 3:
return "Tuesday"
case 4:
return "Wednesday"
case 5:
return "Thursday"
case 6:
return "Friday"
case 7:
return "Saturday"
case _:
return "Week has only 7 days"
Looping statements enable you to execute a block of code repeatedly. Python offers two types of looping statements: for loop and while loop. Let's take a look at examples of both:
for loop example:
list = [1,2,3]
for i in list:
print(i,end = " ")
#Output = 1 2 3
For loop using range(start, stop, step) method.
start (optional): The starting value of the sequence (inclusive). If not specified, it defaults to 0.
stop (required): The ending value of the sequence (exclusive). The range() function will generate numbers up to but excluding this value.
step (optional): The difference between each number in the sequence. If not specified, it defaults to 1.
list = list(range(1, 6))
print(my_list)
# Output: [1, 2, 3, 4, 5]
for i in range(6):
print(i,end = " ")
#output = 0, 1, 2, 3, 4, 5(this exclude 6)
for i in range(1,6):
print(i,end = " ")
#output = 1, 2, 3, 4, 5
'''Reverse number'''
'''In this statement starts from 5 to (0+1).Here, -1 is for reverse'''
for i in range(5,0,-1):
print(i,end = " ")
#Output = 5, 4, 3, 2, 1
while loop example:
count = 0
while count < 10:
print("Count:", count)
count += 1
Here, the while loop continues executing the code block until the condition count < 10 becomes False.
Python provides control statements to alter the flow of loops. Two commonly used control statements are break and continue. Let's see them in action:
break statement example:fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
if fruit == "banana":
break
print(fruit)
In this example, the break statement is used to terminate the loop prematurely when the value of fruit is "banana". The loop exits at that point.
continue statement example:numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
continue
print(num)
Here, the continue statement skips the remaining code in the loop for even numbers. It moves on to the next iteration and prints only the odd numbers.
The loop-else statement in Python is often confusingly named.
An alternative way to understand it is as a nobreak statement. This means that the else block is executed only if the loop completes naturally without encountering a "break" statement.
Take into account the following implementation of the Sieve of Eratosthenes. This algorithm is widely recognized for its ability to find prime numbers.
'''Prime number from 1 to 20'''
list = []
number = 20
for n in range(2, number):
for factor in list:
if n % factor == 0:
break
else: # no break
list.append(n)
print(list)#output = [2, 3, 5, 7, 11, 13, 17, 19]