Python Looping<\/strong><\/p>\nQuestion 22. \nWhat are the two major loop statements? \nAnswer: \nfor and while<\/p>\n
Question 23. \nUnder what circumstances would you use a while statement rather than for? \nAnswer: \nThe while statement is used for simple repetitive looping and the for statement is used when one wishes to iterate through a list of items, such as database records, characters in a string, etc.<\/p>\n
Question 24. \nWhat happens if you put an else statement after a block? \nAnswer: \nThe code in the else block is executed after the for loop completes, unless a break is encountered in the for loop execution, in which case the else block is not executed.<\/p>\n
Question 25. \nExplain the use of break and continue in Python looping. \nAnswer: \nThe break statement stops the execution of the current loop, and transfers control to the next block. The continue statement ends the current block’s execution and jumps to the next iteration of the loop.<\/p>\n
Question 26. \nWhen would you use a continue statement in a for loop? \nAnswer: \nWhen processing a particular item was complete; to move on to the next, without executing further processing in the block. \nThe continued statement says, “I’m done processing this item, move on to the next item.”<\/p>\n
Question 27. \nWhen would you use a break statement in a for loop? \nAnswer: \nWhen the loop has served its purpose. As an example, after finding the item in a list searched for, there is no need to keep looping. The break statement says, “I’m done in this loop; move on to the next block of code.”<\/p>\n
Question 28. \nWhat is the structure of a for loop? \nAnswer: \nfor <item> in <sequence>:… The ellipsis represents a code block to be executed, once for each item in the sequence. Within the block, the item is available as the current item from the entire list.<\/p>\n
Question 29. \nWhat is the structure of a while loop? \nAnswer: \nwhile <condition>:… The ellipsis represents a code block to be executed until the condition becomes false. The condition is an expression that is considered true unless it evaluates to 0, null or false.<\/p>\n
Question 30. \nUse a for loop and illustrate how you would define and print the characters in a string out, one per line. \nAnswer: \nmy String = “I Love Python” \nfor my Char in my String: \nprint myChar<\/p>\n
Question 31. \neven the string “I LoveQPython” uses a for loop and illustrates printing each character up to, but not including the Q. \nAnswer: \nmy String = “I Love Python” \nfor my car in my String: \nif my car = ‘Q’: \nbreak \nprint myChar<\/p>\n
Question 32. \nGiven the string “I Love Python” print out each character except for the spaces, using a for a loop. \nAnswer: \nmy String = “I Love Python” \nfor myChar in my String: \nif myChar \u2014 ‘ ‘ , \ncontinue print myChar<\/p>\n
Question 33. \nIllustrate how to execute a loop ten times. \nAnswer: \ni = 1 \nwhile i < 10: \ni+=1<\/p>\n
Question 34. \nWhen using a while loop, a condition was encountered that made staying in the loop pointless, what statement is used to transfer control? \nAnswer: \nThe break statement is used to terminate the processing of the loop and move on to the next block of code.<\/p>\n
Question 35. \nHow is execution in the while loop block abandoned, but the loop itself is not exited? \nAnswer: \nThe continue statement is used to terminate the processing of the block and move control to the next iteration of the loop.<\/p>\n
Question 36. \nWhat is a looping use of the range( ) function? \nAnswer: \nThe range function is used to generate a sequence of numbers for iteration. For example range(5) returns the list [0,1, 2, 3, 4] This list could be used in a a loop.<\/p>\n
Question 37. \nCan the else clause be used after a while loop? When is it executed? , \nAnswer: \nYes. The else block is executed after the while condition becomes false, but not if the while loop is exited with a break statement.<\/p>\n
Question 38. \nIllustrate how the range( ) and len( ) functions be used to iterate over the indices of a sequence? \nAnswer: \nmyltems = [T, ‘Love’, ‘Python’] \nfor i in rangeden(myltems)): \nprint i, myltems[i]<\/p>\n
Question 39. \nHow is the body of a loop defined? \nAnswer: \nThe body of the loop is defined by indentation.<\/p>\n
Question 40. \nHow are loops nested? \nAnswer: \nEver greater levels of indentation.<\/p>\n
Question 41. \nIllustrate a nested loop that uses the following list IT, \u2018Love’, ‘Python’] and outputs each character on a separate line. \nAnswer: \nmyltems = [T, ‘Love\u2019, ‘Python’] \nfor myWord in myltems: \nfor myChar in myWord: \nprint myChar<\/p>\n","protected":false},"excerpt":{"rendered":"
We have compiled most frequently asked Python Interview Questions which will help you with different expertise levels. Python Interview Questions on Decision Making and Loops Control Statements Control statements are used to control the flow of program execution. They help in deciding the next steps under specific conditions also allow repetitions of the program a …<\/p>\n
Python Interview Questions on Decision Making and Loops<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Interview Questions on Decision Making and Loops - Python Programs<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n