0% found this document useful (0 votes)
10 views

computer science class 11 Lab 2024_2025

The document outlines various Python programming tasks, including inputting student details, checking if a number is even or odd, performing arithmetic operations, determining prime numbers, calculating student grades, and more. Each task includes a brief description of the aim followed by the corresponding Python code. The programs cover a wide range of topics such as loops, conditionals, string manipulation, and list operations.

Uploaded by

pallathika2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

computer science class 11 Lab 2024_2025

The document outlines various Python programming tasks, including inputting student details, checking if a number is even or odd, performing arithmetic operations, determining prime numbers, calculating student grades, and more. Each task includes a brief description of the aim followed by the corresponding Python code. The programs cover a wide range of topics such as loops, conditionals, string manipulation, and list operations.

Uploaded by

pallathika2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

AIM: Write a Python program that prompts the user to input student details (such as name, father name, mother
name, class, age, school, and city) and then prints those details.
#Program:
#program for taking student details
n=input("Enter your name ")
f=input("Enter your fahter name ")
m=input("Enter your mother name ")
cls=input("Enter your class ")
a=int(input("Enter your age "))
scl=input("Enter your school name ")
city=input("Enter your city ")

print("---------------Student details------------------------")
print("Name of the student :",n)
print("Father name :",f)
print("Mother name :",m)
print("class :",cls)
print("Age :",a)
print("Name of the school :",scl)
print("city :",city)
print("")
print("------------------------------------------------------")
2. AIM: Write a Python program to determine whether a given number is even or odd.
#Program

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


if (num % 2) == 0:
print(num," is even")
else:
print(num,"is odd")

Output:
Enter a number: 97
97 is odd

Result:
Program has been executed successfully

3. AIM: Write a Python program that performs basic arithmetic operations (addition, subtraction, multiplication,
division) based on user selection. The user should choose an operation and then input two numbers to perform the
calculation.
#Program
a=int(input("Enter value 1 : "))
b=int(input("Enter value 2 : "))
option=input("Enter one option (add,sub,div,mul):\n")
if option=="add":
result=a+b
print(result)
elif option=="sub":
result=a-b
print(result)
elif option=="div":
if b==0:
print("Can’t be solved")
else:
result=a/b
print(result)
elif option=="mul":
result=a*b
print(result)
else:
print("wrong input,program stopped")
4. AIM: Write a Python program to check whether a given number is prime or not.
Program:
num = int(input("number"))
if num > 1:
for i in range(2,num):
if num % i == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
5. AIM: Write a Python program to calculate a student's grade based on the marks inputted by the user."
Program:
print("Enter Marks Obtained in 5 Subjects: ")
sub1 = int(input("Enter English marks: ")))
sub2 = int(input("Enter Math marks: "))
sub3 = int(input("Enter Physics marks: "))
sub4 = int(input("Enter Chemistry marks: "))
sub5 = int(input("Enter Computer marks: "))

tot = sub1+sub2+sub3+sub4+sub5
avg = tot/5

if avg>=91 and avg<=100:


print("Your Grade is A1")
elif avg>=81 and avg<91:
print("Your Grade is A2")
elif avg>=71 and avg<81:
print("Your Grade is B1")
elif avg>=61 and avg<71:
print("Your Grade is B2")
elif avg>=51 and avg<61:
print("Your Grade is C1")
elif avg>=41 and avg<51:
print("Your Grade is C2")
elif avg>=33 and avg<41:
print("Your Grade is D")
elif avg>=21 and avg<33:
print("Your Grade is E1")
elif avg>=0 and avg<21:
print("Your Grade is E2")
else:
print("Invalid Input!")
6. AIM: Write a Python program to find largest among three numbers.
Program:
a=int(input("Enter number a :"))
b=int(input('enter number b :'))
c=int(input("Enter number c :"))
if (a >= b) and (a >= c):
largest = a
elif (b >= a) and (b >= c):
largest = b
else:
largest = c
print("The largest number is", largest)
7. AIM: Write a python program Input a number and check if the number is a prime or composite number.
Program:
num = int(input("Enter a number: "))

if num > 1:
is_prime = True # Assume the number is prime initially

for i in range(2, num): # Check for factors only up to the num


if num % i == 0:
is_prime = False
break

if is_prime:
print(num, "is a prime number.")
else:
print(num, "is a composite number.")
elif num == 1:
print(num, "is neither prime nor composite.")
else:
print(num, "is not a valid input. Please enter a positive integer.")
8.AIM: Write a python program to Compute the greatest common divisor and least common multiple of two
integers.
Program:
a = int(input("Enter 1st number: "))
b = int(input("Enter 2nd number: "))
# Store original values for later use
original_a = a
original_b = b
# Using Euclidean Algorithm to find GCD
while b:
a, b = b, a % b
gcd = a # Store GCD
print("GCD is:", gcd)

