Hello everyone and welcome to part two of the Python crash course.
Let's go ahead and jump to Jupiter notebook to get started.
All right.
Here I am at a chip in a notebook.
Last time we left off on how to grab items from a nested list we went over strings and lists and those
are related to each other because they're actually just sequences of elements and you can use the same
index notation whether on a string or on a list you just passen in brackets the actual indices you want
and groud the elements that are at that index or slice of the index.
Let's go ahead and switch gears to thinking about dictionaries.
You can create a dictionary in Python by specifying curly brackets and then you'll have a key a colon
and the value that corresponds with that key.
Show you another example.
We'll go ahead and make two colon and the value for Q2 will be one to three dictionaries behave as key
value pairs just like a hash table instead of holding elements in a sequence.
They hold elements through keys and then their actual values.
So instead of saying something like 0 that's not going to work because there is no key there and it
doesn't hold that in a actual sequence position where you end up passing in the bracket notation is
the actual itself and no return the value.
So for instance if you wanted to get the number 123 here you would call your dictionary and then passen
the key corresponding to that value.
In this case one two three and dictionaries can take in basically any items as their values.
So we can a key K-1 colon and then you can actually have an other list in here.
One two three.
And now if I call a dictionary I have one key K K one and then the list 2:59 has the item.
So I can call K-1 and return that list one two three.
And now that I have this list I can actually then do normal indexing.
That's off the list not the key.
So let's go ahead and grab two.
Which is going to be at index 1 and this is basically the same thing as separating out into two steps
.
So you could do something like.
My list is equal to that item at Q1.
And then if you look at my list that was the list before and you use brechen notation to grab whatever
you want for that list or you just do it all in one step along the same line and you'll have something
similar to this in your exercises or we'll have kind of a nested dictionary and nested list.
Me going to show you one more example where you can have actually a dictionary nested inside of another
dictionary so I can say K-1 colon pasan in other dictionary with its set of curly brackets there pasand
something like inner key colon and then a list of values.
One two three.
So let's say I want to grab this number two.
Well first thing you have to do is call K-1 and that returns this dictionary.
Then I need a call inner key off of that so I can do that of another set of brackets or I could split
it up into multiple steps by using variable assignment.
But we'll do it all in one line.
And now I have one two three and two is that the second index that's 0 1 and that grabs the number two
there.
So again dictionaries do not retain any order.
They are just key value pair mappings.
Now let's talk about booleans billions and Python are quite simple.
It's just true and then false with capitalized T and capitalize f.
OK we'll go ahead and touch back on these when we talk about comparison operators.
But let's talk about tuples and then sets.
Remember that a list is just defined as a sequence of characters inside of square brackets separated
by commas.
And you can go ahead and grab items off that list using indexing a tuple is very similar except instead
of square brackets you use parentheses and you can again just grab items off of that tuple as if it
were a list using index bracket notation.
Here's the key difference between a tuple of these parentheses versus a list of the square brackets
.
If I go back to my list and try to reassign the first item to be the word new lips Well let me run this
again because actually reassigned the whole list.
Let's check list again.
All right.
So if I print out my list I have one two three.
Grab the first item in that list.
Zero.
Perfect.
Now let me re-assign zero to be new just like we did in the previous lecture.
And now for a check out my list I reassigned the element 0 to be new and then to 3 if I want to do that
for tuple.
However if we look at my tuple it's one two three let's say grab 0 instead of equal to new I'm going
to get an error.
And this is because tuples are immutable and they do not support item assignment.
I mean you can't mutate the items inside of a tuple.
Which means you're going to want to use a tuple when you want to make sure a user can't change the items
inside of the sequence of objects.
And that's the main difference between a tuple and a list a tuple is immutable and a list is mutable
meaning you can change the items inside of a list.
You can't do that same thing if a tuple.
All right.
Now to discuss sets in Python a set is a collection of unique elements.
And it looks the same of curly brackets as a dictionary except it doesn't have the colons it's just
elements.
Now notice what happens if I type some of these numbers more than once.
I still get the same output because a set is defined by only unique elements.
Meaning if I try to put in multiples of the same elements it will reduce down to the unique elements
and you can actually call s e t set function and pasan a list to grab the unique elements for you.
So I pass a bunch of repeated elements into the set function it returns a set of the unique elements
.
If you want to add items to a set you can go ahead and just say for instance yes.
One two three you'll say s You'll use the Add method on it put in the item you want to add for instance
5.
And if we check out that set again we get 1 2 3 5.
If I try to add 5 again it won't retrieve an error but it will just keep it the same because it's only
one unique instance of 5 is allowed inside of a set.
All right.
We really want work with sets at all really in this course.
But I do want you to be aware of them.
They are just a collection of unique elements.
OK.
Now let's talk about comparison operators comparison operators allow you to compare two elements to
each other.
These are things such as greater than less than equal to etc..
For instance go in and make a new cell.
I can say is one greater than two and it'll return a boolean value either true or false depending on
what your statement is.
So one greater than two is false.
1 less than 2 is true.
And you can do the same thing for greater than or equal to or less than or equal to.
You can also check for equality using two equal signs and notice how the syntax highlighting kind of
helps you out here.
So is one equal to one.
That is true.
Is one equal to two that is false.
Not true.
Now remember you want to use two equal signs.
If you try to do something like this one equals one you'll get an error because Python thinks you're
trying to do a variable assignment not an actual comparison operation.
If you want to check for any quality you do.
Exclamation mark equal sign.
So one is not equal to three.
Well that's actually true.
It's true that one is not equal to three and you can do the same thing not just of numbers but with
strings as well.
So is high equal to buy.
That's false.
It's not the same is it not equal to it.
Well that's true.
Now let's talk about combining these comparison operators with some logic operator such as and and then
or.
Right now we're just looking at one condition is one less than two.
Well that's true.
What if we wanted to combine it with another condition such as two less than three we can use the keyword
and in order to do that.
So I'll say is one less than two and is to less than three.
And this will return true.
However if one of these is switched to be false is one less than two.
Well that's true and it's too greater than three.
Let's false.
So you have a true and false or return false.
So if the and operator all conditions must be true.
And I personally like to wrap these statements in parentheses.
So they're a little more readable.
You don't have to do it but I do recommend that you do because it's a little more readable when you
come back to this code later on.
You also have the OR operator which is really similar except now in this case only one of them has to
be true.
So we can say is one less than two or is too greater than three.
Well one of these is true.
The first one is.
So then we get true back.
And you can actually stack these on top of each other so you can say in other or here for us one equals
equals one etc..
And if you want to really understand these you can actually just Passons straight boolean value is so
true and true that returns True true and false returns False True or false is true and then that's basically
it.
So just or.
And now let's go ahead and discuss some code blocks by talking about if if else statements.
A lot of times you'll want to execute code if a condition is true and you can use the if you left an
L statements in order to do that.
Let's say we have a condition if one is less than two.
We want to perform some sort of action like a print statements.
We'll go ahead and say Yep.
So we're saying if this condition is true colon and notice there's a block of whitespace here and that's
really important in Python.
Python doesn't use brackets in order to separate block of code execution statements.
It uses whitespace instead.
And if you just type enter here after a colon it will automatically do the indentation for you in Jupiter
.
And most ideas that support Python will also do the same thing.
OK so we have.
If one is less than to perform some action and basically that's just saying this if true print perform
code etc..
Or you can just do items like you can say two plus two equals two plus two etc..
And then when you see X you'll notice it's four.
So that's just an if statement.
So if some condition is true colon execute the block of code that's indented below it.
All right.
Now talk about some other operations we can do.
Let's say we want to check for multiple conditions or let's say we want to have something occur in case
it's not true.
So I will say if one is equal to Supe's prints first else Colan prints last.
And what this is going to do it's going to say if this condition is true execute this code otherwise
.
Or else if none of thing above happens to be true and note how the else lines up with the if statement
that we know they're linked to each other.
Colin Crantz last so if I run this code last ends up being executed if I go ahead and switch this so
that the first condition is true first ends up being executed.
Now maybe you want to check on multiple conditions not just one condition and then some else statement
.
That is where you can use the left to check for multiple conditions.
So you can go ahead.
Go ahead and say something like.
Elif 3 is equal to 3.
Now let's go in and change the first one to be false again.
We'll print something else will print middle.
All right.
So let's go ahead and run this and break down what's happening.
Python is just doing this it's saying if this condition is true execute this block of code otherwise
.
Or else if this condition is true.
Execute this block code else.
If none of the conditions above me are true execute this block code and you can keep stacking as many
statements as you want so you can say another one here.
Elif will go out and say if four is equal to four Crantz seconds.
All right.
And notice what happens here.
Even though this is true three is equal to three.
We're going to only execute the first instance of a condition being true.
And that's something important to keep in mind when you're using if elif else statements it's going
to execute the first true condition and that's it.
All right.
That's it for this part 2 lecture series coming up next we'll begin to discuss for loops while loops
and then some other functions in Python.
OK.
As a quick review of what we did we went over we go back to the top dictionaries which again are just
key value pairs and you can grab the values by inputting the keys.
I remember you can have nested dictionaries or nested lists inside of a dictionary.
Now we have boolean values true and false.
We have lists tuples which are immutable.
Very similar to lists except that you can't go ahead and change stuff once it's inside of a tuple because
the tuple doesn't support item re-assignment.
We have sets I remember sets are just a collection of unique elements.
So if you have a bunch of repeated elements and you have a set it's just the unique ones.
Now we have comparison operators greater than less than greater than or equal to less equal to equality
check and then check for inequality.
And you can do the same thing for strings.
You have these logical operators and and then or you can compare multiple conditional statements.
And then you have the if elefant else logic which allows you to kind of stack and perform blocks of
code based off of some sort of condition being churned up.
All right.
Hope you enjoy this lecture.
We'll see you at the next one.