Python has a number of built-in data types that are used to represent different kinds of values. Here's a breakdown of the basic data types, along with their typical range of values:
Numeric Types:
-
int: Integers (whole numbers)
- Range: Virtually unlimited, limited by available memory.
- Example:
10
,-5
,0
-
float: Floating-point numbers (numbers with decimal points)
- Range: -3.4028235e+38 to 3.4028235e+38 (approximately)
- Example:
3.14
,-2.5
,0.0
-
complex: Complex numbers (numbers with real and imaginary parts)
- Range: Real and imaginary parts are floats.
- Example:
2 + 3j
,-1.5 - 2j
Text Type:
- str: Strings (sequences of characters)
- Range: Can hold any Unicode character.
- Example:
"Hello"
,'Python'
,"123"
Boolean Type:
- bool: Boolean values (represent truth or falsehood)
- Values:
True
orFalse
- Values:
Sequence Types:
-
list: Ordered, mutable (changeable) collections of items
- Range: Can hold any number of items of any data type.
- Example:
[1, 2, 3]
,["apple", "banana"]
,[1, "hello", 3.14]
-
tuple: Ordered, immutable (unchangeable) collections of items
- Range: Can hold any number of items of any data type.
- Example:
(1, 2, 3)
,("apple", "banana")
,(1, "hello", 3.14)
-
range: Represents a sequence of numbers
- Range: Determined by the arguments passed to the
range()
function. - Example:
range(1, 5)
(represents numbers 1, 2, 3, 4)
- Range: Determined by the arguments passed to the
Mapping Type:
- dict: Dictionaries (store key-value pairs)
- Range: Can hold any number of key-value pairs. Keys must be immutable types (like strings, numbers, or tuples).
- Example:
{"name": "John", "age": 30}
,{1: "one", 2: "two"}
Set Types:
-
set: Unordered collections of unique items
- Range: Can hold any number of items of any hashable data type.
- Example:
{1, 2, 3}
,{"apple", "banana"}
-
frozenset: Immutable version of a set
Binary Types:
-
bytes: Sequences of bytes (integers from 0 to 255)
- Example:
b"hello"
- Example:
-
bytearray: Mutable version of bytes
-
memoryview: Allows access to the internal data of an object without copying
Important Notes:
- Python is dynamically typed, meaning you don't need to explicitly declare the data type of a variable. Python infers the type based on the value assigned to it.
- You can use the
type()
function to determine the data type of a variable. - Python provides functions for converting between different data types (e.g.,
int()
,float()
,str()
).
0 Comments
Post a Comment