Category: Functions & Modules
-
Builtin Functions
Built-in Functions in Python? Built-in functions are those functions that are pre-defined in the Python interpreter and you don’t need to import any module to use them. These functions help to perform a wide variety of operations on strings, iterators, and numbers. For instance, the built-in functions like sum(), min(), and max() are used to simplify mathematical operations.…
-
Modules
Python Modules The concept of module in Python further enhances the modularity. You can define more than one related functions together and load required functions. A module is a file containing definition of functions, classes, variables, constants or any other Python object. Contents of this file can be made available to any other program. Python has the import keyword for this…
-
Function Annotations
Function Annotations The function annotation feature of Python enables you to add additional explanatory metadata about the arguments declared in a function definition, and also the return data type. They are not considered by Python interpreter while executing the function. They are mainly for the Python IDEs for providing a detailed documentation to the programmer. Although you can use the docstring feature of Python…
-
Variable Scope
The scope of a variable in Python is defined as the specific area or region where the variable is accessible to the user. The scope of a variable depends on where and how it is defined. In Python, a variable can have either a global or a local scope. Types of Scope for Variables in Python On…
-
Arbitrary or Variable-length Arguments
Arbitrary Arguments (*args) You may want to define a function that is able to accept arbitrary or variable number of arguments. Moreover, the arbitrary number of arguments might be positional or keyword arguments. Arbitrary Arguments Example Given below is an example of arbitrary or variable length positional arguments − # sum of numbersdefadd(*args): s=0for x in args: s=s+x return s result = add(10,20,30,40)print(result)…
-
PositionalOnly Arguments
Positional Only Arguments It is possible in Python to define a function in which one or more arguments can not accept their value with keywords. Such arguments are called positional-only arguments. To make an argument positional-only, use the forward slash (/) symbol. All the arguments before this symbol will be treated as positional-only. Python’s built-in input() function is an example of positional-only…
-
Positional Arguments
Positional Arguments The list of variables declared in the parentheses at the time of defining a function are the formal arguments. And, these arguments are also known as positional arguments. A function may be defined with any number of formal arguments. While calling a function − Positional Arguments Examples Let’s discuss some examples of Positional arguments − Example 1 The following example shows…
-
KeywordOnly Arguments
Keyword-Only Arguments You can use the variables in formal argument list as keywords to pass value. Use of keyword arguments is optional. But, you can force the function to accept arguments by keyword only. You should put an astreisk (*) before the keyword-only arguments list. Let us say we have a function with three arguments, out of which we…
-
Keyword Arguments
Keyword Arguments Python allows to pass function arguments in the form of keywords which are also called named arguments. Variables in the function definition are used as keywords. When the function is called, you can explicitly mention the name and its value. Calling Function With Keyword Arguments The following example demonstrates keyword arguments in Python. In the second function…
-
Default Arguments
Python Default Arguments Python allows to define a function with default value assigned to one or more formal arguments. Python uses the default value for such an argument if no value is passed to it. If any value is passed, the default value is overridden with the actual value passed. Default arguments in Python are the function arguments that…