Commonly Used Python Functions: A Quick Reference Guide
This article presents a concise, organized collection of over 100 commonly used Python functions across twelve categories—including basic I/O, control flow, data structures, modules, and file operations—providing code examples and explanations to help beginners quickly memorize and apply them.
Beginners often get stuck when coding because they cannot recall which built‑in function to use for a given task. To address this, a curated list of more than 100 frequently used Python functions is presented, covering everything from basic input/output to regular expressions, allowing quick review and deeper learning through practice.
1. Basic Functions
Example: Convert a floating‑point number to a string and print its type.
f = 30.5
ff = str(f)
print(type(ff))
# Output: class 'str'2. Control Flow
Example: Determine a grade based on user‑entered score.
s = int(input("请输入分数:"))
if 80 >= s >= 60:
print("及格")
elif 80 < s <= 90:
print("优秀")
elif 90 < s <= 100:
print("非常优秀")
else:
print("不及格")
if s > 50:
print("你的分数在60分左右")
else:
print("你的分数低于50分")3. Lists
Example: Find the index of the number 6 in a list.
l = [1,2,2,3,6,4,5,6,8,9,78,564,456]
n = l.index(6, 0, 9)
print(n)
# Output: 44. Tuples
Example: Modify a tuple by converting it to a list.
# Take three elements with indices 1~4 and convert to list
t = (1,2,3,4,5)
print(t[1:4])
l = list(t)
print(l)
# Insert 6 at index 2
l[2] = 6
print(l)
# Convert back to tuple
t = tuple(l)
print(t)
# Output:
# (2, 3, 4)
# [1, 2, 3, 4, 5]
# [1, 2, 6, 4, 5]
# (1, 2, 6, 4, 5)5. Strings
Example: Three ways to use format() for string formatting.
Method 1 – Positional indices:
"{0} 嘿嘿".format("Python")
a = 100
s = "{0}{1}{2} 嘿嘿"
s2 = s.format(a, "JAVA", "C++")
print(s2)
# Output: 100JAVAC++ 嘿嘿Method 2 – Empty braces:
a = 100
s = "{}{}{} 嘿嘿"
s2 = s.format(a, "JAVA", "C++", "C# ")
print(s2)
# Output: 100JAVAC++ 嘿嘿Method 3 – Named placeholders:
s = "{a}{b}{c} 嘿嘿"
s2 = s.format(b="JAVA", a="C++", c="C# ")
print(s2)
# Output: C++JAVAC# 嘿嘿6. Dictionaries
Example: Retrieve values from a dictionary with a default.
d = {"name": "小黑"}
print(d.get("name2", "没有查到"))
print(d.get("name"))
# Output:
# 没有查到
# 小黑7. Functions
Custom functions are demonstrated, showing how a global variable defined inside a function can be accessed outside.
def fun1():
global b
b = 100
print(b)
fun1()
print(b)
# Output:
# 100
# 1008. Processes and Threads
Example: Create threads by subclassing threading.Thread.
# Multi‑thread creation
class MyThread(threading.Thread):
def __init__(self, name):
super().__init__()
self.name = name
def run(self):
# Thread work
for i in range(5):
print(self.name)
time.sleep(0.2)
# Instantiate threads
t1 = MyThread("凉凉")
t2 = MyThread("最亲的人")
t1.start()
t2.start()9. Modules and Packages
Example: Import a module from a package.
from my_package1 import my_module3
print(my_module3.a)
my_module3.fun4()10. File Operations
(1) Regular file handling
Common file modes and file object attributes/methods are illustrated with diagrams.
11. Decorators
Example: Using @classmethod in a class.
class B:
age = 10
def __init__(self, name):
self.name = name
@classmethod
def eat(cls): # class method
print(cls.age)
def sleep(self):
print(self)
b = B("小贱人")
b.eat()
# Output: 1012. Regular Expressions
Example: Split a string using re.split().
import re
s = "abcabcacc"
l = re.split("b", s)
print(l)
# Output: ['a', 'ca', 'cacc']Conclusion
The purpose of this article is not to teach every detail of each function, but to help readers quickly memorize the names and purposes of frequently used Python functions. Once you know what a function does, you can look up its exact usage as needed.
If you do not even know the function name, you will waste much more time searching for it.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
