Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Akshitha Mote
Page 3 of 4
What are different types of quotes in Python?
In Python, strings can be defined using single quotes ('), double quotes ("), or triple quotes (''' or """). Each type serves different purposes and handles various scenarios like quotes within strings, multiline text, and escape characters. Python supports three types of quotations for creating strings ? Single quotes (') − For basic strings and when double quotes appear inside Double quotes (") − For basic strings and when single quotes appear inside ...
Read MoreHow to create an empty list in Python?
In Python, a list is one of the built-in data types. A Python list is a sequence of items separated by commas and enclosed in square brackets [ ]. The items in a Python list need not be of the same data type. In this article, we will discuss different ways to create an empty list in Python. Using Square Brackets This is the simplest and most common way to create an empty list using square brackets []. An empty list means the list has no elements at the time of creation, but we can add items ...
Read MoreWhat is the difference between dict.items() and dict.iteritems() in Python?
In Python, dict.items() and dict.iteritems() are methods used to access dictionary key-value pairs. The key difference is that dict.items() returns a list of tuple pairs in Python 2 (dict_items view in Python 3), while dict.iteritems() returns an iterator over the dictionary's (key, value) pairs. Note that dict.iteritems() was removed in Python 3. dict.items() in Python 2 In Python 2, dict.items() returns a list of tuples ? # Python 2 syntax (cannot run online) my_dict = {1: 'one', 2: 'two', 3: 'three', 4: 'four'} print(my_dict.items()) print(type(my_dict.items())) [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')] ...
Read MoreDo you think garbage collector can track all the Python objects?
The garbage collector in Python can track most objects, but it focuses specifically on unreachable objects (reference count of zero) and objects involved in circular references. Understanding when and how garbage collection works is crucial for memory management. What is a Garbage Collector? The garbage collector is an automatic process that handles memory allocation and deallocation, ensuring efficient memory usage. Python uses reference counting as its primary memory management mechanism, with garbage collection as a backup for special cases. We can interact with the garbage collector explicitly using the gc module. By default, it is enabled, but ...
Read MoreHow to return an object from a function in Python?
In Python, functions can return objects of any type using the return keyword. This includes simple values, complex data structures, and custom objects. The statements after the return will not be executed. The return keyword cannot be used outside a function. If a function has a return statement without any expression, the special value None is returned. Returning Simple Values Here's a basic example of returning a calculated value ? def sum_numbers(a, b): return a + b my_var1 = 23 my_var2 = 105 result = sum_numbers(my_var1, my_var2) print(result) ...
Read MoreDo you think declarations within Python class are equivalent to those within __init__ method?
In Python, the declarations within a class are not equivalent to those within the __init__() method. Class declarations create class attributes shared by all instances, while __init__() declarations create instance attributes specific to each object. A class is a collection of objects that contains the blueprints or prototype from which objects are created. It is a logical entity that contains attributes and methods. Python __init__() Method The Python __init__() function is a constructor method called automatically whenever an object is created from a class. Constructors are used to initialize objects by assigning values to the data members ...
Read MoreExplain Inheritance vs Instantiation for Python classes.
In Python, inheritance and instantiation are two fundamental object-oriented programming concepts. Inheritance allows one class to derive properties from another class, while instantiation creates objects from a class definition. Understanding Inheritance Inheritance is the capability of one class to derive or inherit properties from another class. The class that derives properties is called the derived class or child class, and the class from which properties are being derived is called the base class or parent class. Syntax class Parent: # base class pass class Child(Parent): # derived class ...
Read MoreHow to convert a string to a Python class object?
Converting a string to a Python class means accessing a Python class using its name stored as a string, allowing dynamic creation of objects at runtime. This is useful for factory patterns, plugin systems, and configuration-driven object creation. Using globals() Function The globals() function retrieves classes from the global namespace using their string names ? class Bike: def start(self): print("Bike started!") class_name = "Bike" cls = globals()[class_name] # Convert string to class obj = cls() obj.start() print(type(obj)) The output of ...
Read MoreWhen are python classes and class attributes garbage collected?
In Python, a class is a blueprint for creating objects. It contains attributes and methods that define the behavior of objects created from it. Understanding when classes and their attributes are garbage collected is crucial for memory management. class TutorialsPoint: def __init__(self): print("Welcome to TutorialsPoint.") obj1 = TutorialsPoint() Welcome to TutorialsPoint. When are Python Classes Garbage Collected? In Python, classes are garbage collected when there are no references to the class itself and no instances ...
Read MoreWould you recommend to define multiple Python classes in a single file?
Yes, it is recommended to define multiple Python classes in a single file when they are logically related. This approach improves code organization and readability while avoiding the overhead of managing numerous small files. If we define one class per file, we may end up creating a large number of small files, which can be difficult to keep track of. Placing all the interrelated classes in a single file increases the readability of the code. If multiple classes are not interrelated, we can place them in different files to improve maintainability and scalability. What is a Python ...
Read More