Python Built-in Functions

A built-in function is a function that is always available in Python. You can call these functions directly without needing to import any library.

Categories of Built-in Functions

  • Type Conversion Functions: Convert one data type to another.
  • Mathematical Functions: Perform basic mathematical operations.
  • String Functions: Manipulate strings.
  • Input/Output Functions: Handle user input and display output.
  • Utility Functions: Miscellaneous helpful functions.

Examples of Python Built-in Functions

1. Type Conversion Functions

These functions help convert values from one type to another.

# Type conversion examples
x = int("10")  # Converts string "10" to integer
y = float("3.14")  # Converts string "3.14" to float
z = str(123)  # Converts integer 123 to string

print(x, y, z)  # Output: 10 3.14 '123'

Try It Now

2. Mathematical Functions

Python provides basic math functions like abs() and pow().

# Mathematical functions
a = abs(-5)  # Returns the absolute value: 5
b = pow(2, 3)  # Returns 2 raised to the power of 3: 8

print(a, b)  # Output: 5 8

Try It Now

3. String Functions

These functions allow you to manipulate strings easily.

# String functions
s = "Hello, Python!"
length = len(s)  # Returns the length of the string
upper_s = s.upper()  # Converts the string to uppercase

print(length, upper_s)  # Output: 14 'HELLO, PYTHON!'

Try It Now

4. Input/Output Functions

Functions like input() and print() are essential for interacting with users.

# Input/Output example
name = input("What is your name? ")  # Takes user input
print(f"Hello, {name}!")  # Greets the user

Try It Now

5. Utility Functions

Some utility functions like type() and id() help in debugging and understanding the state of your program.

# Utility functions
x = 42
print(type(x))  # Returns the type of x: <class 'int'>
print(id(x))  # Returns the memory address of x

Try It Now

Why Use Built-in Functions?

Using built-in functions has several advantages:

  • They are optimized for performance.
  • No need to reinvent the wheel.
  • They make your code concise and readable.

Conclusion

Python’s built-in functions are powerful and easy to use. Mastering these functions will make your programming experience much smoother and more productive.