Program
Program
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:
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):
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
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:
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
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
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.