50 Essential Python Built-in Functions and Their Usage
This article introduces fifty essential Python built-in functions, covering type conversion, numeric operations, sequence handling, I/O, string manipulation, and utility functions, each with concise explanations and code examples to help readers quickly understand and apply these fundamental tools in their programs.
Python’s built‑in functions form the foundation of everyday programming. This guide lists fifty of the most useful built‑in functions, grouped by purpose, and provides short explanations together with runnable code examples.
1. Data Type Conversion Functions
int(), float(), str(), bool() convert values between common types.
num = int(3.14)
print(num) # Output: 3
num = float(3)
print(num) # Output: 3.0
string = str(123)
print(string, type(string)) # Output: 123 <class 'str'>
print(bool(0)) # Output: False
print(bool([1])) # Output: True2. Numeric Operation Functions
abs(), round(), sum(), min(), max(), pow() perform basic arithmetic and aggregation.
print(abs(-5)) # 5
print(round(3.14159, 2)) # 3.14
print(sum([1, 2, 3])) # 6
print(min([5, 3, 7])) # 3
print(max([10, 15, 8])) # 15
print(pow(2, 3)) # 83. Sequence Operation Functions
len(), sorted(), reversed(), enumerate(), zip() help manipulate iterable objects.
string = "hello"
print(len(string)) # 5
lst = [3, 1, 2]
print(sorted(lst)) # [1, 2, 3]
for x in reversed([1, 2, 3]):
print(x) # 3 2 1
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(index, fruit) # 0 apple ...
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
for pair in zip(list1, list2):
print(pair) # (1, 'a') (2, 'b') (3, 'c')4. Input/Output Functions
print() displays output; input() reads user input.
print("Hello", "World") # Hello World
user_input = input("请输入内容: ")
print(user_input)5. File Operation Functions
open(), file.read(), file.write() manage file I/O.
file = open('test.txt', 'w')
file.write("Hello, file!")
file.close()
file = open('test.txt', 'r')
content = file.read()
print(content)
file.close()6. String Handling Functions
capitalize(), upper(), lower(), strip(), split(), join() process text.
s = "python is fun"
print(s.capitalize()) # Python is fun
print("hello".upper()) # HELLO
print("WORLD".lower()) # world
s = " python "
print(s.strip()) # python
csv = "apple,banana,cherry"
print(csv.split(',')) # ['apple', 'banana', 'cherry']
lst = ['a', 'b', 'c']
print(''.join(lst)) # abc7. Function‑Related Keywords
def defines a function; lambda creates anonymous functions; help() shows documentation.
def add(a, b):
return a + b
add = lambda x, y: x + y
print(add(3, 4)) # 7
help(len)8. Miscellaneous Utility Functions
range(), isinstance(), filter(), map(), all(), any(), id(), type(), dir(), globals(), locals(), eval(), exec(), format(), hash(), memoryview(), next() provide assorted capabilities.
for i in range(5):
print(i) # 0 1 2 3 4
print(isinstance(5, int)) # True
lst = [1, 2, 3, 4, 5]
print(list(filter(lambda x: x % 2 == 0, lst))) # [2, 4]
print(list(map(lambda x: x * 2, [1, 2, 3]))) # [2, 4, 6]
print(all([True, True, False])) # False
print(any([False, False, True])) # True
obj = 10
print(id(obj))
print(type('hello'))
print(dir([1,2,3]))
print(globals())
print(locals())
result = eval('1 + 2 * 3')
print(result) # 7
exec("print('Hello, exec!')")
name = "Alice"
age = 25
print("我的名字是{},今年{}岁。".format(name, age))
print(hash('hello'))
byte_data = bytes([1,2,3])
mv = memoryview(byte_data)
print(mv)
it = iter([1,2,3])
print(next(it)) # 1Mastering these fifty built‑in functions will greatly improve coding efficiency and enable you to write cleaner, more Pythonic code.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.