mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
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 <sem@freecodecamp.org> 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 <sem@freecodecamp.org>
This commit is contained in:
+17
-64
@@ -1,8 +1,8 @@
|
|||||||
---
|
---
|
||||||
id: 67fe859a00971c34a23abd43
|
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
|
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--
|
# --description--
|
||||||
@@ -111,58 +111,7 @@ my_none_var = None
|
|||||||
print('None:', my_none_var) # None: None
|
print('None:', my_none_var) # None: None
|
||||||
```
|
```
|
||||||
|
|
||||||
To get the data type of a variable, you can use the `type()` function:
|
In future lessons, you will learn more about how to work with all of these data types.
|
||||||
|
|
||||||
```python
|
|
||||||
my_var_1 = 'Hello world'
|
|
||||||
my_var_2 = 21
|
|
||||||
|
|
||||||
print(type(my_var_1)) # <class 'str'>
|
|
||||||
print(type (my_var_2)) # <class 'int'>
|
|
||||||
```
|
|
||||||
|
|
||||||
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)) # <class 'int'>
|
|
||||||
|
|
||||||
my_float_var = 4.50
|
|
||||||
print(type(my_float_var)) # <class 'float'>
|
|
||||||
|
|
||||||
my_string_var = 'hello'
|
|
||||||
print(type(my_string_var)) # <class 'str'>
|
|
||||||
|
|
||||||
my_boolean_var = True
|
|
||||||
print(type(my_boolean_var)) # <class 'bool'>
|
|
||||||
|
|
||||||
my_set_var = {7, 'hello', 8.5}
|
|
||||||
print(type(my_set_var)) # <class 'set'>
|
|
||||||
|
|
||||||
my_dictionary_var = {'name': 'Alice', 'age': 25}
|
|
||||||
print(type(my_dictionary_var)) # <class 'dict'>
|
|
||||||
|
|
||||||
my_tuple_var = (7, 'hello', 8.5)
|
|
||||||
print(type(my_tuple_var)) # <class 'tuple'>
|
|
||||||
|
|
||||||
my_range_var = range(5)
|
|
||||||
print(type(my_range_var)) # <class 'range'>
|
|
||||||
|
|
||||||
my_list = [22, 'Hello world', 3.14, True]
|
|
||||||
print(type(my_list)) # <class 'list'>
|
|
||||||
|
|
||||||
my_none_var = None
|
|
||||||
print(type(my_none_var)) # <class 'NoneType'>
|
|
||||||
```
|
|
||||||
|
|
||||||
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
|
|
||||||
```
|
|
||||||
|
|
||||||
# --questions--
|
# --questions--
|
||||||
|
|
||||||
@@ -241,31 +190,35 @@ One has decimals, the other doesn't.
|
|||||||
|
|
||||||
## --text--
|
## --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--
|
## --answers--
|
||||||
|
|
||||||
By using the `type()` function, like `type(my_var)`.
|
Boolean
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
By checking the variable's value manually.
|
Integer
|
||||||
|
|
||||||
### --feedback--
|
### --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)`.
|
Float
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
By converting the variable to a string and analyzing its characters.
|
|
||||||
|
|
||||||
### --feedback--
|
### --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--
|
## --video-solution--
|
||||||
|
|
||||||
|
|||||||
+233
@@ -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)) # <class 'str'>
|
||||||
|
```
|
||||||
|
|
||||||
|
The output of `<class 'str'>` 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 "<stdin>", line 1, in <module>
|
||||||
|
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)) # <class 'int'>
|
||||||
|
|
||||||
|
my_float_var = 4.50
|
||||||
|
print(type(my_float_var)) # <class 'float'>
|
||||||
|
|
||||||
|
my_string_var = 'hello'
|
||||||
|
print(type(my_string_var)) # <class 'str'>
|
||||||
|
|
||||||
|
my_boolean_var = True
|
||||||
|
print(type(my_boolean_var)) # <class 'bool'>
|
||||||
|
|
||||||
|
my_set_var = {7, 'hello', 8.5}
|
||||||
|
print(type(my_set_var)) # <class 'set'>
|
||||||
|
|
||||||
|
my_dictionary_var = {'name': 'Alice', 'age': 25}
|
||||||
|
print(type(my_dictionary_var)) # <class 'dict'>
|
||||||
|
|
||||||
|
my_tuple_var = (7, 'hello', 8.5)
|
||||||
|
print(type(my_tuple_var)) # <class 'tuple'>
|
||||||
|
|
||||||
|
my_range_var = range(5)
|
||||||
|
print(type(my_range_var)) # <class 'range'>
|
||||||
|
|
||||||
|
my_list = [22, 'Hello world', 3.14, True]
|
||||||
|
print(type(my_list)) # <class 'list'>
|
||||||
|
|
||||||
|
my_none_var = None
|
||||||
|
print(type(my_none_var)) # <class 'NoneType'>
|
||||||
|
```
|
||||||
|
|
||||||
|
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 "<stdin>", line 1, in <module>
|
||||||
|
# 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--
|
||||||
|
|
||||||
|
`<class 'list'>`
|
||||||
|
|
||||||
|
### --feedback--
|
||||||
|
|
||||||
|
Review the beginning of the lesson for the correct answer.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
`<class 'str'>`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
`<class 'tuple'>`
|
||||||
|
|
||||||
|
### --feedback--
|
||||||
|
|
||||||
|
Review the beginning of the lesson for the correct answer.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
`<class 'range'>`
|
||||||
|
|
||||||
|
### --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
|
||||||
@@ -14,7 +14,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "67fe859a00971c34a23abd43",
|
"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"
|
"blockLabel": "lecture"
|
||||||
|
|||||||
Reference in New Issue
Block a user