SlideShare a Scribd company logo
Python
Lecture 12Lecture 12
- Ravi Kiran Khareedi
CSV MODULE (Continued)
Using Field Names
• CSV module includes classes for working with
rows as dictionaries so that the fields can be
named.
• The DictReader and DictWriter classes
translate rows to dictionaries instead of lists.
Keys for the dictionary can be passed in, or
inferred from the first row in the input.
DictReader
import csv
f = open('sampleread.csv', 'rt')
try:try:
reader = csv.DictReader(f)
for row in reader:
print row
finally:
f.close()
DictWriter
• The main difference is that the fieldnames
have to be entered as a separate list.
• The fieldnames have to match all of the keys• The fieldnames have to match all of the keys
in the dictionary array but can be ordered
differently
Dict Writer
import csv
test_array = []
test_array.append({'fruit': 'apple', 'quantity': 5, 'color': 'red'});
test_array.append({'fruit': 'pear', 'quantity': 8, 'color': 'green'});
test_array.append({'fruit': 'banana', 'quantity': 3, 'color': 'yellow'});
test_array.append({'fruit': 'orange', 'quantity': 11, 'color': 'orange'});
test = {"fruit":"fruit","quantity":"quantity","color":"Colors"}test = {"fruit":"fruit","quantity":"quantity","color":"Colors"}
fieldnames = ['fruit', 'quantity', 'color']
test_file = open('test2.csv','wb')
csvwriter = csv.DictWriter(test_file, delimiter=',', fieldnames=fieldnames)
#csvwriter.writerow(dict((fn,fn+"1") for fn in fieldnames))
csvwriter.writerow(test);
for row in test_array:
csvwriter.writerow(row)
test_file.close()
Advanced Data Structures: Stacks
Data structure to store the elements,
where the main operation is to push,
pop, display.
push – to add an element to the endpush – to add an element to the end
pop – to remove an element from the end
Display – to display the elements
Follows Last In First Out (LIFO) concept
Data structure to store the elements, where the
main operation is to enqueue, dequeue,
display.
enqueue – to add an element to the back
Advanced Data Structures: Queues
enqueue – to add an element to the back
dequeue – to remove an element from the front
Display – to display the elements
Follows First In First Out (FIFO) concept
Stacks and Queues
Ref: http://pakitgroup.blogspot.de/p/data-
structure.html
TASK
• Write a program to implement Stack
operations
• Write a program to implement Queue
operations
TASK
• Given 3 int values, a b c, return their sum.
However, if one of the values is 13 then it does
not count towards the sum and values to its
right do not count. So for example, consider
(a,b,c) if b is 13, then both b and c do not(a,b,c) if b is 13, then both b and c do not
count.
lucky_sum(1, 2, 3) → 6
lucky_sum(1, 2, 13) → 3
lucky_sum(1, 13, 3) → 1
TASK
• Given three ints, a b c, one of them is small, one is
medium and one is large. Return true if the three
values are evenly spaced, so the difference between
small and medium is the same as the difference
between medium and large.
• even_space(2,4,6) – True
• even_space(4,5,2) – False
• even_space(4,6,2) – True
(Can make use of any python functions)
References
• www.codingbat.com
• docs.python.org
• http://www.gadzmo.com/python/reading-
and-writing-csv-files-with-python-dictreader-and-writing-csv-files-with-python-dictreader-
and-dictwriter/

More Related Content

What's hot

Import and Export Excel files using XLConnect in R Studio
Import and Export Excel files using XLConnect in R StudioImport and Export Excel files using XLConnect in R Studio
Import and Export Excel files using XLConnect in R Studio
Rupak Roy
 
Import and Export Excel Data using openxlsx in R Studio
Import and Export Excel Data using openxlsx in R StudioImport and Export Excel Data using openxlsx in R Studio
Import and Export Excel Data using openxlsx in R Studio
Rupak Roy
 
Net framework
Net frameworkNet framework
Net framework
Abhishek Mukherjee
 
Unit 4
Unit 4Unit 4
strings in c language and its importance
strings in c language and its importancestrings in c language and its importance
strings in c language and its importance
sirmanohar
 
23 priority queue
23 priority queue23 priority queue
23 priority queue
Godo Dodo
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
sumitbardhan
 
Data Analysis packages
Data Analysis packagesData Analysis packages
Data Analysis packages
Devashish Kumar
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
P M Patil
 
Mule data weave_4
Mule data weave_4Mule data weave_4
Mule data weave_4
kunal vishe
 
Java Tutorial Lab 7
Java Tutorial Lab 7Java Tutorial Lab 7
Java Tutorial Lab 7
Berk Soysal
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
DevoAjit Gupta
 
Array and string
Array and stringArray and string
Array and string
prashant chelani
 
Namespace in C++ Programming Language
Namespace in C++ Programming LanguageNamespace in C++ Programming Language
Namespace in C++ Programming Language
Himanshu Choudhary
 
Arrays
ArraysArrays
Arrays
Neeru Mittal
 
