Programming
Python Data Structures Data-types and Objects
Handy concepts on class objects in python
Python is an object-oriented language and the basis of all data types are formed by classes. Its variable assignment is different from c, c++, and java. The variable does not have a declaration, it is just an assignment statement.
>>>marks=79Python Objects
Python is a dynamically typed language. It has no knowledge about the variable’s datatype until the code executes. Hence, a declaration is of no use. The value is stored at some memory location, which is bound up with the identifier and makes the contents of the container accessible through that identifier. So the data type does not matter and the type of the particular identifier will be known during runtime.
Therefore, an identifier can be associated with any object type and can be later assigned to another object of the same or different type.

How to create objects?
The process of creating a new object or a new instance of a class is known as instantiation. In general, we do this by invoking the constructor of a class.

Many of the python’s built-in classes support literal form for designating new instances. For example,
>>> Age = 20Creates a new instance of the int class, the term 20 in the expression is a literal form.
Python’s Built-in Classes
Before moving on, we should know what exactly mutable and immutable classes are. The Immutability of a class means each object of that class points to a fixed value on instantiation, and a unique object ID is assigned to it. The type of the object is defined at the runtime and it can’t be changed later on. Whereas, in the case of mutable, it’s the state can be changed.

The bool Class
- Used to manipulate logical values.
- The only two instances of the class are expressed as literals True and False.
- By default, the constructor bool () returns False.
- Any expression evaluated to zero is False and the rest is True.
>>> result=bool()
False
>>>answer=True
>>>print(answer)
TrueThe int Class
- Designed to represent integer value with arbitrary magnitude.
- In some contexts, it is convenient to express an integral value using binary, octal and hexadecimal.
>>> x=20 #decimal literal
>>> y=0o47 #octal literal
>>> z=0x1f #hexadecimal literal
>>> w=0b10101 #binary literal- By default, the constructor int() returns value 0.
The float Class
- The float class is the main floating-point type using a fixed-precision representation.
- The float() form of constructor returns 0.0.
- One other form of literal for floating-point value uses scientific notation.
Sequence Types of data structures- The list, tuple and str Classes
The list Classes
- An ordered collection of one or more data items can be of the same or different types, enclosed in square brackets.
>>>myLIst = ["kajal",2021,20,5.5,True]
>>>myList
["kajal",2021,20,5.5,True]- Just like the arrays, declared in other languages, indexed from 0 to n-1 where n is the size of the list. List datatype is quite flexible as the elements don’t need to be of the same type.
- By default, the constructor list() returns an empty list. This constructor can accept any parameter of iterable type. E.g.,

The tuple Class
- Same as a list, however, immutable.
- Immutable because one can’t add and remove elements from the container.
- In-place sorting also can’t be done.
- In python, [] character is used to delimit a list, similarly, parenthesis () delimits a tuple.

Note: For a one-element tuple, the expression (element) won’t work as this expression considered as a simple parenthesized expression rather than a tuple element. Therefore, the element should be followed by a comma like this: (element,)
e.g,

The str Class
- Strings are collections of letters, numbers and other symbols, aka characters in a sequence. Literal string values are differentiated from identifiers by using single or double-quotes.
>>>name="Kajal"
>>>newName='Aman'- A major difference between lists and strings is that lists can be modified while strings cannot, which means that str class is immutable i.e., list elements can be changed using assignment and indexing whereas string’s element can’t be changed.

- Triple-quotation is used for multiline strings in the code. It is because the newline character is embedded naturally rather than escaped as \n.

The set and frozenset Classes
- Python uses {} as a delimiter for sets.
- This set class represents the mathematical notion of a set, i.e., a collection of distinct elements (without duplicates).
- There are two important restrictions :
- The set doesn’t maintain the elements in any particular order.
2. Only instances of immutable type can be added to the python’s set.
- The frozenset class is the set only, but immutable one.
- The hash table is used based on a data structure.

The dict Class
Python’s dict is the same as the dictionary. It is mapping from a set of distinct keys to associated values.
- A dictionary literal also uses curly braces {}, this literal form {} produces an empty dictionary. A non-empty dictionary is represented using a comma-separated series of key-value pairs.

Conclusion:
The Data Structures in python is very easy to learn and understand. They are the backbone of programming language.
I hope you like the article. Reach me on my LinkedIn and twitter.
Recommended Articles
1. 15 Most Usable NumPy Methods with Python 2. NumPy: Linear Algebra on Images 3. Exception Handling Concepts in Python 4. Pandas: Dealing with Categorical Data 5. Hyper-parameters: RandomSeachCV and GridSearchCV in Machine Learning 6. Fully Explained Linear Regression with Python 7. Fully Explained Logistic Regression with Python 8. Data Distribution using Numpy with Python 9. 40 Most Insanely Usable Methods in Python 10. 20 Most Usable Pandas Shortcut Methods in Python






