1. Write Pythonic code to check if a 2. Input five integers (+ve and −ve).
given year is a leap year or not. Write Pythonic code to find the sum
Sol: of negative numbers, positive
numbers and print them. Also, find
year=int(input("Enter year to be checked:")) the average of all the numbers and
if(year%4==0 and year%100!=0 or year numbers above average.
%400==0): Sol:
print("The year is a leap year!)
else: n=int(input("Enter the number of
print("The year isn't a leap year!) elements to be in the list:"))
b=[]
for i in range(0,n):
a=int(input("Element: "))
b.append(a)
sum1=0
sum2=0
sum3=0
for j in b:
if(j>0):
if(j%2==0):
sum1=sum1+j
else:
sum2=sum2+j
else:
sum3=sum3+j
print("Sum of all positive even
numbers:",sum1)
print("Sum of all positive odd
numbers:",sum2)
print("Sum of all negative
numbers:",sum3)
3. Write Pythonic code to find Mean,
Variance and Standard Deviation for 3.O/P please :
a list of numbers.
Sol: # initializing list r = lambda q: q * 2
test_list = [4, 5, 8, 9, 10] s = lambda q: q * 3
x=2
# printing list
print("The original list : " + x = r(x)
str(test_list)) x = s(x)
x = r(x)
# Standard deviation of list print x
# Using sum() + list comprehension
mean = sum(test_list) /
len(test_list) i)42
variance = sum([((x - mean) ** 2) ii)24
for x in test_list]) / iii)12
len(test_list)
res = variance ** 0.5 iv)Code will not run
# Printing result
print("Standard deviation of sample
is : " + str(res))
4.Output Please : 5. Output Please:
a = 4.5 a = True
b=2 b = False
print a//b c = False
i)3.0 if a or b and c:
ii)1.0 print "SCOE"
iii)2.0 else:
iv)4.0 print "scoe"
i) SCOE
ii)scoe
iii)true
iv)false
6.Output Please: 7.Output Please:
a = True numberGames = {}
b = False numberGames[(1,2,4)] = 8
c = False numberGames[(4,2,1)] = 10
numberGames[(1,2)] = 12
if not a or b:
print 1 sum = 0
elif not a or not b and c: for k in numberGames:
print 2 sum += numberGames[k]
elif not a or b or not b and a:
print 3 print len(numberGames) + sum
else:
print 4 i)33
i)4 ii)73
ii)3 iii)43
iii)2 iv)23
iv)1
8.Output please 9.Output Please:
my_tuple = (1, 2, 3, 4) def __init__(self, id):
my_tuple.append( (5, 6, 7) ) self.id = id
print len(my_tuple)
manager = Geeks(100)
i)7
ii)4 manager.__dict__['life'] = 49
iii)Error!
iv)3 print manager.life + len(manager.__dict__)
i)51
ii)49
iii)50
iv)error!
10.Output Please: 11.Output Please:
dictionary = {} from random import randrange
dictionary[1] = 1 L = list()
dictionary['1'] = 2 for x in range(5):
dictionary[1] += 1 L.append(randrange(0, 100, 2)-10)
sum = 0 # Choose which of outputs below are valid
for k in dictionary: for this code.
sum += dictionary[k] print(L)
a) [-8, 88, 8, 58, 0]
print sum b) [-8, 81, 18, 46, 0]
c) [-7, 88, 8, 58, 0]
i)1 d) [-8, 88, 94, 58, 0]
ii)2
iii)4
iv)’1’
12.Output Please 13.Output Please
x = ['ab', 'cd'] i=1
for i in x: while True:
i.upper() if i%3 == 0:
print(x) break
print(i)
i)[‘AB’,’CD’] i + = 1
ii)[ab,cd]
iii)[‘ab’,’cd’] i)1
iv)error ii)2
iii)invalid syntax
iv)0
14.Output Please: 15. class Test {
public
for i in [1, 2, 3, 4][::-1]: static void main(String[] args)
print (i) {
int i = 0, j = 9;
i)4321 do {
ii)4 i++;
3 if (j-- < i++) {
2 break;
1 }
iii)1234 } while (i < 5);
iv)1 System.out.println(i + "" + j);
2 }
3 }
4
i) 44
ii)55
iii)66
iv)77
16. class Test { 17.
public
static void main(String[] args)
{
int x = 10;
if (++x < 10 && (x / 0 > 10)) {
System.out.println("Bishal");
} else {
System.out.println("GEEKS");
}
}
}
i.Compile time error
ii.RuntimeException:ArithmeticException: /
by zero
iii. Bishal
iv. GEEKS