Found 27759 Articles for Server Side Programming

How we can call Python function from MATLAB?

Rajendra Dharmkar
Updated on 27-Sep-2019 07:37:37

255 Views

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. Let us use the Python calendar module as an example.py.calendar.isleap(2016); py.calendar.isleap(2017);OUTPUTans =1 ans = 0To run our own function, we can create a file in our current MATLAB working directory. Let’s say we created a file called ‘hello.py’ that contains these two lines:def world():     return 'hello world';#  In ... Read More

How we can store Python functions in a Sqlite table?

Rajendra Dharmkar
Updated on 13-Feb-2020 06:59:23

144 Views

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 integer)’’’) emp_1 = employee('John', 'Doe', 50000 ) emp_2 = employee('Jane', 'Doe', 60000) emp_3 = employee('James', 'Dell', 80000) c.execute(‘’’INSERT INTO employee VALUES(:first, :last,   :pay)’’’, {'first':emp_1.first, 'last':emp_1.last, 'pay':emp_1.pay}) c.execute(‘’’INSERT INTO employee VALUES(:first, :last, :pay)’’’, {'first':emp_2.first, 'last':emp_2.last, 'pay':emp_2.pay}) c.execute(‘’’INSERT INTO employee VALUES(:first, :last, :pay)’’’, {'first':emp_3.first, 'last':emp_3.last, 'pay':emp_3.pay}) c.execute("SELECT * FROM employee WHERE ... Read More

How to pass a json object as a parameter to a python function?

Rajendra Dharmkar
Updated on 13-Feb-2020 06:58:27

8K+ Views

We have this code below which will the given json object as a parameter to a python function.exampleimport 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():            print k,v     print dict(a)       func(json_string) Output desig developer age 25 name Rupert {u'desig': u'developer', u'age': 25, u'name': u'Rupert'}

How to return a json object from a Python function?

Rajendra Dharmkar
Updated on 13-Feb-2020 06:57:35

1K+ Views

We return a json object from a python function as follows using given python dictionary.Exampleimport 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"}

How to expand tabs in string to multiple spaces in Python?

Sarika Singh
Updated on 23-Nov-2022 07:49:30

2K+ Views

The handling of white spaces between strings is managed effectively in Python. In this article, we'll examine the methods to provide a space in the string when we are unsure of how much space is necessary. Using string expandtabs() method A built-in method in Python called String expandtabs() is used to handle spaces in strings. The expandtabs() method in Python creates a copy of the string that has all tab characters '\t' up to the next multiple tabsize parameters replaced with whitespace characters. There may be instances when it is necessary to define how much space should be left in ... Read More

Which is more fundamental between Python functions and Python object-methods?

Manogna
Updated on 13-Feb-2020 06:40:52

84 Views

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.  exampledef a(): pass # a() is an example of function print a print type(a)OutputC:/Users/TutorialsPoint/~.py A method is a special class of function, one that can be bound or unbound.Exampleclass C:       def c(self): pass print C.c   # example of unbound method print type(C.c) print C().c  # example of bound method print type(C().c) print C.c()Of course, an unbound method cannot be called without passing as argument.Output ... Read More

Why would you use the return statement in Python?

Rajendra Dharmkar
Updated on 02-May-2023 14:00:23

3K+ Views

The return statement in Python is used to return a value or a set of values from a function. When a return statement is encountered in a function, the function execution is stopped and the value specified in the return statement is returned to the caller. The main purpose of the return statement is to pass data from a function back to the caller. The caller can then use this data for further processing. Why use the return statement? The return statement is useful in many situations. Some common use cases include − Passing data back to the caller: As ... Read More

How variable scope works in Python function?

Sarika Singh
Updated on 19-Dec-2022 12:01:30

210 Views

In this article we will discuss about how a variable scope works in Python function. The scope of a name is the area of a program in which you can clearly access a name, such as variables. Hence, a variable scope is the region in which we can access a variable as shown in the following example - def multiplication(): product = 7*9 Here, the “product” variable is created inside the function, therefore it can only be accessed within it. In general, the python scope concept is presented using the rule called as the ... Read More

How to use *args and **kwargs in Python?

Sarika Singh
Updated on 19-Dec-2022 11:57:47

1K+ Views

In this article we will discuss about *args and **kwargs properties and their usage in Python. To pass unspecified number of arguments to a function usually *args and **kwargs properties are used. The *args property is used to send a non-keyword variable length argument to a function call whereas **kwargs keyword is used to pass a keyword length of arguments to a function. Keyword arguments (or arguments) are the values that is preceded by an identifier “=” in a function call for e.g. “name=”. The primary benefits of using *args and **kwargs are readability and convenience, but ... Read More

What is an anonymous function in Python?

Sarika Singh
Updated on 19-Dec-2022 12:35:50

18K+ Views

An anonymous function in Python is one that has no name when it is defined. In Python, the lambda keyword is used to define anonymous functions rather than the def keyword, which is used for normal functions. As a result, lambda functions are another name for anonymous functions. Syntax Following is the syntax of the lambda function - lambda [args] : expression Although a lambda function can have only one expression, it can have any number of arguments. A lambda can also be immediately invoked and is written in a single line of code. Example: Calling a lambda function ... Read More

Advertisements