Practical File Computer Science-1
Practical File Computer Science-1
Practical File Computer Science-1
RANIPUR, HARIDWAR
Computer Science
Practical File
Session 2022-23
1. Write a program to print the table of a given number
n=3
for i in range(1,10):
print("3 x",i,"=", 3 * i)
2. Write a program that accepts three numbers from user and display the largest number
a)
n=5
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end="")
print()
for i in range(1, n + 1):
for j in range(1, i + 1):
print(i, end='')
print()
n=5
for i in range(n, 0, -1):
num = i
for j in range(i, n+1):
print(num, end=' ')
print()
n=5
for i in range(n+1, 0, -1):
for j in range(i-1):
print(j+1, end=" ")
print()
10. Write a program to print the following pattern using concept of nested loop
n=5
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
n=5
for i in range(n, 0, -1):
for j in range(i):
print("*", end="")
print()
n=5
for i in range(n):
for j in range(n-i-1):
print(" ", end="")
for k in range(i+1):
print("* ", end="")
print()
11.Write a program to print the following Alphabetical pattern using concept of nested loop.
n=5
for i in range(n):
for j in range(i+1):
print(chr(65+j), end=" ")
print()
rows = 5
for i in range(rows):
for j in range(i+1):
print(chr(65+i), end="")
print()
x = int(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
sum = 0
for i in range(n+1):
sum += x**i
print(sum)
13. Write a program to accept a character from the user and display whether it is a vowel or
consonant.
char = input("Enter a character: ")
if char in ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'):
print("Vowel")
else:
print("Consonant")
14. Write a program to print the sum of First n natural numbers, accept the value of n from
user.
a= []
n = int(input("Enter the number of elements in the list: "))
for i in range(n):
a.append(int(input("Enter the element: ")))
b = a[0]
for i in a:
if i > b:
b=i
print("Maximum is",b)
for i in a:
if i < b:
b=i
print("Minimum is",b)
18. Input a list of numbers and swap elements at the even location with the elements at the
odd location.
list1 = [ ]
n = int(input("Enter the number of elements in the list: "))
for i in range(n):
list1.append(int(input("Enter the element: ")))
for i in range(0,len(list1),2):
list1[i],list1[i+1]=list1[i+1],list1[i]
print(list1)
19. WAP that returns the largest even number in the list of integers. If there is no even
number in input, print “No even number”.
a= []
n = int(input("Enter the number of elements in the list: "))
for i in range(n):
a.append(int(input("Enter the element: ")))
b = a[0]
c=0
for i in a:
if (i % 2) == 0:
if i > b:
b=i
c += 1
if c == 0:
print("No even number")
if b != a[0]:
print("Largest even number is",b)
20. WAP that prints the sum of the even indexed elements of L, minus the sum of the odd
indexed elements of L.
a = eval(input("Enter the list: "))
even_sum = 0
odd_sum = 0
for i in range(len(a)):
if i % 2 == 0:
even_sum += a[i]
else:
odd_sum += a[i]
print("Result is", even_sum - odd_sum)
21. WAP to print the alternate elements of a tuple T
a = eval(input("Enter the tuple: "))
print(a[::2])
22. WAP to print every 3rd element of a tuple T
a = eval(input("Enter the elements of the tuple: "))
print(a[::3])
23.Write a python program that inputs two tuples and creates a third that contains all
elements of the first followed by all elements of the second.
a = eval(input("Enter the elements of the first tuple: "))
b = eval(input("Enter the elements of the second tuple: "))
c=a+b
print(c)
24. 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
n=int(input("Enter number of students: "))
d={}
for i in range(n):
roll_no=int(input("Enter roll no: "))
name=input("Enter name: ")
marks=int(input("Enter marks: "))
d[roll_no]=[name,marks]
print("Students with more than 75: ")
for k in d:
if(d[k][1]>75):
print(d[k][0])
25.Create a nested dictionary that stores the marks details along with student names and then
prints the output.