This will make Python great
Original "25 Useful Python One-Liners That You Should Know" by Abhay Parashar
Before reading: every developer should have convenient and practical tools in their hands. One-liners, like syntactic sugar, are examples of smart coding that improves your productivity and quality in the eyes of your peers, but doesn't require any supernatural effort. I hope the translation of this article will be helpful.
, Python, , . Python.
1.
# a = 4 b = 5
a,b = b,a
# print(a,b) >> 5,4
- , , . - , .
2.
a,b,c = 4,5.5,'Hello'
#print(a,b,c) >> 4,5.5,hello
, . , var . . .
a,b,*c = [1,2,3,4,5]
print(a,b,c)
> 1 2 [3,4,5]
3.
, - .
a = [1,2,3,4,5,6]
s = sum([num for num in a if num%2 == 0])
print(s)
>> 12
4.
del
- , Python .
####
a = [1,2,3,4,5]
del a[1::2]
print(a)
>[1, 3, 5]
5.
lst = [line.strip() for line in open('data.txt')]
print(lst)
, . for
. strip
. , .
list(open('data.txt'))
## with
with open("data.txt") as f: lst=[line.strip() for line in f]
print(lst)
6.
with open("data.txt",'a',newline='\n') as f: f.write("Python is awesome")
data.txt, , Python is awesome
.
7.
lst = [i for i in range(0,10)]
print(lst)
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
lst = list(range(0,10))
print(lst)
, .
lst = [("Hello "+i) for i in ['Karl','Abhay','Zen']]
print(lst)
> ['Hello Karl', 'Hello Abhay', 'Hello Zen']
8. Mapping ,
. , , - , , . Python. map
, .
list(map(int,['1','2','3']))
> [1, 2, 3]
list(map(float,[1,2,3]))
> [1.0, 2.0, 3.0]
#
[float(i) for i in [1,2,3]]
> [1.0, 2.0, 3.0]
9.
, , . , .
#
{x**2 for x in range(10) if x%2==0}
> {0, 4, 16, 36, 64}
10. Fizz Buzz
, , 1 100. , , «Fizz» , «Buzz». ( , , , , FizzBuzz).
, if-else. , , , 10 . python, FizzBuzz .
['FizzBuzz' if i%3==0 and i%5==0 else 'Fizz' if i%3==0 else 'Buzz' if i%5==0 else i for i in range(1,20)]
1 20, , 3 5. , Fizz Buzz ( FizzBuzz).
11.
- , .
text = 'level'
ispalindrome = text == text[::-1]
ispalindrome
> True
12. , ,
lis = list(map(int, input().split()))
print(lis)
> 1 2 3 4 5 6 7 8
[1, 2, 3, 4, 5, 6, 7, 8]
13. -
- - .
- , __.
sqr = lambda x: x * x ##,
sqr(10)
> 100
14.
num = 5
if num in [1,2,3,4,5]:
print('present')
> present
15.
- , . python , .
n = 5
print('\n'.join('?' * i for i in range(1, n + 1)))
>
?
??
???
????
?????
16.
- .
import math
n = 6
math.factorial(n)
> 720
17.
- , ( ) . : 1, 1, 2, 3, 5, 8, 13 .. for .
fibo = [0,1]
[fibo.append(fibo[-2]+fibo[-1]) for i in range(5)]
fibo
> [0, 1, 1, 2, 3, 5, 8]
18.
- , 1. : 2,3,5,7 . . , .
list(filter(lambda x:all(x % y != 0 for y in range(2, x)), range(2, 13)))
> [2, 3, 5, 7, 11]
19.
findmax = lambda x,y: x if x > y else y
findmax(5,14)
> 14
max(5,14)
- .
20.
2 5 . , .
def scale(lst, x): return [i*x for i in lst]
scale([2,3,4], 2) ##
> [4,6,8]
21.
If you need to convert all rows to columns and vice versa, in python you can transpose a matrix in just one line of code using the zip function.
a=[[1,2,3],
[4,5,6],
[7,8,9]]
transpose = [list(i) for i in zip(*a)]
transpose
> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
22. Counting the occurrences of the pattern
This is an important and working method when we need to know the number of repetitions of a pattern in a text. Python has a re library that will do the job for us.
import re; len(re.findall('python','python is a programming language. python is python.'))
> 3
23. Replacing text with other text
"python is a programming language. python is python".replace("python",'Java')
> Java is a programming language. Java is Java
24. Simulated coin toss
This may not be that important, but it can be very useful when you need to generate a random selection from a given set of options.
import random; random.choice(['Head',"Tail"])
> Head
25. Generation of groups
groups = [(a, b) for a in ['a', 'b'] for b in [1, 2, 3]]
groups
> [('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3)]
I have shared all the helpful and important one-liners I know. If you know any more, share in the comments.