Locked lesson.
About this lesson
What is a variable and how can you name them?
Exercise files
Download this lesson’s related exercise files.
Variables.docx58.7 KB Variables - Solution.docx
58.8 KB
Quick reference
Variables
Variables are containers that hold things.
When to use
Use variables whenever you want to store information for later use.
Instructions
To create a variable, simply name it and set it equal to some data:
first_name = "John"
Variables start with lowercase letters. You can use underscores and numbers in them.
Variables should not start with uppercase letters or numbers.
Variables can be changed over time, whenever you like.
Hints & tips
- Variables are containers that hold data
- Variables hold one thing at a time
- first_name = "John"
- 00:04 In this video I want to talk about variables and
- 00:07 variables are just very important to all programming.
- 00:10 They're sort of a fundamental thing.
- 00:11 You're always going to have them, you're always going to use them.
- 00:14 And a variable is basically a bucket.
- 00:16 It's a container, it holds something and it generally holds one thing.
- 00:20 You can put different things in over time.
- 00:22 You could put something in and then take it out, and put something else in, but
- 00:26 it's just very important and creating a variable on Python is super easy.
- 00:30 You don't have to declare it, you don't have to do anything except just name it,
- 00:35 and so lets create a variable called first_name.
- 00:39 Now, we have a variable and to put something in it we just put an equal sign,
- 00:43 this is called an assignment operator, and
- 00:45 then quotation marks and I'm going to type John.
- 00:47 So this is actually a string, and we're going to talk about strings
- 00:50 in a little bit, but you can use single quotes or double quotes,
- 00:55 and there's a reason to use either one of those.
- 00:57 We'll talk about that later too, not really important at the moment.
- 01:01 But now we have assigned John into the variable first_name.
- 01:05 So if we save this and run our program, you see nothing happens.
- 01:09 Because we've created a variable, but we haven't actually done anything with it.
- 01:13 So if we want to say print this to the screen, we just type in the variable.
- 01:18 And you can see as I start to type this out,
- 01:20 our text editor here already knows what we're trying to do.
- 01:24 So you can click on this or you can hit your tab key and
- 01:27 it will auto fill it in for you, which is really nice.
- 01:29 So if we save this,
- 01:30 come back and run it again, we see it prints out John to the screen.
- 01:34 So that's really, really cool.
- 01:36 So like I said, you can could put stuff in variables, they're like a bucket,
- 01:38 they're a container.
- 01:39 You could put strings of text, you could put numbers and
- 01:42 we're going to talk about numbers in a bit.
- 01:44 We could say, my_fav_number = 41.
- 01:49 And notice, this number does not have a quotation marks around and
- 01:53 there's a reason for that, strings have quotation marks numbers do not,
- 01:56 they're a different data type.
- 01:57 And we'll talk about data types in a couple of videos from now,
- 02:00 but those are variables.
- 02:01 Now, a couple of things, when you're naming your variable first off,
- 02:04 it's important to name your variable in a descriptive way.
- 02:08 In this case, we're going to be putting a first name in here.
- 02:11 So I named the variable first name that sort of makes sense.
- 02:14 If you're going to put somebody's age, you might name your variable age.
- 02:17 Now, whatever you're trying to do, be descriptive.
- 02:20 That's very helpful.
- 02:21 The next thing to notice is
- 02:22 variables have to be one word.
- 02:24 And you see this is two words, but it's all connected.
- 02:27 We could not have first and then space, that's invalid.
- 02:32 You see we get this angry x that says invalid.
- 02:36 So you need to, when you using multiple words like that use an underscore.
- 02:40 Now, variable names can only be characters, letters,
- 02:45 underscore or numbers.
- 02:47 But you can't start your variable with a number so we couldn't say like
- 02:52 1_first_ name, we get this angry x, that's invalid.
- 02:57 Also variable names are case sensitive.
- 02:59 So here we have lowercase f, lowercase m.
- 03:02 We could go First_Name, let's just change it to Tim.
- 03:08 Now, these are two very separate variables.
- 03:11 They're completely different because Python is case sensitive.
- 03:14 It sees this uppercase F and uppercase N, it says okay,
- 03:18 this is a different variable than this one.
- 03:20 So keep that in mind.
- 03:22 Generally speaking, you don't want to use uppercase letters for the first word.
- 03:27 Generally those are reserved for classes, you can get away with it but
- 03:29 it's not a good thing to do.
- 03:31 Also, we use these underscores.
- 03:33 Some people use camel case, and that's like first and then capital Name,
- 03:37 and we call this camel case because there's like a hump in the middle,
- 03:41 this uppercase is like a camel has a hump.
- 03:43 Now, this is popular in a lot of programming languages,
- 03:46 JavaScript is like this, you can do this in Python but I don't recommend it,
- 03:51 sort of the convention is to use an underscore, sort of a personal preference
- 03:55 but the Python community tends to use this underscore instead of camel case.
- 03:59 So those are some rules for naming variables.
- 04:03 Like I said, we can change these over time.
- 04:05 So if I come back here and go first_name = "tim" and
- 04:10 then print(first_name), if I save this and
- 04:15 run it it's going to be John, and then Tim.
- 04:19 because the first time it's John, and then we changed it to Tim, and
- 04:23 printed it out the second time, and so now it becomes Tim.
- 04:26 So you can change them over time, you can add things in, you can take things out.
- 04:30 We can remove anything, save this and run it.
- 04:33 We get John and nothing, so like I said variables you're going to use them
- 04:37 forever, they're a fundamental part of all programming doesn't matter what
- 04:40 programming language you'll use they all have variables and very handy.
- 04:43 So in the next video we're going to look at data types.
Lesson notes are only available for subscribers.