Python Programming Assignment 2
Python Programming Assignment 2
Nested if statements involve placing an if within another if. To check divisibility by both 2 and 3: num = int(input('Enter number: ')); if num % 2 == 0: if num % 3 == 0: print('Divisible by both 2 and 3') else: print('Divisible by 2 only') else: print('Not divisible by 2').
Python provides several conditional statements including 'if', 'if-else', and 'if-elif-else'. The 'if' statement executes code if a condition is true. The 'if-else' adds an alternative path if the condition is false. The 'if-elif-else' allows checking for multiple conditions. Code example: if x > y: print('x is greater than y') elif x == y: print('x and y are equal') else: print('y is greater than x').
To reverse a number, convert it to a string, reverse the string, and convert back to a number. Code: num = input('Enter number: '); reversed_num = int(str(num)[::-1]); print('Reversed:', reversed_num).
Nested loops help create grid-like patterns. Example to construct the star pattern provided: for i in range(5): for j in range(i+1): print('*', end=' '); print() creates an upright triangle of stars across five rows .
A leap year is divisible by 4, but not by 100 unless also divisible by 400. A program for this check might be: year = int(input('Enter year: ')); if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): print('Leap Year') else: print('Not a Leap Year').
Short circuiting in Python means logical operators 'and' and 'or' stop evaluation as soon as the result is determined. For 'and', if any operand is false, the result is false and remaining operands are not evaluated. For 'or', if any operand is true, the result is true. Example: 'a = False; b = True; print(a and b)' only evaluates 'a' and stops, resulting in 'False'. 'print(a or b)' evaluates both and outputs 'True' .
A flowchart helps visualize decision-making paths for determining the largest number. Start by comparing the first two numbers; then compare the larger of those with the third number. Implementing this in Python involves nested if-else statements: if a >= b and a >= c: largest = a elif b >= a and b >= c: largest = b else: largest = c .
'For' loops iterate over a sequence, such as a list or a string. Syntax: for element in sequence: code block. 'While' loops run as long as a condition is true. Syntax: while condition: code block. For example, 'for i in range(3): print(i)' iterates and prints 0, 1, and 2. 'i = 0; while i < 3: print(i); i += 1' does the same .
'Continue' skips the current iteration and moves to the next. 'Break' exits the loop. 'Pass' does nothing and acts as a placeholder. Example: for i in range(5): if i == 3: continue; print(i) prints 0, 1, 2, 4. If 'break' replaced 'continue', it would stop at 2. 'Pass' could replace this conditional for structural compatibility without action .
The code fragment outputs: 0 1 2 0. The 'while' loop iterates while i < 3, printing i and incrementing it each time. After the loop condition fails, the 'else' clause executes, printing 0 .