0% found this document useful (0 votes)
107 views1 page

Python Programming Assignment 2

The document contains an assignment on Python programming with 10 questions. [Q1] asks to explain conditional statements with code examples. [Q2] asks to draw a flowchart and write a program to find the largest of three numbers. [Q3] asks what the output of a sample while loop code is. [Q4] asks to explain for and while loops with diagrams and examples. The remaining questions [Q5-Q10] involve writing Python programs using various concepts like continue, break, pass, loops, functions, conditionals, and patterns.

Uploaded by

herereh446
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)
107 views1 page

Python Programming Assignment 2

The document contains an assignment on Python programming with 10 questions. [Q1] asks to explain conditional statements with code examples. [Q2] asks to draw a flowchart and write a program to find the largest of three numbers. [Q3] asks what the output of a sample while loop code is. [Q4] asks to explain for and while loops with diagrams and examples. The remaining questions [Q5-Q10] involve writing Python programs using various concepts like continue, break, pass, loops, functions, conditionals, and patterns.

Uploaded by

herereh446
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

GALGOTIAS COLLEGE OF ENGINEERING AND TECHNOLOGY

1, Knowledge Park-II, Greater Noida, U.P.


Odd Semester 2023-24

Python Programming BCC-302


ASSIGNMENT-2

CO-2: Express proficiency in the handling of strings and functions

Q1. Explain different conditional statements in Python using code examples?

Q2. Draw a flow chart to find largest of three numbers input. Write a program a to find
largest of the three numbers input according to the flowchart?

Q3. What is the output of following code fragment?


i=0
while i< 3:
print(i)
i += 1
else:
print(0)

Q4. Explain the following loops with a flow diagram, syntax, and suitable examples.
I) For II) while.

Q5. Explain continue, break, and pass statements with suitable examples.

Q6. Write a program to determine if the year input by user is a leap year or not.

Q7. What is short circuiting in Python? Give examples.

Q8. Write a program to calculate the reverse of any entered number.

Q9. Program to check whether a number input is divisible by 2 or 3 using nested if.

Q10. Write a Python program to construct the following pattern, using a nested for loop.
*
**
***
****
*****
****
***
**
*

Common questions

Powered by AI

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 .

You might also like