Fundamentals 11 min read

Common Python Functions and Practical Code Examples

This article presents a curated collection of over 100 frequently used Python functions across twelve categories, providing concise explanations and ready-to-run code snippets to help beginners quickly memorize and apply essential built‑in operations, from basic I/O to decorators and regular expressions.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Common Python Functions and Practical Code Examples

Beginners often get stuck when writing code because they cannot recall which built‑in functions to use; this article compiles a list of common Python functions to help them memorize and apply these tools efficiently.

1. Basic Functions

Example: Convert a floating‑point number to a string and print its type.

<span>f = 30.5<br/>ff = str(f)<br/>print(type(ff))<br/><br/># Output: class 'str'</span>

2. Flow Control

Example: Determine a grade based on a user‑entered score.

<span>s = int(input("请输入分数:"))<br/>if 80 >= s >= 60:<br/>    print("及格")<br/>elif 80 < s <= 90:<br/>    print("优秀")<br/>elif 90 < s <= 100:<br/>    print("非常优秀")<br/>else:<br/>    print("不及格")<br/>    if s > 50:<br/>        print("你的分数在60分左右")<br/>    else:<br/>        print("你的分数低于50分")</span>

3. List Operations

Example: Find the index of the number 6 in a list.

<span>l = [1,2,2,3,6,4,5,6,8,9,78,564,456]<br/>n = l.index(6, 0, 9)<br/>print(n)<br/><br/># Output: 4</span>

4. Tuple Operations

Example: Slice a tuple, convert it to a list, modify the list, and convert back.

<span># Take elements with indices 1‑3 from the tuple and convert to list<br/>t = (1,2,3,4,5)<br/>print(t[1:4])<br/>l = list(t)<br/>print(l)<br/># Insert 6 at index 2 in the list<br/>l[2] = 6<br/>print(l)<br/># Convert back to tuple<br/>t = tuple(l)<br/>print(t)</span>
<span># Output:<br/>(2, 3, 4)<br/>[1, 2, 3, 4, 5]<br/>[1, 2, 6, 4, 5]<br/>(1, 2, 6, 4, 5)</span>

5. String Formatting

Three ways to use format():

<span>"{0} 嘿嘿".format("Python")<br/>a = 100<br/>s = "{0}{1}{2} 嘿嘿"<br/>s2 = s.format(a, "JAVA", "C++")<br/>print(s2)<br/># Output: 100JAVAC++ 嘿嘿</span>
<span>a = 100<br/>s = "{}{}{} 嘿嘿"<br/>s2 = s.format(a, "JAVA", "C++", "C# ")<br/>print(s2)<br/># Output: 100JAVAC++ 嘿嘿</span>
<span>s = "{a}{b}{c} 嘿嘿"<br/>s2 = s.format(b="JAVA", a="C++", c="C# ")<br/>print(s2)<br/># Output: C++JAVAC#  嘿嘿</span>

6. Dictionaries

Example: Retrieve values with dict.get().

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

7. Functions

Example: Define a function that creates a global variable.

<span>def fun1():<br/>    global b<br/>    b = 100<br/>    print(b)<br/>fun1()<br/>print(b)</span>
<span># Output:<br/>100<br/>100</span>

8. Processes and Threads

Example: Subclass threading.Thread and start two threads.

<span># Multi‑thread creation<br/>class MyThread(threading.Thread):<br/>    def __init__(self, name):<br/>        super().__init__()<br/>        self.name = name<br/>    def run(self):<br/>        # Thread work<br/>        for i in range(5):<br/>            print(self.name)<br/>            time.sleep(0.2)<br/><br/>t1 = MyThread("凉凉")<br/>t2 = MyThread("最亲的人")<br/>t1.start()<br/>t2.start()</span>

9. Modules and Packages

Example: Import a module from a package and use its attributes.

<span>from my_package1 import my_module3<br/>print(my_module3.a)<br/>my_module3.fun4()</span>

10. File Operations

(1) Regular file handling

Explanation of common file modes, attributes, and methods (illustrated with images).

(2) os module

Illustrates file‑system related functions (images omitted for brevity).

11. Decorators

Example: Using @classmethod inside a class.

<span>class B:<br/>    age = 10<br/>    def __init__(self, name):<br/>        self.name = name<br/>    @classmethod<br/>    def eat(cls):  # class method<br/>        print(cls.age)<br/><br/>    def sleep(self):<br/>        print(self)<br/><br/>b = B("小贱人")<br/>b.eat()</span>

# Output: 10

12. Regular Expressions

Example: Split a string using re.split().

<span>import re<br/>s = "abcabcacc"<br/>l = re.split("b", s)<br/>print(l)<br/><br/># Output: ['a', 'ca', 'cacc']</span>

Conclusion

The purpose of this article is not to teach every function in depth but to help readers quickly memorize the names and purposes of frequently used Python functions; detailed usage can be looked up as needed, and repeated practice will solidify understanding.

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.

Pythonprogramming fundamentalsfunctions
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.