Types of dictionaries list12/15/2023 A has a value of 15, B has a value of test, and C has a value of 5.5. So in my example here I have a dictionary with three items, or keys: A, B, and C. Each key-value pairing is then separated by a comma, and then the entire dictionary is encapsulated in curly braces. These items and values or keys and values, are separated by colons. Like a real dictionary, the dictionaries in Python have items, and then each item also has a value. Up here on line 26 I've made a dictionary variable. These item-definition combos are what's known as key-value pairs. A Python dictionary is simply a unique list of items each of which has a definition. Dictionaries work much like lists do except they allow you to organize the data a little bit better. Another important data type is the dictionary. The first item is an integer of value 12, the second item is a list object, and the third item is the value 18. You'll notice on line 19 I have a final list here. In addition to basic data types, list objects can be any other object including lists themselves. However this time the first value is a string, "A", the second value is an integer, 15, and the last value is a float. You'll notice on line 18 I have a list with three different values again. Lists are simply collections of objects, meaning they don't have to be the same data type. 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. 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. I can use the print command to print out this value to the console on the right hand side. 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. 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. 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. Lists are simply a comma-separated collection of items within square brackets. In this video, we're going to be going over some of the more complex Python data types: lists and dictionaries.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |