23BCE9683
23BCE9683
PROGRAM-1:
PROGRAM:
import datetime
current_time=datetime.datetime.now()
print(“year:”,current_time.year)
print(“month:”,current_time.month)
print(“date:”,current_time.day)
print(“hour:”,current_time.hour)
print(“minute:”,current_time.minute)
print(“second:”,current_time.second)
print(“microsecond:”,current_time.microsecond)
OUTPUT:
PROGRAM-2:
import platform
print(platform.python_version())
import sys
print(sys.version)
OUTPUT:
PROGRAM-3:
PROGRAM:
n=input(“enter number:”)
n2=n+n
n3=n+n+n
n1=int(n)
n4=int(n2)
n5=int(n3)
print(n1+n4+n5)
OUPUT:
PROGRAM-4:
AIM:To read and print various types of variables.
PROGRAM:
a=7
k='n'
pi=3.14
str=”praneeth”
print(a)
print(k)
print(pi)
print(str)
OUTPUT:
PROGRAM-5:
PROGRAM:
import calendar
year=2023
print(calendar.calendar(2023))
OUTPUT:
Week-2
Program-1:
Aim:Python Program to find the Square Root
Program:
import math
n=int(input(“enter number:”))
sqrt1=math.sqrt(n)
print(sqrt1)
from math import sqrt
m=int(input(“enter the number:”))
sqrt2=sqrt(m)
print(sqrt2)
output:
Program-2:
Aim:Python program to calculate area &
perimeter of triangle & circle.
Program:
a=int(input(“enter ‘a’ value:”))
b=int(input(“enter ‘b’ value:”))
c=int(input(“enter ‘c’ value:”))
s=a+b+c/2
print(“perimeter of triangle is”,s)
from math import sqrt
area=sqrt(s*(s-a)*(s-b)*(s-c))
print(“area of triangle is”,area)
r=int(input(“enter radius of circle:”))
from math import pi
perimeter=2*pi*r
print(“perimeter of circle is”,perimeter)
a1=pi*r*r
print(“area of circle is”,a1)
output:
Program-3:
Aim:Python program to solve quadratic
equation
Program:
a=int(input(“enter coefficient of x^2:”))
b=int(input(“enter coefficient of x:”))
c=int(input(“enter constant:”))
import cmath
d=cmath.sqrt(b*b-4*a*c)
r1=(-b+d)/(2*a)
r2=(-b-d)/(2*a)
print(“roots are {0} and {1}”.format(r1,r2))
output:
Program-4:
Aim:python program to swap 2 variables
Program:
a=int(input(“enter value of a:”))
b=int(input(“enter value of b:”))
t=a
a=b
b=t
print(“a=”,a)
print(“b=”,b)
output:
Program-5:
Aim:python program to convert kilometers
into miles
Program:
#1 km=0.621371192 miles
N=int(input(“enter number of kilometers”))
Miles=N*(0.621371192)
print(“miles=”,miles)
output:
program-6:
aim:python program to convert Celsius into
fahrenheit
program:
Celsius=int(input(“enter Celsius:”))
Fahrenheit=Celsius*(9/5)+32
Print(“Fahrenheit=”,Fahrenheit)
Output:
WEEK-3
PROGRAM -1:
AIM:python program to find
whether given number is even or
odd
PROGRAM:
n=int(input("enter number:"))
if (n%2==0):
print("{0} is even".format(n))
else:
print("{0} is odd".format(n))
OUTPUT:
PROGRAM-2:
AIM:python program to get
difference between a given
number &17,if number is > 17
return double absolute difference
PROGRAM:
def difference(n):
if n<=17:
return 17-n
else:
return (17-n)*2
print(difference(14))
print(difference(27))
OUTPUT:
PROGRAM-3:
AIM:python program to test
whether a number is within 100 of
1000 or 2000.
PROGRAM:
def near_thousand(n):
return ((abs(1000-n)<=100) or
(abs(2000-n)<=100))
print(near_thousand(900))
print(near_thousand(1000))
print(near_thousand(2200))
OUTPUT:
PROGRAM-4:
AIM:Python program to calculate
sum of 3 given numbers,if values
are equal then return 3 times of
their sum
PROGRAM:
def sum_thrice(x,y,z):
sum=x+y+z
if x==y==z:
sum=sum*3
return sum
a=int(input("enter 1 no"))
b=int(input("enter 2 no"))
c=int(input("enter 3 no"))
print(sum_thrice(a,b,c))
OUTPUT:
PROGRAM-5:
AIM:Python program to find
factorial of a number.
PROGRAM:
def factorial(n):
if n<0:
return 0
elif n==0 or n==1:
return 1
else:
fact=1
while(n>1):
fact=fact*n
n=n-1
return fact
print(factorial(5))
OUTPUT:
PROGRAM-6:
AIM:To find the maximum of given
3 numbers.
PROGRAM:
a=int(input("enter 1st number:"))
b=int(input("enter 2nd number:"))
c=int(input("enter 3rd number:"))
largest=0
if a>b and a>c:
largest=a
elif b>a and b>c:
largest=b
else:
largest=c
print(largest,"is the maximum of 3
numbers")
OUTPUT:
PROGRAM-7:
AIM:Python program to find
whether a given year is leap or
not.
PROGRAM:
Year=int(input(“enter year:”))
if (Year%4==0 and Year%100!=0)
or (Year%400==0):
print(Year,”is a leap year”)
else:
print(Year,”is not a leap year”)
OUTPUT:
Week-4
Program-1:
Aim: Write a program which will find all such numbers
which are divisible by 7 but are not a multiple of 5,
between 2000 and 3200 (both included). The numbers
obtained should be printed in a comma-separated sequence
on a single line. Consider use range(#begin, #end) method
Program:
n1=[]
for x in range(2000,3201):
if x%7==0 and x%5!=0:
n1.append(str(x))
print(','.join(n1))
Output:
Program-2:
Aim: Write a python program to check whether a
number is divisible by 5 and 11 or not.
Program:
n=int(input("enter integer:"))
if n%5==0 and n%11==0:
print("%d is divisible by 5 and 11"%n)
else:
print("%d is not divisible by 5 and 11"%n)
Output:
Program-3:
Aim:
Program:
n=input("enter any key:")
if n>='a' and n<='z':
print(n,"is an alphabet")
elif n>='A' and n<='Z':
print(n,"is an alphabet")
else:
print(n,"is not a character")
Output:
Program-4:
Program-6:
Aim: Write a python program to input week number and print week
day.
Program:
w=int(input("enter weekday number (1-7):"))
if w==1:
print("\nmonday")
elif w==2:
print("\ntuesday")
elif w==3:
print('\nwednesday')
elif w==4:
print('\nthursday')
elif w==5:
print('\nfriday')
elif w==6:
print('\nsaturday')
elif w==7:
print('\nsunday')
else:
print("\nenter any weekday number(1-7)")
Output:
Program-7:
Week-5
Program-1:
Aim:write a python program to print all natural
numbers from 1 to n-using while loop
Program:
n=int(input("enter a number:"))
i=1
while i<=n:
print(i,end=",")
i=i+1
‘’’for i in range(1,n+1):
print(i)’’’
output:
Program-2:
Aim:write a python program to find sum of all odd
numbers between 1 to n
Program:
n=int(input("enter a number:"))
sum=0
i=1
while i<=n:##for i in range(1,n+1):
if i%2!=0:
sum=sum+i
i=i+1
print("sum of all odd numbers from 1 to %d is"%n,sum)
output:
Program-3:
Aim:write a python program to count number of digits in a number
Program:
n=int(input("enter a number:"))
m=n
count=0
while n>0:
count=count+1
n=n//10
print("number of digits in %d is"%m,count)
output:
Program-4:
Aim:write a program to find first and last digit of a number
Program:
n=int(input("enter a number:"))
m=n
rev=0
if n>0:
last=n%10
while n>0:
rev=(rev*10 )+(n%10)
n=n//10
if rev>0:
first=rev%10
print("first digit of %d"%m,first)
print("last digit of %d"%m,last)
output:
Program-5:
Aim:write a python program to calculate sum of digits of a
number
Program:
n=int(input(“enter number:”))
m=n
sum=0
while n>0:
sum=sum+(n%10)
n=n//10
print(“sum of digits in %d”%m,sum)
output:
Program-6:
Aim:write a python program to enter a number and print its
reverse
Program:
n=int(input(“enter a number:”))
m=n
rev=0
while n>0:
rev=(rev*10)+(n%10)
n=n//10
print(“reverse of %d is”%m,rev)
output:
Week-6
Program-1:
Aim: Write a Python program to check whether a number is
palindrome or not.
Program:
n=int(input("enter number:"))
m=n
rev=0
while n>0:
rev=(rev*10)+(n%10)
n=n//10
if rev==m:
print("%d is palindrome number"%m)
else:
print("%d is not palindrome number"%m)
Output:
Program-2:
Aim: Write a Python program to find frequency of each digit in a given integer.
Program:
print("digit\tfrequency")
for i in range(0,10):
count=0
temp=n
while temp>0:
digit=temp%10
if (digit==i):
count=count+1
temp=temp//10
print(i,"\t",count)
output:
Program-3:
Aim: Write a Python program to print all ASCII character with their
values.
Program:
ch=input("enter the number:")
asc=ord(ch)
print("ASCII of %c"%ch,asc)
Output:
Program-4:
Aim: Write a Python program to find all factors of a number.
Program:
n=int(input("enter number:"))
count=0
for i in range(1,n+1):
if n%i==0:
count=count+1
print("%d is factor"%i)
else:
continue
Output:
Program-5:
Aim: Write a Python program to calculate factorial of a number.
Program:
n=int(input("enter number:"))
fact=1
m=n
while n>0:
fact=fact*n
n=n-1
print("factorial of %d is"%m,fact)
Output:
Program-6:
Aim: Write a Python program to print all Prime numbers between 1 to n.
Program:
n=int(input("enter number:"))
print("prime numbers from 1 to %d are"%n)
for i in range(1,n+1):
count=0
for j in range(1,i+1):
if (i%j==0):
count=count+1
if count==2:
print("\t%d"%i)
Output:
Program-7:
Aim: Write a Python program to check whether a number is Armstrong number or Strong or
Prime Number or Perfect number or magic number or not
Program:
n=int(input("enter number:"))
sum=0
r=0
m=n
while n!=0:
r=n%10
sum=sum+(r*r*r)
n=n//10
if sum==m:
print("%d is armstrong number"%m)
output:
def fact(r):
fact=1
while r>0:
fact=fact*r
r=r-1
return fact
n=int(input('enter number:'))
sum=0
m=n
while n>0:
r=n%10
sum=sum+fact(r)
n=n//10
if sum==m:
print("%d is strong number"%m)
output:
n=int(input('enter number:'))
m=n
sum=0
for i in range(1,n+1):
if n%i==0:
sum=sum+i
else:
continue
if sum==m:
print("%d is perfect number"%m)
output:
def magic(n):
sum = 0
while n> 0:
sum =sum+ n% 10
n= n// 10
return magic(sum) if sum > 9 else sum
m = int(input("Enter number : "))
x = magic(m)
if x==1:
print(m, "is a magic number.")
else:
print(m,"is not a magic number.")
output:
n=int(input("enter number:"))
count=0
for i in range(1,n+1):
if n%i==0:
count=count+1
else:
continue
if count==2:
print("%d is a prime number"%n)
else:
print("%d is not a prime number"%n)
Program-8:
Aim: Write a Python program to print Fibonacci series up to n terms.
Program:
n=int(input("enter n:"))
a=0
b=1
c=0
i=1
while i<=n:
print(a)
c=a+b
a=b
b=c
i=i+1
Output:
Week-7
Program-1:
Program:
list1=[]
for i in range(0,n):
list1.append(ele)
list1.sort()
print(list1[-1])
output:
Program-2:
Program:
list1=[]
for i in range(0,n):
list1.append(ele)
list1.sort()
print(list1[-2])
output:
Program-3:
Aim:write a python program to put even and odd elements in a list into 2 different lists
Program:
list1=[]
list2=[]
list3=[]
for i in range(0,n):
ele=int(input("enter the element into list"))
list1.append(ele)
for j in range(0,n):
elem=list1[j]
if elem%2==0:
list2.append(elem)
else:
list3.append(elem)
print("even list:",list2)
print("odd list:",list3)
Output:
Program-4:
Program:
list1=[]
list2=[]
list3=[]
for i in range(0,n):
list1.append(ele)
for j in range(0,n):
elem=list1[j]
if elem%2==0:
list2.append(elem)
else:
list3.append(elem)
k=list2+list3
k.sort()
print(k)
output:
Program-5:
Aim:write a python program to sort the list according to the second element in sublist
Program:
list1=[]
for i in range(0,n):
sublist=[]
for j in range(0,2):
elem=int(input("enter elements:"))
sublist.append(elem)
list1.append(sublist)
l=len(list1)
for i in range(0,l):
for j in range(0,l-i-1):
if(list1[j][1]>list1[j+1][1]):
t=list1[j]
list1[j]=list1[j+1]
list1[j+1]=t
print(list1)
Output:
Program-9:
aim:write a python program to find intersection of 2 lists
program:
l=[1,2,3,6,7,8,0,5]
p=[3,4,5,2,9]
c=[]
for i in l:
for j in p:
if i==j:
c.append(i)
print(c)
list1=[]
x=[]
for i in range(0,n):
list1.append(ele)
for i in range(0,n):
if(i%2!=0):
x.append(list1[i])
print(x)
output:
Week-8
Program-1:
Aim:write a python program to ge 4th element and 4th
element from last of a tuple
Program:
f=tuple()
l=list(f)
n=int(input("enter number of elements:"))
for i in range(n):
ele=int(input("enter elements:"))
l.append(ele)
f=tuple(l)
print(f)
print(f[3])
print(f[-4])
output:
Program-2:
Aim:write a python program to find the repeated items of a
tuple
Program:
t1=(1,2,3,2,3,5,6,6,7,8,9)
L=[]
for i in t1:
if t1.count(i)>1 and i not in L:
print(i,"-",t1.count(i),"times")
L.append(i)
Output:
Program-3:
Aim:write a python program to check whether an element
exists within a tuple
Program:
t1=(1,2,3,2,4,5,6,6,7,7,8,9)
i=int(input("enter an element to search:"))
if i in t1:
print("exist")
else:
print("not exist")
Output:
Program-4:
Aim:write a python program to unzip a list of tuples into
individual lists
Program:
l=[(10,20,40),(40,50,60),(70,80,90)]
l1=list(zip(*l))
print(l1)
Output:
Program-5:
Aim:write a python program to replace last value of tuples in
a list
Program:
l=[(10,20,40),(40,50,60),(70,80,90)]
for i in range(len(l)):
l1=list(l[i])
l1[-1]=100
l.append(tuple(l1))
l.remove(l[0])
l.remove(l[0])
l.remove(l[0])
print(l)
Output:
Program-6:
Aim:write a python program to remove an empty tuples from
a list of tuples
Program:
l=[(),(),('',),('a','b'),('a','b','c'),('d')]
l2=list(filter(None,l))
print(l2)
Output:
Program-7:
Aim:write a python program to convert list of tuples into a
dictionary
Program:
l=[('k1',1),('k2',2),('k3',3),('k4',4),('k5',5),('k6',6)]
print(dict(l))
output:
Week-9
Program-1:
Aim: Write a Python function that prints out the first n rows of Pascal's triangle.
Program:
n=int(input())
def factorial(n):
fact=1
while n>0:
fact=fact*n
n=n-1
return fact
for i in range(n):
for j in range(n-i+1):
print(end=' ')
for j in range(i+1):
print(factorial(i)//(factorial(j)*factorial(i-j)),end=' ')
print()
Output:
Program-2:
Aim: Write a Python function to create and print a list where the values are square of numbers
between 1 and 30 (both included).
Program:
def square():
l=[]
for i in range(1,31):
l.append(i*i)
return l
print(square())
Output:
Program-3:
Aim: Write a Python program to detect the number of local variables declared in a function
Program:
def function():
a=1
b=2
c=a+b
str='praneeth'
p=9.0
print(function.__code__.co_nlocals)
Output:
Program-4:
Aim: Write a Python program that invoke a given function after specific milliseconds.
Program:
def s():
print('printing after delay..')
sleep(millisecond/1000)
s()
Output:
Program-5:
Program:
def sum(n):
if n==0:
return 0
else :
return n%10+sum(int(n/10))
print(sum(123))
Output:
Week-10
Program-1:
Program:
n=int(input())
sum=0
for i in range(1,n+1):
sum=sum+(1/i)
print(sum)
def harmonic_sum(n):
if n<2:
return 1
else:
return 1/n+(harmonic_sum(n-1))
print(harmonic_sum(num))
Output:
Program-2:
Aim: Write a Python program to calculate the sum of the positive integers of n+(n-2)+(n-4)... (until n-
x => 0)
Program:
n=int(input())
x=0
sum=0
while n-x>=0:
sum=sum+(n-x)
x=x+2
print(sum)
def sum_series(n):
if n<1:
return 0
else:
return n+sum_series(n-2)
print(sum_series(num))
Output:
Program-3:
Aim: Write a Python program to find the greatest common divisor (gcd) of two integers using
Recursion
Program:
l=[]
m=[]
for i in range(1,a+1):
if a%i==0:
l.append(i)
for j in range(1,b+1):
if b%j==0:
m.append(j)
l=set(l)
m=set(m)
n=l.intersection(m)
n=list(n)
print(max(n))
def gcd(a,b):
if (b==0):
return a
else:
return gcd(b,a%b)
div=gcd(a,b)
print("gcd is:",div)
Output:
Program-4:
Program:
def swap(A,i,j):
temp=A[i]
A[i]=A[j]
A[j]=temp
def bubbleSort(A,n):
for i in range(n-1):
if A[i]>A[i+1]:
swap(A,i,i+1)
if n-1>1:
bubbleSort(A,n-1)
if __name__=='__main__':
A=[3,5,8,4,1,9,-2]
bubbleSort(A,len(A))
print(A)
Output:
Week-11
Program-1:
Aim: Write a Python Program to Replace all Occurrences of ‘a’ with ‘b’ in a String. If ‘a’ is not present
then print appropriate message.
Program:
modified_str=''
for i in range(0,len(str)):
if str[i]=='a':
modified_str+='b'
else:
modified_str+=str[i]
print(modified_str)
Output:
Program-2:
aim: Write a Python Program to Remove the nth Index Character from a Non-Empty String
Program:
str=input('enter string')
l=[]
m=''
for i in range(0,len(str)):
l.append(str[i])
l.remove(l[n])
for j in l:
m+=j
print("modified string:",m)
def remove(string,n):
first=string[:n]
last=string[n+1:]
return first+last
print("modified string:")
print(remove(string,n))
Output:
Program-3:
Program:
str1=str(input())
str2=str(input())
l=[]
k=[]
for i in range(0,len(str1)):
l.append(str1[i])
for j in range(0,len(str2)):
k.append(str2[j])
l.sort()
k.sort()
if l==k:
else:
Output:
Program-4:
Aim: Write a Python Program to Form a New String where the First Character and the Last Character
have been Exchanged.
Program:
def swap(string):
start=string[0]
end=string[-1]
swapped_string=end+string[1:-1]+start
print(swapped_string)
swap(str)
l=[]
m=''
for i in range(0,len(str)):
l.append(str[i])
t=l[0]
l[0]=l[-1]
l[-1]=t
for i in l:
m=m+i
print(m)
Output:
Week-12
Program-1:
Aim:Write a Python program to get the last part of a string before a specified character
Program:
str="praneemamidi04042005@gmail.com"
print(str[0:str.index("@")])
Output:
Program-2:
Aim: Write a Python program to count the occurrences of each word in a given sentence
Program:
l=str.split(" ")
for i in l:
print("'",i,"'",":",l.count(i))
Output:
Program-3:
Program:
s="{{}}"
s=s[0:len(s)//2]+"Praneeth"+s[len(s)//2:]
print(s)
Output:
Program-4:
Aim: Write a Python function to get a string made of its first three characters of a specified string. If
the length of the string is less than 3 then return the original string.
Program:
s="py"
if len(s)<3:
print(s)
else:
print(s[0:3])
l="python"
if len(l)<3:
print(l)
else:
print(l[0:3])
Output:
Program-5:
Aim: Write a Python program to add a prefix text to all of the lines in a string
Program:
txt='''thon is high-level
l=txt.split("\n")
j=[]
o=''
for i in l:
i="py"+i
j.append(i)
for k in j:
print(k)
Output:
Program-6:
Aim: Write a Python program to convert a given string into a list of words.
Program:
Output: