SlideShare a Scribd company logo
1 of 13
Download to read offline
{ x|x > 1, x ∈ N }
 List Comprehension - Quick Generation of List




NCCU, Department of Computer Science
Python Programming for Non-Programmer
List Comprehension
                                Math World:
   •   Components
                                   S = {2 · x|x ∈ N, x < 4}
       •   for

       •   if
                               Python World:
       •   else
                               S = [2*x for x in range(1,20) if x<4]



NCCU, Department of Computer Science
Python Programming for Non-Programmer             {x}  List Comprehension
                                                       Introduction
Case 1

   • Q: Generate a list contains square of
       numbers between 1 and 20.
   • A:
       [x**2 for x in range(1,20+1)]



NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x}  List Comprehension
                                             Introduction
Case II

   • Q: Give dictionary D as following:
       {‘a’:1, ‘b’:2, ‘c’:3, ‘d’:4, ‘e’:5}
       Please generate the letters who has the
       value which is greater than or equal 3.
   • A: for x in D.keys() if D[x]>=3]
     [x
       [k for k,v in D.items() if v>=3]


NCCU, Department of Computer Science
Python Programming for Non-Programmer     {x}
                                            List Comprehension
                                            Introduction
Practice


   • Review assignment IV
   • Warm up for assignment V


NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x}
                                          List Comprehension
                                          Introduction
Practice

   • Q:
       It’s about height of people, please design a
       program to summarize and statistic the
       data in data file given in TA session.
   • Data file: pastie.org/1393033
   • Architecture file: pastie.org/1393073
NCCU, Department of Computer Science
Python Programming for Non-Programmer     {x}  List Comprehension
                                               Introduction
Functions

   • Load from file
   • Summary of heights
   • Lookup a person’s height
   • Person above a height
   • Print height report (three in a row)
NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x} List Comprehension
                                            Introduction
Load from file

   • Load data from a file into a dictionary
   • Key: name
   • Value: height
   • Hint: as what last assignment does

NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x}   List Comprehension
                                              Introduction
Summary the Height

   • Print out the following three information:
       - Highest height
       - Lowest height
       - Average height
   • Hint: try to sort the data by values, and pick
       up the head and the tail of the sequence.


NCCU, Department of Computer Science
Python Programming for Non-Programmer    {x} List Comprehension
                                             Introduction
Lookup a Height


   • Ask a name and query the height of it
   • Hint: as what last assignment does


NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x}  List Comprehension
                                             Introduction
Person Above Height


   • Find out who is taller than specific height
   • Hint: use “List Comprehension”


NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x} List Comprehension
                                            Introduction
Print Report
   • Enumerate all people and their height each
       by each
   • Make output three records in row, like
       following:
       a, 152; b,180; c, 190;
       d, 161; e, 229; f, 191;
       g, 175; h, 159;
   • Hint: Embedded a counter to your loop
NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x}
                                          List Comprehension
                                          Introduction
That’s all, just do it.


NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x}
                                          List Comprehension
                                          Introduction

More Related Content

What's hot

class and objects
class and objectsclass and objects
class and objects
Payel Guria
 

What's hot (20)

Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
What is Python Lambda Function? Python Tutorial | Edureka
What is Python Lambda Function? Python Tutorial | EdurekaWhat is Python Lambda Function? Python Tutorial | Edureka
What is Python Lambda Function? Python Tutorial | Edureka
 
How to use Map() Filter() and Reduce() functions in Python | Edureka
How to use Map() Filter() and Reduce() functions in Python | EdurekaHow to use Map() Filter() and Reduce() functions in Python | Edureka
How to use Map() Filter() and Reduce() functions in Python | Edureka
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Python variables and data types.pptx
Python variables and data types.pptxPython variables and data types.pptx
Python variables and data types.pptx
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
Python
PythonPython
Python
 
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | EdurekaWhat is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
C++ classes
C++ classesC++ classes
C++ classes
 
Numeric Data types in Python
Numeric Data types in PythonNumeric Data types in Python
Numeric Data types in Python
 
class and objects
class and objectsclass and objects
class and objects
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 

Similar to Python-List comprehension

Similar to Python-List comprehension (20)

Data science : R Basics Harvard University
Data science : R Basics Harvard UniversityData science : R Basics Harvard University
Data science : R Basics Harvard University
 
Python Basis Tutorial
Python Basis TutorialPython Basis Tutorial
Python Basis Tutorial
 
Python ppt
Python pptPython ppt
Python ppt
 
Data analysis in R
Data analysis in RData analysis in R
Data analysis in R
 
PPT ON MACHINE LEARNING by Ragini Ratre
PPT ON MACHINE LEARNING by Ragini RatrePPT ON MACHINE LEARNING by Ragini Ratre
PPT ON MACHINE LEARNING by Ragini Ratre
 
Python_intro.ppt
Python_intro.pptPython_intro.ppt
Python_intro.ppt
 
Learning with classification and clustering, neural networks
Learning with classification and clustering, neural networksLearning with classification and clustering, neural networks
Learning with classification and clustering, neural networks
 
Deep Learning Introduction - WeCloudData
Deep Learning Introduction - WeCloudDataDeep Learning Introduction - WeCloudData
Deep Learning Introduction - WeCloudData
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
 
Deep Learning for Personalized Search and Recommender Systems
Deep Learning for Personalized Search and Recommender SystemsDeep Learning for Personalized Search and Recommender Systems
Deep Learning for Personalized Search and Recommender Systems
 
PYTHON
PYTHONPYTHON
PYTHON
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
Tips and tricks for data science projects with Python
Tips and tricks for data science projects with Python Tips and tricks for data science projects with Python
Tips and tricks for data science projects with Python
 
PyTorch 튜토리얼 (Touch to PyTorch)
PyTorch 튜토리얼 (Touch to PyTorch)PyTorch 튜토리얼 (Touch to PyTorch)
PyTorch 튜토리얼 (Touch to PyTorch)
 
Fast Feature Selection for Learning to Rank - ACM International Conference on...
Fast Feature Selection for Learning to Rank - ACM International Conference on...Fast Feature Selection for Learning to Rank - ACM International Conference on...
Fast Feature Selection for Learning to Rank - ACM International Conference on...
 
On the Necessity and Inapplicability of Python
On the Necessity and Inapplicability of PythonOn the Necessity and Inapplicability of Python
On the Necessity and Inapplicability of Python
 
On the necessity and inapplicability of python
On the necessity and inapplicability of pythonOn the necessity and inapplicability of python
On the necessity and inapplicability of python
 
Python001 training course_mumbai
Python001 training course_mumbaiPython001 training course_mumbai
Python001 training course_mumbai
 

More from Colin Su

Introduction to Google Compute Engine
Introduction to Google Compute EngineIntroduction to Google Compute Engine
Introduction to Google Compute Engine
Colin Su
 
Functional programming in Python
Functional programming in PythonFunctional programming in Python
Functional programming in Python
Colin Su
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code Lab
Colin Su
 
How to Speak Charms Like a Wizard
How to Speak Charms Like a WizardHow to Speak Charms Like a Wizard
How to Speak Charms Like a Wizard
Colin Su
 
房地產報告
房地產報告房地產報告
房地產報告
Colin Su
 
Facebook Python SDK - Introduction
Facebook Python SDK - IntroductionFacebook Python SDK - Introduction
Facebook Python SDK - Introduction
Colin Su
 

More from Colin Su (20)

Introduction to Google Compute Engine
Introduction to Google Compute EngineIntroduction to Google Compute Engine
Introduction to Google Compute Engine
 
Introduction to Google Cloud Endpoints: Speed Up Your API Development
Introduction to Google Cloud Endpoints: Speed Up Your API DevelopmentIntroduction to Google Cloud Endpoints: Speed Up Your API Development
Introduction to Google Cloud Endpoints: Speed Up Your API Development
 
Functional programming in Python
Functional programming in PythonFunctional programming in Python
Functional programming in Python
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code Lab
 
A Tour of Google Cloud Platform
A Tour of Google Cloud PlatformA Tour of Google Cloud Platform
A Tour of Google Cloud Platform
 
Introduction to Facebook JavaScript & Python SDK
Introduction to Facebook JavaScript & Python SDKIntroduction to Facebook JavaScript & Python SDK
Introduction to Facebook JavaScript & Python SDK
 
Introduction to MapReduce & hadoop
Introduction to MapReduce & hadoopIntroduction to MapReduce & hadoop
Introduction to MapReduce & hadoop
 
Introduction to Google App Engine
Introduction to Google App EngineIntroduction to Google App Engine
Introduction to Google App Engine
 
Django Deployer
Django DeployerDjango Deployer
Django Deployer
 
Introduction to Google - the most natural way to learn English (English Speech)
Introduction to Google - the most natural way to learn English (English Speech)Introduction to Google - the most natural way to learn English (English Speech)
Introduction to Google - the most natural way to learn English (English Speech)
 
How to Speak Charms Like a Wizard
How to Speak Charms Like a WizardHow to Speak Charms Like a Wizard
How to Speak Charms Like a Wizard
 
房地產報告
房地產報告房地產報告
房地產報告
 
Introduction to Git
Introduction to GitIntroduction to Git
Introduction to Git
 
Introduction to Facebook Javascript SDK (NEW)
Introduction to Facebook Javascript SDK (NEW)Introduction to Facebook Javascript SDK (NEW)
Introduction to Facebook Javascript SDK (NEW)
 
Introduction to Facebook Python API
Introduction to Facebook Python APIIntroduction to Facebook Python API
Introduction to Facebook Python API
 
Facebook Python SDK - Introduction
Facebook Python SDK - IntroductionFacebook Python SDK - Introduction
Facebook Python SDK - Introduction
 
Web Programming - 1st TA Session
Web Programming - 1st TA SessionWeb Programming - 1st TA Session
Web Programming - 1st TA Session
 
Nested List Comprehension and Binary Search
Nested List Comprehension and Binary SearchNested List Comprehension and Binary Search
Nested List Comprehension and Binary Search
 
Python-FileIO
Python-FileIOPython-FileIO
Python-FileIO
 
Python Dictionary
Python DictionaryPython Dictionary
Python Dictionary
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 

Python-List comprehension

  • 1. { x|x > 1, x ∈ N } List Comprehension - Quick Generation of List NCCU, Department of Computer Science Python Programming for Non-Programmer
  • 2. List Comprehension Math World: • Components S = {2 · x|x ∈ N, x < 4} • for • if Python World: • else S = [2*x for x in range(1,20) if x<4] NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 3. Case 1 • Q: Generate a list contains square of numbers between 1 and 20. • A: [x**2 for x in range(1,20+1)] NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 4. Case II • Q: Give dictionary D as following: {‘a’:1, ‘b’:2, ‘c’:3, ‘d’:4, ‘e’:5} Please generate the letters who has the value which is greater than or equal 3. • A: for x in D.keys() if D[x]>=3] [x [k for k,v in D.items() if v>=3] NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 5. Practice • Review assignment IV • Warm up for assignment V NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 6. Practice • Q: It’s about height of people, please design a program to summarize and statistic the data in data file given in TA session. • Data file: pastie.org/1393033 • Architecture file: pastie.org/1393073 NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 7. Functions • Load from file • Summary of heights • Lookup a person’s height • Person above a height • Print height report (three in a row) NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 8. Load from file • Load data from a file into a dictionary • Key: name • Value: height • Hint: as what last assignment does NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 9. Summary the Height • Print out the following three information: - Highest height - Lowest height - Average height • Hint: try to sort the data by values, and pick up the head and the tail of the sequence. NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 10. Lookup a Height • Ask a name and query the height of it • Hint: as what last assignment does NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 11. Person Above Height • Find out who is taller than specific height • Hint: use “List Comprehension” NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 12. Print Report • Enumerate all people and their height each by each • Make output three records in row, like following: a, 152; b,180; c, 190; d, 161; e, 229; f, 191; g, 175; h, 159; • Hint: Embedded a counter to your loop NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 13. That’s all, just do it. NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction