Python
BASIC EXAMPLE PROBLEMS
1)AIM: Implement the python program script for checking the given year is leap year or not
PROGRAM:
y=int(input(" Entre the year :"))
if y%100==0:
if y%400==0:
print(y, "is a leap year ")
else:
print(y, "is not a leap year ")
else:
if y%4==0:
print(y," is a leap year ")
else:
print(y, "is not a leap year ")
2)AIM: Implement the python script finding biggest number among three numbers
PROGRAM:
a=int(input("enter the first number :"))
b=int(input("enter the second number:"))
c=int(input("enter the third number :"))
if a>b and a>c :
print(" biggest =",a)
else :
if b>c :
print("biggest =",b)
else :
print("biggest =",c)
3)
AIM: Implement python script for displaying the reversal of a given number
PROGRAM:
n=int(input("enter the number :"))
print("reverse of the number =",end=" ")
while n!=0:
r=n%10
print(r, end=" ")
n=n//10
4)AIM: Implement the python script to check given number is Armstrong number or not
PROGRAM:
n=int(input("enter the number :"))
m=n
s=0
while m!=0:
r=m%10
s=s+r**3
m=m//10
if s==n:
print(n," is a armstrong number ")
else:
print(n, "is not a armstrong number ")
5)AIM: Implement python script to print sum of N natural numbers
PROGRAM:
n=int(input("enter the number :"))
s=0
for i in range(1,n+1):
s= s+ i
print("sum of first", n, "natural numbers=",s)
6)AIM: Implement the python script to check whether the given number is palindrome or not
PROGRAM:
n=int(input("enter the number"))
rev=0
m=n
while n!=0:
r=n%10
rev=rev*10+r
n=n//10
if rev==m:
print(m, "is a palindrome")
else:
print(m, "is not a palindrome")
7)AIM: Implement python script print factorial of a number
PROGRAM:
n=int(input("enter the number :"))
f=1
for i in range(1,n+1):
f=f*i
print("factorial of ",n ,"is=",f). 8)AIM : Implement python script to print all the prime numbers with in the given range
PROGRAM:
n1,n2=input("Enter the range:").split()
n1=int(n1)
n2=int(n2)
print("prime numbers in the range",n1,"to",n2,"are")
for n in range(n1,n2+1):
flag=1
for i in range(2,n//2+1):
r=n%i
if(r==0):
flag=0
break
if flag==1:
print(n, end=" ")
Module-1
1)MODULE-1 PROGRAM 1.A
AIM: Implement python script to display elements of list in reverse order.
PROGRAM:
l=list(input("Enter the list values : ").split())
l.reverse()
print(" Elements of the list in reverse order are: ",end=' ')
for i in l:
print(i,end=' ')
2)MODULE-1 PROGRAM 1.B
AIM: Implement python script to find minimum and maximum elements without using built-in operations in the lists.
PROGRAM:
l=list(map(int,input("Enter list values : ").split()))
minimum=maximum=l[0]
for i in l:
if i>maximum:
maximum=i
if i<minimum:
minimum=i
print("Minimum Element in the list = ",minimum)
print("Maximum Element in the list = ",maximum)
MODULE-2 PROGRAM 2.A
AIM: Implement python script to create a tuple with different data types.
PROGRAM:
n=input(" Enter Your Name : ")
m=int(input("Enter marks in Python Subject : "))
res=bool(input(" Passed in the subject ( Y/N) : "))
a=float(input(" Enter your Average Marks : "))
t=(n,m,res,a)
print("The tuple With Different types is : ",t)
MODULE-3 PROGRAM 3.A
AIM: Implement python script to add members in a set
PROGRAM:
S=set()
n=int(input("Enter the number of elments in a set :"))
for i in range(n):
num=input("enter the element : ")
S.add(num)
print(" The set of elements are :",S)
MODULE-3 PROGRAM 3.B
AIM: Implement python script to perform union, Intersection, Difference of given two sets
PROGRAM:
s1=set(input("Enter the elements of the first set :").split())
s2=set(input("Enter the elements of the second set :").split())
print("elements of the first set :",s1)
print("elements of the second set :",s2)
print("Union of (S1,S2) is :",s1|s2)
print("Intersection of s1&s2 is :",s1.intersection(s2))
print("Difference of s1-s2 is :",s1-s2)
print("Symmetric Difference of (s1,s2) is :",s1^s2)
MODULE -4 PROGRAM 4.A
PROGRAM: Implement python script to sort (ascending and descending) a dictionary by values.
n=int(input("Enter No Of Students : "))
markdict={}
for i in range(n):
sname,marks=input("Enter name and marks of %d student : "%(i+1)).split()
markdict[sname]=int(marks)
marklist=list(markdict.items())
marklist1 = sorted(markdict.items(), key=lambda x:x[1])
l=len(marklist)
for i in range(l-1):
for j in range(l-i-1):
if marklist[j][1]<marklist[j+1][1]:
t=marklist[j]
marklist[j]=marklist[j+1]
marklist[j+1]=t
sortdict=dict(marklist)
print(" Data in sorted ( Descending ) order : ",sortdict)
sortdict=dict(marklist1)
print(" Data in sorted ( Ascending ) order : ",sortdict)
MODULE-5 PROGRAM 5.A
AIM: Define a function, which generates Fibonacci series up to n numbers.
PROGRAM:
def fib(n):
if n==0 or n==1:
return n
return fib(n-1)+fib(n-2)
n=int(input('enter the No.Of terms : '))
print(' Fibonacci series upto %d terms'%n)
for i in range(n):
f=fib(i)
print(f,end=' ')
AIM: Implement a python script for factorial of number by using recursion.
PROGRAM:
def fact(n):
if n==0:
return 1
return n*fact(n-1)
n=int(input(" Enter the number : "))
f=fact(n)
print(" Factorial of %d = %d"%(n,f)). .AIM: Implement a python script for factorial of number by using recursion.
PROGRAM:
def fact(n):
if n==0:
return 1
return n*fact(n-1)
n=int(input(" Enter the number : "))
f=fact(n)
print(" Factorial of %d = %d"%(n,f)). .MODULE-5 PROGRAM 5.C
AIM: Implement a python script to find GCD of given two numbers using recursion.
PROGRAM:
def GCD(x,y):
if x==y:
return x
elif x>y:
return GCD(x-y,y)
else:
return GCD(x,y-x)
a,b=map(int,input(" Enter any two numbers : ").split())
g=GCD(a,b)
print("GCD of %d ,%d is %d"%(a,b,g)) .MODULE-6 PROGRAM 6.B
AIM: Implementation of python script to accept line of text and find the number of characters, number of vowels and number of blank spaces in it.
PROGRAM:
s=input("Please Enter line of Text : ")
cvol=0
cspace=0
cc=0
for ch in s:
cc=cc+1
if (ch=='a' or ch=='e' or ch=='i' or ch=='o' or ch=='u' or ch=='A' or ch=='E' or ch=='I' or ch=='O' or ch=='U'):
cvol = cvol + 1
elif ch==' ':
cspace = cspace + 1
print("Number of Vowels in the Text = ",cvol)
print("Number of spaces = ",cspace)
print("Number of characters = ",cc). .MODULE-7 PROGRAM 7.A
Aim: Write a Python script to check that a string contains only a certain set of characters (in this case a-z, A-Z and 0-9).
Program:
Import re
def is_alnum(s):
m=re.fullmatch("[a-zA-Z0-9]*",s)
if m!= None:
print("String is formed with specified pattern")
else:
print("String is not formed with specified pattern ")
s=input("Enter the String : ")
is_alnum(s) .MODULE-10 PROGRAM 8.A
AIM : Write a Python programming to create a pie chart with a title.
PROGRAM 21 :
import matplotlib.pyplot as plt
# Plot data
languages = 'Java', 'Python', 'PHP', 'JavaScript', 'C#', 'C++'
popuratity = [22.2, 17.6, 8.8, 8, 7.7, 6.7]
#colors = ['red', 'gold', 'yellowgreen', 'blue', 'lightcoral', 'lightskyblue']
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b"]
# explode 1st slice
explode = (0, 0.1, 0, 0, 0, 0)
# Plot
plt.pie(popuratity, explode=explode, labels=languages, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
plt.title("PopularitY of Programming Language\n" + "Worldwide, Oct 2017 compared to a year ago", bbox={'facecolor':'0.8', 'pad':5})
plt.show()
Thank you for sharing very useful information.
ReplyDeletePython Online Course
Python Online Training in India