-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoops.py
More file actions
164 lines (141 loc) · 3.78 KB
/
Copy pathLoops.py
File metadata and controls
164 lines (141 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#Their are two types of loops in python: for and while
'''Syntax :
var = conditon maker
while condition:
your code
(updation)
'''
#Ex: print hello 5x .
'''count = 1 #iterators
while count <= 5:
print("Hello!", count)
count += 1
#iteration = no.of times we looped the code
#print(count)'''
#q] print numbers from 1 to 100
'''i = 1
while i <= 100:
print(i)
i += 1
print("Loop Ended")'''
#q2]print numbers from 100 to 1
'''i = 100
while i >= 1:
print(i)
i -= 1
print("Blast off!")
#q3] print the table of number n.
n = 3
i = 1
while i <= 10:
print(i*n)
i += 1
#q4] print the elements of the following list using a loop: [1,4,9,16,36,49,81,100]
nums = [1,4,9,16,36,49,64,81,100]
idx = 0
while idx <= len(nums):
print(nums[idx]) #--->nums[idx] = gives the names of the list
idx += 1'''
#q5] search for a number x in this tuple using loop
'''tup = (1,4,9,16,25,36,49,64,81,100)
x = 49
i = 0
while i < len(tup):
if(tup[i] == x):
print("Found your number at index ", i)
else:
print("Not available")
i += 1'''
#Break = terminates the loop
#Conti inue = skips / terminates the loop at the point it is written such that the later code is not executed
'''i = 0
while i <= 5:
if(i == 3):
i += 1
continue #will skip 3 because 3 turned 4 as directly went to the loop again
print(i)
i += 1
while i <= 5:
if( i == 3):
break
print(i)
i += 1'''
#for loops are used for the data types which have index like list,tupples,etc.
'''Syntax :
for element in list:
some work '''
#ex 1:
'''Anime = ["Solo Leveling", "Demon Slayer","Naruto", "Baruto", "Drangon Ball"]
for name in Anime:
print(name)'''
#ex 2:
'''Manga = ("Desharow Mermaid", "Lying puppies get eaten", "Heaven's offocial Blessing")
for name in Manga:
print(name)'''
#ex 3:
'''fav = "SherLiam"
for char in fav:
print(char)
else:
print("...")'''
#ex 4:Print the elements of the list
'''list = [1,4,9,16,25,36,49,64,81,100]
for el in list:
print(el)'''
#ex 5:Search for a number x in this tupple
'''tup = (1,4,9,16,25,36,49,64,81,100)
x = 100
idx = 0
for el in tup:
if(el == x):
print("x found at index", idx)
break
idx += 1
print(el)
else:
print("None")'''
#range is a function that returns a number of elements of numbers?, etc.
'''Syntax:
range( starting value[optional], stopping value, step size[optional])
start value = where you wanna start or by default 0,
stop value = where you wanna stop (given number not included),
step size = from how much you wanna increase[ by default +1].'''
#ex 1:
'''for i in range(3,31,3):
print(i)'''
#ex 2:Print numbers from 1 to 100
'''for i in range(1, 101):
print(i)
print("End")
#ex 3:Print numbers from 100 to 1
for i in range(100, 0, -1):
print(i)
print("End this loop!")
#ex 4:Print the multiplication table of a number n
n = 5
for i in range(n, 11*n, n):
print(i)'''
#pass = pass statement is used when we want our loop or if-else condition to have null value
'''for i in range(6):
pass
if i >100:
pass
#it is used to as a placeholder for the future code
#if you laze around or excited about the below code and you just don't want to write te code for the loo or if-else you use pass!
print("9")'''
#Bex 2:Wap to find the factorial of first n numbers. (using for)
'''n = 10
fact = 1
for i in range(1,n+1):
fact *= i
i += 1
print("factorial =",fact)
#for i in range(1,n+1,)'''
# to iterate over a list use range(len(my_list)):
# my_list[i]
def change_elements(lst , index, new_element):
change = lst.insert(index, new_element)
change = lst.remove(index + 1)
return change
new = change_elements(['a','b','c'],1,'d')
print(new)