Skip to content

Commit

Permalink
strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ariefrahmansyah committed Aug 7, 2024
1 parent 3858cb8 commit 833b61d
Show file tree
Hide file tree
Showing 4 changed files with 743 additions and 137 deletions.
5 changes: 4 additions & 1 deletion docs/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
154 changes: 154 additions & 0 deletions docs/python/python_fundamentals/data_types.ipynb
Original file line number Diff line number Diff line change
@@ -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": [
"<class 'str'>\n",
"<class 'str'>\n",
"<class 'int'>\n",
"<class 'float'>\n",
"<class 'complex'>\n",
"<class 'bool'>\n",
"<class 'list'>\n",
"<class 'dict'>\n",
"<class 'tuple'>\n",
"<class 'zip'>\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
}
Loading

0 comments on commit 833b61d

Please sign in to comment.