20 Python Functions syntax, example and definition







20 Python Functions Every Analyst Uses




1. Length

Definition: Returns the length of a list.
Syntax: len(lst)

Example:

lst = [1, 2, 3, 4, 5]
length = len(lst)
print(length)

Output: 5





2. Append


Definition: Adds an element to the end of a list.
Syntax: lst.append(x)

Example:

lst = [1, 2, 3, 4, 5]
lst.append(6)
print(lst)
Output:
[1, 2, 3, 4, 5, 6]



3. Extend

extend(iterable): Adds all elements of an iterable to the end of a list.
Syntax: lst.extend(iterable)

Example:

lst = [1, 2, 3, 4, 5]
lst.extend([6, 7, 8])
print(lst)
Output:
[1, 2, 3, 4, 5, 6, 7, 8]

4. Insert

insert(i, x): Inserts an element at a specific position in a list.
Syntax: lst.insert(i, x)

Example:

lst = [1, 2, 3, 4, 5]
lst.insert(2, 6)
print(lst)
Output:
[1, 2, 6, 3, 4, 5]



5. Remove

remove(x): Removes the first occurrence of an element from a list.
Syntax: lst.remove(x)

Example:

lst = [1, 2, 3, 4, 5, 6, 7, 8]
lst.remove(4)
print(lst)
Output:
[1, 2, 3, 5, 6, 7, 8]



6. POP

pop([i]): Removes and returns an element at a specific position in a list, or the last element if no index is specified.
Syntax: lst.pop([i])

Example:

lst = [1, 2, 3, 4, 5]
element = lst.pop(2)
print(element)
print(lst)
Output:3
[1, 2, 4, 5]



7. Clear

clear(): Removes all elements from a list.
Syntax: lst.clear()

Example:

lst = [1, 2, 3, 4, 5]
lst.clear()
print(lst)
Output:[]

8. Index

index(x[, start[, end]]): Returns the index of the first occurrence of an element in a list. Optionally, you can specify a start and end index to search within a slice of the list.
count(x): Returns the number of occurrences of an element in a list.
Syntax: lst.count(x)

Example:

lst = [1, 2, 3, 4, 5, 1, 2, 3]
count = lst.count(1)
print(count)
Output:
2

9. Reverse

reverse(): Reverses the elements of a list in place.
Syntax: lst.reverse()

Example:

lst = [1, 2, 3, 4, 5]
lst.reverse()
print(lst)
Output:
[5, 4, 3, 2, 1]


10. Sorted

sorted(iterable[, key][, reverse]): Returns a new sorted list from the elements of an iterable. Optionally, you can specify a key function to specify how to extract a comparison key from each element, and a reverse flag to specify whether to sort in descending order.
Syntax: sorted(iterable[, key][, reverse])

Example:

lst = [5, 3, 1, 4, 2]
sorted_lst = sorted(lst)
print(sorted_lst)
Output:
[1, 2, 3, 4, 5]



11. Sum

sum(iterable[, start]): Returns the sum of the elements of an iterable. Optionally, you can specify a start value to add to the sum.
Syntax: sum(iterable[, start])

Example:

lst = [1, 2, 3, 4, 5]
total = sum(lst)
print(total)
Output:
15


12. Min

min(iterable[, key]): Returns the smallest element of an iterable. Optionally, you can specify a key function to specify how to extract a comparison key from each element.
Syntax: min(iterable[, key])

Example:

lst = [5, 3, 1, 4, 2]
min_element = min(lst)
print(min_element)
Output:
1

13. Max

max(iterable[, key]): Returns the largest element of an iterable. Optionally, you can specify a key function to specify how to extract a comparison key from each element.
Syntax: max(iterable[, key])

Example:

lst = [5, 3, 1, 4, 2]
max_element = max(lst)
print(max_element)
Output:5

14. All

all(iterable): Returns True if all elements of an iterable are truthy (i.e., not False or None), and False otherwise.
Syntax: all(iterable)

Example:

lst = [True, 1, 'a', [1, 2, 3]]
result = all(lst)
print(result)
Output:
True



15. Any

any(iterable): Returns True if any element of an iterable is truthy (i.e., not False or None), and False otherwise.
Syntax: any(iterable)

Example:

lst = [False, 0, '', []]
result = any(lst)
print(result)
Output:
False

16. Enumerate

enumerate(iterable[, start]): Returns an iterator that yields pairs of index and element from an iterable. Optionally, you can specify a start index for the indices.
Syntax: enumerate(iterable[, start])

Example:

lst = ['a', 'b', 'c']
for i, element in enumerate(lst):
print(i, element)
Output:
0 a
1 b
2 c

17. Reversed

reversed(seq): Returns an iterator that yields the elements of a sequence in reverse order.
Syntax: reversed(seq)

Example:

lst = [1, 2, 3, 4, 5]
for element in reversed(lst):
print(element)
Output:
5
4
3
2
1


18. Zip

zip(*iterables): Returns an iterator that aggregates elements from each of the iterables. The iterator stops when the shortest iterable is exhausted.
Syntax: zip(*iterables)

Example:

lst1 = [1, 2, 3]
lst2 = ['a', 'b', 'c']
zipped = zip(lst1, lst2)
print(list(zipped))
Output:
[(1, 'a'), (2, 'b'), (3, 'c')]

19. Filter


filter(function, iterable): Returns an iterator that filters elements from an iterable based on a function that returns a truthy or falsy value for each element.
Syntax: filter(function, iterable)

Example:

def is_even(x):
return x % 2 == 0
lst = [1, 2, 3, 4, 5, 6]
evens = filter(is_even, lst)
print(list(evens))
Output:
[2, 4, 6]


20. Split

split() is a built-in function in Python that is used to split a string into a list of substrings based on a specified delimiter.
Here is the syntax for the split() function:
str.split(sep=None, maxsplit=-1)

  • str is the input string.
  • sep (optional) is the delimiter that is used to split the string. If sep is not specified, any whitespace (space, tab, newline, etc.) is used as the delimiter.
  • maxsplit (optional) is the maximum number of splits to perform. If maxsplit is not specified, or if it is set to -1, then the string is split as many times as possible.

Example of how to use the split() function:


s = 'Hello, world!'
words = s.split()
print(words)
Output:
['Hello,', 'world!']

In this example, the string is split using the default delimiter (whitespace), and the resulting list contains two elements: 'Hello,' and 'world!'.
You can also specify a different delimiter by passing it as the sep argument:

s = 'Hello;world;!'
words = s.split(';')
print(words)
Output:
['Hello', 'world', '!']
In this example, the string is split using the ';' character as the delimiter, and the resulting list contains three elements: 'Hello', 'world', and '!'.

No comments:
Write comments

Please do not enter spam links

Meet US

Services

More Services