You can help by commenting or suggesting your edit directly into the transcript. We'll review any changes before posting them. All comments are completely anonymous. For any comments that need a reply, consider emailing firstname.lastname@example.org.
We are experiencing playback issues from our video hosting provider. Please check back shortly.
This lesson is part of the Scripting in Ignition course. You can browse the rest of the lessons below.
2:47Basic Python - Variables and Comments
5:19Basic Python - Lists and Dictionaries
5:29Basic Python - Dates, Colors, and JSON Strings
6:58Basic Python - Flow Control
9:44Working with Datasets
Take topic challenge
Take topic challenge
Basic Python - Lists and Dictionaries
Learn about Python data structures and how they can be used in your project.
Video recorded using: Ignition 8.0
Transcript(open in window)
[00:00] In this video, we're going to be going over some of the more complex Python data types: lists and dictionaries. Lists are simply a comma-separated collection of items within square brackets. You'll notice here on line 15 I've created a variable called "myList" and I've assigned it a list value, which has values 1, 2, and 3 inside of it. To access a value in the list, you simply need to provide the variable name in this case, "myList", and then in square brackets at the end list the index of the object you want. Items in the list are zero-indexed, meaning by asking for the item in the list with index one I'm essentially asking for the second item within the list. I can use the print command to print out this value to the console on the right hand side. So when I hit the execute button down below, you should see that I get a value of 2, because 2 is the item at index location one within my list. If I wanted to I can change the index that I'm looking for to say two and it will now give me the third item in the list, in this case, a value of 3. Lists are simply collections of objects, meaning they don't have to be the same data type. You'll notice on line 18 I have a list with three different values again. However this time the first value is a string, "A", the second value is an integer, 15, and the last value is a float, .78. In addition to basic data types, list objects can be any other object including lists themselves. You'll notice on line 19 I have a final list here. This list also has three items in it. The first item is an integer of value 12, the second item is a list object, and the third item is the value 18. Another important data type is the dictionary. Dictionaries work much like lists do except they allow you to organize the data a little bit better. A Python dictionary is simply a unique list of items each of which has a definition. These item-definition combos are what's known as key-value pairs. Up here on line 26 I've made a dictionary variable. Like a real dictionary, the dictionaries in Python have items, and then each item also has a value. These items and values or keys and values, are separated by colons. Each key-value pairing is then separated by a comma, and then the entire dictionary is encapsulated in curly braces. So in my example here I have a dictionary with three items, or keys: A, B, and C. Each key then has a value. A has a value of 15, B has a value of test, and C has a value of 5.5. Accessing a value within a dictionary works much in the same way that we access the value within the list. Here on line 27 you can see I place the name of my dictionary, and then at the end I've placed square brackets, and within the square brackets, you place the key of the value that you want to access. In this case I've placed B in there, letting the system know that I want to access the value at key B. If I execute this code you'll notice over on the right hand side it gives me a value of test which is the value of the key B. In addition to the basic data types, dictionary values can also be the more complex types, like lists or dictionaries. Take my example on line 29. I have a dictionary here that has two items. The items "jdoe" and "jsmith". The value of each of those items is then a dictionary. Accessing items in the dictionary still works in the same way though. For example, on line 30 I'm trying to access the item within my dictionary called "jsmith". When we print that out, you'll notice it returns the value of "jsmith", which in this case is the entire dictionary. If I wanted to access a specific value within the inner dictionary, I simply need to place another set of square brackets and a new key like I've done on line 31. Here in my new dictionary I'm asking for the value of key "jsmith" and once I have that value I'm then asking for the value at the key age, which in this case is 44. The next video we'll go over some additional data types that may be useful to you.