3. Data types and variable naming convention in python
Oct 8, 2023
There are different data types including numeric, string, sequential, mapping, boolean, set, and binary. For these different types we have different variables including integer, float, complex, string, list, tuple, set, dictionary, boolean, byte, bytearray, and memoryview.
Variables can be named with flexibility. Preference is given for short, descriptive names and this video explains when we use camelCase, CapCase, and snake case along with general rules for naming variables, functions, modules, and constants.
All right, in this video, we're going to talk about the different data types available in Python.
So, variables are the things that we're going to use in our code.
We use them to store information, right?
We can also manipulate and change these variables in our code as we need to.
So, what are the different types that are available?
There's lots of different types.
Your type could be text, right?
If it's a string of text, you could store that in a string variable.
They could be numeric, those are going to be our integers,
but we also have float and complex,
and we'll talk about some of the differences there.
They could be a sequence of things, right?
So, like a shopping list has apples, grapes, and bananas, right?
It's a sequence of things.
We could store those in a list or a tuple.
It could be something for mapping, correlating something to another thing.
We're going to use dictionaries for those.
It could be a set, which is similar to a list and a tuple.
We'll talk about those.
A Boolean, which is a true or false value.
Or binary, right?
So, those are our data types.
Let's dive into some of the specifics of what these all are, right?
So, first up is integers, right?
An integer is a whole number, either positive or negative,
but no decimals, okay?
And it can be unlimited length.
You can make this as long as you want, as long as it's a whole number.
Now, after that, we've got floats.
Float stands for floating point number.
Now, again, these could be positive or negative,
but they can contain decimal values, right?
One or more decimal points, okay?
We can have complex numbers, right?
Complex numbers, we write them just like normal,
but they now have a J part for the imaginary part, okay?
We can have strings.
Strings are defined with quotes.
We use quotes when we define strings.
Booleans can be true or false values.
We have our lists,
and that is a collection of ordered and changeable values,
and it allows duplicate members, okay?
We're going to show you examples of all these things in a minute.
You can have a tuple.
A tuple is a collection that is ordered but not changeable.
You can't change that, right?
And it also allows duplicate members.
A set is unordered and unindexed,
and it does not allow duplicate members.
And then a dictionary is a collection which is unordered, changeable, and indexed.
No duplicate members, okay?
So those are the different types of data
and the different types of variable that we're going to work with.
We're going to see lots of examples of them.
What about names that we can choose for our variables?
So in Python and in most program languages,
you have a lot of flexibility when naming your variables,
but not everything is allowed, right?
For example, you can't start your variable with a digit.
It has to start with an alpha character, alpha, right?
And after that, you can use any alphanumeric character that you want, right?
A through Z, 0 through 9,
and you can use underscores as well in your variables.
There are some names that will be forbidden,
and those are keywords.
They're reserved for other things.
So for example, the word if, or is, or and, true.
All of these keywords are reserved,
and you cannot use them for a variable.
You have to use something else.
If you wanted to name false underscore something else,
false underscore dog, then that's a fair name.
You could have false dog as a variable,
but you can't use the word false as it is or any of these other ones, okay?
Now, in some languages,
we mentioned that you have to define your variables up front
before you ever use them.
In Arduino and C and some of these other languages, you have to do that.
You don't have to do that in Python.
If you in the moment think,
oh, I need another variable to represent this, great.
You can type it in the middle of your code.
No big deal, okay?
Now, what names should you use for things?
There is some convention, right?
There's something called camel case, right?
The second and all subsequent words are capitalized in camel case, right?
And that makes it easier to see the different words, right?
So for example here, if the variable you're trying to collect
has something to do with the number of college graduates,
you might name it something descriptive,
like number of college graduates.
But you can see that we've capitalized G, C, and O.
That would be camel case.
I guess it kind of looks like a camel,
like it starts low and then there's bumps
that kind of look like the humps.
Anyways, then you've got cap case.
So this is just like camel case,
except all the first letter of each word is capitalized.
So number of college graduates, all capitalized.
And then you've got snake case,
where it's just the words separated by underscores.
These are typical conventions that are out there.
Now, in Python, there's a few additional rules that we should follow.
For example, you should avoid lowercase L.
You should avoid 1, O, or big O, right?
Where possible, try and avoid those,
because they can be confused, right?
You can think that L is a 1, that O is a 0, or whatever.
So where possible, try and avoid those.
When you're naming your module, remember that's your file name
that has the .py extension on the end,
you should use all lowercase names and an underscore
if it makes it easier to read and understand.
Python packages, like pandas, scikit-learn, like numpy,
we'll show you some of these later,
those are all lowercase names,
and you try and go for shorter names there, okay?
Variables typically are written in camel case, right?
So the first letter is lowercase,
but the next words that come are uppercase, right?
And we have a preference for short names for variables.
Functions should be lowercase, and you should use underscores
if that improves the readability.
And constants should be uppercase with underscores for readability.
So those are variables in Python,
and now we're going to dive in and show you some examples of each kind.

