CS Project
CS Project
CS Project
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}
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 ?
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'?
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.?
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 ?
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’] ?
The End