Exercise Conditional Operator
Exercise Conditional Operator
Tags ---
(2)selection
(3)iteration
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 .
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? .
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 = the else clause of a loop does not get executed when loop is not terminating .
17. What are jump statements? Name them.
(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” ?
(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)
# 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
Answer =
if a < 0 :
print("invaild")
else :
Answer =
if item < 10 :
else :
Answer =
When this program -----
else :
Answer =
Answer =
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 :
else :
Answer =
for i in range((n*2-1),0,-2):
print(i,end=" , ")
(i) 1 4 7 10 ………. 40
(ii) 1 -4 7 -10 ………….. -40.
Answer =
(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=" , ")
Answer =
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
Answer =
Answer =
Answer =
Answer =
Answer =
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 ")
Answer =
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 ;
Answer =
Answer =
(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 )
Answer =
When run this program ---
(a) 26-35
(b) 36-45
(c) 46-55
Answer =
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)
Answer =
(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)
a.
Answer =
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()
(i) A
AB
ABC
ABCD
ABCDE
ABCDEF
Answer =
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) )
ANSWER =
When run this program ----
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()
ANSWER =
c = (5/9)*(temp - 32)
f = (9/5)*temp + 32
print("temperatrue in fahrenheit = ",f,"F")
else :