Skip to content

Instantly share code, notes, and snippets.

@scubamut
Created January 10, 2025 14:48
Show Gist options
  • Save scubamut/e07d0b25c340c905bd11c5d7c6bc1c5a to your computer and use it in GitHub Desktop.
Save scubamut/e07d0b25c340c905bd11c5d7c6bc1c5a to your computer and use it in GitHub Desktop.
'''
The asterisk * is the unpacking operator in Python.
- When used before an iterable (like a list, tuple, or generator), it "unpacks" the elements of that iterable.
- Unpacking can be used in multiple contexts:
+ Function arguments: function(*iterable) passes elements of the iterable as separate positional arguments to the function.
+ Creating new iterables: [ *iterable1 , *iterable2 ] creates a new list by combining elements from both.
'''
def my_function(a, b, c):
print(f"a = {a}, b = {b}, c = {c}")
my_list = [10, 20, 30]
my_function(*my_list) # equivalent to my_function(10, 20, 30)
my_function(*[x * 2 for x in range(1, 4)]) # my_function(2, 4, 6)
from ipywidgets import VBox, Layout, HBox, Button
def make_buttons(labels):
"""Generates a list of buttons."""
return [Button(description=label) for label in labels]
button_list = ["Button1","Button2","Button3","Button4","Button5"]
# Create HBox with buttons
h_box = HBox([*make_buttons(button_list)])
display(h_box)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment