Skip to content

Latest commit

 

History

History
217 lines (157 loc) · 6.23 KB

File metadata and controls

217 lines (157 loc) · 6.23 KB

Implementing Python Data Structures Without External Libraries

Slide 1: Introduction to Underscore in Python

The underscore (_) in Python is a versatile character with multiple uses. It serves as a convention to indicate that a value is meant to be ignored or is considered unimportant in certain contexts. This practice enhances code readability and conveys intent to other developers.

# Unpacking a tuple, ignoring the second value
first, _, third = (1, 2, 3)
print(f"First: {first}, Third: {third}")

# Output:
# First: 1, Third: 3

Slide 2: Iterating with Underscore

When iterating through a sequence and the loop variable is not needed, the underscore can be used to signify that the value is unused. This is particularly useful in situations where you're only interested in the number of iterations.

# Printing "Hello" 3 times without using the loop variable
for _ in range(3):
    print("Hello")

# Output:
# Hello
# Hello
# Hello

Slide 3: Unpacking in For Loops

The underscore can be used to ignore specific elements when unpacking in a for loop. This is helpful when working with sequences of tuples or lists where only some elements are relevant.

# Unpacking coordinates, ignoring the y-value
points = [(1, 2), (3, 4), (5, 6)]
for x, _, z in points:
    print(f"X: {x}, Z: {z}")

# Output:
# X: 1, Z: 2
# X: 3, Z: 4
# X: 5, Z: 6

Slide 4: Ignoring Function Return Values

When a function returns multiple values but only some are needed, the underscore can be used to ignore the unwanted return values.

def get_user_info():
    return "John", "Doe", 30

# Ignoring the last name
first_name, _, age = get_user_info()
print(f"Name: {first_name}, Age: {age}")

# Output:
# Name: John, Age: 30

Slide 5: Underscore in Internationalization

In internationalization (i18n), the underscore is often used as a function name to mark strings for translation. While this isn't directly related to ignoring values, it's a common use of the underscore in Python.

import gettext

_ = gettext.gettext  # Set up the translation function

# Mark strings for translation
print(_("Hello, World!"))
print(_("Welcome to Python"))

# Output (before translation):
# Hello, World!
# Welcome to Python

Slide 6: Separating Digits in Numeric Literals

Python 3.6+ allows the use of underscores in numeric literals to improve readability of large numbers. The interpreter ignores these underscores.

# Using underscores to separate thousands in a large number
population = 7_837_952_000
print(f"World population: {population:,}")

# Separating bytes in a binary literal
binary_value = 0b1010_1010
print(f"Binary value: {binary_value}")

# Output:
# World population: 7,837,952,000
# Binary value: 170

Slide 7: Naming Conventions with Underscore

Single and double leading underscores in variable or method names have special meanings in Python, related to name mangling and indicating private attributes.

class MyClass:
    def __init__(self):
        self._protected_var = 42
        self.__private_var = 100
    
    def _internal_method(self):
        print("This method is intended for internal use")

obj = MyClass()
print(obj._protected_var)  # Accessible, but conventionally treated as protected
# print(obj.__private_var)  # This would raise an AttributeError
obj._internal_method()  # Callable, but conventionally internal

# Output:
# 42
# This method is intended for internal use

Slide 8: Underscore as a Variable Name

The underscore can be used as a valid variable name. In interactive Python sessions, it holds the result of the last expression evaluated.

# In an interactive Python session:
>>> 5 + 3
8
>>> _
8
>>> _ * 2
16
>>> _
16

Slide 9: Ignoring Exceptions

When catching exceptions, if you don't need to use the exception object, you can use an underscore as the variable name.

try:
    # Some code that might raise an exception
    result = 10 / 0
except ZeroDivisionError as _:
    print("Cannot divide by zero")

# Output:
# Cannot divide by zero

Slide 10: Placeholder Names in Lambda Functions

In lambda functions, the underscore can be used as a placeholder for arguments that won't be used in the function body.

# Sorting a list of tuples based on the second element
pairs = [(1, 'one'), (3, 'three'), (2, 'two')]
sorted_pairs = sorted(pairs, key=lambda _: _[1])
print(sorted_pairs)

# Output:
# [(1, 'one'), (3, 'three'), (2, 'two')]

Slide 11: Real-Life Example: Data Processing

Consider a scenario where you're processing weather data, but only interested in temperature and humidity.

weather_data = [
    ("New York", 25, 60, 1013),  # City, Temperature, Humidity, Pressure
    ("Los Angeles", 30, 50, 1012),
    ("Chicago", 22, 65, 1015)
]

for city, temp, humidity, _ in weather_data:
    print(f"{city}: {temp}°C, {humidity}% humidity")

# Output:
# New York: 25°C, 60% humidity
# Los Angeles: 30°C, 50% humidity
# Chicago: 22°C, 65% humidity

Slide 12: Real-Life Example: Image Processing

In image processing, when working with RGB values, you might sometimes only need the red and blue channels.

def process_rgb_image(pixels):
    processed = []
    for r, _, b in pixels:
        gray = (r + b) // 2
        processed.append(gray)
    return processed

# Simulated image data (list of RGB tuples)
image_data = [(255, 100, 0), (128, 200, 128), (0, 150, 255)]
result = process_rgb_image(image_data)
print("Processed image data:", result)

# Output:
# Processed image data: [127, 128, 127]

Slide 13: Conclusion

The underscore in Python serves as a powerful convention for improving code readability and expressing intent. Whether it's used for ignoring values, enhancing numeric literal readability, or following naming conventions, the underscore is a versatile tool in a Python developer's toolkit. Understanding its various uses can lead to cleaner, more expressive code.

Slide 14: Additional Resources

For more information on Python conventions and best practices, including the use of underscores, refer to the following resources:

  1. PEP 8 -- Style Guide for Python Code https://arxiv.org/abs/2207.07285
  2. "The Hitchhiker's Guide to Python" by Kenneth Reitz and Tanya Schlusser ArXiv reference: arXiv:1412.7515