We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1
Question;Answer
What is an iterative structure in programming?;An iterative structure, or loop,
allows repeating a block of code multiple times based on certain conditions. Why are iterative structures fundamental in programming?;They automate repetitive tasks, making code more efficient and concise. What is a 'for' loop used for?;A 'for' loop iterates over a sequence or iterable object, ideal for repeating operations a specific number of times. When is a 'for' loop particularly useful?;When you want to execute code once for each item in a collection or for a fixed range of values. What is the basic syntax of a 'for' loop in Python?;for variable in sequence: (code block) What type of data structure is a list in Python?;A list is a collection of items (numbers, strings, or other lists) stored in a specific order. How do you access individual elements in a Python list?;By using an index, with indexing starting at 0 for the first element. Can 'for' loops iterate over strings in Python?;Yes, 'for' loops can iterate over each character in a string individually. What does the 'range()' function do in a 'for' loop?;It generates a sequence of numbers for repeating an action a specific number of times. What are the three parameters of the 'range()' function?;start, stop, and step. What is the default starting value of the 'range()' function if 'start' is omitted?;0 What does the 'stop' parameter in 'range()' represent?;It is the endpoint, but the sequence stops at stop - 1. What is the default 'step' value in the 'range()' function?;1 What is a 'while' loop used for?;A 'while' loop repeatedly executes a block of code as long as a given condition is true. What is the syntax of a basic 'while' loop in Python?;while condition: (code block) What does the 'break' statement do in a loop?;It terminates the loop completely, skipping any remaining iterations. What is the purpose of the 'continue' statement in a loop?;It skips the current iteration and moves to the next one. How is 'break' typically used in loops?;To exit a loop when a specific condition is met. How is 'continue' used in loops?;To skip the rest of the code in the current iteration and start the next iteration. In a list, how would you find the first even number greater than 10 using a loop?;Use a loop with a condition and a 'break' statement when the condition is met. What are nested loops?;Loops within loops, where each iteration of the outer loop runs the inner loop completely. When are nested loops commonly used?;For handling multi-dimensional data, repeated patterns, and layered repetition scenarios. What is the primary use of a 'for' loop with 'range()' and nested loops?;To generate structured outputs like multiplication tables or grids. If a loop has 'for i in range(3): for j in range(2): print(i, j)', what will the output be?;0 0, 0 1, 1 0, 1 1, 2 0, 2 1 How does Python handle indentation in nested loops?;Each level of loop requires additional indentation for its code block. What would 'for i in range(3): print(i)' print?;0, 1, 2 What will be printed if a loop uses 'for i in range(5): if i == 3: break; print(i)'?;0, 1, 2 (the loop stops at 3) What will be printed if a loop uses 'for i in range(5): if i == 3: continue; print(i)'?;0, 1, 2, 4 (3 is skipped)