|
162 | 162 | "print(\"{:03b}\".format(binary))"
|
163 | 163 | ]
|
164 | 164 | },
|
| 165 | + { |
| 166 | + "cell_type": "markdown", |
| 167 | + "metadata": {}, |
| 168 | + "source": [ |
| 169 | + "# Python Type Conversion (Type Casting)\n", |
| 170 | + "Type conversion is the process of converting data of one type to another\n", |
| 171 | + "\n", |
| 172 | + "**Type conversion in Python**\n", |
| 173 | + "\n", |
| 174 | + "- Implicit Conversion - automatic type conversion\n", |
| 175 | + "- Explicit Conversion - manual type conversion\n", |
| 176 | + "\n", |
| 177 | + "**Explicit Type Conversion is also called `Type Casting`**\n", |
| 178 | + "- Python avoids the loss of data in Implicit Type Conversion.\n", |
| 179 | + "- In Type Casting, loss of data may occur as we enforce the object to a specific data type." |
| 180 | + ] |
| 181 | + }, |
165 | 182 | {
|
166 | 183 | "cell_type": "code",
|
167 | | - "execution_count": null, |
| 184 | + "execution_count": 6, |
168 | 185 | "metadata": {},
|
169 | | - "outputs": [], |
170 | | - "source": [] |
| 186 | + "outputs": [ |
| 187 | + { |
| 188 | + "name": "stdout", |
| 189 | + "output_type": "stream", |
| 190 | + "text": [ |
| 191 | + "Value: 125.56\n", |
| 192 | + "Data Type: <class 'float'>\n" |
| 193 | + ] |
| 194 | + } |
| 195 | + ], |
| 196 | + "source": [ |
| 197 | + "## Implicit Type Conversion >> integer to float\n", |
| 198 | + "number = 123\n", |
| 199 | + "\n", |
| 200 | + "number += 2.56\n", |
| 201 | + "\n", |
| 202 | + "# display new value and resulting data type\n", |
| 203 | + "print(\"Value:\",number)\n", |
| 204 | + "print(\"Data Type:\",type(number))" |
| 205 | + ] |
171 | 206 | },
|
172 | 207 | {
|
173 | 208 | "cell_type": "code",
|
174 | | - "execution_count": null, |
| 209 | + "execution_count": 7, |
175 | 210 | "metadata": {},
|
176 | | - "outputs": [], |
177 | | - "source": [] |
| 211 | + "outputs": [ |
| 212 | + { |
| 213 | + "name": "stdout", |
| 214 | + "output_type": "stream", |
| 215 | + "text": [ |
| 216 | + "Data type of num_string before Type Casting: <class 'str'>\n", |
| 217 | + "Data type of num_string after Type Casting: <class 'int'>\n", |
| 218 | + "Sum: 35\n", |
| 219 | + "Data type of num_sum: <class 'int'>\n" |
| 220 | + ] |
| 221 | + } |
| 222 | + ], |
| 223 | + "source": [ |
| 224 | + "# Explicit Type Conversion\n", |
| 225 | + "num_string = '12'\n", |
| 226 | + "num_integer = 23\n", |
| 227 | + "\n", |
| 228 | + "print(\"Data type of num_string before Type Casting:\",type(num_string))\n", |
| 229 | + "\n", |
| 230 | + "# explicit type conversion\n", |
| 231 | + "num_string = int(num_string)\n", |
| 232 | + "\n", |
| 233 | + "print(\"Data type of num_string after Type Casting:\",type(num_string))\n", |
| 234 | + "\n", |
| 235 | + "num_sum = num_integer + num_string\n", |
| 236 | + "\n", |
| 237 | + "print(\"Sum:\",num_sum)\n", |
| 238 | + "print(\"Data type of num_sum:\",type(num_sum))" |
| 239 | + ] |
| 240 | + }, |
| 241 | + { |
| 242 | + "cell_type": "markdown", |
| 243 | + "metadata": {}, |
| 244 | + "source": [ |
| 245 | + "# Python Operators\n", |
| 246 | + "Operators are special symbols that perform operations on variables and values.\n", |
| 247 | + "\n", |
| 248 | + "**Types of Python Operators**\n", |
| 249 | + "\n", |
| 250 | + "- Arithmetic operators\n", |
| 251 | + "- Assignment Operators\n", |
| 252 | + "- Comparison Operators\n", |
| 253 | + "- Logical Operators\n", |
| 254 | + "- Bitwise Operators\n", |
| 255 | + "- Special Operators\n", |
| 256 | + "\n", |
| 257 | + "<!-- using Table Style for this -->\n", |
| 258 | + "| *1. Arithmetic operators* | \n", |
| 259 | + "|:--:| \n", |
| 260 | + "| |\n", |
| 261 | + "\n", |
| 262 | + "\n", |
| 263 | + "| *2. Assignment Operators* | \n", |
| 264 | + "|:--:| \n", |
| 265 | + "|  |\n", |
| 266 | + "\n", |
| 267 | + "\n", |
| 268 | + "| *3. Comparison Operators* | \n", |
| 269 | + "|:--:| \n", |
| 270 | + "|  |\n", |
| 271 | + "\n", |
| 272 | + "\n", |
| 273 | + "| *4. Logical Operators* | \n", |
| 274 | + "|:--:| \n", |
| 275 | + "|  |\n", |
| 276 | + "\n", |
| 277 | + "\n", |
| 278 | + "| *5. Bitwise Operators* | \n", |
| 279 | + "|:--:| \n", |
| 280 | + "|  |\n", |
| 281 | + "\n", |
| 282 | + "\n", |
| 283 | + "6. Special Operators\n", |
| 284 | + "- `Identity operators:` is and is not are used to check if two values are located on the same part of the memory.\n", |
| 285 | + "- `Membership operators:` in and not in are the membership operators. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).\n", |
| 286 | + "\n", |
| 287 | + "[PyDocumentation > 6.10.2. Membership test operations](https://docs.python.org/3/reference/expressions.html#is)\n", |
| 288 | + "\n", |
| 289 | + "- The operators `in and not in` test for membership. x in s evaluates to True if x is a member of s. \n", |
| 290 | + "- For container types such as list, tuple, set, frozenset, dict, or collections.deque, \n", |
| 291 | + "- **the expression x in y is equivalent to any(x is e or x == e for e in y).**\n", |
| 292 | + "- For the string and bytes types, x in y is True if and only if x is a substring of y. An equivalent test is y.find(x) != -1. \n", |
| 293 | + "- Empty strings are always considered to be a substring of any other string, so \"\" in \"abc\" will return True\n", |
| 294 | + "\n", |
| 295 | + "[PyDocumentation > 6.10.3. Identity comparisons](https://docs.python.org/3/reference/expressions.html#is-not)\n", |
| 296 | + "\n", |
| 297 | + "- The operators is and is not test for an object’s identity: x is y is true if and only if x and y are the same object. \n", |
| 298 | + "- An Object’s identity is determined using the `id()` function. x is not y yields the inverse truth value.\n", |
| 299 | + "\n", |
| 300 | + "\n", |
| 301 | + "[PyDocumentation > 3. Data model > 3.1. Objects, values and types](https://docs.python.org/3/reference/datamodel.html#objects-values-and-types)\n", |
| 302 | + "\n", |
| 303 | + "- All data in a Python program is represented by objects or by relations between objects.\n", |
| 304 | + "- Every object has an identity, a type and a value. \n", |
| 305 | + "- An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. \n", |
| 306 | + "- The `is` operator compares the identity of two objects; the `id()` function returns an integer representing its identity.\n", |
| 307 | + "- An object’s type determines the operations that the object supports (e.g., "does it have a length?") and also defines the possible values for objects of that type. The type() function returns an object’s type (which is an object itself). Like its identity, an object’s type is also unchangeable.\n", |
| 308 | + "\n", |
| 309 | + "**Mutable vs Immutable Objects**\n", |
| 310 | + "- The value of some objects can change. Objects whose value can change are said to be `mutable`; objects whose value is unchangeable once they are created are called `immutable`. \n", |
| 311 | + "- (The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.) \n", |
| 312 | + "- An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable." |
| 313 | + ] |
| 314 | + }, |
| 315 | + { |
| 316 | + "cell_type": "code", |
| 317 | + "execution_count": 18, |
| 318 | + "metadata": {}, |
| 319 | + "outputs": [ |
| 320 | + { |
| 321 | + "name": "stdout", |
| 322 | + "output_type": "stream", |
| 323 | + "text": [ |
| 324 | + "x_id:2252713034096 \n", |
| 325 | + "y_id:2252713034096 x1 is y1 True\n", |
| 326 | + "x1 is not y1: False\n", |
| 327 | + "x2 is y2: True\n", |
| 328 | + "x3 is y3: False\n", |
| 329 | + "x3_id:2252796120832 \n", |
| 330 | + "y3_id:2252796229312 x3 is y3 False\n" |
| 331 | + ] |
| 332 | + } |
| 333 | + ], |
| 334 | + "source": [ |
| 335 | + "x1 = 5\n", |
| 336 | + "y1 = 5\n", |
| 337 | + "x2 = 'Hello'\n", |
| 338 | + "y2 = 'Hello'\n", |
| 339 | + "x3 = [1,2,3]\n", |
| 340 | + "y3 = [1,2,3]\n", |
| 341 | + "\n", |
| 342 | + "print(f\"x_id:{id(x1)} \\ny_id:{id(y1)} x1 is y1 {x1 is y1}\") # prints True\n", |
| 343 | + "\n", |
| 344 | + "print(\"x1 is not y1: \", x1 is not y1) # prints False\n", |
| 345 | + "print(\"x2 is y2: \", x2 is y2) # prints True\n", |
| 346 | + "print(\"x3 is y3: \", x3 is y3) # prints False\n", |
| 347 | + "\n", |
| 348 | + "print(f\"x3_id:{id(x3)} \\ny3_id:{id(y3)} x3 is y3 {x3 is y3}\") # prints True" |
| 349 | + ] |
| 350 | + }, |
| 351 | + { |
| 352 | + "cell_type": "markdown", |
| 353 | + "metadata": {}, |
| 354 | + "source": [ |
| 355 | + "# Python Namespace and Scope\n", |
| 356 | + "\n", |
| 357 | + "- `Namespace` is a mapping of every name used to store the values of variables and other objects in the program, and to associate them with a specific name\n", |
| 358 | + "- This allows us to use the same name for different variables or objects in different parts of your code, without causing any conflicts or confusion.\n", |
| 359 | + "\n", |
| 360 | + "\n", |
| 361 | + "|  | \n", |
| 362 | + "|:--:| \n", |
| 363 | + "| *Python Namespaces* |\n", |
| 364 | + "\n", |
| 365 | + "**`Built-in namespace`**\n", |
| 366 | + "- `built-in namespace` is created when we start the Python interpreter and exists as long as the interpreter runs. \n", |
| 367 | + "- This is the reason that built-in functions like id(), print() etc. are always available to us from any part of the program.\n", |
| 368 | + "\n", |
| 369 | + "**`Global namespace`**\n", |
| 370 | + "- Each module creates its own `Global namespace`.\n", |
| 371 | + "- These different namespaces are isolated. Hence, the same name that may exist in different modules does not collide.\n", |
| 372 | + "- Modules can have various functions and classes. \n", |
| 373 | + "\n", |
| 374 | + "`Local namespace`\n", |
| 375 | + "- A local namespace is created when a function is called, which has all the names defined in it.\n", |
| 376 | + "- Similar is the case with class.\n", |
| 377 | + "\n", |
| 378 | + "## Python Variable Scope << Namespaces >>\n", |
| 379 | + "**When a reference is made inside a function, the name is searched in the local namespace, then in the global namespace and finally in the built-in namespace.**" |
| 380 | + ] |
| 381 | + }, |
| 382 | + { |
| 383 | + "cell_type": "code", |
| 384 | + "execution_count": 20, |
| 385 | + "metadata": {}, |
| 386 | + "outputs": [ |
| 387 | + { |
| 388 | + "name": "stdout", |
| 389 | + "output_type": "stream", |
| 390 | + "text": [ |
| 391 | + "global namespace 10\n", |
| 392 | + "local namespace 20\n", |
| 393 | + "nested local namespace: 30\n" |
| 394 | + ] |
| 395 | + } |
| 396 | + ], |
| 397 | + "source": [ |
| 398 | + "# number is in the global namespace\n", |
| 399 | + "number = 10\n", |
| 400 | + "\n", |
| 401 | + "def outer_function():\n", |
| 402 | + " # number is in the local namespace \n", |
| 403 | + " number = 20\n", |
| 404 | + "\n", |
| 405 | + " def inner_function():\n", |
| 406 | + " # number is in the nested local namespace \n", |
| 407 | + " number = 30\n", |
| 408 | + "\n", |
| 409 | + " print(\"nested local namespace: \", number)\n", |
| 410 | + "\n", |
| 411 | + " print(\"local namespace\", number)\n", |
| 412 | + "\n", |
| 413 | + " inner_function()\n", |
| 414 | + "\n", |
| 415 | + "# print the value of the global variable\n", |
| 416 | + "print(\"global namespace\", number)\n", |
| 417 | + "\n", |
| 418 | + "# call the outer function and print local and nested local variables\n", |
| 419 | + "outer_function()" |
| 420 | + ] |
| 421 | + }, |
| 422 | + { |
| 423 | + "cell_type": "markdown", |
| 424 | + "metadata": {}, |
| 425 | + "source": [ |
| 426 | + "# References\n", |
| 427 | + "\n", |
| 428 | + "- [The Python Language Reference](https://docs.python.org/3/reference/index.html)\n", |
| 429 | + "- [Programiz - Python](https://www.programiz.com/python-programming/)" |
| 430 | + ] |
178 | 431 | }
|
179 | 432 | ],
|
180 | 433 | "metadata": {
|
|
0 commit comments