PYTHON DATA TYPES😄
Types of data types
1️⃣ Numeric data types
int
- An integer, is a whole number, positive or negative, without decimals, of unlimited length.
numbers = 20
print(numbers)
print(type(numbers))
float
- A float is a number, positive or negative, containing one or more decimals.
my_float = 20.5
print(float)
print(type(float))
complex
- A complex is a number made up of some parts that are unknown
my_complex = 1j
print(my_complex)
print(type(my_complex))
2️⃣ Sequence Types
lists
- Lists are used to store multiple items in a single variable.
- Take note of the square brackets
- Unlike a set, a list can allow duplicate values
- They can contain different data types
fruits = ["apple", "banana", "cherry"]
print(fruits)
print(type(fruits))
tuple
- Tuples are used to store multiple items in a single variable.
- Take note of the round brackets
- Unlike lists, tuples are unchangeable.
- Allows duplicate members.
cars = ("toyota", "porsche", "mercedes")
print(cars)
print(type(cars))
range
- A range is a sequence of numbers starting from 0 by default and increments by one
my_range = range(6)
print(my_range)
print(type(my_range))
3️⃣ Mapping types
dict
- Dictionaries are used to store data values in key:value pairs.
- Dictionaries are ordered and changeable
- Like sets they do not allow duplication of items
my_dict = {"name" : "John", "age" : 36}
print(my_dict)
print(type(my_dict))
4️⃣ Set types
set
- Sets are used to store multiple items in a single variable.
- Sets are unindexed, unordered and unchangeable however you can add or remove items from a set
- Sets also do not allow duplicates
- Take note of the curly braces
my_set = {"gold", "silver", "titanium"}
print(my_set)
print(type(my_set))
frozen set
- Similar to a set but completely immutable; you cannot add or remove items
my_frozenset = frozenset({"apple", "banana", "cherry"})
print(my_frozenset)
print(type(my_frozenset))
5️⃣ Boolean types
boolean
- A boolean is a value that can be true or false
my_bool = True
print(my_bool)
print(type(my_bool))
6️⃣ Binary types
bytes
- Bytes represent an immutable sequence of 8-bit values
z = b"Hello"
print(z)
print(type(z))
bytesarray
- Bytesarray represents a mutable sequence of 8-bit values.
my_bytearray = bytearray(5)
print(my_bytearray)
print(type(my_bytearray))
memoryview
- A memoryview object provides a view into the memory of a bytes-like object (like bytes or bytearray) without copying the underlying data.
my_memoryviews = memoryview(bytes(5))
print(my_memoryviews)
print(type(my_memoryviews))
7️⃣ None types
NoneTypes
- This is simply a data type that represents an absence of value
z = None
print(z)
print(type(z))
8️⃣ Text types
string
- A string is a collection of characters
greetings = "Hello World"
print(greetings)
print(type(greetings))
Top comments (0)