# LCM PROGRAM
# Start with the maximum of the two original numbers
lcm = max(original_a, original_b)
# Use a for loop to find the LCM
for i in range(lcm, (original_a * original_b) + 1):
if i % original_a == 0 and i % original_b == 0:
lcm = i
break # Found the LCM

print("LCM is:", lcm)

9. AIM: python program Display the terms of a Fibonacci series.


Program:
n_terms = int(input("Enter the number of terms in the Fibonacci series: "))

# First two terms


a, b = 0, 1
print("Fibonacci Series:")
for i in range(n_terms):
print(a, end=" ")
# Update values for next term
a, b = b, a + b
10. Write a program to input the value of x and n and print the sum of the following series:

Program:
x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))

# a. Sum of the series: 1 + x + x2 + x3 + ... + xn


sum_a = 0
for i in range(n + 1):
sum_a += x ** i

# b. Sum of the series: 1 - x + x2 - x3 + ... + (-1)n * xn


sum_b = 0
for i in range(n + 1):
sum_b += (-1) ** i * (x ** i)

# c. Sum of the series: x^1/1 + x^2/2 + x^3/3 + ... + x^n/n


sum_c = x
for i in range(1, n + 1):
sum_c += (x ** i) / i

print(f"Sum of series a: {sum_a}")


print(f"Sum of series b: {sum_b}")
print(f"Sum of series c: {sum_c}")
11. AIM: Write a python program to print left triangle star pattern and right-side triangle star pattern.
* *
** **
*** ***
**** ****
***** *****
# a. Program
n = int(input("Enter number of rows: "))
for i in range(1, n + 1):
for j in range(i):
print("*", end="")
print()

# b. Program
row = int(input("Enter number of rows: "))
for i in range(1, row + 1):
# Print leading spaces
for j in range(1, row - i + 1):
print(" ", end="")
# Print stars
for k in range(0, i):
print("*", end="")
print()
12. AIM: Write a python program to print following pattern.
1 1
12 22
123 333
1234 4444
12345 55555
#a_Program:
n = int(input("Enter number of rows: "))
# Outer loop to handle the rows
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end="")
print()

#a_Program:
n = int(input("Enter number of rows: "))
# Outer loop to handle the rows
for i in range(1, n + 1):
for j in range(i):
print(i, end="")
print()
13. AIM: Write a python program to print following pattern.
A ****
AB ***
ABC **
ABCD *
#a_Program:
n = int(input("Enter number of rows: "))
# Outer loop to handle the rows
for i in range(1, n + 1):
for j in range(i):
print(chr(65 + j), end="")#chr(65) is 'A',this prints the alphabet
print()

#a_Program:
n = int(input("Enter number of rows: "))
# Outer loop to handle the rows
for i in range(n, 0, -1):
# Inner loop to print stars
for j in range(i):
print("*", end="")
print()

14. Write a Python program that takes a string and a specific word as input from the user, and counts how many
times the specific word occurs in the given string.
#Program:
# Initialize an empty string
text = ""
while True:
user_input = input("Enter a string: ")
text += user_input + " " # Append the input to the text with space

opt = input('Do you want to enter a string? (Yes or No): ')

if opt.lower() == 'yes':
continue
elif opt.lower() == 'no':
break

word_to_count = input("Enter the word to count: ")


count = 0
words = text.split()
# Use a while loop to count occurrences of the word

for i in words:
if i.lower()==word_to_count.lower():
count += 1

print(f"The word '{word_to_count}' occurs {count} times.")


15. Write a Python program that takes a string input from the user and counts the number of vowels, consonants,
uppercase letters, and lowercase letters in the string.
# Program:
Text=input("Enter the string :")
c1=0
c2=0
c3=0
c4=0
for i in Text:
if (i.islower()):
c1=c1+1
elif (i.isupper()):
c2=c2+1

Vow = 'aeiouAEIOU'
for ch in Text:
if ch in Vow:
c3=c3+1
elif ch.isalpha() and ch not in Vow:
c4=c4+1
print("The number of lower characters ",c1)
print("the number of upper characters ",c2)
print("The number of vowel characters ",c3)
print("the number of consonant characters ",c4)

16. Write a program that takes a string and replaces all vowels in the string with '*' character.
# Program:
st = input("Enter a String: ")
# Initialize an empty string to store the modified string
newstr = ''

for char in st:


if char in 'aeiouAEIOU': # Check if the character is a vowel
newstr += '*' # Replace vowel with '*'
else:
newstr += char # Keep non-vowel characters unchanged

