It's easy to mix and match data structures in Python because everything is a first-class object. Any element of a collection may be any object that meets the criteria for the containing type.
- [Instructor] In Python, everything is an object and variables store references to objects. This means that it's possible to store anything in a data structure. Here in Komodo, I've opened a working copy of mixed.py from chapter eight of the exercise files. And you notice here in my main function, I have a rather complicated set of stuff. I have r is a range, l is a list, and that list includes integers, strings, a dictionary, and all kinds of stuff.
T is a tuple, which again, includes strings and a None value in the middle of it. S is a set, d is a dictionary. And then I have this mixed which is a list of all of these different structures. And then I have this display function, which I'll show you in a minute which will display the mixed. And I'm just going to run it right now and you can see down here, here's all our different elements. So first is the list, and you remember the list has one, two, and it's got a dictionary in the middle of it.
Then is the range. Then I have the set. And so that's all of the characters in the set. Then I have the dictionary, and see the dictionary has one is r, which is the range, two is l which is the list. Three is s which is the set. So we have all this stuff in that dictionary there. And then we have the tuple with the one, two, none, which is displaying as Nada because of the way that I have my display function set up.
So let's take a look at the display function. You can see that this is a really interesting data structure and what I've done is I've used isinstance to determine is it a list, is it a range, is it a tuple, is it a set? And I have different print functions for each of those. And you notice this function is called display and each of these print_list, print_tuple, they each call display for the elements within it. And so it can test and see if it has to print another structure or if it's none, it's going to print the word Nada.
Or otherwise, if it's none of those things, it's just going to print a representation. I also have this global variable here, dlevel to keep track of my nesting level so that when I get to an outer level, I can print a new line. And so when I run it, this is the result that we get. You can see that our structures can be as complex as we want them to be and it's possible to iterate through these structures and introspect each element of each of these structures and decide what to do with it.
And so I just wanted to show you this. I know it's kind of complex, but this is the power of Python's object model. Everything is an object. Objects may contain references to other objects including your own defined classes and objects. And we'll get to that in a later chapter. And this allows you to create virtually any structured data.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
Learning Python Web Penetration Testingwith Christian Martorella2h 49m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.