Cs Practical File
Cs Practical File
arithmetic operations
OUTPUT:
Enter first number: 8
Enter second number: 3
Results:-
Addition: 11
Subtraction: 5
Multiplication: 24
Division: 2.6666666666666665
Modulus: 2
Floor Division: 2
Exponentiation: 512
#Program to tell the user when they will turn 100 Years
old
name = input("Enter your name: ")
age = int(input("Enter your age: "))
hundred = 2020 + (100 - age)
print("Hi",name,"! You will turn 100 years old in the
year",hundred)
OUTPUT:
Enter your name: John
Enter your age: 15
Hi John ! You will turn 100 years old in the year 2105
OUTPUT:
OUTPUT:
OUTPUT:
Enter the number: 19
Enter the number: 5
Enter the number: 21
Enter the number: 50
Enter the number: 7
The smallest number is 5
The largest number is 50
OUTPUT 1:
Enter the Number: 227722
The entered number is a palindrome.
OUTPUT 2:
Enter the Number: 13361
The entered number is not a palindrome.
OUTPUT:
Enter your name: Vidyut
Enter your gender: M for Male, and F for Female: M
Hello, Mr. Vidyut
Program:
#Program that creates a GK quiz consisting of any five
questions of your choice. The questions should be
displayed randomly. Create a user defined function
score() to calculate the score of the quiz and another
user defined function remark (scorevalue) that accepts
the final score to display remarks
import random
def score():
s = 0;
for i in range(0,5):
if(userInp[i] == ans[sequence[i]]):
s += 1
return s
def remark(score):
print(remarks[score])
def disp(r):
print(quiz[r])
inp = input("Answer:")
userInp.append(inp.upper())
sequence.append(r)
i = 0;
while i < 5:
r = random.randint(0, 4)
if(r not in sequence):
i += 1
disp(r)
s = score()
print("Your score :", s, "out of 5")
remark(s)
Output:
Which Indian state has the highest literacy rate?
Answer:kerala
Which is India's largest city in terms of population?
Answer:delhi
What is the national song of India?
Answer: vande mataram
What is the capital of Uttar Pradesh?
Answer: lucknow
How many states are in North-East India?
Write the answer in words.
Answer:three
Your score : 3 out of 5
Good
Program:
#Input a string having some digits. Write a function to
return the sum of digits present in this string
def sumDigit(string):
sum = 0
for a in string:
if(a.isdigit()):
sum += int(a)
return sum
result = sumDigit(userInput)
print("The sum of all digits in the string",userInput,"'
is:",result)
OUTPUT:
Enter any string with digits: The cost of this book is
Rs. 380
The sum of all digits in the string ' The cost of this
book is Rs. 3450 ' is: 11
OUTPUT:
OUTPUT:
list1 = list()
inp = int(input("How many elements do you want to add in
the list? (Element can be both positive and negative) "))
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
print("The list with all the elements: ",list1)
list2 = list()
list3 = list()
for j in range(inp):
if list1[j] < 0:
list3.append(list1[j])
else:
list2.append(list1[j])
OUTPUT:
How many elements do you want to add in the list?
(Element can be both positive and negative) 5
Enter the elements: -1
Enter the elements: -2
Enter the elements: -3
Enter the elements: 4
Enter the elements: 5
The list with all the elements: [-1, -2, -3, 4, 5]
The list with positive elements: [4, 5]
The list with negative elements: [-1, -2, -3]
def medianValue(list1):
list1.sort()
indexes = len(list1)
if(indexes%2 == 0):
num1 = (indexes) // 2
num2 = (indexes // 2) + 1
med = (list1[num1 - 1] + list1[num2 - 1]) / 2
return med
else:
middle = (indexes - 1) // 2
med = list1[middle]
return med
list1 = list()
inp = int(input("How many elements do you want to add in
the list? "))
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
print("The median value is",medianValue(list1))
OUTPUT:
How many elements do you want to add in the list? 6
Enter the elements: 1
Enter the elements: 2
Enter the elements: 3
Enter the elements: 4
Enter the elements: 5
Enter the elements: 6
The median value is 3.5
list1 = []
inp = int(input("How many elements do you want to add in
the list? "))
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
print("The list entered is:",list1)
print("The list without any duplicate element
is:",removeDup(list1))
OUTPUT:
How many elements do you want to add in the list? 6
Enter the elements: 1
Enter the elements: 1
Enter the elements: 2
Enter the elements: 2
Enter the elements: 3
Enter the elements: 4
The list entered is: [1, 1, 2, 2, 3, 4]
The list without any duplicate element is: [1, 2, 3, 4]
emails = tuple()
username = tuple()
domainname = tuple()
n = int(input("How many email ids you want to enter?: "))
for i in range(0,n):
emid = input("> ")
emails = emails +(emid,)
spl = emid.split("@")
username = username + (spl[0],)
domainname = domainname + (spl[1],)
OUTPUT:
How many email ids you want to enter?: 3
> [email protected]
> [email protected]
> [email protected]
dic={"A":12,"B":13,"C":9,"D":89,"E":34,"F":17,"G":65,"H":
36,"I":25,"J":11}
lst = list()
for a in dic.values():
lst.append(a)
lst.sort()
print("Highest value:",lst[-1])
print("Second highest value:",lst[-2])
OUTPUT:
Highest value: 89
Second highest value: 65
Program:
#Count the number of times a character appears in a given
string
st = input("Enter a string: ")
dic = {}
for ch in st:
if ch in dic:
dic[ch] += 1
else:
dic[ch] = 1
print(dic)
OUTPUT:
Enter a string: interference
{'i': 1, 'n': 2, 't': 1, 'e': 4, 'r': 2, 'f': 1, 'c': 1}
OUTPUT: