From bd3ae458148cb39d0ea266710bbf32df01cb719a Mon Sep 17 00:00:00 2001 From: Jessica Wilkins <67210629+jdwilkin4@users.noreply.github.com> Date: Thu, 21 May 2026 00:34:41 -0700 Subject: [PATCH] feat(curriculum): add new lesson for type and isinstance functions (#67346) Signed-off-by: Jessica Wilkins <67210629+jdwilkin4@users.noreply.github.com> Signed-off-by: Sem Bauke Co-authored-by: majestic-owl448 <26656284+majestic-owl448@users.noreply.github.com> Co-authored-by: Dario <105294544+Dario-DC@users.noreply.github.com> Co-authored-by: Sem Bauke --- .../67fe859a00971c34a23abd43.md | 81 ++---- .../6a03f5d143c8688a9265c0a0.md | 233 ++++++++++++++++++ ...nderstanding-variables-and-data-types.json | 6 +- 3 files changed, 255 insertions(+), 65 deletions(-) create mode 100644 curriculum/challenges/english/blocks/lecture-understanding-variables-and-data-types/6a03f5d143c8688a9265c0a0.md diff --git a/curriculum/challenges/english/blocks/lecture-understanding-variables-and-data-types/67fe859a00971c34a23abd43.md b/curriculum/challenges/english/blocks/lecture-understanding-variables-and-data-types/67fe859a00971c34a23abd43.md index a0cb9f15431..e8868f74f29 100644 --- a/curriculum/challenges/english/blocks/lecture-understanding-variables-and-data-types/67fe859a00971c34a23abd43.md +++ b/curriculum/challenges/english/blocks/lecture-understanding-variables-and-data-types/67fe859a00971c34a23abd43.md @@ -1,8 +1,8 @@ --- id: 67fe859a00971c34a23abd43 -title: What Are Common Data Types in Python and How Do You Get the Type of a Variable? +title: What Are Common Data Types in Python? challengeType: 19 -dashedName: what-are-common-data-types-in-python-and-how-do-you-get-the-type-of-a-variable +dashedName: what-are-common-data-types-in-python --- # --description-- @@ -111,58 +111,7 @@ my_none_var = None print('None:', my_none_var) # None: None ``` -To get the data type of a variable, you can use the `type()` function: - -```python -my_var_1 = 'Hello world' -my_var_2 = 21 - -print(type(my_var_1)) # -print(type (my_var_2)) # -``` - -And here's are all the data types covered in this lesson, along with their types in the terminal: - -```python -my_integer_var = 10 -print(type(my_integer_var)) # - -my_float_var = 4.50 -print(type(my_float_var)) # - -my_string_var = 'hello' -print(type(my_string_var)) # - -my_boolean_var = True -print(type(my_boolean_var)) # - -my_set_var = {7, 'hello', 8.5} -print(type(my_set_var)) # - -my_dictionary_var = {'name': 'Alice', 'age': 25} -print(type(my_dictionary_var)) # - -my_tuple_var = (7, 'hello', 8.5) -print(type(my_tuple_var)) # - -my_range_var = range(5) -print(type(my_range_var)) # - -my_list = [22, 'Hello world', 3.14, True] -print(type(my_list)) # - -my_none_var = None -print(type(my_none_var)) # -``` - -The built-in `isinstance()` function lets you check if a variable matches a specific data type. It takes in an object and the type you want to check it against, then returns a boolean. Here are some examples: - -```python -isinstance('Hello world', str) # True -isinstance(True, bool) # True -isinstance(42, int) # True -isinstance('John Doe', int) # False -``` +In future lessons, you will learn more about how to work with all of these data types. # --questions-- @@ -241,31 +190,35 @@ One has decimals, the other doesn't. ## --text-- -How can you check the data type of a variable in Python? +Which of the following is a `True` or `False` type in Python? ## --answers-- -By using the `type()` function, like `type(my_var)`. +Boolean --- -By checking the variable's value manually. +Integer ### --feedback-- -There's a built-in function for this. +Review the middle of the lesson for the correct answer. --- -By using the `typeof` function, like `typeof(my_var)`. - ---- - -By converting the variable to a string and analyzing its characters. +Float ### --feedback-- -There's a built-in function for this. +Review the middle of the lesson for the correct answer. + +--- + +List + +### --feedback-- + +Review the middle of the lesson for the correct answer. ## --video-solution-- diff --git a/curriculum/challenges/english/blocks/lecture-understanding-variables-and-data-types/6a03f5d143c8688a9265c0a0.md b/curriculum/challenges/english/blocks/lecture-understanding-variables-and-data-types/6a03f5d143c8688a9265c0a0.md new file mode 100644 index 00000000000..1acbf036aa2 --- /dev/null +++ b/curriculum/challenges/english/blocks/lecture-understanding-variables-and-data-types/6a03f5d143c8688a9265c0a0.md @@ -0,0 +1,233 @@ +--- +id: 6a03f5d143c8688a9265c0a0 +title: How Do the type() and isinstance() Functions Work? +challengeType: 19 +dashedName: how-do-the-type-and-isinstance-functions-work +--- + +# --description-- + +In the prior lesson, you learned about common data types in Python including the string, integer, and float types. As you build out your programs, you will need to learn how to view the type for a variable. + + +Here is an example variable: + +```py +developer = 'Devin' +``` + +To see what type `developer` is, you can use the `type()` function like this: + +```py +developer = 'Devin' + +print(type(developer)) # +``` + +The output of `` means that `developer` is a string type. + +If you fail to provide any arguments to the `type()` function, then you will receive the following error message: + +```md +Traceback (most recent call last): + File "", line 1, in +TypeError: type() takes 1 or 3 arguments +``` + +Here are all of the data types you have learned so far along with their types in the terminal: + +```python +my_integer_var = 10 +print(type(my_integer_var)) # + +my_float_var = 4.50 +print(type(my_float_var)) # + +my_string_var = 'hello' +print(type(my_string_var)) # + +my_boolean_var = True +print(type(my_boolean_var)) # + +my_set_var = {7, 'hello', 8.5} +print(type(my_set_var)) # + +my_dictionary_var = {'name': 'Alice', 'age': 25} +print(type(my_dictionary_var)) # + +my_tuple_var = (7, 'hello', 8.5) +print(type(my_tuple_var)) # + +my_range_var = range(5) +print(type(my_range_var)) # + +my_list = [22, 'Hello world', 3.14, True] +print(type(my_list)) # + +my_none_var = None +print(type(my_none_var)) # +``` + +There will be times in your program where you will need to verify that a particular variable is a specific type before performing operations on it. This is where the `isinstance()` function comes in handy. + +Here is an example variable with a string assigned to it: + +```py +account_balance = '12' +``` + +If you try to do mathematical expressions like division using the `account_balance` variable, then you will receive an error message. + +```py +account_balance = '12' + +account_balance / 2 + +# Traceback (most recent call last): +# File "", line 1, in +# TypeError: unsupported operand type(s) for /: 'str' and 'int' +``` + +To see if `account_balance` is an integer, you can check using the `isinstance()` function like this: + +```py +account_balance = '12' + +isinstance(account_balance, int) # False +``` + +The built-in `isinstance()` function lets you check if a variable matches a specific data type. It takes in an object and the type you want to check it against, then returns a boolean. In this case, since `account_balance` is a string, it will return `False`. + +The `isinstance()` function also allows you to check for multiple types at once. + +Here is an example checking if `account_balance` is an `int` or `float`: + +```py +account_balance = 12 +isinstance(account_balance, (int, float)) # True +``` + +In this example, `account_balance` is an integer so `isinstance()` returns `True`. If `account_balance` were `12.0`, `isinstance()` would still return `True` because you are checking for integers or floats. + +In future workshops and labs, you will use the `type()` and `isinstance()` functions to ensure your variables contain the correct data types before performing operations on them. + +# --questions-- + +## --text-- + +What will be the output for the following code? + +```py +developer = 'Devin' + +print(type(developer)) +``` + +## --answers-- + +`` + +### --feedback-- + +Review the beginning of the lesson for the correct answer. + +--- + +`` + +--- + +`` + +### --feedback-- + +Review the beginning of the lesson for the correct answer. + +--- + +`` + +### --feedback-- + +Review the beginning of the lesson for the correct answer. + +## --video-solution-- + +2 + +## --text-- + +Which of the following functions is used to check if a variable is a specific type? + +## --answers-- + +`isinstance()` + +--- + +`istype()` + +### --feedback-- + +Review the end of the lesson for the correct answer. + +--- + +`isclass()` + +### --feedback-- + +Review the end of the lesson for the correct answer. + +--- + +`isinstantiate()` + +### --feedback-- + +Review the end of the lesson for the correct answer. + +## --video-solution-- + +1 + +## --text-- + +What will be returned in this use of the `isinstance()` function? + +```py +account_balance = 12 +isinstance(account_balance, (int, float)) +``` + +## --answers-- + +`False` + +### --feedback-- + +Review the end of the lesson for the correct answer. + +--- + +`True` + +--- + +`Int` + +### --feedback-- + +Review the end of the lesson for the correct answer. + +--- + +`None` + +### --feedback-- + +Review the end of the lesson for the correct answer. + +## --video-solution-- + +2 diff --git a/curriculum/structure/blocks/lecture-understanding-variables-and-data-types.json b/curriculum/structure/blocks/lecture-understanding-variables-and-data-types.json index 475830511c7..0a909aacad6 100644 --- a/curriculum/structure/blocks/lecture-understanding-variables-and-data-types.json +++ b/curriculum/structure/blocks/lecture-understanding-variables-and-data-types.json @@ -14,7 +14,11 @@ }, { "id": "67fe859a00971c34a23abd43", - "title": "What Are Common Data Types in Python and How Do You Get the Type of a Variable?" + "title": "What Are Common Data Types in Python?" + }, + { + "id": "6a03f5d143c8688a9265c0a0", + "title": "How Do the type() and isinstance() Functions Work?" } ], "blockLabel": "lecture"