Found 35163 Articles for Programming

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

214 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

What is the difference between attributes and properties in python?

Rajendra Dharmkar
Updated on 08-May-2023 12:03:59

5K+ Views

In python, everything is an object. And every object has attributes and methods or functions. Attributes are described by data variables for example like name, age, height etc. Properties are special kind of attributes which have getter, setter and delete methods like __get__, __set__ and __delete__ methods. A property decorator in Python provides getter/setter access to an attribute. You can define getters, setters, and delete methods with the property function. If you just want the read property, there is also a @property decorator you can add above your method. # create a class class C(object): ... Read More

What are required arguments of a function in python?

Pranathi M
Updated on 16-Sep-2022 07:29:41

2K+ Views

Functions accept arguments that can contain data. The function name is followed by parenthesis that list the arguments. Simply separate each argument with a comma to add as many as you like. As the name implies, mandatory arguments are those that must be given to the function at the time of the function call. Failure to do so will lead to a mistake. Simply put, default function parameters are the exact opposite of required arguments. As we previously saw, while declaring the function, we give the function parameters a default value in the case of default arguments. The function automatically ... Read More

Advertisements