SPSS TUTORIALS BASICS ANOVA REGRESSION FACTOR CORRELATION

# Quick Overview Python Object Types

In our examples, you'll encounter terms such as a Python list or dict object. So which object types do we find in Python and what are their basic properties? The table below presents a quick overview.

## Quick Overview Python Object Types

TypeMeaningIterable?Mutable?Looks likeWhat is it?
str StringYesNoappleSequence of 0 or more characters
list ListYesYes[1,2,3]Sequence of objects that are referenced by their positions
tuple Python tupleYesNo('apple','banana')Sequence of objects that are referenced by their positions
dict DictionaryYesYes{1:'apple',2:'banana'}Unordered set of (unique) keys, each of which has some value
int Integer numberNoNo1Number that has no decimal places
float Floating point numberNoNo1.0Number that has decimal places
function Python functionNoNodef myfunction():Named amount of code that takes zero or more arguments and executes something
module Python moduleNoNo(Text file with .py extension)One or more Python files that define functions and/or other objects.
bool BooleanNoNoTrue / FalseObject that can only indicate True or False
range Python rangeYesNorange(0, 10)Sequence of integer numbers
NoneType Empty objectNoNoNoneEmpty object without type

## What Does “Iterable” Mean in Python?

Python objects are iterable if you can loop over them. For instance, you can loop over (the characters contained in) a Python string object. However, you can't loop over a Python int object.

Whether you can (not) loop over an object only depends on the type of object, not its contents. Like so, you can loop over an empty list as shown below.

*CREATE STRING OBJECT (ITERABLE) AND LOOP OVER ITS CHARACTERS.

begin program python3.
myString = '13579'
for char in myString:
print(char)
end program.

*INT OBJECT IS NOT ITERABLE.

begin program python3.
myInt = 13579
for char in myInt:
print(char) # TypeError: 'int' object is not iterable
end program.

*PYTHON LIST IS ITERABLE, EVEN IF EMPTY.

begin program python3.
myList = []
for elem in myList:
print(elem)
end program.

## What Does “Mutable” Mean in Python?

Python objects are mutable if their values can be changed. Confusingly, it seems you can change the value of, for instance, a string object. Doing so, however, simply creates an entirely new string object with the same name but a (possibly) different value than the original string object.

The examples below demonstrate this by inspecting the Python IDs before and after modifying some string object.

*CHANGING VALUE OF STRING OBJECT CREATES NEW STRING OBJECT.

begin program python3.
myString = 'apple'
print(myString) # apple
print(id(myString)) # 51135968
myString += 's'
print(myString) # apples
print(id(myString)) # 51043720
end program.

*CHANGING ELEMENTS OF LIST OBJECT DOES NOT RESULT IN NEW LIST OBJECT.

begin program python3.
myList = ['apple','banana','cherry']
print(myList) # ['apple', 'banana', 'cherry']
print(id(myList)) # 45378824
myList.append('durian')
print(myList) # ['apple', 'banana', 'cherry', 'durian']
print(id(myList)) # 45378824
end program.

## Python String Object

A Python string simply consists of 0 or (usually) more characters. For a detailed overview of Python string methods, read up on Overview Python String Methods. The syntax below demonstrates some minimal basics.

*CREATE PYTHON STRING OBJECT AND INSPECT IT.

begin program python3.
myString = 'apple'
print(type(myString)) # <type 'str'>
print(myString) # apple
end program.

*RETRIEVE LAST ELEMENT (CHARACTER) FROM STRING.

begin program python3.
print(myString[-1]) # e
end program.

Why are Python string objects so important? Well, a main goal of using Python in SPSS is creating (large amounts of) SPSS syntax to accomplish several tasks. Such syntax is created as one or many Python string objects which are then passed on to be run in SPSS.

## Python List Object

Python list objects consist of 0 or (usually) more elements separated by commas and enclosed by square brackets.

*CREATE PYTHON LIST OBJECT.

begin program python3.
myList = ['apple','banana','cherry']
print(type(myList)) # <class 'list'>
print(myList) # ['apple','banana','cherry']
end program.

*RETRIEVE ITEM FROM LIST BY INDEX.

begin program python3.
print(myList[0]) # apple
end program.

List objects are similar to tuples except that they are editable (“mutable”). As a consequence, many methods are available for lists. We may present a quick overview of those in a future tutorial.

## Python Tuple Object

Python tuples consist of 0 or more elements enclosed by parentheses and separated by commas. The syntax below demonstrates a handful of basics.

*CREATE AND INSPECT TUPLE.

begin program python3.
myTuple = ('apple','banana','cherry')
print(type(myTuple)) # <class 'tuple'>
print(myTuple) # ('apple','banana','cherry')
end program.

*EXTRACT LAST ELEMENT FROM TUPLE.

begin program python3.
print(myTuple[-1]) # cherry
end program.

Python tuples are similar to Python list objects, except that they are not editable (“immutable”). We usually just extract elements from them with square brackets.

## Python Dictionary Object

A Python dict object consists of 0 or (usually) more key-value pairs. Value can only be retrieved from their keys, not from any indices as shown below.

Python dict keys (but not values) must be unique. Both keys and values are usually Python string or integer objects but they can technically be any type including

• lists;
• tuples;
• dictionaries;
• or any other Python object.

The syntax below demonstrates a handful of Python dict basics.

