Python OOPE Exam
2024 Sep Oppe 1 Set 1
๐ข Function: Count Positive Integers (Ignore None)
Letโs build an easy-to-use Python function! โจ
๐ฉ Function Definition
def count_positive_integers(lst):
return sum(1 for x in lst if x is not None and isinstance(x, int) and x > 0)
๐ Step-by-Step Explanation
- Iterate through each element
x
in the list. - Ignore
None
values (x is not None
). - Check if the value is an integer (
isinstance(x, int)
). - Count only if the integer is positive (
x > 0
, so zero is ignored). - Sum up the count (
sum(1 for ...)
).
๐งช Practice Questions
Practice 1
lst = [4, -3, None, 7, 0, None, 10]
print(count_positive_integers(lst)) # _______
Answer: 3
(Counts 4, 7, 10)
Practice 2
lst = [None, None, -50, 0, 2]
print(count_positive_integers(lst)) # _______
Answer: 1
(Only 2 is positive)
Practice 3
lst = [None, 0, -1, None]
print(count_positive_integers(lst)) # _______
Answer: 0
(No positive integers)
โจ Key Points
- Ignores all
None
values. - Does not count zeros or negative numbers.
- Works for any mixed list of integers and
None
.
Happy Coding! ๐
๐ข Function: count_positive_ignore_none
Let’s build a Python function that counts the number of positive integers in a list, ignoring None
values and zeros. โจ
๐งโ๐ป Function Implementation
def count_positive_ignore_none(nums: list):
'''
Count the number of positive integers in the list, ignoring `None` values and zeros.
Args:
nums (list): A list of numbers, possibly containing `None` values.
Returns:
int: The count of positive integers in the list.
'''
return sum(1 for x in nums if x is not None and isinstance(x, int) and x > 0)
๐ Step-by-Step Explanation
- Iterate through each item (
x
) innums
. - Ignore if
x
isNone
. - Check if
x
is an integer usingisinstance(x, int)
. - Count only if
x
is greater than zero (x > 0
)โthis means strictly positive. - Sum up all such valid items to get the final count.
๐งช Practice Questions
Practice 1
lst = [1, -2, 3, 0, None, 4]
print(count_positive_ignore_none(lst)) # What is the result?
Answer: 3
(Counts: 1, 3, 4)
Practice 2
lst = [None, 0, -1, 2, None]
print(count_positive_ignore_none(lst)) # What is the result?
Answer: 1
(Only 2 is positive)
Practice 3
lst = [None, None, 0, -10]
print(count_positive_ignore_none(lst)) # What is the result?
Answer: 0
(No positive integers)
Practice 4
lst = [10, None, 5, 0, 6, -5]
print(count_positive_ignore_none(lst)) # What is the result?
Answer: 3
(Counts: 10, 5, 6)
โจ Key Points
- None values and zeros are ignored.
- Only strictly positive integers are counted.
- Works for lists with mixed types if you only want integers.
Happy Coding! ๐