SlideShare a Scribd company logo
Python 1-Liners
     @neizod
<http://about.me/neizod>
1-Liners?
1st Impression
C                Python

int i = 7;       i = 7
int j = 11;      j = 11

int temp;        i, j = j, i
temp = i;
i = j;
j = temp;
1-Liner = 1 Line of Code
JavaScript Array Summation

a = [1, 1, 2, 3, 5, 8, 13];

for(var s=i=0;(b=a[i++])?s+=b:alert(s););
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;
}
Let's Do It
Looping w/ List Comprehension
[x**2 for x in range(10)]


output:

[0, 1, 4, 9, 16, 25, 3, 49, 64, 81]
Sanitize w/ Map & Filter
[int(c) for c in '4f3c87' if c.isdigit()]


output:

[4, 3, 8, 7]
Use Shorthand If-Else
-x if x < 0 else x


r = [5, -2, 31, 13, -17]
[-x if x < 0 else x for x in r]


output:

[5, 2, 31, 13, 17]
Go For Functional
OOP doesn't return value!

a = [42, 8, 16, 15, 4, 23]
a.sort()
a.reverse()


Use this instead:

sorted([42, 8, 16, 15, 4, 23])[::-1]
Join Those String
' '.join(['hello', 'world'])


' < '.join(sorted('powerful'))


output:

'e < f < l < o < p < r < u < w'
Zip and Enumerate
[a+b for a, b in zip('hello', 'world')]


output:

['hw', 'eo', 'lr', 'll', 'od']
Hide Input w/ String Formatting
'{0} <3 {2}'.format('i', input(), 'u')


output:

'i <3 u'
Use Lambda
sorted([2, 1, 8, -7], key=lambda x: x**2)


output:

[1, 2, -7, 8]
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
Question?
Reference
●   Python 1-Liners - gist
●   Powerful Python One-Liners - wiki.python
●   Obfuscated one-liners in Python - effbot
●   Stupid lambda tricks - p-nand-q

More Related Content

PDF
Talk Code
PDF
C++ Programming - 14th Study
PDF
C++ Programming - 1st Study
PDF
C++ Programming - 4th Study
PDF
C++ Programming - 11th Study
PDF
C++ Programming - 2nd Study
PDF
Operator overloading
Talk Code
C++ Programming - 14th Study
C++ Programming - 1st Study
C++ Programming - 4th Study
C++ Programming - 11th Study
C++ Programming - 2nd Study
Operator overloading

What's hot (20)

PDF
C++ TUTORIAL 2
PDF
Data Structure - 2nd Study
PPTX
Introduction to F# for the C# developer
DOCX
Mcq cpup
PDF
Project_Euler_No_104_Pandigital_Fibonacci_ends
 
PDF
C++ TUTORIAL 8
PDF
C++ TUTORIAL 5
PPTX
Dr.nouh part summery
PDF
C++ Programming - 3rd Study
PDF
C++ TUTORIAL 3
PDF
C++ TUTORIAL 1
PDF
C++ TUTORIAL 6
PDF
array, function, pointer, pattern matching
PPTX
F# intro
PDF
Static and const members
PDF
C++ TUTORIAL 7
PDF
Stl algorithm-Basic types
PDF
C++ TUTORIAL 10
PPT
A gremlin in my graph confoo 2014
PDF
I have a stream - Insights in Reactive Programming - Jan Carsten Lohmuller - ...
C++ TUTORIAL 2
Data Structure - 2nd Study
Introduction to F# for the C# developer
Mcq cpup
Project_Euler_No_104_Pandigital_Fibonacci_ends
 
C++ TUTORIAL 8
C++ TUTORIAL 5
Dr.nouh part summery
C++ Programming - 3rd Study
C++ TUTORIAL 3
C++ TUTORIAL 1
C++ TUTORIAL 6
array, function, pointer, pattern matching
F# intro
Static and const members
C++ TUTORIAL 7
Stl algorithm-Basic types
C++ TUTORIAL 10
A gremlin in my graph confoo 2014
I have a stream - Insights in Reactive Programming - Jan Carsten Lohmuller - ...
Ad

Viewers also liked (18)

