Python Articles

Page 202 of 855

How do I get a list of all instances of a given class in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 7K+ Views

Python provides several ways to get a list of all instances of a given class. The most common approaches use the gc module or the weakref module. The gc module is part of Python's standard library and doesn't need separate installation. Using the gc Module The gc (garbage collector) module allows you to access all objects tracked by Python's garbage collector. You can filter these to find instances of a specific class ? import gc # Create a class class Demo: pass # Create four instances ob1 = Demo() ob2 ...

Read More

How do I use strings to call functions/methods in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 7K+ Views

Python functions are generally called using their name. However, you can also use strings to call functions dynamically. This is useful when the function name is determined at runtime or stored in variables. Using locals() and globals() The locals() function returns a dictionary of local variables, while globals() returns global variables. You can use these dictionaries to call functions by name ? def demo1(): print('Demo Function 1') def demo2(): print('Demo Function 2') # Call functions using string names locals()['demo1']() globals()['demo2']() Demo Function 1 ...

Read More

How do I convert a string to a number in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

Python provides several built-in functions to convert strings to numbers. The most common methods are int() for integers and float() for decimal numbers. Using int() for Integer Conversion The int() function converts a string containing digits to an integer ? # String to be converted my_str = "200" # Display the string and its type print("String =", my_str) print("Type =", type(my_str)) # Convert the string to integer using int() my_int = int(my_str) print("Integer =", my_int) print("Type =", type(my_int)) String = 200 Type = Integer = 200 Type = ...

Read More

What are the best Python resources?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 259 Views

Learning Python effectively requires access to quality resources across different formats and skill levels. This guide covers the best official documentation, tutorials, and specialized learning paths to help you master Python programming. Python Official Documentation The official Python documentation remains the most authoritative and comprehensive resource for learning Python. These resources provide everything from beginner guides to advanced implementation details ? Beginner's Guide − https://wiki.python.org/moin/BeginnersGuide Developer's Guide − https://devguide.python.org/ Free Python Books − https://wiki.python.org/moin/PythonBooks Python Standard Library − https://docs.python.org/3/library/index.html Python HOWTOs − https://docs.python.org/3/howto/index.html Python Video Talks − https://pyvideo.org/ Comprehensive Tutorial Resources Beyond official ...

Read More

Why are there separate tuple and list data types in Python?

Sindhura Repala
Sindhura Repala
Updated on 26-Mar-2026 342 Views

Python provides both tuple and list data types because they serve different purposes. The key difference is that tuples are immutable (cannot be changed after creation), while lists are mutable (can be modified). This fundamental distinction makes each suitable for different scenarios. Tuples use parentheses () and are ideal for storing data that shouldn't change, like coordinates or database records. Lists use square brackets [] and are perfect when you need to add, remove, or modify elements frequently. Creating a Tuple Tuples are created using parentheses and can store multiple data types ? # Creating ...

Read More

How can I find the methods or attributes of an object in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

To find the methods or attributes of an object in Python, you can use several built-in functions. The getattr() method retrieves attribute values, hasattr() checks if an attribute exists, and setattr() sets attribute values. Additionally, dir() lists all available attributes and methods. Using getattr() to Access Attributes Example The getattr() function retrieves the value of an object's attribute ? class Student: st_name = 'Amit' st_age = '18' st_marks = '99' def demo(self): ...

Read More

How can I sort one list by values from another list in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 4K+ Views

Sometimes you need to sort one list based on the corresponding values in another list. Python provides several efficient approaches using zip() and sorted() functions to achieve this. Using zip() and sorted() The most Pythonic way is to zip the lists together and sort by the reference list − # Two Lists cars = ['BMW', 'Toyota', 'Audi', 'Tesla', 'Hyundai'] priorities = [2, 5, 1, 4, 3] print("Original cars:", cars) print("Priority values:", priorities) # Sorting cars based on priorities sorted_cars = [car for (priority, car) in sorted(zip(priorities, cars))] print("Sorted cars by priority:", sorted_cars) ...

Read More

How do I create a multidimensional list in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

Multidimensional lists are lists within lists that allow you to store data in a table-like structure. You access elements using two indices: the first for the row and the second for the column, like list[row][column]. Basic Structure In a multidimensional list, each element can be accessed using bracket notation ? list[r][c] Where r is the row number and c is the column number. For example, a 2x3 multidimensional list would be accessed as list[2][3]. Creating a Multidimensional List You can create a multidimensional list by nesting lists inside another list ? ...

Read More

How do you remove multiple items from a list in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 4K+ Views

Removing multiple items from a list in Python can be accomplished using several approaches. Each method has its own advantages depending on whether you're removing by value, index, or condition. Using del with Slice Notation The del keyword with slice notation removes consecutive items by index range − # Creating a List names = ["David", "Jacob", "Harry", "Mark", "Anthony", "Steve", "Chris"] # Displaying the List print("List =", names) # Remove multiple items from a list using del keyword del names[2:5] # Display the updated list print("Updated List =", names) List ...

Read More

How do you remove duplicates from a list in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 612 Views

Removing duplicates from a list is a common task in Python. There are several efficient approaches: using set(), OrderedDict, and list comprehension. Each method has different characteristics regarding order preservation and performance. Using set() Method The simplest approach converts the list to a set, which automatically removes duplicates. However, this doesn't preserve the original order ? # Creating a List with duplicate items names = ["Jacob", "Harry", "Mark", "Anthony", "Harry", "Anthony"] # Displaying the original List print("Original List =", names) # Remove duplicates using set unique_names = list(set(names)) print("Updated List =", unique_names) ...

Read More
Showing 2011–2020 of 8,549 articles
« Prev 1 200 201 202 203 204 855 Next »
Advertisements