Learning Looping In Python Program

Studying Recurrence In Python Program
Looping in all programming languages ​​to work for a computer to do something over and over again. There are two types of Looping the python programming language, which is looping using for and use while.
Looping for the programming language that is commonly called a counted loop iteration that can be counted, while the while loop that is commonly called uncounted loop iteration that can not be calculated. The difference in the two is:

Looping with for normally used to repeat execute code already known to the large number of iteration. As for use while typically used for loops that have terms and do not have the number of how many recurrence.

1.) The loop with For

General shape for the python syntax:

for index in range (number_of_looping):
# run this code
# run this code also
#this code will not be repeated because it is outside for

Example coding programs:
# file: Example LoopingFor.py
re = 10
for i in range (reset):
     print "Repeat Number-" + str (i)
Explanation of coding:
we determine a lot of recurrence by 10 times with the following statement

re = 10

The variable i serves to accommodate the number of indexes, and function range () function to create a list with the range of 0-10. The function str () function changes the data type of integer to a string.

for i in range(re):
print “Repeat Number-“+str(i)

The result:
Learning Looping In Python Program
Examples of other coding program using the list for:
# file: ExampleLoopingFor.py
items = ['rice', 'side dishes', 'tea', 'crackers']
for contents in items:
     print contents
The result:
Learning Looping In Python Program

2. )The Loop with While

General shape while the python syntax:

while (True):
# run this code
# This code is outside the while loop

Example coding programs:
#file: ExampleLoopingWhile.py
answer='yes'
count=0
while(answer=='yes'):
  count+=1
  answer=raw_input ("Want to loop again?")
print "Total looping:"+str(count)
Or you can try with a shape like this, using the key words break
#file: ExampleLoopingWhile.py
answer = 'yes'
count = 0
while (True):
  count += 1
  answer = raw_input ("Want to loop again?")
  if answer == 'no':
   break
print "Total looping:" +str (count)
Explanation Coding:
In the first line we define a variable to count, and also determines when the loop will stop. if the answer is not then the loop stops.

answer = ‘yes’
count = 0

Then next looping with while, then add one count variable to store the value of each repeat. Then ask the user, whether to stop repeating or not?

while (answer == ‘yes’):
count + = 1
answer = raw_input ( ” Want to loop again?  “)

Once completed redo, print the number of times of looping occurs

print “Total Looping: ” + str(count)

Results:

fajaryusufcom @ example: ~ $ python ExampleLoopingWhile.py
Want to loop again?  yes
Want to loop again?  yes
Want to loop again?  yes
Want to loop again?  yes
Want to loop again?  yes
Want to loop again? no
Total looping: 6

Learning Looping In Python Program

See also  Make Output By Javascript

Fajar (Ucup)

The owner and author of FajarYusuf.com and also someone who likes to share interesting information with others.

Leave a Reply

Your email address will not be published. Required fields are marked *