print(isinstance(1, int)) print(isinstance(True, int)) # True, because booleans are a subclass of integers
In this class, we will use assert, type(), isinstance() to validate function parameters
This is not recommended in production code.
callable()
Check if something is a function
def my_func(): return print(callable(my_func))
all()
The all() function returns True if all items in an iterable are true, otherwise it returns False.
lst1 = [1, 2, 3, 4] lst2 = [1.1, 2, 3, 4] assert all([isinstance(elem, int) for elem in lst1]), 'each elem is not int in lst1' assert all([isinstance(elem, int) for elem in lst2]), 'each elem is not int in lst2'
any()
The any() function returns True if at least one item in the iterable is true, otherwise it returns False.
Can't check for integers in list before checking if it is actually a list
str[16] = "" ‘str’ item does not support item assignment (immutable) str = str[:16] + " " + str[17:]
str.replace()
newStr = "The exam is coming." print(newStr.replace("coming", "cancelled")) # Does not modify newStr print(newStr) exam = newStr.replace("coming", "cancelled") print(exam)