1#x starts at 1 and goes up to 80 @ intervals of 2
2for x in range(1, 80, 2):
3 print(x)
1# prints Hello Geek 3 Times
2count = 0
3while (count < 3):
4 count = count+1
5 print("Hello Geek")
6
1# A for loop in python is used to iterate (or loop) over a sequence.
2# The sequence is usually range(start,end), but can also be a list.
3# Example of for loop with range():
4for num in range(1,10):
5 print(num)
6 # This prints all the numbers from 1 to 10, including 1 but excluding 10.
7
8 # Here is an example of a for loop with a list:
9lst = [1,2,3,4,5]
10for item in lst:
11 print(item)
12 # This prints all items in the list.
13
14# For loops are also used in list comprehensions.
15lst = [1,2,3,4,5]
16new_lst = [item for item in lst if i % 2 == 0]
17# The list comprehension above generates a new list, which new_lst is put equal to.
18# It iterates through the items in lst and only puts them in the new list if it is a multiple of 2.
19
1# how to use for in python for (range, lists)
2fruits = ["pineapple","apple", "banana", "cherry"]
3for x in fruits:
4 if x == "apple":
5 continue
6 if x == "banana":
7 break
8 print(x)
9# fron 2 to 30 by 3 step
10for x in range(2, 30, 3):
11 print(x)
1a_list = [1,2,3,4,5]
2
3#this loops through each element in the list and sets it equal to x
4for x in a_list:
5 print(x)