# Simple while
# Loop runs till the condition is True
v1 = 5
while v1 <= 10:
print (v1)
v1 += 1
# Infinite Loops
while 1:
print (1)
while True:
print (1)
# One Liner while
v1 = 0
while v1 <= 40: v1 += 1
print (v1)
v1 = 2
++v1
++v1
print (v1)
# Terminate loop on a certain user input
# Note: The loop will break only when the user inputs 100
v1 = 1
while v1 != 100:
v1 = int(input("Enter new v1: "))
print ("v1 modified to: " + str(v1))
# 'break' -> breaks out of loop, doesn't execute any statement after it
while 1:
v1 = int(input())
if v1 == 100:
break;
print (v1)
# 'continue' -> continues to next iteration, skips all statements after it for that iteration
# Note: When 'v1' < 100 the last print statement is skipped and the control moves to the next iteration
while 1:
print ("Iteration begins")
v1 = int(input())
if v1 == 100:
break;
elif v1 < 100:
print ("v1 less than 100")
continue;
print ("Iteration complete")
# while with lists
l1 = ["Jennifer", 12, "Python", 'A', 56, 'B', 2.12, "Scarlett"]
l2 = []
i = 0
while i < len(l1):
if type(l1[i]) == int:
l2.append(l1[i])
i += 1
print (l2)
# Removing all instances of a specific value in list
l1 = ['A', 'B', 'C', 'D', 'A', 'E', 'Q', 'A', 'Z', 'A', 'Q', 'D', 'A']
while 'A' in l1: l1.remove('A')
print (l1)
# Filing a dictionary
d1 = {}
while 1:
key = input("Enter a key: ")
value = input("Enter a value: ")
d1[key] = value;
if input("exit? ") == "yes": break;
print (d1)