All language subtitles for [English (auto-generated)] Python Full Course for Beginners [DownSub.com] (1)

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (SoranĂ®)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,399 --> 00:00:01,920 hi my name is mosh and i'm going to be 2 00:00:01,920 --> 00:00:03,919 your instructor in this python course in 3 00:00:03,919 --> 00:00:04,960 this course you're going to learn 4 00:00:04,960 --> 00:00:06,560 everything you need to get started 5 00:00:06,560 --> 00:00:08,880 programming in python python is one of 6 00:00:08,880 --> 00:00:10,719 the most popular programming languages 7 00:00:10,719 --> 00:00:12,400 and every day people use it to do cool 8 00:00:12,400 --> 00:00:15,040 things like automation they use it in ai 9 00:00:15,040 --> 00:00:16,640 as well as building applications and 10 00:00:16,640 --> 00:00:19,600 websites like instagram and dropbox so 11 00:00:19,600 --> 00:00:21,279 if you're looking for a job python is 12 00:00:21,279 --> 00:00:23,039 for you in this course i'm going to 13 00:00:23,039 --> 00:00:24,880 teach you everything you need to get 14 00:00:24,880 --> 00:00:26,640 started with python we're going to talk 15 00:00:26,640 --> 00:00:28,800 about all the core concepts in python 16 00:00:28,800 --> 00:00:30,800 and then we're gonna build three python 17 00:00:30,800 --> 00:00:32,640 projects together here's the first 18 00:00:32,640 --> 00:00:34,079 project we're gonna build you're gonna 19 00:00:34,079 --> 00:00:35,520 learn how to create this beautiful 20 00:00:35,520 --> 00:00:38,239 website for an imaginary grocery store 21 00:00:38,239 --> 00:00:39,920 here on the home page we can see all the 22 00:00:39,920 --> 00:00:42,160 products in the shop and we also have an 23 00:00:42,160 --> 00:00:44,640 admin area for managing the stock we're 24 00:00:44,640 --> 00:00:46,160 going to build this using a popular 25 00:00:46,160 --> 00:00:48,160 python framework called django now if 26 00:00:48,160 --> 00:00:49,760 you have never built a website before 27 00:00:49,760 --> 00:00:51,199 don't worry i will teach you everything 28 00:00:51,199 --> 00:00:52,800 from scratch you're also going to learn 29 00:00:52,800 --> 00:00:54,960 how to use python in machine learning or 30 00:00:54,960 --> 00:00:56,800 artificial intelligence so you will 31 00:00:56,800 --> 00:00:58,960 learn how to write a python program that 32 00:00:58,960 --> 00:01:00,399 will predict the kind of music that 33 00:01:00,399 --> 00:01:02,800 people like based on their profile just 34 00:01:02,800 --> 00:01:04,799 like how youtube recommends videos based 35 00:01:04,799 --> 00:01:06,479 on the videos you have watched before 36 00:01:06,479 --> 00:01:08,479 i'll also teach you how to use python to 37 00:01:08,479 --> 00:01:10,960 automate boring repetitive tasks that 38 00:01:10,960 --> 00:01:13,119 waste your time you will write a python 39 00:01:13,119 --> 00:01:15,280 program that will process thousands of 40 00:01:15,280 --> 00:01:17,520 stretches in under a second i've 41 00:01:17,520 --> 00:01:19,280 designed this python course for anyone 42 00:01:19,280 --> 00:01:20,880 who wants to learn python if you're a 43 00:01:20,880 --> 00:01:22,560 beginner don't worry i will hold your 44 00:01:22,560 --> 00:01:24,400 hands through this entire course you're 45 00:01:24,400 --> 00:01:26,479 not too old or too young and python is 46 00:01:26,479 --> 00:01:27,920 super easy to learn you can write your 47 00:01:27,920 --> 00:01:29,840 first python program in literally 48 00:01:29,840 --> 00:01:31,520 seconds plus i'm going to give you 49 00:01:31,520 --> 00:01:33,520 plenty of exercises to help you build 50 00:01:33,520 --> 00:01:35,680 your confidence writing cool python 51 00:01:35,680 --> 00:01:37,840 programs my name is mosh i'm a software 52 00:01:37,840 --> 00:01:40,000 engineer with two decades of experience 53 00:01:40,000 --> 00:01:41,680 and i've taught over 3 million people 54 00:01:41,680 --> 00:01:43,520 how to code i'm super excited to be 55 00:01:43,520 --> 00:01:45,520 teaching you python in this course so i 56 00:01:45,520 --> 00:01:47,040 hope you stick around and learn this 57 00:01:47,040 --> 00:01:48,880 beautiful and powerful programming 58 00:01:48,880 --> 00:01:51,880 language 59 00:01:54,000 --> 00:01:55,520 this tutorial i'm going to show you how 60 00:01:55,520 --> 00:01:57,600 to download and install python on your 61 00:01:57,600 --> 00:01:59,680 computer so the first thing i want you 62 00:01:59,680 --> 00:02:02,240 to do is open up your browser and head 63 00:02:02,240 --> 00:02:03,399 over to 64 00:02:03,399 --> 00:02:04,960 python.org 65 00:02:04,960 --> 00:02:08,080 on this page click on downloads here you 66 00:02:08,080 --> 00:02:09,920 can see the latest version of python 67 00:02:09,920 --> 00:02:11,760 currently at the time of recording this 68 00:02:11,760 --> 00:02:16,400 video the latest version is python 3.7.2 69 00:02:16,400 --> 00:02:18,000 chances are in the future when you're 70 00:02:18,000 --> 00:02:19,920 watching this tutorial there is a newer 71 00:02:19,920 --> 00:02:22,000 version of python available don't worry 72 00:02:22,000 --> 00:02:23,280 all the materials you're going to learn 73 00:02:23,280 --> 00:02:25,440 in this course will apply to the latest 74 00:02:25,440 --> 00:02:27,680 version of python as well so let's go 75 00:02:27,680 --> 00:02:31,440 ahead and download python 3. 76 00:02:31,440 --> 00:02:32,800 all right now look at your downloads 77 00:02:32,800 --> 00:02:35,680 folder here we should have python 3 78 00:02:35,680 --> 00:02:39,040 installer simply double click that 79 00:02:39,040 --> 00:02:40,560 if you're on windows you're going to see 80 00:02:40,560 --> 00:02:42,319 this checkbox here 81 00:02:42,319 --> 00:02:44,879 add python to path this is really 82 00:02:44,879 --> 00:02:46,879 important make sure to tick this box 83 00:02:46,879 --> 00:02:48,239 otherwise you're not going to be able to 84 00:02:48,239 --> 00:02:50,959 follow this tutorial 85 00:02:50,959 --> 00:02:52,319 if you're on a mac you're going to see 86 00:02:52,319 --> 00:02:54,239 an installer like this with this setup 87 00:02:54,239 --> 00:02:56,560 wizard we can install python 3 on our 88 00:02:56,560 --> 00:02:59,120 computer so simply click continue and 89 00:02:59,120 --> 00:03:01,760 again and again i agree with the license 90 00:03:01,760 --> 00:03:03,519 agreement 91 00:03:03,519 --> 00:03:06,159 and install it 92 00:03:06,159 --> 00:03:07,599 this is going to take a few seconds so 93 00:03:07,599 --> 00:03:10,879 i'm going to pause the recording 94 00:03:12,400 --> 00:03:14,239 all right we have successfully installed 95 00:03:14,239 --> 00:03:18,720 python3 on our computer let's close this 96 00:03:18,720 --> 00:03:20,720 all right next we need to install a code 97 00:03:20,720 --> 00:03:23,040 editor we use a code editor to write our 98 00:03:23,040 --> 00:03:25,280 code just like how we use microsoft word 99 00:03:25,280 --> 00:03:27,440 to write documents we use a code editor 100 00:03:27,440 --> 00:03:29,599 to write code now there's so many code 101 00:03:29,599 --> 00:03:31,280 editors out there the one that i'm going 102 00:03:31,280 --> 00:03:33,680 to show you in this tutorial is pycharm 103 00:03:33,680 --> 00:03:35,519 that is one of the most popular code 104 00:03:35,519 --> 00:03:38,159 editors for writing python code you can 105 00:03:38,159 --> 00:03:40,879 get it from jetbrains.com 106 00:03:40,879 --> 00:03:42,319 pycharm 107 00:03:42,319 --> 00:03:44,239 now more accurately pycharm is 108 00:03:44,239 --> 00:03:46,720 considered an ide which is short for 109 00:03:46,720 --> 00:03:49,120 integrated development environment and 110 00:03:49,120 --> 00:03:50,959 that's basically a code editor on 111 00:03:50,959 --> 00:03:53,280 steroids it has some additional features 112 00:03:53,280 --> 00:03:55,760 that makes it really easy to rate code 113 00:03:55,760 --> 00:03:59,120 so let's go ahead and download pycharm 114 00:03:59,120 --> 00:04:00,640 now here on the download page you can 115 00:04:00,640 --> 00:04:02,959 see two versions of pycharm one is the 116 00:04:02,959 --> 00:04:04,560 professional edition which has 117 00:04:04,560 --> 00:04:06,080 additional features and you'll have to 118 00:04:06,080 --> 00:04:08,239 pay for them the other is the community 119 00:04:08,239 --> 00:04:10,159 edition that is absolutely free and 120 00:04:10,159 --> 00:04:11,519 that's what we're going to use in this 121 00:04:11,519 --> 00:04:14,080 tutorial so go ahead and download this 122 00:04:14,080 --> 00:04:16,239 as well 123 00:04:16,239 --> 00:04:17,759 now once again look at your downloads 124 00:04:17,759 --> 00:04:20,639 folder here you should have pycharm so 125 00:04:20,639 --> 00:04:23,440 double click this 126 00:04:23,440 --> 00:04:24,880 now if you're on windows you're going to 127 00:04:24,880 --> 00:04:26,960 see an installation wizard simply click 128 00:04:26,960 --> 00:04:29,520 next next next until you install pycharm 129 00:04:29,520 --> 00:04:31,199 if you're on mac you need to drag and 130 00:04:31,199 --> 00:04:33,440 drop this icon onto the applications 131 00:04:33,440 --> 00:04:36,160 folder so drag and drop 132 00:04:36,160 --> 00:04:38,080 all right now let's double click this to 133 00:04:38,080 --> 00:04:40,000 run it 134 00:04:40,000 --> 00:04:41,360 the first time you run pycharm you're 135 00:04:41,360 --> 00:04:42,960 gonna get this warning because this is 136 00:04:42,960 --> 00:04:44,560 an application that you download from 137 00:04:44,560 --> 00:04:46,880 the internet so let's go ahead and open 138 00:04:46,880 --> 00:04:48,000 it 139 00:04:48,000 --> 00:04:49,600 next you're gonna see this dialog box 140 00:04:49,600 --> 00:04:51,759 for importing some settings leave this 141 00:04:51,759 --> 00:04:53,520 to do not import settings it doesn't 142 00:04:53,520 --> 00:04:54,840 really matter 143 00:04:54,840 --> 00:04:57,759 okay on this page select i've never used 144 00:04:57,759 --> 00:04:59,680 pycharm you can see some keyword 145 00:04:59,680 --> 00:05:01,199 shortcuts that might be useful in the 146 00:05:01,199 --> 00:05:03,520 future just accept this and down the 147 00:05:03,520 --> 00:05:06,400 bottom click the next button 148 00:05:06,400 --> 00:05:08,160 and then next again 149 00:05:08,160 --> 00:05:10,880 one more time and finally start using 150 00:05:10,880 --> 00:05:13,199 pie chart 151 00:05:13,199 --> 00:05:14,720 so here's the main page of pie chart 152 00:05:14,720 --> 00:05:15,759 that you're going to see every time you 153 00:05:15,759 --> 00:05:19,759 open it click on create new project 154 00:05:19,759 --> 00:05:21,759 this is the location for our project 155 00:05:21,759 --> 00:05:24,479 let's call this project hello 156 00:05:24,479 --> 00:05:25,680 world 157 00:05:25,680 --> 00:05:28,160 now before clicking create expand this 158 00:05:28,160 --> 00:05:29,440 item here 159 00:05:29,440 --> 00:05:31,520 make sure that base interpreter is set 160 00:05:31,520 --> 00:05:33,600 to python 3. 161 00:05:33,600 --> 00:05:35,440 python interpreter is basically a 162 00:05:35,440 --> 00:05:38,000 program that knows how to execute python 163 00:05:38,000 --> 00:05:40,800 code it will interpret or translate 164 00:05:40,800 --> 00:05:42,639 python instructions into instructions 165 00:05:42,639 --> 00:05:44,960 that a computer can understand now mac 166 00:05:44,960 --> 00:05:47,759 computers by default come with an older 167 00:05:47,759 --> 00:05:50,720 installation of python that is python 2. 168 00:05:50,720 --> 00:05:52,639 it's considered legacy which means it's 169 00:05:52,639 --> 00:05:54,800 no longer maintained or supported so 170 00:05:54,800 --> 00:05:57,680 earlier we downloaded python3 make sure 171 00:05:57,680 --> 00:06:00,400 that this is set to python3 if not from 172 00:06:00,400 --> 00:06:04,240 this list select python 3.7 173 00:06:04,240 --> 00:06:06,800 all right now let's go ahead and create 174 00:06:06,800 --> 00:06:09,520 this project 175 00:06:14,560 --> 00:06:16,479 now here right click on hello world 176 00:06:16,479 --> 00:06:19,440 folder and go to new 177 00:06:19,440 --> 00:06:21,360 python file 178 00:06:21,360 --> 00:06:24,400 call this file app.pi 179 00:06:24,400 --> 00:06:27,199 so by convention all python files should 180 00:06:27,199 --> 00:06:29,440 have this pi extension 181 00:06:29,440 --> 00:06:31,680 all right let's go ahead 182 00:06:31,680 --> 00:06:34,000 now let's collapse this project panel by 183 00:06:34,000 --> 00:06:36,880 clicking here and write our first python 184 00:06:36,880 --> 00:06:40,960 program simply type print pr int all in 185 00:06:40,960 --> 00:06:44,639 lowercase open and close parenthesis and 186 00:06:44,639 --> 00:06:47,759 inside this parenthesis add quotations 187 00:06:47,759 --> 00:06:50,160 you can either use single quotes or 188 00:06:50,160 --> 00:06:51,520 double quotes 189 00:06:51,520 --> 00:06:53,759 now in between the codes write your name 190 00:06:53,759 --> 00:06:55,840 i am mash hamadani so let's write that 191 00:06:55,840 --> 00:06:56,639 here 192 00:06:56,639 --> 00:06:59,120 so this is your first python program 193 00:06:59,120 --> 00:07:00,720 with this piece of code you can print 194 00:07:00,720 --> 00:07:02,720 your name on the screen 195 00:07:02,720 --> 00:07:05,120 now to run this go on the top 196 00:07:05,120 --> 00:07:08,639 under the run menu click run also note 197 00:07:08,639 --> 00:07:10,639 that there is a shortcut associated with 198 00:07:10,639 --> 00:07:12,960 this command when i program i always use 199 00:07:12,960 --> 00:07:14,840 shortcuts because that increases my 200 00:07:14,840 --> 00:07:17,120 productivity so here on a mac computer 201 00:07:17,120 --> 00:07:20,400 the shortcut is control option and r on 202 00:07:20,400 --> 00:07:22,560 windows is different so let's go ahead 203 00:07:22,560 --> 00:07:25,039 with that now it's asking where you want 204 00:07:25,039 --> 00:07:27,520 to run this click on app 205 00:07:27,520 --> 00:07:29,520 now down below 206 00:07:29,520 --> 00:07:30,880 you should see this little terminal 207 00:07:30,880 --> 00:07:32,800 window this is like a little window into 208 00:07:32,800 --> 00:07:34,720 our program so here we can see the 209 00:07:34,720 --> 00:07:37,199 results or output of our program in the 210 00:07:37,199 --> 00:07:39,440 future as you learn more python you'll 211 00:07:39,440 --> 00:07:41,120 be able to build applications that have 212 00:07:41,120 --> 00:07:44,000 a graphical user interface or gui that 213 00:07:44,000 --> 00:07:46,000 is a little bit complicated so for now 214 00:07:46,000 --> 00:07:47,759 we're going to use this little terminal 215 00:07:47,759 --> 00:07:50,160 window to see the results of our program 216 00:07:50,160 --> 00:07:52,479 so as you can see my name is printed 217 00:07:52,479 --> 00:07:54,560 here now as you write code this window 218 00:07:54,560 --> 00:07:56,720 might get in the way so you can always 219 00:07:56,720 --> 00:07:59,360 resize it or minimize it by clicking 220 00:07:59,360 --> 00:08:00,960 this icon here 221 00:08:00,960 --> 00:08:03,759 so this was our first python program now 222 00:08:03,759 --> 00:08:05,280 if you didn't see the result i showed 223 00:08:05,280 --> 00:08:07,360 you in this video use the comment box 224 00:08:07,360 --> 00:08:08,879 below and tell me what error you 225 00:08:08,879 --> 00:08:10,720 encountered i'll do my best to help you 226 00:08:10,720 --> 00:08:13,840 move forward 227 00:08:15,840 --> 00:08:17,280 in this python tutorial we're going to 228 00:08:17,280 --> 00:08:18,800 take this program to the next level 229 00:08:18,800 --> 00:08:20,479 we're going to make it more interesting 230 00:08:20,479 --> 00:08:22,319 so i'm going to show you how to draw a 231 00:08:22,319 --> 00:08:23,440 dog here 232 00:08:23,440 --> 00:08:25,039 as part of this tutorial you will learn 233 00:08:25,039 --> 00:08:27,440 how python code gets executed and you 234 00:08:27,440 --> 00:08:29,360 will also learn about a few programming 235 00:08:29,360 --> 00:08:30,240 terms 236 00:08:30,240 --> 00:08:33,120 so press enter and on line two write 237 00:08:33,120 --> 00:08:35,760 another print statement so print open 238 00:08:35,760 --> 00:08:38,479 and close parenthesis add a quotation 239 00:08:38,479 --> 00:08:41,120 now here we want to draw a dog so i 240 00:08:41,120 --> 00:08:43,599 don't know that is the head of our dog 241 00:08:43,599 --> 00:08:46,240 followed by four hyphens one two three 242 00:08:46,240 --> 00:08:48,800 four so this is the body all right now 243 00:08:48,800 --> 00:08:50,000 one more time 244 00:08:50,000 --> 00:08:52,720 another print statement with quotations 245 00:08:52,720 --> 00:08:55,040 now we need to draw the legs so add a 246 00:08:55,040 --> 00:08:56,240 space 247 00:08:56,240 --> 00:08:59,600 followed by four vertical bars so like 248 00:08:59,600 --> 00:09:01,120 this 249 00:09:01,120 --> 00:09:03,760 so here's a little imaginary dog now 250 00:09:03,760 --> 00:09:05,200 let's run this program and see what we 251 00:09:05,200 --> 00:09:06,720 get so in the top right corner you 252 00:09:06,720 --> 00:09:09,200 should see this play button click that 253 00:09:09,200 --> 00:09:11,839 there you go so we have our name and 254 00:09:11,839 --> 00:09:14,000 right below that we have our imaginary 255 00:09:14,000 --> 00:09:14,959 dog 256 00:09:14,959 --> 00:09:16,560 now what you need to understand here is 257 00:09:16,560 --> 00:09:19,279 that our python code gets executed line 258 00:09:19,279 --> 00:09:21,200 by line from the top 259 00:09:21,200 --> 00:09:23,440 so earlier i told you about python 260 00:09:23,440 --> 00:09:25,680 interpreter that is the program that 261 00:09:25,680 --> 00:09:28,399 knows how to translate or interpret our 262 00:09:28,399 --> 00:09:30,800 python code into instructions that a 263 00:09:30,800 --> 00:09:32,959 computer can understand so when we run 264 00:09:32,959 --> 00:09:35,680 this program by clicking this icon here 265 00:09:35,680 --> 00:09:38,480 python interpreter starts executing or 266 00:09:38,480 --> 00:09:41,200 running our program line by line from 267 00:09:41,200 --> 00:09:44,000 the top so first it executes line one 268 00:09:44,000 --> 00:09:47,040 then moves on to line two and so on 269 00:09:47,040 --> 00:09:48,800 so this is how python programs get 270 00:09:48,800 --> 00:09:50,640 executed now let me show you something 271 00:09:50,640 --> 00:09:54,240 cool let's add another print statement 272 00:09:54,240 --> 00:09:56,480 with quotations now in between the 273 00:09:56,480 --> 00:09:59,760 quotations add a star or an asterisk 274 00:09:59,760 --> 00:10:02,640 like this now after the quotation and 275 00:10:02,640 --> 00:10:05,600 before the parenthesis add a space once 276 00:10:05,600 --> 00:10:08,720 again add an asterisk space 277 00:10:08,720 --> 00:10:10,079 10. 278 00:10:10,079 --> 00:10:11,600 what is going on here 279 00:10:11,600 --> 00:10:12,399 well 280 00:10:12,399 --> 00:10:15,200 anywhere we have quotations like here or 281 00:10:15,200 --> 00:10:18,399 here we're defining a string a string is 282 00:10:18,399 --> 00:10:20,720 a programming term which means a series 283 00:10:20,720 --> 00:10:23,200 of characters so here we have a string 284 00:10:23,200 --> 00:10:25,680 we also have a string on line three 285 00:10:25,680 --> 00:10:28,000 as well as line two and line one 286 00:10:28,000 --> 00:10:31,600 now here we are multiplying the string 287 00:10:31,600 --> 00:10:33,440 by number 10. so this is the 288 00:10:33,440 --> 00:10:35,200 multiplication operator just like the 289 00:10:35,200 --> 00:10:38,000 multiplication operator we have in math 290 00:10:38,000 --> 00:10:40,399 so with this piece of code we can draw 291 00:10:40,399 --> 00:10:42,640 10 asterisks on the terminal let me show 292 00:10:42,640 --> 00:10:44,480 you so let's run this program one more 293 00:10:44,480 --> 00:10:46,720 time there you go 294 00:10:46,720 --> 00:10:49,440 so we have 10 asterisks 295 00:10:49,440 --> 00:10:51,680 now what we have here this piece of code 296 00:10:51,680 --> 00:10:52,640 here 297 00:10:52,640 --> 00:10:55,440 is called an expression an expression is 298 00:10:55,440 --> 00:10:58,480 a piece of code that produces a value so 299 00:10:58,480 --> 00:11:01,040 when py's interpreter tries to execute 300 00:11:01,040 --> 00:11:02,240 line 4 301 00:11:02,240 --> 00:11:04,800 first it will evaluate the code that we 302 00:11:04,800 --> 00:11:07,120 put in between parentheses so it will 303 00:11:07,120 --> 00:11:09,519 evaluate our expression our expression 304 00:11:09,519 --> 00:11:12,240 will produce 10 asterisks and then those 305 00:11:12,240 --> 00:11:14,079 asterisks will be printed on the 306 00:11:14,079 --> 00:11:15,120 terminal 307 00:11:15,120 --> 00:11:16,720 now as an exercise you can use this 308 00:11:16,720 --> 00:11:18,800 print statement to draw another shape 309 00:11:18,800 --> 00:11:20,959 you can draw a heart a ball whatever you 310 00:11:20,959 --> 00:11:24,959 like i will see you in the next tutorial 311 00:11:24,959 --> 00:11:26,720 one of the questions i get a lot on my 312 00:11:26,720 --> 00:11:28,480 channel is how long does it take to 313 00:11:28,480 --> 00:11:31,040 learn python and become job ready well 314 00:11:31,040 --> 00:11:33,040 there is no single answer it really 315 00:11:33,040 --> 00:11:35,040 depends on you and how much effort and 316 00:11:35,040 --> 00:11:37,200 commitment you want to put into this but 317 00:11:37,200 --> 00:11:39,680 i would say if you spend two hours every 318 00:11:39,680 --> 00:11:41,760 day consistently 319 00:11:41,760 --> 00:11:43,360 after about three months you should be 320 00:11:43,360 --> 00:11:46,079 able to write basic python programs but 321 00:11:46,079 --> 00:11:47,760 quite honestly that doesn't get you a 322 00:11:47,760 --> 00:11:48,640 job 323 00:11:48,640 --> 00:11:50,000 in order to get a job you need to 324 00:11:50,000 --> 00:11:52,160 specialize in one area what do you want 325 00:11:52,160 --> 00:11:53,760 to use python for do you want to use it 326 00:11:53,760 --> 00:11:56,480 to build web applications or desktop 327 00:11:56,480 --> 00:11:58,240 applications or do you want to use it in 328 00:11:58,240 --> 00:11:59,839 machine learning and artificial 329 00:11:59,839 --> 00:12:01,680 intelligence so whatever you want to do 330 00:12:01,680 --> 00:12:04,240 you need to take additional courses for 331 00:12:04,240 --> 00:12:05,680 example if you want to become a web 332 00:12:05,680 --> 00:12:08,240 developer in addition to learning python 333 00:12:08,240 --> 00:12:11,519 you should also learn about html css 334 00:12:11,519 --> 00:12:14,240 some javascript and django which is a 335 00:12:14,240 --> 00:12:16,480 popular python framework for building 336 00:12:16,480 --> 00:12:18,320 web applications learning all these 337 00:12:18,320 --> 00:12:20,800 things would take you another six months 338 00:12:20,800 --> 00:12:23,279 so in total you need nine to 12 months 339 00:12:23,279 --> 00:12:25,680 to become job ready at that point you 340 00:12:25,680 --> 00:12:27,839 can get a junior developer job with a 341 00:12:27,839 --> 00:12:31,360 salary of about 50 to 60 000 a year now 342 00:12:31,360 --> 00:12:33,360 as you work more as you do more python 343 00:12:33,360 --> 00:12:35,920 projects your resume starts to build and 344 00:12:35,920 --> 00:12:39,440 you can ask for 100 000 a year or 120 345 00:12:39,440 --> 00:12:41,680 000 a year depending on where you are 346 00:12:41,680 --> 00:12:44,320 what company what geographical area it 347 00:12:44,320 --> 00:12:46,480 really depends so there is no single 348 00:12:46,480 --> 00:12:48,079 answer so are you excited to learn 349 00:12:48,079 --> 00:12:49,920 python and get started on this career 350 00:12:49,920 --> 00:12:50,720 path 351 00:12:50,720 --> 00:12:52,320 if you are i would encourage you to make 352 00:12:52,320 --> 00:12:54,880 a commitment and spend two hours every 353 00:12:54,880 --> 00:12:57,279 day practicing python and use the 354 00:12:57,279 --> 00:12:59,279 comment box below and let me know why 355 00:12:59,279 --> 00:13:00,880 you're learning python what do you want 356 00:13:00,880 --> 00:13:02,880 to do with it what's your dream job i 357 00:13:02,880 --> 00:13:06,839 would love to hear your story 358 00:13:08,240 --> 00:13:09,600 in this python tutorial you're going to 359 00:13:09,600 --> 00:13:11,200 learn about variables which are one of 360 00:13:11,200 --> 00:13:13,200 the most fundamental concepts in 361 00:13:13,200 --> 00:13:14,800 programming they're not specific to 362 00:13:14,800 --> 00:13:16,800 python they exist in pretty much every 363 00:13:16,800 --> 00:13:18,639 programming language out there 364 00:13:18,639 --> 00:13:20,800 we use variables to temporarily store 365 00:13:20,800 --> 00:13:23,120 data in a computer's memory here's an 366 00:13:23,120 --> 00:13:27,200 example let's type price equals 10. 367 00:13:27,200 --> 00:13:29,040 when python interpreter executes this 368 00:13:29,040 --> 00:13:31,440 code it will allocate some memory then 369 00:13:31,440 --> 00:13:33,279 it will store the number 10 in that 370 00:13:33,279 --> 00:13:36,000 memory and finally it will attach this 371 00:13:36,000 --> 00:13:38,720 price label to that memory location 372 00:13:38,720 --> 00:13:41,519 as a metaphor imagine we have a box in 373 00:13:41,519 --> 00:13:44,480 that box we have number 10 and price is 374 00:13:44,480 --> 00:13:46,639 the label that we put on the box 375 00:13:46,639 --> 00:13:48,399 now we can use this label anywhere in 376 00:13:48,399 --> 00:13:50,639 our program to access the value that we 377 00:13:50,639 --> 00:13:52,480 have in that box this is a very 378 00:13:52,480 --> 00:13:54,800 simplified explanation 379 00:13:54,800 --> 00:13:58,000 so now let's print price on the terminal 380 00:13:58,000 --> 00:13:59,920 print now this time we're not going to 381 00:13:59,920 --> 00:14:02,000 add quotations because if we put 382 00:14:02,000 --> 00:14:04,079 quotations here we will see the text 383 00:14:04,079 --> 00:14:06,800 price on the terminal not the value of 384 00:14:06,800 --> 00:14:10,320 the price variable so delete quotations 385 00:14:10,320 --> 00:14:12,160 and type price 386 00:14:12,160 --> 00:14:14,399 now let's run this program one more time 387 00:14:14,399 --> 00:14:16,399 there you go so we see 10 on the 388 00:14:16,399 --> 00:14:17,519 terminal 389 00:14:17,519 --> 00:14:19,760 so this is how we define variables we 390 00:14:19,760 --> 00:14:22,000 start with an identifier which is the 391 00:14:22,000 --> 00:14:24,880 name of our variable then an equal sign 392 00:14:24,880 --> 00:14:27,680 and finally a value now more accurately 393 00:14:27,680 --> 00:14:29,199 when this number 10 is about to be 394 00:14:29,199 --> 00:14:31,600 stored in the memory first it will get 395 00:14:31,600 --> 00:14:34,560 converted to its binary representation 396 00:14:34,560 --> 00:14:36,639 so this number 10 that we have is in the 397 00:14:36,639 --> 00:14:38,800 decimal system which has all the digits 398 00:14:38,800 --> 00:14:41,440 from 0 to 9. computers don't understand 399 00:14:41,440 --> 00:14:43,600 all these digits they only understand 400 00:14:43,600 --> 00:14:46,160 zeros and ones so when we store the 401 00:14:46,160 --> 00:14:48,320 number 10 in the computer's memory first 402 00:14:48,320 --> 00:14:50,399 it will get converted to its binary 403 00:14:50,399 --> 00:14:52,240 representation which would be a bunch of 404 00:14:52,240 --> 00:14:54,639 zeros and ones like zero zero one zero 405 00:14:54,639 --> 00:14:57,040 zero one whatever i don't know then it 406 00:14:57,040 --> 00:14:58,560 would get stored in the computer's 407 00:14:58,560 --> 00:15:01,120 memory so let's take this program to the 408 00:15:01,120 --> 00:15:02,639 next level 409 00:15:02,639 --> 00:15:04,959 on the second line we can update the 410 00:15:04,959 --> 00:15:07,199 value of this price variable so we can 411 00:15:07,199 --> 00:15:10,240 reset it to a new value like 20. now 412 00:15:10,240 --> 00:15:12,800 when we run our program we should see 20 413 00:15:12,800 --> 00:15:14,639 because as i told you before python 414 00:15:14,639 --> 00:15:17,279 interpreter executes our code line by 415 00:15:17,279 --> 00:15:20,000 line from the top so first we set price 416 00:15:20,000 --> 00:15:23,600 to 10 then we reset it to 20 and finally 417 00:15:23,600 --> 00:15:25,680 we print it on the terminal let's run 418 00:15:25,680 --> 00:15:26,880 the program 419 00:15:26,880 --> 00:15:31,360 there you go so we see 20 here okay 420 00:15:31,360 --> 00:15:33,040 now these numbers that we have here are 421 00:15:33,040 --> 00:15:36,160 whole numbers without a decimal point in 422 00:15:36,160 --> 00:15:38,720 programming we refer to these numbers as 423 00:15:38,720 --> 00:15:41,279 integers so an integer is a number 424 00:15:41,279 --> 00:15:43,199 without a decimal point 425 00:15:43,199 --> 00:15:45,120 we can also use numbers with a decimal 426 00:15:45,120 --> 00:15:47,360 point for example here on line 2 we can 427 00:15:47,360 --> 00:15:50,480 define another variable called rating 428 00:15:50,480 --> 00:15:53,279 and we set it to 4.9 429 00:15:53,279 --> 00:15:55,120 now in programming refer to this kind of 430 00:15:55,120 --> 00:15:57,440 number as a floating point number or 431 00:15:57,440 --> 00:16:00,399 float for short so we have integers and 432 00:16:00,399 --> 00:16:02,160 floats 433 00:16:02,160 --> 00:16:04,000 we can also define a variable and set it 434 00:16:04,000 --> 00:16:06,480 to a string for example 435 00:16:06,480 --> 00:16:09,120 name equals mosh 436 00:16:09,120 --> 00:16:11,040 we also have another kind of value which 437 00:16:11,040 --> 00:16:13,680 is called boolean which can be true or 438 00:16:13,680 --> 00:16:15,680 false they are like yes and no in 439 00:16:15,680 --> 00:16:17,440 english here's an example i'm going to 440 00:16:17,440 --> 00:16:21,440 define a variable is underline published 441 00:16:21,440 --> 00:16:23,920 so we use an underscore to separate 442 00:16:23,920 --> 00:16:26,560 multiple words in our variable's name 443 00:16:26,560 --> 00:16:28,000 we set this to 444 00:16:28,000 --> 00:16:29,279 true 445 00:16:29,279 --> 00:16:30,160 or 446 00:16:30,160 --> 00:16:33,360 false these are the boolean values 447 00:16:33,360 --> 00:16:36,079 now note that python is a case sensitive 448 00:16:36,079 --> 00:16:38,160 language which means it's sensitive to 449 00:16:38,160 --> 00:16:40,639 lowercase and uppercase characters so 450 00:16:40,639 --> 00:16:42,880 when defining variables we should always 451 00:16:42,880 --> 00:16:46,000 use lowercase letters but here false and 452 00:16:46,000 --> 00:16:47,759 true are special keywords in the 453 00:16:47,759 --> 00:16:49,600 language so if we spell it with a 454 00:16:49,600 --> 00:16:52,079 lowercase f python doesn't understand it 455 00:16:52,079 --> 00:16:53,920 you can see we have a red underline here 456 00:16:53,920 --> 00:16:56,480 which indicates an error so make sure to 457 00:16:56,480 --> 00:16:59,199 spell this with a capital f or if you 458 00:16:59,199 --> 00:17:01,440 want to set this to true make sure 459 00:17:01,440 --> 00:17:03,360 t is capital 460 00:17:03,360 --> 00:17:05,520 so in this program we are storing simple 461 00:17:05,520 --> 00:17:08,000 values in our computer's memory simple 462 00:17:08,000 --> 00:17:10,079 values can be numbers which can be 463 00:17:10,079 --> 00:17:12,959 integers or floats or they can be 464 00:17:12,959 --> 00:17:16,160 strings or booleans but in python we can 465 00:17:16,160 --> 00:17:18,799 also store complex values like lists and 466 00:17:18,799 --> 00:17:19,839 objects and that's what i'm going to 467 00:17:19,839 --> 00:17:21,520 show you in the future 468 00:17:21,520 --> 00:17:23,760 so before going any further i want you 469 00:17:23,760 --> 00:17:25,919 to do a little exercise imagine we're 470 00:17:25,919 --> 00:17:28,160 going to write a program for a hospital 471 00:17:28,160 --> 00:17:30,480 so we're checking a patient named john 472 00:17:30,480 --> 00:17:33,120 smith he's 20 years old and is a new 473 00:17:33,120 --> 00:17:35,440 patient i want you to define three 474 00:17:35,440 --> 00:17:38,320 variables here for his name his age and 475 00:17:38,320 --> 00:17:40,320 another variable to tell if this is a 476 00:17:40,320 --> 00:17:42,880 new or an existing patient so pause the 477 00:17:42,880 --> 00:17:44,400 video and spend one minute on this 478 00:17:44,400 --> 00:17:46,160 exercise when you're done come back 479 00:17:46,160 --> 00:17:49,600 continue see my solution 480 00:17:51,200 --> 00:17:52,320 all right so here we need three 481 00:17:52,320 --> 00:17:54,880 variables the first one is the patient's 482 00:17:54,880 --> 00:17:56,080 name 483 00:17:56,080 --> 00:17:57,760 we said that to 484 00:17:57,760 --> 00:18:01,520 john smith we can also call this full 485 00:18:01,520 --> 00:18:03,600 name these are both valid names for our 486 00:18:03,600 --> 00:18:04,640 variables 487 00:18:04,640 --> 00:18:06,640 the second variable is for the age of 488 00:18:06,640 --> 00:18:09,600 our patient so age is 20 489 00:18:09,600 --> 00:18:11,360 and finally we need a variable to tell 490 00:18:11,360 --> 00:18:14,000 if this is a new or an existing patient 491 00:18:14,000 --> 00:18:16,320 that's where we can use a boolean value 492 00:18:16,320 --> 00:18:20,000 so we define a variable is new and we 493 00:18:20,000 --> 00:18:22,960 set it to true 494 00:18:26,559 --> 00:18:27,840 so you have learned how to print 495 00:18:27,840 --> 00:18:30,160 messages on the terminal window in this 496 00:18:30,160 --> 00:18:31,679 tutorial i'm going to show you how to 497 00:18:31,679 --> 00:18:34,000 receive input from the user so we're 498 00:18:34,000 --> 00:18:36,000 going to write a small program that asks 499 00:18:36,000 --> 00:18:38,400 the user's name and then it will print a 500 00:18:38,400 --> 00:18:40,400 greeting message customized for that 501 00:18:40,400 --> 00:18:41,360 user 502 00:18:41,360 --> 00:18:44,320 so instead of prints we're going to use 503 00:18:44,320 --> 00:18:45,679 input 504 00:18:45,679 --> 00:18:48,400 now both this input and print are 505 00:18:48,400 --> 00:18:51,200 functions that are built into python as 506 00:18:51,200 --> 00:18:53,039 a metaphor think of the remote control 507 00:18:53,039 --> 00:18:55,039 of your tv on this remote control we 508 00:18:55,039 --> 00:18:56,960 have a bunch of buttons these are the 509 00:18:56,960 --> 00:18:59,280 functions built into your tv you can 510 00:18:59,280 --> 00:19:01,120 turn it on turn it off change the volume 511 00:19:01,120 --> 00:19:03,520 and so on in python we also have a bunch 512 00:19:03,520 --> 00:19:05,679 of functions for common tasks such as 513 00:19:05,679 --> 00:19:08,320 printing messages receiving input and so 514 00:19:08,320 --> 00:19:10,320 on so we're going to use the input 515 00:19:10,320 --> 00:19:11,360 function 516 00:19:11,360 --> 00:19:13,679 now whenever we have this parenthesis we 517 00:19:13,679 --> 00:19:16,160 say we're calling or executing that 518 00:19:16,160 --> 00:19:18,240 function it's like pressing a button on 519 00:19:18,240 --> 00:19:20,640 a remote control so we're going to call 520 00:19:20,640 --> 00:19:22,320 the input function 521 00:19:22,320 --> 00:19:24,400 and in between parentheses we want to 522 00:19:24,400 --> 00:19:26,640 add a string to print something on the 523 00:19:26,640 --> 00:19:27,679 terminal 524 00:19:27,679 --> 00:19:29,919 what is your name 525 00:19:29,919 --> 00:19:32,480 with a question mark followed by a space 526 00:19:32,480 --> 00:19:34,320 you will see why in a second 527 00:19:34,320 --> 00:19:37,120 so this input function will print this 528 00:19:37,120 --> 00:19:38,960 message on the terminal and then it will 529 00:19:38,960 --> 00:19:41,440 wait for the user to enter a value 530 00:19:41,440 --> 00:19:43,440 whatever the user enters this input 531 00:19:43,440 --> 00:19:45,520 function will return so now we can get 532 00:19:45,520 --> 00:19:47,440 that value and store it in the memory 533 00:19:47,440 --> 00:19:50,000 using a variable so we get the result 534 00:19:50,000 --> 00:19:52,960 and put it in a variable called name 535 00:19:52,960 --> 00:19:54,480 okay 536 00:19:54,480 --> 00:19:56,640 now on the second line we want to print 537 00:19:56,640 --> 00:19:58,720 a message like hi john or hi mosh 538 00:19:58,720 --> 00:20:01,120 whatever so print 539 00:20:01,120 --> 00:20:03,120 quotations hi 540 00:20:03,120 --> 00:20:06,000 with a space now after the quotation we 541 00:20:06,000 --> 00:20:08,640 want to dynamically print 542 00:20:08,640 --> 00:20:11,520 what we have in the name variable so we 543 00:20:11,520 --> 00:20:12,320 add 544 00:20:12,320 --> 00:20:15,039 a plus sign and then name 545 00:20:15,039 --> 00:20:18,000 so here we have hi which is a string 546 00:20:18,000 --> 00:20:20,559 we're concatenating or combining the 547 00:20:20,559 --> 00:20:23,360 string with another string that is what 548 00:20:23,360 --> 00:20:26,320 we have in the name variable 549 00:20:26,320 --> 00:20:28,080 so here's another example of an 550 00:20:28,080 --> 00:20:29,520 expression remember what is an 551 00:20:29,520 --> 00:20:31,679 expression it's a piece of code that 552 00:20:31,679 --> 00:20:34,080 produces a value so this expression 553 00:20:34,080 --> 00:20:36,640 concatenates or combines two strings 554 00:20:36,640 --> 00:20:38,080 let's run this program and see what 555 00:20:38,080 --> 00:20:40,559 happens so run 556 00:20:40,559 --> 00:20:42,400 okay here's the question what is your 557 00:20:42,400 --> 00:20:43,360 name 558 00:20:43,360 --> 00:20:45,440 mosh now note that earlier we added a 559 00:20:45,440 --> 00:20:48,080 space after the question mark we did 560 00:20:48,080 --> 00:20:49,600 this so 561 00:20:49,600 --> 00:20:51,840 here in the terminal window the cursor 562 00:20:51,840 --> 00:20:53,280 is separated from the question mark 563 00:20:53,280 --> 00:20:55,440 otherwise it will be so close so let's 564 00:20:55,440 --> 00:20:58,320 type whatever here press enter now we 565 00:20:58,320 --> 00:21:00,799 get this message hi mosh 566 00:21:00,799 --> 00:21:02,720 now here's a little exercise for you i 567 00:21:02,720 --> 00:21:04,720 want you to extend this program and ask 568 00:21:04,720 --> 00:21:07,120 two questions first ask the person's 569 00:21:07,120 --> 00:21:09,360 name and then their favorite color and 570 00:21:09,360 --> 00:21:12,159 then print a message like mosh likes 571 00:21:12,159 --> 00:21:13,200 blue 572 00:21:13,200 --> 00:21:15,039 so pause the video do this exercise and 573 00:21:15,039 --> 00:21:18,320 then come back continue watching 574 00:21:21,039 --> 00:21:22,720 all right so here's the first question 575 00:21:22,720 --> 00:21:24,400 right after that we're going to call the 576 00:21:24,400 --> 00:21:26,640 input function one more time this time 577 00:21:26,640 --> 00:21:29,200 we're going to ask a different question 578 00:21:29,200 --> 00:21:31,679 what is your favorite 579 00:21:31,679 --> 00:21:33,039 color 580 00:21:33,039 --> 00:21:35,600 now we get the new value and store it in 581 00:21:35,600 --> 00:21:38,480 a variable called color or we could call 582 00:21:38,480 --> 00:21:42,400 it favorite underline color either works 583 00:21:42,400 --> 00:21:44,080 and finally we're going to change what 584 00:21:44,080 --> 00:21:46,400 we pass to the print function 585 00:21:46,400 --> 00:21:48,559 so first we print the name 586 00:21:48,559 --> 00:21:51,440 then we concatenate this with a string 587 00:21:51,440 --> 00:21:53,600 here we're going to type likes 588 00:21:53,600 --> 00:21:56,880 we also put one space before and after 589 00:21:56,880 --> 00:22:00,000 likes and once again we concatenate this 590 00:22:00,000 --> 00:22:02,640 with the favorite color so 591 00:22:02,640 --> 00:22:04,320 favorite color 592 00:22:04,320 --> 00:22:06,480 now let's run this program 593 00:22:06,480 --> 00:22:08,320 so what is your name 594 00:22:08,320 --> 00:22:12,559 mosh enter favorite color blue enter 595 00:22:12,559 --> 00:22:17,200 we get this message mosh likes blue 596 00:22:17,600 --> 00:22:19,520 hey guys marsh here i just wanted to let 597 00:22:19,520 --> 00:22:20,880 you know that you really don't have to 598 00:22:20,880 --> 00:22:22,960 memorize anything in this course because 599 00:22:22,960 --> 00:22:24,720 i put together a cheat sheet with 600 00:22:24,720 --> 00:22:26,880 summary notes so you can quickly review 601 00:22:26,880 --> 00:22:28,960 the materials in this course the link is 602 00:22:28,960 --> 00:22:30,400 below this video 603 00:22:30,400 --> 00:22:32,480 so i have done my best to create the 604 00:22:32,480 --> 00:22:34,960 best possible python course on youtube 605 00:22:34,960 --> 00:22:36,400 and i really appreciate it if you 606 00:22:36,400 --> 00:22:38,400 support my hard work by liking this 607 00:22:38,400 --> 00:22:40,559 video and sharing it with others so they 608 00:22:40,559 --> 00:22:42,400 can learn as well and be sure to 609 00:22:42,400 --> 00:22:43,840 subscribe to my channel for more 610 00:22:43,840 --> 00:22:46,400 tutorials like this alright now let's 611 00:22:46,400 --> 00:22:49,679 move on to the next tutorial 612 00:22:51,840 --> 00:22:53,280 in this python tutorial we're going to 613 00:22:53,280 --> 00:22:55,520 write a program that will ask the year 614 00:22:55,520 --> 00:22:57,280 we were born in and then it will 615 00:22:57,280 --> 00:22:59,200 calculate our age and print it on the 616 00:22:59,200 --> 00:23:01,440 terminal so let's start with our input 617 00:23:01,440 --> 00:23:04,320 function input 618 00:23:04,320 --> 00:23:07,360 let's print birth year followed by a 619 00:23:07,360 --> 00:23:09,360 colon and a space 620 00:23:09,360 --> 00:23:11,200 now let's get the return value and store 621 00:23:11,200 --> 00:23:14,720 it in a variable called birth underline 622 00:23:14,720 --> 00:23:16,240 year 623 00:23:16,240 --> 00:23:17,840 so as i told you before we use an 624 00:23:17,840 --> 00:23:20,880 underscore to separate multiple words 625 00:23:20,880 --> 00:23:23,200 next we need to calculate the age so we 626 00:23:23,200 --> 00:23:25,919 define another variable called age 627 00:23:25,919 --> 00:23:28,159 and here we do some basic math currently 628 00:23:28,159 --> 00:23:31,360 we are in 2019. so let's write an 629 00:23:31,360 --> 00:23:36,559 expression like this 2019 minus birth 630 00:23:36,559 --> 00:23:37,600 year 631 00:23:37,600 --> 00:23:39,679 now finally let's print 632 00:23:39,679 --> 00:23:41,919 age on the terminal let's run our 633 00:23:41,919 --> 00:23:44,720 program and see what happens so 634 00:23:44,720 --> 00:23:47,279 my birth year is 1982. 635 00:23:47,279 --> 00:23:50,159 enter oops we got an error what is going 636 00:23:50,159 --> 00:23:51,279 on here 637 00:23:51,279 --> 00:23:52,799 so whenever you say this red message 638 00:23:52,799 --> 00:23:54,159 that means there is something wrong in 639 00:23:54,159 --> 00:23:56,240 your program with the information here 640 00:23:56,240 --> 00:23:58,400 we can find exactly where the error 641 00:23:58,400 --> 00:24:00,240 occurred so 642 00:24:00,240 --> 00:24:02,240 next to the file you can see the file 643 00:24:02,240 --> 00:24:04,960 that generated this error in this case 644 00:24:04,960 --> 00:24:06,400 that is 645 00:24:06,400 --> 00:24:09,120 app.pi so currently our program has only 646 00:24:09,120 --> 00:24:11,760 a single file but real complex programs 647 00:24:11,760 --> 00:24:14,159 often have hundreds or even thousands of 648 00:24:14,159 --> 00:24:17,840 files so in this file on line two this 649 00:24:17,840 --> 00:24:20,080 is where we got this error and right 650 00:24:20,080 --> 00:24:21,760 below that you can see 651 00:24:21,760 --> 00:24:23,840 the piece of code that generated this 652 00:24:23,840 --> 00:24:26,320 error so that is where we're calculating 653 00:24:26,320 --> 00:24:28,559 the h and right below that you can see 654 00:24:28,559 --> 00:24:30,320 the type of the error in this case we 655 00:24:30,320 --> 00:24:32,559 have a type error and here's the message 656 00:24:32,559 --> 00:24:36,320 unsupported operand types for 657 00:24:36,320 --> 00:24:37,840 subtraction 658 00:24:37,840 --> 00:24:39,600 and and 659 00:24:39,600 --> 00:24:40,480 stir 660 00:24:40,480 --> 00:24:42,640 so int is short for integer and that 661 00:24:42,640 --> 00:24:44,559 represents a whole number 662 00:24:44,559 --> 00:24:47,200 and stir is short for string so here 663 00:24:47,200 --> 00:24:49,520 we're subtracting a string from an 664 00:24:49,520 --> 00:24:51,679 integer and python doesn't know what to 665 00:24:51,679 --> 00:24:53,440 do with it let me explain so i'm going 666 00:24:53,440 --> 00:24:55,520 to close the terminal window 667 00:24:55,520 --> 00:24:57,760 so after the first line is executed we 668 00:24:57,760 --> 00:25:00,640 have this birth year variable set to a 669 00:25:00,640 --> 00:25:02,480 string so whatever we type in the 670 00:25:02,480 --> 00:25:05,120 terminal is always treated as a string 671 00:25:05,120 --> 00:25:08,080 even if we type a number in other words 672 00:25:08,080 --> 00:25:10,880 when we run this program this burst year 673 00:25:10,880 --> 00:25:12,640 variable will be set 674 00:25:12,640 --> 00:25:14,559 to a string 675 00:25:14,559 --> 00:25:17,039 with four characters one nine eight two 676 00:25:17,039 --> 00:25:19,120 this string is different from the actual 677 00:25:19,120 --> 00:25:22,799 number 1982 one is an integer the other 678 00:25:22,799 --> 00:25:24,159 is a string 679 00:25:24,159 --> 00:25:25,760 right so 680 00:25:25,760 --> 00:25:27,440 back to line two 681 00:25:27,440 --> 00:25:29,200 where this error occurred 682 00:25:29,200 --> 00:25:31,440 at runtime which means when we run our 683 00:25:31,440 --> 00:25:33,919 program this expression on the right 684 00:25:33,919 --> 00:25:36,159 side of the assignment operator is going 685 00:25:36,159 --> 00:25:37,600 to look like this 686 00:25:37,600 --> 00:25:38,960 2019 687 00:25:38,960 --> 00:25:40,080 minus 688 00:25:40,080 --> 00:25:42,400 string 1982. 689 00:25:42,400 --> 00:25:44,640 python doesn't know how to interpret or 690 00:25:44,640 --> 00:25:47,200 how to evaluate this expression 691 00:25:47,200 --> 00:25:49,760 to fix this problem we need to convert 692 00:25:49,760 --> 00:25:52,480 this 1982 into an integer and then we'll 693 00:25:52,480 --> 00:25:55,279 be able to subtract it from 2019 and 694 00:25:55,279 --> 00:25:57,279 that is very easy so far you have 695 00:25:57,279 --> 00:26:00,080 learned about two built-in functions one 696 00:26:00,080 --> 00:26:03,200 is print the other is input we have a 697 00:26:03,200 --> 00:26:05,279 few more functions for converting values 698 00:26:05,279 --> 00:26:08,640 into different types so we have int for 699 00:26:08,640 --> 00:26:10,880 converting a string into an integer we 700 00:26:10,880 --> 00:26:14,159 also have float for converting a string 701 00:26:14,159 --> 00:26:16,400 into a float or a number with a decimal 702 00:26:16,400 --> 00:26:19,200 point and we also have bool for 703 00:26:19,200 --> 00:26:22,240 converting a string into a boolean value 704 00:26:22,240 --> 00:26:23,120 so 705 00:26:23,120 --> 00:26:25,120 to fix this problem 706 00:26:25,120 --> 00:26:27,200 we need to go back on line two 707 00:26:27,200 --> 00:26:28,320 and 708 00:26:28,320 --> 00:26:31,120 pass this birth year variable to the int 709 00:26:31,120 --> 00:26:32,640 function like this 710 00:26:32,640 --> 00:26:35,840 int parenthesis like this 711 00:26:35,840 --> 00:26:37,840 so we pass this string to the in 712 00:26:37,840 --> 00:26:39,679 function int will convert it to an 713 00:26:39,679 --> 00:26:41,279 integer and then 714 00:26:41,279 --> 00:26:42,799 python interpreter will be able to 715 00:26:42,799 --> 00:26:45,039 evaluate this expression now let's run 716 00:26:45,039 --> 00:26:47,440 this program one more time 717 00:26:47,440 --> 00:26:52,240 so birth here is 1982 enter so i am 37 718 00:26:52,240 --> 00:26:53,840 years old 719 00:26:53,840 --> 00:26:56,000 in python we have a useful function for 720 00:26:56,000 --> 00:26:58,000 getting the type of variables for 721 00:26:58,000 --> 00:27:01,039 example let's print the type of birth 722 00:27:01,039 --> 00:27:05,760 year so right after line one let's print 723 00:27:05,760 --> 00:27:07,360 now here we're going to call another 724 00:27:07,360 --> 00:27:09,679 built-in function called type 725 00:27:09,679 --> 00:27:11,279 and now let's pass 726 00:27:11,279 --> 00:27:13,440 birth year 727 00:27:13,440 --> 00:27:16,640 okay now similarly after line three 728 00:27:16,640 --> 00:27:21,200 let's also print the type of h so print 729 00:27:21,200 --> 00:27:24,000 type of age 730 00:27:24,000 --> 00:27:26,799 okay let's run our program 731 00:27:26,799 --> 00:27:30,559 so birth here one more time 1982 732 00:27:30,559 --> 00:27:33,360 okay here's the result so the type of 733 00:27:33,360 --> 00:27:35,279 burst year as you can see 734 00:27:35,279 --> 00:27:38,480 is a class of stir or string 735 00:27:38,480 --> 00:27:40,000 we'll look at classes in the future so 736 00:27:40,000 --> 00:27:42,000 for now don't worry about them and also 737 00:27:42,000 --> 00:27:44,000 below that you can see that the type of 738 00:27:44,000 --> 00:27:47,679 the age variable is int or integer 739 00:27:47,679 --> 00:27:49,760 so here's what you need to take away 740 00:27:49,760 --> 00:27:52,159 whenever you use the input function you 741 00:27:52,159 --> 00:27:54,000 always get a string so if you're 742 00:27:54,000 --> 00:27:56,000 expecting a numerical value you should 743 00:27:56,000 --> 00:27:58,320 always convert that string into either 744 00:27:58,320 --> 00:28:01,039 an integer or a float 745 00:28:01,039 --> 00:28:02,880 but here's a little exercise for you i 746 00:28:02,880 --> 00:28:04,880 want you to write a program ask the user 747 00:28:04,880 --> 00:28:06,480 their weight and then convert it to 748 00:28:06,480 --> 00:28:08,880 kilograms and print it on the terminal 749 00:28:08,880 --> 00:28:10,799 so pause the video do the exercise and 750 00:28:10,799 --> 00:28:12,000 when you're ready come back continue 751 00:28:12,000 --> 00:28:15,000 watching 752 00:28:17,120 --> 00:28:18,480 all right so let's use our input 753 00:28:18,480 --> 00:28:19,520 function 754 00:28:19,520 --> 00:28:21,600 and ask for the weight 755 00:28:21,600 --> 00:28:24,000 in pounds 756 00:28:24,000 --> 00:28:25,919 here we get the weight 757 00:28:25,919 --> 00:28:28,640 in lbs or pounds 758 00:28:28,640 --> 00:28:30,240 now we need to convert this into 759 00:28:30,240 --> 00:28:32,480 kilogram that is very easy so we define 760 00:28:32,480 --> 00:28:34,559 another variable weight underline 761 00:28:34,559 --> 00:28:37,840 kilogram or kg we set this to 762 00:28:37,840 --> 00:28:40,320 weight underline lbs 763 00:28:40,320 --> 00:28:41,279 times 764 00:28:41,279 --> 00:28:44,000 0.45 765 00:28:44,000 --> 00:28:46,960 and finally let's print weight 766 00:28:46,960 --> 00:28:49,200 on the line kilogram let's run this 767 00:28:49,200 --> 00:28:51,520 python program and see what happens 768 00:28:51,520 --> 00:28:54,320 so my weight is 160. 769 00:28:54,320 --> 00:28:56,080 all right once again we got an error 770 00:28:56,080 --> 00:28:59,360 can't multiply sequence by non-end of 771 00:28:59,360 --> 00:29:02,559 type float so as i told you before this 772 00:29:02,559 --> 00:29:05,200 input function returns a string so we 773 00:29:05,200 --> 00:29:07,840 cannot multiply a string by a float 774 00:29:07,840 --> 00:29:09,600 python doesn't know what to do with it 775 00:29:09,600 --> 00:29:11,120 so in this case we should convert this 776 00:29:11,120 --> 00:29:13,760 number to an integer or a float and then 777 00:29:13,760 --> 00:29:16,640 multiply it by 0.45 778 00:29:16,640 --> 00:29:21,360 so let's call the int function 779 00:29:21,520 --> 00:29:24,480 and pass weight underline lbs and run 780 00:29:24,480 --> 00:29:26,399 our program one more time 781 00:29:26,399 --> 00:29:28,720 160 782 00:29:28,720 --> 00:29:33,520 okay so i am 72 kilograms 783 00:29:37,039 --> 00:29:38,240 in this tutorial you're going to learn 784 00:29:38,240 --> 00:29:40,240 more about python strings so i've 785 00:29:40,240 --> 00:29:42,720 defined this course variable and set it 786 00:29:42,720 --> 00:29:45,200 to python for beginners now earlier i 787 00:29:45,200 --> 00:29:47,039 told you that you can use both single 788 00:29:47,039 --> 00:29:49,520 and double quotes to define a string but 789 00:29:49,520 --> 00:29:51,039 there are times that you have to use a 790 00:29:51,039 --> 00:29:52,880 specific form otherwise you're going to 791 00:29:52,880 --> 00:29:55,120 run into issues here's an example 792 00:29:55,120 --> 00:29:56,720 imagine we want to change this string 793 00:29:56,720 --> 00:30:00,159 into python's course for beginners so we 794 00:30:00,159 --> 00:30:02,799 want to add an apostrophe like this 795 00:30:02,799 --> 00:30:04,320 pythons 796 00:30:04,320 --> 00:30:05,440 course 797 00:30:05,440 --> 00:30:07,279 for beginners you can immediately see 798 00:30:07,279 --> 00:30:08,480 this is going crazy 799 00:30:08,480 --> 00:30:11,039 because our string starts here and then 800 00:30:11,039 --> 00:30:13,360 terminates here all these characters 801 00:30:13,360 --> 00:30:16,399 that we have after the second apostrophe 802 00:30:16,399 --> 00:30:18,159 python interpreter doesn't know what 803 00:30:18,159 --> 00:30:20,480 they are so to solve this problem we 804 00:30:20,480 --> 00:30:22,240 need to use double quotes to define our 805 00:30:22,240 --> 00:30:24,880 string so we can have a single quote in 806 00:30:24,880 --> 00:30:27,200 the middle of the string so let's change 807 00:30:27,200 --> 00:30:29,600 this to double quotes now you can see it 808 00:30:29,600 --> 00:30:32,320 adds another double quote to close it we 809 00:30:32,320 --> 00:30:34,399 have to manually remove this 810 00:30:34,399 --> 00:30:36,320 and also one more time at the beginning 811 00:30:36,320 --> 00:30:37,679 of the string 812 00:30:37,679 --> 00:30:39,919 we need to add another double quote now 813 00:30:39,919 --> 00:30:42,080 you can see the error is gone so if we 814 00:30:42,080 --> 00:30:43,120 print 815 00:30:43,120 --> 00:30:45,120 course 816 00:30:45,120 --> 00:30:46,080 we see 817 00:30:46,080 --> 00:30:48,240 python scores for beginners 818 00:30:48,240 --> 00:30:50,159 beautiful now let's say we don't want 819 00:30:50,159 --> 00:30:52,960 this apostrophe here so we have python 820 00:30:52,960 --> 00:30:54,640 for beginners but we want to put 821 00:30:54,640 --> 00:30:56,960 beginners in double quotes 822 00:30:56,960 --> 00:30:58,399 once again if you add a double quote 823 00:30:58,399 --> 00:31:00,720 here python interpreter gets confused 824 00:31:00,720 --> 00:31:02,799 because it assumes the second double 825 00:31:02,799 --> 00:31:04,880 code indicates the end of our string so 826 00:31:04,880 --> 00:31:06,399 it doesn't know what these characters 827 00:31:06,399 --> 00:31:07,200 are 828 00:31:07,200 --> 00:31:08,799 so to solve this 829 00:31:08,799 --> 00:31:11,440 we need to change our double quotes to 830 00:31:11,440 --> 00:31:14,000 single quotes like this and then we can 831 00:31:14,000 --> 00:31:16,320 add double quotes in the middle of the 832 00:31:16,320 --> 00:31:17,519 string 833 00:31:17,519 --> 00:31:19,440 now let's run this program 834 00:31:19,440 --> 00:31:21,200 there you go so we get python for 835 00:31:21,200 --> 00:31:23,120 beginners 836 00:31:23,120 --> 00:31:25,120 so these are the cases for using single 837 00:31:25,120 --> 00:31:27,919 or double quotes now in all the examples 838 00:31:27,919 --> 00:31:29,840 i've shown you so far we only deal with 839 00:31:29,840 --> 00:31:31,440 short strings but what if you want to 840 00:31:31,440 --> 00:31:33,679 define a string that is multiple lines 841 00:31:33,679 --> 00:31:35,600 for example what if you want to define a 842 00:31:35,600 --> 00:31:37,760 string for the message that we send in 843 00:31:37,760 --> 00:31:39,919 an email in that case we need to use 844 00:31:39,919 --> 00:31:44,000 triple quotes so let me delete this 845 00:31:44,000 --> 00:31:46,799 now we add three quotes so one two three 846 00:31:46,799 --> 00:31:48,000 there you go 847 00:31:48,000 --> 00:31:50,559 so we have three quotes to start our 848 00:31:50,559 --> 00:31:53,600 string and three to terminate it again 849 00:31:53,600 --> 00:31:55,600 these codes can be single or double 850 00:31:55,600 --> 00:31:56,880 quotes okay 851 00:31:56,880 --> 00:31:59,039 now with this we can define a string 852 00:31:59,039 --> 00:32:01,600 that spans multiple lines for example we 853 00:32:01,600 --> 00:32:03,760 can say hi john 854 00:32:03,760 --> 00:32:07,279 here is our first email to you 855 00:32:07,279 --> 00:32:08,720 thank you 856 00:32:08,720 --> 00:32:10,960 the support team 857 00:32:10,960 --> 00:32:12,080 like that 858 00:32:12,080 --> 00:32:12,880 now 859 00:32:12,880 --> 00:32:15,440 let's run this program and 860 00:32:15,440 --> 00:32:17,440 here's the result so 861 00:32:17,440 --> 00:32:21,360 we get this beautiful multi-line string 862 00:32:21,360 --> 00:32:22,960 now let's change this back to something 863 00:32:22,960 --> 00:32:24,000 simple 864 00:32:24,000 --> 00:32:26,559 so we can look at other characteristics 865 00:32:26,559 --> 00:32:28,880 of strings in python so i'm going to use 866 00:32:28,880 --> 00:32:31,519 single quotes and set the course name to 867 00:32:31,519 --> 00:32:34,640 python for beginners 868 00:32:34,640 --> 00:32:36,960 here we can use square brackets to get a 869 00:32:36,960 --> 00:32:39,200 character at a given index in this 870 00:32:39,200 --> 00:32:41,360 string let me show you so to get the 871 00:32:41,360 --> 00:32:44,000 first character we use square brackets 872 00:32:44,000 --> 00:32:47,200 and type zero so the index of the first 873 00:32:47,200 --> 00:32:49,760 character in this string is zero in 874 00:32:49,760 --> 00:32:51,679 other words this is how 875 00:32:51,679 --> 00:32:54,720 python strings are indexed zero one two 876 00:32:54,720 --> 00:32:57,200 three 4 and so on so the index of the 877 00:32:57,200 --> 00:32:59,279 first character is 0 the second 878 00:32:59,279 --> 00:33:01,679 character is 1 and so on so let me 879 00:33:01,679 --> 00:33:04,799 delete this and run this program 880 00:33:04,799 --> 00:33:06,720 we get p 881 00:33:06,720 --> 00:33:08,720 we can also use a negative index here 882 00:33:08,720 --> 00:33:10,000 and this is one of the features that we 883 00:33:10,000 --> 00:33:11,679 don't have in other programming 884 00:33:11,679 --> 00:33:13,679 languages as far as i know so with the 885 00:33:13,679 --> 00:33:16,240 negative index we can get the characters 886 00:33:16,240 --> 00:33:18,960 starting from the end so if i pass 887 00:33:18,960 --> 00:33:22,080 negative 1 here assuming that 0 is the 888 00:33:22,080 --> 00:33:24,399 index of the first character negative 889 00:33:24,399 --> 00:33:27,200 one is the index of the last character 890 00:33:27,200 --> 00:33:28,880 so when we run this program we should 891 00:33:28,880 --> 00:33:31,440 see s let's run it 892 00:33:31,440 --> 00:33:34,559 there you go we get s if we pass 893 00:33:34,559 --> 00:33:36,960 negative two this will return the second 894 00:33:36,960 --> 00:33:38,880 character from the end let's run it one 895 00:33:38,880 --> 00:33:40,080 more time 896 00:33:40,080 --> 00:33:42,880 now we get r because that is the second 897 00:33:42,880 --> 00:33:45,279 character from the end okay 898 00:33:45,279 --> 00:33:46,880 so pay close attention to this square 899 00:33:46,880 --> 00:33:49,360 bracket syntax because quite often it's 900 00:33:49,360 --> 00:33:52,559 the topic for online python tests or 901 00:33:52,559 --> 00:33:54,720 university exams so if you're preparing 902 00:33:54,720 --> 00:33:56,720 for a python test make sure to watch 903 00:33:56,720 --> 00:33:58,320 this tutorial one more time and 904 00:33:58,320 --> 00:34:00,399 understand exactly how this square 905 00:34:00,399 --> 00:34:02,320 bracket syntax works 906 00:34:02,320 --> 00:34:04,559 you can also use a similar syntax to 907 00:34:04,559 --> 00:34:06,880 extract a few characters instead of just 908 00:34:06,880 --> 00:34:09,760 one character for example 909 00:34:09,760 --> 00:34:10,960 if we type 910 00:34:10,960 --> 00:34:15,040 0 colon 3 python interpreter will return 911 00:34:15,040 --> 00:34:17,760 all the characters starting from this 912 00:34:17,760 --> 00:34:20,879 index all the way to the second index 913 00:34:20,879 --> 00:34:22,879 but it does not return the character at 914 00:34:22,879 --> 00:34:25,599 this index in other words 915 00:34:25,599 --> 00:34:28,000 back to these indexes 916 00:34:28,000 --> 00:34:30,639 so we have 0 1 2 3 and so on 917 00:34:30,639 --> 00:34:32,560 when we run this program python 918 00:34:32,560 --> 00:34:34,879 interpreter will return the characters 919 00:34:34,879 --> 00:34:37,280 starting from index 0 920 00:34:37,280 --> 00:34:41,359 all the way to index 3 but excludes the 921 00:34:41,359 --> 00:34:44,399 character and index 3. so when we run 922 00:34:44,399 --> 00:34:46,159 this python program we're going to see 923 00:34:46,159 --> 00:34:48,000 pipe let me show you so i'm going to 924 00:34:48,000 --> 00:34:49,919 delete this line 925 00:34:49,919 --> 00:34:52,480 run this program there you go we get 926 00:34:52,480 --> 00:34:53,599 pipe 927 00:34:53,599 --> 00:34:56,079 now here we also have default values for 928 00:34:56,079 --> 00:34:59,040 the start and end index so if we don't 929 00:34:59,040 --> 00:35:00,079 supply 930 00:35:00,079 --> 00:35:02,480 the end index python will return all the 931 00:35:02,480 --> 00:35:04,720 characters to the end of the string 932 00:35:04,720 --> 00:35:06,720 let's take a look so run this program 933 00:35:06,720 --> 00:35:09,200 there you go we get python for beginners 934 00:35:09,200 --> 00:35:11,680 but if we change the start index to 935 00:35:11,680 --> 00:35:12,640 1 936 00:35:12,640 --> 00:35:15,599 this will exclude the first character so 937 00:35:15,599 --> 00:35:17,480 when we run this program we see 938 00:35:17,480 --> 00:35:21,440 y-t-h-o-n so p is removed okay 939 00:35:21,440 --> 00:35:23,520 now similarly we have a default value 940 00:35:23,520 --> 00:35:25,520 for the start index so if we don't 941 00:35:25,520 --> 00:35:27,920 supply the starting index 942 00:35:27,920 --> 00:35:30,960 but add an end index like 5 python 943 00:35:30,960 --> 00:35:33,680 interpreter will assume 0 as the 944 00:35:33,680 --> 00:35:36,880 starting index so let's run this program 945 00:35:36,880 --> 00:35:40,560 there you go we get python 946 00:35:40,560 --> 00:35:42,240 now what if we leave 947 00:35:42,240 --> 00:35:45,599 both the start and end index well i told 948 00:35:45,599 --> 00:35:48,320 you that in this case 0 will be assumed 949 00:35:48,320 --> 00:35:50,960 as the start index and the length of the 950 00:35:50,960 --> 00:35:53,520 string will be assumed as the end index 951 00:35:53,520 --> 00:35:55,680 so with this syntax we can basically 952 00:35:55,680 --> 00:35:59,440 copy or clone a string in other words if 953 00:35:59,440 --> 00:36:00,720 i define 954 00:36:00,720 --> 00:36:02,160 another variable here let's call it 955 00:36:02,160 --> 00:36:04,800 another and set it to course square 956 00:36:04,800 --> 00:36:07,280 brackets with just a colon now this 957 00:36:07,280 --> 00:36:08,800 expression will return all the 958 00:36:08,800 --> 00:36:11,359 characters in the course variable so 959 00:36:11,359 --> 00:36:13,920 this second variable will be a copy of 960 00:36:13,920 --> 00:36:16,880 our first variable let's take a look so 961 00:36:16,880 --> 00:36:18,960 let's print another 962 00:36:18,960 --> 00:36:21,119 and run our program there you go we get 963 00:36:21,119 --> 00:36:22,960 python for beginners 964 00:36:22,960 --> 00:36:25,280 so once again this square bracket syntax 965 00:36:25,280 --> 00:36:26,400 is pretty important 966 00:36:26,400 --> 00:36:28,320 if you're preparing for online python 967 00:36:28,320 --> 00:36:30,320 tests or college exams make sure to 968 00:36:30,320 --> 00:36:32,240 watch this tutorial again 969 00:36:32,240 --> 00:36:34,000 now here's a little exercise for you i'm 970 00:36:34,000 --> 00:36:36,000 going to delete all this code 971 00:36:36,000 --> 00:36:38,960 define a variable called name and set it 972 00:36:38,960 --> 00:36:41,599 to jennifer 973 00:36:41,599 --> 00:36:43,440 now when we print 974 00:36:43,440 --> 00:36:44,320 name 975 00:36:44,320 --> 00:36:45,280 of 976 00:36:45,280 --> 00:36:46,160 one 977 00:36:46,160 --> 00:36:48,800 colon negative one what do you think 978 00:36:48,800 --> 00:36:50,560 we're going to see on the terminal i 979 00:36:50,560 --> 00:36:52,480 want you to use your knowledge to tell 980 00:36:52,480 --> 00:36:53,839 what we're going to see on the terminal 981 00:36:53,839 --> 00:36:56,240 without running this program so pause 982 00:36:56,240 --> 00:36:57,599 the video think about it for a few 983 00:36:57,599 --> 00:36:59,280 seconds and then come back continue 984 00:36:59,280 --> 00:37:01,599 watching 985 00:37:04,400 --> 00:37:06,000 so this expression will return the 986 00:37:06,000 --> 00:37:10,000 characters starting from index 1 which 987 00:37:10,000 --> 00:37:12,000 is the second character 988 00:37:12,000 --> 00:37:14,720 all the way to the first character from 989 00:37:14,720 --> 00:37:17,200 the end but excluding the character at 990 00:37:17,200 --> 00:37:19,359 this index in this case the first 991 00:37:19,359 --> 00:37:21,839 character from the end is r so r will be 992 00:37:21,839 --> 00:37:24,000 excluded in other words we're gonna see 993 00:37:24,000 --> 00:37:26,640 all the characters starting from e all 994 00:37:26,640 --> 00:37:28,000 the way 995 00:37:28,000 --> 00:37:29,359 to the second e 996 00:37:29,359 --> 00:37:31,119 let's take a look so i'm gonna run this 997 00:37:31,119 --> 00:37:33,359 program 998 00:37:33,359 --> 00:37:35,920 there you go this is what we get and i 999 00:37:35,920 --> 00:37:39,119 hope you guessed you're great 1000 00:37:42,160 --> 00:37:43,440 in this tutorial we're going to look at 1001 00:37:43,440 --> 00:37:45,920 formatted strings in python programming 1002 00:37:45,920 --> 00:37:47,920 language formatted strings are 1003 00:37:47,920 --> 00:37:50,400 particularly useful in situations where 1004 00:37:50,400 --> 00:37:52,079 you want to dynamically generate some 1005 00:37:52,079 --> 00:37:54,720 text with your variables let me show you 1006 00:37:54,720 --> 00:37:56,560 let's say we have two variables first 1007 00:37:56,560 --> 00:37:59,680 name and last name so first we set this 1008 00:37:59,680 --> 00:38:01,119 to john 1009 00:38:01,119 --> 00:38:03,839 and last we send this to smith now it's 1010 00:38:03,839 --> 00:38:05,680 better to call these variables first 1011 00:38:05,680 --> 00:38:07,520 name and last name because they're more 1012 00:38:07,520 --> 00:38:09,599 descriptive but here i'm using shorter 1013 00:38:09,599 --> 00:38:11,040 names because i want you to see the 1014 00:38:11,040 --> 00:38:13,359 entire code on the screen 1015 00:38:13,359 --> 00:38:15,119 so let's say with these two variables we 1016 00:38:15,119 --> 00:38:17,920 want to generate some text like this 1017 00:38:17,920 --> 00:38:18,800 john 1018 00:38:18,800 --> 00:38:21,200 in square brackets smith 1019 00:38:21,200 --> 00:38:23,440 is a coder let's say we want to print 1020 00:38:23,440 --> 00:38:25,760 this on a terminal how do we do this 1021 00:38:25,760 --> 00:38:28,240 well we define another variable like 1022 00:38:28,240 --> 00:38:29,200 message 1023 00:38:29,200 --> 00:38:32,240 now here we add the first name now we 1024 00:38:32,240 --> 00:38:34,160 need to concatenate this with a string 1025 00:38:34,160 --> 00:38:37,040 that contains a space and a square 1026 00:38:37,040 --> 00:38:38,079 bracket 1027 00:38:38,079 --> 00:38:41,040 next we need to add the last name 1028 00:38:41,040 --> 00:38:43,440 then we need to add a string that 1029 00:38:43,440 --> 00:38:46,000 contains the closing square bracket 1030 00:38:46,000 --> 00:38:49,520 followed by is a coder 1031 00:38:49,520 --> 00:38:51,839 okay so then 1032 00:38:51,839 --> 00:38:54,320 if we print message 1033 00:38:54,320 --> 00:38:56,560 and run this program 1034 00:38:56,560 --> 00:38:59,599 we see john smith is a coder right 1035 00:38:59,599 --> 00:39:00,400 now 1036 00:39:00,400 --> 00:39:03,040 while this approach perfectly works it's 1037 00:39:03,040 --> 00:39:05,520 not ideal because as our text gets more 1038 00:39:05,520 --> 00:39:07,760 complicated it becomes harder to 1039 00:39:07,760 --> 00:39:09,440 visualize the output 1040 00:39:09,440 --> 00:39:11,280 so someone else reading this code they 1041 00:39:11,280 --> 00:39:13,040 have to visualize all these string 1042 00:39:13,040 --> 00:39:15,280 concatenations in their head this is 1043 00:39:15,280 --> 00:39:17,200 where we use formatted strings they make 1044 00:39:17,200 --> 00:39:19,760 it easier for us to visualize the output 1045 00:39:19,760 --> 00:39:21,359 so 1046 00:39:21,359 --> 00:39:23,119 i'm going to define another variable 1047 00:39:23,119 --> 00:39:25,839 let's say msg short for message and set 1048 00:39:25,839 --> 00:39:28,079 this to a formatted string a formatted 1049 00:39:28,079 --> 00:39:31,119 string is one that is prefixed with an f 1050 00:39:31,119 --> 00:39:32,720 so f 1051 00:39:32,720 --> 00:39:33,760 quotes 1052 00:39:33,760 --> 00:39:35,680 now in between the quotes first we want 1053 00:39:35,680 --> 00:39:37,920 to add the value of the first name 1054 00:39:37,920 --> 00:39:39,760 variable so 1055 00:39:39,760 --> 00:39:42,560 we add curly braces and here we type 1056 00:39:42,560 --> 00:39:43,839 first 1057 00:39:43,839 --> 00:39:46,079 next we add a space we add our square 1058 00:39:46,079 --> 00:39:48,640 brackets in between the square brackets 1059 00:39:48,640 --> 00:39:50,800 we want to display the last name so once 1060 00:39:50,800 --> 00:39:53,440 again we add curly braces 1061 00:39:53,440 --> 00:39:56,160 and type last and finally here we type 1062 00:39:56,160 --> 00:39:59,440 is a coder so this is what we call a 1063 00:39:59,440 --> 00:40:01,359 formatted string 1064 00:40:01,359 --> 00:40:03,359 with these curly braces we're defining 1065 00:40:03,359 --> 00:40:05,760 placeholders or holes in our string and 1066 00:40:05,760 --> 00:40:07,680 when we run our program these holes will 1067 00:40:07,680 --> 00:40:09,440 be filled with the value of our 1068 00:40:09,440 --> 00:40:11,359 variables so here we have two 1069 00:40:11,359 --> 00:40:14,160 placeholders or two holes in our string 1070 00:40:14,160 --> 00:40:16,319 one is for the value of our first name 1071 00:40:16,319 --> 00:40:18,319 variable and the other is for the value 1072 00:40:18,319 --> 00:40:20,400 of the last name variable 1073 00:40:20,400 --> 00:40:23,599 now compare this format a string with 1074 00:40:23,599 --> 00:40:26,400 string concatenation with this formatted 1075 00:40:26,400 --> 00:40:28,800 string we can easily visualize what the 1076 00:40:28,800 --> 00:40:31,280 output looks like right now let's print 1077 00:40:31,280 --> 00:40:32,880 this on the terminal to make sure we get 1078 00:40:32,880 --> 00:40:36,839 the exact same output so let's print 1079 00:40:36,839 --> 00:40:41,040 message there you go so john smith is a 1080 00:40:41,040 --> 00:40:42,480 coder 1081 00:40:42,480 --> 00:40:45,040 so to define formatted strings prefix 1082 00:40:45,040 --> 00:40:47,599 your strings with an f and use curly 1083 00:40:47,599 --> 00:40:50,319 braces to dynamically insert values into 1084 00:40:50,319 --> 00:40:53,480 your strings 1085 00:40:56,240 --> 00:40:57,599 in this python tutorial i'm going to 1086 00:40:57,599 --> 00:40:59,119 show you some really cool things you can 1087 00:40:59,119 --> 00:41:01,280 do with python strings 1088 00:41:01,280 --> 00:41:03,760 so let's start by defining a variable 1089 00:41:03,760 --> 00:41:04,800 course 1090 00:41:04,800 --> 00:41:07,119 and we set that to python 1091 00:41:07,119 --> 00:41:10,319 for beginners 1092 00:41:10,560 --> 00:41:11,920 now to calculate the number of 1093 00:41:11,920 --> 00:41:13,920 characters in this string we can use a 1094 00:41:13,920 --> 00:41:17,280 built-in function called len so len 1095 00:41:17,280 --> 00:41:19,920 we give it this course variable and then 1096 00:41:19,920 --> 00:41:22,800 we can print the result let's run this 1097 00:41:22,800 --> 00:41:24,480 program 1098 00:41:24,480 --> 00:41:26,720 so as you can see we have 20 characters 1099 00:41:26,720 --> 00:41:28,480 in this string this is particularly 1100 00:41:28,480 --> 00:41:30,640 useful when we receive input from the 1101 00:41:30,640 --> 00:41:32,560 user for example you have noticed that 1102 00:41:32,560 --> 00:41:34,400 when you fill out a form online 1103 00:41:34,400 --> 00:41:37,359 each input field quit often has a limit 1104 00:41:37,359 --> 00:41:39,200 for example you might have 50 characters 1105 00:41:39,200 --> 00:41:41,760 for your name so using this len function 1106 00:41:41,760 --> 00:41:43,839 we can enforce a limit on the number of 1107 00:41:43,839 --> 00:41:46,079 characters in an input field if the user 1108 00:41:46,079 --> 00:41:47,920 types in more characters than we allow 1109 00:41:47,920 --> 00:41:50,720 them we can display an error okay 1110 00:41:50,720 --> 00:41:51,520 now 1111 00:41:51,520 --> 00:41:53,040 this len function 1112 00:41:53,040 --> 00:41:55,599 is another function built into python 1113 00:41:55,599 --> 00:41:57,680 it's a general purpose function so it's 1114 00:41:57,680 --> 00:41:59,359 not limited to counting the number of 1115 00:41:59,359 --> 00:42:01,280 characters in a string in the future 1116 00:42:01,280 --> 00:42:02,960 when we look at lists i'm going to show 1117 00:42:02,960 --> 00:42:04,800 you that we can use this function to 1118 00:42:04,800 --> 00:42:06,960 count the number of items in a list so 1119 00:42:06,960 --> 00:42:09,119 it's a general purpose function 1120 00:42:09,119 --> 00:42:11,359 now we also have functions specific to 1121 00:42:11,359 --> 00:42:13,119 strings for example we have functions 1122 00:42:13,119 --> 00:42:15,440 for converting all these characters to 1123 00:42:15,440 --> 00:42:18,079 uppercase or lowercase to access these 1124 00:42:18,079 --> 00:42:20,560 functions we use the dot operator let me 1125 00:42:20,560 --> 00:42:22,800 show you so 1126 00:42:22,800 --> 00:42:25,760 first we type course then dot 1127 00:42:25,760 --> 00:42:28,319 look these are all the functions that 1128 00:42:28,319 --> 00:42:30,560 are specific to strings 1129 00:42:30,560 --> 00:42:32,800 now in more accurate terms we refer to 1130 00:42:32,800 --> 00:42:35,280 these functions as methods this is a 1131 00:42:35,280 --> 00:42:37,119 term in object-oriented programming that 1132 00:42:37,119 --> 00:42:38,960 we'll look at in the future but for now 1133 00:42:38,960 --> 00:42:40,720 what i want you to take away is that 1134 00:42:40,720 --> 00:42:42,880 when a function belongs to something 1135 00:42:42,880 --> 00:42:44,800 else or is specific to some kind of 1136 00:42:44,800 --> 00:42:47,280 object we refer to that function as a 1137 00:42:47,280 --> 00:42:49,280 method for example here we have this 1138 00:42:49,280 --> 00:42:50,240 function 1139 00:42:50,240 --> 00:42:52,400 upper for converting the string into 1140 00:42:52,400 --> 00:42:55,040 upper case now more accurately because 1141 00:42:55,040 --> 00:42:57,440 this function is specific to strings we 1142 00:42:57,440 --> 00:43:00,560 refer to this as a method in contrast 1143 00:43:00,560 --> 00:43:02,720 len and print are general purpose 1144 00:43:02,720 --> 00:43:04,480 functions they don't belong to strings 1145 00:43:04,480 --> 00:43:06,800 or numbers or other kinds of objects 1146 00:43:06,800 --> 00:43:07,680 okay 1147 00:43:07,680 --> 00:43:09,040 so this is the difference between 1148 00:43:09,040 --> 00:43:10,880 functions and methods 1149 00:43:10,880 --> 00:43:13,920 now let's look at this upper method so 1150 00:43:13,920 --> 00:43:17,119 let's print the result 1151 00:43:17,119 --> 00:43:20,160 and run our program there you go we get 1152 00:43:20,160 --> 00:43:22,079 all these characters displayed in 1153 00:43:22,079 --> 00:43:24,400 uppercase now note that this method does 1154 00:43:24,400 --> 00:43:27,680 not change or modify our original string 1155 00:43:27,680 --> 00:43:29,839 in fact it creates a new string and 1156 00:43:29,839 --> 00:43:31,920 returns it so 1157 00:43:31,920 --> 00:43:33,760 if we print 1158 00:43:33,760 --> 00:43:36,319 our course variable right after we call 1159 00:43:36,319 --> 00:43:38,400 the upper method you can see that our 1160 00:43:38,400 --> 00:43:41,200 course variable still has its original 1161 00:43:41,200 --> 00:43:43,119 form so let's run this program one more 1162 00:43:43,119 --> 00:43:45,599 time there you go look this is our 1163 00:43:45,599 --> 00:43:47,520 original course variable it's not 1164 00:43:47,520 --> 00:43:48,800 modified 1165 00:43:48,800 --> 00:43:51,040 now similar to the upper method we have 1166 00:43:51,040 --> 00:43:53,520 another method for converting a string 1167 00:43:53,520 --> 00:43:56,880 into lowercase so let me show you print 1168 00:43:56,880 --> 00:43:59,839 course dot lower 1169 00:43:59,839 --> 00:44:02,400 now let's run the program 1170 00:44:02,400 --> 00:44:04,400 so on the second line you can see all 1171 00:44:04,400 --> 00:44:07,200 characters are in lower case 1172 00:44:07,200 --> 00:44:08,319 now there are times that you want to 1173 00:44:08,319 --> 00:44:11,119 find a character or a sequence of 1174 00:44:11,119 --> 00:44:12,640 character in a string in those 1175 00:44:12,640 --> 00:44:14,960 situations you can use the find method 1176 00:44:14,960 --> 00:44:18,400 so let me delete this reliance 1177 00:44:18,400 --> 00:44:21,359 we call course dot find 1178 00:44:21,359 --> 00:44:24,640 here we pass a character let's say p 1179 00:44:24,640 --> 00:44:26,960 and this will return the index of the 1180 00:44:26,960 --> 00:44:29,200 first occurrence of that character let 1181 00:44:29,200 --> 00:44:34,000 me show you so let's print the result 1182 00:44:34,000 --> 00:44:35,280 we get zero 1183 00:44:35,280 --> 00:44:38,160 because the index of the first capital p 1184 00:44:38,160 --> 00:44:41,040 in our string is zero 1185 00:44:41,040 --> 00:44:42,880 as another example if we pass a 1186 00:44:42,880 --> 00:44:44,960 lowercase o here 1187 00:44:44,960 --> 00:44:46,560 let's see what we get 1188 00:44:46,560 --> 00:44:47,760 we get four 1189 00:44:47,760 --> 00:44:51,040 because the index of this o here is 4. 1190 00:44:51,040 --> 00:44:53,040 now note that the find method is case 1191 00:44:53,040 --> 00:44:56,000 sensitive so it's sensitive to lowercase 1192 00:44:56,000 --> 00:44:58,480 and uppercase characters as an example 1193 00:44:58,480 --> 00:45:01,280 if we pass an uppercase o here and run 1194 00:45:01,280 --> 00:45:04,000 this program we get negative 1 because 1195 00:45:04,000 --> 00:45:06,400 we don't have an uppercase o anywhere in 1196 00:45:06,400 --> 00:45:07,520 the string 1197 00:45:07,520 --> 00:45:09,920 okay we can also pass a sequence of 1198 00:45:09,920 --> 00:45:12,480 characters for example we can pass 1199 00:45:12,480 --> 00:45:14,319 beginners 1200 00:45:14,319 --> 00:45:16,000 with a capital b 1201 00:45:16,000 --> 00:45:17,760 let's run this program 1202 00:45:17,760 --> 00:45:21,040 we get 11 because the word beginners 1203 00:45:21,040 --> 00:45:23,599 starts at index 11. 1204 00:45:23,599 --> 00:45:25,520 now we also have a method for replacing 1205 00:45:25,520 --> 00:45:28,160 a character or a sequence of characters 1206 00:45:28,160 --> 00:45:30,640 and that is called replace so let's 1207 00:45:30,640 --> 00:45:33,520 change find to replace let's say we want 1208 00:45:33,520 --> 00:45:36,400 to replace beginners with absolute 1209 00:45:36,400 --> 00:45:39,280 beginners so we add a comma to pass a 1210 00:45:39,280 --> 00:45:41,440 second value to this function or more 1211 00:45:41,440 --> 00:45:44,640 accurately this method we add a string 1212 00:45:44,640 --> 00:45:47,359 here i'm going to pass absolute 1213 00:45:47,359 --> 00:45:49,599 beginners 1214 00:45:49,599 --> 00:45:50,480 okay 1215 00:45:50,480 --> 00:45:52,480 now let's run this program 1216 00:45:52,480 --> 00:45:56,480 so we get python for absolute beginners 1217 00:45:56,480 --> 00:45:58,560 again this method like the find method 1218 00:45:58,560 --> 00:46:00,880 is case sensitive so if we pass 1219 00:46:00,880 --> 00:46:03,839 beginners all in lowercase this method 1220 00:46:03,839 --> 00:46:06,240 is not going to find this exact word in 1221 00:46:06,240 --> 00:46:08,160 our string so it's not going to replace 1222 00:46:08,160 --> 00:46:10,400 it with absolute beginners let's take a 1223 00:46:10,400 --> 00:46:12,079 look so i'm going to run the program one 1224 00:46:12,079 --> 00:46:15,520 more time look we still get python for 1225 00:46:15,520 --> 00:46:18,000 beginners we can also replace a single 1226 00:46:18,000 --> 00:46:20,400 character for example we can replace 1227 00:46:20,400 --> 00:46:22,640 capital p with 1228 00:46:22,640 --> 00:46:25,200 let's say capital j 1229 00:46:25,200 --> 00:46:28,000 now when we run this program we get 1230 00:46:28,000 --> 00:46:30,640 jython for beginners so these are the 1231 00:46:30,640 --> 00:46:32,960 find and replace method 1232 00:46:32,960 --> 00:46:34,480 and one last thing i want to show you in 1233 00:46:34,480 --> 00:46:36,319 this tutorial there are times that you 1234 00:46:36,319 --> 00:46:37,920 want to check the existence of a 1235 00:46:37,920 --> 00:46:39,839 character or a sequence of characters in 1236 00:46:39,839 --> 00:46:42,000 your string in those situations you use 1237 00:46:42,000 --> 00:46:44,640 the in operator so let's say we want to 1238 00:46:44,640 --> 00:46:47,200 know if this string contains the word 1239 00:46:47,200 --> 00:46:49,440 python we can write an expression like 1240 00:46:49,440 --> 00:46:50,880 this 1241 00:46:50,880 --> 00:46:52,640 string python 1242 00:46:52,640 --> 00:46:54,079 space 1243 00:46:54,079 --> 00:46:57,599 in space course so we're checking to see 1244 00:46:57,599 --> 00:47:01,680 if python is in the course variable now 1245 00:47:01,680 --> 00:47:03,760 this is an expression that produces a 1246 00:47:03,760 --> 00:47:06,560 boolean value like a true or false so we 1247 00:47:06,560 --> 00:47:08,640 refer to this expression as a boolean 1248 00:47:08,640 --> 00:47:09,680 expression 1249 00:47:09,680 --> 00:47:13,839 now if we print this on the terminal 1250 00:47:13,839 --> 00:47:16,240 we should get true and by the way let me 1251 00:47:16,240 --> 00:47:17,760 delete the second line we don't need it 1252 00:47:17,760 --> 00:47:21,200 anymore so run the program we get true 1253 00:47:21,200 --> 00:47:23,599 but if i change this capital p to a 1254 00:47:23,599 --> 00:47:25,119 lowercase p 1255 00:47:25,119 --> 00:47:27,599 and run the program we get false because 1256 00:47:27,599 --> 00:47:30,400 we don't have this exact sequence of 1257 00:47:30,400 --> 00:47:32,720 characters in our string now note that 1258 00:47:32,720 --> 00:47:34,960 the difference between the in operator 1259 00:47:34,960 --> 00:47:36,960 and define method is that our find 1260 00:47:36,960 --> 00:47:40,000 method returns the index of that 1261 00:47:40,000 --> 00:47:42,640 character or sequence of characters but 1262 00:47:42,640 --> 00:47:46,160 the in operator produces a boolean value 1263 00:47:46,160 --> 00:47:48,000 do we have this or not 1264 00:47:48,000 --> 00:47:49,839 so that's the difference now let's 1265 00:47:49,839 --> 00:47:51,839 quickly recap all the cool things you 1266 00:47:51,839 --> 00:47:53,280 learn to do with strings in this 1267 00:47:53,280 --> 00:47:54,559 tutorial 1268 00:47:54,559 --> 00:47:57,040 we can use the len function to count the 1269 00:47:57,040 --> 00:47:59,200 number of characters in a string this is 1270 00:47:59,200 --> 00:48:01,280 a general purpose function built into 1271 00:48:01,280 --> 00:48:04,000 python we also have specific functions 1272 00:48:04,000 --> 00:48:06,079 for strings which we referred to as 1273 00:48:06,079 --> 00:48:08,559 methods these include 1274 00:48:08,559 --> 00:48:10,880 upper for converting a string into 1275 00:48:10,880 --> 00:48:13,839 uppercase we also have lower 1276 00:48:13,839 --> 00:48:16,480 and title methods 1277 00:48:16,480 --> 00:48:18,720 you learn about the find method which 1278 00:48:18,720 --> 00:48:21,520 returns the index of a character or 1279 00:48:21,520 --> 00:48:24,079 sequence of characters we have the 1280 00:48:24,079 --> 00:48:26,160 replace method for replacing characters 1281 00:48:26,160 --> 00:48:28,240 and words in a string and finally you 1282 00:48:28,240 --> 00:48:31,280 learned about the in operator so 1283 00:48:31,280 --> 00:48:34,559 some characters in 1284 00:48:34,559 --> 00:48:37,200 a string 1285 00:48:40,800 --> 00:48:42,240 so you have learned that in python 1286 00:48:42,240 --> 00:48:44,240 programming language we have two types 1287 00:48:44,240 --> 00:48:46,559 of numbers integers which are whole 1288 00:48:46,559 --> 00:48:48,240 numbers like 10 they don't have a 1289 00:48:48,240 --> 00:48:50,960 decimal point and floating point numbers 1290 00:48:50,960 --> 00:48:52,960 or floats which are numbers with a 1291 00:48:52,960 --> 00:48:54,319 decimal point 1292 00:48:54,319 --> 00:48:55,920 now in this tutorial we're going to look 1293 00:48:55,920 --> 00:48:58,240 at the arithmetic operations supported 1294 00:48:58,240 --> 00:49:00,319 in python language these are the same 1295 00:49:00,319 --> 00:49:02,480 arithmetic operations that we have in 1296 00:49:02,480 --> 00:49:04,880 math we can add numbers multiply them 1297 00:49:04,880 --> 00:49:07,119 and so on so let's look at a few 1298 00:49:07,119 --> 00:49:09,359 examples we can print 1299 00:49:09,359 --> 00:49:10,160 10 1300 00:49:10,160 --> 00:49:11,760 plus 3. 1301 00:49:11,760 --> 00:49:14,160 so this is the addition operator we also 1302 00:49:14,160 --> 00:49:18,000 have subtraction we have multiplication 1303 00:49:18,000 --> 00:49:20,559 we have two kinds of division here's one 1304 00:49:20,559 --> 00:49:22,640 with a forward slash let's run this 1305 00:49:22,640 --> 00:49:24,640 program and see what we get 1306 00:49:24,640 --> 00:49:27,200 we get a floating point number but we 1307 00:49:27,200 --> 00:49:29,599 also have another division operator for 1308 00:49:29,599 --> 00:49:31,839 getting an integer so 1309 00:49:31,839 --> 00:49:34,480 if we add another slash here and run 1310 00:49:34,480 --> 00:49:36,640 this program 1311 00:49:36,640 --> 00:49:38,960 we get an integer 1312 00:49:38,960 --> 00:49:42,319 we have another operator called modulus 1313 00:49:42,319 --> 00:49:44,720 which is a percent sign and this returns 1314 00:49:44,720 --> 00:49:47,359 the remainder of the division so when we 1315 00:49:47,359 --> 00:49:50,000 run this program we should get 1. 1316 00:49:50,000 --> 00:49:51,520 there you go 1317 00:49:51,520 --> 00:49:53,680 and one last operator we have here is 1318 00:49:53,680 --> 00:49:56,079 exponent which is the power 1319 00:49:56,079 --> 00:49:59,280 so that is indicated with two asterisks 1320 00:49:59,280 --> 00:50:02,160 and this will return 10 to the power of 1321 00:50:02,160 --> 00:50:04,720 three so let's run this program we get a 1322 00:50:04,720 --> 00:50:05,839 thousand 1323 00:50:05,839 --> 00:50:08,400 so these are the arithmetic operators in 1324 00:50:08,400 --> 00:50:10,720 python programming language now for all 1325 00:50:10,720 --> 00:50:12,960 these operators that you learned we have 1326 00:50:12,960 --> 00:50:16,079 an augmented assignment operator that is 1327 00:50:16,079 --> 00:50:18,480 very useful let me show you so 1328 00:50:18,480 --> 00:50:20,960 let's say we have a variable called x we 1329 00:50:20,960 --> 00:50:23,440 set it to 10. now we want to increment 1330 00:50:23,440 --> 00:50:25,760 this by three we'll have to write code 1331 00:50:25,760 --> 00:50:30,240 like this x we set this to x plus three 1332 00:50:30,240 --> 00:50:32,720 so python interpreter will add 10 to 1333 00:50:32,720 --> 00:50:35,200 three the result is 13 and then it gets 1334 00:50:35,200 --> 00:50:39,359 stored into x again so when we print x 1335 00:50:39,359 --> 00:50:40,480 we should see 1336 00:50:40,480 --> 00:50:42,559 13. there you go 1337 00:50:42,559 --> 00:50:45,200 so this is how we can increment a number 1338 00:50:45,200 --> 00:50:47,839 right now augmented assignment operator 1339 00:50:47,839 --> 00:50:50,240 is a way to write the same code but in a 1340 00:50:50,240 --> 00:50:53,599 shorter form this is how it works 1341 00:50:53,599 --> 00:50:55,040 we type x 1342 00:50:55,040 --> 00:50:58,079 plus equals three what we have on line 1343 00:50:58,079 --> 00:51:01,359 three is exactly like what we have on 1344 00:51:01,359 --> 00:51:02,720 line two 1345 00:51:02,720 --> 00:51:05,119 so this is what we call the augmented 1346 00:51:05,119 --> 00:51:08,319 assignment operator we have augmented or 1347 00:51:08,319 --> 00:51:11,040 enhanced the assignment operator 1348 00:51:11,040 --> 00:51:12,880 now in this particular case we are 1349 00:51:12,880 --> 00:51:14,559 incrementing a number using the 1350 00:51:14,559 --> 00:51:16,720 augmented assignment operator but we can 1351 00:51:16,720 --> 00:51:19,359 also subtract or multiply a number by a 1352 00:51:19,359 --> 00:51:21,760 given value for example let's delete 1353 00:51:21,760 --> 00:51:24,079 what we have on line two 1354 00:51:24,079 --> 00:51:25,359 we can type 1355 00:51:25,359 --> 00:51:27,839 subtract equal three so here we are 1356 00:51:27,839 --> 00:51:30,000 subtracting three from x 1357 00:51:30,000 --> 00:51:31,839 when we run this program we should see 1358 00:51:31,839 --> 00:51:33,200 seven 1359 00:51:33,200 --> 00:51:36,680 there you go 1360 00:51:39,839 --> 00:51:41,599 now let me ask you a question 1361 00:51:41,599 --> 00:51:43,839 i'm going to clear all this code here 1362 00:51:43,839 --> 00:51:46,559 define x and set it to 10 1363 00:51:46,559 --> 00:51:50,079 plus 3 times 2. what do you think is the 1364 00:51:50,079 --> 00:51:52,480 result of this expression this is a very 1365 00:51:52,480 --> 00:51:54,640 basic math question that unfortunately a 1366 00:51:54,640 --> 00:51:56,559 lot of people fail to answer 1367 00:51:56,559 --> 00:51:59,520 the answer is 16. because in math we 1368 00:51:59,520 --> 00:52:01,760 have this concept called operator 1369 00:52:01,760 --> 00:52:04,000 precedence which means the order of 1370 00:52:04,000 --> 00:52:05,200 operations 1371 00:52:05,200 --> 00:52:07,760 so the multiplication operator has a 1372 00:52:07,760 --> 00:52:10,480 higher precedence so it's applied first 1373 00:52:10,480 --> 00:52:13,200 which means three by two is executed 1374 00:52:13,200 --> 00:52:16,319 first the result is six and then it's 1375 00:52:16,319 --> 00:52:19,599 added to 10. that is why x should be 16 1376 00:52:19,599 --> 00:52:22,000 after we run this code let's verify that 1377 00:52:22,000 --> 00:52:26,960 so print x run the program x is 16. 1378 00:52:26,960 --> 00:52:29,040 so this is what we call operator 1379 00:52:29,040 --> 00:52:31,520 precedence it's just a basic math 1380 00:52:31,520 --> 00:52:33,440 concept it's not about python 1381 00:52:33,440 --> 00:52:35,200 programming language so all the other 1382 00:52:35,200 --> 00:52:37,200 programming languages behave the same 1383 00:52:37,200 --> 00:52:38,160 way 1384 00:52:38,160 --> 00:52:40,240 so here's the order first we have the 1385 00:52:40,240 --> 00:52:42,720 exponentiation which is the power like 2 1386 00:52:42,720 --> 00:52:45,040 to the power of 3 then we have 1387 00:52:45,040 --> 00:52:46,400 multiplication 1388 00:52:46,400 --> 00:52:47,280 or 1389 00:52:47,280 --> 00:52:48,480 division 1390 00:52:48,480 --> 00:52:50,640 and finally we have addition or 1391 00:52:50,640 --> 00:52:52,079 subtraction 1392 00:52:52,079 --> 00:52:54,720 this is the order of operations let me 1393 00:52:54,720 --> 00:52:56,720 show you another example here i'm going 1394 00:52:56,720 --> 00:52:57,839 to add 1395 00:52:57,839 --> 00:53:01,040 the exponentiation operator so 1396 00:53:01,040 --> 00:53:03,839 2 to the power of 2. once again what do 1397 00:53:03,839 --> 00:53:05,200 you think is the result of this 1398 00:53:05,200 --> 00:53:07,119 expression pause the video and think 1399 00:53:07,119 --> 00:53:10,160 about it for a few seconds 1400 00:53:10,160 --> 00:53:12,240 the answer is 22 1401 00:53:12,240 --> 00:53:14,079 because the exponentiation operator 1402 00:53:14,079 --> 00:53:17,280 takes precedence so first 2 to the power 1403 00:53:17,280 --> 00:53:20,640 of 2 is executed the result is 4. then 4 1404 00:53:20,640 --> 00:53:22,800 is multiplied by 3 1405 00:53:22,800 --> 00:53:25,760 that is 12 and finally 12 is added to 1406 00:53:25,760 --> 00:53:28,960 10. so x should be 22. 1407 00:53:28,960 --> 00:53:30,960 so let's run this program and verify 1408 00:53:30,960 --> 00:53:33,440 this so i'm going to delete these lines 1409 00:53:33,440 --> 00:53:34,240 here 1410 00:53:34,240 --> 00:53:37,680 run the program there you go x is 22. 1411 00:53:37,680 --> 00:53:40,480 now let me bring back these rules here 1412 00:53:40,480 --> 00:53:42,720 we can also use parentheses to change 1413 00:53:42,720 --> 00:53:45,440 the order of operations so if we have 1414 00:53:45,440 --> 00:53:47,359 parenthesis 1415 00:53:47,359 --> 00:53:49,839 it always takes priority 1416 00:53:49,839 --> 00:53:52,640 in this case we can add parenthesis 1417 00:53:52,640 --> 00:53:55,680 around 10 plus 3 so this piece of code 1418 00:53:55,680 --> 00:53:58,960 will be executed first the result is 13 1419 00:53:58,960 --> 00:54:00,880 then the exponentiation operator will be 1420 00:54:00,880 --> 00:54:04,160 executed so 2 to the power of 2 is 4 and 1421 00:54:04,160 --> 00:54:07,920 finally 4 is multiplied by 13. 1422 00:54:07,920 --> 00:54:11,119 now here's a little exercise for you 1423 00:54:11,119 --> 00:54:13,839 i'm going to set x to parenthesis 2 plus 1424 00:54:13,839 --> 00:54:17,680 3 times 10 minus 3. what is the result 1425 00:54:17,680 --> 00:54:19,599 of this pause the video and think about 1426 00:54:19,599 --> 00:54:23,079 it for a few seconds 1427 00:54:25,359 --> 00:54:28,000 so you learn that parenthesis always 1428 00:54:28,000 --> 00:54:30,079 overwrites the order so this piece of 1429 00:54:30,079 --> 00:54:33,359 code is executed first the result is 5. 1430 00:54:33,359 --> 00:54:36,400 then between the multiplication and 1431 00:54:36,400 --> 00:54:38,960 subtraction you know that multiplication 1432 00:54:38,960 --> 00:54:41,760 takes precedence so next 5 will be 1433 00:54:41,760 --> 00:54:44,720 multiplied by 10 the result is 50 and 1434 00:54:44,720 --> 00:54:47,839 finally we have the subtraction so 50 1435 00:54:47,839 --> 00:54:51,440 minus 3 will be 47. let's verify this 1436 00:54:51,440 --> 00:54:52,799 print x 1437 00:54:52,799 --> 00:54:55,119 run the program there you go i hope you 1438 00:54:55,119 --> 00:54:56,480 guessed it right 1439 00:54:56,480 --> 00:54:59,040 so this is all about operator precedence 1440 00:54:59,040 --> 00:55:00,880 it's a very important topic and i see it 1441 00:55:00,880 --> 00:55:02,960 quite often in python tests so if you're 1442 00:55:02,960 --> 00:55:04,960 preparing for a python test make sure to 1443 00:55:04,960 --> 00:55:08,319 watch this tutorial one more time 1444 00:55:11,760 --> 00:55:13,119 in this tutorial we're going to look at 1445 00:55:13,119 --> 00:55:15,200 a few useful functions for working with 1446 00:55:15,200 --> 00:55:17,040 numbers let's start by defining a 1447 00:55:17,040 --> 00:55:19,960 variable like x and set it to 1448 00:55:19,960 --> 00:55:23,119 2.9 now to round this number we can use 1449 00:55:23,119 --> 00:55:26,000 the built-in round function so we call 1450 00:55:26,000 --> 00:55:28,480 the round function give it x and then 1451 00:55:28,480 --> 00:55:32,319 print the result let's run this program 1452 00:55:32,319 --> 00:55:35,040 so we get three we have another useful 1453 00:55:35,040 --> 00:55:37,200 built-in function called abs which is 1454 00:55:37,200 --> 00:55:39,040 short for absolute and this is the 1455 00:55:39,040 --> 00:55:41,280 absolute function that we have in math 1456 00:55:41,280 --> 00:55:43,839 we give it a value and it always returns 1457 00:55:43,839 --> 00:55:45,839 the positive representation of that 1458 00:55:45,839 --> 00:55:48,319 value even if that value is negative 1459 00:55:48,319 --> 00:55:50,720 here's an example let's call 1460 00:55:50,720 --> 00:55:53,559 the abs function and give it negative 1461 00:55:53,559 --> 00:55:56,000 2.9 when we run this program we're going 1462 00:55:56,000 --> 00:55:59,200 to see 2.9 on the terminal so let's go 1463 00:55:59,200 --> 00:56:01,760 ahead there you go so absolute always 1464 00:56:01,760 --> 00:56:03,920 returns a positive number now 1465 00:56:03,920 --> 00:56:05,839 technically in python we have a handful 1466 00:56:05,839 --> 00:56:07,760 of built-in functions for performing 1467 00:56:07,760 --> 00:56:09,599 mathematical operations if you want to 1468 00:56:09,599 --> 00:56:11,920 write a program that involves complex 1469 00:56:11,920 --> 00:56:14,000 mathematical calculations you need to 1470 00:56:14,000 --> 00:56:16,400 import the math module 1471 00:56:16,400 --> 00:56:18,960 a module in python is a separate file 1472 00:56:18,960 --> 00:56:21,280 with some reusable code we use these 1473 00:56:21,280 --> 00:56:23,359 modules to organize our code into 1474 00:56:23,359 --> 00:56:25,599 different files as a metaphor think of a 1475 00:56:25,599 --> 00:56:27,920 supermarket when you go to a supermarket 1476 00:56:27,920 --> 00:56:29,680 you see different sections for fruits 1477 00:56:29,680 --> 00:56:32,079 and vegetables cleaning products junk 1478 00:56:32,079 --> 00:56:34,160 food and so on each section in the 1479 00:56:34,160 --> 00:56:36,880 supermarket is like a module in python 1480 00:56:36,880 --> 00:56:38,880 so in python we have this math module 1481 00:56:38,880 --> 00:56:40,960 which contains a bunch of reusable 1482 00:56:40,960 --> 00:56:42,799 functions for performing mathematical 1483 00:56:42,799 --> 00:56:45,359 calculations so let me show you how to 1484 00:56:45,359 --> 00:56:47,760 use this module on the top 1485 00:56:47,760 --> 00:56:48,880 we type 1486 00:56:48,880 --> 00:56:50,480 import 1487 00:56:50,480 --> 00:56:53,440 math all in lowercase with this we can 1488 00:56:53,440 --> 00:56:56,079 import the math module now math is an 1489 00:56:56,079 --> 00:56:58,640 object like a string so we can access 1490 00:56:58,640 --> 00:57:00,559 its functions or more accurately its 1491 00:57:00,559 --> 00:57:03,440 methods using the dot operator so if you 1492 00:57:03,440 --> 00:57:07,040 type math dot look these are all the 1493 00:57:07,040 --> 00:57:09,599 mathematical functions available in this 1494 00:57:09,599 --> 00:57:11,920 module for example we can call the seal 1495 00:57:11,920 --> 00:57:14,480 method to get the ceiling of a number so 1496 00:57:14,480 --> 00:57:16,640 if we pass 2.9 here 1497 00:57:16,640 --> 00:57:19,119 and then print the result we should see 1498 00:57:19,119 --> 00:57:21,520 three let me delete all this other code 1499 00:57:21,520 --> 00:57:24,160 here all right let's run this program 1500 00:57:24,160 --> 00:57:26,640 there you go so we get three 1501 00:57:26,640 --> 00:57:28,240 another useful method is the floor 1502 00:57:28,240 --> 00:57:31,760 method so let's give that a try floor of 1503 00:57:31,760 --> 00:57:35,119 2.9 what do you think we're gonna get 1504 00:57:35,119 --> 00:57:36,880 we get two 1505 00:57:36,880 --> 00:57:38,400 now there's so many functions built in 1506 00:57:38,400 --> 00:57:39,920 this module and we don't really have 1507 00:57:39,920 --> 00:57:41,920 time to go through all of them but let 1508 00:57:41,920 --> 00:57:43,680 me show you how you can learn about them 1509 00:57:43,680 --> 00:57:44,960 on your own 1510 00:57:44,960 --> 00:57:46,960 open up your browser and search for 1511 00:57:46,960 --> 00:57:50,480 python3 math module make sure to add the 1512 00:57:50,480 --> 00:57:53,119 version python3 because the mass module 1513 00:57:53,119 --> 00:57:55,200 in python 2 is slightly different from 1514 00:57:55,200 --> 00:57:57,440 the math module in python 3. 1515 00:57:57,440 --> 00:58:01,280 so python 3 math module 1516 00:58:01,280 --> 00:58:03,359 now here you can see the documentation 1517 00:58:03,359 --> 00:58:06,000 for this module let's go 1518 00:58:06,000 --> 00:58:08,880 let's have a look if you scroll down you 1519 00:58:08,880 --> 00:58:10,480 can see the list of all the functions 1520 00:58:10,480 --> 00:58:12,240 and their explanation 1521 00:58:12,240 --> 00:58:14,079 so as an exercise i encourage you to 1522 00:58:14,079 --> 00:58:15,760 spend five minutes have a quick look at 1523 00:58:15,760 --> 00:58:17,839 this documentation see what functions 1524 00:58:17,839 --> 00:58:22,040 are there for you in case you need them 1525 00:58:24,880 --> 00:58:26,079 in this tutorial i'm going to talk to 1526 00:58:26,079 --> 00:58:28,880 you guys about if statements in python 1527 00:58:28,880 --> 00:58:30,880 if statements are extremely important in 1528 00:58:30,880 --> 00:58:32,720 programming and they allow us to build 1529 00:58:32,720 --> 00:58:35,119 programs that can make decisions based 1530 00:58:35,119 --> 00:58:37,040 on some conditions so if some condition 1531 00:58:37,040 --> 00:58:38,559 is true we're going to do certain things 1532 00:58:38,559 --> 00:58:40,640 otherwise we're going to do other things 1533 00:58:40,640 --> 00:58:42,000 here's an example 1534 00:58:42,000 --> 00:58:44,079 over here i've got this text file with a 1535 00:58:44,079 --> 00:58:46,000 bunch of rules for our program 1536 00:58:46,000 --> 00:58:47,920 if it's a hot day perhaps you want to 1537 00:58:47,920 --> 00:58:50,240 tell the user it's a hot day so make 1538 00:58:50,240 --> 00:58:52,720 sure to drink plenty of water 1539 00:58:52,720 --> 00:58:53,760 otherwise 1540 00:58:53,760 --> 00:58:56,559 if it's cold so here's another condition 1541 00:58:56,559 --> 00:58:58,319 if this condition is true we're going to 1542 00:58:58,319 --> 00:59:01,119 tell the user it's a cold day so wear 1543 00:59:01,119 --> 00:59:02,480 warm clothes 1544 00:59:02,480 --> 00:59:04,720 and otherwise if it's neither hot or 1545 00:59:04,720 --> 00:59:06,720 cold we want to tell the user it's a 1546 00:59:06,720 --> 00:59:07,839 lovely day 1547 00:59:07,839 --> 00:59:09,280 so let me show you how to write a 1548 00:59:09,280 --> 00:59:11,839 program that simulates these rules 1549 00:59:11,839 --> 00:59:12,640 so 1550 00:59:12,640 --> 00:59:14,640 back to our program here we start by 1551 00:59:14,640 --> 00:59:16,720 defining a boolean variable 1552 00:59:16,720 --> 00:59:20,880 is underline hot we set this to true 1553 00:59:20,880 --> 00:59:23,760 next we add an if statement so if 1554 00:59:23,760 --> 00:59:25,680 here we need to add a condition in this 1555 00:59:25,680 --> 00:59:27,119 case we're going to use our boolean 1556 00:59:27,119 --> 00:59:30,480 variable so is underline hot 1557 00:59:30,480 --> 00:59:32,559 now if this evaluates to true then we're 1558 00:59:32,559 --> 00:59:34,319 going to do certain things in this case 1559 00:59:34,319 --> 00:59:36,400 we want to tell the user hey it's a hot 1560 00:59:36,400 --> 00:59:38,480 day drink plenty of water 1561 00:59:38,480 --> 00:59:40,400 so back to our program 1562 00:59:40,400 --> 00:59:43,680 after our condition we add a colon 1563 00:59:43,680 --> 00:59:46,799 now note that when i press enter pycharm 1564 00:59:46,799 --> 00:59:49,599 automatically indents our cursor 1565 00:59:49,599 --> 00:59:51,440 now any code that we write here will be 1566 00:59:51,440 --> 00:59:54,000 executed if this condition is true 1567 00:59:54,000 --> 00:59:55,920 otherwise it will be ignored here's an 1568 00:59:55,920 --> 00:59:56,960 example 1569 00:59:56,960 --> 00:59:59,280 let's write a print statement 1570 00:59:59,280 --> 01:00:01,119 here i'm going to use double quotes 1571 01:00:01,119 --> 01:00:03,040 because i want to use an apostrophe in 1572 01:00:03,040 --> 01:00:04,720 our string so 1573 01:00:04,720 --> 01:00:06,799 it's a hot day 1574 01:00:06,799 --> 01:00:08,640 now let's press enter you can see the 1575 01:00:08,640 --> 01:00:10,880 cursor is still indented that means we 1576 01:00:10,880 --> 01:00:12,319 can write more code that will be 1577 01:00:12,319 --> 01:00:14,480 executed if this condition is true in 1578 01:00:14,480 --> 01:00:15,839 this case let's say we don't want to 1579 01:00:15,839 --> 01:00:18,960 write any extra code so to terminate 1580 01:00:18,960 --> 01:00:21,599 this block we need to press shift and 1581 01:00:21,599 --> 01:00:23,839 tab now the cursor is at the beginning 1582 01:00:23,839 --> 01:00:25,920 of the line so let's write a print 1583 01:00:25,920 --> 01:00:29,200 statement with a message like enjoy your 1584 01:00:29,200 --> 01:00:30,319 day 1585 01:00:30,319 --> 01:00:32,160 now when we run this program because 1586 01:00:32,160 --> 01:00:33,680 this condition is true we're going to 1587 01:00:33,680 --> 01:00:36,160 see this message followed by the second 1588 01:00:36,160 --> 01:00:39,119 message take a look so run 1589 01:00:39,119 --> 01:00:41,359 there you go it's a hot day enjoy your 1590 01:00:41,359 --> 01:00:43,599 day but if i go over here 1591 01:00:43,599 --> 01:00:46,079 and change this boolean value to false 1592 01:00:46,079 --> 01:00:47,920 and run the program again 1593 01:00:47,920 --> 01:00:50,160 our first message disappears and we only 1594 01:00:50,160 --> 01:00:53,119 see the second message enjoy your day so 1595 01:00:53,119 --> 01:00:55,920 this is how if statements work now back 1596 01:00:55,920 --> 01:00:58,960 here we can add another print statement 1597 01:00:58,960 --> 01:01:00,640 let's say 1598 01:01:00,640 --> 01:01:01,599 drink 1599 01:01:01,599 --> 01:01:02,880 plenty 1600 01:01:02,880 --> 01:01:05,359 of water now because this print 1601 01:01:05,359 --> 01:01:07,760 statement is also indented it will be 1602 01:01:07,760 --> 01:01:09,920 executed if this condition is true so 1603 01:01:09,920 --> 01:01:12,960 i'm going to revert is hot to true and 1604 01:01:12,960 --> 01:01:15,280 run the program one more time 1605 01:01:15,280 --> 01:01:18,079 there you go so it's a hot day drink 1606 01:01:18,079 --> 01:01:21,520 plenty of water and enjoy your day 1607 01:01:21,520 --> 01:01:23,040 all right now let's add a second rule 1608 01:01:23,040 --> 01:01:25,359 here if it's hot we're going to execute 1609 01:01:25,359 --> 01:01:27,520 these two lines otherwise if it's not 1610 01:01:27,520 --> 01:01:29,920 hot we want to print a different message 1611 01:01:29,920 --> 01:01:32,079 so here 1612 01:01:32,079 --> 01:01:33,920 we remove the indentation 1613 01:01:33,920 --> 01:01:35,839 and type else 1614 01:01:35,839 --> 01:01:36,960 colon 1615 01:01:36,960 --> 01:01:38,720 now when we press enter 1616 01:01:38,720 --> 01:01:41,119 once again our cursor is indented so the 1617 01:01:41,119 --> 01:01:43,599 code that we write here will be executed 1618 01:01:43,599 --> 01:01:46,559 if this condition is not true 1619 01:01:46,559 --> 01:01:48,799 so here we can print 1620 01:01:48,799 --> 01:01:51,599 it's a cold day 1621 01:01:51,599 --> 01:01:53,280 print 1622 01:01:53,280 --> 01:01:54,240 wear 1623 01:01:54,240 --> 01:01:56,480 warm clothes 1624 01:01:56,480 --> 01:01:59,119 now let's run our program one more time 1625 01:01:59,119 --> 01:02:01,280 so we get the messages about a hot day 1626 01:02:01,280 --> 01:02:03,599 followed by enjoy your day we don't see 1627 01:02:03,599 --> 01:02:06,319 any messages about a cold day now if we 1628 01:02:06,319 --> 01:02:08,000 go back here on the top 1629 01:02:08,000 --> 01:02:08,799 and 1630 01:02:08,799 --> 01:02:11,119 change this boolean value to false and 1631 01:02:11,119 --> 01:02:12,799 run our program 1632 01:02:12,799 --> 01:02:14,880 we see different set of messages it's a 1633 01:02:14,880 --> 01:02:17,680 cold day wear warm clothes and enjoy 1634 01:02:17,680 --> 01:02:18,960 your day 1635 01:02:18,960 --> 01:02:20,880 but there's a problem with our program 1636 01:02:20,880 --> 01:02:22,640 if it's not hot it doesn't necessarily 1637 01:02:22,640 --> 01:02:24,480 mean that it's cold maybe it's a lovely 1638 01:02:24,480 --> 01:02:26,720 day so the absence of heat doesn't mean 1639 01:02:26,720 --> 01:02:27,839 it's cold 1640 01:02:27,839 --> 01:02:29,760 back to our conditions 1641 01:02:29,760 --> 01:02:31,839 here on line four we have this rule that 1642 01:02:31,839 --> 01:02:35,200 says if it's a cold day then print these 1643 01:02:35,200 --> 01:02:37,520 messages otherwise if it's neither hot 1644 01:02:37,520 --> 01:02:40,799 or cold then say it's a lovely day so to 1645 01:02:40,799 --> 01:02:43,359 implement this rule we need to go back 1646 01:02:43,359 --> 01:02:44,799 to after pi 1647 01:02:44,799 --> 01:02:47,599 and define another variable so 1648 01:02:47,599 --> 01:02:50,319 let's say is underline called 1649 01:02:50,319 --> 01:02:51,920 we set this to 1650 01:02:51,920 --> 01:02:53,039 true 1651 01:02:53,039 --> 01:02:54,559 now here we need to add a second 1652 01:02:54,559 --> 01:02:56,720 condition so after 1653 01:02:56,720 --> 01:02:59,280 our first if statement we can use an 1654 01:02:59,280 --> 01:03:01,359 elif statement to define a second 1655 01:03:01,359 --> 01:03:03,680 condition here's how it works 1656 01:03:03,680 --> 01:03:08,160 so l if which is short for else if or 1657 01:03:08,160 --> 01:03:10,799 otherwise if now here we add another 1658 01:03:10,799 --> 01:03:14,559 condition so is called 1659 01:03:14,559 --> 01:03:16,880 so if it's cold you want to execute 1660 01:03:16,880 --> 01:03:18,880 these two lines 1661 01:03:18,880 --> 01:03:21,599 so let's cut these from here and move 1662 01:03:21,599 --> 01:03:22,400 them 1663 01:03:22,400 --> 01:03:24,880 under our second condition 1664 01:03:24,880 --> 01:03:27,599 and finally if none of these conditions 1665 01:03:27,599 --> 01:03:30,319 are true you want to print a different 1666 01:03:30,319 --> 01:03:31,359 message 1667 01:03:31,359 --> 01:03:34,400 it's a lovely day 1668 01:03:34,400 --> 01:03:35,359 so 1669 01:03:35,359 --> 01:03:36,640 right now 1670 01:03:36,640 --> 01:03:40,319 is hot is false is cold it's true so 1671 01:03:40,319 --> 01:03:41,920 when we run this program python 1672 01:03:41,920 --> 01:03:43,520 interpreter is going to execute the 1673 01:03:43,520 --> 01:03:45,760 first if statement in this case because 1674 01:03:45,760 --> 01:03:48,319 our condition is false these two lines 1675 01:03:48,319 --> 01:03:51,039 will be ignored then python interpreter 1676 01:03:51,039 --> 01:03:53,839 will look at line seven it will evaluate 1677 01:03:53,839 --> 01:03:56,400 this condition in this case is called is 1678 01:03:56,400 --> 01:03:58,240 true so we're going to see these two 1679 01:03:58,240 --> 01:04:00,240 messages on the terminal 1680 01:04:00,240 --> 01:04:02,079 now in this case because one of these 1681 01:04:02,079 --> 01:04:04,319 conditions was true this else statement 1682 01:04:04,319 --> 01:04:06,000 will be ignored so we are not going to 1683 01:04:06,000 --> 01:04:09,119 see this message and finally as before 1684 01:04:09,119 --> 01:04:10,960 we are always going to see this message 1685 01:04:10,960 --> 01:04:14,400 so let's run our program there you go 1686 01:04:14,400 --> 01:04:15,760 it's a cold day 1687 01:04:15,760 --> 01:04:19,280 wear warm clothes and enjoy your day 1688 01:04:19,280 --> 01:04:21,039 now 1689 01:04:21,039 --> 01:04:24,240 back to the top if we change is cold to 1690 01:04:24,240 --> 01:04:25,200 false 1691 01:04:25,200 --> 01:04:27,520 it's neither hot nor cold so it's going 1692 01:04:27,520 --> 01:04:29,839 to be a lovely day let's run the program 1693 01:04:29,839 --> 01:04:30,799 and 1694 01:04:30,799 --> 01:04:33,200 here you go it's a lovely day enjoy your 1695 01:04:33,200 --> 01:04:34,400 day 1696 01:04:34,400 --> 01:04:35,920 so this is the basic of using ip 1697 01:04:35,920 --> 01:04:37,760 statements as you can see they're very 1698 01:04:37,760 --> 01:04:40,000 useful in programming and with this we 1699 01:04:40,000 --> 01:04:41,760 can build all kinds of rules into our 1700 01:04:41,760 --> 01:04:43,440 programs 1701 01:04:43,440 --> 01:04:45,599 okay here's an exercise for you imagine 1702 01:04:45,599 --> 01:04:47,440 the price of a house is one million 1703 01:04:47,440 --> 01:04:50,319 dollars now if the buyer has good credit 1704 01:04:50,319 --> 01:04:52,160 they need to put down 10 1705 01:04:52,160 --> 01:04:54,480 of the price of this property otherwise 1706 01:04:54,480 --> 01:04:56,880 they need to put down 20 1707 01:04:56,880 --> 01:04:58,640 write your program with these rules and 1708 01:04:58,640 --> 01:05:00,720 display the down payment required for a 1709 01:05:00,720 --> 01:05:03,359 buyer with good credit you will see my 1710 01:05:03,359 --> 01:05:06,839 solution next 1711 01:05:08,880 --> 01:05:10,240 all right let's define a variable for 1712 01:05:10,240 --> 01:05:12,880 the price of this house so price we set 1713 01:05:12,880 --> 01:05:16,880 this to 1 million so one with six zeros 1714 01:05:16,880 --> 01:05:18,880 next we need a variable to tell if this 1715 01:05:18,880 --> 01:05:20,960 buyer has good credit 1716 01:05:20,960 --> 01:05:22,400 so has 1717 01:05:22,400 --> 01:05:23,359 good 1718 01:05:23,359 --> 01:05:26,240 credit and we set this to true 1719 01:05:26,240 --> 01:05:28,799 now we need an if statement so 1720 01:05:28,799 --> 01:05:29,920 if has 1721 01:05:29,920 --> 01:05:32,160 good credit is true 1722 01:05:32,160 --> 01:05:33,359 colon 1723 01:05:33,359 --> 01:05:35,039 here we need to calculate the down 1724 01:05:35,039 --> 01:05:38,480 payment so down underline payment 1725 01:05:38,480 --> 01:05:42,079 should be equal to 0.1 times the price 1726 01:05:42,079 --> 01:05:43,280 that is 10 1727 01:05:43,280 --> 01:05:45,680 of the price of this property 1728 01:05:45,680 --> 01:05:47,200 otherwise 1729 01:05:47,200 --> 01:05:48,799 colon 1730 01:05:48,799 --> 01:05:50,960 the down payment should be 1731 01:05:50,960 --> 01:05:52,720 0.2 1732 01:05:52,720 --> 01:05:54,640 times price 1733 01:05:54,640 --> 01:05:57,200 and finally we remove the indentation 1734 01:05:57,200 --> 01:05:58,640 and print 1735 01:05:58,640 --> 01:06:01,599 here we can use a formatted string 1736 01:06:01,599 --> 01:06:03,200 first we add a label 1737 01:06:03,200 --> 01:06:06,240 down payment colon and right after that 1738 01:06:06,240 --> 01:06:09,440 we add a placeholder or a hole for our 1739 01:06:09,440 --> 01:06:11,280 down payment variable so 1740 01:06:11,280 --> 01:06:13,119 curly braces 1741 01:06:13,119 --> 01:06:15,280 down payment 1742 01:06:15,280 --> 01:06:17,599 let's run this program 1743 01:06:17,599 --> 01:06:19,760 so down payment for a buyer with good 1744 01:06:19,760 --> 01:06:22,480 credit is 100 000 1745 01:06:22,480 --> 01:06:23,920 now let's improve this by adding a 1746 01:06:23,920 --> 01:06:26,720 dollar sign before this number so 1747 01:06:26,720 --> 01:06:28,799 back to our formatted string just before 1748 01:06:28,799 --> 01:06:30,079 the curly brace 1749 01:06:30,079 --> 01:06:32,079 i'm going to add a dollar sign let's run 1750 01:06:32,079 --> 01:06:36,079 this one more time that is better 1751 01:06:39,839 --> 01:06:41,119 in this tutorial i'm going to talk to 1752 01:06:41,119 --> 01:06:43,680 you guys about the logical operators in 1753 01:06:43,680 --> 01:06:45,920 python we use these operators in 1754 01:06:45,920 --> 01:06:48,079 situations where we have multiple 1755 01:06:48,079 --> 01:06:50,319 conditions here's an example 1756 01:06:50,319 --> 01:06:51,760 let's say we're building an application 1757 01:06:51,760 --> 01:06:55,359 for processing loans if an applicant has 1758 01:06:55,359 --> 01:06:56,640 high income 1759 01:06:56,640 --> 01:06:59,839 and good credit then they're eligible 1760 01:06:59,839 --> 01:07:02,319 for loan so in this example we have two 1761 01:07:02,319 --> 01:07:04,960 conditions one is having high income and 1762 01:07:04,960 --> 01:07:07,839 the other is having good credit so if 1763 01:07:07,839 --> 01:07:10,000 both these conditions are true then the 1764 01:07:10,000 --> 01:07:12,720 applicant is eligible for loan so this 1765 01:07:12,720 --> 01:07:15,039 is where we use the logical 1766 01:07:15,039 --> 01:07:18,079 and operator we use this operator to 1767 01:07:18,079 --> 01:07:20,000 combine two conditions and by the way 1768 01:07:20,000 --> 01:07:21,440 this is not specific to python 1769 01:07:21,440 --> 01:07:23,280 programming language pretty much any 1770 01:07:23,280 --> 01:07:25,280 programming language that supports if 1771 01:07:25,280 --> 01:07:27,760 statements also supports the logical 1772 01:07:27,760 --> 01:07:30,799 operators so back to our program let's 1773 01:07:30,799 --> 01:07:32,799 define two variables 1774 01:07:32,799 --> 01:07:35,200 has high income 1775 01:07:35,200 --> 01:07:37,119 we set this to true 1776 01:07:37,119 --> 01:07:39,680 and another one has 1777 01:07:39,680 --> 01:07:40,720 good 1778 01:07:40,720 --> 01:07:43,520 credit we also set this to true 1779 01:07:43,520 --> 01:07:46,000 now our if statement if 1780 01:07:46,000 --> 01:07:48,880 has high income is true 1781 01:07:48,880 --> 01:07:49,680 and 1782 01:07:49,680 --> 01:07:50,480 has 1783 01:07:50,480 --> 01:07:52,960 good credit is also true 1784 01:07:52,960 --> 01:07:53,920 then 1785 01:07:53,920 --> 01:07:55,680 we're going to print 1786 01:07:55,680 --> 01:07:57,280 eligible 1787 01:07:57,280 --> 01:07:58,960 for loan 1788 01:07:58,960 --> 01:08:01,039 so this is where we're using the and 1789 01:08:01,039 --> 01:08:02,160 operator 1790 01:08:02,160 --> 01:08:04,160 so if both these conditions are true 1791 01:08:04,160 --> 01:08:06,559 then this message will be printed if one 1792 01:08:06,559 --> 01:08:08,160 of them is false we're not going to see 1793 01:08:08,160 --> 01:08:10,240 this message let's try this out so i'm 1794 01:08:10,240 --> 01:08:12,559 going to run this program 1795 01:08:12,559 --> 01:08:15,039 so we see eligible for loan but if we 1796 01:08:15,039 --> 01:08:16,080 change 1797 01:08:16,080 --> 01:08:18,560 either of these conditions to false 1798 01:08:18,560 --> 01:08:21,440 and run the program again 1799 01:08:21,440 --> 01:08:24,400 look the message disappeared so this is 1800 01:08:24,400 --> 01:08:27,440 the logical and operator we also have 1801 01:08:27,440 --> 01:08:29,279 the logical or and we use that in 1802 01:08:29,279 --> 01:08:31,199 situations where we want to do certain 1803 01:08:31,199 --> 01:08:33,440 things if at least one of the conditions 1804 01:08:33,440 --> 01:08:36,159 is true for example let's change 1805 01:08:36,159 --> 01:08:38,480 the rule for this program such that 1806 01:08:38,480 --> 01:08:40,560 if the applicant has high income 1807 01:08:40,560 --> 01:08:43,600 or good credit then they're eligible for 1808 01:08:43,600 --> 01:08:44,480 loan 1809 01:08:44,480 --> 01:08:46,400 so if either or both these conditions 1810 01:08:46,400 --> 01:08:48,960 are true then the applicant is eligible 1811 01:08:48,960 --> 01:08:49,839 now 1812 01:08:49,839 --> 01:08:52,080 back to our program we can implement 1813 01:08:52,080 --> 01:08:53,839 this rule by using the logical or 1814 01:08:53,839 --> 01:08:56,000 operator so we simply replace 1815 01:08:56,000 --> 01:08:57,920 and with or 1816 01:08:57,920 --> 01:08:59,839 now when we run this program we're going 1817 01:08:59,839 --> 01:09:02,080 to see this message because at least one 1818 01:09:02,080 --> 01:09:04,080 of our conditions is true 1819 01:09:04,080 --> 01:09:06,319 let's take a look 1820 01:09:06,319 --> 01:09:08,319 so the applicant is eligible for loan 1821 01:09:08,319 --> 01:09:10,640 because they have good credit if we 1822 01:09:10,640 --> 01:09:12,480 change this to false 1823 01:09:12,480 --> 01:09:14,799 but set the other condition to true 1824 01:09:14,799 --> 01:09:16,560 and run the program we still see the 1825 01:09:16,560 --> 01:09:18,880 same result but if both these conditions 1826 01:09:18,880 --> 01:09:20,400 are false 1827 01:09:20,400 --> 01:09:21,920 then we're not going to see this message 1828 01:09:21,920 --> 01:09:22,880 anymore 1829 01:09:22,880 --> 01:09:24,799 so this is the difference between these 1830 01:09:24,799 --> 01:09:28,480 operators with the logical and operator 1831 01:09:28,480 --> 01:09:30,000 both conditions 1832 01:09:30,000 --> 01:09:31,199 should be true 1833 01:09:31,199 --> 01:09:34,000 with the logical or operator at least 1834 01:09:34,000 --> 01:09:36,640 one condition should be true 1835 01:09:36,640 --> 01:09:39,040 we also have another logical operator 1836 01:09:39,040 --> 01:09:41,920 called not and that basically inverses 1837 01:09:41,920 --> 01:09:44,080 any boolean value we give it if we give 1838 01:09:44,080 --> 01:09:46,319 it a true boolean value it converts it 1839 01:09:46,319 --> 01:09:49,440 to false for example let's make up a new 1840 01:09:49,440 --> 01:09:50,319 rule 1841 01:09:50,319 --> 01:09:52,080 if applicant 1842 01:09:52,080 --> 01:09:54,719 has good credit 1843 01:09:54,719 --> 01:09:56,960 and doesn't have 1844 01:09:56,960 --> 01:09:58,560 a criminal 1845 01:09:58,560 --> 01:09:59,760 record 1846 01:09:59,760 --> 01:10:02,239 then they're eligible for a loan let me 1847 01:10:02,239 --> 01:10:04,400 show you how to implement this so we go 1848 01:10:04,400 --> 01:10:06,000 back to our program 1849 01:10:06,000 --> 01:10:08,080 in this example we don't need the first 1850 01:10:08,080 --> 01:10:10,320 variable so let's delete that let's set 1851 01:10:10,320 --> 01:10:12,159 this variable to true 1852 01:10:12,159 --> 01:10:15,440 we also define another variable like has 1853 01:10:15,440 --> 01:10:16,800 criminal 1854 01:10:16,800 --> 01:10:18,159 record 1855 01:10:18,159 --> 01:10:19,920 and we set this to false 1856 01:10:19,920 --> 01:10:21,360 now we want to check to see if this 1857 01:10:21,360 --> 01:10:24,560 applicant has good credit and not a 1858 01:10:24,560 --> 01:10:27,199 criminal record this is where we use the 1859 01:10:27,199 --> 01:10:30,080 nut operator so 1860 01:10:30,080 --> 01:10:32,239 if they have good credit 1861 01:10:32,239 --> 01:10:33,280 and 1862 01:10:33,280 --> 01:10:34,640 not 1863 01:10:34,640 --> 01:10:36,400 criminal 1864 01:10:36,400 --> 01:10:37,840 record 1865 01:10:37,840 --> 01:10:39,520 so in this example 1866 01:10:39,520 --> 01:10:42,080 has criminal record is set to false when 1867 01:10:42,080 --> 01:10:44,400 we use the not operator this basically 1868 01:10:44,400 --> 01:10:46,719 gets changed to true so we'll have two 1869 01:10:46,719 --> 01:10:50,080 conditions that are true here's one and 1870 01:10:50,080 --> 01:10:52,080 here's another one so our applicant is 1871 01:10:52,080 --> 01:10:53,760 eligible for loan 1872 01:10:53,760 --> 01:10:57,040 and when we run this program we see 1873 01:10:57,040 --> 01:10:59,360 this familiar message 1874 01:10:59,360 --> 01:11:00,400 however 1875 01:11:00,400 --> 01:11:03,199 if an applicant has criminal record so 1876 01:11:03,199 --> 01:11:05,120 let's change this to true 1877 01:11:05,120 --> 01:11:07,199 now when we run this program we can see 1878 01:11:07,199 --> 01:11:09,360 our applicant is not eligible because 1879 01:11:09,360 --> 01:11:11,920 when we apply the not operator on this 1880 01:11:11,920 --> 01:11:13,520 variable 1881 01:11:13,520 --> 01:11:16,719 will get false so true changes to false 1882 01:11:16,719 --> 01:11:19,520 and will end up with two conditions one 1883 01:11:19,520 --> 01:11:21,600 is true and the other is false so the 1884 01:11:21,600 --> 01:11:23,679 result is false and that's why this 1885 01:11:23,679 --> 01:11:25,600 message is not printed 1886 01:11:25,600 --> 01:11:27,199 so this is all about the logical 1887 01:11:27,199 --> 01:11:30,560 operators in python 1888 01:11:33,920 --> 01:11:35,120 in this tutorial i'm going to talk to 1889 01:11:35,120 --> 01:11:37,280 you guys about the comparison operators 1890 01:11:37,280 --> 01:11:40,159 in python we use comparison operators in 1891 01:11:40,159 --> 01:11:42,080 situations where we want to compare a 1892 01:11:42,080 --> 01:11:45,120 variable with a value for example if 1893 01:11:45,120 --> 01:11:48,320 temperature is greater than 30 then we 1894 01:11:48,320 --> 01:11:50,480 want to print it's a hot day 1895 01:11:50,480 --> 01:11:53,360 otherwise if it's less than 10 it's a 1896 01:11:53,360 --> 01:11:54,400 cold day 1897 01:11:54,400 --> 01:11:56,960 otherwise it's neither hot nor cold and 1898 01:11:56,960 --> 01:11:58,560 by the way here i'm talking about 1899 01:11:58,560 --> 01:12:01,520 celsius not fahrenheit so to build these 1900 01:12:01,520 --> 01:12:03,440 rules into our program we need to use 1901 01:12:03,440 --> 01:12:05,360 comparison operators 1902 01:12:05,360 --> 01:12:07,199 back to app.pi 1903 01:12:07,199 --> 01:12:09,120 i'll define this temperature variable 1904 01:12:09,120 --> 01:12:11,280 let's write an if statement if 1905 01:12:11,280 --> 01:12:12,400 temperature 1906 01:12:12,400 --> 01:12:13,920 now we want to check to see if this is 1907 01:12:13,920 --> 01:12:17,280 greater than 30. so we use the greater 1908 01:12:17,280 --> 01:12:20,640 than operator if this is greater than 30 1909 01:12:20,640 --> 01:12:22,400 we want to print 1910 01:12:22,400 --> 01:12:23,840 it's a hot 1911 01:12:23,840 --> 01:12:25,679 day 1912 01:12:25,679 --> 01:12:27,280 otherwise 1913 01:12:27,280 --> 01:12:29,360 let's just print it's 1914 01:12:29,360 --> 01:12:31,679 not a hot day 1915 01:12:31,679 --> 01:12:33,520 now when we run this program we're going 1916 01:12:33,520 --> 01:12:36,560 to see the second message because 30 is 1917 01:12:36,560 --> 01:12:38,719 not greater than 30. so our first 1918 01:12:38,719 --> 01:12:39,760 condition 1919 01:12:39,760 --> 01:12:42,239 evaluates to false 1920 01:12:42,239 --> 01:12:44,719 let's verify that's a run 1921 01:12:44,719 --> 01:12:47,520 it's not a hot day okay 1922 01:12:47,520 --> 01:12:50,080 now if we change the temperature to 35 1923 01:12:50,080 --> 01:12:51,920 and run this again 1924 01:12:51,920 --> 01:12:53,520 we're going to see a different message 1925 01:12:53,520 --> 01:12:55,840 it's a hot day so this is where we use 1926 01:12:55,840 --> 01:12:58,000 comparison operators now what we have 1927 01:12:58,000 --> 01:13:00,320 here as you know is an expression 1928 01:13:00,320 --> 01:13:01,679 because it's a piece of code that 1929 01:13:01,679 --> 01:13:04,080 produces a value so more accurately this 1930 01:13:04,080 --> 01:13:06,000 is a boolean expression 1931 01:13:06,000 --> 01:13:08,640 so this is the greater than operator we 1932 01:13:08,640 --> 01:13:11,760 also have greater than or equal to 1933 01:13:11,760 --> 01:13:13,679 we have less than 1934 01:13:13,679 --> 01:13:16,000 less than or equal to 1935 01:13:16,000 --> 01:13:17,920 here's the equality operator so if the 1936 01:13:17,920 --> 01:13:20,719 temperature equals to 30 then we can say 1937 01:13:20,719 --> 01:13:22,400 it's a hot day note that this is 1938 01:13:22,400 --> 01:13:24,239 different from the assignment operator 1939 01:13:24,239 --> 01:13:26,719 that has only one equal sign you can see 1940 01:13:26,719 --> 01:13:28,400 if we use one equal sign here we 1941 01:13:28,400 --> 01:13:31,040 immediately get this red underline 1942 01:13:31,040 --> 01:13:32,960 because this is simply an assignment 1943 01:13:32,960 --> 01:13:34,960 statement we're changing the value of 1944 01:13:34,960 --> 01:13:36,800 the temperature we're setting the value 1945 01:13:36,800 --> 01:13:38,880 of the temperature variable to 30. we're 1946 01:13:38,880 --> 01:13:40,640 not comparing it with something else so 1947 01:13:40,640 --> 01:13:42,640 we don't have a boolean expression we're 1948 01:13:42,640 --> 01:13:46,880 not producing a boolean value okay so 1949 01:13:46,880 --> 01:13:49,280 our equality operator has two equal 1950 01:13:49,280 --> 01:13:52,560 signs and finally we have not equal 1951 01:13:52,560 --> 01:13:55,040 which is an exclamation followed by an 1952 01:13:55,040 --> 01:13:56,640 equal sign 1953 01:13:56,640 --> 01:13:58,560 now here's an exercise for you you've 1954 01:13:58,560 --> 01:14:00,159 probably seen that when you fill out a 1955 01:14:00,159 --> 01:14:02,480 form online sometimes the input fields 1956 01:14:02,480 --> 01:14:04,880 have validation messages for example 1957 01:14:04,880 --> 01:14:06,320 let's say we have an input field for the 1958 01:14:06,320 --> 01:14:09,040 user to enter their name now if the name 1959 01:14:09,040 --> 01:14:11,040 is less than three characters long we 1960 01:14:11,040 --> 01:14:13,120 want to display a validation error like 1961 01:14:13,120 --> 01:14:15,920 name must be at least 3 characters 1962 01:14:15,920 --> 01:14:18,320 otherwise if the name is more than 50 1963 01:14:18,320 --> 01:14:20,480 characters long then we want to display 1964 01:14:20,480 --> 01:14:22,719 a different validation error like name 1965 01:14:22,719 --> 01:14:25,600 can be a maximum of 50 characters 1966 01:14:25,600 --> 01:14:27,600 otherwise if the name is between 3 and 1967 01:14:27,600 --> 01:14:29,040 50 characters 1968 01:14:29,040 --> 01:14:31,040 then we just want to tell the user that 1969 01:14:31,040 --> 01:14:32,560 name looks good 1970 01:14:32,560 --> 01:14:34,320 so go ahead and write a program to 1971 01:14:34,320 --> 01:14:37,199 implement these rules 1972 01:14:40,239 --> 01:14:41,920 all right let's define a variable called 1973 01:14:41,920 --> 01:14:45,199 name and set it to let's say j so we're 1974 01:14:45,199 --> 01:14:47,199 assuming this is what the user types 1975 01:14:47,199 --> 01:14:48,719 into an input field 1976 01:14:48,719 --> 01:14:50,320 now we want to get the number of 1977 01:14:50,320 --> 01:14:52,880 characters in this string so we use the 1978 01:14:52,880 --> 01:14:54,400 len function right 1979 01:14:54,400 --> 01:14:56,800 lan of name 1980 01:14:56,800 --> 01:14:58,320 when we print this 1981 01:14:58,320 --> 01:15:00,640 we get one right you have seen this 1982 01:15:00,640 --> 01:15:03,040 before now here we want to use an if 1983 01:15:03,040 --> 01:15:07,040 statement so if len of name 1984 01:15:07,040 --> 01:15:09,120 is less than three 1985 01:15:09,120 --> 01:15:10,400 then 1986 01:15:10,400 --> 01:15:12,239 we want to print 1987 01:15:12,239 --> 01:15:14,000 name must be 1988 01:15:14,000 --> 01:15:15,600 at least 1989 01:15:15,600 --> 01:15:18,719 three characters 1990 01:15:18,800 --> 01:15:20,640 like here we need a second condition to 1991 01:15:20,640 --> 01:15:24,719 check the upper limit so l if 1992 01:15:24,719 --> 01:15:26,480 len of name 1993 01:15:26,480 --> 01:15:28,480 is greater than 50 1994 01:15:28,480 --> 01:15:30,640 then we want to print a different 1995 01:15:30,640 --> 01:15:32,800 message name 1996 01:15:32,800 --> 01:15:35,920 must be a maximum 1997 01:15:35,920 --> 01:15:37,199 of 50 1998 01:15:37,199 --> 01:15:39,840 characters 1999 01:15:39,840 --> 01:15:41,040 okay 2000 01:15:41,040 --> 01:15:44,400 and otherwise so else if none of these 2001 01:15:44,400 --> 01:15:47,199 conditions is true that means the name 2002 01:15:47,199 --> 01:15:49,120 looks good so print 2003 01:15:49,120 --> 01:15:53,920 name looks good let's run our program 2004 01:15:53,920 --> 01:15:55,760 so in this case we get this message 2005 01:15:55,760 --> 01:15:58,080 because our name is too short 2006 01:15:58,080 --> 01:15:59,600 if you go back here 2007 01:15:59,600 --> 01:16:00,880 and 2008 01:16:00,880 --> 01:16:03,520 type something really really long 2009 01:16:03,520 --> 01:16:05,760 and then run our program 2010 01:16:05,760 --> 01:16:07,440 we're going to see a different message 2011 01:16:07,440 --> 01:16:10,480 name must be a maximum of 50 characters 2012 01:16:10,480 --> 01:16:11,840 and finally 2013 01:16:11,840 --> 01:16:14,320 if we type a proper name here like john 2014 01:16:14,320 --> 01:16:16,080 smith 2015 01:16:16,080 --> 01:16:18,800 and run our program 2016 01:16:18,800 --> 01:16:22,719 we get name looks good 2017 01:16:26,239 --> 01:16:27,679 here's another good exercise that 2018 01:16:27,679 --> 01:16:29,199 combines many of the materials you have 2019 01:16:29,199 --> 01:16:31,360 learned so far so earlier you built a 2020 01:16:31,360 --> 01:16:33,520 program to convert someone's weight from 2021 01:16:33,520 --> 01:16:35,440 pounds to kilograms now we want to 2022 01:16:35,440 --> 01:16:37,520 extend this program and allow the user 2023 01:16:37,520 --> 01:16:39,360 to enter their weight in either 2024 01:16:39,360 --> 01:16:41,679 kilograms or pounds and then we'll 2025 01:16:41,679 --> 01:16:43,760 convert it to the other unit here's how 2026 01:16:43,760 --> 01:16:46,320 our program is going to work so i enter 2027 01:16:46,320 --> 01:16:49,840 my weight in pounds so 160. 2028 01:16:49,840 --> 01:16:52,000 now it's telling me is this in pounds or 2029 01:16:52,000 --> 01:16:54,719 kilograms so here i need to enter l for 2030 01:16:54,719 --> 01:16:57,840 pounds or k for kilograms and by the way 2031 01:16:57,840 --> 01:17:00,080 this program is not case sensitive so 2032 01:17:00,080 --> 01:17:02,320 whether i enter a capital l or a 2033 01:17:02,320 --> 01:17:05,840 lowercase l it takes it as pounds 2034 01:17:05,840 --> 01:17:09,199 now it tells me you are 72 kilos 2035 01:17:09,199 --> 01:17:11,360 let's run this program one more time 2036 01:17:11,360 --> 01:17:13,120 this time i'm going to enter my weight 2037 01:17:13,120 --> 01:17:15,199 in kilos so 2038 01:17:15,199 --> 01:17:17,360 72 is the weight 2039 01:17:17,360 --> 01:17:20,719 and the unit is kilograms okay 2040 01:17:20,719 --> 01:17:24,480 and it says you are 160 pounds so go 2041 01:17:24,480 --> 01:17:25,920 ahead and spend a few minutes on this 2042 01:17:25,920 --> 01:17:30,920 exercise you will see my solution next 2043 01:17:32,320 --> 01:17:34,400 all right first let's ask the user their 2044 01:17:34,400 --> 01:17:37,600 weight so we use the input function 2045 01:17:37,600 --> 01:17:39,040 weight colon 2046 01:17:39,040 --> 01:17:41,600 we get the return value and store it in 2047 01:17:41,600 --> 01:17:44,800 a variable called weight now the second 2048 01:17:44,800 --> 01:17:46,320 question 2049 01:17:46,320 --> 01:17:47,760 so one more time we use the input 2050 01:17:47,760 --> 01:17:49,440 function 2051 01:17:49,440 --> 01:17:51,840 l for pounds 2052 01:17:51,840 --> 01:17:53,920 lps or 2053 01:17:53,920 --> 01:17:54,719 k 2054 01:17:54,719 --> 01:17:56,080 for kilograms 2055 01:17:56,080 --> 01:17:58,000 so let's get that 2 and store it in a 2056 01:17:58,000 --> 01:18:00,640 variable called unit 2057 01:18:00,640 --> 01:18:03,600 now we need an if statement so if 2058 01:18:03,600 --> 01:18:04,640 unit 2059 01:18:04,640 --> 01:18:06,480 equals l 2060 01:18:06,480 --> 01:18:08,400 then we need to convert this weight into 2061 01:18:08,400 --> 01:18:10,560 kilograms however with this 2062 01:18:10,560 --> 01:18:12,320 implementation we are only allowing the 2063 01:18:12,320 --> 01:18:15,440 user to enter a capital l if they enter 2064 01:18:15,440 --> 01:18:17,440 a lowercase l this code is not going to 2065 01:18:17,440 --> 01:18:18,320 work 2066 01:18:18,320 --> 01:18:20,480 so this is where we use the upper method 2067 01:18:20,480 --> 01:18:22,960 of string objects so this unit is a 2068 01:18:22,960 --> 01:18:25,120 string because as i told you before the 2069 01:18:25,120 --> 01:18:28,239 input function always returns a string 2070 01:18:28,239 --> 01:18:31,280 so we can use the dot operator to access 2071 01:18:31,280 --> 01:18:34,000 all its methods or functions here we 2072 01:18:34,000 --> 01:18:36,640 call the upper method this will convert 2073 01:18:36,640 --> 01:18:38,800 whatever the user enters to uppercase 2074 01:18:38,800 --> 01:18:41,280 and then we'll compare it with a capital 2075 01:18:41,280 --> 01:18:42,080 l 2076 01:18:42,080 --> 01:18:44,560 now if this condition is true 2077 01:18:44,560 --> 01:18:46,320 then we need to get the weight and 2078 01:18:46,320 --> 01:18:50,239 multiply it by 0.45 2079 01:18:50,239 --> 01:18:53,040 however as you know this weight is a 2080 01:18:53,040 --> 01:18:55,280 string object and we cannot multiply a 2081 01:18:55,280 --> 01:18:58,000 string by a floating point number we 2082 01:18:58,000 --> 01:19:00,239 talked about this earlier in this course 2083 01:19:00,239 --> 01:19:02,000 so first we need to convert this weight 2084 01:19:02,000 --> 01:19:03,840 to a numerical value 2085 01:19:03,840 --> 01:19:06,400 so right here when we call the input 2086 01:19:06,400 --> 01:19:08,719 function we can get the return value and 2087 01:19:08,719 --> 01:19:11,520 pass it to the int function 2088 01:19:11,520 --> 01:19:14,400 so we call the in function 2089 01:19:14,400 --> 01:19:15,679 and 2090 01:19:15,679 --> 01:19:18,159 give it the return value of the input 2091 01:19:18,159 --> 01:19:19,280 function 2092 01:19:19,280 --> 01:19:21,440 now the in function will return an 2093 01:19:21,440 --> 01:19:23,679 integer so we can store it in this 2094 01:19:23,679 --> 01:19:25,600 weight variable 2095 01:19:25,600 --> 01:19:27,600 so here's the converted weight let's 2096 01:19:27,600 --> 01:19:31,840 store it in a variable called converted 2097 01:19:31,840 --> 01:19:33,920 then we print 2098 01:19:33,920 --> 01:19:36,400 here we can use a formatted string so 2099 01:19:36,400 --> 01:19:38,880 we prefix the string with f 2100 01:19:38,880 --> 01:19:40,000 u r 2101 01:19:40,000 --> 01:19:41,760 we add curly braces 2102 01:19:41,760 --> 01:19:44,159 to dynamically insert the value of 2103 01:19:44,159 --> 01:19:46,159 converted variable 2104 01:19:46,159 --> 01:19:49,600 and finally we add kilos 2105 01:19:49,600 --> 01:19:50,560 otherwise 2106 01:19:50,560 --> 01:19:52,719 if the unit is kilogram we need to 2107 01:19:52,719 --> 01:19:54,840 divide the weight by 2108 01:19:54,840 --> 01:19:56,480 0.45 2109 01:19:56,480 --> 01:19:58,960 so once again we type converted equals 2110 01:19:58,960 --> 01:19:59,920 to 2111 01:19:59,920 --> 01:20:02,840 weight divided by 2112 01:20:02,840 --> 01:20:05,600 0.45 and just to refresh our memory this 2113 01:20:05,600 --> 01:20:07,679 division operator returns a floating 2114 01:20:07,679 --> 01:20:09,600 point number but if we use double 2115 01:20:09,600 --> 01:20:12,080 slashes we'll get an integer 2116 01:20:12,080 --> 01:20:13,440 in this case i want to get a floating 2117 01:20:13,440 --> 01:20:14,719 point number 2118 01:20:14,719 --> 01:20:15,760 so 2119 01:20:15,760 --> 01:20:17,600 finally let's print 2120 01:20:17,600 --> 01:20:19,760 a formatted string 2121 01:20:19,760 --> 01:20:21,280 ur 2122 01:20:21,280 --> 01:20:22,800 curly braces 2123 01:20:22,800 --> 01:20:24,320 converted 2124 01:20:24,320 --> 01:20:26,320 pounds 2125 01:20:26,320 --> 01:20:27,840 okay 2126 01:20:27,840 --> 01:20:29,760 now let's run this program and see what 2127 01:20:29,760 --> 01:20:31,440 happens 2128 01:20:31,440 --> 01:20:34,320 weight is 160 2129 01:20:34,320 --> 01:20:36,000 in pounds 2130 01:20:36,000 --> 01:20:39,280 and that equals to 72 kilos perfect if 2131 01:20:39,280 --> 01:20:40,960 we run it one more time 2132 01:20:40,960 --> 01:20:43,280 and enter 72 2133 01:20:43,280 --> 01:20:44,560 kilos 2134 01:20:44,560 --> 01:20:48,400 we get 160 pounds 2135 01:20:52,320 --> 01:20:53,600 in this tutorial i'm going to show you 2136 01:20:53,600 --> 01:20:56,159 how to use while loops in python we use 2137 01:20:56,159 --> 01:20:58,400 while loops to execute a block of code 2138 01:20:58,400 --> 01:21:00,480 multiple times and they're often useful 2139 01:21:00,480 --> 01:21:02,800 in building interactive programs and 2140 01:21:02,800 --> 01:21:04,480 games in a future tutorial i'm going to 2141 01:21:04,480 --> 01:21:06,480 show you how to build a simple game 2142 01:21:06,480 --> 01:21:07,920 using a while loop 2143 01:21:07,920 --> 01:21:10,239 so let's get started with the basics 2144 01:21:10,239 --> 01:21:12,639 we write a wild statement and right 2145 01:21:12,639 --> 01:21:15,679 after that we type a condition followed 2146 01:21:15,679 --> 01:21:17,120 by a colon 2147 01:21:17,120 --> 01:21:19,280 as long as this condition is true the 2148 01:21:19,280 --> 01:21:21,280 code that we write in this block will be 2149 01:21:21,280 --> 01:21:24,239 repetitively executed here is an example 2150 01:21:24,239 --> 01:21:26,480 we can define a variable 2151 01:21:26,480 --> 01:21:29,280 like i as in short for index and set it 2152 01:21:29,280 --> 01:21:30,480 to 1. 2153 01:21:30,480 --> 01:21:33,760 now we set our condition to i less than 2154 01:21:33,760 --> 01:21:36,639 or equal to 5. so as long as i is less 2155 01:21:36,639 --> 01:21:40,480 than or equal to 5 we can print i 2156 01:21:40,480 --> 01:21:43,040 on the terminal and then we need to 2157 01:21:43,040 --> 01:21:47,280 increment i by one so we set i to i plus 2158 01:21:47,280 --> 01:21:49,600 one the reason we do this is that if we 2159 01:21:49,600 --> 01:21:52,159 don't do this i will be one forever so 2160 01:21:52,159 --> 01:21:54,239 we'll end up with an infinite loop 2161 01:21:54,239 --> 01:21:56,239 because this condition will always be 2162 01:21:56,239 --> 01:21:59,120 true 1 is always less than 5. so in 2163 01:21:59,120 --> 01:22:00,800 every iteration of this loop we 2164 01:22:00,800 --> 01:22:03,679 increment i by 1 so at some point i is 2165 01:22:03,679 --> 01:22:06,320 going to be 6 and then that is when this 2166 01:22:06,320 --> 01:22:08,239 condition will be false and then we'll 2167 01:22:08,239 --> 01:22:10,400 jump out of this loop okay 2168 01:22:10,400 --> 01:22:12,960 now to demonstrate how everything works 2169 01:22:12,960 --> 01:22:15,280 after this loop i'm going to add a print 2170 01:22:15,280 --> 01:22:16,960 statement 2171 01:22:16,960 --> 01:22:20,400 say done so note that these two lines 2172 01:22:20,400 --> 01:22:22,639 are indented so they are part of the 2173 01:22:22,639 --> 01:22:23,840 while block 2174 01:22:23,840 --> 01:22:25,840 okay now let's go ahead and run this 2175 01:22:25,840 --> 01:22:28,159 program to see what happens 2176 01:22:28,159 --> 01:22:29,199 so 2177 01:22:29,199 --> 01:22:31,679 take a look we get the numbers 1 to 5 2178 01:22:31,679 --> 01:22:32,800 followed by 2179 01:22:32,800 --> 01:22:33,840 done 2180 01:22:33,840 --> 01:22:36,639 so here's how this program gets executed 2181 01:22:36,639 --> 01:22:39,120 first we set i to one now python 2182 01:22:39,120 --> 01:22:41,199 interpreter executes line two this 2183 01:22:41,199 --> 01:22:43,360 condition is true because i is less than 2184 01:22:43,360 --> 01:22:45,199 five so 2185 01:22:45,199 --> 01:22:47,040 i is printed on the terminal and then 2186 01:22:47,040 --> 01:22:49,040 incremented by one 2187 01:22:49,040 --> 01:22:50,880 then the control moves back to the 2188 01:22:50,880 --> 01:22:52,719 beginning of the while loop so it 2189 01:22:52,719 --> 01:22:55,199 doesn't go to the next statement 2190 01:22:55,199 --> 01:22:57,440 so we come back here now we are in the 2191 01:22:57,440 --> 01:23:00,159 second iteration in the second iteration 2192 01:23:00,159 --> 01:23:03,280 i is 2 and because 2 is less than 5 our 2193 01:23:03,280 --> 01:23:05,760 condition is still true so i will be 2194 01:23:05,760 --> 01:23:07,520 printed on a terminal and once again it 2195 01:23:07,520 --> 01:23:10,000 will be incremented by 1. so at some 2196 01:23:10,000 --> 01:23:13,280 point i is going to be 6 and that's when 2197 01:23:13,280 --> 01:23:15,760 this condition will be false so our loop 2198 01:23:15,760 --> 01:23:18,080 will be terminated and then this dom 2199 01:23:18,080 --> 01:23:20,320 message will be printed on the terminal 2200 01:23:20,320 --> 01:23:22,880 so this is the basics of while loops now 2201 01:23:22,880 --> 01:23:24,159 let's make this program a little bit 2202 01:23:24,159 --> 01:23:25,440 more interesting 2203 01:23:25,440 --> 01:23:26,960 here we can write an expression like 2204 01:23:26,960 --> 01:23:27,760 this 2205 01:23:27,760 --> 01:23:29,840 we add a string 2206 01:23:29,840 --> 01:23:32,960 and in this string we add an asterisk 2207 01:23:32,960 --> 01:23:35,920 and then we multiply this string by i 2208 01:23:35,920 --> 01:23:38,560 so with this expression we can repeat a 2209 01:23:38,560 --> 01:23:40,560 string when we multiply a string by a 2210 01:23:40,560 --> 01:23:43,120 number that string will be repeated for 2211 01:23:43,120 --> 01:23:45,360 example if i is 2 2212 01:23:45,360 --> 01:23:47,679 this expression would produce 2213 01:23:47,679 --> 01:23:50,400 a string with two asterisks 2214 01:23:50,400 --> 01:23:52,800 okay now let's run the program and see 2215 01:23:52,800 --> 01:23:55,440 what we get so we get this little 2216 01:23:55,440 --> 01:23:57,760 triangle shape here because in the first 2217 01:23:57,760 --> 01:24:00,239 iteration i is one so 2218 01:24:00,239 --> 01:24:03,280 one times an asterisk produces one 2219 01:24:03,280 --> 01:24:04,400 asterisk 2220 01:24:04,400 --> 01:24:07,280 in the second iteration i is two so when 2221 01:24:07,280 --> 01:24:09,679 we multiply two by one asterisk we'll 2222 01:24:09,679 --> 01:24:13,320 get two asterisks 2223 01:24:16,560 --> 01:24:17,840 in this tutorial i'm going to show you 2224 01:24:17,840 --> 01:24:19,840 how to use a while loop to build a 2225 01:24:19,840 --> 01:24:21,440 guessing game like this 2226 01:24:21,440 --> 01:24:23,199 so we have this secret number which is 2227 01:24:23,199 --> 01:24:25,440 currently set to nine now the computer 2228 01:24:25,440 --> 01:24:27,920 is asking me to make a guess so let's 2229 01:24:27,920 --> 01:24:29,120 say one 2230 01:24:29,120 --> 01:24:30,800 it's not right because the secret number 2231 01:24:30,800 --> 01:24:33,520 is nine i can try again two 2232 01:24:33,520 --> 01:24:35,840 no it's not right let's try again so i 2233 01:24:35,840 --> 01:24:38,080 only have three chances to make a guess 2234 01:24:38,080 --> 01:24:40,159 if i can't guess the number the program 2235 01:24:40,159 --> 01:24:42,080 tells me that i failed let's run the 2236 01:24:42,080 --> 01:24:44,159 program one more time this time i'm 2237 01:24:44,159 --> 01:24:46,239 gonna guess the number so it's nine 2238 01:24:46,239 --> 01:24:48,719 there you go it says you win so let's go 2239 01:24:48,719 --> 01:24:50,239 ahead and build this program using a 2240 01:24:50,239 --> 01:24:52,080 while loop 2241 01:24:52,080 --> 01:24:53,520 all right let's start by defining a 2242 01:24:53,520 --> 01:24:56,239 variable to store our secret number so 2243 01:24:56,239 --> 01:24:59,199 we call that secret underline number and 2244 01:24:59,199 --> 01:25:00,960 set it to 9. 2245 01:25:00,960 --> 01:25:03,040 now we need a while loop to repeatedly 2246 01:25:03,040 --> 01:25:05,120 ask a user to make a guess 2247 01:25:05,120 --> 01:25:06,480 so while 2248 01:25:06,480 --> 01:25:08,639 condition cola 2249 01:25:08,639 --> 01:25:10,320 what is our condition here 2250 01:25:10,320 --> 01:25:12,400 well we want to give our user a maximum 2251 01:25:12,400 --> 01:25:14,560 of three guesses so similar to the last 2252 01:25:14,560 --> 01:25:18,000 tutorial we can define a variable like i 2253 01:25:18,000 --> 01:25:19,679 set it to zero and assume this 2254 01:25:19,679 --> 01:25:21,360 represents the number of guesses the 2255 01:25:21,360 --> 01:25:22,800 user has made 2256 01:25:22,800 --> 01:25:25,600 and then we can write our condition as i 2257 01:25:25,600 --> 01:25:26,560 less than 2258 01:25:26,560 --> 01:25:27,440 3. 2259 01:25:27,440 --> 01:25:29,199 note that here i'm not using less than 2260 01:25:29,199 --> 01:25:31,520 or equal to operator because with this 2261 01:25:31,520 --> 01:25:34,080 condition our loop will be executed four 2262 01:25:34,080 --> 01:25:38,960 times while i is 0 1 2 and 3. 2263 01:25:38,960 --> 01:25:40,800 so here we should use the less than 2264 01:25:40,800 --> 01:25:42,800 operator now if we give this code to 2265 01:25:42,800 --> 01:25:44,800 someone else it's not quite clear what 2266 01:25:44,800 --> 01:25:47,600 does i represent here it's only in r 2267 01:25:47,600 --> 01:25:49,920 head that i represents the number of 2268 01:25:49,920 --> 01:25:52,239 guesses the user has made so as a best 2269 01:25:52,239 --> 01:25:54,480 practice always use meaningful and 2270 01:25:54,480 --> 01:25:56,719 descriptive names for your variables so 2271 01:25:56,719 --> 01:25:58,960 it's better to rename this variable to 2272 01:25:58,960 --> 01:26:00,880 guess count let me show you how to 2273 01:26:00,880 --> 01:26:02,000 rename 2274 01:26:02,000 --> 01:26:04,480 so right click on i variable 2275 01:26:04,480 --> 01:26:07,440 and then go to refactor 2276 01:26:07,440 --> 01:26:10,239 and rename look at the shortcut on a mac 2277 01:26:10,239 --> 01:26:14,320 computer that's shift and f6 2278 01:26:14,320 --> 01:26:16,000 now in this dialog box we can easily 2279 01:26:16,000 --> 01:26:18,320 rename a variable and pycharm will 2280 01:26:18,320 --> 01:26:19,920 update all the references to that 2281 01:26:19,920 --> 01:26:21,600 variable so we don't have to manually 2282 01:26:21,600 --> 01:26:24,320 update each instance okay let's change 2283 01:26:24,320 --> 01:26:26,000 this to 2284 01:26:26,000 --> 01:26:28,719 yes underline count 2285 01:26:28,719 --> 01:26:32,080 enter there you go now that is better 2286 01:26:32,080 --> 01:26:34,239 also it's better to store three in a 2287 01:26:34,239 --> 01:26:36,080 separate variable to make our code more 2288 01:26:36,080 --> 01:26:38,080 readable because it's not quite clear 2289 01:26:38,080 --> 01:26:41,360 what does three represent here so 2290 01:26:41,360 --> 01:26:43,520 let's define a variable called guess 2291 01:26:43,520 --> 01:26:45,600 limit and set it to three 2292 01:26:45,600 --> 01:26:48,639 and then we can change three to guess 2293 01:26:48,639 --> 01:26:51,600 underline limit now our code is more 2294 01:26:51,600 --> 01:26:54,400 readable while guess count is less than 2295 01:26:54,400 --> 01:26:56,560 guess limit see it reads like a story 2296 01:26:56,560 --> 01:26:58,800 this is how you should write code 2297 01:26:58,800 --> 01:26:59,840 all right 2298 01:26:59,840 --> 01:27:01,520 so while this condition is true you want 2299 01:27:01,520 --> 01:27:03,920 to ask the user to make a guess so here 2300 01:27:03,920 --> 01:27:06,639 we use our input function 2301 01:27:06,639 --> 01:27:07,679 guess 2302 01:27:07,679 --> 01:27:09,679 now whatever the user enters comes out 2303 01:27:09,679 --> 01:27:11,360 as a string so we need to convert it to 2304 01:27:11,360 --> 01:27:14,400 an integer so right here we pass the 2305 01:27:14,400 --> 01:27:16,800 result to the end function 2306 01:27:16,800 --> 01:27:18,239 and then get it and store it in a 2307 01:27:18,239 --> 01:27:20,960 separate variable called guess 2308 01:27:20,960 --> 01:27:23,360 so at this point the user made a guess 2309 01:27:23,360 --> 01:27:27,199 now we need to increment guess count so 2310 01:27:27,199 --> 01:27:29,040 guess count 2311 01:27:29,040 --> 01:27:33,280 we set it to plus equal one okay now we 2312 01:27:33,280 --> 01:27:35,199 need to check to see if the user made 2313 01:27:35,199 --> 01:27:37,280 the right guess so here we need an if 2314 01:27:37,280 --> 01:27:38,400 statement 2315 01:27:38,400 --> 01:27:39,360 if 2316 01:27:39,360 --> 01:27:40,960 what the user guessed 2317 01:27:40,960 --> 01:27:42,080 equals 2318 01:27:42,080 --> 01:27:45,280 our secret number again see our code is 2319 01:27:45,280 --> 01:27:47,520 so readable it's like a story you can 2320 01:27:47,520 --> 01:27:49,520 read it like plain english 2321 01:27:49,520 --> 01:27:51,840 so if this condition is true you want to 2322 01:27:51,840 --> 01:27:55,280 tell the user they won so print 2323 01:27:55,280 --> 01:27:57,040 you one 2324 01:27:57,040 --> 01:27:58,719 now let's go ahead and run our program 2325 01:27:58,719 --> 01:28:01,440 up to this point so 2326 01:28:01,440 --> 01:28:03,520 okay it's asking me to make a guess i'm 2327 01:28:03,520 --> 01:28:05,360 gonna make the wrong guess of one it 2328 01:28:05,360 --> 01:28:08,000 asks me again beautiful two one more 2329 01:28:08,000 --> 01:28:09,360 time three 2330 01:28:09,360 --> 01:28:10,639 okay what is missing in this 2331 01:28:10,639 --> 01:28:12,639 implementation is the message that tells 2332 01:28:12,639 --> 01:28:14,000 me that i failed we're going to take 2333 01:28:14,000 --> 01:28:16,320 care of it momentarily but let's run the 2334 01:28:16,320 --> 01:28:18,480 program one more time and make the right 2335 01:28:18,480 --> 01:28:21,040 guess so nine 2336 01:28:21,040 --> 01:28:23,440 okay it says you won but it's still 2337 01:28:23,440 --> 01:28:25,440 asking me to make a guess because our 2338 01:28:25,440 --> 01:28:27,280 while loop is going to get executed 2339 01:28:27,280 --> 01:28:30,239 three times look one and two 2340 01:28:30,239 --> 01:28:32,400 so we need to change our program such 2341 01:28:32,400 --> 01:28:34,880 that if the user makes the right guess 2342 01:28:34,880 --> 01:28:36,800 we need to terminate our while loop we 2343 01:28:36,800 --> 01:28:38,560 need to jump out of it 2344 01:28:38,560 --> 01:28:40,400 how do we do that 2345 01:28:40,400 --> 01:28:42,960 so over here if the user makes the right 2346 01:28:42,960 --> 01:28:43,760 guess 2347 01:28:43,760 --> 01:28:46,159 after we print this message we can use 2348 01:28:46,159 --> 01:28:49,520 the break statement to terminate a loop 2349 01:28:49,520 --> 01:28:51,600 when python interpreter sees this it's 2350 01:28:51,600 --> 01:28:53,440 going to immediately terminate our loop 2351 01:28:53,440 --> 01:28:55,199 it's not going to evaluate this 2352 01:28:55,199 --> 01:28:56,400 condition again 2353 01:28:56,400 --> 01:28:57,920 now let's run our program and see what 2354 01:28:57,920 --> 01:28:58,880 happens 2355 01:28:58,880 --> 01:28:59,760 so 2356 01:28:59,760 --> 01:29:02,159 i'm going to guess the right number 2357 01:29:02,159 --> 01:29:04,480 you want and look we were not asked to 2358 01:29:04,480 --> 01:29:06,800 make two more guesses beautiful 2359 01:29:06,800 --> 01:29:08,159 now the last thing that we need to add 2360 01:29:08,159 --> 01:29:10,320 here is the message that tells the user 2361 01:29:10,320 --> 01:29:12,000 that they failed if they couldn't guess 2362 01:29:12,000 --> 01:29:13,520 the right number 2363 01:29:13,520 --> 01:29:16,800 how do we do that well in python our 2364 01:29:16,800 --> 01:29:19,760 while loops can optionally have an else 2365 01:29:19,760 --> 01:29:22,239 part similar to the if statements so 2366 01:29:22,239 --> 01:29:23,760 earlier you learned that our if 2367 01:29:23,760 --> 01:29:25,920 statements can optionally have an else 2368 01:29:25,920 --> 01:29:26,719 part 2369 01:29:26,719 --> 01:29:27,840 like here 2370 01:29:27,840 --> 01:29:29,199 so 2371 01:29:29,199 --> 01:29:31,920 if this condition is true do this 2372 01:29:31,920 --> 01:29:34,320 otherwise do something else right in 2373 01:29:34,320 --> 01:29:36,239 this case our if statement doesn't have 2374 01:29:36,239 --> 01:29:37,679 an else part 2375 01:29:37,679 --> 01:29:38,480 now 2376 01:29:38,480 --> 01:29:40,960 similar to the if statements our while 2377 01:29:40,960 --> 01:29:43,440 loops our while statements can also have 2378 01:29:43,440 --> 01:29:44,880 an else part 2379 01:29:44,880 --> 01:29:45,760 so 2380 01:29:45,760 --> 01:29:47,679 right at this level 2381 01:29:47,679 --> 01:29:52,000 we can add an else block so else colon 2382 01:29:52,000 --> 01:29:54,000 and the code that we write here will get 2383 01:29:54,000 --> 01:29:57,679 executed if this while loop completes 2384 01:29:57,679 --> 01:30:01,440 successfully without an immediate break 2385 01:30:01,440 --> 01:30:03,920 in other words if the user guesses the 2386 01:30:03,920 --> 01:30:06,480 right number we break this loop we jump 2387 01:30:06,480 --> 01:30:09,280 out of it so the code that we write in 2388 01:30:09,280 --> 01:30:12,000 the else block will not get executed but 2389 01:30:12,000 --> 01:30:14,560 if the user cannot guess this number 2390 01:30:14,560 --> 01:30:16,000 we're never going to break out of this 2391 01:30:16,000 --> 01:30:18,080 loop so this load will be executed to 2392 01:30:18,080 --> 01:30:21,760 completion until this condition becomes 2393 01:30:21,760 --> 01:30:24,080 false in that case the code that we 2394 01:30:24,080 --> 01:30:25,840 write in the else block will get 2395 01:30:25,840 --> 01:30:27,440 executed and this is the perfect 2396 01:30:27,440 --> 01:30:29,360 opportunity for us to tell the user hey 2397 01:30:29,360 --> 01:30:31,040 you made three guesses but none of them 2398 01:30:31,040 --> 01:30:32,880 were right so 2399 01:30:32,880 --> 01:30:35,760 print sorry 2400 01:30:35,760 --> 01:30:38,000 you failed 2401 01:30:38,000 --> 01:30:41,120 now let's test our program one more time 2402 01:30:41,120 --> 01:30:44,159 so guess one two three 2403 01:30:44,159 --> 01:30:46,320 sorry you failed let's run it one more 2404 01:30:46,320 --> 01:30:48,880 time this time i'm going to make a wrong 2405 01:30:48,880 --> 01:30:49,840 guess 2406 01:30:49,840 --> 01:30:52,239 and then the right guess 2407 01:30:52,239 --> 01:30:54,800 we won and our loop terminated 2408 01:30:54,800 --> 01:30:57,440 immediately 2409 01:31:01,040 --> 01:31:02,239 all right now it's time for you to 2410 01:31:02,239 --> 01:31:04,320 practice what you have learned so far so 2411 01:31:04,320 --> 01:31:06,159 once again we're going to build a game 2412 01:31:06,159 --> 01:31:08,239 this game is a simulation for a card 2413 01:31:08,239 --> 01:31:10,000 game now our game doesn't have a 2414 01:31:10,000 --> 01:31:12,159 graphical user interface or gui and it 2415 01:31:12,159 --> 01:31:14,080 doesn't really matter for now our focus 2416 01:31:14,080 --> 01:31:16,639 is entirely on building the engine for 2417 01:31:16,639 --> 01:31:17,760 this game 2418 01:31:17,760 --> 01:31:20,400 so let's see how this program works when 2419 01:31:20,400 --> 01:31:22,400 we run this we get this little symbol 2420 01:31:22,400 --> 01:31:23,199 here 2421 01:31:23,199 --> 01:31:25,440 and our program is waiting for us to 2422 01:31:25,440 --> 01:31:27,040 enter a command 2423 01:31:27,040 --> 01:31:29,440 if we type help either in lowercase or 2424 01:31:29,440 --> 01:31:31,520 uppercase it doesn't matter we get the 2425 01:31:31,520 --> 01:31:33,760 list of commands that our program or our 2426 01:31:33,760 --> 01:31:35,920 game currently supports so we can type 2427 01:31:35,920 --> 01:31:39,360 the start command to start our car we 2428 01:31:39,360 --> 01:31:41,520 can type the stop command to stop our 2429 01:31:41,520 --> 01:31:44,480 car and quit to terminate the game any 2430 01:31:44,480 --> 01:31:46,239 other commands that we type our program 2431 01:31:46,239 --> 01:31:47,440 is going to tell us hey i don't 2432 01:31:47,440 --> 01:31:49,520 understand that for example if i type 2433 01:31:49,520 --> 01:31:50,880 asd here 2434 01:31:50,880 --> 01:31:53,040 it says i don't understand that if we 2435 01:31:53,040 --> 01:31:54,800 type start 2436 01:31:54,800 --> 01:31:57,440 we get this message car started ready to 2437 01:31:57,440 --> 01:31:59,920 go if you type stop 2438 01:31:59,920 --> 01:32:02,400 it says car stopped and finally if we 2439 01:32:02,400 --> 01:32:04,320 type quit 2440 01:32:04,320 --> 01:32:06,239 our program terminates this is a 2441 01:32:06,239 --> 01:32:08,159 fantastic exercise for you to practice 2442 01:32:08,159 --> 01:32:09,840 what you have learned so pause the video 2443 01:32:09,840 --> 01:32:12,159 and spend five to ten minutes to build 2444 01:32:12,159 --> 01:32:14,800 this program 2445 01:32:17,360 --> 01:32:18,400 all right we're going to start with a 2446 01:32:18,400 --> 01:32:19,679 while loop 2447 01:32:19,679 --> 01:32:21,040 with a condition 2448 01:32:21,040 --> 01:32:22,800 what is our condition here 2449 01:32:22,800 --> 01:32:25,199 we want to run this loop until the user 2450 01:32:25,199 --> 01:32:28,000 types quit so we can define a variable 2451 01:32:28,000 --> 01:32:30,000 for storing the command that the user 2452 01:32:30,000 --> 01:32:32,159 enters and then we can run this loop as 2453 01:32:32,159 --> 01:32:34,159 long as the command does not equal to 2454 01:32:34,159 --> 01:32:35,040 quit 2455 01:32:35,040 --> 01:32:37,040 so right before the loop we define a 2456 01:32:37,040 --> 01:32:39,920 variable command and initially we set it 2457 01:32:39,920 --> 01:32:42,639 to an empty string an empty string is a 2458 01:32:42,639 --> 01:32:44,719 string that has no characters in it we 2459 01:32:44,719 --> 01:32:46,960 only have the quotes okay 2460 01:32:46,960 --> 01:32:47,920 so 2461 01:32:47,920 --> 01:32:50,560 then we type out our condition as 2462 01:32:50,560 --> 01:32:53,440 while command does not equal to 2463 01:32:53,440 --> 01:32:55,520 quit then 2464 01:32:55,520 --> 01:32:56,719 do something 2465 01:32:56,719 --> 01:32:58,639 now immediately we have a problem here 2466 01:32:58,639 --> 01:33:00,400 because we're assuming that the user 2467 01:33:00,400 --> 01:33:02,400 types the command in lower case so if 2468 01:33:02,400 --> 01:33:04,480 they type this in uppercase our program 2469 01:33:04,480 --> 01:33:06,639 is not going to behave properly so to 2470 01:33:06,639 --> 01:33:09,199 fix this problem we need to call the 2471 01:33:09,199 --> 01:33:11,199 lower method of the string object and 2472 01:33:11,199 --> 01:33:13,360 then compare the result 2473 01:33:13,360 --> 01:33:16,560 with this quit we could also call upper 2474 01:33:16,560 --> 01:33:19,280 and then type quit in uppercase it's a 2475 01:33:19,280 --> 01:33:21,199 matter of personal preference in this 2476 01:33:21,199 --> 01:33:23,040 demo i'm going to use lowercase 2477 01:33:23,040 --> 01:33:25,440 characters so okay 2478 01:33:25,440 --> 01:33:27,679 now in this loop we need to ask the user 2479 01:33:27,679 --> 01:33:29,520 to enter a command so once again we're 2480 01:33:29,520 --> 01:33:31,920 going to use our input function 2481 01:33:31,920 --> 01:33:34,560 we add a greater than symbol 2482 01:33:34,560 --> 01:33:36,800 followed by a space whatever the user 2483 01:33:36,800 --> 01:33:40,080 enters we get it and store it in our 2484 01:33:40,080 --> 01:33:42,960 command variable 2485 01:33:42,960 --> 01:33:44,960 now apart from the quit command there 2486 01:33:44,960 --> 01:33:46,320 are three other commands that we need to 2487 01:33:46,320 --> 01:33:50,239 support start stop and help so here we 2488 01:33:50,239 --> 01:33:52,800 need an if statement to compare what the 2489 01:33:52,800 --> 01:33:54,880 user enters with one of the supported 2490 01:33:54,880 --> 01:33:56,000 commands 2491 01:33:56,000 --> 01:33:56,800 so 2492 01:33:56,800 --> 01:33:58,560 if command 2493 01:33:58,560 --> 01:33:59,920 that lower 2494 01:33:59,920 --> 01:34:02,400 equals start 2495 01:34:02,400 --> 01:34:04,639 then you want to print a message like 2496 01:34:04,639 --> 01:34:07,679 the car started so print 2497 01:34:07,679 --> 01:34:09,920 car started 2498 01:34:09,920 --> 01:34:12,320 ready to go it doesn't matter 2499 01:34:12,320 --> 01:34:14,400 now the second condition what if it's 2500 01:34:14,400 --> 01:34:16,840 not start maybe it's stop so 2501 01:34:16,840 --> 01:34:20,239 alif command that lower 2502 01:34:20,239 --> 01:34:21,280 equals 2503 01:34:21,280 --> 01:34:23,760 stop there you go then we print a 2504 01:34:23,760 --> 01:34:26,960 different message car stopped 2505 01:34:26,960 --> 01:34:29,280 now look at our code we have repeated 2506 01:34:29,280 --> 01:34:32,159 this lower lower lower multiple times 2507 01:34:32,159 --> 01:34:34,080 this is bad in programming we have a 2508 01:34:34,080 --> 01:34:37,199 term called dry which is short for don't 2509 01:34:37,199 --> 01:34:38,800 repeat yourself 2510 01:34:38,800 --> 01:34:40,239 so whenever you have duplicating your 2511 01:34:40,239 --> 01:34:41,600 code that means you are doing something 2512 01:34:41,600 --> 01:34:44,239 wrong so how can we solve this problem 2513 01:34:44,239 --> 01:34:47,119 well instead of calling the lower method 2514 01:34:47,119 --> 01:34:49,440 in each condition we can call it right 2515 01:34:49,440 --> 01:34:51,920 here when we get the input from the user 2516 01:34:51,920 --> 01:34:54,080 so this input function as you know 2517 01:34:54,080 --> 01:34:57,199 returns a string we can immediately call 2518 01:34:57,199 --> 01:34:59,520 the lower method on that string and with 2519 01:34:59,520 --> 01:35:02,239 this our command will always be in lower 2520 01:35:02,239 --> 01:35:04,320 case so we don't need to 2521 01:35:04,320 --> 01:35:07,440 call this method in every condition 2522 01:35:07,440 --> 01:35:09,119 look 2523 01:35:09,119 --> 01:35:11,840 we remove the duplication and also our 2524 01:35:11,840 --> 01:35:14,080 conditions are shorter and easier to 2525 01:35:14,080 --> 01:35:16,159 read there's also one more place we need 2526 01:35:16,159 --> 01:35:19,119 to modify so it's right here 2527 01:35:19,119 --> 01:35:20,719 that is better 2528 01:35:20,719 --> 01:35:23,199 now the third command we need one more 2529 01:35:23,199 --> 01:35:24,719 alif 2530 01:35:24,719 --> 01:35:27,520 if the command equals help 2531 01:35:27,520 --> 01:35:29,119 then 2532 01:35:29,119 --> 01:35:30,880 we want to show the commands that we 2533 01:35:30,880 --> 01:35:33,679 support so here we're going to print a 2534 01:35:33,679 --> 01:35:35,520 multi-line string 2535 01:35:35,520 --> 01:35:37,840 so we use triple quotes 2536 01:35:37,840 --> 01:35:39,280 like this 2537 01:35:39,280 --> 01:35:42,320 and give the user a guideline like this 2538 01:35:42,320 --> 01:35:44,560 so start 2539 01:35:44,560 --> 01:35:47,119 to start the car 2540 01:35:47,119 --> 01:35:48,320 stop 2541 01:35:48,320 --> 01:35:51,360 to stop the car 2542 01:35:51,520 --> 01:35:53,040 and quit 2543 01:35:53,040 --> 01:35:54,960 to quit 2544 01:35:54,960 --> 01:35:57,840 now finally we need an else part so if 2545 01:35:57,840 --> 01:35:59,679 what the user enters is none of these 2546 01:35:59,679 --> 01:36:01,280 commands we're going to tell them hey we 2547 01:36:01,280 --> 01:36:02,800 don't understand this 2548 01:36:02,800 --> 01:36:04,719 so 2549 01:36:04,719 --> 01:36:07,040 else colon 2550 01:36:07,040 --> 01:36:08,400 print 2551 01:36:08,400 --> 01:36:12,960 sorry i don't understand that 2552 01:36:12,960 --> 01:36:14,320 and by the way 2553 01:36:14,320 --> 01:36:16,000 note that here because i'm using double 2554 01:36:16,000 --> 01:36:18,400 quotes i can easily use a single quote 2555 01:36:18,400 --> 01:36:20,800 as an apostrophe okay 2556 01:36:20,800 --> 01:36:21,679 so 2557 01:36:21,679 --> 01:36:23,280 let's run our program up to this point 2558 01:36:23,280 --> 01:36:25,440 and see what happens 2559 01:36:25,440 --> 01:36:28,239 all right let's type start car started 2560 01:36:28,239 --> 01:36:30,239 beautiful stop 2561 01:36:30,239 --> 01:36:31,760 car stopped 2562 01:36:31,760 --> 01:36:33,199 help 2563 01:36:33,199 --> 01:36:34,880 we get this guideline but there's so 2564 01:36:34,880 --> 01:36:38,000 much indentation before our commands 2565 01:36:38,000 --> 01:36:40,159 we'll fix that in a second and finally 2566 01:36:40,159 --> 01:36:43,040 let's test the quit command 2567 01:36:43,040 --> 01:36:45,679 oops our program didn't work properly 2568 01:36:45,679 --> 01:36:48,000 here's the reason with this if statement 2569 01:36:48,000 --> 01:36:50,880 we're comparing the command with start 2570 01:36:50,880 --> 01:36:54,159 stop and help anything else will end up 2571 01:36:54,159 --> 01:36:56,639 here so that's why our program says it 2572 01:36:56,639 --> 01:36:58,239 doesn't understand that 2573 01:36:58,239 --> 01:37:00,400 so that's why our program says it 2574 01:37:00,400 --> 01:37:03,040 doesn't understand that command however 2575 01:37:03,040 --> 01:37:05,280 after this else statement the control 2576 01:37:05,280 --> 01:37:07,199 will be moved to the beginning of the 2577 01:37:07,199 --> 01:37:09,920 loop at this point our command is quit 2578 01:37:09,920 --> 01:37:12,159 so our loop will complete and the 2579 01:37:12,159 --> 01:37:14,560 program terminates in other words when 2580 01:37:14,560 --> 01:37:16,080 we run this program 2581 01:37:16,080 --> 01:37:18,080 and type quit 2582 01:37:18,080 --> 01:37:20,080 our program actually quits but we still 2583 01:37:20,080 --> 01:37:22,080 see this message which shouldn't appear 2584 01:37:22,080 --> 01:37:24,560 here how can we solve this problem well 2585 01:37:24,560 --> 01:37:26,719 we can come back here 2586 01:37:26,719 --> 01:37:30,159 and just before the else block add 2587 01:37:30,159 --> 01:37:31,760 another alif 2588 01:37:31,760 --> 01:37:34,480 something like this alif command 2589 01:37:34,480 --> 01:37:35,840 equals 2590 01:37:35,840 --> 01:37:37,679 quit 2591 01:37:37,679 --> 01:37:40,320 then we can immediately break 2592 01:37:40,320 --> 01:37:42,400 this will solve our problem but note 2593 01:37:42,400 --> 01:37:44,719 that we have kind of repeated this 2594 01:37:44,719 --> 01:37:47,280 expression in two places 2595 01:37:47,280 --> 01:37:48,880 the reality is we don't really need this 2596 01:37:48,880 --> 01:37:51,119 condition on the top because with this 2597 01:37:51,119 --> 01:37:53,520 if statements well more accurately with 2598 01:37:53,520 --> 01:37:55,760 this alif we can jump out of this loop 2599 01:37:55,760 --> 01:37:58,239 and terminate our program so we can 2600 01:37:58,239 --> 01:38:00,080 simplify our condition 2601 01:38:00,080 --> 01:38:02,960 to something like this true 2602 01:38:02,960 --> 01:38:05,199 so while true means this block of code 2603 01:38:05,199 --> 01:38:08,080 is going to get executed repeatedly 2604 01:38:08,080 --> 01:38:12,480 until we explicitly break out of it okay 2605 01:38:12,480 --> 01:38:15,600 now let's test our program one more time 2606 01:38:15,600 --> 01:38:16,560 so 2607 01:38:16,560 --> 01:38:17,920 quit 2608 01:38:17,920 --> 01:38:19,520 now our program terminates and we don't 2609 01:38:19,520 --> 01:38:21,440 see that message beautiful 2610 01:38:21,440 --> 01:38:23,679 so let's fix the last problem you saw 2611 01:38:23,679 --> 01:38:25,679 that when we typed help 2612 01:38:25,679 --> 01:38:27,840 these guidelines appeared with so much 2613 01:38:27,840 --> 01:38:30,480 indentation and here's the reason look 2614 01:38:30,480 --> 01:38:32,320 right here in our code they're already 2615 01:38:32,320 --> 01:38:35,199 indented so when we use triple quotes 2616 01:38:35,199 --> 01:38:37,040 what we type here will be printed 2617 01:38:37,040 --> 01:38:38,800 exactly as is 2618 01:38:38,800 --> 01:38:41,199 so because we have indentation here this 2619 01:38:41,199 --> 01:38:43,679 indentation will also be printed on the 2620 01:38:43,679 --> 01:38:46,480 terminal so let's delete 2621 01:38:46,480 --> 01:38:47,840 these 2622 01:38:47,840 --> 01:38:50,320 okay run the program one more time 2623 01:38:50,320 --> 01:38:51,840 type help 2624 01:38:51,840 --> 01:38:54,159 the annotation is gone beautiful now 2625 01:38:54,159 --> 01:38:56,400 here's a challenge for you i want you to 2626 01:38:56,400 --> 01:38:58,880 take this program to the next level so 2627 01:38:58,880 --> 01:39:01,360 right now if we type start we get this 2628 01:39:01,360 --> 01:39:04,000 message car started and if we type start 2629 01:39:04,000 --> 01:39:05,040 again 2630 01:39:05,040 --> 01:39:07,280 we get the same message it would be 2631 01:39:07,280 --> 01:39:09,520 better if we got a message like car is 2632 01:39:09,520 --> 01:39:11,600 already started so it doesn't make sense 2633 01:39:11,600 --> 01:39:14,560 to store the car twice similarly 2634 01:39:14,560 --> 01:39:16,159 if we type stop 2635 01:39:16,159 --> 01:39:18,960 it says car stopped if we type it again 2636 01:39:18,960 --> 01:39:20,880 we get the exact same message it doesn't 2637 01:39:20,880 --> 01:39:23,600 make sense to stop a car twice so here's 2638 01:39:23,600 --> 01:39:25,520 what i need you to do if the car is 2639 01:39:25,520 --> 01:39:27,600 stopped and the user tries to stop it 2640 01:39:27,600 --> 01:39:29,600 again the program should say hey the car 2641 01:39:29,600 --> 01:39:31,760 is already stopped what are you doing 2642 01:39:31,760 --> 01:39:33,440 and similarly if the car is already 2643 01:39:33,440 --> 01:39:35,440 started and the user tries to start it 2644 01:39:35,440 --> 01:39:37,119 again the program should yell at the 2645 01:39:37,119 --> 01:39:39,600 user so go ahead and make the necessary 2646 01:39:39,600 --> 01:39:43,360 changes to implement this scenario 2647 01:39:45,920 --> 01:39:47,840 all right to add this to our program we 2648 01:39:47,840 --> 01:39:49,679 need to know if the car is started or 2649 01:39:49,679 --> 01:39:51,199 not so there is one more piece of 2650 01:39:51,199 --> 01:39:53,040 information that we need to store in the 2651 01:39:53,040 --> 01:39:55,199 memory what is the kind of data we need 2652 01:39:55,199 --> 01:39:58,080 to store here a boolean is the car 2653 01:39:58,080 --> 01:40:00,639 started or not it's a matter of yes or 2654 01:40:00,639 --> 01:40:04,000 no true or false so on the top 2655 01:40:04,000 --> 01:40:06,320 here we can define another variable like 2656 01:40:06,320 --> 01:40:09,119 started and initially we set it to false 2657 01:40:09,119 --> 01:40:11,840 so the car is not started right 2658 01:40:11,840 --> 01:40:12,719 now 2659 01:40:12,719 --> 01:40:15,360 when the user types the start command 2660 01:40:15,360 --> 01:40:17,199 here we need to check to see if the car 2661 01:40:17,199 --> 01:40:20,159 is already started if not we'll start it 2662 01:40:20,159 --> 01:40:22,880 otherwise we'll yell at the user so in 2663 01:40:22,880 --> 01:40:26,159 this block we write another if statement 2664 01:40:26,159 --> 01:40:28,800 if it's already started 2665 01:40:28,800 --> 01:40:30,400 then we print 2666 01:40:30,400 --> 01:40:34,080 car is already started 2667 01:40:34,080 --> 01:40:36,800 otherwise so we add an else statement 2668 01:40:36,800 --> 01:40:38,880 here 2669 01:40:38,880 --> 01:40:43,360 and at this point we set started to true 2670 01:40:43,360 --> 01:40:45,520 so we start the car and print this 2671 01:40:45,520 --> 01:40:47,119 message okay 2672 01:40:47,119 --> 01:40:49,679 now we need to make a similar change 2673 01:40:49,679 --> 01:40:52,239 for the stop command so if the car is 2674 01:40:52,239 --> 01:40:53,679 already stopped 2675 01:40:53,679 --> 01:40:56,000 we need to print a different message 2676 01:40:56,000 --> 01:40:56,960 if 2677 01:40:56,960 --> 01:40:59,600 not started so here we're using the not 2678 01:40:59,600 --> 01:41:02,480 operator to see if the car is stopped 2679 01:41:02,480 --> 01:41:04,480 so if it's not started that means it's 2680 01:41:04,480 --> 01:41:05,920 stopped okay 2681 01:41:05,920 --> 01:41:08,880 so if it stopped your print 2682 01:41:08,880 --> 01:41:11,760 car is already stopped 2683 01:41:11,760 --> 01:41:14,080 with double p's 2684 01:41:14,080 --> 01:41:16,800 otherwise so else 2685 01:41:16,800 --> 01:41:18,480 we need to stop the car how do we do 2686 01:41:18,480 --> 01:41:22,560 that we set started to false and then 2687 01:41:22,560 --> 01:41:24,560 we'll print this message 2688 01:41:24,560 --> 01:41:27,440 as easy as that let's go ahead and run 2689 01:41:27,440 --> 01:41:28,800 our program 2690 01:41:28,800 --> 01:41:29,600 so 2691 01:41:29,600 --> 01:41:31,440 initially our car is stopped so i'm 2692 01:41:31,440 --> 01:41:33,760 going to type stop 2693 01:41:33,760 --> 01:41:36,159 it says the car is already stopped so 2694 01:41:36,159 --> 01:41:38,480 let's start it 2695 01:41:38,480 --> 01:41:40,639 okay now our car is started let's start 2696 01:41:40,639 --> 01:41:42,000 it one more time 2697 01:41:42,000 --> 01:41:43,840 the program is yelling at us so we 2698 01:41:43,840 --> 01:41:46,400 cannot start a car twice beautiful now 2699 01:41:46,400 --> 01:41:47,840 let's stop it 2700 01:41:47,840 --> 01:41:49,920 it says the car is stopped let's stop it 2701 01:41:49,920 --> 01:41:54,320 one more time we get this message again 2702 01:41:57,920 --> 01:41:59,280 in this tutorial i'm going to talk to 2703 01:41:59,280 --> 01:42:01,840 you guys about for loops in python in 2704 01:42:01,840 --> 01:42:03,199 the last tutorial you learned about 2705 01:42:03,199 --> 01:42:04,960 while loops you'll learn that we use 2706 01:42:04,960 --> 01:42:07,520 while loops to execute a block of code 2707 01:42:07,520 --> 01:42:10,159 multiple times in python we have another 2708 01:42:10,159 --> 01:42:12,239 kind of loop that is a for loop and we 2709 01:42:12,239 --> 01:42:14,480 use that to iterate over items of a 2710 01:42:14,480 --> 01:42:17,040 collection such as a string because a 2711 01:42:17,040 --> 01:42:19,600 string is a sequence of characters so it 2712 01:42:19,600 --> 01:42:21,600 looks like a collection so we can use a 2713 01:42:21,600 --> 01:42:24,080 for loop to iterate over each character 2714 01:42:24,080 --> 01:42:25,920 in a string and then do something with 2715 01:42:25,920 --> 01:42:29,440 it here's an example we type out four 2716 01:42:29,440 --> 01:42:31,440 then we define a variable this is what 2717 01:42:31,440 --> 01:42:33,840 we call a loop variable in each 2718 01:42:33,840 --> 01:42:36,400 iteration this variable will hold one 2719 01:42:36,400 --> 01:42:37,360 item 2720 01:42:37,360 --> 01:42:39,840 so let's call it item 2721 01:42:39,840 --> 01:42:43,040 in here we type out a string like python 2722 01:42:43,040 --> 01:42:44,800 and then colon 2723 01:42:44,800 --> 01:42:46,639 so with this for loop we can iterate 2724 01:42:46,639 --> 01:42:49,520 over this string and in each iteration 2725 01:42:49,520 --> 01:42:51,840 this item variable will hold one 2726 01:42:51,840 --> 01:42:53,520 character at a time in the first 2727 01:42:53,520 --> 01:42:56,080 iteration it will be set to p then in 2728 01:42:56,080 --> 01:42:58,400 the second iteration it will be set to y 2729 01:42:58,400 --> 01:43:00,159 in the third iteration it will be set to 2730 01:43:00,159 --> 01:43:02,000 t and so on okay 2731 01:43:02,000 --> 01:43:04,239 so here we are inside our four block 2732 01:43:04,239 --> 01:43:06,000 because of the indentation here so 2733 01:43:06,000 --> 01:43:08,159 whatever we type here will be executed 2734 01:43:08,159 --> 01:43:10,719 in each iteration for now we can simply 2735 01:43:10,719 --> 01:43:12,719 print this item 2736 01:43:12,719 --> 01:43:14,880 now let's run this program and see what 2737 01:43:14,880 --> 01:43:16,800 happens 2738 01:43:16,800 --> 01:43:18,880 so you can see each character in our 2739 01:43:18,880 --> 01:43:21,280 string is printed on a new line 2740 01:43:21,280 --> 01:43:23,119 let's look at another example 2741 01:43:23,119 --> 01:43:25,280 in python we can define lists using 2742 01:43:25,280 --> 01:43:27,920 square brackets so let me remove this 2743 01:43:27,920 --> 01:43:29,600 string from here 2744 01:43:29,600 --> 01:43:32,320 and define a list using square brackets 2745 01:43:32,320 --> 01:43:34,480 a list is simply a list of items like a 2746 01:43:34,480 --> 01:43:36,560 list of numbers a list of customers a 2747 01:43:36,560 --> 01:43:39,119 list of emails products blog posts 2748 01:43:39,119 --> 01:43:41,520 whatever so here we can define a list of 2749 01:43:41,520 --> 01:43:43,840 names like mosh 2750 01:43:43,840 --> 01:43:45,199 john 2751 01:43:45,199 --> 01:43:46,639 sarah 2752 01:43:46,639 --> 01:43:49,760 and then go ahead run our program so you 2753 01:43:49,760 --> 01:43:51,920 can see in each iteration we get one 2754 01:43:51,920 --> 01:43:52,719 name 2755 01:43:52,719 --> 01:43:55,760 and print it on a new line 2756 01:43:55,760 --> 01:43:58,159 we can also loop over a list of numbers 2757 01:43:58,159 --> 01:43:58,880 for example 2758 01:43:58,880 --> 01:44:00,159 [Music] 2759 01:44:00,159 --> 01:44:03,360 one two three four let's run it 2760 01:44:03,360 --> 01:44:05,600 again we see each number on a new line 2761 01:44:05,600 --> 01:44:07,679 but what if we want to iterate over a 2762 01:44:07,679 --> 01:44:09,920 large list of numbers we don't want to 2763 01:44:09,920 --> 01:44:12,960 explicitly type out a list with let's 2764 01:44:12,960 --> 01:44:14,800 say a hundred or a thousand numbers but 2765 01:44:14,800 --> 01:44:17,600 i want to type 567 all the way to 100 2766 01:44:17,600 --> 01:44:20,400 that is when we use the range function 2767 01:44:20,400 --> 01:44:23,679 so let me delete this 2768 01:44:24,239 --> 01:44:25,920 in python we have a built-in function 2769 01:44:25,920 --> 01:44:28,480 called range for creating a range of 2770 01:44:28,480 --> 01:44:30,239 numbers so 2771 01:44:30,239 --> 01:44:33,119 we give it a number let's say 10. let's 2772 01:44:33,119 --> 01:44:34,960 run this program 2773 01:44:34,960 --> 01:44:36,880 now we can see here on the terminal we 2774 01:44:36,880 --> 01:44:39,040 have numbers 0 2775 01:44:39,040 --> 01:44:40,400 all the way 2776 01:44:40,400 --> 01:44:45,040 to 9. so 10 is not included so basically 2777 01:44:45,040 --> 01:44:47,040 when we call the range function this 2778 01:44:47,040 --> 01:44:49,040 range function creates an object it's 2779 01:44:49,040 --> 01:44:51,119 not a list it's a special kind of object 2780 01:44:51,119 --> 01:44:53,280 that we can iterate over in each 2781 01:44:53,280 --> 01:44:55,360 iteration this object will spit out a 2782 01:44:55,360 --> 01:44:57,760 new number we can also work with a range 2783 01:44:57,760 --> 01:44:59,520 of numbers here let's say we want to 2784 01:44:59,520 --> 01:45:01,520 start from 5 2785 01:45:01,520 --> 01:45:03,920 and go all the way to 10. 2786 01:45:03,920 --> 01:45:04,960 so 2787 01:45:04,960 --> 01:45:06,639 let's run our program 2788 01:45:06,639 --> 01:45:10,719 now we have the numbers 5 6 7 and 9. 2789 01:45:10,719 --> 01:45:13,440 also this range function can optionally 2790 01:45:13,440 --> 01:45:17,280 take a step so we can pass 2791 01:45:17,280 --> 01:45:18,159 to 2792 01:45:18,159 --> 01:45:20,159 as the step to this function 2793 01:45:20,159 --> 01:45:22,880 and when we run our program 2794 01:45:22,880 --> 01:45:24,239 you can see that our first number is 2795 01:45:24,239 --> 01:45:27,600 five now we go two steps forward to get 2796 01:45:27,600 --> 01:45:30,480 seven once again we go two steps forward 2797 01:45:30,480 --> 01:45:32,719 we get nine and that is the end of our 2798 01:45:32,719 --> 01:45:35,199 range so this is the basics of using for 2799 01:45:35,199 --> 01:45:37,119 loops in python 2800 01:45:37,119 --> 01:45:38,960 now here's an exercise for you i want 2801 01:45:38,960 --> 01:45:41,040 you to write a program to calculate the 2802 01:45:41,040 --> 01:45:43,199 total cost of all the items in a 2803 01:45:43,199 --> 01:45:45,920 shopping cart so let's say we have a 2804 01:45:45,920 --> 01:45:47,360 list of prices 2805 01:45:47,360 --> 01:45:48,639 like 10 2806 01:45:48,639 --> 01:45:50,639 20 and 30. 2807 01:45:50,639 --> 01:45:52,800 i wanted to use a for loop to calculate 2808 01:45:52,800 --> 01:45:55,280 the total cost of all the items in our 2809 01:45:55,280 --> 01:45:57,520 imaginary shopping cart so calculate 2810 01:45:57,520 --> 01:45:59,679 that and then print it on a terminal 2811 01:45:59,679 --> 01:46:01,199 that's pretty easy and you should do it 2812 01:46:01,199 --> 01:46:04,080 in a couple of minutes 2813 01:46:06,560 --> 01:46:08,639 so as you learned we use for loops to 2814 01:46:08,639 --> 01:46:10,800 iterate over all the items in a 2815 01:46:10,800 --> 01:46:13,280 collection a collection can be a string 2816 01:46:13,280 --> 01:46:15,360 it can be a list it can be a range 2817 01:46:15,360 --> 01:46:17,199 object that is returned from the range 2818 01:46:17,199 --> 01:46:19,440 function anything any kind of object 2819 01:46:19,440 --> 01:46:21,520 that has multiple items 2820 01:46:21,520 --> 01:46:23,119 so in this example we're going to use a 2821 01:46:23,119 --> 01:46:25,199 for loop to iterate over the list of 2822 01:46:25,199 --> 01:46:28,639 prices so for item in 2823 01:46:28,639 --> 01:46:31,600 prices colon and by the way this loop 2824 01:46:31,600 --> 01:46:33,840 variable we don't have to call this item 2825 01:46:33,840 --> 01:46:35,840 we can call it anything for example in 2826 01:46:35,840 --> 01:46:39,360 this case we can rename it to price so 2827 01:46:39,360 --> 01:46:40,560 for price 2828 01:46:40,560 --> 01:46:42,239 in prices 2829 01:46:42,239 --> 01:46:44,560 now in each iteration this price will 2830 01:46:44,560 --> 01:46:46,560 hold one value in the first iteration 2831 01:46:46,560 --> 01:46:48,400 it's going to hold 10 then it's going to 2832 01:46:48,400 --> 01:46:50,880 be 20 and then it's going to be 30. 2833 01:46:50,880 --> 01:46:53,440 so we need to define another variable to 2834 01:46:53,440 --> 01:46:55,119 calculate the total 2835 01:46:55,119 --> 01:46:56,320 so 2836 01:46:56,320 --> 01:46:59,360 we define that outside of a for loop 2837 01:46:59,360 --> 01:47:02,080 let's call it total and initially we set 2838 01:47:02,080 --> 01:47:03,440 it to zero 2839 01:47:03,440 --> 01:47:05,840 now in each iteration we get the current 2840 01:47:05,840 --> 01:47:08,719 price and add it to the total so 2841 01:47:08,719 --> 01:47:13,119 rate total equals total plus price or as 2842 01:47:13,119 --> 01:47:15,119 you learned earlier we can use the 2843 01:47:15,119 --> 01:47:18,560 augmented assignment operator to 2844 01:47:18,560 --> 01:47:20,480 simplify this code 2845 01:47:20,480 --> 01:47:21,360 so 2846 01:47:21,360 --> 01:47:23,679 after our for loop this total variable 2847 01:47:23,679 --> 01:47:26,320 has the total of all the prices we can 2848 01:47:26,320 --> 01:47:28,639 simply print it here 2849 01:47:28,639 --> 01:47:31,600 or we can use a formatted string so 2850 01:47:31,600 --> 01:47:34,080 a string prefix with f 2851 01:47:34,080 --> 01:47:37,040 we add a label like total curly braces 2852 01:47:37,040 --> 01:47:39,119 to dynamically include some value in our 2853 01:47:39,119 --> 01:47:42,159 string in this case our total 2854 01:47:42,159 --> 01:47:43,199 variable 2855 01:47:43,199 --> 01:47:46,239 so let's go ahead and run this program 2856 01:47:46,239 --> 01:47:48,320 there you go so the total cost of all 2857 01:47:48,320 --> 01:47:50,560 the items in our imaginary shopping cart 2858 01:47:50,560 --> 01:47:53,440 is 60. 2859 01:47:57,040 --> 01:47:58,400 in this tutorial i'm going to talk to 2860 01:47:58,400 --> 01:48:01,040 you guys about nested loops in python 2861 01:48:01,040 --> 01:48:03,040 using a nested loop basically means 2862 01:48:03,040 --> 01:48:05,520 adding one loop inside of another loop 2863 01:48:05,520 --> 01:48:06,960 and with this technique we can do some 2864 01:48:06,960 --> 01:48:09,360 amazing things for example we can easily 2865 01:48:09,360 --> 01:48:12,080 generate a list of coordinates so a 2866 01:48:12,080 --> 01:48:14,080 coordinate as you know is a combination 2867 01:48:14,080 --> 01:48:19,119 of an x and y value let's say 0 and 0. 2868 01:48:19,119 --> 01:48:20,880 now let's say we want to generate a list 2869 01:48:20,880 --> 01:48:23,199 of coordinates like this so we have 0 2870 01:48:23,199 --> 01:48:26,560 and 0 then we'll have 0 and 1 then 0 and 2871 01:48:26,560 --> 01:48:27,440 2 2872 01:48:27,440 --> 01:48:28,480 next 2873 01:48:28,480 --> 01:48:30,320 we're going to change x so we're going 2874 01:48:30,320 --> 01:48:32,880 to use 1 for the x and once again we're 2875 01:48:32,880 --> 01:48:35,840 going to use these three values for the 2876 01:48:35,840 --> 01:48:39,199 y coordinates so 1 and 0 then 2877 01:48:39,199 --> 01:48:42,480 1 and 1 and 1 and 2. 2878 01:48:42,480 --> 01:48:44,800 you got the point we can easily generate 2879 01:48:44,800 --> 01:48:47,360 these coordinates using nested loops let 2880 01:48:47,360 --> 01:48:50,400 me show you so we'll start with one loop 2881 01:48:50,400 --> 01:48:54,639 let's say for x in range four 2882 01:48:54,639 --> 01:48:56,320 with this loop we can generate values 2883 01:48:56,320 --> 01:48:58,800 for the x coordinate let's print this on 2884 01:48:58,800 --> 01:49:01,040 the terminal 2885 01:49:01,040 --> 01:49:02,000 okay 2886 01:49:02,000 --> 01:49:05,520 so we get the values 0 to 3. 2887 01:49:05,520 --> 01:49:08,880 now for each x like 0 we should generate 2888 01:49:08,880 --> 01:49:10,560 a few y values 2889 01:49:10,560 --> 01:49:13,679 so that is where we use a nested loop so 2890 01:49:13,679 --> 01:49:16,320 inside of this loop we're going to add 2891 01:49:16,320 --> 01:49:18,320 another loop so instead of just printing 2892 01:49:18,320 --> 01:49:20,560 x first we want to add 2893 01:49:20,560 --> 01:49:22,480 another loop 2894 01:49:22,480 --> 01:49:26,239 for y in range let's say 3 2895 01:49:26,239 --> 01:49:28,880 now we can print x and y together 2896 01:49:28,880 --> 01:49:32,800 so print here we use a formatted string 2897 01:49:32,800 --> 01:49:34,880 to display coordinates like this so we 2898 01:49:34,880 --> 01:49:36,400 add parenthesis 2899 01:49:36,400 --> 01:49:38,159 inside of this parenthesis first we need 2900 01:49:38,159 --> 01:49:41,440 to add x so curly braces x 2901 01:49:41,440 --> 01:49:42,880 then a comma 2902 01:49:42,880 --> 01:49:44,080 followed by 2903 01:49:44,080 --> 01:49:47,760 another set of curly braces and 2904 01:49:47,760 --> 01:49:49,520 y 2905 01:49:49,520 --> 01:49:51,040 let's run this program and see what we 2906 01:49:51,040 --> 01:49:53,280 get 2907 01:49:53,360 --> 01:49:55,760 there you go so we have these 2908 01:49:55,760 --> 01:49:59,520 coordinates 0 and 0 0 and 1 0 and 2. 2909 01:49:59,520 --> 01:50:02,159 then we have 1 and 0 1 and 1 1 and 2 and 2910 01:50:02,159 --> 01:50:04,639 so on so let me explain exactly how this 2911 01:50:04,639 --> 01:50:06,560 program gets executed 2912 01:50:06,560 --> 01:50:08,880 so in the first iteration of our outer 2913 01:50:08,880 --> 01:50:11,599 loop x is zero right 2914 01:50:11,599 --> 01:50:14,560 now we are on line two here we have a 2915 01:50:14,560 --> 01:50:16,960 new loop which we call an inner loop in 2916 01:50:16,960 --> 01:50:20,320 this inner loop in the first iteration y 2917 01:50:20,320 --> 01:50:21,920 is going to be zero 2918 01:50:21,920 --> 01:50:25,280 so we print 0 and 0 on the terminal now 2919 01:50:25,280 --> 01:50:27,920 the control goes back to line 2 or our 2920 01:50:27,920 --> 01:50:30,800 inner loop in this second iteration y 2921 01:50:30,800 --> 01:50:33,119 will be set to 1 but we're still in the 2922 01:50:33,119 --> 01:50:34,719 first iteration 2923 01:50:34,719 --> 01:50:37,679 of our outer loop so x is still 0 but 2924 01:50:37,679 --> 01:50:40,719 now y is incremented to one 2925 01:50:40,719 --> 01:50:43,440 so that is why we see zero and one on 2926 01:50:43,440 --> 01:50:45,599 the terminal once again the control goes 2927 01:50:45,599 --> 01:50:47,760 back to line two we are in the third 2928 01:50:47,760 --> 01:50:50,080 iteration of our inner loop so this will 2929 01:50:50,080 --> 01:50:53,440 continue until this inner loop completes 2930 01:50:53,440 --> 01:50:56,080 that is when y reaches two because this 2931 01:50:56,080 --> 01:50:58,719 range function generates number zero to 2932 01:50:58,719 --> 01:51:00,639 three but not including three so we'll 2933 01:51:00,639 --> 01:51:02,800 have zero one and two 2934 01:51:02,800 --> 01:51:05,119 after this inner loop completes then the 2935 01:51:05,119 --> 01:51:07,520 control goes back to line one at this 2936 01:51:07,520 --> 01:51:09,199 point we're going to be in the second 2937 01:51:09,199 --> 01:51:12,000 iteration of our outer loop so x will be 2938 01:51:12,000 --> 01:51:14,639 one then the control will be moved to 2939 01:51:14,639 --> 01:51:17,360 line two or our inner loop at this point 2940 01:51:17,360 --> 01:51:19,199 this range function is going to generate 2941 01:51:19,199 --> 01:51:22,320 the numbers 0 to 3 one more time so this 2942 01:51:22,320 --> 01:51:24,560 inner loop will be executed three times 2943 01:51:24,560 --> 01:51:26,080 and then we'll go back 2944 01:51:26,080 --> 01:51:28,719 to our outer loop so this is how nested 2945 01:51:28,719 --> 01:51:31,840 loops get executed 2946 01:51:33,520 --> 01:51:35,440 okay here's an exercise for you but this 2947 01:51:35,440 --> 01:51:37,040 one is a little bit more challenging 2948 01:51:37,040 --> 01:51:39,440 than the exercises you have done so far 2949 01:51:39,440 --> 01:51:40,960 so i really don't expect you to do it 2950 01:51:40,960 --> 01:51:43,360 but if you do it wow i will be so proud 2951 01:51:43,360 --> 01:51:44,239 of you 2952 01:51:44,239 --> 01:51:46,159 so see what i have done here 2953 01:51:46,159 --> 01:51:48,400 using nested loops i've written some 2954 01:51:48,400 --> 01:51:51,280 code to draw this f shape can you see 2955 01:51:51,280 --> 01:51:52,080 that 2956 01:51:52,080 --> 01:51:54,000 so let me give you a hint 2957 01:51:54,000 --> 01:51:55,440 first of all 2958 01:51:55,440 --> 01:51:56,960 we have this list 2959 01:51:56,960 --> 01:51:58,239 called numbers 2960 01:51:58,239 --> 01:52:01,280 in this list we have these values 5 2 2961 01:52:01,280 --> 01:52:03,119 five two two 2962 01:52:03,119 --> 01:52:05,840 these values determine the number of x's 2963 01:52:05,840 --> 01:52:09,040 we have on each line so for example the 2964 01:52:09,040 --> 01:52:11,119 first item in this list this tells us 2965 01:52:11,119 --> 01:52:13,360 that we should have five x's on the 2966 01:52:13,360 --> 01:52:16,400 first line there you go so one two three 2967 01:52:16,400 --> 01:52:17,599 four five 2968 01:52:17,599 --> 01:52:19,760 on the second line we're gonna have two 2969 01:52:19,760 --> 01:52:20,719 excess 2970 01:52:20,719 --> 01:52:22,639 on the third line we're gonna have five 2971 01:52:22,639 --> 01:52:24,639 excess like this 2972 01:52:24,639 --> 01:52:27,280 so i've written code to convert 2973 01:52:27,280 --> 01:52:29,360 a simple list of numbers into a shape 2974 01:52:29,360 --> 01:52:30,400 like this 2975 01:52:30,400 --> 01:52:32,000 now here's a tip for you 2976 01:52:32,000 --> 01:52:34,400 using a for loop you need to iterate 2977 01:52:34,400 --> 01:52:37,360 over this list in each iteration you get 2978 01:52:37,360 --> 01:52:39,840 one number this determines the number of 2979 01:52:39,840 --> 01:52:41,679 excess to be displayed on that 2980 01:52:41,679 --> 01:52:44,000 particular line so if you want to cheat 2981 01:52:44,000 --> 01:52:46,400 you can get this number and multiply it 2982 01:52:46,400 --> 01:52:48,960 by a string that contains x so if you 2983 01:52:48,960 --> 01:52:51,679 multiply x by five you'll get five x's 2984 01:52:51,679 --> 01:52:53,920 that's not what i want you to do i want 2985 01:52:53,920 --> 01:52:56,159 you to use an inner loop here to 2986 01:52:56,159 --> 01:52:59,760 generate a string that contains five x's 2987 01:52:59,760 --> 01:53:02,000 so imagine in python we cannot multiply 2988 01:53:02,000 --> 01:53:04,639 a string by a number so to solve this 2989 01:53:04,639 --> 01:53:06,800 problem we need a nested loop 2990 01:53:06,800 --> 01:53:08,719 so go ahead and spend five minutes on 2991 01:53:08,719 --> 01:53:10,560 this exercise and by the way do your 2992 01:53:10,560 --> 01:53:12,560 best to solve this it is a little bit 2993 01:53:12,560 --> 01:53:14,480 challenging but it's not extremely 2994 01:53:14,480 --> 01:53:16,320 difficult it just requires a little bit 2995 01:53:16,320 --> 01:53:20,480 of focus you'll see my solution next 2996 01:53:22,880 --> 01:53:24,400 all right so first we need to iterate 2997 01:53:24,400 --> 01:53:27,280 over all the items in this list 2998 01:53:27,280 --> 01:53:30,400 so for item in numbers 2999 01:53:30,400 --> 01:53:32,880 or we could rename this variable to 3000 01:53:32,880 --> 01:53:35,440 x underline count that is the number of 3001 01:53:35,440 --> 01:53:38,159 x's on each line okay 3002 01:53:38,159 --> 01:53:39,599 now i told you that if you want to cheat 3003 01:53:39,599 --> 01:53:43,040 you can write code like this print x 3004 01:53:43,040 --> 01:53:46,239 times x underline count if we run this 3005 01:53:46,239 --> 01:53:47,440 program 3006 01:53:47,440 --> 01:53:49,760 we get the same output 3007 01:53:49,760 --> 01:53:51,599 so this is the beauty of python with 3008 01:53:51,599 --> 01:53:53,280 python we can write expressions like 3009 01:53:53,280 --> 01:53:55,760 this we can multiply a string by a 3010 01:53:55,760 --> 01:53:57,840 number to repeat it a lot of other 3011 01:53:57,840 --> 01:53:59,520 programming languages don't support this 3012 01:53:59,520 --> 01:54:01,760 feature but for this exercise i wanted 3013 01:54:01,760 --> 01:54:03,199 you to imagine that we don't have this 3014 01:54:03,199 --> 01:54:04,960 feature in python so you would have to 3015 01:54:04,960 --> 01:54:07,840 use an inner loop to solve this problem 3016 01:54:07,840 --> 01:54:09,280 here's how it works 3017 01:54:09,280 --> 01:54:11,840 in the first iteration xcount is going 3018 01:54:11,840 --> 01:54:14,800 to be 5. so we need to generate five x's 3019 01:54:14,800 --> 01:54:17,199 how can we do that well let's say we 3020 01:54:17,199 --> 01:54:19,040 define a variable called output and 3021 01:54:19,040 --> 01:54:21,440 initially we set it to an empty string 3022 01:54:21,440 --> 01:54:23,440 now we need to add five x's to the 3023 01:54:23,440 --> 01:54:26,080 string so we can use another loop for 3024 01:54:26,080 --> 01:54:27,040 count 3025 01:54:27,040 --> 01:54:28,639 in range 3026 01:54:28,639 --> 01:54:31,520 of x underline count so we're using the 3027 01:54:31,520 --> 01:54:33,840 range function to generate a sequence of 3028 01:54:33,840 --> 01:54:35,440 numbers from 0 3029 01:54:35,440 --> 01:54:37,360 up to x count 3030 01:54:37,360 --> 01:54:39,360 so in our first iteration x count is 3031 01:54:39,360 --> 01:54:41,280 going to be five so range of five would 3032 01:54:41,280 --> 01:54:43,760 generate the numbers zero one two three 3033 01:54:43,760 --> 01:54:47,040 four so this inner loop will be executed 3034 01:54:47,040 --> 01:54:51,440 five times that is exactly what xcount 3035 01:54:51,440 --> 01:54:52,880 represents 3036 01:54:52,880 --> 01:54:54,239 so now 3037 01:54:54,239 --> 01:54:56,159 in each iteration we simply need to 3038 01:54:56,159 --> 01:55:00,159 append an x to our output variable so we 3039 01:55:00,159 --> 01:55:01,679 said output 3040 01:55:01,679 --> 01:55:04,560 plus equals x 3041 01:55:04,560 --> 01:55:05,599 and then 3042 01:55:05,599 --> 01:55:08,560 after this inner loop we simply print 3043 01:55:08,560 --> 01:55:10,080 the output 3044 01:55:10,080 --> 01:55:12,480 with this we'll print five excess on the 3045 01:55:12,480 --> 01:55:14,480 first row then we go to the second 3046 01:55:14,480 --> 01:55:16,639 iteration of our outer loop at this 3047 01:55:16,639 --> 01:55:18,880 point xcount is going to be 2. 3048 01:55:18,880 --> 01:55:20,080 now 3049 01:55:20,080 --> 01:55:22,719 on line 3 we're going to reset our 3050 01:55:22,719 --> 01:55:25,199 output variable to an empty string so we 3051 01:55:25,199 --> 01:55:26,320 start over 3052 01:55:26,320 --> 01:55:28,639 then we go to our inner loop this loop 3053 01:55:28,639 --> 01:55:30,639 will be executed two times so we'll 3054 01:55:30,639 --> 01:55:33,280 append two access to the output variable 3055 01:55:33,280 --> 01:55:35,199 and then print it 3056 01:55:35,199 --> 01:55:36,960 as simple as that so see it wasn't 3057 01:55:36,960 --> 01:55:38,320 really that difficult but it was 3058 01:55:38,320 --> 01:55:40,000 slightly more difficult than the 3059 01:55:40,000 --> 01:55:41,520 previous exercises 3060 01:55:41,520 --> 01:55:44,719 so let's run this program there you go 3061 01:55:44,719 --> 01:55:46,400 now if you're adventurous i want you to 3062 01:55:46,400 --> 01:55:49,280 modify the values that we have 3063 01:55:49,280 --> 01:55:50,320 in our 3064 01:55:50,320 --> 01:55:52,639 numbers list 3065 01:55:52,639 --> 01:55:57,080 to print an l here 3066 01:56:01,040 --> 01:56:02,400 now in this tutorial we're going to take 3067 01:56:02,400 --> 01:56:04,400 a closer look at lists 3068 01:56:04,400 --> 01:56:07,280 so i'm going to define a list of names 3069 01:56:07,280 --> 01:56:09,599 let's send them to john 3070 01:56:09,599 --> 01:56:11,040 bob 3071 01:56:11,040 --> 01:56:12,960 marsh 3072 01:56:12,960 --> 01:56:16,000 sarah and mary 3073 01:56:16,000 --> 01:56:17,840 so if we print 3074 01:56:17,840 --> 01:56:19,679 this list here 3075 01:56:19,679 --> 01:56:21,199 what we see on the terminal looks 3076 01:56:21,199 --> 01:56:23,840 exactly like how we define our list so 3077 01:56:23,840 --> 01:56:26,159 we have square brackets and in between 3078 01:56:26,159 --> 01:56:28,880 these square brackets we have our items 3079 01:56:28,880 --> 01:56:32,159 so we have five strings in this list 3080 01:56:32,159 --> 01:56:34,320 now we can also access an individual 3081 01:56:34,320 --> 01:56:36,560 element using an index just like how we 3082 01:56:36,560 --> 01:56:38,880 can access an individual character in a 3083 01:56:38,880 --> 01:56:40,840 string using an index 3084 01:56:40,840 --> 01:56:44,320 so here we type out square brackets and 3085 01:56:44,320 --> 01:56:46,960 specify an index the index of the first 3086 01:56:46,960 --> 01:56:49,520 item in this list is zero 3087 01:56:49,520 --> 01:56:51,760 so let's run this program there you go 3088 01:56:51,760 --> 01:56:54,320 we get john now if you want to print out 3089 01:56:54,320 --> 01:56:57,040 the third element in this list its index 3090 01:56:57,040 --> 01:56:58,159 is two 3091 01:56:58,159 --> 01:56:59,119 so 3092 01:56:59,119 --> 01:57:01,040 names of two 3093 01:57:01,040 --> 01:57:03,040 returns mosh 3094 01:57:03,040 --> 01:57:05,119 now we can also pass a negative index 3095 01:57:05,119 --> 01:57:08,080 here so negative one refers to the last 3096 01:57:08,080 --> 01:57:10,639 item in this list that is mary let's run 3097 01:57:10,639 --> 01:57:12,159 the program 3098 01:57:12,159 --> 01:57:14,960 there you go we see mary if you pass 3099 01:57:14,960 --> 01:57:17,520 negative 2 this returns the second item 3100 01:57:17,520 --> 01:57:19,360 from the end of the list 3101 01:57:19,360 --> 01:57:21,520 so let's run the program there you go we 3102 01:57:21,520 --> 01:57:24,080 get 0. so this is exactly like accessing 3103 01:57:24,080 --> 01:57:26,400 individual characters in a string we can 3104 01:57:26,400 --> 01:57:28,000 also 3105 01:57:28,000 --> 01:57:31,360 use a colon to select a range of items 3106 01:57:31,360 --> 01:57:33,280 for example if we pass 3107 01:57:33,280 --> 01:57:36,400 to colon this will get all the items 3108 01:57:36,400 --> 01:57:39,040 starting from the index of two that is 3109 01:57:39,040 --> 01:57:41,760 mosh here all the way to the end of the 3110 01:57:41,760 --> 01:57:45,840 string so let's run this program 3111 01:57:45,840 --> 01:57:46,880 there you go 3112 01:57:46,880 --> 01:57:49,520 we get this list with three items mosh 3113 01:57:49,520 --> 01:57:51,199 sarah and mary 3114 01:57:51,199 --> 01:57:54,000 we can also specify an end index let's 3115 01:57:54,000 --> 01:57:57,199 say 4 so this will return all the items 3116 01:57:57,199 --> 01:57:59,119 up to this index but it doesn't include 3117 01:57:59,119 --> 01:58:01,360 the item at this index so when we run 3118 01:58:01,360 --> 01:58:04,800 this program we only see mosh and sarah 3119 01:58:04,800 --> 01:58:07,040 the item at index four which is the 3120 01:58:07,040 --> 01:58:09,119 fifth element or the fifth item in this 3121 01:58:09,119 --> 01:58:12,239 list is not returned so mary is not 3122 01:58:12,239 --> 01:58:14,560 returned okay and also here we have 3123 01:58:14,560 --> 01:58:17,360 default values so if we leave out the 3124 01:58:17,360 --> 01:58:19,520 end index this expression is going to 3125 01:58:19,520 --> 01:58:22,239 return all the items starting from the 3126 01:58:22,239 --> 01:58:24,960 index of two to the end of the list or 3127 01:58:24,960 --> 01:58:27,360 if we leave out the start index this 3128 01:58:27,360 --> 01:58:29,760 expression assumes zero as the default 3129 01:58:29,760 --> 01:58:31,920 index so it will return all the items 3130 01:58:31,920 --> 01:58:33,520 from the beginning to the end of the 3131 01:58:33,520 --> 01:58:36,239 list okay and by the way just like 3132 01:58:36,239 --> 01:58:38,880 strings these square brackets here don't 3133 01:58:38,880 --> 01:58:41,360 modify our original list they simply 3134 01:58:41,360 --> 01:58:44,639 return a new list for example if we pass 3135 01:58:44,639 --> 01:58:46,320 two here 3136 01:58:46,320 --> 01:58:48,400 you can see this returns a new list with 3137 01:58:48,400 --> 01:58:51,119 three items so if we go back here and 3138 01:58:51,119 --> 01:58:53,840 print our original list of names right 3139 01:58:53,840 --> 01:58:54,800 after 3140 01:58:54,800 --> 01:58:56,960 you can see it's not affected so 3141 01:58:56,960 --> 01:58:58,880 whenever we use square brackets with a 3142 01:58:58,880 --> 01:59:01,520 colon to select a range of items we get 3143 01:59:01,520 --> 01:59:03,840 a new list and by the way we can also 3144 01:59:03,840 --> 01:59:06,239 modify any of the elements in this list 3145 01:59:06,239 --> 01:59:08,239 for example let's say we made a mistake 3146 01:59:08,239 --> 01:59:11,040 and the first item shouldn't be spelled 3147 01:59:11,040 --> 01:59:13,119 john with an h so we want to remove the 3148 01:59:13,119 --> 01:59:16,320 h that is very easy so we access it 3149 01:59:16,320 --> 01:59:19,520 using an index that is names of zero and 3150 01:59:19,520 --> 01:59:21,199 we set it to a new value 3151 01:59:21,199 --> 01:59:22,320 like this 3152 01:59:22,320 --> 01:59:25,520 now let's print our list 3153 01:59:25,520 --> 01:59:26,960 so you can see 3154 01:59:26,960 --> 01:59:28,639 the first item in this list is now 3155 01:59:28,639 --> 01:59:31,760 updated so this is the basics of lists 3156 01:59:31,760 --> 01:59:33,599 and here's an exercise for you i want 3157 01:59:33,599 --> 01:59:35,520 you to write a program to find the 3158 01:59:35,520 --> 01:59:37,599 largest number in the list this is a 3159 01:59:37,599 --> 01:59:39,760 fantastic exercise for beginners so go 3160 01:59:39,760 --> 01:59:41,360 ahead and spend a few minutes on this 3161 01:59:41,360 --> 01:59:45,320 and then come back continue watching 3162 01:59:47,599 --> 01:59:50,159 all right let's define a list of numbers 3163 01:59:50,159 --> 01:59:51,360 numbers 3164 01:59:51,360 --> 01:59:55,440 with a bunch of random numbers 3 6 2 8 4 3165 01:59:55,440 --> 01:59:56,719 and 10. 3166 01:59:56,719 --> 01:59:58,239 now to find the largest number in this 3167 01:59:58,239 --> 02:00:01,360 list we need to define another variable 3168 02:00:01,360 --> 02:00:03,520 let's call it max this variable will 3169 02:00:03,520 --> 02:00:06,239 hold the largest number now initially we 3170 02:00:06,239 --> 02:00:08,320 want to assume that the first item in 3171 02:00:08,320 --> 02:00:09,360 this list 3172 02:00:09,360 --> 02:00:12,320 is the largest number so we set max to 3173 02:00:12,320 --> 02:00:13,599 numbers 3174 02:00:13,599 --> 02:00:15,920 of zero we're only assuming that the 3175 02:00:15,920 --> 02:00:18,159 first item is the largest number chances 3176 02:00:18,159 --> 02:00:20,159 are our assumption is wrong so we need 3177 02:00:20,159 --> 02:00:22,480 to iterate over this list we need to 3178 02:00:22,480 --> 02:00:23,679 loop through it 3179 02:00:23,679 --> 02:00:26,400 get each item and compare it with max 3180 02:00:26,400 --> 02:00:28,719 if it's greater than max then we need to 3181 02:00:28,719 --> 02:00:31,280 reset max to that number so in the first 3182 02:00:31,280 --> 02:00:35,199 iteration we get 3 and max is also 3. 3183 02:00:35,199 --> 02:00:38,560 is 3 greater than 3 no so we move on we 3184 02:00:38,560 --> 02:00:41,280 get the second number is 6 greater than 3185 02:00:41,280 --> 02:00:45,360 3 it is so we need to reset max to 6. 3186 02:00:45,360 --> 02:00:48,000 once again we continue we get 2 is 2 3187 02:00:48,000 --> 02:00:50,880 greater than 6 no it's not so we move on 3188 02:00:50,880 --> 02:00:54,080 then we get 8 is 8 greater than six it 3189 02:00:54,080 --> 02:00:57,119 is so we should reset max to eight that 3190 02:00:57,119 --> 02:00:58,320 is pretty easy 3191 02:00:58,320 --> 02:01:02,560 so here we need a for loop for number 3192 02:01:02,560 --> 02:01:04,560 in numbers colon 3193 02:01:04,560 --> 02:01:05,920 now we need to check to see if this 3194 02:01:05,920 --> 02:01:08,480 number is greater than max 3195 02:01:08,480 --> 02:01:09,679 so 3196 02:01:09,679 --> 02:01:10,880 if number 3197 02:01:10,880 --> 02:01:13,679 is greater than max colon 3198 02:01:13,679 --> 02:01:16,400 then we need to reset max so we set max 3199 02:01:16,400 --> 02:01:18,960 to this new number 3200 02:01:18,960 --> 02:01:22,480 that's all we had to do so let's print 3201 02:01:22,480 --> 02:01:23,760 max 3202 02:01:23,760 --> 02:01:25,599 and run our program 3203 02:01:25,599 --> 02:01:27,119 we can say the largest number in this 3204 02:01:27,119 --> 02:01:29,199 list is 10. it doesn't matter whether 3205 02:01:29,199 --> 02:01:31,199 this number is at the end of the list or 3206 02:01:31,199 --> 02:01:33,920 at the beginning so if i move 10 3207 02:01:33,920 --> 02:01:37,360 and put it right at the beginning 3208 02:01:37,360 --> 02:01:39,119 we should still see the same result 3209 02:01:39,119 --> 02:01:41,840 let's run our program we still see 10 if 3210 02:01:41,840 --> 02:01:44,000 i put this somewhere in the middle 3211 02:01:44,000 --> 02:01:46,320 our program should still work let's put 3212 02:01:46,320 --> 02:01:47,679 it right after 3213 02:01:47,679 --> 02:01:48,800 two 3214 02:01:48,800 --> 02:01:53,360 run the program we still get 10. 3215 02:01:56,960 --> 02:01:58,239 in this tutorial i'm going to talk to 3216 02:01:58,239 --> 02:02:00,639 you guys about two-dimensional lists in 3217 02:02:00,639 --> 02:02:02,639 python two-dimensional lists are 3218 02:02:02,639 --> 02:02:04,719 extremely powerful and they have a lot 3219 02:02:04,719 --> 02:02:06,880 of applications in data science and 3220 02:02:06,880 --> 02:02:09,360 machine learning here's an example in 3221 02:02:09,360 --> 02:02:11,840 math we have a concept called matrix 3222 02:02:11,840 --> 02:02:14,400 which is like a rectangular array of 3223 02:02:14,400 --> 02:02:17,520 numbers let me show you so yeah one two 3224 02:02:17,520 --> 02:02:20,639 three four five six seven eight nine so 3225 02:02:20,639 --> 02:02:23,440 we have a rectangular array of numbers 3226 02:02:23,440 --> 02:02:24,800 we have rows 3227 02:02:24,800 --> 02:02:27,840 and columns so this is a three by three 3228 02:02:27,840 --> 02:02:30,639 matrix in math now we can model this in 3229 02:02:30,639 --> 02:02:33,760 python using a two-dimensional list a 3230 02:02:33,760 --> 02:02:36,000 two-dimensional list is a list where 3231 02:02:36,000 --> 02:02:39,199 each item in that list is another list 3232 02:02:39,199 --> 02:02:40,320 so 3233 02:02:40,320 --> 02:02:42,800 you want to define a matrix we set it to 3234 02:02:42,800 --> 02:02:43,840 a list 3235 02:02:43,840 --> 02:02:46,080 each item in this list is going to be 3236 02:02:46,080 --> 02:02:49,040 another list and that list represents 3237 02:02:49,040 --> 02:02:51,679 the items in each row 3238 02:02:51,679 --> 02:02:53,440 so the first item in our list is going 3239 02:02:53,440 --> 02:02:55,760 to be another list and in this list 3240 02:02:55,760 --> 02:02:58,400 we're going to have the values 1 2 and 3241 02:02:58,400 --> 02:02:59,520 3. 3242 02:02:59,520 --> 02:03:02,320 now the second item in our matrix list 3243 02:03:02,320 --> 02:03:04,560 once again we have a list this list 3244 02:03:04,560 --> 02:03:07,280 represents the items in the second row 3245 02:03:07,280 --> 02:03:08,239 so 3246 02:03:08,239 --> 02:03:11,440 4 5 and 6 and finally 3247 02:03:11,440 --> 02:03:13,679 7 8 9. 3248 02:03:13,679 --> 02:03:14,880 so as you can see we have a 3249 02:03:14,880 --> 02:03:17,679 two-dimensional list each item 3250 02:03:17,679 --> 02:03:20,719 in our outer list is another list 3251 02:03:20,719 --> 02:03:23,199 okay so this is how we can implement a 3252 02:03:23,199 --> 02:03:26,000 matrix in python now to access an 3253 02:03:26,000 --> 02:03:28,480 individual item in our matrix once again 3254 02:03:28,480 --> 02:03:31,040 we use square brackets let me delete 3255 02:03:31,040 --> 02:03:32,400 this stuff 3256 02:03:32,400 --> 02:03:33,440 all right 3257 02:03:33,440 --> 02:03:36,800 so how do we access one here well 3258 02:03:36,800 --> 02:03:39,040 we start with our list 3259 02:03:39,040 --> 02:03:41,199 then we add square brackets first we 3260 02:03:41,199 --> 02:03:43,920 need to go and get the first item in 3261 02:03:43,920 --> 02:03:47,679 this list right so we pass zero now this 3262 02:03:47,679 --> 02:03:50,800 expression returns another list that is 3263 02:03:50,800 --> 02:03:52,079 the inner list 3264 02:03:52,079 --> 02:03:53,760 in this list let's say we want to access 3265 02:03:53,760 --> 02:03:55,199 the second item 3266 02:03:55,199 --> 02:03:56,400 so 3267 02:03:56,400 --> 02:03:58,960 once again we add square brackets and we 3268 02:03:58,960 --> 02:04:01,840 pass one that is the index 3269 02:04:01,840 --> 02:04:04,639 of two in this list right 3270 02:04:04,639 --> 02:04:07,760 so if we print this on the terminal 3271 02:04:07,760 --> 02:04:11,840 we get two okay so using two square 3272 02:04:11,840 --> 02:04:14,800 brackets we can access individual items 3273 02:04:14,800 --> 02:04:16,960 in our matrix and also we can modify 3274 02:04:16,960 --> 02:04:19,840 these values using this syntax so 3275 02:04:19,840 --> 02:04:22,400 before printing this let's change it to 3276 02:04:22,400 --> 02:04:26,480 20. so matrix of 0 and 1 let's change it 3277 02:04:26,480 --> 02:04:27,760 to 20 3278 02:04:27,760 --> 02:04:29,840 and then print it there you go it's 3279 02:04:29,840 --> 02:04:31,360 modified 3280 02:04:31,360 --> 02:04:33,760 now here we can also use nested loops to 3281 02:04:33,760 --> 02:04:36,239 iterate over all the items in this 3282 02:04:36,239 --> 02:04:39,599 matrix let me show you so 3283 02:04:39,599 --> 02:04:42,639 we start with the rows for row 3284 02:04:42,639 --> 02:04:44,320 in matrix 3285 02:04:44,320 --> 02:04:47,199 so with this loop we are iterating over 3286 02:04:47,199 --> 02:04:50,639 our matrix list in each iteration row 3287 02:04:50,639 --> 02:04:54,400 will contain one list one item okay 3288 02:04:54,400 --> 02:04:55,440 now 3289 02:04:55,440 --> 02:04:57,840 here we need to use an inner loop 3290 02:04:57,840 --> 02:05:00,159 so we need to loop over this row which 3291 02:05:00,159 --> 02:05:02,320 is a list of items 3292 02:05:02,320 --> 02:05:04,000 we can type out four 3293 02:05:04,000 --> 02:05:06,719 item in row colon 3294 02:05:06,719 --> 02:05:09,360 and print item 3295 02:05:09,360 --> 02:05:10,239 okay 3296 02:05:10,239 --> 02:05:14,000 so let's run this program 3297 02:05:14,000 --> 02:05:16,719 there you go we get all the items 3298 02:05:16,719 --> 02:05:19,280 in our list 3299 02:05:19,599 --> 02:05:20,800 hey i just wanted to make a quick 3300 02:05:20,800 --> 02:05:22,480 announcement in case you haven't seen my 3301 02:05:22,480 --> 02:05:24,119 website yet head over to 3302 02:05:24,119 --> 02:05:26,079 codewithmosh.com this is my coding 3303 02:05:26,079 --> 02:05:27,760 school where you can find plenty of 3304 02:05:27,760 --> 02:05:29,840 courses on web and mobile application 3305 02:05:29,840 --> 02:05:31,760 development in fact recently i published 3306 02:05:31,760 --> 02:05:34,159 a comprehensive python course that goes 3307 02:05:34,159 --> 02:05:35,920 way beyond this beginner's course on 3308 02:05:35,920 --> 02:05:37,280 youtube and if you're serious about 3309 02:05:37,280 --> 02:05:39,440 learning python and getting a job i 3310 02:05:39,440 --> 02:05:40,880 highly encourage you to enroll in this 3311 02:05:40,880 --> 02:05:42,719 course just like this tutorial you can 3312 02:05:42,719 --> 02:05:45,679 watch it anytime anywhere as many times 3313 02:05:45,679 --> 02:05:47,679 as you want and you will also receive a 3314 02:05:47,679 --> 02:05:49,440 certificate of completion that you can 3315 02:05:49,440 --> 02:05:51,360 add to your resume and the course comes 3316 02:05:51,360 --> 02:05:53,760 with a 30-day money-back guarantee so if 3317 02:05:53,760 --> 02:05:55,440 you're not happy we give all your money 3318 02:05:55,440 --> 02:05:57,360 back no questions asked the price for 3319 02:05:57,360 --> 02:05:59,760 this course is 149 3320 02:05:59,760 --> 02:06:01,599 but the first 200 students can get it 3321 02:06:01,599 --> 02:06:03,760 for only 15 dollars so if you're 3322 02:06:03,760 --> 02:06:06,079 interested the link is below this video 3323 02:06:06,079 --> 02:06:09,599 click the link and get started 3324 02:06:11,360 --> 02:06:12,719 in this tutorial i'm going to talk to 3325 02:06:12,719 --> 02:06:14,880 you guys about the list methods or list 3326 02:06:14,880 --> 02:06:16,880 functions these are the operations that 3327 02:06:16,880 --> 02:06:19,760 we can perform on a list so let's define 3328 02:06:19,760 --> 02:06:22,000 a list of numbers 3329 02:06:22,000 --> 02:06:23,920 here we pass a bunch of random numbers 3330 02:06:23,920 --> 02:06:25,040 like 5 3331 02:06:25,040 --> 02:06:26,159 2 3332 02:06:26,159 --> 02:06:29,280 1 7 and 4. 3333 02:06:29,280 --> 02:06:30,560 now there are a number of things we can 3334 02:06:30,560 --> 02:06:32,320 do with this list we can add new items 3335 02:06:32,320 --> 02:06:34,320 to it we can remove existing items we 3336 02:06:34,320 --> 02:06:36,239 can check for the existence of an item 3337 02:06:36,239 --> 02:06:37,840 these are the operations that we can 3338 02:06:37,840 --> 02:06:40,400 perform on a list so when we type 3339 02:06:40,400 --> 02:06:41,599 numbers 3340 02:06:41,599 --> 02:06:44,079 dot we can see all these functions or 3341 02:06:44,079 --> 02:06:45,920 more accurately the methods that are 3342 02:06:45,920 --> 02:06:48,239 available in our list objects 3343 02:06:48,239 --> 02:06:50,400 so we can call the append method to add 3344 02:06:50,400 --> 02:06:53,520 a new item to this list let's say 3345 02:06:53,520 --> 02:06:56,320 13 actually no it's not a good number 3346 02:06:56,320 --> 02:06:58,320 i'm joking i'm not superstitious so 3347 02:06:58,320 --> 02:06:59,679 let's add 20. 3348 02:06:59,679 --> 02:07:02,719 it doesn't really matter and then print 3349 02:07:02,719 --> 02:07:03,760 our list 3350 02:07:03,760 --> 02:07:06,079 when we run the program you can see 3351 02:07:06,079 --> 02:07:08,639 20 is added at the end of this list but 3352 02:07:08,639 --> 02:07:10,239 what if you want to add a number 3353 02:07:10,239 --> 02:07:11,679 somewhere in the middle or at the 3354 02:07:11,679 --> 02:07:14,000 beginning of our list for that we use a 3355 02:07:14,000 --> 02:07:17,040 different method that is called insert 3356 02:07:17,040 --> 02:07:18,320 so 3357 02:07:18,320 --> 02:07:21,599 insert now this method takes two values 3358 02:07:21,599 --> 02:07:23,679 let me show you so when we open 3359 02:07:23,679 --> 02:07:25,040 parenthesis 3360 02:07:25,040 --> 02:07:27,280 look at this little tooltip above the 3361 02:07:27,280 --> 02:07:28,639 insert method 3362 02:07:28,639 --> 02:07:30,159 you see the first value that we need to 3363 02:07:30,159 --> 02:07:32,480 pass here is an index so this is the 3364 02:07:32,480 --> 02:07:34,639 index at which we want to insert this 3365 02:07:34,639 --> 02:07:36,800 new item let's say we want to add an 3366 02:07:36,800 --> 02:07:38,880 item at the beginning of our list so we 3367 02:07:38,880 --> 02:07:41,679 pass the index position of zero 3368 02:07:41,679 --> 02:07:43,840 and then the second value is the actual 3369 02:07:43,840 --> 02:07:45,920 object we want to add to this list let's 3370 02:07:45,920 --> 02:07:48,800 say we want to add the number 10. 3371 02:07:48,800 --> 02:07:51,599 now when we print this list you can see 3372 02:07:51,599 --> 02:07:53,280 that the number 10 3373 02:07:53,280 --> 02:07:55,760 is placed at the beginning of the list 3374 02:07:55,760 --> 02:07:57,520 and all the other items are pushed to 3375 02:07:57,520 --> 02:07:58,400 the right 3376 02:07:58,400 --> 02:08:01,440 we can also remove an item so we call 3377 02:08:01,440 --> 02:08:04,560 remove and pass the item that we want to 3378 02:08:04,560 --> 02:08:07,599 remove let's say we want to remove five 3379 02:08:07,599 --> 02:08:10,079 now we print our list so you can see 3380 02:08:10,079 --> 02:08:13,119 five is gone and we have two one seven 3381 02:08:13,119 --> 02:08:15,360 four if you wanna remove all the items 3382 02:08:15,360 --> 02:08:17,360 in the list you can call the clear 3383 02:08:17,360 --> 02:08:19,199 method so 3384 02:08:19,199 --> 02:08:20,239 clear 3385 02:08:20,239 --> 02:08:23,599 this method doesn't take any values so 3386 02:08:23,599 --> 02:08:26,400 we simply call it and it empties our 3387 02:08:26,400 --> 02:08:27,199 list 3388 02:08:27,199 --> 02:08:29,119 all the items are removed 3389 02:08:29,119 --> 02:08:31,119 we also have another useful method 3390 02:08:31,119 --> 02:08:32,880 called pop 3391 02:08:32,880 --> 02:08:34,880 and with this we can remove the last 3392 02:08:34,880 --> 02:08:37,760 item in a list let me show you so run 3393 02:08:37,760 --> 02:08:40,639 our program you can see the number four 3394 02:08:40,639 --> 02:08:43,280 is removed from the end of our list 3395 02:08:43,280 --> 02:08:44,400 now if you want to check for the 3396 02:08:44,400 --> 02:08:46,800 existence of an item in a list you can 3397 02:08:46,800 --> 02:08:49,280 call the index method so 3398 02:08:49,280 --> 02:08:50,960 we call index 3399 02:08:50,960 --> 02:08:53,040 and pass the value here like five and 3400 02:08:53,040 --> 02:08:55,920 this returns the index of the first 3401 02:08:55,920 --> 02:08:58,960 occurrence of this item so let's print 3402 02:08:58,960 --> 02:09:00,880 this on the terminal 3403 02:09:00,880 --> 02:09:03,679 we don't need this line anymore 3404 02:09:03,679 --> 02:09:06,560 so the index of five is zero what if we 3405 02:09:06,560 --> 02:09:08,400 pass a number that doesn't exist in this 3406 02:09:08,400 --> 02:09:10,560 list let's say 50. 3407 02:09:10,560 --> 02:09:12,560 run the program 3408 02:09:12,560 --> 02:09:15,760 we get an error we got a value error 50 3409 02:09:15,760 --> 02:09:17,360 is not in the list 3410 02:09:17,360 --> 02:09:19,040 there is also another way to check for 3411 02:09:19,040 --> 02:09:21,119 the existence of an item we can use the 3412 02:09:21,119 --> 02:09:23,679 in operator so 3413 02:09:23,679 --> 02:09:27,119 let me show you we type out 15 in 3414 02:09:27,119 --> 02:09:28,960 numbers 3415 02:09:28,960 --> 02:09:30,880 earlier we used the in operator with a 3416 02:09:30,880 --> 02:09:32,880 string we could check for the existence 3417 02:09:32,880 --> 02:09:34,719 of a character or a sequence of 3418 02:09:34,719 --> 02:09:36,320 characters in a string 3419 02:09:36,320 --> 02:09:37,440 now here we're checking for the 3420 02:09:37,440 --> 02:09:40,079 existence of 50 in the list of numbers 3421 02:09:40,079 --> 02:09:42,880 so let's print this 3422 02:09:42,880 --> 02:09:45,520 we get a boolean value false so unlike 3423 02:09:45,520 --> 02:09:48,480 the index method this expression doesn't 3424 02:09:48,480 --> 02:09:50,800 generate an error so it's safer to use 3425 02:09:50,800 --> 02:09:53,119 this we also have another method for 3426 02:09:53,119 --> 02:09:55,520 counting the occurrences of an item 3427 02:09:55,520 --> 02:09:58,239 let's say we have another five 3428 02:09:58,239 --> 02:09:59,119 over 3429 02:09:59,119 --> 02:10:01,679 here now we can call 3430 02:10:01,679 --> 02:10:05,040 numbers that count and pass five and 3431 02:10:05,040 --> 02:10:07,119 this should return two because we have 3432 02:10:07,119 --> 02:10:09,920 two fives in this list take a look there 3433 02:10:09,920 --> 02:10:10,800 you go 3434 02:10:10,800 --> 02:10:12,639 that is pretty useful now if you want to 3435 02:10:12,639 --> 02:10:14,480 sort your lists you can call the sort 3436 02:10:14,480 --> 02:10:16,000 method so 3437 02:10:16,000 --> 02:10:18,480 we call the sort method here this method 3438 02:10:18,480 --> 02:10:21,199 doesn't take any values so 3439 02:10:21,199 --> 02:10:23,840 look at the return value that is none 3440 02:10:23,840 --> 02:10:25,679 none is an object in python that 3441 02:10:25,679 --> 02:10:28,400 represents the absence of a value 3442 02:10:28,400 --> 02:10:29,760 so this sort method doesn't really 3443 02:10:29,760 --> 02:10:32,239 return any values it simply sorts this 3444 02:10:32,239 --> 02:10:35,360 list in place so instead of printing the 3445 02:10:35,360 --> 02:10:37,520 return value of this method 3446 02:10:37,520 --> 02:10:40,560 we simply call it to sort our list and 3447 02:10:40,560 --> 02:10:43,119 then print our list 3448 02:10:43,119 --> 02:10:44,239 take a look 3449 02:10:44,239 --> 02:10:46,320 now all the items are sorted in 3450 02:10:46,320 --> 02:10:48,400 ascending order we can also sort the 3451 02:10:48,400 --> 02:10:50,880 items in descending order so after we 3452 02:10:50,880 --> 02:10:53,760 sort the list we can call 3453 02:10:53,760 --> 02:10:55,920 the reverse method and this simply 3454 02:10:55,920 --> 02:10:57,440 reverses our list 3455 02:10:57,440 --> 02:10:59,520 now let's go ahead and run our program 3456 02:10:59,520 --> 02:11:01,199 take a look 3457 02:11:01,199 --> 02:11:03,199 our numbers are sorted in descending 3458 02:11:03,199 --> 02:11:04,159 order 3459 02:11:04,159 --> 02:11:05,679 and one last method i want to show you 3460 02:11:05,679 --> 02:11:08,560 here that is pretty useful is the copy 3461 02:11:08,560 --> 02:11:10,480 method so copy 3462 02:11:10,480 --> 02:11:12,239 with this method we can get a copy of 3463 02:11:12,239 --> 02:11:14,239 our list so let's define another 3464 02:11:14,239 --> 02:11:16,560 variable called numbers2 3465 02:11:16,560 --> 02:11:19,360 now numbers2 is a copy of our original 3466 02:11:19,360 --> 02:11:21,520 list so if you make any changes to our 3467 02:11:21,520 --> 02:11:23,360 original list if you add new items to it 3468 02:11:23,360 --> 02:11:25,280 if you remove existing items these 3469 02:11:25,280 --> 02:11:27,280 operations are not going to impact our 3470 02:11:27,280 --> 02:11:29,679 second list let me show you so after we 3471 02:11:29,679 --> 02:11:32,159 take a copy of our numbers list 3472 02:11:32,159 --> 02:11:35,040 let's add a new item to this list some 3473 02:11:35,040 --> 02:11:37,520 numbers that append 10. 3474 02:11:37,520 --> 02:11:40,000 so the first list is updated so now we 3475 02:11:40,000 --> 02:11:42,400 have a new item in our first list 3476 02:11:42,400 --> 02:11:45,599 then let's print the second list 3477 02:11:45,599 --> 02:11:47,520 take a look we don't have the number 10 3478 02:11:47,520 --> 02:11:49,840 here because these are two independent 3479 02:11:49,840 --> 02:11:50,880 lists 3480 02:11:50,880 --> 02:11:52,560 so these are all the operations that we 3481 02:11:52,560 --> 02:11:54,960 can perform on lists we can add new 3482 02:11:54,960 --> 02:11:57,119 items to a list we can remove existing 3483 02:11:57,119 --> 02:11:59,119 items we can check for the existence of 3484 02:11:59,119 --> 02:12:01,840 an item we can sort our lists and copy 3485 02:12:01,840 --> 02:12:03,040 them 3486 02:12:03,040 --> 02:12:04,400 now here is the exercise for this 3487 02:12:04,400 --> 02:12:06,560 tutorial i want you to write a program 3488 02:12:06,560 --> 02:12:08,639 to remove the duplicates in a list again 3489 02:12:08,639 --> 02:12:10,800 this is a fantastic exercise so spend a 3490 02:12:10,800 --> 02:12:12,239 few minutes on this and then come back 3491 02:12:12,239 --> 02:12:15,040 continue watching 3492 02:12:17,760 --> 02:12:18,960 all right let's say we have a list of 3493 02:12:18,960 --> 02:12:22,719 numbers with a bunch of duplicates so 2 3494 02:12:22,719 --> 02:12:23,440 2 3495 02:12:23,440 --> 02:12:26,000 4 6 3 4 3496 02:12:26,000 --> 02:12:27,599 6 1. 3497 02:12:27,599 --> 02:12:29,679 we want to remove the duplicates 3498 02:12:29,679 --> 02:12:32,560 so we need to define another list let's 3499 02:12:32,560 --> 02:12:35,599 call that unix 3500 02:12:35,599 --> 02:12:38,320 initially we set it to an empty list 3501 02:12:38,320 --> 02:12:40,480 then we need to iterate over 3502 02:12:40,480 --> 02:12:43,440 our first list get each item and if we 3503 02:12:43,440 --> 02:12:46,159 don't have that number in this unix list 3504 02:12:46,159 --> 02:12:48,480 then we'll add it to the second list as 3505 02:12:48,480 --> 02:12:51,280 simple as that so 3506 02:12:51,280 --> 02:12:54,639 for number in numbers 3507 02:12:54,639 --> 02:12:56,560 now we need to check to see if we have 3508 02:12:56,560 --> 02:12:57,920 this number 3509 02:12:57,920 --> 02:12:59,440 in the second list 3510 02:12:59,440 --> 02:13:01,440 so we use the in operator 3511 02:13:01,440 --> 02:13:02,400 if 3512 02:13:02,400 --> 02:13:04,000 number 3513 02:13:04,000 --> 02:13:06,400 not in unix 3514 02:13:06,400 --> 02:13:08,079 so note that here we are using the not 3515 02:13:08,079 --> 02:13:11,119 operator so if we don't have this number 3516 02:13:11,119 --> 02:13:12,639 in this unix list 3517 02:13:12,639 --> 02:13:15,199 then we'll need to add it so 3518 02:13:15,199 --> 02:13:17,679 unix dot append 3519 02:13:17,679 --> 02:13:18,880 number 3520 02:13:18,880 --> 02:13:20,960 that's all we have to do 3521 02:13:20,960 --> 02:13:23,920 so let's go ahead and print the unix 3522 02:13:23,920 --> 02:13:25,520 list 3523 02:13:25,520 --> 02:13:28,880 there you go so we have two four six 3524 02:13:28,880 --> 02:13:33,360 three and 1. the duplicates are removed 3525 02:13:36,880 --> 02:13:38,239 in this tutorial i'm going to talk to 3526 02:13:38,239 --> 02:13:40,000 you guys about another important 3527 02:13:40,000 --> 02:13:42,880 structure in python called topo tuples 3528 02:13:42,880 --> 02:13:44,719 are similar to lists so we can use them 3529 02:13:44,719 --> 02:13:47,360 to store a list of items but unlike 3530 02:13:47,360 --> 02:13:49,679 lists we cannot modify them we cannot 3531 02:13:49,679 --> 02:13:51,679 add new items we cannot remove existing 3532 02:13:51,679 --> 02:13:54,239 items we say tuples are immutable we 3533 02:13:54,239 --> 02:13:56,560 cannot mutate or change them let me show 3534 02:13:56,560 --> 02:13:58,719 you so i'm going to start by defining a 3535 02:13:58,719 --> 02:14:01,520 list of numbers one two three 3536 02:14:01,520 --> 02:14:03,440 so we use square brackets to define 3537 02:14:03,440 --> 02:14:06,800 lists and parentheses to define tuples 3538 02:14:06,800 --> 02:14:08,960 so if we change this to 3539 02:14:08,960 --> 02:14:12,320 parenthesis one two three now we have a 3540 02:14:12,320 --> 02:14:15,599 tuple so if we type numbers dot look 3541 02:14:15,599 --> 02:14:18,079 here we don't have the append or insert 3542 02:14:18,079 --> 02:14:20,000 methods so we cannot add new items to 3543 02:14:20,000 --> 02:14:22,800 this tuple we also don't have remove 3544 02:14:22,800 --> 02:14:25,360 clear and pop we cannot remove any of 3545 02:14:25,360 --> 02:14:26,400 these items here 3546 02:14:26,400 --> 02:14:29,760 we only have two methods count and index 3547 02:14:29,760 --> 02:14:31,280 we use count to count the number of 3548 02:14:31,280 --> 02:14:34,159 occurrences of an item and index to find 3549 02:14:34,159 --> 02:14:36,560 the index of the first occurrence of an 3550 02:14:36,560 --> 02:14:38,719 item so we can only get information 3551 02:14:38,719 --> 02:14:41,360 about a tuple we cannot change it and by 3552 02:14:41,360 --> 02:14:42,960 the way these other methods that you see 3553 02:14:42,960 --> 02:14:45,599 here they start with two underscores we 3554 02:14:45,599 --> 02:14:47,679 refer to this as magic methods they're 3555 02:14:47,679 --> 02:14:49,760 more of an advanced topic so they go 3556 02:14:49,760 --> 02:14:51,760 beyond the scope of this tutorial if 3557 02:14:51,760 --> 02:14:53,119 you're interested to learn about them 3558 02:14:53,119 --> 02:14:54,639 you can get my python course i've 3559 02:14:54,639 --> 02:14:56,320 covered them in detail 3560 02:14:56,320 --> 02:14:59,040 so similar to lists we can access 3561 02:14:59,040 --> 02:15:01,440 individual items using square brackets 3562 02:15:01,440 --> 02:15:04,000 so we can get the first item like this 3563 02:15:04,000 --> 02:15:06,480 and then print it on the terminal 3564 02:15:06,480 --> 02:15:09,040 there you go the first item is one 3565 02:15:09,040 --> 02:15:11,040 but if you try to change the first item 3566 02:15:11,040 --> 02:15:13,920 we'll get an error so numbers of zero we 3567 02:15:13,920 --> 02:15:15,280 set it to 10 3568 02:15:15,280 --> 02:15:17,679 and run our program there you go we get 3569 02:15:17,679 --> 02:15:20,239 this type error because the tuple object 3570 02:15:20,239 --> 02:15:23,440 does not support item assignment 3571 02:15:23,440 --> 02:15:26,079 so we cannot mutate or change tuples 3572 02:15:26,079 --> 02:15:27,840 they are immutable 3573 02:15:27,840 --> 02:15:30,000 now practically speaking most of the 3574 02:15:30,000 --> 02:15:32,079 time you will be using lists but tuples 3575 02:15:32,079 --> 02:15:33,840 are also useful if you want to create a 3576 02:15:33,840 --> 02:15:35,360 list of items and you want to make sure 3577 02:15:35,360 --> 02:15:37,119 that nowhere in your program you 3578 02:15:37,119 --> 02:15:39,599 accidentally modify that list then it's 3579 02:15:39,599 --> 02:15:42,800 better to use a tuple 3580 02:15:46,159 --> 02:15:47,840 in this tutorial i'm going to show you a 3581 02:15:47,840 --> 02:15:49,440 powerful feature we have in python 3582 02:15:49,440 --> 02:15:53,360 called unpacking so let's define a tuple 3583 02:15:53,360 --> 02:15:56,079 called coordinates 3584 02:15:56,079 --> 02:15:58,800 and here we pass three values one two 3585 02:15:58,800 --> 02:16:00,320 three so imagine these are the 3586 02:16:00,320 --> 02:16:03,599 coordinates for x y and z 3587 02:16:03,599 --> 02:16:04,719 now let's say we want to get these 3588 02:16:04,719 --> 02:16:07,119 values and use them in a few expressions 3589 02:16:07,119 --> 02:16:09,040 in a few complex expressions in our 3590 02:16:09,040 --> 02:16:10,960 program maybe we want to include them as 3591 02:16:10,960 --> 02:16:14,239 part of a large complex formula so 3592 02:16:14,239 --> 02:16:15,760 together we'll have to write code like 3593 02:16:15,760 --> 02:16:18,719 this coordinates of zero then let's say 3594 02:16:18,719 --> 02:16:21,199 we want to multiply this by coordinates 3595 02:16:21,199 --> 02:16:23,520 of one and then multiply it by 3596 02:16:23,520 --> 02:16:25,440 coordinates of two 3597 02:16:25,440 --> 02:16:26,960 our code is getting a little bit too 3598 02:16:26,960 --> 02:16:29,280 long this is just a very simple example 3599 02:16:29,280 --> 02:16:30,480 but let's say we want to use these 3600 02:16:30,480 --> 02:16:32,479 values in quite a few places in our 3601 02:16:32,479 --> 02:16:35,679 program so a better approach is to get 3602 02:16:35,679 --> 02:16:37,760 these values and store them in separate 3603 02:16:37,760 --> 02:16:39,519 variables like 3604 02:16:39,519 --> 02:16:42,800 we can get coordinates of 0 and store it 3605 02:16:42,800 --> 02:16:43,840 in x 3606 02:16:43,840 --> 02:16:46,000 then we get coordinates 3607 02:16:46,000 --> 02:16:47,040 a 1 3608 02:16:47,040 --> 02:16:48,880 and store it in y 3609 02:16:48,880 --> 02:16:51,840 and similarly we get coordinates of 2 3610 02:16:51,840 --> 02:16:53,679 and then store it in z 3611 02:16:53,679 --> 02:16:56,000 now instead of repeating coordinates of 3612 02:16:56,000 --> 02:16:58,880 2 or coordinates of 0 multiple times we 3613 02:16:58,880 --> 02:17:01,519 can simply work with these variables x 3614 02:17:01,519 --> 02:17:03,519 times y times z 3615 02:17:03,519 --> 02:17:05,280 that is better right 3616 02:17:05,280 --> 02:17:08,000 so nothing new so far but in python we 3617 02:17:08,000 --> 02:17:10,639 have a powerful feature called unpacking 3618 02:17:10,639 --> 02:17:12,399 and with that we can achieve the same 3619 02:17:12,399 --> 02:17:15,040 result with far less code so we can 3620 02:17:15,040 --> 02:17:17,840 define our variables x y 3621 02:17:17,840 --> 02:17:19,040 and z 3622 02:17:19,040 --> 02:17:22,000 and set them to our tuple 3623 02:17:22,000 --> 02:17:24,559 what we have on line six is exactly 3624 02:17:24,559 --> 02:17:27,519 identical to what we have on lines two 3625 02:17:27,519 --> 02:17:28,719 to four 3626 02:17:28,719 --> 02:17:31,519 so this is a shorthand to achieve the 3627 02:17:31,519 --> 02:17:32,880 same result 3628 02:17:32,880 --> 02:17:35,200 so let me delete this and explain how 3629 02:17:35,200 --> 02:17:37,920 this code works when python interpreter 3630 02:17:37,920 --> 02:17:40,319 sees this statement it will get the 3631 02:17:40,319 --> 02:17:43,439 first item in this tuple and assign it 3632 02:17:43,439 --> 02:17:45,760 to this variable then it will get the 3633 02:17:45,760 --> 02:17:48,080 second item in this tuple and assign it 3634 02:17:48,080 --> 02:17:50,080 to the second variable and similarly it 3635 02:17:50,080 --> 02:17:51,840 will get the third item in this top hole 3636 02:17:51,840 --> 02:17:53,439 and assign it to the third variable we 3637 02:17:53,439 --> 02:17:54,719 have here 3638 02:17:54,719 --> 02:17:58,080 so we are unpacking this tuple into 3639 02:17:58,080 --> 02:17:59,439 three variables 3640 02:17:59,439 --> 02:18:01,760 now if we print 3641 02:18:01,760 --> 02:18:03,760 x you can see 3642 02:18:03,760 --> 02:18:06,719 x is one similarly y 3643 02:18:06,719 --> 02:18:09,040 is two there you go 3644 02:18:09,040 --> 02:18:11,599 so this is unpacking and by the way this 3645 02:18:11,599 --> 02:18:14,160 is not limited to tuples we can use this 3646 02:18:14,160 --> 02:18:16,479 feature with lists as well so if i 3647 02:18:16,479 --> 02:18:18,319 change parenthesis 3648 02:18:18,319 --> 02:18:19,920 to square brackets 3649 02:18:19,920 --> 02:18:22,240 now coordinates is a list so we can 3650 02:18:22,240 --> 02:18:24,880 unpack this list into three variables 3651 02:18:24,880 --> 02:18:25,840 now 3652 02:18:25,840 --> 02:18:30,000 run our program you can see y is two 3653 02:18:34,000 --> 02:18:35,200 in this tutorial i'm going to talk to 3654 02:18:35,200 --> 02:18:38,080 you guys about dictionaries in python we 3655 02:18:38,080 --> 02:18:40,160 use dictionaries in situations where we 3656 02:18:40,160 --> 02:18:42,080 want to store information that come as 3657 02:18:42,080 --> 02:18:44,880 key value pairs here's an example think 3658 02:18:44,880 --> 02:18:47,679 of a customer a customer has a bunch of 3659 02:18:47,679 --> 02:18:50,000 attributes like name 3660 02:18:50,000 --> 02:18:51,120 email 3661 02:18:51,120 --> 02:18:54,000 phone number address and so on but each 3662 02:18:54,000 --> 02:18:56,240 of these attributes has a value for 3663 02:18:56,240 --> 02:18:59,120 example the name can be john smith the 3664 02:18:59,120 --> 02:19:02,080 email can be john gmail.com 3665 02:19:02,080 --> 02:19:04,319 the phone can be whatever so what we 3666 02:19:04,319 --> 02:19:07,920 have here is a bunch of key value pairs 3667 02:19:07,920 --> 02:19:10,559 so in this example our keys are name 3668 02:19:10,559 --> 02:19:12,880 email and phone and each key is 3669 02:19:12,880 --> 02:19:15,679 associated with a value so this is where 3670 02:19:15,679 --> 02:19:18,080 we use a dictionary with a dictionary we 3671 02:19:18,080 --> 02:19:20,559 can store a bunch of key value pairs 3672 02:19:20,559 --> 02:19:22,000 so let me show you how to define a 3673 02:19:22,000 --> 02:19:24,639 dictionary in python back to our program 3674 02:19:24,639 --> 02:19:28,000 i'm going to define a variable customer 3675 02:19:28,000 --> 02:19:30,719 and here we set it to curly braces with 3676 02:19:30,719 --> 02:19:32,559 these curly braces we can define a 3677 02:19:32,559 --> 02:19:34,800 dictionary in this example we have an 3678 02:19:34,800 --> 02:19:36,800 empty dictionary that doesn't have any 3679 02:19:36,800 --> 02:19:39,760 key value pairs now we can add one or 3680 02:19:39,760 --> 02:19:41,760 more key value pairs in between the 3681 02:19:41,760 --> 02:19:44,639 braces so let's add a key value pair 3682 02:19:44,639 --> 02:19:45,519 here 3683 02:19:45,519 --> 02:19:47,519 i'm going to set the key to name 3684 02:19:47,519 --> 02:19:51,120 and the value to john smith 3685 02:19:51,120 --> 02:19:53,200 then we add a comma to add another key 3686 02:19:53,200 --> 02:19:55,600 value pair so let's set 3687 02:19:55,600 --> 02:19:57,760 age to 30. 3688 02:19:57,760 --> 02:20:00,319 let's add another key value pair is 3689 02:20:00,319 --> 02:20:02,800 underline verified 3690 02:20:02,800 --> 02:20:05,760 and we set this to a boolean 3691 02:20:05,760 --> 02:20:08,240 now what matters here is that these keys 3692 02:20:08,240 --> 02:20:10,319 should be unique so if i add another key 3693 02:20:10,319 --> 02:20:11,760 value pair here 3694 02:20:11,760 --> 02:20:13,280 let's set age 3695 02:20:13,280 --> 02:20:14,160 to 3696 02:20:14,160 --> 02:20:15,439 40. 3697 02:20:15,439 --> 02:20:18,240 now look pycharm has highlighted the h 3698 02:20:18,240 --> 02:20:19,920 key because we have duplicated that and 3699 02:20:19,920 --> 02:20:22,319 that's not allowed so each key should be 3700 02:20:22,319 --> 02:20:24,000 unique in a dictionary just like the 3701 02:20:24,000 --> 02:20:25,680 dictionaries we have in the real world 3702 02:20:25,680 --> 02:20:27,280 in a real dictionary we have a bunch of 3703 02:20:27,280 --> 02:20:30,080 words and their definition each word is 3704 02:20:30,080 --> 02:20:32,160 listed only once in a dictionary we 3705 02:20:32,160 --> 02:20:34,640 don't have the word book twice 3706 02:20:34,640 --> 02:20:37,359 so let's delete this second duplicate 3707 02:20:37,359 --> 02:20:38,880 key value pair 3708 02:20:38,880 --> 02:20:41,359 so the keys should be unique and in this 3709 02:20:41,359 --> 02:20:43,439 example i'm using strings but they can 3710 02:20:43,439 --> 02:20:44,720 also be numbers we're going to look at 3711 02:20:44,720 --> 02:20:45,760 that later 3712 02:20:45,760 --> 02:20:48,000 but the value can be anything it can be 3713 02:20:48,000 --> 02:20:51,200 a string a number a boolean a list 3714 02:20:51,200 --> 02:20:52,960 literally anything 3715 02:20:52,960 --> 02:20:55,280 now we can access each item in this 3716 02:20:55,280 --> 02:20:58,720 dictionary using square brackets so 3717 02:20:58,720 --> 02:21:00,240 we type customer 3718 02:21:00,240 --> 02:21:03,200 square bracket and then specify a key 3719 02:21:03,200 --> 02:21:04,319 like name 3720 02:21:04,319 --> 02:21:06,560 and this will return the value 3721 02:21:06,560 --> 02:21:09,359 associated with the name key let's print 3722 02:21:09,359 --> 02:21:12,479 it on the terminal have a look 3723 02:21:12,479 --> 02:21:14,160 there you go so 3724 02:21:14,160 --> 02:21:16,319 the name is john smith now what if we 3725 02:21:16,319 --> 02:21:18,319 pass a key that doesn't exist 3726 02:21:18,319 --> 02:21:19,280 let's say 3727 02:21:19,280 --> 02:21:20,720 birth date 3728 02:21:20,720 --> 02:21:23,120 run the program we get a key error 3729 02:21:23,120 --> 02:21:24,880 because we don't have a key called birth 3730 02:21:24,880 --> 02:21:26,800 date also 3731 02:21:26,800 --> 02:21:28,880 if we spell name 3732 02:21:28,880 --> 02:21:31,680 with let's say a capital n we get the 3733 02:21:31,680 --> 02:21:33,840 same error because we don't have a key 3734 02:21:33,840 --> 02:21:35,280 with the exact 3735 02:21:35,280 --> 02:21:37,520 same sequence of characters in this 3736 02:21:37,520 --> 02:21:38,800 dictionary 3737 02:21:38,800 --> 02:21:40,319 now to get around this we can use the 3738 02:21:40,319 --> 02:21:42,240 get method let me show you 3739 02:21:42,240 --> 02:21:44,399 so instead of using the square brackets 3740 02:21:44,399 --> 02:21:47,600 we call the get method and specify 3741 02:21:47,600 --> 02:21:48,720 our key 3742 02:21:48,720 --> 02:21:50,479 now if we use a key that doesn't exist 3743 02:21:50,479 --> 02:21:52,800 here it doesn't yell at us for example 3744 02:21:52,800 --> 02:21:56,080 if you pass birth date 3745 02:21:56,080 --> 02:21:58,960 it simply returns the non-value earlier 3746 02:21:58,960 --> 02:22:01,120 i told you that none is an object that 3747 02:22:01,120 --> 02:22:03,760 represents the absence of a value 3748 02:22:03,760 --> 02:22:06,080 so instead of getting a key error we get 3749 02:22:06,080 --> 02:22:08,880 none now we can also optionally supply a 3750 02:22:08,880 --> 02:22:11,120 default value for example if this 3751 02:22:11,120 --> 02:22:13,280 dictionary doesn't have this key we can 3752 02:22:13,280 --> 02:22:16,319 supply a default value let's say 3753 02:22:16,319 --> 02:22:19,120 january 1st 1980. 3754 02:22:19,120 --> 02:22:20,960 let's run the program 3755 02:22:20,960 --> 02:22:22,640 now instead of getting none we get this 3756 02:22:22,640 --> 02:22:24,319 default value 3757 02:22:24,319 --> 02:22:26,880 so this is how we can access the value 3758 02:22:26,880 --> 02:22:28,960 associated with the key in a dictionary 3759 02:22:28,960 --> 02:22:31,120 we can also update these values for 3760 02:22:31,120 --> 02:22:33,439 example before our print statement we 3761 02:22:33,439 --> 02:22:35,680 can write code like this customer 3762 02:22:35,680 --> 02:22:36,880 of name 3763 02:22:36,880 --> 02:22:39,120 let's update the name to 3764 02:22:39,120 --> 02:22:41,040 jack smith 3765 02:22:41,040 --> 02:22:42,640 now this little warning here is telling 3766 02:22:42,640 --> 02:22:45,040 us that we could simply put jack smith 3767 02:22:45,040 --> 02:22:47,200 here instead of defining it once and 3768 02:22:47,200 --> 02:22:48,960 then update it don't worry about that it 3769 02:22:48,960 --> 02:22:51,439 doesn't really matter now with this line 3770 02:22:51,439 --> 02:22:53,760 if we print the name of this customer we 3771 02:22:53,760 --> 02:22:56,560 should see jack smith let me show you so 3772 02:22:56,560 --> 02:22:58,000 i'm going to use the square bracket 3773 02:22:58,000 --> 02:23:00,560 notation again let's print the name of 3774 02:23:00,560 --> 02:23:02,800 the customer you can see 3775 02:23:02,800 --> 02:23:05,920 that is updated here we can also add a 3776 02:23:05,920 --> 02:23:07,280 new key here 3777 02:23:07,280 --> 02:23:09,439 let's set the birth date 3778 02:23:09,439 --> 02:23:11,200 to some value like 3779 02:23:11,200 --> 02:23:13,760 january 1st 1980 3780 02:23:13,760 --> 02:23:16,800 and then we can print it here 3781 02:23:16,800 --> 02:23:18,960 so as you see we can easily add new key 3782 02:23:18,960 --> 02:23:21,680 value pairs to a dictionary 3783 02:23:21,680 --> 02:23:23,120 so this is the basics of using 3784 02:23:23,120 --> 02:23:25,600 dictionaries in python they're extremely 3785 02:23:25,600 --> 02:23:27,280 important and they have a lot of 3786 02:23:27,280 --> 02:23:29,600 applications in the real world 3787 02:23:29,600 --> 02:23:31,680 okay here's an exercise for you so here 3788 02:23:31,680 --> 02:23:34,240 we have this program that asks our phone 3789 02:23:34,240 --> 02:23:37,439 number let's type one two three four we 3790 02:23:37,439 --> 02:23:39,520 type it in digits and then this will 3791 02:23:39,520 --> 02:23:43,040 translate it to words take a look enter 3792 02:23:43,040 --> 02:23:46,160 it prints one two three four that's a 3793 02:23:46,160 --> 02:23:48,479 pretty cool program so go ahead and 3794 02:23:48,479 --> 02:23:50,399 spend a few minutes on this exercise 3795 02:23:50,399 --> 02:23:54,160 it's pretty easy i'll see you next 3796 02:23:56,560 --> 02:23:57,760 all right so first we need to get the 3797 02:23:57,760 --> 02:23:59,680 user's phone number we call the input 3798 02:23:59,680 --> 02:24:00,880 function 3799 02:24:00,880 --> 02:24:03,359 with the label phone we get the result 3800 02:24:03,359 --> 02:24:04,640 and store it 3801 02:24:04,640 --> 02:24:06,160 in this variable 3802 02:24:06,160 --> 02:24:07,920 now let's say the user enters one two 3803 02:24:07,920 --> 02:24:09,760 three four so we need to loop through 3804 02:24:09,760 --> 02:24:12,080 this string get each character and 3805 02:24:12,080 --> 02:24:13,760 translate it to a word 3806 02:24:13,760 --> 02:24:15,120 so what we need to implement this 3807 02:24:15,120 --> 02:24:17,200 scenario is a dictionary because a 3808 02:24:17,200 --> 02:24:19,359 dictionary is a structure that allows us 3809 02:24:19,359 --> 02:24:22,560 to map a key to a value so we can have a 3810 02:24:22,560 --> 02:24:25,600 dictionary with keys like one two three 3811 02:24:25,600 --> 02:24:29,120 four and we map each of these keys 3812 02:24:29,120 --> 02:24:31,840 to a word so we can map the digit one to 3813 02:24:31,840 --> 02:24:32,960 the word 3814 02:24:32,960 --> 02:24:36,080 one we can map two 3815 02:24:36,080 --> 02:24:40,240 to two you got the point so let's define 3816 02:24:40,240 --> 02:24:42,720 a dictionary we can call it digits 3817 02:24:42,720 --> 02:24:45,439 underline mapping 3818 02:24:45,439 --> 02:24:47,520 now this dictionary i'm gonna add a few 3819 02:24:47,520 --> 02:24:48,960 key value pairs 3820 02:24:48,960 --> 02:24:51,520 one we map it to one 3821 02:24:51,520 --> 02:24:52,479 two 3822 02:24:52,479 --> 02:24:54,640 to two 3823 02:24:54,640 --> 02:24:57,600 three to three and finally 3824 02:24:57,600 --> 02:25:00,160 four to four now technically we should 3825 02:25:00,160 --> 02:25:02,720 add all the digits from zero to nine but 3826 02:25:02,720 --> 02:25:04,160 i don't wanna waste your time typing 3827 02:25:04,160 --> 02:25:06,560 repetitive things here you got the point 3828 02:25:06,560 --> 02:25:08,960 so let's move on now we need to loop 3829 02:25:08,960 --> 02:25:12,240 through the phone string so 3830 02:25:12,240 --> 02:25:13,040 for 3831 02:25:13,040 --> 02:25:14,080 character 3832 02:25:14,080 --> 02:25:15,439 in phone 3833 02:25:15,439 --> 02:25:17,600 we get each character and then use it to 3834 02:25:17,600 --> 02:25:19,920 access a key value pair in our 3835 02:25:19,920 --> 02:25:22,080 dictionary so 3836 02:25:22,080 --> 02:25:24,399 digits underline mapping 3837 02:25:24,399 --> 02:25:27,359 we can use square brackets or call the 3838 02:25:27,359 --> 02:25:29,439 get method i would prefer to use the get 3839 02:25:29,439 --> 02:25:31,600 method so in case the user enters some 3840 02:25:31,600 --> 02:25:33,040 character that is not part of our 3841 02:25:33,040 --> 02:25:34,640 dictionary our program is not going to 3842 02:25:34,640 --> 02:25:36,000 yell at them 3843 02:25:36,000 --> 02:25:36,800 so 3844 02:25:36,800 --> 02:25:38,720 we call the get method 3845 02:25:38,720 --> 02:25:41,359 and pass this character as the key 3846 02:25:41,359 --> 02:25:43,200 and if we don't have this key we can 3847 02:25:43,200 --> 02:25:44,160 supply 3848 02:25:44,160 --> 02:25:46,560 a default value like an exclamation mark 3849 02:25:46,560 --> 02:25:48,560 so with this we'll get a word now we 3850 02:25:48,560 --> 02:25:50,560 need to add this word to an output 3851 02:25:50,560 --> 02:25:54,080 string so we can define an output string 3852 02:25:54,080 --> 02:25:56,479 initially we set it to an empty string 3853 02:25:56,479 --> 02:25:59,040 in each iteration we get this and add it 3854 02:25:59,040 --> 02:26:02,800 to our output string so we type output 3855 02:26:02,800 --> 02:26:04,640 plus equals 3856 02:26:04,640 --> 02:26:06,240 this 3857 02:26:06,240 --> 02:26:08,880 and we should also add a space at the 3858 02:26:08,880 --> 02:26:09,840 end 3859 02:26:09,840 --> 02:26:11,760 so the words are not close to each other 3860 02:26:11,760 --> 02:26:12,720 okay 3861 02:26:12,720 --> 02:26:14,240 that's all we have to do 3862 02:26:14,240 --> 02:26:18,160 now finally let's print this output 3863 02:26:18,160 --> 02:26:21,280 run our program so i'm going to type one 3864 02:26:21,280 --> 02:26:22,720 three four 3865 02:26:22,720 --> 02:26:26,160 five let's see what we get we get one 3866 02:26:26,160 --> 02:26:31,080 three four with an exclamation mark 3867 02:26:34,160 --> 02:26:35,520 in this tutorial i'm going to show you 3868 02:26:35,520 --> 02:26:37,120 something really cool that you can do 3869 02:26:37,120 --> 02:26:39,040 with dictionaries so here we have this 3870 02:26:39,040 --> 02:26:41,439 program we can type a message like good 3871 02:26:41,439 --> 02:26:42,560 morning 3872 02:26:42,560 --> 02:26:44,880 followed by a smiley face when we press 3873 02:26:44,880 --> 02:26:47,520 enter we get this beautiful smiley face 3874 02:26:47,520 --> 02:26:48,399 or 3875 02:26:48,399 --> 02:26:49,840 we can type 3876 02:26:49,840 --> 02:26:53,520 i am sad with the sad smiley face and it 3877 02:26:53,520 --> 02:26:56,240 gets translated to this beautiful emoji 3878 02:26:56,240 --> 02:26:58,319 so this is another application of using 3879 02:26:58,319 --> 02:27:00,479 dictionaries we have a dictionary that 3880 02:27:00,479 --> 02:27:04,319 maps these characters into smiley faces 3881 02:27:04,319 --> 02:27:06,000 so let's go ahead and build this program 3882 02:27:06,000 --> 02:27:07,439 together 3883 02:27:07,439 --> 02:27:09,600 we start by calling the input function 3884 02:27:09,600 --> 02:27:12,080 here we pass a greater than symbol as an 3885 02:27:12,080 --> 02:27:14,560 indicator for the user to type a message 3886 02:27:14,560 --> 02:27:17,520 we get that message store it here 3887 02:27:17,520 --> 02:27:19,920 as you know that is a string now we need 3888 02:27:19,920 --> 02:27:22,560 to split the string by a space 3889 02:27:22,560 --> 02:27:25,120 so if the user types good morning with a 3890 02:27:25,120 --> 02:27:27,600 smiley face we want to break this down 3891 02:27:27,600 --> 02:27:29,600 into three words good 3892 02:27:29,600 --> 02:27:30,399 morning 3893 02:27:30,399 --> 02:27:32,160 and smiley face 3894 02:27:32,160 --> 02:27:34,240 to do that we can call the split method 3895 02:27:34,240 --> 02:27:35,600 so 3896 02:27:35,600 --> 02:27:38,720 we call message dot split 3897 02:27:38,720 --> 02:27:40,160 and pass 3898 02:27:40,160 --> 02:27:43,359 a string with one space as the separator 3899 02:27:43,359 --> 02:27:45,200 what this method does is basically it 3900 02:27:45,200 --> 02:27:47,200 goes through the string and anywhere it 3901 02:27:47,200 --> 02:27:49,280 finds this character in this case a 3902 02:27:49,280 --> 02:27:51,680 space it uses that as a boundary to 3903 02:27:51,680 --> 02:27:55,200 separate this string into multiple words 3904 02:27:55,200 --> 02:27:56,960 and then it will return a list let me 3905 02:27:56,960 --> 02:27:59,760 show you so let's store that here words 3906 02:27:59,760 --> 02:28:01,439 and then print it 3907 02:28:01,439 --> 02:28:04,160 on the terminal 3908 02:28:04,160 --> 02:28:06,560 let's go ahead and run our program so if 3909 02:28:06,560 --> 02:28:10,000 we type good morning sunshine enter we 3910 02:28:10,000 --> 02:28:13,120 get a list with three items each item is 3911 02:28:13,120 --> 02:28:14,960 a string okay 3912 02:28:14,960 --> 02:28:15,840 now 3913 02:28:15,840 --> 02:28:17,600 back to our program we need to define a 3914 02:28:17,600 --> 02:28:20,000 dictionary for mapping special 3915 02:28:20,000 --> 02:28:22,800 characters like these two into a smiley 3916 02:28:22,800 --> 02:28:23,760 face 3917 02:28:23,760 --> 02:28:25,120 so 3918 02:28:25,120 --> 02:28:28,160 we can call that emojis 3919 02:28:28,160 --> 02:28:30,880 set it to curly braces here we add a key 3920 02:28:30,880 --> 02:28:32,560 value pair 3921 02:28:32,560 --> 02:28:35,120 a smiley face and we map that to 3922 02:28:35,120 --> 02:28:37,520 a string in the string we want to add an 3923 02:28:37,520 --> 02:28:38,560 emoji 3924 02:28:38,560 --> 02:28:39,920 if you're on a mac keyboard you can 3925 02:28:39,920 --> 02:28:41,520 press ctrl 3926 02:28:41,520 --> 02:28:43,760 command and space and this should bring 3927 02:28:43,760 --> 02:28:47,200 up this emoji box if you're on windows 3928 02:28:47,200 --> 02:28:48,880 honestly i really don't know how you can 3929 02:28:48,880 --> 02:28:50,560 do this but there must be applications 3930 02:28:50,560 --> 02:28:52,960 that do this for you so let's go ahead 3931 02:28:52,960 --> 02:28:54,720 and pick this 3932 02:28:54,720 --> 02:28:57,680 happy smiley face good now let's add 3933 02:28:57,680 --> 02:29:01,040 another key value pair for a sad face 3934 02:29:01,040 --> 02:29:03,760 we map this to 3935 02:29:03,760 --> 02:29:04,960 this 3936 02:29:04,960 --> 02:29:06,319 face here 3937 02:29:06,319 --> 02:29:07,200 good 3938 02:29:07,200 --> 02:29:10,399 so we have a list of words now we need 3939 02:29:10,399 --> 02:29:12,800 to loop through this list get each word 3940 02:29:12,800 --> 02:29:16,640 and potentially map it to an emoji so we 3941 02:29:16,640 --> 02:29:20,240 can write a for loop for word in words 3942 02:29:20,240 --> 02:29:22,160 now we want to go to our dictionary and 3943 02:29:22,160 --> 02:29:24,560 see if you have an item with this word 3944 02:29:24,560 --> 02:29:25,840 as the key 3945 02:29:25,840 --> 02:29:27,520 if you have an item with that key then 3946 02:29:27,520 --> 02:29:29,840 we'll get its value otherwise we want to 3947 02:29:29,840 --> 02:29:32,000 use the same word so if the user types 3948 02:29:32,000 --> 02:29:34,000 good morning we don't want to translate 3949 02:29:34,000 --> 02:29:36,000 those words we just want to return those 3950 02:29:36,000 --> 02:29:38,240 so that is the case where we use the get 3951 02:29:38,240 --> 02:29:41,359 method to supply a default value so we 3952 02:29:41,359 --> 02:29:43,200 want to get an item with the key 3953 02:29:43,200 --> 02:29:45,200 word and if we don't have an item with 3954 02:29:45,200 --> 02:29:48,160 that key we simply use that word as the 3955 02:29:48,160 --> 02:29:50,720 default value now similar to the 3956 02:29:50,720 --> 02:29:52,080 exercise that we did in the last 3957 02:29:52,080 --> 02:29:55,280 tutorial we need an output variable so 3958 02:29:55,280 --> 02:29:57,680 let's define that here output set it to 3959 02:29:57,680 --> 02:29:59,359 an empty string 3960 02:29:59,359 --> 02:30:02,000 now back to line 9 3961 02:30:02,000 --> 02:30:03,840 we get the return value of the get 3962 02:30:03,840 --> 02:30:06,800 method and add it to our output variable 3963 02:30:06,800 --> 02:30:08,479 so output 3964 02:30:08,479 --> 02:30:11,359 plus equals whatever we get from the get 3965 02:30:11,359 --> 02:30:14,080 method and finally we need to append a 3966 02:30:14,080 --> 02:30:16,160 space at the end 3967 02:30:16,160 --> 02:30:19,200 that's it we're done so let's print the 3968 02:30:19,200 --> 02:30:20,640 output 3969 02:30:20,640 --> 02:30:22,800 and run our program 3970 02:30:22,800 --> 02:30:25,840 i'm gonna type good morning sunshine 3971 02:30:25,840 --> 02:30:27,520 with a smiley face 3972 02:30:27,520 --> 02:30:30,160 we get that beautiful let's try a sad 3973 02:30:30,160 --> 02:30:32,800 face as well i am sad 3974 02:30:32,800 --> 02:30:34,319 too bad 3975 02:30:34,319 --> 02:30:36,720 beautiful so as you can see dictionaries 3976 02:30:36,720 --> 02:30:38,560 have a lot of use cases in real 3977 02:30:38,560 --> 02:30:41,200 applications 3978 02:30:44,720 --> 02:30:46,080 here's the solution from the last 3979 02:30:46,080 --> 02:30:48,240 tutorial where we created an emoji 3980 02:30:48,240 --> 02:30:50,800 converter now so far we've been writing 3981 02:30:50,800 --> 02:30:53,600 all our code right here in app.pi but as 3982 02:30:53,600 --> 02:30:55,520 our programs grow we need a better way 3983 02:30:55,520 --> 02:30:57,680 to organize our code we need to break up 3984 02:30:57,680 --> 02:30:59,920 our code into smaller more manageable 3985 02:30:59,920 --> 02:31:01,840 and more maintainable chunks which we 3986 02:31:01,840 --> 02:31:03,439 call functions 3987 02:31:03,439 --> 02:31:05,920 so a function is a container for a few 3988 02:31:05,920 --> 02:31:08,399 lines of code that perform a specific 3989 02:31:08,399 --> 02:31:10,720 task for example you have learned about 3990 02:31:10,720 --> 02:31:12,240 if you have the built-in functions in 3991 02:31:12,240 --> 02:31:14,479 python like print and 3992 02:31:14,479 --> 02:31:16,479 input each of these functions have a 3993 02:31:16,479 --> 02:31:18,399 purpose they know how to perform a 3994 02:31:18,399 --> 02:31:19,840 specific task 3995 02:31:19,840 --> 02:31:22,399 so when building large complex programs 3996 02:31:22,399 --> 02:31:24,560 we should break up our code into smaller 3997 02:31:24,560 --> 02:31:26,880 reusable chunks which we call functions 3998 02:31:26,880 --> 02:31:28,880 to better organize our code and that's 3999 02:31:28,880 --> 02:31:30,000 what i'm going to show you in this 4000 02:31:30,000 --> 02:31:32,800 tutorial so let's write a simple program 4001 02:31:32,800 --> 02:31:35,359 for printing a greeting message so we're 4002 02:31:35,359 --> 02:31:37,600 going to do a print 4003 02:31:37,600 --> 02:31:38,960 hi there 4004 02:31:38,960 --> 02:31:41,600 and let's add another message welcome 4005 02:31:41,600 --> 02:31:43,200 aboard 4006 02:31:43,200 --> 02:31:44,960 so here we have a simple program with 4007 02:31:44,960 --> 02:31:47,040 only two lines of code now let's say 4008 02:31:47,040 --> 02:31:48,800 these two lines potentially we're going 4009 02:31:48,800 --> 02:31:51,280 to need them in other programs so we can 4010 02:31:51,280 --> 02:31:53,920 put them in a function that we can reuse 4011 02:31:53,920 --> 02:31:56,560 let me show you how to do that so 4012 02:31:56,560 --> 02:31:57,680 we start 4013 02:31:57,680 --> 02:31:59,840 typing out def that is a reserved 4014 02:31:59,840 --> 02:32:02,000 keyword in python and it's short for 4015 02:32:02,000 --> 02:32:04,399 define when python interpreter sees this 4016 02:32:04,399 --> 02:32:06,720 it knows that we're defining a function 4017 02:32:06,720 --> 02:32:08,880 next we need to give our function a name 4018 02:32:08,880 --> 02:32:10,640 let's say greet 4019 02:32:10,640 --> 02:32:11,920 underline 4020 02:32:11,920 --> 02:32:14,000 user so all the best practices you learn 4021 02:32:14,000 --> 02:32:16,160 about naming your variables also apply 4022 02:32:16,160 --> 02:32:18,240 here which means you should name your 4023 02:32:18,240 --> 02:32:20,640 functions with lowercase characters if 4024 02:32:20,640 --> 02:32:22,080 there are multiple words you need to 4025 02:32:22,080 --> 02:32:24,560 separate them using an underscore and 4026 02:32:24,560 --> 02:32:27,120 you should always always always always 4027 02:32:27,120 --> 02:32:29,359 use meaningful and descriptive names for 4028 02:32:29,359 --> 02:32:30,560 your functions 4029 02:32:30,560 --> 02:32:32,640 so here we're defining a function called 4030 02:32:32,640 --> 02:32:35,120 createuser after that we need to add 4031 02:32:35,120 --> 02:32:38,319 parentheses followed by a colon now you 4032 02:32:38,319 --> 02:32:40,160 know that whenever we add a colon at the 4033 02:32:40,160 --> 02:32:41,840 end of a line we're telling python that 4034 02:32:41,840 --> 02:32:43,760 we're defining a block of code so when 4035 02:32:43,760 --> 02:32:46,640 we press enter the next line is indented 4036 02:32:46,640 --> 02:32:48,560 now any code that'd be right here will 4037 02:32:48,560 --> 02:32:50,399 belong to this function 4038 02:32:50,399 --> 02:32:51,359 so 4039 02:32:51,359 --> 02:32:53,040 i'm going to move these two lines these 4040 02:32:53,040 --> 02:32:54,880 two print statements inside of our 4041 02:32:54,880 --> 02:32:57,439 function we can simply press tab to 4042 02:32:57,439 --> 02:33:00,160 indent them beautiful also let's remove 4043 02:33:00,160 --> 02:33:03,359 this extra line break that is better so 4044 02:33:03,359 --> 02:33:04,640 these two lines 4045 02:33:04,640 --> 02:33:07,359 now are part of our greet user function 4046 02:33:07,359 --> 02:33:09,439 so whenever we call this function these 4047 02:33:09,439 --> 02:33:12,160 two lines will be executed okay now 4048 02:33:12,160 --> 02:33:15,040 let's remove indentation so here we are 4049 02:33:15,040 --> 02:33:16,560 outside of our function 4050 02:33:16,560 --> 02:33:18,640 let's do a print statement 4051 02:33:18,640 --> 02:33:20,880 and print start 4052 02:33:20,880 --> 02:33:22,800 after that let's call our function so 4053 02:33:22,800 --> 02:33:24,640 greet user 4054 02:33:24,640 --> 02:33:26,319 and finally let's do another print 4055 02:33:26,319 --> 02:33:27,920 statement 4056 02:33:27,920 --> 02:33:29,920 and print finish 4057 02:33:29,920 --> 02:33:31,359 now we have this little warning here 4058 02:33:31,359 --> 02:33:32,880 this underline 4059 02:33:32,880 --> 02:33:34,880 if you hover your mouse over this line 4060 02:33:34,880 --> 02:33:36,319 this tooltip is telling you that we 4061 02:33:36,319 --> 02:33:38,479 should add two blank lines after 4062 02:33:38,479 --> 02:33:40,240 function definition this is coming from 4063 02:33:40,240 --> 02:33:42,960 pep8 which is a document that defines 4064 02:33:42,960 --> 02:33:44,800 the best practices for formatting our 4065 02:33:44,800 --> 02:33:47,040 code we'll look at that in the future so 4066 02:33:47,040 --> 02:33:49,359 for now to make pycharm happy let's add 4067 02:33:49,359 --> 02:33:51,920 an extra line break after this function 4068 02:33:51,920 --> 02:33:53,920 so whenever we define a function we need 4069 02:33:53,920 --> 02:33:57,120 to add two line breaks after okay 4070 02:33:57,120 --> 02:33:58,080 so 4071 02:33:58,080 --> 02:34:00,840 now let's run our program and see what 4072 02:34:00,840 --> 02:34:02,880 happens all right so we get four 4073 02:34:02,880 --> 02:34:05,280 messages on the terminal first we get 4074 02:34:05,280 --> 02:34:07,600 the start message then we get the 4075 02:34:07,600 --> 02:34:09,600 messages coming from our create function 4076 02:34:09,600 --> 02:34:11,600 so hi there and welcome aboard and 4077 02:34:11,600 --> 02:34:14,479 finally we see the finished message 4078 02:34:14,479 --> 02:34:16,000 so let me explain the flow of our 4079 02:34:16,000 --> 02:34:18,640 program when python runs this code it 4080 02:34:18,640 --> 02:34:20,319 doesn't actually print these two 4081 02:34:20,319 --> 02:34:22,080 messages on the terminal because these 4082 02:34:22,080 --> 02:34:24,640 lines of code are inside of this 4083 02:34:24,640 --> 02:34:27,520 function so they will only get executed 4084 02:34:27,520 --> 02:34:29,120 if we call this function if we don't 4085 02:34:29,120 --> 02:34:31,359 call this function these lines will not 4086 02:34:31,359 --> 02:34:33,439 get executed okay 4087 02:34:33,439 --> 02:34:35,200 so the execution of our program actually 4088 02:34:35,200 --> 02:34:37,359 starts here the first message that we 4089 02:34:37,359 --> 02:34:40,080 see on the terminal is the start message 4090 02:34:40,080 --> 02:34:42,240 after that python sees that we're 4091 02:34:42,240 --> 02:34:44,560 calling the greet function so it will 4092 02:34:44,560 --> 02:34:47,200 jump over here and then execute these 4093 02:34:47,200 --> 02:34:48,479 two lines 4094 02:34:48,479 --> 02:34:49,920 then it will jump outside of this 4095 02:34:49,920 --> 02:34:51,920 function and continue the normal 4096 02:34:51,920 --> 02:34:54,080 execution of our program so it will 4097 02:34:54,080 --> 02:34:55,520 print the finished message on the 4098 02:34:55,520 --> 02:34:56,560 terminal 4099 02:34:56,560 --> 02:34:58,560 so this is how functions work 4100 02:34:58,560 --> 02:35:00,720 also note that the order of this code 4101 02:35:00,720 --> 02:35:02,880 matters so here we're calling the grid 4102 02:35:02,880 --> 02:35:05,920 function after we have defined it if you 4103 02:35:05,920 --> 02:35:07,760 try to call it before you define it you 4104 02:35:07,760 --> 02:35:09,760 will see an error for example 4105 02:35:09,760 --> 02:35:11,600 if we call 4106 02:35:11,600 --> 02:35:14,319 greet user function right here see we 4107 02:35:14,319 --> 02:35:16,720 immediately get this red underline which 4108 02:35:16,720 --> 02:35:19,760 says unresolved reference greet user 4109 02:35:19,760 --> 02:35:21,520 which basically means python doesn't 4110 02:35:21,520 --> 02:35:23,359 know what is this great user it's not 4111 02:35:23,359 --> 02:35:24,800 defined anywhere 4112 02:35:24,800 --> 02:35:27,760 so we always define our functions first 4113 02:35:27,760 --> 02:35:31,120 and then call that 4114 02:35:34,240 --> 02:35:35,760 here's the function we created in the 4115 02:35:35,760 --> 02:35:37,760 last tutorial now i got a question for 4116 02:35:37,760 --> 02:35:40,000 you what is the difference between 4117 02:35:40,000 --> 02:35:41,600 calling this function 4118 02:35:41,600 --> 02:35:44,160 and the print function of python 4119 02:35:44,160 --> 02:35:45,439 the difference is that the print 4120 02:35:45,439 --> 02:35:48,000 function takes some information in this 4121 02:35:48,000 --> 02:35:50,000 case the message that we want to print 4122 02:35:50,000 --> 02:35:52,080 but our grid function doesn't take any 4123 02:35:52,080 --> 02:35:54,479 information wouldn't that be nicer if 4124 02:35:54,479 --> 02:35:56,160 you could pass the name of the user here 4125 02:35:56,160 --> 02:35:58,399 and then instead of saying hi there we 4126 02:35:58,399 --> 02:36:00,399 could print the name of the user so in 4127 02:36:00,399 --> 02:36:02,080 this tutorial i'm going to show you how 4128 02:36:02,080 --> 02:36:04,080 to pass information to your functions 4129 02:36:04,080 --> 02:36:05,680 and that is pretty easy 4130 02:36:05,680 --> 02:36:06,479 so 4131 02:36:06,479 --> 02:36:08,720 back to the definition of our great user 4132 02:36:08,720 --> 02:36:10,000 function 4133 02:36:10,000 --> 02:36:12,000 inside of this parenthesis we can add 4134 02:36:12,000 --> 02:36:14,000 parameters these parameters are 4135 02:36:14,000 --> 02:36:16,479 placeholders for receiving information 4136 02:36:16,479 --> 02:36:18,720 for example we can add a name parameter 4137 02:36:18,720 --> 02:36:20,399 and with this we can pass the name of 4138 02:36:20,399 --> 02:36:23,520 the user when calling this function so 4139 02:36:23,520 --> 02:36:26,080 let's pass john here 4140 02:36:26,080 --> 02:36:27,760 now when we call this function and pass 4141 02:36:27,760 --> 02:36:30,160 this value this name parameter will be 4142 02:36:30,160 --> 02:36:32,000 set to john 4143 02:36:32,000 --> 02:36:34,000 so it will act like a local variable 4144 02:36:34,000 --> 02:36:36,479 that we define inside of this function 4145 02:36:36,479 --> 02:36:38,399 just imagine we didn't have this 4146 02:36:38,399 --> 02:36:41,040 parameter and instead we had a variable 4147 02:36:41,040 --> 02:36:43,840 like name that was set to john now here 4148 02:36:43,840 --> 02:36:45,680 we could simply print the name of this 4149 02:36:45,680 --> 02:36:48,479 user so we could use a formatted string 4150 02:36:48,479 --> 02:36:51,439 and print the value of the name variable 4151 02:36:51,439 --> 02:36:54,160 right now we don't really have this 4152 02:36:54,160 --> 02:36:56,240 local variable defined in this function 4153 02:36:56,240 --> 02:36:58,160 we have a parameter 4154 02:36:58,160 --> 02:37:00,240 it's exactly the same thing so with 4155 02:37:00,240 --> 02:37:01,600 these parameters we can receive 4156 02:37:01,600 --> 02:37:03,760 information in our functions now let's 4157 02:37:03,760 --> 02:37:07,200 go ahead and run this program 4158 02:37:07,280 --> 02:37:10,640 so take a look we get hi john welcome 4159 02:37:10,640 --> 02:37:12,399 aboard we can take this to the next 4160 02:37:12,399 --> 02:37:14,720 level now we can call this function one 4161 02:37:14,720 --> 02:37:15,920 more time 4162 02:37:15,920 --> 02:37:18,560 so greet user and this time supply a 4163 02:37:18,560 --> 02:37:20,800 different name let's say mary 4164 02:37:20,800 --> 02:37:23,280 so we are reusing our grid function and 4165 02:37:23,280 --> 02:37:25,359 passing it a different value 4166 02:37:25,359 --> 02:37:27,280 imagine if we didn't have this function 4167 02:37:27,280 --> 02:37:29,760 we would have to repeat this line twice 4168 02:37:29,760 --> 02:37:32,000 once for high john and another time for 4169 02:37:32,000 --> 02:37:34,479 hi mary so this is the benefit of 4170 02:37:34,479 --> 02:37:36,479 defining functions we can take a few 4171 02:37:36,479 --> 02:37:38,080 lines of code that have a specific 4172 02:37:38,080 --> 02:37:40,240 purpose and put them inside of a 4173 02:37:40,240 --> 02:37:41,200 function 4174 02:37:41,200 --> 02:37:42,800 so now let's run our program one more 4175 02:37:42,800 --> 02:37:43,840 time 4176 02:37:43,840 --> 02:37:46,479 there you go so we get hi john welcome 4177 02:37:46,479 --> 02:37:48,720 board then we get high mary welcome 4178 02:37:48,720 --> 02:37:50,160 aboard 4179 02:37:50,160 --> 02:37:51,760 now note that when a function has a 4180 02:37:51,760 --> 02:37:54,560 parameter we are obligated to pass a 4181 02:37:54,560 --> 02:37:57,200 value for that parameter so if we remove 4182 02:37:57,200 --> 02:37:58,319 john 4183 02:37:58,319 --> 02:37:59,680 from here 4184 02:37:59,680 --> 02:38:02,319 and run our program we get this type 4185 02:38:02,319 --> 02:38:06,319 error grid user missing one required 4186 02:38:06,319 --> 02:38:08,479 positional argument 4187 02:38:08,479 --> 02:38:10,240 argument in programming is the value 4188 02:38:10,240 --> 02:38:13,120 that we supply to a function so 4189 02:38:13,120 --> 02:38:14,800 back to this example 4190 02:38:14,800 --> 02:38:17,600 mary in this case is an argument that we 4191 02:38:17,600 --> 02:38:20,399 pass to the name parameter a lot of 4192 02:38:20,399 --> 02:38:21,760 programmers don't know the difference 4193 02:38:21,760 --> 02:38:23,680 between parameters and arguments and 4194 02:38:23,680 --> 02:38:25,680 think they are the same but they are not 4195 02:38:25,680 --> 02:38:28,160 parameters or the holes or placeholders 4196 02:38:28,160 --> 02:38:30,080 that we define in our function for 4197 02:38:30,080 --> 02:38:31,840 receiving information 4198 02:38:31,840 --> 02:38:33,760 arguments are the actual pieces of 4199 02:38:33,760 --> 02:38:35,520 information that we supply to these 4200 02:38:35,520 --> 02:38:37,120 functions okay 4201 02:38:37,120 --> 02:38:39,840 so let's bring back 4202 02:38:39,840 --> 02:38:43,359 this john string okay now we can also 4203 02:38:43,359 --> 02:38:45,840 define multiple parameters for example 4204 02:38:45,840 --> 02:38:47,439 let's rename this parameter to first 4205 02:38:47,439 --> 02:38:49,600 name as i told you before you can right 4206 02:38:49,600 --> 02:38:52,319 click here go to refactor and then 4207 02:38:52,319 --> 02:38:54,720 rename or preferably you can use a 4208 02:38:54,720 --> 02:38:57,439 shortcut so the shortcut for this on mac 4209 02:38:57,439 --> 02:39:00,240 is shift and f6 4210 02:39:00,240 --> 02:39:03,040 and this brings up the rename dialog box 4211 02:39:03,040 --> 02:39:04,800 here we can change this to first 4212 02:39:04,800 --> 02:39:06,800 underline name 4213 02:39:06,800 --> 02:39:09,359 now we can add a second parameter so 4214 02:39:09,359 --> 02:39:10,479 comma 4215 02:39:10,479 --> 02:39:13,840 followed by last underline name 4216 02:39:13,840 --> 02:39:15,439 so back to our formatted string let's 4217 02:39:15,439 --> 02:39:18,800 also print the last name beautiful so 4218 02:39:18,800 --> 02:39:20,800 here when calling this function then we 4219 02:39:20,800 --> 02:39:22,000 can supply 4220 02:39:22,000 --> 02:39:23,520 another argument 4221 02:39:23,520 --> 02:39:26,880 so john smith and let's delete 4222 02:39:26,880 --> 02:39:28,000 this line we don't really need it 4223 02:39:28,000 --> 02:39:31,680 anymore run our program take a look hi 4224 02:39:31,680 --> 02:39:35,240 john smith 4225 02:39:38,240 --> 02:39:39,680 so in the last tutorial you'll learn 4226 02:39:39,680 --> 02:39:42,000 that whenever we define parameters for 4227 02:39:42,000 --> 02:39:44,160 our functions we should always supply 4228 02:39:44,160 --> 02:39:46,640 values otherwise we'll get an error 4229 02:39:46,640 --> 02:39:48,319 let's look at that there one more time 4230 02:39:48,319 --> 02:39:50,160 so i'm going to delete the second 4231 02:39:50,160 --> 02:39:53,120 argument and only pass john 4232 02:39:53,120 --> 02:39:54,720 run our program 4233 02:39:54,720 --> 02:39:56,160 we get this message 4234 02:39:56,160 --> 02:39:59,920 greet user missing one required 4235 02:39:59,920 --> 02:40:02,000 positional argument 4236 02:40:02,000 --> 02:40:03,600 so these are arguments that we have here 4237 02:40:03,600 --> 02:40:07,439 like john and smith we refer to these as 4238 02:40:07,439 --> 02:40:09,439 positional arguments which means their 4239 02:40:09,439 --> 02:40:12,800 position or order matters in other words 4240 02:40:12,800 --> 02:40:15,040 the first argument we add here is the 4241 02:40:15,040 --> 02:40:17,200 value for the first parameter and the 4242 02:40:17,200 --> 02:40:19,439 second argument is the value for the 4243 02:40:19,439 --> 02:40:21,520 second parameter so if we change their 4244 02:40:21,520 --> 02:40:24,000 position or order we'll get different 4245 02:40:24,000 --> 02:40:26,479 result let's take a look let's pass 4246 02:40:26,479 --> 02:40:28,800 smith first and then 4247 02:40:28,800 --> 02:40:30,800 john okay 4248 02:40:30,800 --> 02:40:34,399 delete that's good let's go with this 4249 02:40:34,399 --> 02:40:36,399 so instead of getting high john smith we 4250 02:40:36,399 --> 02:40:38,640 get high smith john 4251 02:40:38,640 --> 02:40:40,800 so that is the reason we refer to these 4252 02:40:40,800 --> 02:40:43,439 arguments as positional arguments their 4253 02:40:43,439 --> 02:40:46,160 position or order matters but in python 4254 02:40:46,160 --> 02:40:48,479 we also have keyword arguments and with 4255 02:40:48,479 --> 02:40:50,399 those the position doesn't matter let me 4256 02:40:50,399 --> 02:40:53,120 show you how they work so let's say here 4257 02:40:53,120 --> 02:40:55,760 we want to pass john as the value for 4258 02:40:55,760 --> 02:40:58,399 the first name parameter we can simply 4259 02:40:58,399 --> 02:41:01,439 specify that here so first name equals 4260 02:41:01,439 --> 02:41:02,720 john 4261 02:41:02,720 --> 02:41:04,560 now what we have here is a keyword 4262 02:41:04,560 --> 02:41:05,600 argument 4263 02:41:05,600 --> 02:41:07,279 so this combination of having the 4264 02:41:07,279 --> 02:41:09,600 parameter name followed by its value is 4265 02:41:09,600 --> 02:41:11,920 what we call a keyword argument and with 4266 02:41:11,920 --> 02:41:13,520 these keyword arguments the position 4267 02:41:13,520 --> 02:41:14,960 doesn't really matter 4268 02:41:14,960 --> 02:41:17,359 so here we're supplying john as an 4269 02:41:17,359 --> 02:41:19,680 argument for the first parameter 4270 02:41:19,680 --> 02:41:21,359 now similarly we can use a keyword 4271 02:41:21,359 --> 02:41:24,240 argument for smith let me show you so 4272 02:41:24,240 --> 02:41:26,880 last name equals smith's 4273 02:41:26,880 --> 02:41:29,520 so now we have two keyword arguments so 4274 02:41:29,520 --> 02:41:31,200 with these keyword arguments we don't 4275 02:41:31,200 --> 02:41:33,120 have to worry about the order of the 4276 02:41:33,120 --> 02:41:34,880 parameters that doesn't mean we should 4277 02:41:34,880 --> 02:41:37,439 always use keyword arguments not really 4278 02:41:37,439 --> 02:41:39,359 most of the time we use positional 4279 02:41:39,359 --> 02:41:41,600 arguments but in certain situations 4280 02:41:41,600 --> 02:41:44,080 these keyword arguments help us improve 4281 02:41:44,080 --> 02:41:46,160 the readability of our code 4282 02:41:46,160 --> 02:41:48,960 so on line 7 it's quite clear that we're 4283 02:41:48,960 --> 02:41:51,359 supplying smith as the last name and 4284 02:41:51,359 --> 02:41:53,520 john as the first name 4285 02:41:53,520 --> 02:41:55,439 now in this particular example even if 4286 02:41:55,439 --> 02:41:57,439 we didn't have the keyword arguments 4287 02:41:57,439 --> 02:41:59,359 just passing two positional arguments 4288 02:41:59,359 --> 02:42:02,560 like john and smith would be sufficient 4289 02:42:02,560 --> 02:42:04,720 because it's easy to guess what is the 4290 02:42:04,720 --> 02:42:07,120 purpose of this argument but sometimes 4291 02:42:07,120 --> 02:42:08,560 you're dealing with functions and you're 4292 02:42:08,560 --> 02:42:11,040 passing numerical values to them in 4293 02:42:11,040 --> 02:42:13,040 those situations it may not be quite 4294 02:42:13,040 --> 02:42:15,520 clear what these values represent let me 4295 02:42:15,520 --> 02:42:17,279 show you an example let's say we have a 4296 02:42:17,279 --> 02:42:19,600 function for calculating the total cost 4297 02:42:19,600 --> 02:42:21,840 of an order so we can call that 4298 02:42:21,840 --> 02:42:23,920 calculate underlink cost 4299 02:42:23,920 --> 02:42:25,520 so when calling this function we need to 4300 02:42:25,520 --> 02:42:28,080 supply three pieces of information 4301 02:42:28,080 --> 02:42:30,240 one is the total cost of all the order 4302 02:42:30,240 --> 02:42:33,279 items let's say fifty dollars 4303 02:42:33,279 --> 02:42:36,080 the other is the shipping cost let's say 4304 02:42:36,080 --> 02:42:38,560 five dollars and the last piece is a 4305 02:42:38,560 --> 02:42:42,000 discount let's say 0.1 4306 02:42:42,000 --> 02:42:43,840 someone reading this code may not be 4307 02:42:43,840 --> 02:42:46,640 sure what these three values represent 4308 02:42:46,640 --> 02:42:48,319 in this case we can improve the 4309 02:42:48,319 --> 02:42:50,319 readability of this code by using 4310 02:42:50,319 --> 02:42:53,279 keyword arguments so we can prefix each 4311 02:42:53,279 --> 02:42:54,880 value with the name of the parameter 4312 02:42:54,880 --> 02:42:58,160 they target for example we can set total 4313 02:42:58,160 --> 02:43:02,479 to 50 and then shipping to 5 and 4314 02:43:02,479 --> 02:43:05,600 discount to 0.1 4315 02:43:05,600 --> 02:43:07,840 now if we give this code to someone else 4316 02:43:07,840 --> 02:43:10,080 they can immediately tell what these 4317 02:43:10,080 --> 02:43:12,479 values represent so that is the power of 4318 02:43:12,479 --> 02:43:14,640 keyword arguments so here's what i want 4319 02:43:14,640 --> 02:43:17,760 you to take away for the most part use 4320 02:43:17,760 --> 02:43:19,600 positional arguments but if you're 4321 02:43:19,600 --> 02:43:21,359 dealing with functions that take 4322 02:43:21,359 --> 02:43:23,760 numerical values see if you can improve 4323 02:43:23,760 --> 02:43:26,000 the readability of your code by using 4324 02:43:26,000 --> 02:43:28,960 keyword arguments you simply prefix the 4325 02:43:28,960 --> 02:43:30,479 arguments that you pass with the name of 4326 02:43:30,479 --> 02:43:32,560 their parameters and this will increase 4327 02:43:32,560 --> 02:43:34,800 the readability of your code now there 4328 02:43:34,800 --> 02:43:36,240 is one more thing you need to know about 4329 02:43:36,240 --> 02:43:38,319 keyword arguments these keyword 4330 02:43:38,319 --> 02:43:40,720 arguments should always come after 4331 02:43:40,720 --> 02:43:42,800 positional arguments let me show you 4332 02:43:42,800 --> 02:43:44,560 what i mean so 4333 02:43:44,560 --> 02:43:47,279 back to our simple create user function 4334 02:43:47,279 --> 02:43:49,359 let's use a keyword argument for the 4335 02:43:49,359 --> 02:43:50,399 first name 4336 02:43:50,399 --> 02:43:51,600 so 4337 02:43:51,600 --> 02:43:53,279 let's set first name to john and see 4338 02:43:53,279 --> 02:43:55,359 what happens look we immediately get 4339 02:43:55,359 --> 02:43:57,840 this error take a look positional 4340 02:43:57,840 --> 02:44:01,040 argument after keyword argument so when 4341 02:44:01,040 --> 02:44:03,200 calling this function we're supplying 4342 02:44:03,200 --> 02:44:05,359 one keyword argument and one positional 4343 02:44:05,359 --> 02:44:07,920 argument and python doesn't like this 4344 02:44:07,920 --> 02:44:09,520 so if you're mixing positional and 4345 02:44:09,520 --> 02:44:12,000 keyword arguments you should always use 4346 02:44:12,000 --> 02:44:14,880 the positional arguments first and then 4347 02:44:14,880 --> 02:44:16,560 the keyboard arguments 4348 02:44:16,560 --> 02:44:19,359 in other words let's remove this keyword 4349 02:44:19,359 --> 02:44:20,720 argument here 4350 02:44:20,720 --> 02:44:23,520 so john is now a positional argument is 4351 02:44:23,520 --> 02:44:26,240 targeting the first parameter right now 4352 02:44:26,240 --> 02:44:28,000 after this position argument we can 4353 02:44:28,000 --> 02:44:30,640 supply any number of keyword arguments 4354 02:44:30,640 --> 02:44:33,359 so if we set last name to smith look 4355 02:44:33,359 --> 02:44:34,880 python doesn't have any problem with 4356 02:44:34,880 --> 02:44:36,880 this because we're adding a keyword 4357 02:44:36,880 --> 02:44:40,399 argument after a positional argument so 4358 02:44:40,399 --> 02:44:42,560 let's quickly recap for the most part 4359 02:44:42,560 --> 02:44:44,560 use positional arguments if you're 4360 02:44:44,560 --> 02:44:46,080 dealing with functions that take 4361 02:44:46,080 --> 02:44:48,479 multiple numerical values and it's not 4362 02:44:48,479 --> 02:44:50,800 quite clear what those values represent 4363 02:44:50,800 --> 02:44:53,439 use keyword arguments to improve the 4364 02:44:53,439 --> 02:44:56,000 readability of your code and finally if 4365 02:44:56,000 --> 02:44:58,080 you're passing both positional and 4366 02:44:58,080 --> 02:45:00,080 keyboard arguments use the keyword 4367 02:45:00,080 --> 02:45:04,720 arguments after the positional arguments 4368 02:45:08,240 --> 02:45:09,760 so far you have learned how to create 4369 02:45:09,760 --> 02:45:11,920 functions and send them information 4370 02:45:11,920 --> 02:45:13,520 using their parameters 4371 02:45:13,520 --> 02:45:14,960 in this tutorial i'm going to show you 4372 02:45:14,960 --> 02:45:16,800 how to create functions that return 4373 02:45:16,800 --> 02:45:19,279 values this is particularly useful if 4374 02:45:19,279 --> 02:45:20,720 you're doing some kind of calculation 4375 02:45:20,720 --> 02:45:22,399 your function and you want to return the 4376 02:45:22,399 --> 02:45:24,800 result to whoever is using your function 4377 02:45:24,800 --> 02:45:26,640 let me show you so 4378 02:45:26,640 --> 02:45:28,960 let's start by defining a function 4379 02:45:28,960 --> 02:45:31,680 called square this function simply 4380 02:45:31,680 --> 02:45:33,840 calculates the square of a number 4381 02:45:33,840 --> 02:45:35,920 so we need to add one parameter here 4382 02:45:35,920 --> 02:45:38,080 that is our number 4383 02:45:38,080 --> 02:45:40,640 now here we want to calculate number 4384 02:45:40,640 --> 02:45:43,600 times number that is the square of a 4385 02:45:43,600 --> 02:45:44,720 number 4386 02:45:44,720 --> 02:45:46,960 now to return this number outside of 4387 02:45:46,960 --> 02:45:48,880 this function we simply use the return 4388 02:45:48,880 --> 02:45:51,600 statement so return 4389 02:45:51,600 --> 02:45:53,760 now our square function is very simple 4390 02:45:53,760 --> 02:45:56,399 it only has one line of code but in a 4391 02:45:56,399 --> 02:45:57,920 real program you might have a function 4392 02:45:57,920 --> 02:46:00,720 that has 10 lines of code to calculate 4393 02:46:00,720 --> 02:46:02,479 something so 4394 02:46:02,479 --> 02:46:04,640 this return statement is very valuable 4395 02:46:04,640 --> 02:46:06,640 with that we can return values to the 4396 02:46:06,640 --> 02:46:08,800 colors of our function 4397 02:46:08,800 --> 02:46:11,439 so with this when we call 4398 02:46:11,439 --> 02:46:13,600 the square function let's say we give it 4399 02:46:13,600 --> 02:46:14,479 3 4400 02:46:14,479 --> 02:46:16,880 now this function returns a value just 4401 02:46:16,880 --> 02:46:19,760 like the input function of python this 4402 02:46:19,760 --> 02:46:21,600 input function waits for the user to 4403 02:46:21,600 --> 02:46:24,080 type something and press enter and then 4404 02:46:24,080 --> 02:46:26,399 it will return the user's input as a 4405 02:46:26,399 --> 02:46:29,200 string so we can get it and store it 4406 02:46:29,200 --> 02:46:31,359 in a variable like this right we have 4407 02:46:31,359 --> 02:46:33,920 the same story with our square function 4408 02:46:33,920 --> 02:46:36,640 so our square function returns a value 4409 02:46:36,640 --> 02:46:38,560 we can simply get it and store it in a 4410 02:46:38,560 --> 02:46:42,319 variable let's call that result 4411 02:46:42,319 --> 02:46:44,240 now we can print 4412 02:46:44,240 --> 02:46:46,640 result on the terminal 4413 02:46:46,640 --> 02:46:49,279 there you go so result is nine we could 4414 02:46:49,279 --> 02:46:50,399 also 4415 02:46:50,399 --> 02:46:53,359 pass this function call directly inside 4416 02:46:53,359 --> 02:46:55,760 of the print function without defining a 4417 02:46:55,760 --> 02:46:57,439 separate variable sometimes this is 4418 02:46:57,439 --> 02:47:00,240 useful for writing shorter code so in 4419 02:47:00,240 --> 02:47:01,520 this case we don't really need this 4420 02:47:01,520 --> 02:47:04,319 result variable we simply want to print 4421 02:47:04,319 --> 02:47:06,399 the square of three 4422 02:47:06,399 --> 02:47:08,560 so we could simply pass this function 4423 02:47:08,560 --> 02:47:09,680 call 4424 02:47:09,680 --> 02:47:12,800 right here as an argument for the print 4425 02:47:12,800 --> 02:47:13,840 function 4426 02:47:13,840 --> 02:47:15,920 so let's delete this variable now our 4427 02:47:15,920 --> 02:47:17,920 code is shorter so with this we're 4428 02:47:17,920 --> 02:47:20,319 calling the square function this returns 4429 02:47:20,319 --> 02:47:22,560 a value and we'll pass that value as an 4430 02:47:22,560 --> 02:47:25,040 argument to the print function 4431 02:47:25,040 --> 02:47:25,840 okay 4432 02:47:25,840 --> 02:47:27,520 when we run this program we get the 4433 02:47:27,520 --> 02:47:30,880 exact same result there you go 4434 02:47:30,880 --> 02:47:32,560 now what happens if we don't use a 4435 02:47:32,560 --> 02:47:35,120 return statement in our function so in 4436 02:47:35,120 --> 02:47:37,279 the last example we simply used a print 4437 02:47:37,279 --> 02:47:39,760 statement let's temporarily 4438 02:47:39,760 --> 02:47:42,560 remove this return statement and 4439 02:47:42,560 --> 02:47:45,840 print the result on the terminal like 4440 02:47:45,840 --> 02:47:46,800 this 4441 02:47:46,800 --> 02:47:48,720 so let's run our program one more time 4442 02:47:48,720 --> 02:47:50,960 and see what we get we get two things on 4443 02:47:50,960 --> 02:47:54,880 a terminal the number nine and none what 4444 02:47:54,880 --> 02:47:56,319 is happening here 4445 02:47:56,319 --> 02:47:58,479 well when python interpreter executes 4446 02:47:58,479 --> 02:48:01,439 this code first it will call the square 4447 02:48:01,439 --> 02:48:02,479 function 4448 02:48:02,479 --> 02:48:03,920 so the control moves to the square 4449 02:48:03,920 --> 02:48:06,319 function here we calculate the square of 4450 02:48:06,319 --> 02:48:08,640 this given number and then print it on 4451 02:48:08,640 --> 02:48:12,080 the terminal that is the reason we see 9 4452 02:48:12,080 --> 02:48:13,359 right here 4453 02:48:13,359 --> 02:48:16,240 now by default all functions return the 4454 02:48:16,240 --> 02:48:17,920 value none 4455 02:48:17,920 --> 02:48:20,160 so if we don't have a return statement 4456 02:48:20,160 --> 02:48:21,040 here 4457 02:48:21,040 --> 02:48:23,600 by default python returns 4458 02:48:23,600 --> 02:48:26,000 none none is an object that represents 4459 02:48:26,000 --> 02:48:28,160 the absence of a value it's like nothing 4460 02:48:28,160 --> 02:48:31,279 or null in c c plus plus java and 4461 02:48:31,279 --> 02:48:32,640 javascript 4462 02:48:32,640 --> 02:48:33,359 so 4463 02:48:33,359 --> 02:48:35,359 in this example we didn't have a return 4464 02:48:35,359 --> 02:48:38,880 statement and by default python returned 4465 02:48:38,880 --> 02:48:40,560 none from this function 4466 02:48:40,560 --> 02:48:43,600 so after the square function is executed 4467 02:48:43,600 --> 02:48:46,000 the value none is returned and passed as 4468 02:48:46,000 --> 02:48:48,080 an argument to the print function that 4469 02:48:48,080 --> 02:48:49,520 is the reason we see 4470 02:48:49,520 --> 02:48:51,439 none on the second line 4471 02:48:51,439 --> 02:48:52,880 so here are the two things you need to 4472 02:48:52,880 --> 02:48:55,359 take away from this tutorial by default 4473 02:48:55,359 --> 02:48:58,160 all functions in python return none you 4474 02:48:58,160 --> 02:48:59,680 can change that so if you have a 4475 02:48:59,680 --> 02:49:01,520 function that calculates something you 4476 02:49:01,520 --> 02:49:03,439 can return the result using the return 4477 02:49:03,439 --> 02:49:06,000 statement 4478 02:49:09,439 --> 02:49:11,279 here's the code for our emoji converter 4479 02:49:11,279 --> 02:49:13,120 program that we wrote earlier if you 4480 02:49:13,120 --> 02:49:14,880 missed that tutorial make sure to go 4481 02:49:14,880 --> 02:49:17,120 back and watch it as i told you before 4482 02:49:17,120 --> 02:49:19,279 there is a table of content right below 4483 02:49:19,279 --> 02:49:20,960 this video in the description so you can 4484 02:49:20,960 --> 02:49:22,720 simply click on the emoji converter 4485 02:49:22,720 --> 02:49:24,319 tutorial to watch it 4486 02:49:24,319 --> 02:49:26,880 now your exercise is to reorganize this 4487 02:49:26,880 --> 02:49:29,120 code using a function so i want you to 4488 02:49:29,120 --> 02:49:31,439 extract a function in this code because 4489 02:49:31,439 --> 02:49:34,080 this algorithm for converting these 4490 02:49:34,080 --> 02:49:36,399 smiley faces into emojis is something 4491 02:49:36,399 --> 02:49:37,840 that we probably want to use in a lot of 4492 02:49:37,840 --> 02:49:39,680 different applications you can use it in 4493 02:49:39,680 --> 02:49:41,439 a chat application in an email 4494 02:49:41,439 --> 02:49:43,120 application and so on we don't want to 4495 02:49:43,120 --> 02:49:45,040 write all this code every time we want 4496 02:49:45,040 --> 02:49:47,600 to convert these characters into smiley 4497 02:49:47,600 --> 02:49:48,640 faces 4498 02:49:48,640 --> 02:49:50,640 so out of all these lines of code that 4499 02:49:50,640 --> 02:49:54,240 we have from line 1 to line 10 the lines 4500 02:49:54,240 --> 02:49:57,760 2 to 9 belong to our algorithm 4501 02:49:57,760 --> 02:50:00,319 all these lines together implement this 4502 02:50:00,319 --> 02:50:01,279 feature 4503 02:50:01,279 --> 02:50:02,800 so you should put these lines in a 4504 02:50:02,800 --> 02:50:04,319 separate function 4505 02:50:04,319 --> 02:50:07,040 now why don't we have the input in that 4506 02:50:07,040 --> 02:50:09,520 function because the input can come in 4507 02:50:09,520 --> 02:50:11,279 different forms currently we're using 4508 02:50:11,279 --> 02:50:13,439 the input function to receive the input 4509 02:50:13,439 --> 02:50:15,359 from the terminal but in other 4510 02:50:15,359 --> 02:50:17,040 applications we might receive the input 4511 02:50:17,040 --> 02:50:19,600 from a graphical user interface so the 4512 02:50:19,600 --> 02:50:21,439 input can come in different forms we 4513 02:50:21,439 --> 02:50:22,880 don't want to put that in our function 4514 02:50:22,880 --> 02:50:25,359 because this line will not be reusable 4515 02:50:25,359 --> 02:50:27,359 we want to give our function 4516 02:50:27,359 --> 02:50:29,600 the actual message as a string we don't 4517 02:50:29,600 --> 02:50:32,240 care how we got that message in one 4518 02:50:32,240 --> 02:50:33,600 program we might get it from the 4519 02:50:33,600 --> 02:50:35,680 terminal in another program we may get 4520 02:50:35,680 --> 02:50:38,160 it from a graphical user interface 4521 02:50:38,160 --> 02:50:40,080 so that is the reason this line of code 4522 02:50:40,080 --> 02:50:42,000 should not be in that function by the 4523 02:50:42,000 --> 02:50:43,760 same token 4524 02:50:43,760 --> 02:50:45,359 you shouldn't include the last line in 4525 02:50:45,359 --> 02:50:47,120 that function because what we do with 4526 02:50:47,120 --> 02:50:49,120 the output is different from one program 4527 02:50:49,120 --> 02:50:50,960 to another in this program we're 4528 02:50:50,960 --> 02:50:53,040 printing the output in another program 4529 02:50:53,040 --> 02:50:55,680 we might send that output as an email or 4530 02:50:55,680 --> 02:50:58,240 as a response in a chat application so 4531 02:50:58,240 --> 02:51:00,000 as a general rule of thumb your 4532 02:51:00,000 --> 02:51:01,920 functions should not worry about 4533 02:51:01,920 --> 02:51:05,200 receiving input and printing it these 4534 02:51:05,200 --> 02:51:06,880 lines of code should not belong to your 4535 02:51:06,880 --> 02:51:09,040 functions so go ahead and reorganize 4536 02:51:09,040 --> 02:51:12,960 this code by extracting a function 4537 02:51:16,000 --> 02:51:17,359 all right on the top first we need to 4538 02:51:17,359 --> 02:51:20,960 define our function so we type def 4539 02:51:20,960 --> 02:51:23,439 give our function a name like emoji 4540 02:51:23,439 --> 02:51:26,160 underline converter so the name of this 4541 02:51:26,160 --> 02:51:28,800 function clearly tells me what it does 4542 02:51:28,800 --> 02:51:30,560 each function should be responsible for 4543 02:51:30,560 --> 02:51:33,760 one and only one task and that task 4544 02:51:33,760 --> 02:51:35,840 should be clearly explained in the name 4545 02:51:35,840 --> 02:51:37,680 of the function okay 4546 02:51:37,680 --> 02:51:40,080 so our emoji converter function should 4547 02:51:40,080 --> 02:51:42,479 take a parameter we call that message 4548 02:51:42,479 --> 02:51:44,319 that is a string right 4549 02:51:44,319 --> 02:51:45,600 colon 4550 02:51:45,600 --> 02:51:49,120 now we need to move the lines five 4551 02:51:49,120 --> 02:51:50,720 all the way 4552 02:51:50,720 --> 02:51:52,800 to the end of our four block 4553 02:51:52,800 --> 02:51:54,399 into that function so 4554 02:51:54,399 --> 02:51:56,240 let's select all these lines 4555 02:51:56,240 --> 02:51:58,720 we can cut them from here by pressing 4556 02:51:58,720 --> 02:52:01,760 command and x on mac or control and x on 4557 02:52:01,760 --> 02:52:02,960 windows 4558 02:52:02,960 --> 02:52:05,840 then we paste them here 4559 02:52:05,840 --> 02:52:07,200 as you can see these lines are 4560 02:52:07,200 --> 02:52:09,520 automatically indented so they are part 4561 02:52:09,520 --> 02:52:11,840 of the emoji converter function if this 4562 02:52:11,840 --> 02:52:13,920 didn't happen on your computer make sure 4563 02:52:13,920 --> 02:52:16,880 to select these lines and use the tab to 4564 02:52:16,880 --> 02:52:18,080 indent them 4565 02:52:18,080 --> 02:52:20,080 all right so here's our emoji converter 4566 02:52:20,080 --> 02:52:21,920 function beautiful 4567 02:52:21,920 --> 02:52:22,640 now 4568 02:52:22,640 --> 02:52:24,399 after that 4569 02:52:24,399 --> 02:52:26,720 in the main flow of our program first we 4570 02:52:26,720 --> 02:52:28,399 get the input from the user 4571 02:52:28,399 --> 02:52:30,880 we store it in this variable message 4572 02:52:30,880 --> 02:52:32,160 and then 4573 02:52:32,160 --> 02:52:33,120 we call 4574 02:52:33,120 --> 02:52:36,479 emoji converter and pass this message 4575 02:52:36,479 --> 02:52:38,880 this function should return a value that 4576 02:52:38,880 --> 02:52:40,640 is what we calculate 4577 02:52:40,640 --> 02:52:43,200 at the end of this function so right 4578 02:52:43,200 --> 02:52:45,200 after this 4 block we need to add a 4579 02:52:45,200 --> 02:52:49,279 return statement return output 4580 02:52:49,279 --> 02:52:51,439 now that our function returns a value 4581 02:52:51,439 --> 02:52:53,279 we can get it and store it in a separate 4582 02:52:53,279 --> 02:52:57,279 variable let's call that result and then 4583 02:52:57,279 --> 02:52:59,760 we can simply print that here 4584 02:52:59,760 --> 02:53:01,600 or we can make this code shorter we 4585 02:53:01,600 --> 02:53:03,520 don't really need this variable here so 4586 02:53:03,520 --> 02:53:05,040 we can simply 4587 02:53:05,040 --> 02:53:06,880 call the emoji converter 4588 02:53:06,880 --> 02:53:09,040 and print the result 4589 02:53:09,040 --> 02:53:11,840 like this now here we have another line 4590 02:53:11,840 --> 02:53:12,960 because 4591 02:53:12,960 --> 02:53:14,960 as i told you before we need two blank 4592 02:53:14,960 --> 02:53:18,240 lines after our function definitions so 4593 02:53:18,240 --> 02:53:20,000 currently we have only one black line we 4594 02:53:20,000 --> 02:53:21,040 need to add 4595 02:53:21,040 --> 02:53:23,840 another black line 4596 02:53:23,840 --> 02:53:25,520 let's go ahead and run this program to 4597 02:53:25,520 --> 02:53:27,920 make sure everything works as before 4598 02:53:27,920 --> 02:53:30,399 so i'm going to type a message like good 4599 02:53:30,399 --> 02:53:32,640 morning with a smiley face 4600 02:53:32,640 --> 02:53:35,600 good let's run it one more time 4601 02:53:35,600 --> 02:53:39,439 i'm sad too bad okay beautiful 4602 02:53:39,439 --> 02:53:41,680 so here's the final 4603 02:53:41,680 --> 02:53:44,319 result we defined this function called 4604 02:53:44,319 --> 02:53:46,479 emoji converter this function takes a 4605 02:53:46,479 --> 02:53:48,240 parameter called message 4606 02:53:48,240 --> 02:53:52,840 and finally returns the output 4607 02:53:57,120 --> 02:53:58,560 in this tutorial i'm going to teach you 4608 02:53:58,560 --> 02:54:00,960 guys how to handle errors in your python 4609 02:54:00,960 --> 02:54:03,439 programs so let's start by writing a 4610 02:54:03,439 --> 02:54:05,600 small program to get the user's age from 4611 02:54:05,600 --> 02:54:07,680 the terminal so we call the input 4612 02:54:07,680 --> 02:54:08,960 function 4613 02:54:08,960 --> 02:54:10,640 input 4614 02:54:10,640 --> 02:54:12,080 add a label here 4615 02:54:12,080 --> 02:54:13,439 like age 4616 02:54:13,439 --> 02:54:15,760 this returns a string so we need to pass 4617 02:54:15,760 --> 02:54:17,600 it to the end function 4618 02:54:17,600 --> 02:54:19,680 and then store the result 4619 02:54:19,680 --> 02:54:21,439 in a variable called h 4620 02:54:21,439 --> 02:54:23,760 now let's print h to make sure we have 4621 02:54:23,760 --> 02:54:25,920 implemented everything properly 4622 02:54:25,920 --> 02:54:27,600 run our program 4623 02:54:27,600 --> 02:54:30,399 we type 20 4624 02:54:30,399 --> 02:54:32,640 20 is printed here beautiful now look at 4625 02:54:32,640 --> 02:54:35,359 the exit code of this program exit code 4626 02:54:35,359 --> 02:54:37,279 0 means our program terminated 4627 02:54:37,279 --> 02:54:39,520 successfully there were no errors but 4628 02:54:39,520 --> 02:54:41,520 what if we run this program 4629 02:54:41,520 --> 02:54:44,000 one more time and instead of entering a 4630 02:54:44,000 --> 02:54:46,880 numerical value we enter something like 4631 02:54:46,880 --> 02:54:48,640 asd 4632 02:54:48,640 --> 02:54:50,319 we get a value error 4633 02:54:50,319 --> 02:54:52,800 with this message invalid literal for 4634 02:54:52,800 --> 02:54:55,760 end with base 10. basically what this 4635 02:54:55,760 --> 02:54:58,000 message is telling us is that this 4636 02:54:58,000 --> 02:55:00,800 string asd does not contain a valid 4637 02:55:00,800 --> 02:55:02,880 whole number that can be converted to an 4638 02:55:02,880 --> 02:55:05,040 integer now look at the exit code of 4639 02:55:05,040 --> 02:55:07,279 this program exit code one means our 4640 02:55:07,279 --> 02:55:09,920 program crashed so zero always means 4641 02:55:09,920 --> 02:55:12,160 success and anything but zero means 4642 02:55:12,160 --> 02:55:13,120 crash 4643 02:55:13,120 --> 02:55:14,880 now as a good python programmer you 4644 02:55:14,880 --> 02:55:16,880 should anticipate these situations you 4645 02:55:16,880 --> 02:55:18,240 don't want to let your entire program 4646 02:55:18,240 --> 02:55:20,880 crash just because the user entered an 4647 02:55:20,880 --> 02:55:22,319 invalid value 4648 02:55:22,319 --> 02:55:24,240 so instead of letting the program crash 4649 02:55:24,240 --> 02:55:26,080 you should handle this situation and 4650 02:55:26,080 --> 02:55:28,720 print a proper error message and that's 4651 02:55:28,720 --> 02:55:29,520 what i'm going to show you in this 4652 02:55:29,520 --> 02:55:30,479 tutorial 4653 02:55:30,479 --> 02:55:32,319 now if we're going any further 4654 02:55:32,319 --> 02:55:34,640 look at the type of the error we get 4655 02:55:34,640 --> 02:55:37,920 here value error so remember this we're 4656 02:55:37,920 --> 02:55:39,840 going to get back to this shortly 4657 02:55:39,840 --> 02:55:42,640 now how can we handle these errors 4658 02:55:42,640 --> 02:55:45,600 in python we have a construct called try 4659 02:55:45,600 --> 02:55:46,720 accept 4660 02:55:46,720 --> 02:55:49,040 we use that to handle errors let me show 4661 02:55:49,040 --> 02:55:51,439 you how it works so 4662 02:55:51,439 --> 02:55:54,800 on the top we type try colon 4663 02:55:54,800 --> 02:55:56,960 now as you know we're defining a code 4664 02:55:56,960 --> 02:56:00,319 block so i'm gonna move these two lines 4665 02:56:00,319 --> 02:56:02,880 inside of our try block so press tab 4666 02:56:02,880 --> 02:56:04,240 here 4667 02:56:04,240 --> 02:56:06,319 and remove this blank line 4668 02:56:06,319 --> 02:56:08,399 now these two lines are part of our try 4669 02:56:08,399 --> 02:56:09,920 block right 4670 02:56:09,920 --> 02:56:10,720 so 4671 02:56:10,720 --> 02:56:13,760 after try we type accept 4672 02:56:13,760 --> 02:56:15,920 and then we add the type of the error 4673 02:56:15,920 --> 02:56:18,000 that our program may encounter 4674 02:56:18,000 --> 02:56:20,880 that is value error 4675 02:56:20,880 --> 02:56:21,760 okay 4676 02:56:21,760 --> 02:56:23,439 then we add a colon 4677 02:56:23,439 --> 02:56:25,520 and in this block we can define what 4678 02:56:25,520 --> 02:56:28,240 should happen if our program encounters 4679 02:56:28,240 --> 02:56:31,040 an error of type value error 4680 02:56:31,040 --> 02:56:32,880 in this case we want to print a proper 4681 02:56:32,880 --> 02:56:35,840 error message so print 4682 02:56:35,840 --> 02:56:38,000 invalid value 4683 02:56:38,000 --> 02:56:40,319 so we just try and accept we're telling 4684 02:56:40,319 --> 02:56:43,040 python hey go ahead and try running 4685 02:56:43,040 --> 02:56:45,680 these two lines of code if you encounter 4686 02:56:45,680 --> 02:56:48,720 an error of type value error then 4687 02:56:48,720 --> 02:56:51,120 instead of crashing the program 4688 02:56:51,120 --> 02:56:53,359 print this error message on the terminal 4689 02:56:53,359 --> 02:56:55,279 so this is how this works 4690 02:56:55,279 --> 02:56:57,359 now more accurately in programming terms 4691 02:56:57,359 --> 02:56:58,960 we refer to this kind of error as an 4692 02:56:58,960 --> 02:57:01,120 exception so an exception is a kind of 4693 02:57:01,120 --> 02:57:04,000 error that crashes our program so if the 4694 02:57:04,000 --> 02:57:05,439 user enters 4695 02:57:05,439 --> 02:57:08,160 an invalid value this line of code line 4696 02:57:08,160 --> 02:57:11,520 two will raise an exception and on line 4697 02:57:11,520 --> 02:57:13,439 four we'll catch it 4698 02:57:13,439 --> 02:57:15,279 and print this error message so our 4699 02:57:15,279 --> 02:57:17,439 program will no longer crash let me show 4700 02:57:17,439 --> 02:57:21,840 you so let's go ahead and run this 4701 02:57:21,840 --> 02:57:23,359 age 20 4702 02:57:23,359 --> 02:57:25,359 everything works just like before and 4703 02:57:25,359 --> 02:57:28,000 the exit code is zero beautiful let's 4704 02:57:28,000 --> 02:57:30,160 rerun this program and 4705 02:57:30,160 --> 02:57:32,399 enter asd 4706 02:57:32,399 --> 02:57:34,160 so we get this proper error message 4707 02:57:34,160 --> 02:57:36,640 invalid value and once again the exit 4708 02:57:36,640 --> 02:57:38,880 code is zero so that means our program 4709 02:57:38,880 --> 02:57:42,000 completed successfully didn't crash 4710 02:57:42,000 --> 02:57:43,680 now we can also handle different kinds 4711 02:57:43,680 --> 02:57:46,399 of errors or exceptions for example 4712 02:57:46,399 --> 02:57:48,479 right after we get the age 4713 02:57:48,479 --> 02:57:51,120 let's define a variable income and set 4714 02:57:51,120 --> 02:57:53,040 it to 20 000 4715 02:57:53,040 --> 02:57:55,600 and then define another variable called 4716 02:57:55,600 --> 02:57:58,319 risk and set it to income 4717 02:57:58,319 --> 02:58:00,319 divided by h 4718 02:58:00,319 --> 02:58:02,720 let's run this program and enter 0 as 4719 02:58:02,720 --> 02:58:04,720 the age to see what happens 4720 02:58:04,720 --> 02:58:06,479 so 4721 02:58:06,479 --> 02:58:08,880 0 is obviously a numerical value so when 4722 02:58:08,880 --> 02:58:10,720 we enter this value technically we 4723 02:58:10,720 --> 02:58:12,640 shouldn't get any exceptions 4724 02:58:12,640 --> 02:58:14,399 take a look 4725 02:58:14,399 --> 02:58:16,960 our program crashed with a zero division 4726 02:58:16,960 --> 02:58:18,560 error because we cannot divide the 4727 02:58:18,560 --> 02:58:20,880 number by zero once again look at the 4728 02:58:20,880 --> 02:58:23,600 exit code so we couldn't catch this kind 4729 02:58:23,600 --> 02:58:26,319 of error or this kind of exception 4730 02:58:26,319 --> 02:58:28,720 with this except block this except block 4731 02:58:28,720 --> 02:58:32,160 is only caching exceptions of type value 4732 02:58:32,160 --> 02:58:34,160 error and this happens when we try to 4733 02:58:34,160 --> 02:58:36,720 convert a non-numerical value to an 4734 02:58:36,720 --> 02:58:37,840 integer 4735 02:58:37,840 --> 02:58:39,840 so in a situation like this we should 4736 02:58:39,840 --> 02:58:42,560 handle different kinds of exceptions 4737 02:58:42,560 --> 02:58:46,160 we can add another accept statement for 4738 02:58:46,160 --> 02:58:50,319 an exception of type zero division error 4739 02:58:50,319 --> 02:58:51,920 now in this block we can print a 4740 02:58:51,920 --> 02:58:53,840 different kind of message 4741 02:58:53,840 --> 02:58:56,399 like age cannot be 4742 02:58:56,399 --> 02:58:57,520 zero 4743 02:58:57,520 --> 02:58:59,680 let's run our program one more time 4744 02:58:59,680 --> 02:59:02,640 now i'm going to enter 0 as the age 4745 02:59:02,640 --> 02:59:05,200 so we get this error message h can not 4746 02:59:05,200 --> 02:59:08,080 be 0 and look at the exit code 4747 02:59:08,080 --> 02:59:11,520 it's 0 so our program didn't crash 4748 02:59:11,520 --> 02:59:14,560 so to recap we used try except blocks to 4749 02:59:14,560 --> 02:59:16,960 handle exceptions that are raised in our 4750 02:59:16,960 --> 02:59:19,279 programs as a good programmer you should 4751 02:59:19,279 --> 02:59:20,960 always anticipate this kind of 4752 02:59:20,960 --> 02:59:25,560 exceptions and handle them properly 4753 02:59:28,960 --> 02:59:30,240 in this tutorial i'm going to talk to 4754 02:59:30,240 --> 02:59:32,800 you guys about comments in python we use 4755 02:59:32,800 --> 02:59:35,279 comments to add notes or comments to our 4756 02:59:35,279 --> 02:59:37,439 programs for example here i'm going to 4757 02:59:37,439 --> 02:59:39,680 make a print statement 4758 02:59:39,680 --> 02:59:43,920 sky is blue now before that 4759 02:59:43,920 --> 02:59:46,800 i can add a comment using a height sign 4760 02:59:46,800 --> 02:59:48,720 and then whatever i type here 4761 02:59:48,720 --> 02:59:50,080 it's going to get ignored it's not going 4762 02:59:50,080 --> 02:59:53,040 to get executed let me show you so let's 4763 02:59:53,040 --> 02:59:55,200 run this program 4764 02:59:55,200 --> 02:59:57,279 look we only see this message sky is 4765 02:59:57,279 --> 02:59:58,560 blue 4766 02:59:58,560 --> 03:00:00,160 so with this comment we can explain 4767 03:00:00,160 --> 03:00:02,000 something about our code we can use that 4768 03:00:02,000 --> 03:00:04,800 as reminders to fix things or clean 4769 03:00:04,800 --> 03:00:06,399 things up or we can use it to 4770 03:00:06,399 --> 03:00:08,800 communicate things with other developers 4771 03:00:08,800 --> 03:00:10,720 reading our code why we have written 4772 03:00:10,720 --> 03:00:13,359 this code in a certain way these are 4773 03:00:13,359 --> 03:00:16,160 good use cases for using comments we can 4774 03:00:16,160 --> 03:00:18,640 also have comments over multiple lines 4775 03:00:18,640 --> 03:00:21,120 so we can simply add more comments 4776 03:00:21,120 --> 03:00:23,040 each line should start with a new high 4777 03:00:23,040 --> 03:00:25,200 sign now one thing you need to avoid 4778 03:00:25,200 --> 03:00:27,840 when using comments is explaining what 4779 03:00:27,840 --> 03:00:30,640 the code does so here's an example of a 4780 03:00:30,640 --> 03:00:32,880 bad comment let me just delete these two 4781 03:00:32,880 --> 03:00:34,080 lines 4782 03:00:34,080 --> 03:00:36,640 and with this comment i'm going to say 4783 03:00:36,640 --> 03:00:39,680 print sky is blue 4784 03:00:39,680 --> 03:00:41,439 why is this a bad comment because it's 4785 03:00:41,439 --> 03:00:43,439 telling me the obvious it is obvious 4786 03:00:43,439 --> 03:00:45,760 that the next line is going to print sky 4787 03:00:45,760 --> 03:00:47,680 is blue now the problem with this 4788 03:00:47,680 --> 03:00:50,080 comment is that apart from being verbose 4789 03:00:50,080 --> 03:00:52,080 and repetitive if we come back here and 4790 03:00:52,080 --> 03:00:54,560 change sky to ocean 4791 03:00:54,560 --> 03:00:57,439 now this comment gets outdated so every 4792 03:00:57,439 --> 03:00:59,359 time we change this code then we'll have 4793 03:00:59,359 --> 03:01:01,520 to come back and modify the 4794 03:01:01,520 --> 03:01:03,600 corresponding comment so i've seen some 4795 03:01:03,600 --> 03:01:05,760 developers add comments in front of 4796 03:01:05,760 --> 03:01:08,160 their functions for example they define 4797 03:01:08,160 --> 03:01:11,120 a function let's say square that takes a 4798 03:01:11,120 --> 03:01:14,240 number and simply returns 4799 03:01:14,240 --> 03:01:17,279 number times number well it is obvious 4800 03:01:17,279 --> 03:01:19,279 that this function calculates and 4801 03:01:19,279 --> 03:01:21,439 returns the square of the given number 4802 03:01:21,439 --> 03:01:23,279 so there is really no need to add a 4803 03:01:23,279 --> 03:01:26,720 comment and explain that calculates 4804 03:01:26,720 --> 03:01:29,840 and returns the square of a number 4805 03:01:29,840 --> 03:01:31,680 this is very repetitive and it creates 4806 03:01:31,680 --> 03:01:33,600 noise in your code someone else reading 4807 03:01:33,600 --> 03:01:35,439 your code they get distracted with all 4808 03:01:35,439 --> 03:01:37,760 these unnecessary comments so use 4809 03:01:37,760 --> 03:01:40,960 comments to explain whys and how not 4810 03:01:40,960 --> 03:01:42,720 what's if you have made certain 4811 03:01:42,720 --> 03:01:44,479 assumptions you can use comments to 4812 03:01:44,479 --> 03:01:46,720 explain those assumptions or you can use 4813 03:01:46,720 --> 03:01:49,200 them to add notes to remind yourself or 4814 03:01:49,200 --> 03:01:51,120 other developers to do something in the 4815 03:01:51,120 --> 03:01:52,080 code 4816 03:01:52,080 --> 03:01:54,240 so comments are good but too much of a 4817 03:01:54,240 --> 03:01:57,439 good thing is a bad thing 4818 03:02:01,040 --> 03:02:02,240 in this tutorial i'm going to talk to 4819 03:02:02,240 --> 03:02:05,120 you guys about classes in python classes 4820 03:02:05,120 --> 03:02:06,880 are extremely important in programming 4821 03:02:06,880 --> 03:02:09,600 and they're not specific to python in 4822 03:02:09,600 --> 03:02:11,040 fact a lot of other programming 4823 03:02:11,040 --> 03:02:13,439 languages do support the notion of 4824 03:02:13,439 --> 03:02:15,920 classes we use classes to define new 4825 03:02:15,920 --> 03:02:18,080 types for example so far you have 4826 03:02:18,080 --> 03:02:20,640 learned about the basic types in python 4827 03:02:20,640 --> 03:02:24,399 like numbers strings and booleans these 4828 03:02:24,399 --> 03:02:27,359 are the basic or simple types in python 4829 03:02:27,359 --> 03:02:29,760 you also learn about a couple of complex 4830 03:02:29,760 --> 03:02:33,760 types like lists and dictionaries 4831 03:02:33,760 --> 03:02:35,600 while these types are extremely useful 4832 03:02:35,600 --> 03:02:37,600 they cannot always be used to model 4833 03:02:37,600 --> 03:02:40,160 complex concepts for example think of 4834 03:02:40,160 --> 03:02:42,399 the concept of a point or a shopping 4835 03:02:42,399 --> 03:02:44,880 cart a shopping cart is not a boolean 4836 03:02:44,880 --> 03:02:47,120 it's not a list it's not a dictionary 4837 03:02:47,120 --> 03:02:49,200 it's a different kind of thing so we can 4838 03:02:49,200 --> 03:02:52,640 use classes to define new types to model 4839 03:02:52,640 --> 03:02:54,160 real concepts 4840 03:02:54,160 --> 03:02:55,439 now in this tutorial i'm going to show 4841 03:02:55,439 --> 03:02:57,760 you how to define a new type called 4842 03:02:57,760 --> 03:02:59,680 point and this new type is going to have 4843 03:02:59,680 --> 03:03:02,000 methods for working with points for 4844 03:03:02,000 --> 03:03:04,560 example back to our main program let's 4845 03:03:04,560 --> 03:03:07,840 create a list numbers one two three 4846 03:03:07,840 --> 03:03:10,479 you know that here when we type dot we 4847 03:03:10,479 --> 03:03:13,120 get access to these functions or methods 4848 03:03:13,120 --> 03:03:16,160 available in list objects now similar to 4849 03:03:16,160 --> 03:03:18,160 this we're going to create a new type 4850 03:03:18,160 --> 03:03:20,960 called point this point is going to have 4851 03:03:20,960 --> 03:03:25,600 methods like move draw get distance from 4852 03:03:25,600 --> 03:03:27,600 another point these are the operations 4853 03:03:27,600 --> 03:03:30,240 that we can perform on points 4854 03:03:30,240 --> 03:03:32,479 so let me show you how to do this we 4855 03:03:32,479 --> 03:03:34,720 start by defining a class using the 4856 03:03:34,720 --> 03:03:37,040 class keyword and right after that we 4857 03:03:37,040 --> 03:03:39,439 give our class a name 4858 03:03:39,439 --> 03:03:40,960 now look at the naming convention i have 4859 03:03:40,960 --> 03:03:43,120 used here i've capitalized the first 4860 03:03:43,120 --> 03:03:45,840 letter here this is what we call pascal 4861 03:03:45,840 --> 03:03:47,920 naming convention so the convention we 4862 03:03:47,920 --> 03:03:50,399 use for naming our classes is different 4863 03:03:50,399 --> 03:03:52,319 from the convention we use for naming 4864 03:03:52,319 --> 03:03:54,479 our variables and functions 4865 03:03:54,479 --> 03:03:56,160 for variables and functions we always 4866 03:03:56,160 --> 03:03:58,800 use lowercase letters and we separate 4867 03:03:58,800 --> 03:04:01,520 multiple words using an underscore but 4868 03:04:01,520 --> 03:04:03,359 when naming classes we don't use an 4869 03:04:03,359 --> 03:04:05,920 underscore to separate multiple words 4870 03:04:05,920 --> 03:04:08,880 instead we capitalize the first letter 4871 03:04:08,880 --> 03:04:11,040 of every word so in this case our class 4872 03:04:11,040 --> 03:04:13,279 has only a single word if it had 4873 03:04:13,279 --> 03:04:17,040 multiple words let's say email client 4874 03:04:17,040 --> 03:04:20,080 look i've capitalized the first letter 4875 03:04:20,080 --> 03:04:22,560 of every word once again this is called 4876 03:04:22,560 --> 03:04:24,880 pascal naming convention and it comes 4877 03:04:24,880 --> 03:04:26,960 from the old pascal language that you 4878 03:04:26,960 --> 03:04:28,399 might have heard of 4879 03:04:28,399 --> 03:04:29,200 so 4880 03:04:29,200 --> 03:04:31,920 back to our point class 4881 03:04:31,920 --> 03:04:33,840 here we have a colon which means we're 4882 03:04:33,840 --> 03:04:35,760 going to define a block 4883 03:04:35,760 --> 03:04:38,080 now in this block we can define all the 4884 03:04:38,080 --> 03:04:40,640 functions or methods that belong to 4885 03:04:40,640 --> 03:04:41,600 points 4886 03:04:41,600 --> 03:04:43,680 for example we can define a function 4887 03:04:43,680 --> 03:04:46,720 called move for moving a point 4888 03:04:46,720 --> 03:04:49,040 now note that as soon as we type open 4889 03:04:49,040 --> 03:04:52,000 parenthesis pycharm automatically adds 4890 03:04:52,000 --> 03:04:54,399 self here this is a special keyword and 4891 03:04:54,399 --> 03:04:56,399 we're going to get back to this shortly 4892 03:04:56,399 --> 03:04:57,279 so 4893 03:04:57,279 --> 03:05:00,399 for now in this method let's print 4894 03:05:00,399 --> 03:05:02,479 move on the terminal 4895 03:05:02,479 --> 03:05:04,479 now let's define another method like 4896 03:05:04,479 --> 03:05:05,439 draw 4897 03:05:05,439 --> 03:05:08,640 so define draw 4898 03:05:08,640 --> 03:05:11,040 and in this method let's print draw on 4899 03:05:11,040 --> 03:05:12,479 the terminal 4900 03:05:12,479 --> 03:05:15,200 now let's remove the indentation 4901 03:05:15,200 --> 03:05:16,880 we're done with the definition of our 4902 03:05:16,880 --> 03:05:19,520 point class so we need to add two line 4903 03:05:19,520 --> 03:05:20,880 breaks 4904 03:05:20,880 --> 03:05:22,479 okay 4905 03:05:22,479 --> 03:05:24,880 so with this class we defined a new type 4906 03:05:24,880 --> 03:05:26,880 with this new type we can create new 4907 03:05:26,880 --> 03:05:30,000 objects so an object is an instance of a 4908 03:05:30,000 --> 03:05:32,479 class a class simply defines the 4909 03:05:32,479 --> 03:05:34,479 blueprint or the template for creating 4910 03:05:34,479 --> 03:05:37,439 objects and objects are the actual 4911 03:05:37,439 --> 03:05:40,319 instances based on that blueprint so we 4912 03:05:40,319 --> 03:05:42,560 could have tens or hundreds of points on 4913 03:05:42,560 --> 03:05:45,439 the screen these are the objects or the 4914 03:05:45,439 --> 03:05:46,560 instances 4915 03:05:46,560 --> 03:05:49,120 so to create an object we type out the 4916 03:05:49,120 --> 03:05:50,479 name of our class 4917 03:05:50,479 --> 03:05:52,560 and then call it like a function 4918 03:05:52,560 --> 03:05:54,319 this creates a new object and then 4919 03:05:54,319 --> 03:05:57,200 returns it so we can store that object 4920 03:05:57,200 --> 03:06:00,960 in a variable let's call that point one 4921 03:06:00,960 --> 03:06:02,560 there you go 4922 03:06:02,560 --> 03:06:05,200 now when we use the dot operator 4923 03:06:05,200 --> 03:06:07,120 look we have 4924 03:06:07,120 --> 03:06:09,279 these two methods that we defined draw 4925 03:06:09,279 --> 03:06:11,439 and move we also have a bunch of other 4926 03:06:11,439 --> 03:06:13,840 methods that start with two underscores 4927 03:06:13,840 --> 03:06:15,760 these are called magic methods we'll 4928 03:06:15,760 --> 03:06:17,680 look at them later in the course 4929 03:06:17,680 --> 03:06:20,720 so now let's call the draw method of our 4930 03:06:20,720 --> 03:06:23,040 point object and 4931 03:06:23,040 --> 03:06:25,760 run our program so you can see draw is 4932 03:06:25,760 --> 03:06:27,840 printed on the terminal 4933 03:06:27,840 --> 03:06:30,479 now apart from methods these objects can 4934 03:06:30,479 --> 03:06:32,479 also have attributes and these 4935 03:06:32,479 --> 03:06:34,319 attributes are like variables that 4936 03:06:34,319 --> 03:06:36,560 belong to a particular object for 4937 03:06:36,560 --> 03:06:38,319 example here 4938 03:06:38,319 --> 03:06:41,120 after we define point one 4939 03:06:41,120 --> 03:06:45,920 we can set point 1 dot x to 10 and point 4940 03:06:45,920 --> 03:06:48,560 1 dot y to 20. 4941 03:06:48,560 --> 03:06:50,319 now we can easily print out this 4942 03:06:50,319 --> 03:06:53,319 attributes let's print 4943 03:06:53,319 --> 03:06:55,200 0.1.x 4944 03:06:55,200 --> 03:06:57,359 and run our program one more time take a 4945 03:06:57,359 --> 03:07:00,240 look so the x coordinate of point one is 4946 03:07:00,240 --> 03:07:02,800 ten now we can create another object 4947 03:07:02,800 --> 03:07:05,120 let's call that point two 4948 03:07:05,120 --> 03:07:06,880 so point two 4949 03:07:06,880 --> 03:07:08,640 once again we type out the name of our 4950 03:07:08,640 --> 03:07:11,359 class and then call it like a function 4951 03:07:11,359 --> 03:07:13,680 now this object is completely different 4952 03:07:13,680 --> 03:07:16,479 from the first object so if we print 4953 03:07:16,479 --> 03:07:20,160 point 2 dot x 4954 03:07:20,240 --> 03:07:22,080 flow can we get this attribute error 4955 03:07:22,080 --> 03:07:24,000 because this point object doesn't have 4956 03:07:24,000 --> 03:07:26,160 an attribute called x 4957 03:07:26,160 --> 03:07:29,120 so each object is a different instance 4958 03:07:29,120 --> 03:07:31,760 of a point class 4959 03:07:31,760 --> 03:07:34,319 and here we can assign a value to point 4960 03:07:34,319 --> 03:07:36,080 two dot x 4961 03:07:36,080 --> 03:07:38,319 let's set that to one and then run our 4962 03:07:38,319 --> 03:07:41,040 program so we can see the x coordinate 4963 03:07:41,040 --> 03:07:43,120 of 0.2 is 1. 4964 03:07:43,120 --> 03:07:44,560 so to recap 4965 03:07:44,560 --> 03:07:47,439 we use classes to define new types 4966 03:07:47,439 --> 03:07:48,960 these types can have methods that we 4967 03:07:48,960 --> 03:07:51,600 define in the body of the class and they 4968 03:07:51,600 --> 03:07:54,319 can also have attributes that we can set 4969 03:07:54,319 --> 03:07:57,760 anywhere in our programs 4970 03:08:01,120 --> 03:08:02,960 you have learned how to create new types 4971 03:08:02,960 --> 03:08:05,600 using classes now there is a tiny 4972 03:08:05,600 --> 03:08:08,000 problem in this implementation 4973 03:08:08,000 --> 03:08:11,120 we can create a point object without an 4974 03:08:11,120 --> 03:08:13,920 x or y coordinates let me show you so 4975 03:08:13,920 --> 03:08:15,600 point 4976 03:08:15,600 --> 03:08:18,080 we create this object here and before we 4977 03:08:18,080 --> 03:08:18,840 set 4978 03:08:18,840 --> 03:08:23,359 point.x let's print point.x 4979 03:08:23,359 --> 03:08:26,160 run our program 4980 03:08:26,160 --> 03:08:28,160 we get this attribute error that you saw 4981 03:08:28,160 --> 03:08:30,479 in the last tutorial so point object has 4982 03:08:30,479 --> 03:08:33,680 no attribute called x 4983 03:08:33,680 --> 03:08:35,680 this is the problem we have here it is 4984 03:08:35,680 --> 03:08:38,080 possible to have a point object without 4985 03:08:38,080 --> 03:08:40,720 an x or y coordinates and that doesn't 4986 03:08:40,720 --> 03:08:42,640 really make sense because whenever we 4987 03:08:42,640 --> 03:08:44,720 talk about a point we need to know where 4988 03:08:44,720 --> 03:08:46,960 that point is located to solve this 4989 03:08:46,960 --> 03:08:49,439 problem we use a constructor a 4990 03:08:49,439 --> 03:08:51,600 constructor is a function that gets 4991 03:08:51,600 --> 03:08:54,479 called at the time of creating an object 4992 03:08:54,479 --> 03:08:56,479 so here on line 9 when creating this 4993 03:08:56,479 --> 03:08:59,279 point object we want to pass values for 4994 03:08:59,279 --> 03:09:02,479 x and y coordinates let's say 10 4995 03:09:02,479 --> 03:09:05,680 and 20 and with this this point object 4996 03:09:05,680 --> 03:09:09,040 that we get here will have its x and y 4997 03:09:09,040 --> 03:09:11,840 coordinates initialized so how do we do 4998 03:09:11,840 --> 03:09:14,160 this we need to add a special method in 4999 03:09:14,160 --> 03:09:16,479 this class called constructor 5000 03:09:16,479 --> 03:09:18,160 so on the top 5001 03:09:18,160 --> 03:09:20,080 we define a new function 5002 03:09:20,080 --> 03:09:21,920 look at the name of this function double 5003 03:09:21,920 --> 03:09:23,120 underscore 5004 03:09:23,120 --> 03:09:25,439 init double underscore 5005 03:09:25,439 --> 03:09:27,920 so init is short for initialize and this 5006 03:09:27,920 --> 03:09:29,600 is the function or the method that gets 5007 03:09:29,600 --> 03:09:32,640 called when we create a new point object 5008 03:09:32,640 --> 03:09:34,960 now press enter 5009 03:09:34,960 --> 03:09:36,720 so this automatically adds 5010 03:09:36,720 --> 03:09:38,560 self in parenthesis we're going to come 5011 03:09:38,560 --> 03:09:40,239 back to this shortly 5012 03:09:40,239 --> 03:09:43,279 now right after self we want to add two 5013 03:09:43,279 --> 03:09:46,720 extra parameters x and y so let's type 5014 03:09:46,720 --> 03:09:48,479 out x and y 5015 03:09:48,479 --> 03:09:50,880 and then in the body of this method we 5016 03:09:50,880 --> 03:09:53,279 should read the values passed here and 5017 03:09:53,279 --> 03:09:55,600 use them to initialize our object so 5018 03:09:55,600 --> 03:09:58,960 when we pass 10 and 20 these arguments 5019 03:09:58,960 --> 03:10:00,560 will be used for 5020 03:10:00,560 --> 03:10:03,040 x and y parameters 5021 03:10:03,040 --> 03:10:05,040 now to initialize our object we type out 5022 03:10:05,040 --> 03:10:09,680 code like this self dot x equals x 5023 03:10:09,680 --> 03:10:13,040 and then self.y equals y what is going 5024 03:10:13,040 --> 03:10:14,080 on here 5025 03:10:14,080 --> 03:10:15,680 this self that you see here is a 5026 03:10:15,680 --> 03:10:18,000 reference to the current object 5027 03:10:18,000 --> 03:10:18,800 so 5028 03:10:18,800 --> 03:10:21,120 back to line 13 when we create a new 5029 03:10:21,120 --> 03:10:24,080 point object self references that object 5030 03:10:24,080 --> 03:10:26,399 in memory the same object that we are 5031 03:10:26,399 --> 03:10:28,880 referencing using this variable 5032 03:10:28,880 --> 03:10:31,600 so earlier we typed point that x equals 5033 03:10:31,600 --> 03:10:34,800 10 with this code we set the x attribute 5034 03:10:34,800 --> 03:10:36,960 of this point object now what we have in 5035 03:10:36,960 --> 03:10:39,120 the constructor is exactly the same 5036 03:10:39,120 --> 03:10:40,880 we're using self to reference the 5037 03:10:40,880 --> 03:10:43,520 current object and then we set the x 5038 03:10:43,520 --> 03:10:46,720 attribute to the x argument passed to 5039 03:10:46,720 --> 03:10:47,920 this function 5040 03:10:47,920 --> 03:10:50,000 so using this init method we can 5041 03:10:50,000 --> 03:10:51,760 initialize our objects 5042 03:10:51,760 --> 03:10:53,760 and we refer to this method as a 5043 03:10:53,760 --> 03:10:56,239 constructor this method is used to 5044 03:10:56,239 --> 03:10:59,279 construct or create an object okay 5045 03:10:59,279 --> 03:11:00,239 so 5046 03:11:00,239 --> 03:11:01,600 now 5047 03:11:01,600 --> 03:11:04,319 let's remove line 14. so we create a 5048 03:11:04,319 --> 03:11:06,000 point object with these values and then 5049 03:11:06,000 --> 03:11:09,040 print point.x let's run our program 5050 03:11:09,040 --> 03:11:12,000 there you go x is 10. we can also change 5051 03:11:12,000 --> 03:11:14,319 these values later so just before 5052 03:11:14,319 --> 03:11:17,840 printing point.x we can set point.x to 5053 03:11:17,840 --> 03:11:21,600 11 and then run our program so x is 5054 03:11:21,600 --> 03:11:23,680 updated beautiful 5055 03:11:23,680 --> 03:11:25,600 all right here's an exercise for you i 5056 03:11:25,600 --> 03:11:27,439 want you to define a new type called 5057 03:11:27,439 --> 03:11:28,560 person 5058 03:11:28,560 --> 03:11:30,560 these personal objects should have a 5059 03:11:30,560 --> 03:11:33,600 name attribute as well as a talk 5060 03:11:33,600 --> 03:11:34,640 method 5061 03:11:34,640 --> 03:11:36,720 this is a very easy exercise so you can 5062 03:11:36,720 --> 03:11:38,080 tackle it in probably a couple of 5063 03:11:38,080 --> 03:11:40,479 minutes 5064 03:11:43,520 --> 03:11:45,359 all right so first we start with the 5065 03:11:45,359 --> 03:11:47,840 class keyword we define this person 5066 03:11:47,840 --> 03:11:50,000 class once again look at the naming 5067 03:11:50,000 --> 03:11:52,000 convention i have used here the first 5068 03:11:52,000 --> 03:11:53,920 letter of the first word in this case 5069 03:11:53,920 --> 03:11:56,560 the only word is uppercase 5070 03:11:56,560 --> 03:11:58,319 now in this class we define all the 5071 03:11:58,319 --> 03:12:00,000 methods that we need currently we have 5072 03:12:00,000 --> 03:12:02,080 only one method that is the talk method 5073 03:12:02,080 --> 03:12:04,720 so define talk 5074 03:12:04,720 --> 03:12:07,120 pycharm automatically adds self we come 5075 03:12:07,120 --> 03:12:09,120 back to this shortly 5076 03:12:09,120 --> 03:12:12,640 for now let's just print talk 5077 03:12:12,640 --> 03:12:15,439 so this is the body of our person class 5078 03:12:15,439 --> 03:12:17,359 we remove the indentation 5079 03:12:17,359 --> 03:12:20,000 and add two line breaks after defining 5080 03:12:20,000 --> 03:12:21,200 our classes 5081 03:12:21,200 --> 03:12:22,960 now we can create a personal object 5082 03:12:22,960 --> 03:12:24,720 let's call that john 5083 03:12:24,720 --> 03:12:26,640 we set it to person 5084 03:12:26,640 --> 03:12:29,040 we can call john.talk 5085 03:12:29,040 --> 03:12:31,200 let's run the program 5086 03:12:31,200 --> 03:12:34,080 so we get this message beautiful but our 5087 03:12:34,080 --> 03:12:36,640 personal objects should also have 5088 03:12:36,640 --> 03:12:38,399 a name attribute it doesn't make sense 5089 03:12:38,399 --> 03:12:40,800 to have a person without a name so that 5090 03:12:40,800 --> 03:12:43,520 is when we use the constructor function 5091 03:12:43,520 --> 03:12:46,560 so on the top we add a new method 5092 03:12:46,560 --> 03:12:48,000 the name of this method is double 5093 03:12:48,000 --> 03:12:52,000 underscore init double underscore 5094 03:12:52,000 --> 03:12:54,800 so we call this method a constructor 5095 03:12:54,800 --> 03:12:56,640 now in this method we need to add 5096 03:12:56,640 --> 03:12:59,279 another parameter that is name 5097 03:12:59,279 --> 03:13:04,000 so we simply set self.name to name 5098 03:13:04,000 --> 03:13:05,840 once again self references the current 5099 03:13:05,840 --> 03:13:08,160 object so we're setting the name 5100 03:13:08,160 --> 03:13:10,399 attribute of the current object to the 5101 03:13:10,399 --> 03:13:13,120 name argument passed to this method 5102 03:13:13,120 --> 03:13:15,359 with this we can go back to line nine 5103 03:13:15,359 --> 03:13:17,200 and when creating a personal object we 5104 03:13:17,200 --> 03:13:20,000 pass the name let's say john smith 5105 03:13:20,000 --> 03:13:23,520 now let's print john dot name 5106 03:13:23,520 --> 03:13:26,479 and run our program there you go so john 5107 03:13:26,479 --> 03:13:28,160 smith is printed on the terminal 5108 03:13:28,160 --> 03:13:30,399 beautiful so this is all you had to do 5109 03:13:30,399 --> 03:13:32,640 as part of this exercise but let's make 5110 03:13:32,640 --> 03:13:33,760 this program a little bit more 5111 03:13:33,760 --> 03:13:34,960 interesting 5112 03:13:34,960 --> 03:13:37,200 instead of printing this boring message 5113 03:13:37,200 --> 03:13:40,840 here let's print hi i am john smith 5114 03:13:40,840 --> 03:13:44,239 so we can use a formatted string with a 5115 03:13:44,239 --> 03:13:45,840 message like hi 5116 03:13:45,840 --> 03:13:49,040 i am now we want to dynamically add the 5117 03:13:49,040 --> 03:13:51,600 name of this person how do we do that 5118 03:13:51,600 --> 03:13:53,760 well look at the parameter 5119 03:13:53,760 --> 03:13:56,319 of this talk method as i told you before 5120 03:13:56,319 --> 03:13:58,160 every method in our class should have 5121 03:13:58,160 --> 03:14:00,720 this parameter self and this should be 5122 03:14:00,720 --> 03:14:03,040 the very first parameter of every method 5123 03:14:03,040 --> 03:14:05,920 okay so with itself we can get reference 5124 03:14:05,920 --> 03:14:08,000 to the current object 5125 03:14:08,000 --> 03:14:09,120 so 5126 03:14:09,120 --> 03:14:11,600 we can add curly braces and type out 5127 03:14:11,600 --> 03:14:13,840 self dot name 5128 03:14:13,840 --> 03:14:16,479 this returns the name attribute of the 5129 03:14:16,479 --> 03:14:18,560 current person object 5130 03:14:18,560 --> 03:14:20,239 with this we can remove 5131 03:14:20,239 --> 03:14:21,439 line 10 5132 03:14:21,439 --> 03:14:24,840 where we print john.name and simply call 5133 03:14:24,840 --> 03:14:26,399 john.talk 5134 03:14:26,399 --> 03:14:28,399 let's run our program 5135 03:14:28,399 --> 03:14:31,279 hi i am john smith beautiful 5136 03:14:31,279 --> 03:14:32,960 now we can create another personal 5137 03:14:32,960 --> 03:14:36,000 object let's say bob 5138 03:14:36,000 --> 03:14:37,760 it's a new person 5139 03:14:37,760 --> 03:14:39,760 called bob smith 5140 03:14:39,760 --> 03:14:41,040 when we say 5141 03:14:41,040 --> 03:14:43,040 bob.talk 5142 03:14:43,040 --> 03:14:44,720 and run our program 5143 03:14:44,720 --> 03:14:46,960 we get a different message hi i'm bob 5144 03:14:46,960 --> 03:14:49,359 smith so each object is a different 5145 03:14:49,359 --> 03:14:53,800 instance of a person class 5146 03:14:56,640 --> 03:14:57,920 in this tutorial i'm going to talk to 5147 03:14:57,920 --> 03:15:00,880 you guys about inheritance in python 5148 03:15:00,880 --> 03:15:03,040 inheritance is a mechanism for reusing 5149 03:15:03,040 --> 03:15:05,520 code and once again it's not limited to 5150 03:15:05,520 --> 03:15:07,359 python most languages that support 5151 03:15:07,359 --> 03:15:10,479 classes also support inheritance so let 5152 03:15:10,479 --> 03:15:12,399 me show you how it works let's say we 5153 03:15:12,399 --> 03:15:15,040 have this dot class with a simple walk 5154 03:15:15,040 --> 03:15:16,800 method in this method we're simply 5155 03:15:16,800 --> 03:15:18,960 printing the walk message on the 5156 03:15:18,960 --> 03:15:21,439 terminal now let's say in a real program 5157 03:15:21,439 --> 03:15:22,880 instead of just one line of code here 5158 03:15:22,880 --> 03:15:25,279 we're going to have 10 lines of code 5159 03:15:25,279 --> 03:15:27,279 now what if in the future we want to 5160 03:15:27,279 --> 03:15:31,120 define another class let's say cat 5161 03:15:31,120 --> 03:15:32,880 and we want to add this walk method 5162 03:15:32,880 --> 03:15:35,680 there as well we'll have to repeat all 5163 03:15:35,680 --> 03:15:39,439 that code in this new class 5164 03:15:39,439 --> 03:15:40,560 walk 5165 03:15:40,560 --> 03:15:43,359 this is bad because we have repeated or 5166 03:15:43,359 --> 03:15:45,920 duplicated our code in programming we 5167 03:15:45,920 --> 03:15:48,239 have a principle called dry which is 5168 03:15:48,239 --> 03:15:50,560 short for don't repeat yourself and 5169 03:15:50,560 --> 03:15:51,920 here's the reason 5170 03:15:51,920 --> 03:15:53,279 let's say sometime in the future we 5171 03:15:53,279 --> 03:15:56,160 discover a problem in our walk method if 5172 03:15:56,160 --> 03:15:57,920 we have repeated or duplicated this 5173 03:15:57,920 --> 03:16:00,080 method in many other places then we'll 5174 03:16:00,080 --> 03:16:02,239 have to come back and fix that problem 5175 03:16:02,239 --> 03:16:04,960 in every single place we have duplicated 5176 03:16:04,960 --> 03:16:07,040 this code so that's why in programming 5177 03:16:07,040 --> 03:16:09,520 we should not define something twice 5178 03:16:09,520 --> 03:16:11,439 so how can we solve this problem 5179 03:16:11,439 --> 03:16:13,520 well there are different approaches one 5180 03:16:13,520 --> 03:16:15,120 approach which is easier to understand 5181 03:16:15,120 --> 03:16:17,439 for beginners is inheritance and here's 5182 03:16:17,439 --> 03:16:19,680 how it works we define a new class 5183 03:16:19,680 --> 03:16:22,560 called mammal and move the walk method 5184 03:16:22,560 --> 03:16:23,760 right there 5185 03:16:23,760 --> 03:16:27,279 then we'll have the dog and cat classes 5186 03:16:27,279 --> 03:16:29,520 inherit that method from their parent 5187 03:16:29,520 --> 03:16:31,920 just like how humans inherit certain 5188 03:16:31,920 --> 03:16:33,600 attributes or behaviors from their 5189 03:16:33,600 --> 03:16:35,920 parents it's exactly the same 5190 03:16:35,920 --> 03:16:36,640 so 5191 03:16:36,640 --> 03:16:39,600 on the top we're defining a new class 5192 03:16:39,600 --> 03:16:41,760 let's call it mammal 5193 03:16:41,760 --> 03:16:43,680 then we need to move the walk method 5194 03:16:43,680 --> 03:16:46,640 there so we select it cut it using 5195 03:16:46,640 --> 03:16:50,239 command x on mac or control x on windows 5196 03:16:50,239 --> 03:16:51,840 and then paste it here 5197 03:16:51,840 --> 03:16:52,880 beautiful 5198 03:16:52,880 --> 03:16:54,960 now we want our dot class to inherit 5199 03:16:54,960 --> 03:16:57,359 this method from the mammal class so 5200 03:16:57,359 --> 03:16:59,120 right after the class name we add 5201 03:16:59,120 --> 03:17:01,520 parenthesis and then type out the name 5202 03:17:01,520 --> 03:17:04,399 of the parent class in this case mammal 5203 03:17:04,399 --> 03:17:06,080 that's all we had to do 5204 03:17:06,080 --> 03:17:09,279 so with this the dot class will inherit 5205 03:17:09,279 --> 03:17:11,439 all the methods defined in the mammal 5206 03:17:11,439 --> 03:17:14,239 class now there's a tiny problem here 5207 03:17:14,239 --> 03:17:17,120 if you look at line 9 there is a red 5208 03:17:17,120 --> 03:17:18,319 underline here 5209 03:17:18,319 --> 03:17:20,399 the reason this is happening is because 5210 03:17:20,399 --> 03:17:23,279 python doesn't like an empty class 5211 03:17:23,279 --> 03:17:25,520 so right now our dot class is empty it 5212 03:17:25,520 --> 03:17:27,520 doesn't have anything we either need to 5213 03:17:27,520 --> 03:17:31,040 add methods specific to dogs or just to 5214 03:17:31,040 --> 03:17:33,680 make python happy we can use the pass 5215 03:17:33,680 --> 03:17:35,600 statement and that basically means 5216 03:17:35,600 --> 03:17:37,760 nothing we're telling python interpreter 5217 03:17:37,760 --> 03:17:39,920 hey pass this line don't worry about it 5218 03:17:39,920 --> 03:17:41,840 but with this we don't have an empty 5219 03:17:41,840 --> 03:17:43,600 class okay 5220 03:17:43,600 --> 03:17:45,439 now as i told you before after each 5221 03:17:45,439 --> 03:17:47,439 class you should have two line breaks so 5222 03:17:47,439 --> 03:17:49,200 right now we only have one line break 5223 03:17:49,200 --> 03:17:50,399 and that is the reason we have this 5224 03:17:50,399 --> 03:17:53,359 little warning here so let's add another 5225 03:17:53,359 --> 03:17:55,120 black line beautiful 5226 03:17:55,120 --> 03:17:57,200 so our dog class is inheriting the walk 5227 03:17:57,200 --> 03:17:59,279 method from the mammal we need to apply 5228 03:17:59,279 --> 03:18:02,560 the same change to the cat class so 5229 03:18:02,560 --> 03:18:04,960 we'll have cat 5230 03:18:04,960 --> 03:18:07,439 inherit from mammal 5231 03:18:07,439 --> 03:18:08,960 and then 5232 03:18:08,960 --> 03:18:11,439 we use the pass statement here 5233 03:18:11,439 --> 03:18:13,520 with these changes now we can create 5234 03:18:13,520 --> 03:18:16,239 either a dog or a cat object let's say 5235 03:18:16,239 --> 03:18:17,840 dog one 5236 03:18:17,840 --> 03:18:20,080 we set it to a dog object 5237 03:18:20,080 --> 03:18:21,520 and when we type 5238 03:18:21,520 --> 03:18:23,840 dot look we have the walk method that is 5239 03:18:23,840 --> 03:18:25,840 defined in the mammal class we can 5240 03:18:25,840 --> 03:18:29,120 easily call it let's run our program 5241 03:18:29,120 --> 03:18:32,399 there you go we see the walk message 5242 03:18:32,399 --> 03:18:34,880 so both the dog and cat classes are 5243 03:18:34,880 --> 03:18:37,040 inheriting all the methods defined in 5244 03:18:37,040 --> 03:18:39,600 their parent class now here we can add 5245 03:18:39,600 --> 03:18:42,479 methods specific to dogs for example we 5246 03:18:42,479 --> 03:18:43,760 can define 5247 03:18:43,760 --> 03:18:46,880 a method called bark 5248 03:18:46,880 --> 03:18:49,520 and then print bark 5249 03:18:49,520 --> 03:18:51,120 now we don't need the pass statement 5250 03:18:51,120 --> 03:18:53,439 because we already defined one method in 5251 03:18:53,439 --> 03:18:55,120 our dog class 5252 03:18:55,120 --> 03:18:58,960 so now when we type out dog one dot 5253 03:18:58,960 --> 03:19:01,680 look we have two methods one is the walk 5254 03:19:01,680 --> 03:19:03,680 method that we inherited and the bark 5255 03:19:03,680 --> 03:19:05,680 method that we just defined in the dog 5256 03:19:05,680 --> 03:19:08,640 class similarly we can add a method 5257 03:19:08,640 --> 03:19:10,800 specific to a cat for example we can 5258 03:19:10,800 --> 03:19:13,120 define a method called be 5259 03:19:13,120 --> 03:19:16,000 annoying yeah i hate cats so 5260 03:19:16,000 --> 03:19:17,200 let's print 5261 03:19:17,200 --> 03:19:18,800 annoying 5262 03:19:18,800 --> 03:19:21,439 now when we create a cat object let's 5263 03:19:21,439 --> 03:19:24,319 remove that cat one 5264 03:19:24,319 --> 03:19:26,960 and type out cat one dot we have these 5265 03:19:26,960 --> 03:19:30,080 two methods walk and bnoy 5266 03:19:30,080 --> 03:19:32,080 so these are the basics of inheritance 5267 03:19:32,080 --> 03:19:34,080 in python now there is more to this that 5268 03:19:34,080 --> 03:19:35,600 really goes beyond the scope of this 5269 03:19:35,600 --> 03:19:37,359 beginner's tutorial if you're interested 5270 03:19:37,359 --> 03:19:39,279 to learn more about inheritance and 5271 03:19:39,279 --> 03:19:41,680 other mechanisms for reusing code i 5272 03:19:41,680 --> 03:19:43,520 encourage you to get my complete python 5273 03:19:43,520 --> 03:19:46,520 course 5274 03:19:49,359 --> 03:19:50,640 in this tutorial i'm going to talk to 5275 03:19:50,640 --> 03:19:53,279 you guys about modules in python a 5276 03:19:53,279 --> 03:19:55,359 module in python is basically a file 5277 03:19:55,359 --> 03:19:57,600 with some python code and we use modules 5278 03:19:57,600 --> 03:20:00,399 to organize our code into multiple files 5279 03:20:00,399 --> 03:20:02,399 just like sections and a supermarket 5280 03:20:02,399 --> 03:20:03,920 when you go to a supermarket there are 5281 03:20:03,920 --> 03:20:06,399 different sections for fruits vegetables 5282 03:20:06,399 --> 03:20:08,080 cleaning products and so on we don't 5283 03:20:08,080 --> 03:20:10,160 have one section with all the products 5284 03:20:10,160 --> 03:20:12,080 in the supermarket we have the same 5285 03:20:12,080 --> 03:20:14,399 concept in programming so instead of 5286 03:20:14,399 --> 03:20:15,920 writing all of our code instead of 5287 03:20:15,920 --> 03:20:18,319 writing all the functions and classes in 5288 03:20:18,319 --> 03:20:21,120 app.pi we want to break up our code into 5289 03:20:21,120 --> 03:20:23,760 multiple files we refer to each file as 5290 03:20:23,760 --> 03:20:26,640 a module with this not only is our code 5291 03:20:26,640 --> 03:20:28,720 better organized and structured but 5292 03:20:28,720 --> 03:20:31,359 we'll also have the ability to reuse our 5293 03:20:31,359 --> 03:20:34,479 code for example over here in app.pi 5294 03:20:34,479 --> 03:20:36,000 i've defined two functions for 5295 03:20:36,000 --> 03:20:38,239 converting weight from pounds to 5296 03:20:38,239 --> 03:20:40,960 kilograms and vice versa 5297 03:20:40,960 --> 03:20:42,720 now we can take these two functions and 5298 03:20:42,720 --> 03:20:44,880 put them in a separate module called 5299 03:20:44,880 --> 03:20:47,120 converters and then we can import that 5300 03:20:47,120 --> 03:20:49,840 module into any program that needs this 5301 03:20:49,840 --> 03:20:51,680 converter functions let me show you how 5302 03:20:51,680 --> 03:20:53,600 to do this so 5303 03:20:53,600 --> 03:20:56,239 let's open up our project panel and 5304 03:20:56,239 --> 03:20:57,840 right click the project 5305 03:20:57,840 --> 03:21:01,040 go to new and add a new file 5306 03:21:01,040 --> 03:21:03,680 we're going to call this file converters 5307 03:21:03,680 --> 03:21:06,560 dot pi 5308 03:21:06,560 --> 03:21:09,120 then we go back to app.pi 5309 03:21:09,120 --> 03:21:12,399 select all this code cut it and then 5310 03:21:12,399 --> 03:21:14,560 paste it into our new module 5311 03:21:14,560 --> 03:21:17,279 converters.pi 5312 03:21:17,279 --> 03:21:18,720 with this we have cleaned up our app 5313 03:21:18,720 --> 03:21:20,640 module it's not bloated with different 5314 03:21:20,640 --> 03:21:22,720 functions so as we write more functions 5315 03:21:22,720 --> 03:21:24,399 and classes we put them in their 5316 03:21:24,399 --> 03:21:26,239 corresponding modules 5317 03:21:26,239 --> 03:21:28,479 a module should contain all the related 5318 03:21:28,479 --> 03:21:31,120 functions and classes okay 5319 03:21:31,120 --> 03:21:33,200 now you want to import the converters 5320 03:21:33,200 --> 03:21:35,359 module into our app module and that is 5321 03:21:35,359 --> 03:21:37,680 really easy on the top right 5322 03:21:37,680 --> 03:21:38,800 import 5323 03:21:38,800 --> 03:21:40,160 converters 5324 03:21:40,160 --> 03:21:42,399 without the extension so we don't add 5325 03:21:42,399 --> 03:21:44,880 dot pi we only type out the name of the 5326 03:21:44,880 --> 03:21:45,920 file 5327 03:21:45,920 --> 03:21:48,239 let me close the project panel 5328 03:21:48,239 --> 03:21:51,840 now this converter is an object so we 5329 03:21:51,840 --> 03:21:54,560 can use the data operator to access its 5330 03:21:54,560 --> 03:21:56,800 members so currently we have defined 5331 03:21:56,800 --> 03:21:59,200 these two functions inside of our module 5332 03:21:59,200 --> 03:22:01,200 kilogram to pounds and pounds to 5333 03:22:01,200 --> 03:22:02,319 kilogram 5334 03:22:02,319 --> 03:22:03,600 so we can easily call one of these 5335 03:22:03,600 --> 03:22:06,880 functions let's pass 70 kilos get the 5336 03:22:06,880 --> 03:22:09,680 result and print it out 5337 03:22:09,680 --> 03:22:11,600 run our program 5338 03:22:11,600 --> 03:22:12,319 so 5339 03:22:12,319 --> 03:22:15,200 that is 155 pounds 5340 03:22:15,200 --> 03:22:17,600 now there is also another syntax for 5341 03:22:17,600 --> 03:22:20,239 importing modules let me show you so 5342 03:22:20,239 --> 03:22:22,399 instead of importing the entire module 5343 03:22:22,399 --> 03:22:24,319 we can import specific functions from 5344 03:22:24,319 --> 03:22:26,960 that module here's how it works we type 5345 03:22:26,960 --> 03:22:28,640 out from 5346 03:22:28,640 --> 03:22:30,960 then we add the name of our module so 5347 03:22:30,960 --> 03:22:32,840 converters 5348 03:22:32,840 --> 03:22:36,160 import now here if you press control and 5349 03:22:36,160 --> 03:22:38,160 space you can see the list of all the 5350 03:22:38,160 --> 03:22:40,800 functions defined in this module so here 5351 03:22:40,800 --> 03:22:42,800 we can grab one of these functions let's 5352 03:22:42,800 --> 03:22:45,040 say kilogram to pounds and with this we 5353 03:22:45,040 --> 03:22:46,399 can directly 5354 03:22:46,399 --> 03:22:48,800 call this function just like a function 5355 03:22:48,800 --> 03:22:52,160 defined in this file app.pi 5356 03:22:52,160 --> 03:22:54,319 compare what we have on line 4 with what 5357 03:22:54,319 --> 03:22:57,120 we have on line 6. on line 6 we'll have 5358 03:22:57,120 --> 03:22:59,200 to prefix this function call with the 5359 03:22:59,200 --> 03:23:01,359 name of an object converters so we have 5360 03:23:01,359 --> 03:23:04,399 to type converters dot and then we'll be 5361 03:23:04,399 --> 03:23:06,640 able to access this function 5362 03:23:06,640 --> 03:23:09,439 in contrast when we import a specific 5363 03:23:09,439 --> 03:23:11,279 function from our module then we can 5364 03:23:11,279 --> 03:23:13,359 easily call that function without 5365 03:23:13,359 --> 03:23:16,160 prefixing it with the module name so 5366 03:23:16,160 --> 03:23:18,160 here's what you need to take away we use 5367 03:23:18,160 --> 03:23:20,160 modules to better organize our code 5368 03:23:20,160 --> 03:23:21,760 instead of writing all the code instead 5369 03:23:21,760 --> 03:23:23,920 of writing all our functions in one file 5370 03:23:23,920 --> 03:23:26,319 we break up our code across multiple 5371 03:23:26,319 --> 03:23:29,279 files each file is called a module and 5372 03:23:29,279 --> 03:23:30,960 it should contain all the related 5373 03:23:30,960 --> 03:23:32,640 functions and classes 5374 03:23:32,640 --> 03:23:34,880 then we can import a module into another 5375 03:23:34,880 --> 03:23:36,880 module in this case we are importing the 5376 03:23:36,880 --> 03:23:39,920 converters module into our app module 5377 03:23:39,920 --> 03:23:41,680 and as you saw there are two ways to 5378 03:23:41,680 --> 03:23:43,760 import a module we can import the entire 5379 03:23:43,760 --> 03:23:46,399 module and then we'll get an object with 5380 03:23:46,399 --> 03:23:48,479 the same name as the module so we can 5381 03:23:48,479 --> 03:23:51,279 type converters dot and then we'll be 5382 03:23:51,279 --> 03:23:53,439 able to access all the functions and 5383 03:23:53,439 --> 03:23:56,000 classes defined in this module the other 5384 03:23:56,000 --> 03:23:58,479 approach is to import a specific 5385 03:23:58,479 --> 03:24:00,479 function or class from a module using 5386 03:24:00,479 --> 03:24:04,239 the from import statement 5387 03:24:04,239 --> 03:24:06,239 now here's your exercise earlier in the 5388 03:24:06,239 --> 03:24:08,479 course we had an exercise for finding 5389 03:24:08,479 --> 03:24:10,560 the largest number in the list 5390 03:24:10,560 --> 03:24:12,800 so here on the top we have a list of 5391 03:24:12,800 --> 03:24:15,600 numbers initially we set max for the 5392 03:24:15,600 --> 03:24:17,359 first number in this list 5393 03:24:17,359 --> 03:24:19,920 then we loop through this list get each 5394 03:24:19,920 --> 03:24:20,960 number 5395 03:24:20,960 --> 03:24:22,800 if the current number is greater than 5396 03:24:22,800 --> 03:24:25,760 max then we reset max and finally when 5397 03:24:25,760 --> 03:24:28,399 we're done with our loop we print max on 5398 03:24:28,399 --> 03:24:30,479 the terminal now here's what i wanted to 5399 03:24:30,479 --> 03:24:32,560 do this code currently has no 5400 03:24:32,560 --> 03:24:34,479 organization we don't have any functions 5401 03:24:34,479 --> 03:24:36,560 we don't have any modules we wrote all 5402 03:24:36,560 --> 03:24:39,120 the code in app.pi and that is okay for 5403 03:24:39,120 --> 03:24:41,520 small programs but as your programs grow 5404 03:24:41,520 --> 03:24:43,439 you need to properly organize your code 5405 03:24:43,439 --> 03:24:46,560 into functions classes and modules 5406 03:24:46,560 --> 03:24:48,880 so as part of this exercise i want you 5407 03:24:48,880 --> 03:24:51,680 to write a function call it find 5408 03:24:51,680 --> 03:24:52,560 max 5409 03:24:52,560 --> 03:24:54,319 this function should take a list and 5410 03:24:54,319 --> 03:24:57,439 return the largest number in that list 5411 03:24:57,439 --> 03:24:59,680 now after you do this go ahead and put 5412 03:24:59,680 --> 03:25:02,239 this function in a separate module so 5413 03:25:02,239 --> 03:25:04,319 extract it from here and put it in a 5414 03:25:04,319 --> 03:25:06,720 module called utils 5415 03:25:06,720 --> 03:25:08,000 so in this module we're going to have a 5416 03:25:08,000 --> 03:25:10,000 bunch of utility functions 5417 03:25:10,000 --> 03:25:11,040 okay 5418 03:25:11,040 --> 03:25:11,920 then 5419 03:25:11,920 --> 03:25:13,920 import the utils module into the current 5420 03:25:13,920 --> 03:25:17,040 module and call this function 5421 03:25:17,040 --> 03:25:18,960 finally get the result and print it on 5422 03:25:18,960 --> 03:25:21,040 the terminal this is a fantastic 5423 03:25:21,040 --> 03:25:23,359 exercise to teach you how to properly 5424 03:25:23,359 --> 03:25:25,439 organize your code so go ahead and spend 5425 03:25:25,439 --> 03:25:28,960 a couple of minutes on this exercise 5426 03:25:32,000 --> 03:25:33,840 so the first step is to define a 5427 03:25:33,840 --> 03:25:36,399 function called find max 5428 03:25:36,399 --> 03:25:38,399 so on the top i'm going to define a 5429 03:25:38,399 --> 03:25:39,680 function 5430 03:25:39,680 --> 03:25:41,359 find max 5431 03:25:41,359 --> 03:25:43,120 this function should take a list of 5432 03:25:43,120 --> 03:25:45,279 numbers so we add a parameter called 5433 03:25:45,279 --> 03:25:46,880 numbers 5434 03:25:46,880 --> 03:25:48,800 now instead of this function we want to 5435 03:25:48,800 --> 03:25:51,359 have these few lines this is the 5436 03:25:51,359 --> 03:25:53,520 algorithm for finding the largest number 5437 03:25:53,520 --> 03:25:55,520 in the list we don't want to bring the 5438 03:25:55,520 --> 03:25:57,439 first line because this is where we 5439 03:25:57,439 --> 03:25:59,359 create a list so somewhere in our 5440 03:25:59,359 --> 03:26:00,960 program we're going to create this list 5441 03:26:00,960 --> 03:26:03,920 and then pass it as an argument to find 5442 03:26:03,920 --> 03:26:07,040 max function okay so 5443 03:26:07,040 --> 03:26:09,920 let's go ahead and grab these few lines 5444 03:26:09,920 --> 03:26:11,680 cut them and 5445 03:26:11,680 --> 03:26:15,520 put them inside of find max function 5446 03:26:15,520 --> 03:26:17,120 all right so we find the largest number 5447 03:26:17,120 --> 03:26:19,200 in this list finally we need to return 5448 03:26:19,200 --> 03:26:21,040 it so 5449 03:26:21,040 --> 03:26:22,080 return 5450 03:26:22,080 --> 03:26:23,040 max 5451 03:26:23,040 --> 03:26:24,880 so here's our function now we need to 5452 03:26:24,880 --> 03:26:27,359 move this into a separate module so 5453 03:26:27,359 --> 03:26:29,680 let's go to the project panel 5454 03:26:29,680 --> 03:26:31,279 right click the project 5455 03:26:31,279 --> 03:26:34,160 and add a new file we can also add a new 5456 03:26:34,160 --> 03:26:36,000 python file is the same 5457 03:26:36,000 --> 03:26:38,800 let's call that utils 5458 03:26:38,800 --> 03:26:41,120 so if you add a new python file pycharm 5459 03:26:41,120 --> 03:26:44,000 automatically adds dot pi extension but 5460 03:26:44,000 --> 03:26:45,359 if you go to 5461 03:26:45,359 --> 03:26:48,319 new file we'll have to explicitly type 5462 03:26:48,319 --> 03:26:49,840 out dot pi 5463 03:26:49,840 --> 03:26:52,239 so make sure to get the extension right 5464 03:26:52,239 --> 03:26:54,880 now back to app.pi 5465 03:26:54,880 --> 03:26:58,000 let's cut this code and paste it into 5466 03:26:58,000 --> 03:27:00,160 this new module 5467 03:27:00,160 --> 03:27:01,520 okay beautiful 5468 03:27:01,520 --> 03:27:04,160 now we go back to app.pi and import our 5469 03:27:04,160 --> 03:27:06,319 new module we can either import the 5470 03:27:06,319 --> 03:27:07,600 entire module 5471 03:27:07,600 --> 03:27:10,319 so import utils and then access 5472 03:27:10,319 --> 03:27:12,319 individual functions so we can call 5473 03:27:12,319 --> 03:27:16,720 utils dot find max or we can explicitly 5474 03:27:16,720 --> 03:27:18,800 import this function from the utils 5475 03:27:18,800 --> 03:27:20,479 module it doesn't really make a 5476 03:27:20,479 --> 03:27:22,319 difference so 5477 03:27:22,319 --> 03:27:25,680 we could type from utils import 5478 03:27:25,680 --> 03:27:27,520 find max 5479 03:27:27,520 --> 03:27:28,479 and then 5480 03:27:28,479 --> 03:27:29,920 we don't need to prefix this with the 5481 03:27:29,920 --> 03:27:32,479 name of its object so we simply call 5482 03:27:32,479 --> 03:27:33,680 find max 5483 03:27:33,680 --> 03:27:35,439 now we need to call this after we 5484 03:27:35,439 --> 03:27:37,359 initialize our list 5485 03:27:37,359 --> 03:27:39,279 so first we need to define the list of 5486 03:27:39,279 --> 03:27:40,399 numbers 5487 03:27:40,399 --> 03:27:41,200 then 5488 03:27:41,200 --> 03:27:43,840 we pass that list as an argument to find 5489 03:27:43,840 --> 03:27:46,960 max we get the result which is max and 5490 03:27:46,960 --> 03:27:49,600 finally we print it on the terminal 5491 03:27:49,600 --> 03:27:52,239 let's go ahead and run our program 5492 03:27:52,239 --> 03:27:54,080 so the largest number in this list is 5493 03:27:54,080 --> 03:27:56,960 10. beautiful now one last thing before 5494 03:27:56,960 --> 03:27:58,880 we finish this tutorial 5495 03:27:58,880 --> 03:28:01,279 here we have this little warning under 5496 03:28:01,279 --> 03:28:04,000 max and the tooltip says shadows 5497 03:28:04,000 --> 03:28:06,479 built-in name max the reason we see this 5498 03:28:06,479 --> 03:28:08,239 is because in python we actually have a 5499 03:28:08,239 --> 03:28:10,640 function called max and it does the 5500 03:28:10,640 --> 03:28:14,319 exact same job as our find max function 5501 03:28:14,319 --> 03:28:16,560 so this is actually built into python 5502 03:28:16,560 --> 03:28:18,160 but i ask you to do it as an exercise 5503 03:28:18,160 --> 03:28:19,680 because it's a great exercise for 5504 03:28:19,680 --> 03:28:22,399 beginners now back to the warning 5505 03:28:22,399 --> 03:28:24,000 this warning is basically telling us 5506 03:28:24,000 --> 03:28:26,800 that we are overwriting the built-in max 5507 03:28:26,800 --> 03:28:29,359 function in python so we're changing the 5508 03:28:29,359 --> 03:28:30,720 meaning of this function and this is 5509 03:28:30,720 --> 03:28:32,960 considered a bad practice let me show 5510 03:28:32,960 --> 03:28:35,200 you what i mean so let's comment out 5511 03:28:35,200 --> 03:28:37,359 what we have on line four by putting a 5512 03:28:37,359 --> 03:28:40,000 heist line here we can also use 5513 03:28:40,000 --> 03:28:42,960 a shortcut command and slash on mac or 5514 03:28:42,960 --> 03:28:45,920 ctrl and slash on windows 5515 03:28:45,920 --> 03:28:48,000 so now this line is commented out look 5516 03:28:48,000 --> 03:28:50,239 at the color max here it's purple it's 5517 03:28:50,239 --> 03:28:52,640 the same color as print and this is the 5518 03:28:52,640 --> 03:28:54,880 color used to identify the built-in 5519 03:28:54,880 --> 03:28:55,840 functions 5520 03:28:55,840 --> 03:28:58,000 so now we can call this function 5521 03:28:58,000 --> 03:28:59,040 and give it 5522 03:28:59,040 --> 03:29:00,800 our list of numbers 5523 03:29:00,800 --> 03:29:02,239 run our program 5524 03:29:02,239 --> 03:29:05,680 we get the exact same result however 5525 03:29:05,680 --> 03:29:08,000 if we bring this line back once again we 5526 03:29:08,000 --> 03:29:09,520 can use the same shortcut that is 5527 03:29:09,520 --> 03:29:12,239 command and slash on mac or ctrl slash 5528 03:29:12,239 --> 03:29:14,239 on windows 5529 03:29:14,239 --> 03:29:16,960 now we are redefining max so up to this 5530 03:29:16,960 --> 03:29:19,600 line max used to be a function but after 5531 03:29:19,600 --> 03:29:22,399 line 4 is executed max is set to an 5532 03:29:22,399 --> 03:29:24,399 integer that is the largest number in 5533 03:29:24,399 --> 03:29:25,439 this list 5534 03:29:25,439 --> 03:29:28,160 so we'll no longer be able to call it 5535 03:29:28,160 --> 03:29:29,520 on line 5. 5536 03:29:29,520 --> 03:29:30,800 let's run our program and see what 5537 03:29:30,800 --> 03:29:31,760 happens 5538 03:29:31,760 --> 03:29:34,800 so there you go we get this type error 5539 03:29:34,800 --> 03:29:38,479 int object is not callable because max 5540 03:29:38,479 --> 03:29:41,120 is now an integer it's not a function so 5541 03:29:41,120 --> 03:29:42,640 we cannot call it 5542 03:29:42,640 --> 03:29:44,720 so to solve this problem we can rename 5543 03:29:44,720 --> 03:29:46,640 this variable to something else 5544 03:29:46,640 --> 03:29:48,399 so right click here 5545 03:29:48,399 --> 03:29:50,319 go to refactor 5546 03:29:50,319 --> 03:29:52,399 and rename 5547 03:29:52,399 --> 03:29:56,640 and rename this to maxi mum 5548 03:29:56,880 --> 03:29:59,279 so you can see the warning is gone 5549 03:29:59,279 --> 03:30:01,359 also we have the same warning in our 5550 03:30:01,359 --> 03:30:02,800 utils module 5551 03:30:02,800 --> 03:30:05,439 so here we're redefining max and as i 5552 03:30:05,439 --> 03:30:07,680 told you before this is a bad practice 5553 03:30:07,680 --> 03:30:09,840 we're changing the meaning of built-in 5554 03:30:09,840 --> 03:30:12,640 functions in python so 5555 03:30:12,640 --> 03:30:14,080 let's rename this as well i'm going to 5556 03:30:14,080 --> 03:30:18,239 use the shortcut that is shift and f6 5557 03:30:18,239 --> 03:30:19,520 there you go 5558 03:30:19,520 --> 03:30:22,640 and here we can change this to maximum 5559 03:30:22,640 --> 03:30:25,600 enter done 5560 03:30:29,359 --> 03:30:30,640 in this tutorial we're going to talk 5561 03:30:30,640 --> 03:30:33,279 about packages in python packages are 5562 03:30:33,279 --> 03:30:35,359 basically another way to organize our 5563 03:30:35,359 --> 03:30:37,520 code so currently we have only three 5564 03:30:37,520 --> 03:30:40,000 files or three modules in our project 5565 03:30:40,000 --> 03:30:42,160 but a real project can contain hundreds 5566 03:30:42,160 --> 03:30:44,160 or even thousands of modules we don't 5567 03:30:44,160 --> 03:30:46,080 want to add all those modules here 5568 03:30:46,080 --> 03:30:48,000 because over time this directory will 5569 03:30:48,000 --> 03:30:50,479 get bloated with so many files so a 5570 03:30:50,479 --> 03:30:52,560 better approach is to organize related 5571 03:30:52,560 --> 03:30:55,439 modules inside of a package so a package 5572 03:30:55,439 --> 03:30:58,239 is a container for multiple modules in 5573 03:30:58,239 --> 03:31:00,160 file system terms a package is a 5574 03:31:00,160 --> 03:31:01,920 directory or folder 5575 03:31:01,920 --> 03:31:03,680 so in our project we can add a new 5576 03:31:03,680 --> 03:31:05,840 directory and in that directory we can 5577 03:31:05,840 --> 03:31:08,080 add all the related modules as a 5578 03:31:08,080 --> 03:31:10,160 metaphor think of a mall when you go to 5579 03:31:10,160 --> 03:31:12,080 a mall or a shopping center there are 5580 03:31:12,080 --> 03:31:14,479 different sections for men's women's and 5581 03:31:14,479 --> 03:31:17,520 kids clothing so that is like a package 5582 03:31:17,520 --> 03:31:19,279 now when you go to the men's section 5583 03:31:19,279 --> 03:31:20,960 there are different areas for different 5584 03:31:20,960 --> 03:31:23,120 kinds of products we have a section for 5585 03:31:23,120 --> 03:31:26,479 shoes t-shirts jackets and so on so each 5586 03:31:26,479 --> 03:31:28,239 of these is like a module 5587 03:31:28,239 --> 03:31:29,920 so in this tutorial i'm going to show 5588 03:31:29,920 --> 03:31:32,239 you how to create and use packages in 5589 03:31:32,239 --> 03:31:33,439 python 5590 03:31:33,439 --> 03:31:35,520 here in our project panel right click 5591 03:31:35,520 --> 03:31:40,080 the project and add a new directory 5592 03:31:40,080 --> 03:31:41,760 let's call this directory 5593 03:31:41,760 --> 03:31:42,800 e 5594 03:31:42,800 --> 03:31:43,840 commerce 5595 03:31:43,840 --> 03:31:45,439 so we're going to create a package 5596 03:31:45,439 --> 03:31:47,680 called e-commerce and in this package 5597 03:31:47,680 --> 03:31:49,680 we're going to have all the modules 5598 03:31:49,680 --> 03:31:52,319 related for an e-commerce application 5599 03:31:52,319 --> 03:31:54,399 for example we can have modules for 5600 03:31:54,399 --> 03:31:57,279 sales shipping customer service and so 5601 03:31:57,279 --> 03:32:00,399 on so let's go ahead 5602 03:32:00,399 --> 03:32:03,120 now here we have an empty directory 5603 03:32:03,120 --> 03:32:05,120 in order to convert this to a package we 5604 03:32:05,120 --> 03:32:07,680 need to add a special file in it so 5605 03:32:07,680 --> 03:32:10,640 right click this directory and add 5606 03:32:10,640 --> 03:32:12,880 a new python file 5607 03:32:12,880 --> 03:32:16,319 call that file double underscore in it 5608 03:32:16,319 --> 03:32:17,760 double underscore 5609 03:32:17,760 --> 03:32:21,120 this is a special convention in python 5610 03:32:21,120 --> 03:32:23,279 when python interpreter sees 5611 03:32:23,279 --> 03:32:25,920 a file with this name in a directory it 5612 03:32:25,920 --> 03:32:28,399 treats this directory as a package we 5613 03:32:28,399 --> 03:32:30,640 also have a shortcut for that in pycharm 5614 03:32:30,640 --> 03:32:32,800 so let me delete this directory and 5615 03:32:32,800 --> 03:32:34,160 start again 5616 03:32:34,160 --> 03:32:35,439 so delete 5617 03:32:35,439 --> 03:32:38,080 once again right click the project 5618 03:32:38,080 --> 03:32:40,399 and go to new but instead of creating a 5619 03:32:40,399 --> 03:32:43,040 new directory we can create a new python 5620 03:32:43,040 --> 03:32:44,239 package 5621 03:32:44,239 --> 03:32:47,520 let's call our package ecommerce 5622 03:32:47,520 --> 03:32:49,600 as you can see pycharm automatically 5623 03:32:49,600 --> 03:32:51,040 creates this file for us so we don't 5624 03:32:51,040 --> 03:32:53,760 have to manually create it okay 5625 03:32:53,760 --> 03:32:56,319 now in this package let's add a new 5626 03:32:56,319 --> 03:32:58,960 module so a new python file 5627 03:32:58,960 --> 03:33:01,920 let's call it shipping 5628 03:33:01,920 --> 03:33:02,800 okay 5629 03:33:02,800 --> 03:33:04,319 now in this module we want to add a 5630 03:33:04,319 --> 03:33:06,560 function for calculating the shipping 5631 03:33:06,560 --> 03:33:07,439 cost 5632 03:33:07,439 --> 03:33:10,000 so define 5633 03:33:10,000 --> 03:33:13,200 calculate underline shipping 5634 03:33:13,200 --> 03:33:14,800 and don't worry about parameters or the 5635 03:33:14,800 --> 03:33:17,760 implementation we simply print 5636 03:33:17,760 --> 03:33:21,200 calculate underline shipping 5637 03:33:21,200 --> 03:33:22,960 now let's say we want to import this 5638 03:33:22,960 --> 03:33:26,080 shipping module into our app module 5639 03:33:26,080 --> 03:33:27,840 with this new structure the shipping 5640 03:33:27,840 --> 03:33:29,840 module is now part of the e-commerce 5641 03:33:29,840 --> 03:33:32,239 package so we cannot import it directly 5642 03:33:32,239 --> 03:33:34,160 we have to start from the e-commerce 5643 03:33:34,160 --> 03:33:35,680 package okay 5644 03:33:35,680 --> 03:33:37,120 so 5645 03:33:37,120 --> 03:33:39,359 back to app.pipe once again there are 5646 03:33:39,359 --> 03:33:41,680 two ways to import this module we can 5647 03:33:41,680 --> 03:33:43,680 import the entire module or we can 5648 03:33:43,680 --> 03:33:46,000 import one of its functions or classes 5649 03:33:46,000 --> 03:33:48,160 so let me show you both approaches first 5650 03:33:48,160 --> 03:33:49,920 we're going to import the entire module 5651 03:33:49,920 --> 03:33:50,960 import 5652 03:33:50,960 --> 03:33:52,880 now what is the name of our package that 5653 03:33:52,880 --> 03:33:56,800 is e-commerce right so e-commerce dot 5654 03:33:56,800 --> 03:33:58,160 shipping 5655 03:33:58,160 --> 03:34:01,600 so instead of typing import shipping we 5656 03:34:01,600 --> 03:34:05,680 type import e-commerce dot shipping we 5657 03:34:05,680 --> 03:34:07,520 have to prefix it with the name of its 5658 03:34:07,520 --> 03:34:08,560 package 5659 03:34:08,560 --> 03:34:10,399 now to access any of the functions or 5660 03:34:10,399 --> 03:34:12,399 classes in this module we'll have to 5661 03:34:12,399 --> 03:34:15,439 type out ecommerce.shipping 5662 03:34:15,439 --> 03:34:16,479 dot 5663 03:34:16,479 --> 03:34:19,520 and there you go now we can access the 5664 03:34:19,520 --> 03:34:22,000 calculate shipping function 5665 03:34:22,000 --> 03:34:25,120 so let's go ahead and run our program 5666 03:34:25,120 --> 03:34:26,640 we see this message printed on the 5667 03:34:26,640 --> 03:34:29,040 terminal beautiful however with this 5668 03:34:29,040 --> 03:34:31,279 approach every time you want to call one 5669 03:34:31,279 --> 03:34:33,359 of the functions in this module we'll 5670 03:34:33,359 --> 03:34:36,560 have to prefix it with e-commerce dot 5671 03:34:36,560 --> 03:34:39,920 shipping dot that is very verbose so 5672 03:34:39,920 --> 03:34:42,080 when working with packages we often use 5673 03:34:42,080 --> 03:34:44,239 the second approach using the from 5674 03:34:44,239 --> 03:34:45,279 statement 5675 03:34:45,279 --> 03:34:48,000 so we can type from 5676 03:34:48,000 --> 03:34:50,239 ecommerce.shipping 5677 03:34:50,239 --> 03:34:51,680 import 5678 03:34:51,680 --> 03:34:53,920 calculate shipping 5679 03:34:53,920 --> 03:34:55,439 now we don't have to prefix this 5680 03:34:55,439 --> 03:34:58,000 function with ecommerce.shipping 5681 03:34:58,000 --> 03:35:00,399 and we can call it multiple times in 5682 03:35:00,399 --> 03:35:02,560 this module so our code is a little bit 5683 03:35:02,560 --> 03:35:04,319 shorter but what if you want to use 5684 03:35:04,319 --> 03:35:06,000 multiple functions in the shipping 5685 03:35:06,000 --> 03:35:06,960 module 5686 03:35:06,960 --> 03:35:08,960 we can either import them all here so 5687 03:35:08,960 --> 03:35:10,399 calculate shipping 5688 03:35:10,399 --> 03:35:13,680 comma let's say calculate underline tags 5689 03:35:13,680 --> 03:35:15,760 or we can import the entire module and 5690 03:35:15,760 --> 03:35:18,000 then access all the functions or classes 5691 03:35:18,000 --> 03:35:19,760 in that module 5692 03:35:19,760 --> 03:35:22,239 so let me revert this back 5693 03:35:22,239 --> 03:35:25,600 to import the entire module we type from 5694 03:35:25,600 --> 03:35:29,920 e-commerce so we remove dot shipping 5695 03:35:29,920 --> 03:35:33,359 so from this package import 5696 03:35:33,359 --> 03:35:34,640 the shipping 5697 03:35:34,640 --> 03:35:36,080 module 5698 03:35:36,080 --> 03:35:38,479 now this shipping module is an object 5699 03:35:38,479 --> 03:35:40,160 so we can access 5700 03:35:40,160 --> 03:35:41,920 all the functions and classes defined 5701 03:35:41,920 --> 03:35:44,720 here using the dot operator so shipping 5702 03:35:44,720 --> 03:35:48,239 dot there you go calculate shipping 5703 03:35:48,239 --> 03:35:50,239 so to recap when using the from 5704 03:35:50,239 --> 03:35:52,399 statement we can either start from the 5705 03:35:52,399 --> 03:35:55,520 package and import a specific module 5706 03:35:55,520 --> 03:35:58,960 or we can start from package dot 5707 03:35:58,960 --> 03:36:01,840 module and then import a specific 5708 03:36:01,840 --> 03:36:02,800 function 5709 03:36:02,800 --> 03:36:05,520 like calculate shipping 5710 03:36:05,520 --> 03:36:07,920 so this was the basics of using packages 5711 03:36:07,920 --> 03:36:10,560 in python as i told you before packages 5712 03:36:10,560 --> 03:36:12,160 are extremely important especially when 5713 03:36:12,160 --> 03:36:14,080 you want to work with a framework like 5714 03:36:14,080 --> 03:36:16,720 django we use django for building web 5715 03:36:16,720 --> 03:36:18,640 applications with python and i'm going 5716 03:36:18,640 --> 03:36:20,960 to show you that later in this course 5717 03:36:20,960 --> 03:36:22,880 so in django we have quite a few 5718 03:36:22,880 --> 03:36:25,359 packages and each package contains 5719 03:36:25,359 --> 03:36:27,600 several modules so make sure to watch 5720 03:36:27,600 --> 03:36:29,760 the tutorials about modules and packages 5721 03:36:29,760 --> 03:36:31,279 one more time otherwise you're going to 5722 03:36:31,279 --> 03:36:35,840 get confused later when we get to django 5723 03:36:39,279 --> 03:36:40,560 in this tutorial i'm going to talk to 5724 03:36:40,560 --> 03:36:42,640 you guys about the built-in modules in 5725 03:36:42,640 --> 03:36:45,040 python so python comes with a standard 5726 03:36:45,040 --> 03:36:47,439 library that contains several modules 5727 03:36:47,439 --> 03:36:50,000 for common tasks such as sending emails 5728 03:36:50,000 --> 03:36:52,080 working with data and time generating 5729 03:36:52,080 --> 03:36:54,560 random values and passwords and so on 5730 03:36:54,560 --> 03:36:56,800 there are so many modules already built 5731 03:36:56,800 --> 03:36:58,720 into python and that means there is 5732 03:36:58,720 --> 03:37:00,319 already lots of functionality that we 5733 03:37:00,319 --> 03:37:02,080 can reuse we don't have to code all of 5734 03:37:02,080 --> 03:37:04,239 that from scratch let me show you where 5735 03:37:04,239 --> 03:37:06,800 you can find the standard library so 5736 03:37:06,800 --> 03:37:08,960 open up your browser and search for 5737 03:37:08,960 --> 03:37:12,640 python 3 module index 5738 03:37:12,640 --> 03:37:14,160 make sure to add the version number 5739 03:37:14,160 --> 03:37:16,080 because the modules in python 2 are 5740 03:37:16,080 --> 03:37:18,560 different from modules in python 3. so 5741 03:37:18,560 --> 03:37:21,680 here we search for python 3 module index 5742 03:37:21,680 --> 03:37:23,920 and the first page here you can see it's 5743 03:37:23,920 --> 03:37:27,200 python module index for python 3. 5744 03:37:27,200 --> 03:37:29,439 let's go ahead so these are all the 5745 03:37:29,439 --> 03:37:30,479 modules 5746 03:37:30,479 --> 03:37:33,200 built into python for example we have 5747 03:37:33,200 --> 03:37:36,479 modules for working with data and time 5748 03:37:36,479 --> 03:37:39,520 we have modules for sending emails for 5749 03:37:39,520 --> 03:37:41,600 encoding values there are so many 5750 03:37:41,600 --> 03:37:43,600 modules here now the explanation of all 5751 03:37:43,600 --> 03:37:45,680 this is beyond the scope of this course 5752 03:37:45,680 --> 03:37:48,000 that really requires its own course 5753 03:37:48,000 --> 03:37:50,160 because we have so many modules here but 5754 03:37:50,160 --> 03:37:52,319 don't let this list intimidate you this 5755 03:37:52,319 --> 03:37:53,840 is not something that you need to learn 5756 03:37:53,840 --> 03:37:56,160 right now as you work with python as you 5757 03:37:56,160 --> 03:37:58,479 build more applications gradually you'll 5758 03:37:58,479 --> 03:38:00,319 get more familiar with various modules 5759 03:38:00,319 --> 03:38:02,720 here in fact nobody knows all the 5760 03:38:02,720 --> 03:38:05,279 modules in python standard library 5761 03:38:05,279 --> 03:38:06,960 everybody knows some modules based on 5762 03:38:06,960 --> 03:38:09,359 the applications they have built 5763 03:38:09,359 --> 03:38:10,640 so in this tutorial i'm going to show 5764 03:38:10,640 --> 03:38:12,080 you how to use one of the built-in 5765 03:38:12,080 --> 03:38:14,880 modules for generating random values so 5766 03:38:14,880 --> 03:38:17,520 on the top we import 5767 03:38:17,520 --> 03:38:18,880 the random 5768 03:38:18,880 --> 03:38:19,920 module 5769 03:38:19,920 --> 03:38:22,479 now because random is a built-in module 5770 03:38:22,479 --> 03:38:25,840 we don't need to have a file here called 5771 03:38:25,840 --> 03:38:28,160 so python interpreter knows that random 5772 03:38:28,160 --> 03:38:30,479 is a built-in module and it knows where 5773 03:38:30,479 --> 03:38:32,800 to find it now if you're curious where 5774 03:38:32,800 --> 03:38:34,960 this random module is stored on the disk 5775 03:38:34,960 --> 03:38:36,960 let me show you so here in the project 5776 03:38:36,960 --> 03:38:38,160 panel 5777 03:38:38,160 --> 03:38:41,120 expand external libraries 5778 03:38:41,120 --> 03:38:43,840 then expand python 3 5779 03:38:43,840 --> 03:38:47,439 and then one more time python 3.7 5780 03:38:47,439 --> 03:38:49,199 library root 5781 03:38:49,199 --> 03:38:50,720 so this is a folder somewhere on your 5782 03:38:50,720 --> 03:38:53,040 computer in this folder we have all the 5783 03:38:53,040 --> 03:38:56,239 built-in modules in python so if you 5784 03:38:56,239 --> 03:38:57,840 scroll down 5785 03:38:57,840 --> 03:39:00,319 you should be able to find 5786 03:39:00,319 --> 03:39:02,319 the random module there you go it's 5787 03:39:02,319 --> 03:39:05,199 right here it's a random.pi you can open 5788 03:39:05,199 --> 03:39:06,000 this 5789 03:39:06,000 --> 03:39:06,800 and 5790 03:39:06,800 --> 03:39:09,120 this shows you the source code of this 5791 03:39:09,120 --> 03:39:11,199 module there you go 5792 03:39:11,199 --> 03:39:12,399 now in this tutorial we don't want to 5793 03:39:12,399 --> 03:39:15,120 worry about that so let's go back to our 5794 03:39:15,120 --> 03:39:17,760 app.pi and use this module to generate 5795 03:39:17,760 --> 03:39:19,120 random values 5796 03:39:19,120 --> 03:39:21,040 so when we import this module now we 5797 03:39:21,040 --> 03:39:24,160 have this object random so we can use 5798 03:39:24,160 --> 03:39:26,720 the dot operator to access its methods 5799 03:39:26,720 --> 03:39:28,640 one method that is very useful here is 5800 03:39:28,640 --> 03:39:30,960 the random method and every time we call 5801 03:39:30,960 --> 03:39:33,520 it it generates a random value between 0 5802 03:39:33,520 --> 03:39:34,560 to 1. 5803 03:39:34,560 --> 03:39:36,800 so here we can do a for loop 5804 03:39:36,800 --> 03:39:38,319 let's say four 5805 03:39:38,319 --> 03:39:39,120 i 5806 03:39:39,120 --> 03:39:40,239 in 5807 03:39:40,239 --> 03:39:42,560 range three so just to refresh our 5808 03:39:42,560 --> 03:39:44,399 memory we are using the range function 5809 03:39:44,399 --> 03:39:47,040 to create a range object we can loop 5810 03:39:47,040 --> 03:39:48,880 through this range object and in each 5811 03:39:48,880 --> 03:39:50,880 iteration this object will spit out a 5812 03:39:50,880 --> 03:39:51,840 value 5813 03:39:51,840 --> 03:39:53,920 so with this for loop we can execute 5814 03:39:53,920 --> 03:39:56,399 this code three times now let's indent 5815 03:39:56,399 --> 03:39:59,600 this okay and finally let's print the 5816 03:39:59,600 --> 03:40:02,000 result 5817 03:40:02,479 --> 03:40:04,479 there you go so we can see that in each 5818 03:40:04,479 --> 03:40:07,279 iteration we get a new random value 5819 03:40:07,279 --> 03:40:10,160 between 0 to 1. now what if we want a 5820 03:40:10,160 --> 03:40:12,319 random value for a particular age let's 5821 03:40:12,319 --> 03:40:15,920 say random values between 10 to 20. well 5822 03:40:15,920 --> 03:40:18,800 there is another method here around and 5823 03:40:18,800 --> 03:40:20,720 we passed two arguments here to specify 5824 03:40:20,720 --> 03:40:24,720 our range let's say 10 and 20. 5825 03:40:24,720 --> 03:40:26,560 run our program one more time 5826 03:40:26,560 --> 03:40:30,319 now we have the numbers 20 13 and 18. if 5827 03:40:30,319 --> 03:40:32,880 we run our program one more time we get 5828 03:40:32,880 --> 03:40:35,520 different values so 20 19 11 one more 5829 03:40:35,520 --> 03:40:36,479 time 5830 03:40:36,479 --> 03:40:37,600 look 5831 03:40:37,600 --> 03:40:40,000 we also have another powerful method for 5832 03:40:40,000 --> 03:40:42,239 randomly picking an item from a list 5833 03:40:42,239 --> 03:40:44,160 let's say we have a list of team members 5834 03:40:44,160 --> 03:40:46,080 and we want to randomly pick someone as 5835 03:40:46,080 --> 03:40:47,840 the leader let me show you how to do 5836 03:40:47,840 --> 03:40:50,239 that so we define the list of team 5837 03:40:50,239 --> 03:40:51,279 members 5838 03:40:51,279 --> 03:40:52,560 we set it to 5839 03:40:52,560 --> 03:40:53,840 john 5840 03:40:53,840 --> 03:40:55,199 mary 5841 03:40:55,199 --> 03:40:56,399 bob 5842 03:40:56,399 --> 03:40:58,479 and marsh 5843 03:40:58,479 --> 03:40:59,279 now 5844 03:40:59,279 --> 03:41:02,000 we can call random the choice 5845 03:41:02,000 --> 03:41:03,439 and pass 5846 03:41:03,439 --> 03:41:04,960 our members list 5847 03:41:04,960 --> 03:41:06,880 this method randomly picks an item from 5848 03:41:06,880 --> 03:41:09,600 this list and returns it so we can store 5849 03:41:09,600 --> 03:41:12,880 the result in leader and then print it 5850 03:41:12,880 --> 03:41:14,960 on the terminal 5851 03:41:14,960 --> 03:41:16,880 let's run our program 5852 03:41:16,880 --> 03:41:19,279 so now the leader is merry if we run our 5853 03:41:19,279 --> 03:41:21,120 program one more time this time it's 5854 03:41:21,120 --> 03:41:24,399 mosh now it's march again one more time 5855 03:41:24,399 --> 03:41:26,000 it really likes much 5856 03:41:26,000 --> 03:41:28,239 now it's bob you got the point so the 5857 03:41:28,239 --> 03:41:30,560 random module is very powerful and it 5858 03:41:30,560 --> 03:41:32,560 has a lot of applications 5859 03:41:32,560 --> 03:41:34,960 now here's a very cool exercise i wanted 5860 03:41:34,960 --> 03:41:37,359 to write this program to roll a dice so 5861 03:41:37,359 --> 03:41:39,359 every time we run this program we get 5862 03:41:39,359 --> 03:41:41,520 different values now we have three and 5863 03:41:41,520 --> 03:41:43,760 one next time we get three and six one 5864 03:41:43,760 --> 03:41:46,399 more time five and six four and three 5865 03:41:46,399 --> 03:41:49,120 double one that's a very cool exercise 5866 03:41:49,120 --> 03:41:51,040 so before we get started i want you to 5867 03:41:51,040 --> 03:41:53,279 pay attention to a few things 5868 03:41:53,279 --> 03:41:56,000 i want you to define a class called dice 5869 03:41:56,000 --> 03:41:57,680 in this class we're going to have a 5870 03:41:57,680 --> 03:41:59,520 method called roll 5871 03:41:59,520 --> 03:42:01,680 so every time we call this method we get 5872 03:42:01,680 --> 03:42:04,319 a tuple a tuple remember is a list of 5873 03:42:04,319 --> 03:42:06,399 values but that list cannot be changed 5874 03:42:06,399 --> 03:42:08,399 we cannot add a new item to it we cannot 5875 03:42:08,399 --> 03:42:11,279 remove it so it's like a read-only list 5876 03:42:11,279 --> 03:42:13,439 so every time we call the roll method we 5877 03:42:13,439 --> 03:42:16,399 should get a tuple of two random values 5878 03:42:16,399 --> 03:42:18,239 so go ahead and do this exercise you'll 5879 03:42:18,239 --> 03:42:22,040 see my solution next 5880 03:42:24,560 --> 03:42:26,000 all right we start by importing the 5881 03:42:26,000 --> 03:42:28,160 random module on the top 5882 03:42:28,160 --> 03:42:30,800 then we define a class called dice 5883 03:42:30,800 --> 03:42:32,880 colon in this class we want to have a 5884 03:42:32,880 --> 03:42:36,399 method called role so define role 5885 03:42:36,399 --> 03:42:40,000 parenthesis this automatically adds self 5886 03:42:40,000 --> 03:42:41,680 now in this method we want to generate 5887 03:42:41,680 --> 03:42:44,479 two random values between one to six so 5888 03:42:44,479 --> 03:42:45,600 we call 5889 03:42:45,600 --> 03:42:46,640 random 5890 03:42:46,640 --> 03:42:48,560 dot rand int 5891 03:42:48,560 --> 03:42:51,359 and pass one and six so this generates 5892 03:42:51,359 --> 03:42:53,040 the first number 5893 03:42:53,040 --> 03:42:54,800 we can put that here 5894 03:42:54,800 --> 03:42:56,560 now let's call this one more time so i'm 5895 03:42:56,560 --> 03:42:58,880 going to copy what we have on line five 5896 03:42:58,880 --> 03:43:00,880 put it here and change this variable to 5897 03:43:00,880 --> 03:43:01,840 second 5898 03:43:01,840 --> 03:43:04,000 so we have two variables first and 5899 03:43:04,000 --> 03:43:06,720 second with two random values finally we 5900 03:43:06,720 --> 03:43:09,520 need to return them in a tuple so return 5901 03:43:09,520 --> 03:43:13,040 parenthesis instead of square brackets 5902 03:43:13,040 --> 03:43:15,600 first and second 5903 03:43:15,600 --> 03:43:17,439 now in python when you want to return a 5904 03:43:17,439 --> 03:43:19,760 tuple from a function you don't have to 5905 03:43:19,760 --> 03:43:22,399 add this parenthesis so you can simplify 5906 03:43:22,399 --> 03:43:25,040 your code and python will automatically 5907 03:43:25,040 --> 03:43:27,680 interpret this as a tuple okay 5908 03:43:27,680 --> 03:43:29,439 so we're done with the definition of our 5909 03:43:29,439 --> 03:43:32,239 class we remove the indentation and add 5910 03:43:32,239 --> 03:43:35,439 two line breaks now we create an object 5911 03:43:35,439 --> 03:43:37,359 of this type so 5912 03:43:37,359 --> 03:43:40,399 we set dice to dice 5913 03:43:40,399 --> 03:43:44,239 next we roll the dice so dice.roll and 5914 03:43:44,239 --> 03:43:46,640 print the result on the terminal 5915 03:43:46,640 --> 03:43:48,479 that's all you have to do let's run this 5916 03:43:48,479 --> 03:43:50,720 program 5917 03:43:50,720 --> 03:43:53,199 so now we get two and five let's run it 5918 03:43:53,199 --> 03:43:55,840 one more time three and two one on one 5919 03:43:55,840 --> 03:43:57,439 beautiful 5920 03:43:57,439 --> 03:43:59,279 now we have this little warning here 5921 03:43:59,279 --> 03:44:02,800 that says expected to black lines found 5922 03:44:02,800 --> 03:44:05,600 one once again this is coming from pep8 5923 03:44:05,600 --> 03:44:08,239 pep is short for python enhancement 5924 03:44:08,239 --> 03:44:10,160 proposal we have so many python 5925 03:44:10,160 --> 03:44:13,279 enhancement proposals or peps pep8 is a 5926 03:44:13,279 --> 03:44:15,760 very famous one and in this document we 5927 03:44:15,760 --> 03:44:16,399 have 5928 03:44:16,399 --> 03:44:18,560 all the best practices for formatting 5929 03:44:18,560 --> 03:44:21,359 our code so pep8 says that after we 5930 03:44:21,359 --> 03:44:23,040 define our classes we should have two 5931 03:44:23,040 --> 03:44:25,120 line breaks we already done that so the 5932 03:44:25,120 --> 03:44:27,040 reason this is complaining is because we 5933 03:44:27,040 --> 03:44:29,680 have one line break before the class so 5934 03:44:29,680 --> 03:44:31,680 let's add one more line break now pie 5935 03:44:31,680 --> 03:44:34,640 charm is happy so basically pycharm is 5936 03:44:34,640 --> 03:44:37,040 following all the best practices defined 5937 03:44:37,040 --> 03:44:38,720 in pep 8. 5938 03:44:38,720 --> 03:44:40,160 now you don't have to memorize any of 5939 03:44:40,160 --> 03:44:42,239 these best practices as you code in 5940 03:44:42,239 --> 03:44:44,000 pycharm pycharm gives you little 5941 03:44:44,000 --> 03:44:45,760 warnings and gradually you will learn 5942 03:44:45,760 --> 03:44:48,080 how to properly format your code so it's 5943 03:44:48,080 --> 03:44:51,279 readable by other people 5944 03:44:54,720 --> 03:44:55,920 in this tutorial i'm going to show you 5945 03:44:55,920 --> 03:44:58,720 how to work with directories in python 5946 03:44:58,720 --> 03:45:01,600 so here on python 3 module index you can 5947 03:45:01,600 --> 03:45:03,520 see we have a module called pathlib 5948 03:45:03,520 --> 03:45:05,920 which provides an object oriented file 5949 03:45:05,920 --> 03:45:08,080 system patch that basically means it 5950 03:45:08,080 --> 03:45:09,920 provides classes that we can use to 5951 03:45:09,920 --> 03:45:12,479 create objects to work with directories 5952 03:45:12,479 --> 03:45:15,040 and files so if you click here you can 5953 03:45:15,040 --> 03:45:17,520 see how this module works you can see 5954 03:45:17,520 --> 03:45:19,920 all kinds of examples for example here 5955 03:45:19,920 --> 03:45:22,160 under basic usage you can see this is 5956 03:45:22,160 --> 03:45:25,120 how we import the past class from the 5957 03:45:25,120 --> 03:45:27,760 passlip module let me zoom in so you can 5958 03:45:27,760 --> 03:45:31,920 see clearly okay so look from passlip 5959 03:45:31,920 --> 03:45:35,120 that's our module import path note the 5960 03:45:35,120 --> 03:45:37,520 naming convention p is capitalized that 5961 03:45:37,520 --> 03:45:39,520 means path is a class 5962 03:45:39,520 --> 03:45:41,439 so we need to create an instance of this 5963 03:45:41,439 --> 03:45:43,120 class as you can see 5964 03:45:43,120 --> 03:45:44,319 right here 5965 03:45:44,319 --> 03:45:45,920 now there is so much documentation here 5966 03:45:45,920 --> 03:45:47,680 you can read this on your own but let me 5967 03:45:47,680 --> 03:45:49,279 quickly show you a few examples to help 5968 03:45:49,279 --> 03:45:50,960 you get started 5969 03:45:50,960 --> 03:45:52,560 so back in pycharm 5970 03:45:52,560 --> 03:45:53,600 on the top 5971 03:45:53,600 --> 03:45:57,279 from the passlip module let's import the 5972 03:45:57,279 --> 03:45:59,359 past class 5973 03:45:59,359 --> 03:46:01,439 now we need to create a path object to 5974 03:46:01,439 --> 03:46:04,239 reference a file or a directory on our 5975 03:46:04,239 --> 03:46:05,279 computer 5976 03:46:05,279 --> 03:46:06,880 so there are basically two ways to do 5977 03:46:06,880 --> 03:46:10,560 this we can use an absolute pass 5978 03:46:10,560 --> 03:46:12,800 or a relative path which basically means 5979 03:46:12,800 --> 03:46:14,560 a path starting from the current 5980 03:46:14,560 --> 03:46:16,399 directory for example if you want to 5981 03:46:16,399 --> 03:46:17,600 reference 5982 03:46:17,600 --> 03:46:19,840 this ecommerce directory in our project 5983 03:46:19,840 --> 03:46:22,080 we can use the relative path so we start 5984 03:46:22,080 --> 03:46:24,560 from the current directory and then go 5985 03:46:24,560 --> 03:46:25,840 somewhere else 5986 03:46:25,840 --> 03:46:28,080 with absolute paths we start from the 5987 03:46:28,080 --> 03:46:30,160 root of our hard disk 5988 03:46:30,160 --> 03:46:31,600 for example if you're on windows you 5989 03:46:31,600 --> 03:46:34,479 might have an absolute path like this c 5990 03:46:34,479 --> 03:46:37,680 drive backslash program 5991 03:46:37,680 --> 03:46:38,640 files 5992 03:46:38,640 --> 03:46:41,520 backslash microsoft so these are 5993 03:46:41,520 --> 03:46:42,880 directories 5994 03:46:42,880 --> 03:46:46,239 in c drive on windows we use a backslash 5995 03:46:46,239 --> 03:46:47,520 to build a path 5996 03:46:47,520 --> 03:46:49,680 if you're on mac or linux your paths 5997 03:46:49,680 --> 03:46:51,680 look a little bit different so instead 5998 03:46:51,680 --> 03:46:53,840 of a backslash we have a forward slash 5999 03:46:53,840 --> 03:46:55,680 let's say user 6000 03:46:55,680 --> 03:46:57,920 local slash bin 6001 03:46:57,920 --> 03:47:00,239 so these are examples of absolute paths 6002 03:47:00,239 --> 03:47:01,359 in this tutorial we're going to work 6003 03:47:01,359 --> 03:47:03,600 with a relative pass to work with this 6004 03:47:03,600 --> 03:47:06,000 ecommerce directory that we have in our 6005 03:47:06,000 --> 03:47:07,680 project now if you didn't create this 6006 03:47:07,680 --> 03:47:09,680 directory earlier simply right click on 6007 03:47:09,680 --> 03:47:12,080 the project and then go to new 6008 03:47:12,080 --> 03:47:13,359 directory 6009 03:47:13,359 --> 03:47:14,319 okay 6010 03:47:14,319 --> 03:47:15,359 so 6011 03:47:15,359 --> 03:47:17,359 let's close this panel 6012 03:47:17,359 --> 03:47:19,920 we create a path object 6013 03:47:19,920 --> 03:47:22,239 now if we don't pass an argument here 6014 03:47:22,239 --> 03:47:23,920 this will reference the current 6015 03:47:23,920 --> 03:47:25,040 directory 6016 03:47:25,040 --> 03:47:27,439 alternatively we can pass a string in 6017 03:47:27,439 --> 03:47:29,439 this string we can add a file or a 6018 03:47:29,439 --> 03:47:32,160 directory let's say the e-commerce 6019 03:47:32,160 --> 03:47:33,359 directory 6020 03:47:33,359 --> 03:47:35,600 now this returns a pass object so you 6021 03:47:35,600 --> 03:47:37,439 store it here 6022 03:47:37,439 --> 03:47:39,359 now this pass object has a few 6023 03:47:39,359 --> 03:47:41,439 interesting methods for example we can 6024 03:47:41,439 --> 03:47:44,399 check to see if a path exists by calling 6025 03:47:44,399 --> 03:47:46,640 the exist method this simply returns a 6026 03:47:46,640 --> 03:47:48,479 boolean so let's print it on the 6027 03:47:48,479 --> 03:47:50,160 terminal 6028 03:47:50,160 --> 03:47:51,279 there you go 6029 03:47:51,279 --> 03:47:53,680 so this path exists but if we change 6030 03:47:53,680 --> 03:47:56,399 this to let's say e-commerce 1 and run 6031 03:47:56,399 --> 03:47:58,800 our program we get false 6032 03:47:58,800 --> 03:48:01,040 we can also create a new directory for 6033 03:48:01,040 --> 03:48:03,920 example let's change this path to emails 6034 03:48:03,920 --> 03:48:06,239 now when we run this program obviously 6035 03:48:06,239 --> 03:48:08,239 we don't have this directory but we can 6036 03:48:08,239 --> 03:48:12,000 create it by calling the mkdir 6037 03:48:12,000 --> 03:48:14,640 method so make directory 6038 03:48:14,640 --> 03:48:16,800 let's run our program 6039 03:48:16,800 --> 03:48:19,279 all right this method returns none which 6040 03:48:19,279 --> 03:48:20,880 basically means it doesn't return any 6041 03:48:20,880 --> 03:48:23,120 values now if you look at the project 6042 03:48:23,120 --> 03:48:25,279 panel you can see here we have a new 6043 03:48:25,279 --> 03:48:27,199 directory called emails we can also 6044 03:48:27,199 --> 03:48:29,680 delete this directory so 6045 03:48:29,680 --> 03:48:33,760 instead of mkdir we call rm dir which is 6046 03:48:33,760 --> 03:48:36,000 short for remove directory 6047 03:48:36,000 --> 03:48:37,439 let's go ahead 6048 03:48:37,439 --> 03:48:40,160 now you can see that directory is gone 6049 03:48:40,160 --> 03:48:42,080 we can also find all the files and 6050 03:48:42,080 --> 03:48:44,319 directories in a given pass that is very 6051 03:48:44,319 --> 03:48:45,680 useful if you want to write a little 6052 03:48:45,680 --> 03:48:47,680 program to automate something for 6053 03:48:47,680 --> 03:48:49,760 example you can iterate over all the 6054 03:48:49,760 --> 03:48:52,000 spreadsheets in a directory open them 6055 03:48:52,000 --> 03:48:53,680 and process them 6056 03:48:53,680 --> 03:48:56,080 let me show you so first we change the 6057 03:48:56,080 --> 03:48:56,960 path 6058 03:48:56,960 --> 03:48:58,640 to the current directory 6059 03:48:58,640 --> 03:49:01,920 and then we call the glob method 6060 03:49:01,920 --> 03:49:03,760 with this method we can search for files 6061 03:49:03,760 --> 03:49:06,080 and directories in the current path so 6062 03:49:06,080 --> 03:49:08,319 as the first argument we need to pass a 6063 03:49:08,319 --> 03:49:11,120 string that defines a search pattern we 6064 03:49:11,120 --> 03:49:13,279 can type an asterisk and that means 6065 03:49:13,279 --> 03:49:16,239 everything all files and all directories 6066 03:49:16,239 --> 03:49:18,720 we can optionally add an extension 6067 03:49:18,720 --> 03:49:22,560 so to get all the files we use star dot 6068 03:49:22,560 --> 03:49:23,600 star 6069 03:49:23,600 --> 03:49:25,279 with this pattern we'll only get the 6070 03:49:25,279 --> 03:49:27,279 files in the current directory but not 6071 03:49:27,279 --> 03:49:29,760 the directories we can also search for 6072 03:49:29,760 --> 03:49:32,479 all the pi files or all the excel 6073 03:49:32,479 --> 03:49:34,640 spreadsheets anything 6074 03:49:34,640 --> 03:49:37,120 so let's search for all the pi files and 6075 03:49:37,120 --> 03:49:38,560 the current directory 6076 03:49:38,560 --> 03:49:40,960 now when we run our program 6077 03:49:40,960 --> 03:49:43,920 we get this generator object generator 6078 03:49:43,920 --> 03:49:45,840 objects are kind of an advanced topic 6079 03:49:45,840 --> 03:49:47,520 and they are beyond the scope of this 6080 03:49:47,520 --> 03:49:50,479 beginner's python course but for now all 6081 03:49:50,479 --> 03:49:52,239 you need to know is that we can iterate 6082 03:49:52,239 --> 03:49:54,720 or loop through these generator objects 6083 03:49:54,720 --> 03:49:55,840 so 6084 03:49:55,840 --> 03:49:57,359 instead of printing this generator 6085 03:49:57,359 --> 03:49:59,040 object let's just 6086 03:49:59,040 --> 03:50:02,239 iterate over it using a for loop so 6087 03:50:02,239 --> 03:50:03,120 for 6088 03:50:03,120 --> 03:50:04,960 file in 6089 03:50:04,960 --> 03:50:06,960 the generator object that is returned 6090 03:50:06,960 --> 03:50:08,720 from the glob method 6091 03:50:08,720 --> 03:50:10,319 let's print 6092 03:50:10,319 --> 03:50:12,640 file 6093 03:50:12,640 --> 03:50:15,120 run our program there you go 6094 03:50:15,120 --> 03:50:17,520 so these are all the pi files in my 6095 03:50:17,520 --> 03:50:19,199 current directory this could be 6096 03:50:19,199 --> 03:50:20,800 different on your machine if you didn't 6097 03:50:20,800 --> 03:50:23,120 follow the exercises i gave you earlier 6098 03:50:23,120 --> 03:50:25,840 for example this utils.pi we created 6099 03:50:25,840 --> 03:50:27,760 this as part of one of the exercises 6100 03:50:27,760 --> 03:50:29,680 earlier in this course so don't worry if 6101 03:50:29,680 --> 03:50:31,120 the files you see here are different on 6102 03:50:31,120 --> 03:50:33,040 your machine 6103 03:50:33,040 --> 03:50:35,439 so we can use the glob method to search 6104 03:50:35,439 --> 03:50:37,760 for files using a pattern we can also 6105 03:50:37,760 --> 03:50:39,840 get all the files and directories in the 6106 03:50:39,840 --> 03:50:41,040 current path 6107 03:50:41,040 --> 03:50:44,239 so we just use one asterisk 6108 03:50:44,239 --> 03:50:47,279 run our program take a look 6109 03:50:47,279 --> 03:50:49,920 so we have this ecommerce directory we 6110 03:50:49,920 --> 03:50:51,760 have notes.txt 6111 03:50:51,760 --> 03:50:53,439 this is a file i added earlier just to 6112 03:50:53,439 --> 03:50:55,359 write notes as part of my presentation 6113 03:50:55,359 --> 03:50:57,279 so you're not going to have this file 6114 03:50:57,279 --> 03:50:58,399 you also have a bunch of other 6115 03:50:58,399 --> 03:51:02,279 directories and files 6116 03:51:05,359 --> 03:51:07,439 even though python's standard library is 6117 03:51:07,439 --> 03:51:09,279 very comprehensive and has so many 6118 03:51:09,279 --> 03:51:11,439 modules for common tasks it's not 6119 03:51:11,439 --> 03:51:13,680 complete by any means that's why we have 6120 03:51:13,680 --> 03:51:16,319 this directory called python package 6121 03:51:16,319 --> 03:51:18,880 index or pypi and in this directory you 6122 03:51:18,880 --> 03:51:21,120 can find thousands of packages for doing 6123 03:51:21,120 --> 03:51:23,120 awesome things these are the packages 6124 03:51:23,120 --> 03:51:25,199 that people like you and i have built as 6125 03:51:25,199 --> 03:51:27,040 part of their projects and then they 6126 03:51:27,040 --> 03:51:28,880 published it in this directory for other 6127 03:51:28,880 --> 03:51:30,880 people to use so that's why we have a 6128 03:51:30,880 --> 03:51:33,120 fantastic community in python there are 6129 03:51:33,120 --> 03:51:34,800 so many python developers out there and 6130 03:51:34,800 --> 03:51:37,199 there's so many packages right in front 6131 03:51:37,199 --> 03:51:40,080 of us to reuse in our programs let's say 6132 03:51:40,080 --> 03:51:41,920 you want to send text messages in your 6133 03:51:41,920 --> 03:51:43,439 programs you don't have to build this 6134 03:51:43,439 --> 03:51:45,120 functionality from scratch you can 6135 03:51:45,120 --> 03:51:46,359 simply come to 6136 03:51:46,359 --> 03:51:49,279 pipi.org and search for 6137 03:51:49,279 --> 03:51:50,640 sms 6138 03:51:50,640 --> 03:51:52,000 as you can see there are so many 6139 03:51:52,000 --> 03:51:54,399 projects or packages that give you this 6140 03:51:54,399 --> 03:51:56,239 functionality of course not every 6141 03:51:56,239 --> 03:51:58,960 package not every project is complete or 6142 03:51:58,960 --> 03:52:00,800 bug-free some of them are still in 6143 03:52:00,800 --> 03:52:03,199 development or they have bugs but if you 6144 03:52:03,199 --> 03:52:05,199 look around i'm pretty sure for any kind 6145 03:52:05,199 --> 03:52:07,040 of functionality you can imagine there 6146 03:52:07,040 --> 03:52:09,600 is a good reliable and well-documented 6147 03:52:09,600 --> 03:52:11,920 package for you in my complete python 6148 03:52:11,920 --> 03:52:13,520 course i will show you some of the most 6149 03:52:13,520 --> 03:52:16,640 useful packages on pi pi for example you 6150 03:52:16,640 --> 03:52:19,279 will learn how to access the information 6151 03:52:19,279 --> 03:52:21,920 on yelp.com in your programs so you can 6152 03:52:21,920 --> 03:52:23,600 write your program and search for 6153 03:52:23,600 --> 03:52:26,399 businesses registered on yelp let's say 6154 03:52:26,399 --> 03:52:28,399 you want to search for the best barbers 6155 03:52:28,399 --> 03:52:30,000 in new york city i will show you how to 6156 03:52:30,000 --> 03:52:31,600 do that you also learn about the 6157 03:52:31,600 --> 03:52:33,680 technique called web scraping which 6158 03:52:33,680 --> 03:52:35,520 means you can build an engine and have 6159 03:52:35,520 --> 03:52:38,319 that engine browse a website 6160 03:52:38,319 --> 03:52:41,040 and extract information from html files 6161 03:52:41,040 --> 03:52:42,720 this is the same technique that google 6162 03:52:42,720 --> 03:52:45,120 does to index various websites so they 6163 03:52:45,120 --> 03:52:47,600 have several engines or web crawlers 6164 03:52:47,600 --> 03:52:49,760 these crawlers are continuously looking 6165 03:52:49,760 --> 03:52:52,160 at various websites and extracting 6166 03:52:52,160 --> 03:52:54,160 information from their pages when you 6167 03:52:54,160 --> 03:52:56,640 publish a blog post google's web crawler 6168 03:52:56,640 --> 03:52:58,640 finds the title of your post it's 6169 03:52:58,640 --> 03:53:00,479 keywords and so on so i'm going to show 6170 03:53:00,479 --> 03:53:02,160 you how to do that with python we'll 6171 03:53:02,160 --> 03:53:04,080 also look at browser automation and this 6172 03:53:04,080 --> 03:53:06,080 is extremely powerful because you can 6173 03:53:06,080 --> 03:53:08,160 automate testing of your web 6174 03:53:08,160 --> 03:53:10,399 applications so you might get a job as a 6175 03:53:10,399 --> 03:53:12,960 test engineer and your daily job is to 6176 03:53:12,960 --> 03:53:14,720 browse a website click here and there 6177 03:53:14,720 --> 03:53:16,880 fill out this form that form and ensure 6178 03:53:16,880 --> 03:53:19,120 that the website is functioning properly 6179 03:53:19,120 --> 03:53:20,800 you can automate all of that with a 6180 03:53:20,800 --> 03:53:23,199 powerful python package called selenium 6181 03:53:23,199 --> 03:53:24,880 as i've shown in my complete python 6182 03:53:24,880 --> 03:53:26,080 course 6183 03:53:26,080 --> 03:53:27,439 now in this tutorial i'm going to show 6184 03:53:27,439 --> 03:53:30,199 you how to install packages from 6185 03:53:30,199 --> 03:53:31,760 pipi.org 6186 03:53:31,760 --> 03:53:34,880 so here on pipi.org let's search for 6187 03:53:34,880 --> 03:53:36,479 open pi 6188 03:53:36,479 --> 03:53:38,800 xl this is a package that we use for 6189 03:53:38,800 --> 03:53:41,439 working with excel spreadsheets so let's 6190 03:53:41,439 --> 03:53:43,120 search for this package 6191 03:53:43,120 --> 03:53:44,800 there you go so currently it's at 6192 03:53:44,800 --> 03:53:47,120 version 2.5 6193 03:53:47,120 --> 03:53:49,199 on the top you can see this instruction 6194 03:53:49,199 --> 03:53:52,880 pip install open pi excel 6195 03:53:52,880 --> 03:53:54,479 this is the command that we need to type 6196 03:53:54,479 --> 03:53:56,720 into a terminal window to install this 6197 03:53:56,720 --> 03:53:59,040 package into our program 6198 03:53:59,040 --> 03:54:01,279 so back to pie chart here on the status 6199 03:54:01,279 --> 03:54:04,000 bar we can open up a terminal window for 6200 03:54:04,000 --> 03:54:06,720 executing various commands so let's open 6201 03:54:06,720 --> 03:54:07,439 it 6202 03:54:07,439 --> 03:54:08,960 now if you're on windows you're probably 6203 03:54:08,960 --> 03:54:10,560 going to see something different instead 6204 03:54:10,560 --> 03:54:12,479 of this dollar sign don't worry about 6205 03:54:12,479 --> 03:54:14,640 that just execute this command i'm going 6206 03:54:14,640 --> 03:54:17,120 to show you so pip 6207 03:54:17,120 --> 03:54:19,359 install open 6208 03:54:19,359 --> 03:54:22,560 pi excel make sure to spell everything 6209 03:54:22,560 --> 03:54:25,120 properly so pip is the tool that comes 6210 03:54:25,120 --> 03:54:27,040 with python installation and we use it 6211 03:54:27,040 --> 03:54:29,279 to install or uninstall packages 6212 03:54:29,279 --> 03:54:30,840 registered on 6213 03:54:30,840 --> 03:54:33,359 pipi.org so here we're using pip to 6214 03:54:33,359 --> 03:54:37,439 install this package open pi xl 6215 03:54:37,439 --> 03:54:39,600 enter let's go ahead with that it's 6216 03:54:39,600 --> 03:54:42,080 going to take a few seconds okay done 6217 03:54:42,080 --> 03:54:44,080 now we can import this package and its 6218 03:54:44,080 --> 03:54:46,080 modules just like the built-in modules 6219 03:54:46,080 --> 03:54:48,720 in python or the modules that we have in 6220 03:54:48,720 --> 03:54:50,080 our project 6221 03:54:50,080 --> 03:54:52,160 now if you're curious where this package 6222 03:54:52,160 --> 03:54:54,720 is stored on disk let me show you so 6223 03:54:54,720 --> 03:54:57,040 open up the project panel and then 6224 03:54:57,040 --> 03:54:59,600 expand external libraries 6225 03:54:59,600 --> 03:55:01,840 next expand python3 6226 03:55:01,840 --> 03:55:05,199 and then expand site packages this is 6227 03:55:05,199 --> 03:55:06,800 where all these packages that we 6228 03:55:06,800 --> 03:55:09,040 installed with pip are stored so here we 6229 03:55:09,040 --> 03:55:13,439 have open pi excel you can open this 6230 03:55:13,439 --> 03:55:15,359 in this folder we have another folder 6231 03:55:15,359 --> 03:55:16,720 called cell 6232 03:55:16,720 --> 03:55:19,120 now look what we have here we have this 6233 03:55:19,120 --> 03:55:21,680 init pi area i told you that whenever we 6234 03:55:21,680 --> 03:55:24,239 add this file to a folder python treats 6235 03:55:24,239 --> 03:55:26,800 that folder as a package so cell is a 6236 03:55:26,800 --> 03:55:28,880 package and in this package we have 6237 03:55:28,880 --> 03:55:31,760 these modules cell interface read-only 6238 03:55:31,760 --> 03:55:34,640 and text so as you can see this package 6239 03:55:34,640 --> 03:55:37,680 open pi excel has several sub packages 6240 03:55:37,680 --> 03:55:40,720 like cell chart chart sheet and so on 6241 03:55:40,720 --> 03:55:42,560 and in each package we have several 6242 03:55:42,560 --> 03:55:43,600 modules 6243 03:55:43,600 --> 03:55:45,120 in the next tutorial i will show you how 6244 03:55:45,120 --> 03:55:48,399 to work with excel files 6245 03:55:48,479 --> 03:55:50,399 so we have covered all the core concepts 6246 03:55:50,399 --> 03:55:52,319 in python i hope you learned a lot 6247 03:55:52,319 --> 03:55:54,319 please support my hard work by liking 6248 03:55:54,319 --> 03:55:56,239 this video and sharing it with others 6249 03:55:56,239 --> 03:55:58,000 and be sure to subscribe to my channel 6250 03:55:58,000 --> 03:56:00,080 for more tutorials like this 6251 03:56:00,080 --> 03:56:01,760 next we're going to work on our first 6252 03:56:01,760 --> 03:56:04,239 python project that involves automation 6253 03:56:04,239 --> 03:56:05,840 you're going to write a python program 6254 03:56:05,840 --> 03:56:07,680 that can process thousands of 6255 03:56:07,680 --> 03:56:10,319 spreadsheets in under a second are you 6256 03:56:10,319 --> 03:56:13,920 excited let's get started 6257 03:56:15,840 --> 03:56:17,199 in this tutorial i'm going to show you 6258 03:56:17,199 --> 03:56:18,960 something really awesome you can do with 6259 03:56:18,960 --> 03:56:21,120 python you're going to learn how to 6260 03:56:21,120 --> 03:56:22,800 process spreadsheets and this is 6261 03:56:22,800 --> 03:56:25,359 extremely valuable in situations where 6262 03:56:25,359 --> 03:56:26,960 you have hundreds or thousands of 6263 03:56:26,960 --> 03:56:29,199 spreadsheets that need to be updated if 6264 03:56:29,199 --> 03:56:31,199 you want to do this by hand it may take 6265 03:56:31,199 --> 03:56:34,160 hours days weeks or even months you can 6266 03:56:34,160 --> 03:56:36,080 simply build a python program in less 6267 03:56:36,080 --> 03:56:37,920 than half an hour and have that python 6268 03:56:37,920 --> 03:56:40,000 program automatically process thousands 6269 03:56:40,000 --> 03:56:42,640 of spreadsheets in under one second 6270 03:56:42,640 --> 03:56:44,720 so here we're going to work on this 6271 03:56:44,720 --> 03:56:46,800 excel spreadsheet you can download this 6272 03:56:46,800 --> 03:56:49,120 below this video in this spreadsheet we 6273 03:56:49,120 --> 03:56:51,760 have three columns transaction id 6274 03:56:51,760 --> 03:56:53,680 product id and price 6275 03:56:53,680 --> 03:56:55,600 now don't worry about the fourth column 6276 03:56:55,600 --> 03:56:58,080 yet so in a spreadsheet we record all 6277 03:56:58,080 --> 03:57:00,319 kinds of transactions but let's say due 6278 03:57:00,319 --> 03:57:02,479 to an error it could be a human error or 6279 03:57:02,479 --> 03:57:04,399 a system error the price that we have 6280 03:57:04,399 --> 03:57:06,479 here is wrong let's say we need to 6281 03:57:06,479 --> 03:57:09,120 decrease this by 10 percent if you want 6282 03:57:09,120 --> 03:57:10,960 to do this manually we'll have to come 6283 03:57:10,960 --> 03:57:12,080 back here 6284 03:57:12,080 --> 03:57:14,960 and add a formula so we type out equal 6285 03:57:14,960 --> 03:57:17,120 sign let's say we want to recalculate 6286 03:57:17,120 --> 03:57:20,160 the value of this cell that is c2 so 6287 03:57:20,160 --> 03:57:22,000 we'll have to get that and then multiply 6288 03:57:22,000 --> 03:57:23,000 it by 6289 03:57:23,000 --> 03:57:26,239 0.9 so that is like the 90 6290 03:57:26,239 --> 03:57:28,239 of the original price we are reducing 6291 03:57:28,239 --> 03:57:30,000 the price by 10 6292 03:57:30,000 --> 03:57:31,680 now when we hit enter you can see the 6293 03:57:31,680 --> 03:57:32,960 updated price 6294 03:57:32,960 --> 03:57:34,880 next we'll have to come back here select 6295 03:57:34,880 --> 03:57:38,000 the cell and then apply the same formula 6296 03:57:38,000 --> 03:57:39,760 to all the other rows now what if we 6297 03:57:39,760 --> 03:57:41,680 have thousands of rows here we'll have 6298 03:57:41,680 --> 03:57:43,359 to scroll up and down this is going to 6299 03:57:43,359 --> 03:57:45,279 be very tedious so we're going to write 6300 03:57:45,279 --> 03:57:47,120 a python program that will do this for 6301 03:57:47,120 --> 03:57:49,520 us it's going to automate this process 6302 03:57:49,520 --> 03:57:52,640 and not only that it will also 6303 03:57:52,640 --> 03:57:54,960 add this beautiful 6304 03:57:54,960 --> 03:57:55,920 chart 6305 03:57:55,920 --> 03:57:57,199 right here 6306 03:57:57,199 --> 03:57:58,800 once again if you want to open up 6307 03:57:58,800 --> 03:58:00,640 hundreds or thousands of spreadsheets 6308 03:58:00,640 --> 03:58:02,399 and add a chart to each one this is 6309 03:58:02,399 --> 03:58:04,640 going to take at least a week or two our 6310 03:58:04,640 --> 03:58:06,319 python program is going to do all of 6311 03:58:06,319 --> 03:58:09,120 this in a matter of seconds so let's get 6312 03:58:09,120 --> 03:58:10,640 started 6313 03:58:10,640 --> 03:58:11,840 all right the first thing i want you to 6314 03:58:11,840 --> 03:58:14,800 do is to download this transactions file 6315 03:58:14,800 --> 03:58:16,640 and add it to your project i'll put the 6316 03:58:16,640 --> 03:58:18,080 link below this video for you to 6317 03:58:18,080 --> 03:58:19,120 download 6318 03:58:19,120 --> 03:58:21,040 so if you're wondering how to put this 6319 03:58:21,040 --> 03:58:22,880 in this project you can simply right 6320 03:58:22,880 --> 03:58:24,399 click the project 6321 03:58:24,399 --> 03:58:25,840 and 6322 03:58:25,840 --> 03:58:28,000 if you're on mac you have this item 6323 03:58:28,000 --> 03:58:30,640 reveal in finder if you're on windows 6324 03:58:30,640 --> 03:58:32,479 you should see something like reveal in 6325 03:58:32,479 --> 03:58:35,120 explorer or file explorer or windows 6326 03:58:35,120 --> 03:58:37,199 explorer whatever it's called when you 6327 03:58:37,199 --> 03:58:38,319 click this 6328 03:58:38,319 --> 03:58:40,640 this opens up the directory on your 6329 03:58:40,640 --> 03:58:43,040 machine where this project is stored so 6330 03:58:43,040 --> 03:58:44,880 you can simply copy and paste the 6331 03:58:44,880 --> 03:58:47,600 transactions file right here 6332 03:58:47,600 --> 03:58:49,600 now here on the top first we need to 6333 03:58:49,600 --> 03:58:53,120 import the open pi excel package so 6334 03:58:53,120 --> 03:58:56,720 import open pi excel we can give it an 6335 03:58:56,720 --> 03:59:00,080 alias to make our code shorter so as 6336 03:59:00,080 --> 03:59:02,399 excel this is not required but it just 6337 03:59:02,399 --> 03:59:04,160 makes our code a little bit cleaner let 6338 03:59:04,160 --> 03:59:06,239 me show you so now instead of typing 6339 03:59:06,239 --> 03:59:08,479 open pi excel dot 6340 03:59:08,479 --> 03:59:12,560 we type x l dot okay so that's an alias 6341 03:59:12,560 --> 03:59:13,920 for this package 6342 03:59:13,920 --> 03:59:15,359 now in this package we have a function 6343 03:59:15,359 --> 03:59:18,560 for loading an excel workbook so let's 6344 03:59:18,560 --> 03:59:20,960 call load workbook 6345 03:59:20,960 --> 03:59:21,840 and 6346 03:59:21,840 --> 03:59:25,520 as the argument we pass transactions dot 6347 03:59:25,520 --> 03:59:27,439 xlsx 6348 03:59:27,439 --> 03:59:29,520 so this will load our excel workbook and 6349 03:59:29,520 --> 03:59:32,800 return a workbook object 6350 03:59:32,800 --> 03:59:33,680 okay 6351 03:59:33,680 --> 03:59:35,199 now if you open this workbook you can 6352 03:59:35,199 --> 03:59:37,439 see you have only one sheet and that is 6353 03:59:37,439 --> 03:59:40,640 called sheet one so to access that we 6354 03:59:40,640 --> 03:59:42,960 use square brackets 6355 03:59:42,960 --> 03:59:44,880 and specify the name of the sheet that 6356 03:59:44,880 --> 03:59:48,239 is sheet one make sure to spell this 6357 03:59:48,239 --> 03:59:50,160 with a capital s because this is case 6358 03:59:50,160 --> 03:59:52,080 sensitive if you don't split it properly 6359 03:59:52,080 --> 03:59:53,600 you're going to get an error 6360 03:59:53,600 --> 03:59:55,199 so this returns 6361 03:59:55,199 --> 03:59:57,279 our first sheet 6362 03:59:57,279 --> 04:00:00,080 now in this sheet we have various cells 6363 04:00:00,080 --> 04:00:02,080 so next you need to learn how to access 6364 04:00:02,080 --> 04:00:04,880 a particular cell and that is very easy 6365 04:00:04,880 --> 04:00:06,720 back to pycharm 6366 04:00:06,720 --> 04:00:09,040 with this sheet object we can use square 6367 04:00:09,040 --> 04:00:11,680 brackets and give the coordinate of a 6368 04:00:11,680 --> 04:00:12,720 cell 6369 04:00:12,720 --> 04:00:14,479 the coordinate is the combination of the 6370 04:00:14,479 --> 04:00:17,840 column and the row for example this cell 6371 04:00:17,840 --> 04:00:20,560 its coordinate is a one 6372 04:00:20,560 --> 04:00:23,680 so back to pycharm here we can type a1 6373 04:00:23,680 --> 04:00:25,600 and this returns 6374 04:00:25,600 --> 04:00:26,960 our first cell 6375 04:00:26,960 --> 04:00:28,720 there is also another approach to get a 6376 04:00:28,720 --> 04:00:31,199 cell instead of using square brackets 6377 04:00:31,199 --> 04:00:32,560 with a string 6378 04:00:32,560 --> 04:00:33,680 we can use 6379 04:00:33,680 --> 04:00:35,920 the cell method of the sheet object so 6380 04:00:35,920 --> 04:00:38,800 sheet that cell we pass the row and the 6381 04:00:38,800 --> 04:00:41,920 column so row one and column one 6382 04:00:41,920 --> 04:00:45,439 this returns the exact same cell 6383 04:00:45,439 --> 04:00:47,600 now let's print the value of the cell so 6384 04:00:47,600 --> 04:00:49,680 cell that value 6385 04:00:49,680 --> 04:00:51,920 and run our program 6386 04:00:51,920 --> 04:00:55,840 so the value is transaction id beautiful 6387 04:00:55,840 --> 04:00:57,920 so now we need to iterate over all these 6388 04:00:57,920 --> 04:01:00,080 rows and for each row we need to get the 6389 04:01:00,080 --> 04:01:02,000 value in the third column that is the 6390 04:01:02,000 --> 04:01:04,239 price we'll get that and then multiply 6391 04:01:04,239 --> 04:01:06,640 it by 0.9 6392 04:01:06,640 --> 04:01:08,720 so first we need to know how many rows 6393 04:01:08,720 --> 04:01:10,880 we have in this spreadsheet we can get 6394 04:01:10,880 --> 04:01:13,359 that from the sheet object so sheet 6395 04:01:13,359 --> 04:01:14,160 has 6396 04:01:14,160 --> 04:01:17,359 an attribute called max underline row 6397 04:01:17,359 --> 04:01:19,279 let's print this 6398 04:01:19,279 --> 04:01:22,319 so in this sheet we have four rows 6399 04:01:22,319 --> 04:01:24,560 and you can verify that here so we have 6400 04:01:24,560 --> 04:01:26,720 a total of four rows 6401 04:01:26,720 --> 04:01:29,040 so we need to add a for loop that would 6402 04:01:29,040 --> 04:01:32,319 generate the numbers one to four so 6403 04:01:32,319 --> 04:01:34,640 let's delete this print statement 6404 04:01:34,640 --> 04:01:37,680 and add a for loop for row in we're 6405 04:01:37,680 --> 04:01:38,960 going to use the range function to 6406 04:01:38,960 --> 04:01:41,040 generate a range of numbers 6407 04:01:41,040 --> 04:01:42,640 starting from 1 6408 04:01:42,640 --> 04:01:46,160 all the way to sheet dot max underline 6409 04:01:46,160 --> 04:01:46,960 row 6410 04:01:46,960 --> 04:01:49,520 plus one the reason we're adding one to 6411 04:01:49,520 --> 04:01:51,600 this is because as i told you before 6412 04:01:51,600 --> 04:01:53,040 this range function would generate 6413 04:01:53,040 --> 04:01:55,680 numbers starting from this value all the 6414 04:01:55,680 --> 04:01:57,680 way to this value but it will not 6415 04:01:57,680 --> 04:02:00,479 include the second value so currently 6416 04:02:00,479 --> 04:02:03,600 max row returns four if we use range of 6417 04:02:03,600 --> 04:02:05,120 one to four this would generate the 6418 04:02:05,120 --> 04:02:08,399 numbers one two and three but not four 6419 04:02:08,399 --> 04:02:10,239 so to include four we need to add one to 6420 04:02:10,239 --> 04:02:11,279 it 6421 04:02:11,279 --> 04:02:12,800 let's go back here 6422 04:02:12,800 --> 04:02:16,319 so we type sheet.max underline row plus 6423 04:02:16,319 --> 04:02:17,600 1. 6424 04:02:17,600 --> 04:02:19,040 now before going any further let's just 6425 04:02:19,040 --> 04:02:21,359 print row and make sure our program is 6426 04:02:21,359 --> 04:02:23,120 working up to this point 6427 04:02:23,120 --> 04:02:24,080 so 6428 04:02:24,080 --> 04:02:26,800 let's run this we get the numbers one to 6429 04:02:26,800 --> 04:02:29,680 four beautiful so now we can easily get 6430 04:02:29,680 --> 04:02:33,120 the cells in the third column however we 6431 04:02:33,120 --> 04:02:35,040 don't really want this first cell that's 6432 04:02:35,040 --> 04:02:36,880 the heading so technically we want to 6433 04:02:36,880 --> 04:02:39,040 ignore the first row 6434 04:02:39,040 --> 04:02:40,720 back here we want to change our range 6435 04:02:40,720 --> 04:02:42,960 function and start from two 6436 04:02:42,960 --> 04:02:44,000 okay 6437 04:02:44,000 --> 04:02:44,800 now 6438 04:02:44,800 --> 04:02:47,359 we're going to use sheet dot cell 6439 04:02:47,359 --> 04:02:50,399 to get access to the cell at this row 6440 04:02:50,399 --> 04:02:52,720 and the column should be three 6441 04:02:52,720 --> 04:02:56,000 we get a cell object let's print its 6442 04:02:56,000 --> 04:02:57,680 value and make sure 6443 04:02:57,680 --> 04:02:59,120 we're on the right track 6444 04:02:59,120 --> 04:03:01,439 so run our program 6445 04:03:01,439 --> 04:03:03,239 these are the values we get 6446 04:03:03,239 --> 04:03:07,680 5.95 6.95 and 7.95 6447 04:03:07,680 --> 04:03:09,760 these are the values in the third column 6448 04:03:09,760 --> 04:03:11,920 beautiful next we need to multiply each 6449 04:03:11,920 --> 04:03:14,800 value by 0.9 6450 04:03:14,800 --> 04:03:17,600 so instead of printing cell.value we 6451 04:03:17,600 --> 04:03:20,640 multiply it by 0.9 and this is the 6452 04:03:20,640 --> 04:03:22,800 corrected 6453 04:03:22,800 --> 04:03:24,800 underlying price 6454 04:03:24,800 --> 04:03:26,160 so this is the mistake we're going to 6455 04:03:26,160 --> 04:03:27,840 fix with our program 6456 04:03:27,840 --> 04:03:29,600 now we need to add a new cell to our 6457 04:03:29,600 --> 04:03:30,800 worksheet 6458 04:03:30,800 --> 04:03:32,399 so we want to add all the corrected 6459 04:03:32,399 --> 04:03:34,720 prices in a new column we could also 6460 04:03:34,720 --> 04:03:36,720 overwrite the values in this column but 6461 04:03:36,720 --> 04:03:38,160 in this tutorial i'm going to show you 6462 04:03:38,160 --> 04:03:40,479 how to add a new column 6463 04:03:40,479 --> 04:03:42,560 so using the same method now we need to 6464 04:03:42,560 --> 04:03:45,040 get a reference to the cell in the given 6465 04:03:45,040 --> 04:03:47,279 row but in the fourth column 6466 04:03:47,279 --> 04:03:48,520 so we call 6467 04:03:48,520 --> 04:03:51,760 sheet.cell and pass row and 4 as the 6468 04:03:51,760 --> 04:03:52,960 arguments 6469 04:03:52,960 --> 04:03:55,040 once again this returns a cell object 6470 04:03:55,040 --> 04:03:57,040 let's store that in a separate variable 6471 04:03:57,040 --> 04:03:59,120 and call it corrected 6472 04:03:59,120 --> 04:04:02,399 underline price underline cell 6473 04:04:02,399 --> 04:04:04,399 so note that my variable names are very 6474 04:04:04,399 --> 04:04:06,960 descriptive there is no guesswork 6475 04:04:06,960 --> 04:04:08,800 we have corrected price which holds the 6476 04:04:08,800 --> 04:04:11,439 actual value and we have corrected price 6477 04:04:11,439 --> 04:04:13,359 cell which is a sell object in this 6478 04:04:13,359 --> 04:04:14,720 spreadsheet 6479 04:04:14,720 --> 04:04:16,479 next we need to set the value for this 6480 04:04:16,479 --> 04:04:20,319 cell so we set corrected price sell dot 6481 04:04:20,319 --> 04:04:23,600 value to this corrected price 6482 04:04:23,600 --> 04:04:25,040 with these view lines we can easily 6483 04:04:25,040 --> 04:04:26,960 update our spreadsheet but first we need 6484 04:04:26,960 --> 04:04:29,359 to save it so 6485 04:04:29,359 --> 04:04:32,880 after our for loop we call workbook dot 6486 04:04:32,880 --> 04:04:35,520 save now let's save this in a new file 6487 04:04:35,520 --> 04:04:36,720 so we don't want to accidentally 6488 04:04:36,720 --> 04:04:38,800 overwrite the original file in case our 6489 04:04:38,800 --> 04:04:40,399 program has a bug 6490 04:04:40,399 --> 04:04:41,600 so i'm going to call that 6491 04:04:41,600 --> 04:04:44,600 transactions2.xlsx 6492 04:04:45,439 --> 04:04:48,720 now finally let's run our program 6493 04:04:48,720 --> 04:04:50,800 so you can see that happened in a split 6494 04:04:50,800 --> 04:04:53,120 of a second now here we have a new file 6495 04:04:53,120 --> 04:04:55,520 transactions2 when we open this we get 6496 04:04:55,520 --> 04:04:57,199 something like this so you can see we 6497 04:04:57,199 --> 04:04:59,439 have this fourth column with the updated 6498 04:04:59,439 --> 04:05:01,760 prices beautiful so half of the problem 6499 04:05:01,760 --> 04:05:03,760 is solved now we need to add a chart 6500 04:05:03,760 --> 04:05:04,640 here 6501 04:05:04,640 --> 04:05:06,080 to add a chart we need to import a 6502 04:05:06,080 --> 04:05:07,920 couple of classes on the top 6503 04:05:07,920 --> 04:05:08,720 so 6504 04:05:08,720 --> 04:05:11,040 after this import statement let's type 6505 04:05:11,040 --> 04:05:12,479 out from 6506 04:05:12,479 --> 04:05:15,120 open pi excel dot chart 6507 04:05:15,120 --> 04:05:16,560 import 6508 04:05:16,560 --> 04:05:18,399 bar chart 6509 04:05:18,399 --> 04:05:19,520 comma 6510 04:05:19,520 --> 04:05:20,960 reference 6511 04:05:20,960 --> 04:05:22,960 so let's see what's going on here 6512 04:05:22,960 --> 04:05:25,359 in this package we have a module called 6513 04:05:25,359 --> 04:05:27,199 chart and from this module we're 6514 04:05:27,199 --> 04:05:29,760 importing two classes bar chart and 6515 04:05:29,760 --> 04:05:31,600 reference again look at the naming 6516 04:05:31,600 --> 04:05:33,840 convention used here the first letter of 6517 04:05:33,840 --> 04:05:36,160 every word is capitalized 6518 04:05:36,160 --> 04:05:38,160 also it's better to add a line break 6519 04:05:38,160 --> 04:05:40,319 after our import statement to make our 6520 04:05:40,319 --> 04:05:41,600 code cleaner 6521 04:05:41,600 --> 04:05:42,800 so 6522 04:05:42,800 --> 04:05:44,560 before we save our workbook we need to 6523 04:05:44,560 --> 04:05:46,720 add a chart to the current sheet 6524 04:05:46,720 --> 04:05:48,399 first we need to select a range of 6525 04:05:48,399 --> 04:05:50,399 values for this exercise i'm going to 6526 04:05:50,399 --> 04:05:52,880 select the values in the fourth column 6527 04:05:52,880 --> 04:05:56,160 so all the values in rows 2 to 4. these 6528 04:05:56,160 --> 04:05:57,600 are the values i'm going to use in our 6529 04:05:57,600 --> 04:05:58,479 chart 6530 04:05:58,479 --> 04:05:59,760 now in this particular example it 6531 04:05:59,760 --> 04:06:01,920 doesn't really make sense to add a chart 6532 04:06:01,920 --> 04:06:04,000 per transaction but that doesn't really 6533 04:06:04,000 --> 04:06:05,840 matter we just want a bunch of numbers 6534 04:06:05,840 --> 04:06:07,600 to create a chart 6535 04:06:07,600 --> 04:06:09,920 so after our for loop we're going to use 6536 04:06:09,920 --> 04:06:12,560 the reference class to select a range of 6537 04:06:12,560 --> 04:06:15,199 values now the first argument in the 6538 04:06:15,199 --> 04:06:17,199 constructor is the sheet 6539 04:06:17,199 --> 04:06:19,040 so we pass our sheet here 6540 04:06:19,040 --> 04:06:22,000 then we add four keyword arguments the 6541 04:06:22,000 --> 04:06:24,479 first one is min underline row we set 6542 04:06:24,479 --> 04:06:25,760 this to two 6543 04:06:25,760 --> 04:06:28,319 the second one is max underline row we 6544 04:06:28,319 --> 04:06:30,720 set this to the maximum row in this 6545 04:06:30,720 --> 04:06:33,920 sheet that is sheet dot max underline 6546 04:06:33,920 --> 04:06:36,399 row so we want to select the cells in 6547 04:06:36,399 --> 04:06:37,600 row two 6548 04:06:37,600 --> 04:06:39,359 to four okay 6549 04:06:39,359 --> 04:06:40,800 now to make this code a little bit 6550 04:06:40,800 --> 04:06:43,359 cleaner i'm going to put this on a new 6551 04:06:43,359 --> 04:06:46,560 line so you can see clearly 6552 04:06:46,560 --> 04:06:48,000 now this is going to select all the 6553 04:06:48,000 --> 04:06:50,640 cells in all the columns in these rows 6554 04:06:50,640 --> 04:06:53,120 that's not what we want we only want the 6555 04:06:53,120 --> 04:06:55,680 values in the fourth column 6556 04:06:55,680 --> 04:06:57,680 so back here we need to set a couple 6557 04:06:57,680 --> 04:07:00,399 more keyword arguments min underline 6558 04:07:00,399 --> 04:07:03,760 call or column we set that to four and 6559 04:07:03,760 --> 04:07:06,160 also max underlying call we set that to 6560 04:07:06,160 --> 04:07:07,359 four as well 6561 04:07:07,359 --> 04:07:08,880 so we're limiting the range of cells 6562 04:07:08,880 --> 04:07:11,439 we're selecting to the fourth column 6563 04:07:11,439 --> 04:07:13,120 okay now 6564 04:07:13,120 --> 04:07:14,800 we're creating an instance of the 6565 04:07:14,800 --> 04:07:16,960 reference class let's store the result 6566 04:07:16,960 --> 04:07:19,600 in a variable called values so this 6567 04:07:19,600 --> 04:07:22,160 values object will have all these values 6568 04:07:22,160 --> 04:07:24,160 in the fourth column now we are ready to 6569 04:07:24,160 --> 04:07:25,760 create a chart 6570 04:07:25,760 --> 04:07:27,840 so we create an instance of the bar 6571 04:07:27,840 --> 04:07:29,680 chart class 6572 04:07:29,680 --> 04:07:32,399 and store it in this object 6573 04:07:32,399 --> 04:07:35,600 next we call chart that add underlying 6574 04:07:35,600 --> 04:07:38,479 data and pass our values 6575 04:07:38,479 --> 04:07:40,319 that is pretty straightforward finally 6576 04:07:40,319 --> 04:07:42,800 we need to add this chart to our sheet 6577 04:07:42,800 --> 04:07:46,239 so we call sheet that add underline 6578 04:07:46,239 --> 04:07:47,279 chart 6579 04:07:47,279 --> 04:07:49,920 and pass this chart object we should 6580 04:07:49,920 --> 04:07:51,840 also specify where we want to add this 6581 04:07:51,840 --> 04:07:52,800 chart 6582 04:07:52,800 --> 04:07:54,560 let's say we want to add it here on row 6583 04:07:54,560 --> 04:07:57,199 2 after the fourth column so the 6584 04:07:57,199 --> 04:07:59,920 coordinates of this cell is e 2 and this 6585 04:07:59,920 --> 04:08:01,680 is going to be the top left corner of 6586 04:08:01,680 --> 04:08:02,880 our chart 6587 04:08:02,880 --> 04:08:05,040 so as the coordinate i'm going to pass 6588 04:08:05,040 --> 04:08:08,399 e2 that's all we had to do done 6589 04:08:08,399 --> 04:08:10,560 let's run our program one more time 6590 04:08:10,560 --> 04:08:12,720 beautiful let's open up the updated 6591 04:08:12,720 --> 04:08:13,840 workbook 6592 04:08:13,840 --> 04:08:16,399 and here's the end result so right after 6593 04:08:16,399 --> 04:08:18,080 the fourth column we have this beautiful 6594 04:08:18,080 --> 04:08:20,000 chart now we could take this to the next 6595 04:08:20,000 --> 04:08:21,840 level we could add a legend here we 6596 04:08:21,840 --> 04:08:23,600 could change the color of these bars we 6597 04:08:23,600 --> 04:08:25,199 could even use a different kind of chart 6598 04:08:25,199 --> 04:08:27,439 it doesn't have to be a bar chart so to 6599 04:08:27,439 --> 04:08:28,800 learn more about this you can read the 6600 04:08:28,800 --> 04:08:32,479 documentation for open pi excel so our 6601 04:08:32,479 --> 04:08:34,399 program is complete but our code is 6602 04:08:34,399 --> 04:08:36,960 dirty it's not clean it's not organized 6603 04:08:36,960 --> 04:08:38,800 we don't have any functions here we have 6604 04:08:38,800 --> 04:08:41,520 written all the code in app.pi so let's 6605 04:08:41,520 --> 04:08:43,359 go ahead and organize this code like a 6606 04:08:43,359 --> 04:08:45,680 professional software developer 6607 04:08:45,680 --> 04:08:47,600 so on line four we're loading our 6608 04:08:47,600 --> 04:08:49,439 workbook then we're getting a reference 6609 04:08:49,439 --> 04:08:51,279 to the first sheet but we don't really 6610 04:08:51,279 --> 04:08:52,800 need these two lines here this was 6611 04:08:52,800 --> 04:08:54,800 purely for demonstration to show you how 6612 04:08:54,800 --> 04:08:57,520 you can access a cell so let's delete 6613 04:08:57,520 --> 04:08:59,359 these unnecessary lines this is a very 6614 04:08:59,359 --> 04:09:01,199 important practice as you're coding 6615 04:09:01,199 --> 04:09:03,199 always look at your code always review 6616 04:09:03,199 --> 04:09:04,960 it see if you have some code that is not 6617 04:09:04,960 --> 04:09:07,040 used always delete those 6618 04:09:07,040 --> 04:09:09,439 okay next we're iterating over all the 6619 04:09:09,439 --> 04:09:12,720 rows fixing the prices and then we 6620 04:09:12,720 --> 04:09:14,399 select these values to add a chart and 6621 04:09:14,399 --> 04:09:17,040 finally we save the workbook now if we 6622 04:09:17,040 --> 04:09:19,279 were going to use this to automate the 6623 04:09:19,279 --> 04:09:21,279 process of updating thousands of 6624 04:09:21,279 --> 04:09:23,359 spreadsheets this program wouldn't work 6625 04:09:23,359 --> 04:09:25,439 because it's only relying 6626 04:09:25,439 --> 04:09:27,680 on this file so we want to reorganize 6627 04:09:27,680 --> 04:09:29,760 this code and move it inside of a 6628 04:09:29,760 --> 04:09:31,520 function this function should take the 6629 04:09:31,520 --> 04:09:33,840 name of the file as an input so let's 6630 04:09:33,840 --> 04:09:36,159 define a function 6631 04:09:36,159 --> 04:09:40,080 let's call it process underline workbook 6632 04:09:40,080 --> 04:09:43,760 it gets a file name now we move all the 6633 04:09:43,760 --> 04:09:45,439 code inside of this function so let's 6634 04:09:45,439 --> 04:09:47,040 select everything 6635 04:09:47,040 --> 04:09:48,479 and then 6636 04:09:48,479 --> 04:09:51,279 press tab beautiful 6637 04:09:51,279 --> 04:09:52,080 now 6638 04:09:52,080 --> 04:09:53,439 back to our function 6639 04:09:53,439 --> 04:09:54,960 instead of loading this transactions 6640 04:09:54,960 --> 04:09:57,520 file we're going to load 6641 04:09:57,520 --> 04:10:00,720 this file name that's better 6642 04:10:00,720 --> 04:10:02,560 also now that our program is working 6643 04:10:02,560 --> 04:10:04,399 properly there is really no need to 6644 04:10:04,399 --> 04:10:07,920 store the result in a separate file so 6645 04:10:07,920 --> 04:10:10,239 here we can simply overwrite the same 6646 04:10:10,239 --> 04:10:11,120 file 6647 04:10:11,120 --> 04:10:12,080 so 6648 04:10:12,080 --> 04:10:14,319 let's pass file name 6649 04:10:14,319 --> 04:10:16,239 that is the end of our function 6650 04:10:16,239 --> 04:10:18,960 so now we have this reusable function we 6651 04:10:18,960 --> 04:10:21,120 can simply reuse this to process 6652 04:10:21,120 --> 04:10:22,720 thousands of spreadsheets 6653 04:10:22,720 --> 04:10:24,399 earlier in this python course you 6654 04:10:24,399 --> 04:10:26,159 learned how to find all the files in the 6655 04:10:26,159 --> 04:10:28,080 directory so you can get each file in a 6656 04:10:28,080 --> 04:10:30,319 directory and pass the name of the file 6657 04:10:30,319 --> 04:10:32,479 to this function this function will go 6658 04:10:32,479 --> 04:10:34,000 ahead and update that spreadsheet in 6659 04:10:34,000 --> 04:10:37,120 just a second or less 6660 04:10:37,359 --> 04:10:39,120 so that was just one example of using 6661 04:10:39,120 --> 04:10:41,760 python to automate repetitive boring 6662 04:10:41,760 --> 04:10:43,680 tasks that waste your time but 6663 04:10:43,680 --> 04:10:45,840 automation is not just about processing 6664 04:10:45,840 --> 04:10:47,359 excel spreadsheets there are so many 6665 04:10:47,359 --> 04:10:49,199 things we can automate here's a question 6666 04:10:49,199 --> 04:10:51,439 for you what do you want to automate how 6667 04:10:51,439 --> 04:10:53,040 do you want to use python to make your 6668 04:10:53,040 --> 04:10:55,279 life easier use the comment box below 6669 04:10:55,279 --> 04:10:56,720 and let me know i love to hear about 6670 04:10:56,720 --> 04:10:57,920 your ideas 6671 04:10:57,920 --> 04:10:59,279 next we're going to work on a project 6672 04:10:59,279 --> 04:11:01,199 that involves machine learning which is 6673 04:11:01,199 --> 04:11:03,520 a subset of artificial intelligence are 6674 04:11:03,520 --> 04:11:07,840 you excited let's get started 6675 04:11:09,279 --> 04:11:10,319 in this section you're going to learn 6676 04:11:10,319 --> 04:11:12,239 about machine learning which is a subset 6677 04:11:12,239 --> 04:11:14,720 of ai or artificial intelligence it's 6678 04:11:14,720 --> 04:11:16,560 one of the trending topics in the world 6679 04:11:16,560 --> 04:11:18,319 these days and it's going to have a lot 6680 04:11:18,319 --> 04:11:20,479 of applications in the future here's an 6681 04:11:20,479 --> 04:11:22,479 example imagine i ask you to write a 6682 04:11:22,479 --> 04:11:24,800 program to scan an image and tell if 6683 04:11:24,800 --> 04:11:26,880 it's a cat or a doc 6684 04:11:26,880 --> 04:11:28,399 if you want to build this program using 6685 04:11:28,399 --> 04:11:30,319 traditional programming techniques your 6686 04:11:30,319 --> 04:11:32,880 program is going to get overly complex 6687 04:11:32,880 --> 04:11:34,239 you will have to come up with lots of 6688 04:11:34,239 --> 04:11:37,040 rules to look for specific curves edges 6689 04:11:37,040 --> 04:11:39,279 and colors in an image to tell if it's a 6690 04:11:39,279 --> 04:11:41,520 cat or a dog but if i give you a black 6691 04:11:41,520 --> 04:11:43,520 and white photo your rules may not work 6692 04:11:43,520 --> 04:11:44,960 they may break then you'll have to 6693 04:11:44,960 --> 04:11:46,720 rewrite them or i might give you a 6694 04:11:46,720 --> 04:11:48,399 picture of a cat or a dog from a 6695 04:11:48,399 --> 04:11:50,319 different angle that you did not predict 6696 04:11:50,319 --> 04:11:52,720 before so solving this problem using 6697 04:11:52,720 --> 04:11:54,159 traditional programming techniques is 6698 04:11:54,159 --> 04:11:56,800 going to get overly complex or sometimes 6699 04:11:56,800 --> 04:11:59,359 impossible now to make the matter worse 6700 04:11:59,359 --> 04:12:00,960 what if in the future i ask you to 6701 04:12:00,960 --> 04:12:02,640 extend this program such that it 6702 04:12:02,640 --> 04:12:05,600 supports three kinds of animals cats 6703 04:12:05,600 --> 04:12:07,920 dogs and horses once again you'll have 6704 04:12:07,920 --> 04:12:10,159 to rewrite all those rules that's not 6705 04:12:10,159 --> 04:12:12,239 gonna work so machine learning is a 6706 04:12:12,239 --> 04:12:14,000 technique to solve these kind of 6707 04:12:14,000 --> 04:12:16,239 problems and this is how it works we 6708 04:12:16,239 --> 04:12:18,800 build a model or an engine and give it 6709 04:12:18,800 --> 04:12:21,199 lots and lots of data for example we 6710 04:12:21,199 --> 04:12:23,439 give you thousands or tens of thousands 6711 04:12:23,439 --> 04:12:26,080 of pictures of cats and dogs our model 6712 04:12:26,080 --> 04:12:28,479 will then find and learn patterns in the 6713 04:12:28,479 --> 04:12:30,560 input data so we can give it a new 6714 04:12:30,560 --> 04:12:32,399 picture of a cat that it hasn't seen 6715 04:12:32,399 --> 04:12:34,720 before and ask it is it a cat or a dog 6716 04:12:34,720 --> 04:12:36,399 or a horse and it will tell us with a 6717 04:12:36,399 --> 04:12:38,960 certain level of accuracy the more input 6718 04:12:38,960 --> 04:12:41,040 data we give it the more accurate our 6719 04:12:41,040 --> 04:12:42,640 model is going to be 6720 04:12:42,640 --> 04:12:44,720 so that was a very basic example but 6721 04:12:44,720 --> 04:12:46,640 machine learning has other applications 6722 04:12:46,640 --> 04:12:50,000 in self-driving cars robotics language 6723 04:12:50,000 --> 04:12:52,960 processing vision processing forecasting 6724 04:12:52,960 --> 04:12:54,960 things like stock market trends and the 6725 04:12:54,960 --> 04:12:57,439 weather games and so on so that's the 6726 04:12:57,439 --> 04:12:59,760 basic idea about machine learning next 6727 04:12:59,760 --> 04:13:03,359 we'll look at machine learning in action 6728 04:13:06,720 --> 04:13:08,319 a machine learning project involves a 6729 04:13:08,319 --> 04:13:10,560 number of steps the first step is to 6730 04:13:10,560 --> 04:13:12,720 import our data which often comes in the 6731 04:13:12,720 --> 04:13:14,720 form of a csv file we might have a 6732 04:13:14,720 --> 04:13:17,279 database with lots of data we can simply 6733 04:13:17,279 --> 04:13:19,760 export that data and store it in a csv 6734 04:13:19,760 --> 04:13:21,279 file for the purpose of our machine 6735 04:13:21,279 --> 04:13:24,239 learning project so we import our data 6736 04:13:24,239 --> 04:13:25,760 next we need to clean it and this 6737 04:13:25,760 --> 04:13:27,520 involves tasks such as removing 6738 04:13:27,520 --> 04:13:29,520 duplicated data if you have duplicates 6739 04:13:29,520 --> 04:13:31,040 in the data we don't want to feed this 6740 04:13:31,040 --> 04:13:33,279 to our model because otherwise our model 6741 04:13:33,279 --> 04:13:35,040 will learn bad patterns in the data and 6742 04:13:35,040 --> 04:13:37,279 it will produce the wrong result so we 6743 04:13:37,279 --> 04:13:39,040 should make sure that our input data is 6744 04:13:39,040 --> 04:13:41,040 in a good and clean shape if there are 6745 04:13:41,040 --> 04:13:42,960 data that is irrelevant we should remove 6746 04:13:42,960 --> 04:13:44,960 them if they are duplicated or 6747 04:13:44,960 --> 04:13:47,760 incomplete we can remove or modify them 6748 04:13:47,760 --> 04:13:50,000 if our data is text based like the name 6749 04:13:50,000 --> 04:13:53,040 of countries or genres of music or cats 6750 04:13:53,040 --> 04:13:55,120 and dogs we need to convert them to 6751 04:13:55,120 --> 04:13:57,520 numerical values so this step really 6752 04:13:57,520 --> 04:13:59,120 depends on the kind of data we are 6753 04:13:59,120 --> 04:14:01,840 working with every project is different 6754 04:14:01,840 --> 04:14:03,600 now that we have a clean data set we 6755 04:14:03,600 --> 04:14:06,000 need to split it into two segments one 6756 04:14:06,000 --> 04:14:08,479 for training our model and the other for 6757 04:14:08,479 --> 04:14:10,800 testing it to make sure that our model 6758 04:14:10,800 --> 04:14:12,880 produces the right result 6759 04:14:12,880 --> 04:14:14,319 for example if you have a thousand 6760 04:14:14,319 --> 04:14:16,720 pictures of cats and dogs we can reserve 6761 04:14:16,720 --> 04:14:18,560 eighty percent for training and the 6762 04:14:18,560 --> 04:14:20,720 other twenty percent for testing the 6763 04:14:20,720 --> 04:14:23,199 next step is to create a model and this 6764 04:14:23,199 --> 04:14:25,359 involves selecting an algorithm to 6765 04:14:25,359 --> 04:14:27,359 analyze the data there are so many 6766 04:14:27,359 --> 04:14:28,880 different machine learning algorithms 6767 04:14:28,880 --> 04:14:31,199 out there such as decision trees neural 6768 04:14:31,199 --> 04:14:33,600 networks and so on each algorithm has 6769 04:14:33,600 --> 04:14:36,159 pros and cons in terms of accuracy and 6770 04:14:36,159 --> 04:14:38,399 performance so the algorithm you choose 6771 04:14:38,399 --> 04:14:40,000 depends on the kind of problem you're 6772 04:14:40,000 --> 04:14:42,640 trying to solve and your input data now 6773 04:14:42,640 --> 04:14:44,000 the good news is that we don't have to 6774 04:14:44,000 --> 04:14:46,720 explicitly program an algorithm there 6775 04:14:46,720 --> 04:14:48,319 are libraries out there that provide 6776 04:14:48,319 --> 04:14:50,560 these algorithms one of the most popular 6777 04:14:50,560 --> 04:14:51,840 ones which we're going to look at in 6778 04:14:51,840 --> 04:14:54,319 this tutorial is scikit-learn 6779 04:14:54,319 --> 04:14:56,880 so we build a model using an algorithm 6780 04:14:56,880 --> 04:14:59,439 next we need to train our model so we 6781 04:14:59,439 --> 04:15:01,920 feed it our training data our model will 6782 04:15:01,920 --> 04:15:03,920 then look for the patterns in the data 6783 04:15:03,920 --> 04:15:05,840 so next we can ask it to make 6784 04:15:05,840 --> 04:15:08,000 predictions back to our example of cats 6785 04:15:08,000 --> 04:15:10,479 and dogs we can ask our model is this a 6786 04:15:10,479 --> 04:15:12,880 cat or a dog and our model will make a 6787 04:15:12,880 --> 04:15:14,000 prediction 6788 04:15:14,000 --> 04:15:15,680 now the prediction is not always 6789 04:15:15,680 --> 04:15:18,000 accurate in fact when you start out it's 6790 04:15:18,000 --> 04:15:19,680 very likely that your predictions are 6791 04:15:19,680 --> 04:15:21,760 inaccurate so we need to evaluate the 6792 04:15:21,760 --> 04:15:24,720 predictions and measure their accuracy 6793 04:15:24,720 --> 04:15:26,479 then we need to get back to our model 6794 04:15:26,479 --> 04:15:28,800 and either select a different algorithm 6795 04:15:28,800 --> 04:15:30,880 that is going to produce a more accurate 6796 04:15:30,880 --> 04:15:32,399 result for the kind of problem we're 6797 04:15:32,399 --> 04:15:34,960 trying to solve or fine-tune the 6798 04:15:34,960 --> 04:15:37,120 parameters of our model so each 6799 04:15:37,120 --> 04:15:38,800 algorithm has parameters that we can 6800 04:15:38,800 --> 04:15:41,760 modify to optimize the accuracy 6801 04:15:41,760 --> 04:15:43,199 so these are the high-level steps that 6802 04:15:43,199 --> 04:15:45,600 you follow in a machine learning project 6803 04:15:45,600 --> 04:15:47,120 next we'll look at the libraries and 6804 04:15:47,120 --> 04:15:50,319 tools for machine learning 6805 04:15:53,840 --> 04:15:54,880 in this lecture we're going to look at 6806 04:15:54,880 --> 04:15:57,199 the popular python libraries that we use 6807 04:15:57,199 --> 04:15:59,120 in machine learning projects the first 6808 04:15:59,120 --> 04:16:00,880 one is numpy which provides a 6809 04:16:00,880 --> 04:16:03,199 multi-dimensional array very very 6810 04:16:03,199 --> 04:16:06,000 popular library the second one is pandas 6811 04:16:06,000 --> 04:16:08,080 which is a data analysis library that 6812 04:16:08,080 --> 04:16:10,720 provides a concept called data frame a 6813 04:16:10,720 --> 04:16:12,640 data frame is a two-dimensional data 6814 04:16:12,640 --> 04:16:14,319 structure similar to an excel 6815 04:16:14,319 --> 04:16:17,040 spreadsheet so we have rows and columns 6816 04:16:17,040 --> 04:16:19,439 we can select data in a row or a column 6817 04:16:19,439 --> 04:16:22,080 or a range of rows and columns again 6818 04:16:22,080 --> 04:16:23,840 very very popular in machine learning 6819 04:16:23,840 --> 04:16:25,760 and data science projects 6820 04:16:25,760 --> 04:16:28,720 the third library is matplotlib which is 6821 04:16:28,720 --> 04:16:30,800 a two-dimensional plotting library for 6822 04:16:30,800 --> 04:16:32,640 creating graphs and plots 6823 04:16:32,640 --> 04:16:34,560 the next library is scikit-learn which 6824 04:16:34,560 --> 04:16:36,159 is one of the most popular machine 6825 04:16:36,159 --> 04:16:38,000 learning libraries that provides all 6826 04:16:38,000 --> 04:16:40,239 these common algorithms like decision 6827 04:16:40,239 --> 04:16:43,600 trees neural networks and so on 6828 04:16:43,600 --> 04:16:44,960 now when working with machine learning 6829 04:16:44,960 --> 04:16:46,800 projects we use an environment called 6830 04:16:46,800 --> 04:16:49,279 jupiter for writing our code technically 6831 04:16:49,279 --> 04:16:51,040 we can still use vs code or any other 6832 04:16:51,040 --> 04:16:53,120 code editors but these editors are not 6833 04:16:53,120 --> 04:16:54,880 ideal for machine learning projects 6834 04:16:54,880 --> 04:16:56,560 because we frequently need to inspect 6835 04:16:56,560 --> 04:16:58,640 the data and that is really hard in 6836 04:16:58,640 --> 04:17:01,040 environments like vs code and terminal 6837 04:17:01,040 --> 04:17:02,880 if you're working with a table of 10 or 6838 04:17:02,880 --> 04:17:05,040 20 columns visualizing this data in a 6839 04:17:05,040 --> 04:17:06,560 terminal window is really really 6840 04:17:06,560 --> 04:17:08,800 difficult and messy so that's why we use 6841 04:17:08,800 --> 04:17:10,800 jupiter it makes it really easy to 6842 04:17:10,800 --> 04:17:12,560 inspect our data 6843 04:17:12,560 --> 04:17:14,159 now to install jupiter we're going to 6844 04:17:14,159 --> 04:17:17,600 use a platform called anaconda so head 6845 04:17:17,600 --> 04:17:20,080 over to anaconda.com 6846 04:17:20,080 --> 04:17:21,279 download 6847 04:17:21,279 --> 04:17:22,960 on this page you can download anaconda 6848 04:17:22,960 --> 04:17:25,199 distribution for your operating system 6849 04:17:25,199 --> 04:17:28,319 so we have distributions for windows mac 6850 04:17:28,319 --> 04:17:31,920 and linux so let's go ahead and 6851 04:17:31,920 --> 04:17:36,080 install anaconda for python 3.7 6852 04:17:36,080 --> 04:17:39,080 download 6853 04:17:40,239 --> 04:17:42,000 all right so here's anaconda downloaded 6854 04:17:42,000 --> 04:17:45,199 on my machine let's double click this 6855 04:17:45,199 --> 04:17:46,399 all right first it's going to run a 6856 04:17:46,399 --> 04:17:48,239 program to determine if the software can 6857 04:17:48,239 --> 04:17:51,120 be installed so let's continue 6858 04:17:51,120 --> 04:17:52,960 and once again continue 6859 04:17:52,960 --> 04:17:55,359 continue pretty easy continue one more 6860 04:17:55,359 --> 04:17:58,319 time i agree with the license agreement 6861 04:17:58,319 --> 04:17:59,520 okay 6862 04:17:59,520 --> 04:18:01,120 you can use the default installation 6863 04:18:01,120 --> 04:18:03,279 location so don't worry about that just 6864 04:18:03,279 --> 04:18:04,880 click install 6865 04:18:04,880 --> 04:18:06,880 give it a few seconds now the beautiful 6866 04:18:06,880 --> 04:18:08,560 thing about anaconda is that it will 6867 04:18:08,560 --> 04:18:10,960 install jupyter as well as all those 6868 04:18:10,960 --> 04:18:13,120 popular data science libraries like 6869 04:18:13,120 --> 04:18:15,439 numpy pandas and so on so we don't have 6870 04:18:15,439 --> 04:18:18,840 to manually install this using 6871 04:18:18,840 --> 04:18:21,199 pip right now as part of the next step 6872 04:18:21,199 --> 04:18:23,120 anaconda is suggesting to install 6873 04:18:23,120 --> 04:18:25,199 microsoft vs code we already have this 6874 04:18:25,199 --> 04:18:26,800 on our machine so we don't have to 6875 04:18:26,800 --> 04:18:29,439 install it we can go with continue 6876 04:18:29,439 --> 04:18:32,080 and close the installation now finally 6877 04:18:32,080 --> 04:18:33,760 we can move this to trash because we 6878 04:18:33,760 --> 04:18:37,279 don't need this installer in the future 6879 04:18:37,279 --> 04:18:39,439 all right now open up a terminal window 6880 04:18:39,439 --> 04:18:42,319 and type jupyter 6881 04:18:42,319 --> 04:18:44,399 with a y space 6882 04:18:44,399 --> 04:18:45,760 notebook 6883 04:18:45,760 --> 04:18:47,600 this will start the notebook server on 6884 04:18:47,600 --> 04:18:49,760 your machine so enter 6885 04:18:49,760 --> 04:18:51,920 there you go this will start the 6886 04:18:51,920 --> 04:18:53,439 notebook server on your machine you can 6887 04:18:53,439 --> 04:18:55,279 see these default messages here don't 6888 04:18:55,279 --> 04:18:57,439 worry about them now it automatically 6889 04:18:57,439 --> 04:19:00,000 opens a browser window pointing to 6890 04:19:00,000 --> 04:19:04,080 localhost port 888 6891 04:19:04,080 --> 04:19:06,479 this is what we call jupiter dashboard 6892 04:19:06,479 --> 04:19:08,159 on this dashboard we have a few tabs the 6893 04:19:08,159 --> 04:19:10,399 first tab is the files tab and by 6894 04:19:10,399 --> 04:19:12,239 default this points to your home 6895 04:19:12,239 --> 04:19:14,479 directory so every user on your machine 6896 04:19:14,479 --> 04:19:16,239 has a home directory this is my home 6897 04:19:16,239 --> 04:19:18,000 directory on mac you can see here we 6898 04:19:18,000 --> 04:19:20,000 have a desktop folder as well as 6899 04:19:20,000 --> 04:19:22,239 documents downloads and so on on your 6900 04:19:22,239 --> 04:19:23,279 machine you're going to see different 6901 04:19:23,279 --> 04:19:25,279 folders so someone on your machine you 6902 04:19:25,279 --> 04:19:27,680 need to create a jupyter notebook i'm 6903 04:19:27,680 --> 04:19:30,159 going to go to desktop here's my desktop 6904 04:19:30,159 --> 04:19:31,760 i don't have anything here 6905 04:19:31,760 --> 04:19:33,199 and then 6906 04:19:33,199 --> 04:19:34,560 click new 6907 04:19:34,560 --> 04:19:36,800 i want to create a notebook for python 6908 04:19:36,800 --> 04:19:39,120 3. in this notebook we can write python 6909 04:19:39,120 --> 04:19:41,439 code and execute it line by line we can 6910 04:19:41,439 --> 04:19:43,279 easily visualize our data as you will 6911 04:19:43,279 --> 04:19:45,920 see over the next few videos so let's go 6912 04:19:45,920 --> 04:19:48,560 ahead with this 6913 04:19:48,880 --> 04:19:50,880 all right here's our first notebook we 6914 04:19:50,880 --> 04:19:53,439 can see by default it's called untitled 6915 04:19:53,439 --> 04:19:55,359 let's change that to 6916 04:19:55,359 --> 04:19:57,439 hello world so this is going to be the 6917 04:19:57,439 --> 04:19:59,040 hello world of our machine learning 6918 04:19:59,040 --> 04:20:00,319 project 6919 04:20:00,319 --> 04:20:02,560 let's rename this 6920 04:20:02,560 --> 04:20:04,080 now if you look at your desktop you can 6921 04:20:04,080 --> 04:20:05,159 see this file 6922 04:20:05,159 --> 04:20:07,120 helloworld.i pi 6923 04:20:07,120 --> 04:20:10,399 nb this is a jupyter notebook it's kind 6924 04:20:10,399 --> 04:20:12,399 of similar to our pi files where we 6925 04:20:12,399 --> 04:20:14,319 write our python code but it includes 6926 04:20:14,319 --> 04:20:16,640 additional data that jupiter uses to 6927 04:20:16,640 --> 04:20:18,720 execute our code 6928 04:20:18,720 --> 04:20:20,399 so back to our notebook 6929 04:20:20,399 --> 04:20:21,520 let's do 6930 04:20:21,520 --> 04:20:22,880 a print 6931 04:20:22,880 --> 04:20:24,800 hello world 6932 04:20:24,800 --> 04:20:28,800 and then click this run button here 6933 04:20:28,800 --> 04:20:31,680 and here's the result printed in jupiter 6934 04:20:31,680 --> 04:20:33,199 so we don't have to navigate back and 6935 04:20:33,199 --> 04:20:35,439 forth between the terminal window we can 6936 04:20:35,439 --> 04:20:38,080 see all the result right here 6937 04:20:38,080 --> 04:20:39,680 next i'm going to show you how to load a 6938 04:20:39,680 --> 04:20:44,000 data set from a csv file in jupyter 6939 04:20:47,680 --> 04:20:48,880 all right in this lecture we're going to 6940 04:20:48,880 --> 04:20:51,199 download a data set from a very popular 6941 04:20:51,199 --> 04:20:53,760 website called 6942 04:20:53,760 --> 04:20:55,840 gaggle is basically a place to do data 6943 04:20:55,840 --> 04:20:57,600 science projects so the first thing you 6944 04:20:57,600 --> 04:20:59,199 need to do is to create an account you 6945 04:20:59,199 --> 04:21:01,600 can sign up with facebook google or 6946 04:21:01,600 --> 04:21:04,000 using a custom email and password once 6947 04:21:04,000 --> 04:21:05,760 you sign up then come back here on 6948 04:21:05,760 --> 04:21:08,800 kaggle.com here in the search bar 6949 04:21:08,800 --> 04:21:10,000 search for 6950 04:21:10,000 --> 04:21:13,439 video game sales this is the name of a 6951 04:21:13,439 --> 04:21:15,120 very popular data set that we're going 6952 04:21:15,120 --> 04:21:17,439 to use in this lecture so 6953 04:21:17,439 --> 04:21:19,199 here in this list you can see the first 6954 04:21:19,199 --> 04:21:22,239 item with this kind of reddish icon so 6955 04:21:22,239 --> 04:21:24,080 let's go with that 6956 04:21:24,080 --> 04:21:25,760 as you can see this data set includes 6957 04:21:25,760 --> 04:21:28,560 the sales data for more than 16 000 6958 04:21:28,560 --> 04:21:29,840 video games 6959 04:21:29,840 --> 04:21:31,680 on this page you can see the description 6960 04:21:31,680 --> 04:21:33,600 of various columns in this data set we 6961 04:21:33,600 --> 04:21:34,399 have 6962 04:21:34,399 --> 04:21:38,319 rank name platform year and so on so 6963 04:21:38,319 --> 04:21:40,399 here's our data source it's a csv file 6964 04:21:40,399 --> 04:21:42,640 called vgsales.csv 6965 04:21:42,640 --> 04:21:45,439 as you can see there are over 16 000 6966 04:21:45,439 --> 04:21:48,880 rows and 11 columns in this data set 6967 04:21:48,880 --> 04:21:50,479 right below that you can see the first 6968 04:21:50,479 --> 04:21:52,960 few records of this data set so here's 6969 04:21:52,960 --> 04:21:54,960 our first record the ranking for this 6970 04:21:54,960 --> 04:21:57,600 game is one it's the wii sports game for 6971 04:21:57,600 --> 04:21:59,359 we as the platform and it was released 6972 04:21:59,359 --> 04:22:01,359 in year 2006 6973 04:22:01,359 --> 04:22:03,359 now what i want you to do 6974 04:22:03,359 --> 04:22:05,760 is to go ahead and download this data 6975 04:22:05,760 --> 04:22:07,840 set and as i told you before you need to 6976 04:22:07,840 --> 04:22:10,479 sign in before you can download this 6977 04:22:10,479 --> 04:22:12,159 so this will give you a zip file as you 6978 04:22:12,159 --> 04:22:13,760 can see here 6979 04:22:13,760 --> 04:22:16,239 here's our csv file now i want you to 6980 04:22:16,239 --> 04:22:18,319 put this right next to your jupiter 6981 04:22:18,319 --> 04:22:20,239 notebook on my machine that is on my 6982 04:22:20,239 --> 04:22:22,319 desktop so i'm going to drag and drop 6983 04:22:22,319 --> 04:22:25,120 this onto the desktop folder 6984 04:22:25,120 --> 04:22:26,000 now 6985 04:22:26,000 --> 04:22:27,520 if you look at the desktop you can see 6986 04:22:27,520 --> 04:22:30,319 here's my jupyter hello world notebook 6987 04:22:30,319 --> 04:22:33,159 and right next to that we have 6988 04:22:33,159 --> 04:22:34,960 vgsales.csv 6989 04:22:34,960 --> 04:22:36,000 with that 6990 04:22:36,000 --> 04:22:38,080 we go back to our jupiter notebook 6991 04:22:38,080 --> 04:22:41,439 let's remove the first line and instead 6992 04:22:41,439 --> 04:22:42,960 import 6993 04:22:42,960 --> 04:22:44,479 pandas 6994 04:22:44,479 --> 04:22:47,760 as pd with this we're importing pandas 6995 04:22:47,760 --> 04:22:50,319 module and renaming it to pd so we don't 6996 04:22:50,319 --> 04:22:52,720 have to type pandas. several times in 6997 04:22:52,720 --> 04:22:54,239 this code 6998 04:22:54,239 --> 04:22:56,720 now let's type pd dot 6999 04:22:56,720 --> 04:22:59,760 read underline csv 7000 04:22:59,760 --> 04:23:00,399 and 7001 04:23:00,399 --> 04:23:04,199 pass the name of our csv file that is vg 7002 04:23:04,199 --> 04:23:06,800 sales.csv now because this csv file is 7003 04:23:06,800 --> 04:23:08,560 in the current folder right next to our 7004 04:23:08,560 --> 04:23:10,640 jupyter notebook we can easily load it 7005 04:23:10,640 --> 04:23:12,319 otherwise we'll have to supply the full 7006 04:23:12,319 --> 04:23:15,439 path to this file so 7007 04:23:15,439 --> 04:23:18,159 this returns a data frame object which 7008 04:23:18,159 --> 04:23:20,399 is like an excel spreadsheet let me show 7009 04:23:20,399 --> 04:23:21,760 you so 7010 04:23:21,760 --> 04:23:23,359 we store it here 7011 04:23:23,359 --> 04:23:25,680 and then we can simply type df to 7012 04:23:25,680 --> 04:23:28,000 inspect it so one more time let's run 7013 04:23:28,000 --> 04:23:29,600 this program 7014 04:23:29,600 --> 04:23:31,199 here's our data frame with these rows 7015 04:23:31,199 --> 04:23:34,000 and columns so we have rank name 7016 04:23:34,000 --> 04:23:35,680 platform and so on 7017 04:23:35,680 --> 04:23:37,439 now this data frame object has lots of 7018 04:23:37,439 --> 04:23:39,279 attributes and methods that we're not 7019 04:23:39,279 --> 04:23:40,880 going to cover in this tutorial that's 7020 04:23:40,880 --> 04:23:42,319 really beyond the scope of what we're 7021 04:23:42,319 --> 04:23:43,760 going to do so i'll leave it up to you 7022 04:23:43,760 --> 04:23:46,319 to read panda's documentation or follow 7023 04:23:46,319 --> 04:23:48,000 other tutorials to learn about pandas 7024 04:23:48,000 --> 04:23:49,760 data frames but in this lecture i'm 7025 04:23:49,760 --> 04:23:50,960 going to show you some of the most 7026 04:23:50,960 --> 04:23:53,359 useful methods and attributes 7027 04:23:53,359 --> 04:23:55,040 the first one is shape 7028 04:23:55,040 --> 04:23:57,920 so shape let's run this one more time 7029 04:23:57,920 --> 04:24:00,080 so here's the shape of this data set we 7030 04:24:00,080 --> 04:24:04,640 have over 16 000 records and 11 columns 7031 04:24:04,640 --> 04:24:06,560 technically this is a two dimensional 7032 04:24:06,560 --> 04:24:09,840 array of 16 thousand and 7033 04:24:09,840 --> 04:24:10,800 okay 7034 04:24:10,800 --> 04:24:13,040 now you can see here we have another 7035 04:24:13,040 --> 04:24:14,560 segment for writing code so we don't 7036 04:24:14,560 --> 04:24:16,159 have to write all the code in the first 7037 04:24:16,159 --> 04:24:18,319 segment so here in the second segment we 7038 04:24:18,319 --> 04:24:20,319 can call one of the methods of the data 7039 04:24:20,319 --> 04:24:25,040 frame that is df dot describe 7040 04:24:25,040 --> 04:24:27,760 now when we run this program 7041 04:24:27,760 --> 04:24:30,319 we can see the output for each segment 7042 04:24:30,319 --> 04:24:32,239 right next to it so here's our first 7043 04:24:32,239 --> 04:24:34,640 segment here we have these three lines 7044 04:24:34,640 --> 04:24:38,000 and this is the output of the last line 7045 04:24:38,000 --> 04:24:40,000 below that we have our second segment 7046 04:24:40,000 --> 04:24:41,760 here we're calling the describe method 7047 04:24:41,760 --> 04:24:44,319 and right below that we have the output 7048 04:24:44,319 --> 04:24:46,800 of this segment so this is the beauty of 7049 04:24:46,800 --> 04:24:49,680 jupyter we can easily visualize our data 7050 04:24:49,680 --> 04:24:51,279 doing this with vs code and terminal 7051 04:24:51,279 --> 04:24:53,840 windows is really tedious and clunky so 7052 04:24:53,840 --> 04:24:56,080 what is this describe method returning 7053 04:24:56,080 --> 04:24:57,600 basically it's returning some basic 7054 04:24:57,600 --> 04:24:59,680 information about each column in this 7055 04:24:59,680 --> 04:25:02,159 data set so as you saw earlier we have 7056 04:25:02,159 --> 04:25:04,800 columns like rank year and so on 7057 04:25:04,800 --> 04:25:06,560 these are the columns with numerical 7058 04:25:06,560 --> 04:25:09,600 values now for each column we have the 7059 04:25:09,600 --> 04:25:11,279 count which is the number of records in 7060 04:25:11,279 --> 04:25:13,439 that column you can see our rank column 7061 04:25:13,439 --> 04:25:15,720 has 16 7062 04:25:15,720 --> 04:25:19,199 598 records whereas the year column has 7063 04:25:19,199 --> 04:25:20,680 16 7064 04:25:20,680 --> 04:25:22,720 327 records 7065 04:25:22,720 --> 04:25:24,479 so this shows that some of our records 7066 04:25:24,479 --> 04:25:27,279 don't have the value for the year column 7067 04:25:27,279 --> 04:25:29,600 we have null values so in a real data 7068 04:25:29,600 --> 04:25:31,359 science or machine learning project 7069 04:25:31,359 --> 04:25:32,720 we'll have to use some techniques to 7070 04:25:32,720 --> 04:25:34,960 clean up our data set one option is to 7071 04:25:34,960 --> 04:25:37,120 remove the records that don't have a 7072 04:25:37,120 --> 04:25:39,120 value for the year column or we can 7073 04:25:39,120 --> 04:25:41,520 assign them a default value that really 7074 04:25:41,520 --> 04:25:43,840 depends on the project now another 7075 04:25:43,840 --> 04:25:45,840 attribute for each column is mean so 7076 04:25:45,840 --> 04:25:48,399 this is the average of all the values 7077 04:25:48,399 --> 04:25:50,239 now in the case of the rank column this 7078 04:25:50,239 --> 04:25:52,159 value doesn't really matter but look at 7079 04:25:52,159 --> 04:25:53,279 the year 7080 04:25:53,279 --> 04:25:55,520 so the average year for all these video 7081 04:25:55,520 --> 04:25:58,000 games in our data set is 2006 and this 7082 04:25:58,000 --> 04:25:59,600 might be important in the problem we're 7083 04:25:59,600 --> 04:26:00,800 trying to solve 7084 04:26:00,800 --> 04:26:03,600 we also have standard deviation which is 7085 04:26:03,600 --> 04:26:05,680 a measure to quantify the amount of 7086 04:26:05,680 --> 04:26:08,399 variation in our set of values below 7087 04:26:08,399 --> 04:26:10,080 that we have min 7088 04:26:10,080 --> 04:26:12,000 as an example the minimum value for the 7089 04:26:12,000 --> 04:26:15,920 year column is 1980. so quite often when 7090 04:26:15,920 --> 04:26:17,840 we work with a new data set we call the 7091 04:26:17,840 --> 04:26:19,840 describe method to get some basic 7092 04:26:19,840 --> 04:26:21,920 statistics about our data 7093 04:26:21,920 --> 04:26:24,239 let me show you another useful attribute 7094 04:26:24,239 --> 04:26:26,960 so in the next segment let's type 7095 04:26:26,960 --> 04:26:29,680 df.values 7096 04:26:29,680 --> 04:26:31,359 let's run this 7097 04:26:31,359 --> 04:26:32,800 as you can see this returns a 7098 04:26:32,800 --> 04:26:34,800 two-dimensional array this square 7099 04:26:34,800 --> 04:26:37,359 bracket indicates the outer array 7100 04:26:37,359 --> 04:26:40,399 and the second one represents the inner 7101 04:26:40,399 --> 04:26:43,040 array so the first element in our outer 7102 04:26:43,040 --> 04:26:46,239 array is an array itself these are the 7103 04:26:46,239 --> 04:26:48,479 values in this array which basically 7104 04:26:48,479 --> 04:26:51,359 represent the first row in our data set 7105 04:26:51,359 --> 04:26:53,520 so the video game with ranking one which 7106 04:26:53,520 --> 04:26:55,600 is called wii sports 7107 04:26:55,600 --> 04:26:57,920 so this was a basic overview of pando's 7108 04:26:57,920 --> 04:26:59,760 data frames in the next lecture i'm 7109 04:26:59,760 --> 04:27:01,199 going to show you some of the useful 7110 04:27:01,199 --> 04:27:04,560 shortcuts of jupyter 7111 04:27:08,000 --> 04:27:09,199 in this lecture i'm going to show you 7112 04:27:09,199 --> 04:27:11,279 some of the most useful shortcuts in 7113 04:27:11,279 --> 04:27:12,399 jupyter 7114 04:27:12,399 --> 04:27:13,680 now the first thing i want you to pay 7115 04:27:13,680 --> 04:27:16,080 attention to is this green bar on the 7116 04:27:16,080 --> 04:27:18,560 left this indicates that this cell is 7117 04:27:18,560 --> 04:27:20,720 currently in the edit mode so we can 7118 04:27:20,720 --> 04:27:22,159 write code here 7119 04:27:22,159 --> 04:27:22,960 now 7120 04:27:22,960 --> 04:27:25,520 if we press the escape key 7121 04:27:25,520 --> 04:27:27,840 green turns to blue and that means this 7122 04:27:27,840 --> 04:27:30,479 cell is currently in the command mode so 7123 04:27:30,479 --> 04:27:32,640 basically the activated cell can be 7124 04:27:32,640 --> 04:27:34,960 either in the edit mode or the command 7125 04:27:34,960 --> 04:27:37,120 mode depending on the mode we have 7126 04:27:37,120 --> 04:27:39,199 different shortcuts so here we're 7127 04:27:39,199 --> 04:27:40,960 currently in the command mode 7128 04:27:40,960 --> 04:27:43,120 if we press h 7129 04:27:43,120 --> 04:27:45,680 we can see the list of all the keyboard 7130 04:27:45,680 --> 04:27:47,439 shortcuts 7131 04:27:47,439 --> 04:27:50,080 right above this list you can see 7132 04:27:50,080 --> 04:27:53,120 mac os modifier keys these are the extra 7133 04:27:53,120 --> 04:27:55,359 keys that we have on a mac keyboard if 7134 04:27:55,359 --> 04:27:56,960 you're a windows user you're not going 7135 04:27:56,960 --> 04:27:59,920 to see this so as an example here is the 7136 04:27:59,920 --> 04:28:03,040 shape of the command key this is control 7137 04:28:03,040 --> 04:28:05,359 this is option and so on 7138 04:28:05,359 --> 04:28:06,800 with this guideline you can easily 7139 04:28:06,800 --> 04:28:08,960 understand the shortcut associated with 7140 04:28:08,960 --> 04:28:11,600 each command let me show you 7141 04:28:11,600 --> 04:28:14,000 so here we have all the commands when a 7142 04:28:14,000 --> 04:28:16,479 cell is in the command mode for example 7143 04:28:16,479 --> 04:28:19,120 we have this command open the command 7144 04:28:19,120 --> 04:28:20,960 palette this is exactly like the command 7145 04:28:20,960 --> 04:28:23,680 palette that we have in vs code here's a 7146 04:28:23,680 --> 04:28:26,720 shortcut to execute this command that is 7147 04:28:26,720 --> 04:28:29,359 command shift and f 7148 04:28:29,359 --> 04:28:31,279 okay so here we have 7149 04:28:31,279 --> 04:28:33,279 lots of shortcuts of course you're not 7150 04:28:33,279 --> 04:28:34,880 going to use all of them all the time 7151 04:28:34,880 --> 04:28:36,399 but it's good to have a quick look here 7152 04:28:36,399 --> 04:28:38,399 to see what is available for you with 7153 04:28:38,399 --> 04:28:39,920 this shortcut you can write code much 7154 04:28:39,920 --> 04:28:41,920 faster so let me show you some of the 7155 04:28:41,920 --> 04:28:43,680 most useful ones 7156 04:28:43,680 --> 04:28:45,439 i'm going to close this 7157 04:28:45,439 --> 04:28:46,479 now 7158 04:28:46,479 --> 04:28:47,920 with our first cell 7159 04:28:47,920 --> 04:28:51,439 in the command mode i'm going to press b 7160 04:28:51,439 --> 04:28:54,319 and this inserts a new cell below this 7161 04:28:54,319 --> 04:28:55,680 cell 7162 04:28:55,680 --> 04:28:57,760 we can also go back to our first cell 7163 04:28:57,760 --> 04:29:00,159 press escape now the cell is in the 7164 04:29:00,159 --> 04:29:02,960 command mode we can insert an empty cell 7165 04:29:02,960 --> 04:29:06,000 above this cell by pressing a 7166 04:29:06,000 --> 04:29:09,199 so either a or b a for above and b for 7167 04:29:09,199 --> 04:29:11,520 below okay now if you don't want this 7168 04:29:11,520 --> 04:29:14,800 cell you can press d twice to delete it 7169 04:29:14,800 --> 04:29:16,319 like this 7170 04:29:16,319 --> 04:29:17,920 now in the cell i'm going to print a 7171 04:29:17,920 --> 04:29:20,479 hello world message so 7172 04:29:20,479 --> 04:29:21,600 print 7173 04:29:21,600 --> 04:29:23,439 hello world 7174 04:29:23,439 --> 04:29:26,159 now to run the code in this cell we can 7175 04:29:26,159 --> 04:29:29,279 click on the run button here 7176 04:29:29,279 --> 04:29:30,720 so 7177 04:29:30,720 --> 04:29:32,319 here's our print function and right 7178 04:29:32,319 --> 04:29:34,960 below that you can see the output of 7179 04:29:34,960 --> 04:29:37,120 this function but note that when you run 7180 04:29:37,120 --> 04:29:39,920 a cell this will only execute the code 7181 04:29:39,920 --> 04:29:42,720 in that cell in other words the code in 7182 04:29:42,720 --> 04:29:45,040 other cells will not be executed let me 7183 04:29:45,040 --> 04:29:46,880 show you what i mean so in the cell 7184 04:29:46,880 --> 04:29:48,880 below this cell i'm going to delete the 7185 04:29:48,880 --> 04:29:51,359 call to describe method 7186 04:29:51,359 --> 04:29:53,600 instead i'm going to print 7187 04:29:53,600 --> 04:29:55,520 ocean 7188 04:29:55,520 --> 04:29:56,560 now 7189 04:29:56,560 --> 04:29:58,560 i'm going to put the cursor back 7190 04:29:58,560 --> 04:30:00,239 in this cell where we print the hello 7191 04:30:00,239 --> 04:30:03,680 world message and run this cell 7192 04:30:03,680 --> 04:30:05,680 so we can see hello world is displayed 7193 04:30:05,680 --> 04:30:06,479 here 7194 04:30:06,479 --> 04:30:08,800 but the cell below is still displaying 7195 04:30:08,800 --> 04:30:11,040 the described table so we don't see the 7196 04:30:11,040 --> 04:30:12,560 changes here 7197 04:30:12,560 --> 04:30:15,279 now to solve this problem we can go to 7198 04:30:15,279 --> 04:30:17,840 the cell menu on the top and run all 7199 04:30:17,840 --> 04:30:20,239 cells together 7200 04:30:20,239 --> 04:30:22,319 this can work for small projects but 7201 04:30:22,319 --> 04:30:23,760 sometimes you're working with a large 7202 04:30:23,760 --> 04:30:25,760 data set so if you want to run all these 7203 04:30:25,760 --> 04:30:27,680 cells together it's going to take a lot 7204 04:30:27,680 --> 04:30:30,800 of time that is the reason jupiter saves 7205 04:30:30,800 --> 04:30:32,560 the output of itself so we don't have to 7206 04:30:32,560 --> 04:30:35,520 rerun that code if it hasn't changed 7207 04:30:35,520 --> 04:30:37,600 so this notebook file that we have here 7208 04:30:37,600 --> 04:30:40,080 includes our source code organized in 7209 04:30:40,080 --> 04:30:42,399 cells as well as the output for each 7210 04:30:42,399 --> 04:30:44,560 cell that is why it's different from a 7211 04:30:44,560 --> 04:30:46,800 regular pi file where we only have the 7212 04:30:46,800 --> 04:30:48,319 source code 7213 04:30:48,319 --> 04:30:50,880 here we also have auto completion and 7214 04:30:50,880 --> 04:30:53,840 intellisense so in the cell 7215 04:30:53,840 --> 04:30:57,199 let's call df dataframe 7216 04:30:57,199 --> 04:31:00,239 dot now if you press tab we can see all 7217 04:31:00,239 --> 04:31:02,479 the attributes and methods in this 7218 04:31:02,479 --> 04:31:03,600 object 7219 04:31:03,600 --> 04:31:04,399 so 7220 04:31:04,399 --> 04:31:06,560 let's call describe 7221 04:31:06,560 --> 04:31:08,239 now with the cursor on the name of the 7222 04:31:08,239 --> 04:31:11,040 method we can press shift and tab 7223 04:31:11,040 --> 04:31:13,439 to see this tooltip that describes what 7224 04:31:13,439 --> 04:31:15,520 this method does and what parameter it 7225 04:31:15,520 --> 04:31:17,520 takes so here in front of signature you 7226 04:31:17,520 --> 04:31:20,239 can see the described method these are 7227 04:31:20,239 --> 04:31:22,960 the parameters and their default value 7228 04:31:22,960 --> 04:31:24,720 and right below that you can see the 7229 04:31:24,720 --> 04:31:27,199 description of what this method does 7230 04:31:27,199 --> 04:31:29,520 in this case it generates descriptive 7231 04:31:29,520 --> 04:31:31,439 statistics that summarize the central 7232 04:31:31,439 --> 04:31:33,600 tendency and so on 7233 04:31:33,600 --> 04:31:35,680 similar to vs code we can also convert a 7234 04:31:35,680 --> 04:31:38,560 line to comment by pressing command and 7235 04:31:38,560 --> 04:31:42,080 slash on mac or control slash on windows 7236 04:31:42,080 --> 04:31:43,040 like this 7237 04:31:43,040 --> 04:31:45,279 now this line is a comment we can press 7238 04:31:45,279 --> 04:31:47,359 the same shortcut one more time 7239 04:31:47,359 --> 04:31:49,199 to remove the comment 7240 04:31:49,199 --> 04:31:50,800 so these were some of the most useful 7241 04:31:50,800 --> 04:31:53,040 shortcuts in jupyter now over the next 7242 04:31:53,040 --> 04:31:54,640 few lectures we're going to work on a 7243 04:31:54,640 --> 04:31:56,800 real machine learning project but before 7244 04:31:56,800 --> 04:31:58,960 we get there let's delete all the cells 7245 04:31:58,960 --> 04:32:01,199 here so we start with only a single 7246 04:32:01,199 --> 04:32:03,920 empty cell so here in this cell first 7247 04:32:03,920 --> 04:32:06,319 i'm going to press the escape button now 7248 04:32:06,319 --> 04:32:08,080 the cell is blue so we are in the 7249 04:32:08,080 --> 04:32:10,239 command mode and we can delete the cell 7250 04:32:10,239 --> 04:32:12,159 by pressing d twice 7251 04:32:12,159 --> 04:32:14,640 there you go now the next cell is 7252 04:32:14,640 --> 04:32:16,479 activated and it's in the command mode 7253 04:32:16,479 --> 04:32:19,279 so let's delete this as well 7254 04:32:19,279 --> 04:32:21,279 we have two more cells to delete there 7255 04:32:21,279 --> 04:32:22,159 you go 7256 04:32:22,159 --> 04:32:23,840 and the last one 7257 04:32:23,840 --> 04:32:25,199 like this 7258 04:32:25,199 --> 04:32:27,120 so now we have an empty notebook with a 7259 04:32:27,120 --> 04:32:29,760 single cell 7260 04:32:33,359 --> 04:32:34,640 over the next few lectures we're going 7261 04:32:34,640 --> 04:32:36,399 to work on a real machine learning 7262 04:32:36,399 --> 04:32:38,960 project imagine we have an online music 7263 04:32:38,960 --> 04:32:39,840 store 7264 04:32:39,840 --> 04:32:42,080 when our users sign up we ask their age 7265 04:32:42,080 --> 04:32:44,880 and gender and based on their profile we 7266 04:32:44,880 --> 04:32:46,960 recommend various music albums they're 7267 04:32:46,960 --> 04:32:49,199 likely to buy so in this project we want 7268 04:32:49,199 --> 04:32:51,120 to use machine learning to increase 7269 04:32:51,120 --> 04:32:52,080 sales 7270 04:32:52,080 --> 04:32:54,720 so we want to build a model we feed this 7271 04:32:54,720 --> 04:32:57,199 model with some sample data based on the 7272 04:32:57,199 --> 04:32:59,840 existing users our model will learn the 7273 04:32:59,840 --> 04:33:02,080 patterns in our data so we can ask it to 7274 04:33:02,080 --> 04:33:04,400 make predictions when a new user signs 7275 04:33:04,400 --> 04:33:06,240 up we tell our model hey we have a new 7276 04:33:06,240 --> 04:33:08,480 user with this profile what is the kind 7277 04:33:08,480 --> 04:33:10,959 of music that this user is interested in 7278 04:33:10,959 --> 04:33:13,359 our model will say jazz or hip hop or 7279 04:33:13,359 --> 04:33:15,279 whatever and based on that we can make 7280 04:33:15,279 --> 04:33:16,959 suggestions to the user 7281 04:33:16,959 --> 04:33:18,480 so this is the problem we're going to 7282 04:33:18,480 --> 04:33:19,359 solve 7283 04:33:19,359 --> 04:33:20,879 now back to the list of steps in a 7284 04:33:20,879 --> 04:33:22,480 machine learning project 7285 04:33:22,480 --> 04:33:24,799 first we need to import our data then we 7286 04:33:24,799 --> 04:33:26,879 should prepare or clean it next we 7287 04:33:26,879 --> 04:33:28,799 select a machine learning algorithm to 7288 04:33:28,799 --> 04:33:31,680 build a model we train our model and ask 7289 04:33:31,680 --> 04:33:33,920 it to make predictions and finally we 7290 04:33:33,920 --> 04:33:36,000 evaluate our algorithm to see its 7291 04:33:36,000 --> 04:33:38,480 accuracy if it's not accurate we either 7292 04:33:38,480 --> 04:33:40,879 fine tune our model or select a 7293 04:33:40,879 --> 04:33:43,119 different algorithm so let's focus on 7294 04:33:43,119 --> 04:33:46,639 the first step head over to bit.li 7295 04:33:46,639 --> 04:33:50,240 music csv this is a very basic csv that 7296 04:33:50,240 --> 04:33:52,080 i've created for this project it's just 7297 04:33:52,080 --> 04:33:55,760 some random made-up data it's not real 7298 04:33:55,760 --> 04:33:57,840 so we have a table with three columns 7299 04:33:57,840 --> 04:33:58,719 age 7300 04:33:58,719 --> 04:34:02,000 gender and genre gender can either be 7301 04:34:02,000 --> 04:34:04,480 one which represents a male or zero 7302 04:34:04,480 --> 04:34:06,719 which represents a female here i'm 7303 04:34:06,719 --> 04:34:08,799 making a few assumptions i'm assuming 7304 04:34:08,799 --> 04:34:12,400 that men between 20 and 25 like hip-hop 7305 04:34:12,400 --> 04:34:16,000 men between 26 and 30 like jazz and 7306 04:34:16,000 --> 04:34:18,639 after the age of 30 they like classical 7307 04:34:18,639 --> 04:34:19,920 music 7308 04:34:19,920 --> 04:34:22,080 for women i'm assuming that if they're 7309 04:34:22,080 --> 04:34:24,799 between 20 and 25 they like dance music 7310 04:34:24,799 --> 04:34:27,279 if they're between 26 and 30 they like 7311 04:34:27,279 --> 04:34:29,600 acoustic music and just like men after 7312 04:34:29,600 --> 04:34:32,240 the age of 30 they like classical music 7313 04:34:32,240 --> 04:34:34,080 once again this is a made-up pattern 7314 04:34:34,080 --> 04:34:35,600 it's not the representation of the 7315 04:34:35,600 --> 04:34:36,719 reality 7316 04:34:36,719 --> 04:34:39,199 so let's go ahead and download this csv 7317 04:34:39,199 --> 04:34:42,719 click on this dot dot icon here and 7318 04:34:42,719 --> 04:34:45,039 download this file 7319 04:34:45,039 --> 04:34:47,439 in my downloads folder here we have this 7320 04:34:47,439 --> 04:34:48,959 music.csv 7321 04:34:48,959 --> 04:34:51,119 i'm going to drag and drop this onto 7322 04:34:51,119 --> 04:34:53,760 desktop because that's where i've stored 7323 04:34:53,760 --> 04:34:55,840 this hello world notebook so i want you 7324 04:34:55,840 --> 04:34:58,000 to put the csv file right next to your 7325 04:34:58,000 --> 04:35:00,160 jupyter notebook 7326 04:35:00,160 --> 04:35:02,000 now back to our notebook we need to read 7327 04:35:02,000 --> 04:35:04,879 the csv file so just like before first 7328 04:35:04,879 --> 04:35:07,680 we need to import the pandas module so 7329 04:35:07,680 --> 04:35:11,359 import pandas as pd 7330 04:35:11,359 --> 04:35:14,080 and then we'll call pd that read 7331 04:35:14,080 --> 04:35:16,000 underline csv 7332 04:35:16,000 --> 04:35:19,520 and the name of our file is music.csv 7333 04:35:19,520 --> 04:35:21,600 as you saw earlier this returns a data 7334 04:35:21,600 --> 04:35:23,600 frame which is a two-dimensional array 7335 04:35:23,600 --> 04:35:26,000 similar to an excel spreadsheet so let's 7336 04:35:26,000 --> 04:35:28,080 call that music 7337 04:35:28,080 --> 04:35:31,199 underline data 7338 04:35:31,199 --> 04:35:33,680 now let's inspect this music underline 7339 04:35:33,680 --> 04:35:35,600 data to make sure we loaded everything 7340 04:35:35,600 --> 04:35:38,480 properly so run 7341 04:35:38,480 --> 04:35:40,879 so here's our data frame beautiful next 7342 04:35:40,879 --> 04:35:43,039 we need to prepare or clean the data and 7343 04:35:43,039 --> 04:35:47,240 that's the topic for the next lecture 7344 04:35:50,000 --> 04:35:51,520 the second step in a machine learning 7345 04:35:51,520 --> 04:35:53,439 project is cleaning or preparing the 7346 04:35:53,439 --> 04:35:55,920 data and that involves tasks such as 7347 04:35:55,920 --> 04:35:58,400 removing duplicates null values and so 7348 04:35:58,400 --> 04:36:00,240 on now in this particular data set we 7349 04:36:00,240 --> 04:36:01,760 don't have to do any kind of cleaning 7350 04:36:01,760 --> 04:36:03,840 because we don't have any duplicates and 7351 04:36:03,840 --> 04:36:06,719 as you can see all rows have values for 7352 04:36:06,719 --> 04:36:09,199 all columns so we don't have null values 7353 04:36:09,199 --> 04:36:12,160 but there is one thing we need to do 7354 04:36:12,160 --> 04:36:14,400 we should split this data set into two 7355 04:36:14,400 --> 04:36:16,080 separate data sets 7356 04:36:16,080 --> 04:36:18,000 one with the first two columns which we 7357 04:36:18,000 --> 04:36:20,561 refer to as the input set and the other 7358 04:36:20,561 --> 04:36:22,400 with the last column which we refer to 7359 04:36:22,400 --> 04:36:24,879 as the output set so when we train a 7360 04:36:24,879 --> 04:36:27,600 model we give it two separate data sets 7361 04:36:27,600 --> 04:36:30,240 the input set and the output set the 7362 04:36:30,240 --> 04:36:32,561 output set which is in this case the 7363 04:36:32,561 --> 04:36:35,840 genre column contains the predictions so 7364 04:36:35,840 --> 04:36:37,680 we're telling our model that if we have 7365 04:36:37,680 --> 04:36:40,561 a user who's 20 years old and is a male 7366 04:36:40,561 --> 04:36:42,240 they like hip hop 7367 04:36:42,240 --> 04:36:44,240 once we train our model then we give it 7368 04:36:44,240 --> 04:36:46,799 a new input set for example we say hey 7369 04:36:46,799 --> 04:36:49,760 we have a new user who is 21 years old 7370 04:36:49,760 --> 04:36:52,080 and is a male what is the genre of the 7371 04:36:52,080 --> 04:36:54,561 music that this user probably likes as 7372 04:36:54,561 --> 04:36:56,561 you can see in our input set we don't 7373 04:36:56,561 --> 04:36:59,439 have a sample for a 21 year old male so 7374 04:36:59,439 --> 04:37:01,600 we're going to ask our model to predict 7375 04:37:01,600 --> 04:37:02,320 that 7376 04:37:02,320 --> 04:37:04,240 that is the reason we need to split this 7377 04:37:04,240 --> 04:37:07,760 data set into two separate sets input 7378 04:37:07,760 --> 04:37:08,959 and output 7379 04:37:08,959 --> 04:37:11,039 so back to our code 7380 04:37:11,039 --> 04:37:13,119 this data frame object has a method 7381 04:37:13,119 --> 04:37:16,160 called drop 7382 04:37:16,160 --> 04:37:17,039 now 7383 04:37:17,039 --> 04:37:19,199 if you put the cursor under method name 7384 04:37:19,199 --> 04:37:21,840 and press shift and tab 7385 04:37:21,840 --> 04:37:24,160 you can see this tooltip so this is the 7386 04:37:24,160 --> 04:37:26,400 signature of this drop method these are 7387 04:37:26,400 --> 04:37:28,480 the parameters that we can pass here 7388 04:37:28,480 --> 04:37:29,840 the parameter we're going to use in this 7389 04:37:29,840 --> 04:37:32,160 lecture is columns which is set to none 7390 04:37:32,160 --> 04:37:34,400 by default with this parameter we can 7391 04:37:34,400 --> 04:37:36,799 specify the columns we want to drop 7392 04:37:36,799 --> 04:37:38,160 so 7393 04:37:38,160 --> 04:37:41,039 in this case we set columns 7394 04:37:41,039 --> 04:37:45,039 to an array with one string genre 7395 04:37:45,039 --> 04:37:47,199 now this method doesn't actually modify 7396 04:37:47,199 --> 04:37:49,439 the original data set in fact it will 7397 04:37:49,439 --> 04:37:52,160 create a new data set but without this 7398 04:37:52,160 --> 04:37:53,119 column 7399 04:37:53,119 --> 04:37:54,160 so 7400 04:37:54,160 --> 04:37:56,639 by convention we use a capital x to 7401 04:37:56,639 --> 04:37:59,760 represent that data set so capital x 7402 04:37:59,760 --> 04:38:02,320 equals this expression 7403 04:38:02,320 --> 04:38:03,119 now 7404 04:38:03,119 --> 04:38:05,840 let's inspect x 7405 04:38:05,840 --> 04:38:08,320 so as you can see our input set or x 7406 04:38:08,320 --> 04:38:10,561 includes these two columns age and 7407 04:38:10,561 --> 04:38:12,639 gender it doesn't have the output or 7408 04:38:12,639 --> 04:38:13,760 predictions 7409 04:38:13,760 --> 04:38:16,320 next we need to create our output set 7410 04:38:16,320 --> 04:38:17,439 so 7411 04:38:17,439 --> 04:38:20,160 once again we start with our data frame 7412 04:38:20,160 --> 04:38:21,520 music data 7413 04:38:21,520 --> 04:38:23,840 using square brackets we can get all the 7414 04:38:23,840 --> 04:38:26,719 values in a given column in this case 7415 04:38:26,719 --> 04:38:28,080 genre 7416 04:38:28,080 --> 04:38:30,799 once again this returns a new data set 7417 04:38:30,799 --> 04:38:33,600 by convention we use a lowercase y to 7418 04:38:33,600 --> 04:38:35,760 represent that so that is our output 7419 04:38:35,760 --> 04:38:36,879 data set 7420 04:38:36,879 --> 04:38:40,240 let's inspect that as well 7421 04:38:40,240 --> 04:38:41,199 so 7422 04:38:41,199 --> 04:38:43,039 in this data set we only have the 7423 04:38:43,039 --> 04:38:45,600 predictions or the answers 7424 04:38:45,600 --> 04:38:47,600 so we have prepared our data next we 7425 04:38:47,600 --> 04:38:48,959 need to create a model using an 7426 04:38:48,959 --> 04:38:51,959 algorithm 7427 04:38:54,959 --> 04:38:56,879 the next step is to build a model using 7428 04:38:56,879 --> 04:38:58,799 a machine learning algorithm there are 7429 04:38:58,799 --> 04:39:00,561 so many algorithms out there and each 7430 04:39:00,561 --> 04:39:03,119 algorithm has its pros and cons in terms 7431 04:39:03,119 --> 04:39:05,600 of the performance and accuracy in this 7432 04:39:05,600 --> 04:39:07,119 lecture we're going to use a very simple 7433 04:39:07,119 --> 04:39:09,600 algorithm called decision tree now the 7434 04:39:09,600 --> 04:39:11,039 good news is that we don't have to 7435 04:39:11,039 --> 04:39:13,520 explicitly program these algorithms 7436 04:39:13,520 --> 04:39:15,439 they're already implemented for us in a 7437 04:39:15,439 --> 04:39:18,000 library called scikit-learn 7438 04:39:18,000 --> 04:39:18,799 so 7439 04:39:18,799 --> 04:39:21,359 here on the top 7440 04:39:21,359 --> 04:39:24,680 from sklearn.3 7441 04:39:24,719 --> 04:39:27,840 let's import the decision 7442 04:39:27,840 --> 04:39:28,959 tree 7443 04:39:28,959 --> 04:39:30,639 classifier 7444 04:39:30,639 --> 04:39:33,359 so sklearn is the package that comes 7445 04:39:33,359 --> 04:39:35,199 with scikit-learn library this is the 7446 04:39:35,199 --> 04:39:37,360 most popular machine learning library in 7447 04:39:37,360 --> 04:39:38,480 python 7448 04:39:38,480 --> 04:39:40,560 in this package we have a module called 7449 04:39:40,560 --> 04:39:42,638 tree and in this module we have a class 7450 04:39:42,638 --> 04:39:45,520 called decision tree classifier this 7451 04:39:45,520 --> 04:39:47,760 class implements the decision tree 7452 04:39:47,760 --> 04:39:49,600 algorithm okay 7453 04:39:49,600 --> 04:39:50,480 so 7454 04:39:50,480 --> 04:39:52,320 now we need to create a new instance of 7455 04:39:52,320 --> 04:39:53,840 this class 7456 04:39:53,840 --> 04:39:54,798 so 7457 04:39:54,798 --> 04:39:57,120 at the end let's create an object called 7458 04:39:57,120 --> 04:40:00,320 model and set it to a new instance of 7459 04:40:00,320 --> 04:40:01,520 decision 7460 04:40:01,520 --> 04:40:02,638 tree 7461 04:40:02,638 --> 04:40:04,638 classifier 7462 04:40:04,638 --> 04:40:05,760 like this 7463 04:40:05,760 --> 04:40:07,760 so now we have a model next we need to 7464 04:40:07,760 --> 04:40:09,840 train it so it learns patterns in the 7465 04:40:09,840 --> 04:40:12,638 data and that is pretty easy we call 7466 04:40:12,638 --> 04:40:13,600 model 7467 04:40:13,600 --> 04:40:14,958 that fit 7468 04:40:14,958 --> 04:40:17,200 this method takes two data sets the 7469 04:40:17,200 --> 04:40:19,600 input set and the output set 7470 04:40:19,600 --> 04:40:20,400 so 7471 04:40:20,400 --> 04:40:24,480 they are capital x and y 7472 04:40:24,480 --> 04:40:26,240 and finally we need to ask our model to 7473 04:40:26,240 --> 04:40:28,798 make a prediction so we can ask it what 7474 04:40:28,798 --> 04:40:30,878 is the kind of music that a 21 year old 7475 04:40:30,878 --> 04:40:33,360 male likes now before we do that let's 7476 04:40:33,360 --> 04:40:34,958 temporarily 7477 04:40:34,958 --> 04:40:37,280 inspect our initial data set that is 7478 04:40:37,280 --> 04:40:39,680 music data 7479 04:40:39,680 --> 04:40:40,638 so 7480 04:40:40,638 --> 04:40:42,798 look what we got here 7481 04:40:42,798 --> 04:40:44,958 as i told you earlier i've assumed that 7482 04:40:44,958 --> 04:40:48,718 men between 20 and 25 like hip-hop music 7483 04:40:48,718 --> 04:40:51,360 but here we only have three samples for 7484 04:40:51,360 --> 04:40:53,200 men aged 20 7485 04:40:53,200 --> 04:40:56,000 23 and 25. we don't have a sample for a 7486 04:40:56,000 --> 04:40:58,798 21 year old male so if you ask our model 7487 04:40:58,798 --> 04:41:00,718 to predict the kind of music that a 21 7488 04:41:00,718 --> 04:41:02,958 year old male likes we expect it to say 7489 04:41:02,958 --> 04:41:04,240 hip hop 7490 04:41:04,240 --> 04:41:05,600 similarly 7491 04:41:05,600 --> 04:41:07,360 i've assumed that women between 20 and 7492 04:41:07,360 --> 04:41:10,000 25 like dance music but we don't have a 7493 04:41:10,000 --> 04:41:12,718 sample for a 22 year old female so once 7494 04:41:12,718 --> 04:41:14,718 again if you ask our model to predict 7495 04:41:14,718 --> 04:41:16,718 the kind of music that a 22 year old 7496 04:41:16,718 --> 04:41:19,920 woman likes we expect it to say dance 7497 04:41:19,920 --> 04:41:20,798 so 7498 04:41:20,798 --> 04:41:22,798 with these assumptions let's go ahead 7499 04:41:22,798 --> 04:41:25,440 and ask our model to make predictions 7500 04:41:25,440 --> 04:41:27,840 so 7501 04:41:27,920 --> 04:41:30,560 let's remove the last line 7502 04:41:30,560 --> 04:41:33,200 and instead we're going to call model 7503 04:41:33,200 --> 04:41:34,878 dot predict 7504 04:41:34,878 --> 04:41:36,958 this method takes a two dimensional 7505 04:41:36,958 --> 04:41:40,240 array so here's the outer array in this 7506 04:41:40,240 --> 04:41:42,878 array each element is an array so i'm 7507 04:41:42,878 --> 04:41:45,520 going to pass another array here and in 7508 04:41:45,520 --> 04:41:47,760 this array i'm going to pass a new input 7509 04:41:47,760 --> 04:41:52,560 set a 21 year old male so 21 comma 1. 7510 04:41:52,560 --> 04:41:54,400 that is like a new record 7511 04:41:54,400 --> 04:41:55,920 in this table 7512 04:41:55,920 --> 04:41:58,878 okay so this is one input set let's pass 7513 04:41:58,878 --> 04:42:01,680 another input set for a 22-year female 7514 04:42:01,680 --> 04:42:03,760 so here's another array 7515 04:42:03,760 --> 04:42:06,878 here we add 22 comma zero 7516 04:42:06,878 --> 04:42:08,560 so we're asking our model to make two 7517 04:42:08,560 --> 04:42:10,480 predictions at the same time 7518 04:42:10,480 --> 04:42:13,280 we get the result and store it 7519 04:42:13,280 --> 04:42:15,760 in a variable called predictions and 7520 04:42:15,760 --> 04:42:18,320 finally let's inspect that 7521 04:42:18,320 --> 04:42:20,840 in our notebook 7522 04:42:20,840 --> 04:42:23,680 run look what we got 7523 04:42:23,680 --> 04:42:26,240 our model is saying that a 21 year old 7524 04:42:26,240 --> 04:42:28,958 male likes hip hop and a 22 year old 7525 04:42:28,958 --> 04:42:31,200 female likes dance music 7526 04:42:31,200 --> 04:42:32,878 so our model could successfully make 7527 04:42:32,878 --> 04:42:35,040 predictions here beautiful but wait a 7528 04:42:35,040 --> 04:42:36,958 second building a model that makes 7529 04:42:36,958 --> 04:42:38,560 predictions accurately is not always 7530 04:42:38,560 --> 04:42:40,958 that easy as i told you earlier after we 7531 04:42:40,958 --> 04:42:43,040 build a model we need to measure its 7532 04:42:43,040 --> 04:42:45,520 accuracy and if it's not accurate enough 7533 04:42:45,520 --> 04:42:47,200 we should either fine-tune it or build 7534 04:42:47,200 --> 04:42:49,600 the model using a different algorithm so 7535 04:42:49,600 --> 04:42:50,718 in the next lecture i'm going to show 7536 04:42:50,718 --> 04:42:53,120 you how to measure the accuracy of a 7537 04:42:53,120 --> 04:42:55,520 model 7538 04:42:58,878 --> 04:43:00,000 in this lecture i'm going to show you 7539 04:43:00,000 --> 04:43:02,080 how to measure the accuracy of your 7540 04:43:02,080 --> 04:43:04,638 models now in order to do so first we 7541 04:43:04,638 --> 04:43:07,280 need to split our data set into two sets 7542 04:43:07,280 --> 04:43:09,120 one for training and the other for 7543 04:43:09,120 --> 04:43:11,840 testing because right now we're passing 7544 04:43:11,840 --> 04:43:14,160 the entire data set for training the 7545 04:43:14,160 --> 04:43:16,798 model and we're using two samples for 7546 04:43:16,798 --> 04:43:19,280 making predictions that is not enough to 7547 04:43:19,280 --> 04:43:21,600 calculate the accuracy of a model a 7548 04:43:21,600 --> 04:43:24,320 general rule of thumb is to allocate 70 7549 04:43:24,320 --> 04:43:26,878 to 80 percent of our data for training 7550 04:43:26,878 --> 04:43:28,718 and the other 20 to 30 percent for 7551 04:43:28,718 --> 04:43:31,360 testing then instead of passing only two 7552 04:43:31,360 --> 04:43:33,440 samples for making predictions we can 7553 04:43:33,440 --> 04:43:35,840 pass the data set we have for testing 7554 04:43:35,840 --> 04:43:37,520 we'll get the predictions and then we 7555 04:43:37,520 --> 04:43:39,440 can compare these predictions with the 7556 04:43:39,440 --> 04:43:42,400 actual values in the test set based on 7557 04:43:42,400 --> 04:43:44,400 that we can calculate the accuracy 7558 04:43:44,400 --> 04:43:46,480 that's really easy all we have to do is 7559 04:43:46,480 --> 04:43:48,560 to import a couple of functions and call 7560 04:43:48,560 --> 04:43:50,718 them in this code let me show you so 7561 04:43:50,718 --> 04:43:52,798 first on the top 7562 04:43:52,798 --> 04:43:54,798 from sklearn 7563 04:43:54,798 --> 04:43:58,560 that model underline selection module we 7564 04:43:58,560 --> 04:44:02,240 import a function called train test 7565 04:44:02,240 --> 04:44:04,320 split with this function we can easily 7566 04:44:04,320 --> 04:44:06,878 split our data set into two sets for 7567 04:44:06,878 --> 04:44:09,600 training and testing 7568 04:44:09,600 --> 04:44:13,280 now right here after we define x and y 7569 04:44:13,280 --> 04:44:15,280 sets we call this function 7570 04:44:15,280 --> 04:44:16,160 so 7571 04:44:16,160 --> 04:44:17,200 train 7572 04:44:17,200 --> 04:44:19,040 test split 7573 04:44:19,040 --> 04:44:21,680 we give it three arguments x 7574 04:44:21,680 --> 04:44:24,718 y and a keyword argument that specifies 7575 04:44:24,718 --> 04:44:28,400 the size of our test data set so test 7576 04:44:28,400 --> 04:44:31,920 underline size we set it to 0.2 so we 7577 04:44:31,920 --> 04:44:33,920 are allocating 20 of our data for 7578 04:44:33,920 --> 04:44:35,120 testing 7579 04:44:35,120 --> 04:44:38,400 now this function returns a tuple so we 7580 04:44:38,400 --> 04:44:40,840 can unpack it into four variables right 7581 04:44:40,840 --> 04:44:44,320 here x underline train 7582 04:44:44,320 --> 04:44:46,560 x underline test 7583 04:44:46,560 --> 04:44:48,400 y underline train 7584 04:44:48,400 --> 04:44:51,440 and y underline test 7585 04:44:51,440 --> 04:44:53,680 so the first two variables are the input 7586 04:44:53,680 --> 04:44:56,560 sets for training and testing and the 7587 04:44:56,560 --> 04:44:57,840 other 7588 04:44:57,840 --> 04:45:00,000 are the output sets for training and 7589 04:45:00,000 --> 04:45:01,200 testing 7590 04:45:01,200 --> 04:45:02,080 now 7591 04:45:02,080 --> 04:45:04,080 when training our model 7592 04:45:04,080 --> 04:45:05,920 instead of passing the entire data set 7593 04:45:05,920 --> 04:45:08,000 we want to pass only the training data 7594 04:45:08,000 --> 04:45:10,000 set so 7595 04:45:10,000 --> 04:45:10,798 x 7596 04:45:10,798 --> 04:45:12,160 underline train 7597 04:45:12,160 --> 04:45:13,840 and y 7598 04:45:13,840 --> 04:45:15,920 underline train 7599 04:45:15,920 --> 04:45:18,000 also when making predictions instead of 7600 04:45:18,000 --> 04:45:20,240 passing these two samples we pass 7601 04:45:20,240 --> 04:45:22,718 x underline test 7602 04:45:22,718 --> 04:45:24,878 so that is the data set that contains 7603 04:45:24,878 --> 04:45:27,520 input values for testing 7604 04:45:27,520 --> 04:45:30,080 now we get the predictions to calculate 7605 04:45:30,080 --> 04:45:31,920 the accuracy we simply have to compare 7606 04:45:31,920 --> 04:45:34,718 these predictions with the actual values 7607 04:45:34,718 --> 04:45:37,920 we have in our output set for testing 7608 04:45:37,920 --> 04:45:40,080 that is very easy first on the top we 7609 04:45:40,080 --> 04:45:42,160 need to import a function 7610 04:45:42,160 --> 04:45:46,480 so from sklearn that metrics 7611 04:45:46,480 --> 04:45:48,000 import 7612 04:45:48,000 --> 04:45:49,200 accuracy 7613 04:45:49,200 --> 04:45:50,878 underlying score 7614 04:45:50,878 --> 04:45:52,560 now at the very end we call this 7615 04:45:52,560 --> 04:45:56,480 function so accuracy score 7616 04:45:56,480 --> 04:45:58,320 and 7617 04:45:58,320 --> 04:46:01,200 give it two arguments y underline test 7618 04:46:01,200 --> 04:46:04,840 which contains the expected values and 7619 04:46:04,840 --> 04:46:06,400 predictions 7620 04:46:06,400 --> 04:46:09,200 which contains the actual values 7621 04:46:09,200 --> 04:46:11,120 now this function returns an accuracy 7622 04:46:11,120 --> 04:46:14,160 score between 0 to 1. so we can store it 7623 04:46:14,160 --> 04:46:15,920 here 7624 04:46:15,920 --> 04:46:19,600 and simply display it on the console 7625 04:46:19,600 --> 04:46:22,878 so let's go ahead and run this program 7626 04:46:22,878 --> 04:46:25,280 so the accuracy score is one or a 7627 04:46:25,280 --> 04:46:27,280 hundred percent but if we run this one 7628 04:46:27,280 --> 04:46:28,878 more time we're going to see a different 7629 04:46:28,878 --> 04:46:31,200 result because every time we split our 7630 04:46:31,200 --> 04:46:33,520 data set into training and test sets 7631 04:46:33,520 --> 04:46:35,360 will have different data sets because 7632 04:46:35,360 --> 04:46:37,760 this function randomly picks data for 7633 04:46:37,760 --> 04:46:39,440 training and testing 7634 04:46:39,440 --> 04:46:41,680 let me show you so put the cursor in the 7635 04:46:41,680 --> 04:46:43,200 cell now you can see this cell is 7636 04:46:43,200 --> 04:46:45,280 activated note that if you click this 7637 04:46:45,280 --> 04:46:47,440 button here it will run this cell and 7638 04:46:47,440 --> 04:46:50,638 also insert a new cell below this cell 7639 04:46:50,638 --> 04:46:52,878 let me show you so if i go to the second 7640 04:46:52,878 --> 04:46:53,760 cell 7641 04:46:53,760 --> 04:46:55,840 press escape button 7642 04:46:55,840 --> 04:46:57,920 now we are in the command mode press d 7643 04:46:57,920 --> 04:46:58,958 twice 7644 04:46:58,958 --> 04:47:00,798 okay now it's deleted 7645 04:47:00,798 --> 04:47:03,440 if we click the run button 7646 04:47:03,440 --> 04:47:05,600 you can see this code was executed and 7647 04:47:05,600 --> 04:47:07,520 now we have a new cell 7648 04:47:07,520 --> 04:47:08,878 so if you want to run our first cell 7649 04:47:08,878 --> 04:47:10,638 multiple times every time we have to 7650 04:47:10,638 --> 04:47:12,560 click this and then run it and then 7651 04:47:12,560 --> 04:47:14,560 click again and run it it's a little bit 7652 04:47:14,560 --> 04:47:16,958 tedious so i'll show you a shortcut 7653 04:47:16,958 --> 04:47:18,718 activate the first cell 7654 04:47:18,718 --> 04:47:21,920 and press ctrl and enter 7655 04:47:21,920 --> 04:47:24,080 this runs the current cell without 7656 04:47:24,080 --> 04:47:27,680 adding a new cell below it so back here 7657 04:47:27,680 --> 04:47:29,600 let's try it multiple times okay now 7658 04:47:29,600 --> 04:47:33,200 look the accuracy dropped to 0.75 it's 7659 04:47:33,200 --> 04:47:35,840 still good so the accuracy score here is 7660 04:47:35,840 --> 04:47:39,280 somewhere between 75 percent to 100 7661 04:47:39,280 --> 04:47:40,878 but let me show you something 7662 04:47:40,878 --> 04:47:44,560 if i change the test size from 0.2 7663 04:47:44,560 --> 04:47:47,680 to 0.8 so essentially we're using only 7664 04:47:47,680 --> 04:47:50,480 20 of our data for training this model 7665 04:47:50,480 --> 04:47:52,240 and we're using the other 80 7666 04:47:52,240 --> 04:47:54,718 for testing now let's see what happens 7667 04:47:54,718 --> 04:47:57,280 when we run this cell multiple times so 7668 04:47:57,280 --> 04:47:59,040 control and enter 7669 04:47:59,040 --> 04:48:01,120 look the accuracy immediately dropped to 7670 04:48:01,120 --> 04:48:03,840 0.4 one more time 7671 04:48:03,840 --> 04:48:06,718 now 46 percent 40 7672 04:48:06,718 --> 04:48:08,400 26 7673 04:48:08,400 --> 04:48:10,080 it's really really bad 7674 04:48:10,080 --> 04:48:12,080 the reason this is happening is because 7675 04:48:12,080 --> 04:48:14,160 we are using very little data for 7676 04:48:14,160 --> 04:48:16,240 training this model this is one of the 7677 04:48:16,240 --> 04:48:18,480 key concepts in machine learning the 7678 04:48:18,480 --> 04:48:20,320 more data we give to our model and the 7679 04:48:20,320 --> 04:48:22,400 cleaner the data is we get the better 7680 04:48:22,400 --> 04:48:24,638 result so if you have duplicates 7681 04:48:24,638 --> 04:48:27,360 irrelevant data or incomplete values our 7682 04:48:27,360 --> 04:48:29,200 model will learn bad patterns in our 7683 04:48:29,200 --> 04:48:31,040 data that is why it's really important 7684 04:48:31,040 --> 04:48:33,120 to clean our data before training our 7685 04:48:33,120 --> 04:48:34,080 model 7686 04:48:34,080 --> 04:48:37,600 now let's change this back to 0.2 7687 04:48:37,600 --> 04:48:39,200 run this one more time 7688 04:48:39,200 --> 04:48:41,440 okay now the accuracy is one 7689 04:48:41,440 --> 04:48:42,958 seventy-five percent 7690 04:48:42,958 --> 04:48:45,280 now we drop to fifty percent again the 7691 04:48:45,280 --> 04:48:46,798 reason this is happening is because we 7692 04:48:46,798 --> 04:48:49,120 don't have enough data some machine 7693 04:48:49,120 --> 04:48:51,360 learning problems require thousands or 7694 04:48:51,360 --> 04:48:53,680 even millions of samples to train a 7695 04:48:53,680 --> 04:48:55,920 model the more complex the problem is 7696 04:48:55,920 --> 04:48:58,080 the more data we need for example here 7697 04:48:58,080 --> 04:49:00,000 we're only dealing with a table of three 7698 04:49:00,000 --> 04:49:02,160 columns but if you want to build a model 7699 04:49:02,160 --> 04:49:04,400 to tell if a picture is a cat or a dog 7700 04:49:04,400 --> 04:49:06,718 or a horse or a lion we'll need millions 7701 04:49:06,718 --> 04:49:08,718 of pictures the more animals we want to 7702 04:49:08,718 --> 04:49:10,958 support the more pictures we need in the 7703 04:49:10,958 --> 04:49:12,320 next lecture we're going to talk about 7704 04:49:12,320 --> 04:49:15,520 model persistence 7705 04:49:19,040 --> 04:49:21,280 so this is a very basic implementation 7706 04:49:21,280 --> 04:49:23,520 of building and training a model to make 7707 04:49:23,520 --> 04:49:24,798 predictions 7708 04:49:24,798 --> 04:49:26,878 now to simplify things i have removed 7709 04:49:26,878 --> 04:49:28,320 all the code that we wrote in the last 7710 04:49:28,320 --> 04:49:30,798 lecture for calculating the accuracy 7711 04:49:30,798 --> 04:49:31,840 because in this lecture we're going to 7712 04:49:31,840 --> 04:49:33,920 focus on a different topic 7713 04:49:33,920 --> 04:49:35,200 so basically 7714 04:49:35,200 --> 04:49:36,958 we import our data set 7715 04:49:36,958 --> 04:49:38,320 create a model 7716 04:49:38,320 --> 04:49:40,718 train it and then ask it to make 7717 04:49:40,718 --> 04:49:42,000 predictions 7718 04:49:42,000 --> 04:49:44,480 now this piece of code that you see here 7719 04:49:44,480 --> 04:49:46,480 is not what we want to run every time we 7720 04:49:46,480 --> 04:49:48,480 have a new user or every time we want to 7721 04:49:48,480 --> 04:49:51,040 make recommendations to an existing user 7722 04:49:51,040 --> 04:49:52,718 because training a model can sometimes 7723 04:49:52,718 --> 04:49:55,200 be really time consuming in this example 7724 04:49:55,200 --> 04:49:57,040 we're dealing with a very small data set 7725 04:49:57,040 --> 04:49:59,520 that has only 20 records but in real 7726 04:49:59,520 --> 04:50:01,360 applications we might have a data set 7727 04:50:01,360 --> 04:50:03,840 with thousands or millions of samples 7728 04:50:03,840 --> 04:50:05,440 training a model for that might take 7729 04:50:05,440 --> 04:50:08,400 seconds minutes or even hours so that is 7730 04:50:08,400 --> 04:50:11,520 why model persistence is important once 7731 04:50:11,520 --> 04:50:14,000 in a while we build and train our model 7732 04:50:14,000 --> 04:50:16,240 and then we'll save it to a file now 7733 04:50:16,240 --> 04:50:18,160 next time we want to make predictions we 7734 04:50:18,160 --> 04:50:20,400 simply load the model from the file and 7735 04:50:20,400 --> 04:50:22,798 ask it to make predictions that model is 7736 04:50:22,798 --> 04:50:24,958 already trained we don't need to retrain 7737 04:50:24,958 --> 04:50:27,520 it it's like an intelligent person so 7738 04:50:27,520 --> 04:50:29,520 let me show you how to do this it's very 7739 04:50:29,520 --> 04:50:30,718 very easy 7740 04:50:30,718 --> 04:50:32,878 on the top from 7741 04:50:32,878 --> 04:50:35,680 sklearn.externals 7742 04:50:35,680 --> 04:50:37,200 module 7743 04:50:37,200 --> 04:50:38,718 we import 7744 04:50:38,718 --> 04:50:39,920 joblib 7745 04:50:39,920 --> 04:50:42,240 this joblib object has methods for 7746 04:50:42,240 --> 04:50:44,638 saving and loading models 7747 04:50:44,638 --> 04:50:45,440 so 7748 04:50:45,440 --> 04:50:49,120 after we train our model we simply call 7749 04:50:49,120 --> 04:50:52,120 joblib.dump 7750 04:50:52,240 --> 04:50:53,920 and give it to arguments 7751 04:50:53,920 --> 04:50:55,120 our model 7752 04:50:55,120 --> 04:50:56,718 and the name of the file in which we 7753 04:50:56,718 --> 04:50:58,480 want to store this model 7754 04:50:58,480 --> 04:51:00,400 let's call that music 7755 04:51:00,400 --> 04:51:02,560 dash recommender 7756 04:51:02,560 --> 04:51:03,440 dot 7757 04:51:03,440 --> 04:51:05,200 job lib 7758 04:51:05,200 --> 04:51:07,600 that's all we have to do now temporarily 7759 04:51:07,600 --> 04:51:09,520 i'm going to comment out this line we 7760 04:51:09,520 --> 04:51:10,958 don't want to make any predictions we 7761 04:51:10,958 --> 04:51:12,798 just want to store our trained model in 7762 04:51:12,798 --> 04:51:14,638 a file so 7763 04:51:14,638 --> 04:51:16,560 let's run the cell with control and 7764 04:51:16,560 --> 04:51:18,240 slash 7765 04:51:18,240 --> 04:51:19,360 okay 7766 04:51:19,360 --> 04:51:20,160 look 7767 04:51:20,160 --> 04:51:21,920 in the output we have an array that 7768 04:51:21,920 --> 04:51:24,080 contains the name of our model file so 7769 04:51:24,080 --> 04:51:25,840 this is the return value of the dump 7770 04:51:25,840 --> 04:51:27,280 method 7771 04:51:27,280 --> 04:51:29,440 now back to our desktop right next to my 7772 04:51:29,440 --> 04:51:31,600 notebook you can see our job live file 7773 04:51:31,600 --> 04:51:33,440 this is where our model is stored it's 7774 04:51:33,440 --> 04:51:35,360 simply a binary file 7775 04:51:35,360 --> 04:51:37,680 now back to our jupyter notebook as i 7776 04:51:37,680 --> 04:51:39,440 told you before in a real application we 7777 04:51:39,440 --> 04:51:42,000 don't want to train a model every time 7778 04:51:42,000 --> 04:51:42,878 so 7779 04:51:42,878 --> 04:51:45,440 let's comment out these few lines so 7780 04:51:45,440 --> 04:51:47,680 i've selected these few lines on mac we 7781 04:51:47,680 --> 04:51:50,480 can press command and slash on windows 7782 04:51:50,480 --> 04:51:52,400 control slash 7783 04:51:52,400 --> 04:51:54,638 okay these lines are commented out now 7784 04:51:54,638 --> 04:51:56,798 this time instead of dumping our model 7785 04:51:56,798 --> 04:51:58,400 we're going to load it so we call the 7786 04:51:58,400 --> 04:51:59,600 load method 7787 04:51:59,600 --> 04:52:01,920 we don't have the model we simply pass 7788 04:52:01,920 --> 04:52:04,718 the name of our model file this returns 7789 04:52:04,718 --> 04:52:06,878 our trained model 7790 04:52:06,878 --> 04:52:09,440 now with these two lines we can simply 7791 04:52:09,440 --> 04:52:11,760 make predictions so 7792 04:52:11,760 --> 04:52:13,760 earlier we assumed that men between 20 7793 04:52:13,760 --> 04:52:16,718 and 25 like hip-hop music 7794 04:52:16,718 --> 04:52:18,958 let's print predictions and see if our 7795 04:52:18,958 --> 04:52:21,360 model is behaving correctly or not 7796 04:52:21,360 --> 04:52:24,560 so ctrl and enter there you go 7797 04:52:24,560 --> 04:52:26,638 so this is how we persist and load 7798 04:52:26,638 --> 04:52:29,040 models 7799 04:52:32,638 --> 04:52:34,240 earlier in this section i told you that 7800 04:52:34,240 --> 04:52:36,080 decision trees are the easiest to 7801 04:52:36,080 --> 04:52:37,520 understand and that's why we started 7802 04:52:37,520 --> 04:52:39,920 machine learning with decision trees in 7803 04:52:39,920 --> 04:52:41,440 this lecture we're going to export our 7804 04:52:41,440 --> 04:52:44,080 model in a visual format so you will see 7805 04:52:44,080 --> 04:52:47,200 how this model makes predictions that is 7806 04:52:47,200 --> 04:52:49,200 really really cool let me show you 7807 04:52:49,200 --> 04:52:50,160 so 7808 04:52:50,160 --> 04:52:52,480 once again i've simplified this code so 7809 04:52:52,480 --> 04:52:55,200 we simply import our data set 7810 04:52:55,200 --> 04:52:56,080 create 7811 04:52:56,080 --> 04:52:58,320 input and output sets 7812 04:52:58,320 --> 04:53:01,840 create a model and train it that's all 7813 04:53:01,840 --> 04:53:02,958 we are doing 7814 04:53:02,958 --> 04:53:04,878 now i want you to follow along with me 7815 04:53:04,878 --> 04:53:07,200 type everything exactly as i show you in 7816 04:53:07,200 --> 04:53:08,798 this lecture don't worry about what 7817 04:53:08,798 --> 04:53:09,920 everything means we're going to come 7818 04:53:09,920 --> 04:53:11,360 back to it shortly 7819 04:53:11,360 --> 04:53:13,600 so on the top 7820 04:53:13,600 --> 04:53:15,200 from sklearn 7821 04:53:15,200 --> 04:53:17,760 import tree 7822 04:53:17,760 --> 04:53:20,160 this object has a method for exporting 7823 04:53:20,160 --> 04:53:23,040 our decision tree in a graphical format 7824 04:53:23,040 --> 04:53:25,440 so after we train our model 7825 04:53:25,440 --> 04:53:27,200 let's call tree 7826 04:53:27,200 --> 04:53:31,360 dot export underline graph viz 7827 04:53:31,360 --> 04:53:32,878 now here are a few arguments we need to 7828 04:53:32,878 --> 04:53:36,080 pass the first argument is our model 7829 04:53:36,080 --> 04:53:37,840 the second is the name of the output 7830 04:53:37,840 --> 04:53:39,920 file so here we're going to use keyword 7831 04:53:39,920 --> 04:53:41,840 arguments because this method takes so 7832 04:53:41,840 --> 04:53:43,120 many parameters and we want to 7833 04:53:43,120 --> 04:53:45,360 selectively pass keyword arguments 7834 04:53:45,360 --> 04:53:47,600 without worrying about their order 7835 04:53:47,600 --> 04:53:48,798 so 7836 04:53:48,798 --> 04:53:50,958 the parameter we're going to set is out 7837 04:53:50,958 --> 04:53:52,480 underline file 7838 04:53:52,480 --> 04:53:54,080 let's set this to 7839 04:53:54,080 --> 04:53:57,120 music dash recommender 7840 04:53:57,120 --> 04:54:00,878 dot dot this is the dot format which is 7841 04:54:00,878 --> 04:54:03,040 a graph description language you'll see 7842 04:54:03,040 --> 04:54:04,560 that shortly 7843 04:54:04,560 --> 04:54:06,480 the other parameter we want to set 7844 04:54:06,480 --> 04:54:09,760 is feature underline names we set this 7845 04:54:09,760 --> 04:54:12,560 to an array of two strings 7846 04:54:12,560 --> 04:54:13,520 age 7847 04:54:13,520 --> 04:54:16,718 and gender these are the features or the 7848 04:54:16,718 --> 04:54:19,360 columns of our data set so they are the 7849 04:54:19,360 --> 04:54:22,560 properties or features of our data okay 7850 04:54:22,560 --> 04:54:24,400 the other parameter 7851 04:54:24,400 --> 04:54:28,160 is class names so class underline names 7852 04:54:28,160 --> 04:54:29,440 we should set this to the list of 7853 04:54:29,440 --> 04:54:32,798 classes or labels we have in our output 7854 04:54:32,798 --> 04:54:35,840 data set like hip hop jazz classical and 7855 04:54:35,840 --> 04:54:36,798 so on 7856 04:54:36,798 --> 04:54:39,760 so this white data set includes all the 7857 04:54:39,760 --> 04:54:42,320 genres or all the classes of our data 7858 04:54:42,320 --> 04:54:44,000 but they're repeated a few times in this 7859 04:54:44,000 --> 04:54:45,920 data set so 7860 04:54:45,920 --> 04:54:48,878 here we call y dot unique 7861 04:54:48,878 --> 04:54:51,600 this returns the unique list of classes 7862 04:54:51,600 --> 04:54:53,680 now we should sort this alphabetically 7863 04:54:53,680 --> 04:54:56,320 so we call the sorted function 7864 04:54:56,320 --> 04:55:01,520 and pass the result a y dot unique 7865 04:55:01,520 --> 04:55:02,958 the next parameter 7866 04:55:02,958 --> 04:55:04,638 is label 7867 04:55:04,638 --> 04:55:07,360 we set this to a string all 7868 04:55:07,360 --> 04:55:09,040 once again don't worry about the details 7869 04:55:09,040 --> 04:55:10,400 of these parameters we're going to come 7870 04:55:10,400 --> 04:55:14,240 back to this shortly so set label to all 7871 04:55:14,240 --> 04:55:16,160 then round it 7872 04:55:16,160 --> 04:55:18,480 to true and finally 7873 04:55:18,480 --> 04:55:20,958 filled to true 7874 04:55:20,958 --> 04:55:23,040 so this is the end result 7875 04:55:23,040 --> 04:55:25,040 now let's run this cell using ctrl and 7876 04:55:25,040 --> 04:55:26,560 enter 7877 04:55:26,560 --> 04:55:28,080 okay 7878 04:55:28,080 --> 04:55:30,320 here we have a new file music 7879 04:55:30,320 --> 04:55:32,878 recommender dot dot that's a little bit 7880 04:55:32,878 --> 04:55:35,600 funny so we want to open this file with 7881 04:55:35,600 --> 04:55:38,638 vs code so drag and drop this into a vs 7882 04:55:38,638 --> 04:55:41,840 code window 7883 04:55:42,718 --> 04:55:45,680 okay here is a dot format it's a textual 7884 04:55:45,680 --> 04:55:48,160 language for describing graphs 7885 04:55:48,160 --> 04:55:50,320 now to visualize this graph we need to 7886 04:55:50,320 --> 04:55:52,798 install an extension in vs code so on 7887 04:55:52,798 --> 04:55:55,760 the left side click the extensions panel 7888 04:55:55,760 --> 04:55:59,280 and search for dot d o t 7889 04:55:59,280 --> 04:56:01,120 look at the second extension here 7890 04:56:01,120 --> 04:56:04,000 graphviz or dot language 7891 04:56:04,000 --> 04:56:06,080 by staphon vs 7892 04:56:06,080 --> 04:56:08,240 go ahead and install this extension and 7893 04:56:08,240 --> 04:56:11,120 then reload vs code once you do that you 7894 04:56:11,120 --> 04:56:13,760 can visualize this dot file so let me 7895 04:56:13,760 --> 04:56:16,000 close this tab 7896 04:56:16,000 --> 04:56:17,040 all right 7897 04:56:17,040 --> 04:56:18,878 look at this dot dot here on the right 7898 04:56:18,878 --> 04:56:21,040 side click this you should have a new 7899 04:56:21,040 --> 04:56:24,080 menu open preview to the site so click 7900 04:56:24,080 --> 04:56:25,600 that 7901 04:56:25,600 --> 04:56:27,680 all right here's the visualization of 7902 04:56:27,680 --> 04:56:30,000 our decision tree let's close the dot 7903 04:56:30,000 --> 04:56:31,120 file 7904 04:56:31,120 --> 04:56:33,680 there you go this is exactly how our 7905 04:56:33,680 --> 04:56:36,080 model makes predictions so we have this 7906 04:56:36,080 --> 04:56:38,560 binary tree which means every node can 7907 04:56:38,560 --> 04:56:41,760 have a maximum of two children on top of 7908 04:56:41,760 --> 04:56:44,480 each node we have a condition if this 7909 04:56:44,480 --> 04:56:46,160 condition is true we go to the child 7910 04:56:46,160 --> 04:56:48,400 node on the left side otherwise we go to 7911 04:56:48,400 --> 04:56:50,400 the child node on the right side so 7912 04:56:50,400 --> 04:56:52,080 let's see what's happening here 7913 04:56:52,080 --> 04:56:55,200 the first condition is age less than or 7914 04:56:55,200 --> 04:56:58,400 equal to 30.5 if this condition is false 7915 04:56:58,400 --> 04:57:00,400 that means that user is 30 years or 7916 04:57:00,400 --> 04:57:02,560 older so the genre of the music that 7917 04:57:02,560 --> 04:57:05,200 they're interested in is classical 7918 04:57:05,200 --> 04:57:08,000 so here we're classifying people based 7919 04:57:08,000 --> 04:57:10,320 on their profile that is the reason we 7920 04:57:10,320 --> 04:57:12,958 have the word class here so a user who 7921 04:57:12,958 --> 04:57:15,280 is 30 years or older belongs to the 7922 04:57:15,280 --> 04:57:17,200 class of classical 7923 04:57:17,200 --> 04:57:19,760 or people who like classical music 7924 04:57:19,760 --> 04:57:22,240 now what if this condition is true that 7925 04:57:22,240 --> 04:57:25,840 means that user is younger than 30. so 7926 04:57:25,840 --> 04:57:27,440 now we check the gender 7927 04:57:27,440 --> 04:57:30,480 if it's less than 0.5 which basically 7928 04:57:30,480 --> 04:57:32,718 means if it equals to zero then we're 7929 04:57:32,718 --> 04:57:34,718 dealing with a female so we go to the 7930 04:57:34,718 --> 04:57:36,080 child node here 7931 04:57:36,080 --> 04:57:38,560 now once again we have another condition 7932 04:57:38,560 --> 04:57:40,320 so we're dealing with a female who is 7933 04:57:40,320 --> 04:57:42,400 younger than 30. once again we need to 7934 04:57:42,400 --> 04:57:45,958 check their age so is the age less than 7935 04:57:45,958 --> 04:57:49,360 25.5 if that's the case then that user 7936 04:57:49,360 --> 04:57:52,080 likes dance music otherwise they like 7937 04:57:52,080 --> 04:57:53,760 acoustic music 7938 04:57:53,760 --> 04:57:55,600 so this is the decision tree that our 7939 04:57:55,600 --> 04:57:58,160 model uses to make predictions now if 7940 04:57:58,160 --> 04:57:59,520 you're wondering why we have these 7941 04:57:59,520 --> 04:58:01,638 floating point numbers like 7942 04:58:01,638 --> 04:58:04,240 25.5 these are basically the rules that 7943 04:58:04,240 --> 04:58:06,240 our model generates based on the 7944 04:58:06,240 --> 04:58:08,798 patterns that it finds in our data set 7945 04:58:08,798 --> 04:58:10,958 as we give our model more data these 7946 04:58:10,958 --> 04:58:12,878 rules will change so they're not always 7947 04:58:12,878 --> 04:58:15,680 the same also the more columns or more 7948 04:58:15,680 --> 04:58:17,760 features we have our decision tree is 7949 04:58:17,760 --> 04:58:19,760 going to get more complex currently we 7950 04:58:19,760 --> 04:58:23,120 have only two features age and gender 7951 04:58:23,120 --> 04:58:24,718 now back to our code let me quickly 7952 04:58:24,718 --> 04:58:25,920 explain the meaning of all these 7953 04:58:25,920 --> 04:58:28,638 parameters we set fill to true so each 7954 04:58:28,638 --> 04:58:31,360 box or each node is filled with a color 7955 04:58:31,360 --> 04:58:33,360 we set rounded to true 7956 04:58:33,360 --> 04:58:35,840 so they have rounded corners 7957 04:58:35,840 --> 04:58:37,600 we set label to all 7958 04:58:37,600 --> 04:58:39,600 so every node has labels that we can 7959 04:58:39,600 --> 04:58:40,798 read 7960 04:58:40,798 --> 04:58:42,958 we set class names to the unique list of 7961 04:58:42,958 --> 04:58:45,040 genres and that's for displaying the 7962 04:58:45,040 --> 04:58:47,280 class for each node right here 7963 04:58:47,280 --> 04:58:49,680 and we set feature names to age and 7964 04:58:49,680 --> 04:58:52,320 gender so we can see the rules in our 7965 04:58:52,320 --> 04:58:54,638 notes 7966 04:58:55,040 --> 04:58:56,400 so that was a short and sweet 7967 04:58:56,400 --> 04:58:58,240 introduction to machine learning now you 7968 04:58:58,240 --> 04:58:59,520 can use the materials that you learned 7969 04:58:59,520 --> 04:59:01,600 in this section and apply them to solve 7970 04:59:01,600 --> 04:59:03,520 a different sort of problems now here's 7971 04:59:03,520 --> 04:59:05,440 a question for you what ideas do you 7972 04:59:05,440 --> 04:59:06,958 have for machine learning what kind of 7973 04:59:06,958 --> 04:59:08,240 problems do you want to solve with 7974 04:59:08,240 --> 04:59:10,320 machine learning use the comment box 7975 04:59:10,320 --> 04:59:11,920 below and let me know i love to hear 7976 04:59:11,920 --> 04:59:13,280 about your ideas 7977 04:59:13,280 --> 04:59:14,878 next we're going to use python and a 7978 04:59:14,878 --> 04:59:16,958 popular framework called django to build 7979 04:59:16,958 --> 04:59:19,440 a web application are you ready let's 7980 04:59:19,440 --> 04:59:22,680 get started 7981 04:59:24,080 --> 04:59:25,360 in this tutorial i'm going to show you 7982 04:59:25,360 --> 04:59:27,360 how to create your first website with 7983 04:59:27,360 --> 04:59:29,520 python at a very popular framework 7984 04:59:29,520 --> 04:59:31,920 called django so it's spelled with a 7985 04:59:31,920 --> 04:59:34,560 silent d and it's pronounced django it's 7986 04:59:34,560 --> 04:59:36,958 a web framework for perfectionists with 7987 04:59:36,958 --> 04:59:38,878 deadlines so if you're a perfectionist 7988 04:59:38,878 --> 04:59:40,638 and you want to quickly put together a 7989 04:59:40,638 --> 04:59:43,920 website that is fast scalable and secure 7990 04:59:43,920 --> 04:59:45,760 django is your best friend 7991 04:59:45,760 --> 04:59:48,400 and here are the popular websites built 7992 04:59:48,400 --> 04:59:52,160 with django like instagram spotify 7993 04:59:52,160 --> 04:59:55,280 youtube washington post and so on now 7994 04:59:55,280 --> 04:59:56,958 you might ask what is a framework and 7995 04:59:56,958 --> 04:59:58,878 why do we need a framework to build a 7996 04:59:58,878 --> 05:00:01,040 web application or a website 7997 05:00:01,040 --> 05:00:02,638 well a framework is essentially a 7998 05:00:02,638 --> 05:00:05,120 library of reusable modules these 7999 05:00:05,120 --> 05:00:07,360 modules provide functionality for common 8000 05:00:07,360 --> 05:00:09,360 tasks for example in case of a web 8001 05:00:09,360 --> 05:00:11,520 framework like django we have modules to 8002 05:00:11,520 --> 05:00:15,280 work with http requests urls sessions 8003 05:00:15,280 --> 05:00:17,440 cookies and so on these are the concerns 8004 05:00:17,440 --> 05:00:19,840 of pretty much every website or web 8005 05:00:19,840 --> 05:00:21,840 application out there so all this 8006 05:00:21,840 --> 05:00:23,760 functionality is already baked into 8007 05:00:23,760 --> 05:00:25,440 django we don't have to code it from 8008 05:00:25,440 --> 05:00:27,680 scratch that is why we use a framework 8009 05:00:27,680 --> 05:00:28,958 like django 8010 05:00:28,958 --> 05:00:30,878 now technically a framework is more than 8011 05:00:30,878 --> 05:00:33,120 a library so apart from providing these 8012 05:00:33,120 --> 05:00:35,440 modules it also defines a structure for 8013 05:00:35,440 --> 05:00:37,680 our application it tells us what folders 8014 05:00:37,680 --> 05:00:39,840 and files we should have in our project 8015 05:00:39,840 --> 05:00:42,320 so this provides a consistency amongst 8016 05:00:42,320 --> 05:00:44,160 various django projects so as you move 8017 05:00:44,160 --> 05:00:46,080 from one company to another and work on 8018 05:00:46,080 --> 05:00:47,920 different projects you can easily 8019 05:00:47,920 --> 05:00:49,760 transition from one project to another 8020 05:00:49,760 --> 05:00:51,600 because all these applications follow 8021 05:00:51,600 --> 05:00:53,840 the same structure 8022 05:00:53,840 --> 05:00:55,520 now with that introduction let's create 8023 05:00:55,520 --> 05:00:57,840 our first django project 8024 05:00:57,840 --> 05:01:00,080 so back to pycharm close the hello world 8025 05:01:00,080 --> 05:01:03,760 project and create a new project 8026 05:01:03,760 --> 05:01:07,120 okay let's call this new project pi 8027 05:01:07,120 --> 05:01:08,560 shop 8028 05:01:08,560 --> 05:01:10,560 go ahead 8029 05:01:10,560 --> 05:01:12,480 all right now down the bottom 8030 05:01:12,480 --> 05:01:15,440 let's open up a terminal window 8031 05:01:15,440 --> 05:01:16,878 in this window we're going to install 8032 05:01:16,878 --> 05:01:20,958 django so we use pip to install django 8033 05:01:20,958 --> 05:01:23,520 but be sure to add two equal signs here 8034 05:01:23,520 --> 05:01:25,840 followed by 2.1 with this we're telling 8035 05:01:25,840 --> 05:01:27,600 pip that we want to install django 8036 05:01:27,600 --> 05:01:30,878 version 2.1 the reason i'm doing this is 8037 05:01:30,878 --> 05:01:32,000 because in the future when you're 8038 05:01:32,000 --> 05:01:34,240 watching this video chances are there is 8039 05:01:34,240 --> 05:01:36,000 a newer version of django out there i 8040 05:01:36,000 --> 05:01:37,440 want to make sure that you can easily 8041 05:01:37,440 --> 05:01:39,280 follow these tutorials even though what 8042 05:01:39,280 --> 05:01:40,878 i'm going to show you in these tutorials 8043 05:01:40,878 --> 05:01:42,958 will most likely work with the latest 8044 05:01:42,958 --> 05:01:44,400 version of django i just want to be on 8045 05:01:44,400 --> 05:01:46,958 the safe side so let's go ahead with 8046 05:01:46,958 --> 05:01:49,360 this 8047 05:01:50,000 --> 05:01:52,480 all right django is installed next we 8048 05:01:52,480 --> 05:01:54,878 need to create a django project so here 8049 05:01:54,878 --> 05:01:57,600 press ctrl and l this cleans up our 8050 05:01:57,600 --> 05:01:59,600 terminal window you don't have to do it 8051 05:01:59,600 --> 05:02:01,360 but it's just easier to see 8052 05:02:01,360 --> 05:02:03,360 to create a django project we need to 8053 05:02:03,360 --> 05:02:05,680 execute this command django 8054 05:02:05,680 --> 05:02:07,520 dash admin 8055 05:02:07,520 --> 05:02:09,440 space start 8056 05:02:09,440 --> 05:02:10,638 project 8057 05:02:10,638 --> 05:02:11,600 space 8058 05:02:11,600 --> 05:02:13,680 we're going to call this project pi 8059 05:02:13,680 --> 05:02:14,958 shop 8060 05:02:14,958 --> 05:02:16,798 space period 8061 05:02:16,798 --> 05:02:19,760 so when we install django django brings 8062 05:02:19,760 --> 05:02:22,320 a command line utility called django 8063 05:02:22,320 --> 05:02:24,400 admin so this is a utility or program 8064 05:02:24,400 --> 05:02:26,320 that we can execute from the command 8065 05:02:26,320 --> 05:02:28,798 line or terminal right here 8066 05:02:28,798 --> 05:02:31,680 now this program takes various arguments 8067 05:02:31,680 --> 05:02:32,958 in this case we want to use this 8068 05:02:32,958 --> 05:02:35,760 argument start project with this we're 8069 05:02:35,760 --> 05:02:38,400 going to create a project called pi shop 8070 05:02:38,400 --> 05:02:41,120 in the current folder so this period is 8071 05:02:41,120 --> 05:02:42,718 important here that means the current 8072 05:02:42,718 --> 05:02:43,760 folder 8073 05:02:43,760 --> 05:02:45,440 if you don't add this period this 8074 05:02:45,440 --> 05:02:47,280 utility is going to create an extra 8075 05:02:47,280 --> 05:02:48,798 folder and that looks a little bit 8076 05:02:48,798 --> 05:02:52,160 repetitive so let's go ahead with this 8077 05:02:52,160 --> 05:02:53,120 all right 8078 05:02:53,120 --> 05:02:55,680 now back to the project panel if you 8079 05:02:55,680 --> 05:02:58,080 expand pi shop 8080 05:02:58,080 --> 05:03:00,160 you can see that this is our project 8081 05:03:00,160 --> 05:03:02,160 that we created in pycharm and in this 8082 05:03:02,160 --> 05:03:03,760 folder we have 8083 05:03:03,760 --> 05:03:05,040 these files 8084 05:03:05,040 --> 05:03:06,798 so in it that pi you have seen this 8085 05:03:06,798 --> 05:03:09,120 before that means this folder is a 8086 05:03:09,120 --> 05:03:12,080 package so we can import various modules 8087 05:03:12,080 --> 05:03:14,638 from this package into other modules so 8088 05:03:14,638 --> 05:03:16,320 here we have the settings module in this 8089 05:03:16,320 --> 05:03:17,840 module we defined various settings for 8090 05:03:17,840 --> 05:03:19,520 our application you're going to see this 8091 05:03:19,520 --> 05:03:22,160 later in this course we also have the 8092 05:03:22,160 --> 05:03:24,240 urls module and with this module we 8093 05:03:24,240 --> 05:03:26,160 define what should the user see when 8094 05:03:26,160 --> 05:03:29,360 they go to let's say slash about slash 8095 05:03:29,360 --> 05:03:31,600 contact slash products 8096 05:03:31,600 --> 05:03:33,280 shopping cart again we're going to work 8097 05:03:33,280 --> 05:03:35,440 with this zone and finally we have this 8098 05:03:35,440 --> 05:03:39,040 module ws gi that is short for web 8099 05:03:39,040 --> 05:03:41,840 server gateway interface the purpose of 8100 05:03:41,840 --> 05:03:43,760 this module is to provide a standard 8101 05:03:43,760 --> 05:03:45,680 interface between applications built 8102 05:03:45,680 --> 05:03:48,160 with django and web servers this is an 8103 05:03:48,160 --> 05:03:49,920 advanced topic so for now don't worry 8104 05:03:49,920 --> 05:03:50,878 about it 8105 05:03:50,878 --> 05:03:53,920 the outside of this folder we also have 8106 05:03:53,920 --> 05:03:56,080 this new file in our project that is 8107 05:03:56,080 --> 05:03:57,680 manage.pi 8108 05:03:57,680 --> 05:03:59,680 as the name implies we use this module 8109 05:03:59,680 --> 05:04:02,400 to manage this django project with this 8110 05:04:02,400 --> 05:04:04,480 we can start our web server we can work 8111 05:04:04,480 --> 05:04:06,160 with our database again we're going to 8112 05:04:06,160 --> 05:04:07,600 work with this soon 8113 05:04:07,600 --> 05:04:10,240 so let's open up the terminal window one 8114 05:04:10,240 --> 05:04:11,840 more time 8115 05:04:11,840 --> 05:04:14,400 now type out this command python if 8116 05:04:14,400 --> 05:04:17,040 you're on windows or python 3 if you're 8117 05:04:17,040 --> 05:04:18,798 on mac because as i told you at the 8118 05:04:18,798 --> 05:04:20,878 beginning of the course mac by default 8119 05:04:20,878 --> 05:04:22,798 comes with an installation of python 8120 05:04:22,798 --> 05:04:25,040 that is python 2. but in this course 8121 05:04:25,040 --> 05:04:28,480 we're using python 3. so python 3 space 8122 05:04:28,480 --> 05:04:30,400 manage dot pi 8123 05:04:30,400 --> 05:04:31,360 space 8124 05:04:31,360 --> 05:04:33,280 run server 8125 05:04:33,280 --> 05:04:35,520 what is going on here so with python 8126 05:04:35,520 --> 05:04:37,760 interpreter we are running this program 8127 05:04:37,760 --> 05:04:40,878 manage.pi and passing run server as an 8128 05:04:40,878 --> 05:04:43,600 argument this manage.pi is essentially 8129 05:04:43,600 --> 05:04:46,240 the same program that we worked with 8130 05:04:46,240 --> 05:04:48,240 here django admin 8131 05:04:48,240 --> 05:04:50,240 but we use django admin before creating 8132 05:04:50,240 --> 05:04:52,320 a django project now we have a django 8133 05:04:52,320 --> 05:04:54,638 project so in this project we work with 8134 05:04:54,638 --> 05:04:56,638 manage.pipe this is a module that 8135 05:04:56,638 --> 05:04:59,040 contains some python code so we use this 8136 05:04:59,040 --> 05:05:02,400 module to manage our django project 8137 05:05:02,400 --> 05:05:05,120 so let's go ahead and run our 8138 05:05:05,120 --> 05:05:07,440 development web server 8139 05:05:07,440 --> 05:05:09,200 as you can see this started a 8140 05:05:09,200 --> 05:05:11,600 development web server at this address 8141 05:05:11,600 --> 05:05:15,240 http colon forward slash forward slash 8142 05:05:15,240 --> 05:05:18,798 127.0 0.0.1 that is the address of your 8143 05:05:18,798 --> 05:05:21,200 current machine now note that after this 8144 05:05:21,200 --> 05:05:24,240 we have colon 8000 unfortunately the 8145 05:05:24,240 --> 05:05:26,560 link below this address has a problem as 8146 05:05:26,560 --> 05:05:28,878 you can see the port is excluded so if 8147 05:05:28,878 --> 05:05:30,480 we click this 8148 05:05:30,480 --> 05:05:33,040 it opens up our web browser and points 8149 05:05:33,040 --> 05:05:34,798 it to this address so we don't have the 8150 05:05:34,798 --> 05:05:38,080 port number let's add colon 8000 at the 8151 05:05:38,080 --> 05:05:43,080 end and here's our first django website 8152 05:05:46,080 --> 05:05:48,240 so we created this django project 8153 05:05:48,240 --> 05:05:50,320 now this django project can contain 8154 05:05:50,320 --> 05:05:53,280 multiple apps these apps don't represent 8155 05:05:53,280 --> 05:05:55,360 the entire application there are small 8156 05:05:55,360 --> 05:05:58,080 functional areas in our django project 8157 05:05:58,080 --> 05:06:00,000 here's an example imagine you want to 8158 05:06:00,000 --> 05:06:01,920 build a website or a web application 8159 05:06:01,920 --> 05:06:04,638 like amazon amazon is a big website it 8160 05:06:04,638 --> 05:06:06,560 has so many different functions instead 8161 05:06:06,560 --> 05:06:08,480 of implementing all these functions in a 8162 05:06:08,480 --> 05:06:10,638 django project we divide this project 8163 05:06:10,638 --> 05:06:12,878 into small functional areas that are 8164 05:06:12,878 --> 05:06:15,680 focused on one concern for example we 8165 05:06:15,680 --> 05:06:17,760 could have functional areas like product 8166 05:06:17,760 --> 05:06:20,240 management order management customer 8167 05:06:20,240 --> 05:06:22,080 management these are completely 8168 05:06:22,080 --> 05:06:24,160 different functional areas the functions 8169 05:06:24,160 --> 05:06:26,160 we have for managing the customers are 8170 05:06:26,160 --> 05:06:27,680 different from the functions for 8171 05:06:27,680 --> 05:06:29,920 managing the products by the same token 8172 05:06:29,920 --> 05:06:31,680 when you go to an organization there are 8173 05:06:31,680 --> 05:06:34,160 small departments or small offices 8174 05:06:34,160 --> 05:06:37,040 specializing in different areas so with 8175 05:06:37,040 --> 05:06:39,440 this analogy we divide the django 8176 05:06:39,440 --> 05:06:42,320 project into multiple django apps each 8177 05:06:42,320 --> 05:06:45,040 app is focused on one functional area 8178 05:06:45,040 --> 05:06:46,400 so in this tutorial i'm going to show 8179 05:06:46,400 --> 05:06:48,718 you how to add a new app to manage the 8180 05:06:48,718 --> 05:06:51,120 products in our imaginary shop in the 8181 05:06:51,120 --> 05:06:52,958 future let's say we want to add a blog 8182 05:06:52,958 --> 05:06:54,798 to our online shop we could come back 8183 05:06:54,798 --> 05:06:56,160 here and create a new app that 8184 05:06:56,160 --> 05:06:58,560 represents a blog now the beauty of 8185 05:06:58,560 --> 05:07:00,240 these apps is that we can reuse them 8186 05:07:00,240 --> 05:07:02,638 across different django projects because 8187 05:07:02,638 --> 05:07:05,440 each app is essentially a python package 8188 05:07:05,440 --> 05:07:08,240 let me show you so back to our project 8189 05:07:08,240 --> 05:07:10,878 let's open up a terminal window 8190 05:07:10,878 --> 05:07:12,480 so this is the terminal window we opened 8191 05:07:12,480 --> 05:07:15,200 earlier where we started our web server 8192 05:07:15,200 --> 05:07:17,440 now up here we have this plus button for 8193 05:07:17,440 --> 05:07:19,440 creating a new terminal window so let's 8194 05:07:19,440 --> 05:07:20,638 click that 8195 05:07:20,638 --> 05:07:22,958 now let's type out this command python 8196 05:07:22,958 --> 05:07:25,600 on windows or python 3 on mac 8197 05:07:25,600 --> 05:07:28,000 manage dot pi so we're using the same 8198 05:07:28,000 --> 05:07:29,840 program the same module but this time 8199 05:07:29,840 --> 05:07:33,760 with a different argument start app 8200 05:07:33,760 --> 05:07:35,760 and we're going to call that app 8201 05:07:35,760 --> 05:07:37,200 products 8202 05:07:37,200 --> 05:07:40,160 let's go ahead all right now 8203 05:07:40,160 --> 05:07:42,240 here we have a new folder called 8204 05:07:42,240 --> 05:07:43,520 products 8205 05:07:43,520 --> 05:07:45,440 let's see what we have in this folder we 8206 05:07:45,440 --> 05:07:47,200 have a subfolder called migrations we'll 8207 05:07:47,200 --> 05:07:49,680 look at this later below that we have 8208 05:07:49,680 --> 05:07:53,520 this init.pi so products is a package we 8209 05:07:53,520 --> 05:07:55,760 can take this package and reuse it in 8210 05:07:55,760 --> 05:07:58,000 another django application or more 8211 05:07:58,000 --> 05:08:00,718 specifically django project we can also 8212 05:08:00,718 --> 05:08:03,760 publish this package to pi pi for other 8213 05:08:03,760 --> 05:08:06,320 people to reuse in their django projects 8214 05:08:06,320 --> 05:08:07,840 for example if someone else wants to 8215 05:08:07,840 --> 05:08:10,480 build an online shop they can reuse this 8216 05:08:10,480 --> 05:08:12,160 package for managing the list of 8217 05:08:12,160 --> 05:08:13,600 products they don't have to build this 8218 05:08:13,600 --> 05:08:16,320 functionality from scratch okay we have 8219 05:08:16,320 --> 05:08:18,480 this module admin and this is used for 8220 05:08:18,480 --> 05:08:21,200 defining how the administrative panel of 8221 05:08:21,200 --> 05:08:23,040 this app will look like again we're 8222 05:08:23,040 --> 05:08:24,798 going to come back to this later 8223 05:08:24,798 --> 05:08:27,040 below that we have this module apps 8224 05:08:27,040 --> 05:08:28,480 unfortunately the name is a little bit 8225 05:08:28,480 --> 05:08:30,958 confusing because right now we are 8226 05:08:30,958 --> 05:08:33,520 inside of one app that is the products 8227 05:08:33,520 --> 05:08:35,520 app and it doesn't make sense to have a 8228 05:08:35,520 --> 05:08:38,480 module called apps inside of an app so i 8229 05:08:38,480 --> 05:08:40,480 don't know why django creators decided 8230 05:08:40,480 --> 05:08:42,638 to call this module apps but basically 8231 05:08:42,638 --> 05:08:44,718 we use this module to store various 8232 05:08:44,718 --> 05:08:47,440 configuration settings for this app so 8233 05:08:47,440 --> 05:08:49,680 it would be better to call this config 8234 05:08:49,680 --> 05:08:51,600 but that aside let's move on 8235 05:08:51,600 --> 05:08:53,760 next we have models and this is where we 8236 05:08:53,760 --> 05:08:56,480 define classes or new types for modeling 8237 05:08:56,480 --> 05:08:59,520 the concepts in this app in this domain 8238 05:08:59,520 --> 05:09:01,760 for example in the domain of products 8239 05:09:01,760 --> 05:09:04,480 you probably have concepts like product 8240 05:09:04,480 --> 05:09:07,200 category review and so on so we could 8241 05:09:07,200 --> 05:09:09,520 model these concepts using classes again 8242 05:09:09,520 --> 05:09:10,718 we're going to come back to this and 8243 05:09:10,718 --> 05:09:12,718 define a new class here 8244 05:09:12,718 --> 05:09:15,440 next we have tests.pi and this is where 8245 05:09:15,440 --> 05:09:17,840 we write automated tests for this app 8246 05:09:17,840 --> 05:09:19,440 that's an advanced topic beyond the 8247 05:09:19,440 --> 05:09:21,120 scope of this course so don't worry 8248 05:09:21,120 --> 05:09:23,120 about it and finally we have the views 8249 05:09:23,120 --> 05:09:25,520 module and this is where we define what 8250 05:09:25,520 --> 05:09:27,280 should the user see when they navigate 8251 05:09:27,280 --> 05:09:30,240 to a certain page 8252 05:09:33,760 --> 05:09:34,958 in this tutorial i'm going to show you 8253 05:09:34,958 --> 05:09:36,958 how to create your first view or view 8254 05:09:36,958 --> 05:09:39,200 function in django what is the view 8255 05:09:39,200 --> 05:09:41,360 function a view function is basically a 8256 05:09:41,360 --> 05:09:43,600 function that gets called by django when 8257 05:09:43,600 --> 05:09:46,240 the user navigates to a particular page 8258 05:09:46,240 --> 05:09:48,638 for example here we are on the home page 8259 05:09:48,638 --> 05:09:50,080 of our website 8260 05:09:50,080 --> 05:09:53,600 when we go to slash products our browser 8261 05:09:53,600 --> 05:09:56,798 sends an http request to our web server 8262 05:09:56,798 --> 05:09:59,120 at this point django takes that request 8263 05:09:59,120 --> 05:10:01,920 it inspects the url or the address it 8264 05:10:01,920 --> 05:10:03,360 figures out that we want to see the list 8265 05:10:03,360 --> 05:10:05,360 of products then it will call a function 8266 05:10:05,360 --> 05:10:07,600 which we call a view function and the 8267 05:10:07,600 --> 05:10:09,200 job of this function is to return the 8268 05:10:09,200 --> 05:10:12,000 response to the browser or to the client 8269 05:10:12,000 --> 05:10:13,680 so this function should generate some 8270 05:10:13,680 --> 05:10:16,638 html markup to be returned to the client 8271 05:10:16,638 --> 05:10:18,878 our browser will get this html content 8272 05:10:18,878 --> 05:10:21,680 and display it as a page right here 8273 05:10:21,680 --> 05:10:23,200 now technically if you want to be a web 8274 05:10:23,200 --> 05:10:26,320 developer you need to know html css and 8275 05:10:26,320 --> 05:10:28,480 javascript i'll try to do my best to 8276 05:10:28,480 --> 05:10:30,080 hold your hand throughout this course 8277 05:10:30,080 --> 05:10:31,840 but in the future be sure to watch a 8278 05:10:31,840 --> 05:10:35,200 tutorial about html css and javascript 8279 05:10:35,200 --> 05:10:36,718 on my channel i have plenty of 8280 05:10:36,718 --> 05:10:38,718 javascript tutorials i recommend you to 8281 05:10:38,718 --> 05:10:40,718 start with my javascript tutorial for 8282 05:10:40,718 --> 05:10:42,400 beginners 8283 05:10:42,400 --> 05:10:44,160 so back to our project 8284 05:10:44,160 --> 05:10:46,798 inside the products app let's open up 8285 05:10:46,798 --> 05:10:50,360 the views module 8286 05:10:50,400 --> 05:10:51,840 so here on the top we have some 8287 05:10:51,840 --> 05:10:54,958 boilerplate code from django.shortcuts 8288 05:10:54,958 --> 05:10:57,760 we are importing the render function so 8289 05:10:57,760 --> 05:11:00,000 django is a package shortcuts is a 8290 05:11:00,000 --> 05:11:02,000 module and we are importing the render 8291 05:11:02,000 --> 05:11:04,160 function you will see this soon below 8292 05:11:04,160 --> 05:11:05,760 that we have this comment we don't need 8293 05:11:05,760 --> 05:11:08,638 it let's create our first view function 8294 05:11:08,638 --> 05:11:11,440 so define we call this function index it 8295 05:11:11,440 --> 05:11:13,200 doesn't really matter but by convention 8296 05:11:13,200 --> 05:11:15,920 we use the word index for the main page 8297 05:11:15,920 --> 05:11:17,280 of an app 8298 05:11:17,280 --> 05:11:19,200 so when we go to slash products this 8299 05:11:19,200 --> 05:11:21,120 will be the main page of our products 8300 05:11:21,120 --> 05:11:23,040 app now potentially in the future we 8301 05:11:23,040 --> 05:11:25,520 could support additional urls inside of 8302 05:11:25,520 --> 05:11:28,480 this app for example products slash new 8303 05:11:28,480 --> 05:11:31,040 or training and so on 8304 05:11:31,040 --> 05:11:33,040 so we call this function index 8305 05:11:33,040 --> 05:11:35,280 now all our view functions should always 8306 05:11:35,280 --> 05:11:37,840 take a parameter and 8307 05:11:37,840 --> 05:11:40,638 that is the http request that is passed 8308 05:11:40,638 --> 05:11:42,400 to this view function 8309 05:11:42,400 --> 05:11:44,480 so as i told you before when we navigate 8310 05:11:44,480 --> 05:11:47,040 to slash products our browser sends an 8311 05:11:47,040 --> 05:11:49,840 http request to our web server django 8312 05:11:49,840 --> 05:11:51,680 takes this request and sends it to our 8313 05:11:51,680 --> 05:11:53,120 view function 8314 05:11:53,120 --> 05:11:54,798 with this request object we can find 8315 05:11:54,798 --> 05:11:56,878 information about the client 8316 05:11:56,878 --> 05:11:58,160 now in this tutorial we don't want to 8317 05:11:58,160 --> 05:12:00,320 worry about html we just want to return 8318 05:12:00,320 --> 05:12:02,160 some plain text to the browser like 8319 05:12:02,160 --> 05:12:04,958 hello world so on the top 8320 05:12:04,958 --> 05:12:08,360 from django.httpmodule 8321 05:12:08,638 --> 05:12:12,000 we need to import a class called http 8322 05:12:12,000 --> 05:12:13,440 response 8323 05:12:13,440 --> 05:12:15,520 with this class we can create an http 8324 05:12:15,520 --> 05:12:17,680 response to return to the client or the 8325 05:12:17,680 --> 05:12:20,400 browser so back to our view function 8326 05:12:20,400 --> 05:12:23,840 here we return an http response object 8327 05:12:23,840 --> 05:12:25,600 so we're creating an instance of this 8328 05:12:25,600 --> 05:12:26,958 class right 8329 05:12:26,958 --> 05:12:29,280 now as an argument to the constructor we 8330 05:12:29,280 --> 05:12:31,920 pass hello world 8331 05:12:31,920 --> 05:12:34,638 now we need to add an extra blank line 8332 05:12:34,638 --> 05:12:36,958 above the index function to make python 8333 05:12:36,958 --> 05:12:38,560 happy beautiful 8334 05:12:38,560 --> 05:12:42,200 as simple as that 8335 05:12:45,520 --> 05:12:47,200 so we are done with this view function 8336 05:12:47,200 --> 05:12:49,280 but nowhere django knows that it should 8337 05:12:49,280 --> 05:12:50,798 call this function when we send a 8338 05:12:50,798 --> 05:12:53,440 request to slash products 8339 05:12:53,440 --> 05:12:55,120 so we need to tell django that hey 8340 05:12:55,120 --> 05:12:57,760 whenever there is a request to slash 8341 05:12:57,760 --> 05:13:00,958 products call this index function we 8342 05:13:00,958 --> 05:13:04,560 need to map this url to this function 8343 05:13:04,560 --> 05:13:07,840 a url is short for uniform 8344 05:13:07,840 --> 05:13:10,320 resource locator and that basically 8345 05:13:10,320 --> 05:13:12,000 means address 8346 05:13:12,000 --> 05:13:12,878 okay 8347 05:13:12,878 --> 05:13:15,520 so let's do the mapping we're done with 8348 05:13:15,520 --> 05:13:17,680 this module close it 8349 05:13:17,680 --> 05:13:19,760 back to our products app right click 8350 05:13:19,760 --> 05:13:23,680 this folder and add a new python file 8351 05:13:23,680 --> 05:13:25,680 call that urls the name is very 8352 05:13:25,680 --> 05:13:27,040 important here this is one of the 8353 05:13:27,040 --> 05:13:28,958 conventions that django framework 8354 05:13:28,958 --> 05:13:30,638 defines so we need to follow this 8355 05:13:30,638 --> 05:13:33,600 convention to structure our project 8356 05:13:33,600 --> 05:13:36,638 okay so this adds urls.pi inside of the 8357 05:13:36,638 --> 05:13:39,600 products app now in this module once 8358 05:13:39,600 --> 05:13:41,360 again by convention we should define a 8359 05:13:41,360 --> 05:13:44,958 variable called url patterns 8360 05:13:44,958 --> 05:13:47,120 make sure to spell it exactly as you see 8361 05:13:47,120 --> 05:13:49,280 here so there is no underscore between 8362 05:13:49,280 --> 05:13:51,280 these two words and all characters are 8363 05:13:51,280 --> 05:13:52,560 in lower case 8364 05:13:52,560 --> 05:13:55,120 so we set this to a list object 8365 05:13:55,120 --> 05:13:57,840 in this list we map various urls to 8366 05:13:57,840 --> 05:13:59,360 their view functions 8367 05:13:59,360 --> 05:14:01,440 now to reference a url we need to import 8368 05:14:01,440 --> 05:14:02,878 the path function 8369 05:14:02,878 --> 05:14:04,718 so on the top 8370 05:14:04,718 --> 05:14:06,280 from 8371 05:14:06,280 --> 05:14:09,600 django.urls let's import the path 8372 05:14:09,600 --> 05:14:11,280 function 8373 05:14:11,280 --> 05:14:13,760 with this function we can map a url to a 8374 05:14:13,760 --> 05:14:15,600 view function so we call the path 8375 05:14:15,600 --> 05:14:16,718 function 8376 05:14:16,718 --> 05:14:18,560 the first argument is a string that 8377 05:14:18,560 --> 05:14:21,360 specifies our url endpoint 8378 05:14:21,360 --> 05:14:23,440 here we pass an empty string that is a 8379 05:14:23,440 --> 05:14:25,600 string with no characters in it and this 8380 05:14:25,600 --> 05:14:27,760 represents the root of this app let me 8381 05:14:27,760 --> 05:14:29,680 show you what i mean so 8382 05:14:29,680 --> 05:14:31,520 imagine our app starts 8383 05:14:31,520 --> 05:14:33,680 at slash products 8384 05:14:33,680 --> 05:14:35,680 so if the user sends a request to this 8385 05:14:35,680 --> 05:14:37,280 endpoint they will be at the root of 8386 05:14:37,280 --> 05:14:39,360 this app potentially here we could have 8387 05:14:39,360 --> 05:14:42,480 other urls like slash products slash one 8388 05:14:42,480 --> 05:14:46,480 slash detail or slash products slash new 8389 05:14:46,480 --> 05:14:49,840 or trending or whatever so all the urls 8390 05:14:49,840 --> 05:14:53,440 in this app start with slash products 8391 05:14:53,440 --> 05:14:56,240 okay now back to the first example 8392 05:14:56,240 --> 05:14:59,200 what happens if we take away this slash 8393 05:14:59,200 --> 05:15:01,280 products we are left with nothing and 8394 05:15:01,280 --> 05:15:04,160 that is why we use an empty string here 8395 05:15:04,160 --> 05:15:05,840 now as the second argument to this 8396 05:15:05,840 --> 05:15:07,840 function we need to specify our view 8397 05:15:07,840 --> 05:15:08,718 function 8398 05:15:08,718 --> 05:15:11,680 so we defined that view function inside 8399 05:15:11,680 --> 05:15:13,200 of the views module 8400 05:15:13,200 --> 05:15:16,000 so first we need to import this module 8401 05:15:16,000 --> 05:15:18,560 into the urls module 8402 05:15:18,560 --> 05:15:19,440 now 8403 05:15:19,440 --> 05:15:21,840 this is how we do this from 8404 05:15:21,840 --> 05:15:24,718 period import views 8405 05:15:24,718 --> 05:15:26,878 period means the current folder so from 8406 05:15:26,878 --> 05:15:28,958 the current folder we are importing the 8407 05:15:28,958 --> 05:15:30,560 views module 8408 05:15:30,560 --> 05:15:32,638 so with this syntax we're telling django 8409 05:15:32,638 --> 05:15:34,718 to import the views module that we have 8410 05:15:34,718 --> 05:15:37,440 in this folder now you might ask wash 8411 05:15:37,440 --> 05:15:39,120 why didn't we import the views module 8412 05:15:39,120 --> 05:15:42,000 like this import views because views is 8413 05:15:42,000 --> 05:15:43,760 actually in the current folder so why do 8414 05:15:43,760 --> 05:15:46,560 we have to use the from import syntax 8415 05:15:46,560 --> 05:15:48,878 the reason for this is because views is 8416 05:15:48,878 --> 05:15:51,120 kind of a generic name so it is possible 8417 05:15:51,120 --> 05:15:53,360 that our project has a dependency to 8418 05:15:53,360 --> 05:15:55,600 another library called views 8419 05:15:55,600 --> 05:15:57,760 with this syntax python will end up 8420 05:15:57,760 --> 05:15:59,760 importing that module instead of the 8421 05:15:59,760 --> 05:16:01,840 views module in the current folder 8422 05:16:01,840 --> 05:16:04,240 because that will take priority 8423 05:16:04,240 --> 05:16:05,520 so 8424 05:16:05,520 --> 05:16:07,680 to remove these potential issues we use 8425 05:16:07,680 --> 05:16:10,080 the from import syntax 8426 05:16:10,080 --> 05:16:12,400 so now we imported the views module as 8427 05:16:12,400 --> 05:16:14,240 you know when we import a module that 8428 05:16:14,240 --> 05:16:16,878 module will end up being an object so we 8429 05:16:16,878 --> 05:16:18,958 can use the dot operator to access the 8430 05:16:18,958 --> 05:16:21,120 functions defined in that module so here 8431 05:16:21,120 --> 05:16:23,440 we want to reference the index function 8432 05:16:23,440 --> 05:16:25,840 okay let's delete it from here back to 8433 05:16:25,840 --> 05:16:27,680 our path function 8434 05:16:27,680 --> 05:16:29,840 as the second argument we type out 8435 05:16:29,840 --> 05:16:31,680 views.index 8436 05:16:31,680 --> 05:16:33,280 now note that i'm not calling this 8437 05:16:33,280 --> 05:16:35,360 function i'm simply passing a reference 8438 05:16:35,360 --> 05:16:37,600 to it django will take care of calling 8439 05:16:37,600 --> 05:16:39,760 this function at runtime when the client 8440 05:16:39,760 --> 05:16:42,480 sends an http request to the server so 8441 05:16:42,480 --> 05:16:44,638 here we should not call this function 8442 05:16:44,638 --> 05:16:46,400 so let's quickly recap what we have done 8443 05:16:46,400 --> 05:16:48,638 up to this point 8444 05:16:48,638 --> 05:16:50,638 in the views module 8445 05:16:50,638 --> 05:16:52,798 we created this view function and in 8446 05:16:52,798 --> 05:16:54,878 this view function we simply return some 8447 05:16:54,878 --> 05:16:57,360 plain text to the browser or the client 8448 05:16:57,360 --> 05:16:59,120 now i told you that nowhere django knows 8449 05:16:59,120 --> 05:17:00,878 that it should call this function so 8450 05:17:00,878 --> 05:17:03,040 that's why we added the urls module to 8451 05:17:03,040 --> 05:17:04,320 our app 8452 05:17:04,320 --> 05:17:07,120 and in this module we mapped the root 8453 05:17:07,120 --> 05:17:09,680 url to this view function 8454 05:17:09,680 --> 05:17:11,760 there's one more step left we need to 8455 05:17:11,760 --> 05:17:14,400 tell django about this app because up to 8456 05:17:14,400 --> 05:17:16,480 this point django is not aware of this 8457 05:17:16,480 --> 05:17:17,440 app 8458 05:17:17,440 --> 05:17:18,240 so 8459 05:17:18,240 --> 05:17:20,560 let's collapse the products folder and 8460 05:17:20,560 --> 05:17:23,040 open up the pie shop folder 8461 05:17:23,040 --> 05:17:25,360 in this folder we have another urls 8462 05:17:25,360 --> 05:17:27,600 module and this is like the root or the 8463 05:17:27,600 --> 05:17:30,798 parent urls module in our django project 8464 05:17:30,798 --> 05:17:33,440 let's open this 8465 05:17:33,440 --> 05:17:34,840 and scroll 8466 05:17:34,840 --> 05:17:37,840 down once again you see this url 8467 05:17:37,840 --> 05:17:40,080 patterns variable that is set to a list 8468 05:17:40,080 --> 05:17:42,080 in this list again we're calling the 8469 05:17:42,080 --> 05:17:43,440 path function 8470 05:17:43,440 --> 05:17:45,280 as the first argument we're telling 8471 05:17:45,280 --> 05:17:48,080 django that hey any urls that start with 8472 05:17:48,080 --> 05:17:51,520 admin delegate them to the admin app so 8473 05:17:51,520 --> 05:17:53,440 every django project by default comes 8474 05:17:53,440 --> 05:17:55,200 with this admin app which gives us a 8475 05:17:55,200 --> 05:17:57,360 beautiful administrative panel we're 8476 05:17:57,360 --> 05:17:59,040 going to work with that soon 8477 05:17:59,040 --> 05:18:01,200 so in this module we should also tell 8478 05:18:01,200 --> 05:18:04,160 django about our products app so let's 8479 05:18:04,160 --> 05:18:06,400 add another object to this list once 8480 05:18:06,400 --> 05:18:08,480 again we call the path function 8481 05:18:08,480 --> 05:18:10,958 now we tell django that hey any paths or 8482 05:18:10,958 --> 05:18:14,638 any urls that start with products slash 8483 05:18:14,638 --> 05:18:17,440 delegate them to the products app 8484 05:18:17,440 --> 05:18:19,680 so we want django to delegate the 8485 05:18:19,680 --> 05:18:22,240 handling of that request 8486 05:18:22,240 --> 05:18:24,560 to this urls module that we defined in 8487 05:18:24,560 --> 05:18:25,920 the products app 8488 05:18:25,920 --> 05:18:28,240 okay how do we do that very easy 8489 05:18:28,240 --> 05:18:30,400 back to the main or the parent urls 8490 05:18:30,400 --> 05:18:32,638 module on line 16 we have this import 8491 05:18:32,638 --> 05:18:34,958 statement let's expand it 8492 05:18:34,958 --> 05:18:36,560 so basically we have two import 8493 05:18:36,560 --> 05:18:39,320 statements look at the second one from 8494 05:18:39,320 --> 05:18:41,520 django.urls module we're importing the 8495 05:18:41,520 --> 05:18:44,080 pass function there is another function 8496 05:18:44,080 --> 05:18:46,400 that we need to import and that is the 8497 05:18:46,400 --> 05:18:48,400 include function 8498 05:18:48,400 --> 05:18:50,240 now we're going to use this function on 8499 05:18:50,240 --> 05:18:52,718 line 21 so as the second argument to the 8500 05:18:52,718 --> 05:18:55,120 path function we call include 8501 05:18:55,120 --> 05:18:57,120 and here we need to reference the urls 8502 05:18:57,120 --> 05:19:00,878 module in the product app so we type out 8503 05:19:00,878 --> 05:19:04,080 products dot urls 8504 05:19:04,080 --> 05:19:05,440 so take a look 8505 05:19:05,440 --> 05:19:07,200 in the products app 8506 05:19:07,200 --> 05:19:08,638 we have this module 8507 05:19:08,638 --> 05:19:11,240 urls that is why we type 8508 05:19:11,240 --> 05:19:13,680 products.urls but we don't type out dot 8509 05:19:13,680 --> 05:19:15,520 pi django will automatically take care 8510 05:19:15,520 --> 05:19:16,560 of that 8511 05:19:16,560 --> 05:19:18,160 now at this point you might be a little 8512 05:19:18,160 --> 05:19:21,600 bit confused or intimidated by all these 8513 05:19:21,600 --> 05:19:23,600 various steps that you have to follow 8514 05:19:23,600 --> 05:19:25,360 they may look foreign to you but trust 8515 05:19:25,360 --> 05:19:27,600 me this becomes second nature as you 8516 05:19:27,600 --> 05:19:29,520 practice building django applications 8517 05:19:29,520 --> 05:19:31,360 it's actually very easy it's just the 8518 05:19:31,360 --> 05:19:32,878 first time that you have to follow these 8519 05:19:32,878 --> 05:19:35,920 conventions defined in django framework 8520 05:19:35,920 --> 05:19:37,680 once you understand the concept it 8521 05:19:37,680 --> 05:19:39,360 becomes really easy 8522 05:19:39,360 --> 05:19:41,200 so basically because we created a new 8523 05:19:41,200 --> 05:19:44,560 app the products app only once ever we 8524 05:19:44,560 --> 05:19:46,798 need to write this line of code to tell 8525 05:19:46,798 --> 05:19:49,360 django that hey any urls that start with 8526 05:19:49,360 --> 05:19:50,878 products 8527 05:19:50,878 --> 05:19:52,878 send them to the urls module in the 8528 05:19:52,878 --> 05:19:54,080 products app 8529 05:19:54,080 --> 05:19:56,480 so we write this line of code only once 8530 05:19:56,480 --> 05:19:58,400 anytime in the future we want to define 8531 05:19:58,400 --> 05:20:00,480 a new view function first we go to the 8532 05:20:00,480 --> 05:20:02,718 views module we create a new function 8533 05:20:02,718 --> 05:20:05,280 and then we go to the urls module in 8534 05:20:05,280 --> 05:20:06,560 that app 8535 05:20:06,560 --> 05:20:09,040 and define a new url pattern to map that 8536 05:20:09,040 --> 05:20:11,840 view function to a url endpoint that is 8537 05:20:11,840 --> 05:20:13,600 really easy 8538 05:20:13,600 --> 05:20:15,440 all right now let's test our application 8539 05:20:15,440 --> 05:20:17,040 so back to the browser 8540 05:20:17,040 --> 05:20:20,718 let's head over to slash products 8541 05:20:20,718 --> 05:20:21,920 there you go you see our hello world 8542 05:20:21,920 --> 05:20:23,920 message here beautiful but if we 8543 05:20:23,920 --> 05:20:26,160 navigate back to the home page 8544 05:20:26,160 --> 05:20:28,878 we get this error page not found so our 8545 05:20:28,878 --> 05:20:31,040 beautiful django homepage disappeared 8546 05:20:31,040 --> 05:20:32,558 and the reason for this is that that 8547 05:20:32,558 --> 05:20:34,958 home page is automatically generated and 8548 05:20:34,958 --> 05:20:37,360 is only displayed when we first create a 8549 05:20:37,360 --> 05:20:39,040 django project 8550 05:20:39,040 --> 05:20:41,280 as soon as we define a new url pattern 8551 05:20:41,280 --> 05:20:43,040 and register it in our project that 8552 05:20:43,040 --> 05:20:45,120 homepage disappears but don't worry 8553 05:20:45,120 --> 05:20:46,638 we're going to come back and create a 8554 05:20:46,638 --> 05:20:48,480 new function for our home page later in 8555 05:20:48,480 --> 05:20:50,000 the course 8556 05:20:50,000 --> 05:20:51,840 now here is your exercise i want you to 8557 05:20:51,840 --> 05:20:54,320 modify this django project such that 8558 05:20:54,320 --> 05:20:56,400 when we navigate to products 8559 05:20:56,400 --> 05:20:58,958 slash new we'll see a page here with 8560 05:20:58,958 --> 05:21:02,240 some text like new products i strongly 8561 05:21:02,240 --> 05:21:03,920 encourage you to do this exercise 8562 05:21:03,920 --> 05:21:05,440 because it makes it really easy for you 8563 05:21:05,440 --> 05:21:09,040 to follow the future tutorials 8564 05:21:11,680 --> 05:21:13,440 alright here in the products app let's 8565 05:21:13,440 --> 05:21:15,760 open up the views module 8566 05:21:15,760 --> 05:21:17,680 in this module we define a new view 8567 05:21:17,680 --> 05:21:20,000 function so define let's call that 8568 05:21:20,000 --> 05:21:21,200 function new 8569 05:21:21,200 --> 05:21:23,200 as i told you before every view function 8570 05:21:23,200 --> 05:21:26,080 should take this request parameter 8571 05:21:26,080 --> 05:21:28,400 like here we want to return an http 8572 05:21:28,400 --> 05:21:29,600 response 8573 05:21:29,600 --> 05:21:32,958 with the text new products 8574 05:21:32,958 --> 05:21:35,520 that's it now to make pycharm happy we 8575 05:21:35,520 --> 05:21:36,400 add 8576 05:21:36,400 --> 05:21:38,558 another line break here so we have two 8577 05:21:38,558 --> 05:21:40,878 line breaks after the index function and 8578 05:21:40,878 --> 05:21:43,280 also two line breaks after the new 8579 05:21:43,280 --> 05:21:45,360 function these are the best practices 8580 05:21:45,360 --> 05:21:48,638 defined in pep 8 to format our code 8581 05:21:48,638 --> 05:21:51,120 so we're done with this view function 8582 05:21:51,120 --> 05:21:53,920 now we need to map it to a url endpoint 8583 05:21:53,920 --> 05:21:56,400 so we go back to the products app and 8584 05:21:56,400 --> 05:21:59,120 open up the urls module 8585 05:21:59,120 --> 05:22:01,760 here we need to add another url pattern 8586 05:22:01,760 --> 05:22:03,840 so we call the path function 8587 05:22:03,840 --> 05:22:06,000 the first argument should be new you 8588 05:22:06,000 --> 05:22:08,958 want to map this url endpoint to 8589 05:22:08,958 --> 05:22:12,558 views dot new function that's all you 8590 05:22:12,558 --> 05:22:14,878 have to do now back to our browser let 8591 05:22:14,878 --> 05:22:16,958 me navigate to products new we get this 8592 05:22:16,958 --> 05:22:20,638 message in the browser beautiful 8593 05:22:24,240 --> 05:22:25,520 in this tutorial i'm going to show you 8594 05:22:25,520 --> 05:22:27,360 how to create a model in a django 8595 05:22:27,360 --> 05:22:30,160 project a model is a representation of a 8596 05:22:30,160 --> 05:22:32,480 real world concept for example in an 8597 05:22:32,480 --> 05:22:34,480 online shop we could have models like 8598 05:22:34,480 --> 05:22:37,840 order customer shopping cart product 8599 05:22:37,840 --> 05:22:40,000 review and so on so let's go ahead and 8600 05:22:40,000 --> 05:22:41,680 create our first model 8601 05:22:41,680 --> 05:22:43,360 here in the products app 8602 05:22:43,360 --> 05:22:46,240 open up the models module 8603 05:22:46,240 --> 05:22:47,520 in this module we're going to create a 8604 05:22:47,520 --> 05:22:49,840 new class called product so let's delete 8605 05:22:49,840 --> 05:22:51,920 this comment we don't really need it 8606 05:22:51,920 --> 05:22:52,958 class 8607 05:22:52,958 --> 05:22:54,080 product 8608 05:22:54,080 --> 05:22:56,000 now we need to inherit this class from 8609 05:22:56,000 --> 05:22:58,558 the model class in django so in 8610 05:22:58,558 --> 05:23:02,958 parenthesis we type out models dot model 8611 05:23:02,958 --> 05:23:04,798 let's see what's going on here so on 8612 05:23:04,798 --> 05:23:08,480 line one from django.db package we are 8613 05:23:08,480 --> 05:23:09,600 importing 8614 05:23:09,600 --> 05:23:11,600 the models module 8615 05:23:11,600 --> 05:23:13,760 in this module we have a class called 8616 05:23:13,760 --> 05:23:16,080 model and this class defines all the 8617 05:23:16,080 --> 05:23:18,718 common characteristics and behavior for 8618 05:23:18,718 --> 05:23:20,878 models in a django application for 8619 05:23:20,878 --> 05:23:22,400 example we want to be able to store 8620 05:23:22,400 --> 05:23:24,638 these models in a database or read them 8621 05:23:24,638 --> 05:23:26,878 from a database or delete them these are 8622 05:23:26,878 --> 05:23:28,320 all the common operations that we need 8623 05:23:28,320 --> 05:23:30,718 to perform on these model objects 8624 05:23:30,718 --> 05:23:33,200 so by inheriting this product class from 8625 05:23:33,200 --> 05:23:35,440 this model class our product class will 8626 05:23:35,440 --> 05:23:37,760 have all these capabilities built into 8627 05:23:37,760 --> 05:23:41,760 it we don't have to code it okay so 8628 05:23:41,760 --> 05:23:43,360 colon now in this class we need to 8629 05:23:43,360 --> 05:23:45,440 define a few attributes what are the 8630 05:23:45,440 --> 05:23:46,958 attributes of a product that we need to 8631 05:23:46,958 --> 05:23:49,280 know in an online shop we need to know 8632 05:23:49,280 --> 05:23:50,400 their name 8633 05:23:50,400 --> 05:23:51,360 price 8634 05:23:51,360 --> 05:23:54,558 stock image and so on so let's define a 8635 05:23:54,558 --> 05:23:55,920 new attribute 8636 05:23:55,920 --> 05:23:58,080 we call it name and we set it to an 8637 05:23:58,080 --> 05:23:59,200 instance 8638 05:23:59,200 --> 05:24:02,638 of models the char field class 8639 05:24:02,638 --> 05:24:04,798 so once again in this module we have a 8640 05:24:04,798 --> 05:24:06,958 class called char field which is short 8641 05:24:06,958 --> 05:24:09,120 for character field and this represents 8642 05:24:09,120 --> 05:24:11,600 a field that can contain textual data 8643 05:24:11,600 --> 05:24:13,120 now here as a best practice we can 8644 05:24:13,120 --> 05:24:15,840 supply a maximum length to prevent a 8645 05:24:15,840 --> 05:24:17,840 malicious user or hacker from 8646 05:24:17,840 --> 05:24:20,638 accidentally storing a product with an 8647 05:24:20,638 --> 05:24:22,878 enormously large name this can mess up 8648 05:24:22,878 --> 05:24:24,320 with our application 8649 05:24:24,320 --> 05:24:26,798 so let's pass a keyword argument max 8650 05:24:26,798 --> 05:24:31,920 length and we set it to 255 characters 8651 05:24:31,920 --> 05:24:34,160 now let's define the price attribute so 8652 05:24:34,160 --> 05:24:37,520 we set price to models dot 8653 05:24:37,520 --> 05:24:40,160 float field so this represents a field 8654 05:24:40,160 --> 05:24:42,558 that can contain floating point numbers 8655 05:24:42,558 --> 05:24:44,080 that's it 8656 05:24:44,080 --> 05:24:46,480 now let's define the stock 8657 05:24:46,480 --> 05:24:49,360 we set this to an instance of 8658 05:24:49,360 --> 05:24:51,920 integer field 8659 05:24:51,920 --> 05:24:53,680 and finally let's define 8660 05:24:53,680 --> 05:24:56,558 image underline url 8661 05:24:56,558 --> 05:24:59,200 to an instance of models dot 8662 05:24:59,200 --> 05:25:01,920 one second char field 8663 05:25:01,920 --> 05:25:03,440 so in this course we're gonna use google 8664 05:25:03,440 --> 05:25:05,600 to find an image for our products for 8665 05:25:05,600 --> 05:25:07,600 example we can find this image 8666 05:25:07,600 --> 05:25:09,360 right click it 8667 05:25:09,360 --> 05:25:12,160 and copy its link address which is the 8668 05:25:12,160 --> 05:25:13,280 url 8669 05:25:13,280 --> 05:25:15,120 now if you open up a new tab and paste 8670 05:25:15,120 --> 05:25:18,080 that url you can see this is a very long 8671 05:25:18,080 --> 05:25:19,040 string 8672 05:25:19,040 --> 05:25:20,958 so we don't want to enforce a maximum 8673 05:25:20,958 --> 05:25:23,680 length of 255 characters 8674 05:25:23,680 --> 05:25:25,840 instead we want to use 8675 05:25:25,840 --> 05:25:27,840 2083 8676 05:25:27,840 --> 05:25:30,240 characters that is the standard maximum 8677 05:25:30,240 --> 05:25:33,680 length for urls so our product class or 8678 05:25:33,680 --> 05:25:36,558 product model has these four attributes 8679 05:25:36,558 --> 05:25:38,320 now to make pycharm happy and remove 8680 05:25:38,320 --> 05:25:40,160 this warning let's put the cursor at the 8681 05:25:40,160 --> 05:25:42,400 end of this line press enter then 8682 05:25:42,400 --> 05:25:44,798 backspace to remove the indentation 8683 05:25:44,798 --> 05:25:47,200 beautiful we should also add 8684 05:25:47,200 --> 05:25:49,280 two line breaks before this class so 8685 05:25:49,280 --> 05:25:52,080 here's one press enter the second line 8686 05:25:52,080 --> 05:25:54,320 break beautiful so our code is formatted 8687 05:25:54,320 --> 05:25:57,040 according to pep8 standards next i'll 8688 05:25:57,040 --> 05:25:59,120 show you how to use this model to create 8689 05:25:59,120 --> 05:26:03,360 a database table for storing products 8690 05:26:07,120 --> 05:26:09,120 in this django project we have created 8691 05:26:09,120 --> 05:26:11,480 you can see we have this file 8692 05:26:11,480 --> 05:26:14,000 db.sqlite3 this is an empty database 8693 05:26:14,000 --> 05:26:16,160 file that django creates and we use this 8694 05:26:16,160 --> 05:26:18,878 to store our data now sqlite is a kind 8695 05:26:18,878 --> 05:26:20,958 of database and it's the same place for 8696 05:26:20,958 --> 05:26:22,878 small applications but it's not suitable 8697 05:26:22,878 --> 05:26:25,520 for large enterprise web applications so 8698 05:26:25,520 --> 05:26:27,360 if you want to build a real enterprise 8699 05:26:27,360 --> 05:26:29,360 web application with django you need to 8700 05:26:29,360 --> 05:26:31,840 use a proper database engine like sql 8701 05:26:31,840 --> 05:26:35,520 server mysql oracle postgres and so on 8702 05:26:35,520 --> 05:26:37,200 the good news is that django supports 8703 05:26:37,200 --> 05:26:39,760 all of these database engines but sqlite 8704 05:26:39,760 --> 05:26:41,600 is the easiest to get started so we're 8705 05:26:41,600 --> 05:26:43,040 going to work with this for now and in 8706 05:26:43,040 --> 05:26:44,878 the future i will show you how to use a 8707 05:26:44,878 --> 05:26:46,878 proper database engine 8708 05:26:46,878 --> 05:26:48,958 now for the purpose of this tutorial i 8709 05:26:48,958 --> 05:26:50,558 want you to open up your browser and 8710 05:26:50,558 --> 05:26:54,240 search for db browser for sqlite 8711 05:26:54,240 --> 05:26:58,000 so on this page sqlitebrowser.org 8712 05:26:58,000 --> 05:27:00,000 you can find a very simple and free 8713 05:27:00,000 --> 05:27:03,520 application for opening sqlite databases 8714 05:27:03,520 --> 05:27:04,718 and as you can see here on the right 8715 05:27:04,718 --> 05:27:07,280 side there are executables for windows 8716 05:27:07,280 --> 05:27:08,958 mac and so on 8717 05:27:08,958 --> 05:27:10,400 so when you open it you get something 8718 05:27:10,400 --> 05:27:12,480 like this we can create a new database 8719 05:27:12,480 --> 05:27:15,120 here on the top or open an existing one 8720 05:27:15,120 --> 05:27:16,958 now here i'm going to drag and drop our 8721 05:27:16,958 --> 05:27:19,280 database file right here 8722 05:27:19,280 --> 05:27:20,798 you can see we don't have any tables in 8723 05:27:20,798 --> 05:27:23,360 this database so to store data first we 8724 05:27:23,360 --> 05:27:25,280 need to create a table for example to 8725 05:27:25,280 --> 05:27:27,120 store the list of products we need to 8726 05:27:27,120 --> 05:27:29,120 create a table called product and the 8727 05:27:29,120 --> 05:27:31,360 fields or columns we have in this table 8728 05:27:31,360 --> 05:27:33,120 should be exactly the same as the 8729 05:27:33,120 --> 05:27:34,718 attributes we have 8730 05:27:34,718 --> 05:27:36,480 in our product class 8731 05:27:36,480 --> 05:27:37,760 so in this tutorial i'm going to show 8732 05:27:37,760 --> 05:27:39,920 you how to use django to automatically 8733 05:27:39,920 --> 05:27:41,680 create this table we don't have to do 8734 05:27:41,680 --> 05:27:44,000 this by hand we can just tell django hey 8735 05:27:44,000 --> 05:27:45,760 here's our model go ahead and 8736 05:27:45,760 --> 05:27:47,680 automatically create a database table 8737 05:27:47,680 --> 05:27:49,600 for storing our products 8738 05:27:49,600 --> 05:27:51,120 that is very easy 8739 05:27:51,120 --> 05:27:53,600 so here down the bottom let's open up a 8740 05:27:53,600 --> 05:27:56,718 new terminal window and run python on 8741 05:27:56,718 --> 05:27:59,360 windows or python 3 on mac 8742 05:27:59,360 --> 05:28:01,280 manage.pi 8743 05:28:01,280 --> 05:28:03,680 make migrations 8744 05:28:03,680 --> 05:28:06,000 make sure to spell this properly so make 8745 05:28:06,000 --> 05:28:08,400 migrations in one word 8746 05:28:08,400 --> 05:28:09,520 now when you run this you get this 8747 05:28:09,520 --> 05:28:12,000 message no changes detected because by 8748 05:28:12,000 --> 05:28:14,240 default django doesn't know about our 8749 05:28:14,240 --> 05:28:15,680 product model 8750 05:28:15,680 --> 05:28:17,360 to solve this problem 8751 05:28:17,360 --> 05:28:19,840 we need to go back to our project 8752 05:28:19,840 --> 05:28:23,200 and open the pie shop package 8753 05:28:23,200 --> 05:28:25,120 in this package we have this settings 8754 05:28:25,120 --> 05:28:27,120 module for storing various configuration 8755 05:28:27,120 --> 05:28:28,718 settings for our project 8756 05:28:28,718 --> 05:28:30,718 so let's open this 8757 05:28:30,718 --> 05:28:33,280 and scroll down 8758 05:28:33,280 --> 05:28:36,480 on line 33 you can see a variable called 8759 05:28:36,480 --> 05:28:40,320 installed apps set to a list of strings 8760 05:28:40,320 --> 05:28:42,320 these are applications or apps that are 8761 05:28:42,320 --> 05:28:44,080 automatically installed with every 8762 05:28:44,080 --> 05:28:46,400 django project for example we have this 8763 05:28:46,400 --> 05:28:48,798 admin app which gives us a beautiful 8764 05:28:48,798 --> 05:28:50,718 administrative panel we're going to work 8765 05:28:50,718 --> 05:28:53,760 with it soon you also have this auth app 8766 05:28:53,760 --> 05:28:56,080 and we use this to authenticate users we 8767 05:28:56,080 --> 05:28:57,680 can put them in different roles give 8768 05:28:57,680 --> 05:28:59,440 them different permissions it's all 8769 05:28:59,440 --> 05:29:02,400 about authentication and authorization 8770 05:29:02,400 --> 05:29:03,840 we'll look at the other installed apps 8771 05:29:03,840 --> 05:29:06,320 in a future tutorial so for now let's 8772 05:29:06,320 --> 05:29:08,878 register our products app in this list 8773 05:29:08,878 --> 05:29:10,480 how do we do that 8774 05:29:10,480 --> 05:29:12,400 first we go to our project 8775 05:29:12,400 --> 05:29:14,878 inside the products app earlier i told 8776 05:29:14,878 --> 05:29:17,520 you about the apps module take a look 8777 05:29:17,520 --> 05:29:19,680 here 8778 05:29:19,680 --> 05:29:21,520 the name of this module is a little bit 8779 05:29:21,520 --> 05:29:23,440 misleading because we're in the boundary 8780 05:29:23,440 --> 05:29:25,360 of one app and we have a module called 8781 05:29:25,360 --> 05:29:26,480 apps 8782 05:29:26,480 --> 05:29:29,040 that aside all we have here is a class 8783 05:29:29,040 --> 05:29:31,840 called products config where we store 8784 05:29:31,840 --> 05:29:33,600 configuration settings for this 8785 05:29:33,600 --> 05:29:34,878 particular app 8786 05:29:34,878 --> 05:29:35,760 okay 8787 05:29:35,760 --> 05:29:37,200 now we need to go back to our settings 8788 05:29:37,200 --> 05:29:39,920 module and add the complete path to this 8789 05:29:39,920 --> 05:29:43,200 class including its package and module 8790 05:29:43,200 --> 05:29:45,600 so this class is defined in the apps 8791 05:29:45,600 --> 05:29:48,798 module and this module is inside of the 8792 05:29:48,798 --> 05:29:50,558 products package 8793 05:29:50,558 --> 05:29:53,680 so back to the settings file 8794 05:29:53,680 --> 05:29:55,760 in the list of installed apps we need to 8795 05:29:55,760 --> 05:29:57,760 type out a string like this 8796 05:29:57,760 --> 05:29:59,920 products that's our package 8797 05:29:59,920 --> 05:30:00,878 dot 8798 05:30:00,878 --> 05:30:02,798 apps that's our module 8799 05:30:02,798 --> 05:30:04,638 and then products 8800 05:30:04,638 --> 05:30:05,680 config 8801 05:30:05,680 --> 05:30:07,840 again make sure to spell this properly 8802 05:30:07,840 --> 05:30:08,638 so 8803 05:30:08,638 --> 05:30:11,120 p and c are capitalized because here we 8804 05:30:11,120 --> 05:30:14,080 have a class but products and apps are 8805 05:30:14,080 --> 05:30:16,320 all in lower case okay 8806 05:30:16,320 --> 05:30:18,798 so now django knows about our product 8807 05:30:18,798 --> 05:30:21,280 app so when we go back to the terminal 8808 05:30:21,280 --> 05:30:22,958 and run the last command we'll get a 8809 05:30:22,958 --> 05:30:24,320 different result 8810 05:30:24,320 --> 05:30:27,200 so use the up arrow to bring up the last 8811 05:30:27,200 --> 05:30:29,040 command and enter 8812 05:30:29,040 --> 05:30:30,320 there you go 8813 05:30:30,320 --> 05:30:32,320 now we get this message migrations for 8814 05:30:32,320 --> 05:30:35,280 products so inside the products folder 8815 05:30:35,280 --> 05:30:37,520 inside the migrations folder we have a 8816 05:30:37,520 --> 05:30:39,600 new module with this sequence number 8817 05:30:39,600 --> 05:30:41,360 called initial 8818 05:30:41,360 --> 05:30:42,878 let's have a quick look at the code 8819 05:30:42,878 --> 05:30:44,798 inside this module so back to our 8820 05:30:44,798 --> 05:30:47,360 products app let's expand the migrations 8821 05:30:47,360 --> 05:30:48,320 folder 8822 05:30:48,320 --> 05:30:50,480 here's our initial migration let's have 8823 05:30:50,480 --> 05:30:52,000 a quick look here 8824 05:30:52,000 --> 05:30:54,718 so we have a class called migration and 8825 05:30:54,718 --> 05:30:56,160 in this class 8826 05:30:56,160 --> 05:30:58,558 we have this attribute called operations 8827 05:30:58,558 --> 05:31:01,280 which is set to a list of operations so 8828 05:31:01,280 --> 05:31:02,958 every migration can contain multiple 8829 05:31:02,958 --> 05:31:05,040 operations look at the first operation 8830 05:31:05,040 --> 05:31:07,840 we have here migrations that create 8831 05:31:07,840 --> 05:31:09,040 model 8832 05:31:09,040 --> 05:31:10,958 so with this operation we can create a 8833 05:31:10,958 --> 05:31:14,400 new database table called product and in 8834 05:31:14,400 --> 05:31:16,080 that table we're going to have these 8835 05:31:16,080 --> 05:31:21,200 fields id name price stock and image url 8836 05:31:21,200 --> 05:31:22,718 these are the attributes that we created 8837 05:31:22,718 --> 05:31:24,958 in our product class but we didn't have 8838 05:31:24,958 --> 05:31:27,440 this id attribute django automatically 8839 05:31:27,440 --> 05:31:29,680 generates this because every database 8840 05:31:29,680 --> 05:31:32,638 table must have an id field and we use 8841 05:31:32,638 --> 05:31:34,958 this to uniquely identify records in our 8842 05:31:34,958 --> 05:31:37,120 database now let's go back to the 8843 05:31:37,120 --> 05:31:39,040 terminal 8844 05:31:39,040 --> 05:31:41,040 so we have a new migration but our 8845 05:31:41,040 --> 05:31:43,600 database is still in its previous state 8846 05:31:43,600 --> 05:31:45,840 we don't have a new table here yet 8847 05:31:45,840 --> 05:31:47,760 so we need to run this new migration to 8848 05:31:47,760 --> 05:31:50,320 generate our product table and that is 8849 05:31:50,320 --> 05:31:51,360 very easy 8850 05:31:51,360 --> 05:31:53,600 so one more time python 8851 05:31:53,600 --> 05:31:55,520 manage the pi 8852 05:31:55,520 --> 05:31:56,958 migrate 8853 05:31:56,958 --> 05:31:58,718 when we run this command django will 8854 05:31:58,718 --> 05:32:00,558 look at our project it will look at 8855 05:32:00,558 --> 05:32:02,718 every app and it will run the pending 8856 05:32:02,718 --> 05:32:04,958 migrations for that app against our 8857 05:32:04,958 --> 05:32:08,000 database so take a look when we run this 8858 05:32:08,000 --> 05:32:10,558 we get several messages 8859 05:32:10,558 --> 05:32:13,280 so if you scroll up you can see applying 8860 05:32:13,280 --> 05:32:14,840 content 8861 05:32:14,840 --> 05:32:17,440 types.001 initial so in the content 8862 05:32:17,440 --> 05:32:19,120 types app which is one of the apps 8863 05:32:19,120 --> 05:32:20,558 installed by default we have this 8864 05:32:20,558 --> 05:32:23,200 migration so django ran this migration 8865 05:32:23,200 --> 05:32:25,360 and this created a database table for 8866 05:32:25,360 --> 05:32:27,840 this app similarly in the auth app which 8867 05:32:27,840 --> 05:32:30,080 is used for authenticating users we have 8868 05:32:30,080 --> 05:32:32,558 this initial migration we also have a 8869 05:32:32,558 --> 05:32:35,040 bunch of migrations for the admin app so 8870 05:32:35,040 --> 05:32:38,000 django run all of these migrations and 8871 05:32:38,000 --> 05:32:39,920 down the bottom you can see 8872 05:32:39,920 --> 05:32:43,360 applying products initial migration 8873 05:32:43,360 --> 05:32:45,680 so every app can contain multiple 8874 05:32:45,680 --> 05:32:47,440 migrations and each migration will make 8875 05:32:47,440 --> 05:32:49,600 a slight change to the database to add a 8876 05:32:49,600 --> 05:32:53,280 new table or modify an existing one 8877 05:32:53,280 --> 05:32:56,240 so back to db browser for sqlite let's 8878 05:32:56,240 --> 05:32:58,638 drag and drop our database file one more 8879 05:32:58,638 --> 05:33:01,200 time here to reload it there you go now 8880 05:33:01,200 --> 05:33:03,840 we have 12 tables in this database each 8881 05:33:03,840 --> 05:33:05,920 table is prefixed with the app it 8882 05:33:05,920 --> 05:33:08,080 belongs to for example you can see we 8883 05:33:08,080 --> 05:33:10,798 have a table for storing our users we 8884 05:33:10,798 --> 05:33:12,878 have another table for storing the 8885 05:33:12,878 --> 05:33:15,600 groups a user belongs to we have another 8886 05:33:15,600 --> 05:33:18,160 table for storing permissions and so on 8887 05:33:18,160 --> 05:33:20,240 now here down the bottom you can see 8888 05:33:20,240 --> 05:33:23,280 is table products underlined product so 8889 05:33:23,280 --> 05:33:25,520 this product table is prefixed with the 8890 05:33:25,520 --> 05:33:27,760 name of our app which is products 8891 05:33:27,760 --> 05:33:29,920 now if you expand this you can see 8892 05:33:29,920 --> 05:33:33,040 these fields in this table id name price 8893 05:33:33,040 --> 05:33:35,360 stock and image url so this is the 8894 05:33:35,360 --> 05:33:37,040 beauty of django we didn't have to come 8895 05:33:37,040 --> 05:33:39,360 here and manually create these tables by 8896 05:33:39,360 --> 05:33:41,680 hand that's very time consuming we 8897 05:33:41,680 --> 05:33:43,520 simply created a model with these 8898 05:33:43,520 --> 05:33:45,840 attributes and then we ran two commands 8899 05:33:45,840 --> 05:33:47,840 in the terminal one for creating a new 8900 05:33:47,840 --> 05:33:49,680 migration based on the changes we have 8901 05:33:49,680 --> 05:33:52,080 made in our code and the other to run 8902 05:33:52,080 --> 05:33:54,558 the pending migrations so now all the 8903 05:33:54,558 --> 05:33:56,160 migrations in this project are run 8904 05:33:56,160 --> 05:33:58,320 against our database which means if we 8905 05:33:58,320 --> 05:34:01,120 go back to the terminal and run python 8906 05:34:01,120 --> 05:34:04,400 manage.pi migrate 8907 05:34:04,400 --> 05:34:06,480 we get this new message no migrations to 8908 05:34:06,480 --> 05:34:07,520 apply 8909 05:34:07,520 --> 05:34:09,200 so here's the workflow next time we 8910 05:34:09,200 --> 05:34:11,760 create a new model or modify an existing 8911 05:34:11,760 --> 05:34:13,120 one we should come back here in the 8912 05:34:13,120 --> 05:34:15,520 terminal create a new migration and then 8913 05:34:15,520 --> 05:34:17,920 run it 8914 05:34:20,798 --> 05:34:22,558 alright here's your exercise we want to 8915 05:34:22,558 --> 05:34:24,958 have the concept of offering discounts 8916 05:34:24,958 --> 05:34:27,200 in our imaginary online shop 8917 05:34:27,200 --> 05:34:29,280 so i want you to create a new model 8918 05:34:29,280 --> 05:34:31,200 class called offer 8919 05:34:31,200 --> 05:34:33,360 and in this class we should have three 8920 05:34:33,360 --> 05:34:34,558 attributes 8921 05:34:34,558 --> 05:34:37,600 one is code which is a textual field for 8922 05:34:37,600 --> 05:34:42,320 example our offers code could be vca2142 8923 05:34:42,320 --> 05:34:45,440 it's just a coupon code 8924 05:34:45,440 --> 05:34:47,040 here we should also have an attribute 8925 05:34:47,040 --> 05:34:49,440 called description 8926 05:34:49,440 --> 05:34:50,718 again this is going to be a textual 8927 05:34:50,718 --> 05:34:53,680 field for example we can say 20 percent 8928 05:34:53,680 --> 05:34:56,638 of all products and then we can display 8929 05:34:56,638 --> 05:34:58,558 this text somewhere on the home page in 8930 05:34:58,558 --> 05:34:59,760 the future 8931 05:34:59,760 --> 05:35:01,520 and finally we need another attribute 8932 05:35:01,520 --> 05:35:04,080 called discount which is going to be a 8933 05:35:04,080 --> 05:35:07,840 floating point number like 0.2 for 20 8934 05:35:07,840 --> 05:35:10,240 discount so go ahead create this model 8935 05:35:10,240 --> 05:35:12,798 class next create a migration and run it 8936 05:35:12,798 --> 05:35:15,280 to bring our database up to date this is 8937 05:35:15,280 --> 05:35:16,958 an awesome exercise for you to get your 8938 05:35:16,958 --> 05:35:19,600 hands dirty in django so spend three to 8939 05:35:19,600 --> 05:35:21,280 five minutes on this exercise when 8940 05:35:21,280 --> 05:35:25,040 you're done come back see my solution 8941 05:35:27,760 --> 05:35:29,920 so here in the models module right after 8942 05:35:29,920 --> 05:35:31,520 our product class i'm going to define a 8943 05:35:31,520 --> 05:35:32,798 new class 8944 05:35:32,798 --> 05:35:34,878 class offer 8945 05:35:34,878 --> 05:35:37,520 this class should also derive from the 8946 05:35:37,520 --> 05:35:39,280 model class in django 8947 05:35:39,280 --> 05:35:42,240 here we add the attributes so 8948 05:35:42,240 --> 05:35:44,000 the first one is code which is going to 8949 05:35:44,000 --> 05:35:47,440 be an instance of models dot char field 8950 05:35:47,440 --> 05:35:49,920 and we set the max length to let's say 8951 05:35:49,920 --> 05:35:51,920 10 characters 8952 05:35:51,920 --> 05:35:54,558 next we add the description 8953 05:35:54,558 --> 05:35:56,000 once again we're going to have a chart 8954 05:35:56,000 --> 05:36:00,080 field with max length of 255 characters 8955 05:36:00,080 --> 05:36:03,040 is sufficient and finally we need 8956 05:36:03,040 --> 05:36:04,480 discount 8957 05:36:04,480 --> 05:36:05,920 and we're going to set this to an 8958 05:36:05,920 --> 05:36:11,040 instance of models that float filled 8959 05:36:11,360 --> 05:36:13,520 so here's our offer class now we need to 8960 05:36:13,520 --> 05:36:15,440 create a migration so over here i'm 8961 05:36:15,440 --> 05:36:17,760 going to open up a terminal window 8962 05:36:17,760 --> 05:36:19,520 this is the main terminal window where 8963 05:36:19,520 --> 05:36:21,600 our web server is running so i'm going 8964 05:36:21,600 --> 05:36:24,000 to create a new terminal window and run 8965 05:36:24,000 --> 05:36:25,600 python 8966 05:36:25,600 --> 05:36:27,520 manage dot pi 8967 05:36:27,520 --> 05:36:28,840 make 8968 05:36:28,840 --> 05:36:31,360 migrations django detected a change in 8969 05:36:31,360 --> 05:36:33,040 our models so it created this new 8970 05:36:33,040 --> 05:36:35,200 migration migration number two called 8971 05:36:35,200 --> 05:36:37,600 offer and this migration will create the 8972 05:36:37,600 --> 05:36:40,000 offer table in our database so let's go 8973 05:36:40,000 --> 05:36:41,360 ahead and run it 8974 05:36:41,360 --> 05:36:42,840 python3 8975 05:36:42,840 --> 05:36:46,160 manage.pi migrate 8976 05:36:46,160 --> 05:36:47,600 beautiful 8977 05:36:47,600 --> 05:36:49,200 now let's drag and drop our database 8978 05:36:49,200 --> 05:36:50,718 here one more time 8979 05:36:50,718 --> 05:36:53,360 our database is refreshed as you can see 8980 05:36:53,360 --> 05:36:55,520 in the products app we have this new 8981 05:36:55,520 --> 05:36:58,480 table offer with these four columns 8982 05:36:58,480 --> 05:37:03,120 id code description and discount 8983 05:37:06,638 --> 05:37:07,920 in this tutorial i'm going to talk to 8984 05:37:07,920 --> 05:37:09,920 you guys about the administrative panel 8985 05:37:09,920 --> 05:37:12,320 of django applications so here in the 8986 05:37:12,320 --> 05:37:17,040 browser if we head over to slash admin 8987 05:37:17,040 --> 05:37:18,958 we get this beautiful login screen to 8988 05:37:18,958 --> 05:37:21,040 log into our administrative panel so 8989 05:37:21,040 --> 05:37:23,280 every django application by default 8990 05:37:23,280 --> 05:37:25,040 comes with an administrative panel we 8991 05:37:25,040 --> 05:37:26,558 don't have to create this from scratch 8992 05:37:26,558 --> 05:37:28,400 we don't have to code this it gets 8993 05:37:28,400 --> 05:37:30,400 auto-generated and this is one of the 8994 05:37:30,400 --> 05:37:32,638 fantastic features of django that 8995 05:37:32,638 --> 05:37:34,958 increases your productivity so let me 8996 05:37:34,958 --> 05:37:36,240 show you how to work with this 8997 05:37:36,240 --> 05:37:37,920 administrative panel 8998 05:37:37,920 --> 05:37:39,920 here in pycharm we need to create the 8999 05:37:39,920 --> 05:37:41,760 first user of this application we call 9000 05:37:41,760 --> 05:37:43,680 that super user 9001 05:37:43,680 --> 05:37:46,000 so here in the terminal window let's run 9002 05:37:46,000 --> 05:37:47,280 python 9003 05:37:47,280 --> 05:37:49,520 manage dot pi 9004 05:37:49,520 --> 05:37:51,040 create 9005 05:37:51,040 --> 05:37:52,160 super 9006 05:37:52,160 --> 05:37:54,718 user all in one word 9007 05:37:54,718 --> 05:37:57,040 so let's go ahead with that it asks for 9008 05:37:57,040 --> 05:37:59,840 username let's type admin 9009 05:37:59,840 --> 05:38:02,798 next we need to enter an email address 9010 05:38:02,798 --> 05:38:05,120 i'm going to enter my email address next 9011 05:38:05,120 --> 05:38:07,280 we need to enter a password i'm going to 9012 05:38:07,280 --> 05:38:09,920 type one two three four 9013 05:38:09,920 --> 05:38:12,240 and repeat it one more time 9014 05:38:12,240 --> 05:38:13,680 note that it complains because this 9015 05:38:13,680 --> 05:38:15,840 password is too short it's too common 9016 05:38:15,840 --> 05:38:17,840 and it's entirely numeric 9017 05:38:17,840 --> 05:38:19,680 so at the end it asks if you want to 9018 05:38:19,680 --> 05:38:21,920 bypass password validation for 9019 05:38:21,920 --> 05:38:24,080 simplicity let's go with that but of 9020 05:38:24,080 --> 05:38:25,600 course in a real scenario you'll want to 9021 05:38:25,600 --> 05:38:27,680 create an admin user with such a simple 9022 05:38:27,680 --> 05:38:28,878 password 9023 05:38:28,878 --> 05:38:30,718 all right we successfully created the 9024 05:38:30,718 --> 05:38:32,320 super user 9025 05:38:32,320 --> 05:38:34,000 now back in the browser let's log in 9026 05:38:34,000 --> 05:38:36,878 with admin and one two three four 9027 05:38:36,878 --> 05:38:38,160 there you go 9028 05:38:38,160 --> 05:38:40,320 so this is our administrative panel as 9029 05:38:40,320 --> 05:38:42,240 you can see we can manage the users and 9030 05:38:42,240 --> 05:38:45,200 groups we can add new users or modify or 9031 05:38:45,200 --> 05:38:47,120 delete existing ones let's have a quick 9032 05:38:47,120 --> 05:38:49,040 look here so here we have this search 9033 05:38:49,040 --> 05:38:51,120 bar on the top we can quickly search for 9034 05:38:51,120 --> 05:38:53,200 users below that we have this table of 9035 05:38:53,200 --> 05:38:55,840 users with this columns username email 9036 05:38:55,840 --> 05:38:58,798 first name last name and status we can 9037 05:38:58,798 --> 05:39:01,120 sort this table by any of these columns 9038 05:39:01,120 --> 05:39:02,718 we also have these filters on the right 9039 05:39:02,718 --> 05:39:03,600 side 9040 05:39:03,600 --> 05:39:05,440 so we get all these beautiful features 9041 05:39:05,440 --> 05:39:07,520 for free with every django application 9042 05:39:07,520 --> 05:39:09,760 we don't have to code this 9043 05:39:09,760 --> 05:39:11,440 earlier in this course we talked about 9044 05:39:11,440 --> 05:39:13,200 this settings module let's have a quick 9045 05:39:13,200 --> 05:39:14,958 look here 9046 05:39:14,958 --> 05:39:16,638 in this module we have 9047 05:39:16,638 --> 05:39:19,360 a setting or variable called installed 9048 05:39:19,360 --> 05:39:20,878 apps and one of the apps that is 9049 05:39:20,878 --> 05:39:23,520 installed by default is the admin app 9050 05:39:23,520 --> 05:39:25,440 so this app is the piece that is 9051 05:39:25,440 --> 05:39:27,760 responsible for generating these views 9052 05:39:27,760 --> 05:39:29,520 now the beautiful thing about the admin 9053 05:39:29,520 --> 05:39:32,558 app is that it is easily extensible so 9054 05:39:32,558 --> 05:39:34,080 back to the home page of our 9055 05:39:34,080 --> 05:39:36,400 administrative panel so far we can only 9056 05:39:36,400 --> 05:39:38,958 manage the users and groups but using 9057 05:39:38,958 --> 05:39:41,120 one line of code we can also manage our 9058 05:39:41,120 --> 05:39:43,280 products let me show you 9059 05:39:43,280 --> 05:39:45,760 let's expand our products app 9060 05:39:45,760 --> 05:39:47,440 inside of this app we have this module 9061 05:39:47,440 --> 05:39:49,680 admin and in this module we register the 9062 05:39:49,680 --> 05:39:51,520 models that we need to manage in the 9063 05:39:51,520 --> 05:39:52,958 admin section 9064 05:39:52,958 --> 05:39:55,840 so let's open this module 9065 05:39:55,840 --> 05:39:57,120 now on the top 9066 05:39:57,120 --> 05:39:58,958 first we need to import our product 9067 05:39:58,958 --> 05:40:01,200 model so we can type out some code like 9068 05:40:01,200 --> 05:40:04,638 this from period models that means the 9069 05:40:04,638 --> 05:40:07,280 models module in the current folder 9070 05:40:07,280 --> 05:40:09,760 so from this module we want to import 9071 05:40:09,760 --> 05:40:12,000 the product class or the product model 9072 05:40:12,000 --> 05:40:13,040 right 9073 05:40:13,040 --> 05:40:14,480 now you can see this comment here 9074 05:40:14,480 --> 05:40:16,400 register your models here let's delete 9075 05:40:16,400 --> 05:40:18,400 this comment we don't need it anymore 9076 05:40:18,400 --> 05:40:19,600 instead we're going to write code like 9077 05:40:19,600 --> 05:40:21,120 this so admin 9078 05:40:21,120 --> 05:40:25,440 dot site dot register 9079 05:40:25,440 --> 05:40:28,160 and we pass this product class as an 9080 05:40:28,160 --> 05:40:30,638 argument what is going on here 9081 05:40:30,638 --> 05:40:32,320 we're using this admin object that is 9082 05:40:32,320 --> 05:40:33,360 imported 9083 05:40:33,360 --> 05:40:35,760 on line one this object has an attribute 9084 05:40:35,760 --> 05:40:37,920 called site which is an object itself 9085 05:40:37,920 --> 05:40:40,480 this object has a method called register 9086 05:40:40,480 --> 05:40:42,798 we're calling this method and passing 9087 05:40:42,798 --> 05:40:45,120 our product class as an argument with 9088 05:40:45,120 --> 05:40:46,798 this we're telling django that we want 9089 05:40:46,798 --> 05:40:49,600 to manage our products in the admin area 9090 05:40:49,600 --> 05:40:51,520 so now with only one line of code or 9091 05:40:51,520 --> 05:40:53,760 more accurately two lines if we go back 9092 05:40:53,760 --> 05:40:55,920 to our administrative panel and refresh 9093 05:40:55,920 --> 05:40:57,920 this page 9094 05:40:57,920 --> 05:41:00,400 we see this new table products that 9095 05:41:00,400 --> 05:41:02,558 represents our products app and in this 9096 05:41:02,558 --> 05:41:04,718 app currently we only have one model 9097 05:41:04,718 --> 05:41:07,520 called product so here we can manage all 9098 05:41:07,520 --> 05:41:09,280 our products currently we don't have any 9099 05:41:09,280 --> 05:41:11,200 products in our database so let's go 9100 05:41:11,200 --> 05:41:13,760 ahead and add a new product look we get 9101 05:41:13,760 --> 05:41:15,920 this beautiful form for free we didn't 9102 05:41:15,920 --> 05:41:18,400 write any code to create this form and 9103 05:41:18,400 --> 05:41:20,958 handle the click event of these buttons 9104 05:41:20,958 --> 05:41:23,200 to insert a product into our database 9105 05:41:23,200 --> 05:41:25,200 all this is automatically generated by 9106 05:41:25,200 --> 05:41:26,558 django 9107 05:41:26,558 --> 05:41:29,120 so let's go ahead and add a new product 9108 05:41:29,120 --> 05:41:32,878 call that orange let's say 1.99 each 9109 05:41:32,878 --> 05:41:34,400 and 9110 05:41:34,400 --> 05:41:36,638 set the stock to 50. 9111 05:41:36,638 --> 05:41:38,878 now we need an image url so open up a 9112 05:41:38,878 --> 05:41:40,320 new tab here 9113 05:41:40,320 --> 05:41:44,240 and in google search for orange 9114 05:41:44,240 --> 05:41:45,200 then 9115 05:41:45,200 --> 05:41:47,040 go to images 9116 05:41:47,040 --> 05:41:49,440 click on tools 9117 05:41:49,440 --> 05:41:51,520 from the user rate menu select for 9118 05:41:51,520 --> 05:41:53,760 labeled for reuse this way we don't have 9119 05:41:53,760 --> 05:41:56,080 problem with copyright issues so these 9120 05:41:56,080 --> 05:41:57,440 are the three images that we can use in 9121 05:41:57,440 --> 05:41:59,040 our application 9122 05:41:59,040 --> 05:42:00,558 let's pick this one 9123 05:42:00,558 --> 05:42:03,040 click on it again 9124 05:42:03,040 --> 05:42:05,920 now on this page right click the image 9125 05:42:05,920 --> 05:42:07,680 copy a link address 9126 05:42:07,680 --> 05:42:10,000 and then back to our product form 9127 05:42:10,000 --> 05:42:11,760 paste it here 9128 05:42:11,760 --> 05:42:14,080 and then save 9129 05:42:14,080 --> 05:42:16,400 so we successfully added a product and 9130 05:42:16,400 --> 05:42:18,320 that product is listed here in the 9131 05:42:18,320 --> 05:42:20,558 product table but what we see here is a 9132 05:42:20,558 --> 05:42:22,400 little bit confusing it says product 9133 05:42:22,400 --> 05:42:24,638 object one it's not clear what product 9134 05:42:24,638 --> 05:42:26,480 this is is it an orange is it a 9135 05:42:26,480 --> 05:42:28,480 strawberry what price is it so in the 9136 05:42:28,480 --> 05:42:30,000 next tutorial i will show you how to 9137 05:42:30,000 --> 05:42:32,718 customize this table and add columns 9138 05:42:32,718 --> 05:42:35,840 specific to products 9139 05:42:39,360 --> 05:42:40,638 in this tutorial i'm going to show you 9140 05:42:40,638 --> 05:42:42,958 guys how to customize this products 9141 05:42:42,958 --> 05:42:45,360 table in the admin area that is very 9142 05:42:45,360 --> 05:42:48,240 easy so back to pycharm 9143 05:42:48,240 --> 05:42:49,840 here in the admin module where we 9144 05:42:49,840 --> 05:42:51,520 register the product class for 9145 05:42:51,520 --> 05:42:53,920 administration we need to create a new 9146 05:42:53,920 --> 05:42:57,840 class called product admin so 9147 05:42:57,840 --> 05:42:59,280 we create a class 9148 05:42:59,280 --> 05:43:02,400 called product admin so this is the 9149 05:43:02,400 --> 05:43:04,558 convention we follow in django we start 9150 05:43:04,558 --> 05:43:06,000 with the name of our model that is 9151 05:43:06,000 --> 05:43:09,360 product and then we add the suffix admin 9152 05:43:09,360 --> 05:43:11,760 now we should inherit this class from 9153 05:43:11,760 --> 05:43:13,040 admin 9154 05:43:13,040 --> 05:43:16,878 dot model admin what is going on here 9155 05:43:16,878 --> 05:43:18,958 well this admin is a module that we have 9156 05:43:18,958 --> 05:43:20,718 imported on line one 9157 05:43:20,718 --> 05:43:22,480 in this module we have a class called 9158 05:43:22,480 --> 05:43:24,558 model admin which provides all the 9159 05:43:24,558 --> 05:43:27,120 common functionality for managing models 9160 05:43:27,120 --> 05:43:29,440 in the admin area so with deriving this 9161 05:43:29,440 --> 05:43:32,558 class from the model admin this class 9162 05:43:32,558 --> 05:43:35,440 inherits all this common functionality 9163 05:43:35,440 --> 05:43:36,480 now 9164 05:43:36,480 --> 05:43:38,558 in the body of this class we can 9165 05:43:38,558 --> 05:43:40,638 overwrite or change some of this 9166 05:43:40,638 --> 05:43:43,120 functionality or attributes for example 9167 05:43:43,120 --> 05:43:44,958 in this class we have an attribute 9168 05:43:44,958 --> 05:43:46,400 called list 9169 05:43:46,400 --> 05:43:48,638 underline display and this attribute 9170 05:43:48,638 --> 05:43:50,638 specifies the columns that should be 9171 05:43:50,638 --> 05:43:53,040 visible in this table 9172 05:43:53,040 --> 05:43:54,958 so we set this to a tuple 9173 05:43:54,958 --> 05:43:57,200 and in this tuple we add one or more 9174 05:43:57,200 --> 05:43:59,600 attributes of our product class 9175 05:43:59,600 --> 05:44:01,520 so let's quickly open up our product 9176 05:44:01,520 --> 05:44:02,480 class 9177 05:44:02,480 --> 05:44:05,920 it's here in the models module 9178 05:44:05,920 --> 05:44:08,240 okay there you go so our product class 9179 05:44:08,240 --> 05:44:11,520 has these attributes name price stock 9180 05:44:11,520 --> 05:44:14,320 and image url now image url as you saw 9181 05:44:14,320 --> 05:44:15,840 is a long string so we don't want to 9182 05:44:15,840 --> 05:44:17,920 display that in our table so in this 9183 05:44:17,920 --> 05:44:20,080 tutorial let's add these three 9184 05:44:20,080 --> 05:44:22,638 attributes to our products table 9185 05:44:22,638 --> 05:44:24,400 back to the admin module 9186 05:44:24,400 --> 05:44:28,480 in this tuple we add three strings name 9187 05:44:28,480 --> 05:44:29,520 price 9188 05:44:29,520 --> 05:44:31,680 and stock 9189 05:44:31,680 --> 05:44:33,200 now we have this little warning here 9190 05:44:33,200 --> 05:44:35,680 because we haven't added two line breaks 9191 05:44:35,680 --> 05:44:37,520 before and 9192 05:44:37,520 --> 05:44:38,958 after this class 9193 05:44:38,958 --> 05:44:40,480 that is better 9194 05:44:40,480 --> 05:44:42,638 finally when registering our product 9195 05:44:42,638 --> 05:44:44,558 class for administration 9196 05:44:44,558 --> 05:44:46,798 as the second argument we need to pass 9197 05:44:46,798 --> 05:44:49,840 this new class product admin 9198 05:44:49,840 --> 05:44:51,440 so django will use the information in 9199 05:44:51,440 --> 05:44:53,920 this class to decide how to present the 9200 05:44:53,920 --> 05:44:55,680 list of products 9201 05:44:55,680 --> 05:44:57,280 so back in the browser let's refresh 9202 05:44:57,280 --> 05:44:59,760 this page there you go here's our 9203 05:44:59,760 --> 05:45:02,000 products table now we have three columns 9204 05:45:02,000 --> 05:45:04,160 name price and stock you can see we have 9205 05:45:04,160 --> 05:45:07,440 this orange the price is set to 1.99 and 9206 05:45:07,440 --> 05:45:09,680 currently we have 50 items in the stock 9207 05:45:09,680 --> 05:45:12,000 beautiful let's go ahead and add a new 9208 05:45:12,000 --> 05:45:13,520 product here 9209 05:45:13,520 --> 05:45:15,440 let's say strawberry 9210 05:45:15,440 --> 05:45:18,480 and the price 2.99 9211 05:45:18,480 --> 05:45:20,718 30 for the stock and finally we need an 9212 05:45:20,718 --> 05:45:22,958 image url i'm going to paste this 9213 05:45:22,958 --> 05:45:25,040 address that i just copied before 9214 05:45:25,040 --> 05:45:28,558 recording this video okay let's go ahead 9215 05:45:28,558 --> 05:45:30,558 save the changes 9216 05:45:30,558 --> 05:45:32,798 the product is added beautiful and now 9217 05:45:32,798 --> 05:45:35,120 we have two products in our table now 9218 05:45:35,120 --> 05:45:36,798 here's what i want you to do i want you 9219 05:45:36,798 --> 05:45:39,040 to pause the video and add a few more 9220 05:45:39,040 --> 05:45:40,798 products to our database somewhere 9221 05:45:40,798 --> 05:45:43,360 around 5 to 10. in the next tutorial i'm 9222 05:45:43,360 --> 05:45:45,040 going to show you how to display these 9223 05:45:45,040 --> 05:45:47,360 products to the public users of our 9224 05:45:47,360 --> 05:45:49,600 website because this admin panel is only 9225 05:45:49,600 --> 05:45:52,638 visible to admins so anonymous users or 9226 05:45:52,638 --> 05:45:54,160 other registered users are not going to 9227 05:45:54,160 --> 05:45:56,080 have access to this panel instead 9228 05:45:56,080 --> 05:45:57,600 they're going to go to 9229 05:45:57,600 --> 05:45:58,638 slash 9230 05:45:58,638 --> 05:46:00,000 products 9231 05:46:00,000 --> 05:46:01,440 and currently we're displaying a hello 9232 05:46:01,440 --> 05:46:02,958 world message we're going to change this 9233 05:46:02,958 --> 05:46:04,558 page and display the list of our 9234 05:46:04,558 --> 05:46:06,798 products in a beautiful grid so pause 9235 05:46:06,798 --> 05:46:08,718 the video and add five to ten products 9236 05:46:08,718 --> 05:46:12,000 then come back continue watching 9237 05:46:14,718 --> 05:46:16,878 currently in the admin area we can only 9238 05:46:16,878 --> 05:46:20,080 manage the products in the products app 9239 05:46:20,080 --> 05:46:22,400 so there is no way to manage the offers 9240 05:46:22,400 --> 05:46:25,280 so as an exercise add the ability to 9241 05:46:25,280 --> 05:46:27,440 manage the offers here when we click the 9242 05:46:27,440 --> 05:46:29,840 offers link we should see a table with 9243 05:46:29,840 --> 05:46:32,798 two columns code and discount 9244 05:46:32,798 --> 05:46:34,718 so go ahead and implement this and then 9245 05:46:34,718 --> 05:46:37,680 create a new offer 9246 05:46:40,480 --> 05:46:42,160 all right here's my solution 9247 05:46:42,160 --> 05:46:44,480 in the admin module of the products app 9248 05:46:44,480 --> 05:46:46,958 on line two first we need to import 9249 05:46:46,958 --> 05:46:49,040 the offer class 9250 05:46:49,040 --> 05:46:50,320 next 9251 05:46:50,320 --> 05:46:53,760 we create a new class called offer admin 9252 05:46:53,760 --> 05:46:55,920 we derive this from admin that model 9253 05:46:55,920 --> 05:46:57,120 admin 9254 05:46:57,120 --> 05:46:59,440 and here we set the list display 9255 05:46:59,440 --> 05:47:01,920 attribute to a tuple with two items 9256 05:47:01,920 --> 05:47:02,798 code 9257 05:47:02,798 --> 05:47:05,520 and discount 9258 05:47:05,520 --> 05:47:07,120 and finally we need to register our 9259 05:47:07,120 --> 05:47:09,360 offer model for administration 9260 05:47:09,360 --> 05:47:12,600 so admin.site.register 9261 05:47:12,958 --> 05:47:17,040 offer and offer admin 9262 05:47:17,040 --> 05:47:18,240 that's it 9263 05:47:18,240 --> 05:47:20,718 now if we go back to the admin area 9264 05:47:20,718 --> 05:47:23,040 and refresh the home page we see the 9265 05:47:23,040 --> 05:47:25,200 offers link here beautiful currently we 9266 05:47:25,200 --> 05:47:26,958 don't have any offers so let's go ahead 9267 05:47:26,958 --> 05:47:28,638 and add a new offer 9268 05:47:28,638 --> 05:47:32,080 i'm going to use the code like vca 1010 9269 05:47:32,080 --> 05:47:33,520 let's say 10 9270 05:47:33,520 --> 05:47:35,600 off all products 9271 05:47:35,600 --> 05:47:37,200 and discount i'm going to set this to 9272 05:47:37,200 --> 05:47:38,958 0.1 9273 05:47:38,958 --> 05:47:40,878 now technically in a real application 9274 05:47:40,878 --> 05:47:43,120 this code should be unique but that's a 9275 05:47:43,120 --> 05:47:45,120 topic for a future tutorial so for now 9276 05:47:45,120 --> 05:47:47,120 don't worry about it let's go ahead and 9277 05:47:47,120 --> 05:47:48,718 save this offer 9278 05:47:48,718 --> 05:47:51,040 okay our offer is saved successfully 9279 05:47:51,040 --> 05:47:53,280 beautiful as you can see in this table 9280 05:47:53,280 --> 05:47:57,840 we have two columns code and discount 9281 05:48:01,200 --> 05:48:02,320 in this tutorial i'm going to show you 9282 05:48:02,320 --> 05:48:04,160 guys how to replace this hello world 9283 05:48:04,160 --> 05:48:06,160 message with the list of products in our 9284 05:48:06,160 --> 05:48:08,878 database and that is super easy 9285 05:48:08,878 --> 05:48:11,200 so here in pycharm in the products app 9286 05:48:11,200 --> 05:48:13,840 let's open up the views module 9287 05:48:13,840 --> 05:48:15,760 area i told you that in this module we 9288 05:48:15,760 --> 05:48:17,440 define our view functions which are 9289 05:48:17,440 --> 05:48:19,040 functions that get called when the 9290 05:48:19,040 --> 05:48:21,760 browser sends an http request to the 9291 05:48:21,760 --> 05:48:22,798 server 9292 05:48:22,798 --> 05:48:24,958 for example when the user navigates to 9293 05:48:24,958 --> 05:48:27,600 slash products page our browser sends an 9294 05:48:27,600 --> 05:48:30,400 http request to our django application 9295 05:48:30,400 --> 05:48:32,558 django takes that request and hands it 9296 05:48:32,558 --> 05:48:35,360 off to this function the index function 9297 05:48:35,360 --> 05:48:36,798 and here we're simply returning a hello 9298 05:48:36,798 --> 05:48:38,480 world message we want to replace this 9299 05:48:38,480 --> 05:48:39,680 with the list of products in our 9300 05:48:39,680 --> 05:48:40,718 database 9301 05:48:40,718 --> 05:48:42,718 so the first step is to get the products 9302 05:48:42,718 --> 05:48:44,798 stored in the database and then we need 9303 05:48:44,798 --> 05:48:47,840 to format them using some html template 9304 05:48:47,840 --> 05:48:50,080 so let's go ahead and 9305 05:48:50,080 --> 05:48:53,040 on the top import our product class from 9306 05:48:53,040 --> 05:48:55,440 the models module so 9307 05:48:55,440 --> 05:48:56,320 from 9308 05:48:56,320 --> 05:48:57,680 period models 9309 05:48:57,680 --> 05:49:00,080 import product so we're importing the 9310 05:49:00,080 --> 05:49:02,958 product class in the models module in 9311 05:49:02,958 --> 05:49:05,200 the current folder so period means the 9312 05:49:05,200 --> 05:49:06,480 current folder 9313 05:49:06,480 --> 05:49:07,840 now this product class has a bunch of 9314 05:49:07,840 --> 05:49:10,080 useful methods for getting data from our 9315 05:49:10,080 --> 05:49:12,160 database for example we can call 9316 05:49:12,160 --> 05:49:15,120 product.objects.all 9317 05:49:15,120 --> 05:49:16,878 and this returns all the products we 9318 05:49:16,878 --> 05:49:19,920 have in our database we also have filter 9319 05:49:19,920 --> 05:49:21,840 for filtering products let's say we want 9320 05:49:21,840 --> 05:49:23,680 to get products that are more expensive 9321 05:49:23,680 --> 05:49:25,680 than two dollars or products that are 9322 05:49:25,680 --> 05:49:27,440 out of stock we can use the filter 9323 05:49:27,440 --> 05:49:30,080 method we also have the get method for 9324 05:49:30,080 --> 05:49:32,160 getting a single product and we also 9325 05:49:32,160 --> 05:49:34,080 have the save method for inserting a new 9326 05:49:34,080 --> 05:49:36,958 product or updating an existing one now 9327 05:49:36,958 --> 05:49:38,558 in this tutorial we're going to use the 9328 05:49:38,558 --> 05:49:40,878 all method to get all the products from 9329 05:49:40,878 --> 05:49:42,240 our database 9330 05:49:42,240 --> 05:49:45,200 then we store them here in this variable 9331 05:49:45,200 --> 05:49:46,958 so we have done the first step next we 9332 05:49:46,958 --> 05:49:49,280 need to create an html template for 9333 05:49:49,280 --> 05:49:51,280 presenting this list of products to the 9334 05:49:51,280 --> 05:49:54,400 user so back to the project panel 9335 05:49:54,400 --> 05:49:56,718 inside the products app 9336 05:49:56,718 --> 05:50:00,558 let's add a new folder a new directory 9337 05:50:00,558 --> 05:50:02,240 and by convention we should call that 9338 05:50:02,240 --> 05:50:04,798 templates make sure to spell it properly 9339 05:50:04,798 --> 05:50:06,480 because this is a special directory that 9340 05:50:06,480 --> 05:50:09,440 django looks at the load templates 9341 05:50:09,440 --> 05:50:11,600 so let's go ahead with that now we want 9342 05:50:11,600 --> 05:50:14,080 to create a template for the index 9343 05:50:14,080 --> 05:50:16,878 function so inside of this folder let's 9344 05:50:16,878 --> 05:50:20,160 add a new file 9345 05:50:20,160 --> 05:50:22,400 and call that file index 9346 05:50:22,400 --> 05:50:24,958 dot html 9347 05:50:24,958 --> 05:50:26,958 now here we can write some html code if 9348 05:50:26,958 --> 05:50:28,480 you're not familiar with html don't 9349 05:50:28,480 --> 05:50:30,480 worry just follow along with me it's 9350 05:50:30,480 --> 05:50:32,000 super easy to learn 9351 05:50:32,000 --> 05:50:35,040 so first let's type h1 and then press 9352 05:50:35,040 --> 05:50:35,920 tab 9353 05:50:35,920 --> 05:50:38,558 this inserts an h1 tag into our html 9354 05:50:38,558 --> 05:50:40,638 document and we use this to display a 9355 05:50:40,638 --> 05:50:43,680 big bold heading so inside these tags 9356 05:50:43,680 --> 05:50:46,718 let's type products 9357 05:50:46,718 --> 05:50:49,680 now below this h1 let's type ul and 9358 05:50:49,680 --> 05:50:50,878 press tab 9359 05:50:50,878 --> 05:50:53,200 now this inserts a ul tag and we use 9360 05:50:53,200 --> 05:50:55,760 this to display a list of items now we 9361 05:50:55,760 --> 05:50:58,160 need to surround each item with an li 9362 05:50:58,160 --> 05:51:02,160 tag so type out li press tab 9363 05:51:02,160 --> 05:51:05,120 and in between the tags let's type item 9364 05:51:05,120 --> 05:51:08,160 1 then copy and paste this a couple 9365 05:51:08,160 --> 05:51:12,320 times so item 2 and item 3. now instead 9366 05:51:12,320 --> 05:51:14,400 of item 1 item 2 and item 3 we want to 9367 05:51:14,400 --> 05:51:16,558 display the name of our products but 9368 05:51:16,558 --> 05:51:18,638 before we get there let's simply return 9369 05:51:18,638 --> 05:51:20,878 this html markup to the client instead 9370 05:51:20,878 --> 05:51:22,558 of the hello world message 9371 05:51:22,558 --> 05:51:25,040 so back to the views module 9372 05:51:25,040 --> 05:51:26,958 here in the index function we need to 9373 05:51:26,958 --> 05:51:29,760 change the return statement on line 8. 9374 05:51:29,760 --> 05:51:31,120 so 9375 05:51:31,120 --> 05:51:33,040 you want to call the render function 9376 05:51:33,040 --> 05:51:34,878 this function is already imported on the 9377 05:51:34,878 --> 05:51:37,840 top on line 2. we use this function to 9378 05:51:37,840 --> 05:51:39,600 render a template 9379 05:51:39,600 --> 05:51:40,638 so 9380 05:51:40,638 --> 05:51:42,480 let's call this function as the first 9381 05:51:42,480 --> 05:51:44,080 argument we need to pass 9382 05:51:44,080 --> 05:51:46,080 this request object that we get as an 9383 05:51:46,080 --> 05:51:47,680 argument to this function 9384 05:51:47,680 --> 05:51:50,240 so let's pass the request here 9385 05:51:50,240 --> 05:51:51,680 as the second argument we need to 9386 05:51:51,680 --> 05:51:53,920 specify the name of our template so we 9387 05:51:53,920 --> 05:51:55,840 pass a string what is the name of our 9388 05:51:55,840 --> 05:51:57,638 template that is 9389 05:51:57,638 --> 05:52:00,558 index.html right now before going any 9390 05:52:00,558 --> 05:52:03,040 further let's go back to the browser 9391 05:52:03,040 --> 05:52:04,080 and 9392 05:52:04,080 --> 05:52:05,520 refresh this page 9393 05:52:05,520 --> 05:52:07,760 so instead of hello world we see this 9394 05:52:07,760 --> 05:52:09,600 product's heading this is the result of 9395 05:52:09,600 --> 05:52:14,400 an h1 tag and with ul and li tags we see 9396 05:52:14,400 --> 05:52:17,280 these bullet points over here beautiful 9397 05:52:17,280 --> 05:52:19,360 now finally we need to replace item one 9398 05:52:19,360 --> 05:52:21,040 item two and item three with the list of 9399 05:52:21,040 --> 05:52:22,558 our products 9400 05:52:22,558 --> 05:52:24,480 so back to our view function 9401 05:52:24,480 --> 05:52:26,638 as the third argument to the render 9402 05:52:26,638 --> 05:52:29,120 function we need to pass the list of 9403 05:52:29,120 --> 05:52:30,400 products that we loaded from the 9404 05:52:30,400 --> 05:52:32,638 database so here we need to pass a 9405 05:52:32,638 --> 05:52:34,320 dictionary 9406 05:52:34,320 --> 05:52:35,760 in this dictionary we're going to add 9407 05:52:35,760 --> 05:52:37,680 one key value pair we're going to call 9408 05:52:37,680 --> 05:52:39,520 the key products but we could call it 9409 05:52:39,520 --> 05:52:41,360 anything it doesn't really matter 9410 05:52:41,360 --> 05:52:43,040 the value for that key should be the 9411 05:52:43,040 --> 05:52:45,040 products object that we define on line 9412 05:52:45,040 --> 05:52:46,080 7. 9413 05:52:46,080 --> 05:52:46,878 so 9414 05:52:46,878 --> 05:52:48,840 here in our dictionary let's add this 9415 05:52:48,840 --> 05:52:51,440 key product 9416 05:52:51,440 --> 05:52:53,760 colon and the value should be 9417 05:52:53,760 --> 05:52:55,760 this product object 9418 05:52:55,760 --> 05:52:58,320 so let me break up what we have on line 9419 05:52:58,320 --> 05:52:59,520 eight 9420 05:52:59,520 --> 05:53:01,280 over multiple lines so you can see it 9421 05:53:01,280 --> 05:53:02,400 clearly 9422 05:53:02,400 --> 05:53:04,240 recording the render function with three 9423 05:53:04,240 --> 05:53:06,638 arguments the request object the name of 9424 05:53:06,638 --> 05:53:08,480 our template and a dictionary that 9425 05:53:08,480 --> 05:53:11,520 contains the data to be passed to this 9426 05:53:11,520 --> 05:53:12,718 template 9427 05:53:12,718 --> 05:53:14,160 and finally we're going to go to this 9428 05:53:14,160 --> 05:53:15,600 template 9429 05:53:15,600 --> 05:53:17,200 index.html 9430 05:53:17,200 --> 05:53:20,160 and replace these items with our 9431 05:53:20,160 --> 05:53:22,558 products that is very easy 9432 05:53:22,558 --> 05:53:25,360 so just after the ul tag i want you to 9433 05:53:25,360 --> 05:53:27,840 type out a curly brace followed by a 9434 05:53:27,840 --> 05:53:31,280 percent a space a percent one more time 9435 05:53:31,280 --> 05:53:33,040 and a closing brace 9436 05:53:33,040 --> 05:53:35,200 this is what we call a template tag in 9437 05:53:35,200 --> 05:53:38,240 django so in between these markers we 9438 05:53:38,240 --> 05:53:40,558 can write dynamic code for example we 9439 05:53:40,558 --> 05:53:42,160 can add loops to repeat some 9440 05:53:42,160 --> 05:53:44,798 instructions or write if statements to 9441 05:53:44,798 --> 05:53:47,520 conditionally render some content 9442 05:53:47,520 --> 05:53:51,040 so here we want to write a for loop for 9443 05:53:51,040 --> 05:53:52,240 product 9444 05:53:52,240 --> 05:53:53,040 in 9445 05:53:53,040 --> 05:53:54,240 products 9446 05:53:54,240 --> 05:53:56,480 so earlier in our view function we 9447 05:53:56,480 --> 05:53:58,840 passed these products here using a 9448 05:53:58,840 --> 05:54:01,680 dictionary back to the view function 9449 05:54:01,680 --> 05:54:02,558 look 9450 05:54:02,558 --> 05:54:04,000 as the third argument to the render 9451 05:54:04,000 --> 05:54:06,558 function we passed is dictionary this 9452 05:54:06,558 --> 05:54:08,718 dictionary is called the context so it 9453 05:54:08,718 --> 05:54:10,638 provides data to be used in our 9454 05:54:10,638 --> 05:54:12,958 templates in this case we have a 9455 05:54:12,958 --> 05:54:15,040 property called products and that is the 9456 05:54:15,040 --> 05:54:17,840 reason we can access it here so we are 9457 05:54:17,840 --> 05:54:20,080 looping over this product 9458 05:54:20,080 --> 05:54:22,718 now for each product you want to render 9459 05:54:22,718 --> 05:54:25,360 a list item so let's indent this item 9460 05:54:25,360 --> 05:54:26,320 here 9461 05:54:26,320 --> 05:54:27,360 and 9462 05:54:27,360 --> 05:54:29,600 replace item 1 with the name of the 9463 05:54:29,600 --> 05:54:31,680 product so here we want to dynamically 9464 05:54:31,680 --> 05:54:34,400 render the name attribute of this 9465 05:54:34,400 --> 05:54:36,160 product object 9466 05:54:36,160 --> 05:54:39,440 to do that we use double curly braces so 9467 05:54:39,440 --> 05:54:42,480 open two curly braces and close them and 9468 05:54:42,480 --> 05:54:45,120 in between them type out product dot 9469 05:54:45,120 --> 05:54:47,680 name so whenever we use double curly 9470 05:54:47,680 --> 05:54:50,400 braces in a django template django will 9471 05:54:50,400 --> 05:54:52,480 evaluate the expression that we put here 9472 05:54:52,480 --> 05:54:54,958 and render it in html 9473 05:54:54,958 --> 05:54:58,000 now the last step is to end this four 9474 05:54:58,000 --> 05:55:00,718 block because django unlike python 9475 05:55:00,718 --> 05:55:03,360 doesn't rely on indentation to determine 9476 05:55:03,360 --> 05:55:05,280 the scope of a block 9477 05:55:05,280 --> 05:55:06,240 so 9478 05:55:06,240 --> 05:55:08,638 right after this for loop once again we 9479 05:55:08,638 --> 05:55:11,440 add curly braces eight percent and type 9480 05:55:11,440 --> 05:55:13,760 out and four 9481 05:55:13,760 --> 05:55:16,000 and then we close it here 9482 05:55:16,000 --> 05:55:17,760 now we no longer need these items here 9483 05:55:17,760 --> 05:55:19,360 so let's delete them 9484 05:55:19,360 --> 05:55:21,120 now back to the browser let's refresh 9485 05:55:21,120 --> 05:55:23,120 our page 9486 05:55:23,120 --> 05:55:25,360 so instead of item 1 to 3 we see the 9487 05:55:25,360 --> 05:55:27,120 name of the products stored in our 9488 05:55:27,120 --> 05:55:28,320 database 9489 05:55:28,320 --> 05:55:30,000 now let's take this to the next level by 9490 05:55:30,000 --> 05:55:31,920 displaying the price of each product 9491 05:55:31,920 --> 05:55:33,440 right next to it 9492 05:55:33,440 --> 05:55:34,718 so 9493 05:55:34,718 --> 05:55:36,878 back to our template this is where we're 9494 05:55:36,878 --> 05:55:38,798 rendering the name of the product 9495 05:55:38,798 --> 05:55:40,958 after the curly braces let's add 9496 05:55:40,958 --> 05:55:42,798 parenthesis now inside of the 9497 05:55:42,798 --> 05:55:44,320 parenthesis we want to dynamically 9498 05:55:44,320 --> 05:55:46,878 render the price of each product so once 9499 05:55:46,878 --> 05:55:48,080 again we use 9500 05:55:48,080 --> 05:55:50,480 the double curly brace syntax to render 9501 05:55:50,480 --> 05:55:53,360 some value dynamically that is the value 9502 05:55:53,360 --> 05:55:56,718 of product dot price 9503 05:55:56,718 --> 05:55:58,638 like this 9504 05:55:58,638 --> 05:56:00,958 now back here let's refresh the page 9505 05:56:00,958 --> 05:56:02,718 now we can see the price of each product 9506 05:56:02,718 --> 05:56:05,120 right here beautiful we could also 9507 05:56:05,120 --> 05:56:07,680 prefix that with a dollar sign so before 9508 05:56:07,680 --> 05:56:10,400 the curly braces we add a dollar sign 9509 05:56:10,400 --> 05:56:13,360 and then when we refresh the page we see 9510 05:56:13,360 --> 05:56:15,840 the actual dollar value 9511 05:56:15,840 --> 05:56:17,680 so this is what i want you to take away 9512 05:56:17,680 --> 05:56:20,160 in this tutorial in django templates we 9513 05:56:20,160 --> 05:56:23,120 have two special syntaxes one is a curly 9514 05:56:23,120 --> 05:56:25,600 brace and a percent this is called a 9515 05:56:25,600 --> 05:56:27,520 template tag and we use this to 9516 05:56:27,520 --> 05:56:29,920 dynamically execute logic 9517 05:56:29,920 --> 05:56:32,320 so in between these template tags we can 9518 05:56:32,320 --> 05:56:35,520 write for loops to repeat some code or 9519 05:56:35,520 --> 05:56:38,000 we can use if statements to dynamically 9520 05:56:38,000 --> 05:56:39,520 render content 9521 05:56:39,520 --> 05:56:41,520 the other syntax we have is the double 9522 05:56:41,520 --> 05:56:43,760 curly brace syntax and we use this to 9523 05:56:43,760 --> 05:56:46,400 dynamically render values in our html 9524 05:56:46,400 --> 05:56:48,638 markup so whatever we put in between 9525 05:56:48,638 --> 05:56:51,520 them will get evaluated and written to 9526 05:56:51,520 --> 05:56:53,600 the html markup 9527 05:56:53,600 --> 05:56:56,000 now this page is pretty boring so far in 9528 05:56:56,000 --> 05:56:57,920 the next tutorial i will show you how to 9529 05:56:57,920 --> 05:56:59,680 convert these bullet points into 9530 05:56:59,680 --> 05:57:03,160 beautiful cards 9531 05:57:06,000 --> 05:57:07,440 over the next few tutorials we're going 9532 05:57:07,440 --> 05:57:10,000 to replace this boring list of bullet 9533 05:57:10,000 --> 05:57:13,120 points with a beautiful modern card like 9534 05:57:13,120 --> 05:57:14,400 this so on the top we're going to 9535 05:57:14,400 --> 05:57:16,480 display the image for each product below 9536 05:57:16,480 --> 05:57:18,798 that we're going to display the title or 9537 05:57:18,798 --> 05:57:20,480 the name of the product as well as the 9538 05:57:20,480 --> 05:57:22,558 price and potentially a button to add 9539 05:57:22,558 --> 05:57:25,120 that product to a shopping cart now to 9540 05:57:25,120 --> 05:57:26,320 present something like this we're going 9541 05:57:26,320 --> 05:57:28,558 to use a framework called bootstrap it's 9542 05:57:28,558 --> 05:57:30,878 a very popular framework for building 9543 05:57:30,878 --> 05:57:33,840 modern and responsive applications so 9544 05:57:33,840 --> 05:57:37,200 head over to getbootstrap.com 9545 05:57:37,200 --> 05:57:38,878 on the home page 9546 05:57:38,878 --> 05:57:40,400 you should see something like this and 9547 05:57:40,400 --> 05:57:43,600 then go to the documentation 9548 05:57:43,600 --> 05:57:45,280 on this page you can see how you can add 9549 05:57:45,280 --> 05:57:47,200 this bootstrap framework to your web 9550 05:57:47,200 --> 05:57:50,160 applications so if you scroll down 9551 05:57:50,160 --> 05:57:53,200 here you should see a starter template 9552 05:57:53,200 --> 05:57:55,680 so here we have some basic html markup 9553 05:57:55,680 --> 05:57:57,520 we want to bring all this html markup 9554 05:57:57,520 --> 05:57:59,878 and store it in a template called 9555 05:57:59,878 --> 05:58:02,638 base.html so over here you should see 9556 05:58:02,638 --> 05:58:06,080 this copy button click that to copy this 9557 05:58:06,080 --> 05:58:08,718 then back to pycharm 9558 05:58:08,718 --> 05:58:10,638 in the project panel 9559 05:58:10,638 --> 05:58:13,520 inside of the templates folder let's add 9560 05:58:13,520 --> 05:58:16,558 a new html file so a new file we call 9561 05:58:16,558 --> 05:58:19,798 this base.html 9562 05:58:20,160 --> 05:58:21,040 now 9563 05:58:21,040 --> 05:58:23,440 with command and v on mac or ctrl and v 9564 05:58:23,440 --> 05:58:26,320 on windows paste all this markup that we 9565 05:58:26,320 --> 05:58:28,958 copied from bootstrap website 9566 05:58:28,958 --> 05:58:29,760 now 9567 05:58:29,760 --> 05:58:32,400 here on line 13 9568 05:58:32,400 --> 05:58:34,320 we have the body tag and this is the 9569 05:58:34,320 --> 05:58:36,798 container for what the user sees in a 9570 05:58:36,798 --> 05:58:38,638 given web page 9571 05:58:38,638 --> 05:58:41,040 now in this example we have an h1 tag 9572 05:58:41,040 --> 05:58:43,760 for displaying a hello world title we 9573 05:58:43,760 --> 05:58:45,040 want to replace this with something 9574 05:58:45,040 --> 05:58:47,280 dynamic so for each page in our 9575 05:58:47,280 --> 05:58:49,120 application we want to render something 9576 05:58:49,120 --> 05:58:51,200 different for example when the user goes 9577 05:58:51,200 --> 05:58:54,000 to the products page over here instead 9578 05:58:54,000 --> 05:58:56,638 of this h1 tag we want to render what we 9579 05:58:56,638 --> 05:58:59,920 defined in index.html here 9580 05:58:59,920 --> 05:59:00,718 okay 9581 05:59:00,718 --> 05:59:04,320 so back to base.html let's delete this 9582 05:59:04,320 --> 05:59:06,638 h1 tag once again we're going to use a 9583 05:59:06,638 --> 05:59:09,200 template tag so curly brace percent 9584 05:59:09,200 --> 05:59:11,280 percent curly brace 9585 05:59:11,280 --> 05:59:12,798 now the template tag we're going to use 9586 05:59:12,798 --> 05:59:15,120 is called block and with this we can 9587 05:59:15,120 --> 05:59:17,360 define a block or a hole in this 9588 05:59:17,360 --> 05:59:19,280 document so we're going to create a 9589 05:59:19,280 --> 05:59:21,920 block called content we could call it 9590 05:59:21,920 --> 05:59:23,360 anything but here we're defining the 9591 05:59:23,360 --> 05:59:25,440 content area of our website 9592 05:59:25,440 --> 05:59:28,638 so that's why we call this block content 9593 05:59:28,638 --> 05:59:31,120 now after this once again we need to add 9594 05:59:31,120 --> 05:59:33,040 another template tag 9595 05:59:33,040 --> 05:59:34,840 and this is called end 9596 05:59:34,840 --> 05:59:38,638 block now we should go to index.html 9597 05:59:38,638 --> 05:59:40,638 on the top we should tell django that we 9598 05:59:40,638 --> 05:59:43,200 want to extend this base template which 9599 05:59:43,200 --> 05:59:44,718 means we're going to use all the markup 9600 05:59:44,718 --> 05:59:46,240 defined here 9601 05:59:46,240 --> 05:59:48,320 and this gives our applications a modern 9602 05:59:48,320 --> 05:59:49,520 look and feel 9603 05:59:49,520 --> 05:59:52,240 so back to index.html once again we're 9604 05:59:52,240 --> 05:59:55,200 going to use another template tag 9605 05:59:55,200 --> 05:59:58,798 this one is called extends so extend 9606 05:59:58,798 --> 06:00:00,400 what are we extending 9607 06:00:00,400 --> 06:00:03,280 the template defined in base.html so 9608 06:00:03,280 --> 06:00:06,160 here we add a string base 9609 06:00:06,160 --> 06:00:07,760 dot html 9610 06:00:07,760 --> 06:00:09,920 now we want to fill the block or the 9611 06:00:09,920 --> 06:00:13,840 hole that we defined in base.html 9612 06:00:13,840 --> 06:00:15,440 with 9613 06:00:15,440 --> 06:00:16,798 these view lines 9614 06:00:16,798 --> 06:00:19,760 so once again we add a template tag 9615 06:00:19,760 --> 06:00:21,680 block what is the block that we're 9616 06:00:21,680 --> 06:00:24,400 filling that is the content block right 9617 06:00:24,400 --> 06:00:26,000 and we close it here 9618 06:00:26,000 --> 06:00:28,718 so now all this markup is going to go 9619 06:00:28,718 --> 06:00:30,400 in the content block 9620 06:00:30,400 --> 06:00:32,558 now finally we need to close this block 9621 06:00:32,558 --> 06:00:35,520 so after the last line 9622 06:00:35,520 --> 06:00:38,160 let's add another template tag 9623 06:00:38,160 --> 06:00:40,080 and block 9624 06:00:40,080 --> 06:00:42,080 now to make this code cleaner i would 9625 06:00:42,080 --> 06:00:44,718 prefer to select these few lines that 9626 06:00:44,718 --> 06:00:47,600 belong to the content block and 9627 06:00:47,600 --> 06:00:49,760 indent them like this this is not 9628 06:00:49,760 --> 06:00:51,600 required but it just makes our code 9629 06:00:51,600 --> 06:00:53,200 easier to read 9630 06:00:53,200 --> 06:00:54,958 so with these changes that we made in 9631 06:00:54,958 --> 06:00:57,040 this tutorial when django tries to 9632 06:00:57,040 --> 06:00:58,400 render this page 9633 06:00:58,400 --> 06:01:01,360 first it will look at base.html so it 9634 06:01:01,360 --> 06:01:03,680 will take all the markup defined in that 9635 06:01:03,680 --> 06:01:06,080 file and then it's going to fill 9636 06:01:06,080 --> 06:01:08,400 this content block with the markup we 9637 06:01:08,400 --> 06:01:10,240 have defined here 9638 06:01:10,240 --> 06:01:12,160 now if we go back to our browser 9639 06:01:12,160 --> 06:01:13,360 and 9640 06:01:13,360 --> 06:01:14,798 refresh this page we're going to see 9641 06:01:14,798 --> 06:01:18,160 something a little bit better so refresh 9642 06:01:18,160 --> 06:01:19,920 you can see the fonts are changed and 9643 06:01:19,920 --> 06:01:21,600 there is a little bit more space in 9644 06:01:21,600 --> 06:01:23,920 between the bullet points 9645 06:01:23,920 --> 06:01:26,000 now you're ready to replace each bullet 9646 06:01:26,000 --> 06:01:28,320 point with a bootstrap card and that's 9647 06:01:28,320 --> 06:01:31,440 what i'm going to show you next 9648 06:01:34,878 --> 06:01:36,240 in this tutorial i'm going to show you 9649 06:01:36,240 --> 06:01:38,558 how to display these beautiful cards on 9650 06:01:38,558 --> 06:01:40,480 our products page now note that this 9651 06:01:40,480 --> 06:01:43,120 page is responsive so if you view it on 9652 06:01:43,120 --> 06:01:45,360 a small mobile device we're going to 9653 06:01:45,360 --> 06:01:47,680 have one card on each row 9654 06:01:47,680 --> 06:01:49,680 like this 9655 06:01:49,680 --> 06:01:51,680 and if you view this on a bigger screen 9656 06:01:51,680 --> 06:01:54,320 we're going to have more columns or more 9657 06:01:54,320 --> 06:01:56,958 cards on each row so let me show you how 9658 06:01:56,958 --> 06:01:58,240 to build this 9659 06:01:58,240 --> 06:02:02,320 first head over to get bootstrap.com 9660 06:02:02,320 --> 06:02:04,718 click on the documentation 9661 06:02:04,718 --> 06:02:06,320 here on the left side you can see a link 9662 06:02:06,320 --> 06:02:08,400 called components 9663 06:02:08,400 --> 06:02:09,760 on this page you can see all the 9664 06:02:09,760 --> 06:02:12,080 components that are defined in bootstrap 9665 06:02:12,080 --> 06:02:13,520 these are the components that we can add 9666 06:02:13,520 --> 06:02:15,600 to our web pages to make them beautiful 9667 06:02:15,600 --> 06:02:16,878 and modern 9668 06:02:16,878 --> 06:02:19,200 so the component that we're going to use 9669 06:02:19,200 --> 06:02:22,638 is called a card 9670 06:02:23,280 --> 06:02:24,878 over here you can see 9671 06:02:24,878 --> 06:02:26,638 an example of a card 9672 06:02:26,638 --> 06:02:28,240 and right below that you can see the 9673 06:02:28,240 --> 06:02:30,718 markup that we need to use to render 9674 06:02:30,718 --> 06:02:32,160 something like this 9675 06:02:32,160 --> 06:02:33,280 so 9676 06:02:33,280 --> 06:02:36,000 let me resize this screen okay on the 9677 06:02:36,000 --> 06:02:38,480 top right corner we have this button 9678 06:02:38,480 --> 06:02:40,480 copy this markup 9679 06:02:40,480 --> 06:02:42,718 now back to index.html 9680 06:02:42,718 --> 06:02:45,120 instead of rendering a ul and a bunch of 9681 06:02:45,120 --> 06:02:48,080 lis we want to render a bunch of divs 9682 06:02:48,080 --> 06:02:50,480 because with ul and li we'll get bullet 9683 06:02:50,480 --> 06:02:52,000 points but we don't want to display 9684 06:02:52,000 --> 06:02:53,440 bullet points here 9685 06:02:53,440 --> 06:02:55,040 so 9686 06:02:55,040 --> 06:02:57,360 right below the h1 tag 9687 06:02:57,360 --> 06:03:02,000 type out div dot row and then press tab 9688 06:03:02,000 --> 06:03:06,080 so this creates a div with the class row 9689 06:03:06,080 --> 06:03:08,000 now in this row we want to repeat 9690 06:03:08,000 --> 06:03:11,600 multiple columns so type out div 9691 06:03:11,600 --> 06:03:12,558 dot 9692 06:03:12,558 --> 06:03:14,240 call as in short for column and then 9693 06:03:14,240 --> 06:03:16,240 press tab 9694 06:03:16,240 --> 06:03:19,360 so we use this divs to create a layout 9695 06:03:19,360 --> 06:03:21,280 like this we have multiple columns in a 9696 06:03:21,280 --> 06:03:22,080 row 9697 06:03:22,080 --> 06:03:23,200 okay 9698 06:03:23,200 --> 06:03:25,280 now inside of a column 9699 06:03:25,280 --> 06:03:27,600 we want to render a card so now we 9700 06:03:27,600 --> 06:03:29,520 should paste the markup for rendering a 9701 06:03:29,520 --> 06:03:31,120 bootstrap card 9702 06:03:31,120 --> 06:03:32,718 so paste that here 9703 06:03:32,718 --> 06:03:34,160 now we can see our code looks a little 9704 06:03:34,160 --> 06:03:36,320 bit ugly because it's not indented 9705 06:03:36,320 --> 06:03:38,718 properly so on the top go to the code 9706 06:03:38,718 --> 06:03:39,920 menu 9707 06:03:39,920 --> 06:03:42,718 here we have this item reformat code and 9708 06:03:42,718 --> 06:03:44,558 look at the shortcut that is associated 9709 06:03:44,558 --> 06:03:47,200 with this command so let's click that 9710 06:03:47,200 --> 06:03:49,280 now our code is beautifully formatted 9711 06:03:49,280 --> 06:03:52,400 and it's easier to read and modify 9712 06:03:52,400 --> 06:03:54,638 so we have a row inside this row we have 9713 06:03:54,638 --> 06:03:56,638 a column and inside this column we have 9714 06:03:56,638 --> 06:03:58,000 a card 9715 06:03:58,000 --> 06:04:00,558 now we're going to take our for loop and 9716 06:04:00,558 --> 06:04:02,798 put it above this column 9717 06:04:02,798 --> 06:04:05,200 so for each product we want to render a 9718 06:04:05,200 --> 06:04:06,558 column 9719 06:04:06,558 --> 06:04:09,040 so scroll down 9720 06:04:09,040 --> 06:04:10,080 and 9721 06:04:10,080 --> 06:04:12,558 grab this line of code cut it 9722 06:04:12,558 --> 06:04:13,600 and then 9723 06:04:13,600 --> 06:04:15,600 paste it right above 9724 06:04:15,600 --> 06:04:17,360 this column 9725 06:04:17,360 --> 06:04:19,120 now we should close this for loop so 9726 06:04:19,120 --> 06:04:22,320 let's collapse this div below it 9727 06:04:22,320 --> 06:04:24,558 okay now right after this div we want to 9728 06:04:24,558 --> 06:04:26,878 add a template tag 9729 06:04:26,878 --> 06:04:29,440 and type out and four 9730 06:04:29,440 --> 06:04:31,360 with these changes we don't need the 9731 06:04:31,360 --> 06:04:33,920 code from before we don't need the ul 9732 06:04:33,920 --> 06:04:38,240 and li tags so let's delete all of these 9733 06:04:38,240 --> 06:04:40,638 so this is the final result 9734 06:04:40,638 --> 06:04:43,600 inside the content block we should have 9735 06:04:43,600 --> 06:04:44,878 these tags 9736 06:04:44,878 --> 06:04:47,040 we have an h1 on the top for the heading 9737 06:04:47,040 --> 06:04:49,200 below that we have a row 9738 06:04:49,200 --> 06:04:51,360 then we have our for loop and in each 9739 06:04:51,360 --> 06:04:54,638 iteration we render a column so let's 9740 06:04:54,638 --> 06:04:57,440 also indent this line to go inside the 9741 06:04:57,440 --> 06:04:59,760 for loop that's better and finally we 9742 06:04:59,760 --> 06:05:02,160 close our for loop and after that we 9743 06:05:02,160 --> 06:05:03,840 close our row 9744 06:05:03,840 --> 06:05:05,600 with this change if we go back to the 9745 06:05:05,600 --> 06:05:06,638 browser 9746 06:05:06,638 --> 06:05:08,160 and refresh 9747 06:05:08,160 --> 06:05:09,760 we get something like this we're 9748 06:05:09,760 --> 06:05:12,558 basically repeating all that static or 9749 06:05:12,558 --> 06:05:15,760 fixed markup from bootstrap over here 9750 06:05:15,760 --> 06:05:17,600 so we're not rendering the actual name 9751 06:05:17,600 --> 06:05:19,600 or the image of each product but that is 9752 06:05:19,600 --> 06:05:20,958 a very easy fix 9753 06:05:20,958 --> 06:05:23,600 so back to our markup let's expand this 9754 06:05:23,600 --> 06:05:25,120 column 9755 06:05:25,120 --> 06:05:27,760 so here's our card 9756 06:05:27,760 --> 06:05:30,080 inside this card we have an image on 9757 06:05:30,080 --> 06:05:32,718 line nine look at the source attribute 9758 06:05:32,718 --> 06:05:34,160 of this image 9759 06:05:34,160 --> 06:05:36,240 we need to change this 9760 06:05:36,240 --> 06:05:39,360 and dynamically render the image url 9761 06:05:39,360 --> 06:05:41,120 attribute of each product so let's 9762 06:05:41,120 --> 06:05:42,320 delete this 9763 06:05:42,320 --> 06:05:44,798 and inside the quotes we add double 9764 06:05:44,798 --> 06:05:47,200 curly braces to render some value we 9765 06:05:47,200 --> 06:05:50,558 want to render product dot image 9766 06:05:50,558 --> 06:05:52,638 underline url 9767 06:05:52,638 --> 06:05:53,680 like this 9768 06:05:53,680 --> 06:05:56,480 now over here on line 11 we have this 9769 06:05:56,480 --> 06:05:59,040 text card title we want to replace this 9770 06:05:59,040 --> 06:06:01,920 with the name of each product so delete 9771 06:06:01,920 --> 06:06:05,440 double credit braces and we add product 9772 06:06:05,440 --> 06:06:07,280 dot name 9773 06:06:07,280 --> 06:06:09,440 next we need to replace this text 9774 06:06:09,440 --> 06:06:12,320 with the price of each product so delete 9775 06:06:12,320 --> 06:06:14,558 here we have a paragraph tag inside this 9776 06:06:14,558 --> 06:06:17,680 tag we add double curly braces to render 9777 06:06:17,680 --> 06:06:20,240 product dot price and just before these 9778 06:06:20,240 --> 06:06:22,958 braces we want to add a dollar sign 9779 06:06:22,958 --> 06:06:25,360 and the last change 9780 06:06:25,360 --> 06:06:28,080 is the label for this button 9781 06:06:28,080 --> 06:06:29,600 it's called go somewhere we want to 9782 06:06:29,600 --> 06:06:33,120 change that to add to cart 9783 06:06:33,120 --> 06:06:35,600 now back to this page refresh we should 9784 06:06:35,600 --> 06:06:39,280 get something like this beautiful 9785 06:06:43,040 --> 06:06:44,480 in this tutorial i'm going to show you a 9786 06:06:44,480 --> 06:06:46,400 couple of techniques for improving the 9787 06:06:46,400 --> 06:06:48,400 look and feel of this page first we're 9788 06:06:48,400 --> 06:06:50,798 going to add a navigation bar on the top 9789 06:06:50,798 --> 06:06:52,400 and then we're gonna add some padding 9790 06:06:52,400 --> 06:06:54,320 around with elements currently this 9791 06:06:54,320 --> 06:06:56,718 product card is way too close to the 9792 06:06:56,718 --> 06:06:58,718 left side of the screen so we should add 9793 06:06:58,718 --> 06:07:01,440 some padding to let this page breathe 9794 06:07:01,440 --> 06:07:03,760 now to add a navigation bar 9795 06:07:03,760 --> 06:07:05,200 once again we go to the bootstrap 9796 06:07:05,200 --> 06:07:08,558 website look at the documentation for 9797 06:07:08,558 --> 06:07:11,200 the navbar component so here's the 9798 06:07:11,200 --> 06:07:12,718 navbar 9799 06:07:12,718 --> 06:07:14,638 now if you scroll down 9800 06:07:14,638 --> 06:07:16,958 you can see a typical nav bar so we can 9801 06:07:16,958 --> 06:07:19,120 have these links we could have a drop 9802 06:07:19,120 --> 06:07:21,520 down we could have a search bar now this 9803 06:07:21,520 --> 06:07:23,120 looks a little bit clunky because i've 9804 06:07:23,120 --> 06:07:25,120 resized the screen so it doesn't look 9805 06:07:25,120 --> 06:07:27,520 pretty but below that you can see 9806 06:07:27,520 --> 06:07:28,638 another 9807 06:07:28,638 --> 06:07:31,200 simple navigation bar with only the 9808 06:07:31,200 --> 06:07:33,920 brand or the name of the application 9809 06:07:33,920 --> 06:07:35,520 so over here we have the markup to 9810 06:07:35,520 --> 06:07:37,680 render this navigation bar so let's go 9811 06:07:37,680 --> 06:07:40,080 ahead and copy this 9812 06:07:40,080 --> 06:07:42,240 now back to pycharm where should we add 9813 06:07:42,240 --> 06:07:43,520 this markup 9814 06:07:43,520 --> 06:07:45,920 we should add it to base.html because 9815 06:07:45,920 --> 06:07:47,760 this is supposed to be a base template 9816 06:07:47,760 --> 06:07:50,400 for all pages in our application 9817 06:07:50,400 --> 06:07:51,920 so 9818 06:07:51,920 --> 06:07:54,798 here's the opening body tag right below 9819 06:07:54,798 --> 06:07:57,040 this body tag we want to paste the 9820 06:07:57,040 --> 06:07:59,280 markup for rendering a navigation bar 9821 06:07:59,280 --> 06:08:01,120 now the code is not formatted properly 9822 06:08:01,120 --> 06:08:03,200 it looks clunky so once again go to the 9823 06:08:03,200 --> 06:08:06,718 code menu and then reformat code as you 9824 06:08:06,718 --> 06:08:09,280 can see the shortcut for mac is alt 9825 06:08:09,280 --> 06:08:11,120 command and l so i'm going to use a 9826 06:08:11,120 --> 06:08:12,558 shortcut here 9827 06:08:12,558 --> 06:08:14,480 there you go that is better 9828 06:08:14,480 --> 06:08:16,400 so here's our navigation bar 9829 06:08:16,400 --> 06:08:18,558 now let's change this text from navbar 9830 06:08:18,558 --> 06:08:19,520 to 9831 06:08:19,520 --> 06:08:21,840 pi shop that's the name of our 9832 06:08:21,840 --> 06:08:23,440 application right 9833 06:08:23,440 --> 06:08:25,120 now back to the browser 9834 06:08:25,120 --> 06:08:28,400 let's refresh this page we have this 9835 06:08:28,400 --> 06:08:30,558 navigation bar on the top let's go back 9836 06:08:30,558 --> 06:08:32,638 to pycharm and make a slight change in 9837 06:08:32,638 --> 06:08:35,280 the structure of our application 9838 06:08:35,280 --> 06:08:37,360 so here in the products app we created 9839 06:08:37,360 --> 06:08:39,200 the templates folder and in this folder 9840 06:08:39,200 --> 06:08:41,520 currently we have two templates the base 9841 06:08:41,520 --> 06:08:43,360 template for our application and the 9842 06:08:43,360 --> 06:08:45,520 template for the products page the 9843 06:08:45,520 --> 06:08:47,600 problem with this structure is that this 9844 06:08:47,600 --> 06:08:50,638 base html is not reusable because we 9845 06:08:50,638 --> 06:08:53,440 have added it inside the template of the 9846 06:08:53,440 --> 06:08:55,280 products app what if in the future we 9847 06:08:55,280 --> 06:08:57,280 want to add another app to this project 9848 06:08:57,280 --> 06:08:59,680 we don't want to recreate this base.html 9849 06:08:59,680 --> 06:09:01,360 in every app that just doesn't make 9850 06:09:01,360 --> 06:09:04,240 sense so we want to reuse this across 9851 06:09:04,240 --> 06:09:06,320 multiple apps and that's why we need to 9852 06:09:06,320 --> 06:09:08,480 move this outside of this folder and put 9853 06:09:08,480 --> 06:09:11,520 it somewhere central so 9854 06:09:11,520 --> 06:09:13,520 in the root of our project 9855 06:09:13,520 --> 06:09:15,360 here we need to add a new folder called 9856 06:09:15,360 --> 06:09:18,080 templates so right click the project and 9857 06:09:18,080 --> 06:09:18,958 add 9858 06:09:18,958 --> 06:09:20,638 a new directory 9859 06:09:20,638 --> 06:09:23,200 called templates 9860 06:09:23,200 --> 06:09:24,878 now we want to go back to the products 9861 06:09:24,878 --> 06:09:25,760 app 9862 06:09:25,760 --> 06:09:28,160 inside the templates folder 9863 06:09:28,160 --> 06:09:31,680 we drag this base.html file and drop it 9864 06:09:31,680 --> 06:09:34,480 into the main templates folder 9865 06:09:34,480 --> 06:09:36,718 okay 9866 06:09:36,840 --> 06:09:39,360 good now our base template is somewhere 9867 06:09:39,360 --> 06:09:41,840 central so we can reuse it in multiple 9868 06:09:41,840 --> 06:09:42,878 apps 9869 06:09:42,878 --> 06:09:44,958 however if we go back to the browser and 9870 06:09:44,958 --> 06:09:47,280 refresh this page we get this error 9871 06:09:47,280 --> 06:09:49,760 template does not exist 9872 06:09:49,760 --> 06:09:51,840 if you scroll down you can see why we 9873 06:09:51,840 --> 06:09:53,360 get this error 9874 06:09:53,360 --> 06:09:55,600 so over here you can see this red line 9875 06:09:55,600 --> 06:09:57,760 let me zoom in 9876 06:09:57,760 --> 06:09:59,760 this error happens because django 9877 06:09:59,760 --> 06:10:01,240 couldn't load 9878 06:10:01,240 --> 06:10:02,798 base.html 9879 06:10:02,798 --> 06:10:04,718 now if you scroll up 9880 06:10:04,718 --> 06:10:07,520 you can see the paths that django 9881 06:10:07,520 --> 06:10:09,680 searches for to find this template 9882 06:10:09,680 --> 06:10:11,200 here's the first path 9883 06:10:11,200 --> 06:10:14,878 admin slash templates slash base.html so 9884 06:10:14,878 --> 06:10:16,798 django is looking for this template 9885 06:10:16,798 --> 06:10:18,798 inside the admin app 9886 06:10:18,798 --> 06:10:21,120 here's the second path aus slash 9887 06:10:21,120 --> 06:10:24,000 templates slash base.html so django 9888 06:10:24,000 --> 06:10:25,680 couldn't find it in the admin app it 9889 06:10:25,680 --> 06:10:28,000 thinks maybe it's in the us app and 9890 06:10:28,000 --> 06:10:30,480 finally here's the last pass products 9891 06:10:30,480 --> 06:10:33,440 slash templates slash base.html that is 9892 06:10:33,440 --> 06:10:35,600 our products app so what is going on 9893 06:10:35,600 --> 06:10:37,920 here django is looking for this template 9894 06:10:37,920 --> 06:10:40,798 only inside of the installed apps 9895 06:10:40,798 --> 06:10:42,320 but this templates folder that we 9896 06:10:42,320 --> 06:10:44,240 created it's not really part of any apps 9897 06:10:44,240 --> 06:10:46,558 it's just a standalone folder 9898 06:10:46,558 --> 06:10:49,040 so we need to tell django to look inside 9899 06:10:49,040 --> 06:10:51,280 this folder in addition to the installed 9900 06:10:51,280 --> 06:10:53,360 apps and that is very easy 9901 06:10:53,360 --> 06:10:54,480 so 9902 06:10:54,480 --> 06:10:57,680 in the main package pi shop 9903 06:10:57,680 --> 06:10:59,760 you have seen the settings file before 9904 06:10:59,760 --> 06:11:02,160 let's have another look here if you 9905 06:11:02,160 --> 06:11:04,480 scroll up 9906 06:11:04,480 --> 06:11:07,840 you can see this variable base directory 9907 06:11:07,840 --> 06:11:09,680 this variable is set to the complete 9908 06:11:09,680 --> 06:11:11,600 path of where you have stored this 9909 06:11:11,600 --> 06:11:13,920 django project on your disk 9910 06:11:13,920 --> 06:11:15,520 we're going to take this directory and 9911 06:11:15,520 --> 06:11:17,440 append templates to it 9912 06:11:17,440 --> 06:11:18,480 so 9913 06:11:18,480 --> 06:11:20,638 scroll down 9914 06:11:20,638 --> 06:11:24,080 on line 55 you can see the configuration 9915 06:11:24,080 --> 06:11:26,160 for loading templates 9916 06:11:26,160 --> 06:11:27,680 look at the settings we have here the 9917 06:11:27,680 --> 06:11:29,520 first setting is back end and this is 9918 06:11:29,520 --> 06:11:31,200 the engine that is used to parse our 9919 06:11:31,200 --> 06:11:33,680 templates so all those template tags as 9920 06:11:33,680 --> 06:11:35,760 well as the double curly braces this is 9921 06:11:35,760 --> 06:11:37,120 the engine 9922 06:11:37,120 --> 06:11:39,840 that knows how to interpret those 9923 06:11:39,840 --> 06:11:41,760 look at the second setting 9924 06:11:41,760 --> 06:11:44,400 directories this is set to an empty list 9925 06:11:44,400 --> 06:11:45,680 so here we need to reference the 9926 06:11:45,680 --> 06:11:48,240 templates folder in this project 9927 06:11:48,240 --> 06:11:50,400 so let's type out os 9928 06:11:50,400 --> 06:11:53,120 dot path so we're using the path object 9929 06:11:53,120 --> 06:11:55,840 defined in the os or operating system 9930 06:11:55,840 --> 06:11:58,080 module that is important on the top now 9931 06:11:58,080 --> 06:11:59,840 this path object has a method called 9932 06:11:59,840 --> 06:12:02,320 join and with this we can join multiple 9933 06:12:02,320 --> 06:12:03,760 parts of a path 9934 06:12:03,760 --> 06:12:05,200 the first part 9935 06:12:05,200 --> 06:12:07,600 is base underlined 9936 06:12:07,600 --> 06:12:09,040 which is the variable that i showed you 9937 06:12:09,040 --> 06:12:11,040 earlier it's set to the complete path of 9938 06:12:11,040 --> 06:12:12,878 your django project 9939 06:12:12,878 --> 06:12:14,958 now we want to take this and append 9940 06:12:14,958 --> 06:12:16,400 templates to it 9941 06:12:16,400 --> 06:12:19,200 that's all you have to do so back to the 9942 06:12:19,200 --> 06:12:21,920 browser let's refresh the page there you 9943 06:12:21,920 --> 06:12:24,480 go the problem is fixed so now our base 9944 06:12:24,480 --> 06:12:26,240 template is somewhere central and we can 9945 06:12:26,240 --> 06:12:29,440 reuse it across multiple apps beautiful 9946 06:12:29,440 --> 06:12:31,440 and one last change 9947 06:12:31,440 --> 06:12:33,840 currently as you see these items are so 9948 06:12:33,840 --> 06:12:36,320 close to the left side of the screen so 9949 06:12:36,320 --> 06:12:37,840 i'm going to add some padding to make 9950 06:12:37,840 --> 06:12:40,240 this page breathe and that is very easy 9951 06:12:40,240 --> 06:12:41,840 back to pycharm 9952 06:12:41,840 --> 06:12:43,760 here in our base template 9953 06:12:43,760 --> 06:12:45,200 where we define 9954 06:12:45,200 --> 06:12:47,840 this content block we need to wrap this 9955 06:12:47,840 --> 06:12:50,798 with a div with the class container this 9956 06:12:50,798 --> 06:12:52,160 is one of the classes defined in 9957 06:12:52,160 --> 06:12:54,160 bootstrap that we use to define a 9958 06:12:54,160 --> 06:12:56,240 container area in our application this 9959 06:12:56,240 --> 06:12:59,360 container area has some padding so type 9960 06:12:59,360 --> 06:13:02,558 out div dot container and press tab okay 9961 06:13:02,558 --> 06:13:06,080 and then move these template tags inside 9962 06:13:06,080 --> 06:13:07,520 of this div 9963 06:13:07,520 --> 06:13:09,760 that's it now back to the browser let's 9964 06:13:09,760 --> 06:13:11,040 refresh 9965 06:13:11,040 --> 06:13:13,040 so this is our container area as you can 9966 06:13:13,040 --> 06:13:15,440 see we have some padding to the left and 9967 06:13:15,440 --> 06:13:17,040 right side of this container area 9968 06:13:17,040 --> 06:13:18,240 beautiful 9969 06:13:18,240 --> 06:13:19,600 so that brings us to the end of this 9970 06:13:19,600 --> 06:13:22,080 django tutorial in this python course i 9971 06:13:22,080 --> 06:13:23,600 hope you learned a lot and thank you for 9972 06:13:23,600 --> 06:13:26,000 watching 9973 06:13:26,320 --> 06:13:27,840 so that was a short and sweet 9974 06:13:27,840 --> 06:13:29,840 introduction to django but django really 9975 06:13:29,840 --> 06:13:32,080 requires his own course and i'm planning 9976 06:13:32,080 --> 06:13:33,840 to create a comprehensive course on the 9977 06:13:33,840 --> 06:13:35,840 topic in the near future now here's a 9978 06:13:35,840 --> 06:13:37,040 question for you 9979 06:13:37,040 --> 06:13:38,480 what kind of applications do you want to 9980 06:13:38,480 --> 06:13:40,638 build with django use the comment box 9981 06:13:40,638 --> 06:13:42,240 below and let me know i will take your 9982 06:13:42,240 --> 06:13:44,080 ideas into consideration as part of 9983 06:13:44,080 --> 06:13:46,480 creating my upcoming django course 9984 06:13:46,480 --> 06:13:48,240 so that brings us to the end of this 9985 06:13:48,240 --> 06:13:50,160 course i hope you learned a lot please 9986 06:13:50,160 --> 06:13:52,000 support my hard work by liking this 9987 06:13:52,000 --> 06:13:54,160 video and sharing it with others and 9988 06:13:54,160 --> 06:13:55,920 also subscribe to my channel for more 9989 06:13:55,920 --> 06:13:58,000 tutorials like this thank you so much 9990 06:13:58,000 --> 06:13:59,600 for watching and i hope to see you again 9991 06:13:59,600 --> 06:14:02,230 soon 9992 06:14:02,230 --> 06:14:06,638 [Music] 9993 06:14:06,638 --> 06:14:08,718 you678740

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.