0% found this document useful (0 votes)
148 views15 pages

Dry Run

The document contains 10 user-defined function programs in Python. Program 4 defines a function to check if a number is within a given range. Program 5 defines a function that prints marks with default values. Program 6 defines a function that calculates the cube of numbers. Program 10 defines a function that prints the value and id of a passed number. The remaining programs define functions for tasks like calculating sum, average, maximum number, area of a circle, and checking even/odd numbers.

Uploaded by

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

Dry Run

The document contains 10 user-defined function programs in Python. Program 4 defines a function to check if a number is within a given range. Program 5 defines a function that prints marks with default values. Program 6 defines a function that calculates the cube of numbers. Program 10 defines a function that prints the value and id of a passed number. The remaining programs define functions for tasks like calculating sum, average, maximum number, area of a circle, and checking even/odd numbers.

Uploaded by

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

User define function programs

Program 1:
Code:
def sum(*numbers):
s=0
for n in numbers:
s += n
return s

total=sum (1,2,3,4)
print(total)
output:
10
Dry run:
Total n s
1 0
2 1
3 3
4 6
Retur 10 x x
n
Program 2:
Code:

def average (x, y):


a= (x + y)/2
return a
result=average (4, 3)
print(result)
output:
3.5
Dry run:
Result X y a

4 3 3.5

Return 3.5

Program 3:
Code:
def max(n1,n2,n3):
m=n1
if n2>m:
m=n2
if n3>m:
m=n3
return(m)
a=int(input("entre 1st number"))
b=int(input("entre 2nd number"))
c=int(input("entre 3rd number"))
d=max(a,b,c)
print(f'{d} is max number')
output:
entre 1st number1
entre 2nd number4
entre 3rd number3
4 is max number
Dry run:
a b c d n1 n2 n3 m
1 4 3 1 4 3 1
4
4 x x x x

Program 4:
Code:
def rag(num,rg):
if num<=rg:
print("number is in given range.")
else:
print("number is not in given range.")
a=int(input("entre number"))
b=int(input("entre rang"))
rag(a,b)
output:
entre number32
entre rang300
number is in given range.
Dry run:
a b num rg

32 300 32 300

Program 5:
Code:
def marks(english, math = 85, science = 80):
print('Marks in : English is - ', english,', Math - ',math, ', Science -
',science)
marks(71, 77)
marks(65, science = 74)
marks(science = 70, math = 90, english = 75)
output:
Marks in: English is - 71 , Math - 77 , Science - 80
Marks in: English is - 65 , Math - 85 , Science - 74
Marks in: English is - 75 , Math - 90 , Science - 70

Dry run:
english math science
71 77 80
65 85 74
75 90 70
Program 6:
Code:
def cub(*num):
cube=[]
num=list(num)
for i in num:
y=i**3
cube.append(y)
return(cube)
a=int(input("entre 1st number"))
b=int(input("entre 2nd number"))
c=int(input("entre 3rd number"))
t=cub(a,b,c)
output:
entre 1st number3
entre 2nd number2
entre 3rd number1
[27, 8, 1]

Dry run:
a b c t num cube i y
3 2 1 (3,2,1) []
[3,2,1] [27] 3 27
[27,8]
[27,8,1]
[27,8,1]

Program 7:
Code:
num=int(input("Enter a number for check odd or even: "))
def find-Evenodd(num):

if(num%2==0):
print(num," Is an even")
else:
print(num," is an odd")
find Even-odd(num)
output:
Enter a number for check odd or even: 2
2 Is an even

Dry run:
num
2

Program 8:
Code:
def sum(a,b):
c = a+b
return c
d = sum(5,6)
print(d)
output:
11
Dry run:
d a b C

5 6 11

return 5 x x x

Program 9:

Code:
def circle(radius):
area=(22/7)*(radius)**2
return area
a=circle(6)
print(a)
output:
113.14285714285714

Dry run:
a radius area

6 113.14285714285714

return 113.14285714285714 x x
Program 10:

Code:
def myFun(x):
print("Value passed:", x, "id:", id(x))
myFun(12)

output:
Value passed: 12 id: 1856537520784

Dry run:
x Id
12 Value passed: 12 id: 1856537520784

Lists and loop programs:


Program 1:
Code:

#print each element of a given list


a=[3,5,6,7]
for i in a:
print(i,end=" ")

output:

3567
Dry run:
a i
[3,5,6,7] 3
5
6
7

Program 2:
Code:
#append:add element at the end of existing list
b=[2,3,6,7]
b.append(0)
for i in b:
print(i,end=" ")

output:
23670
Dry run:
b i
[2,3,6,7] 2
[2,3.6,7,o] 3
6
7
0

Program 3:
Code:
#access list elements and its index
a=[3,5,6,7]
print(f 'index: element')
n=len(a)
for i in range(n):
print(f'{i}: {a[i]}')

output:
index: element
0: 3
1: 5
2: 6
3: 7
Dry run:
a n i a[i]
[3,5,6,7] 4 0 3
1 5
2 6
3 7

Program 4:
Code:
def cub(*num):
cube=[]
num=list(num)
for i in num:
y=i**3
cube.append(y)
return(cube)
a=int(input("entre 1st number"))
b=int(input("entre 2nd number"))
c=int(input("entre 3rd number"))
t=cub(a,b,c)
output:
entre 1st number3
entre 2nd number2
entre 3rd number1
[27, 8, 1]

Dry run:
a b c t num cube i y
3 2 1 (3,2,1) []
[3,2,1] [27] 3 27
[27,8]
[27,8,1]
[27,8,1]

Program 5:
Code:
extend list
#apend element from another list to existing list
q=[1,2,3,4]
p=[5,6,7]
q.extend(p)
print(q)
print(p)

output:
[1, 2, 3, 4, 5, 6, 7]
[5, 6, 7]

Dry run:
p q
[5,6,7] [1,2,3,4]
[1,2,3,4,5,6,7]

You might also like