0% found this document useful (0 votes)
16 views11 pages

50 Essential Python One-Liners

Uploaded by

studiesdbtest
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

50 Essential Python One-Liners

Uploaded by

studiesdbtest
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

an

ith
ad
ig
ita
l
50 Python One-Liners

🔹 1. Reverse a string

l
ita
print("Python"[::-1])

🔹 2. Swap two variables


ig
a, b = 5, 10; a, b = b, a

🔹 3. Check if a number is even


ad
print(10 % 2 == 0)

🔹 4. Find factorial
ith

import math; print([Link](5))

🔹 5. Flatten a list of lists


an

print([item for sub in [[1,2],[3,4]] for


item in sub])

🔹 6. Check if a string is a palindrome


print("madam" == "madam"[::-1])
🔹 7. Sum of a list
print(sum([1, 2, 3, 4]))

🔹 8. Find max of a list

l
ita
print(max([3, 5, 7, 2]))

🔹 9. Count frequency of elements


ig
from collections import Counter;
ad
print(Counter("banana"))

🔹 10. Merge two dictionaries


print({**{'a':1}, **{'b':2}})
ith

🔹 11. Get unique elements from list


an

print(list(set([1, 2, 2, 3])))

🔹 12. Convert list to comma string


print(",".join(map(str, [1,2,3])))
🔹 13. Find common elements
print(set([1,2,3]) & set([2,3,4]))

🔹 14. Check if all elements are True

l
ita
print(all([True, True, False]))

🔹 15. Check if any element is True


ig
print(any([False, False, True]))

🔹 16. List comprehension with condition


ad
print([x for x in range(10) if x % 2 ==
ith

0])

🔹 17. Read a file


an

print(open("[Link]").read())

🔹 18. Get current date


from datetime import date;
print([Link]())
🔹 19. Generate random number
import random; print([Link](1,
100))

l
🔹 20. Create a dictionary from two lists

ita
print(dict(zip(["a", "b"], [1, 2])))

ig
🔹 21. Square each number in a list
ad
print([x**2 for x in range(5)])

🔹 22. Remove duplicates while preserving order


ith

print(list([Link]([1,2,2,3])))

🔹 23. Sort a list in descending order


an

print(sorted([4, 2, 7], reverse=True))

🔹 24. Find length of a string


print(len("hello"))
🔹 25. Replace substring
print("hello world".replace("world",
"Python"))

l
🔹 26. Check if key in dict

ita
print("a" in {"a":1, "b":2})

🔹 27. Create a list of n zeros


ig
print([0]*5)
ad
🔹 28. Convert string to int
ith

print(int("100"))

🔹 29. Filter even numbers


an

print(list(filter(lambda x: x%2==0,
range(10))))

🔹 30. Square with map


print(list(map(lambda x: x**2,
range(5))))
🔹 31. List of ASCII codes

l
ita
print([ord(c) for c in "abc"])

🔹 32. ASCII to characters


ig
print([chr(i) for i in [97, 98, 99]])

🔹 33. Convert int to binary


ad
print(bin(10))

🔹 34. Convert binary to int


ith

print(int("1010", 2))

🔹 35. Get list of even numbers


an

print([i for i in range(20) if i % 2 ==


0])

🔹 36. Check prime number (basic)


print(all(7 % i for i in range(2, 7)))
🔹 37. Read first line of file

l
print(open("[Link]").readline())

🔹 38. Get file extension

ita
print("[Link]".split('.')[-1])

🔹 39. Count vowels in a string


ig
print(sum(1 for c in "hello" if c in
ad
"aeiou"))

🔹 40. Capitalize each word


ith

print("hello world".title())
an

🔹 41. Filter positive numbers


print([x for x in [-1, 2, -3, 4] if x >
0])
🔹 42. Reverse list

l
print([1, 2, 3][::-1])

ita
🔹 43. Multiply all elements
from functools import reduce;

ig
print(reduce(lambda x,y: x*y, [1,2,3,4]))

🔹 44. Sort list of tuples by second value


ad
print(sorted([(1,3),(2,2),(3,1)],
key=lambda x: x[1]))

🔹 45. Find intersection of two lists


ith

print(list(set([1,2,3]) & set([2,3,4])))

🔹 46. Remove punctuation


an

import string;
print("hello!".translate([Link](''
, '', [Link])))
🔹 47. Generate n random numbers

l
import random;

ita
print([[Link](1,10) for _ in
range(5)])

🔹 48. Merge two lists ig


print([*["a", "b"], *["c", "d"]])
ad
🔹 49. Check if list is empty
ith

print(not [])

🔹 50. Get index of item in list


an

print(["a", "b", "c"].index("b"))


an
ith
ad
ig
ita
l

You might also like