How everything is an object in python?

Image credit: AnalyticsVidhya

In recent times, me and my friends thinking about why everything is an object in python?

To know more about we have done the research and finally found it.

I can guess you also want to know why everything is an object in python?

What features and quality made the python object-oriented programming language?

To know more about why everything is an object in python? You have to find what is an object?

The object has a different definition in every programming language.

You are going to see what is object?


What is an object in python?

Object

In python, the object is a component of a program that holds both data(variables) and methods(function).

For example, In the factory, 🏭 the component of the system, holding raw material and action in assembly lines is called an object.

In this example, the raw material is data and the assembly line is methods.

Sometimes in the place of a method python could hold attributes.

For example,

Output:

In the above example, you can see that I have applied the method type to the data of x.

The most interesting part is that class and type both synonymous with each other.

In this example, you can see that an str is an object which holds both data and attributes.


Everything is an object

In object-oriented programming languages like python, the object is a component that holds data and functionality together.

In python, everything is an object that means every component has data(attributes) and action(methods or functions).

These data and methods can access via dot(.) Syntax.

For example,

Output:

List

In the above example, you can see that I have worked on variable x with the help of remove method.

Also, access the attribute with the help of dot(.) Syntax.

When I say everything in Python is an object, I mean it.

Even the attribute and methods in object are also objects.

For example,

>>>x = 5.0
>>>x.is_integer()
True
>>>type(x.is_integer())

builtin_function_or_method

In the above example, you can see that data x and method is_integer is also objects themselves.


Why everything is an object?

Everything is an object that gives great benefit to using python. The objected-oriented created great advantages.

For example,

1. Easy troubleshooting

Object-oriented programming gives easy troubleshooting that means if the problem found in some parts you have not to look anywhere.

You can attack that part and solve it.

2. Reuse code through Inheritance

You can reuse the code in python with the help of Inheritance.

Inheritance is building new in the base of old code.

For example, you have to build a new car tesla in the base of Honda.

There are some commonalities in both car but you have to build a tesla car.

For more, you can visit my blog python-inheritance on reusable code.

3.Data redundancy

With the help of the data redundancy, you can store the same piece of data in a two different place

For example, the sale record of a salesman. In this case multiple sales of the same salesman store in a different place.

But the name of the salesman is the same in both entries. It’s data redundancy.

4. Make a security

With the use of data hiding and abstract, you can secure the data in python. So data will not be visible to attackers.

5. Effective problem solving

Suppose, you have to solve the problem. So, you don’t check the whole program.

Simply you can break the program into small pieces and solve the problem in a specified area without affecting another part of the program.

The great benefit of OOP(object-oriented programming) is that it does affect the whole program to solve the problem.


This all advantage of OOP makes the reason you should use python 🐍 programming language.


Conclusion:

Everything in python is object means every data and methods are associated to each other.

With the help of the OOP(object-oriented programming), you can store same piece of data in different places.

In python, you can group similar things.

You don’t need to keep track of little things in python because Everything is in python is an object.

There are many advantages of OOP(object-oriented programming) which increase the productivity of developers.



I hope you loved this blog. If you found something misinform you can comment on it.


For more, you can visit

python-inheritance
python-class
python-intro

Also, you can


"Quality brings clarity"

Python Syntax- Rule of programming language

Rule :

Most of the things in the world are happening with a certain rule.

Language just English, Hindi, Marathi every language has rule.

The same python has some rule. they called syntax. The syntax has a value for every programming language.

They were made upon that base.

Python Syntax

Table of Contents :

• Execute Python Syntax

• Python Indentation

• Python Variables

• Python Comments

1. Execute Python Syntax

Syntax in Command line

Python syntax can directly execute in Command-Line.

Also, execute in making a file and save it.

Using file

Python Indentation

Indentation referring spaces at the beginning of the code line.

In another programming language Indentation use for purpose of readability.

In python, it represents the block of code.

Example

Python Indentation

Python will give you an error if you missed an indentation because the second line of code separate from the first line.

Example

Indentation error ⚠️

The number of spaces is up to you. I recommend giving 4 spaces because it is a python principle.

Example

Number of spaces

You have to use the same number of spaces in the same blog of code. Otherwise, Python will give you an error.

Example

Different spaces in same blog

Python Varibles

Python variable creates when you assign a value to it.

Example: Variables in Python

• We can declare a variable without a command in Python.

Comments

Comments are describing what we are doing in a particular line.

It starts with #. After # python renders the rest of the line as a comment.

Example:-

Mistakes You can make:-

• Taking spaces in code not seriously.

Remember one extra or less space can lose your 1hour or the whole day.

• Giving different spaces in a different block of code.

• Not using comments. Comments are used if you come after some time then you know what you have done previously.

Conclusion:-

Every programming language has its syntax. Python has an easy syntax comparison to other programming language. Easy to implement less confusion and execute on both command line and as a file.

Python Generation

After C,C++ and Java python programming changing the world ,how we see the coding. So,why we are talking about python generation ?

Python in US, the 5th grade students are learning. The teacher s making it interesting through quizzes,games,problem solving approaches. So, I ask to many people what can we do ?

In india we can introduce early. Make a new system to learn programming,not make only boring logical stuff. The reason behind the lack of coding language is that we don’t know what we need through this stuff. Python has large library support, easy syntax and easy to adapt . We can make great web structure from python .

We can learn programming through fun game making language. Python has a facility of pygame in through which we can create game.

Python is changing the world,it has added to Google search engine. Netflix is using it largely. It is gaining large access to corporate world. In this generation kids are growing with great technological advancement. They are using Python as theire primary programming language to learn.

Python also facilities with great framework,Just like django,numpy etc. I recommend you to study the book python for kids. I know it’s for kid,but it giving great insight of python programming language. It is not only for python. It’s about your logical thinking and intuition how you can adapt change easily.

