Skip to content
Coffing Data Warehousing Software

Understanding Python Basics: Part Three

This post is the third part of a series on the Python Basics. Here you can find Part One and Part Two. In this series, we’ll teach you about the very basics of the Python programming language. In Part 3, you’ll be learning about the differences between a Function, a List, and a Tuple.

If you want to use the Nexus as your Python editor, then you will find instructions at the end of the blog on how to set this up!

 


Tera Tom here! In our last post, we talked about defining variables, mixing math and strings, handling single and double quotes, and slicing strings in Python. Now it’s time to dive into something more complicated! We’re going to speak about the differences between a Function, a List, and a Tuple.

If you have a Python editor you want to use then you are all ready to go, but if you want an easy way to try these examples then go to www.CoffingDW.com and download the Nexus.  Once you have it installed, just look at the picture below and see where the Python editor of Nexus exists.

The Differences Between a Function, List, and Tuple  

A function starts with the keyword def and is a set of code you expect to use multiple times.  We are applying this function, named func_tom to our tuples and our lists. The difference between a list and a tuple is that a list can be changed.  The term for an object that can be changed or updated is mutable. A tuple cannot be changed so it is considered immutable.

 

Creating Lists  

A list creates a list of values and then allows you to manipulate them.  Each value has a sequential index with the first value’s index = 0. An index is similar to a label so that we can manipulate a value in our list, based on its order in the list. There are three examples below.  First, we create our list and then we print our list. Finally, we are printing the first item in our list.

 

Modifying a List  

Below, in line three we are modifying the list named My_Workout_List.  We have chosen to modify (update) the first entry in the list, and this is accomplished with the index of [0].  All list entries start with an index of zero (0) and increment by 1. The old list had the value as ‘Weights’ and the new list has changed that value to ‘Barbell Weights’.  What a strong example!

 


Printing Portions of a List
 

The last print example below shows exactly how to print out a portion of a list.  You must remember that list value indexes start at 0 and increment by 1. So, a print command of [1:4] means print from 1-3 or from one up to four but exclude index 4.


Creating New Lists with Portions of other Lists
 

The Morning_Workout_List example shows exactly how to build a list that takes portions from another list.  A list index starts at 0 and increments by 1. So, our new list named Morning_Workout_List takes the first [0] and second [1] entries only from the old list and excludes the third entry [2].


Creating New Lists From Multiple Lists
 

You can build lists from multiple lists.  We are doing that with the list called My_Schedule.  It contains values from two lists. The first is named Morning_Workout_List and the second is named Evening_Workout_List.  Notice, when we print the My_Schedule list that the values are separated with square brackets to show that they come from two separate lists.


Tips – Building A Unified List with a Loop

The example below unifies three different lists into one and does so using a for loop.


Creating a Tuple
 

A Tuple is a list of values that cannot be changed.  That means it is immutable. You can change a list (mutable), but not a Tuple.  The Tuple uses ( ) round braces when created, but a list uses [ ] square brackets. Why would you use a tuple instead of a list?  So, you can be assured that the tuple can’t be changed. If you create a tuple with four elements inside, it will always have four elements inside.  Consider a list written in pencil, but a tuple written in stone!


Tuples – Finding the Number of Values, Min, and Max Values
 

A Tuple is a list of values that cannot be changed.  You can change a list, but not a Tuple. Our examples show us how to get the number of values, the first value and the last value in the tuple using the ‘len’, ‘min’ and ‘max’ commands.


Creating Dictionaries or Maps
 

A Dictionary/Map is made up of values with a unique key for each value stored.  A dictionary or map (interchangeable terms) are similar to lists, but you can’t join a dictionary or map using a + sign like you can a list.


Finding Individual Entries in Dictionaries/Maps
 

The example below shows how to get the value for a unique key in our dictionary.  We put in ‘California’ (key) and it returns the state capital value of ‘Sacramento’.


Finding Individual Entries in Dictionaries/Maps Using Get
 

Our example below shows how to get the value for a unique key in our dictionary.  We put in ‘Alaska’ (key) with the keyword ‘get’ and it returns the state capital value of ‘Juneau’.


Finding Individual Entries in Dictionaries/Maps Using Get
 

Our example below shows how to get the value for a unique key in our dictionary.  We put in ‘Alaska’ (key) with the keyword ‘get’ and it returns the state capital value of ‘Juneau’.


How to Install and Setup Python on your Nexus

  1. Download the latest version of Nexus from our website.  https://www.coffingdw.com/nexus-trial/
  2. Once installed download Python for Windows.  https://www.python.org/downloads/windows/

Your version number can vary, but you will want to download the Windows installers. So for Python for Windows 3.7.3 located here (https://www.python.org/downloads/release/python-373/) you will select one of these highlighted links:

Note:  Nexus comes in three versions:  Nexus 64-bit, Nexus 32-bit, and Nexus WOW-64 (For 64-bit machines with 32-bit Microsoft Office installed).X86-64 can be used for the 64-bit or Wow64 version of Nexus and x86 can be used only on the 32-bit version of Nexus.

When installing, if you have the option, select “Add Python to PATH”.

If you are using an older version of Python and do not have this option, then you will need to add the path to your python executable to your PATH environment variable. Be careful when editing this variable as it can affect many Windows functionality issues. If you don’t feel comfortable about this then consult with your desktop support.

Python 3.7.3 was installed in the following directory on our PC.

C:\Users\Todd\AppData\Local\Programs\Python\Python37

You can view your Python directory by opening File Explorer and pasting in the following path:

%LocalAppData%\Programs\Python

Once you have the path to the python.exe executable then add it to your Environment PATH variable by following the direction in this link:

https://www.computerhope.com/issues/ch000549.htm

And that is how you install Python into the Nexus Query Chameleon!

Ready for more Python training? Keep an eye out for more articles here on our blog! Or, if you’d prefer Tera-Tom to come teach at your organization, contact our team for booking information.