Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Grade 9 AI Python Programs (1)

The document contains a series of programming exercises that demonstrate basic Python programming concepts, including input/output, arithmetic operations, control structures, and list manipulations. Each exercise provides a specific task, such as calculating the square of a number, finding the sum of two numbers, or creating and modifying lists. The document serves as a practical guide for beginners to practice and enhance their coding skills.

Uploaded by

viyoban733
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Grade 9 AI Python Programs (1)

The document contains a series of programming exercises that demonstrate basic Python programming concepts, including input/output, arithmetic operations, control structures, and list manipulations. Each exercise provides a specific task, such as calculating the square of a number, finding the sum of two numbers, or creating and modifying lists. The document serves as a practical guide for beginners to practice and enhance their coding skills.

Uploaded by

viyoban733
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Question : Write a program to print personal information like name, father’s name,

class, school name.


name = input("Enter your name: ")
father_name = input("Enter your father's name: ")
class_name = input("Enter your class: ")
school_name = input("Enter your school name: ")

print("Name:", name)
print("Father's Name:", father_name)
print("Class:", class_name)
print("School Name:", school_name)

1
Question: Write a program to print the following patterns using multiple print
commands

print("* *****")
print("** ****")
print("*** ***")
print("**** **")
print("***** *")

2
Question : Write a program to find Square of Number
num = float(input("Enter a number: "))
square = num ** 2
print("Square of", num, "is", square)
==================================================================

3
Question: Write a program to find sum of any two numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
sum = num1 + num2
print("Sum of", num1, "and", num2, "is", sum)
==================================================================

4
Question : Write a program to convert kilometres to meters

km = float(input("Enter distance in kilometers: "))


meters = km * 1000
print(km, "kilometers is equal to", meters, "meters")
==================================================================

5
Question : Write a program to print table of any input number

num = int(input("Enter a number: "))


n = int(input("Enter number of terms: "))
for i in range(1, n+1):
print(num, "x", i, "=", num * i)
==================================================================

6
Question : Write a program calculate simple interest

principle_amount = float(input("Enter principle amount: "))


rate_of_interest = float(input("Enter rate of interest: "))
time = float(input("Enter time in years: "))
simple_interest = (principle_amount * rate_of_interest * time) / 100
print("Simple Interest is", simple_interest)
=================================================================

7
Question : Write a program calculate area and perimeter of rectangle

length = float(input("Enter length of rectangle: "))


width = float(input("Enter width of rectangle: "))
area = length * width
perimeter = 2 * (length + width)
print("Area of rectangle is", area)
print("Perimeter of rectangle is", perimeter)
=================================================================

8
Question : Write a program calculate area of triangle

base = float(input("Enter base of triangle: "))


height = float(input("Enter height of triangle: "))
area = 0.5 * base * height
print("Area of triangle is", area)
=================================================================

9
Question : Write a program calculate average marks

marks1 = float(input("Enter marks of subject 1: "))


marks2 = float(input("Enter marks of subject 2: "))
marks3 = float(input("Enter marks of subject 3: "))
average = (marks1 + marks2 + marks3) / 3
print("Average marks are", average)
==================================================================

10
Question : Write a program calculate discounted amount

amount = float(input("Enter amount: "))


discount_percent = float(input("Enter discount percentage: "))
discount_amount = (amount * discount_percent) / 100
discounted_amount = amount - discount_amount
print("Discounted amount is", discounted_amount)
=================================================================

11
Question: Write a program calculate surface area and volume of cuboid
# Function to calculate surface area and volume of a cuboid
def calculate_cuboid_surface_area_and_volume(length, width, height):
# Surface area formula: 2 * (lw + lh + wh)
surface_area = 2 * (length * width + length * height + width * height)

# Volume formula: l * w * h
volume = length * width * height

return surface_area, volume

# Input dimensions of the cuboid


try:
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: "))

if length > 0 and width > 0 and height > 0:


# Calculate surface area and volume
surface_area, volume = calculate_cuboid_surface_area_and_volume(length, width,
height)

# Display the results


print(f"Surface Area of the cuboid: {surface_area:.2f}")
print(f"Volume of the cuboid: {volume:.2f}")
else:
print("Please enter positive values for length, width, and height.")
except ValueError:
print("Invalid input. Please enter numerical values.")
==================================================================

