Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Basic Python Questions

The document contains 6 Python programs: 1) to check if a string is a palindrome, 2) to find the second largest number in a list, 3) to reverse a string, 4) to remove duplicates from a list, 5) to check if a number is prime, and 6) to find the median of a list of numbers. It also contains a 7th program to calculate the mean, median, and mode of an array without using the statistics module. Each program includes the function definition and an example call with output.

Uploaded by

kanishkaa R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Basic Python Questions

The document contains 6 Python programs: 1) to check if a string is a palindrome, 2) to find the second largest number in a list, 3) to reverse a string, 4) to remove duplicates from a list, 5) to check if a number is prime, and 6) to find the median of a list of numbers. It also contains a 7th program to calculate the mean, median, and mode of an array without using the statistics module. Each program includes the function definition and an example call with output.

Uploaded by

kanishkaa R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.Write a Python program to check if a string is a palindrome.

def is_palindrome(string):

reversed_str = string[::-1]

if string == reversed_str:

return True

else:

return False

print(is_palindrome("radar"))

# Output: True

2. Write a Python program to find the second largest number in a list.

def find_second_largest(numbers):

largest = float('-inf')

second_largest = float('-inf')

for num in numbers:

if num > largest:

second_largest = largest

largest = num

elif num > second_largest and num != largest:

second_largest = num

return second_largest
nums = [5, 10, 2, 8, 3]

print(find_second_largest(nums))

# Output: 8

3. Write a Python program to reverse a string.

def reverse_string(string):

return string[::-1]

print(reverse_string("Hello, World!"))

# Output: "!dlroW ,olleH"

4. Write a Python program to remove duplicates from a list.

def remove_duplicates(numbers):

return list(set(numbers))

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

print(remove_duplicates(nums))

# Output: [1, 2, 3, 4, 5]
5.Write a Python program to check if a number is prime.

def is_prime(n):

if n <= 1:

return False

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

if n % i == 0:

return False

return True

print(is_prime(13))

# Output: True

5. Write a Python program to find the median of a list of numbers.


def find_median(numbers):

sorted_nums = sorted(numbers)

length = len(sorted_nums)

if length % 2 == 0:

return (sorted_nums[length//2 - 1] + sorted_nums[length//2]) / 2

else:

return sorted_nums[length//2]

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

print(find_median(nums))

# Output: 3
6. Write a Python program to calculate the mean, median, and mode
of a given array without using the statistics module.
def calculate_mean(arr):

return sum(arr) / len(arr)

def calculate_median(arr):

sorted_arr = sorted(arr)

length = len(sorted_arr)

if length % 2 == 0:

mid1 = sorted_arr[length // 2]

mid2 = sorted_arr[length // 2 - 1]

median = (mid1 + mid2) / 2

else:

median = sorted_arr[length // 2]

return median

def calculate_mode(arr):

counts = {}

for num in arr:

if num in counts:

counts[num] += 1

else:

counts[num] = 1
max_count = max(counts.values())

mode = [num for num, count in counts.items() if count == max_count]

return mode

array = [1, 2, 3, 4, 5, 5, 6, 7, 8, 9]

mean = calculate_mean(array)

median = calculate_median(array)

mode = calculate_mode(array)

print("Mean:", mean)

print("Median:", median)

print("Mode:", mode)

Output:

Mean: 5.0

Median: 5.0

Mode: [5]

You might also like