*CREATE PYTHON DICT OBJECT.

begin program python3.
myDict = {'a':'apple','b':'banana','c':'cherry'}
print(type(myDict)) # <class 'dict'>
print(myDict) # {'b': 'banana', 'a': 'apple', 'c': 'cherry'}
end program.

*RETRIEVE DICT VALUE FROM DICT KEY.

begin program python3.
print(myDict['a']) # apple
end program.

*NOTE: YOU CAN'T RETRIEVE DICT VALUE FROM INDEX.

begin program python3.
print(myDict[0]) # ... KeyError ...
end program.

*LOOP OVER DICT KEYS AND VALUES.

begin program python3.
for key,value in myDict.items():
print(key,value)
end program.

## Python Integer Object

An “int” in Python indicates an integer number: a number without decimal places.

*CREATE AND INSPECT TUPLE.

begin program python3.
myInt = 5
print(type(myInt)) # <class 'int'>
print(myInt) # 5
end program.

*NOTE THAT / OPERATOR INDICATES DIVISION FOR INT / FLOAT.

begin program python3.
print(myInt / 2)
end program.

Note that a number with decimal places is a float or a decimal in Python.

## Python Float Object

Numbers with decimal places are floats or decimals in Python. The syntax below demonstrates a handful of basics.

*CREATE PYTHON FLOAT OBJECT.

begin program python3.
myFloat = 3.0
print(type(myFloat)) # <class 'float'>
print(myFloat) # 3.0
end program.

*FOR FLOAT, + OPERATOR INDICATES NUMERIC ADDITION.

begin program python3.
print(myFloat + 1) # 1.5
end program.

Floats in Python are single-precision floating point numbers. In contrast, all numbers in SPSS are double-precision floating-point numbers.

## Python Functions

A function in Python consists of 1 or more lines of Python code. These typically accomplish a general task that is needed in several different situations. A minimal example is shown below.

*DEFINE PYTHON FUNCTION.

begin program python3.
def myFunction(myName = 'Ruben'):
print('Hello! My name is {}'.format(myName))
end program.

*INSPECT FUNCTION.

begin program python3.
print(type(myFunction)) # <class 'function'>
print(myFunction) # <function myFunction at 0x00000000030A3978>
end program.

*RUN FUNCTION WITH(OUT) ARGUMENT.

begin program python3.
myFunction() # Hello! My name is Ruben
myFunction(myName = 'Chrissy') # Hello! My name is Chrissy
end program.

Note that this function takes at most 1 argument: myName. If no argument is passed, it defaults to “Ruben”.

In Python and other programming languages, functions are very useful to make code shorter and more manageable: we basically break up a large amount of code into tiny building blocks that we can adjust and correct separately of the others.

A lesson in which we'll define and use Python functions is SPSS - Cloning Variables with Python.

## Python Modules

A Python module basically consists of one or more text files containing Python code with the .py extension.

*IMPORT AND INSPECT MODULE.

begin program python3.
import os
print(type(os)) # <class 'module'>
print(os) # <module 'os' from 'C:\\Program Files\\IBM\\SPSS\\Statistics\\Python3\\lib\\os.py'>
end program.

Python modules typically define classes and functions that are related to specific tasks such as

• working with regular expressions (Python re module);
• interacting with Excel files (openpyxl module);
• creating, moving, copying or deleting files and/or folders (Python os module).

We'll create some very simple SPSS Python modules in SPSS - Cloning Variables with Python.

## Python Boolean Object

Boolean objects are simply True or False. They're used to specify if you want some task to be performed or not. Also, conditions implicitly result in Booleans (True if they're met, False otherwise).

*CREATE AND INSPECT PYTHON BOOLEAN OBJECT.

begin program python3.
myBoolean = True
print(type(myBoolean)) # <class 'bool'>
print(myBoolean) # True
end program.

*USE BOOLEAN IN IF STATEMENT.

begin program python3.
if myBoolean:
print('Yes!')
else:
print('No!')
end program.

*IMPLICIT BOOLEAN IN IF STATEMENT.

begin program python3.
if 'a' in 'banana':
print('Yes!')
else:
print('No!')
end program.

*USE BOOLEAN AS FUNCTION ARGUMENT.

begin program python3.
myList = [5,2,8,1,9]
print(sorted(myList,reverse = True)) # [9, 8, 5, 2, 1]
end program.

## Python Range Object

A Python range object creates a series of consecutive integers when looped over. Note that range(10) results in 0 through 9. For 1 through 10, use range(1,11).

*CREATE AND INSPECT RANGE OBJECT.

begin program python3.
myRange = range(10)
print(type(myRange)) #<class 'range'>
print(myRange) # range(0, 10)
end program.

*LOOP OVER NUMBERS 1 - 10.

begin program python3.
for myInt in range(1,11):
print(myInt)
end program.

## Python NoneType Object

A NoneType object in Python indicates an empty object that has been declared but does not have any value (yet).

*CREATE AND INSPECT NONETYPE OBJECT.

begin program python3.
myNone = None
print(type(myNone)) # <class 'NoneType'>
print(myNone) # None
end program.

When reading SPSS data values with the spssdata module, missing values usually result in NoneType objects in Python. Also, optional arguments sometimes have None as their default in Python functions.

## Final Notes

So much for my basic overview of Python object types. Did I miss anything? Did you find it helpful? Help me out and let me know.