Course Content
1.
Why Learn Python?
4 min
5 min
0
2.
What is the Best Software for Learning Python?
5 min
5 min
0
3.
Python List Functions
0 min
1 min
0
4.
All Python Operators (with examples)
0 min
1 min
0
5.
How to Declare a Variable in Python
2 min
3 min
0
6.
Variable Naming Rules and Conventions
8 min
6 min
0
7.
Use Descriptive Variable Names
4 min
3 min
0
8.
Integers and Floats
0 min
6 min
0
9.
Familiar Arithmetic (math) Operators
0 min
2 min
0
- Save
- Run All Cells
- Clear All Output
- Runtime
- Download
- Difficulty Rating
Loading Runtime
In the previous video, we created a variable called my_age
and we stored the value of 30
to it.
Variables (even ones as simple as this one) are useful, because now instead of using the literal value of 30 in our code we can use the variable itself and it will represent the value that is stored inside of it. Imagine you have a birthday (congratulations by the way) and you become a year older.
You could express it like this:
31
What's the point of variables?
You may be thinking to yourself, "But Ryan, making that variable and typing out my_age = 30
seems like more work than just using the number 30
directly. So what's the point of using a variable here?"
Well, the biggest reason why variables are going to be so critical in the code that we write is that we're going to store a lot more than just simple numbers like the number 30
to variables.
Here's some examples of other values that we will be storing to variables in later lessons:
Animate these onto the screen as they're said.
Now, we've already used integers, like the number 30. But very soon we'll learn about another way of representing numbers called "floats".
- Integers:
30
,0
,-279394
- Floats:
3.14
,0.5
,27.2
We'll learn how to store text to variables in the form of (what are called) "strings."
- Strings:
"Hello, World!"
,"Ryan Allred"
But also, there's Booleans, Lists, Tuples, Dictionaries, Objects, and a whole host of other kinds of values that we'll be storing to variables.
- Booleans:
True
,False
- Lists:
[1, 2, 3, 4, 5]
- Tuples:
(3, 5)
- Dictionaries:
{"first": "Ryan", "last": "Allred"}
- Objects:
<object Car at 0x00yc4040>
In fact, as data scientists we often store entire datasets within a single variable. Imagine that you had to type out your entire dataset anytime you wanted to do something with it. It's much easier to let a variable hold our data and then do things to the variable as needed.
The Importance of Descriptive Variable Names
Another big reason why variables are so important is that by giving our variables good names we can make it easier for others to understand what our code is doing. This is called "Readability". If your code has high readability that means that it's easier for others to understand what your code is doing when they read through it.
What if instead of saying
my_age + 1
We had instead used the literal value 30
?
30 + 1
?
Well that could refer to anything. 30 + 1
could be the number of potato chips I've eaten while editing this video. It could represent the number of students in a class when somebody new moves in. Or a million other things. The expression just doesn't do a very good job at conveying any context about itself.
Now imagine that the line of code was this:
kids_in_family + 1
That line of code means something very different to the reader than:
number_of_ex_wives + 1
Descriptive variable names give helpful context about our code and make it more readable –makes it easier for the person reading it to understand what it's doing at-a-glance. Having readable code is often more important than the number of lines of code that you write. It's more important than how fast your code runs. Readable code is easier to maintain and makes it easier for both your colleagues and future you (most importantly, in my opinion) to make sense of what you've done.
You will be reading code more than you will be writing it.
So I hope you can see just how important it is to use descriptive variable names.
However, unfortunately, we can't name a variable absolutely anything we want. There are some small restrictions –which we will go over in the next lesson.