Most important Python one-liners for faster coding


Python is an easy-to-learn, multi-purpose programming language, which also has a shorter syntax than most programming languages. We can shorten the code further by using one-liner tricks that reduce the multi-line code into a single line. In this article, we will explore some Python one-liners for faster coding and day-to-day use.

Contents

Ternary Operator (if…else one-liner)

The ternary operator in Python is a bit different than other common programming languages. In other languages, the ternary operator looks like this: a >= b ? a : b . However, we use a one-liner if..else statement in Python for the functionality of the ternary operator.

num = 12

# Ternary operator
text = "EVEN" if num % 2 == 0 else "ODD"

print(text)

The above code checks if the variable num is even or odd and assigns the text value accordingly.

Reverse a list

We can use list slicing to reverse a list in Python.

nums = [10, 20, 30, 40, 50]

# Reverse the list
reversed_nums = nums[::-1]

print(reversed_nums)

This also works for string reversal. The following code snippet uses string reversal and ternary operator.

name = "hannah"

# Reverse the string
reversed_name = name[::-1]

# Ternary operator to find out if the name is palindrome
is_palindrome = True if name == reversed_name else False

print(is_palindrome)

Multiple variable assignment

We can assign values of multiple variables in a single line in Python as shown below. Moreover, the values are assigned to the variables on the basis of the order they are written.

first_name, last_name, birth_year = "Neil", "Armstrong", 1930

print(first_name, last_name, birth_year)

List Comprehension

We can write loops for lists, tuples, or dictionaries in Python in a one-line expression. This is called list comprehension, and it can contain complex expressions and nested functions. One of its benefits is the faster code execution time because it requires fewer function calls in most cases.

squares_of_evens = [(num, num**2) for num in range(100) if num%2 ==0]
print(squares_of_evens)

The above code loops through the numbers from 0 through 99, and creates a list of tuples containing the number and its square if the number is even number(i.e. divisible by 2).

Python Lambda

Lambda is an inline and anonymous function in Python. In general, lambda functions are comparatively faster but these should only be used if function logic is simple and small. You can read more about lambda functions in the official documentation.

full_name = (lambda a, b: a + " " + b)("John", "Smith")
print(full_name)

Numbers in a text to a list

We can separate numbers present in a text and create a list from those numbers.

numbers = "1,2,3,4,5,6"
num_list = [num for num in numbers.split(",")]
print(num_list)

Conclusion

In this article, we explored some Python one-liners that can be used for faster coding.

If you have any comments or suggestions regarding this post or any other programming topic, please feel free to post them below.

You can also explore more articles on Programming and Python.


admin

Tech and programming enthusiast

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *