Skip to content

10 Python One Liner You Must Know

Ten beginner-friendly Python one liner that are fun to know and easy to use.


Here are 10 beginner-friendly Python one liner you should know.

Careful

A one-liner does not alwasy mean it's the best choice. You should always prefer readability over shortening your code! However, these one-liners are fun to know and some of them are pretty useful.

You can also watch the video here:

1. Swap variables

You can swap variables without the need of a third temporary variable:

a = 5
b = 10

a, b = b, a

print(a, b) # 10, 5

2. List comprehension

Instead of a for loop that appends the items, you can create a list right away with the list comprehension syntax. It also allows for if-statements:

squares = [i * i for i in range(5)]
# [0, 1, 4, 9, 16]

squares = [i * i for i in range(5) if i % 2 == 0]
# [0, 4, 16]

3. Ternary operator (if-else)

The ternary operator is an if-else statement in one line:

var = 42 if 3 > 2 else 999
# 42

4. Print without new lines

If you only want to print the items, and not the whole list, you can unpack the items with the * operator and print it in one line:

# No need to do this:
data = [0, 1, 2, 3, 4, 5]
for i in data:
    print(i, end=" ")
print()

# One-liner
print(*data)
# 0 1 2 3 4 5

5. Days left in year

Some fun calculations. Determine how many days are left in this year, e.g., to work on your goals ;

import datetime;print((datetime.date(2023,1,1)-datetime.date.today()).days)
# 36

You can also run it from the terminal using python -c "statement", or even create an alias in your configuration so that you can easily call it:

>> python -c "import datetime;print((datetime.date(2023,1,1)-datetime.date.today()).days)"
36

>> alias daysleft='python -c "import datetime;print((datetime.date(2023,1,1)-datetime.date.today()).days)"'

>> daysleft
36

6. Reversing a List

You can reverse a list in one line with list slicing and a step of -1:

a = [1, 2, 3, 4, 5, 6]
a = a[::-1]
# [6, 5, 4, 3, 2, 1]

7. Multiple variable assignments

You can assign multiple variables of different data types in one line:

a, b, c = 3, 99, 'Python'

print(a, b, c) # 3, 99, 'Python'

8. Space separated numbers to integer list

You can read a string of space separated numbers into an integer list using the split()methond combined with the map() function:

user_input = "1 2 3 4 5 6"

my_list = list(map(int, user_input.split()))
# [1, 2, 3, 4, 5, 6]

9. Reading a file into list

Using list comprehension again, you can read all lines of a file into a list.

(Note that this might not properly close the file afterward, but it shouldn't cause harm in this example).

my_list = [line.strip() for line in open('filename.txt', 'r')]

10. HTTP server

Run this in your terminal to start an HTTP server:

$ python -m http.server

FREE VS Code / PyCharm Extensions I Use

鉁 Write cleaner code with Sourcery, instant refactoring suggestions: Link*

* This is an affiliate link. By clicking on it you will not have any additional costs. Instead, you will support my project. Thank you! 馃檹