Python (*)
Python (*):
In the mystic world of Python programming, the asterisk (*) is a powerful sorcerer, donning multiple cloaks and performing versatile spells, leaving onlookers enchanted.
- The spell of Multiplication & Power
In its most straightforward avatar, the asterisk serves as a common mathematical operator, executing multiplication of two numbers a * b
, and also raising a number to a power a ** b
.
print(5 * 3) # Conjures: 15
print(2 ** 3) # Conjures: 8
- The spell of List Replication
The asterisk can also enchant a list to multiply its contents, thereby casting a replication spell.
print([1, 2, 3] * 3) # Conjures: [1, 2, 3, 1, 2, 3, 1, 2, 3]
- The spell of Argument Unpacking
As an advanced spell, the asterisk allows wizards to unpack sequences like lists and tuples into individual elements, extremely handy when dealing with function arguments.
def potion(x, y, z):
return f"The ingredients are {x}, {y}, {z}"
ingredients = ['eye of newt', 'wool of bat', 'tongue of dog']
print(potion(*ingredients)) # Conjures: "The ingredients are eye of newt, wool of bat, tongue of dog"
- The spell of Any Number of Arguments (VarArgs)
Another intriguing role of the asterisk is when it allows a function to accept any number of arguments. It conjures a tuple from the passed arguments.
def arcane_spell(*ingredients):
for ingredient in ingredients:
print(f"Adding {ingredient} to the brew!")
arcane_spell('dragon scale', 'phoenix feather', 'unicorn hair')
# Conjures:
# Adding dragon scale to the brew!
# Adding phoenix feather to the brew!
# Adding unicorn hair to the brew!
- The spell of Any Number of Keyword Arguments (VarKwargs)
This master-level spell uses a double asterisk (**) to allow functions to accept any number of keyword arguments, conjuring a dictionary.
def enchant(**kwargs):
for item, magic in kwargs.items():
print(f"{item} is enchanted with {magic}!")
enchant(sword='flames', shield='invincibility')
# Conjures:
# sword is enchanted with flames!
# shield is enchanted with invincibility!
In the realm of Python, the asterisk (*) is indeed a powerful glyph, adaptable and dynamic, opening up realms of possibilities in the code. Every Python mage must learn to wield it skillfully to harness its full power.
Python Training Demo Day 1
Conclusion:
Unogeeks is the No.1 IT Training Institute for Python Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Python here – Python Blogs
You can check out our Best In Class Python Training Details here – Python Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks