top of page
Writer's pictureHui Wang

90. Machine learning: Control Statements in Python

Python has several types of control statements, including:


1. Conditional statements


The conditional statement allows you to check whether a certain condition is true, and then execute a block of code if it is.


The most common conditional statement in Python is the if statement.


Here is an example of an if statement:

x = 5
if x > 0:
    print("x is positive")

In this example, the condition x > 0 is checked. If it is true (in this case, it is), then the block of code that follows the if statement (print("x is positive")) will be executed. If the condition is false, the block of code will be skipped.


You can also add an else clause to an if statement to specify what should happen if the condition is false:

x = -5
if x > 0:
    print("x is positive")
else:
    print("x is non-positive")

In this case, the condition x > 0 is false, so the code in the else block is executed instead, which outputs "x is non-positive".


Additionally, you can use elif (short for "else if") to specify additional conditions that should be checked if the previous conditions are not true:

x = 0
if x > 0:
    print("x is positive")
elif x < 0:
    print("x is negative")
else:
    print("x is 0")

Here, the condition x > 0 is false, so the next condition x < 0 is checked, as it's false too, so the output will be "x is 0"


You can also chain multiple conditions by using logical operators and & or

x = 5
y = 6
if x>0 and y>0:
    print("x and y are positive")

Here both conditions must be true for the if block to execute and output "x and y are positive"



2. Loop statements


Loop statements allow you to execute a block of code multiple times.


There are two most common loop statement types.


The for loop is used to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item in the sequence. For example:

fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    print(fruit)

This code will print each item in the fruits list on a separate line.


The while loop, on the other hand, is used to execute a block of code as long as a certain condition is true. For example:

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

This code will print the numbers 1 through 5. The while loop will continue to execute as long as the value of i is less than or equal to 5.


There is also another Loop called range() that used mostly with for loops, it is a built-in function that generates a sequence of numbers. for example:

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

This code will print numbers from 0 to 4.


You can also add a third parameter, which is the step size, for example:

for i in range(1,10,2):
    print(i)

This code will print numbers from 1 to 9 with step size of 2 so the output will be : 1,3,5,7,9


You can also use the break and continue statements to control the flow of a loop. The break statement is used to exit a loop prematurely, while the continue statement is used to skip the current iteration of a loop and continue with the next one.


Additionally, you can use else block in a for-loop (not available in while loop) after the loop has finished iteration, the code inside the else-block will execute

nums = [1,2,3,4,5]
for num in nums:
    if num == 3:
        print("Found!")
        break
else:
    print("Not Found!")

In this example, the output will be "Found!" as the loop has found the number 3 in the nums list.


3. Control flow statements


Control flow statements allow you to control the flow of execution in your program. Python provides several control flow statements:


  • break statement

It is used to exit a loop when a certain condition is met.

numbers = [1, 2, 3, 4, 5]
for number in numbers:
    if number == 3:
        break
    print(number)

In this example, the for loop iterates over the numbers list, and for each item in the list, it is checking if the number is equal to 3. If it is, the break statement is executed, which causes the loop to exit early. If the condition is not met, the code inside the loop (print(number)) will be executed.


  • continue statement

It is used to skip over a certain iteration in a loop.

numbers = [1, 2, 3, 4, 5]
for number in numbers:
    if number == 3:
        continue
    print(number)

In this example, the for loop iterates over the numbers list, and for each item in the list, it is checking if the number is equal to 3. If it is, the continue statement is executed, which causes the current iteration to be skipped, and moves on to the next iteration without executing any code inside the current iteration. If the condition is not met, the code inside the loop (print(number)) will be executed.


  • pass statement

It is used as a placeholder in situations where a statement is required syntactically, but you do not want any code to be executed.

def do_nothing():
    pass

In this example, the pass statement is used as a placeholder in the definition of the do_nothing function, which does not have any code inside of it but required as a function declaration in Python



You can use these control statements in combination with each other to create more complex logic in your programs.

 

Follow me on:

Recent Posts

See All

Comments


bottom of page