Try using this:

for k in range(1,c+1,2):
Answer from carlosdc on Stack Overflow
๐ŸŒ
Python
peps.python.org โ€บ pep-0284
PEP 284 โ€“ Integer for-loops | peps.python.org
March 1, 2002 - One of the most common uses of for-loops in Python is to iterate over an interval of integers. Python provides functions range() and xrange() to generate lists and iterators for such intervals, which work best for the most frequent case: half-open ...
๐ŸŒ
Snakify
snakify.org โ€บ for loop with range
For loop with range - Learn Python 3 - Snakify
Let's have more complex example and sum the integers from 1 to n inclusively. ... result = 0 n = 5 for i in range(1, n + 1): result += i # this ^^ is the shorthand for # result = result + i print(result) Pay attention that maximum value in range() is n + 1 to make i equal to n on the last step. To iterate over a decreasing sequence, we can use an extended form of range() with three arguments - range(start_value, end_value, step).
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ is there a clean way to treat "i" in a for loop as an integer? (example code inside)
r/learnpython on Reddit: Is there a clean way to treat "i" in a for loop as an integer? (Example code inside)
February 26, 2019 -

I wrote an example code. I want to treat "i" in the for loop as an integer. In this case I needed it to index though a list. I find myself writing a "counting variable" really often, and I was wondering if there was a better way to write it.

#this program will form pairs of fighters from the fighters list 

fighters = ['ryu', 'ken', 'zangief', 'balrog', 'chun-li', 'blanka', 'sagat', 'ehonda']

count = 0

for i in range(int(len(fighters)/2)):
     print(fighters[count] + " will fight " + fighters[count + 1])
     count = count + 2
Top answer
1 of 5
106
Rather than have a counting variable, you could enumerate: for index, value in enumerate(fighters): print(index, value) 0 ryu 1 ken 2 zangief 3 balrog 4 chun-li 5 blanka 6 sagat 7 ehonda For your given example, though, you could zip: for first, second in zip(fighters, fighters[2:]): print(first, second) ryu zangief ken balrog zangief chun-li balrog blanka chun-li sagat blanka ehonda You could also check out combinations , although it'll produce more pairings than your current version, it's a bit easier to use. Edit: Editor mangling. Edit: u/CylonSaydrah has suggested the following improvement to the zip version, which uses a step of 2 to skip elements in each list: for first, second in zip(fighters[::2],fighters[1::2]):
2 of 5
11
Short answer: i is already an integer. Long Answer The range() built-in allows you iterate over a range of numbers, so in the sequence for i in range(10), i starts at 0 and counts up to 9 for the duration of the loop. >>> for i in range(10) ... print(f"{type(i)} {i}") ... 0 1 2 # -- 8< SNIP --- Another note is that it's not necessary to call int() on len(fighters) / 2 if you want to truncate the result; instead, use the floor division operator // (IIRC it always rounds down). Anyway, for the code snippet you provided, you don't need to use // anyway as you can just leverage range()'s step parameter. range() doesn't take keyword arguments, so you have to specify the start index (0) as well: for i in range(0, len(fighters), 2): print(fighters[i] + " will fight " + fighters[i+1]) However, if the roster is always going to be ordered the same, you can also couple the fighters in tuples: fighters = [("ruy", "ken"), ("zangief", "balrog"), ("chun-li", "blanka"), ("sagat", "ehonda")]. You may also want to take a look at zip() which does the above: >>> player1 = ["ruy", "zangief", "chun-li", "sagat"] >>> player2 = ["ken", "balrog", "blanka", "ehonda"] >>> zipped = zip(player1, player2) >>> list(zipped) [('ruy', 'ken'), ('zangief', 'balrog'), ('chun-li', 'blanka'), ('sagat', 'ehonda')] Then you just iterate over zipped: >>> for pair in zipped: ... print(pair[0] + " will fight " + pair[1]) EDIT: Thanks to u/warbird2k for pointing out that you can iterate over zipped immediately without wrapping it in a list.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ how to create a for loop using an integer input??
r/learnprogramming on Reddit: How to create a for loop using an integer input??
February 4, 2025 -

Hello! I'm new to coding and I'm learning for loops, and decided to start playing around with them, because why not? I for the LIFE of me can't figure out how to get a for loop to print the word "Python" for the amount of the number I input lol. What I'm essentially trying to do is have an input ask me "How many times can you say python", and whatever my integer input is, iterate "Python" that number of times. This is what I IMAGINE it would look like, but to no avail. (Sorry if this code looks silly, it doesn't even work but I'm trying LOL!)

