Python For Loop
The for statement in Python differs a bit from what you may be used to in C The for..in statement is another looping statement which iterates over a sequence of objects i.e. go through each item in a sequence.
Syntax:
for var in sequence:
statements(s)
Note that range() generates only one number at a time, if you want the full list of numbers, call list() on the range()
program on for loop.
for k in "vision computers":
print(k)
program to print the numbers from 1 to 10
for i in range(1, 10):
print(i)
print the numbers from 1 to n
n=int(input("Enter n value"))
for i in range(1, n):
print(i)
else:
print('The for loop is over')
print the odd numbers from 1 to 50
for i in range(1, 50,2):
print(i)
print the even numbers from 50 to 0
for i in range(50, 1,-2):
print(i)
Note that range() generates only one number at a time, if you want the full list of numbers, call list() on the range()
Ex: list(range(5))
Print the numbers from 1 to 50 using list range
for i in list(range(50)):
print(i)
1
12
123
1234
12345
for i in list(range(6)):
for j in list(range(i)):
print(j+1,end=””)
print()
For
More Explanation
&
Online Classes
More Explanation
&
Online Classes
Contact Us:
+919885348743
+919885348743