Check out a free preview of the full Python Fundamentals course

The "Variables" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina introduces variables by speaking to naming conventions, a few "gotchas", as well as NoneType and reassigning variables.

Preview
Close

Transcript from the "Variables" Lesson

[00:00:00]
>> Nina Zakharenko: Variables in Python, they allow us to store information and give it a useful label that we can then use to retrieve the information later. We can use these variables to store strings, numbers, or even more advanced data types, lists, etc. We assign values to variables by putting them at the right of an equal sign.

[00:00:22]
So the variable name is on the left side equal sign followed by the thing that you're assigning. And because Python is a dynamic language, we don't need to declare the type of the variable before we store data in them. So, I can make x a number, I can make x the string, doesn't matter, no errors.

[00:00:45]
I think for you JavaScript folks, you're like yeah, whatever. But if you come from like, Java or .Net, or a compiled language, it's kind of a big deal, right? When creating variables in python, there are a few best practices you should follow, we talked about PEP 8. So PEP 8 convention says that names should all be in lower case, whole words separated by an underscore.

[00:01:14]
So if I wanted to say my list, I can do that, right? This is the convention you all see followed. In general, it's okay to be a little bit verbose, for example, x is not a great variable name, right? Unless you're doing some sort of mathematical operation, x doesn't necessarily makes sense.

[00:01:35]
We're gonna use some shorter variables names just today for the sake of time and typing. But in your programs, it's okay to name things. It's okay to name things you know as descriptive as you like. There are few other naming gotchas like you can't name your variable with operations in Python.

[00:01:57]
Like don't name your variable, ands. But in general, as long as you're variable name is pretty descriptive, then you'll be fine. We talked about the type function a little bit. So if I look at type of x, we'll see that it's a stir. And then just to mention one special type in Python, you might have seen it called null in other languages, right?

[00:02:26]
It signifies nothing, no value. And in Python, that type is none. So, if I set my variable x to none, and I try to just enter the variable and press enter in the wrapper which should show me some results, I'll see nothing. And if I check the type of x, I'll see that it's nonetype.

[00:02:54]
And if I set my variable to something that's not nonetype in the REPL only, I can just type that variable and instantly see the value.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now