Python: Control Statements


In this article, we take a look at the major control statements in Python. Primary we look at the control statements if-elif-else, for,while and control exit statement break. We list sample code for each of these and use in-line comments to explain what the code line does. Note that we declare the same functions in a class to adhere to object-oriented semantics. You can easily copy/paste the code in your Python code.

if-elif-else statement
class DemoClass:
    def IfThenElse(self):
        # Using integer
        inp = int(input("Enter a number: "))
        if inp < 0:
            print("Number is negative")
        elif inp == 0: #Else-if
            print("Number is 0")
        else:
            print("Number is positive")

        # Using a string input
        inp = input("Enter your name: ")
        if inp == "Albert":
            print("Are you Albert Einstein ?")
        elif inp == "Blaise":  # Else-if
            print("Are you Blaise Pascal ?")
        else:
            print("You are nobody !")


dc = DemoClass()
dc.IfThenElse()
The output for the above code is:
Sum is :3
Enter a number: 12
Number is positive
Enter your name: Chaitanya
You are nobody !

for loop

class DemoClass:
    def ForLoop(self):
        # Print values from 0 to 5
        for value in range(6):
            print(value)

        # Print values from 1 to 5
        for value in range(1,6):
                print(value)

        # Print values from 1 to 5, step of 2
        for value in range(1, 6, 2):
                print(value)

        # Print values from 6 to 2
        for value in range(6, 1, -1):
                print(value)

        # Print all values in an array
        # equivalent to foreach
        values=[1,2,3,4,5,6,7]
        for value in values:
                print(value)


dc = DemoClass()
dc.ForLoop()
The output for the above code is:
0
1
2
3
4
5
1
2
3
4
5
1
3
5
6
5
4
3
2
1
2
3
4
5
6
7

while loop

class DemoClass:
    def WhileLoop(self):
        # Print values from 0 to 5
        x = 0
        while x < 6:
            print(x)
            x += 1

        # Print values from 1 to 5, step of 2
        x = 1
        while x < 6:
            print(x)
            x += 2

        # Print values from 6 to 2
        x = 6
        while x > 1:
            print(x)
            x -= 1

        # Print all values in an array
        # equivalent to foreach
        values=[1, 2, 3, 4, 5, 6, 7]
        x = 0
        while x <= 6:
            print(x, ":", values[x])
            x += 1


dc = DemoClass()
dc.WhileLoop()
The output for the above code is:
0
1
2
3
4
5
1
3
5
6
5
4
3
2
0 : 1
1 : 2
2 : 3
3 : 4
4 : 5
5 : 6
6 : 7

break statement

class DemoClass:
    def Break(self):
        # Print values from 0 to 5
        x = 0
        while True:
            print(x)
            x += 1
            if x > 5:
                # exit while
                break

        # Print values from 1 to 5, step of 2
        x = 1
        while True:
            print(x)
            x += 2
            if x > 5:
                # exit while
                break;

        # Print values from 6 to 2
        x = 6
        while True:
            print(x)
            x -= 1
            if x <= 1:
                # exit while
                break;

        # Print all values in an array
        # equivalent to foreach
        values=[1, 2, 3, 4, 5, 6, 7]
        x = 0
        while True:
            print(x, ":", values[x])
            x += 1
            if x > 6:
                # exit while
                break


dc = DemoClass()
dc.Break()

The output for the above code is:
0
1
2
3
4
5
1
3
5
6
5
4
3
2
0 : 1
1 : 2
2 : 3
3 : 4
4 : 5
5 : 6
6 : 7

Comments

Post a Comment

Popular posts from this blog

Part III: Backpropagation mechanics for a Convolutional Neural Network

Introducing Convolution Neural Networks with a simple architecture

Deriving Pythagoras' theorem using Machine Learning