How to use for loop in Python?

0 votes
Can someone tell me how to use for loop in Python? I am new to Python. Thank you.
Mar 4 in Python by Trisha
36 views

1 answer to this question.

0 votes

There are multiple ways of using for loop in Python.

1. To iterate within a String

for letter in 'Priyaj':
   print 'Letter Present:', letter #This will print letters in the string that you iterate.

2. To iterate in an array/list

letter = ['a', 'b',  'c']
for index in range(len(letter)): #this will iterate through the element of the list/array
   print 'Current fruit :', letter[index]

3. To iterate in a range

for index in range (10,20): #this will iterate between 10 - 20
   print 'Current Number:', index 

Hope this helps.

answered Mar 4 by Priyaj
• 56,960 points

Related Questions In Python

0 votes
1 answer

How to use string.replace() in python 3.x

replace() is a method of <class 'str'> ...READ MORE

answered Aug 3, 2018 in Python by Priyaj
• 56,960 points
104 views
+1 vote
2 answers

How to use the pass statement in Python

In Python programming, pass is a null statement. The ...READ MORE

answered Apr 5 in Python by anonymous
75 views
0 votes
1 answer

how to use Binary tree search in Python

The key thing to notice is that ...READ MORE

answered Sep 27, 2018 in Python by Priyaj
• 56,960 points
87 views
0 votes
1 answer

In python how to test a string for a substring

if "ABCD" in "xxxxABCDyyyy": This can be used ...READ MORE

answered Oct 24, 2018 in Python by Priyaj
• 56,960 points
107 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 399 views
0 votes
1 answer
0 votes
2 answers

How to use threading in Python?

 Thread is the smallest unit of processing that ...READ MORE

answered Apr 6 in Python by anonymous
123 views
0 votes
1 answer

How to use “raise” keyword in Python

You can use it to raise errors ...READ MORE

answered Jul 30, 2018 in Python by Priyaj
• 56,960 points
52 views