Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
Now in this lesson, I want to talk to you about Python variables and it's a
2
really, really useful concept. But before we get started,
3
first I want you to head over to the course resources and get hold of the Day
4
1 variables start Repl.it sandbox or you can simply type the URL into the
5
browser.
6
I want you to first go ahead and fork this starting code so that you can work on
7
your own version of it. Now once you've done that,
8
then the first thing I want to do is to take a look at this line of code that
9
you should have already. And when we run this,
10
we should be pretty familiar with what happens, right?
11
It will ask us for our name and when we hit enter, nothing happens.
12
But behind the scenes,
13
this name or this input has now been received by this function.
14
But once that's done, it kind of just disappears, right?
15
And there's no way for us to be able to refer to it in the future. Or is there?
16
Well, this is where variables come in really handy.
17
If I give the results of this action a name,
18
well then I'll be able to refer to it later on.
19
So at the beginning of the line,
20
I'm going to call it name and then I'm going to add an equal sign
21
and what this does is it assigns whatever it is that the user typed in as the
22
input to this prompt to a variable called name.
23
So now if I go ahead and run my code again and I enter a name,
24
now once I hit enter,
25
normally there's no way for me to get hold of this anymore, right?
26
But now I can actually go ahead and write print.
27
I'm going to print the value of this variable name.
28
So now if I run my code and I write a name,
29
hit enter,
30
you'll see that this gets printed and I can use this at any point in my code
31
just by referring to the name that's attached to this value.
32
So it's almost like we've saved the data from this action to a name.
33
And if you think about it, if you had a phone book, let's say,
34
and you just jotted down the numbers of people,
35
the next time you look at this,
36
there's no way for you to know who's number that is, right?
37
In a sense it's the same thing with the computer.
38
Even though we've inputted this piece of data,
39
there's no way for us to be able to refer to this data unless we give it a name.
40
So in our phone book, we might say that this particular number is associated with
41
the name James. And in programming we would call this name James a variable.
42
So we could write something like James equals and then his phone number.
43
And this means that in the future, if we ever need this piece of data,
44
we can just refer to it by its name,
45
the variable name of James. Now,
46
as the name variable suggests,
47
it's something that can be changed or can be varied.
48
So for example,
49
just because this name is set to something up here doesn't mean that I can't
50
change it later on. So to make things simple,
51
let's go ahead and delete the input function and let's just say name is equal to
52
Jack. Print name.
53
When I run that Jack is what gets printed inside the console because I'm now
54
referring to that piece of data by the variable name.
55
But if later on I decide to give this variable a different piece of data to hold
56
on to,
57
let's say Angela and I print name at this time point after I've changed it,
58
what do you think these two lines will print?
59
What do you think will first be printed?
60
What do you think will be printed second? Let's hit command + enter or control +
61
enter and see for ourselves.
62
So even though they're both printing the same variable, at this point when we first
63
call print, it's holding on to the value of Jack,.
64
But by line 5 I've now changed it so that it's holding onto the value of
65
Angela.
66
This makes it a lot easier when we want to write code like what we did before.
67
For example,
68
this is the line of code that we had before and we knew that we were going to
69
get a piece of data from this input function and then we would calculate a new
70
piece of data using the Len function
71
and finally we're going to print everything out.
72
Instead of having such a long line of code, which can be quite confusing,
73
we can, if we wanted to, use variables to store each of these steps.
74
So for example,
75
we could say that the name was equal to whatever the user inserted as the
76
input
77
and then when we wanted to calculate the length,
78
then we could set that variable to equal the Len function.
79
And then inside the parentheses, we'll pass in the name, the value, that's stored
80
in the name variable right here. And finally,
81
all that we need to do is to print the length,
82
like so. So in this case,
83
this data is stored under this name and then that data is what's going to be
84
used in order to carry out this function
85
and then the result will get stored in yet another variable
86
and finally that variable will be printed out.
87
So let's go ahead and run this code and type in my name.
88
So Angela and hit enter and it does exactly the same thing,
89
but now we've got all of these pieces of data all associated with a name that we
90
can refer to at any later point in our code. Now it's time,
91
you guessed it, for a code challenge.
92
And I'm going to get you to apply what you've learned.
93
So for all of that and more, I'll see you on the next lesson.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.