SlideShare a Scribd company logo
1 of 18
Download to read offline
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

What's hot

Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd StudyChris Ohk
 
Introduction to F# for the C# developer
Introduction to F# for the C# developerIntroduction to F# for the C# developer
Introduction to F# for the C# developernjpst8
 
Project_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_endsProject_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_ends? ?
 
C++ Programming - 3rd Study
C++ Programming - 3rd StudyC++ Programming - 3rd Study
C++ Programming - 3rd StudyChris Ohk
 
array, function, pointer, pattern matching
array, function, pointer, pattern matchingarray, function, pointer, pattern matching
array, function, pointer, pattern matchingShakila Mahjabin
 
A gremlin in my graph confoo 2014
A gremlin in my graph confoo 2014A gremlin in my graph confoo 2014
A gremlin in my graph confoo 2014Damien Seguy
 
I have a stream - Insights in Reactive Programming - Jan Carsten Lohmuller - ...
I have a stream - Insights in Reactive Programming - Jan Carsten Lohmuller - ...I have a stream - Insights in Reactive Programming - Jan Carsten Lohmuller - ...
I have a stream - Insights in Reactive Programming - Jan Carsten Lohmuller - ...Codemotion
 

What's hot (20)

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

Viewers also liked

бумаа
бумаабумаа
бумааbumdari
 
c-Energy+
c-Energy+c-Energy+
c-Energy+pnc2011
 
Fp7 health2012 calls
Fp7 health2012 callsFp7 health2012 calls
Fp7 health2012 callspnc2011
 
Sound of Jura (2010)
Sound of Jura  (2010)Sound of Jura  (2010)
Sound of Jura (2010)Jon Wyatt
 
Convocatorias abiertas 7 pm energía, medio ambiente, tic's
Convocatorias abiertas 7 pm   energía, medio ambiente, tic'sConvocatorias abiertas 7 pm   energía, medio ambiente, tic's
Convocatorias abiertas 7 pm energía, medio ambiente, tic'spnc2011
 
Meeting report
Meeting reportMeeting report
Meeting reportpnc2011
 
Web 20-kb-mini-1216949509436115-8
Web 20-kb-mini-1216949509436115-8Web 20-kb-mini-1216949509436115-8
Web 20-kb-mini-1216949509436115-8lorenatarcia
 
Waitronews june2011
Waitronews june2011Waitronews june2011
Waitronews june2011pnc2011
 
Ancient egypt isaac gutierrez
Ancient egypt isaac gutierrezAncient egypt isaac gutierrez
Ancient egypt isaac gutierrezisaacg123
 
The Sixth Extinction (2014)
The Sixth Extinction (2014)The Sixth Extinction (2014)
The Sixth Extinction (2014)Jon Wyatt
 
Lecture 2
Lecture 2Lecture 2
Lecture 2Mr SMAK
 
Lecture 9 animation
Lecture 9 animationLecture 9 animation
Lecture 9 animationMr SMAK
 

Viewers also liked (18)

Pedro buddhism
Pedro buddhismPedro buddhism
Pedro buddhism
 
бумаа
бумаабумаа
бумаа
 
Ezmath
EzmathEzmath
Ezmath
 
c-Energy+
c-Energy+c-Energy+
c-Energy+
 
Fp7 health2012 calls
Fp7 health2012 callsFp7 health2012 calls
Fp7 health2012 calls
 
Sound of Jura (2010)
Sound of Jura  (2010)Sound of Jura  (2010)
Sound of Jura (2010)
 
Convocatorias abiertas 7 pm energía, medio ambiente, tic's
Convocatorias abiertas 7 pm   energía, medio ambiente, tic'sConvocatorias abiertas 7 pm   energía, medio ambiente, tic's
Convocatorias abiertas 7 pm energía, medio ambiente, tic's
 
Meeting report
Meeting reportMeeting report
Meeting report
 
Pedro buddhism
Pedro buddhismPedro buddhism
Pedro buddhism
 
Web 20-kb-mini-1216949509436115-8
Web 20-kb-mini-1216949509436115-8Web 20-kb-mini-1216949509436115-8
Web 20-kb-mini-1216949509436115-8
 
Waitronews june2011
Waitronews june2011Waitronews june2011
Waitronews june2011
 
K2 presentation
K2 presentationK2 presentation
K2 presentation
 
Ancient egypt isaac gutierrez
Ancient egypt isaac gutierrezAncient egypt isaac gutierrez
Ancient egypt isaac gutierrez
 
523 assig 1
523 assig 1523 assig 1
523 assig 1
 
K2 presentation
K2 presentationK2 presentation
K2 presentation
 
The Sixth Extinction (2014)
The Sixth Extinction (2014)The Sixth Extinction (2014)
The Sixth Extinction (2014)
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture 9 animation
Lecture 9 animationLecture 9 animation
Lecture 9 animation
 

Similar to Python 1 liners

Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語ikdysfm
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscationguest9006ab
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programmingLukasz Dynowski
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in rubyKoen Handekyn
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with GroovyArturo Herrero
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slidejonycse
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fuclimatewarrior
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a ElixirSvet Ivantchev
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchainedEduard Tomàs
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick touraztack
 
From Javascript To Haskell
From Javascript To HaskellFrom Javascript To Haskell
From Javascript To Haskellujihisa
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2Amit Kapoor
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)Pavlo Baron
 

Similar to Python 1 liners (20)

Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
pointers 1
pointers 1pointers 1
pointers 1
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
 
Java operators
Java operatorsJava operators
Java operators
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
C arrays
C arraysC arrays
C arrays
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slide
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick tour
 
From Javascript To Haskell
From Javascript To HaskellFrom Javascript To Haskell
From Javascript To Haskell
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
Groovy
GroovyGroovy
Groovy
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
Music as data
Music as dataMusic as data
Music as data
 

Recently uploaded

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 

Recently uploaded (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

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