Mon. May 6th, 2024

Objects

An object is a software bundle of variables and related methods.

To create an object:

To create an object first we need to create a class.

To create a class:

Object Methods

Objects can also contain methods. Methods in objects are functions that belong to the object.

Let us create a method in the Person class:

Example:

Container Objects

Containers are any object that holds an arbitrary number of other objects. Generally, containers provide a way to access the contained objects and to iterate over them. Examples of containers include tuple , list , set , dict ; these are the built-in containers.

List

The list is the primary data structure used in Python. It holds a sequence of data elements. You can think of a list as an advanced array for now. The main differences between lists and arrays, which we see in Java and C, is that lists may contain data of differing data types and lists also have features such as list.append(), list.insert(), list.pop() and list.remove() (delete), which give lists additional functionality similar to linked lists in Java and C. The advance capabilities of lists allow one to construct common abstract data storage models such as stacks and queues in Python with very little effort or code.

In addition to being used to hold a sequence of data, lists also represents one of the most common iterator data types. An iterator is any sequence which may evaluated, one item at a time, in a for loop. The range() fuction, which we can use to create a definite loop, returns a list object.

Square brackets [] are used when we create or print a list:

Example:

Tuples

A tuple is a simplified list. Tuples are immutable, which means that after the tuple is created, it can not be modified (it is read-only). Lists are more useful, but tuples are useful in certain situations and the implementation of tuples is a little leaner, so they can be faster. Use a tuple anytime you want to keep several values together as a single item and do not need to modify those values, at least not while they are stored inside the tuple. Parenthesis () is used when we create or print a tuple.

Example:

Dictionary

A dictionary provides a way to associate key words with a value. Each key, which is usually a string, must be unique. The keys are also immutable. Items in the dictionary may removed with the dict.del() statement.

Curly brackets {} are used when we create or print a dictionary and the key and value are separated using the colon “ : “ .

Example:

Mutability of objects in python

  Every variable in python holds an instance of an object. There are two types of objects in python i.e. Mutable and Immutable objects. Whenever an object is instantiated, it is assigned a unique object id. The type of the object is defined at the runtime and it can’t be changed afterward. However, its state can be changed if it is a mutable object.

To summarise the difference, mutable objects can change their state or contents and immutable objects can’t change their state or content.

  • Immutable Objects: These are of in-built types like int, float, bool, string, Unicode, tuple. In simple words, an immutable object can’t be changed after it is created.

Numbers

There are three numeric types in Python:

  • int
  • float
  • complex

Variables of numeric types are created when you assign a value to them:

To verify the type of any object in python, type( ) command is used.

Int

Int or integer is a whole number, positive or negative, without decimals of unlimited length.

Example:

Float

Float, or “floating point number” is a number, positive or negative, containing one or more decimals.

Example:

Complex

Complex numbers are written with a “j” as the imaginary part:

Example:

Type Conversion

You can convert from one type to another with the int(), float(), and complex() methods:

Example:

Booleans

Booleans represent one of two values: True or False.

Boolean Values

In programming, you often need to know if an expression is True or False.

You can evaluate any expression in Python, and get one of two answers, True or False.

When you compare two values, the expression is evaluated and Python returns the Boolean answer.

Example:

When you run a condition in an if statement, Python returns True or False:

Example:

Evaluate Values and Variables

The bool() function allows you to evaluate any value, and give you True or False in return.

Example:

To evaluate two variables.

Example:

If most values are true

Almost any value is evaluated to True if it has some sort of content.

Any string is True, except empty strings.

Any number is True, except 0.

Any list, tuple, set, and dictionary are True, except empty ones.

Example:

Some Values are False

In fact, there are not many values that evaluate to False, except empty values, such as (), [], {}, “”, the number 0, and the value None. And of course the value False evaluates to False.

Example:

Functions can Return a Boolean

You can create functions that returns a Boolean Value:

Example:

We can execute code based on the Boolean answer of a function:

Example:

Python also has many built-in functions that return a boolean value, like the isinstance() function, which can be used to determine if an object is of a certain data type:

Example:

Strings

Apart from the numbers python can also handle strings which can be operated in many ways which can be enclosed as the single quotes (‘…..’) or the double quotes (“…..”) and \ can be used to escape quotes

‘hai’ is the same as “hai”.

You can display a string literal with the print() function.

Example:

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *