You are on page 1of 16

Write a Python function to multiply all the numbers in a list.

SAMple List : (8, 2, 3, -1, 7) Expected


Output : -336

def multiply_list(numbers):

result = 1

for num in numbers:

result *= num

return result

sample_list = [8, 2, 3, -1, 7]

output = multiply_list(sample_list)

print("Expected Output:", output)


Expected Output: -336
Write a Python program to reverse a string. Sample String : "1234abcd" Expected Output :
"dcba4321"

def reverse_string(input_string):

return input_string[::-1]

sample_string = "1234abcd"

reversed_string = reverse_string(sample_string)

print("Expected Output:", reversed_string)


Expected Output: dcba4321
Write a Python function to calculate the factorial of a number (a non-negative integer). The function
accepts the number as an argument.

def factorial(n):

if n == 0:

return 1

else:

return n * factorial(n - 1)

n = 5 # Replace with the non-negative integer for which you want to calculate the factorial

result = factorial(n)

print(f"The factorial of {n} is {result}")


The factorial of 5 is 120
Write a Python function to check whether a number is in a given range

def is_in_range(number, start, end):

if start <= number <= end:

return True

else:

return False
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 count_upper_lower(string):

upper_count = 0

lower_count = 0

for char in string:

if char.isupper():

upper_count += 1

elif char.islower():

lower_count += 1

return upper_count, lower_count

sample_string = 'The quick Brow Fox'

upper, lower = count_upper_lower(sample_string)

print("No. of Upper case characters:", upper)

print("No. of Lower case characters:", lower)


No. of Upper case characters: 3

No. of Lower case characters: 12


Write a Python function that takes a list and returns a new list with unique elements of the first list.
Sample List : [1,2,3,3,3,3,4,5] Unique List : [1, 2, 3, 4, 5]

def unique_elements(input_list):

unique_list = []

for item in input_list:

if item not in unique_list:

unique_list.append(item)

return unique_list

sample_list = [1, 2, 3, 3, 3, 3, 4, 5]

result = unique_elements(sample_list)

print("Unique List:", result)


Unique List: [1, 2, 3, 4, 5]
Write a Python function that takes a number as a parameter and check the number is prime or not

def is_prime(number):

if number <= 1:

return False

for i in range(2, int(number**0.5) + 1):

if number % i == 0:

return False

return True
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 Pal(string):

left_pos = 0

right_pos = len(string) - 1

while right_pos >= left_pos:

if not string[left_pos] == string[right_pos]:

return False

left_pos += 1

right_pos -= 1

return True

print(Pal('racecar'))
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 print_pascals_triangle(n):

for i in range(n):

number = 1

for j in range(n - i):

print(" ", end=" ")

for j in range(i + 1):

print(number, end=" ")

number = number * (i - j) // (j + 1)

print()

n = 5 # Replace with the number of rows you want to print

print_pascals_triangle(n)
1

11

121

1331

14641

You might also like