Rock Solid Python with Python Typing Transcripts
Chapter: Typing in Python
Lecture: Typing and Variables

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, we're on to another chapter. So now we got a new section in our project in our code here, we're on this 03 typing
0:09 in Python, you can see that we've got our virtual environment selected, although until next chapter, it won't really matter. But that is set up.
0:18 And we're going to explore this in six big sections. And the first section we're going to talk about is variables. I don't know file here.
0:27 You want variables. And the first thing we're going to talk about in this section is how to work with creating
0:35 and typing a variable and what the implications of that are most importantly. Now let's go ahead and set this to run straight away.
0:42 Nothing's going to happen yet, but just so when we press run, it's running the right file.
0:47 Now we're going to see that there's two ways you can create variables in Python and variables in Python are no nonsense sort of thing.
0:54 So we can say x equals seven. Done. We've created a variable. does have a type integer right now, but it could be anything.
1:00 We haven't restricted it in any way. And that's about as simple as it can get in a programming language, isn't it?
1:07 So we could come down here and we could later on decide that this is gonna be something like happy numbers, right?
1:16 Talks about numbers, but it sure isn't a number. Now you might see a warning on the screen here. It says this is redeclared before it's used.
1:23 This is just the linter in PyCharm trying to be helpful saying you basically created a variable twice with doing nothing.
1:30 So we're just gonna print it out just to make that error go away. This is not a typing error. This is just like, hey, you didn't use a variable.
1:37 Print it out one more time. We've got X is seven. Now it's happy numbers. Excellent. We could go and explicitly define a variable though,
1:47 as we have a little bit in that quick fly through on the typing, we could say we want a variable and its colon type is an integer
1:54 and that is equal to 10. So just like our X, we have a variable Y, it has a integer value, but unlike the X down here,
2:04 if we try to say this is sad numbers, PyCharm is going to say, whoa, whoa, whoa, there is a problem here. Look at this, we wanted an integer
2:14 because that's the only thing you should be able to assign to Y, and yet here we have a string
2:19 and definitely, definitely strings and integers don't mix. So if I just tried to say, do not mix with numbers, plus seven, and I try to run that
2:31 you can see crash cannot concatenate string and integers. It's true, they don't mix. So PyCharm is right to give us this warning.
2:40 There's this other warning about it not being used, but that's the same as we saw before. So let's just do that real quick.
2:45 But here's the interesting thing. Remember, Python types are optional and gradual.
2:49 So if we run this, you can see happy numbers, sad numbers, even though there's this warning.
2:55 Sure enough, it still runs just fine, because the typing does not affect the runtime behavior yet, at least in this context, it doesn't.
3:03 It just helps you write better code, it helps you understand this code better.
3:07 So in this case, it says, Look, you're trying to use a string where an integer was expected. Don't do that. It's not going to work out.
3:14 Well, it's not technically a syntax or a runtime error, but it probably will lead to one if you keep assuming it's a string when it's not.


Talk Python's Mastodon Michael Kennedy's Mastodon