2. 1. Write a simple Python program to print an output.
print("Hello, World!")
3. 2. Write a Python code to check if a number is even or odd
def is_even(num):
return num % 2 == 0
print(is_even(4)) # True
print(is_even(5)) # False
4. Write a Python program to count the number of vowels in a string
def count_vowels(s):
return sum(1 for char in s if char.lower() in
'aeiou')
print(count_vowels("Hello World")) # 3
5. Write a Python code to convert a string to an integer
str_num = "12345"
int_num = int(str_num)
print(int_num) # 12345
7. Write a Python program to find common elements in two lists
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
common = list(set(list1) & set(list2))
print(common) #[3, 4]
8. Write a Python code to remove duplicates from a list
list1 = [1, 2, 2, 3, 4, 4]
unique_list1 = list(set(list1))
print(unique_list1) #[1, 2, 3, 4]
9. Write a Python program to find the longest word in a sentence
def longest_word(sentence):
words = sentence.split()
return max(words, key=len)
print(longest_word("The fox jumps over the
lazy dog")) # jumps
10. Write a Python code to find the first non-repeating character in a
string
def first_non_repeating_char(s):
char_count = {}
for char in s:
char_count[char] = char_count.get(char, 0) + 1
for char in s:
if char_count[char] == 1:
return char
return None
print(first_non_repeating_char("nxtwave")) # n
11. Write a Python code to implement a binary search algorithm
def binary_search(arr, target):
low, high = 0, len(arr) – 1
while low <= high:
mid = (low + high) // 2
if arr[mid] < target:
low = mid + 1
elif arr[mid] > target:
high = mid – 1
else:
return mid
return -1
print(binary_search([1, 2, 3, 4, 5], 3)) # 2
12. Write a Python code to implement a function to flatten a nested
list
def flatten(nested_list):
flat_list = []
for item in nested_list:
if isinstance(item, list):
flat_list.extend(flatten(item))
else:
flat_list.append(item)
return flat_list
print(flatten([1, [2, [3, 4], 5], 6])) # [1, 2, 3, 4, 5, 6]
13. Write a Python code to find the maximum subarray sum
def max_subarray_sum(arr):
max_sum = current_sum = arr[0]
for num in arr[1:]:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum
print(max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5,
4])) # 6
14. Write a Python program to find the intersection of two sets
def intersection(set1, set2):
return set1 & set2
print(intersection({1, 2, 3}, {2, 3, 4})) # {2, 3}
15. Write a Python code to implement a simple calculator
def calculator(a, b, operation):
if operation == 'add':
return a + b
elif operation == 'subtract':
return a - b
elif operation == 'multiply':
return a * b
elif operation == 'divide':
return a / b if b != 0 else "Cannot divide by zero"
else:
return "Invalid operation"
print(calculator(5, 3, 'add')) # 8
16. Write a Python code to find the GCD of two numbers
def gcd(a, b):
while b:
a, b = b, a % b
return a
print(gcd(48, 18)) # 6
17. Write a Python program to find the largest
element in a list.
def find_largest(numbers):
largest = numbers[0]
for num in numbers:
if num > largest:
largest = num
return largest
# Test the function
nums = [10, 5, 8, 20, 3]
largest_num = find_largest(nums)
print(f"The largest number is {largest_num}")
18. Write a Python program to reverse a string.
def reverse_string(string):
return string[::-1]
# Test the function
text = "Hello, World!"
reversed_text = reverse_string(text)
print(reversed_text)
19. Write a Python program to count the
frequency of each element in a list.
def count_frequency(numbers):
frequency = {}
for num in numbers:
if num in frequency:
frequency[num] += 1
else:
frequency[num] = 1
return frequency
# Test the function
nums = [1, 2, 3, 2, 1, 3, 2, 4, 5, 4]
frequency_count = count_frequency(nums)
print(frequency_count)
20. Write a Python program to check if a
number is prime.
def is_prime(number):
if number < 2:
return False
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False
return True
# Test the function
num = 17
if is_prime(num):
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")
21. Write a Python program to find the common
elements between two lists.
def find_common_elements(list1, list2):
common_elements = []
for item in list1:
if item in list2:
common_elements.append(item)
return common_elements
# Test the function
list_a = [1, 2, 3, 4, 5]
list_b = [4, 5, 6, 7, 8]
common = find_common_elements(list_a, list_b)
print(common)
22. Write a Python program to sort a list of
elements using the bubble sort algorithm.
def bubble_sort(elements):
n = len(elements)
for i in range(n - 1):
for j in range(n - i - 1):
if elements[j] > elements[j + 1]:
elements[j], elements[j + 1] = elements[j + 1], elements[j]
# Test the function
nums = [5, 2, 8, 1, 9]
bubble_sort(nums)
print(nums)
23. 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
# Test the function
nums = [10, 5, 8, 20, 3]
second_largest_num = find_second_largest(nums)
print(f"The second largest number is {second_largest_num}")
24. Write a Python program to remove
duplicates from a list.
def remove_duplicates(numbers):
unique_numbers = []
for num in numbers:
if num not in unique_numbers:
unique_numbers.append(num)
return unique_numbers
# Test the function
nums = [1, 2, 3, 2, 1, 3, 2, 4, 5, 4]
unique_nums = remove_duplicates(nums)
print(unique_nums)
25. Program to find the sum of digits of a
given number in Python
def sum_digits(num):
Sum = 0
for digit in str(num):
sum = sum + int(digit)
return "Sum of digits :",sum
sum_digits(int(input("Enter Number :")))
26. Tell the output of the following
code:
import array
a = [4, 6, 8, 3, 1, 7]
print(a[-3])
print(a[-5])
print(a[-1])
Output:
3 6 7
27. How do you print the summation of all the numbers from 1 to
101?
print(sum(range(1, 102)))
Output:
5151