There are many programming languages available in the market, here one of the most programming language is Python which is widely used high-level programming language. With the multiple functions, it has become very popular. It has the capability to iterate over those things of any series.
Syntax :
for iterating_var in sequence:
statements(s)
Loop is a way by which expression can be executed up to given n number of the condition. If there is a sequence that contains an expression list so it will be evaluated first.
#!/usr/bin/python
for letter in Demo: # First Example
print 'First Example :', letter
Day = ['sun', 'mon', 'tue']
for days in day: # Second Example
print 'Second Example :', day
print "Got?"
If you have exact code and you are going to execute so you will get the following result
First Example: D
First Example : e
First Example : m
First Example : o
Second Example : sun
Second Example : mon
Second Example : tue
Got?
Iterating Python for loop using range() function :
Many of you have used range() in Python but here It is need to what exactly is. It creates a list of numbers, which is used for loops. This function works up to the particular range and runs fine.
Here we are going to understand iterating using the range(). See the following simple example which is given below..
#!/usr/bin/python
days = [sun, 'mon', 'tue']
for ran in range(len(days)):
print 'Current day :', days[ran]
print "Got?"
Here you can see below result when above code will be executed, see the following result
Current day : sun
Current day : mon
Current day : tue
Got?
Dictionary data type example using for loop :
As we have seen that Python is the widely used programming language in this each key value separated by a colon : , and all items are separated by commas, whole body will enclosed in curly braces. If there is an empty dictionary no any items so just two curly braces will be like {}.
Dictionary values can be of any type.

How to Access the Values in Dictionary:
Every execution produces some result so here we see how to access dictionary elements.
#!/usr/bin/python
dict = {'Name': 'Shail', 'Contact': 1234567890, 'City': 'Indore'}
print "dict['Name']: ", dict['Name']
print "dict['Contact']: ", dict['Contact']
After execution the above code, you will get following result
dict['Name']: Shail
dict[Contact]: 1234567890
Python Infinite Loop :
If you are executing code which contain the loop and when execution never get the condition false so it is called Infinite Loop. In this condition there is no end for results and continue run. It is good when client/server programming where the server required.
#!/usr/bin/python
var_name = 1
while var_name == 1 : # This constructs an infinite loop
number = raw_input("Please enter a number :")
print "You have entered: ", number
print "Good luck!"
Now when you execute above-given code you will get the following result
Enter a number :25
You entered: 25
Enter a number :7
You entered: 7
Enter a number :1987
You entered: 1987
Please enter a number between: Traceback (recent call last):
File "test.py", line 5, in <module>
num = raw_input("Please enter a number :")
KeyboardInterrupt
Author
bhawanisingh
Views
2,413
First release
Last update
Rating
0.00 star(s) 0 ratings
Top