Loops in python 3
The looping statements allow a set of instructions to be
performed repeatedly until a certain condition is fulfilled. The looping
statements are also called as iterations statements.
Python provides 2 kinds of loops:
- For loop in python 3
- While loop
for loop in python 3 | for loop in python shell
The for loop
in Python is designed to process the items of any sequence, such as a list or a
string one by one.
Syntax:
for variable-name in sequence:
#statements
For example:
for a in (1,4,7):
Print(a)
Print(a*a)
Here, the loop variable a. Variable a will be
assigned each value of list one by one, for the first time ‘a’ will be 1 then 4
and then 7.
Firstly, the loop variable ‘a’ will be assigned first value
of list 1 and the statements in the body of the loop will be executed with this
value of ‘a’. Hence value 1 will be printed with statement print(a) value
1 will again be printed with print(a*a).
Next, ‘a’ will be assigned next value in the list 4 and loop
body executed. Thus 4 as result of print(a) and 16 as result of print(a*a) are
printed.
Next, ‘a’ will be assigned next value in the list 7 and loop
body executed. Thus 7 as result of print(a) and 49 as result of print(a*a) are
printed.
All the values in the list are executed hence loop ends.
Output
1
1
4
16
7
49
Let’s take another example of for loop
for ch in ‘calm’:
print(ch)
The above loop will produce output as:
c
a
l
m
Here variable ch given values as ‘c’, ’a’, ‘l’, ‘m’ from the
string ’calm’.
But what if we have to iterate through a very large list
such as containing natural numbers between 1 to 100? Writing such a big list is
neither easy nor good on readability. Then we use range () function to
represent a list.
For example:
for val in range (2,16):
print (val)
Here, range (2,16) will first generate a list [2, 3, 4, 5, .
. .., 14,15] with which for loop will work.
python for loop example
Question: Write a program to print a table of 5 number?
Answer:
Question: Write a
program to print sum of natural numbers between 1 to 7. Print the sum after
adding each natural number and print sum so far?
Answer:
While loop in python
A while loop is a conditional loop that will repeat the
instructions within itself as long as a conditional remains true.
Python while loop syntax:
while <condition> :
#statement
The loop iterates while the condition evaluates to true.
When the condition becomes false, the program control passes to the line after
the loop-body.
For example:
a=5
while a>0:
print (“Hello
Python”, a)
a=a-3
print (“Loop is over!!”)
Output:
Hello Python 5
Hello Python 2
Loop is over!!
Here, the condition is tested. If it is true, the loop body
is executed.
The condition a>5 is evaluated. If the condition is true
then all statements in the loop’s body are executed. If the condition is false
then control moves to the next statement after the body of the loop.
Let’s take another example
n=1
while n<5 :
print (“Square
of”, n, “is”, n*n)
n=n+1
print (“Thank you”)
Output:
Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Thank you
Thus, you see that as long as the condition n<5 is true
above, the while loop’s body is executed. After exiting from the loop, the
statement following the while loop is executed that prints Thank you.
While loop has 4 control elements
1. Initialisation Expressions (Starting): Before entering
in a while loop, its loop variable must be initialised. The initialization of
the loop variable takes place under initialization expressions. The
initialization expression gives the loop variable their first value. The
initialization expression for a while loop are outside the while loop before it
starts.
2.Test Expression (Repeating or stopping): The test expression
whose truth value decides whether the loop body will be executed or not. If the
test expression evaluates to true, the loop body gets executed, other wise the
loop is terminated.
In a while loop, the test expression is evaluated before
entering into a loop.
3.The Body of the Loop (Doing): The statements that are executed
repeatedly from the body of the loop. In a while loop, before every iteration
the test expression is evaluated and if it is true, the body of the loop is
executed; if the test expression evaluates to false, the loop is terminated.
4.Update Expression (Changing): The update expression changes
the value of loop variable. The update expression is given as a statement
inside the body of while loop.
It is also called entry-controlled loop because it
controls the entry of the loop by testing a condition.
Python does not offer any exit-controlled loop.
Python while loop questions
Question: Write a program to accept transaction made in a
day and items sold in day for a week and then print average sales made per
transaction?
Answer:
Question: Write a program to calculate total amount
raised in a charity camp where people can buy donated items or even make monetary
donations. The charity camp ran for 3 days. Also, calculate if they were able
to raise target amount of Rs 2,00,000.
Answer:
No comments:
Post a Comment