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
PPTX
Logic programming in python
PDF
Intro To BOOST.Spirit
PDF
Python tutorial
PDF
libpinyin
PDF
Response to uspto on the first topic v5
PPTX
Introduction to Structure Programming with C++
KMP Pattern Search
Logic programming in python
Intro To BOOST.Spirit
Python tutorial
libpinyin
Response to uspto on the first topic v5
Introduction to Structure Programming with C++

Viewers also liked (19)

PDF
Lets learn Python !
PPT
Introduction to Python
PPT
Python 4 Arc
PPT
The Python Programming Language and HDF5: H5Py
PDF
Python in real world.
PPT
Substituting HDF5 tools with Python/H5py scripts
PPTX
Introduction To Programming with Python-1
PDF
Logic Over Language
PDF
Python and HDF5: Overview
PDF
An Introduction to Interactive Programming in Python 2013
PDF
Introduction to Databases
PDF
Logic: Language and Information 1
PPTX
Introduction To Programming with Python-5
PPTX
Introduction To Programming with Python-4
PDF
Introduction to UBI
PPTX
Clase 2 estatica
PPT
Using HDF5 and Python: The H5py module
Lets learn Python !
Introduction to Python
Python 4 Arc
The Python Programming Language and HDF5: H5Py
Python in real world.
Substituting HDF5 tools with Python/H5py scripts
Introduction To Programming with Python-1
Logic Over Language
Python and HDF5: Overview
An Introduction to Interactive Programming in Python 2013
Introduction to Databases
Logic: Language and Information 1
Introduction To Programming with Python-5
Introduction To Programming with Python-4
Introduction to UBI
Clase 2 estatica
Using HDF5 and Python: The H5py module
Ad

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

PDF
A gentle introduction to algorithm complexity analysis
PDF
علم البيانات - Data Sience
PPTX
Python 1&2.pptx
PPTX
Python 1&2.pptx
PDF
PDF
Python Interview Questions PDF By ScholarHat.pdf
PDF
Hasktut
PDF
Howto argparse
PDF
An Overview Of Java | Object Oriented Programming
PDF
First Steps in Python Programming
PDF
Explainability for Learning to Rank
DOCX
Top 10 Interview Questions for Coding Job.docx
DOCX
Top 10 Interview Questions for Coding Job.docx
PPTX
Sam python pro_points_slide
PPTX
Automation Testing theory notes.pptx
PDF
What is Range Function? | Range in Python Explained | Edureka
PPTX
Python fundamentals
PDF
Intro-to-Python-Part-1-first-part-edition.pdf
PPTX
Introduction on basic python and it's application
PPT
Object-Oriented Programming Using C++
A gentle introduction to algorithm complexity analysis
علم البيانات - Data Sience
Python 1&2.pptx
Python 1&2.pptx
Python Interview Questions PDF By ScholarHat.pdf
Hasktut
Howto argparse
An Overview Of Java | Object Oriented Programming
First Steps in Python Programming
Explainability for Learning to Rank
Top 10 Interview Questions for Coding Job.docx
Top 10 Interview Questions for Coding Job.docx
Sam python pro_points_slide
Automation Testing theory notes.pptx
What is Range Function? | Range in Python Explained | Edureka
Python fundamentals
Intro-to-Python-Part-1-first-part-edition.pdf
Introduction on basic python and it's application
Object-Oriented Programming Using C++
Ad

Recently uploaded (20)

PPTX
Online Work Permit System for Fast Permit Processing
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
AI in Product Development-omnex systems
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Introduction to Artificial Intelligence
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
ai tools demonstartion for schools and inter college
PDF
System and Network Administraation Chapter 3
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
Online Work Permit System for Fast Permit Processing
2025 Textile ERP Trends: SAP, Odoo & Oracle
How Creative Agencies Leverage Project Management Software.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Internet Downloader Manager (IDM) Crack 6.42 Build 41
AI in Product Development-omnex systems
Wondershare Filmora 15 Crack With Activation Key [2025
Odoo Companies in India – Driving Business Transformation.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Design an Analysis of Algorithms I-SECS-1021-03
CHAPTER 2 - PM Management and IT Context
ISO 45001 Occupational Health and Safety Management System
VVF-Customer-Presentation2025-Ver1.9.pptx
Introduction to Artificial Intelligence
How to Migrate SBCGlobal Email to Yahoo Easily
Navsoft: AI-Powered Business Solutions & Custom Software Development
ai tools demonstartion for schools and inter college
System and Network Administraation Chapter 3
Operating system designcfffgfgggggggvggggggggg
ManageIQ - Sprint 268 Review - Slide Deck

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