Reader small image

You're reading from  Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

Product typeBook
Published inSep 2015
Reading LevelBeginner
Publisher
ISBN-139781783551590
Edition1st Edition
Languages
Right arrow
Author (1)
Dan Nixon
Dan Nixon
author image
Dan Nixon

Dan Nixon is a software and electronics engineer living in the north of England. He has past experience of creating software for data analysis, process control, and business intelligence applications. In most of these projects, Python was one of the main languages used. Dan previously authored another book on the uses of the Raspberry Pi, called Raspberry Pi Blueprints, and has worked on many personal projects that use both Python and the Raspberry Pi.
Read more about Dan Nixon

Right arrow

Chapter 3. Working with Data Structures and I/O

In this chapter, we will take a look at:

  • The various data structures that are included in the standard Python types

  • How they can be used to manage multiple sets of data in a Python application

  • Reading and writing files to and from the disk to allow your applications to save their state or operate over data that already exists in a file on disk

Data structures


Data structures are containers that hold multiple variables, depending on the particular use case. There are multiple different data structures that can be used which we will now take a look at.

Lists

Lists are probably the most basic data structure; it is simply a list of variables that are numerically indexed by their position in the list.

Lists are most easily compared to array types that can be found in the other programming languages. However, they have the following properties that should be noted:

  • Zero indexed: The numerical indices of lists start from 0 (as per the majority of other programming languages) rather than 1.

  • Dynamically sized: Lists do not have a fixed size so they can grow to hold any number of elements.

  • Type agnostic: Lists do not care about the type of the value that is stored within them (as the type is defined by the instance rather than the container). This means that there is no requirement for the values held in a list to be of the same type.

Creating...

Input/output


We will now take a look at some of the ways we can access the files and directories on the filesystem and create, modify, and read the files. Here we will look at using Python's file objects which are documented in full at https://docs.python.org/2/library/stdtypes.html#bltin-file-objects.

While this will suffice for simple files, there is a good selection of free libraries available online that take a lot of work out of creating and parsing the more complex files such as XML, JSON, and MIDI.

The os.path module

The os.path module contains various functions for performing manipulation of path names specific to the host operating system. This goes hand in hand with the file objects for accessing the files and directories on the filesystem, and helps to ensure that code can be used on any platform by handing all of the platform dependent and specific tasks for you (for example, the differences in file paths on Windows and Linux).

The following example demonstrates the most commonly...

Summary


In this chapter, we looked at storing data sets in Python using the container types and how we can perform operations over an entire data set using the container operations as well as accessing files saved on the disk as plain text.

In the next chapter, we will learn about the Object Oriented Programming (OOP) paradigm, how this differs from the functional programming style we have been doing so far, and in what cases it should be used.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)
Published in: Sep 2015Publisher: ISBN-13: 9781783551590
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Dan Nixon

Dan Nixon is a software and electronics engineer living in the north of England. He has past experience of creating software for data analysis, process control, and business intelligence applications. In most of these projects, Python was one of the main languages used. Dan previously authored another book on the uses of the Raspberry Pi, called Raspberry Pi Blueprints, and has worked on many personal projects that use both Python and the Raspberry Pi.
Read more about Dan Nixon