0% found this document useful (0 votes)
13 views4 pages

Python Programming Questions & Solutions

The document contains a series of Python programming questions along with their solutions, covering topics such as basic arithmetic operations, counting odd/even numbers, finding the largest of three numbers, checking for prime numbers, and more. Each question is followed by a code snippet that demonstrates the solution. Additionally, there are tips for running the code and suggestions for improving practices like input validation.

Uploaded by

mdemamuddin554
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Python Programming Questions & Solutions

The document contains a series of Python programming questions along with their solutions, covering topics such as basic arithmetic operations, counting odd/even numbers, finding the largest of three numbers, checking for prime numbers, and more. Each question is followed by a code snippet that demonstrates the solution. Additionally, there are tips for running the code and suggestions for improving practices like input validation.

Uploaded by

mdemamuddin554
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Programming Questions (With Answers)

1. Calculator
Question:
User se 2 numbers aur ek operator (+, -, *, /) lo. Correct result print karo. Invalid
operator ho to error message do.

Solution (code):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
op = input("Enter operator (+, -, *, /): ").strip()
if op == "+":
print(num1 + num2)
elif op == "-":
print(num1 - num2)
elif op == "*":
print(num1 * num2)
elif op == "/":
if num2 == 0:
print("Error: Division by zero")
else:
print(num1 / num2)
else:
print("Invalid operator")
---

2. Odd/Even Counter
Question:
User se ek number lo. 1 se us number tak kitne odd aur even numbers hain, count karke
print karo.

Solution (code):
n = int(input("Enter a number: "))
even = n // 2
odd = n - even
print(f"Even: {even}, Odd: {odd}")
# Alternative loop method:
# even = odd = 0
# for i in range(1, n+1):
# if i % 2 == 0: even += 1
# else: odd += 1
# print(even, odd)
---

3. Largest of Three
Question:
Teen numbers input lo. Bina max() use kiye sabse bada number find karo.

Solution (code):
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
largest = a
if b > largest:
largest = b
if c > largest:
largest = c
print("Largest:", largest)
---

4. Prime Number Checker


Question:
Ek number lo aur batao prime hai ya nahi.

Solution (code):
n = int(input("Enter a number: "))
if n <= 1:
print("Not prime")
else:
is_prime = True
i = 2
while i * i <= n:
if n % i == 0:
is_prime = False
break
i += 1
print("Prime" if is_prime else "Not prime")
---

5. Word Frequency
Question:
User se ek sentence lo. Kaun sa word kitni baar aya hai dictionary me store karke
print karo.

Solution (code):
sentence = input("Enter a sentence: ").strip().lower()
words = [Link]()
freq = {}
for w in words:
freq[w] = [Link](w, 0) + 1
print(freq)
---

6. List Sorting (without sort())


Question:
User se 10 numbers lo. Bina sort() use kiye ascending order me sort karo.

Solution (code):
nums = []
for _ in range(10):
[Link](int(input("Enter number: ")))
# Simple bubble sort
n = len(nums)
for i in range(n):
for j in range(0, n-i-1):
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
print("Sorted:", nums)
---

7. Palindrome Checker
Question:
User se ek word lo aur check karo palindrome hai ya nahi.

Solution (code):
s = input("Enter a word: ").strip()
if s == s[::-1]:
print("Palindrome")
else:
print("Not palindrome")
---

8. Reverse String Without Slicing


Question:
User se ek string lo aur loop use karke reverse print karo.

Solution (code):
s = input("Enter a string: ")
rev = ""
for ch in s:
rev = ch + rev
print("Reversed:", rev)
---
9. Factorial Using Loop
Question:
User se number lo. Uska factorial loop se calculate karo.

Solution (code):
n = int(input("Enter a non-negative integer: "))
if n < 0:
print("Invalid input")
else:
fact = 1
for i in range(1, n+1):
fact *= i
print(f"Factorial of {n} is {fact}")
---

10. Unique Items


Question:
User se 10 inputs lo. Duplicate items hatao aur sirf unique values print karo.
Solution (code):
items = []
for _ in range(10):
[Link](input("Enter item: ").strip())
unique = list([Link](items)) # preserves order
print("Unique items:", unique)

Tips:
- Yeh code snippets interactive console ke liye hain. Agar tum IDE me run kar rahe ho
to input prompts ka dhyan rakho.
- Exceptions aur input validation add karna achha practice hai (e.g., numbers ke liye
try/except).
- Agar chaho, inhe functions me wrap karke ek single file bana ke deta hoon.

You might also like