PPT
Pedro buddhism
ODP
бумаа
PPT
PDF
c-Energy+
PDF
Fp7 health2012 calls
PDF
Sound of Jura (2010)
PDF
Convocatorias abiertas 7 pm energía, medio ambiente, tic's
PDF
Meeting report
PPT
Pedro buddhism
PPT
Web 20-kb-mini-1216949509436115-8
PDF
Waitronews june2011
PPTX
K2 presentation
PPT
Ancient egypt isaac gutierrez
PPTX
523 assig 1
PDF
K2 presentation
PDF
The Sixth Extinction (2014)
PPT
Lecture 2
PPT
Lecture 9 animation
Pedro buddhism
бумаа
c-Energy+
Fp7 health2012 calls
Sound of Jura (2010)
Convocatorias abiertas 7 pm energía, medio ambiente, tic's
Meeting report
Pedro buddhism
Web 20-kb-mini-1216949509436115-8
Waitronews june2011
K2 presentation
Ancient egypt isaac gutierrez
523 assig 1
K2 presentation
The Sixth Extinction (2014)
Lecture 2
Lecture 9 animation
Ad

Similar to Python 1 liners (20)

KEY
Haskellで学ぶ関数型言語
PPS
pointers 1
PDF
C Code and the Art of Obfuscation
PPT
Python 101 language features and functional programming
PPT
Java operators
PDF
Functional programming in ruby
PDF
Functional Programming with Groovy
PPTX
python beginner talk slide
ODP
Python quickstart for programmers: Python Kung Fu
PDF
Introducción a Elixir
PPTX
EcmaScript unchained
PDF
Ruby Language - A quick tour
PDF
From Javascript To Haskell
PDF
Refactoring to Macros with Clojure
DOCX
C interview question answer 2
PPTX
Groovy
PPT
Lập trình C
PDF
What can be done with Java, but should better be done with Erlang (@pavlobaron)
Haskellで学ぶ関数型言語
pointers 1
C Code and the Art of Obfuscation
Python 101 language features and functional programming
Java operators
Functional programming in ruby
Functional Programming with Groovy
python beginner talk slide
Python quickstart for programmers: Python Kung Fu
Introducción a Elixir
EcmaScript unchained
Ruby Language - A quick tour
From Javascript To Haskell
Refactoring to Macros with Clojure
C interview question answer 2
Groovy
Lập trình C
What can be done with Java, but should better be done with Erlang (@pavlobaron)

Recently uploaded (20)

PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Basic Mud Logging Guide for educational purpose
PDF
Classroom Observation Tools for Teachers
PPTX
Institutional Correction lecture only . . .
PDF
Insiders guide to clinical Medicine.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Cell Structure & Organelles in detailed.
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Lesson notes of climatology university.
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Anesthesia in Laparoscopic Surgery in India
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Microbial diseases, their pathogenesis and prophylaxis
Basic Mud Logging Guide for educational purpose
Classroom Observation Tools for Teachers
Institutional Correction lecture only . . .
Insiders guide to clinical Medicine.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Cell Structure & Organelles in detailed.
STATICS OF THE RIGID BODIES Hibbelers.pdf
Lesson notes of climatology university.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Microbial disease of the cardiovascular and lymphatic systems
Final Presentation General Medicine 03-08-2024.pptx
RMMM.pdf make it easy to upload and study
Module 4: Burden of Disease Tutorial Slides S2 2025
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...

Python 1 liners

  • 1. Python 1-Liners @neizod
  • 4. 1st Impression C Python int i = 7; i = 7 int j = 11; j = 11 int temp; i, j = j, i temp = i; i = j; j = temp;
  • 5. 1-Liner = 1 Line of Code JavaScript Array Summation a = [1, 1, 2, 3, 5, 8, 13]; for(var s=i=0;(b=a[i++])?s+=b:alert(s););
  • 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; }
  • 8. Looping w/ List Comprehension [x**2 for x in range(10)] output: [0, 1, 4, 9, 16, 25, 3, 49, 64, 81]
  • 9. Sanitize w/ Map & Filter [int(c) for c in '4f3c87' if c.isdigit()] output: [4, 3, 8, 7]
  • 10. Use Shorthand If-Else -x if x < 0 else x r = [5, -2, 31, 13, -17] [-x if x < 0 else x for x in r] output: [5, 2, 31, 13, 17]
  • 11. Go For Functional OOP doesn't 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. Join Those String ' '.join(['hello', 'world']) ' < '.join(sorted('powerful')) output: 'e < f < l < o < p < r < u < w'
  • 13. Zip and Enumerate [a+b for a, b in zip('hello', 'world')] output: ['hw', 'eo', 'lr', 'll', 'od']
  • 14. Hide Input w/ String Formatting '{0} <3 {2}'.format('i', input(), 'u') output: 'i <3 u'
  • 15. Use Lambda sorted([2, 1, 8, -7], key=lambda x: x**2) output: [1, 2, -7, 8]
  • 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
  • 18. Reference ● Python 1-Liners - gist ● Powerful Python One-Liners - wiki.python ● Obfuscated one-liners in Python - effbot ● Stupid lambda tricks - p-nand-q