Found 27104 Articles for Server Side Programming

What is best way to check if a list is empty in Python?

Malhar Lathkar
Updated on 16-Jun-2020 11:19:42

235 Views

The best way is to use not operator on list object. If list is empty it returns true, otherwise false.>>> L1=[] >>> not L1 True >>> L1=[1,2] >>> not L1 FalseAnother method is to check if length of list is zero which means it is empty>>> L1=[] >>> len(L1) 0 >>> L1=[1,2] >>> len(L1) 2

How do I check what version of Python is running my script?

Gireesha Devara
Updated on 24-Aug-2023 13:17:21

417 Views

Python is being updated regularly with new features and supports. Starting from 1994 to current release, there are lots of updates in Python versions. By using Python standard libraries like sys or platform modules we can get the version information of Python that is actually running on our script. In general the python version is displayed automatically on the console immediately after starting the interpreter from the command line. Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. Using the version attribute The sys ... Read More

How we can update a Python list element value?

Gireesha Devara
Updated on 24-Aug-2023 13:00:55

539 Views

In Python lists are one the built−in data structure which is used to store collections of data. The lists are mutable, which means we can modify its element after it is created. We have some list methods like append, insert, extend, remove, and clear which are used to change the list content by adding, updating, or removing elements from the list object. In this article, we will discuss how we can update an existing element in the list. Updating the list elements using the index position The list is an index−based sequential data structure, so that we can access the ... Read More

How we can update a Python tuple element value?

Gireesha Devara
Updated on 24-Aug-2023 15:52:50

1K+ Views

Python tuple is an immutable object, which means once tuple is created you cannot change, update, add, or remove its values. If we try to update any of its elements, then it will throw the TypeError. In this article, we will use three different ways like list conversion, slicing, packing, and unpacking to modify a tuple without raising any errors. Using list conversion By converting tuple to a list then converting it back, we can update the values in tuple, but this will create a list copy of tuple in memory. Example After converting the tuple to list, we ... Read More

Python3 - Why loop doesn't work?

Malhar Lathkar
Updated on 20-Jun-2020 07:39:18

109 Views

It does work. As you have used sleep() method inside the loop, no activity takes place for (0.9*36) seconds. It is not asking for input. After the end of loop, the window will show text field with the given string inside it.

When are python objects candidates for garbage collection?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

189 Views

A python object or variable will be eligible for garbage collection as soon as all references to it go out of scope or are manually deleted (del x). We would have to presume there were no references to the object anywhere else for it to be garbage collected.

How to create Python objects based on an XML file?

Gireesha Devara
Updated on 24-Aug-2023 12:45:36

1K+ Views

XML (Extensible Markup Language), which is a markup−language that is used to structure, store, and transfer data between systems. At some point we need to read/write the XML data using the Python language. By using the untangle library we can create Python objects based on an XML file. The untangle is a small Python library which converts an XML document to a Python object. The untangle has a very simple API. We just need to call the parse() function to get a Python object. Syntax untangle.parse(filename, **parser_features) Parameters Filename: it can be a XML string, a XML filename, ... Read More

Do you think garbage collector can track all the Python objects?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

111 Views

Python uses two techniques to clean up garbage. One is reference counting, which affects all objects but which can't clean up objects that directly or indirectly refer to each other. That's where the actual garbage collector comes in: python has the gc module, which searches for cyclic references in objects it knows about. Only objects that can potentially be part of a reference cycle participate in the cyclic gc. So, for example, lists do, but strings do not; strings don't reference any other objects.All Python classes and their instances automatically get tracked by the cyclic gc. Types defined in C ... Read More

Is there any Python object inspector?

Gireesha Devara
Updated on 23-Aug-2023 19:09:55

266 Views

In python there is no built−in or normal function that acts as an object inspector. But we can use functions like type(), help(), dir(), vars() or modules like inspect are used to find the attributes, properties and methods of any object. Also we have other functions like id(), getattr(), hasattr(), globals(), locals(), callable() are useful in looking inside an object to know its attributes and methods. Here we will inspect the objects using some built−in functions. Before that we will create a simple python class and its objects, to refer throughout this article. Following is the syntax to defing ... Read More

How to wrap python object in C/C++?

Gireesha Devara
Updated on 24-Aug-2023 16:01:06

259 Views

To wrap existing C or C++ functionality in Python, there are number of options available, which are: Manual wrapping using PyMethodDef and Py_InitModule, SWIG, Pyrex, ctypes, SIP, Boost.Python, and pybind1. Using the SWIG Module Let’s take a C function and then tune it to python using SWIG. The SWIG stands for “Simple Wrapper Interface Generator”, and it is capable of wrapping C in a large variety of languages like python, PHP, TCL etc. Example Consider simple factorial function fact() in example.c file. /* File : example.c */ #include // calculate factorial int fact(int n) ... Read More

Advertisements