Loops
A for loop is used for
iterating over a sequence (that is either a list, a tuple, a dictionary, a set,
or a string).
This is less like
the for keyword in other programming languages, and works more like
an iterator method as found in other object-orientated programming languages.
Loops (repeated steps) have iteration variables that
change each time through a loop. Often
these iteration variables go through a sequence of numbers.
There are so many type of looping statement are available
to doing various operations
Python has two primitive loop
commands:
- while loops
- for loops
While Loops Are:
·
The while Loop
·
The break Statement
·
The continue Statement
·
The else Statement
For Loops Are: -
·
Looping
Through a String
·
The break
Statement
·
The
continue Statement
·
The range()
Function
·
Else in For
Loop
·
Nested
Loops
·
The pass
Statement
lest we first understand with use case diagram the how
the loop works:
Lest we understanding the all of methods one by one in
brief are mentioned here
1)
While Loop :
The while loop are used in conditional
looping i.e when the condition are true then executed the set of statement are
alog with condition are masoned
Lest we more getting clearance about the
syntax of the while loop
Code:-
x = 1
while x < 5:
print (x)
x += 1
·
The break Statement:
·
The break
statement ends the current loop and jumps to the statement immediately
following the loop
·
It is like
a loop test that can happen anywhere in the body of the loop
Code : -
while True:
line
= input('> ')
if line
== 'done' :
break
print(line)
print(“Done!”)
lest we understand with use case diagram how
the code is work,
The continue Statement : -
With the continue statement we can stop the current iteration, and
continue with the next: The continue statement ends the current
iteration and jumps to the top of the loop and starts the next iteration
Code : -
while True:
line = input('> ')
if line[0] == '#' :
continue
if line == 'done' :
break
print(line)
print('Done!')
lest we understand the continue loops is work
·
The else Statement : ( Repeated Steps )
With the else statement we can run a block of code once when the
condition no longer is true:
Loops (repeated steps) have iteration
variables that change each time through a loop.
Often these iteration variables go through a sequence of numbers.
Code :
n = 5
while n > 0 :
print(n)
n = n – 1
print('Techshoot03!')
print(n)
Indefinite Loops : -
•
While loops are called “indefinite
loops” because they keep going until a
logical condition becomes False
•
The loops we have seen so far are
pretty easy to examine to see if they will terminate or if they will be
“infinite loops”
•
Sometimes it is a little harder to be
sure if a loop will terminate
•
Quite often we have a list of items of
the lines in a file - effectively a finite set of things
•
We can write a loop to run the loop
once for each of the items in a set using the Python for construct
•
These loops are called “definite
loops” because they execute an exact number of times
A Definite Loop
with Strings: -
In the type of
the program user the String value instant of any logic or any of characters ( Special
character , numbers )
Code : -
frnd = ["encky", "mincky", "chincky"]
for x in frnd:
print(x)
The range() Function :
To loop through a set of code a specified number
of times, we can use the range() function,
The range() function returns a
sequence of numbers, starting from 0 by default, and increments by 1 (by
default), and ends at a specified number.
Using the range() function:
Code: -
for x in range(9):
print(x)
The is and is not Operators
•
Python has an is operator that can be used in
logical expressions
•
Implies “is the same as”
•
Similar to, but stronger than ==
•
is not
also is a logical operator
Code : -
smallest = None
print('Before')
for value in [13, 141, 112, 19,
714, 115] :
if smallest is None :
smallest = value
elif value < smallest :
smallest = value
print(smallest, value)
print('After', smallest)
No comments:
Post a Comment