Table of contents
- 1. Anagram Checker: A Glimpse of Python's Collections ๐
- 2. Binary to Decimal Conversion: Simplicity in Bits ๐งฎ
- 3. String Transformation: Uppercase to Lowercase ๐
- 4. String Transformation: Uppercase Extravaganza ๐
- 5. String to Bytes: Encoding Magic ๐งโโ๏ธ
- 6. File Copying: Swift and Simple ๐
- 7. Quicksort: Concise Sorting at Its Best ๐งน
- 8. Sum of Consecutive Numbers: Simplicity Trumps Complexity โ
- 9. Value Swap: Python's Elegant Dance ๐
- 10. Fibonacci Fun: Lambda for Fibonacci Series ๐
In the world of coding, elegance and brevity can be just as captivating as complexity. Welcome to the first instalment of our Python One-Liners series, where we'll explore the art of achieving remarkable tasks in a single line of Python code. Inspired by the fascinating world of JavaScript one-liners, this series aims to showcase the beauty and efficiency of Python in solving various programming challenges.
1. Anagram Checker: A Glimpse of Python's Collections ๐
Have you ever wondered if two words are anagrams of each other? Python makes this task a breeze with its Collections module. Here's a one-liner that checks if two words, s1
and s2
, are anagrams:
2. Binary to Decimal Conversion: Simplicity in Bits ๐งฎ
Converting binary to decimal doesn't need to be a tedious task. Python's built-in functions can do the heavy lifting for you. Here's a one-liner that converts a binary string to its decimal equivalent:
3. String Transformation: Uppercase to Lowercase ๐
Python provides effortless ways to manipulate strings. Want to convert a string to a lowercase or case-insensitive format? Check out these one-liners:
4. String Transformation: Uppercase Extravaganza ๐
On the flip side, if you need to go uppercase, Python's got your back:
5. String to Bytes: Encoding Magic ๐งโโ๏ธ
Sometimes, you might need to convert a string into bytes for various purposes. Python's encode method makes it a breeze:
6. File Copying: Swift and Simple ๐
Copying files can be effortless in Python, thanks to the shutil library. Here's a one-liner to duplicate a file:
7. Quicksort: Concise Sorting at Its Best ๐งน
Sorting doesn't have to be complicated. Check out this elegant one-liner for quicksort in Python:
// Traditional
def quicksort(lst):
if len(lst) <= 1:
return lst
else:
pivot = lst[0]
less = [x for x in lst[1:] if x < pivot]
greater = [x for x in lst[1:] if x >= pivot]
return quicksort(less) + [pivot] + quicksort(greater)
qsort = lambda l : l if len(l)<=1 else qsort([x for x in l[1:] if x < l[0]]) + [l[0]] + qsort([x for x in l[1:] if x >= l[0]])
8. Sum of Consecutive Numbers: Simplicity Trumps Complexity โ
Calculating the sum of consecutive numbers can be done with a one-liner:
9. Value Swap: Python's Elegant Dance ๐
Python's simplicity extends to swapping values. Here's a one-liner to swap the values of two variables, a
and b
:
10. Fibonacci Fun: Lambda for Fibonacci Series ๐
Want to generate a Fibonacci series? Python's lambda function can handle it in just one line:
With this series kickoff, you've delved into the world of Python one-liners. Stay tuned for more code magic in our upcoming articles. If you're itching to explore the wonders of concise code, Python is your wand, and these one-liners are your spells.