print("The original String is:", st)


print("The modified String is:", newstr)
17. Write a program that takes a string as input and outputs the reversed version of that string.
# Program:
st = input("Enter a string: ")

# Initialize an empty string to store the reversed string


reversed_st = ''
length = len(st)

for i in range(-1,-length-1, -1):


reversed_st += st[i]

print("The original string is:", st)


print("The reversed string is:", reversed_st)
18. Python program that takes a string input from the user and determines whether it is a palindrome or
not.
#Program:
str_1 = input("Enter a string: ")

# Checking if the string is the same as its reverse using slicing


if str_1 == str_1[::-1]:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
19. Write a Python program that allows the user to input a list of elements. Then, the program should ask
the user to input an element to search for in the list. The program should check if the element exists in the
list and display an appropriate message.
n = int(input('Enter a number of elements to be added to the list: '))
L = []

# Taking input for list elements


for i in range(n):
e = int(input('Enter an element: '))
L.append(e)

# Ask for element to search


Ele_serch = int(input("Enter element to search: "))

print("Original List:", L)

# Variable to check whether the element is found or not


f = 0

# Searching for the element in the list


for i in range(len(L)):
if L[i] == Ele_serch:
f = 1
break

# Output whether the element was found or not


if f == 1:
print(f"Element {Ele_serch} found in the list")
else:
print(f"Element {Ele_serch} not found in the list")
20. Write a Python program that takes a list of integers as input from the user. The program should increase each
element that is divisible by 5 by 100.
# Program
n = int(input("Enter the number of elements in the list: "))

# Initialize an empty list


my_list = []

# Use a for loop to take inputs from the user


for i in range(n):
element = int(input(f"Enter element {i+1}: "))
my_list.append(element)

print('original list',my_list)

# Update elements divisible by 5 by adding 100 to them


for i in range(len(my_list)):
if my_list[i] % 5 == 0:
my_list[i] += 100

# Print the updated list

print("Updated list:", my_list)

21. Write a python program to find the largest number in a list.


#Program:
n = int(input("Enter the number of elements in the list: "))

# Initialize an empty list


my_list = []

# Use a for loop to take inputs from the user


for i in range(n):
element = int(input(f"Enter element {i+1}: "))
my_list.append(element)

print('original list',my_list)
#Finding maximum
Max=0
for i in range(len(my_list)):
if my_list[i]>Max:
Max=my_list[i]
print("The lasrgest number is:",Max)

22. Write a program to swap elements at the even location with the element’s odd location.
#Program:
#Program:
n=int(input("Enter the number of elements:"))
Ls=[]
for i in range(n):
val=int(input("Enter element:"))
Ls.append(val)
print("The original list:",Ls)

for i in range(0, len(Ls) - 1, 2):


# Swap elements at index i (even) and i+1 (odd)
Ls[i], Ls[i + 1] = Ls[i + 1], Ls[i]

# Print the modified list


print("After swapping:", Ls)
23.Write a Python program that performs the following tasks:
1. Allows the user to input the elements one by one to create the list.
2. Asks the user to input an element that they want to delete from the list.
3. If the element exists, remove it, and display the updated list after performing the delete operation.
4. If the element does not exist in the list, display a message indicating that the element was not found.
#Program:
a=[]
n=int(input("enter num of elements to insert:"))
for i in range(n):
e=input(int("enter element to insert:"))
a.append(e)
val=input('Enter the Value to Delete:')
if val in a:
a.remove(val)
else:
print("\nElement doesn't exist in the List!")

print("\nThe New list is: ")


print(a)

24. Write a Python program that creates a list of strings. The program should then identify the strings from the list
that do not contain any vowels. These strings should be pushed into a new list called NoVowel. Finally, the program
should print the NoVowel list containing all the strings that do not have vowels.
#Program:
strings_list = []
n=int(input('Enter no of elements to be inserted'))
for i in range(n):
ele=input('Enter element')
strings_list.append(ele)

NoVowel = []
for string in strings_list:

has_vowel = False
for char in string:
if char in "aeiouAEIOU":
has_vowel = True
break # Exit the loop if a vowel is found

# If no vowel is found, add the string to the NoVowel list


if not has_vowel:
NoVowel.append(string)

print("Strings with no vowels:", NoVowel)


25. Write a Python program that:
1. Creates a list named My_List with integers, including some zeros.
2. Creates another list named indexList that stores the indices of all non-zero elements from My_List.
3. Prints the indexList containing the indices of non-zero elements in My_List.
#Program:
My_List = []
n=int(input('Enter no of elements to be inserted: '))

for i in range(n):
element=int(input('Enter element :'))
My_List.append(element)

# Initialize an empty list to store indices of non-zero elements


indexList = []

# Loop through the list to find the indices of non-zero elements


for i in range(len(My_List)):
if My_List[i] != 0:
indexList.append(i)

# Print the list of indices of non-zero elements


print("Indices of non-zero elements:", indexList)

26. Create a nested list where each element represents a student with three elements: the roll number,
name, and marks. Then, implement a search functionality that allows the user to search for a student's
name based on a given roll number. If the roll number is found, display the student's name, marks;
otherwise, display an appropriate message indicating that the student was not found.

#Program:

n=int(input('Enter no of students to create list: '))

students = []

for i in range(n):

Roll_no=int(input('Enter roll number: '))

na=input('Enter name: ')

ma=int(input('Enter marks: '))

data=[Roll_no,na,ma]

students.append(data)

# Input: roll number to search

search_roll_number = int(input("Enter the roll number to search: "))

found = 0

for stu in students:

# If roll number matches, display the student's name and marks

if stu[0] == search_roll_number:

print("Name:",stu[1])

print('Marks:' ,stu[2])

found = 1

break

# If no match is found, display an appropriate message

if found==0:

print("Student not found.")

27. Create a nested list with roll number, name and marks. Input a roll number and update the marks. if roll
number is not found display appropriate message.

#Program

n=int(input('Enter no of students to create list: '))

students = []

for i in range(n):

Roll_no=int(input('Enter roll number: '))

na=input('Enter name: ')

ma=int(input('Enter marks: '))

data=[Roll_no,na,ma]

students.append(data)

print('created list',students)

# Input: roll number to search

roll_no = int(input("Enter the roll number to update the marks: "))

found=0

for i in students:

if i[0]==roll_no:

i[2]=int(input('Enter marks to be updated'))

found=1

break

if found==0:

print('The student is not found')

else:

print('Data after updated',students)


28. Write a python program create a dictionary with roll number, name. Take a roll number from user to
find respective name of the roll number. If roll number is not found display appropriate message.
#Program:

stu_dict = {}

n=int(input('Enter number of students'))

for _ in range(n):

roll_no= int(input("Enter roll number : "))

name = input("Enter student name")

stu_dict[roll_no] = name

print('student data:',stu_dict)

roll_number = int(input("Enter the roll number to find the respective


name: "))

f=0

for i in stu_dict:

if i==roll_number:

print(f" roll number:{roll_number}: {stu_dict[i]} " )

f=1

break

if f==0:

print( f"Roll number {roll_number} not found.")

29.Write a python program to take a sentence and count occurrences of each word in the sentence.
#Program:
sentence = input("Enter a sentence: ")

words = sentence.split()
word_count = {}

for w in words:
if w in word_count: #word is already in the dictionary, increment it
word_count[w] += 1
else:
word_count[w] = 1 #add the word to the dict with a count of 1.

for word, count in word_count.items():


print(f"'{word}': {count}")
30. Write a python program to create a dictionary with name and marks.
a) Take a name from the user to update the respective marks of the name.
b) Take a name from the user to delete the item.
c) If name is not found, display the appropriate message.
#Program
students = {}

# Adding student names and marks


n = int(input("Enter the number of students: "))
for _ in range(n):
name = input("Enter student name: ")
marks = int(input(f"Enter marks for {name}: "))
students[name] = marks

print(" student data:", students)

# Update marks for a student


update_name = input(" Enter student name to update marks: ")
if update_name in students:
new_marks = int(input(f"Enter new marks for {update_name}: "))
students[update_name] = new_marks
print(f"Updated marks for {update_name}: {students[update_name]}")
else:
print(f"Student {update_name} not found!")

# Deleting a student
delete_name = input("Enter the name of the student to delete: ")
if delete_name in students:
del students[delete_name]
print(f"{delete_name} has been deleted from the records.")
else:
print(f"Student {delete_name} not found!")

# Final dictionary after updates and deletions


print("Final student data :", students)

31. Create a dictionary with the roll number, name and marks of n students in a class and display the
names of students who have scored marks above 75.
# Program:
n = int (input ("Enter number of students: "))

Stu_data = {}

for i in range(n):
print(f"Enter Details of student No. {i+1}")

roll_no = int(input("Roll No: "))


std_name = input("Student Name: ")
marks = int(input("Marks: "))

Stu_data[roll_no] = [std_name, marks]

print("All student details:", Stu_data)

print("Students who scored more than 75 marks:")


for k in Stu_data:
if Stu_data[k][1] > 75:
print(f"name {Stu_data[k][0]}")

You might also like