0% found this document useful (0 votes)
13 views4 pages

ch-9, Flow of Control C.W

The document covers Python control flow concepts, including compound statements, loops, and conditional execution. It explains the structure of Python statements, the importance of sequence, selection, and repetition, as well as various programming constructs like empty statements and jump statements. Additionally, it provides practical programming exercises demonstrating these concepts.

Uploaded by

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

ch-9, Flow of Control C.W

The document covers Python control flow concepts, including compound statements, loops, and conditional execution. It explains the structure of Python statements, the importance of sequence, selection, and repetition, as well as various programming constructs like empty statements and jump statements. Additionally, it provides practical programming exercises demonstrating these concepts.

Uploaded by

varsha.ashwin321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CLASS:XI CH-9 FLOW OF CONTROL DATE:06/12/2024

TYPE-A

1. What is the common structure of Python compound statements?


The common structure of a Python compound statement is as shown below:
<compound statement header>:
<indented body with multiple simple\
and/or compound statements>
It has the following components:
 A header line which begins with a keyword and ends with a colon.
 A body containing a sequence of statements at the same level of indentation.

2. What is the importance of the three programming constructs?


The importance of the three programming constructs is a given below:
1. Sequence — Statements get executed sequentially.
2. Selection — Execution of statements depends on a condition test.
3. Repetition\Iteration — Repetition of a set of statements depends on a condition test.

3.What is empty statement in Python? What is its need?

In Python, an empty statement is pass statement. Its syntax is: pass. When pass statement is
encountered, Python does nothing and moves to next statement in the flow of control.

4. What is entry-controlled loop?


An entry-controlled loop checks the condition at the time of entry. Only if the condition is
true, the program control enters the body of the loop.

5. What are the four elements of a while loop in Python?


The four elements of a while loop in Python are:
1. Initialization Expressions — It initializes the loop control variable and it is given outside the
while loop before the beginning of the loop.
2. Test Expression — If its truth value is true then the loop-body gets executed otherwise not.
3. The Body of the Loop — It is the set of statements that are executed repeatedly in loop.
4. Update Expressions — It updates the value of loop control variable and it is given inside the
while loop.

5. What is the difference between else clause of if-else and else clause of Python loops?
The else clause of an if-else statement is executed when the condition of the if statement
results into false. The else clause of a loop is executed when the loop is terminating normally
i.e., when its test condition has become false for a while loop or when the for loop has
executed for the last value in sequence.

6. In which cases, the else clause of a loop does not get executed?
The else clause of a loop does not get executed if the loop is terminated due to the
execution of a break statement inside the loop.
7. What are jump statements? Name them.
Jump statements are used to unconditionally transfer program control to other parts
within a program. Python provides the below jump statements:
1. break
2. continue

8. What are endless loops? Why do such loops occur?


A loop which continues iterating indefinitely and never stops is termed as an endless or
infinite loop. Such loops can occur primarily due to,
Logical errors when the programmer misses updating the value of loop control variable.

9. How is break statement different from continue?


When the break statement gets executed, it terminates its loop completely and control
reaches to the statement immediately following the loop. The continue statement
terminates only the current iteration of the loop by skipping rest of the statements in the
body of the loop.
TYPE:C

1.Write a python script that asks the user to enter a length in centimeters. If the user enters
a negative length, the program should tell the user that the entry is invalid. Otherwise, the
program should convert length to inches and print out the result. There are 2.54 cms in an
inch.
Ans: len=int(input(“Enter length in cm:”))
if len<0:
print(“Invalid Input”)
else:
print(len, ”cm=”, len/2.54, “inches”)

2.A store charges Rs. 120 per item if you buy less than 10 items. If you buy between 10 and
99 items, the cost is Rs. 100 per item. If you buy 100 more items the cost is Rs. 70 per item.
Write a program that asks the user how many items they are buying and prints the total.
Ans:
n = int(input("Enter number of items: "))
cost = 0
if n >= 100 :
cost = n * 70
elif n >= 10 :
cost = n * 100
else :
cost = n * 120
print("Total Cost =", cost)

3. Write a program that reads from user — (i) an hour between 1 to 12 and (ii) number of hours
ahead. The program should then print the time after those many hours, e.g.,
Enter hour between 1-12 : 9
How many hours ahead : 4
Time at that time would be : 1 O'clock

Ans:

hour = int(input("Enter hour between 1-12 : "))


n = int(input("How many hours ahead : "))
time = hour + n
if time> 12:
time -= 12
print("Time at that time would be : ", time, "O'clock")

4. Write a program to input length of three sides of a triangle. Then check if these sides will
form a triangle or not.
(Rule is: a+b>c;b+c>a;c+a>b)
Ans:
a = int(input("Enter first side : "))
b = int(input("Enter second side : "))
c = int(input("Enter third side : "))
if a + b > c and b + c > a and a + c > b :
print("Triangle Possible")
else :
print("Triangle Not Possible")

5. Write a short program to print first n odd numbers in descending order.


Ans:
n = int(input("Enter n: "))
x=n*2-1
for i in range(x, 0, -2) :
print(i)
6. Write a short program to print the following series :
(i) 1 4 7 10 .......... 40.
(ii) 1 -4 7 -10 .......... -40
Ans:
i) for i in range(1,41,3):
print(i,end=" , ")

ii) for i in range(1,41,3):


if i % 2 == 0 :
print(-i,end=" , ")
else :
print(i,end=" , ")

7. Write a program to input 3 sides of a triangle and print whether it is an equilateral, scalene
or isosceles triangle.

Ans:
a = int(input("Enter first side : "))
b = int(input("Enter second side : "))
c = int(input("Enter third side : "))
if a == b and b == c :
print("Equilateral Triangle")
elif a == b or b == c or c == a:
print("Isosceles Triangle")
else :
print("Scalene Triangle")
8. Write a program to take N (N > 20) as an input from the user. Print numbers from 11 to N.
When the number is a multiple of 3, print "Tipsy", when it is a multiple of 7, print "Topsy".
When it is a multiple of both, print "TipsyTopsy".
Ans:
n = int(input("Enter a number greater than 20: "))
if n <= 20 :
print("Invalid Input")
else :
for i in range(11, n + 1) :
print(i)
if i % 3 == 0 and i % 7 == 0 :
print("TipsyTopsy")
elif i % 3 == 0 :
print("Tipsy")
elif i % 7 == 0 :
print("Topsy")
9. Write Python programs to sum the given sequences:

12 + 32 + 52 + ..... + n2 (Input n)

Ans:

n = int(input("Enter the value of n: "))


i=1
sum = 0
while i <= n :
sum += i ** 2
i += 2
print("Sum =", sum)

10. Write a Python program to sum the sequence:


1 + 1/1! + 1/2! + 1/3! + ..... + 1/n! (Input n)

Ans:
n = int(input("Enter the value of n: "))
sum = 0
for i in range(n + 1) :
fact = 1
for j in range(1, i) :
fact *= j
term = 1 / fact
sum += term
print(“Sum=”, sum)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy