Computerscience Assignment
Computerscience Assignment
String Functions
print(phrase.capitalize())
2. sentence = "learn python programming"
print(sentence.title())
3. word = "UPPERCASE"
print(word.lower())
4. string = "lowercase"
print(string.upper())
5. text = "Python is powerful. Python is versatile."
print(text.count("Python"))
6. phrase = "The quick brown fox"
print(phrase.find("brown"))
7. word = "Hello, World!"
print(word.index("World"))
8. sentence = "Welcome to Python"
print(sentence.endswith("Python"))
9. string = "Python123"
print(string.startswith("Python"))
10. text = "OpenAI123"
print(text.isalnum())
11. phrase = "OpenAI"
print(phrase.isalpha())
12. word = "12345"
print(word.isdigit())
13. sentence = "pythonrocks"
print(sentence.islower())
14. string = "UPPERCASE"
print(string.isupper())
15. text = " "
print(text.isspace())
16. phrase = " Leading and Trailing Spaces "
print(phrase.lstrip())
17. word = " Spaces on the Right "
print(word.rstrip())
18. sentence = " Strip Spaces on Both Sides "
print(sentence.strip())
19. string = "Replace the old word"
print(string.replace("old", "new"))
20. text = "python"
print("#".join(text))
Assignment 3
List
1. Write the most appropriate list method to perform the following tasks:
(i) To delete 4th element from the list.
(ii) To get the position of an item in the list.
(iii) To add an element in any position
(iv) To removes all elements
2. Suppose L=[“xyz”,6,9,”the”,2019,’a’,’b’,’s’,’x’]
Consider the above list and give output for the following:
(i) print(L[2:6])
(ii) print(L.index(2019))
(iii) print(len(L))
(iv) print(L.count(‘x’))
(v) print(L.pop(3))
(vi) print(L.remove(‘a’))
(vii) L.insert(2,’Sonia’)
print(L)
(viii) L.clear()
print(L)
3. Write the output:
my_list = [3, 8, 1, 6, 0, 8, 4]
my_list.sort()
print(my_list)
my_list.reverse()
print(my_list)
4. Based on the following python list, write the output of the questions:
L=[(“A”, “B”, “C”), [10,20,30], “Paramount Public School”, 11.0)
(i) print(L[0][1])
(ii) print(L[1][2])
(iii) print(L[2][4])
(iv) print(L[1])
(v) print(L[2])
(vi) pint(L[2][2:7])
5. Give the output of the following code:
a=[10,20,'abc',30,3.14,40,50]
print(a)
for i in range(0,len(a)):
print(a[i], end=' ')
print('\n')
for i in range(len(a)-1,-1,-1):
print(a[i], end=' ')
print('\n')
for i in a[::-1]:
print(i, end=' ')
print('\n')
for i in reversed(a):
print(i, end=' ')
6. What will be the output of following code?
>>>a = [[[4,5],[1,0],9],[6,7]]
>>>a[0][1][1]
7. What does each of the following expressions evaluates to ?
Suppose that L is the list
[‘These’,[‘are’,’a’,’few’,’words’],’that’,’we’,’will’,’use’]
(a) L[1][3][1:]
(b) L[2][1].upper()
8. Predict an output:
Odd=[3,4,5]
print((Odd+[12,13,14])[3])
print((Odd+[78,89,90])[4] – (Odd+[2,4,6])[4])
9. Predict an output of the following:
my_list=[‘c’,’l’,’a’,’s’,’s’,’i’,’c’]
my_list[2:3]=[]
print(my_list)
my_list[2:5]=[]
print(my_list)
Assignment 4
Tuples
Tuple Initialization:
1. Write a Python code snippet to create a tuple named months containing the names of
the months in a year.
2. Create a tuple named countries containing the names of five different countries.
Tuple Concatenation:
3. Concatenate two tuples tuple_a = (1, 2, 3) and tuple_b = (4, 5, 6) and print the result.
4. Concatenate two tuples tuple_x = (10, 20, 30) and tuple_y = (40, 50, 60) and store the
result in a new tuple.
Tuple Repetition:
5. Create a tuple named repeat_tuple that repeats the values (7, 8, 9) three times.
6. Create a tuple named repeat_tuple that repeats the values (3, 6, 9) four times.
Membership Test:
7. Check if the value 5 is present in the tuple test_tuple = (2, 4, 6, 8, 10) and print the
result.
8. Check if the value 15 is present in the tuple test_values = (10, 15, 20, 25, 30) and print
the result.
Tuple Slicing:
9. Given the tuple numbers = (1, 2, 3, 4, 5), print a new tuple containing the elements
from index 1 to 3.
10. Given the tuple letters = ('a', 'b', 'c', 'd', 'e'), print a new tuple containing elements from
index 2 to the end.
11. Given the tuple numbers = (11, 22, 34, 45, 56), print a new tuple containing elements
from index 2 to 3.
Tuple Indexing:
25. Find and print the third element of the tuple colors = ("red", "green", "blue",
"yellow").
26. Access the second element of the tuple marks = (85, 90, 75, 88, 92) and print its
value.
27. Consider the tuple languages = ("Python", "Java", "C++", "JavaScript"). Write a
Python code snippet to print the third element of the tuple.
28. Given the tuple grades = (85, 90, 78, 92, 88), write a Python code snippet to find the
index of the element 78.
Tuple Operations:
29. Create a tuple tuple_a = (1, 2, 3) and a tuple tuple_b = (4, 5, 6). Perform tuple
multiplication and print the result.
30. Create a tuple tuple_p = (3, 6, 9) and a tuple tuple_q = (2, 5, 8). Perform tuple
concatenation and print the result.
31. Create two tuples tuple_a = (1, 2, 3) and tuple_b = (4, 5, 6). Concatenate these tuples
and print the result.
32. Write a Python code snippet to repeat the elements of the tuple numbers = (2, 4, 6)
three times
Dictionaries Assignment-2
1. Given below are two lists: convert them into a dictionary
keys = ['A', 'E', 'I', ‘O’, ‘U’]
values = [“Vowels”]
2. Merge following two Python dictionaries into one
dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Forty': 40, 'Fifty': 50}
3. Given below is a dictionary, marks.
Marks = { 'Physics': 82, 'Math': 65, 'history': 75 ‘CS’:99 }
Find the subject with minimum and maximum marks.
4. WAP to implement library usning a dictionary. The library has the following
fields: Bookno, Author, Title, Noofcopies
The progrm should do the following:
• Add a book
• Delete a given book
• Search for the book in the library
• Give the number of copies of each book
5. Given a Python dictionary, Salesman, change Kiranpreet’s salary to 60000
Salesmant = { 'SNO1': {'name': 'Dheeraj', 'salary':65000},
'SNO2': {'name': 'Meenakshi', 'salary': 80000},
'SNO3': {'name': 'Kiranpreet', 'salary': 45000},
‘SNO4:{‘name’:’Pritika’, ‘salary’ : 67000}
}
6. WAP to merge two dictionaries classxic and classxid to form a new dictionary,
XICS. Also count the total number of students in the new class.
The fields in the dictionaries are:
Rollno, name,class, marks
7. Given a dictionary employee having fields empno and salary. Arrange the
records in ascending order of salary.
Assignment 6
Functions
Q 1. Rewrite the following code after removing error. Underline each correction done
by you.
def SI(p,t=2,r):
return (p*r*t)/100
def chksum :
x = input ( “Enter a number”)
if (x % 2 = 0) :
for i range ( 2 * x ):
print (i)
loop else:
print ( “#”)
def Tot(Number) #Method to find Total
Sum=0
for C in Range (l, Number+l):
Sum+=C
RETURN Sum
print Tot[3] #Function Calls
print Tot[6]
Q 5. Name the Python Library modules which need to be imported to invoke the following functions:
(i) sin( ) (ii) randint ( ) (iii) uniform( ) (iv) ceil( )
(b) x = 50
def func():
global x
print('x is', x)
x=2
print('Changed global x to', x)
func()
print('Value of x is', x)
(c) def func(a, b=5, c=10):
print('a is', a, 'and b is', b, 'and c is', c)
func(3, 7)
func(25, c = 24)
func(c = 50, a = 100)
Assignment -7
Files
Assignment 8
BINARY FILE HANDLING & CSV
f = open("Emp.txt","rb")
s= #code to get first record
print(s.decode())
# code to position at 5th
record
s = f.read(size)
print(s.decode())
Write Python function DISPEMP() to read the content of file emp.csv and display only those
records where salary is 4000 or above
9. Write a python function CSVCOPY() to take sourcefile, targetfile as parameter and create a
targetfile and copy the contents of sourcefile to targetfile
10. Diffrentiate between reader and writer?
11. Differntiate between Seekg and tellg
Assignment 9
RDBMS
3 Rajesh a database developer at Store India wants to search the record of those employees
whose name starts from „R‟ and they have not allotted any project, for this he has
written the following query-
Select * from Employee where Name = ‘R%’ and Project=Null;
But the query is not producing the correct output. Rewrite the query after correcting
the errors
4 Which clause is used to see the output of query in ascending or descending order?
7. Raj is a database programmer, He has to write the query from EMPLOYEE table to search for the
employee whose name begins from letter „R‟, for this he has written the query as:
SELECT * FROM EMPLOYEE WHERE NAME=‟R%‟;
But the query is not producing the correct output, help Raj and correct the query so that he gets the
desired output.
8. Raj is a database programmer, has to write the query from EMPLOYEE table to search for the
employee who are working in „Sales‟ or „IT‟ department, for this he has written the query as:
SELECT * FROM EMPLOYEE WHERE department=‟Sales‟ or „IT‟;
But the query is not producing the correct output, help Raj and correct the query so that he gets
the desired output.
9. If Table Sales contains 5 records and Raj executed the following queries; find out the output of
both the query.
(i) Select 100+200 from dual;
(ii) Select 100+200 from Sales;
10. What is the difference between Equi-Join and Natural Join?
11. What is the difference between CHAR and VARCHAR?
12. Which SQL function is used to get the average value of any column?
13. What is the Difference between ALTER Table command and UPDATE command?
14. Write MYSQL command to see the list of tables in current database
15. A table Employee contains 5 Rows and 4 Columns and another table PROJECT contains
5 Rows and 3 Columns. How many rows and columns will be there if we obtain
Cartesian product of these two tables?
16. DIFFERENCE BETWEEN
1. HAVING AND WHERE
2. % AND _
Assignment10
SQL
1. Observe the given Table TEACHER and give the output of question (i) and (ii)
4. Rajesh a database developer at StoreIndia wants to search the record of those employees whose
name starts from „R‟ and they have not allotted any project, for this he has written the following
query-
Select * from Employee where Name = ‘R%’ and Project=Null;
But the query is not producing the correct output. Rewrite the query after correcting the errors
(i) To display details of all Trains which starts from New Delhi
(ii) To display PNR, PNAME, GENDER and AGE of all passengers whose AGE is below 50
(iv) To display records of all passengers travelling in trains whose TNO is 12015
(vi) SELECT END, COUNT(*) FROM TRAINS GROUP BY END HAVING COUNT(*)>1;
(viii) SELECT TNAME, PNAME FROM TRAINS T, PASSENGERS P WHERE T.TNO=P.TNO AND AGE
BETWEEN 50 AND 60
1. Which of the following commands will remove the entire database from
MYSQL?
(A) DELETE (B) DROP (C) REMOVE (D) ALTER
2. __________ is a non-key attribute, whose values are derived from the
primary key of some other table.
(A) Primary Key (B) Candidate Key (C) Foreign Key (D) Alternate Key
3.The SELECT statement when combined with clause, returns
records withoutrepetition.
(A) DISTINCT (B) DESCRIBE (C) UNIQUE (D) NULL
4. Which of the following commands will delete the rows of table?
(a) DELETE (b) DROP (c) REMOVE (d) ALTER
5. Logical Operators used in SQL are?
(a) AND,OR,NOT (b) &&,||,! (c) $,|,! (d) None of these
6. Which of the following ignores the NULL values inSQL?
a)Count(*) b) count() c)total(*) d)None of these
1. Write the output of the queries (i) to (vi) based on the table given below
TABLE:
CHIPS
BRAND_NAME FLAVOUR PRICE QUNATIT
Y
LAYS ONION 10 5
LAYS TOMATO 20 12
UNCLE CHIPS SPICY 12 10
UNCLE CHIPS PUDINA 10 12
HALDIRAM SALTY 10 20
HALDIRAM TOMATO 25 30
(i) Select BRAND_NAME, FLAVOUR from CHIPS where PRICE <> 10;
(ii) Select * from CHIPS where FLAVOUR=”TOMATO” and PRICE > 20;
(iii) Select BRAND_NAME from CHIPS where price > 15 and QUANTITY < 15;
(iv) Select count( distinct (BRAND_NAME)) from CHIPS;
(v) Select price , price *1.5 from CHIPS where FLAVOUR = “PUDINA”;
(vi) Select distinct (BRAND_NAME) from CHIPS order by BRAND_NAME desc;
2. Consider the following tables BOOKS and ISSUED in a database named “LIBRARY”.
WriteSQL commands for the statements (i) to (iv).
Table:BOOKS
(i) Display book name and author name and price of computer type books.
(ii) To increase the price of all history books by Rs 50.
(iii) Show the details of all books in ascending order of their prices.
(iv) To display book id, book name and quantity issued for all books which have been
issued.
6. Based on the given set of tables write answers to the following questions.
Table: flights
flightid model company
10 747 Boeing
12 320 Airbus
15 767 Boeing
Table: Booking
ticketno passenger source destination quantity price Flightid
10001 ARUN BAN DEL 2 7000 10
10002 ORAM BAN KOL 3 7500 12
10003 SUMITA DEL MUM 1 6000 15
10004 ALI MUM KOL 2 5600 12
10005 GAGAN MUM DEL 4 5000 10
a) Write a query to display the passenger, source, model and price for all
bookings whose destination is KOL.
b) Identify the column acting as foreign key and the table name where it
is present in the given example.
Write a query to delete all the rows from the table which are not
having any rating.
8. Write the output of the queries (a) to (d) based on the table
Which field will be considered as the foreign key if the tablesCOUNTRY and CAPITAL are related in
a database?
9.Write the outputs of the SQL queries (i) to (iii) based on the relations Teacher and
Posting given below:
Table: Stationary
S_ID StationaryName Company Price
DP01 Dot Pen ABC 10
PL02 Pencil XYZ 6
ER05 Eraser XYZ 7
PL01 Pencil CAM 5
GP02 Gel Pen ABC 15
10. Write a output for SQL queries (i) to (iii), which are based on the table: SCHOOL and
ADMIN givenbelow:
i) SELECT SUM (PERIODS), SUBJECT FROM SCHOOL GROUP BY SUBJECT;
ii) SELECT TEACHERNAME, GENDER FROM SCHOOL, ADMIN WHERE
DESIGNATION = ‘COORDINATOR’ AND SCHOOL.CODE=ADMIN.CODE;
iii) SELECT COUNT (DISTINCT SUBJECT) FROM SCHOOL;
11.Write SQL commands for the following queries (i) to (v) based on the relation