Python is an object-oriented programming language ideally designed for rapid prototyping of complex applications. Like Perl, Python source code is also available under the GNU General Public License (GPL).

In this article we will see how to use, "for loop" and "While Loop", break, continue and How to Iterate a list in python with example.


1) Simple for loop in python:

for x in range (2,7):
	print x

Output: Output will be something like this:

2 3 4 5 6


2) How to Iterate through a list in python

list = [ 'tech', 786 , 2.23, 'burps', 70.2 ]

for x in list:
	print x

Output: Output will be something like this:

tech
786
2.23
burps
70.2


3) How to use 'break' in python for loop

list = [ 'tech', 786 , 2.23, 'burps', 70.2 ]

for x in list:
	if x==786:
		break;
	print x

Output: Output will be something like this:

tech


4) How to use 'continue' in python for loop

list = [ 'tech', 786 , 2.23, 'burps', 70.2 ]

for x in list:
	if x==786:
		continue;
	print x

Output: Output will be something like this:

tech
2.23
burps
70.2


5) How to use 'while loop' in python

i =0;
while i<3:
	print i
	i=i+1;

Output: Output will be something like this:

0 1 2


In this article we have seen, how to use, "for loop" and "While Loop", break, continue and How to Iterate a list in python. In upcoming articles we will see more complex usecases and tricks of python.
  • By Techburps.com
  • Sep 30, 2017
  • Python