Found 27759 Articles for Server Side Programming

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

216 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

What are the allowed characters in Python function names?

Manogna
Updated on 12-Jun-2020 11:27:30

1K+ Views

Python IdentifiersIdentifier is the name given to entities like class, functions, variables etc. in Python. It helps in knowing one entity from another.Rules for writing identifiersIdentifiers can be a combination of lowercase letters (a to z) or uppercase letters (A to Z) or digits (0 to 9) or an underscore (_). Names like myClass, var_3 and print_to_screen, all are valid examples.An identifier cannot start with a digit. 2variable is invalid, but variable2 is perfectly correct.Keywords cannot be used as identifiers. The word ‘global’ is a keyword in python. So we get an invalid syntax error hereExampleglobal = "syntex" print globalOutputFile ... Read More

How do you test that a Python function throws an exception?

Manogna
Updated on 12-Jun-2020 11:30:14

239 Views

We write a unittest that fails only if a function doesn't throw an expected exception.We also test if a Python function throws an exception.For example, see the sample code we paste into the Python shell to test Python's type-safety:Exampleimport unittest class MyTestCase(unittest.TestCase):    def test_1_cannot_add_int_and_str(self):       with self.assertRaises(TypeError):          1 + '1'       def test_2_cannot_add_int_and_str(self):       import operator       self.assertRaises(TypeError, operator.add, 1, '1')  unittest.main(exit=False)Running the testsOutputAnd the terminal outputs the following − .. ---------------------------------------------------------------------- Ran 2 tests in 0.001s OKTest one uses assertRaises as a context ... Read More

Why does Python code run faster in a function?

Manogna
Updated on 30-Jul-2019 22:30:20

317 Views

It is found that if python code is run normally and then if it is run in a python function, it runs faster in the latter case. I want to know why python code runs faster in a function.It is generally found that it is faster to store local variables than global variables in a python function. This can be explained as under.Aside from local/global variable store times, opcode prediction makes the function faster.CPython is the original Python implementation we download from Python.org. It is called CPython to distinguish it from later Python implementations, and to distinguish the implementation of ... Read More

What are the basic scoping rules for python variables?

Pranathi M
Updated on 16-Sep-2022 07:37:34

334 Views

Variables are classified into Global variables and Local variables based on their scope. The main difference between Global and local variables is that global variables can be accessed globally in the entire program, whereas local variables can be accessed only within the function or block in which they are defined. Local variables are those that are defined inside a function but have a scope that is only applicable to that function, as opposed to global variables, which are defined outside of any function and have a global scope. In other words, we may argue that although global variables are accessible ... Read More

Advertisements