Created
September 6, 2021 12:00
-
-
Save rafsid/acd38f486dfe0193829052c5b307245d to your computer and use it in GitHub Desktop.
CodewithMosh - Python for Beginners
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"print('Sheringa Poting')" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"print('^ ^')\n", | |
"print(' -')" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"print('*' * 10)\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"prices = 10\n", | |
"prices = 20\n", | |
"print (prices)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"full_name = 'John Smith'\n", | |
"age = 20\n", | |
"is_new = True" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"naam = input('what is your name?' )\n", | |
"print('Hi '+naam)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Exercise\n", | |
"naam = input('what is your name? ' )\n", | |
"color = input('what is your favourite color? ')\n", | |
"print(naam+' likes '+color)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"birth_year = input('Birth year: ')\n", | |
"print(type(birth_year))\n", | |
"age = 2020 - int(birth_year)\n", | |
"print(type(age))\n", | |
"print(age)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"weight = input('What is your weight in kgs ')\n", | |
"weight_in_pounds= int(weight)*2.2\n", | |
"print('Your weight in kgs is '+ str(weight_in_pounds))" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Using \"\" helps in including text with single quotes\n", | |
"course = \"Python's Course for Beginners\"\n", | |
"print(course)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Triple quotes for lengthier texts\n", | |
"course ='''\n", | |
"Dear Johny,\n", | |
"\n", | |
"Your iphone designs are boring. Do something new!\n", | |
"\n", | |
"Thanks\n", | |
"'''\n", | |
"print(course)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#extracting text\n", | |
"course = 'Python for Beginners'\n", | |
"print(course[0])\n", | |
"\n", | |
"#Python supports negative indices and will return data from the right\n", | |
"print(course[-1])" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Returning particular part of the text\n", | |
"print(course[0:3])\n", | |
"\n", | |
"#Returning entire text\n", | |
"print(course[0:])\n", | |
"\n", | |
"#Returning without mentioning start index\n", | |
"print(course[:5])" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Exercise\n", | |
"name = 'Jeniffer'\n", | |
"print(name[1:-1])" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Formatted string\n", | |
"first = 'John'\n", | |
"last = 'Smith'\n", | |
"message = first +' [' + last + '] is a coder' #Difficult to imagine the output\n", | |
"print(message)\n", | |
"\n", | |
"#Solution is formatted strings\n", | |
"msg = f'{first}[{last}] is a coder'\n", | |
"#{} or curly brackets are used as placeholders. f is added at the start of the message for formatting the message\n", | |
"print(msg)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"course = 'Python for Beginners'\n", | |
"#len is a general purpose function baked into Python used for counting\n", | |
"print(len(course))\n", | |
"\n", | |
"# . shows the methods or functions associated with the objects\n", | |
"course.upper()\n", | |
"\n", | |
"#Upper is a method while len is a function\n", | |
"print(course.upper())\n", | |
"print(course.lower())\n", | |
"\n", | |
"#methods do not modify variables\n", | |
"print(course)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Returning index of first occurence of the character\n", | |
"course.find('Beginners')" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Returning index of first occurence of set of characters\n", | |
"course.find('for')" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Replacing set of characters\n", | |
"print(course.replace('Beginners','Absolute Beginners'))" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Replacing one character\n", | |
"print(course.replace('B','D'))" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Check whether the string exists in the variable. This will output a Boolean. Note, as usual, Python is case sensitive\n", | |
"'Python' in course #The \"in\" operator is a Boolean Test" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Integers are numbers without decimals\n", | |
"#Floats are numbers with decimals\n", | |
"\n", | |
"#Arithmetic Operators\n", | |
"print(10+3) #additions\n", | |
"print(10-3) #subtraction\n", | |
"print(10/3) #this will output a float\n", | |
"print(10//3) # this will output an integer\n", | |
"print(10%3) # this will output remainder\n", | |
"print(10*3) #this will multiply\n", | |
"print(10**3) #this is exponent operator" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Augmented assignment operator\n", | |
"x = 10\n", | |
"x += 3 #Here =+ is the augmented assignment operator\n", | |
"print(x)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#The hierarchy of mathematical operations or operator precedence of EDMAS( exponentiation, division, multiplication, addition and subtraction) works in Python\n", | |
"x = 10 + 3*2\n", | |
"print(x)\n", | |
"\n", | |
"#Example\n", | |
"x= 10+3*2**2\n", | |
"print(x)\n", | |
"\n", | |
"#paraenthesis overrides operator precedence\n", | |
"x= (10+3)*2**2\n", | |
"print(x)\n", | |
"\n", | |
"#Exercise\n", | |
"x=(2+3)*10 - 3\n", | |
"print(x)\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Mathematical Functions\n", | |
"x = 2.9\n", | |
"\n", | |
"#Roounding numbers\n", | |
"print(round(x))\n", | |
"\n", | |
"#Absolute\n", | |
"y = -3.4\n", | |
"print(abs(y))" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 97, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": "-4" | |
}, | |
"execution_count": 97, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#Imagine module as different departments/sections in a supermarket like for junk food, cleaning products, grocery etc\n", | |
"#Similarly different modules have different functionalities\n", | |
"\n", | |
"import math\n", | |
"#Now we have imported math which is now an object like a string and we can use its methods using the . operator\n", | |
"\n", | |
"math.ceil(x)\n", | |
"math.floor(y)\n", | |
"\n", | |
"#Search google Python 3 for math module and checkout the documentation" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 107, | |
"outputs": [], | |
"source": [ | |
"#If Statements\n", | |
"#Example\n", | |
"example_text = '''\n", | |
"(if it's hot\n", | |
" It's a hot day\n", | |
" Drink plenty of water\n", | |
"otherwise if it's cold\n", | |
" It's a cold day\n", | |
" Wear warm clothes\n", | |
"otherwise\n", | |
" It it's a lovely day\n", | |
"'''" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 117, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"It's a hot day\n", | |
"Drink plenty of water\n", | |
"Enjoy your day\n" | |
] | |
} | |
], | |
"source": [ | |
"#If statement are boolean statements and returns the value/string if the statement is true\n", | |
"is_hot = True\n", | |
"is_cold = False\n", | |
"if is_hot:\n", | |
" print(\"It's a hot day\")\n", | |
" print(\"Drink plenty of water\")\n", | |
"elif is_cold: #elif is another if condition\n", | |
" print(\"It's a cold day\")\n", | |
" print(\"Wear warm clothes\")\n", | |
"else: #This will return when none of the conditions are true\n", | |
" print(\"It's a lovely day\")\n", | |
"print(\"Enjoy your day\")\n", | |
"\n", | |
"#Note, if we keep both is_hot and is_cold as True, it will return the first statement for which condition is True" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 144, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Your down payment is $200000.0\n" | |
] | |
} | |
], | |
"source": [ | |
"#Exercise\n", | |
"exercise_text = '''\n", | |
"Price of a house is $1 million\n", | |
"If buyers has good credit,\n", | |
" they need to put down 10%\n", | |
"Otheriwse\n", | |
" they need to put down 20%\n", | |
"'''\n", | |
"house_price = 1000000\n", | |
"has_credit_good = False\n", | |
"if has_credit_good:\n", | |
" print(\"Your down payment amout is $\"+str(house_price*0.1))\n", | |
"else:\n", | |
" print(\"Your down payment is $\"+ str(house_price*0.2))\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 146, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Down Payment: 100000.0\n" | |
] | |
} | |
], | |
"source": [ | |
"price = 1000000\n", | |
"has_credit_good = True\n", | |
"if has_credit_good:\n", | |
" down_payment = price * 0.1\n", | |
"else:\n", | |
" down_payment = price * 0.2\n", | |
"\n", | |
"print(f'Down Payment: {down_payment}')" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%#Solution by Mosh\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 161, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Better luck next time\n" | |
] | |
} | |
], | |
"source": [ | |
"#Logical Operotor used in any programming language which supports if statements\n", | |
"example_text = '''\n", | |
"if applicant has high income AND good credit\n", | |
" Eligible for loan\n", | |
"'''\n", | |
"#AND Operator\n", | |
"has_high_income = True\n", | |
"has_good_credit = False\n", | |
"\n", | |
"if has_high_income and has_good_credit:\n", | |
" print(\"Eligible for loan\")\n", | |
"else:\n", | |
" print(\"Better luck next time\")" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 162, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Eligible for loan\n" | |
] | |
} | |
], | |
"source": [ | |
"#Or Operator\n", | |
"has_high_income = True\n", | |
"has_good_credit = False\n", | |
"\n", | |
"if has_high_income or has_good_credit:\n", | |
" print(\"Eligible for loan\")\n", | |
"else:\n", | |
" print(\"Better luck next time\")" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 165, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Eligible for loan\n" | |
] | |
} | |
], | |
"source": [ | |
"example_text = '''\n", | |
"if applicant has high income AND doesn't have a criminal record\n", | |
" Eligible for loan\n", | |
"'''\n", | |
"#AND Not Operator\n", | |
"has_high_income = True\n", | |
"has_criminal_record = False\n", | |
"\n", | |
"if has_high_income and not has_criminal_record:\n", | |
" print(\"Eligible for loan\")\n", | |
"else:\n", | |
" print(\"Better luck next time\")" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 172, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"It's a hot day\n" | |
] | |
} | |
], | |
"source": [ | |
"#Comparison Operators\n", | |
"example_text = '''\n", | |
"if temperature is greater than 30\n", | |
" it's a hot day\n", | |
"otheriwse if it's less than 10\n", | |
" it's a cold day\n", | |
"otherwise\n", | |
" it's neither hot nor cold\n", | |
"'''\n", | |
"temperature = 30\n", | |
"\n", | |
"if temperature == 30:\n", | |
" print(\"It's a hot day\")\n", | |
"else:\n", | |
" print(\"It's not a hot day\")\n", | |
"\n", | |
"# Types of comparision operators are greater than (>), less than (<), greater than or equal to (>=),\n", | |
"# less than or equal to (<=) and equality (==). Note that == is not same as the assignment operator =\n", | |
"# #%%\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Name looks good\n" | |
] | |
} | |
], | |
"source": [ | |
"#Exercise\n", | |
"exercise_text='''\n", | |
"If name is less than 3 characters long\n", | |
" name must be at least 3 characters long\n", | |
"otherwise if it's more than 50 characters long\n", | |
" name can be a maximum 50 characters\n", | |
"otherwise\n", | |
" name looks good\n", | |
"'''\n", | |
"name = input()\n", | |
"\n", | |
"name_len = len(name)\n", | |
"\n", | |
"if name_len <= 3:\n", | |
" print(\"Name must be atleast 3 characters long\")\n", | |
"elif name_len > 50:\n", | |
" print(\"Name can be a maximum 50 characters\")\n", | |
"else:\n", | |
" print(\"Name looks good\")" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Your weight in kgs is 90.0\n" | |
] | |
} | |
], | |
"source": [ | |
"#Exercise\n", | |
"#The user can input the weight in pounds and kilos and we need to convert the weight to kilograms\n", | |
"\n", | |
"weight = input(\"Your weight\")\n", | |
"weight = int(weight)\n", | |
"metrics = input('Type l for weight entered in pounds or k for weight entered in kilos' )\n", | |
"\n", | |
"if metrics == 'l' :\n", | |
" weight = weight//2.2\n", | |
" print (\"Your weight in kgs is \" +str(weight))\n", | |
"else:\n", | |
" weight = weight*2.2\n", | |
" print(\"Your weight in pounds is \" + str(weight))" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"You are 90.0 kilos\n" | |
] | |
} | |
], | |
"source": [ | |
"#Solution by Mosh\n", | |
"weight = int(input('Weight: '))\n", | |
"unit = input('(L)bs or (K)g: ')\n", | |
"if unit.upper() == \"L\":\n", | |
" converted = weight * 0.45\n", | |
" print(f\"You are {converted} kilos\")\n", | |
"else:\n", | |
" converted = weight // 0.45\n", | |
" print(f\"You are {converted} pounds\")\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"ename": "NameError", | |
"evalue": "name 'i' is not defined", | |
"output_type": "error", | |
"traceback": [ | |
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", | |
"\u001B[0;31mNameError\u001B[0m Traceback (most recent call last)", | |
"\u001B[0;32m/var/folders/zh/w6kxh4611fz3my_7yl9hrz600000gn/T/ipykernel_30119/1239953497.py\u001B[0m in \u001B[0;36m<module>\u001B[0;34m\u001B[0m\n\u001B[1;32m 5\u001B[0m \u001B[0;31m#Exmple\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 6\u001B[0m \u001B[0mguess_count\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0;36m1\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 7\u001B[0;31m \u001B[0;32mwhile\u001B[0m \u001B[0mi\u001B[0m\u001B[0;34m<=\u001B[0m \u001B[0;36m5\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m 8\u001B[0m \u001B[0mprint\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mi\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 9\u001B[0m \u001B[0mi\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0mi\u001B[0m\u001B[0;34m+\u001B[0m\u001B[0;36m1\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n", | |
"\u001B[0;31mNameError\u001B[0m: name 'i' is not defined" | |
] | |
} | |
], | |
"source": [ | |
"# While loops\n", | |
" #while condition is true, the code will be repeatedly executed\n", | |
" #Beware of infinite loops\n", | |
"\n", | |
"#Exmple\n", | |
"guess_count = 1\n", | |
"i = 1\n", | |
"while i<= 5:\n", | |
" print(i)\n", | |
" i = i+1\n", | |
"print(\"Done\")\n", | |
"\n", | |
"#Repeating strings\n", | |
"#Exmple\n", | |
"i = 1\n", | |
"while i<= 5:\n", | |
" print('*'*i)\n", | |
" i = i+1\n", | |
"print(\"Done\")" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"You won!\n" | |
] | |
} | |
], | |
"source": [ | |
"#Guess the secret name\n", | |
"secret_number = 9\n", | |
"\n", | |
"guess_count = 0 #we refactored i here to guess_count so that the expression is more descriptive\n", | |
"guess_limit = 3\n", | |
"while guess_count < guess_limit:\n", | |
" guess = int(input('Guess: '))\n", | |
" guess_count += 1\n", | |
" if guess == secret_number:\n", | |
" print('You won!')\n", | |
" break #this breaks the while function since the condition has been met\n", | |
"else:\n", | |
" print(\"Sorry, You lost.\")" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"outputs": [], | |
"source": [ | |
"#Car game\n", | |
"example_text = '''\n", | |
"This is a car game. There are three actions - start, stop and exit.\n", | |
"The user can input help or any of these three action. These can be inputted in lower or upper case\n", | |
"If the user inputs any other action, the program will return \"I don't understand that...\" response.\n", | |
"\n", | |
"'''" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Car started....\n", | |
"Car is already started!\n" | |
] | |
} | |
], | |
"source": [ | |
"#Solution by Mosh\n", | |
"#In programming there is a saying - DRY or Dont Repeat Yourself\n", | |
"\n", | |
"command = ''\n", | |
"started = False\n", | |
"while True:\n", | |
" command = input(\"> \").lower()\n", | |
" if command == \"start\":\n", | |
" if started:\n", | |
" print(\"Car is already started!\")\n", | |
" else:\n", | |
" started = True\n", | |
" print(\"Car started....\")\n", | |
" elif command == \"stop\":\n", | |
" if not started:\n", | |
" print(\"Car is already stopped!\")\n", | |
" else:\n", | |
" started = False\n", | |
" print(\"Car has stopped\")\n", | |
" elif command == \"help\":\n", | |
" print(\"\"\"\n", | |
"start - to start the car\n", | |
"stop - to stop the car\n", | |
"quit - to quit\"\"\")\n", | |
" elif command == \"quit\":\n", | |
" break\n", | |
" else:\n", | |
" print(\"I don't understand that\")" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"P\n", | |
"y\n", | |
"t\n", | |
"h\n", | |
"o\n", | |
"n\n", | |
"Mosh\n", | |
"John\n", | |
"Sarah\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n" | |
] | |
} | |
], | |
"source": [ | |
"#For Loops for iterating over items of a collection\n", | |
"\n", | |
"for item in 'Python':\n", | |
" print(item)\n", | |
"\n", | |
"#List is a list of items and denoted by []\n", | |
"\n", | |
"for item in ['Mosh', 'John', 'Sarah']:\n", | |
" print(item)\n", | |
"\n", | |
"for item in [1, 2, 3, 4]:\n", | |
" print(item)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 66, | |
"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", | |
"5\n", | |
"6\n", | |
"7\n", | |
"8\n", | |
"9\n" | |
] | |
} | |
], | |
"source": [ | |
"for item in range(10):\n", | |
" print(item)\n", | |
"\n", | |
"for item in range (5, 10):\n", | |
" print(item)\n", | |
"\n", | |
"for item in range(5, 10, 2): #range function can also use steps for skipping\n", | |
" print(item)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 80, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"20\n", | |
"40\n", | |
"60\n" | |
] | |
} | |
], | |
"source": [ | |
"#Exercise - calculate the total price of items in a shopping cart/list\n", | |
"\n", | |
"for item in [10,20,30]:\n", | |
" item += item\n", | |
" print(item) #keeping print under this indent will make it print for every for loop iteration" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Total: 60\n" | |
] | |
} | |
], | |
"source": [ | |
"#Solution by Mosh\n", | |
"prices = [10, 20, 30]\n", | |
"\n", | |
"total = 0\n", | |
"for price in prices:\n", | |
" total += price\n", | |
"print(f'Total: {total}')" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"(0,0)\n", | |
"(0,1)\n", | |
"(0,2)\n", | |
"(1,0)\n", | |
"(1,1)\n", | |
"(1,2)\n", | |
"(2,0)\n", | |
"(2,1)\n", | |
"(2,2)\n", | |
"(3,0)\n", | |
"(3,1)\n", | |
"(3,2)\n" | |
] | |
} | |
], | |
"source": [ | |
"#Nested Loop\n", | |
"for x in range(4):\n", | |
" print(x)\n", | |
"\n", | |
"for x in range(4):\n", | |
" for y in range(3):\n", | |
" print(f'({x},{y})')\n", | |
"#The way nested loop is working here is once x = 0 and the function moves to y, the iteration takes over y = 0, 1, 2\n", | |
"# Once iterations of range of y is complete, the control will move to x = 1 and gets repeated" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 88, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"xxxxx\n", | |
"xx\n", | |
"xxxxx\n", | |
"xx\n", | |
"xx\n" | |
] | |
} | |
], | |
"source": [ | |
"#Exercise - create shape f with x\n", | |
"numbers = [5, 2, 5, 2, 2]\n", | |
"\n", | |
"for x in numbers:\n", | |
" x = int(x)\n", | |
" print('x'*x)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 92, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"xxxxx\n", | |
"xx\n", | |
"xxxxx\n", | |
"xx\n", | |
"xx\n" | |
] | |
} | |
], | |
"source": [ | |
"#Solution by Mosh\n", | |
"for x_count in numbers:\n", | |
" print('x' * x_count)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 91, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"xxxxx\n", | |
"xx\n", | |
"xxxxx\n", | |
"xx\n", | |
"xx\n" | |
] | |
} | |
], | |
"source": [ | |
"for x_count in numbers:\n", | |
" output = ''\n", | |
" for count in range(x_count):\n", | |
" output += 'x'\n", | |
" print(output)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 109, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Sarah\n", | |
"Mary\n", | |
"['Sarah', 'Mary', 'Mosh']\n", | |
"['Sarah', 'Mary']\n", | |
"['Sarah', 'Mary']\n", | |
"['John', 'Bob', 'Sarah', 'Mary']\n", | |
"['John', 'Bob', 'Sarah', 'Mary', 'Mosh']\n", | |
"['Jon', 'Bob', 'Sarah', 'Mary', 'Mosh']\n" | |
] | |
} | |
], | |
"source": [ | |
"#List\n", | |
"# names = ['John', 'Bob', 'Sarah', 'Mary', 'Mosh']\n", | |
"\n", | |
"print(names[2])\n", | |
"\n", | |
"print(names[-2])\n", | |
"\n", | |
"print(names[2:])\n", | |
"\n", | |
"print(names[2:4])\n", | |
"\n", | |
"print(names[2:-1])\n", | |
"\n", | |
"print(names[:-1])\n", | |
"\n", | |
"print(names[:])\n", | |
"\n", | |
"names[0]='Jon'\n", | |
"print(names)\n", | |
"#The [] operations does not modify the original list" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 117, | |
"outputs": [], | |
"source": [ | |
"#Exercise - Write a program to find the largest number in a list\n", | |
"number = [2,5,6,8,3,2,4,5,8,99,931]\n", | |
"\n", | |
"for x in number:\n", | |
" if x>x:\n", | |
" print(x)\n", | |
"\n", | |
"import math as math\n", | |
"\n", | |
"number = [2,5,6,8,3,2,4,5,8,99,931]\n", | |
"\n", | |
"print(math.ceil(number))\n", | |
"\n", | |
"#COULD NOT SOLVE THE PROBLEM\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 118, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10\n" | |
] | |
} | |
], | |
"source": [ | |
"#Solution by Mosh\n", | |
"numbers = [3, 6,2, 8, 4, 10]\n", | |
"max = numbers[0]\n", | |
"\n", | |
"for number in numbers:\n", | |
" if number > max:\n", | |
" max = number\n", | |
"print(max)\n", | |
"\n", | |
"#Reshuffling the list will not impact the proble" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 119, | |
"outputs": [ | |
{ | |
"ename": "SyntaxError", | |
"evalue": "invalid syntax (1353429324.py, line 3)", | |
"output_type": "error", | |
"traceback": [ | |
"\u001B[0;36m File \u001B[0;32m\"/var/folders/zh/w6kxh4611fz3my_7yl9hrz600000gn/T/ipykernel_19614/1353429324.py\"\u001B[0;36m, line \u001B[0;32m3\u001B[0m\n\u001B[0;31m 1 2 3\u001B[0m\n\u001B[0m ^\u001B[0m\n\u001B[0;31mSyntaxError\u001B[0m\u001B[0;31m:\u001B[0m invalid syntax\n" | |
] | |
} | |
], | |
"source": [ | |
"#Two dimensional lists or matrices\n", | |
"[\n", | |
" 1 2 3\n", | |
" 4 5 6\n", | |
" 7 8 9\n", | |
"]" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 123, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1, 2, 3]\n", | |
"2\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"5\n", | |
"6\n", | |
"7\n", | |
"8\n", | |
"9\n" | |
] | |
} | |
], | |
"source": [ | |
"matrix = [\n", | |
" [1,2,3],\n", | |
" [4,5,6],\n", | |
" [7,8,9]\n", | |
"]\n", | |
"\n", | |
"#Accessing the matrix\n", | |
"#Row\n", | |
"print(matrix[0])\n", | |
"\n", | |
"#item in a row\n", | |
"print(matrix[0][1])\n", | |
"\n", | |
"#Nested Loop\n", | |
"for row in matrix:\n", | |
" for item in row:\n", | |
" print(item)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 141, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[5, 2, 1, 7, 4, 20]\n", | |
"[10, 5, 2, 1, 7, 4, 20]\n", | |
"[10, 2, 1, 7, 4, 20]\n", | |
"[10, 2, 1, 7, 4, 20]\n", | |
"[5, 2, 1, 7]\n", | |
"True\n", | |
"2\n", | |
"None\n", | |
"[1, 2, 4, 5, 5, 7]\n", | |
"None\n", | |
"[7, 5, 5, 4, 2, 1]\n", | |
"[7, 5, 5, 4, 2, 1, 10]\n", | |
"[7, 5, 5, 4, 2, 1]\n" | |
] | |
} | |
], | |
"source": [ | |
"#List methods\n", | |
"\n", | |
"numbers = [5,2,1,7,4]\n", | |
"numbers.append(20) #Numbers are added to the end of the list\n", | |
"print(numbers)\n", | |
"\n", | |
"numbers.insert(0,10) #Numbers are added to the specific index of the list\n", | |
"print(numbers)\n", | |
"\n", | |
"numbers.remove(5) #Remove a particular number\n", | |
"print(numbers)\n", | |
"\n", | |
"numbers.clear #Clears the entire list\n", | |
"print(numbers)\n", | |
"\n", | |
"numbers = [5,2,1,7,4] #Removes the number at the end of the list\n", | |
"numbers.pop()\n", | |
"print(numbers)\n", | |
"\n", | |
"numbers.index(1) #Returns the index of the first occurence of the numbers. Returns error if number not in the list\n", | |
"print(1 in numbers) #Returns Boolean value ofr checking the first occurence of the number\n", | |
"\n", | |
"numbers = [5,2,1,5,7,4]\n", | |
"print(numbers.count(5)) #Returns the count of a particular number\n", | |
"print(numbers.sort()) #Sorts the list in place in ascending prder and returns None.\n", | |
"print(numbers)\n", | |
"print(numbers.reverse()) #Reverses the order\n", | |
"print(numbers)\n", | |
"\n", | |
"numbers2 = numbers.copy() #This creates a second list and ensures original list is not impacted/altered\n", | |
"numbers.append(10)\n", | |
"print(numbers)\n", | |
"print(numbers2)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 150, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"3\n" | |
] | |
} | |
], | |
"source": [ | |
"#Exercise - Write a program to remove the duplicates in a list\n", | |
"\n", | |
"numbers = [3, 6,2, 8, 4, 10,10,6,8]\n", | |
"number_repeat = numbers[0]\n", | |
"\n", | |
"for number in numbers:\n", | |
" if number == number_repeat:\n", | |
" numbers.remove(number)\n", | |
"print(numbers)\n", | |
"\n", | |
"\n", | |
"# COULD NOT SOLVE" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 153, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[2, 4, 6, 3, 1]\n" | |
] | |
} | |
], | |
"source": [ | |
"#Solution by Mosh\n", | |
"numbers = [2,2,4,6,3,4,6,1]\n", | |
"uniques = []\n", | |
"for number in numbers:\n", | |
" if number not in uniques:\n", | |
" uniques.append(number)\n", | |
"print(uniques)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 154, | |
"outputs": [], | |
"source": [ | |
"#Tuples - similar to lists but we cannot add/remove and thus are immutable (cannot mutate)\n", | |
"numbers = [1,2,3] # This is a list\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 157, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n" | |
] | |
} | |
], | |
"source": [ | |
"numbers = (1,2,3) #This is a tuple. Tuples have functionalities called magic methods covered in advance courses\n", | |
"print(numbers[0])" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 158, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1 2 3\n" | |
] | |
} | |
], | |
"source": [ | |
"#Unpacking\n", | |
"coordinates = (1,2,3)\n", | |
"x, y, z = coordinates #This is unpacking of the tuple 'coordinates' here to individual variables. This works with lists also\n", | |
"\n", | |
"print(x,y,z)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 171, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"John Smith\n", | |
"John Smith\n", | |
"None\n", | |
"Jan 1 1980\n", | |
"Jack Smith\n", | |
"Jan 1 1980\n" | |
] | |
} | |
], | |
"source": [ | |
"#Dictionaries are used for storing information which come in key-value pairs\n", | |
"\n", | |
"customer = {\n", | |
" \"name\": \"John Smith\",\n", | |
" \"age\": 30,\n", | |
" \"is_verified\": True\n", | |
"}\n", | |
"print(customer[\"name\"])\n", | |
"print(customer.get(\"name\"))\n", | |
"\n", | |
"print(customer.get(\"Nae\")) #None represents the absence of a value. Using 'get' function ensures that python doesn't output Error\n", | |
"\n", | |
"print(customer.get(\"birthdate\", \"Jan 1 1980\")) # This adds the value if not there already in the list\n", | |
"\n", | |
"customer[\"name\"] = \"Jack Smith\"\n", | |
"print(customer[\"name\"])\n", | |
"\n", | |
"customer[\"birthdate\"] = \"Jan 1 1980\"\n", | |
"print(customer[\"birthdate\"])" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 187, | |
"outputs": [ | |
{ | |
"ename": "KeyError", | |
"evalue": "'number'", | |
"output_type": "error", | |
"traceback": [ | |
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", | |
"\u001B[0;31mKeyError\u001B[0m Traceback (most recent call last)", | |
"\u001B[0;32m/var/folders/zh/w6kxh4611fz3my_7yl9hrz600000gn/T/ipykernel_19614/2327159778.py\u001B[0m in \u001B[0;36m<module>\u001B[0;34m\u001B[0m\n\u001B[1;32m 16\u001B[0m \u001B[0moutput\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0;34m[\u001B[0m\u001B[0;34m]\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 17\u001B[0m \u001B[0;32mfor\u001B[0m \u001B[0mnumber\u001B[0m \u001B[0;32min\u001B[0m \u001B[0mphone\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m---> 18\u001B[0;31m \u001B[0moutput\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mappend\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mNumber\u001B[0m\u001B[0;34m[\u001B[0m\u001B[0;34m\"number\"\u001B[0m\u001B[0;34m]\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m 19\u001B[0m \u001B[0mprint\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0moutput\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n", | |
"\u001B[0;31mKeyError\u001B[0m: 'number'" | |
] | |
} | |
], | |
"source": [ | |
"#Exercise - Convert numbers to words\n", | |
"\n", | |
"Number = {\n", | |
" \"1\": \"One\",\n", | |
" \"2\": \"Two\",\n", | |
" \"3\": \"Three\",\n", | |
" \"4\": \"Four\",\n", | |
" \"5\": \"Five\",\n", | |
" \"6\": \"Six\",\n", | |
" \"7\": \"Seven\",\n", | |
" \"8\": \"Eight\",\n", | |
" \"9\": \"Nine\",\n", | |
" \"0\": \"Zero\"\n", | |
"}\n", | |
"phone= input(\"What is your phone number? \")\n", | |
"output = []\n", | |
"for number in phone:\n", | |
" output.append(Number[\"number\"])\n", | |
"print(output)\n", | |
"\n", | |
"#COULD NOT SOLVE" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 193, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"! \n" | |
] | |
} | |
], | |
"source": [ | |
"#Solution by Mosh\n", | |
"phone = input(\"Phone: \")\n", | |
"digits_mapping = {\n", | |
" \"1\": \"One\",\n", | |
" \"2\": \"Two\",\n", | |
" \"3\": \"Three\",\n", | |
" \"4\": \"Four\",\n", | |
"}\n", | |
"output = \"\"\n", | |
"for ch in phone:\n", | |
" output += digits_mapping.get(ch, \"!\") + \" \"\n", | |
"print(output)\n", | |
"\n", | |
"#Observation - Note we used [] in case of output while Mosh used \"\". also under the for loop, we used append while Mosh used +=\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 197, | |
"outputs": [], | |
"source": [], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 205, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"😀 Good morning sunshine 😀 \n" | |
] | |
} | |
], | |
"source": [ | |
"#Emoji Converter\n", | |
"message = input(\"Type a message\")\n", | |
"words = message.split(' ') #splits at locations where it finds the variable and returns a list\n", | |
"\n", | |
"emojis = {\n", | |
" \":)\": \"😀\",\n", | |
" \":(\": \"😔\"\n", | |
"}\n", | |
"output = \"\"\n", | |
"for word in words:\n", | |
" output += emojis.get(word, word) + \" \"\n", | |
"print(output)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 208, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Start\n", | |
"Hi there!\n", | |
"Welcome aboard\n", | |
"Finish\n" | |
] | |
} | |
], | |
"source": [ | |
"# Functions - Users can create their own functions which can be called later\n", | |
"def greet_user():\n", | |
" print('Hi there!')\n", | |
" print('Welcome aboard') #whenever a funtion is being defined, always add two lines of space\n", | |
"\n", | |
"print(\"Start\")\n", | |
"greet_user()\n", | |
"print(\"Finish\")\n", | |
" #Always defined functions first and then call it" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 216, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Start\n", | |
"Hi John\n", | |
"Welcome aboard\n", | |
"Hi Mary\n", | |
"Welcome aboard\n", | |
"Finish\n", | |
"Start\n", | |
"Hi Sheringa\n", | |
"Welcome aboard\n", | |
"Finish\n" | |
] | |
} | |
], | |
"source": [ | |
"#Parameters\n", | |
"def greet_user(name):\n", | |
" print(f'Hi {name}')\n", | |
" print('Welcome aboard')\n", | |
"\n", | |
"print(\"Start\")\n", | |
"greet_user('John') #Always supply values to parameters\n", | |
"greet_user('Mary')\n", | |
"print(\"Finish\")\n", | |
"\n", | |
"#What if we ask the user to input the name\n", | |
"print(\"Start\")\n", | |
"greet_user(name=input(\"What's your name?\"))\n", | |
"print(\"Finish\")" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 217, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Start\n", | |
"Hi John Smith\n", | |
"Welcome aboard\n", | |
"Finish\n" | |
] | |
} | |
], | |
"source": [ | |
"def greet_user(first_name, last_name):\n", | |
" print(f'Hi {first_name} {last_name}')\n", | |
" print('Welcome aboard')\n", | |
"\n", | |
"print(\"Start\")\n", | |
"greet_user('John', 'Smith')\n", | |
"print(\"Finish\")" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 220, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Start\n", | |
"Hi John Smith\n", | |
"Welcome aboard\n", | |
"Finish\n" | |
] | |
} | |
], | |
"source": [ | |
"#Keyword Arguments\n", | |
"def greet_user(first_name, last_name):\n", | |
" print(f'Hi {first_name} {last_name}')\n", | |
" print('Welcome aboard')\n", | |
"\n", | |
"print(\"Start\")\n", | |
"greet_user(last_name='Smith', first_name='John') #This is example of keyword argument. Without supplying keywords, it is then called positional arguments where the position or the order is also under consideration\n", | |
"print(\"Finish\")\n", | |
"\n", | |
"#Usually keyword arguments are used where numbers will be supplied for improving the readibility of the code\n", | |
"#Example - total_cost(total = 50, shipping = 5, discount = 0.1)\n", | |
"#Always use positional argument first and keyword argument later\n", | |
"#Example greet_user(\"John\", last_name=\"Smith\")" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 227, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": "9" | |
}, | |
"execution_count": 227, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#Functions that return values\n", | |
"def square(number):\n", | |
" return number*number\n", | |
"\n", | |
"square(int(input()))\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 229, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"sad 😔 \n" | |
] | |
} | |
], | |
"source": [ | |
"#Creating a reusable function\n", | |
"def emoji_converter(message):\n", | |
" words = message.split(' ')\n", | |
"\n", | |
" emojis = {\n", | |
" \":)\": \"😀\",\n", | |
" \":(\": \"😔\"\n", | |
" }\n", | |
" output = \"\"\n", | |
" for word in words:\n", | |
" output += emojis.get(word, word) + \" \"\n", | |
" return output\n", | |
"\n", | |
"\n", | |
"message = input('> ')\n", | |
"print(emoji_converter(message))" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 243, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Age cannot be zero\n" | |
] | |
} | |
], | |
"source": [ | |
"#Exception Handling errors\n", | |
"try:\n", | |
" age = int(input('Age: '))\n", | |
" income = 20000\n", | |
" risk = income / age\n", | |
" print(age)\n", | |
"except ZeroDivisionError:\n", | |
" print(\"Age cannot be zero\")\n", | |
"except ValueError:\n", | |
" print('Invalid Value')" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 244, | |
"outputs": [], | |
"source": [ | |
"#Comments should be precise and not repetitiitive. Use #for writing comments" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 258, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"draw\n", | |
"None\n" | |
] | |
} | |
], | |
"source": [ | |
"#Classes\n", | |
"\n", | |
"class Point: # We are using Pascal naming function\n", | |
" def move(self):\n", | |
" print(\"move\")\n", | |
"\n", | |
" def draw(self):\n", | |
" print(\"draw\")\n", | |
"\n", | |
"point1 = Point()\n", | |
"print(point1.draw())\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 257, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10\n", | |
"draw\n", | |
"10\n" | |
] | |
} | |
], | |
"source": [ | |
"point1.x = 10\n", | |
"point1.y = 20\n", | |
"print(point1.x)\n", | |
"point1.draw()\n", | |
"print(point1.x)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 252, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10\n" | |
] | |
} | |
], | |
"source": [ | |
"#Constructors\n", | |
"class Point:\n", | |
" def __init__(self, x, y):\n", | |
" self.x = x\n", | |
" self.y = y\n", | |
"\n", | |
" def move(self):\n", | |
" print(\"move\")\n", | |
"\n", | |
" def draw(self):\n", | |
" print(\"draw\")\n", | |
"\n", | |
"point1 = Point(10,20)\n", | |
"print(point1.x)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 260, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"talk\n", | |
"None\n" | |
] | |
} | |
], | |
"source": [ | |
"#Exercise\n", | |
"class Person:\n", | |
" def name(self):\n", | |
" print(\"name\")\n", | |
"\n", | |
" def talk(self):\n", | |
" print(\"talk\")\n", | |
"\n", | |
"name1 = Person()\n", | |
"print(name1.talk())" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 268, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hi, I am John Smith\n", | |
"Hi, I am Bon Smith\n" | |
] | |
} | |
], | |
"source": [ | |
"#Solution by Mosh\n", | |
"class Person:\n", | |
" def __init__(self, name):\n", | |
" self.name = name\n", | |
"\n", | |
" def talk(self):\n", | |
" print(f\"Hi, I am {self.name}\")\n", | |
"\n", | |
"john = Person(\"John Smith\")\n", | |
"john.talk()\n", | |
"\n", | |
"bob = Person(\"Bob Smith\")\n", | |
"bob.talk()" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 274, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"walk\n", | |
"bark\n", | |
"annoying\n" | |
] | |
} | |
], | |
"source": [ | |
"#Inheritance\n", | |
"class Mammal:\n", | |
" def walk(self):\n", | |
" print(\"walk\")\n", | |
"\n", | |
"class Dog(Mammal):\n", | |
" def bark(self):\n", | |
" print(\"bark\")\n", | |
"\n", | |
"\n", | |
"class Cat(Mammal):\n", | |
" def be_annoying(self):\n", | |
" print(\"annoying\")\n", | |
"\n", | |
"dog1 = Dog()\n", | |
"dog1.walk()\n", | |
"dog1.bark()\n", | |
"cat1 = Cat()\n", | |
"cat1.be_annoying()" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 273, | |
"outputs": [], | |
"source": [ | |
"#Modules\n", | |
"def lbs_to_kg(weight):\n", | |
" return weight * 0.45\n", | |
"\n", | |
"def kg_to_lbs(weight):\n", | |
" return weight / 0.45" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 276, | |
"outputs": [], | |
"source": [ | |
"import convertes\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 280, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"90.0\n", | |
"177.77777777777777\n" | |
] | |
} | |
], | |
"source": [ | |
"print(convertes.lbs_to_kg(200))\n", | |
"print(convertes.kg_to_lbs(80))\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 281, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"155.55555555555554\n" | |
] | |
} | |
], | |
"source": [ | |
"from convertes import kg_to_lbs\n", | |
"\n", | |
"kg_to_lbs(100)\n", | |
"\n", | |
"print(convertes.kg_to_lbs(70))" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 288, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": "'\\nnumbers = [3, 6,2, 8, 4, 10]\\nmax = numbers[0]\\n\\nfor number in numbers:\\n if number > max:\\n max = number\\nprint(max)\\n\\n'" | |
}, | |
"execution_count": 288, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#Exercise - Create functions or modules for below code\n", | |
"\n", | |
"\"\"\"\n", | |
"numbers = [3, 6,2, 8, 4, 10]\n", | |
"max = numbers[0]\n", | |
"\n", | |
"for number in numbers:\n", | |
" if number > max:\n", | |
" max = number\n", | |
"print(max)\n", | |
"\n", | |
"\"\"\"" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 317, | |
"outputs": [ | |
{ | |
"ename": "ImportError", | |
"evalue": "cannot import name 'max1' from 'utils2' (/Users/rafatsiddiqui/Downloads/Code with Mosh/utils2.py)", | |
"output_type": "error", | |
"traceback": [ | |
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", | |
"\u001B[0;31mImportError\u001B[0m Traceback (most recent call last)", | |
"\u001B[0;32m/var/folders/zh/w6kxh4611fz3my_7yl9hrz600000gn/T/ipykernel_19614/175475403.py\u001B[0m in \u001B[0;36m<module>\u001B[0;34m\u001B[0m\n\u001B[0;32m----> 1\u001B[0;31m \u001B[0;32mfrom\u001B[0m \u001B[0mutils2\u001B[0m \u001B[0;32mimport\u001B[0m \u001B[0mmax1\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m 2\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 3\u001B[0m \u001B[0mtest\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0;34m[\u001B[0m\u001B[0;36m2\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;36m34\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;36m4545\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;36m657\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;36m578\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;36m797\u001B[0m\u001B[0;34m]\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 4\u001B[0m \u001B[0mmax2\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0mmax1\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mtest\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 5\u001B[0m \u001B[0mprint\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mmax2\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n", | |
"\u001B[0;31mImportError\u001B[0m: cannot import name 'max1' from 'utils2' (/Users/rafatsiddiqui/Downloads/Code with Mosh/utils2.py)" | |
] | |
} | |
], | |
"source": [ | |
"from utils2 import max1\n", | |
"\n", | |
"test = [2,34,4545,657,578,797]\n", | |
"max2 = max1(test)\n", | |
"print(max2)\n", | |
"\n", | |
"#COULD NOT SOLVE" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 316, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"97979797\n" | |
] | |
} | |
], | |
"source": [ | |
"#Solution by Mosh\n", | |
"from util import find_max\n", | |
"numbers = [2,34,4545,657,578,797,97979797]\n", | |
"max = find_max(numbers)\n", | |
"print(max)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 318, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"calc_shipping\n" | |
] | |
} | |
], | |
"source": [ | |
"#Packages\n", | |
"import ecommerce.shipping\n", | |
"ecommerce.shipping.calculate_shipping()" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 319, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"calc_shipping\n" | |
] | |
} | |
], | |
"source": [ | |
"from ecommerce.shipping import calculate_shipping\n", | |
"from ecommerce import shipping\n", | |
"\n", | |
"ecommerce.shipping.calculate_shipping()\n", | |
"\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 328, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.6182633075688364\n", | |
"0.05237814894249837\n", | |
"0.7029762655291829\n", | |
"18\n", | |
"13\n", | |
"15\n", | |
"Bob\n" | |
] | |
} | |
], | |
"source": [ | |
"# Generating Random Value\n", | |
"import random\n", | |
"\n", | |
"for i in range(3):\n", | |
" print(random.random())\n", | |
"\n", | |
"for i in range(3):\n", | |
" print(random.randint(10,20))\n", | |
"\n", | |
"members = ['John', 'Mary', 'Bob', 'Mosh']\n", | |
"leader = random.choice(members)\n", | |
"print(leader)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 345, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(2,1)\n" | |
] | |
} | |
], | |
"source": [ | |
"#Exercise - Roll a dice\n", | |
"def roll():\n", | |
" for i in range(6):\n", | |
" x = random.randint(1,6)\n", | |
" y = random.randint(1,6)\n", | |
" print(f'({x},{y})')\n", | |
"\n", | |
"roll()\n", | |
"#Got it in one shot...yayyyy!!" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 349, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(5, 5)\n" | |
] | |
} | |
], | |
"source": [ | |
"#Solution by Mosh\n", | |
"import random\n", | |
"\n", | |
"class Dice:\n", | |
" def roll(self):\n", | |
" first = random.randint(1,6)\n", | |
" second = random.randint(1,6)\n", | |
" return first, second\n", | |
"dice = Dice()\n", | |
"print(dice.roll())" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 362, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"None\n", | |
"None\n", | |
"<generator object Path.glob at 0x7ff3f9e467b0>\n", | |
"<generator object Path.glob at 0x7ff3f9e467b0>\n", | |
"convertes.py\n", | |
"utils2.py\n", | |
"main.py\n", | |
"app.ipynb\n", | |
"ecommerce\n", | |
"convertes.py\n", | |
"__pycache__\n", | |
"venv\n", | |
"utils2.py\n", | |
"main.py\n", | |
".idea\n" | |
] | |
} | |
], | |
"source": [ | |
"#Files and Directories\n", | |
"\n", | |
"# Absolute path\n", | |
"# On Windows - c:\\Program Files\\Microsoft\n", | |
"# On Mac - /usr/local/bin\n", | |
"# Relative path\n", | |
"from pathlib import Path\n", | |
"\n", | |
"path = Path(\"emails\")\n", | |
"print(path.mkdir()) #Creates directory\n", | |
"print(path.rmdir()) #Remove directory\n", | |
"print(path.glob('*.*')) #Shows all files in current path (but no directories)\n", | |
"print(path.glob('*.py')) #Shows files of specific types\n", | |
"\n", | |
"path = Path()\n", | |
"for file in path.glob('*.py'):\n", | |
" print(file)\n", | |
"\n", | |
"path = Path()\n", | |
"for file in path.glob('*'):\n", | |
" print(file)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 363, | |
"outputs": [], | |
"source": [ | |
"#PyPI" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 379, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"transaction_id\n", | |
"4\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"5.95\n", | |
"6.95\n", | |
"7.95\n" | |
] | |
} | |
], | |
"source": [ | |
"#Automation using Python\n", | |
"import openpyxl as xl\n", | |
"wb = xl.load_workbook('Book1.xlsx')\n", | |
"sheet = wb['Sheet1']\n", | |
"cell = sheet['a1']\n", | |
"cell = sheet.cell(1,1,)\n", | |
"print(cell.value)\n", | |
"print(sheet.max_row)\n", | |
"\n", | |
"for row in range(1,sheet.max_row + 1):\n", | |
" print(row)\n", | |
"\n", | |
"for row in range(2,sheet.max_row + 1):\n", | |
" print(row)\n", | |
"\n", | |
"for row in range(2,sheet.max_row + 1):\n", | |
" cell = sheet.cell(row,3)\n", | |
" print(cell.value)\n", | |
"\n", | |
"for row in range(2,sheet.max_row + 1):\n", | |
" cell = sheet.cell(row,3)\n", | |
" corrected_price = cell.value*0.9\n", | |
" corrected_price_cell = sheet.cell(row,4)\n", | |
" corrected_price_cell.value = corrected_price\n", | |
"\n", | |
"# Charts\n", | |
"from openpyxl.chart import BarChart, Reference\n", | |
"values = Reference(sheet,\n", | |
" min_row = 2,\n", | |
" min_col = 4,\n", | |
" max_col = 4,\n", | |
" max_row = sheet.max_row)\n", | |
"\n", | |
"chart= BarChart()\n", | |
"chart.add_data(values)\n", | |
"sheet.add_chart(chart, 'e2')\n", | |
"wb.save('Book2.xlsx')\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 381, | |
"outputs": [], | |
"source": [ | |
"#Cleaned up code\n", | |
"import openpyxl as xl\n", | |
"from openpyxl.chart import BarChart, Reference\n", | |
"\n", | |
"def proces_workbook(filename):\n", | |
" wb = xl.load_workbook(filename)\n", | |
" sheet = wb['Sheet1']\n", | |
"\n", | |
" for row in range(2,sheet.max_row + 1):\n", | |
" cell = sheet.cell(row,3)\n", | |
" corrected_price = cell.value*0.9\n", | |
" corrected_price_cell = sheet.cell(row,4)\n", | |
" corrected_price_cell.value = corrected_price\n", | |
"\n", | |
" # Charts\n", | |
"\n", | |
" values = Reference(sheet,\n", | |
" min_row = 2,\n", | |
" min_col = 4,\n", | |
" max_col = 4,\n", | |
" max_row = sheet.max_row)\n", | |
"\n", | |
" chart= BarChart()\n", | |
" chart.add_data(values)\n", | |
" sheet.add_chart(chart, 'e2')\n", | |
" wb.save(filename)\n", | |
"\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 383, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": "'\\nSteps in Machine Learning\\n1. Import the Data\\n2. Clean the Data\\n3. Split the Data into Training/Test Sets\\n4. Create a Model\\n5. Train the Model\\n6. Make Predictions\\n7. Evaluate and Improve\\n'" | |
}, | |
"execution_count": 383, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#Machine Learning\n", | |
"\n", | |
"\"\"\"\n", | |
"Steps in Machine Learning\n", | |
"1. Import the Data\n", | |
"2. Clean the Data\n", | |
"3. Split the Data into Training/Test Sets\n", | |
"4. Create a Model\n", | |
"5. Train the Model\n", | |
"6. Make Predictions\n", | |
"7. Evaluate and Improve\n", | |
"\"\"\"" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 384, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": "'\\nPopular Libraries are\\na. Numpy\\nb. Pandas\\nc. MatPlotLib\\nd. Scikit-Learn\\n'" | |
}, | |
"execution_count": 384, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Libraries and Tools in Machine Learning Projects\n", | |
"\"\"\"\n", | |
"Popular Libraries are\n", | |
"a. Numpy\n", | |
"b. Pandas\n", | |
"c. MatPlotLib\n", | |
"d. Scikit-Learn\n", | |
"\"\"\"" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 390, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": "array([[1, 'Wii Sports', 'Wii', ..., 3.77, 8.46, 82.74],\n [2, 'Super Mario Bros.', 'NES', ..., 6.81, 0.77, 40.24],\n [3, 'Mario Kart Wii', 'Wii', ..., 3.79, 3.31, 35.82],\n ...,\n [16598, 'SCORE International Baja 1000: The Official Game', 'PS2',\n ..., 0.0, 0.0, 0.01],\n [16599, 'Know How 2', 'DS', ..., 0.0, 0.0, 0.01],\n [16600, 'Spirits & Spells', 'GBA', ..., 0.0, 0.0, 0.01]],\n dtype=object)" | |
}, | |
"execution_count": 390, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#Importing a Data Set\n", | |
"import pandas as pd #Importing pandas library\n", | |
"pd.read_csv('vgsales.csv') #reading the dataset. Download the dataset from https://www.kaggle.com/gregorut/videogamesales\n", | |
"df = pd.read_csv('vgsales.csv') #naming the data set\n", | |
"df.shape #shows the shape of the dataset\n", | |
"df.describe() #gives basic information about each coloumn in the dataset\n", | |
"df.values #shows the few rows of the data" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 410, | |
"outputs": [], | |
"source": [ | |
"#A Real Problem\n", | |
"import pandas as pd\n", | |
"music_data = pd.read_csv('music.csv')" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 411, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": "(18, 3)" | |
}, | |
"execution_count": 411, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"music_data.describe()\n", | |
"music_data.values\n", | |
"music_data.shape" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 415, | |
"outputs": [], | |
"source": [ | |
"#Cleaning the data\n", | |
"X = music_data.drop(columns=['genre'])\n", | |
"y = music_data['genre']" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 419, | |
"outputs": [], | |
"source": [ | |
"#Learning and Predicting\n", | |
"from sklearn.tree import DecisionTreeClassifier #Decision Tree Classifier is a Machine Learning algorithm\n", | |
"\n", | |
"model = DecisionTreeClassifier()\n", | |
"model.fit(X,y)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 424, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": "array(['HipHop', 'Dance'], dtype=object)" | |
}, | |
"execution_count": 424, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"predictions = model.predict([[21,1], [22,0]])\n", | |
"predictions\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 474, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": "1.0" | |
}, | |
"execution_count": 474, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#Calculating the accuracy of the model\n", | |
"from sklearn.model_selection import train_test_split #This will split the data into train and test sets\n", | |
"from sklearn.metrics import accuracy_score #This will be used for testing the accuracy of the model\n", | |
"X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2) #Depending upon the ration of train/test, the accuracy level will change\n", | |
"model = DecisionTreeClassifier()\n", | |
"model.fit(X_train, y_train)\n", | |
"predictions = model.predict(X_test)\n", | |
"\n", | |
"accuracy_score(y_test, predictions)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 482, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['HipHop' 'Dance']\n" | |
] | |
} | |
], | |
"source": [ | |
"#Model Persistence\n", | |
"import pandas as pd\n", | |
"from sklearn.tree import DecisionTreeClassifier\n", | |
"import joblib\n", | |
"\n", | |
"music_data = pd.read_csv('music.csv')\n", | |
"X = music_data.drop(columns=['genre'])\n", | |
"y = music_data['genre']\n", | |
"\n", | |
"model = DecisionTreeClassifier()\n", | |
"model.fit(X,y)\n", | |
"\n", | |
"joblib.dump(model,'music-recommender.joblib') #Creates and saves the model which can be used later and doesn't need to be retrained again\n", | |
"predictions = model.predict([[21,1], [22,0]])\n", | |
"print(predictions)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 481, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['HipHop']\n" | |
] | |
} | |
], | |
"source": [ | |
"#Loading the model\n", | |
"\n", | |
"model = joblib.load('music-recommender.joblib')\n", | |
"\n", | |
"predictions = model.predict([[22,1]])\n", | |
"print(predictions)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 485, | |
"outputs": [], | |
"source": [ | |
"#Visualizing Decision Tree\n", | |
"import pandas as pd\n", | |
"from sklearn.tree import DecisionTreeClassifier\n", | |
"from sklearn import tree\n", | |
"\n", | |
"music_data = pd.read_csv('music.csv')\n", | |
"X = music_data.drop(columns=['genre'])\n", | |
"y = music_data['genre']\n", | |
"\n", | |
"model = DecisionTreeClassifier()\n", | |
"model.fit(X,y)\n", | |
"\n", | |
"tree.export_graphviz(model, out_file='music-recommender.dot',\n", | |
" feature_names=['age','gender'],\n", | |
" class_names=sorted(y.unique()),\n", | |
" label='all',\n", | |
" rounded= True,\n", | |
" filled=True)" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [ | |
"#Thanks a lot to Mosh. Visit codewithmosh.com for more courses\n", | |
"\n" | |
], | |
"metadata": { | |
"collapsed": false, | |
"pycharm": { | |
"name": "#%%\n" | |
} | |
} | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment