Python :
Assignment 1:
1. Write a program to check whether number is prime or not
Solution:
n=int(input("Enter No")) #taking input no.
cnt=0
for i in range(2,n):
if(n%i==0):
print("No is not prime")
break;
else:
cnt=cnt+1
if(cnt==i-1):
print("No is prime")
2. Write a program to display Fibonacci series
Solution 1:
a=0
b=1
print(a)
print(b)
for i in range(0,6):
n=a+b
a=b
b=n
print(n)
3. Write a program to display table in the following format:
2 3 .... 10
4 6 .... 20
6 9 ....30
8 12 ....40
. . . .
20 .... 100
Solution :
for a in range(1,11):
print()
for n in range(2,11):
b=a*n
print(b,end=" ")
4. Write a program to swap two nos. without using third variable
Solution:
a=int(input("Enter a"))
b=int(input("Enter b"))
a,b=b,a
5. Write a program to display even numbers from given range(1,21)
Solution 1:
l=[ ]
for i in range(1,21):
if i%2==0:
l.append(i)
print(l)
Solution 2: using list comprehension
l=[i for i in range(1,21) if i%2==0]
print(list(l))
6. Write a program to display even numbers from range (0,10) using lambda function
Solution :
l=lambda seq:[i for i in seq if i%2==0]
print(l(list(range(0,10))))
7. Write a program to check a given number is prime or not using lambda function
Solution :
l=lambda n:[1 for i in range(2,n) if n%i==0]
a=l(int(input("Enter no")))
if len(a)==0:print("no is prime")
else:print("no is not prime")
0 Comments