diff --git a/notebooks/beginner/images/truth_table.png b/notebooks/beginner/images/truth_table.png new file mode 100644 index 0000000..1163603 Binary files /dev/null and b/notebooks/beginner/images/truth_table.png differ diff --git a/notebooks/beginner/images/while.webp b/notebooks/beginner/images/while.webp new file mode 100644 index 0000000..f0190ee Binary files /dev/null and b/notebooks/beginner/images/while.webp differ diff --git a/notebooks/beginner/notebooks/1-basics.ipynb b/notebooks/beginner/notebooks/1-basics.ipynb new file mode 100644 index 0000000..a0627c0 --- /dev/null +++ b/notebooks/beginner/notebooks/1-basics.ipynb @@ -0,0 +1,122 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "print(\"Hello world\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#Python is case sensitive\n", + "#This will not work\n", + "Print(\"Hello world\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#Getting input\n", + "input('What is your name? ')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#Creating variables\n", + "x = 10\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "name = input('What is your name? ')\n", + "print(name)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# (letter|\"_\") (letter | digit | \"_\")*\n", + "\n", + "# This is OK\n", + "name_of_user = 'Jack'\n", + "\n", + "# This is OK\n", + "nameOfUser = 'Jack'\n", + "\n", + "# This is OK\n", + "p2p = 10" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#This is not OK\n", + "2tree = 100" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Variable Types\n", + "num = 10\n", + "print(type(num))\n", + "\n", + "text = \"Salam\"\n", + "print(type(text))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/notebooks/beginner/notebooks/classes.ipynb b/notebooks/beginner/notebooks/10-classes.ipynb similarity index 72% rename from notebooks/beginner/notebooks/classes.ipynb rename to notebooks/beginner/notebooks/10-classes.ipynb index b39b340..7e9dd27 100644 --- a/notebooks/beginner/notebooks/classes.ipynb +++ b/notebooks/beginner/notebooks/10-classes.ipynb @@ -13,12 +13,58 @@ "metadata": {}, "outputs": [], "source": [ - "class MyFirstClass:\n", - " def __init__(self, name):\n", - " self.name = name\n", + "class Person:\n", + " pass\n", "\n", - " def greet(self):\n", - " print('Hello {}!'.format(self.name))" + "p1 = Person()\n", + "p2 = Person()\n", + "\n", + "print(p1)\n", + "print(p2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "p1.name = 'John'\n", + "p2.name = 'David'\n", + "\n", + "print(p1.name)\n", + "print(p2.name)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "p1.age = 20\n", + "p2.age = 30\n", + "\n", + "print('{} is {} years old'.format(p1.name, p1.age))\n", + "print('{} is {} years old'.format(p2.name, p2.age))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Lets make a funtion for creating humans with name and age**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def set_person_name_and_age(person, name, age):\n", + " person.name = name\n", + " person.age = age" ] }, { @@ -27,10 +73,14 @@ "metadata": {}, "outputs": [], "source": [ - "my_instance = MyFirstClass('John Doe')\n", - "print('my_instance: {}'.format(my_instance))\n", - "print('type: {}'.format(type(my_instance)))\n", - "print('my_instance.name: {}'.format(my_instance.name))" + "joe = Person()\n", + "set_person_name_and_age(joe, 'Joe', 23)\n", + "\n", + "bob = Person()\n", + "set_person_name_and_age(bob, 'Bob', 40)\n", + "\n", + "print('{} is {} years old'.format(joe.name, joe.age))\n", + "print('{} is {} years old'.format(bob.name, bob.age))" ] }, { @@ -47,8 +97,26 @@ "metadata": {}, "outputs": [], "source": [ - "alice = MyFirstClass(name='Alice')\n", - "alice.greet()" + "class Person():\n", + " def set_name_and_age(self, name, age):\n", + " self.name = name\n", + " self.age = age" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "p1 = Person()\n", + "p1.set_name_and_age('John', 20)\n", + "\n", + "p2 = Person()\n", + "p2.set_name_and_age('David', 30)\n", + "\n", + "print('{} is {} years old'.format(p1.name, p1.age))\n", + "print('{} is {} years old'.format(p2.name, p2.age))" ] }, { @@ -65,13 +133,23 @@ "metadata": {}, "outputs": [], "source": [ - "class Example:\n", - " def __init__(self):\n", - " print('Now we are inside __init__')\n", - " \n", - "print('creating instance of Example')\n", - "example = Example()\n", - "print('instance created')" + "class Person:\n", + " def __init__(self, name, age):\n", + " self.name = name\n", + " self.age = age" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "p1 = Person('Joe', 40)\n", + "p2 = Person('David', 30)\n", + "\n", + "print('{} is {} years old'.format(p1.name, p1.age))\n", + "print('{} is {} years old'.format(p2.name, p2.age))" ] }, { @@ -87,16 +165,16 @@ "metadata": {}, "outputs": [], "source": [ - "class Example:\n", - " def __init__(self, var1, var2):\n", - " self.first_var = var1\n", - " self.second_var = var2\n", + "class Person:\n", + " def __init__(self, name, age):\n", + " self.name = name\n", + " self.age = age\n", " \n", - " def print_variables(self):\n", - " print('{} {}'.format(self.first_var, self.second_var))\n", + " def say_hello(self):\n", + " print('Hello Im {} and Im {} years old'.format(self.name, self.age))\n", " \n", - "e = Example('abc', 123)\n", - "e.print_variables()\n", + "p = Person('Jack', 25)\n", + "p.say_hello()\n", " " ] }, @@ -120,10 +198,10 @@ " self.age = age\n", " \n", " def __str__(self):\n", - " return 'Person: {}'.format(self.name)\n", + " return 'Person: {} with age {}'.format(self.name, self.age)\n", " \n", "jack = Person('Jack', 82)\n", - "print('This is the string presentation of jack: {}'.format(jack))" + "print(jack)" ] }, { @@ -220,7 +298,7 @@ "# Now you can do this:\n", "print(example_person.age)\n", "# But not this:\n", - "#example_person.age = 16" + "# example_person.age = 16 " ] }, { @@ -271,12 +349,13 @@ "\n", " @property\n", " def favorite_food(self):\n", - " return 'beef'\n", + " return '???'\n", "\n", "\n", "class Dog(Animal):\n", - " def greet(self):\n", - " print('wof wof')\n", + " @property\n", + " def favorite_food(self):\n", + " return 'beef'\n", "\n", "\n", "class Cat(Animal):\n", @@ -299,6 +378,13 @@ "cat.greet()\n", "print(\"Cat's favorite food is {}\".format(cat.favorite_food))" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -317,7 +403,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.4" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/notebooks/beginner/notebooks/testing1.ipynb b/notebooks/beginner/notebooks/11-testing1.ipynb similarity index 99% rename from notebooks/beginner/notebooks/testing1.ipynb rename to notebooks/beginner/notebooks/11-testing1.ipynb index 50a0bbb..cb02d0e 100644 --- a/notebooks/beginner/notebooks/testing1.ipynb +++ b/notebooks/beginner/notebooks/11-testing1.ipynb @@ -144,7 +144,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.4" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/notebooks/beginner/notebooks/file_io.ipynb b/notebooks/beginner/notebooks/12-file_io.ipynb similarity index 99% rename from notebooks/beginner/notebooks/file_io.ipynb rename to notebooks/beginner/notebooks/12-file_io.ipynb index 76c8ee2..79f90af 100644 --- a/notebooks/beginner/notebooks/file_io.ipynb +++ b/notebooks/beginner/notebooks/12-file_io.ipynb @@ -152,7 +152,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.4" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/notebooks/beginner/notebooks/exceptions.ipynb b/notebooks/beginner/notebooks/13-exceptions.ipynb similarity index 98% rename from notebooks/beginner/notebooks/exceptions.ipynb rename to notebooks/beginner/notebooks/13-exceptions.ipynb index 3fe2f0b..9985c86 100644 --- a/notebooks/beginner/notebooks/exceptions.ipynb +++ b/notebooks/beginner/notebooks/13-exceptions.ipynb @@ -145,16 +145,16 @@ "language_info": { "codemirror_mode": { "name": "ipython", - "version": 3.0 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.4" + "version": "3.6.7" } }, "nbformat": 4, - "nbformat_minor": 0 -} \ No newline at end of file + "nbformat_minor": 1 +} diff --git a/notebooks/beginner/notebooks/modules_and_packages.ipynb b/notebooks/beginner/notebooks/14-modules_and_packages.ipynb similarity index 100% rename from notebooks/beginner/notebooks/modules_and_packages.ipynb rename to notebooks/beginner/notebooks/14-modules_and_packages.ipynb diff --git a/notebooks/beginner/notebooks/debugging.ipynb b/notebooks/beginner/notebooks/15-debugging.ipynb similarity index 100% rename from notebooks/beginner/notebooks/debugging.ipynb rename to notebooks/beginner/notebooks/15-debugging.ipynb diff --git a/notebooks/beginner/notebooks/std_lib.ipynb b/notebooks/beginner/notebooks/16-std_lib.ipynb similarity index 100% rename from notebooks/beginner/notebooks/std_lib.ipynb rename to notebooks/beginner/notebooks/16-std_lib.ipynb diff --git a/notebooks/beginner/notebooks/testing2.ipynb b/notebooks/beginner/notebooks/17-testing2.ipynb similarity index 100% rename from notebooks/beginner/notebooks/testing2.ipynb rename to notebooks/beginner/notebooks/17-testing2.ipynb diff --git a/notebooks/beginner/notebooks/venv.ipynb b/notebooks/beginner/notebooks/18-venv.ipynb similarity index 100% rename from notebooks/beginner/notebooks/venv.ipynb rename to notebooks/beginner/notebooks/18-venv.ipynb diff --git a/notebooks/beginner/notebooks/project_structure.ipynb b/notebooks/beginner/notebooks/19-project_structure.ipynb similarity index 92% rename from notebooks/beginner/notebooks/project_structure.ipynb rename to notebooks/beginner/notebooks/19-project_structure.ipynb index f3b0878..010575c 100644 --- a/notebooks/beginner/notebooks/project_structure.ipynb +++ b/notebooks/beginner/notebooks/19-project_structure.ipynb @@ -23,17 +23,8 @@ "metadata": {}, "outputs": [], "source": [ - "# the content of my_script.py\n", - "\n", - "# imports\n", - "import logging\n", - "\n", - "# constants\n", - "LOGGER = logging.getLogger()\n", - "\n", - "\n", "def magical_function():\n", - " LOGGER.warning('We are about to do some magical stuff')\n", + " print(\"Magic!!!\")\n", "\n", "\n", "def main():\n", @@ -113,7 +104,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.4" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/notebooks/beginner/notebooks/numbers.ipynb b/notebooks/beginner/notebooks/2-numbers.ipynb similarity index 69% rename from notebooks/beginner/notebooks/numbers.ipynb rename to notebooks/beginner/notebooks/2-numbers.ipynb index 95f45c5..eaf2c27 100644 --- a/notebooks/beginner/notebooks/numbers.ipynb +++ b/notebooks/beginner/notebooks/2-numbers.ipynb @@ -14,6 +14,17 @@ "## `int`" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_int = 6\n", + "print(type(my_int))\n", + "print(my_int)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -23,7 +34,14 @@ "outputs": [], "source": [ "my_int = 6\n", - "print('value: {}, type: {}'.format(my_int, type(my_int)))" + "\n", + "x = 6 + 2\n", + "y = my_int * 2\n", + "z = my_int / 3\n", + "\n", + "print(x)\n", + "print(y)\n", + "print(z)" ] }, { @@ -33,16 +51,43 @@ "## `float`" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_float = 2.33\n", + "\n", + "print(type(my_float))\n", + "print(my_float)" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": { - "scrolled": true + "scrolled": false }, "outputs": [], "source": [ "my_float = float(my_int)\n", - "print('value: {}, type: {}'.format(my_float, type(my_float)))" + "\n", + "print(type(my_float))\n", + "print(my_float)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "f1 = 2.0\n", + "f2 = 2.\n", + "\n", + "print(f1)\n", + "print(f2)" ] }, { @@ -58,8 +103,18 @@ "metadata": {}, "outputs": [], "source": [ - "print(1 / 1)\n", - "print(6 / 5)" + "x = 1 / 1\n", + "print(type(x))\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(2 / 3)" ] }, { @@ -73,7 +128,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "scrolled": true + "scrolled": false }, "outputs": [], "source": [ @@ -82,6 +137,37 @@ "print(val)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Get numeric input" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_int = int(input(\"Enter an integer: \"))\n", + "\n", + "print(type(my_int))\n", + "print(my_int)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_float = float(input(\"Enter a float: \"))\n", + "\n", + "print(type(my_float))\n", + "print(my_float)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -196,7 +282,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.4" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/notebooks/beginner/notebooks/strings.ipynb b/notebooks/beginner/notebooks/3-strings.ipynb similarity index 88% rename from notebooks/beginner/notebooks/strings.ipynb rename to notebooks/beginner/notebooks/3-strings.ipynb index c87bba4..649c41c 100644 --- a/notebooks/beginner/notebooks/strings.ipynb +++ b/notebooks/beginner/notebooks/3-strings.ipynb @@ -13,7 +13,10 @@ "metadata": {}, "outputs": [], "source": [ - "my_string = 'Python is my favorite programming language!'" + "my_string1 = 'Python is my favorite programming language!'\n", + "my_string2 = \"Python is good\"\n", + "my_string3 = 'hjgfhjagdsj 12344 $$$ kk'\n", + "my_string4 = 'سلام'" ] }, { @@ -22,7 +25,10 @@ "metadata": {}, "outputs": [], "source": [ - "my_string" + "print(my_string1)\n", + "print(my_string2)\n", + "print(my_string3)\n", + "print(my_string4)" ] }, { @@ -31,7 +37,15 @@ "metadata": {}, "outputs": [], "source": [ - "type(my_string)" + "print(type(my_string1))\n", + "print(type(my_string2))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Get String length" ] }, { @@ -40,7 +54,10 @@ "metadata": {}, "outputs": [], "source": [ - "len(my_string)" + "print(len(my_string1))\n", + "print(len(my_string2))\n", + "print(len(my_string3))\n", + "print(len(my_string4))" ] }, { @@ -56,11 +73,11 @@ "metadata": {}, "outputs": [], "source": [ - "long_story = ('Lorem ipsum dolor sit amet, consectetur adipiscing elit.' \n", + "long_story = ('Lorem ipsum dolor sit amet, consectetur adipiscing elit.'\n", " 'Pellentesque eget tincidunt felis. Ut ac vestibulum est.' \n", " 'In sed ipsum sit amet sapien scelerisque bibendum. Sed ' \n", " 'sagittis purus eu diam fermentum pellentesque.')\n", - "long_story" + "print(long_story)" ] }, { @@ -74,7 +91,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If you don't know how it works, you can always check the `help`:" + "This will not modify `my_string` because replace is not done in-place." ] }, { @@ -83,14 +100,15 @@ "metadata": {}, "outputs": [], "source": [ - "help(str.replace)" + "my_string1.replace('a', '?')\n", + "print(my_string1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "This will not modify `my_string` because replace is not done in-place." + "You have to store the return value of `replace` instead." ] }, { @@ -99,15 +117,15 @@ "metadata": {}, "outputs": [], "source": [ - "my_string.replace('a', '?')\n", - "print(my_string)" + "my_modified_string = my_string1.replace('a', '?')\n", + "print(my_modified_string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "You have to store the return value of `replace` instead." + "If you don't know how it works, you can always check the `help`:" ] }, { @@ -116,8 +134,7 @@ "metadata": {}, "outputs": [], "source": [ - "my_modified_string = my_string.replace('is', 'will be')\n", - "print(my_modified_string)" + "help(str.replace)" ] }, { @@ -127,6 +144,16 @@ "## `str.format()`" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "num = int(input('Please Enter the number: '))\n", + "print('Your number is: {}'.format(num))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -378,7 +405,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.4" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/notebooks/beginner/notebooks/conditionals.ipynb b/notebooks/beginner/notebooks/4-conditionals.ipynb similarity index 63% rename from notebooks/beginner/notebooks/conditionals.ipynb rename to notebooks/beginner/notebooks/4-conditionals.ipynb index 3850f20..87939ba 100644 --- a/notebooks/beginner/notebooks/conditionals.ipynb +++ b/notebooks/beginner/notebooks/4-conditionals.ipynb @@ -27,7 +27,21 @@ "metadata": {}, "outputs": [], "source": [ - "print('type of True and False: {}'.format(type(True)))" + "t = True\n", + "f = False\n", + "\n", + "print(t)\n", + "print(f)\n", + "\n", + "print(type(t))\n", + "print(type(f))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Convert to bool" ] }, { @@ -36,9 +50,34 @@ "metadata": {}, "outputs": [], "source": [ - "print('0: {}, 1: {}'.format(bool(0), bool(1)))\n", - "print('empty list: {}, list with values: {}'.format(bool([]), bool(['woop'])))\n", - "print('empty dict: {}, dict with values: {}'.format(bool({}), bool({'Python': 'cool'})))" + "print(bool(1))\n", + "print(bool(0))\n", + "print(bool('Salam'))\n", + "print(bool(''))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## False Values in Python" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. False\n", + "\n", + "* None\n", + "\n", + "* zero of any numeric type, for example, 0, 0L, 0.0, 0j.\n", + "\n", + "* any empty sequence, for example, '', (), [].\n", + "\n", + "* any empty mapping, for example, {}.\n", + "\n", + "* instances of user-defined classes, if the class defines a __nonzero__() or __len__() method, when that method returns the integer zero or bool value False." ] }, { @@ -89,6 +128,13 @@ "### `and, or, not`" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, { "cell_type": "code", "execution_count": null, @@ -97,7 +143,6 @@ "source": [ "python_is_cool = True\n", "java_is_cool = False\n", - "empty_list = []\n", "secret_value = 3.14" ] }, @@ -117,8 +162,7 @@ "metadata": {}, "outputs": [], "source": [ - "print('Python or java is cool: {}'.format(python_is_cool or java_is_cool))\n", - "print('1 >= 1.1 or 2 < float(\"1.4\"): {}'.format(1 >= 1.1 or 2 < float('1.4')))" + "print('Python or java is cool: {}'.format(python_is_cool or java_is_cool))" ] }, { @@ -130,6 +174,22 @@ "print('Java is not cool: {}'.format(not java_is_cool))" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Boolean Operator precedence" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. **not**\n", + "2. **and**\n", + "3. **or**" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -143,8 +203,8 @@ "metadata": {}, "outputs": [], "source": [ - "print(bool(not java_is_cool or secret_value and python_is_cool or empty_list))\n", - "print(bool(not (java_is_cool or secret_value and python_is_cool or empty_list)))" + "print(not java_is_cool or secret_value and python_is_cool)\n", + "print(not (java_is_cool or secret_value and python_is_cool))" ] }, { @@ -160,12 +220,12 @@ "metadata": {}, "outputs": [], "source": [ - "statement = True\n", - "if statement:\n", - " print('statement is True')\n", + "condition = True\n", + "if condition:\n", + " print('condition is True')\n", " \n", - "if not statement:\n", - " print('statement is not True')" + "if not condition:\n", + " print('condition is not True')" ] }, { @@ -174,10 +234,10 @@ "metadata": {}, "outputs": [], "source": [ - "empty_list = []\n", - "# With if and elif, conversion to `bool` is implicit\n", - "if empty_list:\n", - " print('empty list will not evaluate to True') # this won't be executed" + "num = int(input('Enter an integer : '))\n", + "\n", + "if (num % 2 == 0):\n", + " print(\"The number is even\")" ] }, { @@ -191,6 +251,26 @@ " print('Value is positive and less than one or value is three')" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Implicit conversion to bool in if" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if 1:\n", + " print(\"bool(1) true so if body is executed\")\n", + "\n", + "if 0:\n", + " print(\"bool(0) is false so if body will neve execute\")" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -204,11 +284,12 @@ "metadata": {}, "outputs": [], "source": [ - "my_dict = {}\n", - "if my_dict:\n", - " print('there is something in my dict')\n", + "num = int(input('Enter an integer : '))\n", + "\n", + "if (num % 2 == 0):\n", + " print('The number is even')\n", "else:\n", - " print('my dict is empty :(')" + " print('The number in odd')" ] }, { @@ -224,13 +305,13 @@ "metadata": {}, "outputs": [], "source": [ - "val = 88\n", - "if val >= 100:\n", + "val = 9\n", + "if val > 100:\n", " print('value is equal or greater than 100')\n", - "elif val > 10:\n", - " print('value is greater than 10 but less than 100')\n", + "elif val < 10:\n", + " print('value is less than 10')\n", "else:\n", - " print('value is equal or less than 10')" + " print('the is between 10 and 100')" ] }, { @@ -246,10 +327,15 @@ "metadata": {}, "outputs": [], "source": [ - "greeting = 'Hello fellow Pythonista!'\n", - "language = 'Italian'\n", + "greeting = ''\n", "\n", - "if language == 'Swedish':\n", + "language = input(\"Enter the language: \")\n", + "\n", + "if language == 'Farsi':\n", + " greeting = 'درود!'\n", + "elif language == 'English':\n", + " greeting = 'Hello!'\n", + "elif language == 'Swedish':\n", " greeting = 'Hejsan!'\n", "elif language == 'Finnish':\n", " greeting = 'Latua perkele!'\n", @@ -257,6 +343,11 @@ " greeting = 'Hola!'\n", "elif language == 'German':\n", " greeting = 'Guten Tag!'\n", + "elif language == 'Italian':\n", + " greeting = 'Hello fellow Pythonista!'\n", + "else:\n", + " greeting = 'Can not recognize language!'\n", + " \n", " \n", "print(greeting)" ] @@ -285,7 +376,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.4" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/notebooks/beginner/notebooks/lists.ipynb b/notebooks/beginner/notebooks/5-lists.ipynb similarity index 88% rename from notebooks/beginner/notebooks/lists.ipynb rename to notebooks/beginner/notebooks/5-lists.ipynb index 0a08876..b7b5ee9 100644 --- a/notebooks/beginner/notebooks/lists.ipynb +++ b/notebooks/beginner/notebooks/5-lists.ipynb @@ -25,7 +25,26 @@ "source": [ "list_of_ints = [1, 2, 6, 7]\n", "list_of_misc = [0.2, 5, 'Python', 'is', 'still fun', '!']\n", - "print('lengths: {} and {}'.format(len(list_of_ints), len(list_of_misc)))" + "\n", + "print(list_of_ints)\n", + "print(list_of_misc)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Getting length of a list" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_list = [1, 11 ,100 , 23]\n", + "print(len(my_list))" ] }, { @@ -121,8 +140,12 @@ "outputs": [], "source": [ "original = [1, 2, 3]\n", + "\n", "modified = original\n", + "\n", + "#changing this will affect original\n", "modified[0] = 99\n", + "\n", "print('original: {}, modified: {}'.format(original, modified))" ] }, @@ -147,6 +170,25 @@ "print('original: {}, modified: {}'.format(original, modified))" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Empty list is False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "empty_list = []\n", + "# With if and elif, conversion to `bool` is implicit\n", + "if empty_list:\n", + " print('empty list will not evaluate to True') # this won't be executed" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -310,7 +352,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.4" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/notebooks/beginner/notebooks/dictionaries.ipynb b/notebooks/beginner/notebooks/6-dictionaries.ipynb similarity index 93% rename from notebooks/beginner/notebooks/dictionaries.ipynb rename to notebooks/beginner/notebooks/6-dictionaries.ipynb index f1b4f39..f8864ca 100644 --- a/notebooks/beginner/notebooks/dictionaries.ipynb +++ b/notebooks/beginner/notebooks/6-dictionaries.ipynb @@ -18,6 +18,19 @@ "print('dict: {}, type: {}'.format(my_empty_dict, type(my_empty_dict)))" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "d = {'cat' : 'گربه', 'dog' : 'سگ'}\n", + "print(d)\n", + "\n", + "print(d['cat'])\n", + "print(d['dog'])" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -93,7 +106,7 @@ "metadata": {}, "outputs": [], "source": [ - "# print(my_dict['nope'])" + "print(my_dict['nope'])" ] }, { @@ -138,7 +151,7 @@ "my_other_dict = my_dict\n", "my_other_dict['carrot'] = 'super tasty'\n", "my_other_dict['sausage'] = 'best ever'\n", - "print('my_dict: {}\\nother: {}'.format(my_dict, my_other_dict))\n", + "print('my_dict: {} \\n other: {}'.format(my_dict, my_other_dict))\n", "print('equal: {}'.format(my_dict == my_other_dict))" ] }, @@ -157,8 +170,8 @@ "source": [ "my_dict = {'ham': 'good', 'carrot': 'semi good'}\n", "my_other_dict = dict(my_dict)\n", - "my_other_dict['beer'] = 'decent'\n", - "print('my_dict: {}\\nother: {}'.format(my_dict, my_other_dict))\n", + "my_other_dict['beer'] = 'bad'\n", + "print('my_dict: {} \\n other: {}'.format(my_dict, my_other_dict))\n", "print('equal: {}'.format(my_dict == my_other_dict))" ] }, @@ -241,13 +254,17 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [], "source": [ "dict1 = {'a': 1, 'b': 2}\n", "dict2 = {'c': 3}\n", "dict1.update(dict2)\n", + "\n", "print(dict1)\n", + "print(dict2)\n", "\n", "# If they have same keys:\n", "dict1.update({'c': 4})\n", @@ -312,7 +329,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.4" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/notebooks/beginner/notebooks/7-while_loop.ipynb b/notebooks/beginner/notebooks/7-while_loop.ipynb new file mode 100644 index 0000000..2e1e515 --- /dev/null +++ b/notebooks/beginner/notebooks/7-while_loop.ipynb @@ -0,0 +1,114 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# [`While` loop](https://docs.python.org/3/reference/compound_stmts.html#while)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```python\n", + "while condition :\n", + " code\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "num = int(input(\"Enter the number: \"))\n", + "\n", + "tmp = num\n", + "sum_of_numbers = 0\n", + "\n", + "while tmp > 0 :\n", + " sum_of_numbers = sum_of_numbers + tmp\n", + " tmp = tmp - 1\n", + "\n", + "print(\"The sum of numbers from 1 to {} is : {}\".format(num, sum_of_numbers))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## break" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "i = 0\n", + "\n", + "while True :\n", + " i = i + 1\n", + " print(i)\n", + " \n", + " if i == 10 :\n", + " break" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## continue" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "i = 0\n", + "while i < 10 :\n", + " i = i + 1\n", + " \n", + " if i == 5 :\n", + " print('Continue')\n", + " continue\n", + " \n", + " print(i)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/notebooks/beginner/notebooks/for_loops.ipynb b/notebooks/beginner/notebooks/8-for_loops.ipynb similarity index 92% rename from notebooks/beginner/notebooks/for_loops.ipynb rename to notebooks/beginner/notebooks/8-for_loops.ipynb index a61c1c1..323b972 100644 --- a/notebooks/beginner/notebooks/for_loops.ipynb +++ b/notebooks/beginner/notebooks/8-for_loops.ipynb @@ -97,8 +97,8 @@ "outputs": [], "source": [ "my_dict = {'hacker': True, 'age': 72, 'name': 'John Doe'}\n", - "for val in my_dict:\n", - " print(val)" + "for key in my_dict:\n", + " print(key)" ] }, { @@ -118,6 +118,16 @@ "## `range()`" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "zero_to_nine = range(0, 10)\n", + "print(zero_to_nine[1])" + ] + }, { "cell_type": "code", "execution_count": null, @@ -165,7 +175,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.4" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/notebooks/beginner/notebooks/functions.ipynb b/notebooks/beginner/notebooks/9-functions.ipynb similarity index 94% rename from notebooks/beginner/notebooks/functions.ipynb rename to notebooks/beginner/notebooks/9-functions.ipynb index ca00f31..801f19a 100644 --- a/notebooks/beginner/notebooks/functions.ipynb +++ b/notebooks/beginner/notebooks/9-functions.ipynb @@ -16,8 +16,6 @@ "def my_first_function():\n", " print('Hello world!')\n", "\n", - "print('type: {}'.format(my_first_function))\n", - "\n", "my_first_function() # Calling a function" ] }, @@ -46,14 +44,14 @@ "metadata": {}, "outputs": [], "source": [ - "# Function with return value\n", - "def strip_and_lowercase(original):\n", - " modified = original.strip().lower()\n", - " return modified\n", + "def is_even(num):\n", + " if num % 2 == 0:\n", + " return True\n", + " else:\n", + " return False\n", "\n", - "uggly_string = ' MixED CaSe '\n", - "pretty = strip_and_lowercase(uggly_string)\n", - "print('pretty: {}'.format(pretty))" + "print(is_even(20))\n", + "print(is_even(11))" ] }, { @@ -246,7 +244,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.4" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/notebooks/intermediate/notebooks/idiomatic_misc2.ipynb b/notebooks/intermediate/notebooks/idiomatic_misc2.ipynb index 08a48d5..5c6c474 100644 --- a/notebooks/intermediate/notebooks/idiomatic_misc2.ipynb +++ b/notebooks/intermediate/notebooks/idiomatic_misc2.ipynb @@ -541,7 +541,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.4" + "version": "3.6.7" } }, "nbformat": 4,