Sucgang Week13 LabExercise

You might also like

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

PAMANTASAN NG LUNGSOD NG MUNTINLUPA

COLLEGE OF INFORMATION TECHNOLOGY AND COMPUTER STUDIES

University Road, Poblacion, Muntinlupa City

Sucgang, Richard L. PROFELEC2 – Python Programming


BSIT – 3H Ms. Kaycee R. Mendez, MIT, LPT

Week 13 – Laboratory Exercise

Directions: Write a Python program that demonstrate the use of Functions.


Screenshot/Printscreen your codes and output, copy the raw codes and paste it here.

Note: Your program should be properly COMMENTED and include the name of the
student.

1. Write a function called rectangle that takes two integers m and n as arguments and prints
out an m × n box consisting of asterisks. Shown below is the output of rectangle(2,4)

rectangle(2,4)
****
****
rectangle(4, 6)
4x6

******
******
******
******
You may name your file Lastname_RectangleFunctions.py

2. Find the sum of integers from 1 to 10, 20 to 37, and 35 to 39 respectively using functions.

You may name your file Lastname_AdditionFunctions.py

3. Write a function called merge that takes two already sorted lists of possibly different
lengths, and merges them into a single sorted list.
a. Do this using the sort method.
You may name your file Lastname_MergeFunctions.py

Week 13 – Functions
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
COLLEGE OF INFORMATION TECHNOLOGY AND COMPUTER STUDIES

University Road, Poblacion, Muntinlupa City

Answers:

def rectangle(m, n):


for i in range(m):
for j in range(n):
print('*', end='')
print()

#rectangle(2, 4)
rectangle(4, 6)

Week 13 – Functions
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
COLLEGE OF INFORMATION TECHNOLOGY AND COMPUTER STUDIES

University Road, Poblacion, Muntinlupa City

def calculate_sum(start, end):


total = 0
for number in range(start, end + 1):
total += number
return total

# Sum of integers from 1 to 10


result_1 = calculate_sum(1, 10)
print(result_1)

# Sum of integers from 20 to 37


result_2 = calculate_sum(20, 37)
print(result_2)

# Sum of integers from 35 to 39


result_3 = calculate_sum(35, 39)
print(result_3)

Week 13 – Functions
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
COLLEGE OF INFORMATION TECHNOLOGY AND COMPUTER STUDIES

University Road, Poblacion, Muntinlupa City

def merge(list1, list2):


merged_list = list1 + list2
merged_list.sort()
return merged_list
list1 = [1, 3, 5]
list2 = [2, 4, 6]
merged_list = merge(list1, list2)
print(merged_list)

Week 13 – Functions

You might also like