SlideShare a Scribd company logo
Examples in Python
 Python is a programming language with strengths of both functional and object-oriented 
programming. 
 Here I will present common problems and their solutions that you might use while 
starting coding in Python. 
 You may use this material as a reference. 
By Ashish Sharma for Next Craft
 One who has began learning Python. 
 One who has made oneself acquainted with concepts of Python taught in 
codecademy.com Python course. 
 One who wishes to learn the idioms of python to solve problems in pythonic way. 
 One who is interested in free, good content.  
By Ashish Sharma for Next Craft
 Learn by Doing: Here are examples of python code used in different scenarios. 
 You can try to pick the concepts and then mould them to use in your own 
situation. 
 Use of Classes, Exceptions, rest of Standard Library not covered here. 
By Ashish Sharma for Next Craft
 Let us suppose we have a file containing a student’s SGPAs from 8 semesters and 
we would like to write a new file with CGPA. 
 SGPA.txt , has list of numbers in different lines. 
 Generating CGPA.txt from SGPA.txt 
 We will first read the SGPA.txt file line by line, 
 Then accumulate the values into a single variable, 
 Then calculate CGPA value and write that back into a new file. 
Try out yourself with sample 
data 
By Ashish Sharma for Next Craft
Code 
By Ashish Sharma for Next Craft
 Let us suppose you have a list of SGPAs on scale of 10 and from it you want to 
calculate CGPA on scale of 4. 
 We will do this in two ways, 
 Using for loop, 
 Using map, reduce (they are not as scary as they may sound) 
 The ways of coding this by map, reduce etc. are less verbose and more 
transportable. 
By Ashish Sharma for Next Craft
Using for loop 
By Ashish Sharma for Next Craft
Using Reduce 
Using Map/Reduce 
By Ashish Sharma for Next Craft
 In Python the data structure of Map is implemented in Dictionary. 
 A map is a structure to store <key, value> relationships. 
 Suppose you need to compute sum of squares of given odd numbers that can not 
be smaller than 1 and larger than 99 
 In the example, we will pre compute as the limits are small and return answers 
from a dict. 
By Ashish Sharma for Next Craft
Code. 
By Ashish Sharma for Next Craft
 From the older sgpa example, if we wish to filter out the sgpa from list that were 
more than 8.5. 
 From the older odd_squares example, if we wish to filter out the odd squares 
where the number is a multiple of 3. 
By Ashish Sharma for Next Craft
 Here we will validate data sent our way with regular expressions. You can also use 
them for search, match and replace. 
 Suppose we want input from user to be a number, here is how to gracefully handle 
this requirement. 
By Ashish Sharma for Next Craft
 Unit testing is a very important aspect of software development. In Python, you 
can find extensive support for unit testing you code. 
 With unit testing we can test single functions, classes or whole modules. 
 Having code and test separately and testable is a prime software development 
requirement. 
 This fact is elicited in this advice that developers get from creator of Unit testing 
framework for Small talk, (one of the first or may be the first frameworks). 
 I recommend that developers spend 25-50% of their time developing tests. 
 Quoted from - http://www.xprogramming.com/testfram.htm 
By Ashish Sharma for Next Craft
 Here is a sample of code and test files. 
By Ashish Sharma for Next Craft
Please let me know in the comments if I am missing any general scenarios, which 
you would like to see in the examples here. 
I will be more than happy to extend this presentation. 
By Ashish Sharma for Next Craft
 Python Reference – The best guide on Python out there. 
 Codecademy – Beginner course on Python, totally free . 
 http://learnpythonthehardway.org/ - If you want to develop a deep sense of 
knowledge of Python, venture here. 
By Ashish Sharma for Next Craft
Find me at @ashish424242 and @kartaa. 
Checkout NextCraft’s JMP 
By Ashish Sharma for Next Craft

More Related Content

PDF
KMP Pattern Search
Arjun SK
 
PPTX
Logic programming in python
Pierre Carbonnelle
 
PDF
Intro To BOOST.Spirit
Will Shen
 
PDF
Python tutorial
Andrei Tomoroga
 
PDF
libpinyin
Peng Wu
 
PDF
Response to uspto on the first topic v5
getsocialize
 
PPTX
Introduction to Structure Programming with C++
Mohamed Essam
 
KMP Pattern Search
Arjun SK
 
Logic programming in python
Pierre Carbonnelle
 
Intro To BOOST.Spirit
Will Shen
 
Python tutorial
Andrei Tomoroga
 
libpinyin
Peng Wu
 
Response to uspto on the first topic v5
getsocialize
 
Introduction to Structure Programming with C++
Mohamed Essam
 

