Built-in Functions in Python

Python provides a rich set of built-in functions that perform common operations without requiring external libraries. These functions are categorized as follows:


1️⃣ Input/Output Functions

These functions help with user input and displaying output.

Function Description Example
print() Prints to console print("Hello World")
input() Takes user input name = input("Enter name: ")
format() Formats output print("My name is {}".format("Alice"))

Example:

name = input("Enter your name: ")
print("Hello", name)

2️⃣ Type Conversion Functions

Convert data from one type to another.

Function Description Example
int(x) Converts x to an integer int("10") → 10
float(x) Converts x to a float float("10.5") → 10.5
str(x) Converts x to a string str(100) → "100"
bool(x) Converts x to a boolean bool(0) → False
list(x) Converts x to a list list("abc") → ['a', 'b', 'c']
tuple(x) Converts x to a tuple tuple([1,2,3]) → (1,2,3)
set(x) Converts x to a set set([1,2,2,3]) → {1,2,3}

Example:

num = "100"
converted_num = int(num)
print(type(converted_num))  # Output: <class 'int'>

3️⃣ Mathematical Functions

Perform mathematical operations.

Function Description Example
abs(x) Absolute value abs(-5) → 5
pow(x, y) x raised to the power y pow(2, 3) → 8
round(x, n) Rounds x to n decimal places round(3.14159, 2) → 3.14
max(iterable) Finds the largest value max([1,2,3]) → 3
min(iterable) Finds the smallest value min([1,2,3]) → 1
sum(iterable) Sums values in an iterable sum([1,2,3]) → 6

Example:

print(abs(-10))        # Output: 10
print(pow(3, 2))       # Output: 9
print(round(3.14159, 2))  # Output: 3.14

4️⃣ Sequence and Collection Functions

Functions for working with lists, tuples, and sets.

Function Description Example
len(x) Returns length of sequence len("hello") → 5
sorted(x) Returns sorted sequence sorted([3,1,2]) → [1,2,3]
reversed(x) Returns reversed sequence list(reversed([1,2,3])) → [3,2,1]
enumerate(x) Returns index-value pairs list(enumerate("abc")) → [(0,'a'),(1,'b')]
zip(a, b) Combines two sequences list(zip([1,2],['a','b'])) → [(1, 'a'), (2, 'b')]

Example:

names = ["Alice", "Bob", "Charlie"]
for index, name in enumerate(names):
    print(index, name)

5️⃣ Logical and Comparison Functions

Used for checking conditions.

Function Description Example
all(iterable) Returns True if all elements are True all([True, True, False]) → False
any(iterable) Returns True if any element is True any([False, False, True]) → True
bool(x) Converts value to boolean bool([]) → False

Example:

print(all([1, 2, 3, 0]))  # Output: False (0 is False)
print(any([0, "", None, 5]))  # Output: True (5 is True)

6️⃣ File Handling Functions

Used for working with files.

Function Description Example
open(file, mode) Opens a file open("file.txt", "r")
read() Reads file content file.read()
write(str) Writes to a file file.write("Hello")
close() Closes the file file.close()

Example:

file = open("test.txt", "w")
file.write("Hello, Python!")
file.close()

7️⃣ Object and Type Functions

Functions for checking data types.

Function Description Example
type(x) Returns type of x type(10) → <class 'int'>
isinstance(x, type) Checks if x is of type isinstance(10, int) → True
id(x) Returns unique ID of x id(10)

Example:

num = 100
print(type(num))  # Output: <class 'int'>
print(isinstance(num, int))  # Output: True

8️⃣ Memory and Performance Functions

Used for memory-efficient programming.

Function Description Example
hash(x) Returns hash value hash("hello")
vars(x) Returns dictionary of object’s attributes vars(obj)
dir(x) Lists attributes of x dir(str)

Example:

print(hash("Python"))  # Unique hash value

🔹 Summary Table

Category Common Functions
Input/Output print(), input()
Type Conversion int(), float(), str(), list()
Math abs(), pow(), sum(), max()
Collections len(), sorted(), zip(), enumerate()
Logic all(), any(), bool()
File Handling open(), read(), write(), close()
Object Handling type(), isinstance(), id()

💡 Final Thoughts

Python’s built-in functions simplify programming by providing efficient, optimized, and easy-to-use utilities. Would you like real-world use cases for any of these functions? 🚀


Leave a Reply

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