Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 40

Ex.

no: 1 Addition of two numbers

Date:4.7.2023

Aim:

To write a python program to add two numbers

Program:

n1=int(input("enter the value :"))

n2=int(input("enter the value :"))

ans=n1+n2

print("the result is",ans)

Output:

enter the value10

enter the value20

the result is 30

Result:

Thus the program will be successfully executed.


Ex.no: 2 Factorial of numbers

Date:6.7.2023

Program:

n=int(input("enter the value of n"))

fact=1

for x in range(1,n+1):

fact=fact*x

print(fact)

Output:

enter the value of n6

720

Result:

Thus the program will be successfully executed.


Ex.no: 1 Addition of two numbers

Date:4.7.2023

3.Reverse number using while loop:

Program:

n=int(input(" Enter the number:"))

temp=n

r=0

while(n>0):

dig=n%10

r=(r*10)+dig

n=n//10

print(r)

Output:

Enter the number:45678


87654

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

4.Armstrong number checking:

Program:

n=int(input(" Enter the number:"))

temp=n

r=0

while(n>0):

dig=n%10
r=r+dig*dig*dig

n=n//10

print(r)

if (r==temp):

print(temp,"is Armstrong")

else:

print (temp,"is not a Armstrong")

Output:

Enter the number:256

349

256 is not a Armstrong

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

5.Prime number checking:

Program:

n=int(input("Enter the number"))

c=0
for i in range (2,n):

if (n%i)==0:

c=1

if(c==0):

print (n,"is prime number")

else:

print (n,"is not a prime number")

Output:

Enter the number97

97 is prime number

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

6.Palindrome number checking


Program:

n=int(input(" Enter the number:"))

temp=n

r=0

while(n>0):

dig=n%10

r=(r*10)+dig

n=n//10

print(r)

if (r==temp):

print(temp,"is pallindrome")

else:

print (temp,"is not a pallindrome")

Output:

Enter the number:2345

5432

2345 is not a palindrome

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023
7.Sum of digits:

Program:

num=int(input("Enter the number:"))

sum=0

while (num>0):

r=num%10

sum=sum+r

num=num//10

print ("the sum of digit is",sum)

Output:

Enter the number:2901

the sum of digit is 12

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

8.Odd or even:

Program:

n=int(input("Enter n value:"))

i=1
while(i<=n):

if (i%2!=0):

print(i,"is odd number")

else:

print(i,"is even number")

i+=1

Output:

Enter n value:21

1 is odd number

2 is even number

3 is odd number

4 is even number

5 is odd number

6 is even number

7 is odd number

8 is even number

9 is odd number

10 is even number

11 is odd number

12 is even number

13 is odd number

14 is even number

15 is odd number

16 is even number

17 is odd number
18 is even number

19 is odd number

20 is even number

21 is odd number

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

9.Prime number within the limit:

Program:

lower=int(input("Enter the lower limit"))

upper=int(input("Enter the upper limit"))

for i in range(lower,upper+1):

if i>1:

for j in range(2,i):

if (i%j)==0:

break

else:
print(i)

break

Output:

Enter the lower limit3

Enter the upper limit10

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

10.Factorial using recursion:

Program:

def factorial(n):

if n==0:

return 1

else:
return n*factorial (n-1)

n=int(input ("Enter the number:"))

print (factorial(n))

Output:

Enter the number:4

24

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

11.Print constant in string:

Program:

s= str(input("Enter the name:"))

for val in s:

if val=="a" or val== "e" or val=="i" or val=="o" or val=="u":

continue

print(val)

Output:

Enter the name:dhanu ammu

d
h

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

12.String in free defined function:

Program:

c=1

while(c>0):

s=input("Enter Any String")

print("Menu")

print("****")

print("1.Capitalize First Letter")

print("2.Convert Uppercase")

print("3.Convert Lowercase")

print("4.Count occurence")
ch=int(input("Enter Any Choice"))