Viewers also liked (19)

PDF
Lets learn Python !
Kiran Gangadharan
 
PPT
Introduction to Python
Nowell Strite
 
PPT
Python 4 Arc
absvis
 
PPT
The Python Programming Language and HDF5: H5Py
The HDF-EOS Tools and Information Center
 
PDF
Python in real world.
[email protected]
 
PPT
Substituting HDF5 tools with Python/H5py scripts
The HDF-EOS Tools and Information Center
 
PPTX
Introduction To Programming with Python-1
Syed Farjad Zia Zaidi
 
PDF
Logic Over Language
Purple, Rock, Scissors
 
PDF
Python and HDF5: Overview
andrewcollette
 
PDF
An Introduction to Interactive Programming in Python 2013
Syed Farjad Zia Zaidi
 
PDF
Introduction to Databases
Syed Farjad Zia Zaidi
 
PDF
Logic: Language and Information 1
Syed Farjad Zia Zaidi
 
PPTX
Introduction To Programming with Python-5
Syed Farjad Zia Zaidi
 
PPTX
Introduction To Programming with Python-4
Syed Farjad Zia Zaidi
 
PDF
Introduction to UBI
Roy Lee
 
PPTX
Clase 2 estatica
Gerald Moreira Ramírez
 
PPT
Using HDF5 and Python: The H5py module
The HDF-EOS Tools and Information Center
 
Lets learn Python !
Kiran Gangadharan
 
Introduction to Python
Nowell Strite
 
Python 4 Arc
absvis
 
The Python Programming Language and HDF5: H5Py
The HDF-EOS Tools and Information Center
 
Python in real world.
[email protected]
 
Substituting HDF5 tools with Python/H5py scripts
The HDF-EOS Tools and Information Center
 
Introduction To Programming with Python-1
Syed Farjad Zia Zaidi
 
Logic Over Language
Purple, Rock, Scissors
 
Python and HDF5: Overview
andrewcollette
 
An Introduction to Interactive Programming in Python 2013
Syed Farjad Zia Zaidi
 
Introduction to Databases
Syed Farjad Zia Zaidi
 
Logic: Language and Information 1
Syed Farjad Zia Zaidi
 
Introduction To Programming with Python-5
Syed Farjad Zia Zaidi
 
Introduction To Programming with Python-4
Syed Farjad Zia Zaidi
 
Introduction to UBI
Roy Lee
 
Clase 2 estatica
Gerald Moreira Ramírez
 
Using HDF5 and Python: The H5py module
The HDF-EOS Tools and Information Center
 
Ad

Similar to Python programming - Everyday(ish) Examples (20)

PDF
A gentle introduction to algorithm complexity analysis
Lewis Lin 🦊
 
PDF
علم البيانات - Data Sience
App Ttrainers .com
 
PPTX
Python 1&2.pptx
Chahbar1
 
PPTX
Python 1&2.pptx
Chahbar1
 
PDF
Project
Guilherme Torres
 
PDF
Python Interview Questions PDF By ScholarHat.pdf
Scholarhat
 
PDF
Hasktut
kv33
 
PDF
Howto argparse
Manuel Cueto
 
PDF
An Overview Of Java | Object Oriented Programming
ArghyaGayen1
 
PDF
First Steps in Python Programming
Dozie Agbo
 
PDF
Explainability for Learning to Rank
Sease
 
DOCX
Top 10 Interview Questions for Coding Job.docx
Surendra Gusain
 
DOCX
Top 10 Interview Questions for Coding Job.docx
Surendra Gusain
 
PPTX
Sam python pro_points_slide
"Samprateek "Sam"" Sinha
 
PPTX
Automation Testing theory notes.pptx
NileshBorkar12
 
PDF
What is Range Function? | Range in Python Explained | Edureka
Edureka!
 
PPTX
Python fundamentals
natnaelmamuye
 
PDF
Intro-to-Python-Part-1-first-part-edition.pdf
ssuser543728
 
PPTX
Introduction on basic python and it's application
sriram2110
 
PPT
Object-Oriented Programming Using C++
Salahaddin University-Erbil
 
A gentle introduction to algorithm complexity analysis
Lewis Lin 🦊
 
علم البيانات - Data Sience
App Ttrainers .com
 
Python 1&2.pptx
Chahbar1
 
Python 1&2.pptx
Chahbar1
 
Python Interview Questions PDF By ScholarHat.pdf
Scholarhat
 
Hasktut
kv33
 
Howto argparse
Manuel Cueto
 
An Overview Of Java | Object Oriented Programming
ArghyaGayen1
 
