csc305 PYTHON - LAB EXERCISE 1
csc305 PYTHON - LAB EXERCISE 1
QUESTION 1
print len(xxxx)
print xxxx[1:5]
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
print myList2
print myList1[2:]
print myList1[1] , myList2[4]
myList1[3] = 'Banana'
print myList1
print myList1[0] * len(myList2)
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
QUESTION 4
int number;
int positiveNum = 0;
positiveNum = 0
if number > 0:
positiveNum = positiveNum + 1
print(positiveNum)
QUESTION 5
Given is a dictionary written in Python for melting points of seven kitchen materials:
abovefifty = {}
for key, value in melting_point.items():
if value >= 50:
abovefifty[key] = value
print(abovefifty)
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'}
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.
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:
bonusRaya = calcBonusRaya(grade)
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:
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