In this article, we can check some of the built-in functions and methods commonly used with examples in python. We can also check out the difference between them.
Built-in functions and methods
Imagine, you have some lines of code which need to be repeated at various parts of your code, you will feel comfortable if we write those lines in one place and give a name to them. Now, whenever we need to write those lines we can just call them by their name. Exactly, the same process is called functions.
Now, in python, some code is already written for you and you can just use those functions by calling them. This is what we do in built-in functions and methods.
There is a total of 70 built-in functions available in python and numerous built-in methods available. Let us see some of the commonly used functions and methods for example.
Commonly used built-in function with numbers
|abs(x)||Returns the absolute value of a number.|
|bin(x)||Convert an integer number to a binary string prefixed with “0b”.||print(bin(5))#0b101|
|divmod(a,b)||Take two (non-complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using integer division.||print(divmod(5,3))#(1,2)|
|hex(x)||Convert an integer number to a lower-case hexa-decimal string prefixed with “0x”.||print(hex(60))#0x3c|
|max(iterable)||Returns the largest element in the iterable||print(max(2,5,7,5,52,23))#52|
|min(iterable)||Returns the smallest element in the iterable||print(min(2,5,7,5,52,23))#2|
|oct(x)||Convert an integer number to an octal string prefixed with “0o”.||print(oct(60))#0o74|
|pow(base, exp[, mod])||Return base to the power exp; if mod is present, return base to the power exp, modulo mod. The two-argument form pow(base, exp) is equivalent to using the power operator: base**exp.||print(pow(5,2,4))#1print(pow(5,2))#25|
|round(number[, ndigits])||Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.||print(round(5.656,2))#5.66print(round(5.656))#6|
|sum(iterable, /, start=0)||Sums start and the items of an iterable from left to right return the total. The iterable’s items usually are numbers, and the start value is not allowed to be a string.||print(sum([1,2,5,6,7]))#21|
Built-in function for type conversion
|complex([real[, imag]])||Return a complex number with the value real + imag*1j or convert a string or number to a complex number.|
|dict()||Creates a new empty dictionary|
|float([x])||Return a floating point number constructed from a number or string x.||print(float(2))#2.0|
|int(x)||Return an integer object constructed from a number or string x, or return 0 if no arguments are given.||print(int(2.55))#2|
|list()||Creates an empty list||print(list())#|
|set()||Creates an empty set||print(set())#set()|
|str(x)||Return a str version of object.||print(type(str(55)))#<class ‘str’>|
|tuple(iterable)||Converts the iterable to a tuple||print(tuple([1,2,3,4,5]))#(1, 2, 3, 4, 5)|
Important Built-in functions
|dir([object])||Without arguments, return the list of names in the current local scope.||print(dir())|
|id(object)||Return the “identity” of an object. This is an integer that is guaranteed to be unique and constant for this object during its lifetime.||print(id(5))#139635281848384|
|input([prompt])||If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.||print(input(“Enter your name:”))#Enter your name:ss|
|len(s)||Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).||print(len(“Happy”))#5|
|print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)||Print objects to the text stream file, separated by sep and followed by end. sep, end, file, and flush, if present, must be given as keyword arguments.||print(“Hi”) #Hi|
|type(object)||With one argument, return the type of an object.||print(type(3.7))#<class ‘float’>|
Built-in methods for string
|upper()||Converts the given string to Upper case.||print(“hello”.upper())#HELLO|
|lower()||Converts the given string to lower case.||print(“hEllo”.lower())#hello|
|title()||Converts the first character of each word to upper case.||print(“hEllo world”.title())#Hello World|
|swapcase()||Converts lower case to upper and upper to lower.||print(“HEllo WorLD”.swapcase())#heLLO wORld|
|strip()||Returns the trimmed version of the string.||print(” Heloo “.strip())#Heloo|
|startswith()||Returns true if the string starts with the specified value.||print(“Hello”.startswith(“H”))#True|
|split()||Splits the string at the specified separator, and returns a list||print(“HEllo WorLD”.split(” “))#[‘HEllo’, ‘WorLD’]|
|isalnum()||Returns True if all characters in the string are alphanumeric||print(“Hello_world”.isalnum())#False|
|isalpha()||Returns True if all characters in the string are in the alphabet.||print(“Heloo world”.isalpha())#False|
|isdigit()||Returns True if all characters in the string are digits||print(“123”.isdigit())#True|
|isnumeric()||Returns True if all characters in the string are numbers||print(“123”.isnumeric())#True|
|islower()||Returns True if all characters in the string are lower case.||print(“hello”.islower())#True|
|isupper()||Returns True if all characters in the string are upper case.||print(“hello”.isupper())#False|
Difference between built-in functions and Methods
- Methods are defined inside the class, whereas functions no need to be inside the class.
- We cannot call a method just by its name, whereas we can call a function just by its name.
- In simple words we always write object_name.method_name(optional parameters) for calling a method, whereas functions calls are written as function_name(optional parameters).
Thus, we have discussed all the frequently used built-in functions with explanations and examples. If you want to use the built-in function click on the following GitHub, Built-in git hub.