Python Data Type
Python Data Type
Python includes several built-in data types to store and manipulate different kinds of data. These can be broadly categorized as follows:
1. Numeric Types:
int (Integers): Whole numbers, positive or negative, without a decimal point.
Python
age = 30
float (Floating-point numbers): Numbers with a decimal point.
Python
price = 19.99
complex (Complex numbers): Numbers with a real and an imaginary part, represented as x + yj.
Python
z = 2 + 3j
2. Sequence Types:
str (Strings): Sequences of characters enclosed in single or double quotes.
Python
name = "Alice"
list (Lists): Ordered, mutable collections of items enclosed in square brackets. Lists can contain items of different data types.
Python
my_list = [1, "hello", 3.14]
tuple (Tuples): Ordered, immutable collections of items enclosed in parentheses. Tuples are similar to lists but cannot be modified after creation.
Python
my_tuple = (10, 20, 30)
range (Ranges): An immutable sequence of numbers, often used for looping a specific number of times.
Python
numbers = range(5) # Represents 0, 1, 2, 3, 4
3. Mapping Type:
dict (Dictionaries): Unordered, mutable collections of key-value pairs enclosed in curly braces. Keys must be unique and immutable.
Python
person = {"name": "Bob", "age": 25}
4. Set Types:
set (Sets): Unordered, mutable collections of unique items enclosed in curly braces. Duplicate elements are automatically removed.
Python
unique_numbers = {1, 2, 2, 3} # Result will be {1, 2, 3}
frozenset (Frozensets): Immutable versions of sets.
5. Boolean Type:
bool (Booleans): Represents truth values, either True or False.
Python
is_active = True
6. Binary Types:
bytes: Immutable sequences of bytes.
bytearray: Mutable sequences of bytes.
memoryview: A memory view of other binary data types.
6. 7. None Data Types:
None is used to define a null value or Null object in Python. It is not the same as an empty string, a False, or a zero. It is a data type of the class NoneType object
Python
print(type(None))
Output
<class 'NoneType'>

No comments: