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