SlideShare a Scribd company logo
2
Most read
9
Most read
11
Most read
{ 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

PPTX
Python Lambda Function
PPTX
Python basics
PPTX
Python Functions
PDF
What is Python Lambda Function? Python Tutorial | Edureka
PPTX
Java abstract class & abstract methods
PDF
Arrays in Java
PPTX
This keyword in java
PPTX
Decision making and branching
Python Lambda Function
Python basics
Python Functions
What is Python Lambda Function? Python Tutorial | Edureka
Java abstract class & abstract methods
Arrays in Java
This keyword in java
Decision making and branching

What's hot (20)

ODP
Python Modules
PPTX
Python dictionary
PPTX
Chapter 05 classes and objects
PPT
Java static keyword
PPTX
Inline function
PPTX
Method overloading
PPTX
Hashing in datastructure
PPTX
Inter Thread Communicationn.pptx
PDF
List comprehensions
PDF
Introduction to NumPy (PyData SV 2013)
PDF
Python set
PPTX
Templates in C++
PDF
Python-02| Input, Output & Import
PDF
List,tuple,dictionary
PPT
Asymptotic notation
PDF
Applications of stack
PPTX
CLASS OBJECT AND INHERITANCE IN PYTHON
PPTX
Python dictionary
PPTX
Recursion
PPTX
Python: Modules and Packages
Python Modules
Python dictionary
Chapter 05 classes and objects
Java static keyword
Inline function
Method overloading
Hashing in datastructure
Inter Thread Communicationn.pptx
List comprehensions
Introduction to NumPy (PyData SV 2013)
Python set
Templates in C++
Python-02| Input, Output & Import
List,tuple,dictionary
Asymptotic notation
Applications of stack
CLASS OBJECT AND INHERITANCE IN PYTHON
Python dictionary
Recursion
Python: Modules and Packages
Ad

Similar to Python-List comprehension (20)

PPTX
Python.pptx
PDF
python_lab_manual_final (1).pdf
PDF
xii cs practicals class 12 computer science.pdf
PPTX
ANSHUL RANA - PROGRAM FILE.pptx
PDF
GE3171-PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
DOCX
cs class 12 project computer science .docx
PPTX
Python Homework Help
PDF
Python Lab manual program for BE First semester (all department
PPTX
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
PDF
Sample Program file class 11.pdf
PDF
Programming with Python - Adv.
DOCX
python file for easy way practicle programs
PDF
Python for High School Programmers
PDF
solution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdf
PDF
Xi CBSE Computer Science lab programs
PPTX
Python programming workshop session 3
PDF
paython practical
PDF
03 standard Data Types
PDF
1 Write a python code that initializes a list called Number.pdf
PDF
python practicals-solution-2019-20-class-xii.pdf
Python.pptx
python_lab_manual_final (1).pdf
xii cs practicals class 12 computer science.pdf
ANSHUL RANA - PROGRAM FILE.pptx
GE3171-PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
cs class 12 project computer science .docx
Python Homework Help
Python Lab manual program for BE First semester (all department
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Sample Program file class 11.pdf
Programming with Python - Adv.
python file for easy way practicle programs
Python for High School Programmers
solution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdf
Xi CBSE Computer Science lab programs
Python programming workshop session 3
paython practical
03 standard Data Types
1 Write a python code that initializes a list called Number.pdf
python practicals-solution-2019-20-class-xii.pdf
Ad

More from Colin Su (20)

PDF
Introduction to Google Compute Engine
PDF
Introduction to Google Cloud Endpoints: Speed Up Your API Development
PDF
Functional programming in Python
PDF
Web2py Code Lab
PDF
A Tour of Google Cloud Platform
PDF
Introduction to Facebook JavaScript & Python SDK
PDF
Introduction to MapReduce & hadoop
PDF
Introduction to Google App Engine
PDF
Django Deployer
PDF
Introduction to Google - the most natural way to learn English (English Speech)
PDF
How to Speak Charms Like a Wizard
PDF
房地產報告
PDF
Introduction to Git
PDF
Introduction to Facebook Javascript SDK (NEW)
PDF
Introduction to Facebook Python API
PDF
Facebook Python SDK - Introduction
PDF
Web Programming - 1st TA Session
PDF
Nested List Comprehension and Binary Search
PDF
Python-FileIO
KEY
Python Dictionary
Introduction to Google Compute Engine
Introduction to Google Cloud Endpoints: Speed Up Your API Development
Functional programming in Python
Web2py Code Lab
A Tour of Google Cloud Platform
Introduction to Facebook JavaScript & Python SDK
Introduction to MapReduce & hadoop
Introduction to Google App Engine
Django Deployer
Introduction to Google - the most natural way to learn English (English Speech)
How to Speak Charms Like a Wizard
房地產報告
Introduction to Git
Introduction to Facebook Javascript SDK (NEW)
Introduction to Facebook Python API
Facebook Python SDK - Introduction
Web Programming - 1st TA Session
Nested List Comprehension and Binary Search
Python-FileIO
Python Dictionary

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Spectroscopy.pptx food analysis technology
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
cuic standard and advanced reporting.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PPT
Teaching material agriculture food technology
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Approach and Philosophy of On baking technology
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Encapsulation theory and applications.pdf
Empathic Computing: Creating Shared Understanding
The Rise and Fall of 3GPP – Time for a Sabbatical?
Encapsulation_ Review paper, used for researhc scholars
Spectroscopy.pptx food analysis technology
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
cuic standard and advanced reporting.pdf
Understanding_Digital_Forensics_Presentation.pptx
MYSQL Presentation for SQL database connectivity
Teaching material agriculture food technology
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”
Network Security Unit 5.pdf for BCA BBA.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Programs and apps: productivity, graphics, security and other tools
Dropbox Q2 2025 Financial Results & Investor Presentation
Approach and Philosophy of On baking technology
Unlocking AI with Model Context Protocol (MCP)
Encapsulation theory and applications.pdf

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