assignment function solutions
assignment function solutions
assignment function solutions
ASSIGNMENT-
FUNCTIONS
# Write a Python function to multiply all the numbers
in a list.SAMple List : (8, 2, 3, -1, 7)
Expected Output : -336
def multiply(numbers):
total = 1
for x in numbers:
total *= x
return total
print(multiply((8, 2, 3, -1, 7)))
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(n))
# Write a Python function to check whether a
number is in a given range.
def testingrange(n):
if n in range(3,9):
else :
testingrange(5)
# Write a Python function that accepts a string and
calculate the number of upper case letters and lower case
letters. Sample String : 'The quick Brow Fox'
Expected Output :
No. of Upper case characters : 3
No. of Lower case Characters : 12
def stringtesting(s):
k={"UPPER_CASE":0, "LOWER_CASE":0}
for c in s:
if c.isupper():
k["UPPER_CASE"]+=1
elif c.islower():
k["LOWER_CASE"]+=1
else:
pass
print ("Original String : ", s)
print ("No. of Upper case characters : ", k["UPPER_CASE"])
print ("No. of Lower case Characters : ", k["LOWER_CASE"])
print(unique_list([1,2,3,3,3,3,4,5]))
# Write a Python function that takes a number as a
parameter and check the number is prime or not.
def testprimenumber(n):
if (n==1):
return False
elif (n==2):
return True;
else:
for x in range(2,n):
if(n % x==0):
return False
return True
print(testprimenumber(11))
# Write a Python function that checks whether a passed
string is palindrome or not. Note: A palindrome is a word,
phrase, or sequence that reads the same backward as
forward, e.g., madam or nurses run.
def Palindrome(string):
left= 0
right= len(string) - 1
while right>=left:
if not string[left] == string[right]:
return False
left+=1
right-=1
return True
print(Palindrome('madam'))
# 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.
def pascaltriangle(n):
row = [1]
y = [0]
for x in range(max(n,0)):
print(row)
row=[l+r for l,r in zip(row+y, y+row)]
return n>=1
pascaltriangle(6)
DIFFERENCE BETWEEN FORMAL PARAMETERS AND ACTUAL
PARAMETERS=
formal parameter — the identifier used in a method to stand for
the value that is passed into the method by a caller.
actual parameter — the actual value that is passed into the method
by a caller.
def addition(x, y) #X AND Y ARE FORMAL PARAMETERS
addition = x+y
print(f”{addition}”)
addition(2, 3) #2 AND 3 ARE ACTUAL PARAMETERS
addition(4, 5) #4 AND 5 ARE ALSO ACTUAL PARAMETRS