|
| 1 | +--- |
| 2 | +id: datatype-python |
| 3 | +title: Python Data Types |
| 4 | +sidebar_label: Python Data Types #displays in sidebar |
| 5 | +sidebar_position: 4 |
| 6 | +tags: |
| 7 | + [ |
| 8 | + python, |
| 9 | + introduction of python, |
| 10 | + Data Type, |
| 11 | + |
| 12 | + ] |
| 13 | +description: Learn all standard data types in Python with examples and explanations. |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | + |
| 18 | +# Python Data Types |
| 19 | + |
| 20 | +In Python, every value has a **data type**. Data types define the nature of a value, and Python provides a wide variety of built-in data types to handle different kinds of data. Understanding these is crucial for effective programming. |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## Data Types in Python |
| 25 | + |
| 26 | +| **Category** | **Data Type** | |
| 27 | +|------------------|----------------------------------------| |
| 28 | +| Text Type | `str` | |
| 29 | +| Numeric Types | `int`, `float`, `complex` | |
| 30 | +| Sequence Types | `list`, `tuple`, `range` | |
| 31 | +| Mapping Type | `dict` | |
| 32 | +| Set Types | `set`, `frozenset` | |
| 33 | +| Boolean Type | `bool` | |
| 34 | +| Binary Types | `bytes`, `bytearray`, `memoryview` | |
| 35 | +| None Type | `NoneType` | |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## Text Type: `str` |
| 40 | + |
| 41 | +A sequence of Unicode characters. |
| 42 | + |
| 43 | +```python |
| 44 | +name = "Dhruba" |
| 45 | +```` |
| 46 | + |
| 47 | +You can perform operations like: |
| 48 | + |
| 49 | +* Slicing |
| 50 | +* Concatenation |
| 51 | +* Length check with `len()` |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## Numeric Types |
| 56 | + |
| 57 | +### `int` |
| 58 | + |
| 59 | +Whole numbers: |
| 60 | + |
| 61 | +```python |
| 62 | +age = 25 |
| 63 | +``` |
| 64 | + |
| 65 | +### `float` |
| 66 | + |
| 67 | +Decimal numbers: |
| 68 | + |
| 69 | +```python |
| 70 | +pi = 3.14 |
| 71 | +``` |
| 72 | + |
| 73 | +### `complex` |
| 74 | + |
| 75 | +Numbers with real and imaginary parts: |
| 76 | + |
| 77 | +```python |
| 78 | +z = 2 + 3j |
| 79 | +``` |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## Sequence Types |
| 84 | + |
| 85 | +### `list` |
| 86 | + |
| 87 | +Mutable, ordered sequence: |
| 88 | + |
| 89 | +```python |
| 90 | +fruits = ["apple", "banana", "cherry"] |
| 91 | +``` |
| 92 | + |
| 93 | +### `tuple` |
| 94 | + |
| 95 | +Immutable, ordered sequence: |
| 96 | + |
| 97 | +```python |
| 98 | +dimensions = (1024, 768) |
| 99 | +``` |
| 100 | + |
| 101 | +### `range` |
| 102 | + |
| 103 | +Represents a sequence of numbers: |
| 104 | + |
| 105 | +```python |
| 106 | +nums = range(5) |
| 107 | +``` |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +## Mapping Type: `dict` |
| 112 | + |
| 113 | +Unordered collection of key-value pairs: |
| 114 | + |
| 115 | +```python |
| 116 | +person = { |
| 117 | + "name": "Alice", |
| 118 | + "age": 30 |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +## Set Types |
| 125 | + |
| 126 | +### `set` |
| 127 | + |
| 128 | +Unordered, mutable, no duplicates: |
| 129 | + |
| 130 | +```python |
| 131 | +unique_ids = {1, 2, 3} |
| 132 | +``` |
| 133 | + |
| 134 | +### `frozenset` |
| 135 | + |
| 136 | +Immutable version of a set: |
| 137 | + |
| 138 | +```python |
| 139 | +readonly_ids = frozenset([1, 2, 3]) |
| 140 | +``` |
| 141 | + |
| 142 | +--- |
| 143 | + |
| 144 | +## Boolean Type: `bool` |
| 145 | + |
| 146 | +Only `True` or `False`: |
| 147 | + |
| 148 | +```python |
| 149 | +is_active = True |
| 150 | +``` |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +## Binary Types |
| 155 | + |
| 156 | +### `bytes` |
| 157 | + |
| 158 | +Immutable byte sequence: |
| 159 | + |
| 160 | +```python |
| 161 | +b = b"Hello" |
| 162 | +``` |
| 163 | + |
| 164 | +### `bytearray` |
| 165 | + |
| 166 | +Mutable version: |
| 167 | + |
| 168 | +```python |
| 169 | +ba = bytearray([65, 66, 67]) |
| 170 | +``` |
| 171 | + |
| 172 | +### `memoryview` |
| 173 | + |
| 174 | +Provides memory-efficient access: |
| 175 | + |
| 176 | +```python |
| 177 | +mv = memoryview(bytes([1, 2, 3])) |
| 178 | +``` |
| 179 | + |
| 180 | +--- |
| 181 | + |
| 182 | +## None Type |
| 183 | + |
| 184 | +Represents no value: |
| 185 | + |
| 186 | +```python |
| 187 | +response = None |
| 188 | +``` |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +## Type Checking and Conversion |
| 193 | + |
| 194 | +### Check type |
| 195 | + |
| 196 | +```python |
| 197 | +type(3.14) # Output: <class 'float'> |
| 198 | +``` |
| 199 | + |
| 200 | +### Type Conversion |
| 201 | + |
| 202 | +```python |
| 203 | +int("5") # Output: 5 |
| 204 | +str(10) # Output: "10" |
| 205 | +list("abc") # Output: ['a', 'b', 'c'] |
| 206 | +``` |
| 207 | + |
| 208 | +--- |
| 209 | + |
| 210 | +## Conclusion |
| 211 | + |
| 212 | +Python provides a variety of built-in data types to handle data in efficient and expressive ways. Knowing when and how to use each data type is essential for writing clean and effective Python code. |
0 commit comments