Python
Condition Execution
Fill in the blanks
1.for i in range(2,10,2):
print(i)
what is the output of above condition 2
4 6 8
2.while entry controlled loop
3.do while is exit controlled
loop
4.for val in “hello”:
If val==”l”:
break
print(val)
what is the output of above condition h e
5. for
val in “hello”:
If val==”l”:
continue
print(val)
what is the output of above condition h e o
Answer the following Question
(4
marks)
1.Write a Program to find a number is divisible by 4 or not
num=int(input("enter a number
.."))
if num %4==0:
print(num,
"is divisible by 4.")
else:
print(num, "is
not divisible by 4")
2.Find the greatest of three number using Nested if
a=20
b=30
c=15
if a>b:
if a>c:
res=a
else:
res=c
elif b>c:
res=b
else:
res=c
print(“Greatest value=”,res)
3.Write a Program to check the given number is Positive or
Negative or Zero
num=float(input("enter a number:
"))
if num >=0:
if num ==0:
print("zero")
else:
print("positive
number")
else:
print("negative number")
4.Write a program to print days of week in numbers(1 .. 7)
using if..elif..else
dofweek=int(input(“Enter a
day of week between 1…7?”))
if dofweek>=1 and dofweek<=7
if dofweek== 1 : print(“Sunday”)
if dofweek== 2 : print(“Monday”)
if dofweek== 3 : print(“Tuesday”)
if dofweek== 4 : print(“Wednesday”)
if dofweek== 5 : print(“Thursday”)
if dofweek== 6 : print(“Friday”)
if dofweek== 7 : print(“Saturday”)
5.Write a program to print 1 to 100 odd number using for loop
Answer:
for val in range(1,10,2):
print(val)
Output:
>>>
1
3
5
7
9
6.Write a program to print for loop with else
digits=[1,2,3,4,5,6,7,8,9,10]
for i digits:
print(i)
else:
print("no items left.")
7.write a program to print sum of the digit using while loop
n=int(input("enter n:"))
sum=0
i=1
while i<=n:
sum= sum +i
i = i+1
print("the sum
is",sum)
8.Write a program for break,continue and pass
Break
for val in "string":
if val ==
"i":
break
print(val)
print("The end")
Continue
for val in "string":
if val ==
"i":
continue
print(val)
print("the end")
Pass
sequence ={'p','a','s','s'}
for val in sequence:
pass
No comments:
Post a Comment