python for loop increment by 2 – Increment by 2 in Python for Loop With the range() Function and Loop Using the Slicing Method, Using while loop and Using another variable.
for Loop Increment by 2 in Python
there are the following 5+ Essential Python For Loop Command Examples with demo.
Loop With the range() Function
Example : 1
for num in range(0, 12, 2): print(num)
Result
0 2 4 6 8 10
Loop Using the Slicing Method
Example : 2
ranks = [1,2,3,4,5,6,7,8,9,10] for num in ranks[1::2]: print (num)
Result
2 4 6 8 10
increment Iterator from inside the For loop in Python
Using while loop
ranks = [1, 2, 3, 4, 5] num = 0 while(num < len(ranks)): print(ranks[num], end = " ") num += 2
Output
1 3 5
Using another variable:
Example
ranks = [1, 2, 3, 4, 5] num = 0 for j in range(len(ranks)): if(num >= len(ranks)): break print(ranks[num], end = " ") num += 2
Output
1 3 5
don't Miss : bash for loop
Python For Loop Command Examples
here demo 5 for python for loop increment by 2.
1. Python For Loop for Numbers
# cat demo1.py for num in [1, 2, 3, 4, 5]: print(num)
2. Python For Loop for Strings
# cat demo2.py websites = ["pakainfo", "w3diy", "infinityknow"] for num in websites: print(num)
3. Python For Loop Using Default Range Function
# cat demo3.py for num in range(5): print(num)
4. Python For Loop With Custom Start and End Numbers
# cat demo4.py for num in range(1,6): print(num)
5. Python For Loop With Incremental Numbers
# cat demo5.py for num in range(1,6,2): print(num)
I hope you get an idea about python for loop increment by 2.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.