a function with the following syntax: def function_name(): STATEMENTS If the function needs to accept parameters then it will have following syntax: def function_name(parameters): STATEMENTS
the function block. 2. Indentation, which is the part of syntax in python and a must have (unlike C-style languages where it's optional). 3. End of indent means end of the block.
to use snake_case (i.e lowercase with underscores to separate words) for naming functions and variables. For example these could be some examples of good function names: get_user_name() create_user() add_user() validate()
by assignning default values to parameters. They also act as optional parameters as the user can omit them. def greet(message = 'Hello'): return message + ' World!' # Invoke this function print greet() # Hello World! print greet('Hi') # Hi World!
calling it by providing the arguments in the order they're de ned as parameters in the function. For instance: def sum(x, y, z = 0): return x + y + z sum(1, 2, 3) # 3 positional arguments sum(1, 5) # 2 positional arguments for x & y, z takes t
using the parameter names in the form arg=value . For instance: def sum(x, y, z = 0): return x + y + z sum(x=1, y=2, z=3) # keyword arguments sum(y=5, z=1, x=4) # position of args doesn't matter here. sum(x=1, y=5) # 2 keyword arguments for x & y, z take
both the positional and keyword arguments like this: sum(1, y=3, z=5) sum(5, 6, z=10) sum(10, y=2) But ensure that keyword arguments do follow positional arguments and same arguments aren't provided more than once.
to the caller of the function. For example: def foo(): # This function returns the value 'Bar' return 'Bar' def baz(): # This function returns nothing return a = foo() # a = 'Bar' b = baz() # b = None
exibility to structure your code as you like. It is recommended that you make your code organized and modular. In many programming languages they strictly enforce you to de ne a main() function and don't allow to write code outside a function. a Python doesn't force you to do the same, but it's a good practice to have something like main() function to keep your code organized.
can be used in expressions. For example: Lamda expression for computing the product of two numbers c = (lambda a, b: a * b)(2, 5) Or func = lambda a, b: a * b c = func(2, 5)
make use of a lot of higher order functions and closures. Or need to use functions in expressions or pass them to other functions as arguments. Especially while doing functional programming, lambdas become pretty common.
# A function that returns another function # to add a number with another number def get_adder_of(a = 1): return lambda x: x + a add_five = get_adder_of(5) add_two = get_adder_of(2) add_seven = get_adder_of(7) print('10 + 5 = %d' % add_five(10)) print('8 + 2 = %d' % add_two(8)) print('8 + 7 = %d' % add_seven(8))
new list of squares of the original list. my_list = [1, 2, 3, 4, 5] squares = list(map(lambda x: x ** 2, my_list)) # The same thing could be achieved using list comprehension. squares_2 = [x ** 2 for x in my_list] print(squares) print(squares_2)
function(s). 2. Do not invoke the functions directly. Use a main() function instead. 3. Protip Read user inputs and print the outputs in the main() function keeping just the core logic in the other functions.