This video, we're going to talk about data types and operations in Python to get you started,
more on being able to write some of your own Python code,
are learning outcomes for this video or for you to understand basic python data types of operations,
to be able to work with python variables, storing objects in them, to write some simple python code,
to do arithmetic and perform basic operations with lists and dictionaries. Also, this slide deck is a notebook.
And so rather than the little embedded slides widget like we have for a lot of the videos, for this one, there will be a link to the notebooks.
You can download it and run the code yourself.
There's also going to be more resources linked in the class notes that I'll talk about briefly at the end of this video.
Python supports couple primary types of numbers. First, we can write integers just by writing the number.
There's no decimal point in there. And if we run, that is just a it's a python line.
So a python line is called a statement. A statement can have something like an F or something like that.
Or it can just be what we call an expression. And an expression is a set of operations that results in a value.
And so a number just writing the number itself is an expression. So we can we can write an integer.
We can write a floating point number with a decimal point. These are stored in floating point format.
There are a couple of nuances about that. We'll talk about when we talk more in detail about different types of data.
We can also use scientific notation with the E notations,
the six point O to each of the twenty three Avogadro's number a mole and we write E twenty three and that means times ten to twenty third power.
So we can also do arithmetic on these numbers, so the usual arithmetic operations, addition, subtraction, etc., they work as we would expect.
They were just like they do.
And when you're writing them in math, when they work in other programing languages, we can add five in to order of operation disrespected.
So three times six at plus two.
We can also then use parentheses to change the groupings so we can add two and three before multiplying by six.
It works like you would expect from almost any other programing language.
No surprises here if you're familiar with Java or Perl or something else.
Those are our basic arithmetic operations.
If we want to raise something to a power, the star star operator is what Python uses to raise something to a power.
So two to the fifth power is two star star five.
We can also get a number of other mathematical operations from two different python modules math and num pi num.
Pi has duplicates of most of the math ones, so I usually just work with num pi.
You have to import a module before you can use its function. So I'm going to import the num pi module here and I want to give it a shorter alias enpi.
This is very common in Jupiter notebook's that we that we import num pi S&P so then
we can just write n.p. dot log to compute the natural logarithm of the number twenty.
We can also store values in variables.
Let's just give them a name so X equals seven stores, the value seven in the variable X, there is no declaration necessary, unlike an JARBOE.
Just assign the value to a variable. Then we can use it.
So X is plus five is going to return a 12 because X currently stores the value seven.
Now if we change the variable, so we say X equals two.
OK. It's changed the variable. Python. So the variables are all stored in a commons memory space.
And the Jupiter notebook runs the cells in the order we ran them.
And it shows us here that number. But in in. Is the order in which that cell was run nine, 10, 11, if we so we've changed the value of of X here.
If we go back up and rerun this cell, it's going to use the new value of X.
This is important to keep in mind, and it's an easy way to get your notebook very confused if you've been running cells out of order.
When we're developing a notebook we're working on, a data analysis will often run things out of order, try things out.
But it's important to keep things clear and consistent in your notebook so that if you were to rerun the notebook from top to bottom,
it runs and produces the correct results. You can do experiments, but before you go,
say to submit your notebook to me in an assignment or before you go to to submit it to your client or use it for your final analysis.
Make sure that if you rerun it from top to bottom,
you get the right results so that you can be confident that you're actually computing the results you want.
And there's not something that's just an artifact of the order in which you happened to run the cells.
So we've seen numbers do seem variables that we can also write strings.
We can put them in quotes. Python takes both double and single quotes.
There's no difference between them. The backslash is an escape character.
So if we want to have double quotes and a double coded string, we can we can do that with by by prefixing them with a backslash.
We can one of the fundamental string operations is to contaminate two strings, and if you have strings,
the plus operator, it's the same operator we use for additions and with numbers, it can cat Nates the two strings.
So hello. Plus world is Hello World. There's a bunch of other operations.
For example, split separates a string into a list by default.
If you don't tell it how to split it, it uses whitespace. So this is going to split the string.
Hello Space World into a list of two items.
Hello and world. So Python is strict about types, every object, every value has a type.
And it won't auto convert them. So if you've programed in Perl or JavaScript or P HP and you take a string and a number and you can cabinet them,
it tries to convert the number to a string. Python won't do that.
So if we do this, we try to add a number to a string. It's going to give us a type error and a type error and tells us what's going on.
One of the skills you're going to need to develop in this class is the ability to read error messages.
And this error message tells us a cup, a few important things. It tells us that the error is a type error.
Other errors you're going to see are value errors, index errors, key errors, et cetera.
But this is telling us a type error, which means that we're trying to do something with the wrong type of data.
It then tells us two other things. It gives us this trace back of the code.
So it shows us where in the code it went wrong. The only thing we're doing here, we're not calling in many library functions or anything like that.
We're just trying to add a string in a number. We have our line of code. So it's showing us that it happened on line one, Maroon plus five,
and then it tells us a little bit more about the error can only concatenate stir, not end to stir.
So what this is telling us is that and Cat Nation only works on strings.
You can't concatenate things that aren't strings to a string.
And what we have here, we have a string and we have a number.
So if we want to if we want to to put five at the end of our our string, we can convert it with the Sturr function.
So Sturr is a function that takes an object and returns a string representation of that object intended for human consumption.
So if we do this, then we get our strength, then that will concatenate correctly and we get the string Maroon five.
So we've now seen three different kinds of operations that we can perform on python values.
We've seen an operator like Plasty Duck, the binary operators that go between their two operands so we can say six plus seven.
There's quite a few of these operators. We've seen a function which in this case, the function comes from a module.
So ENPI dialog and a function takes a value in return some other values.
We can compute the natural log of 10. And we've seen a method which is a function that's attached to an object.
So speed up. So the log function isn't attached to any particular object.
It's just a function hanging around. But the method. Hello.
So split is going to work on the hello world. String and.
And split it. So we've seen a method. If you're familiar with Java, they're like methods in Java and they operate on a particular.
They operate on a particular object. The Java equivalent of a python function would be a static method.
We've seen these three different kinds of operations in this class. We're going to learn how to write functions.
Eventually, we're going to learn how to write our own methods. But we aren't going to need that for the vast majority of this class.
You can also define how operators work on custom, on custom data types in Python.
We're not going to do that. It's learning to do that is outside the scope of this class.
But it is how some of the libraries that we're gonna be using work on the inside.
So we've got these different kinds of operations. There's a few other things that we can do.
So a few other data types you can work with. So now that split method, it returns a list.
Hello, World. And in Python, we write lists with square brackets and commas separating the values and we can write them.
So I can make a list that consists of these three values. Martin Cross and Grip's.
I can also save a list of variables. Now I have the variable Rowdy three.
That contains a list of these three names. We can then, though, add to the list.
So if too rowdy three that append Vogul. And we're going to now have a list that contains Martin Cross scripts and vocal.
Now, notice that in this code I did Rowdy three at a pen and then I just wrote Rowdy three.
That's because if you remember from the previous video. Jupiter shows the value of the last expression in your cell.
Rowdy three A doesn't return anything. The list append method.
Add something to the end of a list and actually it modifies that particular list.
We don't have a new list here. It modified our list object and and stuck Voegele at the end of it.
And it doesn't return anything.
So instead, what we're gonna do, what I often like to do when I do an operation like this is then at the end of the cell,
I just put the variable that I've been modifying so that it'll then show me what's currently in the variable.
So we can see that after we appended Voegele to the list stored in the variable Rowdy three, that list now consists of four items.
And yet the list now consists of four items and includes our new entry at the end.
So lists are indexed, starting with zero so rowdy, three of zero gives us Martin.
We can index backwards from the end, rowdy three negative one gives us voegele a slice, takes multiple elements from a list.
So rowdy three one Colen three gives us element the element that one and two.
What it does is it gives you it starts at the first index of the slice and gives
you all of the elements up to but not including the last element to the slice.
So. Zero is the first item, one is the second item.
So it's giving us items one and two and then three is one past the end.
These kinds of half open intervals. We call this a half open interval because it includes the left side and not the right side.
They're very, very common when we're using zero based indexing in a data structure because it's a very convenient way to express a range.
Also, the length of the range is. The end, minus the beginning, three, minus one.
It's going to give us a list of length to. One more thing we can do here is the land function.
Is a standard python function that will give you the length of anything that has a length like a list.
A number of other data structures have links. Most data structures that can contain other data structures will have a length.
It will also work on a string. But the length of rowdy three is currently four.
So we can also loop over a list. So this list, this loop here is going to loop for each person in the list.
Stored and rowdy three. It's going to print the person. So we get our four people.
Martin Cross scripts and vocal. What if we want to know the position of each item in the list as we go through the loop?
The enumerate function wraps a list and returns the list, but also the position in the list as we go through the loop.
And then this string here, this. That's prefixed with an F. We call this an F string.
And when you put a F right before the opening quote of a string,
you can then use squiggly braces and variable names to include variable values in the string.
It's one of the ways that Python lets you easily build up strings that contain additional data.
And so we're going to we're going to run this loop.
And now we see each. Each person is now prefixed with their member number.
And it's starting from zero because as we saw before, Python always starts from zero.
So the first one is members zero. And this enumerate function is just giving us the positions along with each item.
Python for loops operate over what what it calls iterable, something that iterable just feeds an object that you can use in a for loop.
Lists are iterable, but if you want to loop over a sequence of numbers like you might in Java.
So you want to go from zero one and two, you use a range.
So this is going to print zero one and two, because, again, the python does not include upper bounds of ranges and slices by default.
So we're going to go zero, one and two. So a tuple is another container.
It's like a list, except its size can't be changed. It's used for representing things like pairs.
So I'm going to create a variable called coords and I'm going to store the tuple three five in it coords.
Sub-Zero, then, is the first element of the tuple three chords.
One is five. If we did the Len of this tuple, we would we would get would get to a tuple can be unpacked by unpacking the tuple we take it.
So this tuple has two elements and we can say X, comma, Y to unpack the tuple into two different variables and then X is going to be three.
So the parentheses with the comma packs the tuple and assigning it to variable separated with a comma unpacks the tuple.
The tuple size has to match. So if we say X, Y, Z of coords and try to run that, it's going to tell us not enough values to unpack.
It expects three values X, Y and Z. But coords only has two values.
A dictionary is another data structure that maps, keys, often strings, but not always you can.
You can use numbers, tuples, any data structure that can't be changed, you can use as the key for a string or for a dictionary.
And so we're going to map some different animals to what they eat here. We created a variable.
Assigning to a variable doesn't return a value, so there's nothing to print here.
And then we can look up a value by its key. So we say Dietze of rabbit equals plants.
And that gives there like lists, except we can look them up by any key we want instead of having to look them up by a position.
So everything in Python is an object which has a type. We saw this when we saw the type error.
We try to to try to add the number five to the string maroon.
The Sipes we've seen in this video are integers, strings, lists, tuples and dictionaries.
There's a lot more to do with these.
I refer to you to the readings and also we're going to be introducing a various features of them as we go throughout the class.
Now, another thing that's important to understand is that in Python variables, store references to objects.
This is how Java works as well. But this matters, particularly for mutable objects.
So we have our list. Rowdy three. Now, if we assign the list to another variable, rowdy five and then we add Amanda and we print routing.
We show the result of rowdy five. OK. We have our list. Now we've added Amanda to the end of it.
The rowdy five and rowdy three variables are references to the same list objects.
We only look at Rowdy three. It's now going to show five elements, including Amanda.
Because when we assign the variable, it doesn't make a copy of the list.
All it does is it creates another variable that also refers to the list, too.
We modify this list, object and append modifies the object, what we call in place.
That means it modifies the object itself. It does not return a new object.
It the object changes and any variable that's referring to that object gets the change of this in-place
distinction is going to be important throughout the semester because some of the libraries we use,
they they offer options to whether you want to modify something in place or whether you want to return a new object that has the new data.
So there's a variety of ways. Resources to learn Python.
I'm going to be making some videos, but we're not going to have time in the videos to go into every piece of python you might need.
The tech chapters two, and I'm going to be writing some resources, chapters two and three in the text book are going to.
They talk about basic python operations and data structures. There's the Python tutorial that I'm providing you a link to.
That's a relatively comprehensive tutorial from the Python developers about the key Python language features.
If you really want to dove in depth, there's a book, Learn Python the Hard Way, which is quite comprehensive.
I'm also going to be providing in the resources section of the class site some additional notebook's that walk
through and demonstrate different Python features and give you information about the different operations.
For example, I'm planning on one such notebook that goes over a bunch of different things you
can do with lists more than I've had a chance to get into in in this video.
So to wrap up, Python supports many different data types. Everything's that object and variable store references to objects.
If you do an operation that modifies an object, all variables that refer to the same object are going to get are going to see the change.
You can perform a number of standard arithmetic operations on python variables.
And there's many, many more operations that we're going to be seeing as we go throughout the semester.