all(iterable)
Function accepts like argument an iterable structure (like list, tuple, dictionary, etc), function returns True if all elements of the iterable structure are true.
Example:
mylist1 = [1, 'two', 'xyz', 8.35, True]
mylist2 = [1, 'two', 'xyz', 8.35, 'yhg']
mylist3 = [1, 0, 'xyz', 8.35, 'test']
mylist4 = [1, 'tt', False, 8.35, 'test']
print(f'all(mylist1) give: {all(mylist1)}')
print(f'all(mylist2) give: {all(mylist2)}')
print(f'all(mylist3) give: {all(mylist3)}')
print(f'all(mylist4) give: {all(mylist4)}')
output:
all(mylist1) give: True
all(mylist2) give: True
all(mylist3) give: False
all(mylist4) give: False
Here all(mylist1) and all(mylist2) return True because all elements of mylist1 and mylist2 are True (anything that is not False or 0 is considered true).
all(mylist3) return False because mylist3 contain 0 element. all(mylist4) return False because mylist4 contain False.
filter(function, iterable)
Python built-in filter function construct an itertor based on an iterable parameter like list, tuple, etc, taking from it only elements that fulfil function parameter condition. Description sounds a little bit complicated, but some examples show that filter function is simple.
Let's presume we have list1 = [45, 12, 5, 7, 32, 20, 2, 18, 21, 9, 16]
We need to obtain from list1 a new list that should contain only numbers greater than 15. Code for this is:
list1 = [45, 12, 5, 7, 32, 20, 2, 18, 21, 9, 16]
# filter function for number greater than 15
def fg15(x):
if x>15:
return x
# apply filter function
result=filter(fg15, list1)
list2=list(result)
print(list2)
output:
[45, 32, 20, 18, 21, 16]
We observe here that list2 contains elements from list1 that are greater than 15 (which fulfil filter restriction).
class slice ([start], stop[, step])
Explanation of slice according with python documentation https://docs.python.org/3/library/functions.html#slice is a little bit complicated.
Slice function returns a "slice object" representing a set of indices similar to range(start, stop, step). Step parameter is optional, if not specified it is 1. Start is optional, if it is not specified start is first element. It is easy to understand slice by examples:
list1 = (23, True, 5, 12, 'yes', 'cat', 'dog', 34)
slc1=slice(1,6,2)
print(slc1) # here slc1 is the slice objet
print(list1[slc1])
print(list1[1:6:2])
output:
slice(1, 6, 2)
(True, 12, 'cat')
(True, 12, 'cat')