if(ch==1):

print(str.capitalize(s))

elif(ch==2):

print(str.upper(s))

elif(ch==3):

print(str.lower(s))

else:

ss=input("Enter Any search character")

print(str.count(s,ss))

c=int(input("Do you want to Continue(1-0)"))

Output:

>>>

Enter Any Stringdhanusiya

Menu

****

1.Capitalize First Letter

2.Convert Uppercase

3.Convert Lowercase

4.Count occurence

Enter Any Choice2

DHANUSIYA

Do you want to Continue(1-0)0

Result:
Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

13.Find largest number in an array:

Program:

from array import*

myarray=array('i',[])

n=int(input("Enter the number of elements"))

for i in range (0,n):

v=int(input("Enter the number "))

myarray.insert(i,v)

print("The given element are")

m=0

for i in range(len(myarray)):

if m< myarray[i]:

m=myarray[i]

print("The largest is",m)

Output:

Enter the number of elements2

Enter the number 20


Enter the number 90

The given element are

The largest is 90

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

14.Array free defined method:

Program:

from array import*

c=1

while (c>0):

print("menu")

print("******")

print("1.creation of array")

print("2.append")

print("3.insert")

print("4.pop")

print("5.remove")

print("6.display")

ch=int(input("enter the value"))


if(ch==1):

myarray=array('i',[])

n=int(input("enter the number of elements"))

for i in range(0,n):

v=int(input("enter the members"))

myarray.insert(i,v)

for i in myarray:

print(i)

elif(ch==2):

v=int(input("enter the number"))

myarray.append(v)

elif(ch==3):

v=int(input("enter the number"))

i=int(input("enter the place to insert values"))

myarray.insert(i,v)

elif(ch==4):

v=int(input("enter the index of remove valu"))

myarray.pop(i)

elif(ch==5):

v=int(input("enter the element to remove"))

myarray.remove(v)

elif(ch==6):

for i in myarray:

print(i)

c=int(input("do you want to continue(y-1/n-0)"))


Output:

menu

******

1.creation of array

2.append

3.insert

4.pop

5.remove

6.display

enter the value1

enter the number of elements6

enter the members35

enter the members67

enter the members89

enter the members90

enter the members24

enter the members26

35

67

89

90

24

26

do you want to continue(y-1/n-0)0


Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

15.List program for pre-defined function:

Program:

print("list")

c=1

while(c>0):

print("menu")

print("----")

print("1.creation of list")

print("2.length")

print("3.search")

print("4.max and min")

print("5.sorting")

ch=int(input("Enter your choice"))

if(ch==1):
list_A=[]

n=int(input("enter the number of elements"))

for i in range(n):

ele=int(input())

list_A.append(ele)

if(ch==2):

print("Length of list is",len(list_A))

if(ch==3):

sv=int(input("Enter the number to search"))

if sv in list_A:

print(sv,"is present")

else:

print(sv,"is not present")

if(ch==4):

print("Maximum value is ",max(list_A))

print("Minimum value is",min (list_A))

if(ch==5):

print("Sorting value is ",sorted(list_A))

c=int (input("DO YOU WANT TO CONTINUE(y-1/n-0)"))

Output:

list

menu

----

1.creation of list
2.length

3.search

4.max and min

5.sorting

Enter your choice1

enter the number of elements4

100

50

30

36

DO YOU WANT TO CONTINUE(y-1/n-0)1

menu

----

1.creation of list

2.length

3.search

4.max and min

5.sorting

Enter your choice4

Maximum value is 100

Minimum value is 30

DO YOU WANT TO CONTINUE(y-1/n-0)

Result:
Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

16.List of sum:

Program:

list_A=[]

n=int(input("Enter the number of elements"))

for i in range(n):

ele=int(input())

list_A.append(ele)

print("SUM=" the result is,(list_A))

Output:

Enter the number of elements3

20

30

