19
loading...
This website collects cookies to deliver better user experience
Please don't use these one liners in any interview, or in any production based code. The article is just for fun seeing the replacement of some programs/code block in one line. Some like list comprehension is a must know as well.
if 3 < 2:
var=21
else:
var=42
var = 21 if 3<2 else 42
>>> x = 42
>>> if x > 42:
>>> print("no")
>>> elif x == 42:
>>> print("yes")
>>> else:
>>> print("maybe")
yes
>>> print("no") if x > 42 else print("yes") if x == 42 else print("maybe")
yes
condition = True
if condition:
print('hi')
if condition: print('hello')
print('hello') if condition else None
def f(x):
return "hello "+ x
f = lambda x: "hello "+ x
f = exec("def f(x):\n return 'hello '+ x")
squares = []
for i in range(10):
squares.append(i**2)
squares=[i**2 for i in range(10)]
squares = []
for i in range(10):
if i%2==0:
squares.append(i**2)
squares = [i**2 for i in range(10) if i%2==0]
squares = []
for i in range(10):
if i%2==0:
squares.append(i**2)
else:
squares.append(False)
squares = [i**2 if i%2==0 else False for i in range(10)]
c=0
while c < 10:
if c!=5:
print(c)
else:
print("FIVE")
c+=1
while c < 10: c+=1; print(c) if c!=5 else print("FIVE")
>>> def swap(x,y):
x = x ^ y
y = x ^ y
x = x ^ y
return x, y
>>> swap(10,20)
(20,10)
>>> x, y = 10, 20
>>> x, y = y, x
(20, 10)
a="ONE"
b=2
c=3.001
a, b, c = "One", 2, 3.001
text = "Helllloooooo"
fileName = "hello.txt"
f=open(fileName, "a")
f.write(text)
f.close()
text = "Helllloooooo"
fileName = "hello.txt"
print(text, file=open(fileName, 'a'))
# Source - https://stackabuse.com/quicksort-in-python/
def partition(array, start, end):
pivot = array[start]
low = start + 1
high = end
while True:
while low <= high and array[high] >= pivot:
high = high - 1
while low <= high and array[low] <= pivot:
low = low + 1
if low <= high:
array[low], array[high] = array[high], array[low]
else:
break
array[start], array[high] = array[high], array[start]
return high
def quick_sort(array, start, end):
if start >= end:
return
p = partition(array, start, end)
quick_sort(array, start, p-1)
quick_sort(array, p+1, end)
array = [29,99,27,41,66,28,44,78,87,19,31,76,58,88,83,97,12,21,44]
quick_sort(array, 0, len(array) - 1)
print(array)
array = [29,99,27,41,66,28,44,78,87,19,31,76,58,88,83,97,12,21,44]
q = lambda l: q([x for x in l[1:] if x <= l[0]]) + [l[0]] + q([x for x in l if x > l[0]]) if l else []
print(q(array))
def fib(x):
if x <= 2:
return 1
return fib(x - 1) + fib(x - 2)
fib=lambda x: x if x<=1 else fib(x-1) + fib(x-2)
import http.server
import socketserver
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
python -m http.server 8000
iter1 = [1, 2, 3, 4]
iter2 = ['a', 'b', 'c']
for x in iter1:
for y in iter2:
print(x, y)
[print(x, y) for x in iter1 for y in iter2]
for i in range(1,5):
print(i, end=" ")
print(*range(1,5))
class School():
fun = {}
School = type('School', (object,), {'fun':{}})
command = input("> ")
while command != "quit":
print("You entered:", command)
while (command := input("> ")) != "quit": print("You entered:", command)