How to return void from Python function?

Rajendra Dharmkar
Published on 07-Dec-2017 12:43:20
Since Python is dynamic-typed and you don't specify a return type when defining a function, then you can return anything with any type, that includes None which is the default return value (when you don't return anything, the function actually returns None at the bottom of function)Functions like this are ... Read More

What are required arguments of a function in python?

Rajendra Dharmkar
Published on 07-Dec-2017 12:46:21
Required arguments are the mandatory arguments of a function. These argument values must be passed in correct number and order during function call. If you run the given code you get the following outputHi 15 Traceback (most recent call last):   File "requiredarg1.py", line 4, in <module>     requiredArg('Hello') TypeError: ... Read More

What are default arguments in python?

Manogna
Published on 07-Dec-2017 12:49:15
Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default valueDefault arguments:ExamplePython has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument ... Read More

What are the allowed characters in Python function names?

Manogna
Published on 07-Dec-2017 12:52:30
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 ... Read More

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

Manogna
Published on 07-Dec-2017 12:56:54
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): ... Read More

Why does Python code run faster in a function?

Manogna
Published on 07-Dec-2017 13:03:59
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 ... Read More

What are the basic scoping rules for python variables?

Rajendra Dharmkar
Published on 07-Dec-2017 10:46:05
In the book Learning Python by Mark Lutz, he suggests the following mnemonic for remember how Python scoping works: LEGBGoing from the narrowest scope to the widest scope:L stands for “Local”. It refers to variables that are defined in the local scope of functions.E stands for “Enclosing”. It refers to ... Read More

How to run Python functions in Eclipse command line?

Rajendra Dharmkar
Published on 07-Dec-2017 13:40:35
We can use PyDev, which is a plugin for running Python programs from Eclipse. Once installed, we will have the option of running the applications themselves directly or from the PyDev console/command line.

How we can call Python function from MATLAB?

Rajendra Dharmkar
Published on 07-Dec-2017 08:16:15
The Python libraries are now available in MATLAB (since 2014b). We can run code directly in MATLAB if we are using version 2014b or later.This makes it possible to use python modules in MATLAB. Without any other changes, just prefix ‘py’ before the python library name you want to use. ... Read More

How we can store Python functions in a Sqlite table?

Rajendra Dharmkar
Published on 07-Dec-2017 13:34:06
In the following code, we import sqlite3 module and establish a database connection. We create a table and then insert data and retrieve information from the sqlite3 database and close the connection finally.Example#sqlitedemo.py import sqlite3 from employee import employee conn = sqlite3.connect('employee.db') c=conn.cursor() c.execute(‘’’CREATE TABLE employee(first text, last text, pay ... Read More
Advertisements