{"id":159,"date":"2025-04-21T07:08:34","date_gmt":"2022-03-21T06:34:54","guid":{"rendered":""},"modified":"2022-03-21T08:34:54","modified_gmt":"2022-03-21T06:34:54","slug":"overview-python-object-types","status":"publish","type":"post","link":"https:\/\/www.spss-tutorials.com\/overview-python-object-types\/","title":{"rendered":"Quick Overview Python Object Types"},"content":{"rendered":"<!--body-->\n<p>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.<\/p>\n\n\n<h2 id='overview-python-object-types'>Quick Overview Python Object Types<\/h2>\n\n<div class='table'>\n    <table>\n        <tr><th>Type<\/th><th>Meaning<\/th><th>Iterable?<\/th><th>Mutable?<\/th><th>Looks like<\/th><th>What is it?<\/th><\/tr>\n        <tr><td><a href=\"#str\">str<\/a> <\/td><td>String<\/td><td>Yes<\/td><td>No<\/td><td>apple<\/td><td>Sequence of 0 or more characters<\/td><\/tr>\n        <tr><td><a href=\"#list\">list<\/a> <\/td><td>List<\/td><td>Yes<\/td><td>Yes<\/td><td>[1,2,3]<\/td><td>Sequence of objects that are referenced by their positions<\/td><\/tr>\n        <tr><td><a href=\"#tuple\">tuple<\/a> <\/td><td>Python tuple<\/td><td>Yes<\/td><td>No<\/td><td>('apple','banana')<\/td><td>Sequence of objects that are referenced by their positions<\/td><\/tr>\n        <tr><td><a href=\"#dict\">dict<\/a> <\/td><td>Dictionary<\/td><td>Yes<\/td><td>Yes<\/td><td>{1:'apple',2:'banana'}<\/td><td>Unordered set of (unique) keys, each of which has some value<\/td><\/tr>\n        <tr><td><a href=\"#int\">int<\/a> <\/td><td>Integer number<\/td><td>No<\/td><td>No<\/td><td>1<\/td><td>Number that has no decimal places<\/td><\/tr>\n        <tr><td><a href=\"#float\">float<\/a> <\/td><td>Floating point number<\/td><td>No<\/td><td>No<\/td><td>1.0<\/td><td>Number that has decimal places<\/td><\/tr>\n        <tr><td><a href=\"#function\">function<\/a> <\/td><td>Python function<\/td><td>No<\/td><td>No<\/td><td>def myfunction():<\/td><td>Named amount of code that takes zero or more arguments and executes something<\/td><\/tr>\n        <tr><td><a href=\"#module\">module<\/a> <\/td><td>Python module<\/td><td>No<\/td><td>No<\/td><td>(Text file with .py extension)<\/td><td>One or more Python files that define functions and\/or other objects.<\/td><\/tr>\n        <tr><td><a href=\"#bool\">bool<\/a> <\/td><td>Boolean<\/td><td>No<\/td><td>No<\/td><td>True \/ False<\/td><td>Object that can only indicate True or False<\/td><\/tr>\n        <tr><td><a href=\"#range\">range<\/a> <\/td><td>Python range<\/td><td>Yes<\/td><td>No<\/td><td>range(0, 10)<\/td><td>Sequence of integer numbers<\/td><\/tr>\n        <tr><td><a href=\"#nonetype\">NoneType<\/a> <\/td><td>Empty object<\/td><td>No<\/td><td>No<\/td><td>None<\/td><td>Empty object without type<\/td><\/tr>\n    <\/table>\n<\/div>\n\n\n<h2 id=\"iterable\">What Does &ldquo;Iterable&rdquo; Mean in Python?<\/h2>\n\n<p>\n<span class='def'>Python objects are iterable if you can loop over them.<\/span>\nFor instance, you can loop over (the characters contained in) a Python string object. However, you can't loop over a Python int object.<\/p>\n\n<p>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.<\/p>\n\n<div class='code'><strong>*CREATE STRING OBJECT (ITERABLE) AND LOOP OVER ITS CHARACTERS.<br><\/strong><br>begin program python3.<br>myString = &#39;13579&#39;<br>for char in myString:<br>&nbsp;&nbsp;&nbsp;&nbsp;print(char)<br>end program.<br><br><strong>*INT OBJECT IS NOT ITERABLE.<br><\/strong><br>begin program python3.<br>myInt = 13579<br>for char in myInt:<br>&nbsp;&nbsp;&nbsp;&nbsp;print(char) <strong># TypeError: &#39;int&#39; object is not iterable<br><\/strong>\nend program.<br><br><strong>*PYTHON LIST IS ITERABLE, EVEN IF EMPTY.<br><\/strong><br>begin program python3.<br>myList = []<br>for elem in myList:<br>&nbsp;&nbsp;&nbsp;&nbsp;print(elem)<br>end program.<\/div><!--class='code'-->\n\n<h2 id=\"mutable\">What Does &ldquo;Mutable&rdquo; Mean in Python?<\/h2>\n\n<p>\n<span class='def'>Python objects are mutable if their values can be changed.<\/span>\nConfusingly, it <em>seems<\/em> 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.<\/p>\n\n<p>The examples below demonstrate this by inspecting the Python IDs before and after modifying some string object.<\/p>\n\n<div class='code'><strong>*CHANGING VALUE OF STRING OBJECT CREATES NEW STRING OBJECT.<br><\/strong><br>begin program python3.<br>myString = &#39;apple&#39;<br>print(myString) <strong># apple<br><\/strong>\nprint(id(myString)) <strong># 51135968 <br><\/strong>\nmyString += &#39;s&#39;<br>print(myString) <strong># apples<br><\/strong>\nprint(id(myString)) <strong># 51043720<br><\/strong>\nend program.<br><br><strong>*CHANGING ELEMENTS OF LIST OBJECT DOES NOT RESULT IN NEW LIST OBJECT.<br><\/strong><br>begin program python3.<br>myList = [&#39;apple&#39;,&#39;banana&#39;,&#39;cherry&#39;]<br>print(myList) <strong># [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;] <br><\/strong>\nprint(id(myList)) <strong># 45378824 <br><\/strong>\nmyList.append(&#39;durian&#39;)<br>print(myList) <strong># [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;durian&#39;] <br><\/strong>\nprint(id(myList)) <strong># 45378824<br><\/strong>\nend program.<\/div><!--class='code'-->\n\n\n<h2 id=\"str\">Python String Object<\/h2> \n\n<p>A Python string simply consists of 0 or (usually) more characters. For a detailed overview of Python string methods, read up on <a href=\"https:\/\/www.spss-tutorials.com\/overview-python-string-methods\/\">Overview Python String Methods<\/a>. The syntax below demonstrates some minimal basics.<\/p>\n\n<div class='code'><strong>*CREATE PYTHON STRING OBJECT AND INSPECT IT.<br><\/strong><br>begin program python3.<br>myString = &#39;apple&#39;<br>print(type(myString)) <strong># &lt;type &#39;str&#39;&gt;<br><\/strong>\nprint(myString) <strong># apple<br><\/strong>\nend program.<br><br><strong>*RETRIEVE LAST ELEMENT (CHARACTER) FROM STRING.<br><\/strong><br>begin program python3.<br>print(myString[-1]) <strong># e<br><\/strong>\nend program.<\/div><!--class='code'-->\n<p>Why are Python string objects so important? Well, a main goal of using Python in SPSS is creating (large amounts of) <a href=\"https:\/\/www.spss-tutorials.com\/spss-syntax\/\">SPSS syntax<\/a> 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.<\/p>\n\n\n<h2 id=\"list\">Python List Object<\/h2> \n\n<p>Python list objects consist of 0 or (usually) more elements separated by commas and enclosed by square brackets.<\/p>\n\n<div class='code'><strong>*CREATE PYTHON LIST OBJECT.<br><\/strong><br>begin program python3.<br>myList = [&#39;apple&#39;,&#39;banana&#39;,&#39;cherry&#39;]<br>print(type(myList)) <strong># &lt;class &#39;list&#39;&gt;<br><\/strong>\nprint(myList) <strong># [&#39;apple&#39;,&#39;banana&#39;,&#39;cherry&#39;]<br><\/strong>\nend program.<br><br><strong>*RETRIEVE ITEM FROM LIST BY INDEX.<br><\/strong><br>begin program python3.<br>print(myList[0]) <strong># apple<br><\/strong>\nend program.<\/div><!--class='code'-->\n<p>List objects are similar to tuples except that they are editable (&ldquo;mutable&rdquo;). As a consequence, many methods are available for lists. We may present a quick overview of those in a future tutorial.<\/p>\n\n\n\n<h2 id=\"tuple\">Python Tuple Object<\/h2> \n\n<p>Python tuples consist of 0 or more elements enclosed by parentheses and separated by commas. The syntax below demonstrates a handful of basics.<\/p>\n\n<div class='code'><strong>*CREATE AND INSPECT TUPLE.<br><\/strong><br>begin program python3.<br>myTuple = (&#39;apple&#39;,&#39;banana&#39;,&#39;cherry&#39;)<br>print(type(myTuple)) <strong># &lt;class &#39;tuple&#39;&gt;<br><\/strong>\nprint(myTuple) <strong># (&#39;apple&#39;,&#39;banana&#39;,&#39;cherry&#39;)<br><\/strong>\nend program.<br><br><strong>*EXTRACT LAST ELEMENT FROM TUPLE.<br><\/strong><br>begin program python3.<br>print(myTuple[-1]) <strong># cherry<br><\/strong>\nend program.<\/div><!--class='code'-->\n<p>Python tuples are similar to Python list objects, except that they are not editable (&ldquo;immutable&rdquo;). We usually just extract elements from them with square brackets.<\/p>\n\n\n<h2 id=\"dict\">Python Dictionary Object<\/h2> \n\n<p>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.<\/p>\n\n<p>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<\/p>\n\n<ul>\n\t<li>lists;<\/li>\n\t<li>tuples;<\/li>\n\t<li>dictionaries;<\/li>\n\t<li>or any other Python object.<\/li>\n<\/ul>\n\n<p>The syntax below demonstrates a handful of Python dict basics.<\/p>\n\n<div class='code'><strong>*CREATE PYTHON DICT OBJECT.<br><\/strong><br>begin program python3.<br>myDict = {&#39;a&#39;:&#39;apple&#39;,&#39;b&#39;:&#39;banana&#39;,&#39;c&#39;:&#39;cherry&#39;}<br>print(type(myDict)) <strong># &lt;class &#39;dict&#39;&gt;<br><\/strong>\nprint(myDict) <strong># {&#39;b&#39;: &#39;banana&#39;, &#39;a&#39;: &#39;apple&#39;, &#39;c&#39;: &#39;cherry&#39;}<br><\/strong>\nend program.<br><br><strong>*RETRIEVE DICT VALUE FROM DICT KEY.<br><\/strong><br>begin program python3.<br>print(myDict[&#39;a&#39;]) <strong># apple<br><\/strong>\nend program.<br><br><strong>*NOTE: YOU CAN&#39;T RETRIEVE DICT VALUE FROM INDEX.<br><\/strong><br>begin program python3.<br>print(myDict[0]) <strong># ... KeyError ...<br><\/strong>\nend program.<br><br><strong>*LOOP OVER DICT KEYS AND VALUES.<br><\/strong><br>begin program python3.<br>for key,value in myDict.items():<br>&nbsp;&nbsp;&nbsp;&nbsp;print(key,value)<br>end program.<\/div><!--class='code'-->\n\n<h2 id=\"int\">Python Integer Object<\/h2> \n\n<p>An &ldquo;int&rdquo; in Python indicates an integer number: a number without decimal places.<\/p>\n\n<div class='code'><strong>*CREATE AND INSPECT TUPLE.<br><\/strong><br>begin program python3.<br>myInt = 5<br>print(type(myInt)) <strong># &lt;class &#39;int&#39;&gt;<br><\/strong>\nprint(myInt) <strong># 5<br><\/strong>\nend program.<br><br><strong>*NOTE THAT \/ OPERATOR INDICATES DIVISION FOR INT \/ FLOAT.<br><\/strong><br>begin program python3.<br>print(myInt \/ 2)<br>end program.<\/div><!--class='code'-->\n<p>Note that a number <em>with<\/em> decimal places is a <a href=\"https:\/\/www.spss-tutorials.com\/overview-python-object-types\/#float\">float<\/a> or a decimal in Python.<\/p>\n\n\n<h2 id=\"float\">Python Float Object<\/h2> \n\n<p>Numbers with decimal places are floats or decimals in Python. The syntax below demonstrates a handful of basics.<\/p>\n\n<div class='code'><strong>*CREATE PYTHON FLOAT OBJECT.<br><\/strong><br>begin program python3.<br>myFloat = 3.0<br>print(type(myFloat)) <strong># &lt;class &#39;float&#39;&gt;<br><\/strong>\nprint(myFloat) <strong># 3.0<br><\/strong>\nend program.<br><br><strong>*FOR FLOAT, + OPERATOR INDICATES NUMERIC ADDITION.<br><\/strong><br>begin program python3.<br>print(myFloat + 1) <strong># 1.5<br><\/strong>\nend program.<\/div><!--class='code'-->\n<p>Floats in Python are single-precision floating point numbers. In contrast, <em>all<\/em> numbers in SPSS are double-precision floating-point numbers.<\/p>\n\n\n<h2 id=\"function\">Python Functions<\/h2> \n\n<p>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.<\/p>\n\n<div class='code'><strong>*DEFINE PYTHON FUNCTION.<br><\/strong><br>begin program python3.<br>def myFunction(myName = &#39;Ruben&#39;):<br>&nbsp;&nbsp;&nbsp;&nbsp;print(&#39;Hello! My name is {}&#39;.format(myName))<br>end program.<br><br><strong>*INSPECT FUNCTION.<br><\/strong><br>begin program python3.<br>print(type(myFunction)) <strong># &lt;class &#39;function&#39;&gt;<br><\/strong>\nprint(myFunction) <strong># &lt;function myFunction at 0x00000000030A3978&gt;<br><\/strong>\nend program.<br><br><strong>*RUN FUNCTION WITH(OUT) ARGUMENT.<br><\/strong><br>begin program python3.<br>myFunction() <strong># Hello! My name is Ruben<br><\/strong>\nmyFunction(myName = &#39;Chrissy&#39;) <strong># Hello! My name is Chrissy<br><\/strong>\nend program.<\/div><!--class='code'-->\n<p>Note that this function takes at most 1 argument: myName. If no argument is passed, it defaults to &ldquo;Ruben&rdquo;.<\/p>\n\n<p>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.<\/p>\n\n<p>A lesson in which we'll define and use Python functions is <a href=\"https:\/\/www.spss-tutorials.com\/spss-cloning-variables-with-python\/\">SPSS - Cloning Variables with Python<\/a>.<\/p>\n\n\n<h2 id=\"module\">Python Modules<\/h2> \n\n<p>A Python module basically consists of one or more text files containing Python code with the .py extension. <\/p>\n\n<div class='code'><strong>*IMPORT AND INSPECT MODULE.<br><\/strong><br>begin program python3.<br>import os<br>print(type(os)) <strong># &lt;class &#39;module&#39;&gt; <br><\/strong>\nprint(os) <strong># &lt;module &#39;os&#39; from &#39;C:\\\\Program Files\\\\IBM\\\\SPSS\\\\Statistics\\\\Python3\\\\lib\\\\os.py&#39;&gt;<br><\/strong>\nend program.<\/div><!--class='code'-->\n<p>Python modules typically define classes and functions that are related to specific tasks such as<\/p>\n\n<ul>\n\t<li>working with regular expressions (Python re module);<\/li>\n\t<li>interacting with Excel files (openpyxl module);<\/li>\n\t<li>creating, moving, copying or deleting files and\/or folders (Python os module).<\/li>\n<\/ul>\n\n<p>We'll create some very simple SPSS Python modules in <a href=\"https:\/\/www.spss-tutorials.com\/spss-cloning-variables-with-python\/\">SPSS - Cloning Variables with Python<\/a>.<\/p>\n\n\n\n<h2 id=\"bool\">Python Boolean Object<\/h2> \n\n<p>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).<\/p>\n\n<div class='code'><strong>*CREATE AND INSPECT PYTHON BOOLEAN OBJECT.<br><\/strong><br>begin program python3.<br>myBoolean = True<br>print(type(myBoolean)) <strong># &lt;class &#39;bool&#39;&gt;<br><\/strong>\nprint(myBoolean) <strong># True<br><\/strong>\nend program.<br><br><strong>*USE BOOLEAN IN IF STATEMENT.<br><\/strong><br>begin program python3.<br>if myBoolean:<br>&nbsp;&nbsp;&nbsp;&nbsp;print(&#39;Yes!&#39;)<br>else:<br>&nbsp;&nbsp;&nbsp;&nbsp;print(&#39;No!&#39;)<br>end program.<br><br><strong>*IMPLICIT BOOLEAN IN IF STATEMENT.<br><\/strong><br>begin program python3.<br>if &#39;a&#39; in &#39;banana&#39;:<br>&nbsp;&nbsp;&nbsp;&nbsp;print(&#39;Yes!&#39;)<br>else:<br>&nbsp;&nbsp;&nbsp;&nbsp;print(&#39;No!&#39;)<br>end program.<br><br><strong>*USE BOOLEAN AS FUNCTION ARGUMENT.<br><\/strong><br>begin program python3.<br>myList = [5,2,8,1,9]<br>print(sorted(myList,reverse = True)) <strong># [9, 8, 5, 2, 1]<br><\/strong>\nend program.<\/div><!--class='code'-->\n\n<h2 id=\"range\">Python Range Object<\/h2> \n\n<p>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).<\/p>\n\n<div class='code'><strong>*CREATE AND INSPECT RANGE OBJECT.<br><\/strong><br>begin program python3.<br>myRange = range(10)<br>print(type(myRange)) <strong>#&lt;class &#39;range&#39;&gt; <br><\/strong>\nprint(myRange) <strong># range(0, 10)<br><\/strong>\nend program.<br><br><strong>*LOOP OVER NUMBERS 1 - 10.<br><\/strong><br>begin program python3.<br>for myInt in range(1,11):<br>&nbsp;&nbsp;&nbsp;&nbsp;print(myInt)<br>end program.<\/div><!--class='code'-->\n\n<h2 id=\"nonetype\">Python NoneType Object<\/h2>\n\n<p>A NoneType object in Python indicates an empty object that has been declared but does not have any value (yet).<\/p>\n\n<div class='code'><strong>*CREATE AND INSPECT NONETYPE OBJECT.<br><\/strong><br>begin program python3.<br>myNone = None<br>print(type(myNone)) <strong># &lt;class &#39;NoneType&#39;&gt; <br><\/strong>\nprint(myNone) <strong># None<br><\/strong>\nend program.<\/div><!--class='code'-->\n<p>When reading SPSS data values with the spssdata module, <a href=\"https:\/\/www.spss-tutorials.com\/spss-missing-values\/ \">missing values<\/a> usually result in NoneType objects in Python. Also, optional arguments sometimes have None as their default in Python functions.<\/p>\n\n\n<h2>Final Notes<\/h2>\n\n<p>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.<\/p>\n\n<p>Thanks for reading!<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>This lesson presents an overview of the most important Python object types. We&#8217;ll also highlight some of our objects&rsquo; main methods.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[289],"tags":[],"class_list":["post-159","post","type-post","status-publish","format-standard","hentry","category-python-reference-tutorials"],"_links":{"self":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts\/159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/comments?post=159"}],"version-history":[{"count":0,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts\/159\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/media?parent=159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/categories?post=159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/tags?post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}