Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

PRACTICAL ASSIGNMENT

XI SCIENCE (JEE-NEET-BOARD)
COMPUTER SCIENCE-(083)
FOR THE SESSION-2022-23

1. Write a python program to Input a welcome message and display it.

message=input("Enter welcome message : ") #input a message


print("Hello, ",message)

output
Enter welcome message : India
Hello, India

2. Input two numbers and display the larger / smaller number.

a = int(input(“Enter the first number: “)) #input first number


b = int(input(“Enter the second number: “)) #input second number
if(a >b): #comparing
print(a, “is greater”)
else:
print(b, “is greater”)

output
Enter the first number: 10
Enter the second number: 20
20 s greater

3. Input three numbers and display the largest / smallest number.

#input first,Second and third number


num1=int(input("Enter First Number"))
num2=int(input("Enter Second Number"))
num3=int(input("Enter Third Number"))
#Check if first number is greater than rest of the two numbers.
if (num1> num2 and num1> num3):
print("The Largest number is", num1)
#Check if Second number is greater than rest of the two numbers.
elif (num2 > num1 and num2> num3):
print ("The Largest number is", num2)
else:
print ("The Largest number is", num3)

output
Enter First Number 10
Enter Second Number 20
Enter Third Number 15
The Largest number is 20

4. Generate the following patterns using nested loop.

1
Pattern-1

# This is the example of print simple pyramid pattern


n = int(input("Enter the number of rows"))
# outer loop to handle number of rows
for i in range(0, n):
# inner loop to handle number of columns
# values is changing according to outer loop
for j in range(0, i + 1):
# printing stars
print("* ", end="")

# ending line after each row


print()

output
*
* *
* * *
* * * *
* * * * *

Pattern-2
# This is the example of print simple pyramid pattern
rows = 5
# outer loop to handle number of rows
for i in range(0,rows,1):
# inner loop to handle number of columns
# values is changing according to outer loop

for j in range(1,6-i,1):
# printing numbers
print(j, end=' ')
# ending line after each row
print()

output

Pattern-3
# This is the example of print simple pyramid pattern
rows = 5
# outer loop to handle number of rows

2
for i in range(1,rows+1,1):
# inner loop to handle number of columns
# values is changing according to outer loop
a=65
for j in range(1,i+1,1):
# printing numbers
print(str(a), end=' ')
a+=1
# ending line after each row
print()

output

5. Determine whether a number is a perfect number, an Armstrong number


or a palindrome.

def palindrome(n):
temp = n #temp is taken as input
rev = 0 #reverse is 0
while n > 0: #if n is greater than 0 this loop runs
dig = n % 10
rev = rev * 10 + dig
n = n // 10
if temp == rev:
print(temp, "The number is a palindrome!")
else:
print(temp, "The number isn't a palindrome!")
def armstrong(n):
count = 0
temp = n
while temp > 0:
digit = temp % 10
count += digit ** 3
temp //= 10
if n == count:
print(n, "is an Armstrong number")
else:
print(n, "is not an Armstrong number")
def perfect(n):
count = 0
for i in range(1, n):
if n % i == 0:
count = count + i
if count == n:
print(n, "The number is a Perfect number!")
else:
print(n, "The number is not a Perfect number!")

n = int(input("Enter number:"))

3
palindrome(n)
armstrong(n)
perfect(n)

6. Input a number and check if the number is a prime or composite number.

num = int(input("Enter any 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")
elif num == 0 or 1:
print(num, "is a neither prime NOR composite number")
else:
print(num, "is NOT a prime number it is a COMPOSITE number")

7. Display the terms of a Fibonacci series.

nterms = int(input("How many terms? "))


# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

8. Compute the greatest common divisor and least common multiple of two
integers.

print("Enter Two Numbers: ", end="")


no = int(input())
nt = int(input())
a = no
b = nt
while b!=0:
temp = b
b = a%b

4
a = temp
hcf = a
lcm = int((no*nt)/hcf)
print("\nLCM (" + str(no) + ", " + str(nt) + ") = ", lcm)
print("HCF (" + str(no) + ", " + str(nt) + ") = ", hcf)

9. Count and display the number of vowels, consonants, digit, special characters in
string.
# Python3 Program to count vowels,
# consonant, digits and special

def countCharacterType(str):

vowels = 0
consonant = 0
specialChar = 0
digit = 0

for i in range(0, len(str)):

ch = str[i]

if ( (ch >= 'a' and ch <= 'z') or


(ch >= 'A' and ch <= 'Z') ):

# To handle upper case letters


ch = ch.lower()

if (ch == 'a' or ch == 'e' or ch == 'i'


or ch == 'o' or ch == 'u'):
vowels += 1
else:
consonant += 1

elif (ch >= '0' and ch <= '9'):


digit += 1
else:
specialChar += 1

print("Vowels:", vowels)
print("Consonant:", consonant)
print("Digit:", digit)
print("Special Character:", specialChar)

# Driver function.
str = "Vikasites121"
countCharacterType(str)

10. Input a string and determine whether it is a palindrome or not.

string=raw_input("Enter string:")

5
if(string==string[::-1]):
print("The string is a palindrome")
else:
print("The string isn't a palindrome")

11. Find the largest/smallest number in a list/tuple

st = []
num = int(input('How many numbers: '))
for n in range (num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Maximum element in the list is :", max(lst))
print(“Minimum element in the list is :", min(lst))

12. Input a list of numbers and swap elements at the even location with the elements
at the odd location.

val=eval(input("Enter a list "))


print("Original List is:",val)
s=len(val)
if s%2!=0:
s=s-1
for i in range(0,s,2):
val[i],val[i+1]=val[i+1],val[i]
print("List after swapping :",val)

13. Input a list/tuple of elements, search for a given element in the list/tuple.

mylist = []
print("Enter 5 elements for the list: ")
for i in range(5):
value = int(input())
mylist.append(value)
print("Enter an element to be search: ")
element = int(input())
for i in range(5):
if element == mylist[i]:
print("\nElement found at Index:", i)
print("Element found at Position:", i+1)

14. 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 n: "))
d={}
for i in range(n):
roll_no=int(input("Enter roll no: "))
name=input("Enter name: ")

6
marks=int(input("Enter marks: "))
d[roll_no]=[name,marks]

for k in d:
if(d[k][1]>75):
print(d[k][0])

15. Write a Python script to merge two Python dictionaries.


d1 = {'a': 100, 'b': 200}
d2 = {'x': 300, 'y': 200}
d = d1.copy()
d.update(d2)
print(d)

16. Write a Python program to iterate over dictionaries using for loops

d={"Name":"Ram" , "Age":23 , "City": "Salem", "Gender": "Male"}


for k, v in d.items():
print(k, ' : ', d[k])

17. Write a Python program to Replace words from Dictionary.

val = "Tutor alok's Computer Education"


print("Original string : ",val)
dic = {"Computer" : "Software ", "Education" : "Solution"}
word = val.split()
res = []
for w in word:
res.append(dic.get(w, w))
res = ' '.join(res)
print("Replaced Strings : " ,res)

18. Write a Program to remove the empty list from a list of lists.

# Python program to remove empty list


# from a list of lists
# Initializing list of lists
listofList = [[5], [54, 545,9], [], [1, 4, 7], [], [8, 2, 5] ]

# Printing original list


print("List of List = ", end = " ")
print(listofList)
nonEmptyList = []

# Iterating using loop and eliminating empty list


nonEmptyList = [listEle for listEle in listofList if listEle != []]
# Printing list without empty list
print("List without empty list: ", end = " " )
print(nonEmptyList)

19. Write a Program to find the cumulative sum of elements in a list


# Python program to find cumulative sum
# of elements of list

7
# Getting list from user
myList = []
length = int(input("Enter number of elements : "))
for i in range(0, length):
value = int(input())
myList.append(value)

# finding cumulative sum of elements


cumList = []
sumVal = 0
for x in myList:
sumVal += x
cumList.append(sumVal)

# Printing lists
print("Entered List ", myList)
print("Cumulative sum List ", cumList)

20. Write a Program to convert a Tuple matrix to Tuple list.

# Python3 code to demonstrate working of


# Convert Tuple Matrix to Tuple List

# initializing list
test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]

# printing original list


print("The original list is : " + str(test_list))

x=[]
for i in test_list:
for j in i:
j=list(j)
x.extend(j)
re1=[]
re2=[]
for i in range(0,len(x)):
if(i%2==0):
re1.append(x[i])
else:
re2.append(x[i])
res=[]
res.append(tuple(re1))
res.append(tuple(re2))

# printing result
print("The converted tuple list : " + str(res))

You might also like