Data Types in Python
? Data Types in Python
In Python, data types define the type of a value or variable. Python has several built-in data types that allow you to store different kinds of values, such as numbers, text, sequences, and more. These data types are classified into mutable (changeable) and immutable (unchangeable) types.
? Built-in Data Types in Python
1?? Numeric Types
int(Integer): Represents whole numbers, both positive and negative, without decimals.x = 5y = -10float(Floating-point number): Represents numbers with a decimal point.a = 3.14b = -7.5complex(Complex number): Represents complex numbers in the forma + bj, whereais the real part andbis the imaginary part.c = 2 + 3j
2?? Sequence Types
list: An ordered, mutable collection of items. Lists can store multiple data types and can be modified (elements can be added, removed, or changed).fruits = ["apple", "banana", "cherry"]numbers = [1, 2, 3, 4, 5]tuple: An ordered, immutable collection of items. Once created, you cannot modify the elements of a tuple.colors = ("red", "green", "blue")range: Represents a sequence of numbers, typically used inforloops. It is often used for iterating over a sequence of numbers in a specific range.r = range(0, 10)
3?? Text Type
str(String): Represents a sequence of characters. Strings are immutable in Python.message = "Hello, world!"
4?? Mapping Type
dict(Dictionary): An unordered collection of key-value pairs. Keys are unique and immutable, while values can be any data type. Dictionaries are mutable.person = {"name": "John", "age": 30, "city": "New York"}
5?? Set Types
set: An unordered collection of unique items. Sets are mutable and do not allow duplicate values.fruits = {"apple", "banana", "cherry"}frozenset: An immutable version of a set. Once created, you cannot add or remove elements from a frozenset.frozen_fruits = frozenset({"apple", "banana", "cherry"})
6?? Boolean Type
bool: Represents two values,TrueandFalse, used for logical operations.is_valid = Trueis_ready = False
7?? Binary Types
bytes: Immutable sequence of bytes, often used for binary data.byte_data = b"hello"bytearray: Mutable sequence of bytes. Unlikebytes, the contents of abytearraycan be modified.byte_array = bytearray([65, 66, 67])memoryview: Provides memory access to binary data without copying it.mv = memoryview(b"hello")
? Type Conversion in Python
You can convert one data type to another using built-in functions like int(), float(), str(), etc.
Example: Type Conversion
# Convert a float to an integerx = 3.14int_x = int(x) # Result: 3# Convert an integer to a stringy = 10str_y = str(y) # Result: "10"# Convert a string to an integers = "20"int_s = int(s) # Result: 20? Checking the Data Type
You can check the data type of a variable using the type() function.
Example:
x = 5print(type(x)) # Output: <class 'int'>y = "hello"print(type(y)) # Output: <class 'str'>? Summary of Data Types
Numeric:
int,float,complexSequence:
list,tuple,rangeText:
strMapping:
dictSet:
set,frozensetBoolean:
boolBinary:
bytes,bytearray,memoryview
Let me know if you'd like more details on any specific data type or any other questions! ?