0% found this document useful (0 votes)
9 views

Exercise Conditional Operator

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)
9 views

Exercise Conditional Operator

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/ 50

TYPE-A

1. What is the common structure of


Python compound statements?

Answer = common structure of python statement is :-

<compound statement header> :

<indented body containing simple / compound statement >

You can uderstand by watching videos -----

Tags ---

common structure of python compound statements


what is the common structure of python compound statements
python compound statements

2 = What is the importance of the three


programming constructs?
Answer = the three programming constructs are as follow :-
(1)sequence

(2)selection

(3)iteration

You can uderstand by watching videos -----

Tags ----
what is the importance of three programming constructs in python
what is the importance of the three programming constructs
what is the importance of three programming constructs
3 =What is empty statement? What is its
need ?
Answer = A statement which does nothing is known as empty
statement .

In python empty statement is pass statement .

A pass statement is useful in those place where the syntax of


language requires the presence of a statement but where the
logic of the program does not.

Example = in looping
4 .Which Python statement can be termed
as empty statement ?
Answer = pass statement can be termed as empty statement .

5 . What is an algorithm?
Answer = An algorithm is well defined instructions to solve a
given problem .
6. What is a flowchart ? How is it useful? .

Answer = a flowchart is a graphical representation of algorithm


to solve a given problem .

It is useful because a flowchart show different subtasks with


different symbols .
7 . What is pseudocode ? How is it useful in
developing logic for the solution of a
problem?
Answer = Pseudocode :- it is an informal language that helps programmers describe
steps of a programs solution without using any programming language syntax .

It gives the idea of how to algorithm work and how the control flow from one step to
another .
8. What is decision tree ? Where are these
useful?

Answer = Decision tree are a way of presenting values in


hierarchical and sequential structure .

It is useful in learning algorithm of modern programming


language .

13. What is entry-controlled loop?

Answer = Entry controlled loop , the loop which has condition


checked at the entrance of the loop , the loop executes only and
only if the condition is satisfied is called as entry control loop.

Example = while loop, for loop.


OR

Loop where test condition is checked before entering the loop


body , know as entry controlled loop.
14. What are the four elements of a while
loop in Python?

Answer = the four element of a while loop in python are as


follow :
(i) initialization expressions (starting )

(ii) test expression (reporting or stopping)

(iii) the body of the loop (doing)

(iv) update expression (changing )


15. What is the difference between else
clause of if-else and else clause of Python
loops?
Answer = the else clause of an if statement is executed when the
condition of the if statement result into false . The else clause of loop is
executed when the loop is terminating normally , when its test –condition has gone false
for a while loop or when the for loop has executed for the last value in sequences .
16. In which cases, the else clause of a loop
does not get executed?

Answer = the else clause of a loop does not get executed when loop is not terminating .
17. What are jump statements? Name them.

Answer = statement that unconditionally transfers program


control within a function .

Type of jump statement :

(i)break

(ii)continue

(iii)return
18. How and when are named conditions
useful?

Answer = named condition are used to store complex condition , it can be later use in
program
19. What are endless loops? Why do such
loops occur?
Answer = endless loop means that loop can never stop / terminate.
They can occur in the absence of a terminating condition ..

TYPE - B
1. Rewrite the following code
fragment that saves on the
number of comparisons:
2. Under what conditions will
this code fragment print
“water” ?

3. What is the output produced


by the following code ?
4. What is the error in following
code ? Correct the code :

5. What is the output of the


following lines of code ?

6. Find the errors in the code


given below and correct the
code :
Errors are mentioned using ‘#’ in the code
7. What is following code doing
? What would it print for input
as 3 ?
8. How are following two code
fragments different from one
another ? Also, predict the
output of the following code
fragments :
9. Rewrite the following code
fragments using for loop :

(b)
10. Rewrite following code
fragments using while loops :
11. Predict the output of the
following code fragments :

# Output (a)
# Output (b)

# Output (c)
# Output (d)

45 infinite times because no increment of conditional variable is being done

# Output (e)

# Output (f)

# Output (g)
# Output (h)

# Output (i)

# Output (j)

No output because 3rd argument is positive and 2nd argument is less than 1st argument
# Output (k)

# Output (l)

# Output (m)
# Output (n)

50

12. What is the output of the


following code ?
13. In the nested for loop code
above (Q. 12), how many times
is the condition of the if clause
evaluated?
20 times. 4 times for loop 1 X 5 times for loop 2
14. Which of the following
Python programs implement
the control flow graph shown ?
Ans : (b)
TYPE-C
Q 1= Write a python script that ask 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 the length to inches and print out
the result . there are 2.54 centimeters in an
inch.

Answer =

When run this program ---

conditional and iterative statements class 11 q 1


a = int(input("enter the length in centimeter = "))

if a < 0 :

print("invaild")

else :

print("length in inch = ",a/2.54)

Q 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 RS100 per item . if you buy more
items , the cost is RS 70 the user how
many items they are buying and print the
total cost .

Answer =

When run this program –

conditional and iterative statements class 11 q2


ANSWER =

item = int(input("enter the total number of item = "))

if item < 10 :

print("total cost = ",item * 120 , "rs")

elif 10 < item and item < 99 :

print("total cost = ",item * 100, "rs")

else :

print("total cost = ",item * 70 , "rs")

Q 5 = Write a program to input length of


three side of a triangle . then check if
these side will form a triangle or not .

Answer =
When this program -----

conditional and iterative statements class 11 q 5


Please click on photo for zoom

a = int (input ("Enter the frist side of triangle ="))

b = int (input ("Enter the second side of triangle ="))

c= int (input ("Enter the third side of triangle ="))

if a< b +c and b<a+c and c<a+b :

print ("it form the triangle ")

else :

print ("it can not form triangle ")


Q 6 = Write a short program to input a
digit(1 -10) and print it in words .

Answer =

When run this program –

conditional and iterative statements class 11 q 6


Please click on photo for zoom

a = int (input ("enter the digit (1-10) ="))


if a ==1 :
print ("one ")
elif a ==2 :
print ("two")
elif a ==3 :
print ("three")
elif a == 4:
print ("four ")
elif a == 5:
print ("five")
elif a == 6:
print ("six ")
elif a == 7:
print ("seven")
elif a == 8:
print ("eight ")
elif a == 9:
print ("nine")
elif a == 10 :
print ("ten")
else:
print ("digit is out of range ")
Q 7 = Write a short program to check
weather square root of a number is
prime or not .

Answer =

You can understand by seeing video ----

num = int(input("enter a number = "))

import math

a = int(math.sqrt(num))

b = 0

for i in range(2,a):

if a % i == 0 :

b = b +1

if b == 0 :

print(a,"is a prime number ")

else :

print(a,"is not prime number ")


Q 8 = Write a short program to print first
n odd number in descending order .

Answer =

You can understand by seeing video ----

n = int(input("enter n term of odd number = "))

for i in range((n*2-1),0,-2):

print(i,end=" , ")

Q 9 type C || conditional and iterative


statements || Sumita arora || class 11||
computer science || IP ||

Q 9 = Write a short program to print the


following series :

(i) 1 4 7 10 ………. 40
(ii) 1 -4 7 -10 ………….. -40.
Answer =

You can understand by seeing video ----

(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=" , ")

Q 10 = Write a short program to find


average of list of
number entered through keyboard .

Answer =

When run this program –


conditional and iterative statements class 11 q 10
Please click on photo for zoom

sum = 0
feq = 0
while True :
a = input("enter a number (for quit enter Yes )= ")
if a == "Yes" or a == "yes" :
print("average of numbers = ",sum/feq)
break
else :
sum = sum + int(a)
feq = feq + 1

Q11 = Write a program to input 3 sides of


triangle and print whether it is an
equilateral
, scalene or isosceles triangle .

Answer =

When run this program ---


conditional and iterative statements class 11 q 11
Please click on photo for zoom

side1 = int(input("enter 1st side of triangle = "))


side2 = int(input("enter 2nd side of triangle = "))
side3 = int(input("enter 3rd side of triangle = "))
if side1 == side2 == side3 :
print("it is a equilateral triangle ")
elif side1 == side2 != side3 or side1 != side2 == side3 or side1 ==
side3 != side2 :
print("it is a isosceles triangle ")
elif side1 != side2 != side3 :
print("it is a scalene triangle ")

Q 12 = Write a program to take an


integer “a” as an input and
check whether it ends with 4 or 8 .if its
ends with 4 , print “ends with 4
“, if it end with 8 ,print “ends with 8”,
otherwise print “ ends with neither ” .

Answer =

When run this program ---


conditional and iterative statements class 11 q 12
Please click on photo for zoom

a = int(input("enter a number = "))


if a % 10 == 4 :
print(a,"end with 4")
elif a% 10 == 8 :
print(a,"end with 8 ")
else :
print(a,"end with neither ")

Q 13 = Write a program to take N(N>20) as


an input from the user . print number from
11 to N .when the number is multiple of
3 ,print , “ tipsy ” . when it is
multiple of 7 print “topsy ”
.when it is a multiple of both print
“tipsyTopsy”.

Answer =

When run this program ---


conditional and iterative statements class 11 q 13

Please click on photo for zoom

n = int(input("enter a number greater than 20 = "))


for i in range(11,n+1):
if i % 3 == 0 and i % 7==0 :
print(i,"tipsytopsy")
elif i % 3==0 :
print(i,"tipsy")
elif i % 7==0 :
print(i,"topsy")

