Locked lesson.
About this lesson
In this lesson, we'll discuss how to store data within your program using variables and how to display them using the Print() command.
Exercise files
Download this lesson’s related exercise files.
7 - Variables.docx60.5 KB 7 - Variables SOLUTION.docx
55.4 KB
Quick reference
Python Variables and Print()
Variables are containers used to hold things like text, numbers, lists, tuples, and many other things.
When to use
Use them whenever you need to store data in your program.
Instructions
To create a variable, just name it and assign some data to it:
first_name = "John"
or
favorite_number = 41
To print out variables, just reference them in a print statement:
print(first_name)
Hints & tips
- first_name = "John"
- favorite_number = 41
- print(first_name)
- 00:05 Okay, in this video I want to talk about variables.
- 00:07 And variables are one of the most key fundamental things in computer
- 00:10 programming.
- 00:11 Every program you ever create will have lots and lots of variables.
- 00:15 And a variable is like a bucket.
- 00:17 It's a box, it holds things.
- 00:18 And they can hold all different types of things.
- 00:20 They can hold words.
- 00:21 They can hold letters.
- 00:22 They can hold numbers.
- 00:23 They can hold other variables.
- 00:25 They can hold lists.
- 00:27 They can hold tumbles.
- 00:28 They can hold dictionaries.
- 00:29 We don't know what any of those things are, but
- 00:31 we're going to learn them pretty soon.
- 00:32 They're just all around very important items.
- 00:36 So in Python to create a variable all you have to do is name it.
- 00:39 So I'm going to call this one first underscore name, all right?
- 00:43 So this is now a variable.
- 00:45 And to assign something to it, we use an assignment operator.
- 00:48 In this case, the equal to sign.
- 00:51 And then we can just put whatever we want in there.
- 00:53 So I'm going to type in John.
- 00:55 So now we've created a variable, first_name.
- 00:58 And we've assigned some text, which Python calls strings.
- 01:03 We've assigned this string, a string of characters to this variable.
- 01:07 So now anytime we reference first underscore name, we are referencing John.
- 01:13 So we can come down here and we can for instance, print it out.
- 01:16 So we can print first underscore name.
- 01:19 So let's go ahead and save this control S to save it,
- 01:22 headed back over to our terminal.
- 01:25 And let me clear the screen.
- 01:27 And let's run this file Python Hello.pi, and it says Hello World and
- 01:32 then it prints out John, which makes sense.
- 01:35 We told it to just print out John.
- 01:36 Now I can comment out this line, this little hashtag,
- 01:40 this number sign this comments things out.
- 01:44 Comments are useful in that we can describe what our texts, what our code is.
- 01:49 So for instance, I could go a create variable to hold first names, right?
- 01:55 So this is a comment.
- 01:57 So Python will ignore this, but it's good for
- 01:59 us as we look back over our code in the future.
- 02:02 It helps us know what we were doing when we actually wrote the code.
- 02:06 So you can see we've created this first name variable.
- 02:09 We've called it John, and we printed it out.
- 02:11 So very cool.
- 02:12 Like I mentioned, you can assign anything to variables.
- 02:15 So let's call, let's create another one called favorite underscore number.
- 02:20 Let's set that equal to 41.
- 02:21 Now you notice this time I didn't put quotation marks around
- 02:25 it because with text, we always put quotation marks around it.
- 02:28 With numbers we don't.
- 02:30 So now we can just come down here and just like before,
- 02:33 instead of first name, we can print out favorite number.
- 02:38 So let's head over to our terminal and run this guy.
- 02:41 And you can see it prints out 41.
- 02:43 Notice it did not print out John.
- 02:46 We've created a variable.
- 02:48 We've set John in it, but
- 02:49 we're not actually doing anything with that variable.
- 02:52 We're not printing it out.
- 02:53 We're just printing out favorite number.
- 02:55 So it's sort of there.
- 02:56 It's in our program.
- 02:57 It's been saved to memory, but we haven't actually used it and
- 03:00 we can use it later on if we want.
- 03:02 But that's how that works.
- 03:04 So variables, like I said, are just super important and
- 03:07 creating them is just as easy.
- 03:09 Now let's take a look at how I name this thing.
- 03:12 You'll notice I put an underscore between these two words.
- 03:16 I could just as easily have done it like this, but
- 03:19 that's not the convention in Python.
- 03:21 And if you think about it, this is really kind of hard to read.
- 03:24 It's a good idea to separate these things somehow.
- 03:27 And so we separate them with underscores.
- 03:30 You cannot use spaces.
- 03:32 Python won't know what to make of this.
- 03:35 It has to be connected with something.
- 03:36 Now you might be tempted to do something like that.
- 03:39 You can't really do that because that's the subtract sign, right?
- 03:43 Just like the addition sign.
- 03:45 So in this case, Python would try to subtract number from favorite,
- 03:50 and it would get very confused by trying to do that.
- 03:54 So, there are some characters we can't use when naming a variable.
- 03:58 You also wouldn't go favorite plus number, right?
- 04:01 For the same reasons, but underscores work well.
- 04:04 Some people like to do something called camel case,
- 04:07 which would be something like favorite number equals 41.
- 04:11 And you notice we call it camels because the second number is always capitalized,
- 04:17 sort of like the hump of a camel, right?
- 04:19 A lot of programming languages use this.
- 04:22 I know this is popular with JavaScript, they like to do things like this.
- 04:25 Python generally doesn't use camel case.
- 04:28 I mean you can, it's perfectly valid, but
- 04:31 the convention is to use underscores when using more than one word.
- 04:35 Also mention when naming variables, try to be as descriptive as possible.
- 04:40 You'll notice up here, we're saving a first name, so I called it first name.
- 04:45 I could just as easily have called this variable, 31
- 04:50 equals John, right?
- 04:55 This is valid too.
- 04:57 But this doesn't tell me anything, variable 31.
- 04:59 I don't know what that is.
- 05:01 On the other hand, I know exactly what first name is.
- 05:03 It's likely to be a first name, right?
- 05:06 So you want to be as descriptive as possible when you're creating variable
- 05:09 names, and it just makes life easier.
- 05:11 So finally, you'll notice this is lowercase and not uppercase.
- 05:15 You can use uppercase,
- 05:16 but again, the convention is to use lowercase when naming variables.
- 05:21 It's just sort of what Python people do.
- 05:22 Uppercase works, this is valid, but the convention is to use lowercase.
- 05:27 So that's a crash course in variables.
- 05:29 We'll use these throughout the course and you get very familiar with them and
- 05:32 very used to using them, and really not much to them.
- 05:35 Just remember, they're sort of like a box.
- 05:37 They're a bucket, you can put things in, you can take them out,
- 05:40 you can use them again.
- 05:41 You can add new things in later on, you can delete what's in there.
- 05:44 And we'll get into all that if we need to throughout the course.
- 05:47 But really, this is all you need to know about variables right now.
- 05:50 And that's all there is to it.
- 05:52 So in the next video, we'll start to look at the Python list.
Lesson notes are only available for subscribers.