In this article, you’ll learn some important Python one-liners that will help you code like a pro.
1. Convert String to Integer
You can convert a string to an integer using the inbuilt int() function.
Output:
2. Reverse a List
You can reverse a list in Python using various methods:
Using the Slicing Technique
Using this technique, the original list is not modified, but a copy of the list is created.
Output:
You can use the same slicing technique to reverse a string in Python.
Output:
Using the Inbuilt reversed() Function
The reversed() function returns an iterator that accesses the given list in the reverse order.
Output:
Using the Inbuilt reverse() Method
The reverse() method reverses the elements of the original list.
Output:
3. Swap Two Variables
You can swap two variables using the following syntax:
You can swap variables of any data type using this method.
Output:
4. FizzBuzz One-Liner in Python
The FizzBuzz challenge is a classic challenge that’s used as an interview screening device for computer programmers. You can solve the FizzBuzz challenge in just one line of code:
Output:
5. Generate Random Password
You can generate random passwords in Python using the following one-liner code:
Output:
This code generates a password of length 10. If you want to change the length of the password, update the parameter of the choices() method. Also, each time when you run the code, you’ll get a different random output.
6. Display the Current Date and Time in String Format
You can display the current date and time in Python using the datetime module. Here’s the one-liner code to display the current date and time in string format:
Output:
7. Check if a String Is a Palindrome
A string is said to be a palindrome if the original string and its reverse are the same. You can check if a string is a palindrome or not using the following code:
Output:
8. Find Factorial of a Number
The factorial of a non-negative integer n is the product of all positive integers less than or equal to n. You can find the factorial of a number in one line of code using lambda functions. Lambda functions in Python are some of the most important features to know about.
Output:
9. Print Fibonacci Series Up to N Terms
A Fibonacci series is a series of numbers where each term is the sum of the two preceding ones, starting from 0 and 1. You can print the Fibonacci series up to n terms using the lambda function in Python.
Output:
10. Calculate the Sum of a List
You can calculate the sum of a list using the sum() function in Python.
Output:
11. Sort a List
You can sort a list using the sort() method. Here’s the one-liner code for the same:
Output:
12. Convert List to String in Python
You can convert a list to a string in Python using the join() method and list comprehension.
Output:
Write More Pythonic Code Using Built-In Methods and Functions
Inbuilt methods and functions help to shorten the code and increase its efficiency. Python provides many built-in methods and functions like reduce(), split(), enumerate(), eval(), and so on. Make use of all of them and write more Pythonic code.