SlideShare une entreprise Scribd logo
1  sur  18
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

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
 

Tendances (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 - ...
 

En vedette

бумаа
бумаабумаа
бумаа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
 

En vedette (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
 

Similaire à 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
 

Similaire à 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
 

Dernier

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
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
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
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
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
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
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
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 

Dernier (20)

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
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
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...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
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
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
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
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.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