0% found this document useful (0 votes)
24 views44 pages

Class-XII Computer Science

The document discusses functions in Python. It provides 6 examples of Python functions with their code and flow charts. The functions include finding the maximum of three numbers, summing and multiplying all numbers in a list, checking if a number is in a given range, counting uppercase and lowercase letters in a string, creating a list of squares of numbers from 1 to 30, and invoking a function after a specified time delay. The examples help explain how to define and use different types of functions in Python.

Uploaded by

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

Class-XII Computer Science

The document discusses functions in Python. It provides 6 examples of Python functions with their code and flow charts. The functions include finding the maximum of three numbers, summing and multiplying all numbers in a list, checking if a number is in a given range, counting uppercase and lowercase letters in a string, creating a list of squares of numbers from 1 to 30, and invoking a function after a specified time delay. The examples help explain how to define and use different types of functions in Python.

Uploaded by

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

CLASS-XII COMPUTER

SCIENCE(083)

Chapter: 3 Working with Functions


Programs
Pictorial presentation:

1. Write a Python function to find the maximum of three numbers.


Code: Flow Chart
def max_of_two( x, y ):
if x > y:
return x
return y
def max_of_three( x, y, z ):
return max_of_two( x, max_of_two( y, z ) )
print(max_of_three(3, 6, -5))
2. Write a Python function to sum all the numbers in a list. ( Sample List: (8,2,3,0,7) ,
Expected Output: 20).

Code: Pictorial presentation:

def sum(numbers):
total = 0
for x in numbers:
total += x
return total
print(sum((8, 2, 3, 0, 7)))
3. Write a Python function to multiply all the numbers in a list. (Sample List : (8, 2, 3, -1, 7)
Expected Output : -336)

Code: Pictorial presentation:

def multiply(numbers):
total = 1
for x in numbers:
total *= x
return total
print(multiply((8, 2, 3, -1, 7)))
4. Write a Python function to check whether a number falls within a given
range.

Code: Pictorial presentation:


def test_range(n):
if n in range(3,9):
print( " %s is in the
range"%str(n))
else :
print("The number is outside
the given range.")
test_range(5)
5. Write a Python function that accepts a string and counts the number of upper and lower case letters.
Sample String : 'The quick Brow Fox'
Expected Output : No. of Upper case characters : 3, No. of Lower case Characters : 12

Code: Pictorial presentation:


def string_test(s):
d={"UPPER_CASE":0, "LOWER_CASE":0}
for c in s:
if c.isupper():
d["UPPER_CASE"]+=1
elif c.islower():
d["LOWER_CASE"]+=1
else:
pass
print ("Original String : ", s)
print ("No. of Upper case characters : ", d["UPPER_CASE"])
print ("No. of Lower case Characters : ", d["LOWER_CASE"])

string_test('The quick Brown Fox')


6. Write a Python function to create and print a list where the values are
the squares of numbers between 1 and 30 (both included).
Code: Pictorial presentation:

def printValues():
l = list()
for i in range(1,21):
l.append(i**2)
print(l)
printValues()

Output:
6. Write a Python program that invokes a function after a specified period of time.
Sample Output: Square root after specific miliseconds: 4.0, 10.0, 158.42979517754858

Code: Pictorial presentation:


from time import sleep
import math def
delay(fn, ms, *args):
sleep(ms / 1000)
return fn(*args)
print("Square root after specific miliseconds:")
print(delay(lambda x: math.sqrt(x), 100, 16))
print(delay(lambda x: math.sqrt(x), 1000, 100))
print(delay(lambda x: math.sqrt(x), 2000, 25100))

Output:
Square root after specific miliseconds:
4.0
10.0

You might also like