1. dedine fuction for % marks
def percent(marks):
p = ((marks[0] + marks[1] + marks[2]+ marks[3])/400 )*100
return p
marks1 = [45, 78, 86, 77]
percentage1 = percent(marks1)
marks2 = [75, 98, 88, 78]
percentage2 = percent(marks2)
print(percentage1, percentage2)
O/P:
>> 71.5 84.75
2. simple function
def greet(name):
print("good day, "+ name)
def mysum(num1, num2):
return num1 + num2
greet(avi)
s= mysum(12, 13)
print(s)
O/P:
>> good day, avi
25
3. default agrument
def greet(name="strenger"):
print('Good day, ' + name)
greet("avi")
greet()
O/P:
>> Good day, avi
Good day, strenger
4. Recursive method
factorial by simple method
n=5
product= 1
for i in range(n):
product= product*(i+1)
print(product)
def factorial_iter(n):
product=1
for i in range(n):
product = product* (i+1)
print(product)
factorial_iter(5)
def factorial_recursive(n):
if n==1 or n==0:
return 1
return n * factorial_recursive(n-1)
print(factorial_recursive(5))
O/P:
>> 120
120
120
5. define function to find max number
def maximum(num1, num2, num3):
if (num1>num2):
if(num1>num3):
return num1
else:
return num3
else:
if(num2>num3):
return num2
else:
return num3
m = maximum(13, 55, 2)
print("The value of the maximum is " + str(m))
O/P:
>> The value of the maximum is 55
6. define function for cecius to faherehit
def farh(cel):
return (cel *(9/5)) + 32
c = 0
f = farh(c)
print("Fahreheit Temperature is " + str(f))
O/P:
>> Fahreheit Temperature is 32.0
7. print on single line
print("Hello", end=" ")
print("How", end=" ")
print("are", end=" ")
print("you?", end=" ")
O/P:
>> Hello How are you?
8. sum with recursive
def sum(n):
if n==1:
return 1
return (n + sum(n-1))
s= sum(10)
print(s)
O/P:
>> 55
9. print star
n = 3
for i in range(n):
print ("*" * (n-i))
O/P:
>>
***
**
*
10. define function inch to cm
def cm(inch):
return (inch*2.54)
c= cm(25)
print(c, 'cm')
O/P:
>> 63.5 cm
11. function to Remove specific word from string
def remov_split(string, word):
new_str= string.replace(word, "")
return new_str.strip()
this = "avi is good"
s = remov_split(this, 'avi')
print(s)
O/P:
>> is good
12.
O/P:
>>
1.27.
O/P:
>>
1.
O/P:
>>
1.O/P:
>>
1.
O/P:
>>
1.27.
O/P:
>>
1.
O/P:
>>
1.O/P:
>>
1.
O/P:
>>
1.27.
O/P:
>>
1.
O/P:
>>
1.O/P:
>>
1.
O/P:
>>
1.27.
O/P:
>>
1.
O/P:
>>
1.O/P:
>>
1.
O/P:
>>
1.27.
O/P:
>>
1.
O/P:
>>
1.O/P:
>>
1.
O/P:
>>
1.27.
O/P:
>>
1.
O/P:
>>
1.O/P:
>>
1.
O/P:
>>
1.