Python for Absolute Beginners Transcripts
Chapter: Writing your first lines of code
Lecture: Demo: Happy birthday to me
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's write a little bit of code play with some variables and even do a little calculation here in our Python REPL.
0:07
Remember we get started by typing Python3 on macOS and Linux, sometimes on Windows we'll figure that out when we get there.
0:15
We can get started by declaring a variable like we can say x = 7, y = 11. And then you can do just standard math.
0:26
So the math operations, they're very much like, well what you will done in math class so x + y, we get 18, there's a prize there, right?
0:35
And if we want to work with that value of it being added up we need to have another variable like let's say z = x + y.
0:42
And then we can ask, what is the value of whoops that wasn't it, x + y. And then we can say, what is the value z? Again, it's 18.
0:50
But then you could do things like add one more to z. Now what is z? z is 19. But then there is this little shortcut we can do a += to say
0:59
take the value and increment by one. It's like z = z + 1, you'll see that all the time. So this is interesting.
1:06
We can do basic things with these variables here we have x, y, we have now z, we can add them together we can even say z / y
1:16
and get some sort of floating point number that comes out of it like that. Let's do something a little bit simpler and more fun maybe.
1:23
What we're going to do is we're going to clear the screen and on macOS, you hit command K, and clear that.
1:28
The other operating systems don't have that feature wish they did. So what we're going to do is we're going to a creative name.
1:34
And I'll say my name is Michael. Now we want this to be text. And in programming language, in programming terms
1:43
text always has to go into these quotes. We have double quotes, you can use single quotes or double quotes, it doesn't make any difference in Python.
1:50
Some languages it does, Python, it doesn't matter. You can just use whichever you like. Often I actually use single quotes
1:56
but sometimes double as well. So we're going to create this variable and ask what it is. Notice Python prefer single quotes when it refers back to us.
2:05
It says, here's a piece of text. Now this type of data that we're working with when we have text is called a string. That's different than say numbers.
2:15
So if we had like name, or we tried to add a number to it it'll say, oh, no, no, no you cannot put strings and numbers together.
2:23
It's really important that you have a basic sense of the type of data you're working with because certain operations are allowed to be done on data
2:31
like again, with x / y, or z / y you kind of can divide name by a number. We have a variable called name.
2:39
And we have a piece of data, Michael, which is text and that's a string or str in programming syntax. Now, let's do something interesting with it.
2:49
Let's say my age, I'm going to be 42, right? Let's put these pieces together. We already saw that if we try to say name + age
2:58
it gives us an error, that's okay. We're going to make string, a piece of text out of those two pieces of data, those two variables
3:07
though what we can do is we can create what's called an f-string. So I'll say that there's going to be a message
3:12
and normally an f-string is like this or like this. But if you want to put other data into it
3:19
like name and age, input just a little f at the beginning. These are format strings strings that take data as part of what they do.
3:25
So we could say, Hi there, and we want to put the name we just put the name inside these curly braces.
3:31
So we say name, you are, age years old. Like that. Though if we say what is the message? Hi there, Michael, you are 42 years old.
3:42
Cool, right? So what we have is we have our string data which we stored into name, we have our integer data. That's what type? This is right?
3:50
Notice this int here, integer age 42. And then we created a more interesting string using format
3:56
a format, we said Hi there, whatever the value of name is you are, whatever the value of age is, years old. So that's cool.
4:04
Let's go ahead and suppose I had a birthday, my age I could say equals age plus one, or again we could do that shortcut plus equals one.
4:14
And now, if we go and do our format again we just hit up arrow to go back and run it. But if we recreate this message, we can ask what it is now.
4:22
Hi there Michael, you are 43 years old. Cool, okay. So we can actually ask what type of data we're working with.
4:30
It's very rare in Python that you would do this. But as we're kind of exploring this idea you can use this operator called type
4:38
so I could say what type is message what type is name, what type is age. So you can see that it's a string, string and an integer.
4:47
It's not important that you know the exact types or often that you even say them some programming languages
4:52
I'd have to declare this variable as a string before I get assigned string values to it. I'd have to declare the age as an integer
5:00
before I could assign integer values to it. Python is way more loose than that it just lets you write values and work with them.
5:07
But as you can see, as we try to do these operations it does care about them. And so having a little bit of a sense
5:12
that there are these different types of data that store different types of information numbers as integers and string as text.
5:19
There's also different types of numbers like floating point numbers like 1.2. Integers just represent home numbers and so on.
5:26
But having this little sense of a data type just is there in the background that you're working with tells you what you can do with the data
5:34
the way you can combine it and so on.