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, 2019 in Python by Trisha
38 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, 2019 by Priyaj
• 57,490 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
• 57,490 points
132 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, 2019 in Python by anonymous
83 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
• 57,490 points
102 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
• 57,490 points
134 views
0 votes
1 answer
+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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 539 views
0 votes
0 answers
0 votes
1 answer

Python: For loop to iterate class object

Refer to the below code: class Try: ...READ MORE

answered May 22, 2019 in Python by Raj
45 views
0 votes
2 answers

How to use threading in Python?

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

answered Apr 6, 2019 in Python by anonymous
147 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
• 57,490 points
59 views