1#x starts at 1 and goes up to 80 @ intervals of 2
2for x in range(1, 80, 2):
3 print(x)
1Python Loops
2
3for x in range(1, 80, 2):
4 print(x)
5
6words=['zero','one','two']
7for operator, word in enumerate(words):
8 print(word, operator)
9
10for x in range(1, 80, 2):
11 print(x)
12
13
14
15
16
1for i in range(1,10,2): #(initial,final but not included,gap)
2 print(i);
3 #output: 1,3,5,7,9
4
5for i in range (1,4): # (initial, final but not included)
6 print(i);
7 #output: 1,2,3 note: 4 not included
8
9for i in range (5):
10 print (i);
11 #output: 0,1,2,3,4 note: 5 not included
12
13python = ["ml","ai","dl"];
14for i in python:
15 print(i);
16 #output: ml,ai,dl
17
18for i in range(1,5): #empty loop...if pass not used then it will return error
19 pass;
20
1# A loop is used to iterate over a sequence
2# The format of a loop is
3for variable in object:
4 pass
5
6# A common use of a for loop is using the range() function
7for num in range(1, 10):
8 print(num)
9
10# It can also be used with a list
11new_list = ["Number 1", "Number 2", "Number 3", "Number 4"]
12for x in new_list:
13 print(x)
1#While Loop:
2while ("condition that if true the loop continues"):
3 #Do whatever you want here
4else: #If the while loop reaches the end do the things inside here
5 #Do whatever you want here
6
7#For Loop:
8for x in range("how many times you want to run"):
9 #Do whatever you want here
1# A Sample Python program to show loop (unlike many
2# other languages, it doesn't use ++)
3# this is for increment operator here start = 1,
4# stop = 5 and step = 1(by default)
5print("INCREMENTED FOR LOOP")
6for i in range(0, 5):
7 print(i)
8
9# this is for increment operator here start = 5,
10# stop = -1 and step = -1
11print("\n DECREMENTED FOR LOOP")
12for i in range(4, -1, -1):
13 print(i)