Python Data Types Flashcards
__________ can store data of different types, and different types can do different things.
Variables
What is a Text Data Type?
String (str)
What are the three Numeric Data Types?
int, float, complex
What are the three Sequence Data Types?
list, tuple, range
What is the Mapping Data Type?
dict
What are the two Set Data Types?
set, frozenset
What is a Boolean Data Type?
bool
What are the three Binary Data Types?
bytes, bytearray, memoryview
What is a None Type Data Type?
NoneType
What are the 8 Python built-in data types?
Text Type
Numeric Types
Sequence Types
Mapping Type
Set Types
Boolean Type
Binary Types
None Type
You can get the data type of any object by using the ____________ function:
type()
Example:
Print the data type of the variable x:
x = 5
print(type(x))
Get the Output and the Data Type:
x = “Hello World”
display x:
Code:
x = “Hello World”
print(x)
print(type(x))
Output:
Hello World
<class ‘str’>
Get the Output and the Data Type:
x = 20
display x:
Code:
x = 20
print(x)
print(type(x))
Output:
20
<class ‘int’>
Get the Output and the Data Type:
x = 20.5
display x:
Code:
x = 20.5
print(x)
print(type(x))
Output:
20.5
<class ‘float’>
Get the Output and the Data Type:
x = 1j
display x:
Code:
x = 1j
print(x)
print(type(x))
Output:
lj
<class ‘complex’>
Get the Output and the Data Type:
x = [“apple”, “banana”, “cherry”]
display x:
Code:
x = [“apple”, “banana”, “cherry”]
print(x)
print(type(x))
Output:
[‘apple’, ‘banana’, ‘cherry’]
<class ‘list’>
Get the Output and the Data Type:
x = (“apple”, “banana”, “cherry”)
display x:
Code:
x = (“apple”, “banana”, “cherry”)
print(x)
print(type(x))
Output:
(‘apple’, ‘banana’, ‘cherry’)
<class ‘tuple’>
Get the Output and the Data Type:
x = range(6)
display x:
Code:
x = range(6)
print(x)
print(type(x))
Output:
range(0, 6)
<class ‘range’>
Get the Output and the Data Type:
x = {“name” : “John”, “age” : 36}
display x:
Code:
x = {“name” : “John”, “age” : 36}
print(x)
print(type(x))
Output:
{‘name’: ‘John’, ‘age’: 36}
<class ‘dict’>
Get the Output and the Data Type:
x = {“apple”, “banana”, “cherry”}
display x:
Code:
x = {“apple”, “banana”, “cherry”}
print(x)
print(type(x))
Output:
{‘apple’, ‘cherry’, ‘banana’}
<class ‘set’>
Get the Output and the Data Type:
x = frozenset({“apple”, “banana”, “cherry”})
display x:
Code:
x = frozenset({“apple”, “banana”, “cherry”})
print(x)
print(type(x))
Output:
frozenset({‘apple’, ‘banana’, ‘cherry’})
<class ‘frozenset’>
Get the Output and the Data Type:
x = True
display x:
Code:
x = True
print(x)
print(type(x))
Output:
True
<class ‘bool’>
Get the Output and the Data Type:
x = b”Hello”
display x:
Code:
x = b”Hello”
print(x)
print(type(x))
Output:
b’Hello’
<class ‘bytes’>
Get the Output and the Data Type:
x = bytearray(5)
display x:
Code:
x = bytearray(5)
print(x)
print(type(x))
Output:
bytearray(b’\x00\x00\x00\x00\x00’)
<class ‘bytearray’>
Get the Output and the Data Type:
x = memoryview(bytes(5))
display x:
Code:
x = memoryview(bytes(5))
print(x)
print(type(x))
Output:
<memory at 0x0368AFA0>
<class ‘memoryview’>
Get the Output and the Data Type:
x = None
display x:
Code:
x = None
print(x)
print(type(x))
Output:
None
<class ‘NoneType’>
If you want to specify the data type, you can use ______________ functions.
constructor
If you want to specify the data type, you can use constructor functions. Give the 14 examples and then after it give the sample code on how to generate the output.
display x:
x = str(“Hello World”)
x = int(20)
x = float(20.5)
x = complex(1j)
x = list((“apple”, “banana”, “cherry”))
x = tuple((“apple”, “banana”, “cherry”))
x = range(6)
x = dict(name=”John”, age=36)
x = set((“apple”, “banana”, “cherry”))
x = frozenset((“apple”, “banana”, “cherry”))
x = bool(5)
x = bytes(5)
x = bytearray(5)
x = memoryview(bytes(5))
Sample Code:
x = str(“Hello World”)
print(x)
print(type(x))
Output:
Hello World
<class ‘str’>