Thursday, 26 December 2019

Python Loops

Python Loops


Python If-else and elif statements
Decision making is the most important aspect of almost all the programming languages. As the name implies, decision making allows us to run a particular block of code for a particular decision. Here, the decisions are made on the validity of the particular conditions. Condition checking is the backbone of decision making.
Eg: Program to print the largest of the three numbers.
a = int(input("Enter a? "));  
b = int(input("Enter b? "));  
c = int(input("Enter c? "));  
if a>b and a>c:  
    print("a is largest");  
if b>a and b>c:  
    print("b is largest");  
if c>a and c>b:  
    print("c is largest");
>>>Output
Enter a? 100
Enter b? 120
Enter c? 130
c is largest

>>>Program to check whether a person is eligible to vote or not.
age = int (input("Enter your age? "))  
if age>=18:  
    print("You are eligible to vote !!");  
else:  
    print("Sorry! you have to wait !!");  
>>>Output
Enter your age? 90
You are eligible to vote !!
The elif statement
The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them. We can have any number of elif statements in our program depending upon our need. However, using elif is optional.
The elif statement works like an if-else-if ladder statement in C. It must be succeeded by an if statement.
The syntax of the elif statement is given below:
Eg:
if expression 1:   
    # block of statements   
elif expression 2:   
    # block of statements   
elif expression 3:   
    # block of statements   
else:   
    # block of statements  

Python for loop
The for loop in Python is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like list, tuple, or dictionary.
The syntax of for loop in python is given below.
for iterating_var in sequence:  
    statement(s)  
Eg:
i=1; 
num = int(input("Enter a number:")); 
for i in range(1,11): 
    print("%d X %d = %d"%(num,i,num*i)); 
>>>Output:
Enter a number:10
10 X 1 = 10
10 X 2 = 20
10 X 3 = 30
10 X 4 = 40
10 X 5 = 50
10 X 6 = 60
10 X 7 = 70
10 X 8 = 80
10 X 9 = 90
10 X 10 = 100

Nested for loop in python
Python allows us to nest any number of for loops inside a for loop. The inner loop is executed n number of times for every iteration of the outer loop. The syntax of the nested for loop in python is given below.
for iterating_var1 in sequence: 
    for iterating_var2 in sequence: 
        #block of statements  
#Other statements 
Example 1
n = int(input("Enter the number of rows you want to print?")) 
i,j=0,0 
for i in range(0,n): 
    print() 
    for j in range(0,i+1): 
        print("*",end="") 
>>>Output:
Enter the number of rows you want to print?5
*
**
***
****
*****

Using else statement with for loop
Unlike other languages like C, C++, or Java, python allows us to use the else statement with the for loop which can be executed only when all the iterations are exhausted. Here, we must notice that if the loop contains any of the break statement then the else statement will not be executed.
Example 1
for i in range(0,5): 
    print(i) 
else:print("for loop completely exhausted, since there is no break."); 
In the above example, for loop is executed completely since there is no break statement in the loop. The control comes out of the loop and hence the else block is executed.
>>>Output:
0
1
2
3
4

Python while loop
The while loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed as long as the given condition is true.
It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance.
The syntax is given below.
while expression: 
    statementsExample 1
i=1; 
while i<=10: 
    print(i); 
    i=i+1; 
>>>Output:
1
2
3
4
5
6
7
8
9
10
Example 2
i=1 
number=0 
b=9 
number = int(input("Enter the number?")) 
while i<=10: 
    print("%d X %d = %d \n"%(number,i,number*i)); 
    i = i+1; 
>>>Output:
Enter the number?10
10 X 1 = 10
10 X 2 = 20
10 X 3 = 30
10 X 4 = 40
10 X 5 = 50
10 X 6 = 60
10 X 7 = 70
10 X 8 = 80
10 X 9 = 90
10 X 10 = 100
Using else with Python while loop
Python enables us to use the while loop with the while loop also. The else block is executed when the condition given in the while statement becomes false. Like for loop, if the while loop is broken using break statement, then the else block will not be executed and the statement present after else block will be executed.
Consider the following example.
i=1; 
while i<=5: 
    print(i) 
    i=i+1; 
else:print("The while loop exhausted"); 
>>>Output:
1
2
3
4
5
The while loop exhausted

Python continue Statement
The continue statement in python is used to bring the program control to the beginning of the loop. The continue statement skips the remaining lines of code inside the loop and start with the next iteration. It is mainly used for a particular condition inside the loop so that we can skip some specific code for a particular condition.
The syntax of Python continue statement is given below.
#loop statements 
continue; 
#the code to be skipped  

Example 1
i = 0; 
while i!=10: 
    print("%d"%i); 
    continue; 
    i=i+1; 
>>>Output:
infinite loop

Example 2
i=1; #initializing a local variable 
#starting a loop from 1 to 10 
for i in range(1,11): 
    if i==5: 
        continue; 
    print("%d"%i); 
>>>Output:
1
2
3
4
6
7
8
9
10
Python Pass
In Python, pass keyword is used to execute nothing; it means, when we don't want to execute code, the pass can be used to execute empty. It is same as the name refers to. It just makes the control to pass by without executing any code. If we want to bypass any code pass statement can be used.
Python Pass Syntax
pass 
Python Pass Example
for i in [1,2,3,4,5]: 
    if i==3: 
        pass 
        print "Pass when value is",i 
    print i, 
>>>Output:
1 2 Pass when value is 3 
3 4 5  

No comments:

Post a Comment

Python Datatype Interview Questions

Q)  What is Python? Python is a high-level, interpreted, interactive, and object-oriented scripting language. Python is designed to be...