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 - Python Tutorial
From the course: Introduction to Python: Learn How to Program Today with Python by Pearson
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
-
-
-
-
-
-
-
(Locked)
Learning objectives1m 30s
-
(Locked)
More context: Discuss how to keep learning15m 14s
-
(Locked)
Look at more data structures14m 51s
-
(Locked)
Read files11m 53s
-
(Locked)
Write more complex functions15m 51s
-
(Locked)
Program with classes19m 33s
-
(Locked)
Import external packages8m 12s
-
(Locked)
Add type-hints9m 15s
-
(Locked)
-
-
-