Python is a very simple and easy-to-use language that provides a fascinating experience with its simplicity, popularity, and its famous one-liners to the users.
So, Let’s have a look at some of its one-liners. This would help you so much during a Python interview. Also, while we’re at it, do test yourself with Python Interview Questions to be better prepared for it: Visit here!
1. How to Swap Two Variables?
Let’s start with a simple program that is used for swapping two variables with each other. This is one of the easiest methods that anyone with little effort can write. The need to use a temp variable or apply any sort of arithmetic operations is eliminated.
Python code to swap two variables
- # declaring the two variables
- x = 50
- y = 100
- #printing the initial values of the variables
- print(“The initial value for x variable:”, x)
- print(“The initial value for y variable:”, y)
- # writing the code for swapping the two variables
- x , y = y , x
- # printing the swapped variables
- print(“The interchanged value for x variable:”, x)
- print(“The interchanged value for y variable:”, y)
Output:
The initial value for x variable: 50
The initial value for y variable: 100
The interchanged value for x variable: 100
The interchanged value for y variable: 50
Python One liner code to swap two variables
- # declaring the two variables
- x , y = 50 , 100
- #printing the initial values of the variables
- print(“The initial value for x variable:”, x)
- print(“The initial value for y variable:”, y)
- # writing the code for swapping the two variables
- x , y = y , x
- # printing the swapped variables
- print(“The interchanged value for x variable:”, x)
- print(“The interchanged value for y variable:”, y)
Output:
The initial value for x variable: 50
The initial value for the y variable: 100
The interchanged value for x variable: 100
The interchanged value for the y variable: 50
2. How to Assign Multiple Variables?
When defining many variables, it is very time-consuming to define each and every variable separately. Users can declare multiple variables using single lines of codes by using commas to assign multiple values to variables. Following are the examples for multiple variable assignments.
Python code to assign multiple variables
- # declaring the multiple variables
- x = 50
- y = 100
- z = ‘Hello, this is an example of assigning multiple variables.’
- # printing the assigned variables
- print(“The value of x variable is:”, x)
- print(“The value of y variable is:”, y)
- print(“The value of z variable is:”, z)
Output:
The value of the x variable is: 50
The value of the y variable is: 100
The value of the z variable is: Hello, this is an example of assigning multiple variables.
Python One liner code to assign multiple variables
- # declaring the multiple variables
- x, y, z = 50, 100, ‘Hello, this is an example of assigning multiple variables.’
- # printing the assigned variables
- print(“The value of x variable is:”, x)
- print(“The value of y variable is:”, y)
- print(“The value of z variable is:”, z)
Output:
The value of the x variable is: 50
The value of the y variable is: 100
The value of the z variable is: Hello, this is an example of assigning multiple variables.
3. How to Find Sum of Even and Odd Numbers In a List?
Find the sum of even or odd numbers in a list can be solved using many different approaches, following are the best and the easiest way to solve such problems.
Python code to find the sum of even numbers in a list
- # creating a list containing both even and odd numbers
- list = [10,15,20,25,30]
- # declaring summation
- summation = 0
- # writing the code for the sum of even number of the list
- for i in range(5):
- if list[i]%2 == 0:
- summation = summation + list[i]
- # printing the sum of even number of the list
- print(“The addition of even number of the list is:”, summation)
Output:
The addition of an even number to the list is: 60
Python One liner code to find the sum of even numbers in a list
- # creating a list containing both even and odd numbers
- list = [10,15,20,25,30]
- # writing the code for the sum of even number of the list
- summation = sum([num for num in list if num%2 == 0])
- # printing the sum of even number of the list
- print(“The addition of even number of the list is:”, summation)
Output:
The addition of an even number to the list is: 60
Python code to find the sum of odd numbers in a list
- # creating a list containing both even and odd numbers
- list = [10,15,20,25,30]
- # declaring summation
- summation = 0
- # writing the code for the sum of the odd number of the list
- for i in range(5):
- if list[i]%2 != 0:
- summation = summation + list[i]
- # printing the sum of odd number of the list
- print(“The addition of odd number of the list is:”, summation)
Output:
The addition of an odd number to the list is: 40
Python One liner code to find the sum of even numbers in a list
- # creating a list containing both even and odd numbers
- list = [10,15,20,25,30]
- # writing the code for the sum of the odd number of the list
- summation = sum([num for num in list if num%2 == 0])
- # printing the sum of odd numbers of the list
- print(“The addition of an odd number of the list is:”, summation)
Output:
The addition of an odd number to the list is: 40
4. How to Eliminate Items (multiple) from a List?
If the user wants to delete or remove single or multiple elements or items from a particular list, the del keyword can be used to serve the purpose.
Python One liner code to delete multiple elements from a list
- # creating a list containing both even and odd numbers
- list = [56 , 85 , 97 , 65 , 23 , 12]
- # declaring the items to be removed from the list
- items_to_be_removed = {12 , 97}
- list = [item for item in list if item not in items_to_be_removed]
- # printing the updated list
- print(“The updated list from which the desired elements are eliminated is: “, list)
Output:
The updated list from which the desired elements are eliminated is: [56, 85, 65, 23]
Python One liner code to delete multiple even elements from a list
- # creating a list containing both even and odd numbers
- list = [56 , 85 , 97 , 65 , 23 , 12]
- # declaring the items to be removed from the list
- list = [ item for item in list if item % 2 == 0]
- # printing the updated list
- print (“The updated list from which the desired elements are eliminated is: “, list)
Output:
The updated list from which the desired elements are eliminated is: [56, 12]
Python One liner code to delete multiple odd elements from a list
- # creating a list containing both even and odd numbers
- list = [56 , 85 , 97 , 65 , 23 , 12]
- # declaring the items to be removed from the list
- list = [ item for item in list if item % 2 != 0]
- # printing the updated list
- print(“The updated list from which the desired elements are eliminated is: “, list)
Output:
The updated list from which the desired elements are eliminated is: [85, 97, 65, 23]
Python One liner code for deleting multiple elements from a list
# creating a list containing both even and odd numbers
list = [56 , 85 , 97 , 65 , 23 , 12]
# writing the code for deleting the unwanted items from the list
# m:n signifies the items between the index m and index n will be deleted
del list[2:5]
# printing the updated list
print(“The updated list from which the desired elements are eliminated is: “, list)
Output:
The updated list from which the desired elements are eliminated is: [56, 85, 12]
Python One liner code for deleting multiple elements from a list
# creating a list containing both even and odd numbers
list = [56 , 85 , 97 , 65 , 23 , 12]
# writing the code for deleting the unwanted items from the list
# m::n signifies the items from the index m will start getting removed from the list following the skip counting of n
del list[1::2]
# printing the updated list
print(“The updated list from which the desired elements are eliminated is: “, list)
Output:
The updated list from which the desired elements are eliminated is: [56, 97, 23]
5. How to Read Files?
In this Python program, list comprehension is used. In the first step, open a text file, and use a “for loop” function to read it line by line. In the end, remove all unnecessary space with the help of the “strip” function.
Python One liner code to read files
- list_example = [text.strip() for text in open(‘draft.txt’)]
- print(list_example)
- list(open(‘draft.txt’))
- ##Using “with” code will also close the file after use
- with open(“draft.txt”) as f: list_example=[text.strip() for text in f]
- print(list_example)
6. How to Write Data to File?
By using the code mentioned in the below program, first, a file draft.txt will be created if not existing, and then “This is an example for writing data to a file.” will be written in the file.
Python One liner code to write data to files
- with open(“draft.txt”,’a’,newline=’\n’) as f: f.write(“This is an example for writing data to a file.”)
Methods and their description:
Method for creating a new file:
- ‘x’ mode: If the user wants to create a new file, then this method can be implemented to serve the purpose. If the file already exists the executed operation fails.
Defaults Methods
- ‘r’ mode: This mode is used to open the file for reading, it is one of the default modes.
- ‘t’ mode: This mode is used to open the file in text mode, it is one of the default modes.
Other methods
- ‘w’ mode: This mode is used to open the file for writing. If the file is absent, then it automatically creates a new file, and if the file already exists, then the method makes the changes in the existing file.
- ‘x’ mode: If the user wants to create a new file, then this method can be implemented to serve the purpose. If the file already exists the executed operation fails.
- ‘a’ mode: This method is used to open the file in append mode. It automatically creates a new file if the file is absent.
- ‘b’ mode: This mode is used to open the file in binary mode.
- ‘+’ mode: if the user wants to open the file in both reading and writing (or updating the file) mode, this method can be implemented to serve the purpose.
7. How to Create Lists?
Python lists are considered one of the most versatile data types that allow users to work with multiple elements at once.
Python One liner code to create lists
- # creating a new list
- list = [i for i in range(0,30)]
- # printing the new created list
- print(“The new created list is:\n”, list)
Output:
The new created list is:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]Python One liner code to create lists
- # creating a new list
- list = list(range(0,30))
- # printing the new created list
- print(“The new created list is:\n”, list)
Output:
The new created list is:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]Python One liner code to create list of strings
- # creating a list of string
- list = [(“The names of the days are: “+i) for i in [‘Monday’, ‘Tuesday’,’Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’, ‘Sunday’]]
- # printing the new created list of string
- print(“The new created list of string is:\n”, list)
Output:
The newly created list of strings is:
[‘The names of the days are: Monday’, ‘The names of the days are: Tuesday’, ‘The names of the days are: Wednesday’, ‘The names of the days are: Thursday’, ‘The names of the days are: Friday’, ‘The names of the days are: Saturday’, ‘The names of the days are: Sunday’]8. How to Check the Presence of a Number in a List?
There are many situations when it is required to check the presence of a number in a given list. Users can use the following oneliner code example to easily check if the respective number is present in the list.
Python One liner code to check the availability of a number in the list
- # declaring the number to be checked for in the list
- num = 10
- # creating the list
- list = [i for i in range (0,30)]
- # printing the list
- print(“The list is: \n “, list)
- # writing the code to check the presence of the declared number in the list
- if num in list:
- # printing the result
- print(‘Yes, the number is present in the list.’)
Output:
The list is:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]Yes, the number is present in the list.
9. How To Find the Max Value?
The method can be used to find the maximum value.
Python One liner code to find the maximum value
- # creating the list
- list = [i for i in range (0,30)]
- # printing the list
- print(“The list is: \n “, list)
- # writing the code to find the maximum value, and then printing the maximum value
- print(max(list, key=lambda value: int(value)) )
Output:
The list is:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]29
10. How to Replace a Text with Some Other Text?
It is very simple to replace the text with some other text. The following approach can be used.
Python One liner code to replace a text with some other text
- String = “Python is a very simple language to learn. With proper dedication, it can be mastered in a couple of months.”.replace(“Python”, ‘C’)
- print(String)
Output:
C is a very simple language to learn. With proper dedication, it can be mastered in a couple of months.
Conclusion
We have shared all the useful and important information about how one-liners can be so effective and useful. It is a fantastic way of coding. Also, did you know that there is a newer version of Python?