CS Project

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

VIBGYOR INTERNATIONAL SCHOOL

Computer science project

Class & section


XI Science
Non-medical

Submitted by : Deepanshu Tomer

Submitted to : Mr. Akhil tyagi


 SOME PROGRAMS ON DATA TYPES

1. Write a code fragment to generate a random floating number between 45.0 and 95.0.
print this number along with nearest integer greater than it?

Import random
import math
fnum = random.random() * (95-45) +45
inum = math.ceil(fnum)
print(“Random numbers between 45..95:”) print(fnum)
print(“Nearest higher integer:”, inum)
Output:
Random numbers between 45..95 :
48.24212504903489
Nearest higher integer: 49

2. Write a code fragment to generate two random integer between 450 and 950. Print these
two numbers along with their average?

import random
num1 = random.randint(450, 950) – 450
num2 = random.randint(450, 950) – 450
avg = (num1 + num2)/2
print("Random integers in the\ range 450 to 950:", num1,
num2)
print("Their average : ", avg)
Output:
Random integers in the range 450 to 950 472 145 Their
average: 308.5
3. Write a code fragment to generate three random integers in the range 10,70 with a step
of 13. Create a set with these numbers ?

import random
num1 = random.randrange (10, 70, 13)
num2 = random.randrange (10, 70, 13)
num3 = random.randrange (10, 70, 13)
set1 = {num1, num2, num3}
print(“Random integers in the range 18..78,\ step-value
13:”, num1, num2, num3) print(“Set created:”, set1)
Output:
Random integers in the range 10..70,
step-value 13: 10 10 62
Set created: {10, 62}

4. Given a list containing these values [22,13,28,13,22,25,7,13,25]. Write code to calculate


mean, median and mode of this list ?

import statistics as stat


list1 = [ 22, 13, 28, 13, 22, 25, 7, 13, 25]
list_mean = stat.mean(list1)
list_median = stat.median (list1)
list_mode = stat.mode (list1)
print("Given list : ", list1)
print("Mean : ", list_mean)
print("Median : ", list_median)
print("Mode:", list_mode)
Output:
Given list: [22, 13, 28, 13, 22, 25, 7, 13, 25]
Mean : 18.666666666666668
Median : 22
Mode: 13
o SOME PROGRAMS ON STRING MANIPULATION

1. Program to read a string and display it in reverse order-display one character Do not
create a reverse string, just display in reverse onder ?

string1-input("Enter a string :")


print ("The", string1, "in reverse order is:")
length-len(string1)
for a in range(-1, (-length-1), -1):
print (string[a])
Output :
Enter a string : python
The python in reverse order is:
N
O
H
T
Y
P

2. Program to read a string and display it in the form: ?


string1 input("Enter a string :")
length len(string1)
i=0
for a in range(-1, (-length-1), -1):
print (string1[1], "\t", stringi(a))
i+= 1
Output :
Enter a string : python
P N
Y O
T H
H T
O Y
N P
3. Write a program to input an integer and check if it contains any 0 in it.
n=int(input(“Enter a number :”))
s = str(n)
if ‘0’ in s:
print(“There’s a 0 in”, n)
else:
print(“No e in”, n)
Output :
Enter a number : 6708
There's a 0 in 6708

4. Write a program that asks a user for a username and a pcode. Make sure that pcode does
not contain username and matches 'Trident111'?

uname = input ("Enter user name :")


pcode input ("Enter code :")
if uname not in pcode and pcode == 'Trident111':
print("Your code is Valid to proceed.")
else:
print("Your code is Not Valid.")
Output :
Enter user name : Raina
Enter code : Xerxes111
Your code is Not valid.
Thank you
o SOME PROGRAMS ON LIST MANIPULATION
1. a program that inputs a list, replicates it twice and then prints the sorted list in ascending
and descending orders. ?
val = eval(input(“Enter a list :”))
print(“Original list : “, val)
val = val * 2
print(“Replicated list :”, val)
val.sort()
print(“Sorted in ascending order :”, val)
val.sort(reverse=True)
print(“Sorted in descending order : “, val)
Output :
Enter a list : [23, 11, 29]
original list: [23, 11, 29]
Replicated list: [23, 11, 29, 23, 11, 29]
Sorted in ascending order: [11, 11, 23, 23, 29, 29]
Sorted in descending order : [29, 29, 23, 23, 11, 11]

2. Program to calculate the mean of a given list of numbers. ?

1st = eval(input(“Enter list :”))


length = len(lst)
mean sum = 0
for I in range(0, length) :
sum += lst[i]
mean = sum/length
print(“Given list is : “, lst)
print(“The mean of the given list is :”, mean)
Output :
Enter list: [7, 23, -11, 55, 13.5, 20.05, -5.5]
Given list is: [7, 23, -11, 55, 13.5, 20.05, -5.5]
The mean of the given list is : 14.578571428571427
3. Write a program to check if the maximum element of the list lies in the first half of the list
or second half ?

1st eval(input("Enter a list:"))


Ln-len(1st)
mx = max(1st)
ind-1st.index(mx)
if ind <= (In/2):
print("The maximum element", mx, "lies in the 1st
half.")
else:
print("The maximum element", mx, "lies in the 2nd
half.")
Output :
Enter a list: [6, 8, 11, 6, 12, 7, 16]
The maxinum element 16 lies in the 2nd half.

4. Write a program to input two lists and display the maximum element from the elemem of
both the lists combined, along with its indes in its list.?

Lst1- eval(input("Enter listi:"))


1st2-eval(input("Enter list2:"))
mx1 = max(1st1)
mx2 = max(1st2)
if mx1 >= mx2:
print (mx1, "the maximum value is in listi at index, isti.index
(xi))
else:
print(mx2, "the maximum value is in list2 at index",
1st2.index(m2))
Output :
Enter list1: [6, 8, 11, 6, 12, 7, 16]
Enter list2: [34, 11, 23, 11]
34 the maximum value is in list2 at index 0
 SOME PROGRAMS ON TUPLES

1. a program to create three tuples from the inputs given below:


(i)"abcdef” (ii) 3, 4, 5, 6
(iii) [11, 12, 13]
t1-tuple(eval (input("Enter input for tuple1: ")))
t2-tuple (eval (input("Enter input for tuple2: ")))
t3-tuple (eval (input("Enter input for tuple3: ")))
print("Tuple 1:", t1)
print("Tuple 2:", t2)
print("Tuple 3:", t3)
Output :
Enter input for tuplel: "abcdef"
Enter input for tuple2: 3, 4, 5, 6
Enter input for tuple3: [11, 12, 13]
Tuple 1: ('a', 'b', 'c', 'd', 'e', 'f')
Tuple 2 (3, 4, 5, 6)
Tuple 3 (11, 12, 13)

2. What will be the types of t1, t2, t3 created as per the code given below considering the
three inputs of example 1?
t1 = eval(input(“Enter input for tuple1 : “))
t2 = eval(input(“Enter input for tuple2 : “))
t3 = eval(input(“Enter input for tuple3 : “))
print(“Type of t1 : “, type(t1))
print(“Type of t2:”, type(t2))
print(“Type of t3 : “, type(t3))
Output :
Enter input for tuplel: “abcdef”
Enter input for tuple2 : 3, 4, 5, 6
Enter input for tuple3 : [11, 12, 13]
Type of t1: <class ‘str’>
Type of t2 : <class ‘tuple’>
Type of t3: <class ‘list’>
3. Write a program to create a tuple with a single input value.?
T1 = eval(input(“Enter input for tuple: “))
print(“Created tuple is:”, t1)
Output :
Enter input for tuple : 67,
Created tuple is: (67,)

4. Given below is a code, which is trying to create a tuple from a single element and its
sample run. Why is the created variable not of tuple type?
t1 = eval(input("Enter input for tuple: "))
print("Type of t1:", type(t1))
Output :
Enter input for tuple : 67
Type of tl: <class 'int'>
 SOME PROGRAMS ON DICTIONARIES
1. Consider the dictionary created in the previous program (program 13.1) Modify the previous
program to check if the roll number 2 has scored more than 75 marks ?

If d[2] > 75:


print(“Roll number 2 scored”, d[2], “(>75)”)
else :
print(“Roll number 2 scored”, d[2], “(<75)”)
Output :
Created dictionary
{1: 67.5, 2: 45.6, 3: 78.4, 4: 70.5}
Roll number 2 scored 45.6 (< 75)

2. Write a program to create a dictionary M which stores the marks of the students of class
with roll numbers as the keys and marks as the values. Get the number of students as
input. ?

M= {}
n = int(input("How many students?"))
for a in range(n):
r, m = eval(input("Enter Roll No., Marks :"))
M[r] = m
print("Created dictionary")
print (M)
Output :
How many students? 5
Enter Roll No., Marks :1, 67.3
Enter Roll No., Marks :2, 54.5
Enter Roll No., Marks :3, 77.9
Enter Roll No., Marks :4, 89.9
Enter Roll No., Marks :5, 83.5
Created dictionary
{1: 67.3, 2: 54.5, 3: 77.9, 4: 89.9, 5: 83.5)
3. Write a program to create a dictionary namely Numerals from following two lists keys =
[‘One’, ‘Two’, ‘Three’] ?

Keys = [ ‘One’, ‘Two’, ‘Three’]


values = [1, 2, 3]
Numerals = dict(zip (keys, values))
print(“Given two lists are:”, keys, values)
print(“Dictionary created with these lists is”)
print (Numerals)
Output :
Given two lists are : [‘one’, ‘Two’, ‘Three’] [1, 2, 3]
Dictionary created with these lists is
{‘One’: 1, ‘Two’: 2, ‘Three’: 3}

The End

You might also like