Hello everyone and welcome to Python crash course Part 1 and Part 1 we're going to go ahead and begin
our discussion of Python by going over basic data types.
Let me go ahead and show you where you can find that you've been in notebooks for this lecture series
.
Ok here I am at the downloaded and unzip repository.
If you go ahead and go to the Python crash course folder you'll see three notebooks here are IPY inby
files.
We're going to be working with the python crash course notebook.
And if you click on that you should see a notebook that looks something like this and it has all the
data types are going to be going over.
We'll start off by just going over numbers streams printing lists dictionary booleans tuples and sets
.
We'll see how far along we get.
And later on we'll cover some more advanced features.
I'm going to go ahead and start a new notebook here and set it ready to go.
I'll go ahead and what's in the view tug with the head bar and toggle the toolbar just to get a little
more room here.
And again all I'm going to be doing is typing through all the examples here and explaining the code
as we go along.
So if you ever need a reference something you can just copy and paste from this lecture notebook.
All right.
Let's go ahead and get started with basic numbers and arithmetic I thought has two basic number types
.
There is the integer which is just something like 1.
And then there is the floating point number which is something like point zero.
So a floating point number has a decimal attached to it and with either of these numbers you can perform
basic arithmetic as you would expect.
So for addition it's just a plus sign.
One plus one and I'm just doing shift enter here it's run the cells for multiplication is just an asterix
So 1 times 3.
Just an asterix like that.
The vision is just going to be a forward slash one divided by two is 0.5.
And note that even I put into integers.
I get a floating point out if you're a Python 2 user you'll have to make sure that one of these itself
is a floating point number in order to get the same result.
I just it for exponents for instance to the power of four.
Just two Asterix put together.
So two to the power of four is 16.
That is two times two times two times to something else to know is that Python will follow an order
of operations as you would expect.
And you can use parentheses to actually clarify the order of operations.
For instance if I go ahead and type in two plus three times five plus five mathematic order of operations
would actually declare the multiplication to occur first and then the addition.
So if I run this I will get 15 plus two plus five.
So 15 plus seven is 22.
If you actually want to say something like two plus three times the result of five plus five you can
use parentheses in order to just clarify your order so you can use parentheses in this manner to actually
say five times 10 which will get you 50.
The last arithmetic operation I want to show you is the model function.
So the Ma'at function or modulus as it's also known is the percent sign in Python.
And it basically will return what remains after the division.
So for divided by two is two remainder.
Zero.
So when you run for maade two you'll get zero as the output five divided by two is to remainder one
since four plus one is five.
And so you'll get out the remainder 1.
And this is a nice way to check if numbers are even so you'll know that if for instance a mod 2 returns
0 that 8 isn't even number.
Because two goes on evenly to that number and it has remainder zero.
Now let's talk quickly about variable assignments.
A lot of times you want to pick variable names in order to assign some object or data type to a variable
name.
In Python the assignment operator is just an equal sign.
So you'll choose the name of your variable by just starting to type stuff out.
Such as V-A are they all say equals and then whatever object type you want to put in there.
So for instance we can put in two and then whenever I call that variable name V A R I will get a two
out.
So for instance I can say x is equal to 2 y is equal to three.
And now I can actually say X plus Y and get out 5.
I can also reassign X by defining it with by itself.
So I can say x is equal to x plus X so that will end up resulting as x is equal to 4.
And now whenever I call X again it's going to be equal to 4 meaning if I were to run that cell again
the X equals x plus X every time I run it.
So essentially going to double the value of x and it's going to re-assign X as it's all double value
.
All right.
A couple of rules about creating these variable names.
They shouldn't start with numbers so you can't say something like 12 V-A are as equal to 12.
Otherwise you'll get a syntax error in o'War knew that it's invalid syntax Alexiou give you the error
.
So you can't start off with numbers and you also can't start off with special symbols so you can't say
something like Dollar Sign vr you'll start getting problems here so you'll get errors of syntax.
They should just start with a lowercase letter if you want to chain together multiple words.
You should use an underscore to separate them so name underscore of underscore the a.
Are other languages how do you use a period to separate this for Python.
The proper syntax is to use an underscore.
Now let's go ahead and talk about strings.
So I'm going to make a comments by putting in a hash tag on strings and anything of a hash tag in front
of it.
You'll notice it's color differently and see because it's a comment so it's just not going to run.
So you can type in whatever here.
It won't run.
All right let's go ahead to move on to strings and I'm going to go ahead and zoom in a bit so you get
a better view of this.
And for strings there's two ways of creating a string.
You can use single quotes and note how the color has change in the Kading that this is a string or you
can use double quotes so this is a string.
And then you can also wrap double quotes around single quotes so let's say you have a sentence that
says something like this like I cants go.
So if you have this single quote but you want to keep it inside then you can just wrap this around double
quotes and you'll notice that that single quote gets retained within the string.
So if you ever have are having to deal with either you want a single quote or a double quote inside
of a string you can always just wrap it around another set of either double or single quotes the pending
and what's on the inside.
OK let's go ahead and talk about printing strings.
I'm going to make a variable called X in a sign that the string hello for Jupiter notebook if I just
type in X it will show with out and then I'll show the quotes Hello here.
And this is essentially the same thing as if you were to type this directly into the console in Python
just typing in the variable x.
I'll put the string hello or Python However if you want to actually print the outputs of X you'll have
to pass it into the print function which is going to be prints parentheses and then x inside of those
parentheses.
And note.
Now the difference here.
I don't have that out indicator any more and I don't have the single quotes anymore.
So this is how you should officially display stuff when you're trying to show the result in Python is
the print statement.
Now let me go ahead and show you how you can format your print statements.
Let's say I have a variable name is equal to 12 and a name equal to Sam.
Now I want to actually print stuff based off those variables.
I can do that with print formatting.
I'll take a string and say My number is and I will pasan a block of quotes or curly brackets there.
And my name is and I'm going to passen another set of curly brackets.
And then in the mislay dots formats and note how it's colored here to indicate that this is a special
method off of the string and then you can go ahead and do is pass in the variable names in the order
you want them to be filled into these curly brackets.
So if I just typed this in and run this what Python does is it reads that you have curly brackets here
in your string and that you have that format and it will put them in the order you want.
So I'll say OK numb gets replaced here for these curly brackets and name gets replaced here.
For these curly brackets.
And that's a convenient way for you to use the variable names in your print statements instead of having
to write out 12 or Sam all the time.
And note that I'm actually not printing this yet.
I'll have to wrap this entire thing around the print statements.
So make sure your princes are balanced and you can see just by selecting one of the print princes to
get highlighted green.
Now if I were on this this has truly been printed and I no longer have that out sell the other way you
can use print formatting is to say if we go ahead and change this up you can pass any variable into
the curly brackets.
So I can say one inside of the curly brackets and two inside of the curly brackets.
And now that I've passed in these names I can go ahead an inside of my doc format statement say one
is equal to and then whatever variable I want one to be equal to.
In this case I want it to be equal to that numb and then two I wanted it to be equal to name.
And if I run this I'll get the exact same result.
What's nice about this is you no longer have to worry about the formatting being in the exact same order
you want for the curly brackets and you can continually put in more curly brackets so more and then
you can type in one here again and noticed that Python can now search and put in your variables in more
than just once so it comes here looks at the variable name then goes to your format and checks.
OK what is the variable name there.
So for instance 2 it looks in here says 2 is equal to name.
All right.
And then a fill in name over here and then we know that name is equal to the string Sam.
So it goes ahead and puts it inside there.
All right go ahead and feel free to post any questions.
The Q&A forum if you're confused on any of this print's formatting but it's basically straightforward
.
Just put in your string put in your curly brackets.
Then I would suggest that you use this method of passing in variable names because it's a little more
clear when you go back and you don't have to worry about messing up the order inside of this format
call.
Now let's go ahead and expand our topic of strings into indexing strings.
If I have a string called S and I'll say x is equal to hello.
S here is just a sequence of elements.
And in this case each element is a letter and I can grab specific elements from that sequence of characters
by using this square bracket notation and indexing it.
Pythoness starts at zero.
And this is something to know if I say X square brackets at zero and run this or turn back the zero
elements.
So here we see s it's grabs Hello checks as there are elements and then returns 0.
Go ahead and see if you can grab the last o there in Hilo.
Well what would you do you would say well what spacer index is that it's at 0 1 2 3 4.
You put in four into these brackets and grab that out.
The other thing to note is you can also use what's known as slice notation to actually just grab slices
of the string.
I'm going to go ahead and type in instead of aesthetical to hello.
I'll say a b c d e f g h i j k.
I'll run that and now what I'm going to go ahead and see if I type in s of 0.
Right here in this brackets I should get a back.
OK that makes sense.
Let's say I wanted to grab everything like a and beyond.
Well I can use colon notation which is also known as slice syntax in order to do this.
I will say s of 0 as in a colon.
Run this and this colon basically says OK starting at zero.
Grab everything beyond it and this notation works the other way around.
I can say grab everything with the colon up to a certain index.
So let's say I say a b c d.
So D is the index 3 0 1 2 3.
If I pass in three here this is basically commanding Python to say OK grab everything up to but not
including index 3.
This is sometimes confusing for beginners because they expect d to show up.
Well because it's the index position 3 but because indexing starts at zero you should just think of
this command as grab everything up to and not including the character element index three.
And we're going to be showing you more about slicing notation when we talk about lists because it's
the same sort of idea.
And then finally we combine these two ideas here as far as starting and grabbing everything beyond it
or grabbing everything up to but not including a certain index point by saying s start at zero.
Colin grab everything up to a certain index such as three.
Now that returns the same result as ABC.
But what I can do is replace these two numbers to set my start point and my End Point.
Let's go ahead and grab D E F so d starts at index 3.
Meaning if I go ahead and replace this I'm going to start at index 3 0 1 2 3 and then I'm going to go
up to and including G.
So that's 0 1 2 3 4 5 6 7 to say 3 colon 6.
Run that and I get d e f.
Again it's just the index you start at that's included colon.
Which means grab all the way through and then your last index is the index that you don't include because
indexing starts at zero.
So g is not included even the index 6.
OK well again reiterate this idea when we talk about this which is coming up right now lists are a sequence
of elements in a square set of square brackets separated by commas.
So here you have a list one two three list can take in basically any data type.
So I can also have a list of strings a b c and there's no complaints here.
I'm going to go ahead and make a variable called my list and set it equal to a b c run that and if I
want to add an element to the end of my list I use the append method.
I say my underscore list appends.
And then let's say I want to append the letter D.
I just run that and if I check my list now.
Now what has appended that value.
D And they list is also a sequence just like a string is.
Meaning if I want to grab the first item in the list I can use the same brechen notation I did for a
string pass in the index i Watz run this and I'll return the item at the index so 0 1 to where each
index point is not separated by a comma.
For instance if I wanted to grab a b and c I can use the same slice notation we just went over.
I want to start at index 1 and go all the way up to but not including index 3.
So I'll say one colon three not returns B and C.
I can also reassign positions using this index notation.
So if I wanted the first element to be something different I just say my underscore list pasand zero
into those square brackets and then pass in whatever element I want for instance.
I want that element to be new now.
So if I grab my list I see new B C D.
Something to note here is that you can nest list inside of each other so I can say illest one comma
to comma and then put in another list 3 comma for.
So if I take a look at list it's a list inside of a list.
So in fact let's say I wanted to grab this for as a single element.
Well that's a list the index 0 1 2.
So I pass on to here and this returns in other lists.
So let's say I want a graph for well that is now index 0 1 meaning I can tack on another set of brackets
1 and get the actual elements for.
Let me go in and show you one more example of grabbing stuff from nested lists.
You don't do this too often with no less lists or very deep.
But I just want you to get the idea of using multiple brackets so let's say I have a nested list that
looks like this.
One two three.
Another list four or five other lists target.
Well I can grab the third or fourth element to index three in my nest list.
That looks like this.
Then I can set another one to go at index to 0 1 2 that's the target list.
And let's say I want the only elements in that target list.
No it still has the square brackets around that I just pasan zero.
And now I have the string target.
And if you want to print this string I can wrap this whole thing in a print statement.
Now I grab my target.
You'll have an exercise where you have to kind of find something in a nested list.
So make sure you understand the topic of these brackets kind of stacked on top of each other.
All right.
Well go ahead and stop here for part one.
And coming up next we'll go ahead and cover some more data types in this lecture we went over lists
slice notation how to grab things from an index strings.
How did this imprint formatting basic variable assignments and also just basic arithmetic.
OK.
Hope you enjoy this lecture.
Coming up next we'll go over some more stuff.
Thanks everyone.
See at the next lecture