Fundamentals 9 min read

Commonly Used Python Functions: A Quick Reference Guide

This article compiles over 100 commonly used Python functions across twelve topics—from basic I/O and data structures to threading and decorators—providing concise code examples to help beginners quickly recall function names and their purposes for more efficient coding.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Commonly Used Python Functions: A Quick Reference Guide

Preface

Beginners often get stuck when coding because they cannot remember which function to use for a given requirement. To address this, I have organized more than 100 frequently used Python functions into twelve sections, from basic input/output to regular expressions, to help developers quickly review and memorize them.

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: Evaluate a score entered by the user and print the corresponding grade.

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. List

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: 4

4. Tuple

Example: Convert a slice of a tuple to a list, modify the list, and convert it back to a tuple.

# Take three elements with indices 1‑3 from the tuple and convert to list
t = (1,2,3,4,5)
print(t[1:4])
l = list(t)
print(l)
# Insert 6 at index 2 in the list
l[2] = 6
print(l)
# Convert the modified list back to a tuple
t = tuple(l)
print(t)
(2, 3, 4)
[1, 2, 3, 4, 5]
[1, 2, 6, 4, 5]
(1, 2, 6, 4, 5)

5. String

Example: Use format() in three different ways.

Method 1 – Positional indexes:

"{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. Dictionary

Example: Retrieve values with dict.get() and provide a default.

d = {"name": "小黑"}
print(d.get("name2", "没有查到"))
print(d.get("name"))
# Output:
# 没有查到
# 小黑

7. Functions

Example: Define a global variable inside a function and access it outside.

def fun1():
    global b
    b = 100
    print(b)
fun1()
print(b)
# Output:
# 100
# 100

8. 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):
        # Work performed by the thread
        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 and use its attributes.

from my_package1 import my_module3
print(my_module3.a)
my_module3.fun4()

10. File Operations

(1) Common file handling modes and methods are illustrated with diagrams (omitted for brevity).

(2) The os module – basic file and directory operations are shown.

11. Decorators / Class Methods

Example: Use @classmethod to access class attributes.

class B:
    age = 10
    def __init__(self, name):
        self.name = name
    @classmethod
    def eat(cls):  # ordinary function
        print(cls.age)
    def sleep(self):
        print(self)

b = B("小贱人")
b.eat()
# Output: 10

12. 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 how each function works in detail, 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 syntax as needed and become more efficient in coding.

If you do not know a function’s name or its use, you will waste more time and effort; searching with a clear purpose is much faster.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonfunctionsprogramming basics
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.