From the course: Introduction to Python: Learn How to Program Today with Python by Pearson

Unlock this course with a free trial

Join today to access over 25,400 courses taught by industry experts.

Add type-hints

Add type-hints

So, as we talked about in lesson two, Python is a dynamically typed language, so you don't have to specify the type of anything when you are creating variables or getting inputs to functions or anything like that. Some other languages are statically typed, so you do have to define what things are. a new feature that was added in Python, I think it came in around 3.5 as something built in, is this ability to add hints to specify what a type should be. Let's look at adding those now to some code. You can go to examples, example 18, type hints, and I'm going to work through it here. So if we've got a function, this is the most common usage of type hints, is to say what a a function takes in and what it outputs. If we've got a function let's just use the simple add one so n1 and n2 returns n1 plus n2. Okay if I were to use that and say total is equal to add 1 comma 2. That's cool. If I were to turn these into strings, or one's a string and one is a number, this would give me a type error.…

Contents