Check out a free preview of the full Practical Guide to Python course

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

Nina demonstrates how to create variables in Python. Variables are dynamically typed and the recommended naming convention is all lowercase letters with underscores between words.

Preview
Close

Transcript from the "Variables" Lesson

[00:00:00]
>> All right, so we're going to dive into variables and data types. And these are gonna be the simple ones. So numbers, strings, lists, those kinds of things. We're going to go ahead and open up the ripple. Once you have the rappel open. My fiuas code does not want to start the rappel.

[00:00:37]
Unless it's done so in a different terminal.
>> I was having a similar issue, where I had the repo open on one of the terminals, I tried to open a new one and run the file, it would keep using the old one that the repo running. I don't know how to open a new Python terminal, like a second one.

[00:00:57]
>> Yeah. So there, that's a great question. Somebody asked, how do you open a new Python terminal, envious code. You should see here a drop down and this'll be all of the different terminals that you have open. You can open a regular terminal with the plus symbol here.

[00:01:14]
Or in the command palette, if you have the Python extension you'll see Python Create Terminal. And that will create a new terminal with your virtual environment activated. I'm going to hit Delete and close all of these out and try to start my rebel again. There we go. Let's come up now.

[00:01:37]
Yeah, sometimes there's small changes in VS code throughout versions. So if something's not working, shut it down, start it back up, or in this case, just close all of your open terminal windows and start from scratch. Hopefully you don't run into the same issue that I did. Well, so you should all have your REPL open and see these three arrows and a blinking cursor to indicate that you can now start typing into it.

[00:02:11]
So I mentioned earlier that Python is a dynamic language. And we defined some variables earlier on when we were exploring the REPL. If you come from a language like Java, you will notice that I didn't have to specify the type. When I instantiated this variable, I didn't have to tell Python, hey, this is an integer.

[00:02:35]
I could just define whatever variable I wanted and the. Type it doesn't need to be consistent throughout the execution of your code. So this could be a number in one instance, I can change it to a string and in other. This is something that you want to be very mindful of because it can introduce a lot of subtle bugs.

[00:03:05]
And because Python is a dynamic language, naming variables is very, very important. So you want to be as explicit as possible. Once you've moved on from the learning phase. You really want to avoid having any single variable names where you're not really sure. What they do, because you're gonna end up in a world of pain afterwards.

[00:03:25]
So be as descriptive in naming your variables as possible. Now, in Python convention says that your variable should be lowercased and separated with underscores. That is called snake casing, which I think is pretty cute. Snake case naming your variables and snake case is convention in Python. So try to follow that as much as possible regardless of what the convention is in the programming language that you're coming to Python from.

[00:04:05]
In Python variable names cannot start with special symbols or numbers. So if I try to name a variable like this, I'm going to get a syntax error saying that this is invalid syntax. There are some variable names that Python will allow you to use. But you really shouldn't.

[00:04:30]
And these are special keywords in Python. They are built in functions. And this is something that might bite you as you're learning Python. But I think you'll quickly start to recognize when this is happening. And when we start working in files, those keywords will be a different color.

[00:04:51]
And so you'll be able to recognize them a little bit more easily. So some obvious keywords are going to result in syntax errors. If I tried to name a variable if that's a invalid syntax syntax error, because that's the definition of an if statement. But here's a really subtle bug that bit me so many times when I was learning Python.

[00:05:21]
So in Python, one of the ways that you can declare a list because in Python everything is an object is by calling the list constructor. And that just returns an empty list. Will this let's do this is this a syntax error in Python? Now, Note is not. Now if we try to make a new list again using the list constructor, we're going to get an error that says that stir.

[00:05:57]
Stir meaning string. Object is not callable. Because what's actually happening under the hood is that Python is trying to do this. You've just made the list constructor into a string. And this is one way of having very, very subtle annoying errors. So don't name. Don't name your variables, things like stir list, etc.

[00:06:32]
If you're not sure while you're working on the RAPLE you can type in one of these keywords just right in the RAPLE and it will come back with class. In this case class int. That means that this is one of those built in Python.

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