Tutorial by Examples

In assignments, you can split an Iterable into values using the "unpacking" syntax: Destructuring as values a, b = (1, 2) print(a) # Prints: 1 print(b) # Prints: 2 If you try to unpack more than the length of the iterable, you'll get an error: a, b, c = [1] # Raises: ValueError:...
In functions, you can define a number of mandatory arguments: def fun1(arg1, arg2, arg3): return (arg1,arg2,arg3) which will make the function callable only when the three arguments are given: fun1(1, 2, 3) and you can define the arguments as optional, by using default values: def fun...
When you want to create a function that can accept any number of arguments, and not enforce the position or the name of the argument at "compile" time, it's possible and here's how: def fun1(*args, **kwargs): print(args, kwargs) The *args and **kwargs parameters are special parame...

Page 1 of 1