0% found this document useful (0 votes)
36 views19 pages

Functions (FINAL)

This document contains code snippets and output for 9 problems involving Python functions. The problems cover topics like multiplying elements in a list, reversing a string, checking if a number is prime, calculating factorials, and generating Pascal's triangle.

Uploaded by

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

Functions (FINAL)

This document contains code snippets and output for 9 problems involving Python functions. The problems cover topics like multiplying elements in a list, reversing a string, checking if a number is prime, calculating factorials, and generating Pascal's triangle.

Uploaded by

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

C.

Sc Practical

WORKING WITH FUNCTIONS –


ASSIGNMENT

NAME: Tirush Jindal


CLASS: XII-B
Q1) Write a Python function to multiply all the
numbers list.

CODE :
def mul(x):
length=len(x)
for i in range(0,length-1):
x[i+1]=x[i+1]*x[i]
print("The product of the elements is :",x[i+1])
L=[]
n=int(input("Enter the number of elements :"))
for i in range(n):
e=int(input("Enter the element :"))
L.append(e)
print("The original list is :",L)
mul(L)
OUTPUT:
#2)Write a Python program reverse
string.

CODE :
def reverse(x):
st=""
for i in x:
st=i+st
print(st)
s=input("Enter a string :")
reverse(s)
OUTPUT :
#3) Write a Python function to calculate the
factorial of a number (a non-negative integer). The
function accepts the number as an argument.

CODE :
def fact(f):
f=1
for i in range(1,x+1):
f=f*i
print("The factorial of",x,"is",f)
x=int(input("Enter a number :"))
fact(x)

OUTPUT :
#4 )Write a Python function to check whether a number
is in a given range.

CODE:
def rng(x,y,z):
if z in range(x,y):
print("The number",z,"lies in range between",x,"and",y)
else:
print("The number",z," does not lie in range between",x,"and",y)
a=int(input("Enter the starting range :"))
b=int(input("Enter the ending number :"))
c=int(input("Enter a number :"))
rng(a,b,c)
OUTPUT:
#5 )Write a Python function that takes a list and
returns a new list with unique elements of the first
list.

CODE:

def list(x):
d=[]
for a in x:
if a not in d:
d.append(a)
print("The list with unique elements is :",d)
L=[]
n=int(input("Enter the number of elements:"))
for i in range(n):
e=int(input("Enter the elements: "))
L.append(e)
print("The original list is :",L)
list(L)
OUTPUT:
#6 )Write a Python function that takes a number as a
parameter and check the number is prime or not.

CODE:

def prime(x):
t=False
if(x>1):
for i in range(2,x):
if (x%i==0):
t=True
break
if t==True:
print(x,"is not a prime number.")
else:
print(x,"is a prime number.")
else:
print("Enter a positive number.")
n=int(input("Enter a no. :"))
prime(n)
OUTPUT:
#7)Write a Python function that checks whether a
passed string is palindrome or not.

CODE:
def palindrome(x):
st=""
for i in x:
st=i+st
if s==st:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
s=input("Enter a string :")
palindrome(s)
OUTPUT:
#8)Write a function to calculate the sum of the
digits of a number passed as a parameter.

CODE:

def smdigit(n):
sm=0
a=str(n)
for i in a:
b=int(i)
sm=sm+b
print(sm)
x=int(input("Enter a number :"))
smdigit(x)
OUTPUT:
#9)Write a Python function that prints out the
first n rows of Pascal's triangle. Note :
Pascal's triangle is an arithmetic
and geometric figure first imagined by Blaise
Pascal.

CODE:

def pascaltr(x):
for i in range(x+1):
for j in range(x-i):
print(' ',end=" ")
m=1
for j in range(1,i+1):
print(m," ",sep=" ",end=" ")
m=m*(i-j)//j
print()
n=int(input("Enter No of lines(n):"))
pascaltr(n)
OUTPUT:

You might also like