Q 14 = Write a short program to find


largest number of the list of number
entered through keyboard .

Answer =

When run it in python ----

conditional and iterative statements class 11 q 14


Please click on photo for zoom
b = 0
while True :
a = int(input("enter a number (for quit enter 0 (zero) )= "))
if a == 0 or a== 0 :
break
else :
if a > b :
b = a
print(b,"is biggest number ")

Q 15 = Write a program to input N number


and then print the second largest number .

Answer =

When run this program ---

conditional and iterative statements class 11 q 15


Please click on photo for zoom

largest = 0
second = 0
while True :
a = int(input("enter a number (for quit enter 0 (zero) )= "))
if a == 0 or a== 0 :
break
else :
if a > largest :
largest = a
elif a > second :
second = a
print(second,"is second biggest number ")

Q 16 = Given list of integer, write a


program to find those which are
palindromes.

Answer =

When run this program in python ---

conditional and iterative statements class 11 q 16


Please click on photo for zoom

while True :
a = input("enter a number (for quit enter q = ")
if a == "q" or a== "Q" :
break
else :
reverse = a[ -1 : - len(a) - 1 : - 1]

if a == reverse :
print(a,"is polindromes of ",reverse)
else :
print(a,"is not polindromes of ",reverse)

Q 17 =
Write a complete python program to do
the following ;

(a) read an integer X .


(b) determine the number of digit n in X .
(c)from an integer Y that has the number of
digit n at tens place and the most
significant
Digit of X at ones place .
(d)output Y

Answer =

When run this program ----


conditional and iterative statements
class 11 q 17
Please click on photo for zoom

x = input("enter a number = ")


n = len(x)
y = str(n) + x[0]
print("new number = ",y )

Q 18 = Write a python program to print


every integer between 1 and n divisible by
m . also report whether the number that is
divisible by m is even or odd .

Answer =

When run this program ---


conditional and iterative statements class 11 q 18
Please click on photo for zoom

You can understand by seeing video ----

n = int(input("enter the n number = "))


m = int(input("enter m as divisor = "))
for i in range(1,n):
if i % m == 0 :
if i % 2 == 0 :
print(i,"is divisible m and this is even number ")
else :
print(i,"is divisible m and this is odd number ")

Q 19 = Write python program to sum the


given sequences :

(a): 2/9 – 5/13+18/17………(print 7 th term )


(b): 12 + 3 2 + 52+…….+n2 (input n)
Answer =

You can understand by seeing video ----

(a)=
a= 2
b = 9
sum = 2/9
for i in range(6):
a = a + 3
b = b + 4
if i % 2 == 0 :
sum = sum - a/b
else :
sum = sum + a/b
print("sum of sequensumes = ",sum)

(b)=
n = int(input("enter the n term = "))
sum = 0
for i in range(1,n+1):
sum = sum + (i ** 2)
print("sum of sequence = ",sum )

Q 20 = Write a python program to sum the


sequence:

1 + 1/1! + 1/2! + 1/3! + …….. + 1/n!

Answer =
When run this program ---

conditional and iterative statements class 11 q 20


Please click on photo for zoom

n = int(input("enter a nth term = "))


fact = 1
sum = 1
for i in range(1,n+1):
fact = fact * i
sum = sum + 1/fact
print("sum of sequence = ",sum)

Q 21 = Write a program to accept the age


of an employees and count the number of
person in the following age group .

(a) 26-35
(b) 36-45
(c) 46-55

Answer =

When run this program ----

conditional and iterative statements class 11 q 21


Please click on photo for zoom