First Steps in Python Programming
Dozie Agbo
 
Explainability for Learning to Rank
Sease
 
Top 10 Interview Questions for Coding Job.docx
Surendra Gusain
 
Top 10 Interview Questions for Coding Job.docx
Surendra Gusain
 
Sam python pro_points_slide
"Samprateek "Sam"" Sinha
 
Automation Testing theory notes.pptx
NileshBorkar12
 
What is Range Function? | Range in Python Explained | Edureka
Edureka!
 
Python fundamentals
natnaelmamuye
 
Intro-to-Python-Part-1-first-part-edition.pdf
ssuser543728
 
Introduction on basic python and it's application
sriram2110
 
Object-Oriented Programming Using C++
Salahaddin University-Erbil
 
Ad

Recently uploaded (20)

PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PDF
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PDF
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
DOCX
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PPTX
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
Presentation about variables and constant.pptx
kr2589474
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
Activate_Methodology_Summary presentatio
annapureddyn
 

Python programming - Everyday(ish) Examples

  • 2.  Python is a programming language with strengths of both functional and object-oriented programming.  Here I will present common problems and their solutions that you might use while starting coding in Python.  You may use this material as a reference. By Ashish Sharma for Next Craft
  • 3.  One who has began learning Python.  One who has made oneself acquainted with concepts of Python taught in codecademy.com Python course.  One who wishes to learn the idioms of python to solve problems in pythonic way.  One who is interested in free, good content.  By Ashish Sharma for Next Craft
  • 4.  Learn by Doing: Here are examples of python code used in different scenarios.  You can try to pick the concepts and then mould them to use in your own situation.  Use of Classes, Exceptions, rest of Standard Library not covered here. By Ashish Sharma for Next Craft
  • 5.  Let us suppose we have a file containing a student’s SGPAs from 8 semesters and we would like to write a new file with CGPA.  SGPA.txt , has list of numbers in different lines.  Generating CGPA.txt from SGPA.txt  We will first read the SGPA.txt file line by line,  Then accumulate the values into a single variable,  Then calculate CGPA value and write that back into a new file. Try out yourself with sample data By Ashish Sharma for Next Craft
  • 6. Code By Ashish Sharma for Next Craft
  • 7.  Let us suppose you have a list of SGPAs on scale of 10 and from it you want to calculate CGPA on scale of 4.  We will do this in two ways,  Using for loop,  Using map, reduce (they are not as scary as they may sound)  The ways of coding this by map, reduce etc. are less verbose and more transportable. By Ashish Sharma for Next Craft
  • 8. Using for loop By Ashish Sharma for Next Craft
  • 9. Using Reduce Using Map/Reduce By Ashish Sharma for Next Craft
  • 10.  In Python the data structure of Map is implemented in Dictionary.  A map is a structure to store <key, value> relationships.  Suppose you need to compute sum of squares of given odd numbers that can not be smaller than 1 and larger than 99  In the example, we will pre compute as the limits are small and return answers from a dict. By Ashish Sharma for Next Craft
  • 11. Code. By Ashish Sharma for Next Craft
  • 12.  From the older sgpa example, if we wish to filter out the sgpa from list that were more than 8.5.  From the older odd_squares example, if we wish to filter out the odd squares where the number is a multiple of 3. By Ashish Sharma for Next Craft
  • 13.  Here we will validate data sent our way with regular expressions. You can also use them for search, match and replace.  Suppose we want input from user to be a number, here is how to gracefully handle this requirement. By Ashish Sharma for Next Craft
  • 14.  Unit testing is a very important aspect of software development. In Python, you can find extensive support for unit testing you code.  With unit testing we can test single functions, classes or whole modules.  Having code and test separately and testable is a prime software development requirement.  This fact is elicited in this advice that developers get from creator of Unit testing framework for Small talk, (one of the first or may be the first frameworks).  I recommend that developers spend 25-50% of their time developing tests.  Quoted from - http://www.xprogramming.com/testfram.htm By Ashish Sharma for Next Craft
  • 15.  Here is a sample of code and test files. By Ashish Sharma for Next Craft
  • 16. Please let me know in the comments if I am missing any general scenarios, which you would like to see in the examples here. I will be more than happy to extend this presentation. By Ashish Sharma for Next Craft
  • 17.  Python Reference – The best guide on Python out there.  Codecademy – Beginner course on Python, totally free .  http://learnpythonthehardway.org/ - If you want to develop a deep sense of knowledge of Python, venture here. By Ashish Sharma for Next Craft
  • 18. Find me at @ashish424242 and @kartaa. Checkout NextCraft’s JMP By Ashish Sharma for Next Craft