Sunday, October 14, 2018

Python Ninja Bootcamp 22-Method & Function

Learn Python like a Professional! Take You from 0 to Hero




In this Python lecture, we will learn about the Method & Function.


Method in python        
 
"Method is a function that belongs to an object"

Let's see some example-

l=[5,4,3,2,1]
print(l) 
>>[5,4,3,2,1]


l.append(0) # add 0 at the End of list
>>[0] 

Note: If Press TAB after l. in the jupyter notebook. It will show the all the Methods available in list.

print(l)
>> [5,4,3,2,1,0]

Let's try another method

l.sort() # Sort the list in ascending order 
print(l) 
>> [0,1,2,3,4,5]

Note: If Press SHIFT+TAB after l.sort in the jupyter notebook. It will show the documentation of that Method.

If we are using another IDE, we could use HELP function to show documentation

help(l.sort)
>>  Help on built-in function sort:
sort(...) method of builtins.list instance L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*


Function in python        
 
"Function is a group of related statements or could say reusable code that put together perform a specific task"
Let's see some example-

def first_function():
    print('hello')
    print('Tamta')
 
first_function() 
>>hello
    Tamta




Parameter in function

It's used to Pass the values to the function 


def second_function(name):
    print('hello')
    print(name)
 
second_function('Dheru') 
>>hello
    Dheru 

Default Parameter
  
Parameters initialized with default values if no value or undefined is passed.


def second_function(name='Piku'):
    print('hello')
    print(name)
 
second_function() 
>>hello
    Piku


Return to Function

If any value is send back to the calling function is called as return to function

def sum_function(a,b):
    return a+b
 
print(sum_function(1,2))
>>3


EDITS ARE WELCOMED!!

In the next Blog, we will discuss Lambda Expression 

https://sngurukuls247.blogspot.com/2018/10/python-ninja-bootcamp-23-lambda.html


......................................................................................................................................

Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnw

Instagram-
https://www.instagram.com/python.india/ 
 

View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 



Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

No comments:

Post a Comment