Aaa ped-6-Data manipulation: Data Files, and Data Cleaning & Preparation
Aaa ped-6-Data manipulation:  Data Files, and Data Cleaning & PreparationAaa ped-6-Data manipulation:  Data Files, and Data Cleaning & Preparation
Aaa ped-6-Data manipulation: Data Files, and Data Cleaning & Preparation
AminaRepo
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
GC University Faisalabad
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Streams in Java 8
Streams in Java 8Streams in Java 8
Streams in Java 8
Tobias Coetzee
 

What's hot (20)

Import and Export Excel files using XLConnect in R Studio
Import and Export Excel files using XLConnect in R StudioImport and Export Excel files using XLConnect in R Studio
Import and Export Excel files using XLConnect in R Studio
 
Import and Export Excel Data using openxlsx in R Studio
Import and Export Excel Data using openxlsx in R StudioImport and Export Excel Data using openxlsx in R Studio
Import and Export Excel Data using openxlsx in R Studio
 
Net framework
Net frameworkNet framework
Net framework
 
Unit 4
Unit 4Unit 4
Unit 4
 
strings in c language and its importance
strings in c language and its importancestrings in c language and its importance
strings in c language and its importance
 
23 priority queue
23 priority queue23 priority queue
23 priority queue
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
 
Data Analysis packages
Data Analysis packagesData Analysis packages
Data Analysis packages
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
Mule data weave_4
Mule data weave_4Mule data weave_4
Mule data weave_4
 
Java Tutorial Lab 7
Java Tutorial Lab 7Java Tutorial Lab 7
Java Tutorial Lab 7
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
Array and string
Array and stringArray and string
Array and string
 
Namespace in C++ Programming Language
Namespace in C++ Programming LanguageNamespace in C++ Programming Language
Namespace in C++ Programming Language
 
Arrays
ArraysArrays
Arrays
 
Aaa ped-6-Data manipulation: Data Files, and Data Cleaning & Preparation
Aaa ped-6-Data manipulation:  Data Files, and Data Cleaning & PreparationAaa ped-6-Data manipulation:  Data Files, and Data Cleaning & Preparation
Aaa ped-6-Data manipulation: Data Files, and Data Cleaning & Preparation
 
Strings
StringsStrings
Strings
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Strings in C
Strings in CStrings in C
Strings in C
 
Streams in Java 8
Streams in Java 8Streams in Java 8
Streams in Java 8
 

Similar to Python - Lecture 12

C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
Abhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
GauravPandey43518
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
ParveenShaik21
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
NishantKumar1179
 
ADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASADADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASAD
PADYALAMAITHILINATHA
 
Lecture 9.pptx
Lecture 9.pptxLecture 9.pptx
Lecture 9.pptx
MathewJohnSinoCruz
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
Guy Lebanon
 
Matlab Manual
Matlab ManualMatlab Manual
MatlabIntro (1).ppt
MatlabIntro (1).pptMatlabIntro (1).ppt
MatlabIntro (1).ppt
AkashSingh728626
 
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docxAssg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
festockton
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
C++ Homework Help
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdf
MemeMiner
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
chestialtaff
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
Akash Gawali
 
Lab Manual-OOP.pdf
Lab Manual-OOP.pdfLab Manual-OOP.pdf
Lab Manual-OOP.pdf
MUNAZARAZZAQELEA
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
vikrammutneja1
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
GauravPatil318
 

Similar to Python - Lecture 12 (20)

C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
 
ADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASADADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASAD
 
Lecture 9.pptx
Lecture 9.pptxLecture 9.pptx
Lecture 9.pptx
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
 
Matlab Manual
Matlab ManualMatlab Manual
Matlab Manual
 
MatlabIntro (1).ppt
MatlabIntro (1).pptMatlabIntro (1).ppt
MatlabIntro (1).ppt
 
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docxAssg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdf
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
Lab Manual-OOP.pdf
Lab Manual-OOP.pdfLab Manual-OOP.pdf
Lab Manual-OOP.pdf
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Matlab lec1
Matlab lec1Matlab lec1
Matlab lec1
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 

More from Ravi Kiran Khareedi

Python - Lecture 10
Python - Lecture 10Python - Lecture 10
Python - Lecture 10
Ravi Kiran Khareedi
 
Python - Lecture 9
Python - Lecture 9Python - Lecture 9
Python - Lecture 9
Ravi Kiran Khareedi
 
Python - Lecture 8
Python - Lecture 8Python - Lecture 8
Python - Lecture 8
Ravi Kiran Khareedi
 
Python - Lecture 7
Python - Lecture 7Python - Lecture 7
Python - Lecture 7
Ravi Kiran Khareedi
 
Python - Lecture 6
Python - Lecture 6Python - Lecture 6
Python - Lecture 6
Ravi Kiran Khareedi
 
Python - Lecture 5
Python - Lecture 5Python - Lecture 5
Python - Lecture 5
Ravi Kiran Khareedi
 
