Programs using - print()
Practical 1: Personal Information
Aim: To print personal information like Name, Father's name, class, school name.
Code:
# Define personal information
name = "John"
Father_name = "Mr.Sam"
CLASS = "9 A"
School_name = "Presidency School"
# Print Personal information using print()
print("NAME =", name)
print("FATHER'S NAME =",Father_name)
print("CLASS =", CLASS)
print("SCHOOL NAME =", School_name)
Output:
NAME = John
FATHER'S NAME = Mr.Sam
CLASS = 9 A
SCHOOL NAME = Presidency School
Practical 2: Pattern Program
Aim: To print the following pattern using multiple print()
*
* *
* * *
* * * *
* * * * *
Code:
print("*")
print("*", "\t", "*")
print("*", "\t", "*", "\t", "*")
print("*", "\t", "*", "\t", "*", "\t", "*")
print("*", "\t", "*", "\t", "*", "\t", "*", "\t", "*")
Output:
*
* *
* * *
* * * *
* * * * *
Practical 3: Pattern Program
Aim: To find square of number 7
Code:
# Calculate the square of 7
a = 7
square = a*a
# Print the result
print("The square of 7 is ", square)
Output:
The square of 7 is 49
Practical 4: Sum of two numbers
Aim: To find the sum of two numbers 15 and 20
Code:
# Calculate the sum of 15 and 20
a = 15
b = 20
sum = a + b
# print the result
print(" The sum of 15 and 20 is", sum)
Output:
The sum of 15 and 20 is 35
Practical 5: Convert length
Aim: To convert length given in kilometers into meters.
Code:
#define the length in km
km = 5
# Convert the length to meters
m = km * 1000
# Print the result
print("The length in meters is: ", m)
Output:
The length in meters is: 5000
Practical 6: Tables
Aim: To print the table of 5 up to five terms.
Code:
# define the number for which we want the multiplication table
n = 5
print(n, 'x', 1, '=', n*1)
print(n, 'x', 2, '=', n*2)
print(n, 'x', 3, '=', n*3)
print(n, 'x', 4, '=', n*4)
print(n, 'x', 5, '=', n*5)
Output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
Practical 7: Simple Interest
Aim: To print the simple Interest
Code:
# Given Values
P = 2000
R = 4.5
T = 10
# Calculate Simple Interest
SI = (P*R*T)/100
# Print the result
print("The Simple Interest is: ", SI)
Output:
The Simple Interest is: 900.0
Programs using -input()
Practical 8: Area and Perimeter of a rectangle
Aim: To calculate area and perimeter of a rectangle.
Code:
# Reading length and breadth
length = int(input("Enter the length:"))
breadth = int(input("Enter the breadth:"))
# Calculate area
area = length * breadth
# Calculate perimeter
perimeter = 2*(length + breadth)
# Displaying results
print("Area of rectangle:", area)
print("Perimeter of rectangle:", perimeter)
Output:
Enter the length:5
Enter the breadth:4
Area of rectangle: 20
Perimeter of rectangle: 18
Practical 9: Area of a triangle with base and height
Aim: To calculate area of a triangle with base and height
Code:
# Read the base and height
b = int(input("Enter the base:"))
h = int(input("Enter the height:"))
# Calculate the area of the triangle
area = (b*h)/2
# Print the calculated area
print("Area =", area)
Output:
Enter the base:5
Enter the height:4
Area = 10.0
Practical 10: Calculate average marks of 3 subjects
Aim: To calculate average marks of 3 subjects
Code:
# Read 3 subject marks
m1 = int(input("Enter marks of English:"))
m2 = int(input("Enter marks of Science:"))
m3 = int(input("Enter marks of Maths:"))
Total = m1 + m2 + m3
average = Total // 3
print("Total marks:", Total)
print("Average:", average)
Output:
Enter marks of English:87
Enter marks of Science:98
Enter marks of Maths:76
Total marks: 261
Average: 87
Practical 11: Calculate surface area & volume of a cuboid
Aim: To Calculate surface area & volume of a cuboid
Code:
length = float(input("Enter the length of the cuboid: "))
width = float(input("Enter the width of the cuboid: "))
height = float(input("Enter the height of the cuboid: "))
# Surface area of a cuboid: 2(lw + lh + wh)
surface_area = 2 * (length * width + length * height + width * height)
# Volume of a cuboid: lwh
volume = length * width * height
# Print the results
print(surface_area)
print(volume)
Output:
Enter the length of the cuboid: 4
Enter the width of the cuboid: 5
Enter the height of the cuboid: 6
148.0
120.0
Programs using - if, for, while
Practical 12: Check if a person can vote
Aim: To check if a person can vote or not.
Code:
# Input age from the user
age = int(input("Enter your age: "))
# Check if the person is eligible to vote
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote yet.")
Output:
Enter your age: 19
You are eligible to vote.
Practical 13: Check the grade of a student
Aim: To check the grade of a student.
Code:
# Input marks from the user
marks = float(input("Enter your marks: "))
# Check the grade based on the marks
if marks >= 90:
grade = 'A'
elif marks >= 80:
grade = 'B'
elif marks >= 70:
grade = 'C'
elif marks >= 60:
grade = 'D'
elif marks >= 50:
grade = 'E'
else:
grade = 'F'
# Print the grade
print("Your grade is:", grade)
Output:
Enter your marks: 76
Your grade is: C
Practical 14: Check if the number is positive, negative or zero
Aim: To Input a number and check if the number is positive, negative or zero and display an appropriate message .
Code:
# Input a number from the user
number = int(input("Enter a number: "))
# Check if the number is positive, negative, or zero
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
Output:
Enter a number: -11
The number is negative.
Practical 15: Print natural numbers
Aim: To print first 10 natural numbers.
Code:
# Loop to print the first 10 natural numbers
for i in range(1, 11):
print(i)
Output:
1
2
3
4
5
6
7
8
9
10
Practical 16: Print even numbers
Aim: To print first 10 even numbers .
Code:
# Loop to print the first 10 even numbers
for i in range(1, 11):
print(i * 2)
(OR)
for i in range(2, 21,2):
print(i)
Output:
2
4
6
8
10
12
14
16
18
20
Practical 17: Print odd numbers
Aim: To print odd numbers from 1 to n.
Code:
# Input the value of n from the user
n = int(input("Enter a number: "))
# Loop to print odd numbers from 1 to n
for i in range(1, n+1, 2):
print(i)
(OR)
for i in range(1, n+1):
if i % 2 != 0: # Check if the number is odd
print(i)
Output:
Enter a number: 7
1
3
5
7
Practical 18: Print sum of natural numbers
Aim: To print sum of first 10 natural numbers .
Code:
# Initialize the sum variable
sum = 0
# Loop to add the first 10 natural numbers
for i in range(1, 11):
sum = sum + i
# Print the sum
print("The sum of the first 10 natural numbers is:", sum)
Output:
The sum of the first 10 natural numbers is: 55
Practical 19: find the sum of all numbers stored in a list
Aim: Program to find the sum of all numbers stored in a list .
Code:
# Define a list of numbers
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Initialize the sum variable
sum = 0
# Loop through the list and add each number to the sum
for i in L:
sum = sum + i
# Print the sum
print("The sum of all numbers in the list is: ", sum)
Output:
The sum of all numbers in the list is: 55
Programs using - List
Practical 20: Create a list in Python of children selected for science quiz with following names
Arjun, Sonakshi, Vikram, Sandhya, Sonal, Isha, Kartik
Perform the following tasks on the list in sequence-
Print the whole list
Delete the name “Vikram” from the list
Add the name “Jay” at the end
Remove the item which is at the second position.
Aim: Program to create a list and perform the above given task .
Code:
# Creating the list of children selected for the science quiz
quiz_children = ["Arjun", "Sonakshi", "Vikram", "Sandhya", "Sonal", "Isha", "Kartik"]
# 1. Print the whole list
print("Initial list:", quiz_children)
# 2. Delete the name “Vikram” from the list
quiz_children.remove("Vikram")
print("After removing Vikram:", quiz_children)
# 3. Add the name “Jay” at the end
quiz_children.append("Jay")
print("After adding Jay:", quiz_children)
# 4. Remove the item which is at the second position
del quiz_children[1] # Second position is index 1
print("After removing the second item:", quiz_children)
Output:
Initial list: ['Arjun', 'Sonakshi', 'Vikram', 'Sandhya', 'Sonal', 'Isha', 'Kartik']
After removing Vikram: ['Arjun', 'Sonakshi', 'Sandhya', 'Sonal', 'Isha', 'Kartik']
After adding Jay: ['Arjun', 'Sonakshi', 'Sandhya', 'Sonal', 'Isha', 'Kartik', 'Jay']
After removing the second item: ['Arjun', 'Sandhya', 'Sonal', 'Isha', 'Kartik', 'Jay']
Practical 21: Create a list num = [23,12, 5, 9, 65, 44]
print the length of the list
print the elements from second to fourth position using positive indexing
print the elements from position third to fifth using negative indexing
Aim: Program to create a list and perform the above given task .
Code:
# Creating the list of numbers
num = [23, 12, 5, 9, 65, 44]
# 1. Print the length of the list
print("Length of the list:", len(num))
# 2. Print the elements from second to fourth position using positive indexing
print("Elements from second to fourth position:", num[1:4])
# 3. Print the elements from position third to fifth using negative indexing
print("Elements from third to fifth position using negative indexing:", num[-4:-1])
Output:
Length of the list: 6
Elements from second to fourth position: [12, 5, 9]
Elements from third to fifth position using negative indexing: [5, 9, 65]
Practical 22: Create a list of first 10 even numbers, add 1 to each list item and print the final list.
Aim: Program to create a list and perform the above given task .
Code:
# Creating a list of the first 10 even numbers
even_numbers = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# Creating an empty list to store the final results
final_list = []
# Using a simple for loop to add 1 to each item in the list
for num in even_numbers:
final_list.append(num + 1)
# Printing the final list
print("Final list after adding 1 to each item:", final_list)
Output:
Final list after adding 1 to each item: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Practical 23: Create a list List_1 = [10, 20, 30, 40]. Add the elements [14,15,12] using extend function. Now sort the final list in ascending order and print it.
Aim: Program to create a list and perform the above given task .
Code:
# Creating the initial list
List_1 = [10, 20, 30, 40]
# Adding the elements [14, 15, 12] using the extend function
List_1.extend([14, 15, 12])
# Sorting the final list in ascending order
List_1.sort()
# Printing the final sorted list
print("Final sorted list:", List_1)
Output:
Final sorted list: [10, 12, 14, 15, 20, 30, 40]