Found 10805 Articles for Python

How to pass keyword parameters to a function in Python?

Alekhya Nagulavancha
Updated on 24-Feb-2023 11:14:02

4K+ Views

In Python, functions are usually called by passing none, one or more arguments to it. There are two types of arguments that can be used − Positional arguments Keyword arguments. Positional arguments depend on the order they are passed to a function; they are the more common type of arguments used in programming languages. Whereas, keyword arguments are passed as key-value pairs so that the arguments are mapped to a key using the “=” operator which allows the Python interpreter to ignore the positions. In this article, we will be discussing about keyword parameters/arguments and how to pass ... Read More

How to use variable-length arguments in a function in Python?

Pranathi M
Updated on 16-Sep-2022 07:28:09

15K+ Views

As the name implies, an argument with a variable length can take on a variety of values. You define a variable argument using a '*', for example *args, to show that the function can take a variable number of arguments. Observations on Python's variable-length arguments are as follows. The designation "*args" for variable length arguments is not required. The only thing needed is *; the variable name can be anything, like *names or *numbers. You can send zero or more arguments to a function using a variable length argument. A tuple is used to store the values passed to ... Read More

How to set default parameter values to a function in Python?

Pranathi M
Updated on 16-Sep-2022 07:38:48

13K+ Views

Python functions are used to implement logic that you wish to run repeatedly at various locations across your code. These functions accept function arguments as input parameters. You can specify default argument values in Python functions in addition to supplying parameters to them through function calls. If you don't explicitly specify a parameter value to the given argument, these default values are applied to function arguments. The actual values supplied to function arguments are called parameters. The syntax representation and default values for function parameters are different in Python. If no argument value is given during the function call, default ... Read More

How to pass optional parameters to a function in Python?

Pranathi M
Updated on 23-Aug-2023 21:54:29

47K+ Views

A type of argument with a default value is a Python optional parameter. A function definition's assignment operator or the Python **kwargs statement can be used to assign an optional argument. Positional and optional arguments are the two sorts of arguments that a Python function can take. Values that are not required to be given in order to call a function are known as optional parameters. Function in Python A function is a section of code that only executes when called. You can supply parameters that is data to a function. As a result, a function may or may not ... Read More

How to pass arguments by value in Python function?

Manogna
Updated on 27-Sep-2019 07:52:12

566 Views

For given code the output is as followsb = 30 a = ['10']In this case, "a" seems to be passed by value, because the value is  unchanged even after the call to the function. So it is clear that the arguments have been passed by value in the python function.

Can we explicitly define datatype in a Python Function?

Manogna
Updated on 13-Feb-2020 06:27:43

460 Views

In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally. In Java, C++, and other statically-typed languages, you must specify the datatype of the function return value and each function argument.If we explicitly define datatype in a python function, it still works like a normal program where data type is not declared explicitly.ExampleWe get the following output for the given codeC:/Users/TutorialsPoint1/~.py The required Sum is:  13.0Consider this functiondef addSum(x, y):        return x+y print addSum(2.2, 5.6) print addSum(float(2.2), float(5.6))Output7.8 7.8So the declaration of the datatype of a ... Read More

How can we return a dictionary from a Python function?

Sarika Singh
Updated on 27-Aug-2023 14:36:49

29K+ Views

Any object, such as dictionary, can be the return value of a Python function. Create the dictionary object in the function body, assign it to any variable, and return the dictionary to the function's caller. Data values are stored as key:value pairs in dictionaries. A Python dictionary is a collection that is ordered, changeable, and forbids duplicates. In this article we will discuss various methods to return a dictionary from a Python function. Using Dictionary Comprehension One line of Python code can create and initialise dictionaries using the simple and memory efficient way known as Dictionary Comprehension. Expression and context ... Read More

Write down a python function to convert camel case to snake case?

Rajendra Dharmkar
Updated on 08-May-2023 12:17:32

2K+ Views

Camel case and snake case are ways of writing words together when we are programming. In camel case, we write words together without spaces and we start each new word with a capital letter except for the first word. For example, if we want to write a variable name for someone's date of birth, we can write it like this: dateOfBirth. In snake case, we write words together with an underscore symbol between them, and all the letters are lowercase. For example, if we want to write a variable name for someone's home address, we can write it like this: ... Read More

How to produce documentation for Python functions?

Rajendra Dharmkar
Updated on 19-May-2023 14:19:35

215 Views

Documentation is an important aspect of writing code, especially when it comes to functions. It helps others understand what the function does, how to use it, and what parameters it takes. Python has a built-in documentation tool called docstrings. A docstring is a string that appears as the first statement in a function and provides documentation about the function. Information about a function or documentation is put in docstrings in a function. The following are the guidelines to be followed while writing the docstrings.The first line should always be a short, concise summary of the object’s purpose. For brevity, ... Read More

How to eliminate repeated lines in a python function?

Sarika Singh
Updated on 09-Sep-2023 09:24:47

5K+ Views

In this article we will discuss how to delete multiple lines that are repeated in Python. If the file is small and only has a few lines, the process of removing repeated lines from it could be performed manually. However, when dealing with huge files, Python can assist. Using File Handling Method Python has built-in methods for creating, opening, and closing files, which makes handling files easier. Python also enables doing several file actions, such as reading, writing, and appending data, while files are open. To remove duplicate lines from a Python text file or function, we use file handling ... Read More

Advertisements