Default parameter values

Lesson Details:
June 29, 2020


I: Introduction

A: Python is a programming language invented by Guido van Rossum in 1991.

B: It supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles.

C: Python features a dynamic type system and automatic memory management.

D: Python interpreters are available for many operating systems, allowing Python code to run on a wide variety of systems.

Python supports multiple programming paradigms, including object-oriented, imperative and functional programming, or procedural styles. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. The language provides constructs intended to enable clear programs on both a small and large scale.

II: Body

A: Default parameter values

Default parameter values are provided by the interpreter when you call a function without specifying the value of that parameter. The value must meet the data type requirements of the corresponding formal argument. If not enough arguments are provided to satisfy the formal parameters, then it will use the default values instead. If too many arguments are provided, then it will raise an error.

B: Assigning variable values without using "=" sign

Python has an easier way to assign variable values without using the "=" sign. For example, if you want to assign the value "apple" to the variable named "fruit", you can do it like this: fruit = 'apple' . So you can actually skip the "=" sign because Python knows what you mean. However, you cannot do this if you want to assign a value to more than one variable at once (e.g. two variables named "fruit" and "vegetable"). This is because Python doesn't know which variable you want to assign the value to. You can do this with the "=" sign. For example: fruit = 'apple' vegetable = 'potato' . Here, Python assigns the value 'apple' to the variable named "fruit", and 'potato' to the variable named "vegetable".

C: The print() function

The print() function can print out text, numbers, lists, tuples, dictionaries, etc. For example, if you want to print out two numbers separated by a space, you can do it like this: print(1, 2) . If you want to print out three numbers separated by spaces then you can do it like this: print(1, 2, 3) . You can also use a comma character to separate numbers that go into a list: print(1, 2, 3) . By default, Python will print out all of your text in a single line without a newline character. To force a newline character you can use a backslash character at the end of each line. For example: print('first line

second line') . Alternatively, you can use triple quotes around your text to force a newline character at the end of each line: print('first line second line') . In addition, the print() function also allows you to specify a separator between elements in a list or tuple. For example: print('{0}t{1}t{2}'.format(-5 - 9 * 0.7)) . This will output a table with a first column containing a minus sign and a minus sign inside brackets, a second column containing the number 9 inside brackets and multiplied by 0.7, and a third column containing a minus sign and a minus sign inside brackets again. In addition, since Python 3.6 you can also use tuple unpacking as an alternative way to pass arguments to functions as if they were lists or tuples. For example: def func(x): return x + 10 func(1, 2) # equivalent to func(1, 2, 3) or func('apple', 'orange', 'banana') # equivalent to func('apple', 'orange', 'banana', 'mango') Func(1, 2) # equivalent to Func(1, 2,) . Note that only positional arguments can be unpacked this way; keyword arguments must still be declared explicitly. You can also optionally specify how many arguments should be passed with the ellipsis ( ... ) operator after the last positional argument (e.g., Func('a', ...) ). This allows you to omit some positional arguments if their number is not important (e.g., Func('a', ... , 'b', ...) ). Also note that any extra positional arguments will still be available as keyword arguments (e.g., Func('a', 'b', 'c', d=4) ). Finally note that tuple unpacking is not valid for nested sequences (e.g., Func(a=(1,) , b=(2,) , c=(3,) ) ). Note If you want to pass an empty string as part of a tuple unpacking or keyword argument sequence then you have to put an empty pair of parentheses at the end of that sequence (e.g., Func(a=('') , b=(2,) , c=(3,) ) ). Without them it would otherwise be interpreted as an empty tuple (instead of an empty string). When it comes to keyword arguments Python 3 does not allow duplicate names for different keyword arguments unless they are explicitly marked as allowed with * , ** or _underscore_ (e.g., func(**{'color':'blue', 'size':'medium'}) ). However this limitation does not apply to Python 2 . Note that you cannot use tuple unpacking or keyword argument sequences within function calls or class definitions because these are not valid syntax in these contexts. Note that while tuple unpacking allows you to omit unused parameters, keyword argument sequences allow you to effectively rename parameters while still retaining any existing default values for them (e.g., Func(a='apple', color='red', size='large') ). This is particularly useful if you have multiple functions or methods that have long parameter lists but few parameters with unique names, so that you need to keep track of which parameter name corresponds to which default value when calling them. However note that optional parameters always come before required parameters even within keyword argument sequences (e.g., Func(a='apple', color='red', size='small') ), so that if you need to specify both then you must either put required parameters before optional ones (e.g., Func(a='apple', color='red', size='small') ), or explicitly name optional parameters (e.g., Func(a='apple', min_size=size='small') ). If you want to make sure that optional parameters are checked for consistency (e.g., checking whether min_size is less than max_size ) then consider using keyword only parameters instead (e.g., Func(min_size=size='small', max_size=size='large') ). Note that Python's grammar requires that every parameter after *args in this context must be placed in parentheses (i.e., funcs(*args) is invalid). See PEP 448 for more details about why these restrictions exist and how they might change in future versions of Python. Also note that when calling classes or functions with keyword argument sequences then any trailing positional arguments will be appended after any optional keyword arguments (e.g., Class(a='apple', b='orange') ). This makes it easy for callers to provide defaults for any arbitrary positional arguments even if they don't know whether a given class or function accepts a particular key-value pair yet (e.g., Class(a='apple') becomes Class(a='apple', b='orange') ). Note that while trailing commas are allowed in normal function calls they are not allowed here because trailing commas after *args would conflict with trailing commas after **kwargs . This problem doesn't exist in normal function calls because trailing commas there are used consistently in both cases (i.e., funcs(*,*) is valid but funcs(*args,) is not). See PEP 448 for more details about why this restriction exists and how it might change in a future version of Python if it proves problematic for users who prefer commas at the end of their keyword argument sequences rather than at the end of their normal argument lists. Note Since Python 2 python has supported *args which means "all positional arguments passed by position" and **kwargs which means "all keyword arguments passed by name" just like normal function calls do (e.g., funcs(*args) is equivalent to funcs(*,*args) ). However this support was added before Python 3 was created and wasn't ported over when Python

loader
Course content