Test-Driven Development with Python Test-Driven Development with Python Test-Driven Development with Python Harry Percival Gillian McGarvey Rebecca Demarest Wendy Catalano Randy Comer David Futato Copyright © 2014 Harry Percival Printed in the United States of America. O’Reilly books may be purchased for educational, business, or sales promotional use. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. Praise for Test-Driven Development with Python Table of Contents © 2013, O’Reilly Media, Inc.
So You’d Like To Make a Map Using Python Making thematic maps has traditionally been the preserve of a ‘proper’ GIS, such as ArcGIS or QGIS. While these tools make it easy to work with shapefiles, and expose a range of common everyday GIS operations, they aren’t particularly well-suited to exploratory data analysis. In short, if you need to obtain, reshape, and otherwise wrangle data before you use it to make a map, it’s easier to use a data analysis tool (such as Pandas), and couple it to a plotting library. This tutorial will be demonstrating the use of: PandasMatplotlibThe matplotlib Basemap toolkit, for plotting 2D data on mapsFiona, a Python interface to OGRShapely, for analyzing and manipulating planar geometric objectsDescartes, which turns said geometric objects into matplotlib “patches”PySAL, a spatial analysis library Package installation This tutorial uses Python 2.7.x, and the following non-stdlib packages are required: IPythonPandasNumpyMatplotlibBasemapShapelyFionaDescartesPySAL Running the Code Obtaining a basemap
Python List Examples – Insert, Append, Length, Index, Remove, Pop Lists in Python language can be compared to arrays in Java but they are different in many other aspects. Lists are used in almost every program written in Python. In this tutorial we will understand Python lists through practical examples. We will cover the following in this article : How to define a listHow to add elements to a listHow access sub listsHow to search within listsHow to delete elements from a listOperators and lists 1. Defining a List in Python is easy. >>> myList = ["The", "earth", "revolves", "around", "sun"] >>> myList ['The', 'earth', 'revolves', 'around', 'sun'] So you can see that a list named ‘myList’ was created with some elements. Lists in python are zero indexed. >>> myList[0] 'The' >>> myList[4] 'sun' Lists cannot be accessed beyond the rightmost index boundary. >>> myList[5] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>> myList[-1] 'sun' 2. So, the two values were appended towards the end of the list.
Python Dictionary Examples – Create, Update and Delete Elements Dictionary in python consists of keys and their values. This article is part of our ongoing series on python. In the first article of the series, we explained how to use variables, strings and functions in python. In this tutorial, we’ll understand the basics of python dictionaries with examples. 1. Here is an example of how we can create a dictionary in Python : In the above example: A dictionary is created.This dictionary contains three elements.Each element constitutes of a key value pair.This dictionary can be accessed using the variable myDict. 2. Once a dictionary is created, you can access it using the variable to which it is assigned during creation. Here is how this can be done : >>> myDict["A"] 'Apple' >>> myDict["B"] 'Boy' >>> myDict["C"] 'Cat' So you can see that using the variable myDict and Key as index, the value of corresponding key can be accessed. If you just type the name of the variable myDict, all the key value pairs in the dictionary will be printed. 3. 4. 1. 2.
Python Introduction Tutorial – Variable, String, Function Examples Python is an object oriented language with can be used in both scripting and non scripting contexts. Python has clean and easy syntax that makes it easy to read. In this article, we will understand python language from scratch through examples. We will often compare Python programming features/syntax with some popular languages like C/C++. This is a new series. 1. Here is the simple “Hello World” example in python: $ vi firstPYProgram.py print "Hello World" Note that the ‘print’ in python does not require parenthesis. Now run the above python program on command line in the following way : $ python firstPYProgram.py Here is the output : Hello World So we see that the single ‘print’ statement executed and the string “Hello World” was displayed in output. Now lets try adding another ‘print’ statement to the python script example: $ cat firstPYProgram.py print "Hello World" print "This is my first python program" Now when you run this again, here is the output: 2. Here is how an integer can be used :