SlideShare uma empresa Scribd logo
1 de 18
Baixar para ler 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

Mais conteúdo relacionado

Mais procurados

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
? ?
 

Mais procurados (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 - ...
 

Destaque (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
 

Semelhante a Python 1 liners

Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick tour
aztack
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
Amit Kapoor
 

Semelhante a 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
 

Último

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Último (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

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