python programming - everyday(ish) examples

18
Examples in Python

Upload: ashish-sharma

Post on 02-Jul-2015

180 views

Category:

Software


2 download

DESCRIPTION

Learn about how to use the Python language and its constructs in different situations. With classification of scenarios done already you can use this material as a reference guide too. Learn the language and its idioms by trying out the sample code or by looking at code snippet. This presentation was created for Next Craft JMP Student Program to help students jump start with Python programming.

TRANSCRIPT

Page 1: Python programming - Everyday(ish) Examples

Examples in Python

Page 2: Python programming - Everyday(ish) Examples

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

Page 3: Python programming - Everyday(ish) Examples

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

Page 4: Python programming - Everyday(ish) Examples

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

Page 5: Python programming - Everyday(ish) Examples

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

Page 6: Python programming - Everyday(ish) Examples

Code

By Ashish Sharma for Next Craft

Page 7: Python programming - Everyday(ish) Examples

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

Page 8: Python programming - Everyday(ish) Examples

Using for loop

By Ashish Sharma for Next Craft

Page 9: Python programming - Everyday(ish) Examples

Using Reduce

Using Map/Reduce

By Ashish Sharma for Next Craft

Page 10: Python programming - Everyday(ish) Examples

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

Page 11: Python programming - Everyday(ish) Examples

Code.

By Ashish Sharma for Next Craft

Page 12: Python programming - Everyday(ish) Examples

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

Page 13: Python programming - Everyday(ish) Examples

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

Page 14: Python programming - Everyday(ish) Examples

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

Page 15: Python programming - Everyday(ish) Examples

Here is a sample of code and test files.

By Ashish Sharma for Next Craft

Page 16: Python programming - Everyday(ish) Examples

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

Page 17: Python programming - Everyday(ish) Examples

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

Page 18: Python programming - Everyday(ish) Examples

Find me at @ashish424242 and @kartaa.

Checkout NextCraft’s JMP

By Ashish Sharma for Next Craft