"
]
},
{
"cell_type": "markdown",
"id": "2923e0c2",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "3cd521f2",
"metadata": {},
"source": [
"## _02-numbers.ipynb_\n",
"#### [Learn more about Numeric Types](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex)"
]
},
{
"cell_type": "markdown",
"id": "0356606a",
"metadata": {},
"source": [
"## Learning agenda of this notebook\n",
"1. Python Numeric data types (Numbers)\n",
"2. Issues with Float data type\n",
"3. Conversiton / Type Casting"
]
},
{
"cell_type": "markdown",
"id": "e20bffdf",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"id": "f8938a24",
"metadata": {},
"source": [
"## 1. Numeric Data Types\n",
"- Python's Number data types are created by numeric literals and returned as results by arithmetic operators and arithmetic built-in functions. \n",
"- All Numeric objects are immutable; once created their value never changes.\n",
" - **Integers:** The integer data type is comprised of all the positive and negative whole numbers. Integers represent positive or negative whole numbers, from negative infinity to infinity. Note that integers should not include decimal points. Integers have the type `int`. It is immutable and created through numeric literals and output of arithmetic expressions and functions that return numeric values. In Python 2.x there are two types int and long. But in Python 3.x, there is only one type int having unlimited range subject to available memory \n",
" - **Floating Point Numbers:** Floating-point numbers, or floats, refer to positive and negative decimal numbers. These represent machine level double precision floating point numbers. The decimal digits are faithful upto fifteen decimal places. The range in most environment is -1.7976931348623157e+308 to 1.7976931348623157e+308. Floating point numbers can also be written using the scientific notation with an \"e\" to indicate the power of 10.\n",
" - **Complex Numbers:** These represent complex numbers as a pair of machine level double precision floating point numbers. One floating point number represent the real part and another floating point number represent the imaginary part\n",
" - **Boolean:** Also known as bool data type allows us to choose between two values: `True` and `False`. Booleans have the type `bool`. A Boolean is used to determine whether the logic of an expression or a comparison is correct. It plays a huge role in data comparisons.\n",
"- Other types that we will study later are:\n",
" - Sequences\n",
" - Immutable Sequences (Strings, Tuples, Range, Bytes)\n",
" - Mutable Sequences (Lists)\n",
" - Set Types (Sets, Frozen sets)\n",
" - Mappings (Dictionaries)\n",
" - Callable Types \n",
" - Built-in functions (print(), len(), math.sin())\n",
" - Built-in methods (mystring.split(), mylist.append())\n",
" - User-defined functions, \n",
" - Modules\n",
" - Classes\n",
" - Instance methods\n",
" - Generator functions\n",
" - Coroutine functions"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6c0e10f7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\n",
"\n"
]
}
],
"source": [
"w = 10\n",
"x = 20.5\n",
"y = complex(3, 2) \n",
"y = 3 + 2j\n",
"z = True\n",
"print(type(w))\n",
"print(type(x))\n",
"print(type(y))\n",
"print(type(z))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "21aa6467",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"\n"
]
}
],
"source": [
"# Example of Boolean data type \n",
"y = 10 < 100\n",
"print(y)\n",
"print(type(y))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "16d3f182",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x is False\n",
"y is False\n"
]
}
],
"source": [
"\"\"\"\n",
"boolean data type in Python?\n",
"In Python, True represents the value as 1 and False as 0. \n",
"\"\"\"\n",
"x = (-1 == True)\n",
"y = (1 == False)\n",
"print(\"x is\", x)\n",
"print(\"y is\", y)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "dad90945",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "can only concatenate str (not \"int\") to str",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/1t/g3ylw8h50cjdqmk5d6jh1qmm0000gn/T/ipykernel_27014/3417399837.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Booleans are automatically converted to `int`s when used in arithmetic operations.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# `True` is converted to `1` and `False` is converted to `0`.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"\"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m9\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mFalse\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m15\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
]
}
],
"source": [
"# Booleans are automatically converted to `int`s when used in arithmetic operations. \n",
"# `True` is converted to `1` and `False` is converted to `0`.\n",
"a = True + 9\n",
"b = False + 15\n",
"\n",
"print(\"a:\", a)\n",
"print(\"b:\", b)"
]
},
{
"cell_type": "markdown",
"id": "1d37881f",
"metadata": {},
"source": [
"Any value in Python can be converted to a Boolean using the `bool` function. Only the following values evaluate to `False` (they are often called *falsy* values):\n",
"1. The value `False` itself\n",
"2. The integer `0`\n",
"3. The float `0.0`\n",
"4. The empty value `None` \n",
"5. The empty text `\"\"`\n",
"6. The empty list `[]`\n",
"7. The empty tuple `()`\n",
"8. The empty dictionary `{}`\n",
"9. The empty set `set()`\n",
"10. The empty range `range(0)`\n",
"\n",
"Everything else evaluates to `True` (a value that evaluates to `True` is often called a *truthy* value).\n",
"\n",
"The **None** type includes a single value `None`, used to indicate the absence of a value. `None` has the type `NoneType`. It is often used to declare a variable whose value may be assigned later or as a return value of functions that do not return a value"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "790ea70b",
"metadata": {},
"outputs": [],
"source": [
"bool(0)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "523e77fd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(0.0)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "608bb8c8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(None)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a054f711",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(\"\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "fcdc71a5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool([])"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "0ad21ab2",
"metadata": {},
"outputs": [],
"source": [
"var1 = None"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "daee6df3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"NoneType"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(var1)"
]
},
{
"cell_type": "markdown",
"id": "b89bc134",
"metadata": {},
"source": [
"**Use of Complex Numbers:** \n",
">- Complex numbers are mostly used when you are dealing with electronics, dynamics, and control systems.\n",
">- In electronics, the state of a circuit element is defined by the voltage (V) and the current (I). Circuit elements can also have a capacitance (c) and inductance (L) that describes the circuit's tendency to resist changes in V and I. \n",
">- Rather than describing the circuit element's state by V and I, it can be described as `z = V + Ij`. \n",
">- The laws of electricity can then be expressed using the addition and multiplication of complex umbers."
]
},
{
"cell_type": "markdown",
"id": "10f47a7e",
"metadata": {},
"source": [
"## 2. Issues with Float data types"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "d20c1eea",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3.3, 3.0, False)"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'''\n",
"Floating-point numbers are stored in computer hardware in IEEE-754 format\n",
"Be watchful while performing various operations on floating point numbers\n",
"'''\n",
"a = 3.3\n",
"b = 1.0 + 2.0\n",
"c = (a == b)\n",
"a, b, c"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "728acfa8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.3"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "a25db0e1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.3000000000000003"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bfb290c7",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "9af36f0d",
"metadata": {},
"source": [
"## 3. Conversion/Type Casting\n",
"- **Implicit Type Conversion:** While performing arithmetic operations, integers are automatically converted to `float` if any of the operands is a `float`. Also, the division operator `/` always returns a `float`, even if both operands are integers. Use the `//` operator if you want the result of the division to be an `int`.\n",
"- **Explicit Type Conversion or Type Casting** refers to the conversion of an object from one data type to another"
]
},
{
"cell_type": "markdown",
"id": "e2c45a9e",
"metadata": {},
"source": [
"### a. Implicit Type Conversion"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "e9430936",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(int, float)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 2 + 3\n",
"b = 2 + 3.0\n",
"type(a), type(b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "729b1d66",
"metadata": {},
"outputs": [],
"source": [
"a = 3 * 5\n",
"b = 3 * 5.0\n",
"type(a), type(b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5bae316c",
"metadata": {},
"outputs": [],
"source": [
"a = 4/2\n",
"b = 4/2.0\n",
"type(a), type(b)"
]
},
{
"cell_type": "markdown",
"id": "c04d6b15",
"metadata": {},
"source": [
"### Explicit Type Conversion (Type Casting)"
]
},
{
"cell_type": "markdown",
"id": "a192930f",
"metadata": {},
"source": [
"#### Convert to Integer using ` int (a)` function"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3df997bc",
"metadata": {},
"outputs": [],
"source": [
"x = '21'\n",
"type(x), x"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee452e3e",
"metadata": {},
"outputs": [],
"source": [
"y = int(x)\n",
"type(y), y"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "be8e4b6a",
"metadata": {},
"outputs": [],
"source": [
"z = bool(-5)\n",
"type(z), z"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13349a50",
"metadata": {},
"outputs": [],
"source": [
"# You can also use hex() and oct() functions\n",
"print(hex(21))\n",
"print(oct(21))"
]
},
{
"cell_type": "markdown",
"id": "cf69b598",
"metadata": {},
"source": [
"#### Convert to Float using `float (a)` function"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8e51ba53",
"metadata": {},
"outputs": [],
"source": [
"a = 54\n",
"type(a), a"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "038b8ac9",
"metadata": {},
"outputs": [],
"source": [
"b = float(a)\n",
"type(b), b"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d38acc2",
"metadata": {},
"outputs": [],
"source": [
"# Note that a and be are two different objects\n",
"id(a), id(b)"
]
},
{
"cell_type": "markdown",
"id": "b900a11c",
"metadata": {},
"source": [
"#### Convert an Integer to a String using `str (a)` function"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "98952c0a",
"metadata": {},
"outputs": [],
"source": [
"num = 341\n",
"type(num), num"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9fa0b140",
"metadata": {},
"outputs": [],
"source": [
"mystr = str(num)\n",
"type(mystr), mystr"
]
},
{
"cell_type": "markdown",
"id": "25ff32a0",
"metadata": {},
"source": [
"#### Convert two Floats to Complex Data type using `complex (a, b)` function"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ff4da2ff",
"metadata": {},
"outputs": [],
"source": [
"a = 2.6\n",
"b = 3.2\n",
"x = complex(a,b)\n",
"type(x), x"
]
},
{
"cell_type": "markdown",
"id": "d6f6886b",
"metadata": {},
"source": [
"## Check your Concepts\n",
"\n",
"Try answering the following questions to test your understanding of the topics covered in this notebook:\n",
"\n",
"1. What are the built-in data types in Python?\n",
"2. What is a primitive data type? \n",
"3. What are the primitive data types available in Python?\n",
"4. What is a data structure or container data type?\n",
"5. What kind of data does the Integer data type represent?\n",
"6. What are the numerical limits of the integer data type?\n",
"7. What kind of data does the float data type represent?\n",
"8. How does Python decide if a given number is a float or an integer?\n",
"9. How can you create a variable which stores a whole number, e.g., 4 but has the float data type?\n",
"10. How do you create floats representing very large (e.g., 6.023 x 10^23) or very small numbers (0.000000123)?\n",
"11. What does the expression `23e-12` represent?\n",
"12. Can floats be used to store numbers with unlimited precision?\n",
"13. What are the differences between integers and floats?\n",
"14. How do you convert an integer to a float?\n",
"15. How do you convert a float to an integer?\n",
"16. What is the result obtained when you convert 1.99 to an integer?\n",
"17. What are the data types of the results of the division operators `/` and `//`?\n",
"18. What kind of data does the Boolean data type represent?\n",
"19. Which types of Python operators return booleans as a result?\n",
"20. What happens if you try to use a boolean in arithmetic operation?\n",
"21. How can any value in Python be covered to a boolean?\n",
"22. What are truthy and falsy values?\n",
"23. What are the values in Python that evaluate to False?\n",
"24. Give some examples of values that evaluate to True.\n",
"25. What kind of data does the None data type represent?\n",
"26. What is the purpose of None?"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}