10

SUM= 60

Result:
Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

17.List of sorting:

Program:

list_A=[]

n=int(input("Enter the number of elements"))

for i in range(n):

ele=input()

list_A.append(ele)

print("sorting value is",sorted (list_A))

Output:

Enter the number of elements3

33

90

24

sorting value is ['24', '33', '90']

Result:

Thus the program will be successfully executed.


Ex.no: 1 Addition of two numbers

Date:4.7.2023

18.Count the occurence of value(using tuple):

Program:

t1=('p','y','t','h','o','n','p','r','o','g','r','a','m')

print(t1)

t2=str(input("Enter the element to check its occurence"))

print(t2,"is present in",t1.count(t2),"times in the place of",t1.index(t2))

Output:

('p', 'y', 't', 'h', 'o', 'n', 'p', 'r', 'o', 'g', 'r', 'a', 'm')

Enter the element to check its occurencer

r is present in 2 times in the place of 7

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers


Date:4.7.2023

19.Creation of dictionary:

Program:

n=int(input("Enter the value"))

i=0;

dict1={}

while(i<n):

str1=input("Enter the name")

rollno=int(input("Enter the Rollno"))

dict1[str1]=rollno

i=i+1

print('Entered dictionary elements are')

print(dict1)

Output:

Enter the value2

Enter the namedhanusiya

Enter the Rollno1001

Enter the nameniraimathi

Enter the Rollno1002

Entered dictionary elements are

{'dhanusiya': 1001, 'niraimathi': 1002}


Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

20.Creation of dictionary using key and value:

Program:

employees=dict(input('Enter key and value separated by space:').split()for i in range(2))

{'id':'1','name':'Alice'}

print(employees)

Output:

Enter key and value separated by space:10 dhanusiya

Enter key and value separated by space:20 niraimathi

{'10': 'dhanusiya', '20': 'niraimathi'}


Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

21.Validate the password using dictionary:

Program:

common_password=['abcd','asdfg']

while True:

password=input('Enter your password:')

if (len(password)>5and password not in common_password):

print('you Entered{password}')

break

else:

print('pick a strong password')

continue

print(password)

Output:

Enter your password:sld

pick a strong password

Enter your password:dhanusiya


you Entered{password}

dhanusiya

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

22.Students details using defauld func:

Program:

def student(name,age,year='1',course='BCA',college='Noble'):

print('Student Details:',name,age,year,course,college)

n=int(input("Enter the number of students"))

for i in range(n):

name=input("Enter student name")

age=int(input("Enter student age"))

student(name,age)

student(name,age,"II","CS","SBK")
Output:

Enter the number of students2

Enter student namedhanusiya

Enter student age19

Student Details: dhanusiya 19 1 BCA Noble

Enter student nameniraimathi

Enter student age13

Student Details: niraimathi 13 1 BCA Noble

Student Details: niraimathi 13 II CS SBK

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

23.Addition using variable length argument:

Program:

def add_num(*args):

sum=0
print("The sum of values are")

for i in range:

sum +=i

print(sum)

print("Menu")

print("****")

print("1.Addition of two numbers:")

print("2.Addition of three numbers:")

print("3.Addition of four numbers:")

ch=int(input("Enter your choice"))

if(ch==1):

a=int(input("Enter the first number"))

b=int(input("Enter the second number"))

add_num(a,b)

if(ch==2):

a=int(input("Enter the first number"))

b=int(input("Enter the second number"))

c=int(input("Enter the third number"))

add_num(a,b,c)

if(ch==3):

a=int(input("Enter the first number"))

b=int(input("Enter the second number"))

c=int(input("Enter the third number"))

d=int(input("Enter the fourth number"))

add_num(a,b,c,d)
Output:

Menu

****

1.Addition of two numbers:

2.Addition of three numbers:

3.Addition of four numbers:

Enter your choice2

Enter the first number10

Enter the second number20

