Essential Python Functions for Beginners to Experts

Follow + Star, Learn New Python Skills Daily

Due to changes in the public account's push rules, please click "Read" and add "Star" to receive exciting technical sharing in a timely manner. Reply with "python" in the public account backend to receive the latest free trial course for 2023.

Introduction: Beginners often get stuck when coding, especially when they encounter many functions and other knowledge. After reading the requirements, they may not know which method to use to implement them. While you might have the logic, you forget which function to use, which indicates insufficient knowledge retention; if you cannot remember the purpose of each function, you will naturally feel lost.

Recently, I have organized some commonly used Python functions, covering over 100 useful functions from the most basic input/output functions to regular expressions across 12 sections. This is designed to help you quickly memorize them. Review them quickly every day, and when you use them, reinforce your understanding, and gradually, you will overcome the coding block.

While we emphasize understanding and practical coding during self-learning, some things must be memorized; otherwise, coding will be challenging. Experienced programmers have these functions memorized, while beginners can quickly become proficient by remembering frequently used functions.

1. Basic Function Example: Convert a float to a string and output the converted data type.
f = 30.5
ff = str(f)
print(type(ff))

# Output: class 'str'
2. Control Flow Example: Determine the grade based on user input score. If the score is below 50, prompt "Your score is below 50"; if between 50-59, prompt "Your score is around 60"; if 60 or above, it's passing; 80-90 is excellent, and above 90 is outstanding.
s = int(input("Please enter your score:"))
if 80 >= s >= 60:
    print("Passing")
elif 80 < s <= 90:
    print("Excellent")
elif 90 < s <= 100:
    print("Outstanding")
else:
    print("Failing")
    if s > 50:
        print("Your score is around 60")
    else:
        print("Your score is below 50")
3. List Example: Determine the position of the number 6 in the list [1,2,2,3,6,4,5,6,8,9,78,564,456] and output its index.
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: Modify a tuple.
# Get 3 elements from index 1 to 4 of the tuple and convert it to a list.
t = (1,2,3,4,5)
print(t[1:4])
l = list(t)
print(l)
# Insert a 6 at index 2 of the list.
l[2]=6
print(l)
# Convert the modified list back to a tuple and output.
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. String Example: Output a string using the format() function in three ways.
Method 1: Using numeric placeholders (indexes).
"{0} hey hey".format("Python")
a=100
s = "{0}{1}{2} hey hey"
s2 = s.format(a,"JAVA","C++")
print(s2)

# Output: 100JAVAC++ hey hey
Method 2: Using {} placeholders.
a=100
s = "{}{}{} hey hey"
s2 = s.format(a,"JAVA","C++","C# ")
print(s2)

# Output: 100JAVAC++ hey hey
Method 3: Using letter placeholders.
s = "{a}{b}{c} hey hey"
s2 = s.format(b="JAVA",a="C++",c="C# ")
print(s2)

# Output: C++JAVAC# hey hey
6. Dictionary Example: Look up data in a dictionary.
d = {"name": "Little Black"}
print(d.get("name2", "Not Found"))
print(d.get("name"))
# Output:
Not Found
Little Black
7. Functions: The main focus here is on custom functions; there aren't many commonly used built-in functions, mainly the following:
Example: Define a local variable in a function that can still be accessed after exiting the function.
def fun1():
    global b
    b=100
    print(b)
fun1()
print(b)
# Output:
100
100
8. Process and Thread Example: Inherit the Thread class to implement.
# Create multiple threads.
class MyThread(threading.Thread):
    def __init__(self,name):
        super().__init__()
        self.name = name
    def run(self):
        # What the thread needs to do.
        for i in range(5):
            print(self.name)
            time.sleep(0.2)
# Instantiate the child threads.
t1 = MyThread("Cool")
t2 = MyThread("Closest Person")

t1.start()
t2.start()
9. Module and Package Example: Usage of packages.
from my_package1 import my_module3
print(my_module3.a)
my_module3.fun4()
10. File Operations (1) General file operations.
About the general modes of file operations:
Attributes of the file object.
Methods of the file object.
(2) OS module
  • About file functionalities
Essential Python Functions for Beginners to Experts
  • About folder functionalities
Essential Python Functions for Beginners to Experts

11. Decorators

Essential Python Functions for Beginners to Experts

Example: Usage of classmethod.

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

    def sleep(self):
        print(self)

b = B("Little Rascal")
b.eat()

# Output: 10

12. Regular Expressions

Essential Python Functions for Beginners to Experts

Example: Use the split() function to split a string and convert it to a list.

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 everyone how to use functions, but to quickly and conveniently remember commonly used function names. Therefore, I haven’t provided examples for every function; you need to remember the function names and their purposes first. As for how to use them, a quick search on Baidu will yield results, and after using them a few times, you’ll master them.

If you don’t even know the function names and their uses, you’ll spend much more time and effort. It would be faster to look up information with a clear purpose.

It reminds me of 2010 when Python became the most popular programming language, and I started learning Python (previously, I mainly used Java). I memorized dozens of pages of functions, carrying the notebook to and from work, reviewing whenever I had a chance. One night after work, I had dinner with colleagues and drank a bit, and ended up losing my bag, along with the notebook. I regretted it the next day… had to rewrite everything.

Honestly, Python is quite fun, and it’s not as daunting anymore.

Fan Benefits: Recruiting 99 eager learners (only accepting 99 apprentices), free guidance for complete beginners, live teaching every night with free entry-level material packages. Add me on WeChat: LY0731CS to receive it!!

Essential Python Functions for Beginners to Experts

Leave a Comment