Category: Tutorials
-
Python Slicing Strings
Python String slicing is a way of creating a sub-string from a given string. In this process, we extract a portion or piece of a string. Usually, we use the slice operator “[ : ]” to perform slicing on a Python String. Before proceeding with string slicing let’s understand string indexing. In Python, a string is an ordered sequence…
-
Strings
In Python, a string is an immutable sequence of Unicode characters. Each character has a unique numeric value as per the UNICODE standard. But, the sequence as a whole, doesn’t have any numeric value even if all the characters are digits. To differentiate the string from numbers and other identifiers, the sequence of characters is included within single, double or triple…
-
Built-in 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…
-
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 − Open Compiler The args variable prefixed with “*” stores all the values passed to it. Here,…
-
Positional-Only 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…
-
Keyword-Only 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…