diff --git a/00-Python Object and Data Structure Basics/04-Lists.ipynb b/00-Python Object and Data Structure Basics/04-Lists.ipynb index 332c404..98252e7 100644 --- a/00-Python Object and Data Structure Basics/04-Lists.ipynb +++ b/00-Python Object and Data Structure Basics/04-Lists.ipynb @@ -108,7 +108,7 @@ } ], "source": [ - "# Grab element at index 0\n", + "# Grab element at index 0 -- python is a zero index programming language\n", "my_list[0]" ] }, diff --git a/02-Python Statements/01-Introduction to Python Statements.ipynb b/02-Python Statements/01-Introduction to Python Statements.ipynb index 4de998b..8e51e00 100644 --- a/02-Python Statements/01-Introduction to Python Statements.ipynb +++ b/02-Python Statements/01-Introduction to Python Statements.ipynb @@ -117,7 +117,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/02-Python Statements/02-if, elif, and else Statements.ipynb b/02-Python Statements/02-if, elif, and else Statements.ipynb index 262bd64..f64261d 100644 --- a/02-Python Statements/02-if, elif, and else Statements.ipynb +++ b/02-Python Statements/02-if, elif, and else Statements.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To control the flow of logic:\n", + " use\n", + " 1- if\n", + " 2- elif\n", + " 3- else" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -49,10 +60,98 @@ } ], "source": [ + "#if and elif: need condition but else doesn't need condition\n", + "#if -else-elif used to control flow\n", + "#if the dog is hunger: go to feed\n", "if True:\n", " print('It was true!')" ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "It's true!!\n" + ] + } + ], + "source": [ + "if True:\n", + " print (\"It's true!!\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True!!\n" + ] + } + ], + "source": [ + "if 3>2:\n", + " print ('True!!')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I am hungery .. Feed me!\n" + ] + } + ], + "source": [ + "hungery = True\n", + "\n", + "if hungery:\n", + " print ('I am hungery .. Feed me!')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I am not hungery\n" + ] + } + ], + "source": [ + "hungery = False\n", + "\n", + "if hungery:\n", + " print ('Feed me!!')\n", + "else:\n", + " print('I am not hungery')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -97,24 +196,27 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Welcome to the bank!\n" + "Welcome to the store!\n" ] } ], "source": [ - "loc = 'Bank'\n", + "# elif: check multiple conditions\n", + "loc = 'Store'\n", "\n", "if loc == 'Auto Shop':\n", " print('Welcome to the Auto Shop!')\n", "elif loc == 'Bank':\n", " print('Welcome to the bank!')\n", + "elif loc =='Store':\n", + " print('Welcome to the store!')\n", "else:\n", " print('Where are you?')" ] @@ -152,19 +254,19 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Welcome George!\n" + "Welcome, what's your name?\n" ] } ], "source": [ - "person = 'George'\n", + "person = 'Abdel'\n", "\n", "if person == 'Sammy':\n", " print('Welcome Sammy!')\n", @@ -200,7 +302,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/02-Python Statements/03-for Loops.ipynb b/02-Python Statements/03-for Loops.ipynb index 61f54fe..e2aa410 100644 --- a/02-Python Statements/03-for Loops.ipynb +++ b/02-Python Statements/03-for Loops.ipynb @@ -6,7 +6,7 @@ "source": [ "# for Loops\n", "\n", - "A for loop acts as an iterator in Python; it goes through items that are in a *sequence* or any other iterable item. Objects that we've learned about that we can iterate over include strings, lists, tuples, and even built-in iterables for dictionaries, such as keys or values.\n", + "A for loop acts as an iterator in Python; it goes through items that are in a *sequence* or any other iterable item. Objects that we've learned about that we can iterate over include strings, lists, tuples, and even built-in iterables for dictionaries, such as keys or values.\n", "\n", "We've already seen the for statement a little bit in past lectures but now let's formalize our understanding.\n", "\n", @@ -31,17 +31,18 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# We'll learn how to automate this sort of list in the next lecture\n", + "\n", "list1 = [1,2,3,4,5,6,7,8,9,10]" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -62,10 +63,302 @@ } ], "source": [ + "#iterate: character in string - item in a list - key in a dictionary \n", "for num in list1:\n", " print(num)" ] }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n" + ] + } + ], + "source": [ + "for i in list1: #your can use _ as item name\n", + " print ('Hello, Alkahwaji!')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n", + "Hello, Alkahwaji!\n" + ] + } + ], + "source": [ + "for _ in list1: #your can use _ as item name\n", + " print ('Hello, Alkahwaji!')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "4\n", + "6\n", + "8\n", + "10\n" + ] + } + ], + "source": [ + "#print the even number from list1\n", + "for num in list1:\n", + " #check for even\n", + " if num %2==0:\n", + " print (num)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Odd Number!!:1\n", + "2\n", + "Odd Number!!:3\n", + "4\n", + "Odd Number!!:5\n", + "6\n", + "Odd Number!!:7\n", + "8\n", + "Odd Number!!:9\n", + "10\n" + ] + } + ], + "source": [ + "#print the even number from list1 - with else statement\n", + "for num in list1:\n", + " #check for even\n", + " if num %2==0:\n", + " print (num)\n", + " else:\n", + " print ('Odd Number!!:%s'%num)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Odd Number!!: 1\n", + "2\n", + "Odd Number!!: 3\n", + "4\n", + "Odd Number!!: 5\n", + "6\n", + "Odd Number!!: 7\n", + "8\n", + "Odd Number!!: 9\n", + "10\n" + ] + } + ], + "source": [ + "#print the even number from list1 - with else statement\n", + "for num in list1:\n", + " #check for even\n", + " if num %2==0:\n", + " print (num)\n", + " else:\n", + " print (f'Odd Number!!: {num}')" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "3\n", + "6\n", + "10\n", + "15\n", + "21\n", + "28\n", + "36\n", + "45\n", + "55\n", + "The sum of list is 55\n" + ] + } + ], + "source": [ + "list_sum = 0\n", + "\n", + "for i in list1:\n", + " list_sum+=i\n", + " print (list_sum)\n", + " \n", + "print (f\"The sum of list is {list_sum}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "h\n", + "e\n", + "l\n", + "l\n", + "o\n", + " \n", + "w\n", + "o\n", + "r\n", + "l\n", + "d\n" + ] + } + ], + "source": [ + "mystr = 'hello world'\n", + "\n", + "for letter in mystr:\n", + " print (letter)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "h\n", + "e\n", + "l\n", + "l\n", + "o\n", + " \n", + "w\n", + "o\n", + "r\n", + "l\n", + "d\n" + ] + } + ], + "source": [ + "for letter in 'hello world':\n", + " print (letter)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "tup = (1,2,3)\n", + "for item in tup:\n", + " print (item)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cool!!\n", + "Cool!!\n", + "Cool!!\n", + "Cool!!\n", + "Cool!!\n" + ] + } + ], + "source": [ + "#_ as a place holder -- we don't need a variable\n", + "for _ in range(5):\n", + " print('Cool!!')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -382,16 +675,36 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ - "list2 = [(2,4),(6,8),(10,12)]" + "list2 = [(2,4),(6,8),(10,12),(100,101)]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(list2)" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -400,7 +713,8 @@ "text": [ "(2, 4)\n", "(6, 8)\n", - "(10, 12)\n" + "(10, 12)\n", + "(100, 101)\n" ] } ], @@ -419,15 +733,41 @@ "output_type": "stream", "text": [ "2\n", + "4\n", "6\n", - "10\n" + "8\n", + "10\n", + "12\n", + "100\n", + "101\n" ] } ], "source": [ - "# Now with unpacking!\n", + "# Now with unpacking! remove brackets t1,t2 instead of (t1,t2)\n", "for (t1,t2) in list2:\n", - " print(t1)" + " print(t1)\n", + " print(t2)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "6\n", + "10\n" + ] + } + ], + "source": [ + "for a,b in list2: #for 2 or more tuples\n", + " print (a)" ] }, { @@ -446,7 +786,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 34, "metadata": {}, "outputs": [], "source": [ @@ -455,7 +795,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 35, "metadata": {}, "outputs": [ { @@ -469,10 +809,106 @@ } ], "source": [ + "#Iterate through dictionary :: OUTPUT IS KEY\n", "for item in d:\n", " print(item)" ] }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('k1', 1)\n", + "('k2', 2)\n", + "('k3', 3)\n" + ] + } + ], + "source": [ + "#Iterate through items\n", + "for item in d.items():\n", + " print (item) #The output is tuple" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "k1\n", + "1\n", + "k2\n", + "2\n", + "k3\n", + "3\n" + ] + } + ], + "source": [ + "#Iterate through items\n", + "for k,v in d.items():\n", + " print (k)\n", + " print(v)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "#Iterate through values\n", + "for val in d.values():\n", + " print(val)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "k1\n", + "k2\n", + "k3\n" + ] + } + ], + "source": [ + "#Iterate through keys\n", + "for kk in d.keys():\n", + " print(kk)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -619,7 +1055,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/02-Python Statements/04-while Loops.ipynb b/02-Python Statements/04-while Loops.ipynb index f8ff559..ebfcca8 100644 --- a/02-Python Statements/04-while Loops.ipynb +++ b/02-Python Statements/04-while Loops.ipynb @@ -8,11 +8,11 @@ "source": [ "# while Loops\n", "\n", - "The while statement in Python is one of most general ways to perform iteration. A while statement will repeatedly execute a single statement or group of statements as long as the condition is true. The reason it is called a 'loop' is because the code statements are looped through over and over again until the condition is no longer met.\n", + "The while statement in Python is one of most general ways to perform iteration. A while statement will repeatedly execute a single statement or group of statements as long as the condition is true . The reason it is called a 'loop' is because the code statements are looped through over and over again until the condition is no longer met.\n", "\n", "The general format of a while loop is:\n", "\n", - " while test:\n", + " while test (Boolean condition):\n", " code statements\n", " else:\n", " final code statements\n", @@ -22,32 +22,32 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "x is currently: 0\n", + "x is currently: 0\n", " x is still less than 10, adding 1 to x\n", - "x is currently: 1\n", + "x is currently: 1\n", " x is still less than 10, adding 1 to x\n", - "x is currently: 2\n", + "x is currently: 2\n", " x is still less than 10, adding 1 to x\n", - "x is currently: 3\n", + "x is currently: 3\n", " x is still less than 10, adding 1 to x\n", - "x is currently: 4\n", + "x is currently: 4\n", " x is still less than 10, adding 1 to x\n", - "x is currently: 5\n", + "x is currently: 5\n", " x is still less than 10, adding 1 to x\n", - "x is currently: 6\n", + "x is currently: 6\n", " x is still less than 10, adding 1 to x\n", - "x is currently: 7\n", + "x is currently: 7\n", " x is still less than 10, adding 1 to x\n", - "x is currently: 8\n", + "x is currently: 8\n", " x is still less than 10, adding 1 to x\n", - "x is currently: 9\n", + "x is currently: 9\n", " x is still less than 10, adding 1 to x\n" ] } @@ -56,7 +56,7 @@ "x = 0\n", "\n", "while x < 10:\n", - " print('x is currently: ',x)\n", + " print(f'x is currently: {x}')\n", " print(' x is still less than 10, adding 1 to x')\n", " x+=1" ] @@ -70,7 +70,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -87,30 +87,21 @@ " x is still less than 10, adding 1 to x\n", "x is currently: 4\n", " x is still less than 10, adding 1 to x\n", - "x is currently: 5\n", - " x is still less than 10, adding 1 to x\n", - "x is currently: 6\n", - " x is still less than 10, adding 1 to x\n", - "x is currently: 7\n", - " x is still less than 10, adding 1 to x\n", - "x is currently: 8\n", - " x is still less than 10, adding 1 to x\n", - "x is currently: 9\n", - " x is still less than 10, adding 1 to x\n", - "All Done!\n" + "All Done! -- X is not less than 5\n" ] } ], "source": [ + "#We can use else with while.\n", "x = 0\n", "\n", - "while x < 10:\n", + "while x < 5:\n", " print('x is currently: ',x)\n", " print(' x is still less than 10, adding 1 to x')\n", " x+=1\n", " \n", "else:\n", - " print('All Done!')" + " print('All Done! -- X is not less than 5')" ] }, { @@ -141,6 +132,90 @@ "Let's go ahead and look at some examples!" ] }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "x = [1,2,3]\n", + "\n", + "for item in x:\n", + " pass #as a filler -- pass: do nothing" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "S\n", + "m\n", + "m\n", + "y\n" + ] + } + ], + "source": [ + "mystr = 'Sammy'\n", + "for letter in mystr:\n", + " if letter =='a':\n", + " continue # the top of the closest enclosing loop.\n", + " #the letter in mystr loop\n", + " print (letter)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "S\n", + "a\n" + ] + } + ], + "source": [ + "mystr = 'Sammy'\n", + "for letter in mystr:\n", + " if letter =='m':\n", + " break # the current closest enclosing loop.\n", + " #the letter == a loop\n", + " print (letter)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n" + ] + } + ], + "source": [ + "x = 0\n", + "while x <5:\n", + " if x ==3:\n", + " break\n", + " print (x)\n", + " x+=1" + ] + }, { "cell_type": "code", "execution_count": 3, @@ -288,7 +363,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/02-Python Statements/05-Useful-Operators.ipynb b/02-Python Statements/05-Useful-Operators.ipynb index 632406a..ae7bd37 100644 --- a/02-Python Statements/05-Useful-Operators.ipynb +++ b/02-Python Statements/05-Useful-Operators.ipynb @@ -38,6 +38,58 @@ "range(0,11)" ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n" + ] + } + ], + "source": [ + "for num in range(10):\n", + " print (num)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "6\n", + "9\n", + "12\n", + "15\n", + "18\n" + ] + } + ], + "source": [ + "for i in range(3,20,3): #start, stop, step\n", + " print (i)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -141,7 +193,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -195,6 +247,78 @@ " print(\"At index {} the letter is {}\".format(i,letter))" ] }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(0, 'a')\n", + "(1, 'b')\n", + "(2, 'c')\n", + "(3, 'd')\n", + "(4, 'e')\n", + "(5, 'f')\n" + ] + } + ], + "source": [ + "#output is tuple\n", + "word = 'abcdef'\n", + "\n", + "for item in enumerate(word):\n", + " print (item)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "a\n", + "\n", + "\n", + "1\n", + "b\n", + "\n", + "\n", + "2\n", + "c\n", + "\n", + "\n", + "3\n", + "d\n", + "\n", + "\n", + "4\n", + "e\n", + "\n", + "\n", + "5\n", + "f\n", + "\n", + "\n" + ] + } + ], + "source": [ + "#output is tuple\n", + "word = 'abcdef'\n", + "\n", + "for index, letter in enumerate(word):\n", + " print (index)\n", + " print(letter)\n", + " print ('\\n')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -233,10 +357,8 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": true - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "mylist1 = [1,2,3,4,5]\n", @@ -261,6 +383,7 @@ ], "source": [ "# This one is also a generator! We will explain this later, but for now let's transform it to a list\n", + "#The opposite of enumerate -- To the shortest list\n", "zip(mylist1,mylist2)" ] }, @@ -293,7 +416,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -362,6 +485,87 @@ "'x' in [1,2,3]" ] }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'a' in 'a world '" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'mykey' in {'mykey':100}" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d = {'yourkey':356}\n", + "310 in d.keys()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "356 in d.values()" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -433,10 +637,8 @@ }, { "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": true - }, + "execution_count": 22, + "metadata": {}, "outputs": [], "source": [ "from random import shuffle" @@ -444,10 +646,17 @@ }, { "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": true - }, + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "mylist = [10,20,30,40,100]" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, "outputs": [], "source": [ "# This shuffles the list \"in-place\" meaning it won't return\n", @@ -457,16 +666,16 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[40, 10, 100, 30, 20]" + "[40, 10, 20, 100, 30]" ] }, - "execution_count": 36, + "execution_count": 29, "metadata": {}, "output_type": "execute_result" } @@ -561,6 +770,83 @@ "source": [ "input('Enter Something into this box: ')" ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ente a number here: 10\n" + ] + } + ], + "source": [ + "result = input('Ente a number here: ')" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'10'" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "int_res = int(result)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "print(int_res)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -579,7 +865,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/02-Python Statements/06-List Comprehensions.ipynb b/02-Python Statements/06-List Comprehensions.ipynb index 256271b..bc52192 100644 --- a/02-Python Statements/06-List Comprehensions.ipynb +++ b/02-Python Statements/06-List Comprehensions.ipynb @@ -19,6 +19,78 @@ "execution_count": 1, "metadata": {}, "outputs": [], + "source": [ + "mystr = 'hello'" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "mylist = []\n", + "\n", + "for letter in mystr:\n", + " mylist.append(letter)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['h', 'e', 'l', 'l', 'o']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mylist" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "#[item for item in the iterate list or object]\n", + "#if you want to form operation form it in the first item\n", + "mylist1 = [letter for letter in 'hello_world']" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['h', 'e', 'l', 'l', 'o', '_', 'w', 'o', 'r', 'l', 'd']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mylist1" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], "source": [ "# Grab every letter in string\n", "lst = [x for x in 'word']" @@ -26,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -35,7 +107,7 @@ "['w', 'o', 'r', 'd']" ] }, - "execution_count": 2, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -45,6 +117,56 @@ "lst" ] }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "num = [n for n in range(5)]" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "num" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mylist3 = [x for x in range(0,11)]\n", + "mylist3" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -57,17 +179,17 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ - "# Square numbers in range and turn into list\n", + "# Square numbers in range and turn into list >> form operations in the first item\n", "lst = [x**2 for x in range(0,11)]" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -76,7 +198,7 @@ "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" ] }, - "execution_count": 4, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -99,7 +221,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Check for even numbers in a range\n", + "# Check for even numbers in a range \n", "lst = [x for x in range(11) if x % 2 == 0]" ] }, @@ -123,6 +245,63 @@ "lst" ] }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "even = [x if x%2 == 0 else 'ODD' for x in range (0,14)]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 'ODD', 2, 'ODD', 4, 'ODD', 6, 'ODD', 8, 'ODD', 10, 'ODD', 12, 'ODD']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "even" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "ll = [xx if xx%2==0 else 'This is an ODD number!!' for xx in range(11)]" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 'This is an ODD number!!', 2, 'This is an ODD number!!', 4, 'This is an ODD number!!', 6, 'This is an ODD number!!', 8, 'This is an ODD number!!', 10]\n" + ] + } + ], + "source": [ + "print(ll)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -148,7 +327,7 @@ } ], "source": [ - "# Convert Celsius to Fahrenheit\n", + "# Convert Celsius list to Fahrenheit list\n", "celsius = [0,10,20.1,34.5]\n", "\n", "fahrenheit = [((9/5)*temp + 32) for temp in celsius ]\n", @@ -164,6 +343,68 @@ "We can also perform nested list comprehensions, for example:" ] }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "mylist4 = []\n", + "\n", + "for x in [2,4,6]:\n", + " for y in [100,200,300]:\n", + " mylist4.append(x*y)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[200, 400, 600, 400, 800, 1200, 600, 1200, 1800]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mylist4" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "list5 = [x*y for x in [2,4,6] for y in [100,200,300]]" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[200, 400, 600, 400, 800, 1200, 600, 1200, 1800]" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list5" + ] + }, { "cell_type": "code", "execution_count": 8, @@ -209,7 +450,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/02-Python Statements/07-Statements Assessment Test.ipynb b/02-Python Statements/07-Statements Assessment Test.ipynb index b9e5454..3514456 100644 --- a/02-Python Statements/07-Statements Assessment Test.ipynb +++ b/02-Python Statements/07-Statements Assessment Test.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ @@ -29,13 +29,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['Print only the word', ' that ', 'tart with ', ' in thi', ' ', 'entence']" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "st.split('s')" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "start\n", + "s\n", + "sentence\n" + ] + } + ], "source": [ - "#Code here" + "for i in st.split():\n", + " if i[0].lower() == 's': #if capital will be small fit 's'\n", + " print (i)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -46,11 +85,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n", + "6\n", + "8\n", + "10\n" + ] + } + ], "source": [ - "#Code Here" + "list(range(0,11,2))\n", + "for i in range (0,11,2):\n", + " print (i)" ] }, { @@ -63,12 +117,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#Code in this cell\n", - "[]" + "[x for x in range(0,51) if x%3==0]" ] }, { @@ -90,11 +155,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "only is even!\n", + "that is even!\n", + "with is even!\n", + "in is even!\n", + "this is even!\n", + "sentence is even!\n" + ] + } + ], "source": [ - "#Code in this cell" + "for i in st.split():\n", + " if len(i) %2 ==0:\n", + " print (i,\" is even!\")" ] }, { @@ -107,11 +187,126 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "Fizz\n", + "4\n", + "Buzz\n", + "Fizz\n", + "7\n", + "8\n", + "Fizz\n", + "Buzz\n", + "11\n", + "Fizz\n", + "13\n", + "14\n", + "FizzBuzz\n", + "16\n", + "17\n", + "Fizz\n", + "19\n", + "Buzz\n", + "Fizz\n", + "22\n", + "23\n", + "Fizz\n", + "Buzz\n", + "26\n", + "Fizz\n", + "28\n", + "29\n", + "FizzBuzz\n", + "31\n", + "32\n", + "Fizz\n", + "34\n", + "Buzz\n", + "Fizz\n", + "37\n", + "38\n", + "Fizz\n", + "Buzz\n", + "41\n", + "Fizz\n", + "43\n", + "44\n", + "FizzBuzz\n", + "46\n", + "47\n", + "Fizz\n", + "49\n", + "Buzz\n", + "Fizz\n", + "52\n", + "53\n", + "Fizz\n", + "Buzz\n", + "56\n", + "Fizz\n", + "58\n", + "59\n", + "FizzBuzz\n", + "61\n", + "62\n", + "Fizz\n", + "64\n", + "Buzz\n", + "Fizz\n", + "67\n", + "68\n", + "Fizz\n", + "Buzz\n", + "71\n", + "Fizz\n", + "73\n", + "74\n", + "FizzBuzz\n", + "76\n", + "77\n", + "Fizz\n", + "79\n", + "Buzz\n", + "Fizz\n", + "82\n", + "83\n", + "Fizz\n", + "Buzz\n", + "86\n", + "Fizz\n", + "88\n", + "89\n", + "FizzBuzz\n", + "91\n", + "92\n", + "Fizz\n", + "94\n", + "Buzz\n", + "Fizz\n", + "97\n", + "98\n", + "Fizz\n", + "Buzz\n" + ] + } + ], "source": [ - "#Code in this cell" + "for i in range (1,101):\n", + " if i %3 == 0 and i%5 ==0:\n", + " print ('FizzBuzz')\n", + " elif i %5 == 0:\n", + " print ('Buzz')\n", + " elif i%3 ==0:\n", + " print ('Fizz')\n", + " else:\n", + " print (i)" ] }, { @@ -124,7 +319,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ @@ -133,11 +328,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['C', 'a', 'l', 'o', 't', 'f', 'l', 'o', 'e', 'w', 'i', 't', 's']" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#Code in this cell" + "first_let = [x[0] for x in st.split()cc]\n", + "first_let" ] }, { @@ -146,6 +353,13 @@ "source": [ "### Great Job!" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -164,7 +378,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/.ipynb_checkpoints/01-Methods-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/01-Methods-checkpoint.ipynb index fde90b4..623bfa8 100644 --- a/03-Methods and Functions/.ipynb_checkpoints/01-Methods-checkpoint.ipynb +++ b/03-Methods and Functions/.ipynb_checkpoints/01-Methods-checkpoint.ipynb @@ -21,7 +21,20 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "#Method is a function in an object\n", + "#Ways to know methods:\n", + "#1- help function .. Tab in jupyter\n", + "#2- shift +tab in jupyter\n", + "#3- python standard library" + ] + }, + { + "cell_type": "code", + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -170,7 +183,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/.ipynb_checkpoints/02-Functions-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/02-Functions-checkpoint.ipynb index 1e9243d..14ef60e 100644 --- a/03-Methods and Functions/.ipynb_checkpoints/02-Functions-checkpoint.ipynb +++ b/03-Methods and Functions/.ipynb_checkpoints/02-Functions-checkpoint.ipynb @@ -28,6 +28,16 @@ "Let's see how to build out a function's syntax in Python. It has the following form:" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#return : assign the value of the function into the new variable \n", + "#" + ] + }, { "cell_type": "code", "execution_count": 1, @@ -68,14 +78,62 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def say_hello():\n", + " '''\n", + " DOCSTRING: Information about the function\n", + " Input: NO input\n", + " OUTPUT: Hello\n", + " '''\n", " print('hello')" ] }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "say_hello" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on function say_hello in module __main__:\n", + "\n", + "say_hello()\n", + " DOCSTRING: Information about the function\n", + " Input: NO input\n", + " OUTPUT: Hello\n", + "\n" + ] + } + ], + "source": [ + "help(say_hello)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -85,7 +143,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -110,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -120,7 +178,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -135,6 +193,36 @@ "greeting('Jose')" ] }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "#set a default\n", + "def say_hello1(name='NAME'):\n", + " print ('hello %s' %name)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello NAME\n", + "hello Ahmed\n" + ] + } + ], + "source": [ + "say_hello1()#default in case of not providing the argument\n", + "say_hello1('Ahmed')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -145,6 +233,40 @@ "### Example 3: Addition function" ] }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello mana\n" + ] + } + ], + "source": [ + "a = say_hello1('mana')" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + } + ], + "source": [ + "print(a) #No data will be saved in the variable COZ we used print not return in the function" + ] + }, { "cell_type": "code", "execution_count": 6, @@ -251,7 +373,126 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "def dog_check(my_string):\n", + " if 'dog' in my_string.lower():\n", + " return True\n", + " else: \n", + " return False\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dog_check('This is my dog')" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dog_check('Dog ran away')" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'dog' in 'This is my dog'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### PIG LATIN\n", + "* If word starts with a vowel, add 'ay' to end\n", + "* If word does not start with a vowel, put first letter at the end, then add 'ay' \n", + "* word >>> ordway\n", + "* apply>> appleay" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "def pig_latin(word):\n", + " if word[0].lower() in 'aeiou':\n", + " pig_word = word +'ay'\n", + " else:\n", + " pig_word = word[1:] + word[0]+ 'ay' \n", + " return pig_word" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'pplemay'" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pig_latin('mpple')" + ] + }, + { + "cell_type": "code", + "execution_count": 25, "metadata": {}, "outputs": [], "source": [ @@ -383,7 +624,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/.ipynb_checkpoints/03-Function Practice Exercises-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/03-Function Practice Exercises-checkpoint.ipynb index cb906db..a0cee6b 100644 --- a/03-Methods and Functions/.ipynb_checkpoints/03-Function Practice Exercises-checkpoint.ipynb +++ b/03-Methods and Functions/.ipynb_checkpoints/03-Function Practice Exercises-checkpoint.ipynb @@ -707,7 +707,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/.ipynb_checkpoints/07-args and kwargs-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/07-args and kwargs-checkpoint.ipynb index 47fa02a..3372204 100644 --- a/03-Methods and Functions/.ipynb_checkpoints/07-args and kwargs-checkpoint.ipynb +++ b/03-Methods and Functions/.ipynb_checkpoints/07-args and kwargs-checkpoint.ipynb @@ -77,23 +77,33 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 8, "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(40, 60, 20)\n" + ] + }, { "data": { "text/plain": [ "6.0" ] }, - "execution_count": 3, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "#pass as many parameters or arguments as I want\n", "def myfunc(*args):\n", + " print (args) #tuple\n", " return sum(args)*.05\n", + " \n", "\n", "myfunc(40,60,20)" ] @@ -109,7 +119,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -118,18 +128,49 @@ "6.0" ] }, - "execution_count": 4, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "def myfunc(*spam):\n", - " return sum(spam)*.05\n", + "# We can use any keywards by using * before it\n", + "def myfunc(*mana):\n", + " return sum(mana)*.05\n", "\n", "myfunc(40,60,20)" ] }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "def p_item(*zizo):\n", + " for i in zizo:\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mana\n", + "zizo\n", + "migo\n" + ] + } + ], + "source": [ + "p_item('mana','zizo','migo')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -179,6 +220,39 @@ "myfunc()" ] }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "#** as dictionary \n", + "def vfruit (**kwargs):\n", + " print (kwargs)\n", + " if 'fruit' in kwargs:\n", + " print('my fruit of choice is {}'.format(kwargs['fruit']))\n", + " else:\n", + " print('I did not find any fruit here')" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'ffruit': 'mango', 'vegitable': 'lettuce'}\n", + "I did not find any fruit here\n" + ] + } + ], + "source": [ + "vfruit(fruit='mango',vegitable='lettuce')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -246,6 +320,45 @@ "\n", "That's it! Now you should understand how `*args` and `**kwargs` provide the flexibilty to work with arbitrary numbers of arguments!" ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "def myfunc(*args,**kwargs):\n", + " print (args) #tuple\n", + " print(kwargs) #dictionary\n", + " print ('I would like {} {}'.format(args[0],kwargs['food']))" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(10, 20, 30)\n", + "{'fruit': 'orange', 'food': 'eggs', 'animals': 'dog'}\n", + "I would like 10 eggs\n" + ] + } + ], + "source": [ + "#we have to be in the same order args firstS>> then kwargs\n", + "myfunc(10,20,30,fruit='orange',food='eggs',animals='dog')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -264,7 +377,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/01-Methods.ipynb b/03-Methods and Functions/01-Methods.ipynb index fde90b4..623bfa8 100644 --- a/03-Methods and Functions/01-Methods.ipynb +++ b/03-Methods and Functions/01-Methods.ipynb @@ -21,7 +21,20 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "#Method is a function in an object\n", + "#Ways to know methods:\n", + "#1- help function .. Tab in jupyter\n", + "#2- shift +tab in jupyter\n", + "#3- python standard library" + ] + }, + { + "cell_type": "code", + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -170,7 +183,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/02-Functions.ipynb b/03-Methods and Functions/02-Functions.ipynb index 1e9243d..14ef60e 100644 --- a/03-Methods and Functions/02-Functions.ipynb +++ b/03-Methods and Functions/02-Functions.ipynb @@ -28,6 +28,16 @@ "Let's see how to build out a function's syntax in Python. It has the following form:" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#return : assign the value of the function into the new variable \n", + "#" + ] + }, { "cell_type": "code", "execution_count": 1, @@ -68,14 +78,62 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def say_hello():\n", + " '''\n", + " DOCSTRING: Information about the function\n", + " Input: NO input\n", + " OUTPUT: Hello\n", + " '''\n", " print('hello')" ] }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "say_hello" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on function say_hello in module __main__:\n", + "\n", + "say_hello()\n", + " DOCSTRING: Information about the function\n", + " Input: NO input\n", + " OUTPUT: Hello\n", + "\n" + ] + } + ], + "source": [ + "help(say_hello)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -85,7 +143,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -110,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -120,7 +178,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -135,6 +193,36 @@ "greeting('Jose')" ] }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "#set a default\n", + "def say_hello1(name='NAME'):\n", + " print ('hello %s' %name)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello NAME\n", + "hello Ahmed\n" + ] + } + ], + "source": [ + "say_hello1()#default in case of not providing the argument\n", + "say_hello1('Ahmed')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -145,6 +233,40 @@ "### Example 3: Addition function" ] }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello mana\n" + ] + } + ], + "source": [ + "a = say_hello1('mana')" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + } + ], + "source": [ + "print(a) #No data will be saved in the variable COZ we used print not return in the function" + ] + }, { "cell_type": "code", "execution_count": 6, @@ -251,7 +373,126 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "def dog_check(my_string):\n", + " if 'dog' in my_string.lower():\n", + " return True\n", + " else: \n", + " return False\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dog_check('This is my dog')" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dog_check('Dog ran away')" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'dog' in 'This is my dog'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### PIG LATIN\n", + "* If word starts with a vowel, add 'ay' to end\n", + "* If word does not start with a vowel, put first letter at the end, then add 'ay' \n", + "* word >>> ordway\n", + "* apply>> appleay" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "def pig_latin(word):\n", + " if word[0].lower() in 'aeiou':\n", + " pig_word = word +'ay'\n", + " else:\n", + " pig_word = word[1:] + word[0]+ 'ay' \n", + " return pig_word" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'pplemay'" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pig_latin('mpple')" + ] + }, + { + "cell_type": "code", + "execution_count": 25, "metadata": {}, "outputs": [], "source": [ @@ -383,7 +624,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/03-Function Practice Exercises.ipynb b/03-Methods and Functions/03-Function Practice Exercises.ipynb index cb906db..a0cee6b 100644 --- a/03-Methods and Functions/03-Function Practice Exercises.ipynb +++ b/03-Methods and Functions/03-Function Practice Exercises.ipynb @@ -707,7 +707,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/03-Methods and Functions/07-args and kwargs.ipynb b/03-Methods and Functions/07-args and kwargs.ipynb index 47fa02a..3372204 100644 --- a/03-Methods and Functions/07-args and kwargs.ipynb +++ b/03-Methods and Functions/07-args and kwargs.ipynb @@ -77,23 +77,33 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 8, "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(40, 60, 20)\n" + ] + }, { "data": { "text/plain": [ "6.0" ] }, - "execution_count": 3, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "#pass as many parameters or arguments as I want\n", "def myfunc(*args):\n", + " print (args) #tuple\n", " return sum(args)*.05\n", + " \n", "\n", "myfunc(40,60,20)" ] @@ -109,7 +119,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -118,18 +128,49 @@ "6.0" ] }, - "execution_count": 4, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "def myfunc(*spam):\n", - " return sum(spam)*.05\n", + "# We can use any keywards by using * before it\n", + "def myfunc(*mana):\n", + " return sum(mana)*.05\n", "\n", "myfunc(40,60,20)" ] }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "def p_item(*zizo):\n", + " for i in zizo:\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mana\n", + "zizo\n", + "migo\n" + ] + } + ], + "source": [ + "p_item('mana','zizo','migo')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -179,6 +220,39 @@ "myfunc()" ] }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "#** as dictionary \n", + "def vfruit (**kwargs):\n", + " print (kwargs)\n", + " if 'fruit' in kwargs:\n", + " print('my fruit of choice is {}'.format(kwargs['fruit']))\n", + " else:\n", + " print('I did not find any fruit here')" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'ffruit': 'mango', 'vegitable': 'lettuce'}\n", + "I did not find any fruit here\n" + ] + } + ], + "source": [ + "vfruit(fruit='mango',vegitable='lettuce')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -246,6 +320,45 @@ "\n", "That's it! Now you should understand how `*args` and `**kwargs` provide the flexibilty to work with arbitrary numbers of arguments!" ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "def myfunc(*args,**kwargs):\n", + " print (args) #tuple\n", + " print(kwargs) #dictionary\n", + " print ('I would like {} {}'.format(args[0],kwargs['food']))" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(10, 20, 30)\n", + "{'fruit': 'orange', 'food': 'eggs', 'animals': 'dog'}\n", + "I would like 10 eggs\n" + ] + } + ], + "source": [ + "#we have to be in the same order args firstS>> then kwargs\n", + "myfunc(10,20,30,fruit='orange',food='eggs',animals='dog')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -264,7 +377,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/09-Built-in Functions/01-Map.ipynb b/09-Built-in Functions/01-Map.ipynb index 6c08aab..2a96ad7 100644 --- a/09-Built-in Functions/01-Map.ipynb +++ b/09-Built-in Functions/01-Map.ipynb @@ -42,7 +42,7 @@ { "data": { "text/plain": [ - "[32.0, 72.5, 104.0, 212.0]" + "[32.0, 72.5, 104.0, 212.0,-14]" ] }, "execution_count": 2, diff --git a/15-Advanced OOP/01-Advanced Object Oriented Programming.ipynb b/15-Advanced OOP/01-Advanced Object Oriented Programming.ipynb index bb5d231..8db0bcc 100644 --- a/15-Advanced OOP/01-Advanced Object Oriented Programming.ipynb +++ b/15-Advanced OOP/01-Advanced Object Oriented Programming.ipynb @@ -470,7 +470,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/README.md b/README.md index de18f6a..8453a77 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,4 @@ # Complete-Python-3-Bootcamp -Course Files for Complete Python 3 Bootcamp Course on Udemy - - -Get it now for 95% off with the link: -https://www.udemy.com/complete-python-bootcamp/?couponCode=COMPLETE_GITHUB +Course Files for Complete Python 3 Bootcamp Course Thanks!