<jupyter_start><jupyter_text>while Loops In its most complex form, the while statement consists of a header line with a test expression, a body of one or more normally indented statements, and an optional else part that is executed if control exits the loop without a break statement being encountered. Python keeps evaluating the test at the top and executing the statements nested in the loop body until the test returns a false value:```while test:   statementselse:   statements```      Here are two simple examples of while loops<jupyter_code>x='dogs'
while x:
    print(x,end=" ")
    x=x[1:]                         # Strips first character off x
a,b=0,10
while(a<b and a<b-3):
    print(a,end=" ")
    a+=1<jupyter_output>0 1 2 3 4 5 6<jupyter_text>**break** : Jumps out of the closest enclosing loop (past the entire loop statement)**continue** : Jumps to the top of the closest enclosing loop (to the loop’s header line)**pass** : Does nothing at all: it’s an empty statement placeholder**Loop else block** : Runs if and only if the loop is exited normally (i.e., without hitting a break ) **General Loop Format** :```while test:   statements   if test: break   if test: continueelse:   statements```Some examples of the above statements are:<jupyter_code>#break
while True:
    name=input("Enter name:")
    if name == 'stop':
        print("Thanks for your time :)")
        break
    else:
        age=input("Enter age:")
        print('Hello',name,'=>',age)
#Continue
x=10
while x:
    x=x-1
    if(x%2!=0):
        continue                 # If x is an odd number,skip. 
    print(x,end=" ")
#Loop else
y=int(input())
x = y // 2
while x > 1:
   if y% x == 0:
       print(y, 'has factor', x)
       break
   x -= 1
else:
   print(y, 'is prime')<jupyter_output>12
12 has factor 6<jupyter_text>for Loops The following is the general format of for loops:```for target in object:    statementselse:    statements```The for loops's complete format can be described by:```for target in object:    statements    if test: break    if test: continueelse:    statements```Here are some examples of for loops:<jupyter_code>for x in ["dogs","cats","hens"]:
    print(x,end=" ")
sum=0
for x in [1,2,3,4]:
    sum=sum+x
print(sum)
S='lumberjack'
for x in S:
    print(x,end=",")
T=[(1,2),(3,4),(5,6)]
for (a,b) in T:
    print(a,b)
D={'a':1,'b':2,'c':3}
for key in D:
    print(key,'=>',D[key])<jupyter_output>a => 1
b => 2
c => 3<jupyter_text>Nested for Loops<jupyter_code>items = ["aaa", 111, (4, 5), 2.01]
tests = [(4, 5), 3.14]

for key in tests:
   for item in items:
        if item == key:
            print(key, "was found")
            break
   else:
        print(key, "not found!")<jupyter_output>(4, 5) was found
3.14 not found!<jupyter_text>Range function<jupyter_code>print(list(range(5)), list(range(2, 5)), list(range(0, 10, 2)))
for i in range(-3,3):
   print(i, 'Pythons')
S = 'spam'
for i in range(len(S)):
   S = S[i:] + S[:i]
   print(S, end=' ')
S = 'abcdefghijk'
for c in S[::2]: 
    print(c, end=' ')<jupyter_output>a c e g i k<jupyter_text>Zip<jupyter_code>l1=[1,2,3,4]
l2=[5,6,7,8]
for (x,y) in zip(l1,l2):
    print(x, y,'---',x+y)<jupyter_output>1 5 --- 6
2 6 --- 8
3 7 --- 10
4 8 --- 12
{'fraction_non_alphanumeric': 0.14889648741063102, 'fraction_numerical': 0.030463164438918246, 'mean_word_length': 3.457063711911357, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '234623', 'license': 'MIT', 'n_tokens_mistral': 1212, 'n_tokens_neox': 1135, 'n_words': 397, 'path': 'Loops and Functions.ipynb', 'repo_name': 'ayushs2k1/Python-Fundamentals', 'size': '3217'}