A method is a specific kind of function. It behaves like
a function & looks like a function, but differs in the way in which it acts
and in its invocation style.
Does all the things, but is also able to
change the internal state of a selected entity.
Owned by the data it works for, while a
function is owned by the whole code.
result = data.method(arg)
The name of the method is preceded by the
name of the data which owns the method. Next we add a dot, followed by
the method name, and a pair of parenthesis enclosing the arguments.
A function doesn’t belong to
any data. Actually it gets data, it may create new data and it generally
produces a result.
It takes an argument, does something and
returns a result.
result = function(arg)
Adding elements to a list
append () Method
It takes its argument’s value & puts it at
the end of the list which owns the method. The list’s length then increases
by one.
list.append(value)
insert () Method
It can add a new element at any place in
the list, not only at the end.
list.insert(location, value)
See the below examples.
When by creating empty lists,
Example -1
Output is
Example -2
Output is
For Loop with Lists
Example -1
Example -2
The output is 27 in both above examples.
No comments:
Post a Comment