In the previous post, we covered some basic stuff about python like data types, data structures, loops and statements, after reading the tutorial you must be good enough to learn more fundamental principle of almost all programming languages rely on, the principle of object-oriented programming. For most of the new programmes, it takes much time to understand what does objects and classes actually mean, why one should use them and much more. 

Today we will try to learn some basic fundamentals of objects and classes in the Python programming language. 

To learn about classes we first have to learn about methods, in short methods are just a group or some lines of code in simple words, for example here is the basic example of hello world!

def say_hello:
    print('Hello World!')

Here we have defined a method named say_hello let me remind you that you can not write it like say hello, there are some naming conventions in python programming you have to follow. so let's say say_hello is the name of the group, when we call the group or run the method it will run the lines of code that comes under the group, the only line in the method, for now, is print("Hello World!") so it will run the line and stop. so next time when we have to print hello we will just write say_hello() and it will print hello.

Printing hello is a really small task, but sometimes we have to do a much complex task, this is where methods help us, let's understand it here with a simple example.

def do_sum():
    print(1 + 2) #will print 3
do_sum() #here we are calling do_sum method

here we created a do_sum method which has two lines one for doing the sum of two numbers and another is printing them.

Let's take another example by placing variables in the method.

def do_sum():
    a= 1 #assing 1 to a
    b = 2 #assigning 2 to b
    sum_ = a+b #assigning a + b to sum_, We cant use sum so we used sum_
    print(sum_) #will print 3
do_sum()


Here in this example, we defined two variable a and b which were assigned on the place of 1 and 2, and then added it and stored the value in a variable named sum_. then when we printed the some it printed 3, so a was 1, b was 2 so summing them results 3 and that assigned to sum and we printed sum_.

but wait we talked about in methods we don't have to repeat code, but we don't have to do the sum for same numbers again and again, we have to change numbers, how we do it? let's have to look at this simple example.

def do_sum(a,b): # here it will assign 5 to a and 6 to b 
    sum_ = a + b # will sum a and b and store it to sum_
    print(sum_) #print sum that is 11
do_sum(5,6) #pasing 5 and 6 to do_sum method

Here you may have experienced that I have written a and b in the bracket while declaring method, so as you can also notice that I have written 5 and   6  in the bracket while calling that method, it should be making things clear.  

Here when we call the method, we will assign the numbers in the calling bracket to the variables in the bracket of the method, so here the first number in calling bracket is 5, so one will be assigned to a and the second number is 6 so it will be assigned to b.  if its still a little confusing for you here are some more examples you should look at.

def do_sum(a, b):
    sum_ = a +b
    print(sum_)
do_sum(1,2) # will print 3
do_sum(10,10)  # will print 20
do_sum(6,5)  # will print 11
do_sum(3,4)  # will print 7
#here as you have already noticed we have only one method but you can call is as many time
# as you want

Now it should be clear to you, we call the method with offering it numbers, when we call the method we put some variables in the bracket,  then the method takes the number and do whatever we made it do, this is called as variable passing. I would suggest you practice it a little on your computer before proceeding further.


Source : Image:https://www.udemy.com/python-in-2-hours-python-programming-for-beginners
Continue Reading