a = 0
b = 0
c = 0
while True :
emp = int(input("enter the age of employees (enter 0 (zero) for quit )=
"))
if emp == 0 :
break
else :
if 25 < emp and emp < 36 :
a = a + 1
if 35 < emp and emp < 46 :
b = b + 1
if 45 < emp and emp < 56 :
c = c + 1
print("the age of employees 26 - 35 = ",a)
print("the age of employees 36 - 45 = ",b)
print("the age of employees 46 - 55 = ",c)

Q 22 = Write a program to find the sum of


the following series :

(a) x – x2 /2! + x3/3! …….. – x6/6!


(b) x + x2/2 ……… + xn/n (input x and n both)

Answer =

You can understand by seeing video ----

(a)=
x = int(input("enter a term = "))
fact = 1
sum = 0
for i in range(1,7):
fact = fact * i
if i % 2 == 0 :
sum = sum - (x**i)/fact
else :
sum = sum + (x**i)/fact
print("sum of series = ",sum)
(b)=
x = int(input("enter a term = "))
sum = 0
for i in range(1,x+1):
sum = sum + x**i/i
print("sum of series = ",sum)

Q 23 = Write program to print the


following shapes :

a.

Answer =

You can understand by seeing video ----


A =
for i in range(7):
for j in range(7):
if i < 4 :
if j >=3- i and j <= 3+i :
if i % 2 == j % 2 :
print(" ",end="")
else :
print("*",end="")
else :
print(" ",end="")
else :
if j <= 9-i and j >= i - 3 :
if i % 2 == j % 2 :
print(" ",end="")
else :
print("*",end="")
else :
print(" ",end="")
print()

B =

for i in range(1,6):
if i < 4 :
print("*" * i )
else :
print("*" * (6-i))
print()

C =

for i in range(5):
for j in range(5):
if i < 3 :
if j == 2 - i or j == 2 + i :
print("*" , end="")
else :
print(" " , end = "")
else :
if j == i-2 or j == 6 - i :
print("*" , end="")
else :
print(" " , end = "")
print()

D =

for i in range(7):
for j in range(4):
if i == j or j == 6-i :
print("*" , end="")
else :
if j == 0 :
print("*" , end = "")
else :
print(" ",end="")
print()

Q 24 = Write programs using nested loops


to produce the following pattern :

(i) A
AB
ABC
ABCD
ABCDE
ABCDEF

Answer =

You can understand by seeing video ---


( I )

for i in range(6):
for j in range(6):
if i >= j :
print(chr(65+j)+" ",end="")
print()

(II)

for i in range(4):
if i == 0 :
print(0)
else:
print(str(2**i)*(i+1) )

Q 25 = Write a program using nested


loops to produce rectangle of *’s with 6
rows and 20 *’s per row .

ANSWER =
When run this program ----

conditional and iterative statements


class 11 q 25
Please click on photo for zoom

for i in range(6):
for j in range(20):
if i == 0 or i == 5 :
print("*",end=" ")
elif j == 0 or j == 19 :
print("*",end=" ")
else :
print(" ",end=" ")
print()

Q 26 = Given three number A ,B and C .


write a program to write their values in an
ascending order .
for example A= 12,B=10 and C = 15 , your
program should print out :
Smallest number = 10
Next higher number = 12
Highest number = 15

ANSWER =

When run this program ----


conditional and iterative statements class 11 q 26

a = int(input("enter 1st number = "))


b = int(input("enter 2nd number = "))
c = int(input("enter 3rd number = "))
if a > b and b > c :
max,mid,low = a,b,c

elif b > a and a > c :


max,mid,low = b,a,c

elif c > b and b > a :


max,mid,low=c,b,a

elif c > b and c > a and a > b :


max,mid,low=c,a,b

elif b > c and b > a and a<c :


max,mid,low=b,c,a

elif a> b and a > c and b<c :


x,mid,low= a,c,b

print("smallest number ",low)


print("next highest number ",mid)
print("hight number ",max)

Q 27 = Write a python script to input


temperature . then ask them what unit ,
Celsius or Fahrenheit , the temperature is
in . your program should convert the
temperature to other unit . the
conversions are F = 9/5 C + 32
and C= 5/9(F – 32).

Answer = You can understand by seeing this video --

When run this program ---

conditional and iterative statements class 11 q 27


Please click on photo

temp = float(input("enter temperature = "))

unit = input("enter unit of temperature ( c or f ) =")

if unit=="F" or unit == "f" :

c = (5/9)*(temp - 32)

print("temperatrue in celsuis = ",c,"C")

elif unit=="C" or unit == "c" :

f = (9/5)*temp + 32
print("temperatrue in fahrenheit = ",f,"F")

else :

print("invaild unit ")

Q 28 = Ask the user to enter a temperature


in Celsius . the program should print a
message based on the temperature :
(i)if the temperature is less than -273.15
print that the temperature is invalid
because it is below absolute zero :
(ii)if is exactly -273.15 ,print that the
temperature is absolute 0 .
(iii)if the temperature is between -273.15
and 0 ,print that the temperature is below
freezing .
(iv)if it is 0 ,print that the temperature is at
the freezing point .
(v)if it is between 0 and 100 , print that the
temperature is in the normal range .
(vi)if it is 100 ,print that the temperature is
at the boiling point .
(vii) if it above 100 , print that the
temperature is above the boiling point .

Answer = You can understand by seeing this video ---


When run this program ---

conditional and iterative statements class 11 q 28


Please click on photo

temp = float(input("enter temperature in celsius= "))


if temp < -273.15 :
print ("the temperature is invalid ")
elif temp == -273.15 :
print ("the temperature is absolute 0 ")
elif temp > -273.15 and temp <0:
print ("the temperature is below freezing ")
elif temp == 0 :
print ("the temperature is at the freezing point")
elif 0< temp <100 :
print ("the temperature is in the normal range")
elif temp == 100:
print ("the temperature is at the boiling point")
elif temp > 100:
print (" the temperature is above the boiling point ")

You might also like