Methods and Functions in Python

Jayashree domala
3 min readOct 6, 2020

--

A guide to learning about the Python methods and functions in detail.

The built-in objects in Python have a variety of methods to use.

Basic methods

>>> list1 = [11,22,33]>>> list1.append(44)
>>> list1
[11, 22, 33, 44]
>>> list1.pop()
44
>>> list1
[11, 22, 33]

Discovering methods on your own

When you write “.” after the object and hit ‘tab’, you get a list of methods in the Jupyter notebook.

To get help on the method you can use “shift+tab”.

list1.insert

In case you don't use the Jupyter notebook, you can also use the below command.

>>> help(list1.insert)Help on built-in function insert:insert(index, object, /) method of builtins.list instance
Insert object before index.

Functions

Functions allow us to create blocks of code that can be easily executed many times without needing to constantly rewrite the entire block of code.

Function syntax (Source: Google)
>>> def function_name():
'''
DOCSTRING: Information about the function
INPUT: no input
OUTPUT: python
'''
print("python")
#call the function
>>> function_name()
python

“DOCSTRING” allows you to document the information about the function. So if in future anyone needs to know it’s details, one can with the aid of “help” function.

>>> help(function_name)Help on function function_name in module __main__:function_name()
DOCSTRING: Information about the function
INPUT: no input
OUTPUT: python

Functions can also take arguments too as shown below.

>>> def function_name(language):
print(language + ' is fun')
#call the function
>>> function_name("python")
python is fun

We will use the “return” keyword to send back the result of the function instead of just printing it out. “return” allows us to assign the output of the function to a new variable and therefore it is preferred over just printing.

>>> def function_name(language):
return language
>>> result = function_name("python")
>>> print(result)
python

The default name can also be given to the argument. So in case, no argument is passed while calling the function, the default name will be taken.

>>> def function_name(language='python'):
print(language + ' is fun')
#call the function
>>> function_name()
python is fun

Function to find a particular word in a string

>>> def word_check(string1):
if 'python' in string1.lower():
return True
else
:
return False
>>> word_check("python is fun")
True

Notice that “‘python’ in string1.lower()” returns a boolean value itself. So we do not need to return boolean value separately.

>>> 'python' in "python is fun"
True
>>> def word_check(string1):
return 'python' in string1.lower()
>>> word_check("python is fun")
True

Refer to the notebook here.

Beginner-level books to refer to learn Python:

Advance-level books to refer to learn Python:

Reach out to me: LinkedIn

Check out my other work: GitHub

--

--

Jayashree domala
Jayashree domala

Written by Jayashree domala

Self-driven woman who wishes to deliver creative and engaging ideas and solutions in the field of technology.

No responses yet