BASIC PROGRAMMING QUESTIONS
Q1. Find Even and Odd Numbers from a List
numbers = [10, 21, 4, 45, 66, 93, 11]
even = []
odd = []
for num in numbers:
if num % 2 == 0:
[Link](num)
else:
[Link](num)
print("Even numbers:", even)
print("Odd numbers:", odd)
Q2. Write a Python program to count how many vowels (a, e, i, o, u) are in a given string.
text = "Programming in Python"
vowels = set("aeiouAEIOU")
count = 0
for ch in text:
if ch in vowels:
count += 1
print("Number of vowels:", count)
Q3. Write a Python program to print elements that occur only once in a list.
numbers = [1, 2, 3, 2, 4, 5, 1, 6]
unique = []
for num in numbers:
if [Link](num) == 1:
[Link](num)
print("Unique elements:", unique)
Q4. Write a program to find the second largest number from a list.
numbers = [23, 1, 45, 67, 32, 89, 10]
[Link]()
print("Second largest element:", numbers[-2])
Q5. Menu Driven Program on Sets
Create two sets. Show a menu to the user:
1. Union
2. Intersection
3. Difference
Perform the operation based on user’s choice.
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
print("1. Union")
print("2. Intersection")
print("3. Difference")
choice = int(input("Enter choice: "))
if choice == 1:
print("Union:", set1 | set2)
elif choice == 2:
print("Intersection:", set1 & set2)
elif choice == 3:
print("Difference (set1 - set2):", set1 - set2)
else:
print("Invalid choice")
Q6. Sum of All Numbers in a List
numbers = [5, 10, 15, 20]
total = 0
for num in numbers:
total += num
print("Sum:", total)
Q7. Write a program to count how many even numbers are present in a tuple.
numbers = (2, 7, 8, 11, 14, 20, 23)
count = 0
for num in numbers:
if num % 2 == 0:
count += 1
print("Count of even numbers:", count)
Q8. Write a program to take an element from the user and print its index in a tuple (if it exists).
numbers = (10, 20, 30, 40, 50)
numbers = (10, 20, 30, 40, 50)
x = int(input("Enter a number: "))
if x in numbers:
print("Index of", x, "is", [Link](x))
else:
print(x, " not found in tuple")
User Input in Python
1. Basic Input
name = input("Enter your name: ")
print("Hello,", name) Whatever the user types is taken as a string.
2. Numeric Input (int, float)
age = int(input("Enter your age: ")) # converting to integer
height = float(input("Enter your height in meters: ")) # converting to float
print("Age:", age)
print("Height:", height) Always convert to int or float if you need numbers.
3. Multiple Inputs in One Line
x, y = input("Enter two numbers separated by space: ").split()
print("x =", x, "y =", y) Default result is string. Convert if needed:
4. Converting using Map()
x, y = map(int, input("Enter two numbers separated by space: ").split())
print("Sum =", x + y)
Working:
input("Enter two numbers separated by space: ")
Suppose user types: 10 20 This is read as a single string: "10 20"
"10 20".split() split() breaks the string into a list using space as the default separator.
Result: ["10", "20"] (list of strings).
map(int, ["10", "20"])
map(function, iterable) applies the function to each element of the iterable.
Here, int is the function, ["10", "20"] is the iterable.
So it converts each string into integer:
→ [10, 20]
x, y = ...
This is unpacking.
x gets first value → 10
y gets second value → 20
print("Sum =", x + y)
Adds the two integers.
Output: 30
Map() Function
Definition of map()
In Python, the map() function is used to apply a given function to each item of an iterable
(like list, tuple, string, set) and return a new map object (which is an iterator).
We usually convert it into a list, tuple, or set.
map(function, iterable, ...)
function → The function to apply (can be built-in or user-defined).
iterable → One or more iterables (list, tuple, set, etc.).
Returns → a map object (iterator), which can be converted into a list/tuple.
Example:
numbers = list(map(int, input("Enter numbers separated by space: ").split()))
print("List of numbers:", numbers)
Suppose input: 10 20 30
split() → ["10", "20", "30"]
map(int, ...) → [10, 20, 30]
Output:
List of numbers: [10, 20, 30]
Example 2: With float
numbers = list(map(float, input("Enter decimal numbers: ").split()))
print(numbers)
Input: 2.5 3.6 7.8
Output: [2.5, 3.6, 7.8]
Example 3: With [Link]
words = list(map([Link], ["hello", "python"]))
print(words)
Output: ['HELLO', 'PYTHON']
Example 4: With len
lengths = list(map(len, ["apple", "banana", "kiwi"]))
print(lengths)
Output: [5, 6, 4]
Example 5 : Using with Built-in Functions
nums = ["10", "20", "30"]
converted = list(map(int, nums))
print(converted)
Output:
[10, 20, 30]