Data Types in Python | Python for Beginners

Alex The Analyst
1 Nov 202221:58

Summary

TLDRThis educational video script delves into Python's data types, essential for understanding how data is stored and manipulated. It covers numeric types like integers, floats, and complex numbers, explaining their characteristics and operations. The script progresses to Boolean values, emphasizing true or false states, and sequence types including strings, lists, and tuples, highlighting their indexing and mutability. It distinguishes between lists and tuples, noting the latter's immutability. Sets are introduced as unordered collections without duplicates, and their operations for unique value comparison are discussed. The script concludes with dictionaries, Python's key-value pair data structures, illustrating how they store and update data. The comprehensive overview is designed to solidify viewers' grasp of Python's foundational data types.

Takeaways

  • 🔢 Data types in Python are essential for understanding how data is stored and manipulated, with numeric, sequence, set, Boolean, and dictionary types being the main categories.
  • 📏 Numeric types include integers (whole numbers), floats (decimal numbers), and complex numbers (used for imaginary numbers).
  • 🌟 Boolean type represents one of two values: True or False, and is often used in conditional statements and comparison operations.
  • 📝 Sequence types consist of strings, lists, and tuples, with strings being arrays of bytes representing Unicode characters, and lists being ordered, mutable collections of items.
  • 🍦 Tuples are similar to lists but are immutable, meaning they cannot be changed after creation, and are often used for data that should not change.
  • 🔑 Sets are unordered collections of unique elements, without indices, and are useful for membership testing and eliminating duplicates.
  • 📖 Dictionaries store data in key-value pairs, allowing for fast retrieval of values based on keys, and are mutable, allowing for updates and deletions.
  • 🔍 Indexing in Python is used to access elements in data structures like strings and lists, starting from position 0, and can include negative indexing for reverse access.
  • 🔄 Operations like addition, multiplication, and concatenation can be performed on strings and lists, but not on tuples due to their immutability.
  • 🏷 Sets support operations like union, intersection, and difference, which are useful for comparing and manipulating collections of unique items.
  • 🛠️ Dictionaries can be dynamically updated and elements can be added, modified, or removed using keys, providing a flexible way to handle data.

