Programming Articles

Page 180 of 2547

How do I modify a string in place in Python?

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

Strings in Python are immutable, meaning you cannot modify them in place. However, you can create new strings or use mutable alternatives like io.StringIO and the array module for in-place modifications. Why Strings Cannot Be Modified In Place When you try to change a string character, Python creates a new string object rather than modifying the original ? text = "Hello" print("Original:", text) print("ID:", id(text)) # This creates a new string, doesn't modify the original text = text.replace('H', 'J') print("Modified:", text) print("New ID:", id(text)) Original: Hello ID: 140712345678912 Modified: Jello New ID: ...

Read More

How can my code discover the name of an object in Python?

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

In Python, objects don't have inherent names − variable names are just labels that point to objects in memory. When multiple variables reference the same object, there's no way to determine which variable name was used to create it. Why Objects Don't Have Names Consider this example where both ob1 and ob2 reference the same object ? # Creating a Demo Class class Demo: pass # Multiple references to the same object ob1 = Demo() ob2 = ob1 print("ob1 identity:", id(ob1)) print("ob2 identity:", id(ob2)) print("Same object?", ob1 is ob2) ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 6K+ 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 256 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 338 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
Showing 1791–1800 of 25,469 articles
« Prev 1 178 179 180 181 182 2547 Next »
Advertisements