0% found this document useful (0 votes)
10 views7 pages

Program

Uploaded by

praveenraja22cb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Program

Uploaded by

praveenraja22cb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

EX.

6 Python programs using functions

AIM: To write a Python program to implement Factorial, Swapping two values, Largest of 3 numbers in
a list, Area of mathematical shapes using functions.

ALGORITHM:

1. Start the program


2. Define the function using def keyword
3. For factorial function “fact(n)”, pass the number for which the factorial has to be found (n) as
the parameter
3.1 Define the function, def fact(n)
3.2 Initialize i=1
3.3 While(i<n)
3.3.1 If n==1, return 1
3.3.2 Else return(n*n-1).go to step 3.1
4. For swapping two values function” swap(a,b)”,
4.1 Define the function swap()
4.1.1 Swap the variables of different data types using a,b=b,a statement.
4.1.2 Swap using temporary variable
4.1.3 Print the values of a and b after swapping.
5. For finding largest of two numbers using list function large”(list_1)”
5.1 Define the function large(list_1)
5.1.1 Initialize max=Iist_1[0], i=0
5.1.2 For (i<len(list_1)
5.1.2.1 If(list1[index]>max)
5.1.2.1.1 Swap(max, list_1[index])
5.1.2.1.2 i=i+1
5.1.3 Print the maximum element is max
6. For finding the area of shapes, def separate functions to find area of square ,rectangle, triangle,
circle.
6.1 Define function area_square(side)
6.1.1 Compute area=side*side
6.1.2 Print the value of area
6.2 Define function area_triangle(base,height)
6.2.1 Compute area=base*height
6.2.2 Print the value of area
6.3 Define function area_circle(radius)
6.3.1 Compute area=3.14*r*r
6.3.2 Print the value of area
6.4 Define function area_rectangle(length, breadth)
6.4.1 Compute area=length*breadth
6.4.2 Print the value of area
7. Get the input values n, a,b , list, radius, side,length,breadth,base ,height.
8. Call the functions in order with function names.
9. Stop the program

PROGRAM:

max_element=0
a=0
b=0
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
def max_list(list_1):
maxi=list_1[0]
for i in range(0,len(list_1),1):
if(list_1[i]>maxi):
t=maxi
maxi=list_1[i]
list_1[i]=t
return(maxi)
def swap(a,b):
t=a
a=b
b=t
return(a,b)
# the area of a shapes
def calculate_area(name):

# converting all characters


# into lower cases
name = name.lower()
# check for the conditions
if name == "rectangle":
l = int(input("Enter rectangle's length: "))
b = int(input("Enter rectangle's breadth: "))
# calculate area of rectangle
rect_area = l * b
print("The area of rectangle is",rect_area)
elif name == "square":
s = int(input("Enter square's side length: "))
# calculate area of square
sqt_area = s * s
print("The area of square is",sqt_area)
elif name == "triangle":
h = int(input("Enter triangle's height length: "))
b = int(input("Enter triangle's breadth length: "))
# calculate area of triangle
tri_area = 0.5 * b * h
print(f"The area of triangle is",tri_area)
elif name == "circle":
r = int(input("Enter circle's radius length: "))
pi = 3.14
# calculate area of circle
circ_area = pi * r * r
print(f"The area of circle is",circ_area)
elif name == 'parallelogram':
b = int(input("Enter parallelogram's base length: "))
h = int(input("Enter parallelogram's height length: "))
# calculate area of parallelogram
para_area = b * h
print(f"The area of parallelogram is ",para_area)
else:
print("Sorry! This shape is not available")
n=int(input("Input a number to compute the factorial : "))
print("The factorial value of n is",factorial(n))
print("Calculate Shape Area")
shape_name = input("Enter the name of shape whose area you want to
find: ")
# function calling
calculate_area(shape_name)
a=input("enter the a value:")
b=input("enter the b value:")
print("The values of a and b: Before Swapping:",a,b)
a,b=swap(a,b)
print("The values of a and b: After Swapping:",a,b)
list_1=[10,65,32,78,90,121]
print("the length of the list is",len(list_1))
print("maximum element in list using max function is", max(list_1))
max_element=max_list(list_1)
print("The maximum element in the list without using max function
is",max_element)
#list_1.remove(max_element)
max_element=max_list(list_1)
print("The second maximum element in the list without using max
function is ",max_element)

OUTPUT:
Input a number to compute the factorial : 5
The factorial value of n is 120
Calculate Shape Area
Enter the name of shape whose area you want to find: circle
Enter circle's radius length: 6
The area of circle is 113.03999999999999
enter the a value:23
enter the b value:45
The values of a and b: Before Swapping: 23 45
The values of a and b: After Swapping: 45 23
the length of the list is 6
maximum element in list using max function is 121
The maximum element in the list without using max function is 121
The second maximum element in the list without using max function is
90
>

RESULT:
Python program to implement Factorial, Swapping two values, Largest of 3 numbers in a list,
Area of mathematical shapes using functions.
EX 7.A STRING BUILT IN FUNCTIONS

AIM: To write a python program to implement the string built in functions.

ALGORITHM:
1. Start the program
2. Define the string and use string built-in functions for performing string reverse(), string count(),
string replace()methods.
3. Display the results
4. Stop the program

PROGRAM:

txt = "PYTHON PROGRAMMING"


print("Reverse of the string txt is", txt[::-1])
s = "malayalam"
if s==s[::-1]:
print("The given string",s,"is palindrome")
else:
print("The given string",s,"is not palindrome")
str1 = "problem solving and python programming"
str_count1 = str1.count('p') # counting the character “o” in the
givenstring
print("The count of 'p' is", str_count1)
str_count2 = str1.count('p', 0,len(str1))
print("The count of 'p' using start/end is", str_count2)
string = "Dhoni Sachin Kohli Sachin Sachin"
print(string.replace("Sachin", "Dhoni"))
print(string.replace("Sachin", "Dhoni", 2))

OUTPUT:
Reverse of the string txt is GNIMMARGORP NOHTYP
The given string malayalam is palindrome
The count of 'p' is 3
The count of 'p' using start/end is 3
Dhoni Dhoni Kohli Dhoni Dhoni
Dhoni Dhoni Kohli Dhoni Sachin

RESULT:

The python program to execute the string built-in functions is executed successfully.
EX 7.B STRING USER-DEFINED FUNCTIONS

AIM: To write a python program to implement the string built in functions.

ALGORITHM:
1. Start the program
2. Define the string function reverse():
2.1 Get the string from the user
2.2 Use string reverse with slicing operator
3. Get the strings to check for palindrome
4. Find the reverse of the first string using strrev() and store it in strrev variable
5. Compare the strings strrev and string1 and print palindrome or not.
6. Use strcount() function to count the specific character in the string using for loops
7. Use strreplace(string, substring to be replaced, replacing string) to replace the substrings in the
particular string
8. Stop the program

PROGRAM:

def reverse(s):
if len(s) == 0:
return s
else:
return reverse(s[1:]) + s[0]

s = "Hello world"
print ("The original string is : ",end="")
print (s)
print ("The reversed string(using recursion) is : ",end="")
print (reverse(s))
string1=input("palindrome check: Enter the string")
strrev=reverse(string1)
if(string1==strrev):
print("palindrome")
def strcount(s):
count=0
print("the string received is",s)
char=input("Enter the character to be counted in the string")
for i in s:
if(i==char):
count=count+1
print("the count of",char,"is: ",count)
strcount("hello world")
def replace(strng, substr, rplstr):
if substr not in strng:
return strng

low_index = strng.find(substr) # get lowest index of substring


high_index = low_index + len(substr) # evaluate the highest index
of substring

return replace(strng[:low_index]+ rplstr + strng[high_index:],


substr, rplstr)
print("String replace")
string1=input("Enter string to be replaced")
string2=input("Enter the substring to replace")
string3=input("Enter the replacing substring")
print(replace(string1,string2,string3))

OUTPUT:
The original string is : Hello world
The reversed string(using recursion) is : dlrow olleH
palindrome check: Enter the string
malayalam
palindrome the string received is hello world
Enter the character to be counted in the string
o
the count of o is: 2
String replace Enter string to be replaced
hello
Enter the substring to replace
ello
Enter the replacing substring
@@@@
h@@@@

RESULT:

The python program to execute the string user defined functions is executed successfully.

You might also like