Javatpoint.com is changed to TpointTech.com
__getitem__() in PythonIn Python, there are a lot of functions that are used for particular purposes, and they have double underscores before and after the function name. They are called magic functions or dunder methods. For example: __abs__ , __contains__ etc. Some examples of magic methods are as follows: __str__() function: This function is used to return the printable string. __add__() function: This function is used to add the two objects or attributes. _ge__() function: This function is used to overload the greater than or equal operator. ( >=). __floor__() function: This function is called by the Math.floor() function internally. __ceil__() function: This function is called by the Math.ceil() function internally. __trunc__() function: This function is called by the Math.trunc() function internally. __init__() function: This is the constructor for any class, which is called by the __new__() function. __getitem__() method: __getitem__() is also a magic method in Python that is used in a class, and it gives the flexibility to any instance of the class to use the indexer operator. Syntax:So, it is used to evaluate the value of self[key] by any object or instance of the class. Let's suppose we have one object obj, then obj[key] will be equivalent to obj.__getitem(key). We will some examples to understand the __getitem__() function: Example1:Output: ![]() Explanation: In the above code, we have one custom class named JavatPoint. In this class, we have our own definition of the __getitem__() function where we print the some statement with the key. Then we created its instance name myObj, and then we passed the numeric value 6000 as the key. It will print the statement and the value of the key. Then in the next line, we used a string as a key using an indexer, which will print the statement accordingly. Example 2:Output: ![]() Explanation: In the above code, we have one class named JavatPoint, where we have defined four functions. In the init function, we have taken a list. Then we have created an instance of the class with a list with four string values. In the first statement, we have printed the value at 1st index so it will print the value and the statement of the __getitem__() function. Then we changed the value from string to integer and again called the indexer, so it printed again. Then we deleted the first index value of the list using the __delitem__() function. So the value at the second index will be shifted to the left, and it will be printed as the first index. Example 3:Output: ![]() Explanation: In the above code, we have the class named Javatpoint, which accepts the object and it has the function __getitem__ () in which we pass a key or items, and in this function, we print the type of the key and the value of the key. Then we created the object or instance of the Javatpoint class named test and passed different types of keys into the test using indexer([ ]). In the first example, we passed value 69, so it printed the data type as 'int' and its value. The second example is of a type slice object, so it is printed as a class slice. The third example is a statement, which has the datatype as str or string. The fourth example is of a type tuple, and the fifth example is of a type slice. The last example is of a type object, so it prints the value or the address of the object. Next TopicGET and POST Requests using Python |
Javatpoint.com is now changed to TpointTech.com, so we request you to subscribe our newsletter for further updates.
? This tutorial is meant for beginners who are just hearing the word Python too often and are wondering what this Python is, mostly high-schoolers who are just starting their BTech. Welcome to the world of programming and technology. This tutorial will be like your introductory brochure...
6 min read
What is PySpark DataFrame? A dataframe in PySpark is a collection of data that is grouped into columns. A DataFrame is similar to a relational table in SparkSQL. We can create the pyspark dataframe with different functions in SparkSession. PySpark MapType MapType in PySpark is a data type to...
4 min read
In this tutorial, we will learn how to calculate the moving averages of time series data in Python. Moving averages is the statistical measure that refers to a set of average values obtained from the data list of fixed length. These data values are the subset...
13 min read
Selenium is a powerful tool for automating web browsers, and it's widely used for testing web applications. One of the essential functionalities in web testing is navigating through different pages. The back() method in Selenium allows you to simulate the action of clicking the "Back" button...
4 min read
We will gain knowledge about both Python operator precedence and associativity in this tutorial. Understanding the mechanics of Python operators is critical for developers. It would be best if the reader understood how Python assesses the ordering of its operators after checking it. Some operators prioritize others;...
4 min read
Introduction IDLE stands for Integrated Development and Learning Environment. The lightweight and user-friendly Python IDLE (Integrated Development and Learning Environment) is a tool for Python programming. Since version 1.5.2b1, the standard Python implementation has included IDLE, an integrated development environment. Many Linux distributions include it in the Python...
6 min read
It is the StringIO module is an in-memory, file-like object. It can be used to input or output the majority of functions users can expect from an ordinary file object. Once the user creates the StringIO objects, it is initially created by providing a string to...
5 min read
In this tutorial, we will demonstrate how to access a person's data using their person ID, which is an individual identification number assigned to everyone by IMDb. The search person method can be used to find people by name, but because many people have the same...
1 min read
It may seem funny, but infinity is a concept that refers to an ambiguous number that can have either a negative or a positive value. Every arithmetic operation, like subtraction, division, or any other-is conducted on infinity, or an infinite value always results in an infinite...
3 min read
? Sets and Lists are the data structures in Python that consist of groups of data elements. However, these two data structures have some significant differences too. In specific, the lists perform a few ranges of mathematical operations that are useful to search for particular elements -...
4 min read
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India