Python (Page-6)

 12345678910

Page-6

Repetition

Repetition of statements is achieved in python through Looping constructs. We need to repeat some statements to avoid writing a single code again and again. 

For example, if we need to print the first 5 natural numbers, how can we do it?

print(1)

print(2)

print(3)

print(4)

print(5)


But, what if we need to print the first 1000 natural numbers? It would not be a good way to write 1000 print statements. So, the solution to this problem is using Loops in python. 

There are two types of loops we use in python-

1. For Loop

2. While Loop

Both, the looping constructs have their own characteristics and are used in different scenarios. 

For Loop

For Loop is used to iterate over a range or sequence like List or String. The for loop is executed for each of the item in the range or sequence.     

Syntax-

for <control Variable> in <sequence or range>

Example-

for i in range(1,10,1)

    <statements in the body of the loop>

Here, the control variable "i" will traverse each value given in the range. For each value in the range or sequence will tell how many times the loop will execute.

The range() is a function that is passed three arguments-

a. Starting Value(by default it is 0)

b. Stop Value

c. Step Value(by default it is 1)

Starting value and Step value are optional. But, we need to mention the Stop Value necessarily. 

Program to Print First Five Natural Numbers


    While Loop

The While Loop executes the block of code until the control condition is true. The loop checks the condition after each iteration and stops the loop once the condition becomes false. 

Syntax of the While Loop-

while test_condition:

    body of while


Example-

while a>5:

    print("hello")


Print First Five Natural Numbers Using While Loop

Program- To reverse a given number



Break Statement

Sometimes, we may want to exit from the loop upon meeting a particular condition without continuing with further iterations. For this purpose, we use Break Statement. The control is transferred to the subsequent statements written after the loop. 

Example- Find the sum of all the positive numbers entered by the user. As soon as the user enters a negative number, stop taking in any further input from the user and display the sum.



when a negative number -5 is encountered, the loop terminates and the next statement is executed. 

Continue Statement


Continue Statement is used to skip the execution of the remaining statements inside a loop and jump to the next iteration. 

Example- Display the first 10 natural numbers except 3.




In the example above, when the value of i reaches 3, the continue statement occurs and it skips the further execution of the loop and goes to the next iteration. 

Nested Loop

A Loop inside another loop is called a nested Loop. There is no restriction on the number of loops inside another loop. 

Example- Write a Program to print a pattern like-

1

12

123

1234





 12345678910



No comments:

Post a Comment