{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"init_cell": true,
"slideshow": {
"slide_type": "skip"
},
"tags": [
"hide-input"
]
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%html\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": [
"remove-cell"
]
},
"source": [
"---\n",
"license:\n",
" code: MIT\n",
" content: CC-BY-4.0\n",
"github: https://github.com/ocademy-ai/machine-learning\n",
"venue: By Ocademy\n",
"open_access: true\n",
"bibliography:\n",
" - https://raw.githubusercontent.com/ocademy-ai/machine-learning/main/open-machine-learning-jupyter-book/references.bib\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {
"notebookRunGroups": {
"groupValue": ""
},
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Python programming introduction"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## 1. What's Python?\n",
"\n",
"- Python is a high-level programming language for general-purpose programming.\n",
"- Python is open-source.\n",
"- Python was created by a Dutch programmer, Guido van Rossum.\n",
"- The first version was released on February 20, 1991.\n",
"\n",
"\n",
"

\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "center",
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## 2. Why Python?\n",
"\n",
"- Easy to learn.\n",
"- Well adopted.\n",
"- Multipurpose, e.g. web development, software development, mathematics, system scripting.\n",
"- Dominate data engineering and Machine Learning.\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "center",
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## 3. Basic Python \n",
"\n",
"- A Python script can be written in Python interactive shell or the code editor. \n",
"- A Python file has an extension `.py`."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Indentation\n",
"\n",
"An indentation is a white space in a text. Indentation in many languages is used to increase code readability, however, Python uses indentation to create blocks of codes."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"tags": [
"raises-exception"
]
},
"outputs": [],
"source": [
"# incorrect\n",
"def a():\n",
" print(\"a\")\n",
"\n",
"# correct\n",
"def b():\n",
" print(\"b\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Comments\n",
"\n",
"Comments are very important to make the code more readable and to leave remarks in our code. Python does not run comment parts of our code. \n",
"\n",
"**Example: Single Line Comment**\n",
"\n",
"```py\n",
"# This is the first comment\n",
"# This is the second comment\n",
"# Python is eating the world\n",
"```\n",
"\n",
"**Example: Multiline Comment**\n",
"\n",
"```py\n",
"\"\"\"This is multiline comment\n",
"multiline comment takes multiple lines.\n",
"Python is eating the world\n",
"\"\"\"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "split",
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Data types\n",
"\n",
"#### Number\n",
"\n",
"- Integer: integer(negative, zero and positive) numbers.\n",
" Example: ... -3, -2, -1, 0, 1, 2, 3 ...\n",
"- Float: decimal number.\n",
" Example: ... -3.5, -2.25, -1.0, 0.0, 1.1, 2.2, 3.5 ...\n",
"- Complex.®\n",
" Example: 1 + j, 2 + 4j\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"cell_style": "split",
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"#### String\n",
"\n",
"A collection of one or more characters under a single or double quote. If a string is more than one sentence then we use a triple quote.\n",
"\n",
"```py\n",
"'Asabeneh'\n",
"'Finland'\n",
"'Python'\n",
"'I love teaching'\n",
"'I hope you are enjoying the first day of Ocademy Challenge'\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"#### Booleans\n",
"\n",
"```Python\n",
"True # Is the light on? If it is on, then the value is True\n",
"False # I\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"#### List\n",
"\n",
"Python list is an ordered collection that allows to store items of different data types. A list is similar to an array in JavaScript.\n",
"\n",
"**Example:**\n",
"\n",
"```py\n",
"[0, 1, 2, 3, 4, 5] # all are the same data types - a list of numbers\n",
"['Banana', 'Orange', 'Mango', 'Avocado'] # all the same data types - a list of strings (fruits)\n",
"['Finland','Estonia', 'Sweden','Norway'] # all the same data types - a list of strings (countries)\n",
"['Banana', 10, False, 9.81] # different data types in the list - string, integer, boolean and float\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"#### Dictionary\n",
"\n",
"A Python dictionary object is an unordered collection of data in a key-value pair format.\n",
"\n",
"**Example:**\n",
"\n",
"```py\n",
"{\n",
" 'first_name':'Asabeneh',\n",
" 'last_name':'Yetayeh',\n",
" 'country':'Finland', \n",
" 'age':250, \n",
" 'is_married':True,\n",
" 'skills':['JS', 'React', 'Node', 'Python']\n",
"}\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"#### Tuple\n",
"\n",
"A tuple is an ordered collection of different data types like a list, but tuples can not be modified once they are created. They are immutable.\n",
"\n",
"**Example:**\n",
"\n",
"```py\n",
"('Asabeneh', 'Pawel', 'Brook', 'Abraham', 'Lidiya') # Names\n",
"```\n",
"\n",
"```py\n",
"('Earth', 'Jupiter', 'Neptune', 'Mars', 'Venus', 'Saturn', 'Uranus', 'Mercury') # planets\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"#### Set\n",
"\n",
"A set is a collection of data types similar to a list and tuple. Unlike the list and the tuple, a set is not an ordered collection of items. Like in Mathematics, set in Python only stores unique items. In later sections, we will go into detail about every Python data type.\n",
"\n",
"**Example:**\n",
"\n",
"```py\n",
"{2, 4, 3, 5}\n",
"{3.14, 9.81, 2.7} # order is not important in set\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Checking data types\n",
"\n",
"To check the data type of certain data/variables we use the `type` function. \n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(\"abc\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(1)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type([1, 2, 3])"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Python file\n",
"\n",
"A sample Python file `helloworld.py`.\n",
"\n",
"```py\n",
"print(2 + 3) # addition(+)\n",
"print(3 - 1) # subtraction(-)\n",
"print(2 * 3) # multiplication(*)\n",
"print(3 / 2) # division(/)\n",
"print(3 ** 2) # exponential(**)\n",
"print(3 % 2) # modulus(%)\n",
"print(3 // 2) # Floor division operator(//)\n",
"\n",
"# Checking data types\n",
"print(type(10)) # Int\n",
"print(type(3.14)) # Float\n",
"print(type(1 + 3j)) # Complex number\n",
"print(type('Asabeneh')) # String\n",
"print(type([1, 2, 3])) # List\n",
"print(type({'name':'Asabeneh'})) # Dictionary\n",
"print(type({9.8, 3.14, 2.7})) # Set\n",
"print(type((9.8, 3.14, 2.7))) # Tuple\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Python file\n"
]
},
{
"cell_type": "raw",
"metadata": {
"tags": [
"hide-input"
]
},
"source": [
"5\n",
"2\n",
"6\n",
"1.5\n",
"9\n",
"1\n",
"1\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## 4. Your turn! 🚀\n",
"\n",
"[Python programming introduction - Your turn](https://ocademy-ai.github.io/machine-learning/prerequisites/python-programming-introduction.html#your-turn)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## 5. References\n",
"\n",
"1. [Python programming introduction](https://ocademy-ai.github.io/machine-learning/prerequisites/python-programming-introduction.html#)"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"init_cell": "run_on_kernel_ready",
"jupytext": {
"formats": "ipynb"
},
"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.18"
},
"rise": {
"autolaunch": true,
"chalkboard": {
"color": [
"rgb(250, 250, 250)",
"rgb(250, 250, 250)"
]
},
"enable_chalkboard": true,
"header": "",
"scroll": true
},
"vscode": {
"interpreter": {
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}