diff --git a/docs/_toc.yml b/docs/_toc.yml index fb7a428..d3b9220 100644 --- a/docs/_toc.yml +++ b/docs/_toc.yml @@ -21,7 +21,10 @@ parts: chapters: - file: python/python_fundamentals/index sections: - - file: python/python_fundamentals/variables_data_types + - file: python/python_fundamentals/variables + - file: python/python_fundamentals/data_types + sections: + - file: python/python_fundamentals/strings - file: python/python_fundamentals/operators - file: python/python_fundamentals/conditionals - file: python/python_fundamentals/loops diff --git a/docs/python/python_fundamentals/data_types.ipynb b/docs/python/python_fundamentals/data_types.ipynb new file mode 100644 index 0000000..366ce62 --- /dev/null +++ b/docs/python/python_fundamentals/data_types.ipynb @@ -0,0 +1,154 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "630a3d80-68f8-406d-9cca-101f75e7088a", + "metadata": {}, + "source": [ + "# Data Types\n", + "\n", + "There are several data types in Python. To identify the data type we use the `type` built-in function. We would like to ask you to focus on understanding different data types very well. When it comes to programming, it is all about data types. We introduced data types at the very beginning and it comes again because every topic is related to data types. We will cover data types in more detail in their respective sections.\n", + "\n", + "```{contents}\n", + ":local:\n", + "```\n", + "\n", + "## Checking data types\n", + "\n", + "To check the data type of certain data/variables we use the built-in `type()` function.\n", + "\n", + "**Example:**" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "bd460a61-3563-448c-a2d0-20dddd266ee2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + } + ], + "source": [ + "# Different python data types\n", + "# Let's declare variables with various data types\n", + "\n", + "first_name = \"Aurora\" # str\n", + "last_name = \"Luna\" # str\n", + "country = \"Indonesia\" # str\n", + "city = \"Palembang\" # str\n", + "age = 6 # int\n", + "\n", + "# Printing out types\n", + "print(type(\"Aurora\")) # str\n", + "print(type(first_name)) # str\n", + "print(type(10)) # int\n", + "print(type(3.14)) # float\n", + "print(type(1 + 1j)) # complex\n", + "print(type(True)) # bool\n", + "print(type([1, 2, 3, 4])) # list\n", + "print(type({\"name\": \"Aurora\", \"age\": 6, \"is_married\": False})) # dict\n", + "print(type((1, 2))) # tuple\n", + "print(type(zip([1, 2], [3, 4]))) # set" + ] + }, + { + "cell_type": "markdown", + "id": "b7b249ba-3bbe-450d-9cbb-6e436f19d6fa", + "metadata": {}, + "source": [ + "## Casting data types\n", + "\n", + "Casting is a process of converting one data type to another data type. We use `int()`, `float()`, `str()`, `list()`, `set()`. When we do arithmetic operations string numbers should be first converted to `int` or `float` otherwise it will return an error. If we concatenate a number with a string, the number should be first converted to a string.\n", + "\n", + "**Example:**" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "0740ec58-aa16-4c6d-bbf2-c544e35cb44d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "num_int 10\n", + "num_float: 10.0\n", + "9\n", + "10\n", + "10\n", + "num_float 10.6\n", + "num_int 10\n", + "Aurora\n", + "['A', 'u', 'r', 'o', 'r', 'a']\n" + ] + } + ], + "source": [ + "# int to float\n", + "num_int = 10\n", + "print(\"num_int\", num_int) # 10\n", + "num_float = float(num_int)\n", + "print(\"num_float:\", num_float) # 10.0\n", + "\n", + "# float to int\n", + "gravity = 9.81\n", + "print(int(gravity)) # 9\n", + "\n", + "# int to str\n", + "num_int = 10\n", + "print(num_int) # 10\n", + "num_str = str(num_int)\n", + "print(num_str) # \"10\"\n", + "\n", + "# str to int or float\n", + "num_str = \"10.6\"\n", + "print(\"num_float\", float(num_str)) # 10.6\n", + "print(\"num_int\", int(float(num_str))) # 10\n", + "\n", + "# str to list\n", + "first_name = \"Aurora\"\n", + "print(first_name) # \"Aurora\"\n", + "first_name_to_list = list(first_name)\n", + "print(first_name_to_list) # ['A', 'u', 'r', 'o', 'r', 'a']" + ] + } + ], + "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.12.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/python/python_fundamentals/strings.ipynb b/docs/python/python_fundamentals/strings.ipynb new file mode 100644 index 0000000..ac784bb --- /dev/null +++ b/docs/python/python_fundamentals/strings.ipynb @@ -0,0 +1,582 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "4239acf5-0139-4c5f-8e58-90023c164846", + "metadata": {}, + "source": [ + "# Strings\n", + "\n", + "Text is a string data type. Any data type written as text is a string. Any data under single, double or triple quote are strings. There are different string methods and built-in functions to deal with string data types. To check the length of a string use the len() method.\n", + "\n", + "```{contents}\n", + ":local:\n", + "```\n", + "\n", + "## Creating a string" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "92b369ac-e87f-408e-be5d-656fe9c61a78", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "P\n", + "1\n", + "Hello, World!\n", + "13\n", + "I hope you are enjoying Applied Python Training\n" + ] + } + ], + "source": [ + "letter = \"P\" # A string could be a single character or a bunch of texts\n", + "print(letter) # P\n", + "print(len(letter)) # 1\n", + "greeting = \"Hello, World!\" # String could be made using a single or double quote,\"Hello, World!\"\n", + "print(greeting) # Hello, World!\n", + "print(len(greeting)) # 13\n", + "sentence = \"I hope you are enjoying Applied Python Training\"\n", + "print(sentence)" + ] + }, + { + "cell_type": "markdown", + "id": "442c86f1-e063-40ef-897e-6d5c4521d651", + "metadata": {}, + "source": [ + "Multiline string is created by using triple single (''') or triple double quotes (\"\"\"). See the example below." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "2d159785-6988-41c3-b4a9-75c52e1b9b2c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I am an engineer and enjoy sharing.\n", + "I didn't find anything as rewarding as empowering people.\n", + "That is why I created Applied Python Training.\n" + ] + } + ], + "source": [ + "multiline_string = \"\"\"I am an engineer and enjoy sharing.\n", + "I didn't find anything as rewarding as empowering people.\n", + "That is why I created Applied Python Training.\"\"\"\n", + "print(multiline_string)" + ] + }, + { + "cell_type": "markdown", + "id": "771bb137-0d0e-413e-b431-557b4877b0e9", + "metadata": {}, + "source": [ + "## String Concatenation\n", + "\n", + "We can connect strings together. Merging or connecting strings is called concatenation. See the example below:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "103c425a-54c2-483f-8cc9-d95f4e8429e2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Aurora Luna\n", + "6\n", + "4\n", + "True\n", + "11\n" + ] + } + ], + "source": [ + "first_name = \"Aurora\"\n", + "last_name = \"Luna\"\n", + "space = \" \"\n", + "full_name = first_name + space + last_name\n", + "print(full_name)\n", + "# Checking the length of a string using len() built-in function\n", + "print(len(first_name))\n", + "print(len(last_name))\n", + "print(len(first_name) > len(last_name)) # True\n", + "print(len(full_name))" + ] + }, + { + "cell_type": "markdown", + "id": "7abd15f3-c224-4415-b650-e78d83106583", + "metadata": {}, + "source": [ + "## Escape Sequences in Strings" + ] + }, + { + "cell_type": "markdown", + "id": "cfb6608b-3450-4213-b04d-45705a5a13b7", + "metadata": {}, + "source": [ + "In Python and other programming languages \\ followed by a character is an escape sequence. Let us see the most common escape characters:\n", + "\n", + "- `\\n`: new line\n", + "- `\\t`: Tab means(8 spaces)\n", + "- `\\\\`: Back slash\n", + "- `\\'`: Single quote (')\n", + "- `\\\"`: Double quote (\")\n", + "\n", + "Now, let us see the use of the above escape sequences with examples." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "2f739df5-bc4a-436b-b8b5-7ffb300a5521", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I hope everyone is enjoying the Python Training.\n", + "Are you ?\n", + "Days\tTopics\tExercises\n", + "Day 1\t5\t5\n", + "Day 2\t6\t20\n", + "Day 3\t5\t23\n", + "Day 4\t1\t35\n", + "This is a backslash symbol (\\)\n", + "In every programming language it starts with \"Hello, World!\"\n" + ] + } + ], + "source": [ + "print(\"I hope everyone is enjoying the Python Training.\\nAre you ?\") # line break\n", + "print(\"Days\\tTopics\\tExercises\") # adding tab space or 4 spaces\n", + "print(\"Day 1\\t5\\t5\")\n", + "print(\"Day 2\\t6\\t20\")\n", + "print(\"Day 3\\t5\\t23\")\n", + "print(\"Day 4\\t1\\t35\")\n", + "print(\"This is a backslash symbol (\\\\)\") # To write a backslash\n", + "print(\n", + " 'In every programming language it starts with \"Hello, World!\"'\n", + ") # to write a double quote inside a single quote" + ] + }, + { + "cell_type": "markdown", + "id": "c248ff5f-6c5c-4c3c-b9a9-4a4b6e069513", + "metadata": {}, + "source": [ + "## String formatting\n", + "\n", + "### Old Style String Formatting (% Operator)\n", + "\n", + "In Python there are many ways of formatting strings. In this section, we will cover some of them.\n", + "The \"%\" operator is used to format a set of variables enclosed in a \"tuple\" (a fixed size list), together with a format string, which contains normal text together with \"argument specifiers\", special symbols like \"%s\", \"%d\", \"%f\", \"%.nf\".\n", + "\n", + "- `%s` - String (or any object with a string representation, like numbers)\n", + "- `%d` - Integers\n", + "- `%f` - Floating point numbers\n", + "- `\"%.nf\"` - Floating point numbers with fixed `n` precision" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "b7a5a933-a425-4cc9-85a5-0750cfc01d61", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I am Aurora Luna. I learn Python\n", + "The following are python libraries:['Django', 'Flask', 'NumPy', 'Matplotlib', 'Pandas']\n" + ] + } + ], + "source": [ + "# Strings only\n", + "first_name = \"Aurora\"\n", + "last_name = \"Luna\"\n", + "language = \"Python\"\n", + "formated_string = \"I am %s %s. I learn %s\" % (first_name, last_name, language)\n", + "print(formated_string)\n", + "\n", + "# Strings and numbers\n", + "radius = 10\n", + "pi = 3.14\n", + "area = pi * radius**2\n", + "formated_string = \"The area of circle with a radius %d is %.2f.\" % (\n", + " radius,\n", + " area,\n", + ") # 2 refers the 2 significant digits after the point\n", + "\n", + "python_libraries = [\"Django\", \"Flask\", \"NumPy\", \"Matplotlib\", \"Pandas\"]\n", + "formated_string = \"The following are python libraries:%s\" % (python_libraries)\n", + "print(\n", + " formated_string\n", + ") # \"The following are python libraries:['Django', 'Flask', 'NumPy', 'Matplotlib','Pandas']\"" + ] + }, + { + "cell_type": "markdown", + "id": "31117d76-7ac4-4f84-bf5c-6549f3422010", + "metadata": {}, + "source": [ + "### New Style String Formatting (str.format)\n", + "\n", + "This formatting is introduced in Python version 3." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "a0eb6dca-e63b-4d69-a84a-101529e80240", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I am Aurora Luna. I learn Python\n", + "4 + 3 = 7\n", + "4 - 3 = 1\n", + "4 * 3 = 12\n", + "4 / 3 = 1.33\n", + "4 % 3 = 1\n", + "4 // 3 = 1\n", + "4 ** 3 = 64\n", + "The area of a circle with a radius 10 is 314.00.\n" + ] + } + ], + "source": [ + "first_name = \"Aurora\"\n", + "last_name = \"Luna\"\n", + "language = \"Python\"\n", + "formated_string = \"I am {} {}. I learn {}\".format(first_name, last_name, language)\n", + "print(formated_string)\n", + "a = 4\n", + "b = 3\n", + "\n", + "print(\"{} + {} = {}\".format(a, b, a + b))\n", + "print(\"{} - {} = {}\".format(a, b, a - b))\n", + "print(\"{} * {} = {}\".format(a, b, a * b))\n", + "print(\"{} / {} = {:.2f}\".format(a, b, a / b)) # limits it to two digits after decimal\n", + "print(\"{} % {} = {}\".format(a, b, a % b))\n", + "print(\"{} // {} = {}\".format(a, b, a // b))\n", + "print(\"{} ** {} = {}\".format(a, b, a**b))\n", + "\n", + "# Strings and numbers\n", + "radius = 10\n", + "pi = 3.14\n", + "area = pi * radius**2\n", + "formated_string = \"The area of a circle with a radius {} is {:.2f}.\".format(\n", + " radius, area\n", + ") # 2 digits after decimal\n", + "print(formated_string)" + ] + }, + { + "cell_type": "markdown", + "id": "532187f8-43f6-4a09-8309-b1fde3152f5d", + "metadata": {}, + "source": [ + "### String Interpolation / f-Strings (Python 3.6+)\n", + "\n", + "Another new string formatting is string interpolation, f-strings. Strings start with f and we can inject the data in their corresponding positions." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "03f74f4c-cb01-4219-8e7c-67b4b7a957a2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4 + 3 = 7\n", + "4 - 3 = 1\n", + "4 * 3 = 12\n", + "4 / 3 = 1.33\n", + "4 % 3 = 1\n", + "4 // 3 = 1\n", + "4 ** 3 = 64\n" + ] + } + ], + "source": [ + "a = 4\n", + "b = 3\n", + "print(f\"{a} + {b} = {a +b}\")\n", + "print(f\"{a} - {b} = {a - b}\")\n", + "print(f\"{a} * {b} = {a * b}\")\n", + "print(f\"{a} / {b} = {a / b:.2f}\")\n", + "print(f\"{a} % {b} = {a % b}\")\n", + "print(f\"{a} // {b} = {a // b}\")\n", + "print(f\"{a} ** {b} = {a ** b}\")" + ] + }, + { + "cell_type": "markdown", + "id": "94daa839-c633-4940-9a66-ce703c676e80", + "metadata": {}, + "source": [ + "## Python Strings as Sequences of Characters\n", + "\n", + "Python strings are sequences of characters, and share their basic methods of access with other Python ordered sequences of objects – lists and tuples. The simplest way of extracting single characters from strings (and individual members from any sequence) is to unpack them into corresponding variables.\n", + "\n", + "### Unpacking Characters" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "58124504-7dfe-48ce-9ef4-0021fefc9e26", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "P\n", + "y\n", + "t\n", + "h\n", + "o\n", + "n\n" + ] + } + ], + "source": [ + "language = \"Python\"\n", + "a, b, c, d, e, f = language # unpacking sequence characters into variables\n", + "print(a)\n", + "print(b)\n", + "print(c)\n", + "print(d)\n", + "print(e)\n", + "print(f)" + ] + }, + { + "cell_type": "markdown", + "id": "e594ecba-42e9-4992-8847-4146b2569cdb", + "metadata": {}, + "source": [ + "### Accessing Characters in Strings by Index\n", + "\n", + "In Python, indexing starts from zero. Therefore the first letter of a string is at zero index and the last letter of a string is the length of a string minus one.\n", + "\n", + "| P | y | t | h | o | n |\n", + "|---|---|---|---|---|---|\n", + "| 0 | 1 | 2 | 3 | 4 | 5 |" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "7756a031-c3ec-4082-ace7-062bc2336b75", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "P\n", + "y\n", + "n\n" + ] + } + ], + "source": [ + "language = \"Python\"\n", + "first_letter = language[0]\n", + "print(first_letter)\n", + "second_letter = language[1]\n", + "print(second_letter)\n", + "last_index = len(language) - 1\n", + "last_letter = language[last_index]\n", + "print(last_letter)" + ] + }, + { + "cell_type": "markdown", + "id": "ef0007a8-c4d5-41af-bc82-18dfb063a6db", + "metadata": {}, + "source": [ + "If we want to start from right end we can use negative indexing. -1 is the last index." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "fad0283a-6673-4114-a810-1c87f58a3d29", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "n\n", + "o\n" + ] + } + ], + "source": [ + "language = \"Python\"\n", + "last_letter = language[-1]\n", + "print(last_letter)\n", + "second_last = language[-2]\n", + "print(second_last)" + ] + }, + { + "cell_type": "markdown", + "id": "736800b4-78d4-4298-95ae-59727d1893d3", + "metadata": {}, + "source": [ + "### Slicing Python Strings\n", + "\n", + "In python we can slice strings into substrings." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "b35d25c3-1a94-4fc2-9988-b577d5f9f89f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Pyt\n", + "hon\n", + "hon\n", + "hon\n" + ] + } + ], + "source": [ + "language = \"Python\"\n", + "first_three = language[0:3] # starts at zero index and up to 3 but not include 3\n", + "print(first_three)\n", + "last_three = language[3:6]\n", + "print(last_three)\n", + "# Another way\n", + "last_three = language[-3:]\n", + "print(last_three)\n", + "last_three = language[3:]\n", + "print(last_three)" + ] + }, + { + "cell_type": "markdown", + "id": "d98af05f-ca1e-4492-9926-4538e8034f21", + "metadata": {}, + "source": [ + "### Reversing a String\n", + "\n", + "We can easily reverse strings in python." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "db909f48-dae8-4238-8898-7fcfda55fb44", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "!dlroW ,olleH\n" + ] + } + ], + "source": [ + "greeting = \"Hello, World!\"\n", + "print(greeting[::-1])" + ] + }, + { + "cell_type": "markdown", + "id": "7d9fcf3b-8a21-4757-b9a5-215408ac164e", + "metadata": {}, + "source": [ + "### Skipping Characters While Slicing\n", + "\n", + "It is possible to skip characters while slicing by passing step argument to slice method." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "7d43f63b-c35b-46d5-82f6-3de74cd92427", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Pto\n" + ] + } + ], + "source": [ + "language = \"Python\"\n", + "pto = language[0:6:2]\n", + "print(pto)" + ] + }, + { + "cell_type": "markdown", + "id": "07294332-6fc1-4a01-aba3-bdff01d1fb6a", + "metadata": {}, + "source": [ + "## String Methods\n", + "\n", + "There are many string methods which allow us to format strings. You can refer to all of them from here: https://docs.python.org/3/library/stdtypes.html#string-methods" + ] + } + ], + "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.12.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/python/python_fundamentals/variables_data_types.ipynb b/docs/python/python_fundamentals/variables.ipynb similarity index 65% rename from docs/python/python_fundamentals/variables_data_types.ipynb rename to docs/python/python_fundamentals/variables.ipynb index b82b744..9ede101 100644 --- a/docs/python/python_fundamentals/variables_data_types.ipynb +++ b/docs/python/python_fundamentals/variables.ipynb @@ -1,23 +1,15 @@ { "cells": [ - { - "cell_type": "markdown", - "id": "54d0dc5f-5292-4412-aad9-bf85d1382b6c", - "metadata": {}, - "source": [ - "# Variables & Data Types" - ] - }, { "cell_type": "markdown", "id": "248c15e2-fe45-4b2c-a1ad-7da9ea951f0c", "metadata": {}, "source": [ - "## Variables\n", + "# Variables\n", "\n", "Variables store data in a computer's memory. A variable refers to a memory address in which data is stored. A variable can have a short name (like x, y, z), but a more descriptive name (firstname, lastname, age, country) is highly recommended. Number at the beginning, special character, hyphen are not allowed when naming a variable.\n", "\n", - "**Python Variable Naming Rules**\n", + "## Python Variable Naming Rules\n", "\n", "* A variable name must start with a letter or the underscore character\n", "* A variable name cannot start with a number\n", @@ -167,7 +159,7 @@ "id": "48878892-6894-4ba6-a025-147f6c9aa49d", "metadata": {}, "source": [ - "### Declaring Multiple Variable in a Line\n", + "## Declaring Multiple Variable in a Line\n", "\n", "Multiple variables can also be declared in one line:\n", "\n", @@ -250,131 +242,6 @@ "print(first_name)\n", "print(age)" ] - }, - { - "cell_type": "markdown", - "id": "630a3d80-68f8-406d-9cca-101f75e7088a", - "metadata": {}, - "source": [ - "## Data Types\n", - "\n", - "There are several data types in Python. To identify the data type we use the `type` built-in function. We would like to ask you to focus on understanding different data types very well. When it comes to programming, it is all about data types. We introduced data types at the very beginning and it comes again because every topic is related to data types. We will cover data types in more detail in their respective sections.\n", - "\n", - "### Checking data types\n", - "\n", - "To check the data type of certain data/variables we use the built-in `type()` function.\n", - "\n", - "**Example:**" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "bd460a61-3563-448c-a2d0-20dddd266ee2", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n" - ] - } - ], - "source": [ - "# Different python data types\n", - "# Let's declare variables with various data types\n", - "\n", - "first_name = \"Aurora\" # str\n", - "last_name = \"Luna\" # str\n", - "country = \"Indonesia\" # str\n", - "city = \"Palembang\" # str\n", - "age = 6 # int\n", - "\n", - "# Printing out types\n", - "print(type(\"Aurora\")) # str\n", - "print(type(first_name)) # str\n", - "print(type(10)) # int\n", - "print(type(3.14)) # float\n", - "print(type(1 + 1j)) # complex\n", - "print(type(True)) # bool\n", - "print(type([1, 2, 3, 4])) # list\n", - "print(type({\"name\": \"Aurora\", \"age\": 6, \"is_married\": False})) # dict\n", - "print(type((1, 2))) # tuple\n", - "print(type(zip([1, 2], [3, 4]))) # set" - ] - }, - { - "cell_type": "markdown", - "id": "b7b249ba-3bbe-450d-9cbb-6e436f19d6fa", - "metadata": {}, - "source": [ - "### Casting data types\n", - "\n", - "Casting is a process of converting one data type to another data type. We use `int()`, `float()`, `str()`, `list()`, `set()`. When we do arithmetic operations string numbers should be first converted to `int` or `float` otherwise it will return an error. If we concatenate a number with a string, the number should be first converted to a string.\n", - "\n", - "**Example:**" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "0740ec58-aa16-4c6d-bbf2-c544e35cb44d", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "num_int 10\n", - "num_float: 10.0\n", - "9\n", - "10\n", - "10\n", - "num_float 10.6\n", - "num_int 10\n", - "Aurora\n", - "['A', 'u', 'r', 'o', 'r', 'a']\n" - ] - } - ], - "source": [ - "# int to float\n", - "num_int = 10\n", - "print(\"num_int\", num_int) # 10\n", - "num_float = float(num_int)\n", - "print(\"num_float:\", num_float) # 10.0\n", - "\n", - "# float to int\n", - "gravity = 9.81\n", - "print(int(gravity)) # 9\n", - "\n", - "# int to str\n", - "num_int = 10\n", - "print(num_int) # 10\n", - "num_str = str(num_int)\n", - "print(num_str) # \"10\"\n", - "\n", - "# str to int or float\n", - "num_str = \"10.6\"\n", - "print(\"num_float\", float(num_str)) # 10.6\n", - "print(\"num_int\", int(float(num_str))) # 10\n", - "\n", - "# str to list\n", - "first_name = \"Aurora\"\n", - "print(first_name) # \"Aurora\"\n", - "first_name_to_list = list(first_name)\n", - "print(first_name_to_list) # ['A', 'u', 'r', 'o', 'r', 'a']" - ] } ], "metadata": {