Functions Python

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

FUNCTIONS PYTHON MOMINA AFZAL

A PYTHON FUNCTION THAT ACCEPTS A STRING


AND CALCULATE THE NUMBER OF UPPER CASE
LETTERS AND LOWER CASE LETTERS

1. def string_test(s): *string


d={"UPPER_CASE":0, "LOWER_CASE":0} *var initialization

for c in s: *string in parameter for loop runs till s


if c.isupper(): *Loop runs till end of sentence
d["UPPER_CASE"]+=1
elif c.islower():
d["LOWER_CASE"]+=1
else:
pass *error handling
A PYTHON FUNCTION THAT ACCEPTS A STRING
AND CALCULATE THE NUMBER OF UPPER CASE
LETTERS AND LOWER CASE LETTERS
print ("Original String : ", s)
print ("No. of Upper case characters : ", d["UPPER_CASE"])
print ("No. of Lower case Characters : ", d["LOWER_CASE"])

2.string_test('The world is your oyester')


string_test("This is a DMT presentation")
A PYTHON FUNCTION THAT ACCEPTS A STRING
AND CALCULATE THE NUMBER OF UPPER CASE
LETTERS AND LOWER CASE LETTERS

OUTPUT:
PYTHON FUNCTION THAT
RETURNS MULTIPLE VALUES
1.def SwapTwoNumbers(a,b):
3. print("Before Swap: ",a,b)
4. a = a+b *1554 stored in a
b = a-b *1154-987=567
a = a-b *1154-567=987
return a,b *returns(987,587)

2.a,b = SwapTwoNumbers(567,987)
5.print("After Swap: ",a,b)
PYTHON FUNCTION THAT
RETURNS MULTIPLE VALUES

OUTPUT:
PYTHON FUNCTION TO FIND
THE FACTORIAL OF A
NUMBER
1.def factorial(n):
fact = 1 *initialize fact
while(n!=0): *fact 1
fact *= n *loop runs till n=0
n = n-1 *3-1=2
print("The factorial is",fact)

2.inputNumber = int(input("Enter the number: "))


3.factorial(inputNumber)
PYTHON FUNCTION TO FIND
THE FACTORIAL OF A
NUMBER

OUTPUT:
REVERSE A STRING
def string_reverse(str1):
rstr1 = ‘ ‘ *string initialized, Blank
index = len(str1)
while index > 0:
rstr1 += str1[ index - 1 ]
index = index - 1
return rstr1
print(string_reverse(‘DMT course has python in it'))
REVERSE A STRING

OUTPUT:
AVERAGE OF MARKS
marks= [93,88,99,56,97,67,89,56,78,54,85] def letter_grade_total(total):
if total >= 90:
def average(marks):
print( "A")
total=sum(marks)/len(marks) elif total >= 80 and total < 90:
return total print( "B")
elif total >= 70 and total <80:
total=average(marks)
print( "C")
elif total >= 60 and total <70:
print( "D")
return total
grading=letter_grade_total(total)
print(grading)
AVERAGE OF MARKS

OUTPUT:

You might also like