Q & A

  • What are the main data types in Python discussed in the script?

    -The main data types in Python discussed are numeric, sequence, set, Boolean, and dictionary.

  • What are the three different types of numeric data types in Python?

    -The three different types of numeric data types in Python are integers, floats, and complex numbers.

  • How can you verify the data type of a variable in Python?

    -You can verify the data type of a variable in Python by using the 'type()' function, such as 'type(12)' to check if it's an integer.

  • What is the difference between an integer and a float in Python?

    -An integer in Python is a whole number, positive or negative, without a decimal point. A float is a number with a decimal point, representing a fractional part.

  • What is a complex number in Python and how is it represented?

    -A complex number in Python represents imaginary numbers and is represented by using 'j' as the imaginary unit, such as '12 + 3j'.

  • What are the two built-in values of the Boolean data type in Python?

    -The two built-in values of the Boolean data type in Python are 'True' and 'False'.

  • How do you create a string in Python and what are the different ways to enclose it?

    -You create a string in Python by enclosing characters in either single quotes (' '), double quotes (

  • What is the significance of indexing in Python strings and how does it work?

    -Indexing in Python strings allows you to access specific characters or a range of characters within the string. It starts at 0, and you can use positive or negative indices to access characters from the beginning or end of the string.

  • How do lists differ from strings in Python, and what is a key feature of lists?

    -Lists in Python differ from strings in that they can store multiple values, which can be of different data types. A key feature of lists is that they are mutable, meaning you can change, add, or remove items after creation.

  • What is a tuple in Python and how does it differ from a list?

    -A tuple in Python is a collection of values, similar to a list, but it is immutable, meaning its values cannot be changed after creation. Tuples are defined using parentheses.

  • What is a set in Python and how does it differ from a list?

    -A set in Python is an unordered collection of unique elements, which means it does not allow duplicate values. Unlike lists, sets do not support indexing and are defined using curly braces.

  • How can you compare two sets in Python to find common or unique elements?

    -You can compare two sets in Python using set operations like union ('|'), intersection ('&'), difference ('-'), and symmetric difference ('^') to find common or unique elements between them.

  • What is a dictionary in Python and how is it different from other data types?

    -A dictionary in Python is a collection of key-value pairs, where each key is unique. It is different from other data types in that it uses keys to access values, rather than using indices like in lists or tuples.

  • How do you access values in a dictionary in Python?

    -In Python, you access values in a dictionary by using the keys, such as 'dictionary_name['key_name']'.

  • Can you update or delete values in a Python dictionary? If so, how?

    -Yes, you can update values in a Python dictionary by assigning a new value to an existing key or by using the 'update()' method. You can delete values using the 'del' statement followed by the key or the 'pop()' method.

Outlines

00:00

🔢 Introduction to Python Data Types

This paragraph introduces the concept of data types in Python, explaining that they are classifications of data that determine the operations that can be performed on them. The video focuses on the main data types in Python, including numeric (integers, floats, and complex numbers), sequence types, sets, Boolean, and dictionaries. The speaker demonstrates how to check data types using the 'type()' function and provides examples of operations on integers, floats, and complex numbers, highlighting the differences between them.

05:03

📖 Exploring Python's Sequence Types and Strings

The speaker delves into sequence types, starting with strings. They explain that strings in Python are arrays of bytes representing Unicode characters and can be enclosed in single, double, or triple quotes. The paragraph covers string indexing, where the index starts at zero, and demonstrates how to access parts of a string using positive and negative indices. It also touches on string concatenation and repetition. The discussion then shifts to lists, which are mutable and can hold multiple values. Lists are indexed like strings, and the speaker shows how to create lists with both numeric and string values, including nested lists. The paragraph concludes with a demonstration of how lists can be modified after creation using methods like 'append' and how to change list items by index.

10:04

🍦 Tuples, Sets, and Their Unique Characteristics

This paragraph discusses tuples, which are similar to lists but are immutable, meaning they cannot be changed after creation. The speaker shows how to create a tuple and access its elements using indexing. Unlike lists, tuples do not support methods like 'append'. The paragraph then moves on to sets, which are unordered collections of unique elements and do not support indexing. The speaker demonstrates how to create a set and discusses the use of sets for comparing unique values between different collections. Various set operations like union, intersection, and difference are explained with examples.

15:05

📚 Diving into Python Dictionaries

The speaker introduces dictionaries, which are a collection of key-value pairs. Unlike lists and tuples, dictionaries are not indexed by integer indices but by keys. The paragraph explains how to create a dictionary and access its values using keys. It also covers methods to retrieve all keys, values, or items from a dictionary. The speaker demonstrates how to update dictionary values and entire key-value pairs using the 'update' method. The paragraph concludes with a discussion on deleting dictionary keys and their associated values using the 'del' statement.

20:06

👋 Wrapping Up the Data Types Discussion

In the final paragraph, the speaker summarizes the video's coverage of Python data types, expressing gratitude to the viewers for watching. They encourage viewers to like and subscribe for more content, hinting at future videos. The music plays as the video concludes, signaling the end of the data types discussion.

Mindmap

Keywords

💡Data Types

Data types in Python refer to the classification of data, which informs what operations can be performed on that data. In the video, data types are central to understanding how Python handles different kinds of information. Examples include numeric, sequence, set, Boolean, and dictionary types. The script explains that these classifications are crucial for knowing how to manipulate and store data within Python programs.

💡Integers

Integers are a type of numeric data that represent whole numbers, both positive and negative. The video script uses integers to demonstrate basic Python operations, such as addition, and to contrast with other numeric types like floats. For instance, when an integer is added to a decimal, the result is a float, indicating a shift in data type.

💡Float

A float represents a numeric data type that includes decimal points, unlike integers. In the script, the transition from integer to float is illustrated when a whole number is added to a decimal, changing the data type to float. This is significant as it shows how operations can alter the type of data being manipulated in Python.

💡Complex Numbers

Complex numbers are a numeric data type used to represent imaginary numbers. The video mentions complex numbers as a less commonly used data type, denoted by the use of 'j' or 'J' to indicate the imaginary part. This type is important for mathematical operations that extend beyond real numbers.

💡Boolean

Boolean values represent one of two values: True or False. In the video, Booleans are used to illustrate comparison operations, such as checking if one number is greater than another. This type is fundamental in conditional statements and logical operations within Python programming.

💡Sequence Types

Sequence types, including strings, lists, and tuples, are ordered collections of items. The video script discusses how these types allow for indexing and manipulation of data through their ordered nature. For example, strings are used to demonstrate indexing and slicing, which are fundamental operations in text manipulation.

💡Strings

Strings in Python are sequence types used to handle text data. The video explains how strings can be declared using single, double, or triple quotes, and how they can span multiple lines. Strings are indexed, allowing for the extraction of characters or substrings, which is crucial for text processing tasks.

💡Lists

Lists are a mutable sequence type that can hold multiple values of any data type. The script demonstrates how lists are created and how they can be modified, such as adding new items or changing existing ones. Lists are a core data structure in Python, used extensively for storing and manipulating collections of items.

💡Tuples

Tuples are an immutable sequence type similar to lists but cannot be changed after creation. In the video, tuples are presented as suitable for storing data that should not be altered, such as fixed configurations or constant values. The immutability of tuples is a key aspect that differentiates them from lists.

💡Sets

Sets are an unordered collection of unique elements. The video script explains how sets automatically remove duplicate values and cannot be indexed like lists or tuples. Sets are useful for mathematical operations involving collections of unique items, such as finding intersections or differences between groups.

💡Dictionaries

Dictionaries in Python are used to store key-value pairs, allowing for the association of specific identifiers (keys) with values. The video demonstrates how dictionaries are created and manipulated, including adding, updating, and deleting key-value pairs. Dictionaries are essential for creating complex data structures and are widely used in Python for tasks like representing real-world entities with attributes.

Highlights

Introduction to data types in Python, which are classifications that determine what operations can be performed on data.

Explanation of numeric data types including integers, floats, and complex numbers.

Integers are whole numbers, positive or negative, demonstrated with examples.

Floats are non-whole numbers, introduced by performing an operation that results in a decimal.

Complex numbers are used for imaginary numbers and are denoted by 'J'.

Boolean data type with only two values: True or False, used in comparison operations.

Sequence type data includes strings, lists, and tuples, each with unique properties.

Strings are arrays of bytes representing Unicode characters, can be single, double, or triple quoted.

Demonstration of string indexing, showing how to access characters by position.

Lists store multiple values and are indexed, can contain different data types.

Lists are mutable, allowing items to be added, removed, or changed after creation.

Tuples are similar to lists but are immutable, meaning they cannot be changed once created.

Sets are collections without duplicate elements and do not support indexing.

Sets are unordered and can be used to find unique elements or differences between sets.

Dictionaries store key-value pairs and are used for more complex data associations.

Dictionaries allow for the updating and deletion of key-value pairs, but not direct indexing.

Overview of how to access and manipulate data within dictionaries using keys.

Conclusion of the data types video with a summary of the covered topics.

Transcripts

play00:00

hello everybody today we're going to be

play00:01

talking about data types in Python data

play00:04

types are the classification of the data

play00:06

that you are storing these

play00:07

classifications tell you what operations

play00:09

can be performed on your data we're

play00:11

going to be looking at the main data

play00:12

types within python including numeric

play00:14

sequence type set Boolean and dictionary

play00:18

so let's get started actually writing

play00:19

some of this out and first let's look at

play00:21

numeric there are three different types

play00:23

of numeric data types we have integers

play00:25

float and complex numbers let's take a

play00:28

look at integers an integer is basically

play00:30

just a whole number whether it's

play00:32

positive or negative so an integer could

play00:34

be a 12 and we can check that by saying

play00:37

type

play00:38

we'll do an open parentheses and a

play00:40

closed parenthesis and if we say the

play00:42

type of 12 it's going to give us an

play00:44

integer or if we say a negative 12 that

play00:47

is also an integer we can also perform

play00:49

basic calculations like -12 plus 100 and

play00:52

that'll tell us it is also an integer so

play00:55

whether it's just a static value or

play00:57

you're performing an operation on it

play00:58

it's still going to be that data type if

play01:00

those numbers are whole numbers whether

play01:02

negative or positive now let's take this

play01:04

exact one and let's say 12 and we'll do

play01:09

plus

play01:10

10.25 when we run this it's no longer

play01:12

going to be a whole number it'll now be

play01:14

a float so let's check this and now this

play01:17

is a float type because it's no longer a

play01:19

whole number it's now a decimal number

play01:21

and the last data type within the

play01:22

numeric data type is called complex

play01:24

let's copy this right down here now

play01:27

personally this is not one that I've

play01:28

used almost ever but it is one just

play01:31

worth noting so you can do 12 plus and

play01:34

let's say 3 J and if we do this is going

play01:38

to give us a complex the complex data

play01:41

type is used for imaginary numbers for

play01:43

me it's not often used but if you do use

play01:46

it J is used as that imaginary number if

play01:49

you use something like C or any other

play01:52

number it's going to give you an error J

play01:55

is the only one that will work with it

play01:57

now let's take a look at Boolean values

play01:59

so we'll say Boolean the Boolean data

play02:02

type only has two built-in values either

play02:05

true or false so let's go right down

play02:07

here and say type true

play02:11

and when we run this it'll say Bool

play02:13

which stands for Boolean we can do the

play02:15

exact same thing with false and that is

play02:18

also Boolean and this can be used with

play02:20

something like a comparison operator so

play02:22

let's say one is greater than 5. and

play02:26

let's check this this is giving us a

play02:28

Boolean because it's telling us whether

play02:30

one is greater than 5. let's bring that

play02:32

right down here this will give us a

play02:35

false so it's telling us that one is not

play02:37

greater than 5. and just as we got a

play02:39

false we can say 1 is equal to one and

play02:42

this should give us a true so now let's

play02:44

take a look at our sequence type data

play02:46

types and that includes strings lists

play02:48

and tuples we'll start off by looking at

play02:51

strings

play02:52

in Python strings are arrays of bytes

play02:54

representing Unicode characters when

play02:57

you're using strings you put them either

play02:58

in a single quote a double quote or a

play03:00

triple quote I call them apostrophes

play03:02

it's just what I was raised to call them

play03:04

but most people who use Python call them

play03:06

quotes So Right Here we have a single

play03:08

quote

play03:09

and that works well we can do a double

play03:13

quote

play03:14

and that works also and as you can see

play03:16

they are the exact same output and then

play03:19

we have a triple quote just like this

play03:21

and this is called a multi-line so we

play03:24

can write on multiple lines here so

play03:26

let's write a nice little poem so we'll

play03:28

say the ice cream

play03:30

vanquished my longing for sweets

play03:34

upon this diet

play03:36

I look away

play03:38

it no longer exists

play03:42

on this day

play03:43

and then if we run that it's going to

play03:45

look a little bit weird it's basically

play03:47

giving us the raw text which is

play03:50

completely fine but let's call this a

play03:52

multi-line

play03:55

we're going to call this a variable

play03:56

multi-line and we're going to come down

play03:58

here and say print

play04:01

and before I run this I have to make

play04:03

sure that this is ran

play04:05

so now let's print out our multi-line

play04:08

and now we have our nice little poem

play04:10

right down here now something to know

play04:11

about these single and double quotes is

play04:13

how they're actually used so if we use a

play04:16

single quote and we say I've always

play04:19

wanted to eat a gallon of ice cream

play04:23

and then we do an apostrophe at the end

play04:25

obviously something went wrong here what

play04:28

went wrong is when you use a single

play04:30

quote and then within your text within

play04:33

your sentence you have another

play04:34

apostrophe it's going to give you an

play04:36

error so what we want to do is whenever

play04:39

we have a quote within it we need to use

play04:42

a double quote these double quotes will

play04:45

negate any single quotes that you have

play04:47

within your statement they won't however

play04:49

negate another double quote so you need

play04:52

to make sure you aren't using double

play04:53

quotes within your sentence if you want

play04:55

to do something like that you need to

play04:56

use the triple quotes like we did above

play04:58

so we can do double double

play05:02

and then let's paste this within it

play05:07

and anything you do Within These treble

play05:09

quotes will be completely fine as long

play05:11

as you don't do triple quotes within

play05:13

your triple quotes we'll say this is

play05:15

wrong so even though it's between these

play05:17

two triple quotes it doesn't work

play05:19

exactly again you just have to

play05:21

understand how that works you have to

play05:23

use the proper apostrophes or quotes

play05:24

within your string and just to check

play05:26

this we can always say here's our

play05:29

multi-line we can always say type

play05:32

of multi-line and that is still a string

play05:37

one really important thing to know about

play05:39

strings is that they can be indexed

play05:42

indexing means that you can search

play05:43

within it and that index starts at zero

play05:46

so let's go ahead and create a variable

play05:48

and we'll just say a is equal to and

play05:51

let's do the all popular

play05:53

hello world let's run this

play05:56

and now when we print the string we can

play05:58

say a and we're going to do a bracket

play06:01

and now we can search throughout our

play06:03

string using the index so all you have

play06:05

to do is do a colon we can say five what

play06:09

this is going to do is going to say 0

play06:10

position 0 all the way up to five which

play06:13

should give us the whole hello I believe

play06:15

let's run this and it's giving us the

play06:18

first five positions of this string we

play06:20

can also get rid of the colon and just

play06:22

say something like five

play06:25

and then when we run this it's actually

play06:27

going to give us position five so this

play06:29

is 0 1 2 3 4 and then 5 is the space

play06:34

let's do six so we can see the actual

play06:36

letter and that is our w we can also use

play06:39

a negative when we're indexing through

play06:40

our string so we could say negative

play06:43

three and it'll give us the L because

play06:45

it's negative one two and three we can

play06:48

also specify a range if we don't want to

play06:50

use the default of zero so before we did

play06:52

zero to five and it started at zero

play06:54

because that was our default but we

play06:56

could also do two to five let's run this

play06:59

and now we go position 0 1 and then we

play07:03

start at 2 L L O now we can also

play07:06

multiply strings and we have this a

play07:08

hello world so we can do a

play07:11

times three and if we run this it'll

play07:13

give us hello world three times and we

play07:16

can also do a plus a

play07:19

and that is Hello World hello world now

play07:22

let's go down here and take a look at

play07:24

lists lists are really fantastic because

play07:26

they store multiple values the string

play07:28

was stored as one value multiple

play07:30

characters but a list can store multiple

play07:33

separate values so let's create our very

play07:35

first list we'll say list really quickly

play07:39

and then we'll put a bracket and a

play07:41

bracket means this is going to be a list

play07:43

there are other ones like a squiggly

play07:46

bracket and a parenthesis these denote

play07:49

that they are different types of data

play07:50

types the bracket is what makes a list a

play07:52

list so to keep it super simple we'll

play07:54

say one two three and we'll run this and

play07:58

now we have a list that has three

play07:59

separate values in it the comma in our

play08:01

list denotes that they are separate

play08:03

values and a list is indexed just like a

play08:06

string is indexed so position zero is

play08:08

this one position one is the two and

play08:11

position two is the three now when we

play08:13

made this list we didn't have to use any

play08:14

quotes because these are numbers but if

play08:17

we wanted to create a list and we wanted

play08:19

to add string values we have to do it

play08:22

with our quotes so we'll say quote

play08:24

cookie dough

play08:26

then we'll do a comma to separate the

play08:28

value and then we'll say

play08:30

strawberry

play08:32

then we'll do one more and this will

play08:34

just be chocolate and when we run this

play08:36

we have all three of these values stored

play08:39

in our list now one of the best things

play08:40

about lists is you can have any data

play08:42

type within them they don't just have to

play08:44

be numbers or strings you can basically

play08:47

put anything you want in there so let's

play08:49

create a new list

play08:51

and let's say vanilla

play08:53

and then we'll do three and then we'll

play08:56

add a list within a list and we'll say

play08:59

scoops

play09:01

comma spoon and then we'll get out of

play09:05

that list and then we'll add another

play09:07

value of true for Boolean and now we can

play09:11

hit shift enter and we just created a

play09:13

list with several different data types

play09:16

within one list

play09:17

now let's take this one list right here

play09:19

with all of our different ice cream

play09:21

flavors we'll say ice underscore cream

play09:24

is equal to this list now one thing

play09:27

that's really great about lists is that

play09:28

they are changeable that means we can

play09:31

change the data in here we can also add

play09:33

and remove items from the list after

play09:35

we've already created it so let's go and

play09:37

take ice cream and we'll say ice cream

play09:39

dot append and this is going to append

play09:42

it to the very end of the list we'll do

play09:45

an open parenthesis let's say salted

play09:48

caramel now when we run this and we call

play09:51

it just like this it's going to take

play09:54

this list add salted caramel to the end

play09:57

and we'll print it off and as you can

play10:00

see it was added to the list and just

play10:02

like I said before let me go down here

play10:04

we can also change things from this list

play10:06

so let's say ice cream and then we need

play10:09

to look at the indexed position so we're

play10:11

going to say 0 and that's going to be

play10:13

this cookie dough right here we can say

play10:15

that is equal to so we can now change

play10:17

that value so let's call that butter

play10:20

econ

play10:22

and now when we call it

play10:25

we can now see that the cookie dough was

play10:27

changed to butter pecan another thing

play10:29

that you saw just a little bit ago is

play10:31

something called a list within a list

play10:33

basically a nested list so we had Scoops

play10:37

spoon true let's give this and we'll say

play10:40

nested underscore list is equal to

play10:44

now when we run this we now have this

play10:46

nested list so if we look at the index

play10:49

and we say 0 we'll get vanilla if we say

play10:52

2 we'll get scoops and spoons now since

play10:55

we have a list within a list we can also

play10:58

look at the index of that nested list so

play11:01

let's now say one and that should give

play11:04

us just spoon and you can go on and on

play11:07

and on with this you can do lists within

play11:09

lists within lists and all of them will

play11:11

have indexing that you can call now

play11:13

let's go down here and start taking a

play11:14

look at tuples so we list in a tubal are

play11:17

actually quite similar but the biggest

play11:20

difference between a listen a tuple is

play11:22

that a tuple is something called

play11:23

immutable it means it cannot be modified

play11:25

or changed after it's created let's go

play11:28

right up here we're going to say

play11:30

tubal

play11:32

and let's write our very first Tuple so

play11:34

we'll say Tuple underscore scoops

play11:37

is equal to and then we'll do an open

play11:40

parenthesis now these open parentheses

play11:42

you've seen if you do like a print

play11:43

statement but that's different because

play11:45

that's executing a function this is

play11:48

actually creating a tuple which is going

play11:50

to store data for us so we'll say one

play11:52

two three

play11:53

two and one let's go ahead and create

play11:56

that Tuple and we can just check the

play11:59

data type really quickly

play12:01

and it's a tuple and just like we saw

play12:03

before a tuple is also indexed so if we

play12:07

go at the very first position which is a

play12:09

one we will get the output of a one but

play12:12

we can't do something like append

play12:15

and then add a value like three if we do

play12:18

that it's going to say Tuple object has

play12:20

no attribute append it's just because

play12:22

you cannot change or add anything to a

play12:25

tuple just like we were talking about

play12:26

before typically people will use tuples

play12:29

for when data is never going to change

play12:31

an example for this might be something

play12:33

like a city name a country a location

play12:35

something that won't change they

play12:37

definitely have their use cases but I

play12:39

don't think they're as popular as just

play12:40

using a list so now let's scroll down

play12:42

and start taking a look at sets but

play12:45

really quickly let me add a few more

play12:48

cells for us

play12:50

and let's say sets

play12:53

now a set is somewhat similar to a list

play12:56

in a tuple but they are a little bit

play12:59

different in fact that they don't have

play13:01

any duplicate elements another big

play13:03

difference is that the values within a

play13:05

set cannot be accessed using an index

play13:07

because it doesn't have an index because

play13:09

it's actually unordered we can still

play13:11

Loop through the items in a set with

play13:13

something like a for Loop but we can't

play13:15

access it using the bracket and then

play13:17

accessing its index point so let's go

play13:19

ahead and create our very first set so

play13:21

we're going to say daily underscore

play13:24

pints then we're going to say equal to

play13:26

and to create a set we're going to use

play13:28

these squiggly brackets I don't know if

play13:30

there's an actual name for those if I'm

play13:31

being honest I call them squiggly

play13:33

brackets and that's what we're going to

play13:34

go with we're going to put in a one a

play13:36

two and a three so let's go ahead and

play13:38

run this

play13:40

and let's look at the type

play13:42

and as you can see it is a set now when

play13:45

we print this out

play13:46

it's going to show us one a two and a

play13:49

three and those are all the values

play13:50

within our set but if we copy this and

play13:53

we'll say daily pints log this is going

play13:56

to be every single day

play13:58

maybe I had different values

play14:01

now when we run this and we do the exact

play14:03

same thing now when we print this

play14:08

it's going to have just the unique

play14:09

values within that set now a use case

play14:12

for set and this is something that I've

play14:13

done in the past is comparing two

play14:15

separate sets maybe you have a list or a

play14:17

tuple and you convert that into a set

play14:19

and that will narrow it down to its

play14:21

unique values and then you can compare

play14:23

the unique values of one set to the

play14:25

unique values in another set and then we

play14:27

can see what's the same and what's

play14:28

different so let's go down here and

play14:30

let's say wife's underscore daily and

play14:35

we'll just copy this right here we'll

play14:37

say is equal to let's do our squiggly

play14:40

lines let's do one two let's do just

play14:43

random numbers

play14:45

so now this is my daily log and this is

play14:48

my wife's daily log and now we can

play14:50

compare these values so let's go right

play14:52

down here let's say print

play14:55

we'll do my daily logs and then we'll do

play14:59

this bar right here and this is going to

play15:01

show us the combined unique values it's

play15:03

basically like putting them all in one

play15:05

set and then trimming it down to just

play15:07

the unique values so we'll take wife's

play15:09

daily pints log

play15:11

and when we run this we actually need to

play15:13

run this first

play15:14

when we run this we should see all the

play15:16

unique values between these two sets and

play15:18

so as you can see zero one two three

play15:20

four five six seven twenty four Thirty

play15:23

One so these are all the unique values

play15:25

between these two sets

play15:27

we can also do another one

play15:30

and instead of this bar we're gonna do

play15:32

this symbol right here which I believe

play15:34

is called an ampersand don't quote me on

play15:36

that but when we run this it's going to

play15:38

show what matches that means which ones

play15:41

show up in both sets so the only ones

play15:44

that show up in both sets are one two

play15:47

three and five we can also do the

play15:49

opposite of that by doing a minus sign

play15:52

and this is going to show us what

play15:53

doesn't match so we have 4 6 and 31. now

play15:57

where is our 24 that was in our wife's

play16:00

daily pints log it's in this one but

play16:02

we're subtracting the values on this one

play16:04

so let's reverse this and we'll say

play16:06

daily pints log

play16:09

and let's run it now those are our other

play16:11

values so we're taking the values of

play16:13

this and then we're subtracting all the

play16:15

ones that are the same and getting the

play16:17

remaining values and then for our last

play16:20

one

play16:21

we can get rid of this and we'll do this

play16:24

symbol right here and this is going to

play16:26

show if a value is either in one or the

play16:29

other but not in both so let's run this

play16:32

so these values are completely unique

play16:34

only to each of those sets now the very

play16:38

last one that we're going to look at in

play16:39

this video is dictionaries so let's go

play16:42

right down here

play16:43

let's add a few cells and let's say

play16:46

dictionaries

play16:49

now I saved dictionary for last because

play16:50

this one is probably the most different

play16:52

out of all the previous data types that

play16:54

we've looked at within a data type we

play16:56

have something called

play16:57

a key

play16:59

value pair

play17:01

that means when we use a dictionary it's

play17:03

not like a list where you just have a

play17:05

value comma value comma value we have a

play17:08

key that indicates what that value is

play17:10

attributed to so let's write out a

play17:13

dictionary to see how this looks we're

play17:15

going to say dictionary underscore cream

play17:18

and just like a set we use a squiggly

play17:21

line but the thing that differentiates

play17:23

it is that in a dictionary we'll have

play17:25

that key value pair whereas in a set

play17:27

each value is just separated by a comma

play17:29

so let's write name

play17:32

and this is our key and then we do a

play17:34

colon and this is then where we input

play17:36

our value so we're going to say Alex

play17:38

freeberg

play17:40

and then we separate that key value Pair

play17:43

by a comma and now we can do another key

play17:45

value pair so we'll say

play17:48

weekly intake

play17:50

and a colon and we'll say five pints of

play17:54

ice cream do a comma and then we'll do

play17:57

favorite ice creams

play17:59

and now what we're going to do is we're

play18:01

going to put in here a list so within

play18:03

this dictionary we can also add a list

play18:05

we'll do MCC for mint chocolate chip and

play18:09

then we'll add chocolate another one of

play18:11

my favorites so now we have our very

play18:13

first dictionary let's copy this and run

play18:16

it

play18:17

and let's just look at the type

play18:20

and as you can see it says that this is

play18:22

a dictionary let's also print it out

play18:25

now if we want to we can take our

play18:27

dictionary cream and say dot values with

play18:31

an open parenthesis when we execute this

play18:33

we'll see all of the values within this

play18:36

dictionary so here's our values of Alex

play18:37

freeberg five mint chocolate chip and

play18:40

chocolate

play18:41

we can also say keys

play18:43

and when we run this all of the keys the

play18:45

name weekly intake and favorite ice

play18:47

creams

play18:48

and we can also say

play18:51

items so this key value pair is one item

play18:54

and this key value pair is another item

play18:57

now one difference between something

play18:59

like a list and a dictionary is how you

play19:02

call the index but you can't call it by

play19:04

doing something like this where you just

play19:06

do a bracket oops and say zero so this

play19:09

would in theory take this very first one

play19:13

right our very first key value pair

play19:15

that's going to give us an error how you

play19:17

call a dictionary is actually by the key

play19:18

so it doesn't technically have an index

play19:21

but you can specify what you want to

play19:23

call and take it out so we're going to

play19:25

say name and this is going to call that

play19:28

key right here and when we run this

play19:30

we'll get the value which is Alex

play19:32

freeberg one other thing that you can do

play19:34

is you can also update information in a

play19:37

dictionary which we can't with some

play19:39

other data types so for this for the

play19:41

name it was Alex freeberg now let's say

play19:44

Steen freeberg

play19:46

and when we update that I'm also going

play19:49

to print

play19:51

the dictionary get rid of this so it's

play19:54

going to update Christine freeberg in

play19:57

that value of the name so let's go ahead

play20:00

and run this

play20:01

and now it changed the name from Alex

play20:03

freeberg to Christine freberg we can

play20:05

also update all of these values at one

play20:08

time so let's copy this

play20:12

and I'm going to put it right down here

play20:13

I'm going to say dictionary.cream dot

play20:17

update then we're going to put a bracket

play20:20

or not a bracket but a parentheses

play20:21

around these so now what we're going to

play20:24

do is update this entire thing let me

play20:26

take this

play20:27

say print this dictionary now we can

play20:31

update this to anything we want so

play20:34

instead of here I can say

play20:36

I'll say wait

play20:39

and because of all that ice cream I now

play20:41

weigh 300 pounds so let's run this

play20:46

and as you can see it did not delete our

play20:48

key value pair right here instead of

play20:50

just added to it when you're using the

play20:52

update we can't actually delete that's

play20:54

the delete statement and I'll show you

play20:56

that in just a second but all we did was

play20:58

added this new value it also is going to

play21:01

check and see if you changed anything

play21:02

with your key value pair so we can go in

play21:04

here and change this value and we'll say

play21:06

10. so now when we run this the value of

play21:10

this key value pair was changed but

play21:12

let's say we do want to delete it we'll

play21:14

say Del that stands for delete

play21:17

part of this dictionary cream and now

play21:18

let's specify the key which will also

play21:21

delete the value with it let's specify

play21:23

the key that we want to get rid of and

play21:25

let's say wait

play21:27

and then let's print that again

play21:31

and as you can see the weight was

play21:34

deleted from that dictionary so that is

play21:36

all we're going to cover in this data

play21:37

types video thank you guys so much for

play21:39

watching I really appreciate it if you

play21:41

like this video be sure to like And

play21:43

subscribe below and I'll see you in the

play21:44

next video

play21:46

[Music]

play21:51

foreign

play21:54

[Music]

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
PythonData TypesIntegersFloatsBooleanSequencesStringsListsTuplesSetsDictionariesProgramming
Benötigen Sie eine Zusammenfassung auf Englisch?