Introduction Application Questions
Explain how Python's simplicity makes it suitable for beginners compared to languages like C++.
Solution: Python's syntax is clean and uses English-like keywords, reducing the learning curve. Unlike C++ which requires managing memory and complex syntax, Python handles many low-level details automatically.
Basics Application Questions
Write a program to check if a number is even or odd using if-else.
Solution:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
Intermediate Application Questions
Create a program that reads a text file and counts the number of words.
Solution:
with open('sample.txt', 'r') as f:
content = f.read()
words = content.split()
print(f"Number of words: {len(words)}")
Advanced Application Questions
Implement a simple class for a Bank Account with deposit and withdraw methods.
Solution:
class BankAccount:
def __init__(self, balance=0):
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
else:
raise ValueError("Insufficient funds")
account = BankAccount(100)
account.deposit(50)
account.withdraw(30)
print(account.balance) # 120