Python provides a rich set of built-in functions that are always available for use. These functions are part of the Python Standard Library and perform a variety of tasks, such as data type conversion, mathematical operations, input/output operations, and more. Here is an overview of some of the most commonly used built-in functions in Python:
Common Built-in Functions
Data Type Conversion Functions
int()
: Converts a value to an integer.
num = int("10") # Output: 10
float()
: Converts a value to a floating-point number.num = float("10.5") # Output: 10.5
str()
: Converts a value to a string.text = str(10) # Output: '10'
bool()
: Converts a value to a boolean.flag = bool(1) # Output: True
list()
: Converts an iterable (e.g., tuple, string) to a list.my_list = list("hello") # Output: ['h', 'e', 'l', 'l', 'o']
tuple()
: Converts an iterable to a tuple.my_tuple = tuple([1, 2, 3]) # Output: (1, 2, 3)
dict()
: Creates a dictionary.my_dict = dict([('a', 1), ('b', 2)]) # Output: {'a': 1, 'b': 2}
Mathematical Functions
abs()
: Returns the absolute value of a number.
result = abs(-5) # Output: 5
print(result)
round()
: Rounds a number to a specified number of decimal places.result = round(5.678, 2) # Output: 5.68
max()
: Returns the largest item in an iterable or the largest of two or more arguments.result = max(1, 2, 3) # Output: 3
min()
: Returns the smallest item in an iterable or the smallest of two or more arguments.result = min(1, 2, 3) # Output: 1
sum()
: Sums the items of an iterable.result = sum([1, 2, 3]) # Output: 6
Sequence Functions
len()
: Returns the length of an object.
result = len("hello") # Output: 5
range()
: Generates a sequence of numbers.numbers = list(range(1, 5)) # Output: [1, 2, 3, 4]
enumerate()
: Adds a counter to an iterable and returns it as an enumerate object.for index, value in enumerate(['a', 'b', 'c']):
print(index, value)
# Output:
# 0 a
# 1 b
# 2 c
zip()
: Aggregates elements from two or more iterables.names = ['Alice', 'Bob']
scores = [85, 90]
combined = list(zip(names, scores)) # Output: [('Alice', 85), ('Bob', 90)]
Input/Output Functions
print()
: Prints the specified message to the console or other standard output device.
print("Hello, world!") # Output: Hello, world!
input()
: Reads a line from input, converts it to a string.name = input("Enter your name: ")
print(f"Hello, {name}!")
Object and Class Functions
type()
: Returns the type of an objectresult = type(123) # Output: <class 'int'>
isinstance()
: Checks if an object is an instance of a class or a tuple of classes.result = isinstance(123, int) # Output: True
dir()
: Returns a list of the specified object's attributes and methods.attributes = dir([]) # Output: ['__add__', '__class__', ..., 'append', 'clear', ...]
id()
: Returns the identity of an object.obj = "hello"
obj_id = id(obj) # Output: 140248162356656 (the actual number will vary)
Other Useful Functions
open()
: Opens a file and returns a file object.
file = open("example.txt", "r")
content = file.read()
file.close()
sorted()
: Returns a new sorted list from the elements of any iterable.numbers = [3, 1, 4, 1, 5]
sorted_numbers = sorted(numbers) # Output: [1, 1, 3, 4, 5]
reversed()
: Returns a reverse iterator.reversed_list = list(reversed([1, 2, 3])) # Output: [3, 2, 1]
any()
: Returns True
if any element of the iterable is true.result = any([0, 1, 2]) # Output: True
all()
: Returns True
if all elements of the iterable are true.result = all([1, 2, 3]) # Output: True
eval()
: Evaluates a Python expression from a string-based input.result = eval("2 + 2") # Output: 4
exec()
: Executes a dynamic Python code block.code = "for i in range(3): print(i)"
exec(code)
# Output:
# 0
# 1
# 2
Python's built-in functions provide a powerful toolkit for performing a wide range of tasks efficiently. Familiarity with these functions can greatly enhance your ability to write concise and readable Python code. For a full list of Python's built-in functions, you can refer to the official Python documentation.
Comments
Post a Comment