0% found this document useful (0 votes)
21 views2 pages

Python Control Statements: 50 Questions

Uploaded by

avkarthik2004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Python Control Statements: 50 Questions

Uploaded by

avkarthik2004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

50 Python Questions on Conditional & Control

Statements

A. Conditional Statements (if, if-else, nested if)


Write a program to check whether a given number is even or odd.
Write a program to check whether a person is eligible to vote (age ≥ 18).
Write a program to find the largest of two numbers using if-else.
Write a program to check whether a given number is positive, negative, or zero.
Write a program to find the largest of three numbers using nested if.
Write a program to check whether a given year is a leap year.
Write a program to check whether a given character is a vowel or consonant.
Write a program to check whether a given number is divisible by 5 and 11.
Write a program to check whether a given number is a three-digit number.
Write a program to calculate the grade of a student based on marks (A, B, C, D, Fail).

B. Looping Statements (for, while)


Write a program to print numbers from 1 to 10 using a for loop.
Write a program to print numbers from 1 to 10 using a while loop.
Write a program to print even numbers between 1 and 50.
Write a program to print odd numbers between 1 and 50.
Write a program to print the multiplication table of a given number.
Write a program to print the sum of the first 10 natural numbers.
Write a program to find the factorial of a given number.
Write a program to print the Fibonacci series up to n terms.
Write a program to reverse a number using a loop.
Write a program to count the number of digits in a given number.

C. Break, Continue, Pass


Write a program to demonstrate the use of break in a loop.
Write a program to demonstrate the use of continue in a loop.
Write a program to demonstrate the use of pass in a loop.
Write a program to exit the loop when a user enters 0.
Write a program to skip printing multiples of 3 between 1 and 20.
Write a program that searches for an element in a list and stops when found (using break).
Write a program that prints only odd numbers using continue.
Write a program to show how pass can be used inside an if condition.
Write a program that keeps asking for a password until the correct password is entered.
Write a program to print numbers from 1 to 10, but stop when 5 is reached.

D. Loop with else


Write a program to check if a number is prime using for-else.
Write a program to search for an element in a list using for-else.
Write a program to check whether a string contains a given character using for-else.
Write a program to check if a list is empty using loop with else.
Write a program to print numbers from 1 to 10 with an else block at the end.
E. Nested Loops
Write a program to print a square pattern of stars using nested loops.
Write a program to print a right-angled triangle of stars.
Write a program to print a multiplication table using nested loops.
Write a program to print all prime numbers between 1 and 50 using nested loops.
Write a program to print a pyramid pattern using stars.

F. Practical Problems with Control Statements


Write a program to check if a number is an Armstrong number.
Write a program to check if a number is a palindrome.
Write a program to find the sum of digits of a number.
Write a program to find the reverse of a string using loops.
Write a program to count vowels and consonants in a string.
Write a program to find the smallest element in a list using loops.
Write a program to find the largest element in a list using loops.
Write a program to count the frequency of each character in a string using loops.
Write a program to find the sum of all even numbers in a list.
Write a program to find the product of all odd numbers in a list.

Common questions

Powered by AI

Finding factorials with iterative loops presents challenges such as managing large numbers and ensuring efficient accumulative product computation. This exercise demonstrates mastery over control statements by emphasizing the importance of loop initialization, update mechanisms, and termination conditions. It also highlights potential pitfalls in overflow management and precision, offering insights into resource constraints and algorithm optimizations prevalent in large computations .

Using loops with an ‘else’ clause in scenarios like prime checking or list searches elucidates the program's purpose by naturally communicating that a certain condition wasn’t met during iteration (e.g., lack of divisors for primality or unsatisfied search). It enhances readability by embedding the success condition validation directly within the loop statement, removing secondary conditions post-iteration and providing streamlined, clear code logic that is both transparent and concise .

Implementing string reversal using loops instead of built-in methods offers insight into manipulating data structures manually, deepening understanding of iteration and index management. It demonstrates algorithm efficiency, such as time complexity, and involves manually iterating from the end of the string to the beginning while appending characters, which reinforces foundational programming skills necessary in situations where library functions may not be available .

Implementing programs for verifying Armstrong numbers or palindromes aids problem-solving by encouraging decomposition of complex problems into manageable parts, understanding modular arithmetic, and utilizing loops effectively for iterative checks. It exercises creativity in logical structuring, pattern recognition, and enhances algorithmic thinking by requiring manipulation and transformation of numbers, fostering advanced programming competencies and innovative approaches in number theory .

Conditional statements in Python enhance decision-making by allowing programs to test variables and execute different actions based on conditions. For instance, determining leap years involves checking divisibility by 4, excluding years divisible by 100 unless they are also divisible by 400, facilitating correct classification. Similarly, eligibility to vote can be assessed by checking if the age is 18 or older, allowing programs to function based on conditions reflecting real-world rules .

In control flow, 'break' terminates the current loop prematurely, 'continue' skips the rest of the current loop iteration and proceeds to the next, and 'pass' is a null operation placeholder that acts syntactically without any impact. In a loop, 'break' could be used to exit when a specific condition is met, like ending input when a user enters '0'. 'Continue' might skip processing on certain inputs, such as ignoring multiples of three in a sequence, and 'pass' can act as a placeholder in a yet-to-be-coded section, ensuring that syntax remains correct during development .

Searching for an element using loops in a list can lead to inefficiencies if the list is long or the element appears multiple times, potentially missing an element occurrence or unnecessarily iterating through the entire list. The 'for-else' construct can address these limitations by incorporating an 'else' block that triggers only if the loop concludes without interruption (i.e., without finding the element), offering clarity on whether the element was present without checking separately after the loop execution .

Nested loops are effective in creating pattern designs because they systematically manage rows and columns in grids or other complex structures. In designs like triangles or squares, the outer loop typically controls the number of rows, while the inner loop manages the columns. This organized structure simplifies pattern generation, enhances readability by separating control for rows and columns, and reduces chances for error by breaking down an intricate problem into smaller, manageable parts .

The 'for-else' construct in Python allows executing a block of code when the loop is not terminated by a break statement. It is particularly useful in situations where you want to determine if a loop concluded normally. When checking for a prime number, the 'for' loop iterates over a range of potential divisors, and if the number is divisible by any of them, it breaks. If the loop completes without finding any divisors, the 'else' block runs, confirming the number's primality by indicating that it isn’t divisible by any of the numbers in the loop, hence it is prime .

Understanding 'nested if' in determining the largest of three numbers is crucial because it allows for multiple conditional checks in a logical sequence. The outer 'if' initially evaluates a primary condition, disambiguating subsequent actions within nested checks, each considering different number comparisons. This structured conditional design ensures comprehensive evaluation, reducing errors and implementing efficient logic paths for comparison, resulting in accurate outcomes in varied scenarios .

You might also like