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.#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
We have this code below which will the given json object as a parameter to a python function.import json json_string = '{"name":"Rupert", "age": 25, "desig":"developer"}' print type (json_string) def func(strng): a =json.loads(strng) print type(a) for k, v in a.iteritems(): ... Read More
We return a json object from a python function as follows using given python dictionary.import json a = {'name':'Sarah', 'age': 24, 'isEmployed': True } # a python dictionary def retjson(): python2json = json.dumps(a) print python2json retjson()OUTPUT{"age": 24, "isEmployed": true, "name": "Sarah"}
In the code given below we pass given dictionary as an argument to a python function and then call the function which works on the keys/value pairs and gives the result accordinglyd = {'a' : 1, 'b' : 2, 'c' : 3} def f(dict): for k, v in ... Read More
We first import the sys module. We have to use the argv function of the sys module to fetch the arguments of the function entered at the terminal and execute the function.#fubar.py import sys def print_funcargs(arg1, arg2, arg3): print arg1 + ' '+ arg2 + ' ' ... Read More
An object is said to be hashable if it has a hash value that remains the same during its lifetime. It has a __hash__() method and it can be compared to other objects. For this, it needs the __eq__() or __cmp__()method. If hashable objects are equal when compared, then they ... Read More
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
The return statement causes a python function to exit and give back a value to its caller. The purpose of functions in general is to take in inputs and return something. A return statement, once executed, immediately terminates execution of a function, even if it is not the last statement ... Read More
A function is a callable object in Python, i.e. can be called using the call operator. However other objects can also emulate a function by implementing __call__method. For example:def a(): pass # a() is an example of function print a print type(a)OUTPUTC:/Users/TutorialsPoint/~.py <function a at 0x0000000005765C18> <type 'function'>A method is ... Read More
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:import unittest class MyTestCase(unittest.TestCase): def test_1_cannot_add_int_and_str(self): ... Read More
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
Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default valueDefault arguments:Python 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
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
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
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more