A function in python is a built-in function which is available to use at any time.
The function is applicable for all kind of datatypes in python, that means these are applicable for List, Tuple, strings, etc..
The Functions are directly called and not on any objects in python and also we can pass the arguments by using the function and function returns the data as a result.
We can define the function by using the def keyword and call it by using the parenthesis
def my-function():
print("Hello")
The Output is:
We can pass the list as a parameter
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
The List of python built-in functions are as follow:
Functions | Description |
Python abs() | returns the absolute value of a number |
Python all() | returns true when all elements in iterable are true |
Python any() | Checks if any Element of an Iterable is True |
Python ASCII() | Returns String Containing Printable Representation |
Python bin() | converts an integer to a binary string |
Python bool() | Converts a Value to Boolean |
Python bytearray() | returns array of given byte size |
Python bytes() | returns an immutable bytes object |
Python callable() | Checks if the Object is Callable |
Python Chr() |
Returns a Character (a string) from an Integer |
Python classmethod() |
returns class method for a given function |
Python compile() |
Returns a Python code object |
Python complex() |
Creates a Complex Number |
Python delattr() |
Deletes Attribute From the Object |
Python dict() |
Creates a Dictionary |
Python dir() |
Tries to Return Attributes of Object |
Python divmod() |
Returns a Tuple of Quotient and Remainder |
Python enumerate() |
Returns an Enumerate Object |
Python eval() |
Runs Python Code Within Program |
Python exec() |
Executes Dynamically Created Program |
Python filter() | constructs iterator from elements which are true |
Python float() |
returns floating-point number from number, string |
Python format() |
returns a formatted representation of a value |
Python frozenset() |
returns immutable frozenset object |
Python getattr() |
returns value of a named attribute of an object |
Python globals() |
returns dictionary of the current global symbol table |
A method in python is similar to the function except that it is associated with an object/classes.
A method is implicitly used for an object for which it is called.
The following table contains all the list Methods:
Methods | Description |
Python List append() | Add Single Element to The List |
Python List extend() | Add Elements of a List to Another List |
Python List insert() | Inserts Element to The List |
Python List remove() | Removes Element from the List |
Python List index() | Returns smallest index of the element in the list |
Python List count() | Returns occurrences of an element in a list |
Python List pop() | Removes Element at Given Index |
Python List reverse() | Reverses a List |
Python List sort() | Sorts elements of a list |
Python List copy() | Returns Shallow Copy of a List |
Python List clear() | Removes all items from the List |
Python any() | Checks if any Element of an Iterable is True |
Python all() | Returns true when all elements in iterable are true |
Python ASCII() | Returns String Containing Printable Representation |
Python bool() | Converts a Value to Boolean |
Python enumerate() | Returns an Enumerate Object |
Python filter() | Constructs iterator from elements which are true |
Python iter() | Returns iterator for an object |
Python list() | The function creates a list in Python |
Python len() | Returns Length of an Object |
Python max() | Returns largest element |
Python min() | Returns smallest element |
Python map() | Applies Function and Returns a List |
Python reversed() | Returns reversed iterator of a sequence |
Python slice() | Creates a slice object specified by range() |
Python sorted() | Returns sorted the list from a given iterable |
Python sum() | Add items of an Iterable |
Python zip() | Returns an Iterator of Tuples |
The append()
method is used to add the element to the given list at the end.
The syntax for the append() method is
list.append()
Example:
List=[10,20,30]
List.append(40)
print(List)
##The output is:
[10, 20, 30, 40]
The following program is to add the elements from 1 to 100 to the list by using the append() method, which is divisible by 10.
List=[]
for x in range(1,101):
if x%10==0:
List.append(x)
print(List)
##The output is:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
The insert()
method is used to add the element to the given list at the specified index.
The syntax for the insert()
method is
list.insert(index,element)
Example:
List=[10,20,30,40,50]
List.insert(1,100)
print(List)
If the specified index is greater than the maximum index present in the list, then the element is added at the end of the list.
List=[10,20,30,40,50]
List.insert(60,100)
print(List)
If the specified index is less than the minimum index, then the element is added at the beginning of the list.
List=[10,20,30,40,50]
List.insert(-60,100)
print(List)
The extend()
method is used to add all the elements to the list in the same sequence
The syntax for the extend()
method is
list_one.extend(List_two)
Example:
List_one=["fruits","veggies","flower"]
List_two=["grains","fibers"]
List_one.extend(List_two)
print(List_one)
The remove()
method is used to remove a specified element from the list
The syntax for the remove()
method is
list.remove(x)
Example:
List=[10,20,10,20,40]
List.remove(40)
print(List)
If the specified element is present multiple times, then the python will remove the first occurrence of the element.
List=[10,20,10,20,40]
List.remove(10)
print(List)
If the specified element is not present in the list then the remove()
method will throw a value error.
The following example demonstrates the remove() method
List=[1,2,3,4,5,6]
print("Before Removal:",List)
element=int(input("Enter value to remove:"))
if element in List:
List.remove(element)
print("After Removal:",List)
else:
print(element,"not available in the list")
The following example demonstrates how to remove all the occurrences of an element present in the given list.
List=[1,1,1,1,2,2,2,3,3]
print("Before Removal:",List)
element=int(input("Enter value to remove:"))
while True:
if element in List:
List.remove(element)
else:
break
print("After Removal:",List)
If we try to remove an element, which is not present in the list then the output is:
The pop()
method is used to remove an element present at the end of the list and returns the removed element.
The syntax for the pop() method is
list.pop()
Example:
List=[10,20,30,40,50,60]
List.pop()
print(List)
##The output is:
[10,20,30,40,50]
If we try to remove an element which is not present in the given list and if we apply pop() method to an empty list, then pop() method will throw an index error.
List=[10,20,30,40,50,60]
List.pop(70)
print(List)
##The output is:
Traceback (most recent call last):
File "C:UsersAnup kumareclipse-workspaceFirstProjectPythonFirstPythonPackageFirstPythonModule.py", line 2, in <module>
List.pop(70)
IndexError: pop index out of range
<p style="box-si