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

What's hot

C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
Farhan Ab Rahman
 
Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd Study
Chris 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# developer
njpst8
 
Mcq cpup
Mcq cpupMcq cpup
Mcq cpup
tahir_ali786
 
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
Farhan Ab Rahman
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
Farhan Ab Rahman
 
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
Chris Ohk
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
Farhan Ab Rahman
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
Farhan Ab Rahman
 
C++ TUTORIAL 6
C++ TUTORIAL 6C++ TUTORIAL 6
C++ TUTORIAL 6
Farhan Ab Rahman
 
array, function, pointer, pattern matching
array, function, pointer, pattern matchingarray, function, pointer, pattern matching
array, function, pointer, pattern matching
Shakila Mahjabin
 
F# intro
F# introF# intro
F# intro
Alexey Raga
 
Static and const members
Static and const membersStatic and const members
Static and const members
mohamed sikander
 
C++ TUTORIAL 7
C++ TUTORIAL 7C++ TUTORIAL 7
C++ TUTORIAL 7
Farhan Ab Rahman
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
mohamed sikander
 
C++ TUTORIAL 10
C++ TUTORIAL 10C++ TUTORIAL 10
C++ TUTORIAL 10
Farhan Ab Rahman
 
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
Damien 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

Pedro buddhism
Pedro buddhismPedro buddhism
Pedro buddhism
penguinagent2
 
бумаа
бумаабумаа
бумааbumdari
 
Ezmath
EzmathEzmath
c-Energy+
c-Energy+c-Energy+
c-Energy+
pnc2011
 
Fp7 health2012 calls
Fp7 health2012 callsFp7 health2012 calls
Fp7 health2012 calls
pnc2011
 
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's
pnc2011
 
Meeting report
Meeting reportMeeting report
Meeting report
pnc2011
 
Pedro buddhism
Pedro buddhismPedro buddhism
Pedro buddhism
penguinagent2
 
Web 20-kb-mini-1216949509436115-8
Web 20-kb-mini-1216949509436115-8Web 20-kb-mini-1216949509436115-8
Web 20-kb-mini-1216949509436115-8
lorenatarcia
 
Waitronews june2011
Waitronews june2011Waitronews june2011
Waitronews june2011
pnc2011
 
K2 presentation
K2 presentationK2 presentation
K2 presentation
Matt Craven
 
Ancient egypt isaac gutierrez
Ancient egypt isaac gutierrezAncient egypt isaac gutierrez
Ancient egypt isaac gutierrez
isaacg123
 
523 assig 1
523 assig 1523 assig 1
523 assig 1
heyheypaula
 
K2 presentation
K2 presentationK2 presentation
K2 presentation
Matt Craven
 
The Sixth Extinction (2014)
The Sixth Extinction (2014)The Sixth Extinction (2014)
The Sixth Extinction (2014)
Jon Wyatt
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
Mr SMAK
 
Lecture 9 animation
Lecture 9 animationLecture 9 animation
Lecture 9 animation
Mr 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
 
pointers 1
pointers 1pointers 1
pointers 1
gaurav koriya
 
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
guest9006ab
 
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
Lukasz Dynowski
 
Java operators
Java operatorsJava operators
Java operators
Shehrevar Davierwala
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
Koen Handekyn
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
Arturo Herrero
 
C arrays
C arraysC arrays
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slide
jonycse
 
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
climatewarrior
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
Svet Ivantchev
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
Eduard Tomàs
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick tour
aztack
 
From Javascript To Haskell
From Javascript To HaskellFrom Javascript To Haskell
From Javascript To Haskell
ujihisa
 
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
 
Groovy
GroovyGroovy
Groovy
Zen Urban
 
Lập trình C
Lập trình CLập trình C
Lập trình C
Viet NguyenHoang
 
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
 
Music as data
Music as dataMusic as data
Music as data
John Vlachoyiannis
 

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

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 

Recently uploaded (20)

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 

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