A. M.
NAIK SCHOOL, POWAI
ICSE and ISC
Grade 9 SUBJECT: Robotics & AI
Chapter: Programming in Python
1) General Output Programs
Q1. WAP to display the given message:
print("Welcome to Python")
print("May the good God bless You")
Q2. WAP to print your name, class, and division.
print("Name: Komal")
print("Class: 9")
print("Division: A")
Q3. Write a program to display your details in the following format using \t:
Name Class Division
Anil 9 A
print("Name\tClass\tDivision")
print("Anil\t9\tA")
Q4. WAP to print your name, class, division in the given formats using separate print
statements:
Name Class Division
Anil 9 A
print("Name", end=" ")
print("Class", end="\t")
print("Division")
print("Anil", end=" ")
print("9", end="\t")
print("A")
2) Assigning value & Input Programs
Q1. Assign values (Name=Anil, Class=9, Div=A, Marks=375.5) and print.
name = "Anil"
cls = 9
div = "A"
marks = 375.5
print("Name:", name)
print("Class:", cls)
print("Division:", div)
print("Marks:", marks)
Q2. Find sum, product, quotient, remainder (a=50, b=10).
a = 50
1
b = 10
print("Sum =", a+b)
print("Product =", a*b)
print("Quotient =", a//b)
print("Remainder =", a%b)
Q3. Calculate DA, HRA, CTA, Gross Salary (basic=20000).
DA = 20% of basic
HRA = 10% of basic
CTA = 12% of basic
Gross Salary = basic + CTA + HRA + DA.
basic = 20000
DA = 0.2 * basic
HRA = 0.1 * basic
CTA = 0.12 * basic
gross = basic + DA + HRA + CTA
print("DA =", DA)
print("HRA =", HRA)
print("CTA =", CTA)
print("Gross Salary =", gross)
Q4. Area, perimeter, of rectangle (l=24, b=25).
l, b = 24, 25
area = l * b
peri = 2 * (l+b)
print("Area =", area)
print("Perimeter =", peri)
Q5. Input name, class, division and marks.
name = input("Enter name: ")
cls = int(input("Enter class: "))
div = input("Enter division: ")
marks = float(input("Enter total marks: "))
print("Name:", name)
print("Class:", cls)
print("Division:", div)
print("Marks:", marks)
Q6. Input two numbers and find sum, product, quotient, remainder.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum =", a+b)
print("Product =", a*b)
print("Quotient =", a//b)
print("Remainder =", a%b)
Q7. Salary calculation with input.
basic = float(input("Enter basic salary: "))
DA = 0.2 * basic
HRA = 0.1 * basic
CTA = 0.12 * basic
gross = basic + DA + HRA + CTA
print("DA =", DA)
print("HRA =", HRA)
print("CTA =", CTA)
print("Gross Salary =", gross)
Q8. Rectangle area, perimeter, diagonal with input.
l = float(input("Enter length: "))
2
b = float(input("Enter breadth: "))
area = l * b
peri = 2*(l+b)
print("Area =", area)
print("Perimeter =", peri)
3) Conditional Statement Programs
Q1. WAP to check whether a number is positive, negative, or zero.
n = int(input("Enter a number: "))
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")
Q2. WAP to check whether a number is even or odd.
n = int(input("Enter a number: "))
if n % 2 == 0:
print("Even")
else:
print("Odd")
Q3. WAP to check the type of angle (acute, right, obtuse).
angle = int(input("Enter angle in degrees: "))
if angle == 90:
print("Right Angle")
elif angle < 90:
print("Acute Angle")
else:
print("Obtuse Angle")
Q4. WAP to check whether a year is leap year or not.
year = int(input("Enter year: "))
if (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0):
print("Leap Year")
else:
print("Not Leap Year")
Q5. WAP to check type of triangle from sides.
a = int(input("Enter first side: "))
b = int(input("Enter second side: "))
c = int(input("Enter third side: "))
if a == b == c:
print("Equilateral Triangle")
elif a == b or b == c or a == c:
print("Isosceles Triangle")
else:
print("Scalene Triangle")
Q6. WAP to check if two angles are complementary or supplementary.
a = int(input("Enter first angle: "))
b = int(input("Enter second angle: "))
if a + b == 90:
3
print("Complementary Angles")
elif a + b == 180:
print("Supplementary Angles")
else:
print("Neither complementary nor supplementary")
Q7. Input any number between 1 and 10 and print with proper suffix (e.g., 1st, 3rd, 10th).
n=int(input("Enter a number (1-10): "))
if n==1:
print("1st")
elif n==2:
print("2nd")
elif n==3:
print("3rd")
else:
print(str(n)+"th")
Q8. Input a character and print whether it is a vowel or consonant.
ch=input("Enter a character: ")
if ch=="a" or ch=="e" or ch=="i" or ch=="o" or ch=="u" or ch=="A" or ch=="E" or ch=="I" or
ch=="O" or ch=="U":
print(ch,"is a Vowel")
else:
print(ch,"is a Consonant")
Q9. Input a character and print whether digit, uppercase, lowercase, or special character.
ch=input("Enter a character: ")
if ch>="0" and ch<="9":
print("Digit")
elif ch>="A" and ch<="Z":
print("Uppercase letter")
elif ch>="a" and ch<="z":
print("Lowercase letter")
else:
print("Special character")
Q10. Write a program to calculate the monthly electricity bill of the consumers according
to the units consumed (per month) as per the given tariff:
Units Consumed Charges
Up to 100 units Rs. 1.80/unit
101–200 units Rs. 2.10/unit
Above 200 units Rs. 2.50/unit
Python Program:
units = int(input("Enter units consumed: "))
if units <= 100:
bill = units * 1.80
elif units <= 200:
4
bill = (units* 2.10)
else:
bill = (units - 2.50)
print("Electricity Bill = Rs.", bill)
Q11. A shopkeeper offers “Puja Discount” to his/her customer according to the goods
purchased as per given amount.
Goods Purchased Discount
Up to Rs. 1000 5%
Rs. 1001–2000 10%
Rs. 2001–3000 15%
Above Rs. 3000 20%
Write a program to calculate the discount for purchase of the goods, by customers.
Python Program:
amount = float(input("Enter purchase amount: "))
if amount <= 1000:
discount = amount * 0.05
elif amount <= 2000:
discount = amount * 0.10
elif amount <= 3000:
discount = amount * 0.15
else:
discount = amount * 0.20
print("Discount =", discount)
print("Amount to be paid =", amount - discount)
Q12. A cloth showroom has announced the following festival discounts on the purchase
of items, based on the total cost of the items purchased:
Total Cost Discount
Up to Rs. 2000 5%
Rs. 2001–5000 25%
Rs. 5001–10,000 35%
Above Rs. 10,000 50%
Write a program to input the total cost and to compute and display the amount to be paid
by the customer after availing the discount.
Python Program:
cost = float(input("Enter total cost: "))
if cost <= 2000:
discount = cost * 0.05
elif cost <= 5000:
discount = cost * 0.25
elif cost <= 10000:
discount = cost * 0.35
else:
discount = cost * 0.50
print("Discount =", discount)
print("Amount to be paid =", cost - discount)
4) Menu-Based Programs
5
Q1. Write a menu driven program to find the sum, difference and product of two numbers.
Perform the tasks accordingly. Enter '+' to find the sum, `-' to find the difference and '*' to
find the product of two numbers.
print("+. Addition")
print("-. Subtraction")
print("*. Multiplication")
choice = input("Enter choice : ")
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if choice == ‘+’:
print("Sum =", a+b)
elif choice == ‘-’:
print("Difference =", a-b)
elif choice == ‘*’:
print("Product =", a*b)
else:
print("Invalid Choice")
Q2. WAP to convert temperature (Celsius Fahrenheit).
print("1. Celsius to Fahrenheit")
print("2. Fahrenheit to Celsius")
choice = int(input("Enter choice (1/2): "))
if choice == 1:
c = float(input("Enter temperature in Celsius: "))
f = (c * 9/5) + 32
print("Fahrenheit =", f)
elif choice == 2:
f = float(input("Enter temperature in Fahrenheit: "))
c = (f - 32) * 5/9
print("Celsius =", c)
else:
print("Invalid Choice")
Q3. WAP to find area of Circle, Square, or Rectangle.
print("1. Circle")
print("2. Square")
print("3. Rectangle")
choice = int(input("Enter choice (1-3): "))
if choice == 1:
r = float(input("Enter radius: "))
area = 3.14 * r * r
print("Area of Circle =", area)
elif choice == 2:
s = float(input("Enter side: "))
area = s * s
print("Area of Square =", area)
elif choice == 3:
l = float(input("Enter length: "))
b = float(input("Enter breadth: "))
area = l * b
print("Area of Rectangle =", area)
else:
print("Invalid Choice")
5) Looping Programs - for loop fixed no of iterations
6
Q1. Print “Happy Birthday” 10 times.
for i in range(10):
print("Happy Birthday")
Q2. Print natural numbers from 1 to 10.
for i in range(1, 11):
print(i, end=" ")
Q3. Print even numbers from 1 to 100.
for i in range(2, 101, 2):
print(i, end=" ")
Q4. Print odd numbers from 1 to 100.
for i in range(1, 101, 2):
print(i, end=" ")
Q5. Print numbers in descending order from 10 to 1.
for i in range(10, 0, -1):
print(i, end=" ")
Q6. Find sum of first 10 natural numbers.
s=0
for i in range(1, 11):
s += i
print("Sum =", s)
Q7. Find sum of first 10 even numbers.
s=0
for i in range(2, 21, 2):
s += i
print("Sum =", s)
Q8. Find sum of first 10 odd numbers.
s=0
for i in range(1, 20, 2):
s += i
print("Sum =", s)
Q9. Print all numbers in a given range.
start = int(input("Enter start: "))
end = int(input("Enter end: "))
for i in range(start, end+1,1):
print(i, end=" ")
Q10. Count positive, negative, and zero numbers from 10 inputs.
pos = neg = zero = 0
for i in range(10):
n = int(input("Enter number: "))
if n > 0:
pos += 1
elif n < 0:
neg += 1
else:
zero += 1
7
print("Positives =", pos)
print("Negatives =", neg)
print("Zeros =", zero)
6. Looping programs(Fixed no of iterations) using while loop
Q1. Write a program to print “Happy Birthday” 10 times.
Python Program (while loop):
i=1
while i <= 10:
print("Happy Birthday")
i += 1
Q2. Write a program to print natural numbers from 1 to 10.
Python Program:
i=1
while i <= 10:
print(i, end=" ")
i += 1
Q3. Write a program to print even numbers from 1 to 100.
Python Program:
i=2
while i <= 100:
print(i, end=" ")
i += 2
Q4. Write a program to print odd numbers from 1 to 100.
Python Program:
i=1
while i <= 100:
print(i, end=" ")
i += 2
Q5. Write a program to print numbers in descending order from 10 to 1.
Python Program:
i = 10
while i >= 1:
print(i, end=" ")
i -= 1
Q6. Write a program to find the sum of first 10 natural numbers.
Python Program:
i=1
s=0
while i <= 10:
s += i
i += 1
print("Sum =", s)
Q7. Write a program to find the sum of first 10 even numbers.
Python Program:
8
i=2
s=0
count = 0
while count < 10:
s += i
i += 2
count += 1
print("Sum =", s)
Q8. Write a program to find the sum of first 10 odd numbers.
Python Program:
i=1
s=0
count = 0
while count < 10:
s += i
i += 2
count += 1
print("Sum =", s)
Q9. Write a program to print all numbers in a given range.
Python Program:
start = int(input("Enter start: "))
end = int(input("Enter end: "))
i = start
while i <= end:
print(i, end=" ")
i += 1
Q10. Write a program to count positive, negative, and zero numbers from 10 inputs.
Python Program:
pos = neg = zero = 0
i=1
while i <= 10:
n = int(input("Enter number: "))
if n > 0:
pos += 1
elif n < 0:
neg += 1
else:
zero += 1
i += 1
print("Positives =", pos)
print("Negatives =", neg)
print("Zeros =", zero)
7. Number Programs
Factor Based
Q1. WAP to input a number and find its factors.
Example: factors of 6 = 1, 2, 3, 6
n = int(input("Enter a number: "))
9
print("Factors are:", end=" ")
for i in range(1, n+1):
if n % i == 0:
print(i, end=" ")
Q2. WAP to input a number and count the total number of factors.
Example: factors of 6 = 1, 2, 3, 6 Count = 4
n = int(input("Enter a number: "))
count = 0
for i in range(1, n+1):
if n % i == 0:
count += 1
print("Total number of factors =", count)
Q3. WAP to input 2 numbers and find their HCF/GCD and LCM.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
# HCF (GCD)
hcf = 1
for i in range(1, min(a, b)+1):
if a % i == 0 and b % i == 0:
hcf = i
# LCM
lcm = (a * b) // hcf
print("HCF =", hcf)
print("LCM =", lcm)
Q4. Write a program to accept a number and check whether it is a perfect number.
(A number is said to be perfect if the sum of its factors excluding itself is equal to the number.)
n = int(input("Enter a number: "))
s=0
for i in range(1, n):
if n % i == 0:
s += i
if s == n:
print(n, "is a Perfect number")
else:
print(n, "is not a Perfect number")
Prime Number Based
Q5. Input a number and check whether it is a prime number or not.
n = int(input("Enter a number: "))
count = 0
for i in range(1, n+1):
if n % i == 0:
count += 1
if count == 2:
print(n, "is Prime")
else:
10
print(n, "is not Prime")
Q6. WAP to input 2 numbers and check whether they are twin primes or not.
a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
count1=0
for i in range(1,a+1):
if a%i==0:
count1+=1
count2=0
for i in range(1,b+1):
if b%i==0:
count2+=1
if count1==2 and count2==2 and abs(a-b)==2:
print(a,"and",b,"are Twin Primes")
else:
print(a,"and",b,"are not Twin Primes")
Q7. WAP to input a number and print its primorial.
(Primorial of 7 = 2 × 3 × 5 × 7)
n = int(input("Enter a number: "))
prod = 1
for i in range(2, n+1):
# check prime
ctr=0
for j in range1, i+1):
if i % j == 0:
ctr++
break
if ctr==2:
prod *= i
print("Primorial of", n, "=", prod)
Extraction of Digits
Q8. WAP to input a number and find the total number of digits.
n = int(input("Enter a number: "))
count = 0
temp = n
while temp > 0:
temp //= 10
count += 1
print("Number of digits =", count)
Q9. WAP to input a number and find the sum of its digits.
n = int(input("Enter a number: "))
s=0
temp = n
while temp > 0:
digit = temp % 10
s += digit
11
temp //= 10
print("Sum of digits =", s)
Q10. WAP to input a number and check whether it is a Buzz number.
(A Buzz number is divisible by 7 or ends with 7.)
n = int(input("Enter a number: "))
if n % 7 == 0 or n % 10 == 7:
print(n, "is a Buzz number")
else:
print(n, "is not a Buzz number")
Q11. WAP to input a number and check whether it is a Palindrome.
n = int(input("Enter a number: "))
rev = 0
temp = n
while temp > 0:
digit = temp % 10
rev = rev * 10 + digit
temp //= 10
if rev == n:
print(n, "is a Palindrome")
else:
print(n, "is not a Palindrome")
Q12. WAP to input a number and check whether it is an Armstrong number.
(Example: 153 1³ + 5³ + 3³ = 153)
n = int(input("Enter a number: "))
temp = n
s=0
while n !=0:
digit = n % 10
s += digit ** 3 s=s+digit*digit*digit
n //= 10
if s == temp:
print(n, "is an Armstrong number")
else:
print(n, "is not an Armstrong number")
Q13. WAP to input a number and check whether it is a Prime Palindrome.
n = int(input("Enter a number: "))
# check prime
count = 0
for i in range(1, n+1):
if n % i == 0:
count += 1
# check palindrome
temp = n
rev = 0
while temp > 0:
digit = temp % 10
rev = rev * 10 + digit
temp //= 10
if count == 2 and rev == n:
print(n, "is a Prime Palindrome")
else:
print(n, "is not a Prime Palindrome")
12
Q14. Write a program to accept a number and check whether it is a Niven number.
(A Niven number is divisible by the sum of its digits.)
n = int(input("Enter a number: "))
s=0
temp = n
while temp > 0:
digit = temp % 10
s += digit
temp //= 10
if n % s == 0:
print(n, "is a Niven number")
else:
print(n, "is not a Niven number")
Q15. A special two-digit number is such that when the sum of its digits is added to the
product of its digits, the result is equal to the original number.
Example: 59 (5 + 9) + (5 × 9) = 59
n = int(input("Enter a two-digit number: "))
d1 = n // 10
d2 = n % 10
s = d1 + d2
p = d1 * d2
if s + p == n:
print(n, "is a Special two-digit number")
else:
print(n, "is not a Special two-digit number")
Q16. WAP to print the first 10 numbers of the Fibonacci series.
(Series: 0, 1, 1, 2, 3, 5, … till 10 terms)
a=0
b=1
for i in range (1,6,1):
print(a, b, end=" ")
a= a + b
b=a+b
Q17. Write a program to accept a number and check whether it is a perfect square.
Example: 36 6 × 6 = 36
n = int(input("Enter a number: "))
i=1
flag = False
for i in range(n)
if i * i == n:
flag = True
break
if flag:
print(n, "is a Perfect Square")
else:
print(n, "is not a Perfect Square")
13
Q18. Write a program to find the factorial of a number.
Example: 5! = 1 × 2 × 3 × 4 × 5 = 120
n = int(input("Enter a number: "))
fact = 1
for i in range (1,n+1,1)
fact *= i
print("Factorial of", n, "=", fact
8. SERIES PROGRAMS
Q1a) Write a program to print the series: 1,4,9,16… till 10 terms
for i in range(1, 11):
k=i*i
print(k, end=", ")
Q1b) 3,6,9,12…..till n terms
n = int(input("Enter the value of n: "))
s=0
for i in range(1, n + 1):
s = i*3
print(s, end=", ")
Q1c) 2,5,10,17…. till 10 terms
for i in range(1, 11):
k = (i ** 2) + 1
print(k, end=", ")
Q1d) 0,3,8,15… till 10 terms
for i in range(1, 11):
k = (i ** 2) - 1
print(k, end=", ")
Q1e) 4,16,36…. till 10 terms
for i in range(2, 21, 2):
k = i ** 2
print(k, end=", ")
Q2a) Find the sum of series s = 1/1 + 1/2 + … + 1/20
s = 0.0
for i in range(1, 21):
s += 1 / i
print("The sum is", s)
Q2b) s = 1/1 + 1/3 + 1/5 + … to n terms
n = int(input("Enter the number of terms: "))
s = 0.0
for i in range(1, n * 2, 2):
s += 1 / i
print("The sum is", s)
Q2c) s = 1/2 + 2/3 + 3/4 + … to n terms
14
n = int(input("Enter the number of terms: "))
s = 0.0
for i in range(1, n + 1):
s += i / (i + 1)
print("The sum is", s)
Q2d) s = 1 + 1/4 + 1/9 + … to n terms
n = int(input("Enter the number of terms: "))
s = 0.0
for i in range(1, n + 1):
s += 1 / (i * i)
print("The sum is", s)
Q2e) s = (1*2) + (2*3) + … (19*20)
s=0
for i in range(1, 21):
s += i * (i + 1)
print("The sum is", s)
Q2f) s = 1 + 1/4 + 1/8 + 1/12 + … to n terms
n = int(input("Enter the number of terms: "))
s = 1.0
for i in range(1, n +1,1):
s += 1 / (i*4)
print("The sum is", s)
Q2g) s = a/2 + a/4 + a/6 + … to n terms
n = int(input("Enter the number of terms: "))
a = int(input("Enter the value of a: "))
s = 0.0
for i in range(1, n * 2+1, 2):
s += a / (i
))
print("The sum is", s)
Q2h) s = (a+1)/3 + (a+2)/5 + (a+3)/7 … to n terms
n = int(input("Enter the number of terms: "))
a = int(input("Enter the value of a: "))
s = 0.0
j=3
for i in range(1, n + 1):
s += (a + i) / j
j += 2
print("The sum is", s)
Q2i) 1, -3, 5, -7, 9 … till 10 terms
p=1
for i in range(1, 20, 2):
if p % 2 == 0:
print(-i, end=", ")
else:
print(i, end=", ")
p += 1
15
Q2j) s = 2 – 4 + 6 – 8 + … -20
s=0
j=1
for i in range(2, 21, 2):
if j % 2 == 0:
s -= i
else:
s += i
j++;
print("The sum is", s)
9. Pattern Programs (using nested loops)
Square / Rectangle Patterns
Pattern 1
@@
@@
for i in range(1, 3, 1):
for j in range(1, 3, 1):
print("@", end="")
print()
Pattern 2
####
####
####
####
for i in range(1, 5, 1):
for j in range(1, 5, 1):
print("#", end="")
print()
Pattern 3
*****
*****
*****
for i in range(1, 4, 1):
for j in range(1, 6, 1):
print("*", end="")
print()
Pattern 4
16
@@
@@
@@
@@
@@
for i in range(1, 6, 1):
for j in range(1, 3, 1):
print("@", end="")
print()
Pattern 5
1234
1234
1234
1234
1234
for i in range(1, 6, 1):
for j in range(1, 5, 1):
print(j, end="")
print()
Pattern 6
1111
2222
3333
4444
5555
for i in range(1, 6, 1):
for j in range(1, 5, 1):
print(i, end="")
print()
17
Pattern 7
10101
10101
10101
10101
for i in range(1, 5, 1):
for j in range(1, 6, 1):
if j % 2 == 0:
print(0, end="")
else:
print(1, end="")
print()
Right-Angled Triangle Patterns
Pattern 1
1
12
123
1234
12345
for i in range(1, 6, 1):
for j in range(1, i + 1, 1):
print(j, end="")
print()
Pattern 2
*
**
***
****
*****
for i in range(1, 6, 1):
for j in range(1, i + 1, 1):
print("*", end="")
print()
Pattern 3
1
22
333
4444
55555
18
for i in range(1, 6, 1):
for j in range(1, i + 1, 1):
print(i, end="")
print()
Pattern 4
1
13
135
1357
13579
for i in range(1, 6, 1):
num = 1
for j in range(1, i + 1, 1):
print(num, end="")
num += 2
print()
Pattern 5
1
33
555
7777
99999
num = 1
for i in range(1, 6, 1):
for j in range(1, i + 1, 1):
print(num, end="")
print()
num += 2
Pattern 6
1
10
101
1010
10101
for i in range(1, 6, 1):
for j in range(1, i + 1, 1):
if j % 2 == 0:
print(0, end="")
else:
print(1, end="")
print()
19
Inverted Right-Angled Triangle Patterns
Q1
12345
1234
123
12
1
for i in range(5, 0, -1):
for j in range(1, i + 1, 1):
print(j, end="")
print()
Q2
*****
****
***
**
*
for i in range(5, 0, -1):
for j in range(1, i + 1, 1):
print("*", end="")
print()
Q3
55555
4444
333
22
1
for i in range(5, 0, -1):
for j in range(1, i + 1, 1):
print(i, end="")
print()
Q4
13579
1357
135
13
1
for i in range(5, 0, -1):
num = 1
for j in range(1, i + 1, 1):
20
print(num, end="")
num += 2
print()
Q5
99999
7777
555
33
1
num = 9
for i in range(5, 0, -1):
for j in range(1, i + 1, 1):
print(num, end="")
print()
num -= 2
Q6
54321
5432
543
54
5
for i in range(5, 0, -1):
for j in range(5, 5 - i, -1):
print(j, end="")
print()
Q7
1514131211
10987
654
32
1
n = 15
for i in range(5, 0, -1):
for j in range(1, i + 1, 1):
print(n, end="")
n -= 1
print()
Q8
#$#$#
21
#$#$
#$#
#$
#
for i in range(5, 0, -1):
for j in range(1, i + 1, 1):
if j % 2 == 1:
print("#", end="")
else:
print("$", end="")
print()
9) Functions in Python
Examples of different types of functions
🔹 A. Built-in Functions
1. print() Function
Program 1: Display your name, grade, and school.
print("Name: Aarav Mehta")
print("Grade: 9")
print("School: St. Xavier's High School")
Program 2: Print a message on multiple lines using \n.
print("Welcome to Robotics and AI!\nLet's learn Python with fun!")
2. len() Function
Program 1: Find the length of a user-entered word.
word = input("Enter a word: ")
print("The length of the word is:", len(word))
Program 2: Find how many names are in a list.
names = ["Riya", "Karan", "Neha", "Om"]
print("Total number of names:", len(names))
3. range() Function
Program 1: Print all odd numbers between 1 and 20.
22
for i in range(1, 21, 2):
print(i)
Program 2: Print a multiplication table of a number using range().
num = int(input("Enter a number: "))
for i in range(1, 11):
print(num, "x", i, "=", num * i)
4. abs() Function
Program 1: Print the absolute value of a number.
n = int(input("Enter a number: "))
print("Absolute value:", abs(n))
Program 2: Display the difference between two numbers as a positive value.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Difference (positive):", abs(a - b))
5. round() Function
Program 1: Round a number to 2 decimal places.
n = float(input("Enter a number: "))
print("Rounded to 2 decimals:", round(n, 2))
Program 2: Round marks of a student to the nearest integer.
marks = float(input("Enter marks: "))
print("Rounded marks:", round(marks))
6. sorted() Function
Program 1: Sort a list of numbers entered by the user.
nums = [int(x) for x in input("Enter numbers separated by space: ").split()]
print("Sorted list:", sorted(nums))
Program 2: Sort words alphabetically.
words = ["banana", "apple", "cherry", "date"]
print("Alphabetical order:", sorted(words))
23
B. User-Defined Functions
1. Function Without Parameters
Program 1: Display a welcome message.
def welcome():
print("Welcome to the Python world!")
welcome()
Program 2: Display a simple menu.
def show_menu():
print("1. Start Robot")
print("2. Stop Robot")
print("3. Exit")
show_menu()
2. Function With Parameters
Program 1: Find the sum of two numbers.
def add(a, b):
print("Sum =", a + b)
x=int
y=
add(x,y)
Program 2: Display a personalized message.
def greet(name):
print("Hello", name, "! Welcome to Robotics Class.")
greet("Anaya")
3. Function Returning a Value
Program 1: Return the square of a number.
def square(n):
return n * n
num = int(input("Enter a number: "))
print("Square:", square(num))
Program 2: Return the greater of two numbers.
24
def greater(a, b):
if a > b:
return a
else:
return b
print("Greater number is:", greater(10, 15))
4. Function With Default Argument
Program 1: Greet with a default name.
def greet(name="Student"):
print("Hello", name, "— Keep learning Python!")
greet()
greet("Dev")
Program 2: Calculate area of a rectangle with default breadth.
def area(length, breadth=10):
return length * breadth
print("Area:", area(5))
print(area())
5. Function Returning Multiple Values
Program 1: Return area and perimeter of a rectangle.
def rectangle(l, b):
area = l * b
peri = 2 * (l + b)
return area, peri
a, p = rectangle(5, 3)
print("Area =", a)
print("Perimeter =", p)
6. Function Calling Another Function(Recursive Functions)
Program 1: One function calculates, another displays.
def cube(n):
return n ** 3
def display_cube(num):
25
print("Cube =", cube(num))
display_cube(4)
Program 2: Function for temperature conversion and display.
def to_celsius(f):
return (f - 32) * 5/9
def show_temperature(f):
print("Temperature in Celsius:", round(to_celsius(f), 2))
show_temperature(98.6)
26