1
00:00:00,400 --> 00:00:05,320
Now in this lesson, I want to talk to you about Python variables and it's a

2
00:00:05,320 --> 00:00:09,220
really, really useful concept. But before we get started,

3
00:00:09,370 --> 00:00:14,370
first I want you to head over to the course resources and get hold of the Day

4
00:00:15,820 --> 00:00:20,820
1 variables start Repl.it sandbox or you can simply type the URL into the

5
00:00:22,330 --> 00:00:23,163
browser.

6
00:00:23,380 --> 00:00:28,380
I want you to first go ahead and fork this starting code so that you can work on

7
00:00:28,750 --> 00:00:31,990
your own version of it. Now once you've done that,

8
00:00:32,140 --> 00:00:37,140
then the first thing I want to do is to take a look at this line of code that

9
00:00:37,930 --> 00:00:41,830
you should have already. And when we run this,

10
00:00:41,860 --> 00:00:44,260
we should be pretty familiar with what happens, right?

11
00:00:44,260 --> 00:00:48,700
It will ask us for our name and when we hit enter, nothing happens.

12
00:00:49,060 --> 00:00:50,770
But behind the scenes,

13
00:00:51,160 --> 00:00:56,160
this name or this input has now been received by this function.

14
00:00:57,550 --> 00:01:02,020
But once that's done, it kind of just disappears, right?

15
00:01:02,680 --> 00:01:07,680
And there's no way for us to be able to refer to it in the future. Or is there?

16
00:01:10,980 --> 00:01:13,920
Well, this is where variables come in really handy.

17
00:01:14,520 --> 00:01:18,150
If I give the results of this action a name,

18
00:01:18,390 --> 00:01:20,940
well then I'll be able to refer to it later on.

19
00:01:21,540 --> 00:01:23,850
So at the beginning of the line,

20
00:01:23,940 --> 00:01:27,630
I'm going to call it name and then I'm going to add an equal sign

21
00:01:28,170 --> 00:01:33,170
and what this does is it assigns whatever it is that the user typed in as the

22
00:01:33,600 --> 00:01:38,600
input to this prompt to a variable called name.

23
00:01:40,350 --> 00:01:45,350
So now if I go ahead and run my code again and I enter a name,

24
00:01:47,970 --> 00:01:49,980
now once I hit enter,

25
00:01:50,040 --> 00:01:53,910
normally there's no way for me to get hold of this anymore, right?

26
00:01:54,480 --> 00:01:58,800
But now I can actually go ahead and write print.

27
00:01:59,220 --> 00:02:02,580
I'm going to print the value of this variable name.

28
00:02:03,300 --> 00:02:07,170
So now if I run my code and I write a name,

29
00:02:07,560 --> 00:02:08,393
hit enter,

30
00:02:08,759 --> 00:02:13,760
you'll see that this gets printed and I can use this at any point in my code

31
00:02:14,760 --> 00:02:19,760
just by referring to the name that's attached to this value.

32
00:02:21,510 --> 00:02:26,510
So it's almost like we've saved the data from this action to a name.

33
00:02:29,760 --> 00:02:33,660
And if you think about it, if you had a phone book, let's say,

34
00:02:33,660 --> 00:02:37,080
and you just jotted down the numbers of people,

35
00:02:37,560 --> 00:02:39,120
the next time you look at this,

36
00:02:39,150 --> 00:02:43,110
there's no way for you to know who's number that is, right?

37
00:02:43,740 --> 00:02:46,560
In a sense it's the same thing with the computer.

38
00:02:47,100 --> 00:02:50,160
Even though we've inputted this piece of data,

39
00:02:50,310 --> 00:02:55,310
there's no way for us to be able to refer to this data unless we give it a name.

40
00:02:56,760 --> 00:03:01,760
So in our phone book, we might say that this particular number is associated with

41
00:03:02,410 --> 00:03:07,410
the name James. And in programming we would call this name James a variable.

42
00:03:09,400 --> 00:03:14,400
So we could write something like James equals and then his phone number.

43
00:03:15,400 --> 00:03:19,360
And this means that in the future, if we ever need this piece of data,

44
00:03:19,570 --> 00:03:22,480
we can just refer to it by its name,

45
00:03:23,020 --> 00:03:25,900
the variable name of James. Now,

46
00:03:25,960 --> 00:03:28,750
as the name variable suggests,

