Locked lesson.
About this lesson
We'll look at data types including numbers, strings, lists, tuples, and dictionaries.
Exercise files
Download this lesson’s related exercise files.
Data Types.docx58.8 KB Data Types - Solution.docx
58.8 KB
Quick reference
Data Types
There are five main data types that we'll look at.
When to use
Data types are used to store different types of data.
Instructions
There are five main data types:
- Strings
- Numbers
- Lists
- Tuples
- Dictionaries
Hints & tips
- You'll choose a data type based on the type of data you'll be working with
- Strings, Numbers, Lists, Tuples, and Dictionaries
- 00:04 In this video, I want to talk about data types.
- 00:07 Python has a bunch of different data types.
- 00:08 We're going to be looking at five main data types, strings,
- 00:11 numbers, lists, tuples and dictionaries.
- 00:14 And we've already kind of looked at a couple of these strings and numbers.
- 00:17 So, what are data types?
- 00:19 Well, we've got our variable first_name = "John", and John is a string,
- 00:24 what kind of data is this?
- 00:25 It's a string, that's the type of data, data type.
- 00:29 So we already know about strings, strings can have single quotes or
- 00:32 double quotes and we'll get into that a little bit later.
- 00:35 In this video I'm going to give a very,
- 00:37 very quick overview of each of these five things.
- 00:39 We're going to get into them in greater detail later on and
- 00:42 really learn about them much further.
- 00:44 For this video, I just want you to sort of become aware that these are the main data
- 00:48 types that you're going to deal with with Python.
- 00:50 Like I said, Python has a bunch of other data types, but these are the main ones,
- 00:53 they're going to get you like 90% of the way to whatever you ever want to do.
- 00:56 And so that's what we're going to look at.
- 00:58 So we have strings, you could also have a number 41,
- 01:00 and notice the number isn't wrapped in a quotation marks.
- 01:05 If you did wrap this in a quotation mark, it's no longer a number.
- 01:08 Now it's a string, because strings are wrapped in quotation marks.
- 01:11 So like I said, we're going to get into these in more detail later on.
- 01:14 The next one is a list, and a list is an array.
- 01:17 Basically, in any other programming language, we call them arrays, but
- 01:20 Python calls them lists.
- 01:22 They're just lists of things.
- 01:23 And to create one, you use these square brackets and you just go ["John"],
- 01:28 you separate the items in your list with commas.
- 01:34 So John, Tim, and Mary and we can access this just like with a variable.
- 01:40 Print this, we get John, Tim, and Mary.
- 01:42 We can pick out a certain item from our list using these brackets,
- 01:46 now lists start out with zero, this is the zeroth item, this is the first item and
- 01:51 this is second item.
- 01:52 So if we wanted to print out John,
- 01:53 we would print out it's index number which is zero, save this and run it, boom, John.
- 01:58 So those are lists, you can change them, you can add things to them,
- 02:01 you can remove things.
- 02:03 They are what we call mutable and they're ordered from zero, up zero, one, two,
- 02:07 three, four, five, whatever, however many you have, so those are lists.
- 02:11 The next thing is a tuple, and a tuple is just like a list.
- 02:14 It looks exactly like a list.
- 02:15 The only difference is, at least in the way it looks,
- 02:18 is instead of the square brackets, you use these parentheses.
- 02:21 So now it's no longer a list.
- 02:23 Now this is a tuple.
- 02:24 And it's kind of a weird name, tuple, right?
- 02:26 But the thing about tuples is you can't change them.
- 02:29 So with lists, you can add things and remove things, and do all kinds of stuff.
- 02:32 With tuples, once you create them, they're created.
- 02:34 You can't do anything else.
- 02:36 And you can access them in the exact same way as a list.
- 02:40 So boom, we get John if we change this to 1,
- 02:43 we get Tim, have we removed it completely.
- 02:46 It'll print out with the parenthesis, so you know it's a tuple.
- 02:49 So, those are tuples, lists and tuples, very similar.
- 02:52 The last one we're going to look at is a dictionary.
- 02:54 And a dictionary is what we call a hash in most other programming languages,
- 02:59 Python calls them a dictionary.
- 03:00 And they are also a collection of items, but they are unordered.
- 03:04 Where a list and a tuple are ordered, zero, one, two,
- 03:07 three, a dictionary is not ordered.
- 03:09 It's ordered by something called a key and a value pair.
- 03:12 And so to create one of these, let's rename this fav_pizza.
- 03:18 And we use these, instead of the square brackets we use these curly brackets, right?
- 03:23 And you often put them on multiple lines.
- 03:25 So like I said, key value pair, you start out with your key and
- 03:29 let's call this John and use a colon and then the value, and John likes
- 03:34 pepperoni pizza and you separate each item with a comma just like a list and a tuple.
- 03:39 So now we have Tim.
- 03:42 Tim likes "Mushroom" and you can put all these on one line, but
- 03:46 we break them apart into two lines just because it's easier to read.
- 03:49 Mary, likes "Cheese".
- 03:53 And that's it.
- 03:53 So, to access these fav_pizza, we can print out the whole thing
- 03:58 just like with a list and a tuple, we get this thing.
- 04:02 If we want a specific item, same thing, we use the square brackets which doesn't make
- 04:05 a lot of sense, you would think you would use curly brackets, but no, square.
- 04:09 And instead of calling a number like you would with a list or
- 04:12 a tuple, you call the key.
- 04:13 So the first item is a key, the second item is a value.
- 04:16 Key, value.
- 04:18 Key, value, key, value.
- 04:19 So if you want to know what Tim likes on his pizza, you just type in Tim.
- 04:23 So you don't have to know which number it is, you just know the key is Tim.
- 04:27 We save this, run it, boom, mushroom.
- 04:29 want to know what John likes?
- 04:31 Type in John, boom, pepperoni.
- 04:33 So those are the five main data types that you're going to run across most often
- 04:38 in Python.
- 04:39 Like I said, there's some other ones, but they're sort of obscure and
- 04:41 we don't need to learn them at this point in the game.
- 04:43 Strings, numbers, lists. tuples and dictionaries.
- 04:46 Like I said, in the next few videos, we're going to dive into each of these in
- 04:49 more detail, learn how to do things with them, and learn more about them.
- 04:52 So in the next video we're going to jump in and
- 04:57 look at the first one which is strings.
Lesson notes are only available for subscribers.