Programs

Python In-Built Function in Python [With Syntax and Examples]

Built-in functions in Python are the functions that come pre-defined. Just a line of code can trigger these functions, and the programmer needs to pass relevant parameters to get the output from these functions. Python allows programmers to create custom functions and provides many in-built functions that can be used to make scripting faster and more optimized.

Since these functions are in-built and pre-coded, they follow a specific syntax that must be kept in mind while working with them. The table below lists all the built-in functions available in Python.

This article will talk about 8 of these in-built functions that you must know how to work with. Please refer to the official guide if you wish to dive deeper into other functions.

Important Python In-Built Functions to Know

Let’s look at some critical in-built functions available in the Python programming language:

1. hash()

This method returns the hash value of the given object. Hash values can be understood as integers that can be used to compare dictionary keys during a lookup. The hash() method takes immutable objects as inputs and returns the respective hash values.

Syntax: hash(input_object)

Example: 

str = ”Hello World”

a = 13.8

tp = (‘a’, ‘b’, ‘c’)

print(“Hash for string:”, hash(str))

print(“Hash for float:”, hash(a))

print(“Hash for tuple:”, hash(tp))

Output:

hash value for a string:  5497742200142074869

hash value for a decimal:  1152921504606846988

hash value for a tuple:  -5693404743884429734

Check out our data science training to upskill yourself

Check our US - Data Science Programs

2. map()

The map() function allows programmers to execute specific functions for each item in an iterable. As a result, this function takes two inputs – both the required function and the iterable.

Syntax: map(function, iterable)

Example:

def square(n):

    return n*n

num = [1,2,3,4]

res = map(square, num)

print(“The result is: “ list(res))

Output:

The result is:  [1, 4, 9, 16]

As you can see from the output, the map() function mapped each iterable from the num list to the square function and gave the corresponding output.

3. zip()

The zip() method is used to map similar indices of passed iterators. The purpose of this is to take two separate iterators and use them in a single entity. So, if we pass two different iterations in a zip() method, where both iterators contain an equal number of elements, the zip() function returns the iterator of a tuple, where each tuple contains a map of the same index elements from the given iterators.

Syntax: zip(*iterators)

Example: 

num = [1,2,3]

letters = [‘one’, ‘two’, ‘three’]

res = zip(num, letters)

print (“The result is:” set(res))

Output:

The result is:  {(3, ‘Three’), (1, ‘One’), (2, ‘Two’)}

Read our Popular US - Data Science Articles

4. eval()

This function lets you evaluate Python expressions from compiled-code-based or string-based inputs. Simply put, the eval() method takes a string input, evaluates it as a python expression, and returns the output as an integer.

Syntax : eval(string)

Example: 

res1 = eval (‘10+15’)

res2 = eval(‘3*8’)

print(res1, res2)

Output:

25, 24

5. split()

The split() method comes in-built with the String modules in Python. This function splits a given string into smaller substrings based on various delimiters or separators. The function takes two parameters – separator and maxsplit. The value of separator is by default taken as whitespace, and that of maxsplit is -1.

Syntax: str.split(separator, maxsplit)

Example: 

str1 = “Welcome to upGrad”

str2 = “upGrad#for#Data#Science”

res1 = str1.split()

res2 = str2.split(“#”)

Output:

[‘Welcome’, ‘to’, ‘upGrad’]

[‘upGrad’, ‘for’, ‘Data’, ‘Science’]

6. ord()

This method returns the Unicode point of a given character. This function takes a character as input and returns integer numbers representing the input character’s Unicode point.

Syntax: ord(character)

Example: 

x = ord(‘a’)

y = ord(‘$)

z = ord(‘ ‘)

print (x, y, z)

Output:

97, 36, 32

7. dir()

This is a powerful in-built method available in Python that returns a list of all attributes of the specified object. It returns all the valid properties – including those properties that are built-in for default objects.

Syntax: dir(object)

Example: 

class Stud: 

    name = “Ron”

    age = 15

    rollNo = 32

print (dir(stud)

Output:

[‘__class__’, ‘__delattr__’, ‘__dict__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__le__’, ‘__lt__’, ‘__module__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘__weakref__’, ‘age’, ‘name’, ‘rollNo’]

8. pow()

The pow() function takes two integers as inputs and raises the first integer to the power of the second integer. So, pow(x,y) will return x raised to the power of y as the output.

Syntax: pow(x,y)

Example: 

x = pow(2, 4)

y = pow(3, 2)

print(x, y)

Output:

16, 9

In Conclusion

In this article, we went over some key Python in-built functions. Thus, now you have a better idea of how to work around the syntax and invoke different in-built functions. However, since the world of Python is exceptionally vast, you must familiarise yourself with its myriad concepts, tools, functions, and libraries to master Python programming. We recommend exploring all Python in-built functions and experimenting with them in real-world programming.

Also, if you are looking for professional and expert support to guide you in this journey,  check out our Professional Certificate Program in Data Science and Business Analytics. The course is designed to help you start from the very basics and develop enough concepts to work on standalone projects and applications of Data Science.

1. How many in-built functions are available in Python?

Python has all-in-all 68 in-built functions ready for use.

2. How to use in-built functions in Python?

To use in-built functions, you will need to know the syntax and the parameters it takes. Then, simply triggering the function using a one-line command will get the function activated, and you will get the desired output.

3. Are there only in-built functions in Python?

No, Python allows for both in-built and programmer-defined functions, as per your requirements.

Want to share this article?

Leave a comment

Your email address will not be published. Required fields are marked *

Our Best Data Science Courses

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×