0% found this document useful (0 votes)
31 views6 pages

Aliff Ilham Sabki Bin Omar Sabki-Dsc5516b (Python)

The document is a lab test for Python programming, containing various coding questions and tasks for a student named Aliff Ilham Sabki Bin Omar Sabki. It includes exercises on using lambda functions, string manipulation, loops, dictionaries, JSON conversion, file handling, and class creation in Python. Each question specifies the required coding tasks along with the expected output format.

Uploaded by

OMAR KAIRAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

Aliff Ilham Sabki Bin Omar Sabki-Dsc5516b (Python)

The document is a lab test for Python programming, containing various coding questions and tasks for a student named Aliff Ilham Sabki Bin Omar Sabki. It includes exercises on using lambda functions, string manipulation, loops, dictionaries, JSON conversion, file handling, and class creation in Python. Each question specifies the required coding tasks along with the expected output format.

Uploaded by

OMAR KAIRAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

TEST 2: LAB TEST

NAME: Aliff Ilham Sabki Bin Omar Sabki

STUDENT ID: 202269772

GROUP: 6B
Python

- Answer all questions


- Copy all the script/coding and result from python into given box in
every questions.

INSTRUCTION:

1. By using Lambda, Add 10 to argument a, and return the result of 15. Print the result.
(4 marks)

X=lambda a : a+10

print(X(5))

Answer:

2. Based on the given Python code below,

user_string =“from [email protected],[email protected],


[email protected] message = Its raining in Malaysia, It’s
also raining in Singapore.”

Run the correct Python scripts to produce the desired output.

i) Count how many times “raining” appears in the given string.


ii) Count how many emails in the string using string methods.
iii) Assign the three emails in a list.
iv) Replace “fskm.uitm.edu.my” to “uitm.my” in the string
(10 marks)

i) user_string.count("raining")

ii) email_kira = user_string.count("@")


print(email_kira)

iii) emails = re.sub(r"from\s*|message\s*=.*", "", user_string)

email_list = emails.split(",")

print( email_list)

iv) updated= user_string.replace("fskm.uitm.edu.my", "uitm.my")

print("Updated string:", updated)

Answer:

3. Lets a list fruits = ["apple", "banana", "cherry"]. Print each fruit but exit
the loop when x is "banana". Print the result.
(5 marks)

for i in fruits:

if i=="banana":

break

else:

print(i)

Answer:

4. Evaluate two variables: x = "Hello", y = 15 weather in the form of Python Boolean


or not.
(2 marks)
Answer:
print(bool(x))

print(bool(y))

5. The values in dictionary items can be of any data type. Create any dictionaries that
combines string, Boolean and list type. Print the result.
(3 mark)

D={"name":"Aliff","married":False,"hobby":["study","games"]}

print(D)
Answer:

6. Using result in question 7, convert/parse the python dictionary to JSON. Use any two
parameters to make JSON data result more easier to read. Print the result.
(4 marks)

Answer:

7. Create code to save the JSON data structure created in question 8 to local storage.
Print screen the file where the data located.
(3 marks)

Answer:

8. Create a txt format file(any name) which contain text data as follows:

Hold fast to dreams


For if dreams die
Life is a broken-winged bird
That cannot fly.

a. Upload to the folder and open the file with read() method for reading the content of
the file.
(2 marks)
b. Read one line of the file.
(2 marks)
c. Create a new text file (any name) ‘using open() and write text in it the same content
as above.
(4 marks)

a. with open("text.txt","w") as new:


new.write("""Hold fast to dreams
For if dreams die
Life is a broken-winged bird
That cannot fly""")

with open("text.txt","r") as baca:


baca.read()

b. with open("text.txt","r") as baca:


print(baca.readline())

c. with open("new_text.txt","w") as copy:


copy.write("""Hold fast to dreams
For if dreams die
Life is a broken-winged bird
That cannot fly""")

Answer:

9. Create python Class and run in python based on questions below:

i) Create an animal class that can set different types of animal classes with the
given attributes, as an example: Class: Mammals Species: Cat Features:
['four legs', 'fur1’ ]
(3 marks)

ii) Create a function in the class that returns the type of species if the list of
features matches the created animal class.
(3 marks)
iii) Create a function that receives a text file as its parameter and opens it. The
function will return a list of string containing information about the total
number of text in the files, total lines and the content of the file in a multiline
string.

(5 marks)

i) class animal:
def __init__(self,class_input,species_input,feature_input):
self.Class=class_input
self.species=species_input
self.features=feature_input

example_animal = animal("Mammals", "Cat", ["four legs", "fur"])

print(example_animal.Class)
print(example_animal.species)
print(example_animal.features)

ii) def match(self, features_user):


if set(features_user) == set(self.features):
return self.species

iii) def receive(self, text_file):


with open(text_file, 'r') as file:
text = file.read()
lines = text.splitlines()
tot_text = len(content)
tot_lines = len(lines)
return ["Total Characters: "+str(tot_text),
Answer:

You might also like