Python 3 Programming Tutorial - Dictionaries

sentdex · Beginner ·🛠️ AI Tools & Apps ·11y ago

Key Takeaways

This video tutorial covers the basics of Python dictionaries, including creating, referencing, and modifying dictionaries, as well as using them to store and retrieve data.

Full Transcript

What's going on everybody? Welcome to yet another Python 3 basics tutorial video. In this video, we're going to be talking about dictionaries. So, dictionaries are kind of they're really Python specific, but they're a lot like associative arrays and other languages. Um, and the idea of them is they're an unordered unordered unordered assortment of data. And the data is always keys and values. And again, it's unordered. So, you might enter it in an order and then you might expect an order out of it. Or maybe you want to order your dictionary. You you can kind of hacky order a dictionary, but not really. There is no order inherently to a dictionary. So anyway, again, the idea of a dictionary is to have keys and values. A key will be a single element. Values can be anything. It can be lists, it could be lists within lists, multi-dimensional lists. Your values can be functions, which can get exciting fast. Uh it can be anything. So anyway, uh with that, you can even reference another dictionary in a dictionary. So yeah, uh let's go ahead and show some examples. So we're going to say x dict equals. And to notify Python that you're going to create a dictionary, you use curly braces. And then within the curly braces, let's say we have a a collection of people and their ages. So we're going to say we got Jack. He's 15 years old. We have ourselves a Bob who is 22. We have ourselves an Alice who is she's 12. And then we finally have a Kevin. And Kevin is 17 years old. Okay. So, we have this example dick. Now, just for quick uh kicks, print example dicty reads it. As you can see, it's almost an exact print out of exactly what we just typed. Easy enough. Now, what if we want to reference something in this dictionary? We want to ask this dictionary, hey, how old is Jack? Example, dick. Well, you go print eggs dict. [Music] So, this should be kind of familiar to you guys as far as uh lists are concerned. We're kind of referencing a bit of data. And so Jack is the key and then the value is this number right here. So hopefully it's going to tell us 15. Sure enough it does. Now uh the next thing that we're going to do is so we found out how old Jack is. That's cool. We can reference this. How do we let's say we want to add something to this list. So for example, let's say we want to add Tim. So to do that we'll say exact xdict Tim. So this is basically exactly how we referenced Jack, right? We said xdictked Jack. Just print out that that value. Now we're saying xdict Tim, which doesn't exist already, but we're about to make it exist. He's 14. Okay, so now we can print eggs dicty ears. Sorry, guys. And then we have Tim. Okay, so see how it kind of just added Tim like almost like randomly into this. Um, so yeah, keep that in mind. It's there's no order really to it, but anyway, it added Tim. Now, let's say uh Tim just turned 15 years old, big five. So, now we're going to say example dict Tim equals 15. So, as you can see, we already defined Tim here, and now we're like redefining Tim. Um, and so you'll see this is why dictionaries have to have unique keys. Otherwise, it everything would fall apart. The sky would literally fall. Whoops. We need to print Tim again. Uh, so example dick Timals 15. Look how it uh this this time this time it put Tim second. This time it puts Tim all the way at the end. Wow, crazy stuff. Anyway, Tim's 15. Then we'll print example dict. And we can see at first Tim was 14. Now Tim is 15. Fancy that. Sadly, however, Tim has died. We need to remove him from this list. He's no use being in this list anymore. He's dead. So now, how do we get rid of this Tim guy? Well, let's make some space here. Now, to get rid of something, we can just do dell de example dict Tim. This is going to remove the key and value or values to Tim. Um, whoops. We got to let's print it out after deleting Tim. X dict. So, he was here. Great. 14. Turn 15. Good for him. Then he died. Bummer. Now he's dead. Done. So, um, now just as a quick aside, so this is our list. Let me get rid of all this now. And let's say instead, or not instead, but now, not only are we tracking age, but we're going to track hair color. So Jack, he's 15, and his hair color is blonde. Bob is 22. And Bob's hair color is uh What color is Bob's hair? Brown. I heard brown from the crowd. Now, Alice, Alice is 12 years old, but Alice's hair is black. And we're running out of space, but here's Kevin. Kevin's a ginger, so he's got red head. Sorry if I offended anybody. Um, there we go. Good. Now, we can print. Let's do print example dict uh Jack. Oh, okay. My Spidey sense was tingling that I did something and I sure did. Python 27 coming out of me. Anyway, 15 and blonde. Okay, correct. And we can even go like this. We can say, well, we want to reference just his hair color. Boom. Blonde. Nice. Okay, so this list could go forever if you wanted. You could also create other dictionaries and you could say Jack is equal to another dictionary. You can create functions and say Jack is equal to a function. Um, you can do all kinds of stuff. We'll probably expand on that a little bit more once I get through all of the kind of basic things I want to get through with Python uh 3. Uh, then we'll get into some more in-depth kind of hacky things, some more intermediate things, that kind of stuff. Uh, so anyways, if you guys have any questions or comments, feel free to leave those below. Also, I'm still kind of making this series. Probably some of you first viewers to the series. Um, I'll still be generating this Python 3 series. If you have any requests or anything you definitely want to see, I highly suggest you request them. Uh, that way they show up for you guys. Um, but anyway, someone's at the door. So, I'm going to conclude this video here. If you guys have any questions or comments, leave them below. As always, thanks for watching. Thanks for all the support subscriptions and until next

Original Description

In this Python 3 programming tutorial, we cover Python dictionaries. Dictionaries are a data structure in Python that are very similar to associative arrays. They are unordered and contain "keys" and "values." Each key is unique and the values can be just about anything. Dictionaries are sets and they are defined with {} curly braces. Sample code for this basics series: http://pythonprogramming.net/beginner-python-programming-tutorials/ Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M http://seaofbtc.com http://sentdex.com http://hkinsley.com https://twitter.com/sentdex Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This video tutorial teaches the basics of Python dictionaries, including how to create, reference, and modify them, and how to use them to store and retrieve data. By the end of this tutorial, viewers will be able to work with dictionaries in Python and use them to solve real-world problems.

Key Takeaways
  1. Create a dictionary using curly braces
  2. Reference a value in a dictionary using its key
  3. Modify a value in a dictionary
  4. Add a new key-value pair to a dictionary
  5. Remove a key-value pair from a dictionary
💡 Dictionaries are a powerful data structure in Python that allow for efficient storage and retrieval of data, and are particularly useful when working with large datasets or complex data relationships.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →