Python | List Processing,list Comprehensions, variables, objects, type checking

This python programming tutorial covers List Processing, list Comprehensions, variables, objects, type checking etc.

List Processing

Working with lists is very common
Python is very adept at processing lists
Have already seen many examples:
>>> a = [1, 2, 3, 4, 5]
>>> sum(a)
>>> a[0:3]
[1, 2, 3]
>>> a * 2
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
>>> min(a)

List Comprehensions

Creates a new list by applying an operation to each element of a sequence.
>>> a = [1,2,3,4,5]
>>> b = [2*x for x in a]
>>> b

A list comprehension can also filter
>>> a = [1, -5, 4, 2, -2, 10]
>>> b = [2*x for x in a if x > 0]
>>> b

General syntax

[expression for x in s if condition]
What it means
result = []
for x in s:
if condition:
Can be used anywhere a sequence is expected
>>> a = [1,2,3,4]
>>> sum([x*x for x in a])

List Comp: Examples

List comprehensions are hugely useful
Collecting the values of a specific field
stocknames = [s['name'] for s in stocks]
Performing database-like queries
a = [s for s in stocks if s['price'] > 100
and s['shares'] > 50 ]

Variable Assignment

Variables in Python are names for values
A variable name does not represent a fixed memory location into which values are stored (like C, C++, Fortran, etc.)
Assignment is just a naming operation

Variables and Values

At any time, a variable can be redefined to refer to a new value
a = 42
a = "Hello"
Variables are not restricted to one data type

Names, Values, Types

Names do not have a "type"--it's just a name
However, values do have an underlying type
>>> a = 42
>>> b = "Hello World"
>>> type(a)

>>> type(b)

type() function will tell you what it is
The type name is usually a function that creates or converts a value to that type

Reference Counting

Variable assignment never copies anything!
Instead, it just updates a reference count
a = 42
b = a
c = [1,2]
So, different variables might be referring to the same object (check with the is operator)
>>> a is b

Reassignment never overwrites memory, so you normally don't notice any of the sharing
Instead, it just updates a reference count
a = 42
b = a

a = 37
When you reassign a variable, the name is just made to point to the new value.

"Copying" mutable objects such as lists and dicts is a hidden danger as explained below.

>>> a = [1,2,3,4]
>>> b = a
>>> b[2] = -10
>>> a
Changes affect both variables!
Reason: Different variable names are referring to exactly the same object

Making a Copy

You have to take special steps to copy data
>>> a = [2,3,[100,101],4]
>>> b = list(a) # Make a copy
>>> a is b

Sometimes you need to makes a copy of an object and all objects contained within it
Use the copy module
>>> a = [2,3,[100,101],4]
>>> import copy
>>> b = copy.deepcopy(a)
>>> a[2].append(102)
>>> b[2]

Everything is an object

Numbers, strings, lists, functions, exceptions, classes, instances, etc...
All objects are said to be "first-class"
Meaning: All objects that can be named can be passed around as data, placed in containers, etc.,
without any restrictions.
There are no "special" kinds of objects

First Class Objects

A simple example:
>>> import math
>>> items = [abs, math, ValueError ]
>>> items
>>> items[0](-45)

Type Checking

How to tell if an object is a specific type
if type(a) is list:
print "a is a list

if isinstance(a,list): # Preferred
print "a is a list"
Checking for one of many types
if isinstance(a,(list,tuple)):
print "a is a list or tuple"

Similar posts on Python tutorial

Useful DSP codes in Python