Programming Examples
Programming Examples
• Program 1:
• words = ["hello", "world", "python"]
• result = "-".join([word.upper() if i % 2 == 0 else
word for i, word in enumerate(words)])
• words[0] = "goodbye"
• print(result)
• print(words)
Answer
• Output:
• HELLO-world-PYTHON
• goodbye, world, python
Question
• Program 2:
• numbers = [1, 2, 3, 4, 5]
• squares = [x**2 for x in numbers if x % 2 == 0]
• print(squares)
Answer
• Output:
• [4, 16]
Question
• Program 3:
• def add(a, b=5):
• return a + b
• print(add(3))
• print(add(3, 10))
Answer
• Output:
• 8
• 13
Question
• Program 4:
• my_dict = {'a': 1, 'b': 2, 'c': 3}
• result = {k: v**2 for k, v in my_dict.items() if v
% 2 == 1}
• print(result)
Answer
• Output:
• {'a': 1, 'c': 9}
Question
• Program 5:
• text = "hello world"
• char_count = {char: text.count(char) for char
in set(text) if char != " "}
• print(char_count)
Answer
• Output:
• {'h': 1, 'e': 1, 'l': 3, 'o': 2, 'w': 1, 'r': 1, 'd': 1}
Question
• Program 6:
• def factorial(n):
• return 1 if n == 0 else n * factorial(n-1)
• print(factorial(5))
Answer
• Output:
• 120
Question
• Program 7:
• names = ["Alice", "Bob", "Charlie"]
• result = [name[::-1] for name in names]
• print(result)
Answer
• Output:
• ['ecilA', 'boB', 'eilrahC']
Question
• Program 8:
• from math import sqrt
• numbers = [4, 9, 16, 25]
• sqrts = list(map(sqrt, numbers))
• print(sqrts)
Answer
• Output:
• [2.0, 3.0, 4.0, 5.0]
Question
• Program 9:
• def is_palindrome(word):
• return word == word[::-1]
• print(is_palindrome("radar"))
• print(is_palindrome("python"))
Answer
• Output:
• True
• False
Question
• Program 10:
• a, b = 0, 1
• fib = []
• for _ in range(5):
• fib.append(a)
• a, b = b, a + b
• print(fib)
Answer
• Output:
• [0, 1, 1, 2, 3]
Question
• Program 11:
• def greet(name="Guest"):
• return f"Hello, {name}!"
• print(greet())
• print(greet("Alice"))
Answer
• Output:
• Hello, Guest!
• Hello, Alice!
Question
• Program 12:
• numbers = [10, 20, 30, 40]
• doubled = list(map(lambda x: x * 2, numbers))
• print(doubled)
Answer
• Output:
• [20, 40, 60, 80]
Question
• Program 13:
• text = "hello python"
• vowels = [char for char in text if char in
"aeiou"]
• print(vowels)
Answer
• Output:
• ['e', 'o', 'o']
Question
• Program 14:
• def sum_of_digits(n):
• return sum(int(digit) for digit in str(n))
• print(sum_of_digits(12345))
Answer
• Output:
• 15
Question
• Program 15:
• num = 7
• result = "Prime" if all(num % i != 0 for i in
range(2, int(num**0.5) + 1)) else "Not Prime"
• print(result)
Answer
• Output:
• Prime
Question
• Program 16:
• numbers = [1, 2, 3, 4, 5]
• evens = [n for n in numbers if n % 2 == 0]
• print(evens)
Answer
• Output:
• [2, 4]
Question
• Program 17:
• def reverse_words(sentence):
• return " ".join(word[::-1] for word in
sentence.split())
• print(reverse_words("hello world"))
Answer
• Output:
• 'olleh dlrow'
Question
• Program 18:
• def fibonacci(n):
• a, b = 0, 1
• for _ in range(n):
• yield a
• a, b = b, a + b
• print(list(fibonacci(5)))
Answer
• Output:
• [0, 1, 1, 2, 3]
Question
• Program 19:
• numbers = [5, 3, 8, 6]
• sorted_numbers = sorted(numbers,
reverse=True)
• print(sorted_numbers)
Answer
• Output:
• [8, 6, 5, 3]
Question
• Program 20:
• def flatten_list(lst):
• return [item for sublist in lst for item in
sublist]