47
00:03:29,470 --> 00:03:33,670
it's something that can be changed or can be varied.

48
00:03:34,270 --> 00:03:35,530
So for example,

49
00:03:35,650 --> 00:03:40,480
just because this name is set to something up here doesn't mean that I can't

50
00:03:40,480 --> 00:03:43,840
change it later on. So to make things simple,

51
00:03:43,840 --> 00:03:48,610
let's go ahead and delete the input function and let's just say name is equal to

52
00:03:48,610 --> 00:03:50,110
Jack. Print name.

53
00:03:50,320 --> 00:03:55,320
When I run that Jack is what gets printed inside the console because I'm now

54
00:03:55,720 --> 00:03:59,290
referring to that piece of data by the variable name.

55
00:04:00,100 --> 00:04:05,100
But if later on I decide to give this variable a different piece of data to hold

56
00:04:06,010 --> 00:04:06,843
on to,

57
00:04:07,000 --> 00:04:12,000
let's say Angela and I print name at this time point after I've changed it,

58
00:04:15,760 --> 00:04:18,850
what do you think these two lines will print?

59
00:04:19,450 --> 00:04:21,190
What do you think will first be printed?

60
00:04:21,250 --> 00:04:25,990
What do you think will be printed second? Let's hit command + enter or control +

61
00:04:25,990 --> 00:04:27,790
enter and see for ourselves.

62
00:04:28,390 --> 00:04:33,390
So even though they're both printing the same variable, at this point when we first

63
00:04:34,810 --> 00:04:38,080
call print, it's holding on to the value of Jack,.

64
00:04:38,500 --> 00:04:43,500
But by line 5 I've now changed it so that it's holding onto the value of

65
00:04:43,780 --> 00:04:44,613
Angela.

66
00:04:45,550 --> 00:04:50,550
This makes it a lot easier when we want to write code like what we did before.

67
00:04:51,250 --> 00:04:51,880
For example,

68
00:04:51,880 --> 00:04:55,750
this is the line of code that we had before and we knew that we were going to

69
00:04:55,750 --> 00:05:00,750
get a piece of data from this input function and then we would calculate a new

70
00:05:02,050 --> 00:05:04,510
piece of data using the Len function

71
00:05:04,990 --> 00:05:07,540
and finally we're going to print everything out.

72
00:05:08,320 --> 00:05:12,670
Instead of having such a long line of code, which can be quite confusing,

73
00:05:13,120 --> 00:05:18,100
we can, if we wanted to, use variables to store each of these steps.

74
00:05:18,550 --> 00:05:19,390
So for example,

75
00:05:19,390 --> 00:05:24,390
we could say that the name was equal to whatever the user inserted as the

76
00:05:25,120 --> 00:05:25,953
input

77
00:05:26,530 --> 00:05:30,310
and then when we wanted to calculate the length,

78
00:05:30,730 --> 00:05:35,680
then we could set that variable to equal the Len function.

79
00:05:36,070 --> 00:05:41,070
And then inside the parentheses, we'll pass in the name, the value, that's stored

80
00:05:41,800 --> 00:05:45,580
in the name variable right here. And finally,

81
00:05:45,820 --> 00:05:50,710
all that we need to do is to print the length,

82
00:05:53,620 --> 00:05:56,470
like so. So in this case,

83
00:05:56,840 --> 00:06:01,840
this data is stored under this name and then that data is what's going to be

84
00:06:02,360 --> 00:06:04,940
used in order to carry out this function

85
00:06:05,510 --> 00:06:08,780
and then the result will get stored in yet another variable

86
00:06:09,110 --> 00:06:11,360
and finally that variable will be printed out.

87
00:06:11,870 --> 00:06:16,870
So let's go ahead and run this code and type in my name.

88
00:06:17,090 --> 00:06:21,950
So Angela and hit enter and it does exactly the same thing,

89
00:06:22,460 --> 00:06:27,460
but now we've got all of these pieces of data all associated with a name that we

90
00:06:29,450 --> 00:06:34,160
can refer to at any later point in our code. Now it's time,

91
00:06:34,190 --> 00:06:36,560
you guessed it, for a code challenge.

92
00:06:36,860 --> 00:06:39,500
And I'm going to get you to apply what you've learned.

93
00:06:39,800 --> 00:06:42,860
So for all of that and more, I'll see you on the next lesson.

