Examples in Python
Jul 02, 2015
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