Python built-in functions are pre-defined functions that come bundled with Python and can be used directly in a Python program without importing external libraries. They provide essential and commonly used functionality for various tasks, such as data type conversion, mathematical operations, string manipulation, file operations, etc. Here are the most commonly used 15 python built-in functions!
This article was published as a part of the Data Science Blogathon.
The built-in Python Functions which we are going to discuss in this article are as follows:
Function | Example | Description |
---|---|---|
print() | print("Hello, World!") | Prints output to the console |
len() | len("Python") | Returns the length of a string |
range() | range(0, 10) | Generates a range of integers from 0 to 9 |
str() | str(123) | Converts the integer value to a string |
int() | int("123") | Converts the string value to an integer |
float() | float(123) | Converts the integer value to a floating-point number |
max() | max(1, 2, 3, 4, 5) | Returns the maximum value in the list of numbers |
min() | min(1, 2, 3, 4, 5) | Returns the minimum value in the list of numbers |
sorted() | sorted([3, 2, 1]) | Sorts the list in ascending order |
type() | type("Python") | Returns the type of the object as “str” |
input() | name = input("Enter your name: ") | Gets user input from the console and assigns it to a variable |
sum() | sum([1, 2, 3, 4, 5]) | Returns the sum of all elements in the list |
abs() | abs(-10) | Returns the absolute value of the number |
round() | round(3.14159, 2) | Rounds the number to 2 decimal places |
chr() | chr(65) | Returns the character corresponding to the ASCII code 65, which is “A” |
Now, let’s discuss each of the above functions one-by-one with some examples:
The print() function prints the specified message to the screen or another standard output device.
The message that wants to print can be a string or any other object. This function converts the object into a string before written to the screen.
print("Analytics Vidhya is the Largest Data Science Community over whole world")
Output:
Analytics Vidhya is the Largest Data Science Community over whole world
print("Analytics Vidhya is the Largest Data Science Community over whole world")
print("Analytics Vidhya", "Chirag Goyal", "Data Scientist", "Machine Learning")
Output:
Analytics Vidhya Chirag Goyal Data Scientist Machine Learning
The type() function returns the type of the specified object.
list_of_fruits = ('apple', 'banana', 'cherry', 'mango') print(type(list_of_fruits))
Output:
<class 'tuple'>
variable_1 = "Analytics Vidhya" variable_2 = 105 print(type(variable_1)) print(type(variable_2))
Output:
<class 'str'> <class 'int'>
The input() function allows taking the input from the user.
a = input('Enter your name:') print('Hello, ' + a + ' to Analytics Vidhya')
Output:
Enter your name:Chirag Goyal Hello, Chirag Goyal to Analytics Vidhya
The abs() function returns the absolute value of the specified number.
negative_number = -676 print(abs(negative_number))
Output:
676
complex_number = 3+5j print(abs(complex_number))
Output:
5.830951894845301
The absolute value of a complex number is defined in the following way:
Image Source: Link
The pow() function returns the calculated value of x to the power of y i.e, xy.
If a third parameter is present in this function, then it returns x to the power of y, modulus z.
x = pow(3, 4) print(x)
Output:
81
x = pow(3, 4, 5) print(x)
Output:
1
The dir() function returns all the properties and methods of the specified object, without the values.
This function even returns built-in properties which are the default for all objects.
class Person: name = "Chirag Goyal" age = 19 country = "India" education = "IIT Jodhpur" print(dir(Person))
Output:
The sorted() function returns a sorted list of the specified iterable object.
You can specify the order to be either ascending or descending. In this function, Strings are sorted alphabetically, and numbers are sorted numerically.
NOTE: If a list contains BOTH string values AND numeric values, then we cannot sort it.
tuple = ("h", "b", "a", "c", "f", "d", "e", "g") print(sorted(tuple))
Output:
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
tuple = ("h", "b", "a", "c", "f", "d", "e", "g") print(sorted(tuple, reverse=True))
Output:
['h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
The max() function returns the item with the maximum value or the item with the maximum value in an iterable.
If the values this function takes are strings, then it is done using an alphabetical comparison.
names_tuple = ('Chirag', 'Kshitiz', 'Dinesh', 'Kartik') print(max(names_tuple))
Output:
Kshitiz
number_tuple = (2785, 545, -987, 1239, 453) print(max(number_tuple))
Output:
2785
The round() function returns a floating-point number that is a rounded version of the specified number, with the specified number of decimals.
The default number of decimals is 0, meaning that the function will return the nearest integer.
nearest_number = round(87.76432) print(nearest_number)
Output:
88
nearest_number = round(-87.76432) print(nearest_number)
Output:
-88
nearest_number = round(87.46432) print(nearest_number)
Output:
87
The divmod() function returns a tuple containing the quotient and the remainder when the first argument i.e, the dividend is divided by the second argument i.e, the divisor.
x = divmod(7, 3) print(x)
Output:
(2, 1)
x = divmod(72, 6) print(x)
Output:
(12, 0)
The id() function returns a unique id for the specified object. Note that all the objects in Python have their own unique id.
The id is assigned to the object when it is created.
The id is the object’s memory address and will be different for each time you run the program. (except for some object that has a constant unique id, like integers from -5 to 256)
names_tuple = ('Chirag', 'Kshitiz', 'Dinesh', 'Kartik') print(id(names_tuple))
Output:
140727154106096
string = "Analytics Vidhya is the Largest Data Science Community" print(id(string))
Output:
140727154110000
The ord() function returns the number representing the Unicode code of a specified character.
x = ord("h") print(x)
Output:
104
x = ord("H") print(x)
Output:
72
The len() function returns the count of items present in a specified object.
When the object is a string, then the len() function returns the number of characters present in that string.
fruit_list = ["apple", "banana", "cherry", "mango", "pear"] print(len(fruit_list))
Output:
5
string = "Analytics Vidhya is the Largest Data Science Community" print(len(string))
Output:
54
The sum() function returns a number, the sum of all items in an iterable.
If you are a beginner, I think that you don’t have a good understanding of what Iterables and Iterators are. To learn these concepts, you can refer to the link.
a = (1, 2, 3, 4, 5) print(sum(a, 7))
Output:
22
list = [1, 2, 3, 4, 5] print(sum(list))
Output:
15
The help() function is used to display the documentation of modules, functions, classes, keywords, etc.
If we don’t give an argument to the help function, then the interactive help utility starts up on the console.
help(print)
Output:
help(sum)
Output:
A. In-built Python functions are pre-defined functions available for use without importing any external libraries or modules. These functions provide basic functionality and operations used in programming.
A. An example of a built-in function in Python is the print() function, which displays output on the console.
A. Python has several built-in functions that provide functionality in different areas, such as data type conversion, mathematical operations, string manipulation, file operations, and more. Some examples of built-in functions in Python are len(), str(), int(), float(), range(), max(), min(), sum(), open(), sorted(), and type().
Very interesting read!
This is a very interesting and insightful read!
how to only get alphabet as a input in python program?? please guide