Fundamentals 8 min read

Commonly Used Python Functions and Examples for Beginners

This article presents a concise collection of over one hundred frequently used Python functions across twelve categories—basic I/O, control flow, data structures, string formatting, dictionaries, custom functions, threading, modules, file handling, decorators, and regular expressions—each illustrated with clear code examples to help beginners quickly memorize and apply them.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Commonly Used Python Functions and Examples for Beginners

Beginners often get stuck when writing code because they cannot recall which built‑in function to use for a given task; this guide compiles more than 100 common Python functions into twelve sections, providing short examples that can be reviewed daily to reinforce memory.

1. Basic Functions

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

<code>f = 30.5
ff = str(f)
print(type(ff))  # output: class 'str'</code>

2. Control Flow

Example: grade a score entered by the user with multiple conditional branches.

<code>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分")</code>

3. Lists

Example: find the index of the number 6 within a list.

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

4. Tuples

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

<code># take elements with indices 1‑3 and convert to list
t = (1,2,3,4,5)
print(t[1:4])
l = list(t)
print(l)
# replace element at index 2 with 6
l[2] = 6
print(l)
# convert back to tuple
t = tuple(l)
print(t)</code>

5. Strings

Three ways to use format() for string interpolation.

<code>"{0} 嘿嘿".format("Python")
a = 100
s = "{0}{1}{2} 嘿嘿"
s2 = s.format(a, "JAVA", "C++")
print(s2)  # 100JAVAC++ 嘿嘿

s = "{}{}{} 嘿嘿"
s2 = s.format(a, "JAVA", "C++", "C# ")
print(s2)  # 100JAVAC++ 嘿嘿

s = "{a}{b}{c} 嘿嘿"
s2 = s.format(b="JAVA", a="C++", c="C# ")
print(s2)  # C++JAVAC#  嘿嘿</code>

6. Dictionaries

Example: retrieve a value with dict.get() providing a default.

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

7. Functions

Illustrates a custom function that defines a global variable.

<code>def fun1():
    global b
    b = 100
    print(b)
fun1()
print(b)  # both prints 100</code>

8. Processes and Threads

Example of creating a thread by subclassing threading.Thread .

<code>class MyThread(threading.Thread):
    def __init__(self, name):
        super().__init__()
        self.name = name
    def run(self):
        for i in range(5):
            print(self.name)
            time.sleep(0.2)

t1 = MyThread("凉凉")
t2 = MyThread("最亲的人")
t1.start()
t2.start()</code>

9. Modules and Packages

Example of importing a module from a package and using its attributes.

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

10. File Operations

Brief overview of common file modes, file object attributes, and methods, followed by a mention of the os module for directory handling.

11. Decorators / Class Methods

Demonstrates the use of @classmethod to access class variables.

<code>class B:
    age = 10
    def __init__(self, name):
        self.name = name
    @classmethod
    def eat(cls):
        print(cls.age)

b = B("小贱人")
b.eat()  # prints 10</code>

12. Regular Expressions

Using re.split() to split a string.

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

The purpose of this article is to help readers quickly memorize the names and purposes of frequently used Python functions; detailed usage can be looked up as needed, allowing a more purpose‑driven learning approach.

Pythoncode examplesFunctionsbasics
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

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