0% found this document useful (0 votes)
6 views5 pages

csc305 PYTHON - LAB EXERCISE 1

The document contains a lab exercise for a programming course, focusing on Python programming paradigms. It includes multiple questions that require predictions of code outputs, writing Python programs, and converting C code to Python. Additionally, it covers topics such as dictionaries, user input, and functions in Python.

Uploaded by

sereinco23
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)
6 views5 pages

csc305 PYTHON - LAB EXERCISE 1

The document contains a lab exercise for a programming course, focusing on Python programming paradigms. It includes multiple questions that require predictions of code outputs, writing Python programs, and converting C code to Python. Additionally, it covers topics such as dictionaries, user input, and functions in Python.

Uploaded by

sereinco23
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/ 5

CSC305 – PROGRAMMING PARADIGMS

PYTHON: LAB EXERCISE 1

NAME : HANIS ADIBAH BINTI RIZA


STUDENT NO. : 2022465666
GROUP : RCDCS1104B

QUESTION 1

Predict the output for the given codes written in Python:

xxxx = ['File', 'Edit', 'Format', 'Run', 'Options', 'Windows', 'Help']


yyyy = [88, 900, 75, 123, 641]

print len(xxxx)
print xxxx[1:5]

print xxxx[:3] + yyyy[3:]

yyyy.remove(max(yyyy))
yyyy.insert(2, 678)
print yyyy

7
['Edit', 'Format', 'Run', 'Options']
['File', 'Edit', 'Format', 123, 641]
[88, 75, 678, 123, 641]

QUESTION 2

Predict the output for the given code segment in Python:

myList1 = ['Lychee', 'Mango', 'JackFruit', 'Apple', 'Guava']


myList2 = [4.5, 25, 15.5, 2, 6.8]

print myList2
print myList1[2:]
print myList1[1] , myList2[4]
myList1[3] = 'Banana'
print myList1
print myList1[0] * len(myList2)

[4.5, 25, 15.5, 2, 6.8]


[‘JackFruit’, ‘Apple’, ‘Guava’]
Mango 6.8
[‘Lychee’, ‘Mango’, ‘JackFruit’, ‘Banana’, ‘Guava’]
LycheeLycheeLycheeLycheeLychee

PREPARED BY: NYCJ @ KPPIM, UiTM CAWANGAN PERLIS KAMPUS ARAU P a g e |1


CSC305 – PROGRAMMING PARADIGMS
PYTHON: LAB EXERCISE 1

QUESTION 3

Write a Python program segment to prompt user to key in seven integers and store them
in a list. Then, print all the inputs in the list in ascending order. Lastly, calculate and display
the total of all the even integers in the list that are less than or equal to 50.

list = []
sum = 0
i=1

while i < 8:
num = int(input("Enter an integer: "))
list.append(num)
i+=1
list.sort()
print(list)

for n in list:
if n % 2 == 0:
sum += n

print ("Sum of all even numbers in the list: ", sum)

QUESTION 4

Given C programming language below, rewrite it to Python programming language.

int number;
int positiveNum = 0;

for (int x = 0; x < 10; x++)


{
printf(“enter the number:”);
scanf(“%d”, &number);
if (number > 0)
positiveNum = positiveNum + 1;
}
printf(“%d”, positiveNum);

positiveNum = 0

for x in range (10):


number = int(input("enter the number: "))

if number > 0:
positiveNum = positiveNum + 1

print(positiveNum)

PREPARED BY: NYCJ @ KPPIM, UiTM CAWANGAN PERLIS KAMPUS ARAU P a g e |2


CSC305 – PROGRAMMING PARADIGMS
PYTHON: LAB EXERCISE 1

QUESTION 5

Given is a dictionary written in Python for melting points of seven kitchen materials:

melting_point = {'lard':30, 'olive oil':-6, 'chocolate':36, 'water':0,


'sugar':185, 'butter':35, 'beeswax':62}

Add commands to the given codes so that the program can:

(a) Display the melting point for olive oil.


(b) Display the name and its melting point for all materials with melting points of 50oC and
above.
(c) Determine and display the name of material with the highest melting point.

a) print("Olive oil melting point: ", melting_point['olive oil'])

b)melting_point = {'lard':30, 'olive oil':-6, 'chocolate':36, 'water':0, 'sugar':185, 'butter':35,


'beeswax':62}

abovefifty = {}
for key, value in melting_point.items():
if value >= 50:
abovefifty[key] = value
print(abovefifty)

c) Keymax = max(zip(melting_point.values(), melting_point.keys()))[1]


print(Keymax)

QUESTION 6

Write a program in Python that can convert any number entered by the user into its English
word. Use the dictionary given below to help you in completing your program.

D = {'1': 'ONE', '2': 'TWO', '3': 'THREE', '4': 'FOUR', '5': 'FIVE', '6':
'SIX', '7': 'SEVEN', '8': 'EIGHT', '9': 'NINE'}

Sample input: Sample output:


446688 FOUR FOUR SIX SIX EIGHT EIGHT

number = input("Enter a number: ")


word = " ".join(D[digit] for digit in number if digit in D)
print(word)

PREPARED BY: NYCJ @ KPPIM, UiTM CAWANGAN PERLIS KAMPUS ARAU P a g e |3


CSC305 – PROGRAMMING PARADIGMS
PYTHON: LAB EXERCISE 1

QUESTION 7

Given a dictionary of product codes and product value. Write a function name
sumValue() in Python that could count and return the sum of all the value of the products
where the code begins with letter “a”. Some examples of input and output of the program
are given in the table below.

Sample code Sample output

d={"a001":100, "a002":10, "b001": 1}


print (sumValue(d) ) 110

d={"b001":5, "c002":10, "d001":15} 0


print ( sumValue (d) )

d={"a1":6, "a2":7, "a3":8}


print ( sumValue (d) ) 21

def sumValue(d):
total = 0
for key, value in d.items():
if key.startswith('a'):
total += value
return total

QUESTION 8

Study the given codes written in Python below, then answer question that follow:

employeeName = raw_input("Enter the name of the employee: ")


grade = int (raw_input("Enter the current grade: "))

bonusRaya = calcBonusRaya(grade)

print "\nName of the employee = ", employeeName


print "Bonus raya received = RM", bonusRaya

Code 1

In celebrating Aidilfitri, the government of Malaysia is giving away bonuses to its staffs.
Table 1 given below will list out all the bonus rate according to each grade category:

Grade Bonus (RM)


Above 41 350
41 – 29 450
Less than 29 500
Table 1

PREPARED BY: NYCJ @ KPPIM, UiTM CAWANGAN PERLIS KAMPUS ARAU P a g e |4


CSC305 – PROGRAMMING PARADIGMS
PYTHON: LAB EXERCISE 1

Based on the main function given in Code 1 above, write the function definition of
calcBonusRaya(). calcBonusRaya()’s task is to assign the bonus rate according to the
grade input by the user.

def calcBonusRaya(B):
if grade > 41:
bonus = 350
elif grade <= 41 and grade > 29:
bonus = 450
else:
bonus = 500
return bonus

PREPARED BY: NYCJ @ KPPIM, UiTM CAWANGAN PERLIS KAMPUS ARAU P a g e |5

You might also like