12
Question : Write a program Create a list in Python of children selected for science quiz
with following namesArjun, 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.
# Step 1: Create a list of children selected for the science quiz
children = ["Arjun", "Sonakshi", "Vikram", "Sandhya", "Sonal", "Isha", "Kartik"]

# Step 2: Print the whole list


print("Original list:", children)

# Step 3: Delete the name "Vikram" from the list


children.remove("Vikram")
print("List after removing 'Vikram':", children)

# Step 4: Add the name "Jay" at the end of the list


children.append("Jay")
print("List after adding 'Jay':", children)

# Step 5: Remove the item at the second position (index 1)


removed_item = children.pop(1)
print("Removed item at second position:", removed_item)
print("Final list:", children)
=================================================================

13
Question : Write a program 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

# Create the list


num = [23, 12, 5, 9, 65, 44]

# Print the length of the list


print("Length of the list:", len(num))

# Print the elements from the second to fourth position using positive indexing
print("Elements from second to fourth position (positive indexing):", num[1:4])

# Print the elements from the third to fifth position using negative indexing
print("Elements from third to fifth position (negative indexing):", num[-4:-1])

==================================================================

14
Question :Write a program to Create a list of first 10 even numbers, add 1 to each list
item and print the final list.

Create a list of first 10 even numbers


even_numbers = [i for i in range(2, 22, 2)]
print("Original list:", even_numbers)

# Add 1 to each list item


even_numbers = [i + 1 for i in even_numbers]
print("Final list:", even_numbers)
=================================================================

15
Question:Write a program to 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.
# Create a list List_1
List_1 = [10, 20, 30, 40]
print("Original list:", List_1)

# Add elements [14, 15, 12] using extend function


List_1.extend([14, 15, 12])
print("List after adding elements:", List_1)

# Sort the final list in ascending order


List_1.sort()
print("Sorted list:", List_1)
=================================================================

16
Question: Write a program to check if a person can vote

age = int(input("Enter your age: "))

if age >= 18:


print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
==================================================================

17
Question: Write a program To check the grade of a student
# Function to determine the grade
def check_grade(marks):
if marks >= 90:
return "A+"
elif marks >= 80:
return "A"
elif marks >= 70:
return "B"
elif marks >= 60:
return "C"
elif marks >= 50:
return "D"
else:
return "F"

# Input marks from the user


try:
marks = float(input("Enter the student's marks (0-100): "))
if 0 <= marks <= 100:
grade = check_grade(marks)
print(f"The student's grade is: {grade}")
else:
print("Please enter marks within the range of 0 to 100.")
except ValueError:
print("Invalid input. Please enter a number.")
==================================================================

18
Question: Write a program Input a number and check if the number is positive,
negative or zero and display an appropriate message
# Input a number from the user
try:
number = float(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.")
except ValueError:
print("Invalid input. Please enter a valid number.")

=================================================================

19
Question: Write a program To print first 10 natural numbers
# Print the first 10 natural numbers
print("The first 10 natural numbers are:")
for number in range(1, 11):
print(number)
=================================================================

20
Question: Write a program to print first 10 even numbers
# Print the first 10 even numbers
print("The first 10 even numbers are:")
for number in range(2, 21, 2):
print(number)

==================================================================

21
Question: Write a program to print odd numbers from 1 to n
# Input the value of n
try:
n = int(input("Enter the value of n: "))
if n > 0:
print(f"Odd numbers from 1 to {n} are:")
for number in range(1, n + 1, 2):
print(number)
else:
print("Please enter a positive integer.")
except ValueError:
print("Invalid input. Please enter an integer.")
==================================================================

22
Question: Write a program to print sum of first 10 natural numbers
# Calculate the sum of the first 10 natural numbers
sum_of_numbers = sum(range(1, 11))

# Print the result


print(f"The sum of the first 10 natural numbers is: {sum_of_numbers}")
=================================================================

23
Question: Write a program to find the sum of all numbers stored in a list
# List of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Calculate the sum of the numbers in the list


sum_of_numbers = sum(numbers)

# Print the result


print(f"The sum of all numbers in the list is: {sum_of_numbers}")
==================================================================

24

You might also like