-
Python樹林
Python Closures: How to Retain State in Functions and the nonlocal Keyword
In Python, a "Closure" is an important concept in functional programming. Simply put, it refers to "a function that continues to hold variables from the environment (scope) in which it was defined." Normally, local variables defined with... -
Python樹林
Python Inner Functions (Nested Functions): Benefits and Usage of Defining Functions Inside Functions
In Python, you can define a function inside another function. This is called an "Inner Function" or "Nested Function." At first glance, it might seem complicated, but it is a very effective technique when you want to reuse specific proce... -
Python樹林
Python Functions are First-Class Objects: Assignment and Passing as Arguments
In Python, functions are treated as "First-Class Objects." This means that functions can be treated just like other data types such as numbers or strings: they can be assigned to variables, passed as arguments to other functions, or stor... -
Python樹林
Python Global vs. Local Variables: Handling External Variables and the global Keyword
In programming, the range in which a variable can be referenced and remains valid is called its "Scope." In Python, there are specific rules when handling variables defined outside a function (Global/Module variables) from within a funct... -
Python樹林
Returning Multiple Values from Python Functions: Tuple Auto-Packing and Unpacking
In many programming languages, a function can basically return only one value. If you want to return multiple values, you usually have to pack them into an array or an object. However, in Python, you can easily return multiple values sim... -
Python樹林
Python Function Default Arguments: Basics and Why You Should Avoid Mutable Defaults
When defining a function, setting a value for an argument beforehand allows you to omit that argument when calling the function. This is called a "Default Argument." While this is a very convenient feature, you need to be careful with Py... -
Python樹林
Unpacking Argument Dictionaries in Python: How to Pass Dictionaries as Keyword Arguments (** Unpacking)
In Python function calls, there is a feature that allows you to pass data stored in a dictionary (dict) as "keyword arguments" all at once. This uses double asterisks **. It acts as the counterpart to *, which unpacks lists or tuples as ... -
Python樹林
Unpacking Argument Lists in Python: How to Pass Lists and Tuples using *
When passing arguments to a function, sometimes your data is already grouped in a list or a tuple. Normally, you would need to extract elements from the list one by one using indices to specify them as arguments. However, in Python, usin... -
Python樹林
Python Positional-Only Arguments: Meaning and Usage of the Slash (/) Syntax
Since Python 3.8, you can use a slash / in the argument list when defining a function. This syntax is used to define "Positional-Only Arguments." By using this, you can prohibit specific arguments from being called as keyword arguments a... -
Python樹林
Python Variable-Length Keyword Arguments (**kwargs): How to Accept Any Number of Optional Arguments
When defining a function, there are cases where you want to accept an arbitrary number of "optional settings" or "additional attributes" apart from the mandatory arguments. While *args (introduced in the previous article) receives "posit... -
Python樹林
Python Variable-Length Positional Arguments (*args): How to Accept Any Number of Arguments
When defining a function, you may not know the number of arguments in advance. For example, you might need a function that sums up multiple numbers or outputs multiple log messages at once. In Python, adding an asterisk (*) before an arg... -
Python樹林
Specifying Python Function Arguments: Positional vs. Keyword Arguments
When calling a function in Python, there are two ways to pass arguments: "Positional Arguments" and "Keyword Arguments." For simple functions, positional arguments are sufficient. However, when a function has many arguments or complex co... -
Python樹林
How to Define Unimplemented Functions in Python: pass and NotImplementedError
In the early stages of development, you often want to define just the function or class names to design the overall structure, intending to implement the logic later. However, since Python manages blocks using indentation, leaving a func... -
Python樹林
Introduction to Python Functions: Basics of Definition with def, Arguments, and Return Values
In programming, a "function" is a way to group a specific process, give it a name, and make it reusable. Python has many built-in functions like print() and len(), but you can also create (define) your own custom functions. This article ... -
Python樹林
Python’s Assignment Expression (Walrus Operator): How to Assign Variables Inside Expressions
Introduced in Python 3.8, Assignment Expressions allow you to assign a value to a variable and evaluate that value at the same time. The operator := looks like the eyes and tusks of a walrus lying on its side, hence it is commonly called... -
Python樹林
Python’s Structural Pattern Matching: Advanced Branching and Data Extraction with match-case
"Structural Pattern Matching (match statement)," introduced in Python 3.10, is a powerful feature. It goes beyond simple value comparison; it branches based on the structure of data (lists, tuples, dictionaries, objects, etc.) and can si... -
Python樹林
Python’s for-else Statement: Processing When a Loop Completes Without break
It is well known that else in an if statement means "if the condition is not met." However, it is surprisingly less known that else can also be used with loop structures like for and while in Python. The else block in a loop has a unique... -
Python樹林
Controlling Python Loops: Using the continue Statement to Skip Specific Iterations
When processing data in a loop, there are times when you want to "skip specific items that meet certain conditions" without stopping the entire process. For example, you might want to ignore image files in a file list or exclude invalid ... -
Python樹林
Python Loop Control: How to Use the break Statement to Stop Processing
When using for or while loops, you may want to stop the loop immediately if a specific error occurs or if you find the data you were looking for. To achieve this "escape from the loop," Python uses the break statement. This article expla... -
Python樹林
Python while Loops: Repeating Actions While a Condition is Met
When performing repetitive tasks in Python, the for loop is suitable when you know the "number of elements in a list" or a "specific number of iterations." On the other hand, there are cases where the number of iterations is not fixed, b... -
Python樹林
Python Dictionary Comprehensions: How to Create and Manipulate Dictionaries Efficiently and Concisely
Just like List Comprehensions, Python has a feature called "Dictionary Comprehension" for efficiently generating and processing dictionaries (dict). Compared to creating a dictionary using a standard for loop, this method reduces the amo... -
Python樹林
Python Set Comprehensions: Processing Data and Removing Duplicates
Just like "List Comprehensions" for creating lists, Python has "Set Comprehensions" for creating sets (set) concisely. While list comprehensions use [] (square brackets), set comprehensions use {} (curly braces). The biggest feature of s... -
Python樹林
Getting the Index (Loop Count) in Python for Loops: How to Use the enumerate Function
Python's for loops are very convenient because they let you process elements directly from a list. However, there are times when you need information like "which element number is this? (index)" or "how many times has the loop run?". Use... -
Python樹林
Looping Through Python Dictionaries: How to Iterate Over Keys, Values, and Items
When you want to process all data stored in a Python dictionary (dict) sequentially, use the for statement. Unlike lists, dictionaries contain three distinct elements: "Keys," "Values," and "Key-Value Pairs." Therefore, you need to use d... -
Python樹林
Python while Loops: Repeating Actions While a Condition is Met
When performing repetitive tasks in Python, the for loop is suitable when you know the "number of elements in a list" or a "specific number of iterations." On the other hand, there are cases where the number of iterations is not fixed, b... -
Python樹林
Python Dictionary Comprehensions: How to Create and Manipulate Dictionaries Efficiently and Concisely
Just like List Comprehensions, Python has a feature called "Dictionary Comprehension" for efficiently generating and processing dictionaries (dict). Compared to creating a dictionary using a standard for loop, this method reduces the amo... -
Python樹林
Python List Comprehensions: Efficient List Creation in One Line
Python has a feature called "List Comprehensions" that allows you to concisely create a new list based on an existing list (or other iterables). This is one of Python's key features. Compared to using a standard for loop with append(), t... -
Python樹林
Python’s zip() Function: How to Loop Through Multiple Lists Together and Caveats
When programming, you often encounter situations where related data is stored in separate lists, such as a "list of names" and a "list of ages," and you want to retrieve and process them simultaneously. While you could access them using ... -
Python樹林
Looping a Set Number of Times in Python: Using the range() Function
In programming, regardless of the number of elements in a list, there are times when you want to execute a loop a specific number of times, such as "I want to repeat a process only 5 times" or "I want to retry 10 times." In Python, you c... -
Python樹林
Python for Loops: Basics of Looping Through Lists and Iterables
In programming, we frequently encounter situations where we want to apply the same process sequentially to a collection of data (such as a list or tuple). Python allows you to write these repetitive tasks concisely using the for statemen...