Enter the third number30

The sum of values are

60

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

24.Count character ,word and line in a file:

Program:

filename=input("Enter a file name:")

c=0

w=0
l=0

f1=open(filename,'w')

f1.write('computer science engineering\n')

f1.write('electronics and communication engineering\n')

f1.close()

f2=open(filename,'r')

data=f2.readlines()

print(data)

for i in data:

c=c+len(i)

w=w+len(i.split())

l=l+1

f2.close()

print('The number of characters are:',c)

print('The number of words are:',w)

print('The number of lines are:',l)

Output:

Enter a file name:details

['computer science engineering\n', 'electronics and communication engineering\n']

The number of characters are: 71

The number of words are: 7

The number of lines are: 2

Result:
Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

25.Print student name and percentage using dictionary:

Program:

rec={}

i=1

n=int(input("Enter number of students:"))

while(i<=n):

name=input("Enter student name:")

marks=input("Enter % of marks of student:")

rec[name]=marks

i=i+1

print("names of student","\t","% of marks")

for x in rec:

print("\t",x,"\t",rec[x])

Output:

Enter number of students:4

Enter student name:dhanusiya

Enter % of marks of student:99


Enter student name:divya

Enter % of marks of student:99

Enter student name:jeyanthi

Enter % of marks of student:99

Enter student name:priya

Enter % of marks of student:99

names of student % of marks

priya 99

divya 99

jeyanthi 99

dhanusiya 99

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

26.Count occurrence of letter using dictionary:

Program:

word=input("Enter any word:")

d={}

for x in word:

d[x]=d.get(x,0)+1
for k,v in d.items():

print(k,"occured",v,"times")

Output:

Enter any word:colourfullflowers

c occured 1 times

e occured 1 times

f occured 2 times

l occured 4 times

o occured 3 times

s occured 1 times

r occured 2 times

u occured 2 times

w occured 1 times

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

27.Arithmetic operation using positional argument(return more than


one value)

Program:

def calc(a,b):
sum=a+b

sub=a-b

mul=a*b

div=a/b

return sum,sub,mul,div

t=calc(100,50)

for x in t:

print(x)

Output:

150

50

5000

2.0

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

28.Square of number using lambda function:

Program:

s=lambda n:n*n
print("The square of 4 is=",s(4))

print("The square of 5 is=",s(5))

Output:

The square of 4 is= 16

The square of 5 is= 25

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

29.Biggest among w number using lambda function:

Program:

s=lambda a,b:a if a>b else b

print("The biggest of 10,20 is:",s(10,20))

print("The biggest of 100,200 is :",s(100,200))

Output:

The biggest of 10,20 is: 20

The biggest of 100,200 is : 200

Result:

Thus the program will be successfully executed.


Ex.no: 1 Addition of two numbers

Date:4.7.2023

30.Module program:

Program:

def print_table(ourNum):

for x in range(1,11):

result=ourNum*x

print(ourNum,"*",x,"=",result)

import mul

ourNum=int(input("Enter the table Number"))

mul.print_table(ourNum)

Output:

Enter the table Number12

12 * 1 = 12

12 * 2 = 24

12 * 3 = 36

12 * 4 = 48

12 * 5 = 60

12 * 6 = 72

12 * 7 = 84
12 * 8 = 96

12 * 9 = 108

12 * 10 = 120

Result:

Thus the program will be successfully executed.

Ex.no: 1 Addition of two numbers

Date:4.7.2023

31.Command line program:

Program:

import sys

print("Number of arguments:",len(sys.argv))

num=int(sys.argv[1])

if (num%2)==0:

print("The number is even")

else:

print("The provided number is odd")

Output:

C:\Users\Computer 38>D:

D:\ cd Dhanu.0129

D:\Dhanu.0129>py commandlineprogram.py 45
Number of arguments: 2

The provided number is odd

Result:

Thus the program will be successfully executed.

You might also like