Cs Practical File

You might also like

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

#Program to input two numbers and performing all

arithmetic operations

num1 = int(input("Enter first number: "))


num2 = int(input("Enter second number: "))
print("Results:-")
print("Addition: ",num1+num2)
print("Subtraction: ",num1-num2)
print("Multiplication: ",num1*num2)
print("Division: ",num1/num2)
print("Modulus: ", num1%num2)
print("Floor Division: ",num1//num2)
print("Exponentiation: ",num1 ** num2)

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:

#Program to input five numbers and print largest and


smallest numbers
smallest = 0
largest = 0
for a in range(0,5):
x = int(input("Enter the number: "))
if a == 0:
smallest = largest = x
if(x < smallest):
smallest = x
if(x > largest):
largest = x
print("The smallest number is",smallest)
print("The largest number is ",largest)

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

#Program to find if the number is a palindrome


rev = 0
n = int(input("Enter the number: "))
temp = n
while temp > 0:
digit = (temp % 10)
rev = (rev * 10) + digit
temp = temp // 10
if(n == rev):
print("The entered number is a palindrome.")
else:
print("The entered number is not a palindrome.")

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.

#Program that uses a user-defined function that accepts


name and gender (as M for Male, F for Female) and
prefixes Mr/Ms on the basis of the gender
def prefix(name,gender):
if (gender == 'M' or gender == 'm'):
print("Hello, Mr.",name)
elif (gender == 'F' or gender == 'f'):
print("Hello, Ms.",name)
else:
print("Please enter only M or F in gender")

name = input("Enter your name: ")


gender = input("Enter your gender: M for Male, and F for
Female: ")
prefix(name,gender)

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

quiz = ["What is the capital of Uttar Pradesh?","How many


states are in North-East India?\nWrite the answer in
words.","Which is India\'s largest city in terms of
population?","What is the national song of India?","Which
Indian state has the highest literacy rate?"]
ans = ["LUCKNOW","EIGHT","MUMBAI","VANDE
MATARAM","KERALA"]
userInp = []
sequence = []
remarks =["General knowledge will always help you. Take
it seriously", "Needs to take interest", "Read more to
score more", "Good", "Excellent", "Outstanding"]

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

userInput = input("Enter any string with digits: ")

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:

#Program to read a list of n integers (positive as well


as negative). Create two new lists, one having all
positive numbers and the other having all negative
numbers from the given list. Print all three lists

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])

print("The list with positive elements: ",list2)


print("The list with negative elements: ",list3)

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]

#Program to read a list of n integers and find their


median

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

#function to remove the duplicate elements


def removeDup(list1):
length = len(list1)
newList = []
for a in range(length):
if list1[a] not in newList:
newList.append(list1[a])
return newList

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]

#Program to read email id of n number of students. Store


these numbers in a tuple. Create two new tuples, one to
store only the usernames from the email IDs and second to
store domain names from the email ids. Print all three
tuples at the end of the program.

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],)

print("\nThe email ids in the tuple are:")


print(emails)

print("\nThe username in the email ids are:")


print(username)
print("\nThe domain name in the email ids are:")
print(domainname)

OUTPUT:
How many email ids you want to enter?: 3
> alpha@gmail.com
> vashishth@yahoo.com
> bingo@think.com

The email ids in the tuple are:


('alpha@gmail.com','vashishth@yahoo.com','bingo@think.com
')

The username in the email ids are:


('abcde', 'test1', 'testing')

The domain name in the email ids are:


('gmail.com', 'meritnation.com', 'outlook.com')

#Write a Python program to find the highest 2 values in a


dictionary

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:

You might also like