0
For loops
Does a for loop always have to have i in it Because Idk
8 Respuestas
+ 3
You can name "i" to anything you like.
For example:
for word in words: # words = ["A", "B", "C", "D", "E"]
print(word) # output: A, B, C, D, E
We use "i" in examples because "i" is short for "item".
It is much like we use "res" for "result".
+ 2
The for loop has to have a iter variable to assign each element of the iterable to, but it doesn’t have to be called i, and the loop code block doesn’t have to use it. For example…
for ignore in [1, 5, “chex”, True]:
print(“This gets executed four times.”)
+ 2
"""
froi pnon ,
Be aware that a for loop doesn't create a new namespace or scope. If you use an existing name, it'll get reassigned the values of the iterable and retain the most recent value when the loop ends.
"""
test = "This is vulnerable."
for test in range(3):
print(test)
print(test)
""" Output:
0
1
2
2
"""
+ 1
You have to iterate a variable then in the loop you have to print
+ 1
Any legal variable name may be used
+ 1
Kailash Yandrapu For loop code blocks need not contain a print statement.
+ 1
Wilbur Jaywright
Based on the usage or requirement we have to use print in for loop code blocks
example for using print in for code block
i want to print numbers from 1 to 10 in python
for i in range(1,11):
print(i)
example for without using print in for code block
c=0
for i in range(1,11):
if(i%2==0):
c+=1
print(c)
0
No, a for loop does not always have to have `i` in it. While the convention is to use `i` as a loop variable (especially in languages like C, C++, Python, Java, etc.), you can use any valid variable name as the loop variable in a for loop. The key is that the variable should be initialized, updated, and used in the loop condition properly. Here's an example in Python:
for count in range(5):
print(count)
In this example, `count` is used as the loop variable instead of the traditional `i`.