Growth Journey of a Python Beginner · Lesson 3 (Part 1) | Functions

Hello everyone, I am Xingyuan, a 19-year-old self-taught Python beginner 🤓. Today we start the Functions section! First, we will learn about definitions, parameters, and return values, then understand scope and global variables, equipping our code with “LEGO blocks” 🚀.

📌 Today’s Learning Content

👉 “Pack repetitive code into blocks, use them as needed, and avoid typos!”

✨ Knowledge Points Explanation

1️⃣ <span>def</span> statement and parameters

  • Format:

    def function_name(parameter1, parameter2, ...):
        code block
  • Example:

    def hello(name):
        print('Hello ' + name)
    
    hello('Alice')   # Call
    hello('Bob')
  • Tip 🧐: The parameters in the function definition are called “formal parameters”, while those passed during the call are called “actual parameters”.

2️⃣ Return value and <span>return</span>

  • Function: Returns the result back to the calling place.

  • Example:

    import random
    
    def getAnswer(answerNumber):
        if answerNumber == 1:
            return 'It is certain'
        elif answerNumber == 2:
            return 'It is decidedly so'
        ...
        else:
            return 'Very doubtful'
    
    r = random.randint(1, 9)
    fortune = getAnswer(r)
    print(fortune)
  • Tip 🧐: A function without a <span>return</span> statement returns <span>None</span> by default.

3️⃣ <span>None</span> value

  • Concept: Represents “nothing”, and its type is <span>NoneType</span>.

  • Example:

    result = print('Hello')  # Returns None after printing
    print(result)            # None

4️⃣ Keyword parameters and <span>print()</span>

  • Concept: When calling a function, specify parameters using “name=value”, order is arbitrary.

  • Example:

    print('Hello', end='')      # No newline
    print('World')
    print('A', 'B', 'C', sep='|')  # A|B|C
  • Tip 🧐: <span>end</span> and <span>sep</span> are built-in keyword parameters of <span>print()</span>.

5️⃣ Local and global scope

Concept

  • Local variable: Exists only within the function, destroyed when the function ends.

  • Global variable: Usable throughout the entire file, destroyed only when the program ends.

Local variables cannot be used globally

def spam():
    eggs = 31337
spam()
print(eggs)   # NameError!

Local scope cannot access other locals

def spam():
    eggs = 99
    bacon()
    print(eggs)   # 99

def bacon():
    ham = 101
    eggs = 0      # New local variable
spam()

Global variables can be read in local scope

eggs = 42
def spam():
    print(eggs)   # 42
spam()

Local names shadow global names

eggs = 'global'
def spam():
    eggs = 'spam local'
    print(eggs)   # spam local
spam()
print(eggs)       # global
  • Tip 🧐: Avoid using the same name, or it can get confusing.

✅ Summary

  1. <span>def</span> creates a function, with parameters in parentheses.

  2. <span>return</span> brings the result out; if not written, it returns <span>None</span>.

  3. <span>print()</span> has <span>end</span>/<span>sep</span> as keyword parameters, allowing customization of output format.

  4. Local variables exist only within functions, while global variables are accessible throughout the file.

  5. When names are the same, local variables will “cover” global ones, so be cautious with naming!

📢 Interactive Question

👉 “When you first wrote a function, did you accidentally write <span>return</span> as <span>print</span>, resulting in not getting the return value?” Share your funny moments in the comments, and let’s laugh together!

Leave a Comment