Python Articles

Page 457 of 852

How do I modify a string in place in Python?

AmitDiwan
AmitDiwan
Updated on 16-Sep-2022 2K+ Views

Unfortunately, you cannot modify a string in place because strings are immutable. Simply create a new string from the several parts you want to gather it from. Though, if you still need an object with the ability to modify in-place unicode data, you should for The io.StringIO object The Array module Let’s see what we discussed above − Return a string with the entire contents of the buffer Example In this example, we will return a string with the entire contents of the buffer. We have a text stream StringIO − import io myStr = "Hello, How ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 16-Sep-2022 2K+ Views

No, there’s no way to discover the name of an object in Python. The reason is that the objects don’t really have names. Let’s say we have the following code. Herein, we cannot find the actual instance name. Since both ob1 and ob2 are bound to the same value, we cannot come to a conclusion that whether the instance name of ob1 or ob2 − Example # Creating a Demo Class class Demo: pass Example = Demo ob1 = Example() ob2 = ob1 print(ob2) print(ob1) Output As we saw above, we ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 16-Sep-2022 6K+ Views

The gc or weakref module is used to get a list of all instances of a given class. First, we will install the gc module using pip − pip install gc To use the gc module, use the import − import gc Get the instances of a class using the gc module In this example, we have created a Demo class with four instances − ob1 = Demo() ob2 = Demo() ob3 = Demo() ob4 = Demo() We loop through the objects in memory − for ob in gc.get_objects(): Example Using the isinstance(), each object is ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 16-Sep-2022 7K+ Views

Python Functions are generally called using their name. However, you can also use strings to call functions. For that, use the locals() and globals(). Call functions using strings Example In this example, we will learn how to call two functions using strings − def demo1(): print('Demo Function 1') def demo2(): print('Demo Function 2') locals()['demo1']() globals()['demo2']() Output Demo Function 1 Demo Function 2 Call a function using string variable Example In this example, we have created a class Example with the function xyzuvw() that accepts arg and print them. The globals() ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 16-Sep-2022 1K+ Views

To convert a string to a number, there are various ways. Let’s see them one by one. Convert a string to number using int() Example In this example, we will convert a string to a number using the int() method − # String to be converted myStr = "200" # Display the string and it's type print("String = ", myStr) print("Type= ", type(myStr)) # Convert the string to integer using int() and display the type myInt = int(myStr) print("Integer = ", myInt) print("Type = ", type(myInt)) Output String = 200 Type= Integer = ...

Read More

What are the best Python resources?

AmitDiwan
AmitDiwan
Updated on 16-Sep-2022 243 Views

Resources for any programming language includes Video Courses, Notes as well as E-books. Here, I will list the best resources of Python. Python Official Documentation A lot of websites are providing Python resources, but the official documentation still works the best. Let’s see the resources provided by them. Beginners Guide To Python − https://wiki.python.org/moin/BeginnersGuide Developer’s Guide To Python − https://devguide.python.org/ Free Python Books − https://wiki.python.org/moin/PythonBooks Python Standard Library − https://docs.python.org/3/library/index.html HOWTOs on Python −https://docs.python.org/3/howto/index.html Latest Talks on Python −https://pyvideo.org/ Python Resources Now, let us talk about the Python Resources except the official documentation. Following are some of ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 16-Sep-2022 2K+ Views

To find the attributes of an object, use the getarr() method in Python. To check if an attribute exist or not, use the hasattr() method. Set an attribute using the setattr() method in Python. Access the attributes of an object Example To access the attributes of an object, we will use the getattr() method in Python − class student: st_name ='Amit' st_age ='18' st_marks = '99' def demo(self): print(self.st_name) print(self.st_age) ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 16-Sep-2022 4K+ Views

We can sort a list by values from another list by setting up the 2nd list as index numbers for the values in the 1st List in sorted order. Sort a List by values from another list Example In this example, we will sort a list by values from another list i.e. the 2nd list will have the index in the order in which they are placed in sorted order − # Two Lists list1 = ['BMW', 'Toyota', 'Audi', 'Tesla', 'Hyundai'] list2 = [2, 5, 1, 4, 3] print("List1 = ", list1) print("List2 (indexes) = ", list2) # ...

Read More

How do I create a multidimensional list in Python?

AmitDiwan
AmitDiwan
Updated on 16-Sep-2022 1K+ Views

Multidimensional Lists are Lists within Lists. The left index as row number and right index as column number, for example list[r][c] Above, r is row number and c is column number. Let’s see an example. For multidimensional list 2x3 − list [2][3] Create a Multidimensional Python List Example In this example, we will learn how to create a Multidimensional List in Python. We will also iterate and print the array. # Create a Multi-Dimensional Python List mylist = [[2, 5], [10, 24, 68], [80]] print("Multidimensional List") for outList in mylist: print(outList) Output Multidimensional List ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 16-Sep-2022 4K+ Views

To remove more than one item from a list, we can use various ways as discussed in this article. Let’s say have the following input List − ["David", "Jacob", "Harry", "Mark", "Anthony", "Steve", "Chris"] Following is the output when multiple elements “David” and “Harry” are removed − ["Jacob", "Mark", "Anthony", "Steve", "Chris"] Remove multiple items from a List Example To remove multiple items from a List, use the del keyword. The del allows you to add the items you want to delete in a range using square brackets: # Creating a List mylist = ["David", "Jacob", "Harry", "Mark", ...

Read More
Showing 4561–4570 of 8,519 articles
« Prev 1 455 456 457 458 459 852 Next »
Advertisements