0% found this document useful (0 votes)
3 views3 pages

Lab 3

The document contains a series of Python functions that perform various checks on lists, such as verifying if all elements are positive, if any element is even, and if there are prime numbers present. It also includes functions to check conditions across multiple lists and validate specific mathematical conditions. Each example is accompanied by test cases demonstrating the functionality of the defined functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Lab 3

The document contains a series of Python functions that perform various checks on lists, such as verifying if all elements are positive, if any element is even, and if there are prime numbers present. It also includes functions to check conditions across multiple lists and validate specific mathematical conditions. Each example is accompanied by test cases demonstrating the functionality of the defined functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

print("----EX1----")

def is_all_positive(lst):
return all(x > 0 for x in lst)

lst1 = [1, 2, 3, 4, 5]
lst2 = [1, -2, 3, 4, 5]
print(is_all_positive(lst1))
print(is_all_positive(lst2))

print("----EX2----")
def has_even(lst):
return any(x % 2 == 0 for x in lst)

lst1 = [1, -1, 3, 5, 5]


lst2 = [1, -2, 3, 4, 5]
print(has_even(lst1))
print(has_even(lst2))

print("----EX3----")
def not_all_even(lst):
return any(x % 2 != 0 for x in lst)

lst1 = [2, -4, 6, 8, 10]


lst2 = [1, -2, 3, 4, 5]
print(not_all_even(lst1))
print(not_all_even(lst2))

print("----EX4----")
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True

def has_prime(lst):
return any(is_prime(x) for x in lst)

lst1 = [1, 4, 6, 8, 9]
lst2 = [2, 4, 6, 8, 10]
print(has_prime(lst1))
print(has_prime(lst2))

print("----EX5----")
def has_even(lst):
return any(x % 2 == 0 for x in lst)

def each_list_has_even(lists):
return all(has_even(lst) for lst in lists)

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


lists2 = [[1, 3, 5], [2, 4, 6]]

print(each_list_has_even(lists1))
print(each_list_has_even(lists2))

print("----EX6----")
def exists_greater(lst):
return all(any(y > x for y in lst) for x in lst)
lst1 = [1, 2, 3, 4, 5]
lst2 = [5, 5, 5, 5, 5]

print(exists_greater(lst1))
print(exists_greater(lst2))

print("----EX7----")
def validate_condition(lst):
return all(any((x + y) % 5 == 0 and y > x for y in lst) for x in lst)

lst1 = [1, 2, 3, 4, 5]
lst2 = [2, 4, 6, 8, 10]

print(validate_condition(lst1))
print(validate_condition(lst2))

You might also like