Module 1 (2 Marks)
1. Styles of comments in Python
Python supports:
● Single-line comments using #
● Multi-line comments using triple quotes ''' ''' or """ """
Example:
# Single-line comment
''' Multi-line
comment '''
2. Difference between is and ==
● == checks value equality
● is checks object identity (same memory location)
Example:
a = [1,2]
b = [1,2]
print(a == b) # True
print(a is b) # False
3. Purpose of range() function
range() generates a sequence of numbers.
Example:
for i in range(5):
print(i)
Output:
01234
4. Output of the code
Code:
a = [1, 3, 5, 7, 9, 11]
val = 7
x=1
for i in a:
if i == val:
print("Found at", x, "iteration")
break
x += 1
else:
print("not found")
Output:
Found at 4 iteration
5. Define indentation in Python
Indentation means giving spaces/tabs before statements to define blocks of code.
Example:
if 5 > 2:
print("Hello")
6. Display odd numbers from 1 to n
n = int(input("Enter n: "))
for i in range(1, n+1, 2):
print(i)
7. Dynamic typing in Python
In Python, variable type is decided automatically during runtime.
Example:
x = 10
x = "Python"
print(x)
8. Multiple variable declaration in single line
a, b, c = 10, 20, 30
print(a, b, c)
9. Error correction
Incorrect:
msg = 'She said "Good Morning" to me''
print(msg)
Correct:
msg = 'She said "Good Morning" to me'
print(msg)
10. Difference Between input() and print() Functions in
Python
input() print()
Used to take input from the Used to display output to the
user screen
Returns data as string by Displays values or messages
default
Example: name = input() Example: print(name)
Essay Question (8Marks)
Python Program Answers
Essay Answer Questions
1. Disaster Relief Fund Program
# Disaster Relief Fund Program
house_type = input("Enter type of damage (F/P): ")
income = int(input("Enter annual income: "))
relief = 0
if house_type == 'F' or house_type == 'f':
if income < 300000:
relief = 200000
else:
relief = 100000
elif house_type == 'P' or house_type == 'p':
if income < 300000:
relief = 100000
else:
relief = 50000
else:
print("Invalid damage type")
print("Relief Amount = Rs.", relief)
2. Library Fine Calculation Program
# Library Fine Program
book_type = input("Enter book type (r/g/c): ")
days = int(input("Enter number of delayed days: "))
fine = 0
if book_type == 'r' or book_type == 'R':
fine = days * 20
elif book_type == 'g' or book_type == 'G':
fine = days * 15
elif book_type == 'c' or book_type == 'C':
fine = days * 10
else:
print("Invalid book type")
print("Total fine amount = Rs.", fine)
3. Loan Application Processing Program
# Loan Application Processing
n = int(input("Enter number of applicants: "))
eligible = 0
rejected = 0
for i in range(n):
print("\nApplicant", i + 1)
name = input("Enter applicant name: ")
age = int(input("Enter age: "))
income = int(input("Enter annual income: "))
credit_score = int(input("Enter credit score: "))
if age >= 21 and age <= 60 and income >= 300000 and credit_score >= 650:
loan_amount = income * 3
print("Applicant Eligible for Loan")
print("Loan Amount = Rs.", loan_amount)
eligible += 1
else:
print("Applicant Rejected")
rejected += 1
print("\nTotal Eligible Applicants:", eligible)
print("Total Rejected Applicants:", rejected)
4. Program to Check Whether a Number is Prime
import math
n = int(input("Enter a number: "))
if n <= 1:
print(n, "is not a prime number")
else:
prime = True
for i in range(2, int([Link](n)) + 1):
if n % i == 0:
prime = False
break
if prime:
print(n, "is a prime number")
else:
print(n, "is not a prime number")
5. String Operations Program
string = input("Enter a string: ")
vowels = 0
consonants = 0
for ch in string:
if [Link]():
if [Link]() in 'aeiou':
vowels += 1
else:
consonants += 1
print("Number of vowels:", vowels)
print("Number of consonants:", consonants)
# Reverse string
reverse_string = string[::-1]
print("Reversed string:", reverse_string)
# Palindrome check
if string == reverse_string:
print("The string is a palindrome")
else:
print("The string is not a palindrome")
6. Program to Perform Operations on Numbers from 1
to n
n = int(input("Enter a number: "))
count = 0
sum_numbers = 0
print("Numbers from 1 to", n)
for i in range(1, n + 1):
print(i, end=" ")
print("\nNumbers divisible by both 3 and 5:")
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
print(i, end=" ")
count += 1
sum_numbers += i
print("\nCount:", count)
print("Sum:", sum_numbers)