python_count = int(input("How many times can you say python?"))                   say_python = python_count                                                               for x in say_python:                                                       print("Python!")
๐ŸŒ
OpenStax
openstax.org โ€บ books โ€บ introduction-python-programming โ€บ pages โ€บ 5-2-for-loop
5.2 For loop - Introduction to Python Programming | OpenStax
March 13, 2024 - A for loop can be used for iteration and counting. The range() function is a common approach for implementing counting in a for loop. A range() function generates a sequence of integers between the two numbers given a step size.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_for_loops.htm
Python - For Loops
Python's list object is also an indexed sequence, and hence you can iterate over its items using a for loop. In the following example, the for loop traverses a list containing integers and prints only those which are divisible by 2.
Find elsewhere
๐ŸŒ
w3resource
w3resource.com โ€บ python โ€บ python-for-loop.php
Python for loop
September 20, 2024 - The for loop assigns the first item ("Red") to the variable c, then executes the print(c) statement. This process repeats for each item in the list until all items are printed. The range() function returns a list of consecutive integers.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-use-for-loop-in-python
How to use for loop in Python?
March 10, 2021 - Python's built-in range() function returns an iterator object that streams a sequence of numbers. This object contains integers from start to stop, separated by step parameter. You can run a for loop with range as well. ... Start โˆ’ Starting value of the range. Optional. Default is 0 ... Step โˆ’ Integers in the range increment by the step value. Option, default is 1. In this example, we will see the use ...
๐ŸŒ
Codementor
codementor.io โ€บ community โ€บ making python integers iterable
Making Python Integers Iterable | Codementor
December 10, 2020 - Even before the first user command is executed, the Python interpreter, while booting up, has already created 406 lists, for its internal usage. In the example below, we see how a list a is iterated through using a for ... in loop and each element can be accessed via variable x. >>> a = [2, 3, 5, 7, 11, 13] >>> for x in a: print(x, end=" ") 2 3 5 7 11 13 ยท Similar to list, range is a python type that allows us to iterate on integer values starting with the value start and going till end while stepping over step values at each time.
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ ForLoop
ForLoop - Python Wiki
In Python, these are heavily used whenever someone has a list of lists - an iterable object within an iterable object. for x in range(1, 11): for y in range(1, 11): print('%d * %d = %d' % (x, y, x*y)) ... Like the while loop, the for loop can be made to exit before the given object is finished.
๐ŸŒ
Real Python
realpython.com โ€บ python-for-loop
Python for Loops: The Pythonic Way โ€“ Real Python
February 23, 2026 - Learn how to use Python for loops to iterate over lists, tuples, strings, and dictionaries with Pythonic looping techniques.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_for_loops.asp
Python For Loops
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
For Loop in Python - Python Help - Discussions on Python.org
September 7, 2023 - Pardon if I have been posting series of questions. Newbie Python learner here, and am going through PCEP materials, and having some questions along the wayโ€ฆ Thank you in advance for the help. In these two codes, I confused why the answer is such: Code 1: my_list = [0,1,2,3] x = 1 for elem in my_list: x *= elem print (x) Answer: [0] Code 2: my_list = [0,1,2,3] x = 1 for elem in my_list: new_list = x * elem print (new_list) Answer: [3] Question 3: I want to create a code that multi...
๐ŸŒ
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu โ€บ notebooks โ€บ chapter05.01-For-Loops.html
For-Loops โ€” Python Numerical Methods
EXAMPLE: Given a list of integers, a, add all the elements of a. s = 0 a = [2, 3, 1, 3, 3] for i in a: s += i # note this is equivalent to s = s + i print(s) ... The Python function sum has already been written to handle the previous example. However, assume you wish to add only the even numbers.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-for-loops
Python For Loops - GeeksforGeeks
The else block just after for/while is executed only when the loop is NOT terminated by a break statement. ... In Python, enumerate() function is used with the for loop to iterate over an iterable while also keeping track of index of each item.
Published ย  4 weeks ago
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_for_range.asp
Python Looping Through a Range
Python For Loops Tutorial For Loop Through a String For Break For Continue For Else Nested Loops For pass ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com