Python for Absolute Beginners Transcripts
Chapter: Writing your first lines of code
Lecture: Concept: Variables and types
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's review this idea of variables
0:02
data values, and types.
0:04
We saw that we can create variables
0:06
just by saying their name, and then giving them a value.
0:09
Though, in our Python REPL, Read, Eval, Print, Loop
0:12
we were able to type x = 42.
0:15
Notice this green dip with a hash
0:17
in Python you can make comments
0:19
that are meant for people that have no effect
0:21
on how code runs.
0:22
So we're going to say hash if we were to run Typhon X
0:26
but what we'd get back is this class inch.
0:29
Remember we said there's a way in Python to ask
0:31
What type of data are we working with?
0:34
And here when you work with whole numbers in Python
0:36
that's an integer.
0:37
We came up with another variable, 19, also an integer
0:41
then we saw we can use them in expressions
0:43
like what is x + 7 * y?
0:47
Then if we don't assign it to a value
0:48
that just kicks out whatever the response
0:50
or the evaluation, the value of it is, so 175.
0:54
We can also create text base data
0:58
and these are strings.
0:59
They go in single quotes, or double quotes.
1:01
Like we have here, so we said name = 'Michael'.
1:04
Now if we asked the type again
1:06
this one has changed from int to str.
1:08
And you can combine strings and numbers
1:12
by adding them together
1:13
but if you try to mix these types
1:15
it doesn't really like it so much.
1:16
Here we saw that there was an error
1:18
if we said name + x.
1:20
We got an exception.
1:21
The program sort of crashed
1:23
and if it was a real program that we were running
1:24
it wouldn't continue, it would just stop and say
1:26
Something's broken, we're done.
1:28
But in the REPL it just prints out the error
1:30
and carries on.
1:31
If we didn't want to combine numbers and strings
1:34
we can use a format string.
1:36
We know we have a format string
1:37
because it has that little f at the beginning.
1:38
So here we say, Hi, pearly name.
1:42
And that just takes the value, currently Michael.
1:44
Looks like you did math, and then we want to restate
1:47
the equation and then the value.
1:48
So x, {x + 7 * y}
1:52
is going to be whatever that value is.
1:54
Now, I could've assigned that response
1:57
or that evaluation to a variable.
2:00
We could've said something like
2:01
z = x + 7 * Y
2:04
and that's probably the best thing to do actually.
2:06
But I wanted to make the point
2:08
that it's not just variables
2:10
that can go into these format strings
2:12
inside those curly braces.
2:14
More general Python expressions can go in there.
2:16
So, if you wanted to compute something
2:18
or you wanted to call a function
2:20
get some behavior out of a piece of data
2:22
we'll see how to do that later.
2:23
You can do that there.
2:25
So, these are very very flexible.
2:26
And we run this code, we get Hey Michael
2:28
looks like you did math, 42 + 7 * 19 is 175.
2:34
Aright, so here we've created a bunch of variables
2:35
we've set their data and we've set their data values
2:39
which implies a data type.
2:41
We saw that we don't say the type in Python
2:43
but that it is important to know about it.
2:45
Like for example, name plus X, we should know
2:48
that we can't do those things.
2:49
We need to do the thing at the bottom.
2:51
Create that cool little string that we got in the end.