How to use for loop in Python

14 Answers

0 votes
for i in range(10):
    print(i)


'''
run:

0
1
2
3
4
5
6
7
8
9

'''

 



answered Jun 22, 2020 by avibootz
0 votes
for ch in "python":
  print(ch) 



'''
run:

p
y
t
h
o
n

'''

 



answered Jun 22, 2020 by avibootz
0 votes
lst = ["python", "c++", "java"]
for s in lst:
  print(s) 



'''
run:

python
c++
java

'''

 



answered Jun 22, 2020 by avibootz
0 votes
lst = ["python", "c++", "java", "c"]
for s in lst:
  print(s) 
  if s == "java":
     break


'''
run:

python
c++
java

'''

 



answered Jun 22, 2020 by avibootz
0 votes
lst = ["python", "c++", "java", "c"]
for s in lst:
  if s == "c++":
     continue
  print(s) 



'''
run:

python
java
c

'''

 



answered Jun 22, 2020 by avibootz
edited May 6, 2024 by avibootz
0 votes
for i in range(10):
    if i == 4:
       continue
    print(i)


'''
run:
 
0
1
2
3
5
6
7
8
9
 
'''

 



answered Jun 22, 2020 by avibootz
0 votes
for i in range(10):
    print(i)
    if i == 4:
       break


'''
run:
 
0
1
2
3
4
 
'''

 



answered Jun 22, 2020 by avibootz
0 votes
for i in range(2, 5):
  print(i)


'''
run:
 
2
3
4
 
'''

 



answered Jun 23, 2020 by avibootz
0 votes
for i in range(2, 15, 3):
  print(i)


'''
run:
 
2
5
8
11
14
 
'''

 



answered Jun 23, 2020 by avibootz
0 votes
for i in range(5):
    print(i)
else:
    print("After for")


'''
run:
 
0
1
2
3
4
After for
 
'''

 



answered Jun 23, 2020 by avibootz
0 votes
for r in range(10, 1, -2):
    print(r)
    


'''
run

10
8
6
4
2

'''

 



answered Dec 31, 2020 by avibootz
0 votes
for i in range(1, 11):
    print(i)
 


'''
run:

1
2
3
4
5
6
7
8
9
10
 
'''

 



answered May 6, 2024 by avibootz
0 votes
for i in range(0, 10):
    print(i)
 

'''
run:

0
1
2
3
4
5
6
7
8
9
 
'''

 



answered May 6, 2024 by avibootz

Related questions

1 answer 181 views
1 answer 111 views
3 answers 260 views
6 answers 381 views
381 views asked Apr 23, 2021 by avibootz
1 answer 249 views
3 answers 242 views
...