feat: added examples_tutorials
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
def square(x: int) -> int:
|
||||
return x ** 2
|
||||
|
||||
# Main function
|
||||
def main() -> None:
|
||||
"""
|
||||
lambda: a one-liner anonymous function. in its simplest form, it just a function.
|
||||
for example, the following can be rewritten with a add_1 function which returns x + 1
|
||||
"""
|
||||
add_1 = lambda x: x + 1
|
||||
result = add_1(1)
|
||||
print(result)
|
||||
|
||||
"""
|
||||
map function: apply a function to every element in an iterable
|
||||
returns a map object which can then be casted to a list
|
||||
"""
|
||||
numbers = range(1,10)
|
||||
# square is the function and numbers is the iterable where elements will be sent from
|
||||
results = list(map(square, numbers))
|
||||
print(results)
|
||||
|
||||
"""
|
||||
implement the same map function with lambda
|
||||
"""
|
||||
results2 = list(map(lambda x: x ** 2, numbers))
|
||||
print(results2)
|
||||
|
||||
"""
|
||||
filter function: apply function to every element. if true, keep the element. if false, reject it
|
||||
"""
|
||||
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
|
||||
print(even_numbers)
|
||||
|
||||
# Call main function
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user