0

For loops

Does a for loop always have to have i in it Because Idk

4th Apr 2024, 10:27 PM
froi pnon
froi pnon - avatar
8 ответов
+ 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".
5th Apr 2024, 1:36 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 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.”)
5th Apr 2024, 12:40 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 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 """
5th Apr 2024, 4:57 AM
Rain
Rain - avatar
+ 1
You have to iterate a variable then in the loop you have to print
5th Apr 2024, 11:22 AM
Kailash Yandrapu
+ 1
Any legal variable name may be used
5th Apr 2024, 4:25 PM
Λεμόνι
Λεμόνι - avatar
+ 1
Kailash Yandrapu For loop code blocks need not contain a print statement.
5th Apr 2024, 5:14 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 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)
6th Apr 2024, 1:04 AM
Kailash Yandrapu
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`.
11th Apr 2024, 6:40 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar