Upcoming SlideShare
×

# Python 1 liners

667 views

Published on

how to write python in just one line.

Published in: Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
667
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
4
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Python 1 liners

1. 1. Python 1-Liners @neizod
3. 3. 1-Liners?
4. 4. 1st ImpressionC Pythonint i = 7; i = 7int j = 11; j = 11int temp; i, j = j, itemp = i;i = j;j = temp;
5. 5. 1-Liner = 1 Line of CodeJavaScript Array Summationa = [1, 1, 2, 3, 5, 8, 13];for(var s=i=0;(b=a[i++])?s+=b:alert(s););
6. 6. But NOT This Kind of 1 Line!for(int i = 0; i < 100; i++) { printf("hellon"); if(i == 42) break; }Cause this is actually:for(int i = 0; i < 100; i++) { printf("hellon"); if(i == 42) break;}
7. 7. Lets Do It
8. 8. Looping w/ List Comprehension[x**2 for x in range(10)]output:[0, 1, 4, 9, 16, 25, 3, 49, 64, 81]
9. 9. Sanitize w/ Map & Filter[int(c) for c in 4f3c87 if c.isdigit()]output:[4, 3, 8, 7]
10. 10. Use Shorthand If-Else-x if x < 0 else xr = [5, -2, 31, 13, -17][-x if x < 0 else x for x in r]output:[5, 2, 31, 13, 17]
11. 11. Go For FunctionalOOP doesnt return value!a = [42, 8, 16, 15, 4, 23]a.sort()a.reverse()Use this instead:sorted([42, 8, 16, 15, 4, 23])[::-1]
12. 12. Join Those String .join([hello, world]) < .join(sorted(powerful))output:e < f < l < o < p < r < u < w
13. 13. Zip and Enumerate[a+b for a, b in zip(hello, world)]output:[hw, eo, lr, ll, od]
14. 14. Hide Input w/ String Formatting{0} <3 {2}.format(i, input(), u)output:i <3 u
15. 15. Use Lambdasorted([2, 1, 8, -7], key=lambda x: x**2)output:[1, 2, -7, 8]
16. 16. Go For Combinator(lambda i: (lambda f, a: f(f, a))( lambda r, n: n * (r(r, n-1) if n > 1 else 1), i))(10)output:3628800
17. 17. Question?
18. 18. Reference● Python 1-Liners - gist● Powerful Python One-Liners - wiki.python● Obfuscated one-liners in Python - effbot● Stupid lambda tricks - p-nand-q