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

# AB0403 Revision Set 2

# *** Not for re-distribution ***

# Q1. Does readline() take in the "\n" at the end of line?

(a.txt must be in the same folder as the .py file, if we only put “a.txt” in the open() function)

“C:\Users\...\Desktop\a.txt” contains below content


Line 1
Line 2
Line 3

“C:\Users\...\Desktop\Q1.py” contains below code


s=[]

with open("a.txt", "r") as f:


s.append(f.readline())
s.append(f.readline())
s.append(f.readline())
print(s)

# Q2. If we PRINT the elements of s from Q1 to a file, one element at a time, what will happen? Do
you see extra empty lines?

with open("b.txt", "w") as f:


for elem in s:
print(elem, file = f)

# Q3. How do we remove the extra empty lines you see in Q2, if we still use print() function?

with open("b.txt", "w") as f:


for elem in s:
print(elem.strip(), file = f)

# Q4. If we WRITE the elements of s from Q1 to a file, one element at a time, what will happen?
Do you see extra empty lines?

with open("b.txt", "w") as f:


for elem in s:
f.write(elem)

# Q5. Does the code below work?

with open("b.txt", "w") as f:


for elem in s:
f.writeline(elem)

# Q6. Does below code work? Are 1, 2, 3 on three different lines?

with open("b.txt", "w") as f:


s=["1","2","3"]
f.writelines(s)

'''

“C:\Users\...\Desktop\a.csv" contains below content with two empty lines at the end
Name,Age
HB,33
Jane,23

'''
# Q7. Does the code below work? What will the list look like?
import csv
s = []
with open("a.csv", "r") as f:
csv_pointer = csv.reader(f)
for each in csv_pointer:
s.append(each)
print(s)

# Q8. What will the file look like? Why are there empty lines between the 2 lines?
row = ["a","b","c"]
with open("b.csv", "w") as f:
csv_pointer = csv.writer(f)
csv_pointer.writerow(row)
csv_pointer.writerow(row)

# Refer to https://docs.python.org/3/library/functions.html#open and ‘newline’ parameter

# Q9. What will the file look like?


row = ["a","b","c"]
with open("b.csv", "w", newline = "\n") as f:
csv_pointer = csv.writer(f)
csv_pointer.writerow(row)
csv_pointer.writerow(row)

# Q10. What will the file look like?


row = ["a","b","c"]
with open("b.csv", "w") as f:
csv_pointer = csv.writer(f)
csv_pointer.writerows([row,row])
# Q11. What will the file look like?
row = ["a","b",["c", d”]]
with open("b.csv", "w") as f:
csv_pointer = csv.writer(f)
csv_pointer.writerows(row)

# Q12. Given two tables t1 and t2, describe what happens when the following statement
executes?
select * from t1, t2;

# Q13. What type of join is the following statement equivalent to?


select * from t1, t2 where t1.t2_id = t2.id;

# Q14. What is the difference between the two insert statements?


insert into t1 values ('value1','value2');
insert into t1 (attr_1, attr_2) values ('value1','value2');

# Q15. If a String to be stored in the database varies between 10 - 25 characters, what would be
the best data type for the table column?

# Q16. Consider the following table creation and data insertion statements.
create table manager (
manager_code int not null,
manager_name varchar(10));

create table employee (


employee_code int not null,
employee_name varchar(10));
insert into employee values
(1001,'Jason'),(1002,'Mary'),(1003,'Benjamin');
insert into manager values (1001,'Jason'),(1002,'Mary');

What would the sequence of names be like for the following statement?

# Q17. Continue from the tables in qn 6, what would the following statement return?
select * from employee where exists (select * from manager);

# Q18. How can we modify the sub-query of the above statement to only return employees who
are managers?

# Q19. How can we find a String attribute that starts with the word 'Python'?

# Q20. Consider the following table creation and data insertion statements.
create table country_population (
country_name varchar(20),
continent varchar(15),
population int);

insert into country_population


values
('China','Asia',1411439120),
('India','Asia',1386624840),
('United States','N. America',333028031),
('Indoneia','Asia',269603400),
('Pakistan','Asia',220892331),
('Brazil','S. America',214196337),
('Nigeria','Africa',206139587),
('Bangladesh','Asia',172017568),
('Russia','Europe',146748590),
('Mexico','N. America',127792286);
Write the statement that returns the average continent population is between 200 million and 300
million and rank it with the highest result first.

You might also like