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=79

Python 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.

Image for post

Example of Variable Assignment. A photo by Author

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.

Image for post

Example of making class and object. A photo by Author

Many of the python’s built-in classes support literal form for designating new instances. For example,

>>> Age = 20

Creates 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.

Image for post

The table to differentiate type of Data Structure

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)
True

The 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.

#data-visualization #programming #python #machine-learning #developer

Python Data Structures Data-types and Objects
2.30 GEEK