0% found this document useful (0 votes)
20 views4 pages

Comp Prog Reviewer

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)
20 views4 pages

Comp Prog Reviewer

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/ 4

PART I.

Identification

1. FOR LOOP
2. WHILE LOOP
3. CONTINUE
4. BREAK
5. DECREMENT
6. INCREMENT
7. INFINITE LOOP
8. CONDITION
9. INITIALIZATION
10. LOOPS

Multiple Choices

11. C. FOR LOOP


12. B. WHILE LOOP
13. D. FOR LOOP
14. A. FOR LOOP
15. B. WHILE LOOP
16. A. EXCEPT
17. D. ZERO DIVISION ERROR
18. C. VALUE ERROR
19. B. INDEX ERROR
20. B. CHANGE VARIABLE

PART II.

21-25 FOR LOOP

numbers = [5, 10, 15, 20, 25]


index = 0
while index < len(numbers):
print(numbers[index])
index += 1

26-30 WHILE LOOP

for number in range(12):

if number > 5:

break

print(number)

print("Loop Ended")
31-35 WHILE LOOP

LIST_NAMES = ["SALAZAR","SANTOS","BALTAZAR","COMO","TOLENTINO","AGUILA","ANDAL"]

sorted_names = sorted(LIST_NAMES)

for name in sorted_names:

print(name)

36-40 WHILE LOOP


temperature_readings = [25, 28, 30, 32, 27]
temperature_readings = [25,28,30,32,27]
sum_temperature = 0
average_temperature = sum(temperature_readings) / len(temperature_readings)
for temp in temperature_readings:
sum_temperature += temp
print("Average temperature:",
average_temperature average_temperature)
= sum_temperature / len(temperature_readings)
print("Average temperature:", average_temperature)

PART III

41-50 10 ONE LINE CODE

max_value = max([1, 5, 3, 8, 2])

reversed_string = 'helloworld'[::-1]

count = [1, 2, 2, 3, 2,4].count(2)

all_true = all([True, True,False])

any_true = any([False, False,True])

total_sum = sum([1, 2, 3, 4, 5])

sorted_list = sorted([3, 1, 4, 1,5])

absolute_value = abs(-10)

index = [1, 2, 3, 4, 5].index(3)

unique_set = set([1, 2, 2, 3, 4,4, 5])

51-60 SYNTAX OF DEFINING A FUNCTION

def add(x, y):

return x + y

print(add(5, 3))
61-70 MULTIPLE PARAMETERS

def calculate_area(length, width):

"""

Calculates the area of a rectangle.

Args:

length (float): The length of the rectangle.

width (float): The width of the rectangle.

Returns:

float: The area of the rectangle.

"""

return length * width

# Call the function with arguments 5 and 3

area = calculate_area(5, 3)

# Print the result

print("The area of the rectangle is:", area)

71-80 SELECTION STATEMENTS AND LOOPS

def sum_of_odds(numbers):

"""

This function takes a list of numbers and returns the sum of all odd numbers in the list.

"""

# Initialize sum

sum_odds = 0

# Loop through each number in the list

for number in numbers:

# Use a selection statement to check if the number is odd

if number % 2 != 0:

# Add the odd number to the sum

sum_odds += number
return sum_odds

# Example usage:

numbers_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

result = sum_of_odds(numbers_list)

print("The sum of odd numbers is:", result)

You might also like