In last time I can say the world is moving fast. In field of Computer science everything changing fast. If we have to move forward we have to change fastly.

I hope you like this . If there is any mistake you can comment on it,I feel helpful about this. If you have anything interesting you can tell on comment .

Using try-except blog

Python program to demonstrate try-except block…

1.Why we need try-except block ?

Ans: Many times the error is coming can be any type syntax,zero division etc. To give protection from attacker and for understanding of non technical person we using try-except block.

2.What is the example of this ?

Ans: The following are some examples :-

Here we can easily see that in first line is the problem. So,attacker can easily figure out in which line he/she can stole data.

And also it is hard for normal user to understand what is the program wants to tell.

3.just show how can we solve it ?

Ans : use try-except block~~~……….

a = "hello
try:
print(a)
except SyntaxError:
print("Hello")

Output:

Hello


4.try – except – else block use.

Ans : The else block apply when try block execute.

a = 10/2

try:
print(a)
except ZeroDivisionError:
print("Number is not
divisible by
zero !")
else:
print("The number is
odd number !
")

output :

5
The number is odd number !

●●● If try – exception not catch the error ,the else block applies ●●●

Working with Files

♤Write Method

Today,we are going to see how to write in python file. Write in file uses “w”. Syntax of write method. file.write(),write take one display statement.

■■greeting.py

"Hello, world!"
________________________


So,question is how we will do it ?

1.Open the file.
2.Read the file.
3.Close the file.
4.Open the file in write
Mode.
5.use write method.
6.Inside write method,
Whatever content we
want to write.
________________________
________________________
Note: Write method
Overwrite the
file. The old content will erased.
________________________
________________________

■■display.py

f = open("greeting.py",
"r")

with open("greeting.py")
as f:
print(f.read())
f.close()


f = open("greeting.py",
"w")
with open("greeting.py")
as f:
print(f.write("Good
","morning")


Output:

Good,morning

♧append method :

Append method in a file add content to file. Open the file in append mode.

■■Hello.py

"Hello,world ! "

■■go.py
# open the file in
# append mode.

f = open("Hello.py","a")
f.append(" Bob and Alice.")

print(f)

Output :

Hello,world Bob and Alice.

________________________

Note: Append method add
the content in existing file content.
________________________

♤♧Working with files in Python ♧♤

Sometime, we have to work the file in python. For this purpose we use different file operations. The File operations are:-

● Read a file(“r”)

● Write a file(“w”)

● Append a file(“a”)

● Create a file(“x”)

The file operation are happening with help of open() function . Open function return a object.

■ Read the file ■

file.py

1)file_o=open("tesla.py","r")

2)with open("tesla.py")
as file_o:
con = file_o.read()
print(con)

This code open the tesla.py file and read the file.
We have to close the file properly. Because, it can lose some data. We use:

3)file_o.close()

Example,

pi_digits.py

3.1434756892380537685783
Open this file and read the content.

file_reader.py

# store the file name
file_o = "pi_digits.py"

with open("pi_digits.py") as
file_o:
con = file_o.read()
print(con)

Output ■■

3.1434756892380537685783

Inheritance in python

     Python program to demonstrate Inheritance.  so,basically what is Inheritance?

     Inheritance give permission to user to not to start program  from scratch. The first class attribute can be come with help of Inheritance . Example, the first class is car, and second class is ElectricCar .the all car properties can come with help of Inheritance. In this car class is parent class,ElectricCar class is child class.

car.py

class Car():
    """Intialization
of car attributes
"""                 
    def __init__       (self,name,
model,year):
  
      # access the car
  # attribute
      self.name = name
      self.model = model
      self.year = year

    def describe_car(
        self):
        """print the
information    
        about car"""

       
print(self.name+" 
       "+self.model+" "
       +str(self.year))

class ElectricCar(Car):
      """Intialization
of car attributes
"""
      def__init__(self,
name,model,year):
"""Evoking the
class
attributes """
super().__init_
_(name,model,
year)

def display(self):
"""display
ElectricCar
model"""
print("Model:"+
self.model)

# creating a ElectricCar
# object

Vehicle = ElectricCar(
"tesla","S1M",
2016)

print(Vehicle.name)
Vehicle.describr_car()


Output:

tesla
tesla S1M 2016

Note: super() method
evoke the
attributes from
parent class to
child class.



Import the module or File

module is the current file telling python to do the work of stored file in current file.

So,basically why we have to import the file ?

If there is large program so with help of module we can break in small files.

pizza.py

def make_pizza(
size,*toopings
):
""" make_pizza
function give
information about
size and toppings"""

print("The size of
toppings is
"+str(size)
+"!")
for topping in
toopings :
print(tooping)

Create new file im.py

im.py

import pizza

pizza.make_pizza(16,
"mushroom",
"peporoni")

Output:

The size of toppings is:
16

mushroom
peporoni

Arbitary arguments

Python program to show the arbitrary arguments. When program don’t know how many arguments are passed so arbitrary arguments are coming. Arbitrary arguments pack all arguments in tuple.

def show_magicians(         
*magicians
):
"""show_magicians
function show
information about
magicians """

print(magicians)

# calling a function
>>>show_magicians(
"Bob",
"alice")

Output: ("Bob","alice")


Here the values are come in tuple . But we need the information in series of list through loop. So we use for loop to pass arguments as a series of list.

def show_magicians(        
*magicians
):
""" show
information about
magicians """

for magician in
magicians :
print(
magician
)

>>>show_magicians(
"Bob",
"alice"
)

Output:

Bob
alice

Suppose we need positional argument with arbitrary argument. To add another argument with show_magicians. we need circus name with arbitrary arguments.