Python - Lecture 4
Python - Lecture 4Python - Lecture 4
Python - Lecture 4
Ravi Kiran Khareedi
 
Python - Lecture 3
Python - Lecture 3Python - Lecture 3
Python - Lecture 3
Ravi Kiran Khareedi
 
Python - Lecture 2
Python - Lecture 2Python - Lecture 2
Python - Lecture 2
Ravi Kiran Khareedi
 
Python - Lecture 1
Python - Lecture 1Python - Lecture 1
Python - Lecture 1
Ravi Kiran Khareedi
 

More from Ravi Kiran Khareedi (10)

Python - Lecture 10
Python - Lecture 10Python - Lecture 10
Python - Lecture 10
 
Python - Lecture 9
Python - Lecture 9Python - Lecture 9
Python - Lecture 9
 
Python - Lecture 8
Python - Lecture 8Python - Lecture 8
Python - Lecture 8
 
Python - Lecture 7
Python - Lecture 7Python - Lecture 7
Python - Lecture 7
 
Python - Lecture 6
Python - Lecture 6Python - Lecture 6
Python - Lecture 6
 
Python - Lecture 5
Python - Lecture 5Python - Lecture 5
Python - Lecture 5
 
Python - Lecture 4
Python - Lecture 4Python - Lecture 4
Python - Lecture 4
 
Python - Lecture 3
Python - Lecture 3Python - Lecture 3
Python - Lecture 3
 
Python - Lecture 2
Python - Lecture 2Python - Lecture 2
Python - Lecture 2
 
Python - Lecture 1
Python - Lecture 1Python - Lecture 1
Python - Lecture 1
 

Recently uploaded

When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 

Recently uploaded (20)

When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 

Python - Lecture 12

  • 1. Python Lecture 12Lecture 12 - Ravi Kiran Khareedi
  • 2. CSV MODULE (Continued) Using Field Names • CSV module includes classes for working with rows as dictionaries so that the fields can be named. • The DictReader and DictWriter classes translate rows to dictionaries instead of lists. Keys for the dictionary can be passed in, or inferred from the first row in the input.
  • 3. DictReader import csv f = open('sampleread.csv', 'rt') try:try: reader = csv.DictReader(f) for row in reader: print row finally: f.close()
  • 4. DictWriter • The main difference is that the fieldnames have to be entered as a separate list. • The fieldnames have to match all of the keys• The fieldnames have to match all of the keys in the dictionary array but can be ordered differently
  • 5. Dict Writer import csv test_array = [] test_array.append({'fruit': 'apple', 'quantity': 5, 'color': 'red'}); test_array.append({'fruit': 'pear', 'quantity': 8, 'color': 'green'}); test_array.append({'fruit': 'banana', 'quantity': 3, 'color': 'yellow'}); test_array.append({'fruit': 'orange', 'quantity': 11, 'color': 'orange'}); test = {"fruit":"fruit","quantity":"quantity","color":"Colors"}test = {"fruit":"fruit","quantity":"quantity","color":"Colors"} fieldnames = ['fruit', 'quantity', 'color'] test_file = open('test2.csv','wb') csvwriter = csv.DictWriter(test_file, delimiter=',', fieldnames=fieldnames) #csvwriter.writerow(dict((fn,fn+"1") for fn in fieldnames)) csvwriter.writerow(test); for row in test_array: csvwriter.writerow(row) test_file.close()
  • 6. Advanced Data Structures: Stacks Data structure to store the elements, where the main operation is to push, pop, display. push – to add an element to the endpush – to add an element to the end pop – to remove an element from the end Display – to display the elements Follows Last In First Out (LIFO) concept
  • 7. Data structure to store the elements, where the main operation is to enqueue, dequeue, display. enqueue – to add an element to the back Advanced Data Structures: Queues enqueue – to add an element to the back dequeue – to remove an element from the front Display – to display the elements Follows First In First Out (FIFO) concept
  • 8. Stacks and Queues Ref: http://pakitgroup.blogspot.de/p/data- structure.html
  • 9. TASK • Write a program to implement Stack operations • Write a program to implement Queue operations
  • 10. TASK • Given 3 int values, a b c, return their sum. However, if one of the values is 13 then it does not count towards the sum and values to its right do not count. So for example, consider (a,b,c) if b is 13, then both b and c do not(a,b,c) if b is 13, then both b and c do not count. lucky_sum(1, 2, 3) → 6 lucky_sum(1, 2, 13) → 3 lucky_sum(1, 13, 3) → 1
  • 11. TASK • Given three ints, a b c, one of them is small, one is medium and one is large. Return true if the three values are evenly spaced, so the difference between small and medium is the same as the difference between medium and large. • even_space(2,4,6) – True • even_space(4,5,2) – False • even_space(4,6,2) – True (Can make use of any python functions)
  • 12. References • www.codingbat.com • docs.python.org • http://www.gadzmo.com/python/reading- and-writing-csv-files-with-python-dictreader-and-writing-csv-files-with-python-dictreader- and-dictwriter/