Placeholder Variables
Part of the Logic & Flow section of Coddy's Python journey — lesson 4 of 78.
In programming, a placeholder variable is a variable that is used to hold a value temporarily, often during the execution of a specific block of code. Placeholder variables are commonly used in situations where you need to perform operations on a value without changing the original variable. In Python, a common convention for placeholder variables is to use an underscore _ as the variable name.
Using a Single Underscore _:
# Example of using _ as a placeholder in a loop
for _ in range(5):
print("Looping")
# Output:
# Looping
# Looping
# Looping
# Looping
# LoopingIn this example, _ is used as a placeholder because the loop variable is not needed in the body of the loop.
Using Multiple Single Underscores:
In cases where you have multiple values and you only need some of them, you can use the underscore character multiple times as separate placeholder variables. For example:
data = (1, 2, 3, 4, 5)
first, _, _, _, last = data
print(first) # Output: 1
print(last) # Output: 5Here, _ is used to ignore the second, third, and fourth elements of the tuple.
Challenge
EasyWrite a Python program that demonstrates the use of placeholder variables in different scenarios:
- Create a loop that iterates 5 times. Use a placeholder variable (an underscore) since the loop variable is not used within the loop. In each iteration, print the word
Iteration. - You have a list of numbers:
[10, 20, 30, 40, 50]. Unpack this list into three variables:first,middle, andlast. Use a placeholder variable to ignore the second and fourth values. Then, print the values offirst,middle, andlast.
Check the test case for the output format!
Cheat sheet
A placeholder variable holds a value temporarily and is commonly represented by an underscore _ in Python.
Using underscore in loops when the loop variable isn't needed:
for _ in range(5):
print("Looping")Using multiple underscores to ignore values during unpacking:
data = (1, 2, 3, 4, 5)
first, _, _, _, last = data
print(first) # Output: 1
print(last) # Output: 5Try it yourself
# Loop 5 times using a placeholder variable
# List of numbers
numbers = [10, 20, 30, 40, 50]
# Unpack the list using placeholder variables
# Print the values of first, middle, and last
print(f"First: {first}")
print(f"Middle: {middle}")
print(f"Last: {last}")This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter