All language subtitles for Python Full Course for free (1080P_HD)(en)

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
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,160 --> 00:00:03,360 hey what's going on everybody it's bro 2 00:00:02,399 --> 00:00:04,799 hope you're doing well 3 00:00:03,360 --> 00:00:06,560 and in this video i'm going to help you 4 00:00:04,799 --> 00:00:11,280 get started with python so 5 00:00:06,560 --> 00:00:11,280 sit back relax and enjoy the show 6 00:00:11,360 --> 00:00:14,880 if you wouldn't mind please like comment 7 00:00:13,920 --> 00:00:16,880 and subscribe 8 00:00:14,880 --> 00:00:18,400 one like equals one prayer for the 9 00:00:16,880 --> 00:00:20,320 youtube algorithm 10 00:00:18,400 --> 00:00:22,640 i'm gonna tell you why you need to learn 11 00:00:20,320 --> 00:00:24,480 python it's the easiest programming 12 00:00:22,640 --> 00:00:26,560 language to learn as well as being the 13 00:00:24,480 --> 00:00:28,800 most popular in the world right now 14 00:00:26,560 --> 00:00:31,199 and according to glassdoor the salary 15 00:00:28,800 --> 00:00:32,160 for new python developers in the united 16 00:00:31,199 --> 00:00:35,440 states is 17 00:00:32,160 --> 00:00:36,079 64 000 if any of this sounds good to you 18 00:00:35,440 --> 00:00:37,840 well then 19 00:00:36,079 --> 00:00:39,719 let's get started if you need to 20 00:00:37,840 --> 00:00:42,480 download python go to 21 00:00:39,719 --> 00:00:44,000 python.org downloads and click this 22 00:00:42,480 --> 00:00:47,440 yellow download python 23 00:00:44,000 --> 00:00:51,440 button open when done check add 24 00:00:47,440 --> 00:00:53,840 python 3.9 or another version to path 25 00:00:51,440 --> 00:00:56,000 install now and then give it a little 26 00:00:53,840 --> 00:00:58,320 bit it states here that setup was 27 00:00:56,000 --> 00:01:00,879 successful so let's close out of this 28 00:00:58,320 --> 00:01:02,239 our next step is to download an ide 29 00:01:00,879 --> 00:01:04,400 that's an acronym for 30 00:01:02,239 --> 00:01:05,280 integrated development environment think 31 00:01:04,400 --> 00:01:07,040 of it as 32 00:01:05,280 --> 00:01:09,680 software that helps us write other 33 00:01:07,040 --> 00:01:11,799 software one ide that i recommend 34 00:01:09,680 --> 00:01:13,600 is pycharm you can find it at 35 00:01:11,799 --> 00:01:17,040 jetbrains.com 36 00:01:13,600 --> 00:01:19,200 pycharm go to download if you would like 37 00:01:17,040 --> 00:01:19,920 to pay for a professional version you 38 00:01:19,200 --> 00:01:21,920 can click 39 00:01:19,920 --> 00:01:23,680 this download button but since i don't 40 00:01:21,920 --> 00:01:26,799 like to pay for things i'm going to use 41 00:01:23,680 --> 00:01:28,560 the free community version at this point 42 00:01:26,799 --> 00:01:29,920 we just have to follow the standard 43 00:01:28,560 --> 00:01:32,320 installation procedure 44 00:01:29,920 --> 00:01:34,720 click next you can pick a destination 45 00:01:32,320 --> 00:01:37,119 folder but i'll keep the default 46 00:01:34,720 --> 00:01:38,079 next if you would like a desktop 47 00:01:37,119 --> 00:01:40,640 shortcut 48 00:01:38,079 --> 00:01:41,439 then check this i would also recommend 49 00:01:40,640 --> 00:01:44,880 updating the 50 00:01:41,439 --> 00:01:48,079 path variable next 51 00:01:44,880 --> 00:01:50,399 install and then give it a minute or two 52 00:01:48,079 --> 00:01:52,159 then after finishing i have a desktop 53 00:01:50,399 --> 00:01:53,520 shortcut so i can just click on this to 54 00:01:52,159 --> 00:01:56,640 run pycharm 55 00:01:53,520 --> 00:01:58,560 let's create a new project name this 56 00:01:56,640 --> 00:01:59,759 project whatever you like i'm going to 57 00:01:58,560 --> 00:02:03,280 name this project 58 00:01:59,759 --> 00:02:03,680 hello world and i recommend not creating 59 00:02:03,280 --> 00:02:05,920 a 60 00:02:03,680 --> 00:02:07,920 main dot pi welcome script right now 61 00:02:05,920 --> 00:02:09,920 i'll show you how to do that manually 62 00:02:07,920 --> 00:02:12,319 and let's create 63 00:02:09,920 --> 00:02:14,000 if you need a python file to work with 64 00:02:12,319 --> 00:02:17,760 this is how to create one go to 65 00:02:14,000 --> 00:02:18,160 file new python file and i'm going to 66 00:02:17,760 --> 00:02:23,200 name 67 00:02:18,160 --> 00:02:25,120 this main and then click python file 68 00:02:23,200 --> 00:02:27,120 all right ladies and gentlemen we now 69 00:02:25,120 --> 00:02:28,720 have an empty python script that we can 70 00:02:27,120 --> 00:02:31,040 use to begin coding 71 00:02:28,720 --> 00:02:33,440 now if you checked this box create a 72 00:02:31,040 --> 00:02:34,640 main.pi welcome script and then created 73 00:02:33,440 --> 00:02:37,200 a new project 74 00:02:34,640 --> 00:02:38,879 then your main python file will probably 75 00:02:37,200 --> 00:02:40,400 look a little something like this but 76 00:02:38,879 --> 00:02:41,920 that's completely fine we can just 77 00:02:40,400 --> 00:02:43,840 delete this 78 00:02:41,920 --> 00:02:45,360 i don't know about you guys but the font 79 00:02:43,840 --> 00:02:47,360 is very small for me 80 00:02:45,360 --> 00:02:49,360 so let's change that we can change that 81 00:02:47,360 --> 00:02:52,560 by going to file 82 00:02:49,360 --> 00:02:54,000 settings editor font and you can 83 00:02:52,560 --> 00:02:56,480 increase the font size 84 00:02:54,000 --> 00:02:57,680 let's say 25 you can also change the 85 00:02:56,480 --> 00:03:01,200 font too 86 00:02:57,680 --> 00:03:04,239 what's a good one uh how about 87 00:03:01,200 --> 00:03:06,879 that then click apply and 88 00:03:04,239 --> 00:03:07,360 okay much better we can actually see 89 00:03:06,879 --> 00:03:09,519 something 90 00:03:07,360 --> 00:03:11,200 to run a python script just click the 91 00:03:09,519 --> 00:03:13,680 screen play button in the corner 92 00:03:11,200 --> 00:03:15,360 alternatively you can go to the run tab 93 00:03:13,680 --> 00:03:17,280 and then go to run 94 00:03:15,360 --> 00:03:20,000 okay so this box that appeared at the 95 00:03:17,280 --> 00:03:21,440 bottom this is referred to as a terminal 96 00:03:20,000 --> 00:03:23,840 window this will display 97 00:03:21,440 --> 00:03:25,200 any output that your program has and 98 00:03:23,840 --> 00:03:27,519 this line at the end 99 00:03:25,200 --> 00:03:29,120 process finished with exit code zero 100 00:03:27,519 --> 00:03:30,560 that just means that there were no 101 00:03:29,120 --> 00:03:32,319 errors in this program 102 00:03:30,560 --> 00:03:34,080 so currently this program doesn't do 103 00:03:32,319 --> 00:03:35,200 anything but let's change that let's 104 00:03:34,080 --> 00:03:37,680 print something 105 00:03:35,200 --> 00:03:39,760 let's print a message so to print a 106 00:03:37,680 --> 00:03:42,080 message to the terminal window 107 00:03:39,760 --> 00:03:42,959 type print and then add a set of 108 00:03:42,080 --> 00:03:44,720 parentheses 109 00:03:42,959 --> 00:03:46,000 and then you can type a message but it 110 00:03:44,720 --> 00:03:48,959 has to be within quotes 111 00:03:46,000 --> 00:03:49,920 either double quotes or single quotes it 112 00:03:48,959 --> 00:03:52,879 doesn't matter 113 00:03:49,920 --> 00:03:53,599 so let's think of a message i don't know 114 00:03:52,879 --> 00:03:57,519 i 115 00:03:53,599 --> 00:04:00,319 love pizza then let me run this 116 00:03:57,519 --> 00:04:01,120 and in my terminal window it says i love 117 00:04:00,319 --> 00:04:03,439 pizza 118 00:04:01,120 --> 00:04:04,640 to print another line just add another 119 00:04:03,439 --> 00:04:06,799 print statement 120 00:04:04,640 --> 00:04:08,239 print and then your message within 121 00:04:06,799 --> 00:04:12,799 quotes 122 00:04:08,239 --> 00:04:12,799 it's really good 123 00:04:13,519 --> 00:04:17,040 and well what do you know it printed i 124 00:04:16,000 --> 00:04:19,120 love pizza 125 00:04:17,040 --> 00:04:21,759 it's really good so ladies and gentlemen 126 00:04:19,120 --> 00:04:24,080 you just wrote your first python program 127 00:04:21,759 --> 00:04:26,320 by the way you can change the font as 128 00:04:24,080 --> 00:04:27,520 well as the font color of your terminal 129 00:04:26,320 --> 00:04:31,199 window by going to 130 00:04:27,520 --> 00:04:34,240 file settings then go to editor 131 00:04:31,199 --> 00:04:34,960 color scheme console font and console 132 00:04:34,240 --> 00:04:38,160 colors 133 00:04:34,960 --> 00:04:39,120 let's change the font let's say i would 134 00:04:38,160 --> 00:04:43,199 like 135 00:04:39,120 --> 00:04:45,520 whatever this font is then click apply 136 00:04:43,199 --> 00:04:47,280 let's change the font color too by going 137 00:04:45,520 --> 00:04:51,360 to console colors 138 00:04:47,280 --> 00:04:54,320 for any output you can click number two 139 00:04:51,360 --> 00:04:54,800 and select a color let's say i would 140 00:04:54,320 --> 00:04:59,759 like 141 00:04:54,800 --> 00:05:02,160 bright green that should be decent 142 00:04:59,759 --> 00:05:04,400 and for anything else let's say this 143 00:05:02,160 --> 00:05:05,360 will be a dark color so i can barely see 144 00:05:04,400 --> 00:05:08,400 it 145 00:05:05,360 --> 00:05:10,479 so then let's click apply and okay 146 00:05:08,400 --> 00:05:12,400 oh yeah that's much better well 147 00:05:10,479 --> 00:05:14,240 everybody that was your very first 148 00:05:12,400 --> 00:05:16,479 python program i'll post 149 00:05:14,240 --> 00:05:18,880 any useful links and timestamps for this 150 00:05:16,479 --> 00:05:22,800 video in the comments section down below 151 00:05:18,880 --> 00:05:25,199 but yeah welcome to coding with python 152 00:05:22,800 --> 00:05:26,240 hey you yeah i'm talking to you if you 153 00:05:25,199 --> 00:05:28,639 learned something new 154 00:05:26,240 --> 00:05:29,280 then help me help you in three easy 155 00:05:28,639 --> 00:05:31,360 steps 156 00:05:29,280 --> 00:05:32,560 by smashing that like button drop a 157 00:05:31,360 --> 00:05:34,240 comment down below 158 00:05:32,560 --> 00:05:38,640 and subscribe if you'd like to become a 159 00:05:34,240 --> 00:05:56,540 fellow bro 160 00:05:38,640 --> 00:05:56,540 [Music] 161 00:05:56,960 --> 00:06:01,120 hey what's going on everybody it's bro 162 00:05:59,360 --> 00:06:02,400 hope you're doing well and in this video 163 00:06:01,120 --> 00:06:05,280 i'm going to explain how 164 00:06:02,400 --> 00:06:06,400 variables work in python so sit back 165 00:06:05,280 --> 00:06:09,440 relax 166 00:06:06,400 --> 00:06:09,440 and enjoy the show 167 00:06:09,759 --> 00:06:12,800 if you find this video helpful please 168 00:06:11,360 --> 00:06:15,280 remember to like 169 00:06:12,800 --> 00:06:17,120 comment and subscribe your support will 170 00:06:15,280 --> 00:06:20,319 help keep this channel running 171 00:06:17,120 --> 00:06:22,880 all right let's do this a variable is a 172 00:06:20,319 --> 00:06:24,319 container for a value it behaves as the 173 00:06:22,880 --> 00:06:26,720 value that it contains 174 00:06:24,319 --> 00:06:28,560 it's much like in algebra lessons back 175 00:06:26,720 --> 00:06:29,440 in the day where we had to solve for 176 00:06:28,560 --> 00:06:31,919 whatever value 177 00:06:29,440 --> 00:06:34,000 x was and then we can reuse x and it 178 00:06:31,919 --> 00:06:35,759 behaved exactly as that value 179 00:06:34,000 --> 00:06:37,680 well with programming we can do that 180 00:06:35,759 --> 00:06:38,960 plus we can assign variables of 181 00:06:37,680 --> 00:06:40,720 different data types 182 00:06:38,960 --> 00:06:42,800 so they are not limited to just numbers 183 00:06:40,720 --> 00:06:44,639 we can assign whole words 184 00:06:42,800 --> 00:06:46,800 numbers and even these things called 185 00:06:44,639 --> 00:06:48,639 booleans which are either true or false 186 00:06:46,800 --> 00:06:49,840 but i'll get more in depth into data 187 00:06:48,639 --> 00:06:50,479 types a little bit later on in this 188 00:06:49,840 --> 00:06:51,919 video 189 00:06:50,479 --> 00:06:54,000 so let's create a variable and this is 190 00:06:51,919 --> 00:06:54,880 how to do so we need a unique name for 191 00:06:54,000 --> 00:06:57,919 this variable 192 00:06:54,880 --> 00:06:59,680 let's say name name equals some value 193 00:06:57,919 --> 00:07:01,199 now the first data type that we'll cover 194 00:06:59,680 --> 00:07:03,919 is strings a string 195 00:07:01,199 --> 00:07:04,479 is a series of characters so to create a 196 00:07:03,919 --> 00:07:06,720 string 197 00:07:04,479 --> 00:07:08,319 we can either use single quotes or 198 00:07:06,720 --> 00:07:10,479 double quotes in python 199 00:07:08,319 --> 00:07:11,759 and we can assign this a value of 200 00:07:10,479 --> 00:07:14,319 whatever your own name is 201 00:07:11,759 --> 00:07:15,360 so i will assign this variable name a 202 00:07:14,319 --> 00:07:18,000 value of bro 203 00:07:15,360 --> 00:07:18,800 now this variable will behave exactly as 204 00:07:18,000 --> 00:07:20,880 if it was 205 00:07:18,800 --> 00:07:22,960 this value this name and then we can 206 00:07:20,880 --> 00:07:25,840 reuse this variable for something 207 00:07:22,960 --> 00:07:26,560 so let's print whatever our name is 208 00:07:25,840 --> 00:07:28,800 print 209 00:07:26,560 --> 00:07:30,800 name and this will print whatever your 210 00:07:28,800 --> 00:07:32,720 name is to the console window 211 00:07:30,800 --> 00:07:34,479 now when you print your variable make 212 00:07:32,720 --> 00:07:36,479 sure you don't put it within quotes 213 00:07:34,479 --> 00:07:37,840 because what we're doing then is 214 00:07:36,479 --> 00:07:41,199 literally printing 215 00:07:37,840 --> 00:07:42,960 the word name as you can see here so 216 00:07:41,199 --> 00:07:44,400 if you need to use your variable for 217 00:07:42,960 --> 00:07:45,199 something make sure it's not within 218 00:07:44,400 --> 00:07:48,319 quotes 219 00:07:45,199 --> 00:07:50,720 now we can combine our string variable 220 00:07:48,319 --> 00:07:52,000 with another string so within our print 221 00:07:50,720 --> 00:07:55,280 statement let's print 222 00:07:52,000 --> 00:07:58,319 a string literal such as hello space 223 00:07:55,280 --> 00:07:59,840 plus name so we are combining strings 224 00:07:58,319 --> 00:08:02,160 and then we're printing the results to 225 00:07:59,840 --> 00:08:05,120 the console window so this will print 226 00:08:02,160 --> 00:08:06,639 hello bro now you can actually check the 227 00:08:05,120 --> 00:08:08,319 data type of a variable 228 00:08:06,639 --> 00:08:10,319 so i'm going to turn this line into a 229 00:08:08,319 --> 00:08:12,160 comment so what you'll do 230 00:08:10,319 --> 00:08:14,319 is take the name of the variable 231 00:08:12,160 --> 00:08:16,800 surround this with a set of parentheses 232 00:08:14,319 --> 00:08:17,680 and then precede the set of parentheses 233 00:08:16,800 --> 00:08:20,080 with the type 234 00:08:17,680 --> 00:08:21,840 function and this will print the data 235 00:08:20,080 --> 00:08:22,479 type of this variable to the console 236 00:08:21,840 --> 00:08:25,199 window 237 00:08:22,479 --> 00:08:26,160 and you can see that it says class str 238 00:08:25,199 --> 00:08:28,319 short for string 239 00:08:26,160 --> 00:08:30,240 a string is a series of characters a 240 00:08:28,319 --> 00:08:32,959 name is a good example of a string 241 00:08:30,240 --> 00:08:34,640 a series of characters so that is how to 242 00:08:32,959 --> 00:08:37,680 check the data type 243 00:08:34,640 --> 00:08:39,519 of a variable just use the type function 244 00:08:37,680 --> 00:08:41,519 now you can actually combine variables 245 00:08:39,519 --> 00:08:44,480 together as long as they're of the same 246 00:08:41,519 --> 00:08:45,920 data type so let's change our name to 247 00:08:44,480 --> 00:08:47,839 maybe first name and we'll create a 248 00:08:45,920 --> 00:08:49,760 second variable called lastname 249 00:08:47,839 --> 00:08:51,440 now a common naming convention with 250 00:08:49,760 --> 00:08:53,839 variables if it has 251 00:08:51,440 --> 00:08:54,480 two words separate each word with an 252 00:08:53,839 --> 00:08:56,160 underscore 253 00:08:54,480 --> 00:08:58,080 it's not necessary but it's common 254 00:08:56,160 --> 00:08:59,680 practice for python and honestly i 255 00:08:58,080 --> 00:09:00,640 sometimes forget about that but don't 256 00:08:59,680 --> 00:09:02,640 tell anybody 257 00:09:00,640 --> 00:09:03,680 so let's create another variable called 258 00:09:02,640 --> 00:09:07,360 last name last 259 00:09:03,680 --> 00:09:09,519 name equals whatever your last name is 260 00:09:07,360 --> 00:09:10,959 and then let's create a third variable 261 00:09:09,519 --> 00:09:14,160 called full name 262 00:09:10,959 --> 00:09:18,000 full underscore name and let's combine 263 00:09:14,160 --> 00:09:22,080 first name plus last 264 00:09:18,000 --> 00:09:25,120 name and let us display full name 265 00:09:22,080 --> 00:09:26,880 along with hello plus full name 266 00:09:25,120 --> 00:09:28,640 now these two names were combined 267 00:09:26,880 --> 00:09:30,399 together to create a whole 268 00:09:28,640 --> 00:09:32,480 new variable actually what i think i'll 269 00:09:30,399 --> 00:09:35,120 do is add a space 270 00:09:32,480 --> 00:09:37,200 within my variable so i'm doing a bunch 271 00:09:35,120 --> 00:09:39,600 of string concatenation i'm combining 272 00:09:37,200 --> 00:09:40,560 the variable first name plus a space 273 00:09:39,600 --> 00:09:43,040 character plus 274 00:09:40,560 --> 00:09:44,720 last name so the result is hello 275 00:09:43,040 --> 00:09:45,279 whatever your first name and last name 276 00:09:44,720 --> 00:09:48,480 is 277 00:09:45,279 --> 00:09:51,040 so that is a variable of the string data 278 00:09:48,480 --> 00:09:53,360 type it is a series of characters 279 00:09:51,040 --> 00:09:55,200 now with strings we cannot normally use 280 00:09:53,360 --> 00:09:57,279 these for any sort of math 281 00:09:55,200 --> 00:09:59,040 that's where the next data type comes in 282 00:09:57,279 --> 00:10:00,800 for the time being i just turned all of 283 00:09:59,040 --> 00:10:02,399 these lines into comments and we are 284 00:10:00,800 --> 00:10:04,640 going to move on to the int 285 00:10:02,399 --> 00:10:06,160 data type short for integer this time 286 00:10:04,640 --> 00:10:08,640 let's create a variable called 287 00:10:06,160 --> 00:10:09,680 age and we will assign this a value of 288 00:10:08,640 --> 00:10:12,079 whatever your age is 289 00:10:09,680 --> 00:10:12,880 let's just say that i'm 21 now when you 290 00:10:12,079 --> 00:10:15,519 assign 291 00:10:12,880 --> 00:10:16,320 a value of the int data type make sure 292 00:10:15,519 --> 00:10:18,160 that it is 293 00:10:16,320 --> 00:10:19,920 not within quotes because then 294 00:10:18,160 --> 00:10:21,600 technically it would be a string then 295 00:10:19,920 --> 00:10:23,360 and you can see that the color scheme 296 00:10:21,600 --> 00:10:25,839 for this number actually changed 297 00:10:23,360 --> 00:10:26,399 so if this was a string we would treat 298 00:10:25,839 --> 00:10:28,720 it different 299 00:10:26,399 --> 00:10:30,640 than an int data type because with 300 00:10:28,720 --> 00:10:31,680 strings we cannot normally use them for 301 00:10:30,640 --> 00:10:34,399 any sort of math 302 00:10:31,680 --> 00:10:35,920 so we now have a variable called edge 303 00:10:34,399 --> 00:10:38,000 and we can actually 304 00:10:35,920 --> 00:10:38,959 increase or decrease or do whatever we 305 00:10:38,000 --> 00:10:41,040 want with the edge 306 00:10:38,959 --> 00:10:42,880 let's increase our age by one let's say 307 00:10:41,040 --> 00:10:43,600 it's our birthday to do so we would say 308 00:10:42,880 --> 00:10:46,560 age 309 00:10:43,600 --> 00:10:48,399 equals age plus one and then let's print 310 00:10:46,560 --> 00:10:51,200 our edge to the console window 311 00:10:48,399 --> 00:10:52,320 print edge and you can see that it says 312 00:10:51,200 --> 00:10:53,839 i am 22. 313 00:10:52,320 --> 00:10:55,760 now there's a shorthand way of writing 314 00:10:53,839 --> 00:10:58,640 this what you'll do is type 315 00:10:55,760 --> 00:11:00,399 age plus equals one so that's kind of 316 00:10:58,640 --> 00:11:01,040 like a shortcut and this would work the 317 00:11:00,399 --> 00:11:03,200 same too 318 00:11:01,040 --> 00:11:05,040 now let's print the data type of our 319 00:11:03,200 --> 00:11:07,760 inch variable because we can within a 320 00:11:05,040 --> 00:11:10,959 print statement i will print the edge 321 00:11:07,760 --> 00:11:13,120 and add my edge variable to this type 322 00:11:10,959 --> 00:11:15,040 function and this will print the data 323 00:11:13,120 --> 00:11:15,839 type of my edge variable to the console 324 00:11:15,040 --> 00:11:17,839 window 325 00:11:15,839 --> 00:11:19,680 and i will turn this line into a comment 326 00:11:17,839 --> 00:11:21,920 just so that it doesn't get in the way 327 00:11:19,680 --> 00:11:23,040 now if i were to print the data type of 328 00:11:21,920 --> 00:11:25,519 my age variable 329 00:11:23,040 --> 00:11:27,040 as you can see it is of the int data 330 00:11:25,519 --> 00:11:30,720 type short for integer 331 00:11:27,040 --> 00:11:33,519 it only stores a whole integer number 332 00:11:30,720 --> 00:11:34,240 now what if this was a string so what 333 00:11:33,519 --> 00:11:36,880 i'm going to do 334 00:11:34,240 --> 00:11:37,839 is surround my value within a set of 335 00:11:36,880 --> 00:11:39,519 quotes 336 00:11:37,839 --> 00:11:41,040 and i will turn this line into a comment 337 00:11:39,519 --> 00:11:43,040 so that it does not get in the way 338 00:11:41,040 --> 00:11:44,079 you can see that the data type of my age 339 00:11:43,040 --> 00:11:46,240 variable is now 340 00:11:44,079 --> 00:11:48,320 a string and with strings we cannot 341 00:11:46,240 --> 00:11:50,480 normally use them for any sort of math 342 00:11:48,320 --> 00:11:52,000 so let's attempt to increase our age 343 00:11:50,480 --> 00:11:54,959 variable by one 344 00:11:52,000 --> 00:11:56,639 and we ran into an error a type air can 345 00:11:54,959 --> 00:11:59,600 only concatenate string 346 00:11:56,639 --> 00:12:00,000 to string not end now the point being is 347 00:11:59,600 --> 00:12:02,000 that 348 00:12:00,000 --> 00:12:03,519 it's important to use the appropriate 349 00:12:02,000 --> 00:12:05,279 data type because 350 00:12:03,519 --> 00:12:06,959 with strings we cannot normally use them 351 00:12:05,279 --> 00:12:09,279 for any sort of math you'll want to use 352 00:12:06,959 --> 00:12:11,360 a data type of a numeric value 353 00:12:09,279 --> 00:12:13,279 an int or a float which we'll discuss a 354 00:12:11,360 --> 00:12:13,920 little bit later now here's a situation 355 00:12:13,279 --> 00:12:15,680 to consider 356 00:12:13,920 --> 00:12:17,920 what if we would like to display our 357 00:12:15,680 --> 00:12:19,760 edge variable along with a string 358 00:12:17,920 --> 00:12:22,320 literal much like what we did with this 359 00:12:19,760 --> 00:12:22,800 line involving a string literal such as 360 00:12:22,320 --> 00:12:25,040 hello 361 00:12:22,800 --> 00:12:26,800 plus the user's full name so let's 362 00:12:25,040 --> 00:12:28,399 attempt to do so and this is not going 363 00:12:26,800 --> 00:12:30,160 to work and i'll explain why 364 00:12:28,399 --> 00:12:31,760 so within our print statement let's say 365 00:12:30,160 --> 00:12:35,120 something such as 366 00:12:31,760 --> 00:12:38,320 your age is plus 367 00:12:35,120 --> 00:12:38,880 edge now we're going to run into a type 368 00:12:38,320 --> 00:12:40,800 error 369 00:12:38,880 --> 00:12:42,720 that's because we attempted to use 370 00:12:40,800 --> 00:12:43,440 string concatenation with a string 371 00:12:42,720 --> 00:12:46,240 literal 372 00:12:43,440 --> 00:12:47,360 and a variable of a different data type 373 00:12:46,240 --> 00:12:50,399 a variable of the 374 00:12:47,360 --> 00:12:52,320 int data type in order to display our 375 00:12:50,399 --> 00:12:54,480 edge along with the string we would need 376 00:12:52,320 --> 00:12:55,040 to convert our edge variable to the 377 00:12:54,480 --> 00:12:57,040 string 378 00:12:55,040 --> 00:12:59,120 data type and one way in which we can do 379 00:12:57,040 --> 00:13:00,880 that is by type casting 380 00:12:59,120 --> 00:13:02,880 and we'll cover this more in the next 381 00:13:00,880 --> 00:13:05,360 video so we're going to surround 382 00:13:02,880 --> 00:13:07,040 our age variable that is of the in data 383 00:13:05,360 --> 00:13:09,519 type with a set of parentheses 384 00:13:07,040 --> 00:13:11,279 and precede this with str short for 385 00:13:09,519 --> 00:13:12,000 string and this will convert our 386 00:13:11,279 --> 00:13:13,839 variable 387 00:13:12,000 --> 00:13:15,519 to a string that will allow us to 388 00:13:13,839 --> 00:13:17,519 display it with another string 389 00:13:15,519 --> 00:13:19,360 now if we were to run this program this 390 00:13:17,519 --> 00:13:22,639 way this will now display 391 00:13:19,360 --> 00:13:24,800 your age is and in this case 22 so if 392 00:13:22,639 --> 00:13:26,000 you need to display a variable of a 393 00:13:24,800 --> 00:13:27,760 different data type 394 00:13:26,000 --> 00:13:29,920 along with the string you would just 395 00:13:27,760 --> 00:13:31,360 need to use a stringcast to convert that 396 00:13:29,920 --> 00:13:32,800 data type to a string 397 00:13:31,360 --> 00:13:34,560 but we'll cover this more in the next 398 00:13:32,800 --> 00:13:37,839 video on typecasting 399 00:13:34,560 --> 00:13:40,240 so that is the int data type it stores a 400 00:13:37,839 --> 00:13:41,040 whole integer number and the next data 401 00:13:40,240 --> 00:13:43,120 type is the 402 00:13:41,040 --> 00:13:44,880 float data type and i'm going to turn 403 00:13:43,120 --> 00:13:45,440 these lines into comments and we can 404 00:13:44,880 --> 00:13:47,680 move on 405 00:13:45,440 --> 00:13:48,959 now the float data type is a numeric 406 00:13:47,680 --> 00:13:52,000 value that can store 407 00:13:48,959 --> 00:13:54,720 a number that includes a decimal portion 408 00:13:52,000 --> 00:13:56,240 an int data type cannot store a decimal 409 00:13:54,720 --> 00:13:58,160 portion that's why it's in 410 00:13:56,240 --> 00:14:00,399 short for integer it only stores a whole 411 00:13:58,160 --> 00:14:00,959 number this time let's say we have a 412 00:14:00,399 --> 00:14:02,959 height 413 00:14:00,959 --> 00:14:06,240 and i will assign this a perfectly 414 00:14:02,959 --> 00:14:08,800 normal height of maybe 250.5 415 00:14:06,240 --> 00:14:09,519 centimeters so this decimal portion is 416 00:14:08,800 --> 00:14:11,519 important 417 00:14:09,519 --> 00:14:13,920 that's indicating that this is a 418 00:14:11,519 --> 00:14:15,920 floating point number a float for short 419 00:14:13,920 --> 00:14:17,360 so let's print our height to the console 420 00:14:15,920 --> 00:14:19,360 window print 421 00:14:17,360 --> 00:14:20,720 height and this will print whatever my 422 00:14:19,360 --> 00:14:23,440 height is and now 423 00:14:20,720 --> 00:14:25,040 let's check the type of our height using 424 00:14:23,440 --> 00:14:27,199 the type function 425 00:14:25,040 --> 00:14:28,480 so within a print statement i'm going to 426 00:14:27,199 --> 00:14:30,000 add my height 427 00:14:28,480 --> 00:14:32,240 surround this with another set of 428 00:14:30,000 --> 00:14:35,760 parentheses and precede this 429 00:14:32,240 --> 00:14:38,399 with the type function and the data type 430 00:14:35,760 --> 00:14:39,680 of my variable height is a float short 431 00:14:38,399 --> 00:14:42,000 for a floating point number 432 00:14:39,680 --> 00:14:42,720 a numeric value that contains a decimal 433 00:14:42,000 --> 00:14:44,639 portion 434 00:14:42,720 --> 00:14:46,240 and now for practice let's print our 435 00:14:44,639 --> 00:14:48,880 height along with a string 436 00:14:46,240 --> 00:14:50,160 literal so let's create a string literal 437 00:14:48,880 --> 00:14:53,279 such as 438 00:14:50,160 --> 00:14:55,279 your height is 439 00:14:53,279 --> 00:14:56,880 plus height but guess what we're going 440 00:14:55,279 --> 00:14:58,399 to do some type casting 441 00:14:56,880 --> 00:15:00,800 so surround height with a set of 442 00:14:58,399 --> 00:15:03,120 parentheses and precede this with 443 00:15:00,800 --> 00:15:04,399 str so that we convert height to a 444 00:15:03,120 --> 00:15:06,079 string so that we can use 445 00:15:04,399 --> 00:15:07,839 string concatenation within this print 446 00:15:06,079 --> 00:15:08,720 statement and this will print the 447 00:15:07,839 --> 00:15:12,240 message 448 00:15:08,720 --> 00:15:14,000 your height is 250.5 actually let's add 449 00:15:12,240 --> 00:15:17,279 centimeters to the end so 450 00:15:14,000 --> 00:15:19,519 plus cm 451 00:15:17,279 --> 00:15:20,720 and there we go and that is what a 452 00:15:19,519 --> 00:15:22,880 floating point number is 453 00:15:20,720 --> 00:15:25,040 float for short it is a numeric value 454 00:15:22,880 --> 00:15:26,560 that contains a decimal portion 455 00:15:25,040 --> 00:15:28,399 now the last data type that we're going 456 00:15:26,560 --> 00:15:30,720 to cover is the boolean 457 00:15:28,399 --> 00:15:31,839 data type it is a variable that can only 458 00:15:30,720 --> 00:15:34,560 store true 459 00:15:31,839 --> 00:15:35,680 or false let's say we have a variable 460 00:15:34,560 --> 00:15:37,680 called human 461 00:15:35,680 --> 00:15:40,639 if we were to assign this a boolean 462 00:15:37,680 --> 00:15:42,720 value this would be either true or 463 00:15:40,639 --> 00:15:44,720 false and we can print whatever this 464 00:15:42,720 --> 00:15:47,839 value is let's print whatever 465 00:15:44,720 --> 00:15:49,600 human is and this will display false now 466 00:15:47,839 --> 00:15:51,600 the reason that you may want to use 467 00:15:49,600 --> 00:15:52,959 booleans instead of strings even though 468 00:15:51,600 --> 00:15:54,800 you can still store 469 00:15:52,959 --> 00:15:56,079 a string representation of the word 470 00:15:54,800 --> 00:15:58,399 false or true 471 00:15:56,079 --> 00:15:59,600 is that these are very useful when we 472 00:15:58,399 --> 00:16:01,839 get to if statements 473 00:15:59,600 --> 00:16:04,320 we can check to see if some statement is 474 00:16:01,839 --> 00:16:07,120 true so that is a good use of 475 00:16:04,320 --> 00:16:08,320 boolean values now let's check the data 476 00:16:07,120 --> 00:16:11,680 type of 477 00:16:08,320 --> 00:16:15,120 my human variable so let's print 478 00:16:11,680 --> 00:16:16,320 type human and this will print to the 479 00:16:15,120 --> 00:16:18,399 console window 480 00:16:16,320 --> 00:16:20,399 bool short for boolean now make sure 481 00:16:18,399 --> 00:16:22,320 you're not putting this within quotes 482 00:16:20,399 --> 00:16:23,839 because then it's technically a string 483 00:16:22,320 --> 00:16:26,000 and they behave differently 484 00:16:23,839 --> 00:16:27,759 now the data type is a string and you 485 00:16:26,000 --> 00:16:29,440 may have noticed as well that the font 486 00:16:27,759 --> 00:16:31,680 color has changed between 487 00:16:29,440 --> 00:16:32,959 strings and boolean values so that's 488 00:16:31,680 --> 00:16:35,279 another good indicator 489 00:16:32,959 --> 00:16:37,440 now what if you need to display your 490 00:16:35,279 --> 00:16:38,320 value along with a string using string 491 00:16:37,440 --> 00:16:39,920 concatenation 492 00:16:38,320 --> 00:16:43,759 well guess what we're going to typecast 493 00:16:39,920 --> 00:16:43,759 again so let's print something 494 00:16:44,240 --> 00:16:50,880 r u a human 495 00:16:48,639 --> 00:16:52,560 so we need to use string concatenation 496 00:16:50,880 --> 00:16:55,920 so i will convert 497 00:16:52,560 --> 00:16:58,639 my boolean variable of human to a string 498 00:16:55,920 --> 00:16:59,279 and i have been programmed to inform you 499 00:16:58,639 --> 00:17:01,920 that 500 00:16:59,279 --> 00:17:02,800 yes i am in fact a human that is true 501 00:17:01,920 --> 00:17:05,199 well everyone 502 00:17:02,800 --> 00:17:07,439 that is the basics of variables it is a 503 00:17:05,199 --> 00:17:09,760 container for a value and they behave 504 00:17:07,439 --> 00:17:11,600 as the value that they contain there are 505 00:17:09,760 --> 00:17:13,360 four basic data types 506 00:17:11,600 --> 00:17:14,880 strings which store a series of 507 00:17:13,360 --> 00:17:18,000 characters inc 508 00:17:14,880 --> 00:17:20,079 which store a whole integer floats which 509 00:17:18,000 --> 00:17:21,839 are floating point numbers a numeric 510 00:17:20,079 --> 00:17:24,480 value with a decimal 511 00:17:21,839 --> 00:17:25,120 and booleans which only store true or 512 00:17:24,480 --> 00:17:27,039 false 513 00:17:25,120 --> 00:17:29,600 and they're very useful with if 514 00:17:27,039 --> 00:17:31,280 statements so that is it for variables 515 00:17:29,600 --> 00:17:33,200 if you would like a copy of all this 516 00:17:31,280 --> 00:17:35,039 code i will copy and paste all of this 517 00:17:33,200 --> 00:17:37,600 to the comments section down below 518 00:17:35,039 --> 00:17:38,640 but yeah that is how variables work in 519 00:17:37,600 --> 00:17:41,039 python 520 00:17:38,640 --> 00:17:41,840 hey people what's going on it's bro hope 521 00:17:41,039 --> 00:17:43,520 you're doing well 522 00:17:41,840 --> 00:17:45,679 and in this video i'm going to explain 523 00:17:43,520 --> 00:17:50,400 multiple assignment in python so 524 00:17:45,679 --> 00:17:50,400 sit back relax and enjoy the show 525 00:17:50,480 --> 00:17:53,919 well then since you clicked on this 526 00:17:52,080 --> 00:17:55,120 video i should probably explain what 527 00:17:53,919 --> 00:17:57,120 multiple assignment is 528 00:17:55,120 --> 00:17:59,360 now multiple assignment allows us to 529 00:17:57,120 --> 00:18:02,320 assign multiple variables 530 00:17:59,360 --> 00:18:03,600 at the same time using one line of code 531 00:18:02,320 --> 00:18:05,679 here's an example of us 532 00:18:03,600 --> 00:18:06,960 using standard assignment let's say we 533 00:18:05,679 --> 00:18:09,679 have a variable 534 00:18:06,960 --> 00:18:11,120 name and i will set this to a value of 535 00:18:09,679 --> 00:18:12,240 my name but you can use your name for 536 00:18:11,120 --> 00:18:15,440 this example 537 00:18:12,240 --> 00:18:16,880 let's say edge equals 21 and how about a 538 00:18:15,440 --> 00:18:19,679 variable called 539 00:18:16,880 --> 00:18:20,480 attractive i think i'm going to set this 540 00:18:19,679 --> 00:18:22,559 to true 541 00:18:20,480 --> 00:18:23,520 okay so we have a bunch of variables and 542 00:18:22,559 --> 00:18:25,600 then we can print 543 00:18:23,520 --> 00:18:26,960 the value of these variables with some 544 00:18:25,600 --> 00:18:30,880 print statements 545 00:18:26,960 --> 00:18:34,880 so let's print name age and attractive 546 00:18:30,880 --> 00:18:37,280 so we have name age attractive 547 00:18:34,880 --> 00:18:38,799 and as you would expect this prints bro 548 00:18:37,280 --> 00:18:40,799 21 and true 549 00:18:38,799 --> 00:18:42,320 now another way in which we could write 550 00:18:40,799 --> 00:18:44,400 the same code is to use 551 00:18:42,320 --> 00:18:46,320 multiple assignment and this allows us 552 00:18:44,400 --> 00:18:48,799 to assign multiple variables 553 00:18:46,320 --> 00:18:50,080 at the same time using one line of code 554 00:18:48,799 --> 00:18:52,720 so i'm going to turn 555 00:18:50,080 --> 00:18:53,360 all of these lines into comments and 556 00:18:52,720 --> 00:18:55,679 this time 557 00:18:53,360 --> 00:18:57,440 we will only use one line of code but to 558 00:18:55,679 --> 00:18:58,320 do this we're going to list all of our 559 00:18:57,440 --> 00:19:00,960 variables 560 00:18:58,320 --> 00:19:01,679 separated with a comma so that would be 561 00:19:00,960 --> 00:19:05,600 name 562 00:19:01,679 --> 00:19:08,160 comma edge comma attractive 563 00:19:05,600 --> 00:19:08,880 and we will set them equal to those 564 00:19:08,160 --> 00:19:10,880 values 565 00:19:08,880 --> 00:19:12,240 but in the same order separated by 566 00:19:10,880 --> 00:19:15,840 commas so that would be 567 00:19:12,240 --> 00:19:17,600 bro comma 21 comma true 568 00:19:15,840 --> 00:19:19,440 and this would do the same thing only 569 00:19:17,600 --> 00:19:21,600 using one line of code 570 00:19:19,440 --> 00:19:22,480 here's another example let's say we have 571 00:19:21,600 --> 00:19:25,280 four variables 572 00:19:22,480 --> 00:19:25,600 spongebob patrick sandy and squidward 573 00:19:25,280 --> 00:19:27,440 and 574 00:19:25,600 --> 00:19:28,799 let's pretend that they're all around 575 00:19:27,440 --> 00:19:30,559 maybe 30 years old 576 00:19:28,799 --> 00:19:31,919 at least it seems like they're around 30 577 00:19:30,559 --> 00:19:34,240 years old but i could be wrong 578 00:19:31,919 --> 00:19:36,880 i'm not too familiar with spongebob lore 579 00:19:34,240 --> 00:19:39,039 so let's set them all equal to 30. 580 00:19:36,880 --> 00:19:40,080 now we could write this using four lines 581 00:19:39,039 --> 00:19:42,160 of code or 582 00:19:40,080 --> 00:19:43,200 we could use multiple assignment to 583 00:19:42,160 --> 00:19:45,919 write the same thing 584 00:19:43,200 --> 00:19:46,880 using one line of code so if all of 585 00:19:45,919 --> 00:19:49,200 these variables 586 00:19:46,880 --> 00:19:51,039 are all equal to the same value there's 587 00:19:49,200 --> 00:19:53,360 a little bit of different syntax here 588 00:19:51,039 --> 00:19:54,799 so let's begin with the first variable 589 00:19:53,360 --> 00:19:58,720 of spongebob 590 00:19:54,799 --> 00:19:58,720 equals patrick 591 00:19:59,280 --> 00:20:06,159 equals sandy equals 592 00:20:02,559 --> 00:20:09,200 squidward then we're going to set them 593 00:20:06,159 --> 00:20:11,280 all equal to 30 and 594 00:20:09,200 --> 00:20:12,400 this should work just fine using only 595 00:20:11,280 --> 00:20:14,080 one line of code 596 00:20:12,400 --> 00:20:15,760 well everybody that's pretty much it for 597 00:20:14,080 --> 00:20:16,240 multiple assignment it allows us to 598 00:20:15,760 --> 00:20:18,559 assign 599 00:20:16,240 --> 00:20:20,240 multiple variables at the same time 600 00:20:18,559 --> 00:20:22,000 using one line of code 601 00:20:20,240 --> 00:20:23,600 i will post all of this code in the 602 00:20:22,000 --> 00:20:25,360 comments section down below 603 00:20:23,600 --> 00:20:27,200 and well yeah that's how multiple 604 00:20:25,360 --> 00:20:29,520 assignment works in python 605 00:20:27,200 --> 00:20:30,480 hey what's going on everybody it's bro 606 00:20:29,520 --> 00:20:31,600 hope you're doing well 607 00:20:30,480 --> 00:20:33,760 and in this video i'm going to 608 00:20:31,600 --> 00:20:34,799 demonstrate some useful string methods 609 00:20:33,760 --> 00:20:39,520 in python so 610 00:20:34,799 --> 00:20:39,520 sit back relax and enjoy the show 611 00:20:39,679 --> 00:20:43,840 all right let's do this in this video 612 00:20:42,400 --> 00:20:44,640 i'm going to be explaining and 613 00:20:43,840 --> 00:20:47,039 demonstrating 614 00:20:44,640 --> 00:20:47,679 a few useful methods available to us for 615 00:20:47,039 --> 00:20:49,919 strings 616 00:20:47,679 --> 00:20:51,520 now let's create a variable called name 617 00:20:49,919 --> 00:20:52,559 for this example and you can use your 618 00:20:51,520 --> 00:20:54,960 own name if you want 619 00:20:52,559 --> 00:20:56,960 so we now have a variable called name 620 00:20:54,960 --> 00:20:59,360 and for my first example 621 00:20:56,960 --> 00:21:00,159 let's print the length of our name 622 00:20:59,360 --> 00:21:03,280 variable 623 00:21:00,159 --> 00:21:05,919 so to do that we can surround our name 624 00:21:03,280 --> 00:21:06,640 variable with the length method and this 625 00:21:05,919 --> 00:21:09,280 will print 626 00:21:06,640 --> 00:21:09,919 and return how long the length of our 627 00:21:09,280 --> 00:21:11,840 string is 628 00:21:09,919 --> 00:21:13,200 and for me the length of my name 629 00:21:11,840 --> 00:21:15,360 variable is three 630 00:21:13,200 --> 00:21:17,280 for three characters if i were to 631 00:21:15,360 --> 00:21:19,919 increase the size of my name 632 00:21:17,280 --> 00:21:20,480 well then that's going to change to a so 633 00:21:19,919 --> 00:21:22,320 that is 634 00:21:20,480 --> 00:21:24,159 the length method and i'm just going to 635 00:21:22,320 --> 00:21:26,799 turn this line into a comment 636 00:21:24,159 --> 00:21:27,760 and let's move on so next we have the 637 00:21:26,799 --> 00:21:30,080 find method 638 00:21:27,760 --> 00:21:31,200 i just copy and pasted print name so i 639 00:21:30,080 --> 00:21:33,679 can reuse it 640 00:21:31,200 --> 00:21:34,799 name dot and actually if you have 641 00:21:33,679 --> 00:21:37,360 intellisense setup 642 00:21:34,799 --> 00:21:39,280 if you add dot after your variable 643 00:21:37,360 --> 00:21:40,400 you'll get a suggestion or a few 644 00:21:39,280 --> 00:21:42,400 suggestions of 645 00:21:40,400 --> 00:21:44,240 different methods and other features 646 00:21:42,400 --> 00:21:48,400 that you have access to so 647 00:21:44,240 --> 00:21:51,760 let's find find which is 648 00:21:48,400 --> 00:21:54,240 uh screw this i'll just type find find 649 00:21:51,760 --> 00:21:55,600 then we can find the first index of 650 00:21:54,240 --> 00:21:57,360 where this character is 651 00:21:55,600 --> 00:21:58,799 let's say i would like to find where 652 00:21:57,360 --> 00:22:01,120 capital b is 653 00:21:58,799 --> 00:22:03,120 well that is at index zero because 654 00:22:01,120 --> 00:22:05,360 computers always start with zero 655 00:22:03,120 --> 00:22:06,480 so this first character would be zero 656 00:22:05,360 --> 00:22:10,080 then r 657 00:22:06,480 --> 00:22:12,240 would be one and then o would be 658 00:22:10,080 --> 00:22:14,080 two for this example all right so that 659 00:22:12,240 --> 00:22:17,360 is how to find a character 660 00:22:14,080 --> 00:22:19,919 within a string moving on so let's 661 00:22:17,360 --> 00:22:23,200 capitalize our name and i should 662 00:22:19,919 --> 00:22:26,720 probably make this lowercase 663 00:22:23,200 --> 00:22:29,520 so name dot capitalize 664 00:22:26,720 --> 00:22:31,039 and this will make my name capitalized 665 00:22:29,520 --> 00:22:33,200 but if there's any spaces 666 00:22:31,039 --> 00:22:34,240 and then additional words it's not going 667 00:22:33,200 --> 00:22:37,360 to capitalize that 668 00:22:34,240 --> 00:22:40,400 only the first letter and your string 669 00:22:37,360 --> 00:22:42,000 next we have upper this will make your 670 00:22:40,400 --> 00:22:45,280 string all uppercase 671 00:22:42,000 --> 00:22:49,039 name upper and now 672 00:22:45,280 --> 00:22:52,960 it is uppercase guess what's next 673 00:22:49,039 --> 00:22:54,799 lower name dot lower 674 00:22:52,960 --> 00:22:57,120 and i should probably make this capital 675 00:22:54,799 --> 00:22:57,120 again 676 00:22:57,280 --> 00:23:02,799 and everything is lowercase 677 00:23:00,559 --> 00:23:04,000 we're making some good progress today 678 00:23:02,799 --> 00:23:07,200 next we have 679 00:23:04,000 --> 00:23:11,200 is digit this will return true or false 680 00:23:07,200 --> 00:23:14,000 depending on if our string is a digit 681 00:23:11,200 --> 00:23:15,840 which it's not so this will print false 682 00:23:14,000 --> 00:23:17,039 if i changed my string to a bunch of 683 00:23:15,840 --> 00:23:19,919 numbers like 1 2 684 00:23:17,039 --> 00:23:20,960 3 then this will return true let's 685 00:23:19,919 --> 00:23:25,200 change that back 686 00:23:20,960 --> 00:23:25,200 and move on next we have 687 00:23:25,679 --> 00:23:28,480 is alpha 688 00:23:28,799 --> 00:23:32,480 are these alphabetical characters 689 00:23:33,280 --> 00:23:37,520 oh make sure you add the parentheses 690 00:23:35,120 --> 00:23:41,039 afterwards my bad 691 00:23:37,520 --> 00:23:42,559 false because we added a space within 692 00:23:41,039 --> 00:23:46,080 here 693 00:23:42,559 --> 00:23:46,799 now it's true so you can use is alpha to 694 00:23:46,080 --> 00:23:49,200 check to see 695 00:23:46,799 --> 00:23:51,919 if your string contains only letters 696 00:23:49,200 --> 00:23:55,440 alphabetical letters 697 00:23:51,919 --> 00:23:57,760 next we have count 698 00:23:55,440 --> 00:23:58,880 we can count how many characters are 699 00:23:57,760 --> 00:24:00,480 within our string 700 00:23:58,880 --> 00:24:02,640 i would like to count how many o's are 701 00:24:00,480 --> 00:24:06,320 in here and this should return 702 00:24:02,640 --> 00:24:07,039 two that's right okay we're almost at 703 00:24:06,320 --> 00:24:09,679 the end 704 00:24:07,039 --> 00:24:10,640 we can replace characters within our 705 00:24:09,679 --> 00:24:13,919 string 706 00:24:10,640 --> 00:24:16,799 so we use the replace method 707 00:24:13,919 --> 00:24:18,000 and we pass in two arguments the 708 00:24:16,799 --> 00:24:20,159 character we would like to 709 00:24:18,000 --> 00:24:21,840 replace and the character we would like 710 00:24:20,159 --> 00:24:24,559 to replace our character with 711 00:24:21,840 --> 00:24:25,120 i would like to change all o's to a's 712 00:24:24,559 --> 00:24:28,080 and now 713 00:24:25,120 --> 00:24:29,279 my name is brock hod oh you guys are 714 00:24:28,080 --> 00:24:31,760 gonna like this next one 715 00:24:29,279 --> 00:24:33,279 this isn't technically a method but it's 716 00:24:31,760 --> 00:24:34,720 a really neat feature 717 00:24:33,279 --> 00:24:36,640 so one thing that we can do that's 718 00:24:34,720 --> 00:24:38,159 available to us with python is that we 719 00:24:36,640 --> 00:24:40,559 can display a string 720 00:24:38,159 --> 00:24:42,320 multiple times by multiplying that 721 00:24:40,559 --> 00:24:44,159 string by a given number 722 00:24:42,320 --> 00:24:46,080 so let's say i would like to display my 723 00:24:44,159 --> 00:24:47,039 name three times i can just take my 724 00:24:46,080 --> 00:24:49,600 string variable 725 00:24:47,039 --> 00:24:50,240 then add times three and this will print 726 00:24:49,600 --> 00:24:53,440 my name 727 00:24:50,240 --> 00:24:54,159 three times bro bro bro like i said not 728 00:24:53,440 --> 00:24:56,240 technically 729 00:24:54,159 --> 00:24:57,600 a method but it's a useful feature that 730 00:24:56,240 --> 00:24:58,400 you might be interested in that's 731 00:24:57,600 --> 00:25:01,039 related 732 00:24:58,400 --> 00:25:02,960 all right everybody so that is a few 733 00:25:01,039 --> 00:25:04,960 useful methods available to us for 734 00:25:02,960 --> 00:25:05,679 strings if you would like a copy of all 735 00:25:04,960 --> 00:25:07,600 this code 736 00:25:05,679 --> 00:25:09,760 i will post this in the comments section 737 00:25:07,600 --> 00:25:12,400 down below but yeah those are 738 00:25:09,760 --> 00:25:13,039 a few useful methods available to us for 739 00:25:12,400 --> 00:25:14,880 strings 740 00:25:13,039 --> 00:25:16,640 hey what's going on everybody it's you 741 00:25:14,880 --> 00:25:18,000 bro hope you're doing well and in this 742 00:25:16,640 --> 00:25:19,919 video i'm going to teach you guys all 743 00:25:18,000 --> 00:25:24,240 about typecasting in python so 744 00:25:19,919 --> 00:25:24,240 sit back relax and enjoy the show 745 00:25:24,400 --> 00:25:28,320 all right welcome back everybody i have 746 00:25:26,480 --> 00:25:30,159 a super quick video for you guys today 747 00:25:28,320 --> 00:25:32,240 on the subject of typecasting now 748 00:25:30,159 --> 00:25:35,200 typecasting is the ability to convert 749 00:25:32,240 --> 00:25:35,679 the data type of a value to another data 750 00:25:35,200 --> 00:25:36,880 type 751 00:25:35,679 --> 00:25:38,720 here's an example that i've written 752 00:25:36,880 --> 00:25:41,440 already i'm just going to paste it 753 00:25:38,720 --> 00:25:42,320 i have three variables for this example 754 00:25:41,440 --> 00:25:45,440 x y 755 00:25:42,320 --> 00:25:46,080 and z x contains one this is a whole 756 00:25:45,440 --> 00:25:48,880 integer 757 00:25:46,080 --> 00:25:50,720 into for short y contains two point zero 758 00:25:48,880 --> 00:25:52,400 this is a floating point number 759 00:25:50,720 --> 00:25:53,840 float four short you can tell it's a 760 00:25:52,400 --> 00:25:54,559 float because it ends with a decimal 761 00:25:53,840 --> 00:25:57,520 portion 762 00:25:54,559 --> 00:25:58,000 and z contains a string of three a 763 00:25:57,520 --> 00:26:00,320 string 764 00:25:58,000 --> 00:26:01,440 is a series of characters and you can 765 00:26:00,320 --> 00:26:03,440 tell it's a string because it's 766 00:26:01,440 --> 00:26:05,279 surrounded with a set of quotes 767 00:26:03,440 --> 00:26:08,000 and remember with strings we cannot 768 00:26:05,279 --> 00:26:11,120 normally perform math on strings 769 00:26:08,000 --> 00:26:13,200 so i have three print statements for x y 770 00:26:11,120 --> 00:26:14,400 and z and as you would expect they 771 00:26:13,200 --> 00:26:17,120 contain the values of 772 00:26:14,400 --> 00:26:18,960 one two point zero and three now let's 773 00:26:17,120 --> 00:26:21,520 begin by converting both y 774 00:26:18,960 --> 00:26:23,279 and z to the integer data type via 775 00:26:21,520 --> 00:26:24,080 typecasting in order to perform 776 00:26:23,279 --> 00:26:27,039 typecasting 777 00:26:24,080 --> 00:26:28,960 on a value or variable type that value 778 00:26:27,039 --> 00:26:32,000 or variable and surround this with a set 779 00:26:28,960 --> 00:26:32,880 of parentheses whoops i missed and then 780 00:26:32,000 --> 00:26:34,720 pre-save this 781 00:26:32,880 --> 00:26:36,640 with the data type you'd like to convert 782 00:26:34,720 --> 00:26:39,360 this value or variable to 783 00:26:36,640 --> 00:26:40,960 i would like to convert y to an integer 784 00:26:39,360 --> 00:26:41,520 and then you can use this for whatever 785 00:26:40,960 --> 00:26:43,679 you need 786 00:26:41,520 --> 00:26:44,799 so i'm going to replace y within my 787 00:26:43,679 --> 00:26:47,840 print statement 788 00:26:44,799 --> 00:26:50,960 with y after we typecast it as 789 00:26:47,840 --> 00:26:54,240 an integer and this will display 1 2 790 00:26:50,960 --> 00:26:57,919 not 2.0 it's the whole integer 791 00:26:54,240 --> 00:26:58,880 of 2 and then 3. so this is not a 792 00:26:57,919 --> 00:27:00,960 permanent change 793 00:26:58,880 --> 00:27:02,320 if you need to make this as a permanent 794 00:27:00,960 --> 00:27:05,600 change to y 795 00:27:02,320 --> 00:27:07,600 you'd have to reassign it so let's say y 796 00:27:05,600 --> 00:27:09,120 equals y and then we're going to 797 00:27:07,600 --> 00:27:10,559 typecast y 798 00:27:09,120 --> 00:27:12,400 so let's round this with a set of 799 00:27:10,559 --> 00:27:14,000 parentheses and precede this with the 800 00:27:12,400 --> 00:27:17,120 data type we'd like to convert 801 00:27:14,000 --> 00:27:20,480 y2 so now y is a whole 802 00:27:17,120 --> 00:27:23,600 integer and then we will print y without 803 00:27:20,480 --> 00:27:27,600 typecast this time and our result is 804 00:27:23,600 --> 00:27:29,440 one two three now z z is storing a 805 00:27:27,600 --> 00:27:31,760 string and with strings we cannot 806 00:27:29,440 --> 00:27:33,679 normally perform any math on strings 807 00:27:31,760 --> 00:27:35,360 for example within my print statement i 808 00:27:33,679 --> 00:27:37,360 will state z 809 00:27:35,360 --> 00:27:39,360 times three you would think that the 810 00:27:37,360 --> 00:27:41,279 result would be nine right 811 00:27:39,360 --> 00:27:42,880 well you're wrong it's going to print 812 00:27:41,279 --> 00:27:46,320 three three times 813 00:27:42,880 --> 00:27:47,919 if you need to convert a string to a int 814 00:27:46,320 --> 00:27:50,640 or a float you would just have to 815 00:27:47,919 --> 00:27:53,679 typecast it so this time let's type cast 816 00:27:50,640 --> 00:27:56,799 z as an integer z 817 00:27:53,679 --> 00:28:00,240 int and we will type cast z as an 818 00:27:56,799 --> 00:28:04,159 integer so then if we have z times three 819 00:28:00,240 --> 00:28:06,559 the result is now 9 instead of 333 820 00:28:04,159 --> 00:28:08,720 so that's the basic formula for type 821 00:28:06,559 --> 00:28:11,120 casting now let's convert these to 822 00:28:08,720 --> 00:28:12,159 floating point numbers but let's include 823 00:28:11,120 --> 00:28:14,960 x as well because 824 00:28:12,159 --> 00:28:15,360 x is feeling left out so let's convert x 825 00:28:14,960 --> 00:28:17,840 to 826 00:28:15,360 --> 00:28:19,520 a floating point number so float 827 00:28:17,840 --> 00:28:22,960 parenthesis and we will surround 828 00:28:19,520 --> 00:28:26,159 this with x and display x 829 00:28:22,960 --> 00:28:29,600 and actually let's convert y and z to 830 00:28:26,159 --> 00:28:33,520 floats as well because why not 831 00:28:29,600 --> 00:28:35,840 the results are now 1.0 2.0 and 9.0 832 00:28:33,520 --> 00:28:36,559 remember that we multiplied z times 833 00:28:35,840 --> 00:28:38,799 three 834 00:28:36,559 --> 00:28:41,039 and then if we need to convert these 835 00:28:38,799 --> 00:28:45,840 values to a string 836 00:28:41,039 --> 00:28:45,840 we would use the string cast 837 00:28:46,240 --> 00:28:52,960 so the results now are 1 2.0 838 00:28:49,520 --> 00:28:53,520 and 333 now you might be thinking to 839 00:28:52,960 --> 00:28:55,200 yourself 840 00:28:53,520 --> 00:28:57,600 when would you need to convert an 841 00:28:55,200 --> 00:28:59,919 integer or a float to a string 842 00:28:57,600 --> 00:29:02,159 here's a situation let's say within our 843 00:28:59,919 --> 00:29:04,080 print statements we need to print 844 00:29:02,159 --> 00:29:06,080 these variables along with another 845 00:29:04,080 --> 00:29:09,279 string so within our print statement 846 00:29:06,080 --> 00:29:11,360 let's say x is plus 847 00:29:09,279 --> 00:29:12,320 x we would like to display these 848 00:29:11,360 --> 00:29:14,000 together 849 00:29:12,320 --> 00:29:15,360 and let me copy this and do the same 850 00:29:14,000 --> 00:29:18,640 thing for y 851 00:29:15,360 --> 00:29:21,360 we'll say y is plus y 852 00:29:18,640 --> 00:29:22,799 you cannot normally display a string 853 00:29:21,360 --> 00:29:24,880 along with an integer 854 00:29:22,799 --> 00:29:26,320 or a float because we're using some 855 00:29:24,880 --> 00:29:28,159 string concatenation 856 00:29:26,320 --> 00:29:30,480 so this will result in a compilation 857 00:29:28,159 --> 00:29:33,760 error can only concatenate 858 00:29:30,480 --> 00:29:34,559 string not int to string so that's a 859 00:29:33,760 --> 00:29:36,080 situation 860 00:29:34,559 --> 00:29:38,080 in which you might need to use 861 00:29:36,080 --> 00:29:40,960 typecasting to convert an 862 00:29:38,080 --> 00:29:41,600 integer or float to a string so let's 863 00:29:40,960 --> 00:29:45,200 display 864 00:29:41,600 --> 00:29:47,520 x and y as strings via typecasting 865 00:29:45,200 --> 00:29:49,279 so we'll surround x and y with a set of 866 00:29:47,520 --> 00:29:50,960 parentheses and precede this with the 867 00:29:49,279 --> 00:29:51,840 data type we'd like to convert these 868 00:29:50,960 --> 00:29:53,840 values to 869 00:29:51,840 --> 00:29:55,840 we would like to convert x and y to both 870 00:29:53,840 --> 00:29:56,159 strings and now this will compile and 871 00:29:55,840 --> 00:29:58,159 run 872 00:29:56,159 --> 00:30:00,159 just fine well everybody that's the 873 00:29:58,159 --> 00:30:02,000 basics of typecasting 874 00:30:00,159 --> 00:30:03,679 if you would like a copy of all my code 875 00:30:02,000 --> 00:30:04,559 i will post everything in the comments 876 00:30:03,679 --> 00:30:06,399 down below 877 00:30:04,559 --> 00:30:08,320 if you found this video helpful don't be 878 00:30:06,399 --> 00:30:09,039 afraid to help me out by smashing that 879 00:30:08,320 --> 00:30:11,440 like button 880 00:30:09,039 --> 00:30:14,320 drop a comment down below and subscribe 881 00:30:11,440 --> 00:30:16,640 if you'd like to become a fellow bro 882 00:30:14,320 --> 00:30:17,600 hey what's going on everybody it's bro 883 00:30:16,640 --> 00:30:18,960 hope you're doing well 884 00:30:17,600 --> 00:30:20,720 and in this video i'm going to teach you 885 00:30:18,960 --> 00:30:22,000 guys how we can accept some user input 886 00:30:20,720 --> 00:30:26,240 in python so sit 887 00:30:22,000 --> 00:30:27,919 back relax and enjoy the show 888 00:30:26,240 --> 00:30:30,000 ladies and gentlemen this is where the 889 00:30:27,919 --> 00:30:31,679 fun begins in this video i'm going to 890 00:30:30,000 --> 00:30:32,720 explain how we can accept some user 891 00:30:31,679 --> 00:30:35,360 input in python 892 00:30:32,720 --> 00:30:37,279 and we do so by using the input function 893 00:30:35,360 --> 00:30:38,320 and then we type in our input to the 894 00:30:37,279 --> 00:30:40,080 console window 895 00:30:38,320 --> 00:30:41,840 but it would probably be good practice 896 00:30:40,080 --> 00:30:43,200 to let the user know what we want them 897 00:30:41,840 --> 00:30:45,520 to type in exactly 898 00:30:43,200 --> 00:30:47,440 so let's ask for somebody's name within 899 00:30:45,520 --> 00:30:48,960 quotes inside the input function i'm 900 00:30:47,440 --> 00:30:52,320 going to type 901 00:30:48,960 --> 00:30:54,799 what is your name and now 902 00:30:52,320 --> 00:30:55,760 our program will wait for us to enter in 903 00:30:54,799 --> 00:30:57,200 some user input 904 00:30:55,760 --> 00:30:58,880 so you can use your own name for this 905 00:30:57,200 --> 00:31:00,960 example i'm going to type bro 906 00:30:58,880 --> 00:31:02,240 now to submit some user input you just 907 00:31:00,960 --> 00:31:04,880 type or press 908 00:31:02,240 --> 00:31:06,640 enter now you can see that my process 909 00:31:04,880 --> 00:31:08,080 finished with exit code zero that means 910 00:31:06,640 --> 00:31:09,519 the program is done running 911 00:31:08,080 --> 00:31:11,519 so we should probably do something with 912 00:31:09,519 --> 00:31:13,360 this value we can actually assign it to 913 00:31:11,519 --> 00:31:14,159 a variable so we can use it elsewhere in 914 00:31:13,360 --> 00:31:17,360 our program 915 00:31:14,159 --> 00:31:20,000 so to do so we precede input with 916 00:31:17,360 --> 00:31:20,559 a variable like name works name equals 917 00:31:20,000 --> 00:31:22,799 input 918 00:31:20,559 --> 00:31:24,559 and when we accept some user input we 919 00:31:22,799 --> 00:31:25,200 will assign it to this variable that we 920 00:31:24,559 --> 00:31:27,279 called them 921 00:31:25,200 --> 00:31:28,960 and now we can do something with it so 922 00:31:27,279 --> 00:31:31,120 let's print a message 923 00:31:28,960 --> 00:31:32,240 involving our name variable that we have 924 00:31:31,120 --> 00:31:35,360 so how about 925 00:31:32,240 --> 00:31:37,039 hello plus name 926 00:31:35,360 --> 00:31:38,559 now when i run this program we can 927 00:31:37,039 --> 00:31:40,320 accept some user input 928 00:31:38,559 --> 00:31:42,480 and then do something with that input 929 00:31:40,320 --> 00:31:44,399 such as display message that says 930 00:31:42,480 --> 00:31:45,760 hello bro and then you can type in 931 00:31:44,399 --> 00:31:48,000 whatever you want for your name 932 00:31:45,760 --> 00:31:49,360 let's say i'm dude what is your name 933 00:31:48,000 --> 00:31:51,600 dude hello dude 934 00:31:49,360 --> 00:31:54,159 now this next part is very important 935 00:31:51,600 --> 00:31:55,039 when we accept user input it is always 936 00:31:54,159 --> 00:31:58,240 of the string 937 00:31:55,039 --> 00:32:00,000 data type a series of characters what if 938 00:31:58,240 --> 00:32:01,919 we need to accept a number 939 00:32:00,000 --> 00:32:04,080 well you can but you can't normally 940 00:32:01,919 --> 00:32:05,360 perform any math on it because it's of 941 00:32:04,080 --> 00:32:07,519 the string data type 942 00:32:05,360 --> 00:32:09,039 we would need to cast it as either of 943 00:32:07,519 --> 00:32:11,679 the integer data type 944 00:32:09,039 --> 00:32:14,320 or of the float data type so here's an 945 00:32:11,679 --> 00:32:16,960 example of why we can't perform any math 946 00:32:14,320 --> 00:32:17,360 on a string let's say we have a variable 947 00:32:16,960 --> 00:32:20,720 called 948 00:32:17,360 --> 00:32:23,760 edge age equals input and let's 949 00:32:20,720 --> 00:32:28,240 ask how old somebody is how old 950 00:32:23,760 --> 00:32:30,000 are you now i will add one to somebody's 951 00:32:28,240 --> 00:32:32,399 age let's pretend it's their birthday 952 00:32:30,000 --> 00:32:33,440 age equals age plus one this will add 953 00:32:32,399 --> 00:32:35,519 one to their age 954 00:32:33,440 --> 00:32:37,519 now let's try and run this what is your 955 00:32:35,519 --> 00:32:40,159 name bro 956 00:32:37,519 --> 00:32:42,799 how old are you let's say that i'm 21. 957 00:32:40,159 --> 00:32:46,080 okay we ran into an error a type error 958 00:32:42,799 --> 00:32:47,760 can only concatenate string to string 959 00:32:46,080 --> 00:32:49,200 with what i mentioned previously we 960 00:32:47,760 --> 00:32:51,679 cannot normally use 961 00:32:49,200 --> 00:32:53,279 strings for any sort of mathematical 962 00:32:51,679 --> 00:32:55,600 equation or operation 963 00:32:53,279 --> 00:32:56,320 we'll want to cast it to the integer or 964 00:32:55,600 --> 00:32:59,360 the float 965 00:32:56,320 --> 00:33:01,600 data type so let's cast our user input 966 00:32:59,360 --> 00:33:04,720 to the integer data type and we will 967 00:33:01,600 --> 00:33:06,480 surround our user input with a cast 968 00:33:04,720 --> 00:33:08,559 surround your input with a set of 969 00:33:06,480 --> 00:33:11,279 parenthesis and precede this with 970 00:33:08,559 --> 00:33:13,360 int and now we can use this user input 971 00:33:11,279 --> 00:33:15,120 in a mathematical operation 972 00:33:13,360 --> 00:33:16,799 so let's try it even though this won't 973 00:33:15,120 --> 00:33:20,000 display age yet so 974 00:33:16,799 --> 00:33:20,880 what is your name bro 21 and our program 975 00:33:20,000 --> 00:33:22,640 finished with 976 00:33:20,880 --> 00:33:24,880 an x a code of zero there were no 977 00:33:22,640 --> 00:33:25,600 problems now let's attempt to display 978 00:33:24,880 --> 00:33:28,559 this edge 979 00:33:25,600 --> 00:33:28,559 so i will print 980 00:33:28,720 --> 00:33:35,279 u r plus 981 00:33:31,919 --> 00:33:36,880 age plus years 982 00:33:35,279 --> 00:33:38,960 old we're doing some string 983 00:33:36,880 --> 00:33:42,559 concatenation here 984 00:33:38,960 --> 00:33:44,880 what is your name bro how old are you 21 985 00:33:42,559 --> 00:33:47,360 all right we ran into a type error again 986 00:33:44,880 --> 00:33:50,480 can only concatenate string to string 987 00:33:47,360 --> 00:33:52,080 you cannot normally display a variable 988 00:33:50,480 --> 00:33:54,480 of the integer data type 989 00:33:52,080 --> 00:33:55,919 along with strings because we're doing 990 00:33:54,480 --> 00:33:57,039 string concatenation we're adding 991 00:33:55,919 --> 00:33:59,200 strings together 992 00:33:57,039 --> 00:34:00,240 we would need to convert this variable 993 00:33:59,200 --> 00:34:02,880 back to the string 994 00:34:00,240 --> 00:34:03,440 data type so we can cast it we'll 995 00:34:02,880 --> 00:34:05,679 surround 996 00:34:03,440 --> 00:34:07,440 our edge variable with a cast now this 997 00:34:05,679 --> 00:34:09,280 is what's going to happen we will accept 998 00:34:07,440 --> 00:34:10,879 some user input it will be of the string 999 00:34:09,280 --> 00:34:13,440 data type and then we will 1000 00:34:10,879 --> 00:34:15,359 immediately cast it to be of the integer 1001 00:34:13,440 --> 00:34:17,919 data type so that we can treat it 1002 00:34:15,359 --> 00:34:19,679 as a number instead of a character and 1003 00:34:17,919 --> 00:34:21,760 then if we need to display this 1004 00:34:19,679 --> 00:34:23,440 edge we will need to convert it back to 1005 00:34:21,760 --> 00:34:25,599 a string so that we can use 1006 00:34:23,440 --> 00:34:27,040 some string concatenation and display 1007 00:34:25,599 --> 00:34:29,200 all of these strings together 1008 00:34:27,040 --> 00:34:30,720 and now let's try this theory so what is 1009 00:34:29,200 --> 00:34:33,359 your name bro 1010 00:34:30,720 --> 00:34:34,000 how old are you let's say i'm 21 hello 1011 00:34:33,359 --> 00:34:36,720 bro you 1012 00:34:34,000 --> 00:34:37,440 are 22 years old here's a situation what 1013 00:34:36,720 --> 00:34:39,919 if we enter 1014 00:34:37,440 --> 00:34:41,280 a number that contains a decimal portion 1015 00:34:39,919 --> 00:34:44,879 so let's try this again 1016 00:34:41,280 --> 00:34:47,200 let's say i'm 21.5 years old 1017 00:34:44,879 --> 00:34:48,320 well now we ran into a different issue a 1018 00:34:47,200 --> 00:34:51,359 value error 1019 00:34:48,320 --> 00:34:54,240 invalid literal for int with base 10 1020 00:34:51,359 --> 00:34:54,879 that means if we cast a string as an 1021 00:34:54,240 --> 00:34:57,359 integer 1022 00:34:54,879 --> 00:34:58,560 it can only be a whole number a portion 1023 00:34:57,359 --> 00:35:01,440 that does not contain 1024 00:34:58,560 --> 00:35:02,880 a decimal and that is where the float 1025 00:35:01,440 --> 00:35:05,280 data type would come in 1026 00:35:02,880 --> 00:35:06,560 it is a data type that can contain a 1027 00:35:05,280 --> 00:35:09,280 decimal portion 1028 00:35:06,560 --> 00:35:11,200 for this example let's create a third 1029 00:35:09,280 --> 00:35:12,880 variable called height and we will ask 1030 00:35:11,200 --> 00:35:16,720 for somebody's height 1031 00:35:12,880 --> 00:35:21,280 input we'll create a prompt that says 1032 00:35:16,720 --> 00:35:24,160 how tall are you 1033 00:35:21,280 --> 00:35:25,280 and we will cast this to be of the float 1034 00:35:24,160 --> 00:35:28,320 data type 1035 00:35:25,280 --> 00:35:31,119 so that our string our user input 1036 00:35:28,320 --> 00:35:33,359 can contain a decimal portion and then 1037 00:35:31,119 --> 00:35:34,800 we can treat it as a number instead of a 1038 00:35:33,359 --> 00:35:36,640 series of characters 1039 00:35:34,800 --> 00:35:38,160 and i think i will delete this line we 1040 00:35:36,640 --> 00:35:40,560 won't really need it anymore 1041 00:35:38,160 --> 00:35:42,160 and then let's print their height along 1042 00:35:40,560 --> 00:35:45,200 with a message 1043 00:35:42,160 --> 00:35:47,520 u r plus then 1044 00:35:45,200 --> 00:35:50,000 height but we need to cast this back to 1045 00:35:47,520 --> 00:35:54,240 being a string 1046 00:35:50,000 --> 00:35:54,240 so we'll surround our height with a cast 1047 00:35:54,400 --> 00:36:00,800 plus let's say cm 1048 00:35:57,520 --> 00:36:03,839 tall all right let's test this 1049 00:36:00,800 --> 00:36:06,800 what is your name bro how old are you 1050 00:36:03,839 --> 00:36:12,240 let's say i'm 21 how tall are you let's 1051 00:36:06,800 --> 00:36:14,320 pretend that i'm 250.5 centimeters tall 1052 00:36:12,240 --> 00:36:15,920 i'm adding the 0.5 just to test to see 1053 00:36:14,320 --> 00:36:17,680 if this will accept a floating point 1054 00:36:15,920 --> 00:36:21,000 number 1055 00:36:17,680 --> 00:36:23,200 hello bro you are 21 years old you are 1056 00:36:21,000 --> 00:36:25,119 250.5 centimeters tall 1057 00:36:23,200 --> 00:36:26,480 not really all right everybody so that 1058 00:36:25,119 --> 00:36:29,359 is the basics of 1059 00:36:26,480 --> 00:36:30,640 user input in python normally with user 1060 00:36:29,359 --> 00:36:33,200 input it will give you 1061 00:36:30,640 --> 00:36:35,520 a value of the string data type if you 1062 00:36:33,200 --> 00:36:36,240 need to use that value for any sort of 1063 00:36:35,520 --> 00:36:38,240 math 1064 00:36:36,240 --> 00:36:40,000 you'll probably need to cast it to be of 1065 00:36:38,240 --> 00:36:42,400 the integer or the float data 1066 00:36:40,000 --> 00:36:44,160 type so if you would like a copy of this 1067 00:36:42,400 --> 00:36:45,440 code i will post this in the comment 1068 00:36:44,160 --> 00:36:47,920 section down below 1069 00:36:45,440 --> 00:36:49,920 but yeah that's how to accept user input 1070 00:36:47,920 --> 00:36:52,079 in python 1071 00:36:49,920 --> 00:36:53,839 hey what's going on everybody it's bro 1072 00:36:52,079 --> 00:36:55,760 hope you're doing well and in this video 1073 00:36:53,839 --> 00:36:57,920 i'm going to demonstrate a few useful 1074 00:36:55,760 --> 00:36:58,320 functions related to numbers in python 1075 00:36:57,920 --> 00:37:03,200 so 1076 00:36:58,320 --> 00:37:05,760 sit back relax and enjoy the show 1077 00:37:03,200 --> 00:37:07,760 okay let's begin in this video i'm going 1078 00:37:05,760 --> 00:37:09,599 to be demonstrating a bunch of useful 1079 00:37:07,760 --> 00:37:11,359 functions related to numbers 1080 00:37:09,599 --> 00:37:13,119 and a bunch of these functions are 1081 00:37:11,359 --> 00:37:15,839 located within the math 1082 00:37:13,119 --> 00:37:17,119 module now let's create a variable for 1083 00:37:15,839 --> 00:37:21,440 this example let's say 1084 00:37:17,119 --> 00:37:23,520 pi equals 3.14 just the first few digits 1085 00:37:21,440 --> 00:37:25,200 of pi is fine for this example 1086 00:37:23,520 --> 00:37:26,880 and now here's a bunch of useful 1087 00:37:25,200 --> 00:37:28,160 functions that you guys would probably 1088 00:37:26,880 --> 00:37:30,560 be interested in 1089 00:37:28,160 --> 00:37:32,880 the first is round round is a built-in 1090 00:37:30,560 --> 00:37:35,119 function and all we do is type round 1091 00:37:32,880 --> 00:37:36,400 then add a set of parenthesis and then 1092 00:37:35,119 --> 00:37:39,200 we can pass in 1093 00:37:36,400 --> 00:37:40,240 a value or a variable as an argument and 1094 00:37:39,200 --> 00:37:43,119 the round function 1095 00:37:40,240 --> 00:37:45,280 will round that number for us so pi 1096 00:37:43,119 --> 00:37:48,480 rounded to the nearest whole integer 1097 00:37:45,280 --> 00:37:49,440 is you guessed it three now we can round 1098 00:37:48,480 --> 00:37:51,760 a number up 1099 00:37:49,440 --> 00:37:52,800 by using the seal function of the math 1100 00:37:51,760 --> 00:37:55,839 class seal 1101 00:37:52,800 --> 00:37:58,480 short for ceiling so let's print 1102 00:37:55,839 --> 00:38:00,160 our result and in order to access this 1103 00:37:58,480 --> 00:38:02,480 function within the math module 1104 00:38:00,160 --> 00:38:04,400 we would type the name of the module dot 1105 00:38:02,480 --> 00:38:05,599 and here's a bunch of functions that we 1106 00:38:04,400 --> 00:38:07,520 have access to 1107 00:38:05,599 --> 00:38:08,960 so we are looking for seal and it's 1108 00:38:07,520 --> 00:38:11,359 actually at the top here for me 1109 00:38:08,960 --> 00:38:12,880 and at seal we'll round a number up to 1110 00:38:11,359 --> 00:38:16,320 the nearest whole integer 1111 00:38:12,880 --> 00:38:18,160 so pi rounded up is four 1112 00:38:16,320 --> 00:38:19,520 then on the flip side we can round a 1113 00:38:18,160 --> 00:38:21,599 number down by using 1114 00:38:19,520 --> 00:38:22,560 floor and that is also located within 1115 00:38:21,599 --> 00:38:25,760 the math module 1116 00:38:22,560 --> 00:38:29,599 so let's type math dot floor and 1117 00:38:25,760 --> 00:38:32,320 pi rounded down would be three 1118 00:38:29,599 --> 00:38:33,839 up next i have the absolute value 1119 00:38:32,320 --> 00:38:36,960 function but it's abs 1120 00:38:33,839 --> 00:38:38,480 for short abs will give you the absolute 1121 00:38:36,960 --> 00:38:40,720 value of a number 1122 00:38:38,480 --> 00:38:42,880 so the absolute value of pi actually 1123 00:38:40,720 --> 00:38:45,520 let's make this negative 3.14 1124 00:38:42,880 --> 00:38:46,160 the absolute value will tell you how far 1125 00:38:45,520 --> 00:38:48,800 a number 1126 00:38:46,160 --> 00:38:49,680 is away from zero so if you pass in a 1127 00:38:48,800 --> 00:38:52,160 negative number 1128 00:38:49,680 --> 00:38:52,800 it will give you a positive number so 1129 00:38:52,160 --> 00:38:55,599 that is 1130 00:38:52,800 --> 00:38:56,240 abs short for absolute value next we 1131 00:38:55,599 --> 00:38:59,359 have 1132 00:38:56,240 --> 00:39:03,040 the pow function pow will raise 1133 00:38:59,359 --> 00:39:06,160 a base number to a power let's raise 1134 00:39:03,040 --> 00:39:09,200 pi to the power of two so we pass in 1135 00:39:06,160 --> 00:39:11,920 two arguments a base and an exponent 1136 00:39:09,200 --> 00:39:12,480 pi to the power of two is nine point 1137 00:39:11,920 --> 00:39:15,200 eight 1138 00:39:12,480 --> 00:39:16,240 and some change so that is the pow 1139 00:39:15,200 --> 00:39:19,680 function 1140 00:39:16,240 --> 00:39:19,680 next we have square root 1141 00:39:20,800 --> 00:39:24,079 so this is located within the math 1142 00:39:22,640 --> 00:39:27,520 module we'll type 1143 00:39:24,079 --> 00:39:29,680 math dot s q r 1144 00:39:27,520 --> 00:39:31,119 t short for square root and the square 1145 00:39:29,680 --> 00:39:34,240 root of pi 1146 00:39:31,119 --> 00:39:35,119 is let's find out oh i guess we can't 1147 00:39:34,240 --> 00:39:38,000 pass in pi 1148 00:39:35,119 --> 00:39:39,599 uh let's round a different number like 1149 00:39:38,000 --> 00:39:41,119 420 1150 00:39:39,599 --> 00:39:43,839 oh i guess that's because i made pi 1151 00:39:41,119 --> 00:39:44,320 negative okay so the square root of 420 1152 00:39:43,839 --> 00:39:47,200 is 1153 00:39:44,320 --> 00:39:47,839 20.4 and some change so that is the 1154 00:39:47,200 --> 00:39:50,160 square 1155 00:39:47,839 --> 00:39:52,000 root function all right we have two left 1156 00:39:50,160 --> 00:39:54,560 we have the max function 1157 00:39:52,000 --> 00:39:56,000 the max function will find the largest 1158 00:39:54,560 --> 00:39:58,400 of a varying amount 1159 00:39:56,000 --> 00:40:00,079 of values so let's create three 1160 00:39:58,400 --> 00:40:03,599 variables for this example 1161 00:40:00,079 --> 00:40:07,040 let's say x equals one y equals two 1162 00:40:03,599 --> 00:40:08,079 and z equals three we can use the max 1163 00:40:07,040 --> 00:40:10,800 function to find 1164 00:40:08,079 --> 00:40:11,839 the largest of these values so within a 1165 00:40:10,800 --> 00:40:15,040 print statement 1166 00:40:11,839 --> 00:40:17,200 let's use the max function and pass in 1167 00:40:15,040 --> 00:40:18,720 all of the different values or variables 1168 00:40:17,200 --> 00:40:21,119 that you would like to compare 1169 00:40:18,720 --> 00:40:22,160 so i would like to find the largest 1170 00:40:21,119 --> 00:40:26,160 value between 1171 00:40:22,160 --> 00:40:29,520 x y and z and the result in this example 1172 00:40:26,160 --> 00:40:30,720 is three now the min function will find 1173 00:40:29,520 --> 00:40:34,160 the lowest 1174 00:40:30,720 --> 00:40:35,040 so min and then pass in your values or 1175 00:40:34,160 --> 00:40:36,880 variables 1176 00:40:35,040 --> 00:40:38,319 so i would like to find the minimum 1177 00:40:36,880 --> 00:40:42,560 value between x 1178 00:40:38,319 --> 00:40:45,280 y and z and in this case it is one 1179 00:40:42,560 --> 00:40:46,640 well everybody those are a few useful 1180 00:40:45,280 --> 00:40:47,599 functions that you guys might be 1181 00:40:46,640 --> 00:40:49,119 interested in 1182 00:40:47,599 --> 00:40:50,960 if you guys would like a copy of this 1183 00:40:49,119 --> 00:40:52,319 code i will post this in the comments 1184 00:40:50,960 --> 00:40:54,560 section down below 1185 00:40:52,319 --> 00:40:56,400 but yeah those are a few useful 1186 00:40:54,560 --> 00:41:00,240 functions related to numbers 1187 00:40:56,400 --> 00:41:02,400 in python hey what's going on everybody 1188 00:41:00,240 --> 00:41:04,000 it's bro hope you're doing well and in 1189 00:41:02,400 --> 00:41:06,319 this video i'm going to explain string 1190 00:41:04,000 --> 00:41:10,240 slicing in python so sit back 1191 00:41:06,319 --> 00:41:12,720 relax and enjoy the show 1192 00:41:10,240 --> 00:41:14,160 all right then let's talk about slicing 1193 00:41:12,720 --> 00:41:16,240 slicing can be used to create a 1194 00:41:14,160 --> 00:41:17,280 substring by extracting elements from 1195 00:41:16,240 --> 00:41:19,200 another string 1196 00:41:17,280 --> 00:41:21,280 the same principle can also be applied 1197 00:41:19,200 --> 00:41:22,800 to different collections as well 1198 00:41:21,280 --> 00:41:24,960 but in this video we're going to be 1199 00:41:22,800 --> 00:41:27,440 focusing on slicing strings 1200 00:41:24,960 --> 00:41:29,040 now to slice a string we can either use 1201 00:41:27,440 --> 00:41:31,520 the indexing operator 1202 00:41:29,040 --> 00:41:33,119 which is a set of square brackets or by 1203 00:41:31,520 --> 00:41:35,440 use of the slice function 1204 00:41:33,119 --> 00:41:36,640 to create a slice object this is a 1205 00:41:35,440 --> 00:41:38,480 little more complex 1206 00:41:36,640 --> 00:41:39,760 we'll discuss the slice function near 1207 00:41:38,480 --> 00:41:43,119 the end of this video 1208 00:41:39,760 --> 00:41:43,839 so with slicing there are three optional 1209 00:41:43,119 --> 00:41:45,280 arguments 1210 00:41:43,839 --> 00:41:46,880 three fields that we can fill in 1211 00:41:45,280 --> 00:41:49,200 depending on where 1212 00:41:46,880 --> 00:41:50,240 and how we want to slice our string a 1213 00:41:49,200 --> 00:41:53,280 starting index 1214 00:41:50,240 --> 00:41:54,880 a stopping index and a step let's begin 1215 00:41:53,280 --> 00:41:57,520 with the starting index 1216 00:41:54,880 --> 00:41:58,160 now for this example let's say we have a 1217 00:41:57,520 --> 00:42:00,319 string 1218 00:41:58,160 --> 00:42:02,000 variable called name and this will store 1219 00:42:00,319 --> 00:42:04,720 a value of whatever your 1220 00:42:02,000 --> 00:42:06,240 own full name is yes i legally change my 1221 00:42:04,720 --> 00:42:07,520 name to bro code you can use your own 1222 00:42:06,240 --> 00:42:09,359 name for this if you want 1223 00:42:07,520 --> 00:42:10,560 so what i would like to do is to create 1224 00:42:09,359 --> 00:42:13,440 a substring 1225 00:42:10,560 --> 00:42:14,000 based off a sliced portion of my full 1226 00:42:13,440 --> 00:42:16,160 name 1227 00:42:14,000 --> 00:42:18,480 so what i would like to do is create a 1228 00:42:16,160 --> 00:42:20,079 substring based off of just the first 1229 00:42:18,480 --> 00:42:23,520 part of my name just bro 1230 00:42:20,079 --> 00:42:26,720 so let's begin by creating a substring 1231 00:42:23,520 --> 00:42:30,480 maybe i'll call this first 1232 00:42:26,720 --> 00:42:33,040 name equals now to slice a string 1233 00:42:30,480 --> 00:42:34,720 type in a string or the name of a 1234 00:42:33,040 --> 00:42:36,480 variable that contains your string 1235 00:42:34,720 --> 00:42:38,560 and follow this with the indexing 1236 00:42:36,480 --> 00:42:41,359 operator and like i said we'll discuss 1237 00:42:38,560 --> 00:42:42,079 the slice function a little bit later so 1238 00:42:41,359 --> 00:42:45,119 we need 1239 00:42:42,079 --> 00:42:47,599 a starting index and a stopping index 1240 00:42:45,119 --> 00:42:49,040 but we'll begin with the starting index 1241 00:42:47,599 --> 00:42:51,839 where do we want to begin 1242 00:42:49,040 --> 00:42:52,800 our slice so computers they always start 1243 00:42:51,839 --> 00:42:54,880 with zero 1244 00:42:52,800 --> 00:42:56,079 so the first character in your string 1245 00:42:54,880 --> 00:42:58,640 will have an index 1246 00:42:56,079 --> 00:42:59,920 of zero i would like my slice to begin 1247 00:42:58,640 --> 00:43:01,760 at index 0 1248 00:42:59,920 --> 00:43:03,520 so i'll list that for the starting 1249 00:43:01,760 --> 00:43:05,280 position the starting index 1250 00:43:03,520 --> 00:43:07,119 so let's test this just to be sure it 1251 00:43:05,280 --> 00:43:10,560 works i'll just print 1252 00:43:07,119 --> 00:43:11,520 first name and let's see if this prints 1253 00:43:10,560 --> 00:43:13,839 capital b 1254 00:43:11,520 --> 00:43:15,760 which it does so following that same 1255 00:43:13,839 --> 00:43:18,160 pattern the next character 1256 00:43:15,760 --> 00:43:19,520 within my string would have an index of 1257 00:43:18,160 --> 00:43:22,720 one that would be 1258 00:43:19,520 --> 00:43:26,240 r then the next index two would be 1259 00:43:22,720 --> 00:43:29,280 o and three should be an empty space 1260 00:43:26,240 --> 00:43:31,680 all right so that is the starting index 1261 00:43:29,280 --> 00:43:34,400 within string slicing if we would like 1262 00:43:31,680 --> 00:43:37,040 to slice an entire portion of our string 1263 00:43:34,400 --> 00:43:38,240 not just one letter we need a stopping 1264 00:43:37,040 --> 00:43:40,560 index as well 1265 00:43:38,240 --> 00:43:41,680 and both of these fields are separated 1266 00:43:40,560 --> 00:43:43,839 with a colon 1267 00:43:41,680 --> 00:43:46,720 so let's say i would like to slice the 1268 00:43:43,839 --> 00:43:50,000 first three characters of my string 1269 00:43:46,720 --> 00:43:52,720 so this would have an index of zero one 1270 00:43:50,000 --> 00:43:54,079 two and this isn't going to work as you 1271 00:43:52,720 --> 00:43:57,359 would imagine and i'll explain 1272 00:43:54,079 --> 00:43:59,920 why so you would think that indexes 1273 00:43:57,359 --> 00:44:01,839 0 through 2 would give us three letters 1274 00:43:59,920 --> 00:44:05,200 right but it only gives us two 1275 00:44:01,839 --> 00:44:08,720 so this first index is inclusive 1276 00:44:05,200 --> 00:44:09,920 the stopping index is exclusive so if i 1277 00:44:08,720 --> 00:44:12,079 want the first 1278 00:44:09,920 --> 00:44:12,960 three characters the first three letters 1279 00:44:12,079 --> 00:44:15,680 in the string 1280 00:44:12,960 --> 00:44:16,160 i would need the starting index to be 1281 00:44:15,680 --> 00:44:18,240 zero 1282 00:44:16,160 --> 00:44:19,839 and the stopping index to be three and 1283 00:44:18,240 --> 00:44:22,240 now this should print 1284 00:44:19,839 --> 00:44:24,160 my first name and not anything else 1285 00:44:22,240 --> 00:44:24,720 there is a shorthand way of writing this 1286 00:44:24,160 --> 00:44:27,680 too 1287 00:44:24,720 --> 00:44:28,160 so if you were to leave this first index 1288 00:44:27,680 --> 00:44:30,880 blank 1289 00:44:28,160 --> 00:44:31,920 for start well python is going to assume 1290 00:44:30,880 --> 00:44:34,560 that this is zero 1291 00:44:31,920 --> 00:44:35,200 the beginning of your string so this 1292 00:44:34,560 --> 00:44:37,599 would be 1293 00:44:35,200 --> 00:44:38,880 no different than what we had previously 1294 00:44:37,599 --> 00:44:41,040 just having colon 1295 00:44:38,880 --> 00:44:42,160 three now let's try and create a 1296 00:44:41,040 --> 00:44:45,280 separate substring 1297 00:44:42,160 --> 00:44:49,280 for a last name so last 1298 00:44:45,280 --> 00:44:52,319 name name now we need a 1299 00:44:49,280 --> 00:44:54,880 starting index and a stopping index so 1300 00:44:52,319 --> 00:44:56,000 we need the starting index at least so 1301 00:44:54,880 --> 00:44:59,119 this would be 1302 00:44:56,000 --> 00:45:01,599 zero one two three four 1303 00:44:59,119 --> 00:45:02,640 four colon and where do we want this to 1304 00:45:01,599 --> 00:45:05,680 stop 1305 00:45:02,640 --> 00:45:08,240 zero one two three four five 1306 00:45:05,680 --> 00:45:09,119 six seven and remember that this is 1307 00:45:08,240 --> 00:45:11,599 exclusive 1308 00:45:09,119 --> 00:45:13,440 so we're going to place eight here and 1309 00:45:11,599 --> 00:45:16,079 now i would like to display 1310 00:45:13,440 --> 00:45:16,960 last name and this should just display 1311 00:45:16,079 --> 00:45:19,359 code 1312 00:45:16,960 --> 00:45:20,079 and a shortcut is that if you were to 1313 00:45:19,359 --> 00:45:22,400 leave 1314 00:45:20,079 --> 00:45:24,560 stop blank but you still have that colon 1315 00:45:22,400 --> 00:45:27,200 this is a shorthand way of writing 1316 00:45:24,560 --> 00:45:28,160 i would like every character starting at 1317 00:45:27,200 --> 00:45:30,560 index four 1318 00:45:28,160 --> 00:45:32,000 and everything after until the end so 1319 00:45:30,560 --> 00:45:34,319 there would be no change to this 1320 00:45:32,000 --> 00:45:36,079 as well you can write it either way okay 1321 00:45:34,319 --> 00:45:38,640 so we still have to discuss 1322 00:45:36,079 --> 00:45:40,079 step step is an optional field that we 1323 00:45:38,640 --> 00:45:42,800 can set a value to 1324 00:45:40,079 --> 00:45:43,680 so step is how much we're increasing our 1325 00:45:42,800 --> 00:45:46,720 index by 1326 00:45:43,680 --> 00:45:48,000 between starting and stopping so it is 1327 00:45:46,720 --> 00:45:50,640 entirely possible 1328 00:45:48,000 --> 00:45:51,119 to create a substring that will count 1329 00:45:50,640 --> 00:45:53,440 only 1330 00:45:51,119 --> 00:45:55,440 every second character after the first 1331 00:45:53,440 --> 00:45:57,520 normally step is one by default 1332 00:45:55,440 --> 00:45:58,640 but if we were to set that to two we're 1333 00:45:57,520 --> 00:46:01,440 going to count only 1334 00:45:58,640 --> 00:46:02,880 every second character so let's try this 1335 00:46:01,440 --> 00:46:05,200 maybe i'll create a new 1336 00:46:02,880 --> 00:46:06,480 variable called funky name because i 1337 00:46:05,200 --> 00:46:07,680 really don't know what else to call this 1338 00:46:06,480 --> 00:46:10,560 for this example 1339 00:46:07,680 --> 00:46:11,440 let's say funky name equals name and i'm 1340 00:46:10,560 --> 00:46:14,000 going to count 1341 00:46:11,440 --> 00:46:14,880 only every second character including 1342 00:46:14,000 --> 00:46:18,480 the first 1343 00:46:14,880 --> 00:46:20,640 so the indexes would be zero colon 1344 00:46:18,480 --> 00:46:21,839 eight for my example but if you're using 1345 00:46:20,640 --> 00:46:23,200 your own name that's probably going to 1346 00:46:21,839 --> 00:46:26,240 be something different 1347 00:46:23,200 --> 00:46:28,000 so the default first step is one and if 1348 00:46:26,240 --> 00:46:30,480 i were to display funky name as it 1349 00:46:28,000 --> 00:46:31,119 is currently this will just display my 1350 00:46:30,480 --> 00:46:33,920 full name 1351 00:46:31,119 --> 00:46:34,560 but if we set step to two this will only 1352 00:46:33,920 --> 00:46:36,400 display 1353 00:46:34,560 --> 00:46:38,640 every second character including the 1354 00:46:36,400 --> 00:46:42,319 first so funky name is now 1355 00:46:38,640 --> 00:46:44,960 for me at least b o c d now if step 1356 00:46:42,319 --> 00:46:46,880 was three this will print only every 1357 00:46:44,960 --> 00:46:50,160 third character including the first 1358 00:46:46,880 --> 00:46:50,800 where it's b space d a shorthand way of 1359 00:46:50,160 --> 00:46:53,920 writing this 1360 00:46:50,800 --> 00:46:55,920 is that you could leave start and stop 1361 00:46:53,920 --> 00:46:57,280 empty but the important thing is to have 1362 00:46:55,920 --> 00:46:59,119 these two colons 1363 00:46:57,280 --> 00:47:00,720 python is going to assume that if you 1364 00:46:59,119 --> 00:47:03,520 leave your starting index 1365 00:47:00,720 --> 00:47:04,240 empty you're referring to index 0 the 1366 00:47:03,520 --> 00:47:06,800 beginning 1367 00:47:04,240 --> 00:47:07,599 if your second index your stopping index 1368 00:47:06,800 --> 00:47:10,319 is empty 1369 00:47:07,599 --> 00:47:11,440 python will assume this will be the very 1370 00:47:10,319 --> 00:47:13,520 end of your string 1371 00:47:11,440 --> 00:47:14,560 so just having two colons and then step 1372 00:47:13,520 --> 00:47:16,880 three would be 1373 00:47:14,560 --> 00:47:19,040 no different than what we had previously 1374 00:47:16,880 --> 00:47:22,000 or this next part i'm going to explain 1375 00:47:19,040 --> 00:47:24,240 how we can reverse a string in python so 1376 00:47:22,000 --> 00:47:24,720 let's create a new substring variable 1377 00:47:24,240 --> 00:47:27,680 called 1378 00:47:24,720 --> 00:47:29,200 reversed name equals name i would like 1379 00:47:27,680 --> 00:47:32,240 to use the entire string 1380 00:47:29,200 --> 00:47:33,359 so i can just use two colons python will 1381 00:47:32,240 --> 00:47:35,680 fill in the rest 1382 00:47:33,359 --> 00:47:36,800 so for the step this is going to be 1383 00:47:35,680 --> 00:47:38,319 negative one 1384 00:47:36,800 --> 00:47:40,240 it's kind of like we're counting 1385 00:47:38,319 --> 00:47:43,440 backwards and we're going to create 1386 00:47:40,240 --> 00:47:44,240 a new sub string based off my name but 1387 00:47:43,440 --> 00:47:47,200 in reverse 1388 00:47:44,240 --> 00:47:48,400 so if i were to print my reversed name 1389 00:47:47,200 --> 00:47:50,640 this is now 1390 00:47:48,400 --> 00:47:51,440 edok orb i think that's how you 1391 00:47:50,640 --> 00:47:53,920 pronounce it 1392 00:47:51,440 --> 00:47:55,760 so that's how to reverse a string in 1393 00:47:53,920 --> 00:47:57,760 python using slicing 1394 00:47:55,760 --> 00:47:59,680 now let's move on to part two of this 1395 00:47:57,760 --> 00:48:01,760 video i'm going to be explaining the 1396 00:47:59,680 --> 00:48:03,520 slice function and we can use the slice 1397 00:48:01,760 --> 00:48:05,599 function to create a slice 1398 00:48:03,520 --> 00:48:07,040 object which is actually reusable so 1399 00:48:05,599 --> 00:48:08,640 that's somewhat convenient 1400 00:48:07,040 --> 00:48:11,440 let's take a different scenario this 1401 00:48:08,640 --> 00:48:16,000 time let's say we have a website url 1402 00:48:11,440 --> 00:48:18,160 website equals http colon2forwardslashes 1403 00:48:16,000 --> 00:48:20,079 google.com but use whatever website you 1404 00:48:18,160 --> 00:48:20,720 want what i would like to do for this 1405 00:48:20,079 --> 00:48:23,599 program 1406 00:48:20,720 --> 00:48:24,960 is remove and create a substring based 1407 00:48:23,599 --> 00:48:27,040 off the website name 1408 00:48:24,960 --> 00:48:30,480 and not anything else i would like to 1409 00:48:27,040 --> 00:48:31,760 exclude the http portion as well as the 1410 00:48:30,480 --> 00:48:34,160 dot-com portion 1411 00:48:31,760 --> 00:48:35,359 of this website url so let's break it 1412 00:48:34,160 --> 00:48:37,599 down step by step 1413 00:48:35,359 --> 00:48:40,079 our first step is to create a slice 1414 00:48:37,599 --> 00:48:40,880 object so i will name the slice object 1415 00:48:40,079 --> 00:48:43,520 as slice 1416 00:48:40,880 --> 00:48:44,400 slice equals and i will invoke the slice 1417 00:48:43,520 --> 00:48:46,559 function 1418 00:48:44,400 --> 00:48:48,400 so within the parentheses of the slice 1419 00:48:46,559 --> 00:48:52,079 function we can add up to 1420 00:48:48,400 --> 00:48:53,839 three values a start a stop and a step 1421 00:48:52,079 --> 00:48:56,480 exactly the same as what we did with 1422 00:48:53,839 --> 00:48:59,040 indexing however with the slice function 1423 00:48:56,480 --> 00:48:59,520 we separate each of these values with a 1424 00:48:59,040 --> 00:49:01,520 comma 1425 00:48:59,520 --> 00:49:03,359 instead of a colon but it works the same 1426 00:49:01,520 --> 00:49:05,520 way so we would like 1427 00:49:03,359 --> 00:49:06,800 our substring to begin where the name of 1428 00:49:05,520 --> 00:49:10,400 the website begins 1429 00:49:06,800 --> 00:49:13,599 so we would like our start to be zero 1430 00:49:10,400 --> 00:49:15,839 one two three four five six seven 1431 00:49:13,599 --> 00:49:17,200 so i will pass in seven for where my 1432 00:49:15,839 --> 00:49:19,680 substring will begin 1433 00:49:17,200 --> 00:49:21,359 and now each value within the slice 1434 00:49:19,680 --> 00:49:23,839 function will be separated with 1435 00:49:21,359 --> 00:49:25,920 a comma instead of a colon like what we 1436 00:49:23,839 --> 00:49:28,400 did with the indexing operator 1437 00:49:25,920 --> 00:49:29,520 now for the stop index that's going to 1438 00:49:28,400 --> 00:49:31,520 be a little bit tricky 1439 00:49:29,520 --> 00:49:32,800 and let me explain why one issue that 1440 00:49:31,520 --> 00:49:34,880 we're going to run into 1441 00:49:32,800 --> 00:49:36,559 is that not all website names are 1442 00:49:34,880 --> 00:49:37,599 consistent with their length they can 1443 00:49:36,559 --> 00:49:40,079 really vary 1444 00:49:37,599 --> 00:49:41,599 so it is not possible for us to use the 1445 00:49:40,079 --> 00:49:44,319 same stopping index 1446 00:49:41,599 --> 00:49:46,240 for each of these website urls so one 1447 00:49:44,319 --> 00:49:48,400 way in which we can account for that 1448 00:49:46,240 --> 00:49:51,119 is to use what is called a negative 1449 00:49:48,400 --> 00:49:53,760 index each character within a string 1450 00:49:51,119 --> 00:49:54,880 has a positive index as well as a 1451 00:49:53,760 --> 00:49:57,440 negative index 1452 00:49:54,880 --> 00:49:58,880 and a negative index works almost 1453 00:49:57,440 --> 00:50:01,440 exactly the same way 1454 00:49:58,880 --> 00:50:03,520 except the character most on the right 1455 00:50:01,440 --> 00:50:06,240 begins with a negative index 1456 00:50:03,520 --> 00:50:06,800 of minus one and the character on the 1457 00:50:06,240 --> 00:50:09,200 left 1458 00:50:06,800 --> 00:50:10,640 of that would be negative two negative 1459 00:50:09,200 --> 00:50:12,720 three and negative four 1460 00:50:10,640 --> 00:50:14,640 it's as if you're counting backwards so 1461 00:50:12,720 --> 00:50:15,920 with indexing we can use some 1462 00:50:14,640 --> 00:50:18,640 combination of 1463 00:50:15,920 --> 00:50:19,520 positive and negative indexing so i 1464 00:50:18,640 --> 00:50:22,400 would like 1465 00:50:19,520 --> 00:50:23,200 my substring to end where this dot is so 1466 00:50:22,400 --> 00:50:26,240 this would have 1467 00:50:23,200 --> 00:50:28,880 a index of minus one minus two 1468 00:50:26,240 --> 00:50:30,400 minus three minus four and remember that 1469 00:50:28,880 --> 00:50:32,880 this is exclusive 1470 00:50:30,400 --> 00:50:34,079 so i would like my slice to begin at 1471 00:50:32,880 --> 00:50:37,280 index seven 1472 00:50:34,079 --> 00:50:38,559 and a negative index of four for the 1473 00:50:37,280 --> 00:50:40,480 stopping position 1474 00:50:38,559 --> 00:50:41,920 so that will give me just the website 1475 00:50:40,480 --> 00:50:44,960 name and remove 1476 00:50:41,920 --> 00:50:47,119 the http portion as well as the dot com 1477 00:50:44,960 --> 00:50:49,359 portion of my website url 1478 00:50:47,119 --> 00:50:50,559 and now we have a slice object and we 1479 00:50:49,359 --> 00:50:53,520 can reuse this 1480 00:50:50,559 --> 00:50:54,880 so to apply your slice object type in 1481 00:50:53,520 --> 00:50:57,280 the name your string 1482 00:50:54,880 --> 00:50:58,319 index operator and place your slice 1483 00:50:57,280 --> 00:51:01,599 right within here 1484 00:50:58,319 --> 00:51:03,920 and let's print this so print 1485 00:51:01,599 --> 00:51:05,359 our website and apply the slice to it 1486 00:51:03,920 --> 00:51:07,520 and now this should give us 1487 00:51:05,359 --> 00:51:09,680 just the website name and now let's test 1488 00:51:07,520 --> 00:51:11,760 this with maybe another website 1489 00:51:09,680 --> 00:51:14,960 i'll rename this as website one and 1490 00:51:11,760 --> 00:51:17,760 let's create website2 1491 00:51:14,960 --> 00:51:18,640 and let's try a different website name 1492 00:51:17,760 --> 00:51:22,400 for goodmeasure 1493 00:51:18,640 --> 00:51:23,680 so let's try wikipedia so we can reuse 1494 00:51:22,400 --> 00:51:26,319 our slice object 1495 00:51:23,680 --> 00:51:27,359 and we will apply our slice to website 1496 00:51:26,319 --> 00:51:30,400 two this time 1497 00:51:27,359 --> 00:51:32,240 and this should remove the url portions 1498 00:51:30,400 --> 00:51:34,960 http as well as 1499 00:51:32,240 --> 00:51:36,240 com all right everybody so that is 1500 00:51:34,960 --> 00:51:38,240 slicing strings 1501 00:51:36,240 --> 00:51:39,359 in python you can either use the 1502 00:51:38,240 --> 00:51:42,160 indexing operator 1503 00:51:39,359 --> 00:51:43,119 or the slice function to create a slice 1504 00:51:42,160 --> 00:51:44,480 object 1505 00:51:43,119 --> 00:51:46,480 if you would like a copy of all this 1506 00:51:44,480 --> 00:51:48,079 code i will post all of this in the 1507 00:51:46,480 --> 00:51:50,160 comments section down below 1508 00:51:48,079 --> 00:51:52,880 but yeah that is how string slicing 1509 00:51:50,160 --> 00:51:54,800 works in python 1510 00:51:52,880 --> 00:51:56,559 hey what's going on people it's you bro 1511 00:51:54,800 --> 00:51:57,599 hope you're doing well and in this video 1512 00:51:56,559 --> 00:51:59,760 i'm going to explain 1513 00:51:57,599 --> 00:52:01,520 if statements else if statements and 1514 00:51:59,760 --> 00:52:06,160 else statements and python so 1515 00:52:01,520 --> 00:52:06,160 sit back relax and enjoy the show 1516 00:52:06,480 --> 00:52:10,640 all right people let's talk about if 1517 00:52:08,240 --> 00:52:11,119 statements an if statement is a block of 1518 00:52:10,640 --> 00:52:13,680 code 1519 00:52:11,119 --> 00:52:14,640 that will execute only if its condition 1520 00:52:13,680 --> 00:52:16,640 is true 1521 00:52:14,640 --> 00:52:18,400 it's a very basic form of decision 1522 00:52:16,640 --> 00:52:21,119 making with programming 1523 00:52:18,400 --> 00:52:22,640 for this example let's create a prompt 1524 00:52:21,119 --> 00:52:24,400 where we will ask a user 1525 00:52:22,640 --> 00:52:26,960 for their age and depending on what 1526 00:52:24,400 --> 00:52:29,599 their age is we will print a response 1527 00:52:26,960 --> 00:52:30,800 so let's say age equals input and then 1528 00:52:29,599 --> 00:52:33,200 we will add a prompt 1529 00:52:30,800 --> 00:52:33,920 how old are you now this will return a 1530 00:52:33,200 --> 00:52:36,079 string 1531 00:52:33,920 --> 00:52:37,599 so we should probably cast this as an 1532 00:52:36,079 --> 00:52:39,839 integer data type 1533 00:52:37,599 --> 00:52:42,000 so we now have a variable called age 1534 00:52:39,839 --> 00:52:43,839 where we will accept some user input 1535 00:52:42,000 --> 00:52:45,599 let's check somebody's age with a 1536 00:52:43,839 --> 00:52:47,680 variety of if statements 1537 00:52:45,599 --> 00:52:49,680 so let's check to see if somebody's age 1538 00:52:47,680 --> 00:52:51,839 is greater than or equal to 18 1539 00:52:49,680 --> 00:52:53,200 so to create an if statement type if and 1540 00:52:51,839 --> 00:52:56,400 then some sort of condition 1541 00:52:53,200 --> 00:52:58,720 if age is greater than or equal to 18 1542 00:52:56,400 --> 00:52:59,599 to finish this if statement add a colon 1543 00:52:58,720 --> 00:53:01,359 at the end 1544 00:52:59,599 --> 00:53:03,520 now pay attention to this if i move down 1545 00:53:01,359 --> 00:53:04,400 to the next line you'll notice that my 1546 00:53:03,520 --> 00:53:07,440 cursor is now 1547 00:53:04,400 --> 00:53:10,240 indented any indented code underneath an 1548 00:53:07,440 --> 00:53:11,520 if statement is the block of code for 1549 00:53:10,240 --> 00:53:13,760 that if statement 1550 00:53:11,520 --> 00:53:14,960 so if this condition is true we will 1551 00:53:13,760 --> 00:53:17,200 execute some 1552 00:53:14,960 --> 00:53:19,440 block of code if it's false we'll skip 1553 00:53:17,200 --> 00:53:20,880 over it so if somebody's age is greater 1554 00:53:19,440 --> 00:53:24,319 than or equal to 18 1555 00:53:20,880 --> 00:53:27,680 let's print you are an 1556 00:53:24,319 --> 00:53:28,160 adult and let's try this program how old 1557 00:53:27,680 --> 00:53:31,040 are you 1558 00:53:28,160 --> 00:53:32,319 let's say i'm 21 so my condition when we 1559 00:53:31,040 --> 00:53:33,599 get to the if statement 1560 00:53:32,319 --> 00:53:35,599 is going to check to see if this 1561 00:53:33,599 --> 00:53:36,079 condition is true if it is true we're 1562 00:53:35,599 --> 00:53:38,559 going to 1563 00:53:36,079 --> 00:53:39,680 execute this block of code if it's not 1564 00:53:38,559 --> 00:53:42,319 we skip over it 1565 00:53:39,680 --> 00:53:43,359 so how old are you let's say i'm 21 i'm 1566 00:53:42,319 --> 00:53:45,680 going to hit enter 1567 00:53:43,359 --> 00:53:48,000 and my if statement is true so we 1568 00:53:45,680 --> 00:53:49,839 execute this block of code which prints 1569 00:53:48,000 --> 00:53:51,280 you are an adult so what if this 1570 00:53:49,839 --> 00:53:53,200 condition is false 1571 00:53:51,280 --> 00:53:54,880 let's say i'm 12. well then we're just 1572 00:53:53,200 --> 00:53:56,240 going to skip over this block of code 1573 00:53:54,880 --> 00:53:56,800 and continue on with the rest of the 1574 00:53:56,240 --> 00:53:58,480 program 1575 00:53:56,800 --> 00:54:00,000 if you would like your program to take 1576 00:53:58,480 --> 00:54:02,960 some other course of action 1577 00:54:00,000 --> 00:54:03,359 if this condition is false we can add an 1578 00:54:02,960 --> 00:54:06,160 else 1579 00:54:03,359 --> 00:54:06,800 statement so after the if statement add 1580 00:54:06,160 --> 00:54:08,640 else 1581 00:54:06,800 --> 00:54:10,240 make sure you get the indentation right 1582 00:54:08,640 --> 00:54:11,839 you don't want this within the block of 1583 00:54:10,240 --> 00:54:14,800 code for the if statement 1584 00:54:11,839 --> 00:54:15,200 so if this condition is false we can 1585 00:54:14,800 --> 00:54:17,359 take 1586 00:54:15,200 --> 00:54:18,319 some other course of action so if 1587 00:54:17,359 --> 00:54:22,960 somebody's age 1588 00:54:18,319 --> 00:54:26,400 is less than 18 let's print 1589 00:54:22,960 --> 00:54:28,400 you are a child 1590 00:54:26,400 --> 00:54:29,440 so let's try this again i'm going to 1591 00:54:28,400 --> 00:54:31,520 enter that i 1592 00:54:29,440 --> 00:54:33,119 am 12 years old not really but let's 1593 00:54:31,520 --> 00:54:35,760 just pretend i am 1594 00:54:33,119 --> 00:54:36,160 so this will now print you are a child 1595 00:54:35,760 --> 00:54:38,480 if 1596 00:54:36,160 --> 00:54:40,799 this condition is false perform this 1597 00:54:38,480 --> 00:54:43,280 block of code for the else statement 1598 00:54:40,799 --> 00:54:45,440 so with if statements we can check more 1599 00:54:43,280 --> 00:54:46,960 than one condition before reaching the 1600 00:54:45,440 --> 00:54:50,240 else statement by using an 1601 00:54:46,960 --> 00:54:53,280 else if statement and we add that after 1602 00:54:50,240 --> 00:54:56,240 an if statement so it's shortened to e l 1603 00:54:53,280 --> 00:54:57,520 i f for else if so let's check to see if 1604 00:54:56,240 --> 00:55:00,799 somebody's age 1605 00:54:57,520 --> 00:55:03,839 is less than zero for some reason 1606 00:55:00,799 --> 00:55:08,880 so let's print 1607 00:55:03,839 --> 00:55:11,359 you haven't been born yet 1608 00:55:08,880 --> 00:55:12,480 so with if statements we start at the 1609 00:55:11,359 --> 00:55:14,640 top we first check 1610 00:55:12,480 --> 00:55:15,839 the if statement if this is false we 1611 00:55:14,640 --> 00:55:18,000 move down to the next 1612 00:55:15,839 --> 00:55:19,280 statement to check then we check our 1613 00:55:18,000 --> 00:55:21,920 else if statement 1614 00:55:19,280 --> 00:55:23,119 if this is also false then we resort to 1615 00:55:21,920 --> 00:55:25,119 the else statement 1616 00:55:23,119 --> 00:55:27,599 the else statement is sort of like a 1617 00:55:25,119 --> 00:55:30,160 last resort if all conditions 1618 00:55:27,599 --> 00:55:31,040 evaluate to be false then we execute the 1619 00:55:30,160 --> 00:55:33,760 else statement 1620 00:55:31,040 --> 00:55:34,880 so let's try this how old are you let's 1621 00:55:33,760 --> 00:55:38,400 say i'm negative 1622 00:55:34,880 --> 00:55:40,720 1 years old you haven't been born yet 1623 00:55:38,400 --> 00:55:42,880 so with if statements we always begin 1624 00:55:40,720 --> 00:55:44,799 with an if statement and we check 1625 00:55:42,880 --> 00:55:46,400 that condition if that condition 1626 00:55:44,799 --> 00:55:48,640 evaluates to be false 1627 00:55:46,400 --> 00:55:50,880 then we will skip this block of code and 1628 00:55:48,640 --> 00:55:53,359 move down to the else if statement 1629 00:55:50,880 --> 00:55:54,319 if there is one it's optional if this 1630 00:55:53,359 --> 00:55:56,960 condition 1631 00:55:54,319 --> 00:55:58,880 is also false then we will use our else 1632 00:55:56,960 --> 00:56:00,799 statement as a last resort 1633 00:55:58,880 --> 00:56:02,720 if there is one if there is no else 1634 00:56:00,799 --> 00:56:04,160 statement then we just skip over all of 1635 00:56:02,720 --> 00:56:05,359 this then and continue on with the rest 1636 00:56:04,160 --> 00:56:07,680 of the program 1637 00:56:05,359 --> 00:56:10,240 so you can add more than one else if 1638 00:56:07,680 --> 00:56:14,160 statement so let's check something else 1639 00:56:10,240 --> 00:56:17,599 let's check to see else if age 1640 00:56:14,160 --> 00:56:18,160 is equal to 100 so if you need to check 1641 00:56:17,599 --> 00:56:21,119 to see 1642 00:56:18,160 --> 00:56:21,839 if a value is equal to a particular 1643 00:56:21,119 --> 00:56:24,319 value 1644 00:56:21,839 --> 00:56:25,760 make sure you use double equal signs 1645 00:56:24,319 --> 00:56:29,200 this is the comparison 1646 00:56:25,760 --> 00:56:31,440 operator for equality if you use just 1647 00:56:29,200 --> 00:56:32,319 one equal sign that's the assignment 1648 00:56:31,440 --> 00:56:34,319 operator and 1649 00:56:32,319 --> 00:56:35,839 python thinks you're attempting to set 1650 00:56:34,319 --> 00:56:38,079 age equal to 100 1651 00:56:35,839 --> 00:56:39,119 so if you want to check to see if age is 1652 00:56:38,079 --> 00:56:42,400 equal to 100 1653 00:56:39,119 --> 00:56:44,480 then use double equals so let's check to 1654 00:56:42,400 --> 00:56:48,240 see if somebody's age is 1655 00:56:44,480 --> 00:56:52,400 equal to 100 so let's print 1656 00:56:48,240 --> 00:56:54,960 you are a century 1657 00:56:52,400 --> 00:56:56,400 old now pay attention to this if i were 1658 00:56:54,960 --> 00:56:59,599 to run this program 1659 00:56:56,400 --> 00:57:02,000 how old are you let's say i'm 100. so 1660 00:56:59,599 --> 00:57:03,520 this is printing you are an adult that's 1661 00:57:02,000 --> 00:57:06,079 because we first check 1662 00:57:03,520 --> 00:57:07,520 our if statement and well we're 100 1663 00:57:06,079 --> 00:57:10,079 years old and 100 1664 00:57:07,520 --> 00:57:10,720 is greater than or equal to 18. so we 1665 00:57:10,079 --> 00:57:12,720 print 1666 00:57:10,720 --> 00:57:14,480 this block of code we execute this block 1667 00:57:12,720 --> 00:57:17,839 of code and then we skip 1668 00:57:14,480 --> 00:57:19,119 everything else even though age is equal 1669 00:57:17,839 --> 00:57:22,079 to 100 within 1670 00:57:19,119 --> 00:57:23,119 our else if statement so the order of 1671 00:57:22,079 --> 00:57:25,680 your if statements 1672 00:57:23,119 --> 00:57:26,640 does matter so let's change this around 1673 00:57:25,680 --> 00:57:30,160 let's add this 1674 00:57:26,640 --> 00:57:30,559 to the very beginning and this will be 1675 00:57:30,160 --> 00:57:33,599 our 1676 00:57:30,559 --> 00:57:35,520 if statement and we will set age 1677 00:57:33,599 --> 00:57:36,720 is greater than or equal to 18 to be an 1678 00:57:35,520 --> 00:57:38,720 else if statement 1679 00:57:36,720 --> 00:57:40,559 so we're first going to check to see if 1680 00:57:38,720 --> 00:57:43,440 age is equal to 100 1681 00:57:40,559 --> 00:57:45,359 if not we will check to see if age is 1682 00:57:43,440 --> 00:57:47,040 greater than or equal to 18 1683 00:57:45,359 --> 00:57:48,880 and we just follow that order so let's 1684 00:57:47,040 --> 00:57:52,000 try this again how old are you 1685 00:57:48,880 --> 00:57:54,960 i am 100 years old you are 1686 00:57:52,000 --> 00:57:55,599 a century old so we first check our if 1687 00:57:54,960 --> 00:57:57,359 statement 1688 00:57:55,599 --> 00:57:59,119 if this condition is true we will 1689 00:57:57,359 --> 00:58:01,119 execute this block of code 1690 00:57:59,119 --> 00:58:03,520 if it's false we just go down the order 1691 00:58:01,119 --> 00:58:06,160 till we reach our else statement 1692 00:58:03,520 --> 00:58:08,000 well everybody that is the basics of if 1693 00:58:06,160 --> 00:58:10,640 statements we have if statements 1694 00:58:08,000 --> 00:58:12,880 else if statements and else statements 1695 00:58:10,640 --> 00:58:14,720 so if you would like a copy of this code 1696 00:58:12,880 --> 00:58:15,280 i will post this in the comments down 1697 00:58:14,720 --> 00:58:17,599 below 1698 00:58:15,280 --> 00:58:19,680 but yeah that is how if statements work 1699 00:58:17,599 --> 00:58:21,680 in python 1700 00:58:19,680 --> 00:58:23,280 hey what's going on everybody it's bro 1701 00:58:21,680 --> 00:58:24,160 hope you're doing well in this video i'm 1702 00:58:23,280 --> 00:58:26,880 going to explain 1703 00:58:24,160 --> 00:58:27,680 logical operators in python so sit back 1704 00:58:26,880 --> 00:58:30,559 relax 1705 00:58:27,680 --> 00:58:30,559 and enjoy the show 1706 00:58:30,720 --> 00:58:33,760 all right everybody let's talk about 1707 00:58:32,480 --> 00:58:36,640 logical operators 1708 00:58:33,760 --> 00:58:37,920 these are used to check if two or more 1709 00:58:36,640 --> 00:58:40,640 conditional statements 1710 00:58:37,920 --> 00:58:41,760 are true so we'll be discussing the and 1711 00:58:40,640 --> 00:58:44,000 as well as the or 1712 00:58:41,760 --> 00:58:44,799 logical operators there's also a third 1713 00:58:44,000 --> 00:58:46,240 one called 1714 00:58:44,799 --> 00:58:48,000 not but that works a little bit 1715 00:58:46,240 --> 00:58:50,240 different and i'll explain why 1716 00:58:48,000 --> 00:58:51,680 so let's create a program to demonstrate 1717 00:58:50,240 --> 00:58:53,119 this let's create a program 1718 00:58:51,680 --> 00:58:55,119 where we will ask somebody for the 1719 00:58:53,119 --> 00:58:56,640 temperature outside and then depending 1720 00:58:55,119 --> 00:58:57,440 on the temperature if it falls within a 1721 00:58:56,640 --> 00:58:59,599 certain range 1722 00:58:57,440 --> 00:59:01,839 we will print a message so we'll need to 1723 00:58:59,599 --> 00:59:02,640 use and as well as or logical operators 1724 00:59:01,839 --> 00:59:04,559 to do this 1725 00:59:02,640 --> 00:59:06,640 so here i have a prompt temp for 1726 00:59:04,559 --> 00:59:08,319 temperature equals input what is the 1727 00:59:06,640 --> 00:59:10,640 temperature outside 1728 00:59:08,319 --> 00:59:11,839 so i'm going to cast this as an integer 1729 00:59:10,640 --> 00:59:13,839 data type 1730 00:59:11,839 --> 00:59:15,440 so what if we want to check to see if 1731 00:59:13,839 --> 00:59:16,160 our temperature is within a certain 1732 00:59:15,440 --> 00:59:19,040 range 1733 00:59:16,160 --> 00:59:19,680 we can use the and logical operator to 1734 00:59:19,040 --> 00:59:21,839 check 1735 00:59:19,680 --> 00:59:23,040 two or more conditional statements this 1736 00:59:21,839 --> 00:59:26,400 is how we might do that 1737 00:59:23,040 --> 00:59:28,960 let's check if temp 1738 00:59:26,400 --> 00:59:30,559 is greater than or equal to zero zero 1739 00:59:28,960 --> 00:59:32,559 degrees celsius 1740 00:59:30,559 --> 00:59:34,079 and we can check another condition too 1741 00:59:32,559 --> 00:59:36,720 if temp 1742 00:59:34,079 --> 00:59:38,000 is less than or equal to 30 degrees 1743 00:59:36,720 --> 00:59:40,559 celsius 1744 00:59:38,000 --> 00:59:42,720 so we need to add a colon at the end to 1745 00:59:40,559 --> 00:59:44,960 finish this if statement 1746 00:59:42,720 --> 00:59:46,319 and let me just fix some of the spacing 1747 00:59:44,960 --> 00:59:48,720 okay so 1748 00:59:46,319 --> 00:59:51,280 with the and logical operator in order 1749 00:59:48,720 --> 00:59:53,920 for this entire condition to be true 1750 00:59:51,280 --> 00:59:54,880 both conditions must be true this needs 1751 00:59:53,920 --> 00:59:57,200 to be true 1752 00:59:54,880 --> 00:59:57,920 and this needs to be true so if our 1753 00:59:57,200 --> 01:00:01,440 temperature 1754 00:59:57,920 --> 01:00:04,480 is equal to or above 0 as well 1755 01:00:01,440 --> 01:00:05,680 as being less than or equal to 30 that 1756 01:00:04,480 --> 01:00:07,680 means our temperature 1757 01:00:05,680 --> 01:00:08,720 falls within a certain range so let's 1758 01:00:07,680 --> 01:00:12,799 print a message 1759 01:00:08,720 --> 01:00:16,160 such as the temperature 1760 01:00:12,799 --> 01:00:16,160 is good today 1761 01:00:16,319 --> 01:00:23,440 print go 1762 01:00:19,760 --> 01:00:25,280 outside all right let's try it 1763 01:00:23,440 --> 01:00:27,680 so what is the temperature outside let's 1764 01:00:25,280 --> 01:00:30,000 say it's 20 degrees celsius 1765 01:00:27,680 --> 01:00:30,880 well the temperature is good today go 1766 01:00:30,000 --> 01:00:33,359 outside 1767 01:00:30,880 --> 01:00:34,880 this condition is true 20 is greater 1768 01:00:33,359 --> 01:00:37,599 than or equal to zero 1769 01:00:34,880 --> 01:00:38,319 and temperature is less than or equal to 1770 01:00:37,599 --> 01:00:40,240 30. 1771 01:00:38,319 --> 01:00:42,079 let's try a different temperature like 1772 01:00:40,240 --> 01:00:44,640 negative 20 degrees celsius so it's 1773 01:00:42,079 --> 01:00:46,640 really freaking cold outside right now 1774 01:00:44,640 --> 01:00:48,000 as you can see nothing happened because 1775 01:00:46,640 --> 01:00:50,880 our first condition 1776 01:00:48,000 --> 01:00:52,559 was false our temperature is not greater 1777 01:00:50,880 --> 01:00:54,880 than or equal to zero 1778 01:00:52,559 --> 01:00:56,079 but our temperature is less than or 1779 01:00:54,880 --> 01:00:58,000 equal to 30 so 1780 01:00:56,079 --> 01:00:59,520 our second condition is true but our 1781 01:00:58,000 --> 01:01:01,119 first condition was false 1782 01:00:59,520 --> 01:01:03,119 and in order for this entire statement 1783 01:01:01,119 --> 01:01:04,400 to be true since we're using the and 1784 01:01:03,119 --> 01:01:06,640 logical operator 1785 01:01:04,400 --> 01:01:07,680 both conditions must be true in order 1786 01:01:06,640 --> 01:01:10,000 for this statement 1787 01:01:07,680 --> 01:01:11,119 to be true now let's discuss the or 1788 01:01:10,000 --> 01:01:13,200 logical operator 1789 01:01:11,119 --> 01:01:14,160 we can check to see if our temperature 1790 01:01:13,200 --> 01:01:16,720 is below 1791 01:01:14,160 --> 01:01:17,920 or above a certain range so let's write 1792 01:01:16,720 --> 01:01:21,839 else if 1793 01:01:17,920 --> 01:01:25,359 temp is less than zero or 1794 01:01:21,839 --> 01:01:26,720 temp is greater than 30. so that means 1795 01:01:25,359 --> 01:01:29,440 it's going to be either 1796 01:01:26,720 --> 01:01:31,920 very cold outside or very hot outside so 1797 01:01:29,440 --> 01:01:35,440 let's print a message 1798 01:01:31,920 --> 01:01:35,440 the temperature 1799 01:01:35,599 --> 01:01:39,839 is bad today 1800 01:01:39,920 --> 01:01:48,240 print stay 1801 01:01:44,000 --> 01:01:48,240 inside all right let's try this again 1802 01:01:49,040 --> 01:01:53,359 what is the temperature outside it is 1803 01:01:50,880 --> 01:01:55,839 negative 20 degrees outside 1804 01:01:53,359 --> 01:01:56,559 the temperature is bad today stay inside 1805 01:01:55,839 --> 01:01:59,039 so with the 1806 01:01:56,559 --> 01:02:00,000 orological operator as long as one of 1807 01:01:59,039 --> 01:02:02,480 these conditions 1808 01:02:00,000 --> 01:02:04,160 is true then the entire statement is 1809 01:02:02,480 --> 01:02:05,920 true it doesn't matter if one of them is 1810 01:02:04,160 --> 01:02:07,119 false as long as one of them is true 1811 01:02:05,920 --> 01:02:09,440 then the entire statement 1812 01:02:07,119 --> 01:02:11,599 is true last but not least we have the 1813 01:02:09,440 --> 01:02:12,720 not logical operator this works a little 1814 01:02:11,599 --> 01:02:14,960 bit different from the 1815 01:02:12,720 --> 01:02:16,720 and as well as the or logical operator 1816 01:02:14,960 --> 01:02:18,400 because we can check 1817 01:02:16,720 --> 01:02:20,160 one or more conditional statements 1818 01:02:18,400 --> 01:02:21,039 instead of two or more conditional 1819 01:02:20,160 --> 01:02:23,359 statements 1820 01:02:21,039 --> 01:02:24,400 so what the not logical operator will do 1821 01:02:23,359 --> 01:02:26,480 is that it'll take 1822 01:02:24,400 --> 01:02:28,000 a conditional statement if it's true 1823 01:02:26,480 --> 01:02:29,520 it's going to flip it to false 1824 01:02:28,000 --> 01:02:31,920 if it's normally false it's going to 1825 01:02:29,520 --> 01:02:33,599 flip it to true so for this example we 1826 01:02:31,920 --> 01:02:34,160 have another way in which we could write 1827 01:02:33,599 --> 01:02:36,319 this 1828 01:02:34,160 --> 01:02:37,440 although it's a little more complex so 1829 01:02:36,319 --> 01:02:39,520 if our statement 1830 01:02:37,440 --> 01:02:41,200 is true what we can do is that we can 1831 01:02:39,520 --> 01:02:43,520 flip it to false using the 1832 01:02:41,200 --> 01:02:45,119 not logical operator if it's normally 1833 01:02:43,520 --> 01:02:47,599 false we can change it to true 1834 01:02:45,119 --> 01:02:48,960 so what we'll do is surround one or more 1835 01:02:47,599 --> 01:02:49,839 conditional statements with a set of 1836 01:02:48,960 --> 01:02:51,280 parentheses 1837 01:02:49,839 --> 01:02:52,960 and precede everything within the 1838 01:02:51,280 --> 01:02:55,599 parentheses with the not 1839 01:02:52,960 --> 01:02:56,480 logical operator so if the statement is 1840 01:02:55,599 --> 01:02:58,400 normally true 1841 01:02:56,480 --> 01:02:59,520 it's going to become false using the not 1842 01:02:58,400 --> 01:03:01,280 logical operator 1843 01:02:59,520 --> 01:03:03,039 if it's normally false it's going to be 1844 01:03:01,280 --> 01:03:04,799 true then let's do the same with the 1845 01:03:03,039 --> 01:03:06,720 else if statement 1846 01:03:04,799 --> 01:03:08,000 so surround one or more conditional 1847 01:03:06,720 --> 01:03:10,400 statements with the not 1848 01:03:08,000 --> 01:03:12,079 logical operator so now these roles are 1849 01:03:10,400 --> 01:03:13,680 going to be kind of reversed so let's 1850 01:03:12,079 --> 01:03:15,359 try this again what is the temperature 1851 01:03:13,680 --> 01:03:16,079 outside it is negative 20 degrees 1852 01:03:15,359 --> 01:03:18,240 outside 1853 01:03:16,079 --> 01:03:19,760 the temperature is good today go outside 1854 01:03:18,240 --> 01:03:22,720 so what we should probably do 1855 01:03:19,760 --> 01:03:23,680 is kind of reverse these rolls so i'm 1856 01:03:22,720 --> 01:03:25,359 just going to swap 1857 01:03:23,680 --> 01:03:27,680 everything within these if statements 1858 01:03:25,359 --> 01:03:29,119 with each other 1859 01:03:27,680 --> 01:03:30,880 and now this should work the same as it 1860 01:03:29,119 --> 01:03:31,599 did before but we just wrote it a little 1861 01:03:30,880 --> 01:03:34,559 bit different 1862 01:03:31,599 --> 01:03:35,440 what is the temperature outside it is 15 1863 01:03:34,559 --> 01:03:37,440 degrees outside 1864 01:03:35,440 --> 01:03:38,960 15 degrees celsius the temperature is 1865 01:03:37,440 --> 01:03:41,280 good today go outside 1866 01:03:38,960 --> 01:03:42,319 so with the not logical operator you can 1867 01:03:41,280 --> 01:03:44,640 surround one 1868 01:03:42,319 --> 01:03:46,480 or more conditional statements with the 1869 01:03:44,640 --> 01:03:47,039 not logical operator and what this will 1870 01:03:46,480 --> 01:03:49,440 do 1871 01:03:47,039 --> 01:03:50,799 is flip it from being false to true or 1872 01:03:49,440 --> 01:03:52,880 from true to false 1873 01:03:50,799 --> 01:03:55,119 well everybody that's the basics of 1874 01:03:52,880 --> 01:03:57,280 logical operators in python if you would 1875 01:03:55,119 --> 01:03:58,160 like a copy of all this code i will post 1876 01:03:57,280 --> 01:04:00,799 this in the comments 1877 01:03:58,160 --> 01:04:02,079 down below but yeah that's the basics of 1878 01:04:00,799 --> 01:04:05,280 logical operators 1879 01:04:02,079 --> 01:04:07,119 in python hey what's going on everybody 1880 01:04:05,280 --> 01:04:08,720 it's bro hope you're doing well and in 1881 01:04:07,119 --> 01:04:10,799 this video i'm going to explain while 1882 01:04:08,720 --> 01:04:14,640 loops in python so sit back 1883 01:04:10,799 --> 01:04:14,640 relax and enjoy the show 1884 01:04:15,119 --> 01:04:19,839 so while loops a while loop is a 1885 01:04:17,760 --> 01:04:20,319 statement that will execute its block of 1886 01:04:19,839 --> 01:04:23,119 code 1887 01:04:20,319 --> 01:04:25,200 as long as its condition remains true 1888 01:04:23,119 --> 01:04:26,000 later on in this video for this example 1889 01:04:25,200 --> 01:04:28,400 we're going to create 1890 01:04:26,000 --> 01:04:29,520 a program where we will prompt a user to 1891 01:04:28,400 --> 01:04:31,760 enter their name 1892 01:04:29,520 --> 01:04:33,839 if they attempt to skip that prompt then 1893 01:04:31,760 --> 01:04:34,799 we will continually ask the user to 1894 01:04:33,839 --> 01:04:36,160 enter their name 1895 01:04:34,799 --> 01:04:37,920 and they can't continue on with the rest 1896 01:04:36,160 --> 01:04:39,680 of the program until they do so the key 1897 01:04:37,920 --> 01:04:41,039 thing to keep in mind with while loops 1898 01:04:39,680 --> 01:04:42,880 is that you'll want some way to 1899 01:04:41,039 --> 01:04:44,400 eventually escape the while loop if you 1900 01:04:42,880 --> 01:04:46,000 don't have any way to escape the while 1901 01:04:44,400 --> 01:04:48,240 loop that's referred to as an 1902 01:04:46,000 --> 01:04:50,319 infinite loop so let's create an example 1903 01:04:48,240 --> 01:04:52,079 of an infinite loop because it's fun 1904 01:04:50,319 --> 01:04:53,359 so to create a while loop type while and 1905 01:04:52,079 --> 01:04:55,680 then some sort of condition 1906 01:04:53,359 --> 01:04:56,640 so for our condition let's say while one 1907 01:04:55,680 --> 01:04:59,039 is equal to one 1908 01:04:56,640 --> 01:05:00,000 so this will always be true now what do 1909 01:04:59,039 --> 01:05:02,160 we want to do 1910 01:05:00,000 --> 01:05:03,119 when we execute this block of code let's 1911 01:05:02,160 --> 01:05:06,400 print something 1912 01:05:03,119 --> 01:05:10,480 print help i'm 1913 01:05:06,400 --> 01:05:10,880 stuck in a loop all right so let's try 1914 01:05:10,480 --> 01:05:13,119 this 1915 01:05:10,880 --> 01:05:15,599 while one is equal to one print this 1916 01:05:13,119 --> 01:05:17,520 statement 1917 01:05:15,599 --> 01:05:19,440 and we will just continue on and on 1918 01:05:17,520 --> 01:05:21,680 within this while loop because we have 1919 01:05:19,440 --> 01:05:24,079 no way to escape this while loop 1920 01:05:21,680 --> 01:05:26,400 so now let's use this concept and write 1921 01:05:24,079 --> 01:05:27,520 a program where we will prompt a user to 1922 01:05:26,400 --> 01:05:29,280 type in their name 1923 01:05:27,520 --> 01:05:31,119 if they don't type in anything then we 1924 01:05:29,280 --> 01:05:32,240 will continue to prompt them to type in 1925 01:05:31,119 --> 01:05:35,599 at least something 1926 01:05:32,240 --> 01:05:36,960 so let's say name is equal to 1927 01:05:35,599 --> 01:05:39,920 and then to just keep this blank we'll 1928 01:05:36,960 --> 01:05:41,520 just set this to a set of quotes 1929 01:05:39,920 --> 01:05:43,119 and then let's write a while loop here 1930 01:05:41,520 --> 01:05:45,599 while name 1931 01:05:43,119 --> 01:05:46,319 and let's check the length of name if 1932 01:05:45,599 --> 01:05:49,359 the length 1933 01:05:46,319 --> 01:05:52,319 of our name is equal 1934 01:05:49,359 --> 01:05:53,520 to zero then we will ask the user to 1935 01:05:52,319 --> 01:05:57,280 type in their name 1936 01:05:53,520 --> 01:06:00,319 name equals input 1937 01:05:57,280 --> 01:06:02,160 enter your name 1938 01:06:00,319 --> 01:06:04,160 so then once we escape the while loop 1939 01:06:02,160 --> 01:06:07,280 let's print something 1940 01:06:04,160 --> 01:06:10,720 print hello plus 1941 01:06:07,280 --> 01:06:12,799 name whoops hello plus name 1942 01:06:10,720 --> 01:06:14,880 alright so when i run this we are giving 1943 01:06:12,799 --> 01:06:16,400 the user a way to escape the while loop 1944 01:06:14,880 --> 01:06:16,960 they have to type in something for their 1945 01:06:16,400 --> 01:06:19,920 name 1946 01:06:16,960 --> 01:06:20,880 as long as the length of my name is 1947 01:06:19,920 --> 01:06:23,440 equal to zero 1948 01:06:20,880 --> 01:06:23,920 keep on printing this prompt enter your 1949 01:06:23,440 --> 01:06:25,359 name 1950 01:06:23,920 --> 01:06:26,960 so i'm just going to hit enter a bunch 1951 01:06:25,359 --> 01:06:30,240 of times enter your name 1952 01:06:26,960 --> 01:06:32,160 no enter your name no enter your name no 1953 01:06:30,240 --> 01:06:34,000 so we are stuck within this loop until 1954 01:06:32,160 --> 01:06:36,319 we enter something for our name 1955 01:06:34,000 --> 01:06:37,280 so this time let me type in a name then 1956 01:06:36,319 --> 01:06:39,599 hit enter 1957 01:06:37,280 --> 01:06:40,400 so therefore our condition is now false 1958 01:06:39,599 --> 01:06:43,520 the length 1959 01:06:40,400 --> 01:06:46,000 of my name is now one two three so 1960 01:06:43,520 --> 01:06:48,240 three is not equal to zero therefore our 1961 01:06:46,000 --> 01:06:49,520 while statement our while loop is false 1962 01:06:48,240 --> 01:06:51,440 and we continue on with the rest of the 1963 01:06:49,520 --> 01:06:53,359 program there are a few variations in 1964 01:06:51,440 --> 01:06:54,000 which you could write the same program 1965 01:06:53,359 --> 01:06:55,839 so 1966 01:06:54,000 --> 01:06:57,039 let's write this a different way what we 1967 01:06:55,839 --> 01:07:00,480 could do is say 1968 01:06:57,039 --> 01:07:03,440 name is equal to none while 1969 01:07:00,480 --> 01:07:05,440 not name and this will pretty much do 1970 01:07:03,440 --> 01:07:06,720 the same thing it's just another way of 1971 01:07:05,440 --> 01:07:08,400 writing this 1972 01:07:06,720 --> 01:07:10,559 well everybody that's the basics of 1973 01:07:08,400 --> 01:07:11,599 while loops a while loop is a statement 1974 01:07:10,559 --> 01:07:13,920 that will execute 1975 01:07:11,599 --> 01:07:15,599 its block of code as long as its 1976 01:07:13,920 --> 01:07:17,599 condition remains true 1977 01:07:15,599 --> 01:07:18,720 for our example we asked the user to 1978 01:07:17,599 --> 01:07:20,559 type in their name 1979 01:07:18,720 --> 01:07:22,480 if they were to leave it empty or blank 1980 01:07:20,559 --> 01:07:23,440 we would keep on asking the user to type 1981 01:07:22,480 --> 01:07:25,039 in their name 1982 01:07:23,440 --> 01:07:26,799 so if you would like a copy of this code 1983 01:07:25,039 --> 01:07:27,520 i will post this in the comments section 1984 01:07:26,799 --> 01:07:29,440 down below 1985 01:07:27,520 --> 01:07:31,599 but yeah that's the basics of while 1986 01:07:29,440 --> 01:07:33,760 loops in python 1987 01:07:31,599 --> 01:07:35,359 hey what's going on everybody it's bro 1988 01:07:33,760 --> 01:07:37,520 hope you're doing well and in this video 1989 01:07:35,359 --> 01:07:38,799 i'm going to explain for loops in python 1990 01:07:37,520 --> 01:07:40,880 and at the end of this video we're going 1991 01:07:38,799 --> 01:07:45,359 to create a countdown timer so 1992 01:07:40,880 --> 01:07:47,359 sit back relax and enjoy the show 1993 01:07:45,359 --> 01:07:49,920 all right guys and gals let's talk about 1994 01:07:47,359 --> 01:07:52,240 for loops a for loop is a statement that 1995 01:07:49,920 --> 01:07:54,880 will execute its block of code a 1996 01:07:52,240 --> 01:07:56,480 limited amount of times it's similar but 1997 01:07:54,880 --> 01:07:57,359 different from a while loop because a 1998 01:07:56,480 --> 01:08:00,640 while loop 1999 01:07:57,359 --> 01:08:02,319 could iterate an unlimited or infinite 2000 01:08:00,640 --> 01:08:04,240 amount of times depending on the 2001 01:08:02,319 --> 01:08:06,480 condition a for loop 2002 01:08:04,240 --> 01:08:08,799 will only iterate a limited amount of 2003 01:08:06,480 --> 01:08:10,319 times and before we start executing 2004 01:08:08,799 --> 01:08:11,760 its block of code we already know how 2005 01:08:10,319 --> 01:08:12,799 many times we're going to repeat this 2006 01:08:11,760 --> 01:08:14,480 block of code 2007 01:08:12,799 --> 01:08:16,319 so for this example let's create a for 2008 01:08:14,480 --> 01:08:17,759 loop that will simply count it to 10 2009 01:08:16,319 --> 01:08:19,440 and then we'll create a few more 2010 01:08:17,759 --> 01:08:20,960 sophisticated examples 2011 01:08:19,440 --> 01:08:22,719 so to create a for loop that will count 2012 01:08:20,960 --> 01:08:24,480 up to 10 this is what we'll type 2013 01:08:22,719 --> 01:08:26,640 four and then we need some sort of 2014 01:08:24,480 --> 01:08:29,359 counter people usually either write 2015 01:08:26,640 --> 01:08:30,159 index or they shorten it to just i i for 2016 01:08:29,359 --> 01:08:34,000 index 2017 01:08:30,159 --> 01:08:36,799 four i in and we can set a range range 2018 01:08:34,000 --> 01:08:37,279 ten so we will execute this for loop ten 2019 01:08:36,799 --> 01:08:39,199 times 2020 01:08:37,279 --> 01:08:40,319 and to finish this for loop just add a 2021 01:08:39,199 --> 01:08:43,279 colon at the end 2022 01:08:40,319 --> 01:08:45,199 so the next line or lines will be 2023 01:08:43,279 --> 01:08:46,000 indented so that's the block of code for 2024 01:08:45,199 --> 01:08:47,359 the for loop 2025 01:08:46,000 --> 01:08:49,359 what do we want to do during each 2026 01:08:47,359 --> 01:08:51,600 iteration let's just print what 2027 01:08:49,359 --> 01:08:52,880 i is and let's take a look to see what 2028 01:08:51,600 --> 01:08:55,359 would happen 2029 01:08:52,880 --> 01:08:57,279 so we executed this for loop ten times 2030 01:08:55,359 --> 01:08:58,239 except with computers computers always 2031 01:08:57,279 --> 01:09:00,960 start with zero 2032 01:08:58,239 --> 01:09:02,080 so when we said four i in range ten 2033 01:09:00,960 --> 01:09:04,799 we're counting numbers 2034 01:09:02,080 --> 01:09:05,279 zero through nine so it's as if this 2035 01:09:04,799 --> 01:09:07,520 number 2036 01:09:05,279 --> 01:09:09,440 is exclusive so there's two ways we can 2037 01:09:07,520 --> 01:09:12,080 change this we could say range ten 2038 01:09:09,440 --> 01:09:12,799 plus one or what i would do is say print 2039 01:09:12,080 --> 01:09:15,279 i 2040 01:09:12,799 --> 01:09:15,920 plus one so this will give us numbers 2041 01:09:15,279 --> 01:09:18,159 one 2042 01:09:15,920 --> 01:09:20,239 through ten so that's a for loop it will 2043 01:09:18,159 --> 01:09:22,480 execute a limited amount of times 2044 01:09:20,239 --> 01:09:23,440 this time let's count a range between 2045 01:09:22,480 --> 01:09:25,600 two numbers 2046 01:09:23,440 --> 01:09:27,679 not necessarily 0 to 10 but something 2047 01:09:25,600 --> 01:09:31,279 else so let's say 4i 2048 01:09:27,679 --> 01:09:31,920 in range maybe the numbers 50 through 2049 01:09:31,279 --> 01:09:34,159 100 2050 01:09:31,920 --> 01:09:35,440 so within my range function i will pass 2051 01:09:34,159 --> 01:09:37,199 in two numbers 2052 01:09:35,440 --> 01:09:38,880 the first number will be the starting 2053 01:09:37,199 --> 01:09:39,759 point and the second number is the 2054 01:09:38,880 --> 01:09:41,920 ending point 2055 01:09:39,759 --> 01:09:43,920 this first number is inclusive and the 2056 01:09:41,920 --> 01:09:46,880 second number is exclusive 2057 01:09:43,920 --> 01:09:48,239 so let's print this print whatever i is 2058 01:09:46,880 --> 01:09:51,359 so this will count 2059 01:09:48,239 --> 01:09:53,199 50 through 99 actually if you wanted to 2060 01:09:51,359 --> 01:09:55,360 include this last number since it's 2061 01:09:53,199 --> 01:09:56,960 exclusive what we could do is just add 2062 01:09:55,360 --> 01:10:00,320 one to the end so this would give 2063 01:09:56,960 --> 01:10:02,800 us 50 through 100 except that 2064 01:10:00,320 --> 01:10:04,000 this will iterate 51 times since we're 2065 01:10:02,800 --> 01:10:05,360 counting 50. 2066 01:10:04,000 --> 01:10:06,800 so you just have to pay attention to 2067 01:10:05,360 --> 01:10:08,480 your range and the numbers that it's 2068 01:10:06,800 --> 01:10:10,239 counting now one thing that you can do 2069 01:10:08,480 --> 01:10:12,000 with the range function is that you can 2070 01:10:10,239 --> 01:10:13,360 add a third argument and this will 2071 01:10:12,000 --> 01:10:15,600 function as the step 2072 01:10:13,360 --> 01:10:17,600 how much you want to count up or down by 2073 01:10:15,600 --> 01:10:20,640 so this time let's count up by two 2074 01:10:17,600 --> 01:10:22,080 so i'll add comma two we're passing in a 2075 01:10:20,640 --> 01:10:24,960 third argument this time 2076 01:10:22,080 --> 01:10:27,120 so this program will now count up by two 2077 01:10:24,960 --> 01:10:30,320 starting at fifty and go all the way 2078 01:10:27,120 --> 01:10:32,080 to one hundred a benefit of for loops is 2079 01:10:30,320 --> 01:10:33,040 that we can iterate through anything 2080 01:10:32,080 --> 01:10:35,679 that is considered 2081 01:10:33,040 --> 01:10:36,719 iterable this could include a string the 2082 01:10:35,679 --> 01:10:39,280 letters in a string 2083 01:10:36,719 --> 01:10:40,800 or any sort of collection so this time 2084 01:10:39,280 --> 01:10:41,360 let's create a for loop that will 2085 01:10:40,800 --> 01:10:44,320 iterate 2086 01:10:41,360 --> 01:10:44,880 once through each letter in maybe a name 2087 01:10:44,320 --> 01:10:47,840 so for 2088 01:10:44,880 --> 01:10:49,360 i in and let's type a name here type in 2089 01:10:47,840 --> 01:10:50,640 whatever your first name and last name 2090 01:10:49,360 --> 01:10:53,760 is 2091 01:10:50,640 --> 01:10:56,640 and then we will print whatever i is 2092 01:10:53,760 --> 01:10:57,520 for i in whatever your name is print 2093 01:10:56,640 --> 01:11:00,480 each letter 2094 01:10:57,520 --> 01:11:01,040 in the string so when i run this each 2095 01:11:00,480 --> 01:11:03,360 letter 2096 01:11:01,040 --> 01:11:04,239 within my name will be printed to a new 2097 01:11:03,360 --> 01:11:06,320 line 2098 01:11:04,239 --> 01:11:07,840 all right people for my last trick we're 2099 01:11:06,320 --> 01:11:09,760 going to create a program 2100 01:11:07,840 --> 01:11:11,760 where we will simulate a countdown 2101 01:11:09,760 --> 01:11:13,760 starting at 10 and count down to zero 2102 01:11:11,760 --> 01:11:15,280 and then maybe once we reach zero we can 2103 01:11:13,760 --> 01:11:17,440 print something such as 2104 01:11:15,280 --> 01:11:19,280 happy new year so to create this program 2105 01:11:17,440 --> 01:11:21,199 we'll need an import so at the top 2106 01:11:19,280 --> 01:11:22,560 we're going to import the time module 2107 01:11:21,199 --> 01:11:24,640 because we'll be waiting 2108 01:11:22,560 --> 01:11:25,600 one second after each iteration of this 2109 01:11:24,640 --> 01:11:27,840 for loop 2110 01:11:25,600 --> 01:11:29,600 so to create this program let's say four 2111 01:11:27,840 --> 01:11:30,719 you could say i but you can really write 2112 01:11:29,600 --> 01:11:32,800 anything here and 2113 01:11:30,719 --> 01:11:35,040 to demonstrate that let's say four 2114 01:11:32,800 --> 01:11:38,080 seconds four seconds 2115 01:11:35,040 --> 01:11:38,960 in range so the starting point is going 2116 01:11:38,080 --> 01:11:41,120 to be ten 2117 01:11:38,960 --> 01:11:42,159 the ending point will be zero and we'll 2118 01:11:41,120 --> 01:11:44,320 add a step 2119 01:11:42,159 --> 01:11:46,000 what do we want to count up or down by 2120 01:11:44,320 --> 01:11:47,120 let's set this to negative one so this 2121 01:11:46,000 --> 01:11:49,360 will be a countdown 2122 01:11:47,120 --> 01:11:51,440 starting at ten and ending at zero so 2123 01:11:49,360 --> 01:11:54,320 after each iteration 2124 01:11:51,440 --> 01:11:55,040 let's print whatever i is or in this 2125 01:11:54,320 --> 01:11:58,000 case 2126 01:11:55,040 --> 01:11:58,800 seconds and then we can sleep have our 2127 01:11:58,000 --> 01:12:01,280 thread sleep 2128 01:11:58,800 --> 01:12:03,120 for a number of seconds too so after we 2129 01:12:01,280 --> 01:12:06,400 print seconds 2130 01:12:03,120 --> 01:12:08,159 type time dot sleep and pass in 2131 01:12:06,400 --> 01:12:10,159 how long you want to sleep for i would 2132 01:12:08,159 --> 01:12:12,400 like to sleep for one second 2133 01:12:10,159 --> 01:12:14,159 and then at the end let's print happy 2134 01:12:12,400 --> 01:12:15,120 new year make sure this isn't within the 2135 01:12:14,159 --> 01:12:18,719 for loop 2136 01:12:15,120 --> 01:12:21,199 print happy new year and well 2137 01:12:18,719 --> 01:12:23,120 that's it let's test this so we'll begin 2138 01:12:21,199 --> 01:12:25,120 at 10 and count down to zero and you can 2139 01:12:23,120 --> 01:12:25,760 see that my thread that is running this 2140 01:12:25,120 --> 01:12:28,159 program 2141 01:12:25,760 --> 01:12:29,920 is sleeping for one second after each 2142 01:12:28,159 --> 01:12:32,159 iteration of this for loop 2143 01:12:29,920 --> 01:12:33,520 then once we reach zero it will display 2144 01:12:32,159 --> 01:12:35,760 happy new year 2145 01:12:33,520 --> 01:12:37,760 well everybody that's the basics of for 2146 01:12:35,760 --> 01:12:40,239 loops a for loop is a statement 2147 01:12:37,760 --> 01:12:41,840 that will execute its block of code a 2148 01:12:40,239 --> 01:12:43,520 limited amount of times 2149 01:12:41,840 --> 01:12:45,440 it's different from a while loop because 2150 01:12:43,520 --> 01:12:47,920 a while loop could execute 2151 01:12:45,440 --> 01:12:50,239 an infinite or unlimited amount of times 2152 01:12:47,920 --> 01:12:51,280 with for loops they execute limited 2153 01:12:50,239 --> 01:12:53,280 amounts of times 2154 01:12:51,280 --> 01:12:54,640 and before we enter the for loop we 2155 01:12:53,280 --> 01:12:56,400 already know how many times we're going 2156 01:12:54,640 --> 01:12:58,159 to iterate this for loop 2157 01:12:56,400 --> 01:12:59,679 if you'd like a copy of all this code i 2158 01:12:58,159 --> 01:13:00,400 will post this in the comments down 2159 01:12:59,679 --> 01:13:02,840 below 2160 01:13:00,400 --> 01:13:04,400 but yeah that's how for loops work in 2161 01:13:02,840 --> 01:13:06,239 python 2162 01:13:04,400 --> 01:13:07,920 hey what's going on everybody it's you 2163 01:13:06,239 --> 01:13:09,760 bro hope you're doing well and in this 2164 01:13:07,920 --> 01:13:10,880 video i'm going to explain nested loops 2165 01:13:09,760 --> 01:13:15,440 in python so 2166 01:13:10,880 --> 01:13:15,440 sit back relax and enjoy the show 2167 01:13:15,520 --> 01:13:19,840 hey all you people let's talk about 2168 01:13:17,280 --> 01:13:20,960 nested loops a nested loop is a general 2169 01:13:19,840 --> 01:13:22,640 concept of having 2170 01:13:20,960 --> 01:13:24,000 one loop inside of another loop and 2171 01:13:22,640 --> 01:13:26,000 that's really it it doesn't matter if 2172 01:13:24,000 --> 01:13:28,560 it's a for loop or a while loop 2173 01:13:26,000 --> 01:13:29,280 so the inner loop will finish all of its 2174 01:13:28,560 --> 01:13:31,679 iterations 2175 01:13:29,280 --> 01:13:32,480 before we finish one iteration of the 2176 01:13:31,679 --> 01:13:34,000 outer loop 2177 01:13:32,480 --> 01:13:36,239 and to best demonstrate this we're going 2178 01:13:34,000 --> 01:13:38,320 to create a program where we will draw a 2179 01:13:36,239 --> 01:13:39,199 rectangle made out of a certain symbol 2180 01:13:38,320 --> 01:13:41,440 that we choose 2181 01:13:39,199 --> 01:13:43,440 we need to set a width and a height and 2182 01:13:41,440 --> 01:13:45,679 to best do this we'll need to use a 2183 01:13:43,440 --> 01:13:46,560 nested loop we'll create a few prompts 2184 01:13:45,679 --> 01:13:48,320 one for rows 2185 01:13:46,560 --> 01:13:49,600 columns and a symbol that we want to 2186 01:13:48,320 --> 01:13:52,880 make a rectangle of 2187 01:13:49,600 --> 01:13:56,480 so let's begin with rows rows equals 2188 01:13:52,880 --> 01:14:00,159 input how many 2189 01:13:56,480 --> 01:14:01,440 rows and we'll need to cast this as an 2190 01:14:00,159 --> 01:14:04,000 integer data type 2191 01:14:01,440 --> 01:14:07,760 because we're working with numbers okay 2192 01:14:04,000 --> 01:14:07,760 let's do the same thing for columns 2193 01:14:08,159 --> 01:14:12,560 so replace rows with columns for this 2194 01:14:10,159 --> 01:14:15,600 next line 2195 01:14:12,560 --> 01:14:18,159 input how many columns 2196 01:14:15,600 --> 01:14:18,960 and we'll create a symbol variable 2197 01:14:18,159 --> 01:14:21,679 symbol 2198 01:14:18,960 --> 01:14:23,600 equals input and this we don't have to 2199 01:14:21,679 --> 01:14:26,840 cast 2200 01:14:23,600 --> 01:14:29,679 enter a symbol to 2201 01:14:26,840 --> 01:14:31,679 use now it's time for the nested loops 2202 01:14:29,679 --> 01:14:33,360 we're going to create an outer for loop 2203 01:14:31,679 --> 01:14:35,280 as well as an inner for loop 2204 01:14:33,360 --> 01:14:37,440 the outer for loop will be in charge of 2205 01:14:35,280 --> 01:14:38,719 the rows the inner for loop will be in 2206 01:14:37,440 --> 01:14:41,120 charge of the columns 2207 01:14:38,719 --> 01:14:42,080 so let's begin by creating our outer 2208 01:14:41,120 --> 01:14:46,320 loop so 2209 01:14:42,080 --> 01:14:49,440 we'll write 4 i in range 2210 01:14:46,320 --> 01:14:50,800 rows we would like our outer for loop to 2211 01:14:49,440 --> 01:14:52,640 iterate as many times 2212 01:14:50,800 --> 01:14:54,400 as we have rows and we're going to 2213 01:14:52,640 --> 01:14:56,960 create an inner for loop that will 2214 01:14:54,400 --> 01:14:58,800 iterate as many times as we have columns 2215 01:14:56,960 --> 01:15:00,480 so within the block of code for the 2216 01:14:58,800 --> 01:15:01,840 outer for loop we're going to create 2217 01:15:00,480 --> 01:15:04,239 another for loop 2218 01:15:01,840 --> 01:15:05,040 this time it'll be four and a common 2219 01:15:04,239 --> 01:15:07,920 convention 2220 01:15:05,040 --> 01:15:09,120 for inner loops is to write j as an 2221 01:15:07,920 --> 01:15:12,719 index because 2222 01:15:09,120 --> 01:15:17,920 j comes after i i guess so for j 2223 01:15:12,719 --> 01:15:17,920 in range columns 2224 01:15:18,159 --> 01:15:22,080 so our inner for loop will iterate for 2225 01:15:21,040 --> 01:15:24,800 as many times 2226 01:15:22,080 --> 01:15:25,120 as we have columns all we'll do within 2227 01:15:24,800 --> 01:15:27,760 the 2228 01:15:25,120 --> 01:15:28,719 inner for loop is print our symbol that 2229 01:15:27,760 --> 01:15:31,120 we have 2230 01:15:28,719 --> 01:15:32,480 except we'll have one issue using a 2231 01:15:31,120 --> 01:15:34,560 print statement 2232 01:15:32,480 --> 01:15:36,800 after we use the print statement we will 2233 01:15:34,560 --> 01:15:37,679 enter a new line character and move down 2234 01:15:36,800 --> 01:15:39,840 to the next line 2235 01:15:37,679 --> 01:15:40,880 we can actually prevent that by adding 2236 01:15:39,840 --> 01:15:45,040 comma 2237 01:15:40,880 --> 01:15:45,520 and equals quotes so after using a print 2238 01:15:45,040 --> 01:15:47,679 statement 2239 01:15:45,520 --> 01:15:49,280 this will prevent our cursor from moving 2240 01:15:47,679 --> 01:15:52,080 down to the next line 2241 01:15:49,280 --> 01:15:53,600 so pay attention to the indentation too 2242 01:15:52,080 --> 01:15:54,400 so there's kind of two levels of 2243 01:15:53,600 --> 01:15:56,320 indentation 2244 01:15:54,400 --> 01:15:58,159 we're currently within the inner for 2245 01:15:56,320 --> 01:16:00,000 loop so that's pretty much it for the 2246 01:15:58,159 --> 01:16:02,800 inner for loop now we're going to 2247 01:16:00,000 --> 01:16:03,760 print a new line once we exit the inner 2248 01:16:02,800 --> 01:16:05,920 for loop 2249 01:16:03,760 --> 01:16:07,040 and you can see that the indentation is 2250 01:16:05,920 --> 01:16:08,880 on 2251 01:16:07,040 --> 01:16:10,239 this line right here we're within the 2252 01:16:08,880 --> 01:16:12,080 outer for loop now 2253 01:16:10,239 --> 01:16:13,360 and that's pretty much it so let's try 2254 01:16:12,080 --> 01:16:16,480 this program 2255 01:16:13,360 --> 01:16:19,120 how many rows let's say five rows how 2256 01:16:16,480 --> 01:16:21,040 many columns perhaps six 2257 01:16:19,120 --> 01:16:22,480 what symbol do we want to use let's say 2258 01:16:21,040 --> 01:16:25,760 the dollar sign 2259 01:16:22,480 --> 01:16:26,719 and let's try it there we go there's our 2260 01:16:25,760 --> 01:16:30,000 rectangle 2261 01:16:26,719 --> 01:16:32,800 so we have let's see six columns one 2262 01:16:30,000 --> 01:16:33,199 two three four five six and five rows 2263 01:16:32,800 --> 01:16:36,719 one 2264 01:16:33,199 --> 01:16:38,640 two three four five in summary 2265 01:16:36,719 --> 01:16:39,840 a nested loop is really just this 2266 01:16:38,640 --> 01:16:41,440 concept of having 2267 01:16:39,840 --> 01:16:43,440 one loop inside of another loop it 2268 01:16:41,440 --> 01:16:45,600 doesn't matter if it's a while loop or a 2269 01:16:43,440 --> 01:16:47,520 for a loop it's really situational 2270 01:16:45,600 --> 01:16:49,360 the inner loop will finish all of its 2271 01:16:47,520 --> 01:16:51,440 iterations before finishing 2272 01:16:49,360 --> 01:16:53,440 one iteration of the outer loop so what 2273 01:16:51,440 --> 01:16:54,159 we did to demonstrate that is that we 2274 01:16:53,440 --> 01:16:56,400 had 2275 01:16:54,159 --> 01:16:58,000 the outer loop in charge the rows and an 2276 01:16:56,400 --> 01:16:59,600 inner loop in charge of the columns for 2277 01:16:58,000 --> 01:17:01,280 our rectangle program 2278 01:16:59,600 --> 01:17:03,280 so if you would like a copy of this 2279 01:17:01,280 --> 01:17:04,560 program i will post this in the comment 2280 01:17:03,280 --> 01:17:06,800 section down below 2281 01:17:04,560 --> 01:17:09,120 but yeah that's the basics of nested 2282 01:17:06,800 --> 01:17:11,520 loops in python 2283 01:17:09,120 --> 01:17:12,480 hey what's going on everybody it's bro 2284 01:17:11,520 --> 01:17:14,320 hope you're doing well 2285 01:17:12,480 --> 01:17:16,000 and in this video i'm going to explain 2286 01:17:14,320 --> 01:17:21,199 loop control statements in python 2287 01:17:16,000 --> 01:17:21,199 so sit back relax and enjoy the show 2288 01:17:21,360 --> 01:17:24,960 all right ladies and gentlemen let's 2289 01:17:23,040 --> 01:17:26,800 talk about loop control statements 2290 01:17:24,960 --> 01:17:29,679 these are used to change a loop's 2291 01:17:26,800 --> 01:17:30,800 execution from its normal sequence 2292 01:17:29,679 --> 01:17:32,800 and there are three we're going to 2293 01:17:30,800 --> 01:17:35,360 discuss break continue 2294 01:17:32,800 --> 01:17:37,600 and pass let's begin with break break is 2295 01:17:35,360 --> 01:17:39,360 used to terminate the loop entirely 2296 01:17:37,600 --> 01:17:40,640 now here's a good example of where a 2297 01:17:39,360 --> 01:17:43,520 break would be useful 2298 01:17:40,640 --> 01:17:44,640 let's say while true we're going to 2299 01:17:43,520 --> 01:17:46,719 continually ask 2300 01:17:44,640 --> 01:17:48,400 for somebody's name if they don't type 2301 01:17:46,719 --> 01:17:49,920 in anything then we'll continue the 2302 01:17:48,400 --> 01:17:55,040 while loop over and over again 2303 01:17:49,920 --> 01:17:55,040 so let's say name equals input 2304 01:17:55,199 --> 01:18:02,480 enter your name now 2305 01:17:58,960 --> 01:18:04,080 if name does not equal 2306 01:18:02,480 --> 01:18:05,840 a set of quotes that means they don't 2307 01:18:04,080 --> 01:18:08,000 type in anything then 2308 01:18:05,840 --> 01:18:10,400 we will break and break out of this 2309 01:18:08,000 --> 01:18:12,800 while loop and let's test it 2310 01:18:10,400 --> 01:18:14,480 enter your name nah i don't think so i'm 2311 01:18:12,800 --> 01:18:15,920 too lazy to enter my name 2312 01:18:14,480 --> 01:18:17,520 all right i can see that i'm not going 2313 01:18:15,920 --> 01:18:19,600 to escape this while loop so i'm going 2314 01:18:17,520 --> 01:18:22,239 to type in my name because i give up 2315 01:18:19,600 --> 01:18:23,760 and hit enter therefore i will break and 2316 01:18:22,239 --> 01:18:26,080 exit out of this while loop 2317 01:18:23,760 --> 01:18:28,000 so a break is used to terminate the loop 2318 01:18:26,080 --> 01:18:30,480 entirely when it's encountered 2319 01:18:28,000 --> 01:18:32,480 next we have continue continue skips to 2320 01:18:30,480 --> 01:18:35,040 the next iteration of the loop 2321 01:18:32,480 --> 01:18:35,840 now let's say we have a phone number 2322 01:18:35,040 --> 01:18:38,400 phone 2323 01:18:35,840 --> 01:18:39,199 underscore number and i will set the 2324 01:18:38,400 --> 01:18:40,480 sequel to 2325 01:18:39,199 --> 01:18:45,679 let's say a random phone number 2326 01:18:40,480 --> 01:18:47,679 including dashes 123-456-7890 2327 01:18:45,679 --> 01:18:49,760 what i would like to do is to display 2328 01:18:47,679 --> 01:18:51,360 this number within the console window 2329 01:18:49,760 --> 01:18:53,520 without these dashes 2330 01:18:51,360 --> 01:18:54,880 this is how to do that using a continue 2331 01:18:53,520 --> 01:19:00,159 control statement 2332 01:18:54,880 --> 01:19:00,159 for i in phone number 2333 01:19:00,640 --> 01:19:04,000 so for each character within our string 2334 01:19:03,199 --> 01:19:06,239 of phone number 2335 01:19:04,000 --> 01:19:08,560 what we'll do is check to see if we 2336 01:19:06,239 --> 01:19:11,679 encounter a dash character 2337 01:19:08,560 --> 01:19:15,679 if i i is our index is 2338 01:19:11,679 --> 01:19:18,400 equal to a dash then we want to 2339 01:19:15,679 --> 01:19:19,520 continue and skip over this iteration of 2340 01:19:18,400 --> 01:19:23,120 the loop 2341 01:19:19,520 --> 01:19:26,320 so continue 2342 01:19:23,120 --> 01:19:29,360 then i would like to print 2343 01:19:26,320 --> 01:19:30,640 whatever our indexes whatever character 2344 01:19:29,360 --> 01:19:32,640 we're working on 2345 01:19:30,640 --> 01:19:34,400 so if i were to run this as it is it's 2346 01:19:32,640 --> 01:19:37,199 going to print each digit 2347 01:19:34,400 --> 01:19:39,280 on a new line so with print statements 2348 01:19:37,199 --> 01:19:40,719 they will add a new line character to 2349 01:19:39,280 --> 01:19:42,080 the end of your string 2350 01:19:40,719 --> 01:19:44,480 and you can actually change that by 2351 01:19:42,080 --> 01:19:47,440 adding to the end comma 2352 01:19:44,480 --> 01:19:49,440 end equals and then a set of quotes so 2353 01:19:47,440 --> 01:19:50,480 this will print my phone number without 2354 01:19:49,440 --> 01:19:51,920 any dashes 2355 01:19:50,480 --> 01:19:54,239 one two three four five six seven eight 2356 01:19:51,920 --> 01:19:56,480 nine zero so that's a use of 2357 01:19:54,239 --> 01:19:58,719 the continue control statement it skips 2358 01:19:56,480 --> 01:20:01,199 to the next iteration of the loop 2359 01:19:58,719 --> 01:20:01,920 last but not least we have pass pass 2360 01:20:01,199 --> 01:20:04,080 does nothing 2361 01:20:01,920 --> 01:20:05,760 it acts as a placeholder let's say that 2362 01:20:04,080 --> 01:20:09,360 i would like to print the numbers 2363 01:20:05,760 --> 01:20:13,280 1 through 20 using a for loop for i 2364 01:20:09,360 --> 01:20:14,560 in range 1 through 21 and remember that 2365 01:20:13,280 --> 01:20:16,880 the second digit is 2366 01:20:14,560 --> 01:20:18,080 exclusive so this will iterate once 2367 01:20:16,880 --> 01:20:20,320 through the digits of 1 2368 01:20:18,080 --> 01:20:22,320 through 20. let's pretend that i'm 2369 01:20:20,320 --> 01:20:22,639 highly superstitious and i don't want to 2370 01:20:22,320 --> 01:20:24,960 print 2371 01:20:22,639 --> 01:20:26,080 the number 13 because 13 is considered 2372 01:20:24,960 --> 01:20:29,760 an unlucky number 2373 01:20:26,080 --> 01:20:31,760 if i is equal to 13 2374 01:20:29,760 --> 01:20:34,159 what we'll do is pass and pass acts as a 2375 01:20:31,760 --> 01:20:37,360 placeholder and it won't do anything 2376 01:20:34,159 --> 01:20:40,560 else print 2377 01:20:37,360 --> 01:20:42,639 i that's one i there we go 2378 01:20:40,560 --> 01:20:44,800 this will print the numbers 1 through 20 2379 01:20:42,639 --> 01:20:47,040 and skip 13 because we used 2380 01:20:44,800 --> 01:20:49,280 the past control statement which does 2381 01:20:47,040 --> 01:20:51,280 nothing it acts as a placeholder 2382 01:20:49,280 --> 01:20:52,639 so everybody those are loop control 2383 01:20:51,280 --> 01:20:54,719 statements they change 2384 01:20:52,639 --> 01:20:56,480 a loop's execution from its normal 2385 01:20:54,719 --> 01:20:59,040 sequence we have break 2386 01:20:56,480 --> 01:21:00,080 continue and pass so if you would like a 2387 01:20:59,040 --> 01:21:01,679 copy of all this code 2388 01:21:00,080 --> 01:21:03,920 i will post all of this in the comments 2389 01:21:01,679 --> 01:21:04,320 section down below but yeah those are a 2390 01:21:03,920 --> 01:21:07,360 few 2391 01:21:04,320 --> 01:21:08,639 loop control statements in python what's 2392 01:21:07,360 --> 01:21:10,719 going on everybody 2393 01:21:08,639 --> 01:21:12,320 it's bro hope you're doing well and in 2394 01:21:10,719 --> 01:21:15,920 this video i'm going to explain 2395 01:21:12,320 --> 01:21:18,880 lists in python so sit back relax 2396 01:21:15,920 --> 01:21:18,880 and enjoy the show 2397 01:21:19,120 --> 01:21:23,120 all right ladies and gentlemen let's get 2398 01:21:21,040 --> 01:21:25,920 down to business i got to explain 2399 01:21:23,120 --> 01:21:26,560 lists to you all now a list is used to 2400 01:21:25,920 --> 01:21:29,920 store 2401 01:21:26,560 --> 01:21:32,080 multiple items within a single variable 2402 01:21:29,920 --> 01:21:33,120 for example let's say we have a variable 2403 01:21:32,080 --> 01:21:36,320 called food 2404 01:21:33,120 --> 01:21:37,920 and i will store a string value of pizza 2405 01:21:36,320 --> 01:21:40,159 because i like pizza 2406 01:21:37,920 --> 01:21:42,000 what we could do is that we can store 2407 01:21:40,159 --> 01:21:44,719 multiple items within this 2408 01:21:42,000 --> 01:21:45,280 variable by turning this variable into a 2409 01:21:44,719 --> 01:21:47,280 list 2410 01:21:45,280 --> 01:21:49,199 and in order to do so we're going to 2411 01:21:47,280 --> 01:21:50,080 surround all of the values that we would 2412 01:21:49,199 --> 01:21:52,960 like to add 2413 01:21:50,080 --> 01:21:53,920 to our list with a set of square 2414 01:21:52,960 --> 01:21:56,480 brackets 2415 01:21:53,920 --> 01:21:58,719 and boom there you have it we now have a 2416 01:21:56,480 --> 01:22:01,920 list called food it is no longer 2417 01:21:58,719 --> 01:22:04,560 a variable so what we could do is add 2418 01:22:01,920 --> 01:22:06,320 multiple items to this list of food so 2419 01:22:04,560 --> 01:22:07,199 let's add some other food items that we 2420 01:22:06,320 --> 01:22:09,840 might like 2421 01:22:07,199 --> 01:22:11,760 let's see i also like maybe some 2422 01:22:09,840 --> 01:22:15,360 hamburgers 2423 01:22:11,760 --> 01:22:15,360 and a hot dog 2424 01:22:15,679 --> 01:22:19,920 and maybe some spaghetti i think i 2425 01:22:18,719 --> 01:22:22,960 smelled spaghetti right 2426 01:22:19,920 --> 01:22:25,280 okay so we now have a list called food 2427 01:22:22,960 --> 01:22:26,800 now what happens if we attempt to print 2428 01:22:25,280 --> 01:22:30,719 this list of food 2429 01:22:26,800 --> 01:22:34,000 so print food so this will 2430 01:22:30,719 --> 01:22:34,960 print all of the elements found in this 2431 01:22:34,000 --> 01:22:37,760 list 2432 01:22:34,960 --> 01:22:38,400 each item in a list is referred to as an 2433 01:22:37,760 --> 01:22:41,280 element 2434 01:22:38,400 --> 01:22:41,679 if we need to access a certain element 2435 01:22:41,280 --> 01:22:44,400 of 2436 01:22:41,679 --> 01:22:46,239 this list we have to list the index so 2437 01:22:44,400 --> 01:22:47,520 next to our list we'll add a set of 2438 01:22:46,239 --> 01:22:49,920 square brackets again 2439 01:22:47,520 --> 01:22:51,520 and we need to list the numbered index 2440 01:22:49,920 --> 01:22:52,320 of the element that we're trying to 2441 01:22:51,520 --> 01:22:53,760 access 2442 01:22:52,320 --> 01:22:55,360 now computers they always start with 2443 01:22:53,760 --> 01:22:58,320 zero so the first 2444 01:22:55,360 --> 01:22:59,360 element in our list would be element 2445 01:22:58,320 --> 01:23:02,639 number zero 2446 01:22:59,360 --> 01:23:06,239 the next one would be one two and three 2447 01:23:02,639 --> 01:23:07,920 so if we need to access element zero 2448 01:23:06,239 --> 01:23:10,159 which is in the first position we would 2449 01:23:07,920 --> 01:23:12,320 say food square brackets 2450 01:23:10,159 --> 01:23:13,440 and then within here list the index so 2451 01:23:12,320 --> 01:23:15,520 the first position 2452 01:23:13,440 --> 01:23:17,120 is zero and this will now print the 2453 01:23:15,520 --> 01:23:19,280 first element of our list 2454 01:23:17,120 --> 01:23:20,560 which is pizza let's attempt to access 2455 01:23:19,280 --> 01:23:24,159 some of these other elements 2456 01:23:20,560 --> 01:23:28,159 so the element at index one would be 2457 01:23:24,159 --> 01:23:30,960 you guessed it hamburger two 2458 01:23:28,159 --> 01:23:31,920 would be hot dog and three would be 2459 01:23:30,960 --> 01:23:34,719 spaghetti 2460 01:23:31,920 --> 01:23:36,880 now what happens if we attempt to access 2461 01:23:34,719 --> 01:23:38,719 the element at index four 2462 01:23:36,880 --> 01:23:40,159 well currently that's out of range 2463 01:23:38,719 --> 01:23:42,800 because we did not assign 2464 01:23:40,159 --> 01:23:43,600 a value so we're experiencing an error 2465 01:23:42,800 --> 01:23:46,239 an index 2466 01:23:43,600 --> 01:23:47,600 out of range error but if i were to add 2467 01:23:46,239 --> 01:23:51,440 another element 2468 01:23:47,600 --> 01:23:54,560 let's say pudding alright well then 2469 01:23:51,440 --> 01:23:55,199 the element at index 4 would be putting 2470 01:23:54,560 --> 01:23:57,600 then 2471 01:23:55,199 --> 01:23:58,480 one important concept with lists is that 2472 01:23:57,600 --> 01:24:00,719 you can always 2473 01:23:58,480 --> 01:24:01,600 update and change the elements found 2474 01:24:00,719 --> 01:24:03,440 within a list 2475 01:24:01,600 --> 01:24:05,600 later on in the program after you 2476 01:24:03,440 --> 01:24:05,920 declare one so let's say we would like 2477 01:24:05,600 --> 01:24:08,320 to 2478 01:24:05,920 --> 01:24:09,040 immediately change one of these elements 2479 01:24:08,320 --> 01:24:12,320 let's say 2480 01:24:09,040 --> 01:24:15,360 food at index zero i want to 2481 01:24:12,320 --> 01:24:16,080 replace with maybe sushi because sushi 2482 01:24:15,360 --> 01:24:18,880 is great 2483 01:24:16,080 --> 01:24:19,440 so if i were to print the element at 2484 01:24:18,880 --> 01:24:22,719 index 2485 01:24:19,440 --> 01:24:25,760 0 this would no longer be pizza because 2486 01:24:22,719 --> 01:24:28,320 we updated element 0 to be 2487 01:24:25,760 --> 01:24:29,600 sushi then and this prints sushi now if 2488 01:24:28,320 --> 01:24:31,920 you need to display 2489 01:24:29,600 --> 01:24:34,400 all of the elements found within a list 2490 01:24:31,920 --> 01:24:34,880 you can easily do so with a standard for 2491 01:24:34,400 --> 01:24:38,239 loop 2492 01:24:34,880 --> 01:24:41,920 what we'll do is say for x in 2493 01:24:38,239 --> 01:24:45,600 the name of our list which is food 2494 01:24:41,920 --> 01:24:46,639 print x and let me turn this line into a 2495 01:24:45,600 --> 01:24:49,360 comment 2496 01:24:46,639 --> 01:24:51,360 all right so this for loop will print 2497 01:24:49,360 --> 01:24:51,760 all of the elements found within our 2498 01:24:51,360 --> 01:24:55,040 list 2499 01:24:51,760 --> 01:24:57,600 of food and the results are sushi 2500 01:24:55,040 --> 01:24:59,440 hamburger hot dog spaghetti and pudding 2501 01:24:57,600 --> 01:25:01,520 all right people for this next portion 2502 01:24:59,440 --> 01:25:02,400 i'm going to demonstrate a few useful 2503 01:25:01,520 --> 01:25:04,159 functions of 2504 01:25:02,400 --> 01:25:06,320 lists and to access some of these 2505 01:25:04,159 --> 01:25:08,719 functions type the name of your list 2506 01:25:06,320 --> 01:25:10,080 dot and there's a bunch to choose from 2507 01:25:08,719 --> 01:25:12,560 here let's begin with 2508 01:25:10,080 --> 01:25:14,480 event we can add an element to this list 2509 01:25:12,560 --> 01:25:17,679 let's say at the end i would like to add 2510 01:25:14,480 --> 01:25:20,400 ice cream so then if i were to display 2511 01:25:17,679 --> 01:25:22,000 my list we now have ice cream at the end 2512 01:25:20,400 --> 01:25:24,800 because we appended 2513 01:25:22,000 --> 01:25:26,320 this value to my list of food so let's 2514 01:25:24,800 --> 01:25:29,600 go over a few others 2515 01:25:26,320 --> 01:25:32,159 so we can also remove a value food 2516 01:25:29,600 --> 01:25:32,800 dot remove let's say i would like to 2517 01:25:32,159 --> 01:25:36,239 remove 2518 01:25:32,800 --> 01:25:39,520 hotdog so i'm going to type that within 2519 01:25:36,239 --> 01:25:42,880 my function and this will remove hotdog 2520 01:25:39,520 --> 01:25:46,719 and that is no longer here 2521 01:25:42,880 --> 01:25:50,000 next we can pop food.pop 2522 01:25:46,719 --> 01:25:52,560 pop will remove the last element so this 2523 01:25:50,000 --> 01:25:52,560 will remove 2524 01:25:52,840 --> 01:25:57,760 pudding 2525 01:25:54,560 --> 01:26:01,360 we can insert a value at a given index 2526 01:25:57,760 --> 01:26:03,760 food dot insert we need to list an index 2527 01:26:01,360 --> 01:26:05,520 let's say zero that would be the first 2528 01:26:03,760 --> 01:26:06,560 position technically that's where pizza 2529 01:26:05,520 --> 01:26:10,000 is currently 2530 01:26:06,560 --> 01:26:14,639 and i would like to add cake 2531 01:26:10,000 --> 01:26:17,840 and at index 0 we now have cake 2532 01:26:14,639 --> 01:26:20,880 so next up we have sort 2533 01:26:17,840 --> 01:26:22,320 food dot sort this will sort a list 2534 01:26:20,880 --> 01:26:24,480 alphabetically 2535 01:26:22,320 --> 01:26:26,719 so in the first position we have 2536 01:26:24,480 --> 01:26:31,120 hamburger hot dog pudding spaghetti 2537 01:26:26,719 --> 01:26:34,639 then sushi we can also clear a list 2538 01:26:31,120 --> 01:26:37,600 food dot clear and this will remove 2539 01:26:34,639 --> 01:26:39,280 all of the elements of a list so this 2540 01:26:37,600 --> 01:26:40,239 will not print anything because we 2541 01:26:39,280 --> 01:26:42,880 cleared the list 2542 01:26:40,239 --> 01:26:45,600 well everyone in conclusion a list is 2543 01:26:42,880 --> 01:26:48,560 really just a variable that can store 2544 01:26:45,600 --> 01:26:50,000 multiple values think of it that way so 2545 01:26:48,560 --> 01:26:51,360 if you would like a copy 2546 01:26:50,000 --> 01:26:53,199 of all the code that we have written 2547 01:26:51,360 --> 01:26:54,560 here today i will post all of this in 2548 01:26:53,199 --> 01:26:58,239 the comments down below 2549 01:26:54,560 --> 01:27:00,480 but yeah that's how lists work in python 2550 01:26:58,239 --> 01:27:01,280 how's it going everybody it's bro hope 2551 01:27:00,480 --> 01:27:02,960 you're doing well 2552 01:27:01,280 --> 01:27:05,760 and in this video i'm going to explain 2553 01:27:02,960 --> 01:27:09,600 2d lists in python so sit back 2554 01:27:05,760 --> 01:27:09,600 relax and enjoy the show 2555 01:27:10,000 --> 01:27:13,120 i have a super quick video for you all 2556 01:27:12,239 --> 01:27:15,639 today on 2557 01:27:13,120 --> 01:27:17,280 2d lists also referred to as 2558 01:27:15,639 --> 01:27:20,400 multi-dimensional lists 2559 01:27:17,280 --> 01:27:22,239 all it is is a list of separate lists 2560 01:27:20,400 --> 01:27:24,719 so let's begin by creating a few 2561 01:27:22,239 --> 01:27:27,520 separate lists let's say we have a list 2562 01:27:24,719 --> 01:27:28,400 called drinks and let's assign a few 2563 01:27:27,520 --> 01:27:32,000 values 2564 01:27:28,400 --> 01:27:35,440 let's say we have coffee as well 2565 01:27:32,000 --> 01:27:37,760 as soda and maybe some tea 2566 01:27:35,440 --> 01:27:39,679 all right so we currently have one list 2567 01:27:37,760 --> 01:27:40,400 let's make maybe two more let's make a 2568 01:27:39,679 --> 01:27:44,840 list called 2569 01:27:40,400 --> 01:27:49,199 dinner and maybe in here we'll place 2570 01:27:44,840 --> 01:27:49,199 pizza then hamburger 2571 01:27:50,560 --> 01:27:55,040 and hot dog okay 2572 01:27:53,679 --> 01:27:58,400 then let's create one more for this 2573 01:27:55,040 --> 01:27:58,400 example let's say dessert 2574 01:27:59,280 --> 01:28:03,920 and maybe we'll add two items 2575 01:28:02,320 --> 01:28:06,239 they all don't need the same amount of 2576 01:28:03,920 --> 01:28:09,120 items or elements so we have cake 2577 01:28:06,239 --> 01:28:09,120 and ice cream 2578 01:28:09,440 --> 01:28:13,040 all right so we have three lists one 2579 01:28:12,000 --> 01:28:16,080 called drinks 2580 01:28:13,040 --> 01:28:16,560 dinner and dessert so what we can do is 2581 01:28:16,080 --> 01:28:19,600 add 2582 01:28:16,560 --> 01:28:22,480 all of these lists to one list 2583 01:28:19,600 --> 01:28:24,480 and let's create a list called food food 2584 01:28:22,480 --> 01:28:25,600 equals and for each of these elements 2585 01:28:24,480 --> 01:28:29,679 we're going to place 2586 01:28:25,600 --> 01:28:32,480 our lists so we have drinks dinner 2587 01:28:29,679 --> 01:28:33,040 and dessert check this out what if i 2588 01:28:32,480 --> 01:28:36,400 were to 2589 01:28:33,040 --> 01:28:38,480 print my 2d list of food 2590 01:28:36,400 --> 01:28:40,239 so what ends up happening is that this 2591 01:28:38,480 --> 01:28:42,880 will print all of the elements 2592 01:28:40,239 --> 01:28:44,480 found within each individual list and 2593 01:28:42,880 --> 01:28:48,159 they're all grouped together 2594 01:28:44,480 --> 01:28:51,199 so this first portion is my drinks list 2595 01:28:48,159 --> 01:28:51,920 which contains coffee soda tea this next 2596 01:28:51,199 --> 01:28:54,400 element 2597 01:28:51,920 --> 01:28:57,600 is my dinner list which contains pizza 2598 01:28:54,400 --> 01:29:00,880 hamburger hot dog and the last element 2599 01:28:57,600 --> 01:29:02,080 dessert contains cake and ice cream if i 2600 01:29:00,880 --> 01:29:04,960 need to access 2601 01:29:02,080 --> 01:29:05,600 just one of these lists i will add an 2602 01:29:04,960 --> 01:29:08,719 index 2603 01:29:05,600 --> 01:29:11,840 after my food 2d list and 2604 01:29:08,719 --> 01:29:13,360 set an index number so index zero is 2605 01:29:11,840 --> 01:29:15,840 referring to my first 2606 01:29:13,360 --> 01:29:17,760 list of drinks and this will display all 2607 01:29:15,840 --> 01:29:18,560 of the elements found within my first 2608 01:29:17,760 --> 01:29:21,199 list 2609 01:29:18,560 --> 01:29:22,159 and if i need just one of these elements 2610 01:29:21,199 --> 01:29:24,960 i will add 2611 01:29:22,159 --> 01:29:27,199 a second set of square brackets and list 2612 01:29:24,960 --> 01:29:27,920 the index of the item that i'm trying to 2613 01:29:27,199 --> 01:29:31,040 access 2614 01:29:27,920 --> 01:29:33,920 so this very first item coffee would be 2615 01:29:31,040 --> 01:29:34,400 index zero and then index zero again so 2616 01:29:33,920 --> 01:29:36,639 we need 2617 01:29:34,400 --> 01:29:37,679 two sets of square brackets and if i 2618 01:29:36,639 --> 01:29:40,320 were to print this 2619 01:29:37,679 --> 01:29:40,960 this will display coffee so if i go down 2620 01:29:40,320 --> 01:29:44,000 the list 2621 01:29:40,960 --> 01:29:47,199 the next element would be soda 2622 01:29:44,000 --> 01:29:47,760 and then t if i were to change the index 2623 01:29:47,199 --> 01:29:50,320 within 2624 01:29:47,760 --> 01:29:51,360 this first set of square brackets well 2625 01:29:50,320 --> 01:29:53,360 now we're working with 2626 01:29:51,360 --> 01:29:54,960 a different list this time we're working 2627 01:29:53,360 --> 01:29:58,080 with our dinner list 2628 01:29:54,960 --> 01:30:01,280 so the element at index 1 2629 01:29:58,080 --> 01:30:03,920 index 2 that would be our dinner list 2630 01:30:01,280 --> 01:30:04,560 and this will display the element of hot 2631 01:30:03,920 --> 01:30:06,719 dog 2632 01:30:04,560 --> 01:30:08,239 if i change this to the next list which 2633 01:30:06,719 --> 01:30:10,159 would be dessert 2634 01:30:08,239 --> 01:30:11,840 well we're going to receive an index out 2635 01:30:10,159 --> 01:30:14,880 of range error because 2636 01:30:11,840 --> 01:30:18,239 we only have two values within this list 2637 01:30:14,880 --> 01:30:21,760 cake and ice cream there is no element 2638 01:30:18,239 --> 01:30:22,320 at index 2 because we only added two 2639 01:30:21,760 --> 01:30:24,960 elements 2640 01:30:22,320 --> 01:30:26,159 to this list of dessert all right 2641 01:30:24,960 --> 01:30:29,080 everybody so that 2642 01:30:26,159 --> 01:30:30,880 is 2d lists also referred to as 2643 01:30:29,080 --> 01:30:33,120 multi-dimensional lists 2644 01:30:30,880 --> 01:30:34,400 it's a list of separate lists if you 2645 01:30:33,120 --> 01:30:36,880 need to access 2646 01:30:34,400 --> 01:30:37,520 one of the elements within your 2d list 2647 01:30:36,880 --> 01:30:40,159 you need 2648 01:30:37,520 --> 01:30:41,520 two sets of square brackets so if you 2649 01:30:40,159 --> 01:30:43,120 would like a copy of this code 2650 01:30:41,520 --> 01:30:44,880 i will post this in the comments down 2651 01:30:43,120 --> 01:30:48,000 below but yeah that's how 2652 01:30:44,880 --> 01:30:48,639 2d lists work in python hey what's going 2653 01:30:48,000 --> 01:30:50,400 on people 2654 01:30:48,639 --> 01:30:51,920 it's you bro hope you're doing well and 2655 01:30:50,400 --> 01:30:53,440 in this video i'm going to explain 2656 01:30:51,920 --> 01:30:57,920 tuples in python so 2657 01:30:53,440 --> 01:30:57,920 sit back relax and enjoy the show 2658 01:30:58,239 --> 01:31:01,679 so tuples they are collections which are 2659 01:31:01,120 --> 01:31:03,840 ordered 2660 01:31:01,679 --> 01:31:05,040 and unchangeable they're very similar to 2661 01:31:03,840 --> 01:31:07,120 lists but they're ordered 2662 01:31:05,040 --> 01:31:09,199 and we can't change them they're useful 2663 01:31:07,120 --> 01:31:10,880 for grouping together related data 2664 01:31:09,199 --> 01:31:12,719 let's say we would like to create some 2665 01:31:10,880 --> 01:31:14,880 sort of student record 2666 01:31:12,719 --> 01:31:16,719 so we can create a tuple to take care of 2667 01:31:14,880 --> 01:31:18,560 that for us so let's say we would like 2668 01:31:16,719 --> 01:31:20,719 to create a tuple called student 2669 01:31:18,560 --> 01:31:22,000 the process of creating a tuple is very 2670 01:31:20,719 --> 01:31:24,480 similar to lists 2671 01:31:22,000 --> 01:31:26,400 but instead of using a set of square 2672 01:31:24,480 --> 01:31:26,960 brackets to place all of the values 2673 01:31:26,400 --> 01:31:29,360 within 2674 01:31:26,960 --> 01:31:31,360 we're going to use a set of parentheses 2675 01:31:29,360 --> 01:31:33,840 and now we can add a bunch of values 2676 01:31:31,360 --> 01:31:35,760 related to this collection of student 2677 01:31:33,840 --> 01:31:37,600 so let's add some student information 2678 01:31:35,760 --> 01:31:38,159 let's say that this student's name is 2679 01:31:37,600 --> 01:31:40,719 bro 2680 01:31:38,159 --> 01:31:41,440 let's give him an age as well as a 2681 01:31:40,719 --> 01:31:43,360 gender 2682 01:31:41,440 --> 01:31:45,360 all right so there you have it we now 2683 01:31:43,360 --> 01:31:46,800 have a tuple called student 2684 01:31:45,360 --> 01:31:48,400 and let's take a look at some of the 2685 01:31:46,800 --> 01:31:49,920 functions related to tuples 2686 01:31:48,400 --> 01:31:51,679 and you can access them by typing 2687 01:31:49,920 --> 01:31:53,280 student dot 2688 01:31:51,679 --> 01:31:55,280 and then there are a few here not as 2689 01:31:53,280 --> 01:31:56,080 many as lists we have count and we have 2690 01:31:55,280 --> 01:31:58,080 index 2691 01:31:56,080 --> 01:32:00,159 so we can find the count of how many 2692 01:31:58,080 --> 01:32:03,199 times a value appears 2693 01:32:00,159 --> 01:32:04,480 let's count how many times the value of 2694 01:32:03,199 --> 01:32:06,320 bro appears 2695 01:32:04,480 --> 01:32:08,960 so we'll need to put this within a print 2696 01:32:06,320 --> 01:32:12,000 statement so we can actually see it 2697 01:32:08,960 --> 01:32:14,320 so bro appears 2698 01:32:12,000 --> 01:32:16,000 one time within this tuple there is also 2699 01:32:14,320 --> 01:32:19,600 an index method as well 2700 01:32:16,000 --> 01:32:22,080 so type the name of your tuple dot 2701 01:32:19,600 --> 01:32:24,639 and we will use the index method we can 2702 01:32:22,080 --> 01:32:26,239 find the index of a certain value 2703 01:32:24,639 --> 01:32:27,760 let's say we would like to find the 2704 01:32:26,239 --> 01:32:29,360 index of mail 2705 01:32:27,760 --> 01:32:31,120 and then i will just print this to the 2706 01:32:29,360 --> 01:32:34,560 console window 2707 01:32:31,120 --> 01:32:37,600 so the index of the value mail would be 2708 01:32:34,560 --> 01:32:40,159 at two zero one 2709 01:32:37,600 --> 01:32:41,199 two that's it for the methods available 2710 01:32:40,159 --> 01:32:42,960 to tuples 2711 01:32:41,199 --> 01:32:44,880 a few tricks that you can do with tuples 2712 01:32:42,960 --> 01:32:47,760 is that you can display 2713 01:32:44,880 --> 01:32:48,560 all of the contents within a tuple using 2714 01:32:47,760 --> 01:32:51,760 a for loop 2715 01:32:48,560 --> 01:32:55,199 let's say 4x in student 2716 01:32:51,760 --> 01:32:57,199 we will print x so 2717 01:32:55,199 --> 01:32:59,520 this will iterate once through all the 2718 01:32:57,199 --> 01:33:02,080 values found within our tuple 2719 01:32:59,520 --> 01:33:03,440 of student and we can also check to see 2720 01:33:02,080 --> 01:33:05,280 if a certain value 2721 01:33:03,440 --> 01:33:07,679 exists within our tuple using an if 2722 01:33:05,280 --> 01:33:11,120 statement let's say if 2723 01:33:07,679 --> 01:33:13,280 bro in student 2724 01:33:11,120 --> 01:33:14,480 if this is true then what do we want to 2725 01:33:13,280 --> 01:33:17,840 do let's say 2726 01:33:14,480 --> 01:33:21,199 print bro is here 2727 01:33:17,840 --> 01:33:23,920 all right so since bro is within here 2728 01:33:21,199 --> 01:33:24,480 this will be true and this will print 2729 01:33:23,920 --> 01:33:27,120 our 2730 01:33:24,480 --> 01:33:29,280 statement of bro is here all right 2731 01:33:27,120 --> 01:33:31,120 everybody like i said this is a super 2732 01:33:29,280 --> 01:33:34,159 quick video on tuples 2733 01:33:31,120 --> 01:33:36,480 they're very similar to lists except 2734 01:33:34,159 --> 01:33:38,239 they are ordered and unchangeable if 2735 01:33:36,480 --> 01:33:39,840 you'd like a copy of all this code i 2736 01:33:38,239 --> 01:33:40,719 will post all of this in the comments 2737 01:33:39,840 --> 01:33:42,639 down below 2738 01:33:40,719 --> 01:33:44,480 and don't forget to smash that like 2739 01:33:42,639 --> 01:33:46,080 button drop a comment down below 2740 01:33:44,480 --> 01:33:47,600 and subscribe if you'd like to become a 2741 01:33:46,080 --> 01:33:49,760 fellow bro 2742 01:33:47,600 --> 01:33:51,120 hey what's going on everybody it's you 2743 01:33:49,760 --> 01:33:52,880 bro hope you're doing well 2744 01:33:51,120 --> 01:33:55,040 and in this video i'm going to explain 2745 01:33:52,880 --> 01:33:59,920 what a set is in python so 2746 01:33:55,040 --> 01:33:59,920 sit back relax and enjoy the show 2747 01:34:00,080 --> 01:34:04,320 a set is a collection which is unordered 2748 01:34:03,280 --> 01:34:06,480 as well as being 2749 01:34:04,320 --> 01:34:07,760 unindexed they do not allow any 2750 01:34:06,480 --> 01:34:10,159 duplicate values 2751 01:34:07,760 --> 01:34:10,960 for example let's create a set of 2752 01:34:10,159 --> 01:34:12,960 silverware 2753 01:34:10,960 --> 01:34:14,080 so in order to create a set we need a 2754 01:34:12,960 --> 01:34:17,199 name for this set 2755 01:34:14,080 --> 01:34:19,120 let's call it utensils equals 2756 01:34:17,199 --> 01:34:21,280 and in order to create a set we need to 2757 01:34:19,120 --> 01:34:23,920 surround all values with a set 2758 01:34:21,280 --> 01:34:24,719 of curly braces now let's add a few 2759 01:34:23,920 --> 01:34:27,760 utensils 2760 01:34:24,719 --> 01:34:29,440 to the set called utensils let's add a 2761 01:34:27,760 --> 01:34:32,320 fork 2762 01:34:29,440 --> 01:34:33,440 a spoon and maybe a knife and that 2763 01:34:32,320 --> 01:34:36,560 should be good 2764 01:34:33,440 --> 01:34:40,880 and let's display all of the values for 2765 01:34:36,560 --> 01:34:40,880 x in utensils 2766 01:34:41,119 --> 01:34:46,560 print x now with sets 2767 01:34:44,560 --> 01:34:48,400 they're different from lists because 2768 01:34:46,560 --> 01:34:49,360 they're unordered as well as being 2769 01:34:48,400 --> 01:34:51,600 unindexed 2770 01:34:49,360 --> 01:34:52,639 if i were to print all of the elements 2771 01:34:51,600 --> 01:34:54,639 in utensils 2772 01:34:52,639 --> 01:34:56,719 they might not necessarily be in the 2773 01:34:54,639 --> 01:34:59,199 same order in which we placed them 2774 01:34:56,719 --> 01:35:00,880 for example when we printed all of the 2775 01:34:59,199 --> 01:35:03,280 elements found within utensils 2776 01:35:00,880 --> 01:35:05,199 first we had a knife a fork and then a 2777 01:35:03,280 --> 01:35:07,840 spoon if i were to run this again 2778 01:35:05,199 --> 01:35:08,880 they might be in a different order kind 2779 01:35:07,840 --> 01:35:11,840 of like that 2780 01:35:08,880 --> 01:35:14,000 so a set is actually faster than a list 2781 01:35:11,840 --> 01:35:16,960 if you need to check to see if something 2782 01:35:14,000 --> 01:35:19,520 is within a set compared to a list and 2783 01:35:16,960 --> 01:35:21,280 they do not allow any duplicate values 2784 01:35:19,520 --> 01:35:22,960 let me add a bunch of knives and let's 2785 01:35:21,280 --> 01:35:26,159 see what happens 2786 01:35:22,960 --> 01:35:28,000 so i'll have a total of three knives but 2787 01:35:26,159 --> 01:35:30,800 when we print all of the elements 2788 01:35:28,000 --> 01:35:34,000 of the set only one knife appears so 2789 01:35:30,800 --> 01:35:36,560 here's a few useful methods of sets 2790 01:35:34,000 --> 01:35:37,920 the first method is that we can add an 2791 01:35:36,560 --> 01:35:40,320 item to our set 2792 01:35:37,920 --> 01:35:42,960 so type in the name of the set in this 2793 01:35:40,320 --> 01:35:44,960 case it's utensils dot and here's a 2794 01:35:42,960 --> 01:35:47,360 bunch of methods that you can use 2795 01:35:44,960 --> 01:35:48,639 i am looking for add and we can add an 2796 01:35:47,360 --> 01:35:52,159 element to the set 2797 01:35:48,639 --> 01:35:54,800 i would like to add a napkin 2798 01:35:52,159 --> 01:35:56,080 and then if we were to print the set we 2799 01:35:54,800 --> 01:35:58,719 do have a napkin 2800 01:35:56,080 --> 01:35:59,760 within the set we can also remove an 2801 01:35:58,719 --> 01:36:02,880 element 2802 01:35:59,760 --> 01:36:07,600 utensils dot remove 2803 01:36:02,880 --> 01:36:07,600 and i would like to remove the fork 2804 01:36:08,239 --> 01:36:12,800 and now our fork is gone we can also 2805 01:36:11,199 --> 01:36:16,480 clear 2806 01:36:12,800 --> 01:36:18,480 utensils dot clear 2807 01:36:16,480 --> 01:36:20,080 and all the elements within our set 2808 01:36:18,480 --> 01:36:22,239 should be gone 2809 01:36:20,080 --> 01:36:23,440 so let me turn these lines into a 2810 01:36:22,239 --> 01:36:25,520 comment 2811 01:36:23,440 --> 01:36:26,719 all right for this next example we'll 2812 01:36:25,520 --> 01:36:30,560 need a second set 2813 01:36:26,719 --> 01:36:33,280 let's create a second set called dishes 2814 01:36:30,560 --> 01:36:33,600 and then we need a set of curly braces 2815 01:36:33,280 --> 01:36:36,639 and 2816 01:36:33,600 --> 01:36:36,639 let's add a bowl 2817 01:36:36,960 --> 01:36:43,119 a plate and a cup 2818 01:36:40,960 --> 01:36:45,600 our next method is that we're going to 2819 01:36:43,119 --> 01:36:46,800 add one set to another by using the 2820 01:36:45,600 --> 01:36:48,719 update method 2821 01:36:46,800 --> 01:36:51,040 let's say we would like to add our 2822 01:36:48,719 --> 01:36:54,000 dishes set to our utensil set 2823 01:36:51,040 --> 01:36:54,960 so let's type utensils dot and use the 2824 01:36:54,000 --> 01:36:57,679 update method 2825 01:36:54,960 --> 01:36:59,360 and within this method we will pass in 2826 01:36:57,679 --> 01:37:02,639 the set that we would like to add 2827 01:36:59,360 --> 01:37:04,639 so utensils update dishes this will add 2828 01:37:02,639 --> 01:37:07,119 all of the elements found within dishes 2829 01:37:04,639 --> 01:37:08,800 to our utensil set and you can see that 2830 01:37:07,119 --> 01:37:12,000 when we print our utensils 2831 01:37:08,800 --> 01:37:12,639 we have a bowl a plate and a cup in here 2832 01:37:12,000 --> 01:37:14,000 as well 2833 01:37:12,639 --> 01:37:16,400 and then if i were to switch these 2834 01:37:14,000 --> 01:37:20,560 around let's say dishes 2835 01:37:16,400 --> 01:37:23,679 update utensils and then i will 2836 01:37:20,560 --> 01:37:25,360 print everything found within dishes now 2837 01:37:23,679 --> 01:37:28,000 there's a fork a spoon 2838 01:37:25,360 --> 01:37:28,800 and a knife in here as well we can also 2839 01:37:28,000 --> 01:37:31,199 join 2840 01:37:28,800 --> 01:37:32,239 two sets together and create a new set 2841 01:37:31,199 --> 01:37:34,719 entirely 2842 01:37:32,239 --> 01:37:36,560 so let's create a set called dinner 2843 01:37:34,719 --> 01:37:39,040 table like we're setting up a dinner 2844 01:37:36,560 --> 01:37:40,960 table and we need a fork a spoon a knife 2845 01:37:39,040 --> 01:37:44,320 a bowl a plate and a cup 2846 01:37:40,960 --> 01:37:46,960 so we have a dinner table set 2847 01:37:44,320 --> 01:37:48,719 we're going to set the sequel to either 2848 01:37:46,960 --> 01:37:51,760 utensils 2849 01:37:48,719 --> 01:37:54,880 dot union 2850 01:37:51,760 --> 01:37:57,840 dishes or we could do dishes 2851 01:37:54,880 --> 01:37:59,520 union utensils either way would work and 2852 01:37:57,840 --> 01:38:00,800 let's display all the elements found 2853 01:37:59,520 --> 01:38:02,719 within dinner table 2854 01:38:00,800 --> 01:38:04,400 and we should now have elements from 2855 01:38:02,719 --> 01:38:07,040 both sets utensils 2856 01:38:04,400 --> 01:38:09,280 and dishes as well there are also some 2857 01:38:07,040 --> 01:38:11,040 methods in which we can compare 2858 01:38:09,280 --> 01:38:12,320 the similarities as well as the 2859 01:38:11,040 --> 01:38:14,960 differences between 2860 01:38:12,320 --> 01:38:16,880 the elements found within two sets let's 2861 01:38:14,960 --> 01:38:19,679 say i would like to check to see 2862 01:38:16,880 --> 01:38:21,760 what utensils has that dishes doesn't 2863 01:38:19,679 --> 01:38:22,480 and for this example i think i'll add a 2864 01:38:21,760 --> 01:38:24,800 knife 2865 01:38:22,480 --> 01:38:26,639 to my dishes set just so that they have 2866 01:38:24,800 --> 01:38:29,679 at least one thing in common 2867 01:38:26,639 --> 01:38:31,920 so i would like to see what utensils has 2868 01:38:29,679 --> 01:38:32,800 that dishes doesn't so we can do so 2869 01:38:31,920 --> 01:38:35,040 using the 2870 01:38:32,800 --> 01:38:36,880 difference method and i will print the 2871 01:38:35,040 --> 01:38:40,480 results with a print statement 2872 01:38:36,880 --> 01:38:45,040 so i'm going to compare utensils against 2873 01:38:40,480 --> 01:38:48,639 dishes utensils 2874 01:38:45,040 --> 01:38:49,199 dishes and this will print what utensils 2875 01:38:48,639 --> 01:38:52,320 has 2876 01:38:49,199 --> 01:38:54,159 that dishes doesn't which is a fork as 2877 01:38:52,320 --> 01:38:56,000 well as a spoon 2878 01:38:54,159 --> 01:38:58,639 and we can reverse the rolls too we 2879 01:38:56,000 --> 01:39:01,119 could say dishes difference 2880 01:38:58,639 --> 01:39:02,719 utensils what do dishes have that 2881 01:39:01,119 --> 01:39:06,480 utensils doesn't 2882 01:39:02,719 --> 01:39:08,880 and dishes has a cup a bowl and a plate 2883 01:39:06,480 --> 01:39:10,480 they both have knives so that's why 2884 01:39:08,880 --> 01:39:12,159 knife isn't appearing 2885 01:39:10,480 --> 01:39:13,679 we can also check to see if there's 2886 01:39:12,159 --> 01:39:16,880 anything that they have in common 2887 01:39:13,679 --> 01:39:20,320 using the intersection method so i'll 2888 01:39:16,880 --> 01:39:20,320 turn this line into a comment 2889 01:39:20,400 --> 01:39:27,199 we'll print utensils 2890 01:39:23,920 --> 01:39:30,159 dot intersection 2891 01:39:27,199 --> 01:39:32,159 dishes and this will return whatever 2892 01:39:30,159 --> 01:39:35,280 element that they have in common 2893 01:39:32,159 --> 01:39:36,560 which is a knife all right everybody in 2894 01:39:35,280 --> 01:39:39,119 conclusion a set 2895 01:39:36,560 --> 01:39:40,080 is a collection which is unordered as 2896 01:39:39,119 --> 01:39:42,320 well as being 2897 01:39:40,080 --> 01:39:43,520 unindexed they do not allow any 2898 01:39:42,320 --> 01:39:45,920 duplicate values 2899 01:39:43,520 --> 01:39:47,600 and you can do things like compare two 2900 01:39:45,920 --> 01:39:49,440 sets see if there's anything that they 2901 01:39:47,600 --> 01:39:51,440 have in common any differences 2902 01:39:49,440 --> 01:39:53,520 and we can add or remove elements to a 2903 01:39:51,440 --> 01:39:55,360 set if you'd like a copy of all this 2904 01:39:53,520 --> 01:39:56,639 code i will post all of this in the 2905 01:39:55,360 --> 01:39:58,480 comments down below 2906 01:39:56,639 --> 01:40:00,560 don't be afraid to smash that like 2907 01:39:58,480 --> 01:40:02,159 button drop a comment down below 2908 01:40:00,560 --> 01:40:05,119 and subscribe if you'd like to become a 2909 01:40:02,159 --> 01:40:06,960 fellow bro hey what's going on everybody 2910 01:40:05,119 --> 01:40:08,480 it's you bro hope you're doing well and 2911 01:40:06,960 --> 01:40:11,199 in this video i'm going to explain 2912 01:40:08,480 --> 01:40:11,840 how dictionaries work in python so sit 2913 01:40:11,199 --> 01:40:15,679 back 2914 01:40:11,840 --> 01:40:15,679 relax and enjoy the show 2915 01:40:15,760 --> 01:40:19,119 all right let's get down to business a 2916 01:40:17,760 --> 01:40:22,159 dictionary is a 2917 01:40:19,119 --> 01:40:25,679 changeable unordered collection of 2918 01:40:22,159 --> 01:40:27,199 unique key value pairs they're fast 2919 01:40:25,679 --> 01:40:29,440 because they use hashing 2920 01:40:27,199 --> 01:40:30,239 and they allow us to access a value 2921 01:40:29,440 --> 01:40:32,320 quickly 2922 01:40:30,239 --> 01:40:33,679 now to create a dictionary it's very 2923 01:40:32,320 --> 01:40:36,159 similar to creating a 2924 01:40:33,679 --> 01:40:36,880 set based on the last video except we're 2925 01:40:36,159 --> 01:40:39,679 going to store 2926 01:40:36,880 --> 01:40:40,639 unique key value pairs let's create a 2927 01:40:39,679 --> 01:40:43,440 dictionary 2928 01:40:40,639 --> 01:40:44,159 of countries and their capitals we can 2929 01:40:43,440 --> 01:40:46,960 store those 2930 01:40:44,159 --> 01:40:48,159 as key value pairs so let's call this 2931 01:40:46,960 --> 01:40:51,040 dictionary 2932 01:40:48,159 --> 01:40:53,040 capitals capitals equals then we need a 2933 01:40:51,040 --> 01:40:53,360 set of curly braces much like what we 2934 01:40:53,040 --> 01:40:56,480 use 2935 01:40:53,360 --> 01:40:59,600 with sets now we need a key 2936 01:40:56,480 --> 01:41:00,560 and a value let's say we have the usa as 2937 01:40:59,600 --> 01:41:03,119 a key 2938 01:41:00,560 --> 01:41:04,480 and the capital of the usa would be 2939 01:41:03,119 --> 01:41:06,800 washington dc 2940 01:41:04,480 --> 01:41:08,639 so in order to associate a value with 2941 01:41:06,800 --> 01:41:09,119 the key we'll follow the key with a 2942 01:41:08,639 --> 01:41:11,360 colon 2943 01:41:09,119 --> 01:41:12,719 and then add some value and the data 2944 01:41:11,360 --> 01:41:14,800 type really doesn't matter but 2945 01:41:12,719 --> 01:41:16,320 in this case we're just using strings so 2946 01:41:14,800 --> 01:41:19,360 the capital of the usa 2947 01:41:16,320 --> 01:41:21,760 would be washington dc 2948 01:41:19,360 --> 01:41:23,600 and to add another key value pair we'll 2949 01:41:21,760 --> 01:41:25,520 separate each with a comma 2950 01:41:23,600 --> 01:41:28,159 and let's add a few others for good 2951 01:41:25,520 --> 01:41:31,520 measure so let's say we have india next 2952 01:41:28,159 --> 01:41:35,040 as a key and the value would be 2953 01:41:31,520 --> 01:41:38,080 new delhi and we'll add two more 2954 01:41:35,040 --> 01:41:41,679 let's say we have china 2955 01:41:38,080 --> 01:41:46,000 and the capital of china is beijing 2956 01:41:41,679 --> 01:41:49,920 and lastly let's add russia 2957 01:41:46,000 --> 01:41:51,199 and the capital of russia is moscow 2958 01:41:49,920 --> 01:41:53,520 and there you have it ladies and 2959 01:41:51,199 --> 01:41:54,639 gentlemen we now have a dictionary 2960 01:41:53,520 --> 01:41:57,760 called capitals 2961 01:41:54,639 --> 01:41:58,560 that has unique key value pairs so 2962 01:41:57,760 --> 01:42:00,800 remember with 2963 01:41:58,560 --> 01:42:02,480 dictionaries they are unordered in order 2964 01:42:00,800 --> 01:42:04,560 to access one of these values 2965 01:42:02,480 --> 01:42:06,639 instead of using a numbered index we're 2966 01:42:04,560 --> 01:42:07,360 going to use the associated key with 2967 01:42:06,639 --> 01:42:09,199 that value 2968 01:42:07,360 --> 01:42:10,960 here's an example let's say i would like 2969 01:42:09,199 --> 01:42:14,159 to print the capital 2970 01:42:10,960 --> 01:42:14,880 of russia so i need to use this key of 2971 01:42:14,159 --> 01:42:16,960 russia 2972 01:42:14,880 --> 01:42:18,000 so in order to do this type in the name 2973 01:42:16,960 --> 01:42:20,320 of the dictionary 2974 01:42:18,000 --> 01:42:21,520 followed by a set of straight brackets 2975 01:42:20,320 --> 01:42:25,040 and then instead of an 2976 01:42:21,520 --> 01:42:27,600 index number like 0 or 1 2 3 2977 01:42:25,040 --> 01:42:28,400 i'm going to use the key so i would like 2978 01:42:27,600 --> 01:42:31,040 to print 2979 01:42:28,400 --> 01:42:31,920 the value that's stored or associated 2980 01:42:31,040 --> 01:42:35,119 with the key 2981 01:42:31,920 --> 01:42:36,159 of russia so when i run this this will 2982 01:42:35,119 --> 01:42:39,280 print 2983 01:42:36,159 --> 01:42:40,080 that value which is moscow so this isn't 2984 01:42:39,280 --> 01:42:41,840 always safe 2985 01:42:40,080 --> 01:42:43,199 let's say we have a key that doesn't 2986 01:42:41,840 --> 01:42:44,960 exist like germany 2987 01:42:43,199 --> 01:42:47,679 germany is not currently within my 2988 01:42:44,960 --> 01:42:49,440 dictionary so if i were to run this 2989 01:42:47,679 --> 01:42:51,040 well then my program is going to 2990 01:42:49,440 --> 01:42:52,880 encounter an error 2991 01:42:51,040 --> 01:42:54,080 and this will interrupt the normal flow 2992 01:42:52,880 --> 01:42:57,520 of my program 2993 01:42:54,080 --> 01:42:59,360 a much safer way to access a key 2994 01:42:57,520 --> 01:43:01,040 to check to see if it's there or not is 2995 01:42:59,360 --> 01:43:03,520 to use the get method 2996 01:43:01,040 --> 01:43:04,960 of dictionaries so i'm going to turn 2997 01:43:03,520 --> 01:43:06,560 this line into a comment and let's try 2998 01:43:04,960 --> 01:43:07,360 this again but this time we'll use the 2999 01:43:06,560 --> 01:43:09,360 get method 3000 01:43:07,360 --> 01:43:12,000 so type in the name of the dictionary 3001 01:43:09,360 --> 01:43:15,199 dot get 3002 01:43:12,000 --> 01:43:18,560 and list the key that you want to use 3003 01:43:15,199 --> 01:43:21,679 so i would like to see if germany 3004 01:43:18,560 --> 01:43:23,040 is within my dictionary currently there 3005 01:43:21,679 --> 01:43:25,199 isn't so this will return 3006 01:43:23,040 --> 01:43:26,320 none and we will not encounter an error 3007 01:43:25,199 --> 01:43:28,800 so this is a much 3008 01:43:26,320 --> 01:43:30,719 safer way of checking to see if there is 3009 01:43:28,800 --> 01:43:32,639 a key within your dictionary 3010 01:43:30,719 --> 01:43:35,199 here's a few other useful methods there 3011 01:43:32,639 --> 01:43:38,000 is a method to print only the keys 3012 01:43:35,199 --> 01:43:39,119 so we type in the name of the dictionary 3013 01:43:38,000 --> 01:43:41,119 capitals 3014 01:43:39,119 --> 01:43:42,880 and here's a listing of all of the 3015 01:43:41,119 --> 01:43:45,440 methods that we have access to 3016 01:43:42,880 --> 01:43:46,560 i'm looking for the keys method and this 3017 01:43:45,440 --> 01:43:49,360 will print 3018 01:43:46,560 --> 01:43:50,239 only the keys and not the values we can 3019 01:43:49,360 --> 01:43:53,360 also print 3020 01:43:50,239 --> 01:43:55,119 just the values and not the keys so type 3021 01:43:53,360 --> 01:43:59,360 in the name of the dictionary again 3022 01:43:55,119 --> 01:44:02,239 capitals dot values 3023 01:43:59,360 --> 01:44:03,679 and this will print only the values or 3024 01:44:02,239 --> 01:44:06,320 you could print everything 3025 01:44:03,679 --> 01:44:07,199 both the keys and the values and you do 3026 01:44:06,320 --> 01:44:10,719 so by using 3027 01:44:07,199 --> 01:44:14,719 the items method capitals 3028 01:44:10,719 --> 01:44:14,719 dot items 3029 01:44:15,199 --> 01:44:18,880 and this will print your entire 3030 01:44:16,560 --> 01:44:19,440 dictionary one other way in which we can 3031 01:44:18,880 --> 01:44:21,040 display 3032 01:44:19,440 --> 01:44:23,199 all of the key value pairs in a 3033 01:44:21,040 --> 01:44:25,360 dictionary is to use a for loop 3034 01:44:23,199 --> 01:44:26,400 so at the end let's create a for loop 3035 01:44:25,360 --> 01:44:30,320 for 3036 01:44:26,400 --> 01:44:33,360 key comma value in 3037 01:44:30,320 --> 01:44:36,000 capitals dot items 3038 01:44:33,360 --> 01:44:37,119 this is going to iterate once for each 3039 01:44:36,000 --> 01:44:40,000 key value pair 3040 01:44:37,119 --> 01:44:40,800 in my dictionary for each key value in 3041 01:44:40,000 --> 01:44:44,000 capitals 3042 01:44:40,800 --> 01:44:47,520 dot items print 3043 01:44:44,000 --> 01:44:50,080 key comma value 3044 01:44:47,520 --> 01:44:51,840 and this will print my entire dictionary 3045 01:44:50,080 --> 01:44:54,000 a feature of dictionaries 3046 01:44:51,840 --> 01:44:55,840 is that they are mutable that means we 3047 01:44:54,000 --> 01:44:58,320 can change them or alter them 3048 01:44:55,840 --> 01:44:59,760 after the program is already running so 3049 01:44:58,320 --> 01:45:01,840 one way in which we can do that 3050 01:44:59,760 --> 01:45:02,960 is to use the update method of 3051 01:45:01,840 --> 01:45:06,000 dictionaries 3052 01:45:02,960 --> 01:45:06,800 so let's add germany as a key and give 3053 01:45:06,000 --> 01:45:09,840 it a value 3054 01:45:06,800 --> 01:45:11,280 of berlin so type in the name of the 3055 01:45:09,840 --> 01:45:13,760 dictionary capitals 3056 01:45:11,280 --> 01:45:15,679 and we'll use the update method and 3057 01:45:13,760 --> 01:45:18,159 within the parentheses of this method 3058 01:45:15,679 --> 01:45:19,360 we'll add a set of curly braces and now 3059 01:45:18,159 --> 01:45:23,440 we can add a new 3060 01:45:19,360 --> 01:45:27,280 key value pair so let's add germany 3061 01:45:23,440 --> 01:45:28,480 and a value of berlin so if i were to 3062 01:45:27,280 --> 01:45:30,480 run this and display 3063 01:45:28,480 --> 01:45:32,320 all of the key value pairs found within 3064 01:45:30,480 --> 01:45:35,760 my dictionary we now have 3065 01:45:32,320 --> 01:45:38,080 germany as a key and berlin as its value 3066 01:45:35,760 --> 01:45:39,199 not only can you use the update method 3067 01:45:38,080 --> 01:45:41,199 to add a new 3068 01:45:39,199 --> 01:45:43,280 key value pair but you can update an 3069 01:45:41,199 --> 01:45:44,000 existing one let's say we would like to 3070 01:45:43,280 --> 01:45:47,440 change the 3071 01:45:44,000 --> 01:45:51,520 capital of usa so capitals 3072 01:45:47,440 --> 01:45:54,639 dot update parentheses curly braces 3073 01:45:51,520 --> 01:45:57,679 list the key usa colon 3074 01:45:54,639 --> 01:45:59,440 and then we can give the usa a new value 3075 01:45:57,679 --> 01:46:01,760 let's pretend that the united states 3076 01:45:59,440 --> 01:46:05,199 government has decided to relocate 3077 01:46:01,760 --> 01:46:08,719 their capital to las vegas 3078 01:46:05,199 --> 01:46:11,600 so this will now update our key of usa 3079 01:46:08,719 --> 01:46:14,000 with a new value and if i were to print 3080 01:46:11,600 --> 01:46:16,960 all of the key value pairs found within 3081 01:46:14,000 --> 01:46:17,760 my dictionary of capitals the usa has a 3082 01:46:16,960 --> 01:46:21,119 new value 3083 01:46:17,760 --> 01:46:22,719 of las vegas so the last two methods i'm 3084 01:46:21,119 --> 01:46:25,040 going to cover are both pop 3085 01:46:22,719 --> 01:46:25,760 and clear you can use the pop method to 3086 01:46:25,040 --> 01:46:29,040 remove 3087 01:46:25,760 --> 01:46:32,800 a key value pair so capitals 3088 01:46:29,040 --> 01:46:34,480 dot pop parentheses and list the key 3089 01:46:32,800 --> 01:46:36,239 of the key value pair that you would 3090 01:46:34,480 --> 01:46:37,600 like to remove so let's say we would 3091 01:46:36,239 --> 01:46:39,920 like to remove china 3092 01:46:37,600 --> 01:46:41,360 so we'll list that key and then when we 3093 01:46:39,920 --> 01:46:44,000 use the pop method 3094 01:46:41,360 --> 01:46:45,280 that will remove this key value pair 3095 01:46:44,000 --> 01:46:47,440 from my dictionary 3096 01:46:45,280 --> 01:46:49,119 and then of course we have clear as well 3097 01:46:47,440 --> 01:46:52,560 which will just remove everything 3098 01:46:49,119 --> 01:46:55,199 capitals dot clear 3099 01:46:52,560 --> 01:46:56,080 and this will clear my dictionary all 3100 01:46:55,199 --> 01:46:58,880 right everybody 3101 01:46:56,080 --> 01:46:59,840 so in conclusion a dictionary is a 3102 01:46:58,880 --> 01:47:02,800 changeable 3103 01:46:59,840 --> 01:47:03,520 unordered collection of unique key value 3104 01:47:02,800 --> 01:47:05,600 pairs 3105 01:47:03,520 --> 01:47:07,760 they're fast because they use hashing 3106 01:47:05,600 --> 01:47:08,400 and they allow us to access a value 3107 01:47:07,760 --> 01:47:10,800 quickly 3108 01:47:08,400 --> 01:47:11,679 well that's the basics of dictionaries 3109 01:47:10,800 --> 01:47:13,199 in python 3110 01:47:11,679 --> 01:47:15,119 if you would like a copy of all this 3111 01:47:13,199 --> 01:47:16,400 code i will post all of this in the 3112 01:47:15,119 --> 01:47:18,719 comments down below 3113 01:47:16,400 --> 01:47:20,480 but yeah that's how dictionaries work in 3114 01:47:18,719 --> 01:47:22,639 python 3115 01:47:20,480 --> 01:47:24,320 hey what's going on everybody it's bro 3116 01:47:22,639 --> 01:47:25,679 hope you're doing well and in this video 3117 01:47:24,320 --> 01:47:26,560 i'm going to teach you guys all about 3118 01:47:25,679 --> 01:47:29,840 the index 3119 01:47:26,560 --> 01:47:32,800 operator in python so sit back relax 3120 01:47:29,840 --> 01:47:32,800 and enjoy the show 3121 01:47:32,880 --> 01:47:36,719 all right people let's begin i'm going 3122 01:47:34,800 --> 01:47:38,159 to be explaining the index operator 3123 01:47:36,719 --> 01:47:41,040 which is represented by 3124 01:47:38,159 --> 01:47:41,760 a set of square brackets now they give 3125 01:47:41,040 --> 01:47:44,719 access 3126 01:47:41,760 --> 01:47:46,000 to a sequences elements they include but 3127 01:47:44,719 --> 01:47:48,800 are not limited to 3128 01:47:46,000 --> 01:47:50,320 strings lists and tuples for this 3129 01:47:48,800 --> 01:47:52,080 example we're going to be working with 3130 01:47:50,320 --> 01:47:52,560 strings because strings are easy to work 3131 01:47:52,080 --> 01:47:55,199 with 3132 01:47:52,560 --> 01:47:56,159 let's say we have a name and name equals 3133 01:47:55,199 --> 01:47:58,880 whatever your first 3134 01:47:56,159 --> 01:48:00,239 and last name is we can use the index 3135 01:47:58,880 --> 01:48:03,520 operator to access 3136 01:48:00,239 --> 01:48:05,840 an element of the sequence our string 3137 01:48:03,520 --> 01:48:07,280 so let's check to see if the first 3138 01:48:05,840 --> 01:48:09,440 letter in our name 3139 01:48:07,280 --> 01:48:11,040 is lowercase and i should probably 3140 01:48:09,440 --> 01:48:11,840 change that to lowercase for this 3141 01:48:11,040 --> 01:48:14,960 example 3142 01:48:11,840 --> 01:48:16,320 so within an if statement if name and we 3143 01:48:14,960 --> 01:48:19,360 can add a set 3144 01:48:16,320 --> 01:48:22,400 of square brackets after our sequence 3145 01:48:19,360 --> 01:48:25,199 which is a string and we can check 3146 01:48:22,400 --> 01:48:26,000 a given element of our sequence let's 3147 01:48:25,199 --> 01:48:29,119 check to see 3148 01:48:26,000 --> 01:48:31,119 if the first letter is lowercase 3149 01:48:29,119 --> 01:48:33,199 now with computers they always start at 3150 01:48:31,119 --> 01:48:33,760 zero if you need to access the first 3151 01:48:33,199 --> 01:48:36,800 element 3152 01:48:33,760 --> 01:48:38,639 in a sequence that would be index zero 3153 01:48:36,800 --> 01:48:40,719 and then the next sequence well the next 3154 01:48:38,639 --> 01:48:42,880 element in the sequence would be one 3155 01:48:40,719 --> 01:48:44,719 then two then you followed that pattern 3156 01:48:42,880 --> 01:48:47,040 so i would like to check to see if 3157 01:48:44,719 --> 01:48:48,639 name at index zero that would be the 3158 01:48:47,040 --> 01:48:51,119 first character 3159 01:48:48,639 --> 01:48:52,239 is lowercase so there is a method to 3160 01:48:51,119 --> 01:48:55,440 check that 3161 01:48:52,239 --> 01:48:58,239 dot is lower this will return 3162 01:48:55,440 --> 01:48:59,119 true or false if the first letter is 3163 01:48:58,239 --> 01:49:00,960 lowercase 3164 01:48:59,119 --> 01:49:03,360 then our if statement is going to be 3165 01:49:00,960 --> 01:49:06,239 resolved to true 3166 01:49:03,360 --> 01:49:07,360 and what i could do is that i can 3167 01:49:06,239 --> 01:49:10,800 perhaps change it 3168 01:49:07,360 --> 01:49:14,239 to uppercase then so let's reassign 3169 01:49:10,800 --> 01:49:17,520 name to equal name dot 3170 01:49:14,239 --> 01:49:21,520 and there is a capitalize function here 3171 01:49:17,520 --> 01:49:24,880 and then let's print our name print 3172 01:49:21,520 --> 01:49:27,520 name so when we run this 3173 01:49:24,880 --> 01:49:28,639 the first letter in my name is now 3174 01:49:27,520 --> 01:49:30,960 capitalized 3175 01:49:28,639 --> 01:49:32,880 so if you need to access an element 3176 01:49:30,960 --> 01:49:36,000 within a sequence a string or 3177 01:49:32,880 --> 01:49:38,320 list or a tuple you would add a set 3178 01:49:36,000 --> 01:49:39,599 of square brackets afterwards and then 3179 01:49:38,320 --> 01:49:41,119 you list an integer 3180 01:49:39,599 --> 01:49:43,599 of the element that you're trying to 3181 01:49:41,119 --> 01:49:44,480 access so here's a few other examples 3182 01:49:43,599 --> 01:49:47,679 i'm going to turn 3183 01:49:44,480 --> 01:49:49,199 this as well as this into a comment 3184 01:49:47,679 --> 01:49:51,360 and let's say we would like to create 3185 01:49:49,199 --> 01:49:54,080 some substrings and we can do so 3186 01:49:51,360 --> 01:49:54,400 using our index operator so i would like 3187 01:49:54,080 --> 01:49:57,040 to 3188 01:49:54,400 --> 01:49:57,920 create a substring from the first part 3189 01:49:57,040 --> 01:50:00,320 of my name 3190 01:49:57,920 --> 01:50:01,440 so what i'll do is create a new variable 3191 01:50:00,320 --> 01:50:05,599 first 3192 01:50:01,440 --> 01:50:08,880 name equals name index operator 3193 01:50:05,599 --> 01:50:09,679 so you can specify a range for an index 3194 01:50:08,880 --> 01:50:12,480 operator 3195 01:50:09,679 --> 01:50:14,239 so you would set the starting position 3196 01:50:12,480 --> 01:50:16,400 on the left hand side followed by a 3197 01:50:14,239 --> 01:50:18,400 colon and then your ending position 3198 01:50:16,400 --> 01:50:19,679 so i would like the substring to begin 3199 01:50:18,400 --> 01:50:22,880 at index 0 and 3200 01:50:19,679 --> 01:50:25,679 end at 3 so that would be 0 colon 3201 01:50:22,880 --> 01:50:26,639 then three all right and then i would 3202 01:50:25,679 --> 01:50:29,199 like to make this all 3203 01:50:26,639 --> 01:50:32,320 uppercase for some reason and then let's 3204 01:50:29,199 --> 01:50:32,320 print my first name 3205 01:50:33,040 --> 01:50:36,639 so with the index operator we specified 3206 01:50:35,920 --> 01:50:39,199 a range 3207 01:50:36,639 --> 01:50:39,679 we would like to access elements 0 3208 01:50:39,199 --> 01:50:42,080 through 3209 01:50:39,679 --> 01:50:42,960 3. turn them up your case and assign 3210 01:50:42,080 --> 01:50:45,679 them to a new 3211 01:50:42,960 --> 01:50:46,800 variable of first name here's a shortcut 3212 01:50:45,679 --> 01:50:49,440 that you might find useful 3213 01:50:46,800 --> 01:50:50,960 if the index for your range begins with 3214 01:50:49,440 --> 01:50:52,880 zero you can actually delete that you 3215 01:50:50,960 --> 01:50:55,119 don't even necessarily need that zero 3216 01:50:52,880 --> 01:50:57,119 now we just have a colon three and this 3217 01:50:55,119 --> 01:50:59,119 would work the same as it did before 3218 01:50:57,119 --> 01:51:00,960 so that's a useful shortcut and we're 3219 01:50:59,119 --> 01:51:03,920 going to bring it up later if we need to 3220 01:51:00,960 --> 01:51:05,760 access the last element in a sequence 3221 01:51:03,920 --> 01:51:09,599 this time let's create a 3222 01:51:05,760 --> 01:51:13,599 substring of last name so last 3223 01:51:09,599 --> 01:51:16,719 name equals name index operator 3224 01:51:13,599 --> 01:51:20,239 this time i would like to access the 3225 01:51:16,719 --> 01:51:24,080 element at index let's see 0 1 3226 01:51:20,239 --> 01:51:27,360 2 3 4. all right so i'm going to say 3227 01:51:24,080 --> 01:51:29,440 for colon and we're not really sure 3228 01:51:27,360 --> 01:51:30,080 where our name our last name is going to 3229 01:51:29,440 --> 01:51:32,639 end 3230 01:51:30,080 --> 01:51:33,360 so what you could do is just say colon 3231 01:51:32,639 --> 01:51:36,800 and then leave 3232 01:51:33,360 --> 01:51:38,639 the next index blank so that will be 3233 01:51:36,800 --> 01:51:40,159 the index beginning at four and 3234 01:51:38,639 --> 01:51:42,960 everything afterwards 3235 01:51:40,159 --> 01:51:44,639 and let's say two lower to make this all 3236 01:51:42,960 --> 01:51:48,880 lowercase for some reason 3237 01:51:44,639 --> 01:51:52,400 so let's print our last name print 3238 01:51:48,880 --> 01:51:56,480 last name so this should 3239 01:51:52,400 --> 01:51:58,880 take everything starting at index 4 3240 01:51:56,480 --> 01:52:01,040 and after that and turn it into its own 3241 01:51:58,880 --> 01:52:03,840 substring of last name 3242 01:52:01,040 --> 01:52:05,679 now you can access the last element in a 3243 01:52:03,840 --> 01:52:08,880 sequence by using what's called 3244 01:52:05,679 --> 01:52:11,119 negative indexing so let's add 3245 01:52:08,880 --> 01:52:13,040 a exclamation point to the end of our 3246 01:52:11,119 --> 01:52:16,960 name so let's say 3247 01:52:13,040 --> 01:52:21,040 we have a new variable called last 3248 01:52:16,960 --> 01:52:23,599 character and i would like to store 3249 01:52:21,040 --> 01:52:24,080 the last character in my name to this 3250 01:52:23,599 --> 01:52:27,040 new 3251 01:52:24,080 --> 01:52:27,840 variable this new substring so to access 3252 01:52:27,040 --> 01:52:31,199 the last 3253 01:52:27,840 --> 01:52:32,320 element in a sequence within the index 3254 01:52:31,199 --> 01:52:35,520 operator we can use 3255 01:52:32,320 --> 01:52:36,159 negative indexing so the last element 3256 01:52:35,520 --> 01:52:38,880 would be 3257 01:52:36,159 --> 01:52:39,360 negative one all right and then let's 3258 01:52:38,880 --> 01:52:42,400 print 3259 01:52:39,360 --> 01:52:45,679 whatever our last character is so this 3260 01:52:42,400 --> 01:52:48,080 should print an exclamation point 3261 01:52:45,679 --> 01:52:48,880 yep which did so then if this was 3262 01:52:48,080 --> 01:52:51,440 negative 2 3263 01:52:48,880 --> 01:52:52,800 that would be the second to last element 3264 01:52:51,440 --> 01:52:55,599 in a sequence 3265 01:52:52,800 --> 01:52:56,719 so now that would be e because e is in 3266 01:52:55,599 --> 01:52:59,199 the second to last 3267 01:52:56,719 --> 01:53:01,119 element in my sequence all right 3268 01:52:59,199 --> 01:53:02,159 everybody so that's the basics of the 3269 01:53:01,119 --> 01:53:04,880 index operator 3270 01:53:02,159 --> 01:53:05,920 you can add a set of square brackets 3271 01:53:04,880 --> 01:53:08,480 after a string 3272 01:53:05,920 --> 01:53:10,080 a list or a tuple and then list an 3273 01:53:08,480 --> 01:53:11,280 integer or a range 3274 01:53:10,080 --> 01:53:13,520 of the elements that you're trying to 3275 01:53:11,280 --> 01:53:15,280 access so if you would like a copy of 3276 01:53:13,520 --> 01:53:16,560 all this code i will post everything in 3277 01:53:15,280 --> 01:53:18,480 the comments down below 3278 01:53:16,560 --> 01:53:20,320 don't be afraid to help me out and smash 3279 01:53:18,480 --> 01:53:20,960 that like button drop a comment down 3280 01:53:20,320 --> 01:53:22,480 below 3281 01:53:20,960 --> 01:53:24,159 and subscribe if you'd like to become a 3282 01:53:22,480 --> 01:53:26,000 fellow bro 3283 01:53:24,159 --> 01:53:27,199 hey what's going on everybody it's you 3284 01:53:26,000 --> 01:53:28,880 bro hope you're doing well 3285 01:53:27,199 --> 01:53:31,199 and in this video i'm going to explain 3286 01:53:28,880 --> 01:53:35,679 how functions work in python so 3287 01:53:31,199 --> 01:53:35,679 sit back relax and enjoy the show 3288 01:53:36,080 --> 01:53:39,119 if you find this video helpful please 3289 01:53:37,679 --> 01:53:41,599 remember to like 3290 01:53:39,119 --> 01:53:43,440 comment and subscribe your support will 3291 01:53:41,599 --> 01:53:46,480 help keep this channel running 3292 01:53:43,440 --> 01:53:49,119 okay let's do this thing a function 3293 01:53:46,480 --> 01:53:50,560 is a block of code which is executed 3294 01:53:49,119 --> 01:53:53,280 only when it is called 3295 01:53:50,560 --> 01:53:54,159 this process is also known as invoking a 3296 01:53:53,280 --> 01:53:56,000 function 3297 01:53:54,159 --> 01:53:57,280 with programming we try not to repeat 3298 01:53:56,000 --> 01:53:59,599 code if we don't have to 3299 01:53:57,280 --> 01:54:01,040 that's why functions are useful because 3300 01:53:59,599 --> 01:54:03,599 our function can perform 3301 01:54:01,040 --> 01:54:04,480 some specific task for us some block of 3302 01:54:03,599 --> 01:54:06,800 code for us 3303 01:54:04,480 --> 01:54:08,159 whenever it is called so we only need to 3304 01:54:06,800 --> 01:54:10,159 write that block of code 3305 01:54:08,159 --> 01:54:12,239 once and if we need to repeat it we just 3306 01:54:10,159 --> 01:54:13,840 call this function one more time 3307 01:54:12,239 --> 01:54:15,920 so let's define and create a function 3308 01:54:13,840 --> 01:54:17,520 we'll create a function called hello 3309 01:54:15,920 --> 01:54:19,040 and in order to define a function we'll 3310 01:54:17,520 --> 01:54:21,760 type def 3311 01:54:19,040 --> 01:54:23,280 and then a unique function name so let's 3312 01:54:21,760 --> 01:54:25,119 call this function hello 3313 01:54:23,280 --> 01:54:26,960 and with functions they always end with 3314 01:54:25,119 --> 01:54:29,040 a set of parentheses 3315 01:54:26,960 --> 01:54:30,159 and lastly to finish defining this 3316 01:54:29,040 --> 01:54:33,040 function we'll add 3317 01:54:30,159 --> 01:54:33,840 a colon at the end so if you go down to 3318 01:54:33,040 --> 01:54:36,320 the next line 3319 01:54:33,840 --> 01:54:37,280 take notice that my cursor is now 3320 01:54:36,320 --> 01:54:39,679 indented 3321 01:54:37,280 --> 01:54:40,480 any code underneath a function that is 3322 01:54:39,679 --> 01:54:42,880 indented 3323 01:54:40,480 --> 01:54:44,320 belongs to that specific function and 3324 01:54:42,880 --> 01:54:47,119 will only execute 3325 01:54:44,320 --> 01:54:48,880 whenever that function is called so for 3326 01:54:47,119 --> 01:54:50,080 now we need to type in something 3327 01:54:48,880 --> 01:54:52,000 if you don't know what you want your 3328 01:54:50,080 --> 01:54:52,719 function to do you can always type pass 3329 01:54:52,000 --> 01:54:56,320 for now 3330 01:54:52,719 --> 01:55:00,080 but let's print something print the word 3331 01:54:56,320 --> 01:55:02,639 hello and in order to call this function 3332 01:55:00,080 --> 01:55:04,560 all we need to do within our program is 3333 01:55:02,639 --> 01:55:05,360 type the name of the function which is 3334 01:55:04,560 --> 01:55:07,280 hello 3335 01:55:05,360 --> 01:55:09,199 and then add a set of parentheses 3336 01:55:07,280 --> 01:55:10,320 because functions always end with a set 3337 01:55:09,199 --> 01:55:13,199 of parentheses 3338 01:55:10,320 --> 01:55:13,840 so what this will do is execute my 3339 01:55:13,199 --> 01:55:15,679 function 3340 01:55:13,840 --> 01:55:17,040 once and perform this block of code 3341 01:55:15,679 --> 01:55:19,599 whenever it is called 3342 01:55:17,040 --> 01:55:21,119 if i were to remove this well our 3343 01:55:19,599 --> 01:55:21,760 program currently isn't going to do 3344 01:55:21,119 --> 01:55:24,080 anything 3345 01:55:21,760 --> 01:55:25,119 because functions only execute their 3346 01:55:24,080 --> 01:55:27,840 block of code 3347 01:55:25,119 --> 01:55:28,960 only when it is called so this time 3348 01:55:27,840 --> 01:55:31,920 let's print the word 3349 01:55:28,960 --> 01:55:32,480 hello three times so i will call this 3350 01:55:31,920 --> 01:55:35,599 function 3351 01:55:32,480 --> 01:55:38,320 three separate times hello hello hello 3352 01:55:35,599 --> 01:55:39,199 so when i run this our hello function 3353 01:55:38,320 --> 01:55:41,840 will be called 3354 01:55:39,199 --> 01:55:44,400 three separate times now with functions 3355 01:55:41,840 --> 01:55:46,719 they are not limited to just one line 3356 01:55:44,400 --> 01:55:48,880 this is an entire block of code so maybe 3357 01:55:46,719 --> 01:55:52,000 we can do something else too 3358 01:55:48,880 --> 01:55:55,280 let's print have a nice 3359 01:55:52,000 --> 01:55:56,800 day now if i were to run this my hello 3360 01:55:55,280 --> 01:55:59,199 function is going to execute 3361 01:55:56,800 --> 01:56:00,400 this entire block of code three separate 3362 01:55:59,199 --> 01:56:03,119 times hello 3363 01:56:00,400 --> 01:56:04,000 have a nice day now an important feature 3364 01:56:03,119 --> 01:56:06,320 of functions 3365 01:56:04,000 --> 01:56:07,040 is that we can send our function some 3366 01:56:06,320 --> 01:56:08,880 information 3367 01:56:07,040 --> 01:56:10,480 and our function can do something with 3368 01:56:08,880 --> 01:56:11,920 that information that it receives 3369 01:56:10,480 --> 01:56:14,400 let's say this time i would like to 3370 01:56:11,920 --> 01:56:16,320 print hello plus somebody's name 3371 01:56:14,400 --> 01:56:17,920 currently name is an unresolved 3372 01:56:16,320 --> 01:56:19,920 reference what we can do 3373 01:56:17,920 --> 01:56:21,599 when we call this function is send our 3374 01:56:19,920 --> 01:56:23,920 function some information 3375 01:56:21,599 --> 01:56:24,719 this can be a value a variable a 3376 01:56:23,920 --> 01:56:26,560 collection 3377 01:56:24,719 --> 01:56:27,920 all sorts of different things so if you 3378 01:56:26,560 --> 01:56:28,880 need to send your function some 3379 01:56:27,920 --> 01:56:30,320 information 3380 01:56:28,880 --> 01:56:32,639 within the parentheses when you call 3381 01:56:30,320 --> 01:56:34,080 that function just list the data that 3382 01:56:32,639 --> 01:56:36,000 you want to send your function 3383 01:56:34,080 --> 01:56:37,520 so let's say i would like to send my 3384 01:56:36,000 --> 01:56:40,239 function a string 3385 01:56:37,520 --> 01:56:41,760 value of bro whatever your first name is 3386 01:56:40,239 --> 01:56:45,040 when you send information 3387 01:56:41,760 --> 01:56:46,880 to a function these are called arguments 3388 01:56:45,040 --> 01:56:48,800 they are the information that you're 3389 01:56:46,880 --> 01:56:50,880 sending to a function 3390 01:56:48,800 --> 01:56:52,239 and when you define that function you 3391 01:56:50,880 --> 01:56:55,360 need a matching 3392 01:56:52,239 --> 01:56:57,520 set of what is known as parameters 3393 01:56:55,360 --> 01:57:00,320 so our hello function is going to 3394 01:56:57,520 --> 01:57:03,360 receive one argument a string value 3395 01:57:00,320 --> 01:57:05,199 we need a matching number of parameters 3396 01:57:03,360 --> 01:57:06,719 currently this isn't going to work you 3397 01:57:05,199 --> 01:57:09,520 can see here that hello 3398 01:57:06,719 --> 01:57:10,480 takes zero positional arguments but one 3399 01:57:09,520 --> 01:57:13,360 was given 3400 01:57:10,480 --> 01:57:14,400 our function needs a matching number of 3401 01:57:13,360 --> 01:57:17,040 parameters 3402 01:57:14,400 --> 01:57:17,920 so think of it this way when we receive 3403 01:57:17,040 --> 01:57:20,400 this value 3404 01:57:17,920 --> 01:57:21,040 we're going to give a nickname to this 3405 01:57:20,400 --> 01:57:24,159 value 3406 01:57:21,040 --> 01:57:25,199 so when we receive the string value what 3407 01:57:24,159 --> 01:57:27,840 do we want to call it 3408 01:57:25,199 --> 01:57:29,280 well let's call it name so in order to 3409 01:57:27,840 --> 01:57:31,280 add parameters 3410 01:57:29,280 --> 01:57:32,719 to your function just list the 3411 01:57:31,280 --> 01:57:35,040 parameters within 3412 01:57:32,719 --> 01:57:36,639 the parentheses of that function and 3413 01:57:35,040 --> 01:57:40,000 well this should work now 3414 01:57:36,639 --> 01:57:42,080 we have a matching set of arguments and 3415 01:57:40,000 --> 01:57:44,159 parameters when we call the hello 3416 01:57:42,080 --> 01:57:46,800 function this time we are sending 3417 01:57:44,159 --> 01:57:47,440 one argument over and when our hello 3418 01:57:46,800 --> 01:57:49,360 function 3419 01:57:47,440 --> 01:57:52,239 receives this argument we're going to 3420 01:57:49,360 --> 01:57:53,760 give it a temporary nickname of 3421 01:57:52,239 --> 01:57:55,599 name or you can call this whatever you 3422 01:57:53,760 --> 01:57:58,000 want to and then we can use 3423 01:57:55,599 --> 01:57:58,960 this value for whatever we want within 3424 01:57:58,000 --> 01:58:00,880 our function 3425 01:57:58,960 --> 01:58:02,320 now if we were to run this this will 3426 01:58:00,880 --> 01:58:04,880 print hello bro 3427 01:58:02,320 --> 01:58:05,760 have a nice day now i can send maybe a 3428 01:58:04,880 --> 01:58:09,280 different value 3429 01:58:05,760 --> 01:58:11,520 let's say hello and then i will send a 3430 01:58:09,280 --> 01:58:14,719 string value of dude 3431 01:58:11,520 --> 01:58:15,599 hello dude have a nice day so this isn't 3432 01:58:14,719 --> 01:58:18,159 limited to 3433 01:58:15,599 --> 01:58:19,440 just values we can send variables to 3434 01:58:18,159 --> 01:58:23,360 let's say we have a 3435 01:58:19,440 --> 01:58:26,880 variable called name equals 3436 01:58:23,360 --> 01:58:30,719 bro and this time i'm going to send 3437 01:58:26,880 --> 01:58:34,000 this variable over hello and then send 3438 01:58:30,719 --> 01:58:37,760 name so these don't need to be the 3439 01:58:34,000 --> 01:58:40,960 exact same let's say this is 3440 01:58:37,760 --> 01:58:45,440 my name and i'm going to send 3441 01:58:40,960 --> 01:58:48,960 my name over and i will temporarily give 3442 01:58:45,440 --> 01:58:50,880 this value a nickname of just name 3443 01:58:48,960 --> 01:58:52,560 and then i can use this variable for 3444 01:58:50,880 --> 01:58:55,040 something hello bro 3445 01:58:52,560 --> 01:58:55,760 have a nice day now with arguments you 3446 01:58:55,040 --> 01:58:58,480 can send 3447 01:58:55,760 --> 01:58:59,679 more than one value over let's say this 3448 01:58:58,480 --> 01:59:02,800 time i would like to send 3449 01:58:59,679 --> 01:59:05,679 two values over so let's send pro 3450 01:59:02,800 --> 01:59:06,719 maybe for a first name and code for a 3451 01:59:05,679 --> 01:59:08,239 second name but 3452 01:59:06,719 --> 01:59:10,800 you can use your first name and last 3453 01:59:08,239 --> 01:59:13,119 name so this isn't going to work because 3454 01:59:10,800 --> 01:59:15,520 we do not have a matching set 3455 01:59:13,119 --> 01:59:16,159 of arguments and parameters we're 3456 01:59:15,520 --> 01:59:19,040 sending 3457 01:59:16,159 --> 01:59:20,159 two arguments over but our function is 3458 01:59:19,040 --> 01:59:23,199 only designed 3459 01:59:20,159 --> 01:59:26,400 to accept one argument because we have 3460 01:59:23,199 --> 01:59:30,159 one parameter so let's rename 3461 01:59:26,400 --> 01:59:32,400 name as first name and we need 3462 01:59:30,159 --> 01:59:33,280 two parameters this time so what are we 3463 01:59:32,400 --> 01:59:35,599 going to call 3464 01:59:33,280 --> 01:59:38,320 this value when we receive it let's call 3465 01:59:35,599 --> 01:59:40,960 this last name 3466 01:59:38,320 --> 01:59:42,080 so this time we are going to receive two 3467 01:59:40,960 --> 01:59:44,080 values 3468 01:59:42,080 --> 01:59:45,679 they will be nicknamed first name and 3469 01:59:44,080 --> 01:59:48,719 last name so this time 3470 01:59:45,679 --> 01:59:52,400 let's print hello first name 3471 01:59:48,719 --> 01:59:56,480 maybe i'll add a space plus 3472 01:59:52,400 --> 01:59:59,040 last name and this is now valid 3473 01:59:56,480 --> 01:59:59,760 hello bro code have a nice day with 3474 01:59:59,040 --> 02:00:02,000 arguments 3475 01:59:59,760 --> 02:00:03,040 you can mix and match the data types 3476 02:00:02,000 --> 02:00:05,840 that you're sending 3477 02:00:03,040 --> 02:00:06,239 as arguments so currently we're sending 3478 02:00:05,840 --> 02:00:08,880 two 3479 02:00:06,239 --> 02:00:10,960 string values as arguments this time 3480 02:00:08,880 --> 02:00:13,440 let's send an integer value 3481 02:00:10,960 --> 02:00:15,280 let's say we would like to send an edge 3482 02:00:13,440 --> 02:00:18,239 over to our hello function 3483 02:00:15,280 --> 02:00:20,080 so i'll just send maybe the value of 21. 3484 02:00:18,239 --> 02:00:22,639 so currently this isn't going to work 3485 02:00:20,080 --> 02:00:23,920 because the hello function takes two 3486 02:00:22,639 --> 02:00:26,159 positional arguments 3487 02:00:23,920 --> 02:00:27,840 but three were given when we call this 3488 02:00:26,159 --> 02:00:30,000 hello function we're sending 3489 02:00:27,840 --> 02:00:31,440 this argument over but our function 3490 02:00:30,000 --> 02:00:32,159 doesn't know what to do with this 3491 02:00:31,440 --> 02:00:34,880 argument 3492 02:00:32,159 --> 02:00:36,239 so we need a matching parameter for this 3493 02:00:34,880 --> 02:00:39,040 value that it receives 3494 02:00:36,239 --> 02:00:40,400 so let's call this value maybe edge as a 3495 02:00:39,040 --> 02:00:42,239 temporary nickname 3496 02:00:40,400 --> 02:00:43,920 and then we can do something with this 3497 02:00:42,239 --> 02:00:47,040 value when we receive it 3498 02:00:43,920 --> 02:00:50,880 so let's print an additional message 3499 02:00:47,040 --> 02:00:54,719 perhaps u r plus 3500 02:00:50,880 --> 02:00:57,760 age plus years 3501 02:00:54,719 --> 02:00:59,840 old so if you need to display an 3502 02:00:57,760 --> 02:01:01,920 integer or number along with the string 3503 02:00:59,840 --> 02:01:04,800 you do need to convert it to a string 3504 02:01:01,920 --> 02:01:06,239 so let's just cast our edge as a string 3505 02:01:04,800 --> 02:01:08,320 when we display this 3506 02:01:06,239 --> 02:01:09,599 so this should work now we now have a 3507 02:01:08,320 --> 02:01:13,119 matching number 3508 02:01:09,599 --> 02:01:14,000 of arguments and parameters so this will 3509 02:01:13,119 --> 02:01:16,880 display 3510 02:01:14,000 --> 02:01:17,840 hello bro code you are 21 years old have 3511 02:01:16,880 --> 02:01:20,400 a nice day 3512 02:01:17,840 --> 02:01:20,960 so ladies and gentlemen in conclusion a 3513 02:01:20,400 --> 02:01:23,520 function 3514 02:01:20,960 --> 02:01:24,880 is a block of code which is executed 3515 02:01:23,520 --> 02:01:27,520 only when it is called 3516 02:01:24,880 --> 02:01:28,239 if i were to remove this function call 3517 02:01:27,520 --> 02:01:30,320 well then 3518 02:01:28,239 --> 02:01:31,599 our program is just going to completely 3519 02:01:30,320 --> 02:01:34,400 ignore this function and 3520 02:01:31,599 --> 02:01:36,480 this block of code would not be executed 3521 02:01:34,400 --> 02:01:39,360 when we call this function we can pass 3522 02:01:36,480 --> 02:01:40,080 information known as arguments but we 3523 02:01:39,360 --> 02:01:42,880 need a 3524 02:01:40,080 --> 02:01:43,599 matching number of parameters set up to 3525 02:01:42,880 --> 02:01:46,239 receive 3526 02:01:43,599 --> 02:01:48,159 these arguments there are a few advanced 3527 02:01:46,239 --> 02:01:50,000 features of parameters but 3528 02:01:48,159 --> 02:01:51,920 i'll hopefully get to that in future 3529 02:01:50,000 --> 02:01:53,599 videos we'll just stick with the basics 3530 02:01:51,920 --> 02:01:55,920 for now of functions 3531 02:01:53,599 --> 02:01:57,520 so that's the basics of functions if you 3532 02:01:55,920 --> 02:01:59,280 would like a copy of this code 3533 02:01:57,520 --> 02:02:01,679 i will post this in the comments down 3534 02:01:59,280 --> 02:02:04,639 below but yeah that is how functions 3535 02:02:01,679 --> 02:02:06,560 work in python hey what's going on 3536 02:02:04,639 --> 02:02:06,880 everybody it's you bro hope you're doing 3537 02:02:06,560 --> 02:02:08,480 well 3538 02:02:06,880 --> 02:02:10,400 and in this video i'm going to explain 3539 02:02:08,480 --> 02:02:15,119 the return statement in python so 3540 02:02:10,400 --> 02:02:15,119 sit back relax and enjoy the show 3541 02:02:15,520 --> 02:02:19,920 all right people let's begin the return 3542 02:02:18,000 --> 02:02:23,119 statement is used within functions 3543 02:02:19,920 --> 02:02:23,920 to send python values or objects back to 3544 02:02:23,119 --> 02:02:26,320 the caller 3545 02:02:23,920 --> 02:02:26,960 these values or objects are known as the 3546 02:02:26,320 --> 02:02:29,040 function's 3547 02:02:26,960 --> 02:02:30,239 return value so do you know how with 3548 02:02:29,040 --> 02:02:32,560 functions we can pass 3549 02:02:30,239 --> 02:02:34,000 arguments to a function and then our 3550 02:02:32,560 --> 02:02:34,800 function can do something with those 3551 02:02:34,000 --> 02:02:36,880 arguments 3552 02:02:34,800 --> 02:02:38,639 well not only that but our function can 3553 02:02:36,880 --> 02:02:41,040 pass some value or 3554 02:02:38,639 --> 02:02:43,119 object back to the caller so what we'll 3555 02:02:41,040 --> 02:02:45,440 do for this example is create 3556 02:02:43,119 --> 02:02:46,480 a function that will simply multiply two 3557 02:02:45,440 --> 02:02:49,119 numbers together 3558 02:02:46,480 --> 02:02:50,400 and return the result back to the caller 3559 02:02:49,119 --> 02:02:52,639 so let's define 3560 02:02:50,400 --> 02:02:54,560 a function called multiply and we'll set 3561 02:02:52,639 --> 02:02:58,239 up two parameters for this function 3562 02:02:54,560 --> 02:03:01,760 let's say number one and number two 3563 02:02:58,239 --> 02:03:05,679 and what we'll do is say result 3564 02:03:01,760 --> 02:03:07,199 equals number one times number two 3565 02:03:05,679 --> 02:03:08,960 and then at the end we can return 3566 02:03:07,199 --> 02:03:11,199 something what do we want to return 3567 02:03:08,960 --> 02:03:13,119 let's return whatever the result is now 3568 02:03:11,199 --> 02:03:16,080 when we call this function we will pass 3569 02:03:13,119 --> 02:03:17,199 in two values as arguments let's say we 3570 02:03:16,080 --> 02:03:20,320 would like to multiply 3571 02:03:17,199 --> 02:03:22,239 six times eight so when we run this well 3572 02:03:20,320 --> 02:03:24,000 we can't actually see anything 3573 02:03:22,239 --> 02:03:25,360 when we return the result after 3574 02:03:24,000 --> 02:03:27,599 multiplying numbers 3575 02:03:25,360 --> 02:03:29,520 one and two it's going to return the 3576 02:03:27,599 --> 02:03:31,280 result back to the caller 3577 02:03:29,520 --> 02:03:32,880 in order to see the value that is 3578 02:03:31,280 --> 02:03:35,040 returned back to the caller 3579 02:03:32,880 --> 02:03:35,920 one way in which we can do this is to 3580 02:03:35,040 --> 02:03:38,000 print 3581 02:03:35,920 --> 02:03:39,040 the value that is returned within a 3582 02:03:38,000 --> 02:03:41,520 print statement 3583 02:03:39,040 --> 02:03:44,000 so what we'll do is print the value that 3584 02:03:41,520 --> 02:03:44,800 is returned after calling the multiply 3585 02:03:44,000 --> 02:03:47,920 function 3586 02:03:44,800 --> 02:03:48,960 so 6 times 8 is really great 6 times 8 3587 02:03:47,920 --> 02:03:51,280 is 48 3588 02:03:48,960 --> 02:03:53,599 now one other thing that we can do is 3589 02:03:51,280 --> 02:03:54,480 store the returned value within a 3590 02:03:53,599 --> 02:03:58,159 variable 3591 02:03:54,480 --> 02:04:00,320 so let's say x equals multiply 3592 02:03:58,159 --> 02:04:02,400 6 times 8 and then we will print 3593 02:04:00,320 --> 02:04:05,599 whatever x is 3594 02:04:02,400 --> 02:04:07,599 so the result is still the same of 48 3595 02:04:05,599 --> 02:04:09,520 now with the return value there is 3596 02:04:07,599 --> 02:04:10,239 another way in which we could write this 3597 02:04:09,520 --> 02:04:12,159 that uses 3598 02:04:10,239 --> 02:04:14,560 less lines of code so we'll delete this 3599 02:04:12,159 --> 02:04:16,800 first line and instead of returning 3600 02:04:14,560 --> 02:04:18,159 a variable called result what we'll do 3601 02:04:16,800 --> 02:04:20,960 is just return 3602 02:04:18,159 --> 02:04:23,119 number one times number two and this 3603 02:04:20,960 --> 02:04:25,280 would work the same as it does before 3604 02:04:23,119 --> 02:04:26,880 but it's written in less lines of code 3605 02:04:25,280 --> 02:04:27,840 so that's another way in which you could 3606 02:04:26,880 --> 02:04:29,360 write this 3607 02:04:27,840 --> 02:04:31,520 all right everybody so that is the 3608 02:04:29,360 --> 02:04:34,159 return statement functions can send 3609 02:04:31,520 --> 02:04:34,960 python values or objects back to the 3610 02:04:34,159 --> 02:04:36,880 caller 3611 02:04:34,960 --> 02:04:38,000 these values or objects are known as the 3612 02:04:36,880 --> 02:04:40,400 functions return 3613 02:04:38,000 --> 02:04:42,400 value you can either print this value 3614 02:04:40,400 --> 02:04:44,960 directly to the console window 3615 02:04:42,400 --> 02:04:46,159 or you can store it within a variable or 3616 02:04:44,960 --> 02:04:48,159 some other location 3617 02:04:46,159 --> 02:04:51,360 and well ladies and gentlemen that is 3618 02:04:48,159 --> 02:04:53,920 how the return statement works in python 3619 02:04:51,360 --> 02:04:54,719 what's going on everybody it's bro hope 3620 02:04:53,920 --> 02:04:56,239 you're doing well 3621 02:04:54,719 --> 02:04:58,239 and in this video i'm going to explain 3622 02:04:56,239 --> 02:05:02,800 keyword arguments in python so 3623 02:04:58,239 --> 02:05:02,800 sit back relax and enjoy the show 3624 02:05:02,960 --> 02:05:06,400 all right everybody let's discuss 3625 02:05:05,199 --> 02:05:09,280 keyword arguments 3626 02:05:06,400 --> 02:05:11,599 these are arguments that are preceded by 3627 02:05:09,280 --> 02:05:12,480 an identifier when we pass them to a 3628 02:05:11,599 --> 02:05:14,320 function 3629 02:05:12,480 --> 02:05:16,560 the order of the arguments doesn't 3630 02:05:14,320 --> 02:05:18,400 matter unlike positional arguments 3631 02:05:16,560 --> 02:05:20,320 which do matter and we've been working 3632 02:05:18,400 --> 02:05:21,920 with positional arguments already 3633 02:05:20,320 --> 02:05:24,159 and python knows the names of the 3634 02:05:21,920 --> 02:05:26,639 arguments that our function receives 3635 02:05:24,159 --> 02:05:27,520 when we use keyword arguments here's an 3636 02:05:26,639 --> 02:05:29,199 example of us 3637 02:05:27,520 --> 02:05:30,800 using positional arguments and then i'll 3638 02:05:29,199 --> 02:05:32,960 demonstrate the benefits of 3639 02:05:30,800 --> 02:05:34,239 keyword arguments let's say we have a 3640 02:05:32,960 --> 02:05:36,560 function called hello 3641 02:05:34,239 --> 02:05:38,320 that accepts three arguments a first 3642 02:05:36,560 --> 02:05:40,480 name a middle name and a last name 3643 02:05:38,320 --> 02:05:41,840 and the output will be hello first name 3644 02:05:40,480 --> 02:05:43,440 middle name last name 3645 02:05:41,840 --> 02:05:45,360 so if i were to run this this will 3646 02:05:43,440 --> 02:05:47,840 display hello bro 3647 02:05:45,360 --> 02:05:48,560 dude code so we're using positional 3648 02:05:47,840 --> 02:05:50,719 arguments 3649 02:05:48,560 --> 02:05:52,000 the order of the arguments does matter 3650 02:05:50,719 --> 02:05:54,079 what do you think's gonna happen if i 3651 02:05:52,000 --> 02:05:56,639 were to replace some of these values 3652 02:05:54,079 --> 02:05:57,840 let's say i will swap the first argument 3653 02:05:56,639 --> 02:06:01,040 with the 3654 02:05:57,840 --> 02:06:04,320 third argument so let's try it again 3655 02:06:01,040 --> 02:06:05,840 hello code dude bro so obviously the 3656 02:06:04,320 --> 02:06:07,920 order of the arguments matters 3657 02:06:05,840 --> 02:06:09,440 and now if we were to use keyword 3658 02:06:07,920 --> 02:06:10,320 arguments then the order of the 3659 02:06:09,440 --> 02:06:12,800 arguments 3660 02:06:10,320 --> 02:06:15,040 doesn't matter but with each argument we 3661 02:06:12,800 --> 02:06:18,159 need to precede each argument with 3662 02:06:15,040 --> 02:06:20,239 a unique identifier and that identifier 3663 02:06:18,159 --> 02:06:22,239 is the name of the parameter we want to 3664 02:06:20,239 --> 02:06:25,040 associate each argument with 3665 02:06:22,239 --> 02:06:26,400 so for my intended last name i will use 3666 02:06:25,040 --> 02:06:29,520 an identifier of 3667 02:06:26,400 --> 02:06:32,400 last to match this parameter so 3668 02:06:29,520 --> 02:06:33,119 dude will be middle middle equals dude 3669 02:06:32,400 --> 02:06:35,040 and bro 3670 02:06:33,119 --> 02:06:36,880 will be first even though they're not in 3671 02:06:35,040 --> 02:06:37,440 the correct order so let's try this 3672 02:06:36,880 --> 02:06:40,880 again 3673 02:06:37,440 --> 02:06:43,119 and my output is hello bro dude code 3674 02:06:40,880 --> 02:06:44,239 so that's all about keyword arguments 3675 02:06:43,119 --> 02:06:46,719 they are arguments 3676 02:06:44,239 --> 02:06:48,000 preceded with an identifier when we pass 3677 02:06:46,719 --> 02:06:49,679 them to a function 3678 02:06:48,000 --> 02:06:51,520 the order of the arguments doesn't 3679 02:06:49,679 --> 02:06:53,599 matter unlike positional arguments 3680 02:06:51,520 --> 02:06:54,800 which do matter and then python knows 3681 02:06:53,599 --> 02:06:56,800 the names of the arguments 3682 02:06:54,800 --> 02:06:58,079 that our function receives when we use 3683 02:06:56,800 --> 02:07:00,239 keyword arguments 3684 02:06:58,079 --> 02:07:02,079 so that's it for keyword arguments if 3685 02:07:00,239 --> 02:07:03,920 you would like a copy of this code 3686 02:07:02,079 --> 02:07:06,159 i will post this in the comment section 3687 02:07:03,920 --> 02:07:06,800 down below but yeah that's how keyword 3688 02:07:06,159 --> 02:07:10,239 arguments 3689 02:07:06,800 --> 02:07:12,079 work in python hey what's going on 3690 02:07:10,239 --> 02:07:13,520 everybody it's you bro hope you're doing 3691 02:07:12,079 --> 02:07:14,159 well and in this video i'm going to 3692 02:07:13,520 --> 02:07:16,719 explain 3693 02:07:14,159 --> 02:07:17,280 nested function calls in python so sit 3694 02:07:16,719 --> 02:07:21,199 back 3695 02:07:17,280 --> 02:07:21,199 relax and enjoy the show 3696 02:07:21,520 --> 02:07:25,520 all right people nested function calls 3697 02:07:24,000 --> 02:07:27,920 these are function calls 3698 02:07:25,520 --> 02:07:29,119 inside of other function calls and this 3699 02:07:27,920 --> 02:07:31,520 is possible because 3700 02:07:29,119 --> 02:07:33,679 certain functions will return a value 3701 02:07:31,520 --> 02:07:35,920 and we can immediately use that value as 3702 02:07:33,679 --> 02:07:37,760 an argument for the next function 3703 02:07:35,920 --> 02:07:39,440 so here's an example let's say that we 3704 02:07:37,760 --> 02:07:41,679 have the small program 3705 02:07:39,440 --> 02:07:43,199 where we will ask a user to type in a 3706 02:07:41,679 --> 02:07:45,119 whole positive number 3707 02:07:43,199 --> 02:07:47,599 and let's say i don't let's say i type 3708 02:07:45,119 --> 02:07:50,000 in negative 3.14 3709 02:07:47,599 --> 02:07:50,639 and then i'm storing that value within a 3710 02:07:50,000 --> 02:07:52,719 variable 3711 02:07:50,639 --> 02:07:54,320 named num what i'm going to do is 3712 02:07:52,719 --> 02:07:55,920 convert that number 3713 02:07:54,320 --> 02:07:57,360 to a floating point number because 3714 02:07:55,920 --> 02:07:58,400 currently it's a string when you accept 3715 02:07:57,360 --> 02:08:01,280 user input 3716 02:07:58,400 --> 02:08:03,119 i will find the absolute value of num 3717 02:08:01,280 --> 02:08:03,599 and then round it to the nearest whole 3718 02:08:03,119 --> 02:08:05,440 number 3719 02:08:03,599 --> 02:08:07,199 and then print the value so we can 3720 02:08:05,440 --> 02:08:09,360 actually do this using less lines of 3721 02:08:07,199 --> 02:08:11,440 code with nested function calls 3722 02:08:09,360 --> 02:08:13,520 so let's start with the first function 3723 02:08:11,440 --> 02:08:16,159 which will be accepting some user input 3724 02:08:13,520 --> 02:08:17,199 the input function so this will be in 3725 02:08:16,159 --> 02:08:19,760 the very center 3726 02:08:17,199 --> 02:08:21,760 of our nested function calls and then 3727 02:08:19,760 --> 02:08:22,239 the next function we would like to apply 3728 02:08:21,760 --> 02:08:24,480 to 3729 02:08:22,239 --> 02:08:25,679 the variable that is returned the value 3730 02:08:24,480 --> 02:08:27,520 that is returned 3731 02:08:25,679 --> 02:08:29,360 we will pass in as an argument to our 3732 02:08:27,520 --> 02:08:31,760 next function of casting 3733 02:08:29,360 --> 02:08:32,400 this data type to a float so we'll 3734 02:08:31,760 --> 02:08:34,880 surround 3735 02:08:32,400 --> 02:08:35,599 our function with another function and 3736 02:08:34,880 --> 02:08:38,079 use 3737 02:08:35,599 --> 02:08:39,440 the float function and then we'll just 3738 02:08:38,079 --> 02:08:42,079 keep on adding layers to this 3739 02:08:39,440 --> 02:08:42,800 so we have the absolute value function 3740 02:08:42,079 --> 02:08:45,520 next 3741 02:08:42,800 --> 02:08:47,280 so we'll add another layer and we have 3742 02:08:45,520 --> 02:08:50,560 the round function 3743 02:08:47,280 --> 02:08:52,400 so we'll add a layer to that and 3744 02:08:50,560 --> 02:08:55,119 lastly we're going to print and that'll 3745 02:08:52,400 --> 02:08:57,280 be the outermost layer 3746 02:08:55,119 --> 02:08:58,320 so this program will do the exact same 3747 02:08:57,280 --> 02:09:01,679 thing 3748 02:08:58,320 --> 02:09:02,639 let's say negative 3.14 except we've 3749 02:09:01,679 --> 02:09:05,520 written this with 3750 02:09:02,639 --> 02:09:07,520 one line of code so basically we start 3751 02:09:05,520 --> 02:09:08,480 with the innermost function and resolve 3752 02:09:07,520 --> 02:09:10,960 that first 3753 02:09:08,480 --> 02:09:13,520 whatever value is returned we use as an 3754 02:09:10,960 --> 02:09:15,679 argument to the next outermost function 3755 02:09:13,520 --> 02:09:18,480 and in this case it would be float then 3756 02:09:15,679 --> 02:09:20,480 we resolve that then move on to absolute 3757 02:09:18,480 --> 02:09:22,719 value then round and then print 3758 02:09:20,480 --> 02:09:23,599 so it's just another way of writing code 3759 02:09:22,719 --> 02:09:25,440 that takes up less 3760 02:09:23,599 --> 02:09:27,280 lines but this is more of an extreme 3761 02:09:25,440 --> 02:09:29,840 example you probably won't see 3762 02:09:27,280 --> 02:09:31,760 like four or five nested functions well 3763 02:09:29,840 --> 02:09:32,239 everybody those are nested function 3764 02:09:31,760 --> 02:09:34,000 calls 3765 02:09:32,239 --> 02:09:35,599 if you would like a copy of this code i 3766 02:09:34,000 --> 02:09:36,800 will post all of this to the comment 3767 02:09:35,599 --> 02:09:39,199 section down below 3768 02:09:36,800 --> 02:09:40,719 but yeah those are nested function calls 3769 02:09:39,199 --> 02:09:42,560 in python 3770 02:09:40,719 --> 02:09:44,159 hey what's going on people it's your bro 3771 02:09:42,560 --> 02:09:46,159 hope you're doing well and in this video 3772 02:09:44,159 --> 02:09:47,760 i'm going to show you how variable scope 3773 02:09:46,159 --> 02:09:52,079 works in python so 3774 02:09:47,760 --> 02:09:52,079 sit back relax and enjoy the show 3775 02:09:52,159 --> 02:09:56,560 all right let's do a thing people let's 3776 02:09:54,400 --> 02:09:58,639 discuss the scope of a variable 3777 02:09:56,560 --> 02:10:00,400 now the scope of a variable is the 3778 02:09:58,639 --> 02:10:02,960 region that a variable 3779 02:10:00,400 --> 02:10:05,040 is recognized a variable is only 3780 02:10:02,960 --> 02:10:06,960 available from inside the region 3781 02:10:05,040 --> 02:10:08,800 that it is created let's say that we 3782 02:10:06,960 --> 02:10:10,880 have a function and i just gave this 3783 02:10:08,800 --> 02:10:12,960 function a name of display name 3784 02:10:10,880 --> 02:10:14,400 i created a variable inside of this 3785 02:10:12,960 --> 02:10:16,480 function named name 3786 02:10:14,400 --> 02:10:17,599 and you can assign this a value of 3787 02:10:16,480 --> 02:10:19,679 whatever your name is 3788 02:10:17,599 --> 02:10:21,520 preferably a last name for this example 3789 02:10:19,679 --> 02:10:24,400 and what i'll do is just print 3790 02:10:21,520 --> 02:10:25,119 whatever my name is so this variable is 3791 02:10:24,400 --> 02:10:28,079 known to have 3792 02:10:25,119 --> 02:10:29,599 a local scope because it's declared 3793 02:10:28,079 --> 02:10:32,400 inside of a function 3794 02:10:29,599 --> 02:10:33,520 and a variable is only recognized inside 3795 02:10:32,400 --> 02:10:36,079 of the region 3796 02:10:33,520 --> 02:10:37,679 that it is created this variable of name 3797 02:10:36,079 --> 02:10:40,560 since it's created inside 3798 02:10:37,679 --> 02:10:41,280 of this function it is known as a local 3799 02:10:40,560 --> 02:10:44,079 variable 3800 02:10:41,280 --> 02:10:44,800 that has a local scope it's only 3801 02:10:44,079 --> 02:10:46,560 available 3802 02:10:44,800 --> 02:10:49,119 inside of this function that it's 3803 02:10:46,560 --> 02:10:50,000 created now let's attempt to access this 3804 02:10:49,119 --> 02:10:52,320 variable from 3805 02:10:50,000 --> 02:10:53,280 outside of our function so i'm going to 3806 02:10:52,320 --> 02:10:55,679 print my 3807 02:10:53,280 --> 02:10:56,480 name variable but we'll have an issue 3808 02:10:55,679 --> 02:10:59,840 though 3809 02:10:56,480 --> 02:11:02,480 so name name is not defined so 3810 02:10:59,840 --> 02:11:03,040 local variables are declared inside of a 3811 02:11:02,480 --> 02:11:05,760 function 3812 02:11:03,040 --> 02:11:06,159 and they have a local scope they are 3813 02:11:05,760 --> 02:11:07,920 only 3814 02:11:06,159 --> 02:11:09,360 available inside of this function in 3815 02:11:07,920 --> 02:11:12,320 which they're created 3816 02:11:09,360 --> 02:11:14,079 on the other hand a global variable is a 3817 02:11:12,320 --> 02:11:16,320 variable that is declared 3818 02:11:14,079 --> 02:11:17,920 outside of any function but within your 3819 02:11:16,320 --> 02:11:20,239 module that you're working with 3820 02:11:17,920 --> 02:11:21,280 a global variable is known to have a 3821 02:11:20,239 --> 02:11:23,760 global scope 3822 02:11:21,280 --> 02:11:24,880 they are available inside and outside of 3823 02:11:23,760 --> 02:11:27,599 any functions 3824 02:11:24,880 --> 02:11:29,119 now if i was to print my name variable 3825 02:11:27,599 --> 02:11:31,280 this is going to print 3826 02:11:29,119 --> 02:11:33,199 bro or whatever your first name is and 3827 02:11:31,280 --> 02:11:35,920 it is entirely possible to have both 3828 02:11:33,199 --> 02:11:37,199 a global and local version of the same 3829 02:11:35,920 --> 02:11:39,760 named variable 3830 02:11:37,199 --> 02:11:42,239 so we have both a global version of name 3831 02:11:39,760 --> 02:11:45,040 as well as a local version of name 3832 02:11:42,239 --> 02:11:46,400 if i was to print my name variable from 3833 02:11:45,040 --> 02:11:48,960 outside of this function 3834 02:11:46,400 --> 02:11:50,639 it's going to print my global version 3835 02:11:48,960 --> 02:11:52,719 however if i print 3836 02:11:50,639 --> 02:11:54,159 my version of name inside of that 3837 02:11:52,719 --> 02:11:56,079 function in which it's created 3838 02:11:54,159 --> 02:11:57,599 we're going to first use the local 3839 02:11:56,079 --> 02:11:59,920 version before the 3840 02:11:57,599 --> 02:12:01,520 global version of name and let's try 3841 02:11:59,920 --> 02:12:04,960 that so i'm going to call 3842 02:12:01,520 --> 02:12:08,239 my display name function 3843 02:12:04,960 --> 02:12:11,040 and this will print my last name 3844 02:12:08,239 --> 02:12:12,800 code and then my first name of bro so it 3845 02:12:11,040 --> 02:12:15,119 is possible to have a global 3846 02:12:12,800 --> 02:12:16,159 and local version of the same named 3847 02:12:15,119 --> 02:12:18,239 variable 3848 02:12:16,159 --> 02:12:20,079 now if i was to remove this line where 3849 02:12:18,239 --> 02:12:22,239 we declare a local version 3850 02:12:20,079 --> 02:12:24,079 of name well then the next best 3851 02:12:22,239 --> 02:12:26,800 available name to us would be 3852 02:12:24,079 --> 02:12:28,719 the global version of name so if you use 3853 02:12:26,800 --> 02:12:31,440 a variable inside of a function 3854 02:12:28,719 --> 02:12:32,079 you'll first use a local version of that 3855 02:12:31,440 --> 02:12:34,719 variable 3856 02:12:32,079 --> 02:12:36,079 if it's available if not you'll use a 3857 02:12:34,719 --> 02:12:38,079 global version instead 3858 02:12:36,079 --> 02:12:40,000 and python will follow this rule known 3859 02:12:38,079 --> 02:12:42,880 as the legb 3860 02:12:40,000 --> 02:12:43,199 rule you use any local variables first 3861 02:12:42,880 --> 02:12:45,840 then 3862 02:12:43,199 --> 02:12:48,320 enclosed variables then global and then 3863 02:12:45,840 --> 02:12:50,400 any built-in variables in that order 3864 02:12:48,320 --> 02:12:51,520 so that's the main difference between 3865 02:12:50,400 --> 02:12:54,320 local scope and 3866 02:12:51,520 --> 02:12:56,400 global scope the scope of a variable is 3867 02:12:54,320 --> 02:12:58,159 the region in which it's available 3868 02:12:56,400 --> 02:13:00,159 and it's limited to the region in which 3869 02:12:58,159 --> 02:13:02,480 it's created a local scope 3870 02:13:00,159 --> 02:13:04,480 is available only inside of the function 3871 02:13:02,480 --> 02:13:07,280 in which that variable is created 3872 02:13:04,480 --> 02:13:08,639 a global scoped variable is a variable 3873 02:13:07,280 --> 02:13:10,960 that is available 3874 02:13:08,639 --> 02:13:12,719 inside and outside of any functions but 3875 02:13:10,960 --> 02:13:13,119 within your module that you're working 3876 02:13:12,719 --> 02:13:15,360 with 3877 02:13:13,119 --> 02:13:17,040 so that's variable scope in python if 3878 02:13:15,360 --> 02:13:18,639 you would like a copy of this code i 3879 02:13:17,040 --> 02:13:19,840 will post this code to the comment 3880 02:13:18,639 --> 02:13:22,320 section down below 3881 02:13:19,840 --> 02:13:24,480 and well yeah that's how variable scope 3882 02:13:22,320 --> 02:13:26,560 works in python 3883 02:13:24,480 --> 02:13:27,520 hey what's going on everybody it's bro 3884 02:13:26,560 --> 02:13:29,119 hope you're doing well 3885 02:13:27,520 --> 02:13:32,239 and in this video i'm going to explain 3886 02:13:29,119 --> 02:13:36,079 the arcs parameter in python so sit back 3887 02:13:32,239 --> 02:13:38,560 relax and enjoy the show 3888 02:13:36,079 --> 02:13:39,440 all right guys and gals let's talk about 3889 02:13:38,560 --> 02:13:42,000 args 3890 02:13:39,440 --> 02:13:44,079 now args is a parameter that will pack 3891 02:13:42,000 --> 02:13:45,920 all arguments into a tuple 3892 02:13:44,079 --> 02:13:48,400 it's useful so that a function can 3893 02:13:45,920 --> 02:13:50,000 accept a varying amount of arguments 3894 02:13:48,400 --> 02:13:52,079 here's an example of why it would be 3895 02:13:50,000 --> 02:13:54,159 useful let's say we have a function 3896 02:13:52,079 --> 02:13:55,199 called add that will accept two numbers 3897 02:13:54,159 --> 02:13:57,440 as arguments 3898 02:13:55,199 --> 02:13:59,440 and add them together and return the sum 3899 02:13:57,440 --> 02:14:00,320 so this works as intended right as long 3900 02:13:59,440 --> 02:14:02,239 as we pass in 3901 02:14:00,320 --> 02:14:04,639 only two numbers but what if we need to 3902 02:14:02,239 --> 02:14:06,320 pass in three numbers as arguments 3903 02:14:04,639 --> 02:14:08,079 well we can no longer use this add 3904 02:14:06,320 --> 02:14:08,639 function because we have two parameters 3905 02:14:08,079 --> 02:14:10,960 set up 3906 02:14:08,639 --> 02:14:12,639 but we're passing in three arguments you 3907 02:14:10,960 --> 02:14:14,239 can see here within my console window 3908 02:14:12,639 --> 02:14:16,639 that we have a type error 3909 02:14:14,239 --> 02:14:17,679 add takes two positional arguments but 3910 02:14:16,639 --> 02:14:19,440 three were given 3911 02:14:17,679 --> 02:14:20,800 and one way in which we can solve this 3912 02:14:19,440 --> 02:14:22,719 issue is to use this 3913 02:14:20,800 --> 02:14:24,560 args parameter so what we're going to do 3914 02:14:22,719 --> 02:14:27,599 is replace all of our parameters 3915 02:14:24,560 --> 02:14:30,000 with asterisk args and actually 3916 02:14:27,599 --> 02:14:31,119 the args portion isn't as important as 3917 02:14:30,000 --> 02:14:32,800 the asterisk 3918 02:14:31,119 --> 02:14:34,239 args is short for arguments and you can 3919 02:14:32,800 --> 02:14:36,239 really name this whatever you want just 3920 02:14:34,239 --> 02:14:38,239 be sure that you have this asterisk 3921 02:14:36,239 --> 02:14:39,440 because with this asterisk we're doing a 3922 02:14:38,239 --> 02:14:41,520 form of packing 3923 02:14:39,440 --> 02:14:43,199 what we're doing is passing all of these 3924 02:14:41,520 --> 02:14:45,520 arguments and packing them 3925 02:14:43,199 --> 02:14:47,920 into a tuple so let's rewrite this 3926 02:14:45,520 --> 02:14:51,199 program so let's say maybe sum 3927 02:14:47,920 --> 02:14:53,199 equals zero and then we will need to 3928 02:14:51,199 --> 02:14:54,079 iterate through all of the items within 3929 02:14:53,199 --> 02:14:57,599 our tuple because 3930 02:14:54,079 --> 02:15:00,719 tuples are iterable so let's say for i 3931 02:14:57,599 --> 02:15:03,360 in args what we'll do is say sum 3932 02:15:00,719 --> 02:15:05,040 plus equals whatever i is and at the end 3933 02:15:03,360 --> 02:15:06,000 when we escape the for loop we will 3934 02:15:05,040 --> 02:15:08,239 return 3935 02:15:06,000 --> 02:15:09,360 our sum so let's try it again and see if 3936 02:15:08,239 --> 02:15:11,199 it works 3937 02:15:09,360 --> 02:15:13,199 well would you look at that it works 3938 02:15:11,199 --> 02:15:14,079 let's pass in a few other arguments for 3939 02:15:13,199 --> 02:15:16,320 good measure 3940 02:15:14,079 --> 02:15:17,520 let's pass in six arguments this time 3941 02:15:16,320 --> 02:15:19,520 all right look at that 3942 02:15:17,520 --> 02:15:20,960 this does in fact work so you can 3943 02:15:19,520 --> 02:15:23,119 actually name this whatever you want the 3944 02:15:20,960 --> 02:15:25,119 important thing is to have this asterisk 3945 02:15:23,119 --> 02:15:26,400 so let's rename this as i don't know 3946 02:15:25,119 --> 02:15:29,440 stuff 3947 02:15:26,400 --> 02:15:32,320 and let's try this again yep 3948 02:15:29,440 --> 02:15:32,880 it still works with tuples they are 3949 02:15:32,320 --> 02:15:34,960 ordered 3950 02:15:32,880 --> 02:15:36,480 and unchangeable let's attempt to edit 3951 02:15:34,960 --> 02:15:39,599 one of these values 3952 02:15:36,480 --> 02:15:42,960 so let's say stuff at 3953 02:15:39,599 --> 02:15:45,920 index zero is equal to 3954 02:15:42,960 --> 02:15:47,679 zero and let's try and change it so you 3955 02:15:45,920 --> 02:15:48,159 can see that tuple object does not 3956 02:15:47,679 --> 02:15:50,800 support 3957 02:15:48,159 --> 02:15:51,840 item assignment if you need to change 3958 02:15:50,800 --> 02:15:54,239 one of the values 3959 02:15:51,840 --> 02:15:55,119 within your tuple after you pack all of 3960 02:15:54,239 --> 02:15:56,800 these arguments 3961 02:15:55,119 --> 02:15:58,639 you might need to convert this to a 3962 02:15:56,800 --> 02:16:00,400 different collection one way in which we 3963 02:15:58,639 --> 02:16:03,840 can do that is to cast it 3964 02:16:00,400 --> 02:16:06,880 let's say stuff equals and we'll cast 3965 02:16:03,840 --> 02:16:09,199 our tuple as a list because a list 3966 02:16:06,880 --> 02:16:11,280 can be changeable it's mutable so let's 3967 02:16:09,199 --> 02:16:13,679 attempt this again we'll cast 3968 02:16:11,280 --> 02:16:14,320 our tuple as a list and then edit one of 3969 02:16:13,679 --> 02:16:16,560 the values 3970 02:16:14,320 --> 02:16:18,079 and then add all the values together so 3971 02:16:16,560 --> 02:16:20,159 to change one of the values 3972 02:16:18,079 --> 02:16:22,480 say the name your collection stuff in 3973 02:16:20,159 --> 02:16:25,119 this case let's say at index zero 3974 02:16:22,480 --> 02:16:26,880 this now equals zero so now if we were 3975 02:16:25,119 --> 02:16:29,920 to add all these items together 3976 02:16:26,880 --> 02:16:32,559 they should equal 20. there we go so 3977 02:16:29,920 --> 02:16:34,479 yeah that's basically the args parameter 3978 02:16:32,559 --> 02:16:36,160 the name isn't as important as the 3979 02:16:34,479 --> 02:16:37,120 asterisk because you can really name it 3980 02:16:36,160 --> 02:16:39,359 whatever you want 3981 02:16:37,120 --> 02:16:41,679 a common convention is to just name this 3982 02:16:39,359 --> 02:16:44,399 args it's a parameter that will pack 3983 02:16:41,679 --> 02:16:46,000 all arguments into a tuple it's useful 3984 02:16:44,399 --> 02:16:48,559 so that a function can accept 3985 02:16:46,000 --> 02:16:50,000 a varying amount of arguments well that 3986 02:16:48,559 --> 02:16:51,920 is all about args 3987 02:16:50,000 --> 02:16:53,599 if you would like a copy of this code i 3988 02:16:51,920 --> 02:16:54,240 will post this in the comments section 3989 02:16:53,599 --> 02:16:56,639 down below 3990 02:16:54,240 --> 02:16:58,719 but yeah that is how the args parameter 3991 02:16:56,639 --> 02:17:00,880 works in python 3992 02:16:58,719 --> 02:17:02,319 hey what's going on everybody it's bro 3993 02:17:00,880 --> 02:17:04,080 hope you're doing well and in this video 3994 02:17:02,319 --> 02:17:07,120 i'm going to be explaining quarks 3995 02:17:04,080 --> 02:17:10,000 in python so sit back relax 3996 02:17:07,120 --> 02:17:10,000 and enjoy the show 3997 02:17:10,319 --> 02:17:14,719 all right everybody let's talk about 3998 02:17:12,000 --> 02:17:15,120 quarkx now this is a parameter that will 3999 02:17:14,719 --> 02:17:17,760 pack 4000 02:17:15,120 --> 02:17:20,160 all arguments into a dictionary it's 4001 02:17:17,760 --> 02:17:22,960 useful so that a function can accept a 4002 02:17:20,160 --> 02:17:23,760 varying amount of keyword arguments it's 4003 02:17:22,960 --> 02:17:26,479 identical 4004 02:17:23,760 --> 02:17:28,719 to args except with args args will 4005 02:17:26,479 --> 02:17:29,439 accept a varying amount of positional 4006 02:17:28,719 --> 02:17:31,840 arguments 4007 02:17:29,439 --> 02:17:33,679 and pack them into a tuple with quarks 4008 02:17:31,840 --> 02:17:34,800 this will accept a varying amount of 4009 02:17:33,679 --> 02:17:36,719 keyword arguments 4010 02:17:34,800 --> 02:17:38,240 and pack them into a dictionary so 4011 02:17:36,719 --> 02:17:38,960 here's an example of why this would be 4012 02:17:38,240 --> 02:17:40,719 useful 4013 02:17:38,960 --> 02:17:43,200 let's say we have a function called 4014 02:17:40,719 --> 02:17:44,080 hello that accepts two keyword arguments 4015 02:17:43,200 --> 02:17:46,160 a first name 4016 02:17:44,080 --> 02:17:47,120 and a last name all this will do is 4017 02:17:46,160 --> 02:17:49,439 output hello 4018 02:17:47,120 --> 02:17:51,200 your first name and last name but what 4019 02:17:49,439 --> 02:17:53,120 if somebody has more than two names 4020 02:17:51,200 --> 02:17:54,719 let's say somebody has a middle name 4021 02:17:53,120 --> 02:17:56,559 and for this example i'll just say my 4022 02:17:54,719 --> 02:17:59,280 middle name is dude 4023 02:17:56,559 --> 02:18:01,120 so if i were to run this well this hello 4024 02:17:59,280 --> 02:18:03,040 function is no longer going to work 4025 02:18:01,120 --> 02:18:04,399 as you can see in the console window we 4026 02:18:03,040 --> 02:18:06,559 have a type error 4027 02:18:04,399 --> 02:18:08,160 the hello function got an unexpected 4028 02:18:06,559 --> 02:18:10,080 keyword argument middle 4029 02:18:08,160 --> 02:18:11,760 one way in which we can solve this we 4030 02:18:10,080 --> 02:18:14,559 can use this quark's 4031 02:18:11,760 --> 02:18:15,120 parameter so we can accept a varying 4032 02:18:14,559 --> 02:18:17,280 amount 4033 02:18:15,120 --> 02:18:18,319 of keyword arguments so what we'll do is 4034 02:18:17,280 --> 02:18:21,359 replace 4035 02:18:18,319 --> 02:18:23,760 our parameters with two asterisks then 4036 02:18:21,359 --> 02:18:25,519 quarks now if i would like the same 4037 02:18:23,760 --> 02:18:26,880 output as i had before i'm going to need 4038 02:18:25,519 --> 02:18:29,040 to change a few things around 4039 02:18:26,880 --> 02:18:30,399 in order to access a value within a 4040 02:18:29,040 --> 02:18:31,519 dictionary you type the name of the 4041 02:18:30,399 --> 02:18:34,319 dictionary 4042 02:18:31,519 --> 02:18:36,399 square brackets and then within quotes 4043 02:18:34,319 --> 02:18:38,800 list your key 4044 02:18:36,399 --> 02:18:40,559 so i would like to access the value at 4045 02:18:38,800 --> 02:18:42,800 this key of first 4046 02:18:40,559 --> 02:18:44,880 and let's repeat the process for last 4047 02:18:42,800 --> 02:18:46,960 name 4048 02:18:44,880 --> 02:18:48,719 so the key is going to be last we'll 4049 02:18:46,960 --> 02:18:50,880 access this value 4050 02:18:48,719 --> 02:18:52,399 at this key and this should work the 4051 02:18:50,880 --> 02:18:54,479 same as it did before 4052 02:18:52,399 --> 02:18:55,920 now let's take this a step further let's 4053 02:18:54,479 --> 02:18:58,160 say we would like to display 4054 02:18:55,920 --> 02:19:00,080 somebody's full name based on the amount 4055 02:18:58,160 --> 02:19:01,040 of keyword arguments that they pass into 4056 02:19:00,080 --> 02:19:02,559 this function 4057 02:19:01,040 --> 02:19:05,120 well there's a few different ways in 4058 02:19:02,559 --> 02:19:06,880 which we could do this one easy way is 4059 02:19:05,120 --> 02:19:08,000 that we could write our program like 4060 02:19:06,880 --> 02:19:11,599 this 4061 02:19:08,000 --> 02:19:14,080 let's print the word hello 4062 02:19:11,599 --> 02:19:16,000 and we will need to iterate once through 4063 02:19:14,080 --> 02:19:17,040 each key value pair within this 4064 02:19:16,000 --> 02:19:19,359 dictionary 4065 02:19:17,040 --> 02:19:20,080 and we can use a for loop to do that so 4066 02:19:19,359 --> 02:19:22,319 to iterate 4067 02:19:20,080 --> 02:19:23,120 once through each key value pair in your 4068 02:19:22,319 --> 02:19:26,479 dictionary 4069 02:19:23,120 --> 02:19:28,960 we'll type for key value 4070 02:19:26,479 --> 02:19:29,599 in the name of our dictionary quarks in 4071 02:19:28,960 --> 02:19:33,679 this case 4072 02:19:29,599 --> 02:19:36,719 dot items and then we will print 4073 02:19:33,679 --> 02:19:36,719 each value 4074 02:19:37,200 --> 02:19:40,559 so there's one more thing that we need 4075 02:19:38,559 --> 02:19:41,280 to do this kind of works as intended but 4076 02:19:40,559 --> 02:19:44,319 it's going to 4077 02:19:41,280 --> 02:19:44,960 print each value including our word 4078 02:19:44,319 --> 02:19:48,160 hello 4079 02:19:44,960 --> 02:19:48,960 on a new line so with a standard print 4080 02:19:48,160 --> 02:19:50,960 statement 4081 02:19:48,960 --> 02:19:52,720 this will print a new line character 4082 02:19:50,960 --> 02:19:53,520 after each print statement as if we're 4083 02:19:52,720 --> 02:19:56,000 hitting enter 4084 02:19:53,520 --> 02:19:56,800 we can actually change that so after 4085 02:19:56,000 --> 02:19:59,680 your string 4086 02:19:56,800 --> 02:20:01,120 add and equals and we can replace that 4087 02:19:59,680 --> 02:20:02,880 new line character with something else 4088 02:20:01,120 --> 02:20:04,800 maybe a space 4089 02:20:02,880 --> 02:20:06,319 and let's do the same thing for our for 4090 02:20:04,800 --> 02:20:08,800 loop so value 4091 02:20:06,319 --> 02:20:09,920 comma end and then just add space now 4092 02:20:08,800 --> 02:20:12,720 this will print 4093 02:20:09,920 --> 02:20:13,200 all of your names on the same line hello 4094 02:20:12,720 --> 02:20:15,840 bro 4095 02:20:13,200 --> 02:20:17,600 dude code and now we can pass in a 4096 02:20:15,840 --> 02:20:19,280 varying amount of keyword arguments 4097 02:20:17,600 --> 02:20:22,000 based on how long our name is 4098 02:20:19,280 --> 02:20:23,040 perhaps we have like a title title 4099 02:20:22,000 --> 02:20:26,800 equals 4100 02:20:23,040 --> 02:20:30,000 mr and we can print that as well 4101 02:20:26,800 --> 02:20:31,760 hello mr bro dude code here's a fun 4102 02:20:30,000 --> 02:20:34,000 fact of the day you don't necessarily 4103 02:20:31,760 --> 02:20:35,280 need to name this parameter as quarks 4104 02:20:34,000 --> 02:20:37,760 the important thing is to have 4105 02:20:35,280 --> 02:20:39,120 these double asterisks that precede it 4106 02:20:37,760 --> 02:20:41,439 quarks is just a 4107 02:20:39,120 --> 02:20:42,640 common naming convention it's short for 4108 02:20:41,439 --> 02:20:44,160 keyword arguments 4109 02:20:42,640 --> 02:20:45,920 you could name this as something else 4110 02:20:44,160 --> 02:20:47,840 perhaps something more descriptive like 4111 02:20:45,920 --> 02:20:49,840 names since we're passing in a bunch of 4112 02:20:47,840 --> 02:20:52,080 names so the name of our dictionary 4113 02:20:49,840 --> 02:20:53,359 is now names and this would work the 4114 02:20:52,080 --> 02:20:55,040 same as it did before 4115 02:20:53,359 --> 02:20:57,120 the important thing is to have these 4116 02:20:55,040 --> 02:20:58,160 double asterisks that precede the name 4117 02:20:57,120 --> 02:21:00,319 of your dictionary 4118 02:20:58,160 --> 02:21:01,280 but quarks is the common naming 4119 02:21:00,319 --> 02:21:03,280 convention 4120 02:21:01,280 --> 02:21:05,040 well everybody that's all about quarkx 4121 02:21:03,280 --> 02:21:07,680 it's a parameter that will pack 4122 02:21:05,040 --> 02:21:08,399 a varying amount of keyword arguments 4123 02:21:07,680 --> 02:21:10,640 into a 4124 02:21:08,399 --> 02:21:12,000 dictionary if you would like a copy of 4125 02:21:10,640 --> 02:21:13,920 all this code i will post 4126 02:21:12,000 --> 02:21:15,920 this in the comments section down below 4127 02:21:13,920 --> 02:21:18,479 but yeah that is how quarkx 4128 02:21:15,920 --> 02:21:19,040 works in python hey what's going on 4129 02:21:18,479 --> 02:21:20,880 everybody 4130 02:21:19,040 --> 02:21:22,080 it's your bro hope you're doing well and 4131 02:21:20,880 --> 02:21:23,920 in this video i'm going to teach you 4132 02:21:22,080 --> 02:21:27,040 guys all about the format method 4133 02:21:23,920 --> 02:21:29,439 in python so sit back relax and enjoy 4134 02:21:27,040 --> 02:21:29,439 the show 4135 02:21:29,680 --> 02:21:33,359 okay ladies and gentlemen let's begin 4136 02:21:32,240 --> 02:21:35,920 the format method 4137 02:21:33,359 --> 02:21:37,680 is a method available to strings it's 4138 02:21:35,920 --> 02:21:40,479 optional and it gives users 4139 02:21:37,680 --> 02:21:42,560 more control when displaying output to 4140 02:21:40,479 --> 02:21:42,960 best demonstrate this i'm going to use a 4141 02:21:42,560 --> 02:21:45,760 line 4142 02:21:42,960 --> 02:21:47,439 from a popular nursery rhyme titled hey 4143 02:21:45,760 --> 02:21:49,680 diddle diddle because well 4144 02:21:47,439 --> 02:21:50,640 you know i'm a mature adult like that 4145 02:21:49,680 --> 02:21:53,439 i'll be printing 4146 02:21:50,640 --> 02:21:54,080 this line to my console window the cow 4147 02:21:53,439 --> 02:21:56,640 jumped over 4148 02:21:54,080 --> 02:21:57,680 the moon let's do so using a standard 4149 02:21:56,640 --> 02:21:59,200 print statement 4150 02:21:57,680 --> 02:22:01,359 and then i'll show you the benefits of 4151 02:21:59,200 --> 02:22:04,080 using the format method later 4152 02:22:01,359 --> 02:22:05,840 for this example i have two variables a 4153 02:22:04,080 --> 02:22:06,399 variable called animal and i'm going to 4154 02:22:05,840 --> 02:22:09,359 store 4155 02:22:06,399 --> 02:22:10,720 a string of cow and item and i'll be 4156 02:22:09,359 --> 02:22:12,720 storing an item name 4157 02:22:10,720 --> 02:22:14,160 which is moon when we compile and run 4158 02:22:12,720 --> 02:22:16,800 this as you would expect 4159 02:22:14,160 --> 02:22:17,439 the output is the cow jumped over the 4160 02:22:16,800 --> 02:22:19,760 moon 4161 02:22:17,439 --> 02:22:20,720 there's a more elegant way of writing 4162 02:22:19,760 --> 02:22:22,800 this line of code 4163 02:22:20,720 --> 02:22:24,479 and that is by using the format method 4164 02:22:22,800 --> 02:22:26,000 available to strings 4165 02:22:24,479 --> 02:22:27,520 i'm going to turn this line into a 4166 02:22:26,000 --> 02:22:29,840 comment and we're instead 4167 02:22:27,520 --> 02:22:31,040 going to produce the same output but use 4168 02:22:29,840 --> 02:22:33,439 the format method 4169 02:22:31,040 --> 02:22:34,640 instead so we need a standard print 4170 02:22:33,439 --> 02:22:36,479 statement like usual 4171 02:22:34,640 --> 02:22:37,760 and then a string that we would like to 4172 02:22:36,479 --> 02:22:39,600 format so 4173 02:22:37,760 --> 02:22:41,120 we're going to write the same thing but 4174 02:22:39,600 --> 02:22:42,880 in place of the variables we're going to 4175 02:22:41,120 --> 02:22:45,680 add a placeholder 4176 02:22:42,880 --> 02:22:47,359 the instead of the variable name or 4177 02:22:45,680 --> 02:22:50,319 value name i'm going to add 4178 02:22:47,359 --> 02:22:51,760 a set of curly braces so that functions 4179 02:22:50,319 --> 02:22:55,359 as a placeholder 4180 02:22:51,760 --> 02:22:59,040 for a value or a variable the 4181 02:22:55,359 --> 02:23:01,359 this should be cow jumped over 4182 02:22:59,040 --> 02:23:02,560 the and i'm going to insert another 4183 02:23:01,359 --> 02:23:04,960 placeholder 4184 02:23:02,560 --> 02:23:06,240 now this is a string we can format the 4185 02:23:04,960 --> 02:23:10,000 string by following 4186 02:23:06,240 --> 02:23:12,240 this string with dot format parenthesis 4187 02:23:10,000 --> 02:23:14,080 and within the parentheses of the format 4188 02:23:12,240 --> 02:23:16,800 method we can insert 4189 02:23:14,080 --> 02:23:17,439 some values or variables let's begin 4190 02:23:16,800 --> 02:23:20,640 with some 4191 02:23:17,439 --> 02:23:23,680 values i would like to insert cow 4192 02:23:20,640 --> 02:23:26,080 as well as moon so this will produce 4193 02:23:23,680 --> 02:23:27,520 the same output but it's written a 4194 02:23:26,080 --> 02:23:30,160 little more elegantly 4195 02:23:27,520 --> 02:23:31,120 we can also replace these with variables 4196 02:23:30,160 --> 02:23:33,680 because 4197 02:23:31,120 --> 02:23:34,720 they store these values so i'm going to 4198 02:23:33,680 --> 02:23:37,920 replace cow 4199 02:23:34,720 --> 02:23:40,399 with animal and moon with item 4200 02:23:37,920 --> 02:23:41,359 and this will create the same output as 4201 02:23:40,399 --> 02:23:43,600 it did before 4202 02:23:41,359 --> 02:23:44,560 these curly braces are what's known as 4203 02:23:43,600 --> 02:23:46,800 format fields 4204 02:23:44,560 --> 02:23:47,600 they function as a placeholder for a 4205 02:23:46,800 --> 02:23:50,000 value or 4206 02:23:47,600 --> 02:23:52,560 a variable and they work in order the 4207 02:23:50,000 --> 02:23:53,280 first format field will insert the first 4208 02:23:52,560 --> 02:23:56,560 value 4209 02:23:53,280 --> 02:23:59,280 at this location if you have another set 4210 02:23:56,560 --> 02:24:00,000 or more sets of this format field you'll 4211 02:23:59,280 --> 02:24:02,640 insert 4212 02:24:00,000 --> 02:24:03,760 the next value that's listed within the 4213 02:24:02,640 --> 02:24:06,080 format method 4214 02:24:03,760 --> 02:24:08,319 so if we were to switch these around 4215 02:24:06,080 --> 02:24:09,920 we'll switch the position of animal with 4216 02:24:08,319 --> 02:24:12,240 item 4217 02:24:09,920 --> 02:24:13,280 well then the first format field is 4218 02:24:12,240 --> 02:24:15,840 going to insert 4219 02:24:13,280 --> 02:24:17,359 our item and the second format field 4220 02:24:15,840 --> 02:24:20,000 will insert our animal 4221 02:24:17,359 --> 02:24:22,319 this time the moon jumped over the cow 4222 02:24:20,000 --> 02:24:24,240 an additional way of inserting values at 4223 02:24:22,319 --> 02:24:26,840 a given place holder would be to use 4224 02:24:24,240 --> 02:24:29,600 what's referred to as a 4225 02:24:26,840 --> 02:24:32,080 positional argument 4226 02:24:29,600 --> 02:24:33,920 within these format fields we will list 4227 02:24:32,080 --> 02:24:36,319 the index of the value that we would 4228 02:24:33,920 --> 02:24:38,720 like to insert at this location 4229 02:24:36,319 --> 02:24:40,000 if you need to insert the first value 4230 02:24:38,720 --> 02:24:41,840 well that has an index 4231 02:24:40,000 --> 02:24:44,160 of zero because computers always start 4232 02:24:41,840 --> 02:24:47,200 with zero and following that pattern 4233 02:24:44,160 --> 02:24:49,280 the next index would be one if you had 4234 02:24:47,200 --> 02:24:51,680 let's say three values that you need to 4235 02:24:49,280 --> 02:24:53,760 insert if you had another placeholder 4236 02:24:51,680 --> 02:24:54,800 that would be two then so you would just 4237 02:24:53,760 --> 02:24:57,200 follow that pattern 4238 02:24:54,800 --> 02:24:58,319 so there's going to be no visible change 4239 02:24:57,200 --> 02:25:01,280 but what we could do 4240 02:24:58,319 --> 02:25:02,720 is reverse the positional arguments of 4241 02:25:01,280 --> 02:25:04,240 these placeholders 4242 02:25:02,720 --> 02:25:06,000 so what do you think would happen if i 4243 02:25:04,240 --> 02:25:07,120 switched one with zero at these 4244 02:25:06,000 --> 02:25:09,359 locations 4245 02:25:07,120 --> 02:25:11,680 well this format field is going to 4246 02:25:09,359 --> 02:25:14,720 insert the value found at index 4247 02:25:11,680 --> 02:25:15,840 one this first value is index zero and 4248 02:25:14,720 --> 02:25:17,920 the second one 4249 02:25:15,840 --> 02:25:19,920 is index one so now the roles are 4250 02:25:17,920 --> 02:25:22,640 reversed this time the moon 4251 02:25:19,920 --> 02:25:25,040 jumped over the cow our last way of 4252 02:25:22,640 --> 02:25:27,280 inserting some values at a given format 4253 02:25:25,040 --> 02:25:27,840 field would be to use what's referred to 4254 02:25:27,280 --> 02:25:31,200 as a 4255 02:25:27,840 --> 02:25:33,200 keyword argument within 4256 02:25:31,200 --> 02:25:35,359 the format method we're going to list 4257 02:25:33,200 --> 02:25:37,840 some keyword argument pairs 4258 02:25:35,359 --> 02:25:38,560 we need a keyword name followed by a 4259 02:25:37,840 --> 02:25:40,319 value 4260 02:25:38,560 --> 02:25:42,479 so let's say animal is going to be the 4261 02:25:40,319 --> 02:25:45,680 keyword we're going to say equals 4262 02:25:42,479 --> 02:25:48,000 some value animal equals cow and 4263 02:25:45,680 --> 02:25:50,080 item equals moon and you may have 4264 02:25:48,000 --> 02:25:51,040 noticed that the color of your text 4265 02:25:50,080 --> 02:25:53,040 might have changed 4266 02:25:51,040 --> 02:25:55,760 so we no longer need these variables 4267 02:25:53,040 --> 02:25:57,359 actually um but i'm going to comment out 4268 02:25:55,760 --> 02:25:58,800 these two print statements because we 4269 02:25:57,359 --> 02:26:02,000 will get an error then 4270 02:25:58,800 --> 02:26:04,880 so these are in keyword argument pairs 4271 02:26:02,000 --> 02:26:06,000 and within our format field instead of 4272 02:26:04,880 --> 02:26:08,319 keeping these empty 4273 02:26:06,000 --> 02:26:10,880 or using an index we can use the keyword 4274 02:26:08,319 --> 02:26:12,720 name at my first format field i'm going 4275 02:26:10,880 --> 02:26:15,520 to insert my keyword 4276 02:26:12,720 --> 02:26:17,040 of animal and at my second format field 4277 02:26:15,520 --> 02:26:20,000 i would like to insert 4278 02:26:17,040 --> 02:26:20,479 the item keyword so the result is going 4279 02:26:20,000 --> 02:26:22,640 to be 4280 02:26:20,479 --> 02:26:23,760 the cow jumped over the moon so let's 4281 02:26:22,640 --> 02:26:26,319 reverse these now 4282 02:26:23,760 --> 02:26:28,319 we'll place our item keyword here and 4283 02:26:26,319 --> 02:26:28,960 then at the second format field we will 4284 02:26:28,319 --> 02:26:32,080 insert 4285 02:26:28,960 --> 02:26:33,680 our animal and this time the moon 4286 02:26:32,080 --> 02:26:35,120 jumped over the cow here's an important 4287 02:26:33,680 --> 02:26:35,760 concept that i'd like to bring to your 4288 02:26:35,120 --> 02:26:37,359 attention 4289 02:26:35,760 --> 02:26:39,680 with these values found within the 4290 02:26:37,359 --> 02:26:40,319 format method you can actually reuse 4291 02:26:39,680 --> 02:26:42,560 some of these 4292 02:26:40,319 --> 02:26:43,840 more than once so let's say we would 4293 02:26:42,560 --> 02:26:46,240 like to state 4294 02:26:43,840 --> 02:26:46,960 the animal jumped over the animal we're 4295 02:26:46,240 --> 02:26:49,359 going to use 4296 02:26:46,960 --> 02:26:50,240 this animal keyword twice and our output 4297 02:26:49,359 --> 02:26:52,800 is going to be 4298 02:26:50,240 --> 02:26:54,479 the cow jumped over the cow if we're 4299 02:26:52,800 --> 02:26:55,920 doing the same thing with positional 4300 02:26:54,479 --> 02:26:58,560 arguments 4301 02:26:55,920 --> 02:26:59,439 we can reuse the same index this time 4302 02:26:58,560 --> 02:27:02,319 let's say 4303 02:26:59,439 --> 02:27:03,439 that the moon jumped over the moon so 4304 02:27:02,319 --> 02:27:06,479 our item has an 4305 02:27:03,439 --> 02:27:08,080 index of one so within our first format 4306 02:27:06,479 --> 02:27:10,479 field we'll state 4307 02:27:08,080 --> 02:27:12,319 the index will be one and our second 4308 02:27:10,479 --> 02:27:14,399 format field will also be one 4309 02:27:12,319 --> 02:27:16,080 so our output's going to be the moon 4310 02:27:14,399 --> 02:27:17,840 jumped over the moon 4311 02:27:16,080 --> 02:27:19,600 all right let's take things up a level 4312 02:27:17,840 --> 02:27:21,280 there's an even more elegant way in 4313 02:27:19,600 --> 02:27:23,760 which we could write all of this 4314 02:27:21,280 --> 02:27:25,200 what we'll do is store our string that 4315 02:27:23,760 --> 02:27:27,760 we would like to format 4316 02:27:25,200 --> 02:27:29,359 within a variable let's turn this line 4317 02:27:27,760 --> 02:27:30,319 into a comment and let's create a 4318 02:27:29,359 --> 02:27:32,800 variable called 4319 02:27:30,319 --> 02:27:33,840 text text equals the string that we 4320 02:27:32,800 --> 02:27:37,280 would like to format 4321 02:27:33,840 --> 02:27:41,680 which is the format field 4322 02:27:37,280 --> 02:27:44,720 jumped over the format field 4323 02:27:41,680 --> 02:27:47,120 and if we need to format this variable 4324 02:27:44,720 --> 02:27:50,399 we can just call the format method on it 4325 02:27:47,120 --> 02:27:54,080 so within a print statement let's say 4326 02:27:50,399 --> 02:27:54,640 text dot and then use the format method 4327 02:27:54,080 --> 02:27:56,479 and pass 4328 02:27:54,640 --> 02:27:58,720 in the values that we would like to 4329 02:27:56,479 --> 02:28:02,399 insert at these placeholders 4330 02:27:58,720 --> 02:28:05,040 so we're going to insert animal as well 4331 02:28:02,399 --> 02:28:05,680 as item so our result as you would 4332 02:28:05,040 --> 02:28:08,319 imagine 4333 02:28:05,680 --> 02:28:10,080 is the cow jumped over the moon this 4334 02:28:08,319 --> 02:28:11,200 next section i'm going to explain how we 4335 02:28:10,080 --> 02:28:13,359 can add some padding 4336 02:28:11,200 --> 02:28:14,479 to a string when we display it using the 4337 02:28:13,359 --> 02:28:16,720 format method 4338 02:28:14,479 --> 02:28:18,479 let's say we have a name variable and 4339 02:28:16,720 --> 02:28:19,200 assign this a value of whatever your 4340 02:28:18,479 --> 02:28:21,200 name is 4341 02:28:19,200 --> 02:28:22,479 so i would like to display my name along 4342 02:28:21,200 --> 02:28:25,520 with the custom message 4343 02:28:22,479 --> 02:28:28,080 let's say hello my name 4344 02:28:25,520 --> 02:28:30,240 is and then i'll use a format field here 4345 02:28:28,080 --> 02:28:31,200 so let's follow this string by using the 4346 02:28:30,240 --> 02:28:33,600 format method 4347 02:28:31,200 --> 02:28:34,800 and i would like to insert my name at 4348 02:28:33,600 --> 02:28:37,520 this location 4349 02:28:34,800 --> 02:28:38,399 so currently this will display hello my 4350 02:28:37,520 --> 02:28:40,640 name is bro 4351 02:28:38,399 --> 02:28:41,840 or whatever else your name is so we can 4352 02:28:40,640 --> 02:28:44,960 add some padding 4353 02:28:41,840 --> 02:28:46,560 either before our name after or we can 4354 02:28:44,960 --> 02:28:47,520 add some padding to the left and the 4355 02:28:46,560 --> 02:28:49,920 right hand side 4356 02:28:47,520 --> 02:28:51,200 of our value that we have so what we'll 4357 02:28:49,920 --> 02:28:53,280 do to begin 4358 02:28:51,200 --> 02:28:54,960 is let's add some padding to the right 4359 02:28:53,280 --> 02:28:56,880 hand side of 4360 02:28:54,960 --> 02:28:58,560 our name that we have so within our 4361 02:28:56,880 --> 02:28:59,200 format field if we need to add some 4362 02:28:58,560 --> 02:29:01,520 padding 4363 02:28:59,200 --> 02:29:03,200 within the format field add a colon and 4364 02:29:01,520 --> 02:29:04,240 then the amount of space you would like 4365 02:29:03,200 --> 02:29:07,600 to allocate 4366 02:29:04,240 --> 02:29:10,720 to displaying your value so let's say 10 4367 02:29:07,600 --> 02:29:11,200 so i'm going to allocate 10 spaces worth 4368 02:29:10,720 --> 02:29:13,439 of room 4369 02:29:11,200 --> 02:29:15,520 to display my name you can't really see 4370 02:29:13,439 --> 02:29:18,720 it right now but let's take a look 4371 02:29:15,520 --> 02:29:20,000 so i can't move my cursor past this line 4372 02:29:18,720 --> 02:29:22,000 at the end of my name 4373 02:29:20,000 --> 02:29:23,600 but with my second line i have all of 4374 02:29:22,000 --> 02:29:25,840 this room to work with 4375 02:29:23,600 --> 02:29:28,000 so if i were to change my string that i 4376 02:29:25,840 --> 02:29:31,120 have let's say 4377 02:29:28,000 --> 02:29:32,880 nice to meet you well then 4378 02:29:31,120 --> 02:29:34,880 all of this padding that i have is going 4379 02:29:32,880 --> 02:29:38,479 to be more evident 4380 02:29:34,880 --> 02:29:40,319 so we can add a number preceding with a 4381 02:29:38,479 --> 02:29:42,960 colon to add some padding 4382 02:29:40,319 --> 02:29:45,439 to a value and then you can left align 4383 02:29:42,960 --> 02:29:47,439 this right align it or center align it 4384 02:29:45,439 --> 02:29:48,880 so what i'll do is copy this line that i 4385 02:29:47,439 --> 02:29:51,280 have 4386 02:29:48,880 --> 02:29:52,319 if you need to left align it you use the 4387 02:29:51,280 --> 02:29:53,920 less than sign 4388 02:29:52,319 --> 02:29:55,600 but there's going to be no visible 4389 02:29:53,920 --> 02:29:56,399 change because that's already the 4390 02:29:55,600 --> 02:29:59,520 default 4391 02:29:56,399 --> 02:30:01,760 if you need to write a line precede your 4392 02:29:59,520 --> 02:30:04,319 number with a greater than sign 4393 02:30:01,760 --> 02:30:05,120 so your value is going to be right 4394 02:30:04,319 --> 02:30:07,920 aligned 4395 02:30:05,120 --> 02:30:08,479 if you need to center this value then 4396 02:30:07,920 --> 02:30:11,840 use 4397 02:30:08,479 --> 02:30:14,240 the carrot so this will center align 4398 02:30:11,840 --> 02:30:15,280 your value within the padding that you 4399 02:30:14,240 --> 02:30:16,880 have allotted 4400 02:30:15,280 --> 02:30:18,399 one question that you might have for me 4401 02:30:16,880 --> 02:30:20,560 and i'll try and answer that now because 4402 02:30:18,399 --> 02:30:22,319 it's relevant what if we need to add a 4403 02:30:20,560 --> 02:30:24,800 positional argument or a 4404 02:30:22,319 --> 02:30:26,720 keyword argument to our format field if 4405 02:30:24,800 --> 02:30:28,640 there's already some text within here 4406 02:30:26,720 --> 02:30:30,479 so what you would do before the colon 4407 02:30:28,640 --> 02:30:32,880 you would just precede the colon 4408 02:30:30,479 --> 02:30:34,160 with either your positional argument or 4409 02:30:32,880 --> 02:30:36,240 your keyword argument 4410 02:30:34,160 --> 02:30:38,240 followed by colon and then whatever you 4411 02:30:36,240 --> 02:30:40,399 want to add afterwards to format 4412 02:30:38,240 --> 02:30:42,319 your value that you have alright ladies 4413 02:30:40,399 --> 02:30:44,240 and gentlemen we are near the end and i 4414 02:30:42,319 --> 02:30:46,479 promise that this is the last section 4415 02:30:44,240 --> 02:30:47,600 how can we format some numbers let's say 4416 02:30:46,479 --> 02:30:50,000 we have a 4417 02:30:47,600 --> 02:30:52,600 number variable and this will equal the 4418 02:30:50,000 --> 02:30:55,680 first few digits of pi 4419 02:30:52,600 --> 02:30:56,000 3.14159 let's pretend that we would like 4420 02:30:55,680 --> 02:30:59,359 to 4421 02:30:56,000 --> 02:31:02,000 display only the first two digits after 4422 02:30:59,359 --> 02:31:03,040 the decimal so we can do so using the 4423 02:31:02,000 --> 02:31:06,319 format method 4424 02:31:03,040 --> 02:31:09,760 let's print the number 4425 02:31:06,319 --> 02:31:10,560 pi is and then i'll use a format field 4426 02:31:09,760 --> 02:31:12,479 here 4427 02:31:10,560 --> 02:31:13,600 and we will call the format method and 4428 02:31:12,479 --> 02:31:17,439 insert our 4429 02:31:13,600 --> 02:31:20,560 number that we have so if i need to 4430 02:31:17,439 --> 02:31:21,359 display only the first two digits after 4431 02:31:20,560 --> 02:31:24,960 the decimal 4432 02:31:21,359 --> 02:31:28,000 within the format field i will add 4433 02:31:24,960 --> 02:31:30,160 colon dot to f 4434 02:31:28,000 --> 02:31:32,399 f is for floating point numbers that's 4435 02:31:30,160 --> 02:31:35,280 anything with a decimal portion 4436 02:31:32,399 --> 02:31:36,640 so the result is that this will display 4437 02:31:35,280 --> 02:31:39,840 only the first 4438 02:31:36,640 --> 02:31:41,200 two digits after my decimal because we 4439 02:31:39,840 --> 02:31:44,240 placed two within 4440 02:31:41,200 --> 02:31:47,040 the format field so if this was three 4441 02:31:44,240 --> 02:31:47,600 point three f this would display three 4442 02:31:47,040 --> 02:31:50,160 digits 4443 02:31:47,600 --> 02:31:51,760 after the decimal portion oh and 4444 02:31:50,160 --> 02:31:53,120 apparently this will round your number 4445 02:31:51,760 --> 02:31:53,760 so that's something that you should keep 4446 02:31:53,120 --> 02:31:55,600 in mind 4447 02:31:53,760 --> 02:31:56,800 so this time let's change our number to 4448 02:31:55,600 --> 02:31:59,920 something larger like 4449 02:31:56,800 --> 02:32:00,800 1000 and i will copy this line and make 4450 02:31:59,920 --> 02:32:04,160 a few changes 4451 02:32:00,800 --> 02:32:06,640 let's say the number is 4452 02:32:04,160 --> 02:32:07,680 and i would like to add a comma at the 4453 02:32:06,640 --> 02:32:09,600 thousands place 4454 02:32:07,680 --> 02:32:11,439 so after the colon within the format 4455 02:32:09,600 --> 02:32:13,680 field i will add a comma 4456 02:32:11,439 --> 02:32:15,680 so this will automatically add a comma 4457 02:32:13,680 --> 02:32:17,280 to all 1000s places 4458 02:32:15,680 --> 02:32:19,359 another cool thing that you can do is 4459 02:32:17,280 --> 02:32:21,680 that you can display your number as 4460 02:32:19,359 --> 02:32:22,880 binary so within the format field add 4461 02:32:21,680 --> 02:32:25,040 colon b 4462 02:32:22,880 --> 02:32:27,359 this will display a binary 4463 02:32:25,040 --> 02:32:30,080 representation of your number 4464 02:32:27,359 --> 02:32:31,359 you can do the same thing with octal by 4465 02:32:30,080 --> 02:32:34,560 adding colon 4466 02:32:31,359 --> 02:32:35,840 o so our number is now being displayed 4467 02:32:34,560 --> 02:32:37,040 as an octal number 4468 02:32:35,840 --> 02:32:39,200 and you can do the same thing with 4469 02:32:37,040 --> 02:32:42,000 hexadecimal lowercase x 4470 02:32:39,200 --> 02:32:42,880 for lowercase or uppercase x for all 4471 02:32:42,000 --> 02:32:46,720 uppercase 4472 02:32:42,880 --> 02:32:49,920 so my number 1000 is now in hexadecimal 4473 02:32:46,720 --> 02:32:53,040 and lastly we can display this number 4474 02:32:49,920 --> 02:32:55,760 in scientific notation using either 4475 02:32:53,040 --> 02:32:56,880 lowercase e for lowercase or capital e 4476 02:32:55,760 --> 02:33:00,000 for uppercase 4477 02:32:56,880 --> 02:33:01,359 and mynumber 1000 is now in scientific 4478 02:33:00,000 --> 02:33:03,280 notation 4479 02:33:01,359 --> 02:33:05,200 all right everybody so that's a few 4480 02:33:03,280 --> 02:33:07,280 things that you can do with the format 4481 02:33:05,200 --> 02:33:08,399 method it's an optional method that 4482 02:33:07,280 --> 02:33:10,720 gives users 4483 02:33:08,399 --> 02:33:12,240 more control when displaying output if 4484 02:33:10,720 --> 02:33:13,680 you would like a copy of all the code 4485 02:33:12,240 --> 02:33:15,280 that we've written here today i will 4486 02:33:13,680 --> 02:33:16,000 post everything in the comments down 4487 02:33:15,280 --> 02:33:17,840 below 4488 02:33:16,000 --> 02:33:20,160 don't be afraid to smash that like 4489 02:33:17,840 --> 02:33:21,439 button drop a comment down below and 4490 02:33:20,160 --> 02:33:22,399 subscribe if you'd like to become a 4491 02:33:21,439 --> 02:33:24,479 fellow bro 4492 02:33:22,399 --> 02:33:26,000 hey what's going on everybody it's bro 4493 02:33:24,479 --> 02:33:27,760 hope you're doing well and in this video 4494 02:33:26,000 --> 02:33:29,359 i'm going to explain a few useful 4495 02:33:27,760 --> 02:33:34,720 methods of the random module 4496 02:33:29,359 --> 02:33:36,800 so sit back relax and enjoy the show 4497 02:33:34,720 --> 02:33:38,640 okay people in this video i'm going to 4498 02:33:36,800 --> 02:33:39,200 explain how we can generate some random 4499 02:33:38,640 --> 02:33:41,760 numbers 4500 02:33:39,200 --> 02:33:43,120 as well as a few other useful methods of 4501 02:33:41,760 --> 02:33:45,040 the random module 4502 02:33:43,120 --> 02:33:47,120 now we're not going to be creating true 4503 02:33:45,040 --> 02:33:49,040 random numbers but something called 4504 02:33:47,120 --> 02:33:51,280 pseudorandom numbers which are fairly 4505 02:33:49,040 --> 02:33:53,600 darn close so the first step 4506 02:33:51,280 --> 02:33:54,960 is to import the random module at the 4507 02:33:53,600 --> 02:33:57,120 top import 4508 02:33:54,960 --> 02:33:59,439 random we now have access to everything 4509 02:33:57,120 --> 02:34:01,439 that the random module has to offer 4510 02:33:59,439 --> 02:34:02,880 let's begin by generating a random 4511 02:34:01,439 --> 02:34:04,640 number between one and six 4512 02:34:02,880 --> 02:34:06,399 like we're rolling a dice so what i'm 4513 02:34:04,640 --> 02:34:10,319 going to do is say x 4514 02:34:06,399 --> 02:34:12,319 equals random dot rand 4515 02:34:10,319 --> 02:34:13,760 and we can generate a random integer 4516 02:34:12,319 --> 02:34:16,640 between a certain range 4517 02:34:13,760 --> 02:34:17,200 if i want one through six i will state 4518 02:34:16,640 --> 02:34:19,600 one 4519 02:34:17,200 --> 02:34:21,439 comma six and then we can generate a 4520 02:34:19,600 --> 02:34:22,640 random number between one and six like 4521 02:34:21,439 --> 02:34:26,080 we're rolling a dice 4522 02:34:22,640 --> 02:34:27,840 so if i were to print the value of x 4523 02:34:26,080 --> 02:34:30,160 then we will get a random number between 4524 02:34:27,840 --> 02:34:33,680 one and six for example i just got a one 4525 02:34:30,160 --> 02:34:35,920 a two a three and a six all right 4526 02:34:33,680 --> 02:34:37,120 we can also generate a random floating 4527 02:34:35,920 --> 02:34:40,560 point number two 4528 02:34:37,120 --> 02:34:43,760 let's say y equals random dot 4529 02:34:40,560 --> 02:34:44,640 random so this will give us a random 4530 02:34:43,760 --> 02:34:47,840 number between 4531 02:34:44,640 --> 02:34:52,760 0 and 1. this time i'm going to print 4532 02:34:47,840 --> 02:34:56,000 y so we have 0.49 and some change 4533 02:34:52,760 --> 02:34:58,399 0.145 and 0.858 4534 02:34:56,000 --> 02:35:00,479 we can also generate a random choice 4535 02:34:58,399 --> 02:35:02,080 from a list or other collection 4536 02:35:00,479 --> 02:35:04,640 let's say we're playing a game of rock 4537 02:35:02,080 --> 02:35:08,080 paper scissors so let's create a list 4538 02:35:04,640 --> 02:35:10,720 i'll just call this my list equals 4539 02:35:08,080 --> 02:35:11,439 square brackets and we will have three 4540 02:35:10,720 --> 02:35:14,720 choices 4541 02:35:11,439 --> 02:35:17,200 rock paper and 4542 02:35:14,720 --> 02:35:17,200 scissors 4543 02:35:18,560 --> 02:35:24,479 so let's say z 4544 02:35:21,680 --> 02:35:25,359 will be the random string that we 4545 02:35:24,479 --> 02:35:28,479 generate 4546 02:35:25,359 --> 02:35:33,040 z equals random dot 4547 02:35:28,479 --> 02:35:36,880 choice and we will pass in my list 4548 02:35:33,040 --> 02:35:37,359 and let's print z so this will generate 4549 02:35:36,880 --> 02:35:40,080 a 4550 02:35:37,359 --> 02:35:40,560 random choice from my list called my 4551 02:35:40,080 --> 02:35:43,600 list 4552 02:35:40,560 --> 02:35:46,399 so we got scissors paper and 4553 02:35:43,600 --> 02:35:47,840 rock we can also use the shuffle method 4554 02:35:46,399 --> 02:35:50,399 of the random module to 4555 02:35:47,840 --> 02:35:52,720 shuffle a list or other collection let's 4556 02:35:50,399 --> 02:35:56,479 say we're working with a deck of cards 4557 02:35:52,720 --> 02:35:59,040 cards equals let's say one 4558 02:35:56,479 --> 02:35:59,040 through nine 4559 02:36:00,240 --> 02:36:08,160 and we have a jack a queen 4560 02:36:04,640 --> 02:36:10,399 a king and an ace this won't be a full 4561 02:36:08,160 --> 02:36:13,200 deck of cards it'll just be one suit 4562 02:36:10,399 --> 02:36:14,880 so we can actually shuffle this list by 4563 02:36:13,200 --> 02:36:18,479 using the shuffle method 4564 02:36:14,880 --> 02:36:18,960 random dot shuffle what do we want to 4565 02:36:18,479 --> 02:36:22,560 shuffle 4566 02:36:18,960 --> 02:36:23,200 we want to shuffle our cards and i will 4567 02:36:22,560 --> 02:36:26,319 display 4568 02:36:23,200 --> 02:36:28,399 my cards with a print statement so the 4569 02:36:26,319 --> 02:36:29,680 shuffle method will shuffle a list or 4570 02:36:28,399 --> 02:36:31,520 other collection for you 4571 02:36:29,680 --> 02:36:33,280 well everybody that's a few useful 4572 02:36:31,520 --> 02:36:34,560 methods of the random module 4573 02:36:33,280 --> 02:36:36,399 if you would like a copy of all this 4574 02:36:34,560 --> 02:36:37,280 code i will post this in the comments 4575 02:36:36,399 --> 02:36:39,120 down below 4576 02:36:37,280 --> 02:36:40,960 don't be afraid to smash that like 4577 02:36:39,120 --> 02:36:42,560 button drop a comment down below 4578 02:36:40,960 --> 02:36:45,439 and subscribe if you'd like to become a 4579 02:36:42,560 --> 02:36:45,840 fellow bro hey what's going on everybody 4580 02:36:45,439 --> 02:36:47,600 it's 4581 02:36:45,840 --> 02:36:49,120 bro hope you're doing well and in this 4582 02:36:47,600 --> 02:36:50,240 video i'm going to explain exception 4583 02:36:49,120 --> 02:36:55,439 handling in python 4584 02:36:50,240 --> 02:36:55,439 so sit back relax and enjoy the show 4585 02:36:55,680 --> 02:36:59,680 well everyone let's talk about 4586 02:36:57,439 --> 02:37:00,319 exceptions an exception is an event 4587 02:36:59,680 --> 02:37:02,720 detected 4588 02:37:00,319 --> 02:37:04,319 during execution that interrupts the 4589 02:37:02,720 --> 02:37:06,319 normal flow of a program 4590 02:37:04,319 --> 02:37:07,840 here's a program that i've written and 4591 02:37:06,319 --> 02:37:08,640 we're intentionally going to cause an 4592 02:37:07,840 --> 02:37:10,399 exception 4593 02:37:08,640 --> 02:37:12,319 so this program that i've written we 4594 02:37:10,399 --> 02:37:13,920 will accept a numerator and a 4595 02:37:12,319 --> 02:37:15,439 denominator from the user 4596 02:37:13,920 --> 02:37:17,120 we'll divide the numerator by the 4597 02:37:15,439 --> 02:37:19,120 denominator we'll get a result 4598 02:37:17,120 --> 02:37:21,439 and print the result so what if i were 4599 02:37:19,120 --> 02:37:23,439 to divide a number such as 5 4600 02:37:21,439 --> 02:37:24,720 divided by 0 which we cannot 4601 02:37:23,439 --> 02:37:26,800 mathematically do 4602 02:37:24,720 --> 02:37:27,840 well this will cause an exception an 4603 02:37:26,800 --> 02:37:30,080 event detected 4604 02:37:27,840 --> 02:37:31,359 during execution that interrupts the 4605 02:37:30,080 --> 02:37:33,680 flow of a program 4606 02:37:31,359 --> 02:37:35,439 so our program was interrupted and 4607 02:37:33,680 --> 02:37:36,800 within our console window we have this 4608 02:37:35,439 --> 02:37:39,439 traceback message 4609 02:37:36,800 --> 02:37:41,120 which states what went wrong and a name 4610 02:37:39,439 --> 02:37:43,040 for this exception that occurred 4611 02:37:41,120 --> 02:37:44,640 this specific exception that occurred is 4612 02:37:43,040 --> 02:37:47,040 a zero division error 4613 02:37:44,640 --> 02:37:48,960 so this video is all about handling 4614 02:37:47,040 --> 02:37:51,040 these exceptions so they do not 4615 02:37:48,960 --> 02:37:53,200 interrupt the normal flow of a program 4616 02:37:51,040 --> 02:37:53,840 now a very basic form of exception 4617 02:37:53,200 --> 02:37:55,439 handling 4618 02:37:53,840 --> 02:37:57,359 is to surround any code that is 4619 02:37:55,439 --> 02:37:58,479 considered dangerous as in it might 4620 02:37:57,359 --> 02:38:01,120 cause an exception 4621 02:37:58,479 --> 02:38:03,359 within a try block anytime you accept 4622 02:38:01,120 --> 02:38:05,359 user input would be a good indicator too 4623 02:38:03,359 --> 02:38:06,800 so this code is considered dangerous 4624 02:38:05,359 --> 02:38:07,920 because we don't know what the user is 4625 02:38:06,800 --> 02:38:10,399 going to type in 4626 02:38:07,920 --> 02:38:11,439 so we will try all of this code if an 4627 02:38:10,399 --> 02:38:13,840 exception occurs 4628 02:38:11,439 --> 02:38:14,479 we can catch it and handle it so to do 4629 02:38:13,840 --> 02:38:16,640 that 4630 02:38:14,479 --> 02:38:18,319 we'll add an accept block accept and 4631 02:38:16,640 --> 02:38:21,359 then we will catch 4632 02:38:18,319 --> 02:38:21,840 exception normally having this block by 4633 02:38:21,359 --> 02:38:23,520 itself 4634 02:38:21,840 --> 02:38:25,120 isn't considered good practice but i'll 4635 02:38:23,520 --> 02:38:27,600 explain why later so 4636 02:38:25,120 --> 02:38:29,680 this accept exception block will catch 4637 02:38:27,600 --> 02:38:31,280 all sorts of different exceptions 4638 02:38:29,680 --> 02:38:32,880 and it will prevent our program from 4639 02:38:31,280 --> 02:38:34,880 being interrupted and we can do 4640 02:38:32,880 --> 02:38:35,680 something else entirely such as print a 4641 02:38:34,880 --> 02:38:39,359 message 4642 02:38:35,680 --> 02:38:40,160 something went wrong so let's try this 4643 02:38:39,359 --> 02:38:42,560 again 4644 02:38:40,160 --> 02:38:43,439 we will divide five by zero which we 4645 02:38:42,560 --> 02:38:45,600 cannot do 4646 02:38:43,439 --> 02:38:46,960 we no longer have that traceback message 4647 02:38:45,600 --> 02:38:48,640 within our console window 4648 02:38:46,960 --> 02:38:50,399 we caught the exception and we did 4649 02:38:48,640 --> 02:38:53,280 something else we printed 4650 02:38:50,399 --> 02:38:55,040 something went wrong like i said before 4651 02:38:53,280 --> 02:38:56,080 it's not considered good practice to 4652 02:38:55,040 --> 02:38:58,000 have a single 4653 02:38:56,080 --> 02:39:00,240 accept block that will handle all 4654 02:38:58,000 --> 02:39:02,399 exceptions it's much better to first 4655 02:39:00,240 --> 02:39:02,960 handle specific exceptions when they 4656 02:39:02,399 --> 02:39:05,040 occur 4657 02:39:02,960 --> 02:39:06,319 and we can do so by writing additional 4658 02:39:05,040 --> 02:39:08,560 accept blocks 4659 02:39:06,319 --> 02:39:10,000 so let's create an additional accept 4660 02:39:08,560 --> 02:39:13,120 block that will catch 4661 02:39:10,000 --> 02:39:14,479 any zero division error exceptions 4662 02:39:13,120 --> 02:39:16,640 that occurs when somebody attempts to 4663 02:39:14,479 --> 02:39:18,479 divide by zero so if 4664 02:39:16,640 --> 02:39:20,479 this exception occurs how do we want to 4665 02:39:18,479 --> 02:39:24,319 handle it let's just print the message 4666 02:39:20,479 --> 02:39:27,359 let's print you can't divide 4667 02:39:24,319 --> 02:39:30,240 by zero idiot 4668 02:39:27,359 --> 02:39:31,520 and let's try this again so five divided 4669 02:39:30,240 --> 02:39:33,840 by zero 4670 02:39:31,520 --> 02:39:35,439 you can't divide by zero idiot here's 4671 02:39:33,840 --> 02:39:38,000 another one i'm going to turn 4672 02:39:35,439 --> 02:39:39,359 this accept block into a comment so we 4673 02:39:38,000 --> 02:39:41,359 can take a look at something 4674 02:39:39,359 --> 02:39:42,720 what if i divide by something that's not 4675 02:39:41,359 --> 02:39:45,200 a number like we take 4676 02:39:42,720 --> 02:39:46,560 5 and divide it by the word pizza well 4677 02:39:45,200 --> 02:39:50,160 pizza is not a number 4678 02:39:46,560 --> 02:39:52,399 and we ran into a value error exception 4679 02:39:50,160 --> 02:39:54,399 so we should probably handle this too so 4680 02:39:52,399 --> 02:39:55,680 let's create another accept block that 4681 02:39:54,399 --> 02:39:59,840 will handle that 4682 02:39:55,680 --> 02:40:02,000 accept value error 4683 02:39:59,840 --> 02:40:03,920 and how do we want to handle this let's 4684 02:40:02,000 --> 02:40:06,960 print something 4685 02:40:03,920 --> 02:40:10,399 enter only numbers 4686 02:40:06,960 --> 02:40:10,880 please okay let's try this again and see 4687 02:40:10,399 --> 02:40:13,680 if 4688 02:40:10,880 --> 02:40:14,240 this exception is caught five divided by 4689 02:40:13,680 --> 02:40:17,920 the word 4690 02:40:14,240 --> 02:40:20,960 pizza enter only numbers please 4691 02:40:17,920 --> 02:40:23,200 not only that but you can also add the 4692 02:40:20,960 --> 02:40:24,880 accept exception block at the very end 4693 02:40:23,200 --> 02:40:25,840 just in case there's something you won't 4694 02:40:24,880 --> 02:40:27,520 anticipate 4695 02:40:25,840 --> 02:40:30,160 but it's always considered good practice 4696 02:40:27,520 --> 02:40:31,920 to first catch any specific exceptions 4697 02:40:30,160 --> 02:40:34,479 and let the user know exactly what went 4698 02:40:31,920 --> 02:40:35,760 wrong an additional way in which we can 4699 02:40:34,479 --> 02:40:38,000 handle these exceptions 4700 02:40:35,760 --> 02:40:39,840 is that we could display the exception 4701 02:40:38,000 --> 02:40:40,560 that occurs although it is completely 4702 02:40:39,840 --> 02:40:42,319 optional 4703 02:40:40,560 --> 02:40:44,160 so to do that what we'll do is that 4704 02:40:42,319 --> 02:40:45,120 after each of these accept block 4705 02:40:44,160 --> 02:40:48,319 definitions 4706 02:40:45,120 --> 02:40:49,279 let's add as e so we're calling the 4707 02:40:48,319 --> 02:40:50,960 exception as 4708 02:40:49,279 --> 02:40:52,319 e that's just standard practice although 4709 02:40:50,960 --> 02:40:54,479 it's not necessary 4710 02:40:52,319 --> 02:40:55,760 so let's do that for each of these 4711 02:40:54,479 --> 02:40:58,720 except blocks 4712 02:40:55,760 --> 02:40:59,920 and then we can print whatever e is so 4713 02:40:58,720 --> 02:41:02,960 let's add that 4714 02:40:59,920 --> 02:41:04,800 to each of these blocks so 4715 02:41:02,960 --> 02:41:06,800 it should now look something like this 4716 02:41:04,800 --> 02:41:09,200 so when we encounter an exception 4717 02:41:06,800 --> 02:41:10,080 we can display what exception occurred 4718 02:41:09,200 --> 02:41:13,600 too as well 4719 02:41:10,080 --> 02:41:16,800 so let's divide five by pizza invalid 4720 02:41:13,600 --> 02:41:19,680 literal for int with base 10 pizza 4721 02:41:16,800 --> 02:41:20,080 enter only numbers please so like i said 4722 02:41:19,680 --> 02:41:21,680 this 4723 02:41:20,080 --> 02:41:23,279 is an additional way in which you could 4724 02:41:21,680 --> 02:41:24,640 handle an exception although it's 4725 02:41:23,279 --> 02:41:26,319 entirely optional 4726 02:41:24,640 --> 02:41:28,160 another thing that you can do is that 4727 02:41:26,319 --> 02:41:30,800 you can add an else statement 4728 02:41:28,160 --> 02:41:32,319 to the end of your accept blocks so 4729 02:41:30,800 --> 02:41:32,960 what's happening here is that we're 4730 02:41:32,319 --> 02:41:35,279 going to 4731 02:41:32,960 --> 02:41:36,080 try all of this code if an exception 4732 02:41:35,279 --> 02:41:38,880 happens 4733 02:41:36,080 --> 02:41:40,000 then catch it and handle it if not then 4734 02:41:38,880 --> 02:41:42,960 do something else 4735 02:41:40,000 --> 02:41:43,600 so why don't we print our result only if 4736 02:41:42,960 --> 02:41:46,720 there are no 4737 02:41:43,600 --> 02:41:48,399 exceptions that occur so if there are no 4738 02:41:46,720 --> 02:41:51,279 exceptions we will execute 4739 02:41:48,399 --> 02:41:51,840 this else statement if not then we won't 4740 02:41:51,279 --> 02:41:53,840 so 4741 02:41:51,840 --> 02:41:54,960 let's divide five by an actual number 4742 02:41:53,840 --> 02:41:57,439 this time like 4743 02:41:54,960 --> 02:41:58,319 two so there are no exceptions that 4744 02:41:57,439 --> 02:42:00,720 occurred so 4745 02:41:58,319 --> 02:42:01,680 we executed this else statement whatever 4746 02:42:00,720 --> 02:42:03,439 code is within 4747 02:42:01,680 --> 02:42:05,200 our else block and this printed the 4748 02:42:03,439 --> 02:42:07,200 result of 2.5 4749 02:42:05,200 --> 02:42:08,479 but if an exception were to occur like 4750 02:42:07,200 --> 02:42:11,840 we divide 5 by 4751 02:42:08,479 --> 02:42:12,479 0 then we will catch our exception and 4752 02:42:11,840 --> 02:42:15,040 handle it 4753 02:42:12,479 --> 02:42:16,080 and we do not execute whatever block of 4754 02:42:15,040 --> 02:42:18,319 code is within 4755 02:42:16,080 --> 02:42:20,000 our else statement there is one other 4756 02:42:18,319 --> 02:42:20,560 clause that you should be aware of that 4757 02:42:20,000 --> 02:42:22,960 is the 4758 02:42:20,560 --> 02:42:23,840 final clause this is always at the end 4759 02:42:22,960 --> 02:42:25,920 how this works 4760 02:42:23,840 --> 02:42:28,319 is that whether or not we catch an 4761 02:42:25,920 --> 02:42:28,720 exception we will always execute any 4762 02:42:28,319 --> 02:42:30,720 code 4763 02:42:28,720 --> 02:42:32,000 that is within the block of code for our 4764 02:42:30,720 --> 02:42:34,160 final clause 4765 02:42:32,000 --> 02:42:35,120 so this is a good opportunity if you 4766 02:42:34,160 --> 02:42:38,000 open files 4767 02:42:35,120 --> 02:42:38,399 to close them within the finally block 4768 02:42:38,000 --> 02:42:40,399 but 4769 02:42:38,399 --> 02:42:41,600 we haven't dealt with opening files yet 4770 02:42:40,399 --> 02:42:43,120 so we won't do that 4771 02:42:41,600 --> 02:42:45,040 all we'll do is just print something 4772 02:42:43,120 --> 02:42:48,399 print this 4773 02:42:45,040 --> 02:42:50,640 will always execute so 4774 02:42:48,399 --> 02:42:52,720 whether or not we encounter an exception 4775 02:42:50,640 --> 02:42:53,359 this finally block will always execute 4776 02:42:52,720 --> 02:42:55,520 at the end 4777 02:42:53,359 --> 02:42:56,880 so let's try this again so this time 4778 02:42:55,520 --> 02:42:59,600 let's cause an exception 4779 02:42:56,880 --> 02:43:00,240 let's divide five by zero you can't 4780 02:42:59,600 --> 02:43:02,720 divide by 4781 02:43:00,240 --> 02:43:04,479 zero this will always execute and this 4782 02:43:02,720 --> 02:43:04,960 time let's take this seriously and 4783 02:43:04,479 --> 02:43:07,760 divide 4784 02:43:04,960 --> 02:43:09,359 five by another number such as three so 4785 02:43:07,760 --> 02:43:11,680 this will give us a result 4786 02:43:09,359 --> 02:43:13,279 and like i said the finally clause the 4787 02:43:11,680 --> 02:43:14,399 code within the finally clause will 4788 02:43:13,279 --> 02:43:16,240 always execute 4789 02:43:14,399 --> 02:43:18,319 but we won't really be working with this 4790 02:43:16,240 --> 02:43:19,359 until we get to the section on file 4791 02:43:18,319 --> 02:43:21,680 handling 4792 02:43:19,359 --> 02:43:24,080 well everybody those are exceptions in 4793 02:43:21,680 --> 02:43:26,479 python they are events detected 4794 02:43:24,080 --> 02:43:27,520 during execution that interrupt the flow 4795 02:43:26,479 --> 02:43:29,520 of a program 4796 02:43:27,520 --> 02:43:31,680 and we can handle these exceptions by 4797 02:43:29,520 --> 02:43:33,359 using try and accept blocks 4798 02:43:31,680 --> 02:43:34,960 so if you would like a copy of all this 4799 02:43:33,359 --> 02:43:36,319 code i will post all of this in the 4800 02:43:34,960 --> 02:43:38,800 comments down below 4801 02:43:36,319 --> 02:43:40,319 but yeah that is how exceptions work in 4802 02:43:38,800 --> 02:43:42,880 python 4803 02:43:40,319 --> 02:43:43,920 hey what's going on everybody it's bro 4804 02:43:42,880 --> 02:43:45,200 hope you're doing well 4805 02:43:43,920 --> 02:43:47,439 and in this video we're going to be 4806 02:43:45,200 --> 02:43:48,399 covering some basic file detection using 4807 02:43:47,439 --> 02:43:52,960 python so 4808 02:43:48,399 --> 02:43:52,960 sit back relax and enjoy the show 4809 02:43:53,040 --> 02:43:57,120 welcome ladies and gentlemen to the 4810 02:43:55,040 --> 02:43:59,439 beginning of a mini series involving 4811 02:43:57,120 --> 02:44:00,880 doing stuff with files using python and 4812 02:43:59,439 --> 02:44:01,680 in this video we're going to be doing 4813 02:44:00,880 --> 02:44:04,240 some basic 4814 02:44:01,680 --> 02:44:04,800 file detection and in order to do so i 4815 02:44:04,240 --> 02:44:07,120 recommend 4816 02:44:04,800 --> 02:44:08,800 importing the os module it's already 4817 02:44:07,120 --> 02:44:09,359 included with the standard python 4818 02:44:08,800 --> 02:44:11,359 library 4819 02:44:09,359 --> 02:44:13,520 all you need to do is import it all 4820 02:44:11,359 --> 02:44:14,000 we'll be doing is checking to see if a 4821 02:44:13,520 --> 02:44:16,080 file 4822 02:44:14,000 --> 02:44:17,840 exists someplace on our computer so 4823 02:44:16,080 --> 02:44:20,000 we'll probably need a file to work with 4824 02:44:17,840 --> 02:44:21,120 what i'm going to do is go to my desktop 4825 02:44:20,000 --> 02:44:23,520 for convenience 4826 02:44:21,120 --> 02:44:25,760 and just create a new text document i 4827 02:44:23,520 --> 02:44:28,000 will call this test.txt 4828 02:44:25,760 --> 02:44:30,319 now i probably need the file location so 4829 02:44:28,000 --> 02:44:32,960 i'm going to copy that 4830 02:44:30,319 --> 02:44:34,479 and then we can use this so let's create 4831 02:44:32,960 --> 02:44:36,640 a variable called path 4832 02:44:34,479 --> 02:44:38,160 that will include the path to the file 4833 02:44:36,640 --> 02:44:41,760 that i would like to detect 4834 02:44:38,160 --> 02:44:44,319 so that was test.txt now if you have 4835 02:44:41,760 --> 02:44:45,200 backslashes in your file path you'll 4836 02:44:44,319 --> 02:44:47,359 probably need 4837 02:44:45,200 --> 02:44:49,840 double backslashes because that's the 4838 02:44:47,359 --> 02:44:50,560 escape sequence for a backslash within a 4839 02:44:49,840 --> 02:44:53,120 string 4840 02:44:50,560 --> 02:44:54,160 so we now have this variable path that 4841 02:44:53,120 --> 02:44:56,720 we can work with 4842 02:44:54,160 --> 02:44:57,520 now i would like to check to see if this 4843 02:44:56,720 --> 02:45:00,560 location 4844 02:44:57,520 --> 02:45:04,160 exists on my computer so if os 4845 02:45:00,560 --> 02:45:05,120 dot path dot exists and you can see that 4846 02:45:04,160 --> 02:45:07,600 we can pass in 4847 02:45:05,120 --> 02:45:08,560 our path as an argument so i will pass 4848 02:45:07,600 --> 02:45:10,880 in my path 4849 02:45:08,560 --> 02:45:14,319 and if this returns true if that 4850 02:45:10,880 --> 02:45:18,960 location exists i will print 4851 02:45:14,319 --> 02:45:21,279 that location exists 4852 02:45:18,960 --> 02:45:22,319 so this will not tell me if this is a 4853 02:45:21,279 --> 02:45:25,359 file or not 4854 02:45:22,319 --> 02:45:29,680 we'll be covering that later else 4855 02:45:25,359 --> 02:45:32,800 let's print that location 4856 02:45:29,680 --> 02:45:35,439 doesn't exist 4857 02:45:32,800 --> 02:45:37,040 all right so if this location exists 4858 02:45:35,439 --> 02:45:39,359 this will return true 4859 02:45:37,040 --> 02:45:40,399 if not this will execute our else 4860 02:45:39,359 --> 02:45:43,200 statement 4861 02:45:40,399 --> 02:45:44,399 that location exists so what if i were 4862 02:45:43,200 --> 02:45:46,880 to delete this file 4863 02:45:44,399 --> 02:45:48,880 and we run this program again that 4864 02:45:46,880 --> 02:45:51,600 location doesn't exist 4865 02:45:48,880 --> 02:45:52,399 now this won't tell you if the location 4866 02:45:51,600 --> 02:45:55,040 you're dealing with 4867 02:45:52,399 --> 02:45:56,399 is a file or not there is a separate 4868 02:45:55,040 --> 02:45:59,840 function for that 4869 02:45:56,399 --> 02:46:05,040 it is if os 4870 02:45:59,840 --> 02:46:05,040 dot path dot is file 4871 02:46:05,200 --> 02:46:09,760 what we'll do is pass in our path and 4872 02:46:07,680 --> 02:46:12,880 print 4873 02:46:09,760 --> 02:46:16,240 that is a file 4874 02:46:12,880 --> 02:46:18,359 all right so let's create another file 4875 02:46:16,240 --> 02:46:21,279 because i deleted the previous one 4876 02:46:18,359 --> 02:46:22,399 test.txt it's in the same location let's 4877 02:46:21,279 --> 02:46:25,520 run this again 4878 02:46:22,399 --> 02:46:27,439 that location exists that is a file now 4879 02:46:25,520 --> 02:46:29,439 what if this was a folder 4880 02:46:27,439 --> 02:46:30,880 so i'm going to create a new folder i 4881 02:46:29,439 --> 02:46:34,640 will call this folder 4882 02:46:30,880 --> 02:46:39,200 i will delete my test document 4883 02:46:34,640 --> 02:46:41,279 and let's change the location to folder 4884 02:46:39,200 --> 02:46:43,600 that location exists but it is not a 4885 02:46:41,279 --> 02:46:44,880 file you can check to see if a location 4886 02:46:43,600 --> 02:46:48,160 is a directory 4887 02:46:44,880 --> 02:46:52,319 so let's write an else if statement os 4888 02:46:48,160 --> 02:46:54,479 dot path dot is directory and we will 4889 02:46:52,319 --> 02:46:57,840 pass in our path 4890 02:46:54,479 --> 02:47:01,840 and let's print that 4891 02:46:57,840 --> 02:47:03,760 is a directory 4892 02:47:01,840 --> 02:47:05,359 all right so we have our folder we're 4893 02:47:03,760 --> 02:47:08,479 going to check to see 4894 02:47:05,359 --> 02:47:09,120 if this location exists and if it's a 4895 02:47:08,479 --> 02:47:12,240 file or 4896 02:47:09,120 --> 02:47:13,359 a directory that location exists that is 4897 02:47:12,240 --> 02:47:15,760 a directory 4898 02:47:13,359 --> 02:47:16,399 well everyone that is the basics of file 4899 02:47:15,760 --> 02:47:18,319 detection 4900 02:47:16,399 --> 02:47:19,520 we'll be building upon this concept in 4901 02:47:18,319 --> 02:47:21,680 future videos 4902 02:47:19,520 --> 02:47:23,439 so if you would like a copy of this code 4903 02:47:21,680 --> 02:47:24,160 i will post this in the comment section 4904 02:47:23,439 --> 02:47:26,640 down below 4905 02:47:24,160 --> 02:47:28,960 but yeah that is the basics of simple 4906 02:47:26,640 --> 02:47:31,040 file detection using python 4907 02:47:28,960 --> 02:47:32,640 hey what's going on everybody it's bro 4908 02:47:31,040 --> 02:47:34,479 hope you're doing well and in this video 4909 02:47:32,640 --> 02:47:35,439 we're going to be reading a file in 4910 02:47:34,479 --> 02:47:39,920 python so 4911 02:47:35,439 --> 02:47:39,920 sit back relax and enjoy the show 4912 02:47:40,000 --> 02:47:44,000 welcome back guys and gals in this video 4913 02:47:42,240 --> 02:47:46,319 i'm going to explain how we can read the 4914 02:47:44,000 --> 02:47:48,240 contents of a file using python 4915 02:47:46,319 --> 02:47:50,720 so we'll probably need some sort of file 4916 02:47:48,240 --> 02:47:51,720 to work with i created a plain text file 4917 02:47:50,720 --> 02:47:54,640 called 4918 02:47:51,720 --> 02:47:55,279 test.txt it's a plain text file and all 4919 02:47:54,640 --> 02:47:58,960 this says 4920 02:47:55,279 --> 02:48:01,359 is omg you can read this have a nice day 4921 02:47:58,960 --> 02:48:02,560 like comment and subscribe so what i 4922 02:48:01,359 --> 02:48:04,960 would like to do is to read 4923 02:48:02,560 --> 02:48:06,640 the contents of that file line by line 4924 02:48:04,960 --> 02:48:08,479 and print it to the console window 4925 02:48:06,640 --> 02:48:10,080 and here's how and honestly we only need 4926 02:48:08,479 --> 02:48:13,520 two lines of code to do this 4927 02:48:10,080 --> 02:48:15,840 so what we'll do is type with open 4928 02:48:13,520 --> 02:48:17,680 then within the open function list the 4929 02:48:15,840 --> 02:48:20,240 name of your file 4930 02:48:17,680 --> 02:48:21,920 or the file path this file of mine is 4931 02:48:20,240 --> 02:48:24,560 within my project folder 4932 02:48:21,920 --> 02:48:26,640 so i only need to list the file name if 4933 02:48:24,560 --> 02:48:27,200 this was someplace else you'll probably 4934 02:48:26,640 --> 02:48:30,160 need 4935 02:48:27,200 --> 02:48:31,120 the file path so let's say this was on 4936 02:48:30,160 --> 02:48:33,359 my desktop 4937 02:48:31,120 --> 02:48:35,520 well i'll probably need to list the file 4938 02:48:33,359 --> 02:48:38,080 path of where that file is located 4939 02:48:35,520 --> 02:48:40,319 and i'll need to use double backslashes 4940 02:48:38,080 --> 02:48:41,200 because that's the escape sequence for a 4941 02:48:40,319 --> 02:48:43,359 backslash 4942 02:48:41,200 --> 02:48:45,920 but since this file is within my project 4943 02:48:43,359 --> 02:48:49,200 folder i only need to list the file name 4944 02:48:45,920 --> 02:48:50,720 so with open then the name your file or 4945 02:48:49,200 --> 02:48:54,560 the file path 4946 02:48:50,720 --> 02:48:58,720 as file then what we're going to do 4947 02:48:54,560 --> 02:49:02,319 is print file dot 4948 02:48:58,720 --> 02:49:05,359 read and that is it so let's test it 4949 02:49:02,319 --> 02:49:06,240 and it works omg you can read this have 4950 02:49:05,359 --> 02:49:09,120 a nice day 4951 02:49:06,240 --> 02:49:09,680 like comment and subscribe so this will 4952 02:49:09,120 --> 02:49:12,399 actually 4953 02:49:09,680 --> 02:49:12,720 close files automatically after opening 4954 02:49:12,399 --> 02:49:14,560 them 4955 02:49:12,720 --> 02:49:15,840 so that's kind of convenient and let's 4956 02:49:14,560 --> 02:49:19,120 just test that theory 4957 02:49:15,840 --> 02:49:22,160 so i'm going to print file dot 4958 02:49:19,120 --> 02:49:22,800 closed if my file is in fact closed this 4959 02:49:22,160 --> 02:49:24,880 will print 4960 02:49:22,800 --> 02:49:26,000 true if it's still open this will print 4961 02:49:24,880 --> 02:49:28,160 false 4962 02:49:26,000 --> 02:49:29,680 so normally when you read a file it's 4963 02:49:28,160 --> 02:49:31,680 normally open and you need to 4964 02:49:29,680 --> 02:49:32,880 close it manually but if you write your 4965 02:49:31,680 --> 02:49:35,279 program this way 4966 02:49:32,880 --> 02:49:36,880 with open this will close the file 4967 02:49:35,279 --> 02:49:39,840 automatically for you 4968 02:49:36,880 --> 02:49:41,040 so using with open will close any files 4969 02:49:39,840 --> 02:49:43,520 automatically for you 4970 02:49:41,040 --> 02:49:44,640 after they've been opened however this 4971 02:49:43,520 --> 02:49:46,640 does not catch 4972 02:49:44,640 --> 02:49:49,359 and handle any exceptions that might 4973 02:49:46,640 --> 02:49:51,439 occur like if we cannot locate this file 4974 02:49:49,359 --> 02:49:53,200 so let's say that i mistyped the 4975 02:49:51,439 --> 02:49:56,640 extension for this file 4976 02:49:53,200 --> 02:49:58,720 let's say that i type in test.tx 4977 02:49:56,640 --> 02:49:59,760 that's kind of funny because tx is the 4978 02:49:58,720 --> 02:50:02,800 abbreviation for 4979 02:49:59,760 --> 02:50:03,279 texas it's a texas file so this will 4980 02:50:02,800 --> 02:50:05,920 cause 4981 02:50:03,279 --> 02:50:08,080 a file not found error and this will 4982 02:50:05,920 --> 02:50:09,760 interrupt the flow of our program and we 4983 02:50:08,080 --> 02:50:12,240 would probably like to prevent that 4984 02:50:09,760 --> 02:50:13,279 so what we could do is write a try and 4985 02:50:12,240 --> 02:50:15,520 an accept block 4986 02:50:13,279 --> 02:50:16,399 so we will try all of this code if we 4987 02:50:15,520 --> 02:50:19,200 cannot find 4988 02:50:16,399 --> 02:50:20,640 this file we can handle this exception 4989 02:50:19,200 --> 02:50:22,399 file not found error 4990 02:50:20,640 --> 02:50:23,760 so we'll place all of this code within 4991 02:50:22,399 --> 02:50:25,600 the try block then 4992 02:50:23,760 --> 02:50:26,800 if an exception happens we will catch 4993 02:50:25,600 --> 02:50:29,760 that exception 4994 02:50:26,800 --> 02:50:30,800 except we would like to catch any file 4995 02:50:29,760 --> 02:50:33,359 not found air 4996 02:50:30,800 --> 02:50:35,600 exceptions and let's handle this by 4997 02:50:33,359 --> 02:50:40,160 printing 4998 02:50:35,600 --> 02:50:42,880 that file was not found 4999 02:50:40,160 --> 02:50:44,880 so if i were to run this again we will 5000 02:50:42,880 --> 02:50:45,359 catch and handle this exception and this 5001 02:50:44,880 --> 02:50:48,160 will not 5002 02:50:45,359 --> 02:50:50,319 interrupt the normal flow of our program 5003 02:50:48,160 --> 02:50:50,640 well everybody that's how to open and 5004 02:50:50,319 --> 02:50:53,359 read 5005 02:50:50,640 --> 02:50:55,120 a file in python if you'd like a copy of 5006 02:50:53,359 --> 02:50:56,640 all this code i will post this in the 5007 02:50:55,120 --> 02:50:58,960 comment section down below 5008 02:50:56,640 --> 02:51:00,720 but yeah that's how to open and read a 5009 02:50:58,960 --> 02:51:03,040 file using python 5010 02:51:00,720 --> 02:51:04,560 hey what's going on everybody it's bro 5011 02:51:03,040 --> 02:51:06,160 hope you're doing well and in this video 5012 02:51:04,560 --> 02:51:07,840 we're going to be writing files in 5013 02:51:06,160 --> 02:51:11,600 python so sit back 5014 02:51:07,840 --> 02:51:11,600 relax and enjoy the show 5015 02:51:11,760 --> 02:51:15,840 so to begin it's kind of similar to the 5016 02:51:13,920 --> 02:51:18,880 last video on reading files 5017 02:51:15,840 --> 02:51:21,359 with open within the open function list 5018 02:51:18,880 --> 02:51:22,720 the file name or the file path so what 5019 02:51:21,359 --> 02:51:26,399 do you want to call this file 5020 02:51:22,720 --> 02:51:28,800 i will call this file test dot txt 5021 02:51:26,399 --> 02:51:29,920 now normally with the open function 5022 02:51:28,800 --> 02:51:31,520 there is a mode 5023 02:51:29,920 --> 02:51:33,520 and you can actually change that there's 5024 02:51:31,520 --> 02:51:36,560 a second argument that you can pass in 5025 02:51:33,520 --> 02:51:38,720 by default this is r for read but if you 5026 02:51:36,560 --> 02:51:41,520 want to write a file this would be 5027 02:51:38,720 --> 02:51:42,279 w so now we're going to write a file 5028 02:51:41,520 --> 02:51:46,080 called 5029 02:51:42,279 --> 02:51:49,600 test.txt so to finish with open 5030 02:51:46,080 --> 02:51:52,560 as file what are we going to do file 5031 02:51:49,600 --> 02:51:54,240 dot write and within the write function 5032 02:51:52,560 --> 02:51:57,279 we can write some text 5033 02:51:54,240 --> 02:51:58,880 so let's create a text variable and we 5034 02:51:57,279 --> 02:52:03,040 will add a string 5035 02:51:58,880 --> 02:52:06,800 so let's say text equals 5036 02:52:03,040 --> 02:52:07,200 yo and if you need to go down to a new 5037 02:52:06,800 --> 02:52:09,600 line 5038 02:52:07,200 --> 02:52:11,200 be sure to use the new line character so 5039 02:52:09,600 --> 02:52:13,520 after this new line character 5040 02:52:11,200 --> 02:52:15,200 the text that comes after is going to be 5041 02:52:13,520 --> 02:52:16,000 on the next line as if we're hitting 5042 02:52:15,200 --> 02:52:19,439 enter 5043 02:52:16,000 --> 02:52:22,640 so yo this is some 5044 02:52:19,439 --> 02:52:26,319 text have 5045 02:52:22,640 --> 02:52:29,120 a good one 5046 02:52:26,319 --> 02:52:29,920 all right so after i run this program we 5047 02:52:29,120 --> 02:52:32,640 should have 5048 02:52:29,920 --> 02:52:34,560 a file a text file within our project 5049 02:52:32,640 --> 02:52:37,439 folder that appears 5050 02:52:34,560 --> 02:52:38,399 and it is right here and it says yo this 5051 02:52:37,439 --> 02:52:41,439 is some text 5052 02:52:38,399 --> 02:52:42,000 have a good one now this is in write 5053 02:52:41,439 --> 02:52:44,399 mode 5054 02:52:42,000 --> 02:52:45,279 if this text was something else it will 5055 02:52:44,399 --> 02:52:47,520 actually 5056 02:52:45,279 --> 02:52:49,760 overwrite your current file so if i were 5057 02:52:47,520 --> 02:52:53,040 to change this to 5058 02:52:49,760 --> 02:52:56,640 uh oh this text has 5059 02:52:53,040 --> 02:52:59,200 been over written well then 5060 02:52:56,640 --> 02:52:59,760 this will overwrite the text that we 5061 02:52:59,200 --> 02:53:02,560 have 5062 02:52:59,760 --> 02:53:04,640 uh oh this text has been overwritten so 5063 02:53:02,560 --> 02:53:05,760 you can actually append a file by 5064 02:53:04,640 --> 02:53:08,319 changing the mode 5065 02:53:05,760 --> 02:53:09,120 to a for append so let me change this 5066 02:53:08,319 --> 02:53:12,000 back 5067 02:53:09,120 --> 02:53:13,040 we will rewrite this this will save over 5068 02:53:12,000 --> 02:53:15,600 our current file 5069 02:53:13,040 --> 02:53:17,439 now we can append some text so let's add 5070 02:53:15,600 --> 02:53:20,560 maybe a new line 5071 02:53:17,439 --> 02:53:25,120 uh have a nice 5072 02:53:20,560 --> 02:53:26,800 day see ya so i would like to append 5073 02:53:25,120 --> 02:53:28,399 some text to this file i'm going to 5074 02:53:26,800 --> 02:53:31,120 change the mode to a 5075 02:53:28,399 --> 02:53:33,279 for append and we can now append some 5076 02:53:31,120 --> 02:53:35,439 text onto the end of this file 5077 02:53:33,279 --> 02:53:37,520 well everybody that's how to write to a 5078 02:53:35,439 --> 02:53:38,800 file and or append a file if you would 5079 02:53:37,520 --> 02:53:40,560 like a copy of this code 5080 02:53:38,800 --> 02:53:42,960 i will post this in the comment section 5081 02:53:40,560 --> 02:53:46,080 down below but yeah that's how to 5082 02:53:42,960 --> 02:53:48,399 write to and or append a file in python 5083 02:53:46,080 --> 02:53:49,359 hey what's going on everybody it's bro 5084 02:53:48,399 --> 02:53:50,720 hope you're doing well 5085 02:53:49,359 --> 02:53:52,560 and in this video we're going to be 5086 02:53:50,720 --> 02:53:57,200 copying files in python so 5087 02:53:52,560 --> 02:53:59,200 sit back relax and enjoy the show 5088 02:53:57,200 --> 02:54:01,520 welcome back to another video regarding 5089 02:53:59,200 --> 02:54:02,880 doing stuff with files using python and 5090 02:54:01,520 --> 02:54:05,439 in this video we're going to be 5091 02:54:02,880 --> 02:54:06,240 copying some files and in order to do so 5092 02:54:05,439 --> 02:54:08,640 i recommend 5093 02:54:06,240 --> 02:54:10,000 importing the shuttle module there's 5094 02:54:08,640 --> 02:54:11,920 other ways of doing this too 5095 02:54:10,000 --> 02:54:13,040 but i tend to like using the shuttle 5096 02:54:11,920 --> 02:54:15,840 module now 5097 02:54:13,040 --> 02:54:16,479 with this module there are three basic 5098 02:54:15,840 --> 02:54:19,279 functions 5099 02:54:16,479 --> 02:54:20,800 to copy a file starting at the top they 5100 02:54:19,279 --> 02:54:24,000 are copy file 5101 02:54:20,800 --> 02:54:25,120 copy and copy to so copy file is what 5102 02:54:24,000 --> 02:54:27,760 we'll be working with 5103 02:54:25,120 --> 02:54:28,479 this will copy the contents of a file 5104 02:54:27,760 --> 02:54:31,040 copy 5105 02:54:28,479 --> 02:54:32,000 will do everything that copy file does 5106 02:54:31,040 --> 02:54:34,800 plus copy 5107 02:54:32,000 --> 02:54:36,160 the permissions mode and the destination 5108 02:54:34,800 --> 02:54:38,560 can be a directory 5109 02:54:36,160 --> 02:54:39,200 and lastly copy 2 this will do 5110 02:54:38,560 --> 02:54:41,600 everything 5111 02:54:39,200 --> 02:54:42,399 that copy does plus it copies the 5112 02:54:41,600 --> 02:54:44,479 metadata 5113 02:54:42,399 --> 02:54:46,479 of that file including the files 5114 02:54:44,479 --> 02:54:48,479 creation and modification times 5115 02:54:46,479 --> 02:54:49,760 so depending on the project or the 5116 02:54:48,479 --> 02:54:52,479 program you're working with 5117 02:54:49,760 --> 02:54:54,160 you may need to use copy or copy too but 5118 02:54:52,479 --> 02:54:56,800 in this video we're going to be using 5119 02:54:54,160 --> 02:54:57,439 copy file just to copy the contents of a 5120 02:54:56,800 --> 02:55:00,080 file 5121 02:54:57,439 --> 02:55:01,680 now after you import the shuttle module 5122 02:55:00,080 --> 02:55:02,640 this is all you need to do to copy a 5123 02:55:01,680 --> 02:55:05,279 file 5124 02:55:02,640 --> 02:55:07,680 shuttle dot then use the appropriate 5125 02:55:05,279 --> 02:55:10,800 function that you need to copy your file 5126 02:55:07,680 --> 02:55:12,720 we're going to be using copy file copy 5127 02:55:10,800 --> 02:55:13,840 file and within this function there are 5128 02:55:12,720 --> 02:55:17,439 two arguments 5129 02:55:13,840 --> 02:55:19,359 a source and a destination so within my 5130 02:55:17,439 --> 02:55:22,720 project folder i have a plain 5131 02:55:19,359 --> 02:55:25,920 text file called test test.txt 5132 02:55:22,720 --> 02:55:27,920 all this says is yo this is some text 5133 02:55:25,920 --> 02:55:29,439 see ya what i would like to do is copy 5134 02:55:27,920 --> 02:55:32,000 this file because it's cool 5135 02:55:29,439 --> 02:55:34,800 now since this file is within my project 5136 02:55:32,000 --> 02:55:36,800 folder i only have to list the file name 5137 02:55:34,800 --> 02:55:38,720 otherwise if this is someplace else i 5138 02:55:36,800 --> 02:55:42,560 would list the file path here 5139 02:55:38,720 --> 02:55:45,600 so this file is test.txt 5140 02:55:42,560 --> 02:55:46,640 this is the source file and now i need a 5141 02:55:45,600 --> 02:55:48,720 destination 5142 02:55:46,640 --> 02:55:50,560 so the second argument will be separated 5143 02:55:48,720 --> 02:55:53,200 with the comma i'm going to list 5144 02:55:50,560 --> 02:55:53,840 a destination so you can actually rename 5145 02:55:53,200 --> 02:55:57,040 this to 5146 02:55:53,840 --> 02:56:00,399 i would like to name this as copy.txt 5147 02:55:57,040 --> 02:56:02,920 so the destination is my project folder 5148 02:56:00,399 --> 02:56:04,160 so when i create a copy of this file 5149 02:56:02,920 --> 02:56:06,080 test.txt 5150 02:56:04,160 --> 02:56:07,680 this copy will appear in my project 5151 02:56:06,080 --> 02:56:11,040 folder and say the same thing 5152 02:56:07,680 --> 02:56:13,680 so after running this we now have a 5153 02:56:11,040 --> 02:56:14,080 file named copy.txt that says the same 5154 02:56:13,680 --> 02:56:16,640 thing 5155 02:56:14,080 --> 02:56:17,760 as my test file and remember what i said 5156 02:56:16,640 --> 02:56:19,920 you can copy 5157 02:56:17,760 --> 02:56:22,000 this file to a different location on 5158 02:56:19,920 --> 02:56:22,560 your computer so you could list the file 5159 02:56:22,000 --> 02:56:25,359 path 5160 02:56:22,560 --> 02:56:26,399 so this file path would go to my desktop 5161 02:56:25,359 --> 02:56:28,479 and then when i run this 5162 02:56:26,399 --> 02:56:30,640 this will copy my original file my 5163 02:56:28,479 --> 02:56:32,800 source and then copy it to 5164 02:56:30,640 --> 02:56:34,800 wherever my destination is oh and i 5165 02:56:32,800 --> 02:56:35,920 almost forgot so if you need to use copy 5166 02:56:34,800 --> 02:56:38,479 or copy 2 5167 02:56:35,920 --> 02:56:40,080 the arguments are exactly the same so if 5168 02:56:38,479 --> 02:56:42,399 you needed to use copy 5169 02:56:40,080 --> 02:56:43,439 this would be a shuttle dot copy instead 5170 02:56:42,399 --> 02:56:46,640 of copy file 5171 02:56:43,439 --> 02:56:48,880 then copy 2 is shuttle dot copy 2. 5172 02:56:46,640 --> 02:56:50,720 the arguments are exactly the same but 5173 02:56:48,880 --> 02:56:52,240 each of these functions will copy 5174 02:56:50,720 --> 02:56:54,640 different things depending on what you 5175 02:56:52,240 --> 02:56:56,720 need for your own program or project 5176 02:56:54,640 --> 02:56:58,160 all right everybody so that's the basics 5177 02:56:56,720 --> 02:57:00,399 of copying files and 5178 02:56:58,160 --> 02:57:01,520 python i will post this in the comment 5179 02:57:00,399 --> 02:57:04,160 section down below 5180 02:57:01,520 --> 02:57:05,760 but yeah that is how to copy files using 5181 02:57:04,160 --> 02:57:08,080 python 5182 02:57:05,760 --> 02:57:09,840 hey what's going on everybody it's bro 5183 02:57:08,080 --> 02:57:11,520 hope you're doing well and in this video 5184 02:57:09,840 --> 02:57:15,120 i'm going to explain how we can move 5185 02:57:11,520 --> 02:57:18,080 files using python so sit back relax 5186 02:57:15,120 --> 02:57:18,080 and enjoy the show 5187 02:57:18,160 --> 02:57:22,160 welcome back everybody to another video 5188 02:57:20,479 --> 02:57:23,920 regarding that thing that we do called 5189 02:57:22,160 --> 02:57:26,319 programming and in this video we're 5190 02:57:23,920 --> 02:57:28,319 going to be moving files using python 5191 02:57:26,319 --> 02:57:30,399 and in order to do so i recommend 5192 02:57:28,319 --> 02:57:32,000 importing the os module 5193 02:57:30,399 --> 02:57:34,319 this is included with the standard 5194 02:57:32,000 --> 02:57:35,040 python library all you need to do is 5195 02:57:34,319 --> 02:57:37,600 import it 5196 02:57:35,040 --> 02:57:38,880 and now let's create two variables a 5197 02:57:37,600 --> 02:57:41,120 variable called source 5198 02:57:38,880 --> 02:57:42,880 that will hold the location of where our 5199 02:57:41,120 --> 02:57:45,760 source file is located 5200 02:57:42,880 --> 02:57:46,880 as well as a variable called destination 5201 02:57:45,760 --> 02:57:49,760 to function as well 5202 02:57:46,880 --> 02:57:50,000 the destination now for this video i'm 5203 02:57:49,760 --> 02:57:52,720 going 5204 02:57:50,000 --> 02:57:53,920 to move a file within my project folder 5205 02:57:52,720 --> 02:57:56,640 to my desktop 5206 02:57:53,920 --> 02:57:57,359 so to do so i'm going to create a new 5207 02:57:56,640 --> 02:58:00,160 file 5208 02:57:57,359 --> 02:58:02,399 i will name this test.txt but take the 5209 02:58:00,160 --> 02:58:05,120 liberty to name this whatever you want 5210 02:58:02,399 --> 02:58:05,600 so i'm going to list the location of 5211 02:58:05,120 --> 02:58:08,160 where 5212 02:58:05,600 --> 02:58:10,160 my test file is located since this is 5213 02:58:08,160 --> 02:58:12,399 within my project folder i only need 5214 02:58:10,160 --> 02:58:14,080 the file name if this was someplace else 5215 02:58:12,399 --> 02:58:15,200 on your computer you would probably need 5216 02:58:14,080 --> 02:58:16,960 the file path 5217 02:58:15,200 --> 02:58:19,279 and now where would i like to move this 5218 02:58:16,960 --> 02:58:20,399 file to i would like to move this file 5219 02:58:19,279 --> 02:58:22,479 to my desktop 5220 02:58:20,399 --> 02:58:24,560 so i just need the file path so i'm 5221 02:58:22,479 --> 02:58:27,120 going to go to my desktop 5222 02:58:24,560 --> 02:58:28,080 right click go to properties i'm going 5223 02:58:27,120 --> 02:58:30,560 to copy 5224 02:58:28,080 --> 02:58:31,760 this location and paste it within my 5225 02:58:30,560 --> 02:58:34,960 destination 5226 02:58:31,760 --> 02:58:38,319 and i will add slash desktop 5227 02:58:34,960 --> 02:58:40,000 slash test.txt you can also rename this 5228 02:58:38,319 --> 02:58:40,560 too if you want i will just keep it the 5229 02:58:40,000 --> 02:58:43,040 same 5230 02:58:40,560 --> 02:58:45,040 now if you have single backslashes 5231 02:58:43,040 --> 02:58:47,920 you'll likely need to change these 5232 02:58:45,040 --> 02:58:49,279 to double backslashes because that is 5233 02:58:47,920 --> 02:58:52,080 the escape sequence 5234 02:58:49,279 --> 02:58:52,880 to print a backslash within a string so 5235 02:58:52,080 --> 02:58:55,359 we have a 5236 02:58:52,880 --> 02:58:56,080 source location as well as a destination 5237 02:58:55,359 --> 02:58:58,399 location 5238 02:58:56,080 --> 02:59:00,880 now we can begin i would recommend 5239 02:58:58,399 --> 02:59:01,680 writing our code within a try and accept 5240 02:59:00,880 --> 02:59:03,920 block to 5241 02:59:01,680 --> 02:59:05,120 handle any exceptions so i would like to 5242 02:59:03,920 --> 02:59:08,000 handle any file 5243 02:59:05,120 --> 02:59:10,080 not found error exceptions just in case 5244 02:59:08,000 --> 02:59:12,880 our program cannot locate our source 5245 02:59:10,080 --> 02:59:13,760 file so let's print a message if that is 5246 02:59:12,880 --> 02:59:17,600 the case 5247 02:59:13,760 --> 02:59:20,080 perhaps source plus was not 5248 02:59:17,600 --> 02:59:22,319 found now within our try block let's 5249 02:59:20,080 --> 02:59:23,600 check to see if there is already a file 5250 02:59:22,319 --> 02:59:25,680 at this destination 5251 02:59:23,600 --> 02:59:27,600 because i would not like to save over it 5252 02:59:25,680 --> 02:59:30,800 if there's already a file there 5253 02:59:27,600 --> 02:59:31,920 so let's do some basic file detection if 5254 02:59:30,800 --> 02:59:35,200 os 5255 02:59:31,920 --> 02:59:37,760 dot path dot exists 5256 02:59:35,200 --> 02:59:39,200 and i will pass in my destination as an 5257 02:59:37,760 --> 02:59:42,160 argument to the exists 5258 02:59:39,200 --> 02:59:44,479 function so if there is already a file 5259 02:59:42,160 --> 02:59:47,760 here i should probably let the user know 5260 02:59:44,479 --> 02:59:51,120 print there is already a 5261 02:59:47,760 --> 02:59:53,600 file there else 5262 02:59:51,120 --> 02:59:56,080 we can replace our files honestly if you 5263 02:59:53,600 --> 02:59:59,840 do not care about saving over any files 5264 02:59:56,080 --> 03:00:02,880 you can omit this part so else os 5265 02:59:59,840 --> 03:00:06,160 dot replace and we will pass 5266 03:00:02,880 --> 03:00:09,439 in our source and our destination 5267 03:00:06,160 --> 03:00:11,439 as arguments and let's print a message 5268 03:00:09,439 --> 03:00:11,760 to the console window to let the user 5269 03:00:11,439 --> 03:00:16,319 know 5270 03:00:11,760 --> 03:00:19,200 that source plus was 5271 03:00:16,319 --> 03:00:21,120 moved all right let's try this oh i 5272 03:00:19,200 --> 03:00:24,240 accidentally deleted my 5273 03:00:21,120 --> 03:00:27,279 file test.txt all right 5274 03:00:24,240 --> 03:00:31,000 now this should disappear in three 5275 03:00:27,279 --> 03:00:33,840 two one and it's gone 5276 03:00:31,000 --> 03:00:35,040 test.txt was moved and it should now be 5277 03:00:33,840 --> 03:00:37,600 on my desktop 5278 03:00:35,040 --> 03:00:39,120 which it is now you can also use this to 5279 03:00:37,600 --> 03:00:42,160 move a directory as well 5280 03:00:39,120 --> 03:00:44,160 i'm going to create a new directory i 5281 03:00:42,160 --> 03:00:47,200 will name this folder 5282 03:00:44,160 --> 03:00:50,840 and i will change source to folder 5283 03:00:47,200 --> 03:00:53,520 and the destination from test.txt to 5284 03:00:50,840 --> 03:00:55,600 folder and now this should move my 5285 03:00:53,520 --> 03:00:58,960 folder 5286 03:00:55,600 --> 03:00:58,960 there is already a file there 5287 03:00:59,680 --> 03:01:05,439 oh let's delete that okay 5288 03:01:02,880 --> 03:01:06,000 folder was moved all right everybody so 5289 03:01:05,439 --> 03:01:08,880 that is how 5290 03:01:06,000 --> 03:01:09,520 to move a file and or directory using 5291 03:01:08,880 --> 03:01:11,920 python 5292 03:01:09,520 --> 03:01:13,600 if you would like a copy of this code i 5293 03:01:11,920 --> 03:01:14,640 will post this in the comment section 5294 03:01:13,600 --> 03:01:17,279 down below 5295 03:01:14,640 --> 03:01:18,240 but yeah that is how to move a file and 5296 03:01:17,279 --> 03:01:21,279 or directory 5297 03:01:18,240 --> 03:01:21,920 using python hey what's going on 5298 03:01:21,279 --> 03:01:23,920 everybody 5299 03:01:21,920 --> 03:01:25,279 it's bro hope you're doing well and in 5300 03:01:23,920 --> 03:01:27,760 this video i'm going to teach you guys 5301 03:01:25,279 --> 03:01:32,399 how we can delete files using python so 5302 03:01:27,760 --> 03:01:32,399 sit back relax and enjoy the show 5303 03:01:32,720 --> 03:01:37,040 all right everybody so in this video 5304 03:01:35,120 --> 03:01:39,359 we're going to be deleting files using 5305 03:01:37,040 --> 03:01:41,359 python and in order to do so i recommend 5306 03:01:39,359 --> 03:01:42,800 importing the os module 5307 03:01:41,359 --> 03:01:44,800 this is already included with the 5308 03:01:42,800 --> 03:01:45,120 standard python library all you need to 5309 03:01:44,800 --> 03:01:47,600 do 5310 03:01:45,120 --> 03:01:48,240 is import it and now we need a file to 5311 03:01:47,600 --> 03:01:49,760 work with 5312 03:01:48,240 --> 03:01:51,359 so let's right click on our projects 5313 03:01:49,760 --> 03:01:54,399 folder and go to new 5314 03:01:51,359 --> 03:01:56,240 file i will name this test.txt 5315 03:01:54,399 --> 03:01:58,000 and we now have a file that we can work 5316 03:01:56,240 --> 03:02:00,080 with but you know what i would like to 5317 03:01:58,000 --> 03:02:02,160 delete this file because it's lame 5318 03:02:00,080 --> 03:02:03,680 so let's do that in order to delete this 5319 03:02:02,160 --> 03:02:05,840 file let's type os 5320 03:02:03,680 --> 03:02:07,120 dot remove and we're going to list the 5321 03:02:05,840 --> 03:02:09,279 file path here 5322 03:02:07,120 --> 03:02:10,960 since this file is within my project 5323 03:02:09,279 --> 03:02:14,319 folder i only need to list 5324 03:02:10,960 --> 03:02:16,160 the file path test.txt if this was 5325 03:02:14,319 --> 03:02:17,680 someplace else on my computer i would 5326 03:02:16,160 --> 03:02:19,920 probably need the file path 5327 03:02:17,680 --> 03:02:21,680 like that but since this file is within 5328 03:02:19,920 --> 03:02:23,680 my project folder i only need 5329 03:02:21,680 --> 03:02:25,680 the file name another thing that you can 5330 03:02:23,680 --> 03:02:27,600 do too at least what i like to do 5331 03:02:25,680 --> 03:02:29,920 is that i like to pass in a variable 5332 03:02:27,600 --> 03:02:31,200 called path i will make this a variable 5333 03:02:29,920 --> 03:02:33,920 and assign it a 5334 03:02:31,200 --> 03:02:34,960 string value of whatever the path to my 5335 03:02:33,920 --> 03:02:37,359 file is 5336 03:02:34,960 --> 03:02:38,200 so i will assign path the name to my 5337 03:02:37,359 --> 03:02:40,960 file 5338 03:02:38,200 --> 03:02:42,200 test.txt now when i run this this will 5339 03:02:40,960 --> 03:02:44,399 delete my file 5340 03:02:42,200 --> 03:02:45,359 test.txt and you can see that it's no 5341 03:02:44,399 --> 03:02:47,760 longer there 5342 03:02:45,359 --> 03:02:49,120 now what if we attempt to delete a file 5343 03:02:47,760 --> 03:02:51,600 that doesn't exist 5344 03:02:49,120 --> 03:02:52,160 well we'll encounter an exception so if 5345 03:02:51,600 --> 03:02:53,439 you want 5346 03:02:52,160 --> 03:02:56,160 this is optional we can do some 5347 03:02:53,439 --> 03:02:58,640 exception handling so let's write 5348 03:02:56,160 --> 03:02:59,600 the remove function within a try block 5349 03:02:58,640 --> 03:03:02,479 and we will catch 5350 03:02:59,600 --> 03:03:04,080 this exception file not found error and 5351 03:03:02,479 --> 03:03:08,080 let's print something 5352 03:03:04,080 --> 03:03:10,479 print that file was not found 5353 03:03:08,080 --> 03:03:11,439 now if you attempt to delete a file that 5354 03:03:10,479 --> 03:03:13,680 doesn't exist 5355 03:03:11,439 --> 03:03:15,279 your program will not be interrupted and 5356 03:03:13,680 --> 03:03:18,000 instead you'll print something 5357 03:03:15,279 --> 03:03:18,560 that file was not found now this 5358 03:03:18,000 --> 03:03:21,120 function 5359 03:03:18,560 --> 03:03:22,080 does not remove empty folders this is 5360 03:03:21,120 --> 03:03:23,920 how to do so 5361 03:03:22,080 --> 03:03:25,359 and actually let's test that so let's 5362 03:03:23,920 --> 03:03:29,520 create a new directory 5363 03:03:25,359 --> 03:03:31,680 i'll call this empty underscore folder 5364 03:03:29,520 --> 03:03:32,640 now i will attempt to delete this empty 5365 03:03:31,680 --> 03:03:35,920 folder 5366 03:03:32,640 --> 03:03:35,920 empty folder 5367 03:03:36,240 --> 03:03:40,160 we encountered a permission error access 5368 03:03:38,560 --> 03:03:41,439 is denied so let's create an accept 5369 03:03:40,160 --> 03:03:44,560 block for that 5370 03:03:41,439 --> 03:03:48,000 accept permission error 5371 03:03:44,560 --> 03:03:51,359 print i don't know you do 5372 03:03:48,000 --> 03:03:55,200 not have permission to 5373 03:03:51,359 --> 03:03:57,200 delete that right to delete 5374 03:03:55,200 --> 03:03:58,800 an empty folder there is a different 5375 03:03:57,200 --> 03:04:02,160 function 5376 03:03:58,800 --> 03:04:04,080 os dot rmdir 5377 03:04:02,160 --> 03:04:05,200 short for remove directory we're going 5378 03:04:04,080 --> 03:04:07,920 to list the file path 5379 03:04:05,200 --> 03:04:09,200 and pass that in as an argument and 5380 03:04:07,920 --> 03:04:10,479 let's create an else block as well 5381 03:04:09,200 --> 03:04:12,800 because we can 5382 03:04:10,479 --> 03:04:14,160 else if there are no exceptions let's 5383 03:04:12,800 --> 03:04:17,279 print 5384 03:04:14,160 --> 03:04:20,960 path plus was 5385 03:04:17,279 --> 03:04:23,279 deleted all right so rmdir 5386 03:04:20,960 --> 03:04:24,640 remove directory will delete an empty 5387 03:04:23,279 --> 03:04:27,279 directory 5388 03:04:24,640 --> 03:04:27,840 empty folder was deleted now this 5389 03:04:27,279 --> 03:04:30,080 function 5390 03:04:27,840 --> 03:04:31,760 will not delete a folder that contains 5391 03:04:30,080 --> 03:04:34,080 files and let's try that 5392 03:04:31,760 --> 03:04:36,720 so let's create a new directory i'll 5393 03:04:34,080 --> 03:04:38,640 just call this folder 5394 03:04:36,720 --> 03:04:40,439 and within this folder i will create a 5395 03:04:38,640 --> 03:04:43,040 new file 5396 03:04:40,439 --> 03:04:45,120 test.txt 5397 03:04:43,040 --> 03:04:46,960 all right let's attempt to delete this 5398 03:04:45,120 --> 03:04:48,000 folder i will probably need to change 5399 03:04:46,960 --> 03:04:51,279 the file name 5400 03:04:48,000 --> 03:04:52,160 let's change that to folder and we 5401 03:04:51,279 --> 03:04:55,439 encountered an 5402 03:04:52,160 --> 03:04:56,960 os error that directory is not empty so 5403 03:04:55,439 --> 03:04:58,640 let's handle this exception we don't 5404 03:04:56,960 --> 03:05:03,439 need to but i like to 5405 03:04:58,640 --> 03:05:06,640 accept os air print 5406 03:05:03,439 --> 03:05:10,479 you cannot delete that 5407 03:05:06,640 --> 03:05:13,200 using that function 5408 03:05:10,479 --> 03:05:13,920 okay to delete a folder that contains 5409 03:05:13,200 --> 03:05:17,359 files 5410 03:05:13,920 --> 03:05:19,600 we'll need the shuttle module import 5411 03:05:17,359 --> 03:05:20,800 shuttle and the function that we're 5412 03:05:19,600 --> 03:05:24,000 looking for is 5413 03:05:20,800 --> 03:05:26,080 shuttle dot rm tree 5414 03:05:24,000 --> 03:05:27,359 short for remove tree and we will pass 5415 03:05:26,080 --> 03:05:29,200 in our path 5416 03:05:27,359 --> 03:05:30,479 now be careful with this function it is 5417 03:05:29,200 --> 03:05:32,560 considered dangerous 5418 03:05:30,479 --> 03:05:34,560 because it will delete a directory and 5419 03:05:32,560 --> 03:05:36,720 all files contained within 5420 03:05:34,560 --> 03:05:38,399 so now we can delete a folder that 5421 03:05:36,720 --> 03:05:41,359 contains files 5422 03:05:38,399 --> 03:05:41,760 folder was deleted in conclusion these 5423 03:05:41,359 --> 03:05:44,720 are 5424 03:05:41,760 --> 03:05:45,920 three basic functions to delete a file 5425 03:05:44,720 --> 03:05:49,840 or directory they 5426 03:05:45,920 --> 03:05:52,000 are remove which deletes a file rmdir 5427 03:05:49,840 --> 03:05:52,880 which is an abbreviation for remove 5428 03:05:52,000 --> 03:05:55,439 directory 5429 03:05:52,880 --> 03:05:56,160 which will delete an empty directory and 5430 03:05:55,439 --> 03:05:58,800 lastly 5431 03:05:56,160 --> 03:05:59,760 rmtree which is an abbreviation for 5432 03:05:58,800 --> 03:06:01,920 remove tree 5433 03:05:59,760 --> 03:06:04,080 and this is of the shuttle module and 5434 03:06:01,920 --> 03:06:04,960 this will delete a directory containing 5435 03:06:04,080 --> 03:06:06,960 files 5436 03:06:04,960 --> 03:06:08,880 so if you would like a copy of all this 5437 03:06:06,960 --> 03:06:10,080 code i will post this in the comments 5438 03:06:08,880 --> 03:06:12,560 section down below 5439 03:06:10,080 --> 03:06:13,600 but yeah that is how to delete files and 5440 03:06:12,560 --> 03:06:16,800 or directories 5441 03:06:13,600 --> 03:06:18,399 using python yo what's going on 5442 03:06:16,800 --> 03:06:18,800 everybody it's you bro hope you're doing 5443 03:06:18,399 --> 03:06:20,319 well 5444 03:06:18,800 --> 03:06:22,800 and in today's video i'm going to be 5445 03:06:20,319 --> 03:06:27,279 discussing modules in python so 5446 03:06:22,800 --> 03:06:27,279 sit back relax and enjoy the show 5447 03:06:27,439 --> 03:06:31,279 all right well welcome back we're 5448 03:06:29,680 --> 03:06:33,920 talking about modules today 5449 03:06:31,279 --> 03:06:34,399 a module is a file containing python 5450 03:06:33,920 --> 03:06:37,439 code 5451 03:06:34,399 --> 03:06:39,760 it may contain functions classes etc 5452 03:06:37,439 --> 03:06:42,000 it's used with modular programming which 5453 03:06:39,760 --> 03:06:44,240 is this concept of separating a program 5454 03:06:42,000 --> 03:06:46,560 into useful different parts we're 5455 03:06:44,240 --> 03:06:48,880 currently working within our main module 5456 03:06:46,560 --> 03:06:50,720 and mine is named hello youtube this is 5457 03:06:48,880 --> 03:06:52,640 how to create a separate module 5458 03:06:50,720 --> 03:06:54,399 right click on your project folder go to 5459 03:06:52,640 --> 03:06:56,560 new python file 5460 03:06:54,399 --> 03:06:58,000 i think i'll create a module filled with 5461 03:06:56,560 --> 03:07:00,479 maybe message functions 5462 03:06:58,000 --> 03:07:01,680 so i'll call this messages and click 5463 03:07:00,479 --> 03:07:03,600 python file 5464 03:07:01,680 --> 03:07:05,600 all right we have two tabs one for our 5465 03:07:03,600 --> 03:07:06,880 main module mine is called hello youtube 5466 03:07:05,600 --> 03:07:09,040 and a separate module 5467 03:07:06,880 --> 03:07:10,560 for messages and i can write whatever i 5468 03:07:09,040 --> 03:07:12,880 want within here since we're using 5469 03:07:10,560 --> 03:07:15,040 modular programming i'll place a bunch 5470 03:07:12,880 --> 03:07:17,040 of useful functions within this module 5471 03:07:15,040 --> 03:07:18,560 and i'll import them to the main module 5472 03:07:17,040 --> 03:07:21,439 that we're currently working with 5473 03:07:18,560 --> 03:07:24,160 let's define a function called hello all 5474 03:07:21,439 --> 03:07:27,520 this will do is print 5475 03:07:24,160 --> 03:07:30,000 hello have a nice 5476 03:07:27,520 --> 03:07:30,800 day and i'll create a second function 5477 03:07:30,000 --> 03:07:33,840 for good measure 5478 03:07:30,800 --> 03:07:33,840 let's call this pie 5479 03:07:35,200 --> 03:07:42,240 let's say bye have 5480 03:07:38,880 --> 03:07:44,000 a wonderful time 5481 03:07:42,240 --> 03:07:46,000 now if we would like access to this 5482 03:07:44,000 --> 03:07:47,840 module we need to import it 5483 03:07:46,000 --> 03:07:49,600 i will head back to my main module and 5484 03:07:47,840 --> 03:07:52,080 at the top write this 5485 03:07:49,600 --> 03:07:54,160 import then the name of the module and 5486 03:07:52,080 --> 03:07:54,800 for this example we used the name of 5487 03:07:54,160 --> 03:07:57,359 messages 5488 03:07:54,800 --> 03:07:58,800 for our separate module and to use a 5489 03:07:57,359 --> 03:08:01,439 function from that module 5490 03:07:58,800 --> 03:08:02,640 type the name of the module dot and then 5491 03:08:01,439 --> 03:08:05,520 the name of the function 5492 03:08:02,640 --> 03:08:06,000 so messages.hello will call the hello 5493 03:08:05,520 --> 03:08:08,720 function 5494 03:08:06,000 --> 03:08:09,920 found within the messages module and i 5495 03:08:08,720 --> 03:08:13,600 can also call the 5496 03:08:09,920 --> 03:08:16,080 buy function as well messages dot bye 5497 03:08:13,600 --> 03:08:16,720 and that will print bye have a wonderful 5498 03:08:16,080 --> 03:08:18,560 time 5499 03:08:16,720 --> 03:08:20,479 no it can be somewhat tedious to have to 5500 03:08:18,560 --> 03:08:22,080 write the name the module followed by 5501 03:08:20,479 --> 03:08:22,960 the function name what we could do to 5502 03:08:22,080 --> 03:08:25,680 shorten things up 5503 03:08:22,960 --> 03:08:27,200 is to give our messages module an alias 5504 03:08:25,680 --> 03:08:30,399 also known as a nickname 5505 03:08:27,200 --> 03:08:32,640 so at the top write import messages 5506 03:08:30,399 --> 03:08:33,680 as and what sort of nickname should we 5507 03:08:32,640 --> 03:08:37,120 give messages 5508 03:08:33,680 --> 03:08:38,319 let's say msg for short so we'll replace 5509 03:08:37,120 --> 03:08:41,520 messages with 5510 03:08:38,319 --> 03:08:42,960 msg msg hello and msg buy 5511 03:08:41,520 --> 03:08:45,200 and this would work the same and it 5512 03:08:42,960 --> 03:08:46,240 involves less typing there's also an 5513 03:08:45,200 --> 03:08:47,920 additional way of 5514 03:08:46,240 --> 03:08:49,920 importing a module and it goes a little 5515 03:08:47,920 --> 03:08:52,479 something like this from 5516 03:08:49,920 --> 03:08:54,319 the name of the module import and then 5517 03:08:52,479 --> 03:08:55,359 list all of the functions or classes you 5518 03:08:54,319 --> 03:08:58,399 would like to import 5519 03:08:55,359 --> 03:09:00,319 i would like to import hello comma buy 5520 03:08:58,399 --> 03:09:02,160 and we no longer need the module name 5521 03:09:00,319 --> 03:09:04,160 before calling one of these functions 5522 03:09:02,160 --> 03:09:06,000 i can simply just call hello and vi 5523 03:09:04,160 --> 03:09:06,800 directly another way in which this could 5524 03:09:06,000 --> 03:09:09,600 be written is 5525 03:09:06,800 --> 03:09:10,000 from messages import asterisk that means 5526 03:09:09,600 --> 03:09:11,680 to 5527 03:09:10,000 --> 03:09:13,359 import all and i actually would not 5528 03:09:11,680 --> 03:09:15,520 recommend using this if you're working 5529 03:09:13,359 --> 03:09:16,000 on a large program or something that 5530 03:09:15,520 --> 03:09:18,240 contains 5531 03:09:16,000 --> 03:09:19,520 many modules because you can run into a 5532 03:09:18,240 --> 03:09:21,279 naming conflict 5533 03:09:19,520 --> 03:09:22,960 because some of these modules may have 5534 03:09:21,279 --> 03:09:24,000 the same named function names or 5535 03:09:22,960 --> 03:09:25,520 variables 5536 03:09:24,000 --> 03:09:27,279 so if you're working on a small program 5537 03:09:25,520 --> 03:09:28,800 i really don't think it's a big deal but 5538 03:09:27,279 --> 03:09:31,200 if you have multiple modules i would 5539 03:09:28,800 --> 03:09:33,439 actually refrain from using import alt 5540 03:09:31,200 --> 03:09:35,200 it's much safer to write the name of the 5541 03:09:33,439 --> 03:09:36,560 module followed by the name of the 5542 03:09:35,200 --> 03:09:38,399 function that you want to use 5543 03:09:36,560 --> 03:09:40,000 now another cool thing is that python 5544 03:09:38,399 --> 03:09:41,520 has a bunch of pre-written modules that 5545 03:09:40,000 --> 03:09:44,000 you have access to if you would like a 5546 03:09:41,520 --> 03:09:46,720 comprehensive list of them type help 5547 03:09:44,000 --> 03:09:48,720 and pass in the word modules and within 5548 03:09:46,720 --> 03:09:50,399 my console window this will populate a 5549 03:09:48,720 --> 03:09:51,279 listing of all the modules available to 5550 03:09:50,399 --> 03:09:53,120 us 5551 03:09:51,279 --> 03:09:54,560 here they are let's see if math is in 5552 03:09:53,120 --> 03:09:56,880 here yep 5553 03:09:54,560 --> 03:09:58,160 there it is so yeah feel free to take a 5554 03:09:56,880 --> 03:09:59,920 look through some of these modules 5555 03:09:58,160 --> 03:10:01,680 because there are some fairly useful 5556 03:09:59,920 --> 03:10:03,680 tools within some of these modules 5557 03:10:01,680 --> 03:10:05,760 i suppose you can also go to python's 5558 03:10:03,680 --> 03:10:08,000 official documentation and search for 5559 03:10:05,760 --> 03:10:09,520 the python module index and here's a 5560 03:10:08,000 --> 03:10:10,880 listing of all the different modules too 5561 03:10:09,520 --> 03:10:13,279 they have access to 5562 03:10:10,880 --> 03:10:15,040 well everyone those are modules they are 5563 03:10:13,279 --> 03:10:17,680 files containing python code and they 5564 03:10:15,040 --> 03:10:18,960 may contain functions classes etc 5565 03:10:17,680 --> 03:10:20,560 if you would like a copy of all this 5566 03:10:18,960 --> 03:10:22,000 code i will post all of this to the 5567 03:10:20,560 --> 03:10:25,040 comments section down below 5568 03:10:22,000 --> 03:10:26,720 but yeah those are modules in python i 5569 03:10:25,040 --> 03:10:28,960 guess there's a module called pickle 5570 03:10:26,720 --> 03:10:29,920 hey what's going on everybody it's bro 5571 03:10:28,960 --> 03:10:31,120 hope you're doing well 5572 03:10:29,920 --> 03:10:33,120 and in this video we're going to be 5573 03:10:31,120 --> 03:10:34,720 creating a basic game of rock paper 5574 03:10:33,120 --> 03:10:39,279 scissors in python so 5575 03:10:34,720 --> 03:10:39,279 sit back relax and enjoy the show 5576 03:10:39,520 --> 03:10:42,720 let's begin by importing the random 5577 03:10:42,000 --> 03:10:44,080 module 5578 03:10:42,720 --> 03:10:46,560 because we're going to need the computer 5579 03:10:44,080 --> 03:10:46,880 to pick a random choice and we'll create 5580 03:10:46,560 --> 03:10:49,760 a 5581 03:10:46,880 --> 03:10:51,359 list of possible choices so we have 5582 03:10:49,760 --> 03:10:56,399 three strings within here 5583 03:10:51,359 --> 03:10:56,399 rock paper and scissors 5584 03:10:57,600 --> 03:11:01,359 i think i spelled scissors wrong there 5585 03:11:00,000 --> 03:11:04,080 we go 5586 03:11:01,359 --> 03:11:04,479 okay and we will have our computer pick 5587 03:11:04,080 --> 03:11:08,640 a 5588 03:11:04,479 --> 03:11:12,800 random dot choice from our list 5589 03:11:08,640 --> 03:11:16,319 of choices and let's test it by printing 5590 03:11:12,800 --> 03:11:16,319 whatever the computer picks 5591 03:11:16,640 --> 03:11:20,239 so the computer picked paper paper 5592 03:11:19,439 --> 03:11:23,040 scissors 5593 03:11:20,239 --> 03:11:23,760 and scissors okay what about the player 5594 03:11:23,040 --> 03:11:27,200 choice 5595 03:11:23,760 --> 03:11:31,200 let's say player equals 5596 03:11:27,200 --> 03:11:34,640 input rock 5597 03:11:31,200 --> 03:11:34,640 paper or 5598 03:11:34,840 --> 03:11:37,840 scissors 5599 03:11:38,160 --> 03:11:41,600 and let's print what the player has 5600 03:11:40,880 --> 03:11:46,880 print 5601 03:11:41,600 --> 03:11:46,880 player and let's print the computer too 5602 03:11:47,359 --> 03:11:50,960 maybe i'll add a string before these so 5603 03:11:49,359 --> 03:11:54,880 let's say 5604 03:11:50,960 --> 03:11:57,359 computer call in space 5605 03:11:54,880 --> 03:11:59,359 whatever the computer chooses and same 5606 03:11:57,359 --> 03:12:02,960 thing with the player 5607 03:11:59,359 --> 03:12:06,000 player all right 5608 03:12:02,960 --> 03:12:07,600 let's see if this works rock paper or 5609 03:12:06,000 --> 03:12:10,160 scissors let's say rock 5610 03:12:07,600 --> 03:12:12,720 the computer picks paper and i pick rock 5611 03:12:10,160 --> 03:12:14,479 so i would technically lose this round 5612 03:12:12,720 --> 03:12:16,720 now what if the player doesn't pick rock 5613 03:12:14,479 --> 03:12:19,120 paper or scissors what if they type in 5614 03:12:16,720 --> 03:12:19,760 gun so we would like some way to prevent 5615 03:12:19,120 --> 03:12:22,080 that 5616 03:12:19,760 --> 03:12:24,080 so let's put our input for our player 5617 03:12:22,080 --> 03:12:27,359 within a while loop 5618 03:12:24,080 --> 03:12:32,319 while player 5619 03:12:27,359 --> 03:12:34,319 not in choices 5620 03:12:32,319 --> 03:12:36,239 so we're going to need to initialize 5621 03:12:34,319 --> 03:12:38,880 player because right now it's an 5622 03:12:36,239 --> 03:12:42,479 unresolved reference 5623 03:12:38,880 --> 03:12:45,359 so let's say player equals none 5624 03:12:42,479 --> 03:12:46,160 for the time being okay now let's test 5625 03:12:45,359 --> 03:12:48,479 our choices 5626 03:12:46,160 --> 03:12:49,600 our player choices rock paper or 5627 03:12:48,479 --> 03:12:52,080 scissors i pick 5628 03:12:49,600 --> 03:12:53,040 the gun and it's going to keep on asking 5629 03:12:52,080 --> 03:12:56,239 me until i pick 5630 03:12:53,040 --> 03:12:58,479 something from my list of choices so 5631 03:12:56,239 --> 03:12:59,680 if i type in that still won't accept it 5632 03:12:58,479 --> 03:13:02,800 now let's type in 5633 03:12:59,680 --> 03:13:04,560 rock again there we go now what if 5634 03:13:02,800 --> 03:13:05,920 somebody types in one of these choices 5635 03:13:04,560 --> 03:13:07,920 either all caps or 5636 03:13:05,920 --> 03:13:09,600 capitalized well that technically 5637 03:13:07,920 --> 03:13:12,000 wouldn't be in choices because 5638 03:13:09,600 --> 03:13:14,080 these strings are case sensitive so why 5639 03:13:12,000 --> 03:13:14,960 don't we take our user input and make it 5640 03:13:14,080 --> 03:13:17,600 lowercase 5641 03:13:14,960 --> 03:13:18,960 and i'll just add on dot lower and use 5642 03:13:17,600 --> 03:13:22,080 the lower method 5643 03:13:18,960 --> 03:13:24,399 so therefore we can now type in any 5644 03:13:22,080 --> 03:13:25,520 input that is either all caps or 5645 03:13:24,399 --> 03:13:27,040 uppercase 5646 03:13:25,520 --> 03:13:28,640 and then that will still be accepted 5647 03:13:27,040 --> 03:13:29,200 because we're going to take our player 5648 03:13:28,640 --> 03:13:30,880 input 5649 03:13:29,200 --> 03:13:33,040 and then make it lower case so that it 5650 03:13:30,880 --> 03:13:34,960 matches one of these choices 5651 03:13:33,040 --> 03:13:36,319 okay let's work on the win conditions 5652 03:13:34,960 --> 03:13:39,040 next so first 5653 03:13:36,319 --> 03:13:39,760 let's check to see if we encountered a 5654 03:13:39,040 --> 03:13:42,479 tie 5655 03:13:39,760 --> 03:13:43,279 if player is equal to computer that 5656 03:13:42,479 --> 03:13:46,160 means that 5657 03:13:43,279 --> 03:13:48,479 we both chose the same thing so let's 5658 03:13:46,160 --> 03:13:52,640 print whatever the computer picked 5659 03:13:48,479 --> 03:13:52,640 what we picked and we will print 5660 03:13:52,800 --> 03:13:59,120 tie now let's write an else 5661 03:13:56,000 --> 03:14:03,279 if statement e l i 5662 03:13:59,120 --> 03:14:06,960 f player equals 5663 03:14:03,279 --> 03:14:07,359 rock so if we pick rock and then we will 5664 03:14:06,960 --> 03:14:11,520 create 5665 03:14:07,359 --> 03:14:15,040 a nested if statement if computer 5666 03:14:11,520 --> 03:14:18,080 equals paper 5667 03:14:15,040 --> 03:14:20,800 that means we lose so let's print 5668 03:14:18,080 --> 03:14:21,520 everything we have here except we're 5669 03:14:20,800 --> 03:14:25,120 going to change 5670 03:14:21,520 --> 03:14:29,120 tie to you 5671 03:14:25,120 --> 03:14:33,680 lose now if the computer picks 5672 03:14:29,120 --> 03:14:37,279 let's say scissors then we win 5673 03:14:33,680 --> 03:14:40,399 so scissors and you 5674 03:14:37,279 --> 03:14:41,120 win okay then let's just repeat this 5675 03:14:40,399 --> 03:14:44,720 process 5676 03:14:41,120 --> 03:14:44,720 for scissors and paper 5677 03:14:45,840 --> 03:14:48,960 so this will be another else if 5678 03:14:47,520 --> 03:14:53,120 statement 5679 03:14:48,960 --> 03:14:56,239 if we pick 5680 03:14:53,120 --> 03:15:00,319 scissors and the computer picks 5681 03:14:56,239 --> 03:15:04,560 rock then we lose if the computer picks 5682 03:15:00,319 --> 03:15:07,359 paper then you win 5683 03:15:04,560 --> 03:15:09,680 and lastly we have paper as the player 5684 03:15:07,359 --> 03:15:09,680 choice 5685 03:15:12,479 --> 03:15:20,399 so else if player equals paper 5686 03:15:16,080 --> 03:15:21,040 and the computer picks scissors then we 5687 03:15:20,399 --> 03:15:23,840 lose 5688 03:15:21,040 --> 03:15:25,040 if the computer picks rock well then 5689 03:15:23,840 --> 03:15:29,279 paper covers rock 5690 03:15:25,040 --> 03:15:32,080 and we win now let's test it 5691 03:15:29,279 --> 03:15:33,120 rock paper or scissors again i'll try 5692 03:15:32,080 --> 03:15:35,279 and pick the gun 5693 03:15:33,120 --> 03:15:36,479 which i can't let's take this seriously 5694 03:15:35,279 --> 03:15:40,239 i will pick rock 5695 03:15:36,479 --> 03:15:43,040 the computer picked rock so it's a tie 5696 03:15:40,239 --> 03:15:44,880 i'm feeling like paper today oh it's 5697 03:15:43,040 --> 03:15:48,000 another tie i guess 5698 03:15:44,880 --> 03:15:50,479 and how about scissors okay 5699 03:15:48,000 --> 03:15:51,760 so i win this round i picked uh scissors 5700 03:15:50,479 --> 03:15:54,160 and the computer picked paper 5701 03:15:51,760 --> 03:15:55,920 therefore i win let's try and lose this 5702 03:15:54,160 --> 03:15:58,960 time 5703 03:15:55,920 --> 03:16:00,080 just to verify it so i won again let's 5704 03:15:58,960 --> 03:16:02,080 try rock again 5705 03:16:00,080 --> 03:16:04,560 okay so i lost that round so we know 5706 03:16:02,080 --> 03:16:06,479 that the win conditions are working now 5707 03:16:04,560 --> 03:16:08,000 how about we add a feature where we will 5708 03:16:06,479 --> 03:16:08,640 ask the player if they would like to 5709 03:16:08,000 --> 03:16:10,720 play again 5710 03:16:08,640 --> 03:16:12,239 play another round i think the best way 5711 03:16:10,720 --> 03:16:14,640 to do that would be to write 5712 03:16:12,239 --> 03:16:15,279 all of this code within a while loop so 5713 03:16:14,640 --> 03:16:18,880 at the top 5714 03:16:15,279 --> 03:16:20,720 let's write while true and we'll need to 5715 03:16:18,880 --> 03:16:22,800 indent everything so that it's within 5716 03:16:20,720 --> 03:16:31,840 the while loop so let's just indent 5717 03:16:22,800 --> 03:16:31,840 everything here 5718 03:16:39,680 --> 03:16:43,439 now at the bottom we will ask the user 5719 03:16:41,840 --> 03:16:45,600 if they would like to play again and i 5720 03:16:43,439 --> 03:16:49,439 will store this within a variable 5721 03:16:45,600 --> 03:16:54,319 named play again play again equals input 5722 03:16:49,439 --> 03:16:58,319 and the prompt will be play again 5723 03:16:54,319 --> 03:16:58,880 yes slash no and depending on their 5724 03:16:58,319 --> 03:17:02,399 input 5725 03:16:58,880 --> 03:17:02,399 let's just make it all lower case 5726 03:17:02,479 --> 03:17:09,279 if play again 5727 03:17:06,160 --> 03:17:11,600 does not equal yes 5728 03:17:09,279 --> 03:17:14,000 that means they would like to quit so 5729 03:17:11,600 --> 03:17:17,200 let's use a break statement to break out 5730 03:17:14,000 --> 03:17:20,640 of our while loop that we're in 5731 03:17:17,200 --> 03:17:23,040 and at the end let's print by 5732 03:17:20,640 --> 03:17:24,160 and pay attention to the indentation 5733 03:17:23,040 --> 03:17:25,680 this input and 5734 03:17:24,160 --> 03:17:27,680 this if statement should be within the 5735 03:17:25,680 --> 03:17:30,880 while loop so they should have at least 5736 03:17:27,680 --> 03:17:32,399 one indentation and our print statement 5737 03:17:30,880 --> 03:17:33,600 here we'll just say bye 5738 03:17:32,399 --> 03:17:35,359 and you can see that there is no 5739 03:17:33,600 --> 03:17:36,239 indentation so that is outside of the 5740 03:17:35,359 --> 03:17:40,000 while loop 5741 03:17:36,239 --> 03:17:42,560 okay let's test this one last time 5742 03:17:40,000 --> 03:17:43,279 rock paper scissors i'm going to pick 5743 03:17:42,560 --> 03:17:46,160 the 5744 03:17:43,279 --> 03:17:47,600 cannon okay that's not valid input how 5745 03:17:46,160 --> 03:17:49,920 about rock 5746 03:17:47,600 --> 03:17:52,080 okay the computer picks rock i pick rock 5747 03:17:49,920 --> 03:17:54,560 so it's a tie do i want to play again 5748 03:17:52,080 --> 03:17:55,520 i'm going to select actually yes all 5749 03:17:54,560 --> 03:17:57,840 caps 5750 03:17:55,520 --> 03:17:59,840 that should still be acceptable elbow 5751 03:17:57,840 --> 03:18:01,760 paper this time 5752 03:17:59,840 --> 03:18:02,960 it's a tie again man the computer is 5753 03:18:01,760 --> 03:18:04,479 really good at this game 5754 03:18:02,960 --> 03:18:06,720 i'd like to play again i'll pick 5755 03:18:04,479 --> 03:18:08,399 scissors 5756 03:18:06,720 --> 03:18:11,840 okay so i lose i would like to play 5757 03:18:08,399 --> 03:18:14,160 again and i want to win this time 5758 03:18:11,840 --> 03:18:16,560 okay computer pick scissors i pick rock 5759 03:18:14,160 --> 03:18:19,680 you win do you want to play again 5760 03:18:16,560 --> 03:18:22,720 nah and it will print 5761 03:18:19,680 --> 03:18:24,720 bye well everybody that's a simple game 5762 03:18:22,720 --> 03:18:26,239 of rock paper scissors i'll post all of 5763 03:18:24,720 --> 03:18:26,880 this code to the comment section down 5764 03:18:26,239 --> 03:18:29,120 below 5765 03:18:26,880 --> 03:18:32,720 and well yeah that's a basic game of 5766 03:18:29,120 --> 03:18:34,800 rock paper scissors in python 5767 03:18:32,720 --> 03:18:36,239 hey what's going on everybody it's you 5768 03:18:34,800 --> 03:18:37,600 bro hope you're doing well 5769 03:18:36,239 --> 03:18:39,760 and in this video i'm going to show you 5770 03:18:37,600 --> 03:18:40,640 guys how we can build a basic quiz game 5771 03:18:39,760 --> 03:18:45,520 in python 5772 03:18:40,640 --> 03:18:47,279 so sit back relax and enjoy the show 5773 03:18:45,520 --> 03:18:48,960 to begin this project i think it would 5774 03:18:47,279 --> 03:18:50,640 be wise for us to create a skeletal 5775 03:18:48,960 --> 03:18:51,439 structure for how this program is going 5776 03:18:50,640 --> 03:18:52,880 to operate 5777 03:18:51,439 --> 03:18:54,640 and then we'll fill in the gaps later as 5778 03:18:52,880 --> 03:18:56,319 we go along so to begin 5779 03:18:54,640 --> 03:18:58,000 let's define all of the functions that 5780 03:18:56,319 --> 03:19:00,319 we'll need we'll need four 5781 03:18:58,000 --> 03:19:01,040 we'll create a function that is named 5782 03:19:00,319 --> 03:19:02,479 new game 5783 03:19:01,040 --> 03:19:05,040 whenever we call this function it will 5784 03:19:02,479 --> 03:19:07,040 create a new quiz game for us 5785 03:19:05,040 --> 03:19:08,399 and for the time being let's write pass 5786 03:19:07,040 --> 03:19:10,720 just as a placeholder 5787 03:19:08,399 --> 03:19:12,560 we'll have a total of four functions new 5788 03:19:10,720 --> 03:19:16,319 game 5789 03:19:12,560 --> 03:19:16,319 a function to check our answer 5790 03:19:16,479 --> 03:19:20,239 a function to display our score 5791 03:19:21,760 --> 03:19:27,439 and lastly a function to 5792 03:19:24,960 --> 03:19:27,439 play again 5793 03:19:30,319 --> 03:19:34,399 all right now i'm just going to add some 5794 03:19:33,600 --> 03:19:36,399 comments here 5795 03:19:34,399 --> 03:19:37,840 just to separate each of these functions 5796 03:19:36,399 --> 03:19:38,800 that we have just so that it's a little 5797 03:19:37,840 --> 03:19:40,160 bit easier to read 5798 03:19:38,800 --> 03:19:42,960 although this part is not really 5799 03:19:40,160 --> 03:19:44,840 necessary so i just want to separate 5800 03:19:42,960 --> 03:19:47,680 each of these functions for some 5801 03:19:44,840 --> 03:19:49,439 readability 5802 03:19:47,680 --> 03:19:51,439 okay those are all the functions that 5803 03:19:49,439 --> 03:19:53,520 we'll need what we'll need now 5804 03:19:51,439 --> 03:19:55,279 is some sort of collection to hold all 5805 03:19:53,520 --> 03:19:57,279 of the questions and answers that we 5806 03:19:55,279 --> 03:19:58,399 have and i think a dictionary would be 5807 03:19:57,279 --> 03:20:00,239 perfect for this 5808 03:19:58,399 --> 03:20:02,080 so what i did is that i created a 5809 03:20:00,239 --> 03:20:05,120 dictionary named questions 5810 03:20:02,080 --> 03:20:07,120 a dictionary has key value pairs 5811 03:20:05,120 --> 03:20:08,880 each key is a question that i would like 5812 03:20:07,120 --> 03:20:11,359 to ask and each question 5813 03:20:08,880 --> 03:20:12,560 has an associated value we'll have the 5814 03:20:11,359 --> 03:20:16,080 user guess between 5815 03:20:12,560 --> 03:20:17,840 answers of a b c or d so these would be 5816 03:20:16,080 --> 03:20:19,600 all of the correct answers all of the 5817 03:20:17,840 --> 03:20:20,880 values within this dictionary 5818 03:20:19,600 --> 03:20:22,319 so here are some of the questions that i 5819 03:20:20,880 --> 03:20:23,520 want to ask feel free to come up with 5820 03:20:22,319 --> 03:20:25,200 your own questions 5821 03:20:23,520 --> 03:20:26,880 if you would like you can copy the 5822 03:20:25,200 --> 03:20:28,000 questions that i have they should be 5823 03:20:26,880 --> 03:20:30,000 posted in the comment section 5824 03:20:28,000 --> 03:20:31,120 down below so the questions that i'm 5825 03:20:30,000 --> 03:20:33,520 going to ask are 5826 03:20:31,120 --> 03:20:34,399 who created python what year was python 5827 03:20:33,520 --> 03:20:36,560 created 5828 03:20:34,399 --> 03:20:38,960 python is attributed to which comedy 5829 03:20:36,560 --> 03:20:40,319 group and lastly is the earth round 5830 03:20:38,960 --> 03:20:43,040 i couldn't think of a fourth question so 5831 03:20:40,319 --> 03:20:45,120 i just threw in a random stupid question 5832 03:20:43,040 --> 03:20:46,800 and the correct answers to all of these 5833 03:20:45,120 --> 03:20:50,640 questions in order would be 5834 03:20:46,800 --> 03:20:51,279 a b c and a but feel free to come up 5835 03:20:50,640 --> 03:20:53,040 with your own 5836 03:20:51,279 --> 03:20:55,279 well we have our questions but we'll 5837 03:20:53,040 --> 03:20:57,120 need some sort of collection to hold all 5838 03:20:55,279 --> 03:20:59,920 of the different possible answers 5839 03:20:57,120 --> 03:21:02,479 to each of these questions and i think a 5840 03:20:59,920 --> 03:21:05,359 2d list would work perfect for this 5841 03:21:02,479 --> 03:21:07,279 so i have a list of lists a list of 5842 03:21:05,359 --> 03:21:09,760 tuples could work too i suppose 5843 03:21:07,279 --> 03:21:10,880 so here are all of the answers for the 5844 03:21:09,760 --> 03:21:13,520 first question 5845 03:21:10,880 --> 03:21:14,399 and the correct answer is a i believe 5846 03:21:13,520 --> 03:21:16,800 his name is 5847 03:21:14,399 --> 03:21:18,319 guido van rossum hey if you're listening 5848 03:21:16,800 --> 03:21:20,000 man i'm sorry if i'm mispronouncing your 5849 03:21:18,319 --> 03:21:21,920 name i'm really trying here 5850 03:21:20,000 --> 03:21:23,040 the second list corresponds to the 5851 03:21:21,920 --> 03:21:24,960 second question 5852 03:21:23,040 --> 03:21:26,640 what year was python created it was 5853 03:21:24,960 --> 03:21:29,120 created in 1991 5854 03:21:26,640 --> 03:21:31,040 at least according to wikipedia python 5855 03:21:29,120 --> 03:21:34,319 is attributed to which comedy group 5856 03:21:31,040 --> 03:21:35,680 the answer is c monty python like monty 5857 03:21:34,319 --> 03:21:38,160 python and the holy grail 5858 03:21:35,680 --> 03:21:39,120 good movie by the way and lastly is the 5859 03:21:38,160 --> 03:21:41,760 earth round 5860 03:21:39,120 --> 03:21:42,960 this is a highly debated topic but the 5861 03:21:41,760 --> 03:21:45,680 answer is hey 5862 03:21:42,960 --> 03:21:47,120 the earth is in fact round so i have a 5863 03:21:45,680 --> 03:21:49,920 list of lists 5864 03:21:47,120 --> 03:21:52,560 each list corresponds to a key value 5865 03:21:49,920 --> 03:21:54,399 pair within my dictionary of questions 5866 03:21:52,560 --> 03:21:56,399 and this is a lot to type if you want 5867 03:21:54,399 --> 03:21:57,920 you can just copy the code that i posted 5868 03:21:56,399 --> 03:21:59,600 in the comments down below 5869 03:21:57,920 --> 03:22:01,120 just copy and paste and you'll have all 5870 03:21:59,600 --> 03:22:02,000 of this or if you want you can pause the 5871 03:22:01,120 --> 03:22:04,080 video and type it 5872 03:22:02,000 --> 03:22:05,680 i don't care so now that you have your 5873 03:22:04,080 --> 03:22:06,479 questions and your options we're ready 5874 03:22:05,680 --> 03:22:07,920 to begin 5875 03:22:06,479 --> 03:22:09,760 so the first thing that we'll do within 5876 03:22:07,920 --> 03:22:12,399 our program is to call the 5877 03:22:09,760 --> 03:22:12,960 new game function to begin a new game so 5878 03:22:12,399 --> 03:22:14,720 after 5879 03:22:12,960 --> 03:22:16,720 we have our questions and options 5880 03:22:14,720 --> 03:22:19,760 created let's create a 5881 03:22:16,720 --> 03:22:20,640 new game by calling the new game 5882 03:22:19,760 --> 03:22:22,399 function 5883 03:22:20,640 --> 03:22:24,479 now when we run this program we'll 5884 03:22:22,399 --> 03:22:27,200 generate our dictionary of questions 5885 03:22:24,479 --> 03:22:28,239 and our 2d list of different options for 5886 03:22:27,200 --> 03:22:29,920 each question 5887 03:22:28,239 --> 03:22:31,600 and then we will call the new game 5888 03:22:29,920 --> 03:22:33,520 function to begin a new game 5889 03:22:31,600 --> 03:22:35,680 so let's head to the new game function 5890 03:22:33,520 --> 03:22:36,640 and fill in everything within our new 5891 03:22:35,680 --> 03:22:39,040 game function 5892 03:22:36,640 --> 03:22:40,080 so at the top let's declare a few things 5893 03:22:39,040 --> 03:22:43,520 let's say we have 5894 03:22:40,080 --> 03:22:45,520 a list named guesses and guesses will be 5895 03:22:43,520 --> 03:22:48,640 an empty list for now 5896 03:22:45,520 --> 03:22:51,200 i will declare a variable called correct 5897 03:22:48,640 --> 03:22:52,960 guesses set it equal to zero because we 5898 03:22:51,200 --> 03:22:55,760 haven't guessed anything yet 5899 03:22:52,960 --> 03:22:56,479 and we will set a current question 5900 03:22:55,760 --> 03:22:58,720 number 5901 03:22:56,479 --> 03:23:00,560 and set the sequel to one to represent 5902 03:22:58,720 --> 03:23:03,600 the first question 5903 03:23:00,560 --> 03:23:05,760 okay now we need to display all of the 5904 03:23:03,600 --> 03:23:06,479 questions within our dictionary of 5905 03:23:05,760 --> 03:23:09,120 questions 5906 03:23:06,479 --> 03:23:10,080 and we can use a for loop for that so 5907 03:23:09,120 --> 03:23:14,880 for 5908 03:23:10,080 --> 03:23:20,479 key in questions 5909 03:23:14,880 --> 03:23:20,479 i'm going to print my key 5910 03:23:20,560 --> 03:23:25,439 and let's just test this okay yeah 5911 03:23:23,520 --> 03:23:28,319 here's all my questions 5912 03:23:25,439 --> 03:23:30,880 i think what i'll do is actually print 5913 03:23:28,319 --> 03:23:34,160 something to separate each question 5914 03:23:30,880 --> 03:23:36,160 let's say one of these lines i think it 5915 03:23:34,160 --> 03:23:38,880 would look better with them 5916 03:23:36,160 --> 03:23:40,399 okay i'll print this line and then we'll 5917 03:23:38,880 --> 03:23:42,239 move on to a question 5918 03:23:40,399 --> 03:23:44,560 all right now after re-running this we 5919 03:23:42,239 --> 03:23:46,160 have each question and i added just a 5920 03:23:44,560 --> 03:23:47,600 line break between each of these to make 5921 03:23:46,160 --> 03:23:49,600 it look kind of fancy 5922 03:23:47,600 --> 03:23:51,760 now i need to display all of the 5923 03:23:49,600 --> 03:23:54,080 different options for each question 5924 03:23:51,760 --> 03:23:55,120 and we can do that with a nested for 5925 03:23:54,080 --> 03:23:59,359 loop 5926 03:23:55,120 --> 03:24:02,399 so if i were to write i in 5927 03:23:59,359 --> 03:24:05,359 options print i 5928 03:24:02,399 --> 03:24:07,040 let me show you what happens now with 5929 03:24:05,359 --> 03:24:08,239 the way this for loop is written now 5930 03:24:07,040 --> 03:24:10,000 it's going to display 5931 03:24:08,239 --> 03:24:12,080 all of the different options available 5932 03:24:10,000 --> 03:24:14,960 to us for the entire quiz game 5933 03:24:12,080 --> 03:24:16,160 what i would like instead is to display 5934 03:24:14,960 --> 03:24:18,720 only the first 5935 03:24:16,160 --> 03:24:19,600 list for the first question and then the 5936 03:24:18,720 --> 03:24:21,760 second list 5937 03:24:19,600 --> 03:24:23,760 for the second question and follow that 5938 03:24:21,760 --> 03:24:24,640 pattern so we're going to change our for 5939 03:24:23,760 --> 03:24:27,920 loop around 5940 03:24:24,640 --> 03:24:28,640 4i in options and we'll set an index for 5941 03:24:27,920 --> 03:24:30,720 options 5942 03:24:28,640 --> 03:24:31,840 the index is going to be our question 5943 03:24:30,720 --> 03:24:34,560 number 5944 03:24:31,840 --> 03:24:36,080 minus one now remember what i said in 5945 03:24:34,560 --> 03:24:37,359 previous videos where different 5946 03:24:36,080 --> 03:24:40,640 collections including 5947 03:24:37,359 --> 03:24:41,439 lists tuples etc the first element in a 5948 03:24:40,640 --> 03:24:43,600 collection 5949 03:24:41,439 --> 03:24:44,960 has an index of zero because computers 5950 03:24:43,600 --> 03:24:46,880 always start with zero 5951 03:24:44,960 --> 03:24:48,239 then the next element would therefore 5952 03:24:46,880 --> 03:24:50,479 have an index of one 5953 03:24:48,239 --> 03:24:52,800 then two three then you follow that 5954 03:24:50,479 --> 03:24:53,600 pattern we're using this question number 5955 03:24:52,800 --> 03:24:55,760 variable 5956 03:24:53,600 --> 03:24:57,279 as some sort of counter and since we 5957 03:24:55,760 --> 03:24:59,520 initially set this to one 5958 03:24:57,279 --> 03:25:00,399 i'm just going to subtract one so that 5959 03:24:59,520 --> 03:25:03,040 we effectively 5960 03:25:00,399 --> 03:25:04,960 receive zero as the index and now we 5961 03:25:03,040 --> 03:25:05,520 just need to increment our question 5962 03:25:04,960 --> 03:25:08,960 number 5963 03:25:05,520 --> 03:25:11,680 after each iteration so let's do that 5964 03:25:08,960 --> 03:25:13,680 i will add that to the end here after we 5965 03:25:11,680 --> 03:25:16,800 finish displaying all of the options 5966 03:25:13,680 --> 03:25:20,960 let's increment question number by one 5967 03:25:16,800 --> 03:25:23,040 by typing question num plus equals one 5968 03:25:20,960 --> 03:25:24,640 and if i were to run this currently 5969 03:25:23,040 --> 03:25:26,239 we'll display all of the different 5970 03:25:24,640 --> 03:25:28,479 options for each question 5971 03:25:26,239 --> 03:25:30,160 who created python here's all of the 5972 03:25:28,479 --> 03:25:32,560 associated options 5973 03:25:30,160 --> 03:25:34,319 what year is python created python is 5974 03:25:32,560 --> 03:25:37,279 attributed to which comedy group 5975 03:25:34,319 --> 03:25:38,880 and lastly is the earth round now it's 5976 03:25:37,279 --> 03:25:41,680 time for some user input 5977 03:25:38,880 --> 03:25:42,720 so i'm going to create a variable called 5978 03:25:41,680 --> 03:25:44,479 guess 5979 03:25:42,720 --> 03:25:46,479 and make sure you don't put it within 5980 03:25:44,479 --> 03:25:48,080 your inner for loop it should be within 5981 03:25:46,479 --> 03:25:51,600 the outer for loop 5982 03:25:48,080 --> 03:25:53,120 guess equals input and i'll create a 5983 03:25:51,600 --> 03:25:56,880 prompt 5984 03:25:53,120 --> 03:26:00,560 enter a b 5985 03:25:56,880 --> 03:26:02,080 c or d 5986 03:26:00,560 --> 03:26:04,319 here's something to consider we would 5987 03:26:02,080 --> 03:26:04,800 like the user to type in either capital 5988 03:26:04,319 --> 03:26:07,840 a 5989 03:26:04,800 --> 03:26:09,760 b c or d with strings they're case 5990 03:26:07,840 --> 03:26:12,000 sensitive what if the user typed in 5991 03:26:09,760 --> 03:26:13,439 one of these letters but lowercase if 5992 03:26:12,000 --> 03:26:14,560 they're correct we would still like to 5993 03:26:13,439 --> 03:26:16,560 give them their point 5994 03:26:14,560 --> 03:26:17,600 why don't we take our guess and make it 5995 03:26:16,560 --> 03:26:20,560 uppercase 5996 03:26:17,600 --> 03:26:21,439 so guess equals guess and we can make 5997 03:26:20,560 --> 03:26:24,560 this uppercase 5998 03:26:21,439 --> 03:26:26,800 using the upper method of strings at the 5999 03:26:24,560 --> 03:26:29,680 end of this game i'm going to compare 6000 03:26:26,800 --> 03:26:30,800 our guesses to the correct answers so we 6001 03:26:29,680 --> 03:26:33,040 have an empty 6002 03:26:30,800 --> 03:26:34,800 list named guesses and i'm going to 6003 03:26:33,040 --> 03:26:35,600 append our current guest that we're 6004 03:26:34,800 --> 03:26:39,520 working on 6005 03:26:35,600 --> 03:26:42,800 to our list of guesses so guesses 6006 03:26:39,520 --> 03:26:45,840 dot append and we will append our 6007 03:26:42,800 --> 03:26:46,239 guess this round now that we have our 6008 03:26:45,840 --> 03:26:48,560 guess 6009 03:26:46,239 --> 03:26:49,520 and we've appended our guess to our list 6010 03:26:48,560 --> 03:26:51,120 of guesses 6011 03:26:49,520 --> 03:26:53,359 let's check to see if it's the correct 6012 03:26:51,120 --> 03:26:53,920 answer or not so we're just about to 6013 03:26:53,359 --> 03:26:57,760 fill in 6014 03:26:53,920 --> 03:26:59,920 this check answer function next 6015 03:26:57,760 --> 03:27:02,720 so we are going to call this function 6016 03:26:59,920 --> 03:27:07,200 and pass in a few items as arguments 6017 03:27:02,720 --> 03:27:10,080 so let's use the check answer function 6018 03:27:07,200 --> 03:27:12,000 and we'll pass in the key for the 6019 03:27:10,080 --> 03:27:14,560 current question that we're on 6020 03:27:12,000 --> 03:27:15,760 so the key is the correct answer so that 6021 03:27:14,560 --> 03:27:18,880 would be 6022 03:27:15,760 --> 03:27:23,040 our questions 6023 03:27:18,880 --> 03:27:25,279 dot get key 6024 03:27:23,040 --> 03:27:26,479 this would be the answer and we'll also 6025 03:27:25,279 --> 03:27:30,080 pass in 6026 03:27:26,479 --> 03:27:32,720 our guess and we are going to fill in 6027 03:27:30,080 --> 03:27:34,239 the check answer function but we need to 6028 03:27:32,720 --> 03:27:37,520 set up the parameters 6029 03:27:34,239 --> 03:27:41,200 we're receiving effectively our answer 6030 03:27:37,520 --> 03:27:42,160 as well as our guess so i'm going to 6031 03:27:41,200 --> 03:27:45,760 name these as 6032 03:27:42,160 --> 03:27:46,880 answer and guess and we are going to 6033 03:27:45,760 --> 03:27:50,239 check to see if 6034 03:27:46,880 --> 03:27:53,439 our answer is equal to our guess 6035 03:27:50,239 --> 03:27:57,760 if answer is equal 6036 03:27:53,439 --> 03:28:02,239 to guess let's print something 6037 03:27:57,760 --> 03:28:02,239 print correct 6038 03:28:03,200 --> 03:28:06,640 and i think we should give the user a 6039 03:28:04,640 --> 03:28:08,479 point we'll have our check answer 6040 03:28:06,640 --> 03:28:12,319 function return 6041 03:28:08,479 --> 03:28:15,200 one for one point return one 6042 03:28:12,319 --> 03:28:17,840 else if this is not the correct answer 6043 03:28:15,200 --> 03:28:20,960 let's print 6044 03:28:17,840 --> 03:28:24,080 wrong and we will return 6045 03:28:20,960 --> 03:28:26,319 zero they do not get a point 6046 03:28:24,080 --> 03:28:27,600 and since this is returning a value we 6047 03:28:26,319 --> 03:28:30,399 should assign that 6048 03:28:27,600 --> 03:28:31,200 so let's assign the point we may or may 6049 03:28:30,399 --> 03:28:33,600 not receive 6050 03:28:31,200 --> 03:28:35,439 to our variable of correct guesses which 6051 03:28:33,600 --> 03:28:38,640 is initially set to zero 6052 03:28:35,439 --> 03:28:41,520 so we'll type correct guesses plus 6053 03:28:38,640 --> 03:28:42,399 equals check answer and check answer 6054 03:28:41,520 --> 03:28:44,800 will return 6055 03:28:42,399 --> 03:28:46,640 one if we scored a point or zero if we 6056 03:28:44,800 --> 03:28:47,040 did not score a point and make sure you 6057 03:28:46,640 --> 03:28:49,120 have 6058 03:28:47,040 --> 03:28:50,160 plus equals because if you just set this 6059 03:28:49,120 --> 03:28:52,479 to equals 6060 03:28:50,160 --> 03:28:53,600 then we cannot score more than one point 6061 03:28:52,479 --> 03:28:56,319 it will either be 6062 03:28:53,600 --> 03:28:57,600 zero or one so we are effectively 6063 03:28:56,319 --> 03:29:00,000 incrementing our correct 6064 03:28:57,600 --> 03:29:01,040 guesses by one for each point that we 6065 03:29:00,000 --> 03:29:03,120 score 6066 03:29:01,040 --> 03:29:04,399 and now we're going to work on the 6067 03:29:03,120 --> 03:29:06,319 display score 6068 03:29:04,399 --> 03:29:08,239 function and we'll call that at the end 6069 03:29:06,319 --> 03:29:09,120 after we finish all of our questions 6070 03:29:08,239 --> 03:29:10,960 that we have 6071 03:29:09,120 --> 03:29:13,040 so make sure that you do not write this 6072 03:29:10,960 --> 03:29:13,600 within the for loop it should be outside 6073 03:29:13,040 --> 03:29:16,000 of it 6074 03:29:13,600 --> 03:29:17,680 because once we finish iterating through 6075 03:29:16,000 --> 03:29:19,760 all of our questions we're going to 6076 03:29:17,680 --> 03:29:22,880 display a final score 6077 03:29:19,760 --> 03:29:25,200 so let's call the display score 6078 03:29:22,880 --> 03:29:26,160 function and we'll need to pass in some 6079 03:29:25,200 --> 03:29:29,439 arguments 6080 03:29:26,160 --> 03:29:33,600 our correct guesses as well 6081 03:29:29,439 --> 03:29:36,080 as our list of guesses 6082 03:29:33,600 --> 03:29:37,200 okay let's head to the display score 6083 03:29:36,080 --> 03:29:40,319 function 6084 03:29:37,200 --> 03:29:45,520 so we have as parameters 6085 03:29:40,319 --> 03:29:48,399 correct guesses as well as 6086 03:29:45,520 --> 03:29:49,120 guesses i need to remove this pass i'm 6087 03:29:48,399 --> 03:29:51,200 going to add 6088 03:29:49,120 --> 03:29:52,640 one of these fancy lines in just to 6089 03:29:51,200 --> 03:29:56,800 separate the questions 6090 03:29:52,640 --> 03:29:59,439 from the results and i will print 6091 03:29:56,800 --> 03:29:59,439 results 6092 03:29:59,840 --> 03:30:02,960 and maybe another one of these lines 6093 03:30:02,319 --> 03:30:08,080 okay 6094 03:30:02,960 --> 03:30:10,640 so i need to print all of the answers 6095 03:30:08,080 --> 03:30:10,640 answers 6096 03:30:11,760 --> 03:30:15,840 and i do not want to end on a new line 6097 03:30:14,640 --> 03:30:19,520 so i'm going to set 6098 03:30:15,840 --> 03:30:22,640 end equal to nothing and i need to 6099 03:30:19,520 --> 03:30:25,279 display all of the values within 6100 03:30:22,640 --> 03:30:26,239 our dictionary all of the answers i will 6101 03:30:25,279 --> 03:30:29,439 do that using 6102 03:30:26,239 --> 03:30:33,359 a for loop for i 6103 03:30:29,439 --> 03:30:37,359 in questions 6104 03:30:33,359 --> 03:30:41,359 print questions 6105 03:30:37,359 --> 03:30:44,319 dot get i 6106 03:30:41,359 --> 03:30:45,279 and i do not want to end on a new line 6107 03:30:44,319 --> 03:30:48,319 so i'm going to set 6108 03:30:45,279 --> 03:30:50,560 end equal to nothing effectively 6109 03:30:48,319 --> 03:30:52,319 and then i'll print a new line okay 6110 03:30:50,560 --> 03:30:53,840 let's work on the guesses i think i'm 6111 03:30:52,319 --> 03:30:56,239 just going to copy all this 6112 03:30:53,840 --> 03:30:57,120 and make some changes i'll replace 6113 03:30:56,239 --> 03:31:00,479 answers 6114 03:30:57,120 --> 03:31:04,239 with guesses for 6115 03:31:00,479 --> 03:31:07,760 i in guesses 6116 03:31:04,239 --> 03:31:09,760 print i 6117 03:31:07,760 --> 03:31:10,960 okay let's just be sure that 6118 03:31:09,760 --> 03:31:12,080 everything's working we're not 6119 03:31:10,960 --> 03:31:16,640 calculating a score 6120 03:31:12,080 --> 03:31:19,760 quite yet so i'm just going to answer a 6121 03:31:16,640 --> 03:31:22,080 then b c and d 6122 03:31:19,760 --> 03:31:23,520 on second thought after printing each of 6123 03:31:22,080 --> 03:31:25,439 these answers and guesses 6124 03:31:23,520 --> 03:31:26,800 i'm going to add a space after each of 6125 03:31:25,439 --> 03:31:28,720 these so 6126 03:31:26,800 --> 03:31:30,880 within each of these four loops at the 6127 03:31:28,720 --> 03:31:33,920 end i will add a space 6128 03:31:30,880 --> 03:31:37,040 let me try that again a b 6129 03:31:33,920 --> 03:31:39,439 c d okay not too bad 6130 03:31:37,040 --> 03:31:41,359 now let's calculate the final score and 6131 03:31:39,439 --> 03:31:43,120 we're still within our display score 6132 03:31:41,359 --> 03:31:46,399 function 6133 03:31:43,120 --> 03:31:48,960 let's set score equal to 6134 03:31:46,399 --> 03:31:48,960 correct 6135 03:31:49,600 --> 03:31:57,600 guesses divided by 6136 03:31:53,200 --> 03:31:57,600 the length of our questions 6137 03:31:58,319 --> 03:32:03,200 and let me add a set of parentheses 6138 03:32:00,640 --> 03:32:03,200 around here 6139 03:32:03,279 --> 03:32:08,960 then i'm going to multiply our score by 6140 03:32:06,840 --> 03:32:12,319 100 6141 03:32:08,960 --> 03:32:14,080 and if you don't want a decimal portion 6142 03:32:12,319 --> 03:32:16,479 because we'll display a percentage we 6143 03:32:14,080 --> 03:32:19,840 can cast this as an end 6144 03:32:16,479 --> 03:32:21,439 okay that all looks good and we'll print 6145 03:32:19,840 --> 03:32:25,760 the final score 6146 03:32:21,439 --> 03:32:29,680 print your score 6147 03:32:25,760 --> 03:32:31,439 is plus 6148 03:32:29,680 --> 03:32:34,880 we need to cast our score to a string 6149 03:32:31,439 --> 03:32:38,160 because we're using string concatenation 6150 03:32:34,880 --> 03:32:40,880 plus i'll add a percent sign 6151 03:32:38,160 --> 03:32:42,880 all right let's try it i'm intentionally 6152 03:32:40,880 --> 03:32:45,600 going to get the last question wrong 6153 03:32:42,880 --> 03:32:46,960 so we should have a 75 if we have four 6154 03:32:45,600 --> 03:32:50,399 questions 6155 03:32:46,960 --> 03:32:52,239 so that would be a b c 6156 03:32:50,399 --> 03:32:54,319 is the earth round what's earth i'm 6157 03:32:52,239 --> 03:32:57,520 gonna guess d 6158 03:32:54,319 --> 03:33:01,120 answers a b c a your guess is a b c 6159 03:32:57,520 --> 03:33:04,640 d your score is 75 okay let's test it 6160 03:33:01,120 --> 03:33:07,120 but getting all wrong answers d d d 6161 03:33:04,640 --> 03:33:08,720 d your score is zero percent 6162 03:33:07,120 --> 03:33:10,640 congratulations 6163 03:33:08,720 --> 03:33:12,000 and we'll test it one more time but get 6164 03:33:10,640 --> 03:33:14,720 all the right answers 6165 03:33:12,000 --> 03:33:15,840 we just want to be sure that we have 100 6166 03:33:14,720 --> 03:33:20,479 that's a 6167 03:33:15,840 --> 03:33:22,479 b c a your score is 100 6168 03:33:20,479 --> 03:33:24,080 now the last thing that you can add is 6169 03:33:22,479 --> 03:33:26,319 that we can play again if we want 6170 03:33:24,080 --> 03:33:27,680 and i have a separate function for that 6171 03:33:26,319 --> 03:33:28,800 if you would like to add this option 6172 03:33:27,680 --> 03:33:32,160 here's how to do so 6173 03:33:28,800 --> 03:33:34,720 let's remove that pass 6174 03:33:32,160 --> 03:33:36,479 let's set a variable named response 6175 03:33:34,720 --> 03:33:40,000 equal to 6176 03:33:36,479 --> 03:33:45,359 some input do you 6177 03:33:40,000 --> 03:33:45,359 want to play again 6178 03:33:45,600 --> 03:33:55,040 let's ask for yes or no 6179 03:33:50,319 --> 03:33:56,960 actually let me remove that okay 6180 03:33:55,040 --> 03:33:58,800 and depending on the user's response it 6181 03:33:56,960 --> 03:33:59,600 might be a lowercase so let's make it 6182 03:33:58,800 --> 03:34:02,960 uppercase 6183 03:33:59,600 --> 03:34:06,479 response equals response 6184 03:34:02,960 --> 03:34:06,479 and use the upper method 6185 03:34:07,040 --> 03:34:14,960 if response is equal to 6186 03:34:10,880 --> 03:34:18,560 yes all caps if response is equal to yes 6187 03:34:14,960 --> 03:34:22,560 then return true 6188 03:34:18,560 --> 03:34:25,200 else we will return 6189 03:34:22,560 --> 03:34:26,880 false and that is it for this function 6190 03:34:25,200 --> 03:34:28,640 the last thing we need to do 6191 03:34:26,880 --> 03:34:30,319 is to create a while loop that will 6192 03:34:28,640 --> 03:34:31,279 continue to ask the player if they want 6193 03:34:30,319 --> 03:34:33,200 to play again 6194 03:34:31,279 --> 03:34:34,479 this will be after we call the new game 6195 03:34:33,200 --> 03:34:38,080 function 6196 03:34:34,479 --> 03:34:40,399 while play again 6197 03:34:38,080 --> 03:34:41,439 and remember this will return either 6198 03:34:40,399 --> 03:34:44,319 true or 6199 03:34:41,439 --> 03:34:44,800 false depending on the user's response 6200 03:34:44,319 --> 03:34:48,479 while 6201 03:34:44,800 --> 03:34:50,720 play again new game 6202 03:34:48,479 --> 03:34:52,640 and call the new game function to create 6203 03:34:50,720 --> 03:34:54,560 a new game for us 6204 03:34:52,640 --> 03:34:56,399 if we escape the while loop that means 6205 03:34:54,560 --> 03:34:59,520 the user doesn't want to play again 6206 03:34:56,399 --> 03:35:02,960 so let's print the message 6207 03:34:59,520 --> 03:35:06,640 such as by e 6208 03:35:02,960 --> 03:35:10,319 all right let's test it i'm going to get 6209 03:35:06,640 --> 03:35:14,319 all of the correct answers this time a b 6210 03:35:10,319 --> 03:35:16,000 c a all right your score is 100 6211 03:35:14,319 --> 03:35:17,840 do you want to play again i'm going to 6212 03:35:16,000 --> 03:35:22,239 type 6213 03:35:17,840 --> 03:35:26,640 yes all right then we can play again 6214 03:35:22,239 --> 03:35:27,840 d d d d your score is zero percent do 6215 03:35:26,640 --> 03:35:30,880 you want to play again 6216 03:35:27,840 --> 03:35:33,760 no i think i mastered this game 6217 03:35:30,880 --> 03:35:36,239 bye all right everybody so that's how 6218 03:35:33,760 --> 03:35:37,120 you can use python to create a simple 6219 03:35:36,239 --> 03:35:38,960 quiz game 6220 03:35:37,120 --> 03:35:40,640 i will post all of this code to the 6221 03:35:38,960 --> 03:35:43,520 comment section down below 6222 03:35:40,640 --> 03:35:46,399 but well yeah that's a basic quiz game 6223 03:35:43,520 --> 03:35:48,720 in python for you all 6224 03:35:46,399 --> 03:35:49,760 hey what's going on everybody it's bro 6225 03:35:48,720 --> 03:35:51,439 hope you're doing well 6226 03:35:49,760 --> 03:35:53,840 and in this video i'm going to explain 6227 03:35:51,439 --> 03:35:58,800 object-oriented programming in python so 6228 03:35:53,840 --> 03:35:58,800 sit back relax and enjoy the show 6229 03:35:58,880 --> 03:36:03,200 welcome to today's topic on python 6230 03:36:01,600 --> 03:36:06,160 object oriented programming 6231 03:36:03,200 --> 03:36:07,840 also known as pop and in today's video 6232 03:36:06,160 --> 03:36:10,560 we're going to be creating objects 6233 03:36:07,840 --> 03:36:12,319 an object is an instance of a class by 6234 03:36:10,560 --> 03:36:15,279 using programming we can create 6235 03:36:12,319 --> 03:36:16,800 representations of real-life objects so 6236 03:36:15,279 --> 03:36:19,040 look around you wherever you're sitting 6237 03:36:16,800 --> 03:36:21,840 or standing right now you are surrounded 6238 03:36:19,040 --> 03:36:24,560 by objects next to me i have a phone 6239 03:36:21,840 --> 03:36:25,439 a television some snacks and i'm talking 6240 03:36:24,560 --> 03:36:27,439 into a microphone 6241 03:36:25,439 --> 03:36:30,080 point being we can use programming to 6242 03:36:27,439 --> 03:36:32,160 mimic real world objects by assigning a 6243 03:36:30,080 --> 03:36:35,200 combination of attributes 6244 03:36:32,160 --> 03:36:37,520 what an object is or has and methods 6245 03:36:35,200 --> 03:36:39,359 what an object can do now in order to 6246 03:36:37,520 --> 03:36:40,000 create an object we'll need to create a 6247 03:36:39,359 --> 03:36:41,840 class 6248 03:36:40,000 --> 03:36:43,600 a class can function as a blueprint that 6249 03:36:41,840 --> 03:36:44,319 will describe what attributes and 6250 03:36:43,600 --> 03:36:46,239 methods 6251 03:36:44,319 --> 03:36:48,080 that our distinct type of object will 6252 03:36:46,239 --> 03:36:49,680 have you can either create your class 6253 03:36:48,080 --> 03:36:51,279 within your main module 6254 03:36:49,680 --> 03:36:53,359 or you could create a separate file 6255 03:36:51,279 --> 03:36:55,279 dedicated solely for your class 6256 03:36:53,359 --> 03:36:57,120 now to create a class we would type 6257 03:36:55,279 --> 03:36:58,880 class and then the name of the object 6258 03:36:57,120 --> 03:37:00,479 that we would like to create so today i 6259 03:36:58,880 --> 03:37:02,000 would like to create some car objects 6260 03:37:00,479 --> 03:37:04,720 because i like cars 6261 03:37:02,000 --> 03:37:05,359 so we would type car and a common naming 6262 03:37:04,720 --> 03:37:07,279 convention 6263 03:37:05,359 --> 03:37:09,279 with class names is that they should be 6264 03:37:07,279 --> 03:37:10,239 capital so car is going to have a 6265 03:37:09,279 --> 03:37:11,680 capital c 6266 03:37:10,239 --> 03:37:13,600 and for the time being we need to type 6267 03:37:11,680 --> 03:37:14,640 in something i'll just type in pass as a 6268 03:37:13,600 --> 03:37:16,880 placeholder 6269 03:37:14,640 --> 03:37:18,720 so if you have a small program it may be 6270 03:37:16,880 --> 03:37:19,120 better to write your class within your 6271 03:37:18,720 --> 03:37:22,160 main 6272 03:37:19,120 --> 03:37:24,000 module but if your class is fairly large 6273 03:37:22,160 --> 03:37:26,080 you may want to consider placing your 6274 03:37:24,000 --> 03:37:28,000 class within a separate module 6275 03:37:26,080 --> 03:37:29,439 so if you were to take that route we 6276 03:37:28,000 --> 03:37:32,000 would go to file 6277 03:37:29,439 --> 03:37:32,800 new python file and we would name this 6278 03:37:32,000 --> 03:37:35,040 car 6279 03:37:32,800 --> 03:37:36,000 click python file and we would declare 6280 03:37:35,040 --> 03:37:39,200 our class within 6281 03:37:36,000 --> 03:37:40,560 the separate module so class car and for 6282 03:37:39,200 --> 03:37:42,560 now i'll type in pass 6283 03:37:40,560 --> 03:37:44,720 then we just need to import this class 6284 03:37:42,560 --> 03:37:47,359 so within our main module we would type 6285 03:37:44,720 --> 03:37:48,399 from the name of the module import the 6286 03:37:47,359 --> 03:37:51,359 name of the class 6287 03:37:48,399 --> 03:37:53,200 from car import car now objects can have 6288 03:37:51,359 --> 03:37:55,200 some combination of attributes and 6289 03:37:53,200 --> 03:37:56,560 methods attributes describe what an 6290 03:37:55,200 --> 03:37:58,479 object is or has 6291 03:37:56,560 --> 03:38:00,880 so what are a few attributes that cars 6292 03:37:58,479 --> 03:38:02,720 might have they might have a make so 6293 03:38:00,880 --> 03:38:04,319 let's create a few variables 6294 03:38:02,720 --> 03:38:06,000 for the time being i'm just going to use 6295 03:38:04,319 --> 03:38:10,479 none as a placeholder 6296 03:38:06,000 --> 03:38:13,840 cars can have a model a year 6297 03:38:10,479 --> 03:38:13,840 and a color 6298 03:38:14,080 --> 03:38:18,399 now objects can also have methods what 6299 03:38:16,560 --> 03:38:20,640 kinds of methods could cars perform 6300 03:38:18,399 --> 03:38:22,000 perhaps a drive method and a stop method 6301 03:38:20,640 --> 03:38:25,439 so let's define those 6302 03:38:22,000 --> 03:38:26,239 def let's say drive and we'll have one 6303 03:38:25,439 --> 03:38:28,720 argument 6304 03:38:26,239 --> 03:38:30,000 self self refers to the object that is 6305 03:38:28,720 --> 03:38:31,840 using this method 6306 03:38:30,000 --> 03:38:33,120 now what do we want to do when we call 6307 03:38:31,840 --> 03:38:36,399 this method 6308 03:38:33,120 --> 03:38:39,520 let's say this car 6309 03:38:36,399 --> 03:38:40,160 is driving let's create a stop method as 6310 03:38:39,520 --> 03:38:43,520 well 6311 03:38:40,160 --> 03:38:46,720 def stop 6312 03:38:43,520 --> 03:38:49,840 and we will print this 6313 03:38:46,720 --> 03:38:51,600 car is stopped we now have all of the 6314 03:38:49,840 --> 03:38:53,359 different attributes and methods we 6315 03:38:51,600 --> 03:38:54,880 would like our car objects to have but 6316 03:38:53,359 --> 03:38:57,199 there is one more thing that we need 6317 03:38:54,880 --> 03:38:58,640 it is a special method called the init 6318 03:38:57,199 --> 03:39:00,560 method that will construct 6319 03:38:58,640 --> 03:39:02,160 objects for us in other programming 6320 03:39:00,560 --> 03:39:03,040 languages this is known as the 6321 03:39:02,160 --> 03:39:05,040 constructor 6322 03:39:03,040 --> 03:39:06,399 so we need a special method that will 6323 03:39:05,040 --> 03:39:08,960 create objects for us 6324 03:39:06,399 --> 03:39:10,880 so we need to define this method def and 6325 03:39:08,960 --> 03:39:11,439 the syntax on this is somewhat strange 6326 03:39:10,880 --> 03:39:14,080 it is 6327 03:39:11,439 --> 03:39:14,880 two underscores init it's short for 6328 03:39:14,080 --> 03:39:16,800 initialize 6329 03:39:14,880 --> 03:39:18,720 200 scores again and then we need at 6330 03:39:16,800 --> 03:39:21,760 least self as an argument 6331 03:39:18,720 --> 03:39:24,880 and then we can actually assign our car 6332 03:39:21,760 --> 03:39:26,239 objects unique variables so take all of 6333 03:39:24,880 --> 03:39:27,680 these attributes that we have 6334 03:39:26,239 --> 03:39:30,080 and we're going to place them within 6335 03:39:27,680 --> 03:39:30,960 this init method now we can receive 6336 03:39:30,080 --> 03:39:33,040 arguments 6337 03:39:30,960 --> 03:39:34,239 when we create car objects but we need 6338 03:39:33,040 --> 03:39:36,960 to pass them in 6339 03:39:34,239 --> 03:39:38,479 as arguments to our init method so we 6340 03:39:36,960 --> 03:39:39,920 need to set up some parameters 6341 03:39:38,479 --> 03:39:42,720 let's say in order to create a car 6342 03:39:39,920 --> 03:39:45,920 object we need a make 6343 03:39:42,720 --> 03:39:47,600 a model a year and a color 6344 03:39:45,920 --> 03:39:49,199 then when we receive these arguments we 6345 03:39:47,600 --> 03:39:52,080 can actually assign them to 6346 03:39:49,199 --> 03:39:54,160 each car's specific attributes but we 6347 03:39:52,080 --> 03:39:56,160 need to precede each of these with self 6348 03:39:54,160 --> 03:39:57,359 self is referring to the object that 6349 03:39:56,160 --> 03:40:01,279 we're currently working on 6350 03:39:57,359 --> 03:40:05,359 or creating so self.make self.model 6351 03:40:01,279 --> 03:40:07,199 self.ear and self.color 6352 03:40:05,359 --> 03:40:08,880 and then when we assign these we're 6353 03:40:07,199 --> 03:40:11,199 going to say self.make 6354 03:40:08,880 --> 03:40:12,960 equals whatever make that we receive 6355 03:40:11,199 --> 03:40:15,680 when it's passed in as an argument 6356 03:40:12,960 --> 03:40:17,279 self.model equals model and continue 6357 03:40:15,680 --> 03:40:20,720 that pattern for year 6358 03:40:17,279 --> 03:40:23,279 and color all right that is all we need 6359 03:40:20,720 --> 03:40:24,399 for our class car we have a constructor 6360 03:40:23,279 --> 03:40:26,239 we're assigning 6361 03:40:24,399 --> 03:40:28,160 arguments that we receive to the 6362 03:40:26,239 --> 03:40:30,640 attributes of our car object 6363 03:40:28,160 --> 03:40:32,000 and we also have two methods one for 6364 03:40:30,640 --> 03:40:33,439 drive and one for stop 6365 03:40:32,000 --> 03:40:36,000 now we can start creating some car 6366 03:40:33,439 --> 03:40:36,479 objects so going back to my main file 6367 03:40:36,000 --> 03:40:38,319 here 6368 03:40:36,479 --> 03:40:40,239 to create an object we need a unique 6369 03:40:38,319 --> 03:40:41,920 name for it let's call this first car 6370 03:40:40,239 --> 03:40:44,800 just car one 6371 03:40:41,920 --> 03:40:46,239 equals the name of the class car then a 6372 03:40:44,800 --> 03:40:48,479 set of parentheses 6373 03:40:46,239 --> 03:40:50,160 now in order to construct a car object 6374 03:40:48,479 --> 03:40:52,239 we need to pass in a matching 6375 03:40:50,160 --> 03:40:54,319 set of arguments you can see here that 6376 03:40:52,239 --> 03:40:55,120 we need to pass in a make a model a year 6377 03:40:54,319 --> 03:40:57,040 and a color 6378 03:40:55,120 --> 03:40:59,520 so what kind of car should we make let's 6379 03:40:57,040 --> 03:41:03,520 say that the make is going to be 6380 03:40:59,520 --> 03:41:07,279 chevy we need a model 6381 03:41:03,520 --> 03:41:10,640 perhaps corvette a year 6382 03:41:07,279 --> 03:41:13,760 2021 is good and a color let's say blue 6383 03:41:10,640 --> 03:41:15,439 alright now let's actually access some 6384 03:41:13,760 --> 03:41:16,640 of these cars attributes 6385 03:41:15,439 --> 03:41:19,680 and i should probably put this one in a 6386 03:41:16,640 --> 03:41:23,359 print statement so print car ones 6387 03:41:19,680 --> 03:41:25,600 make and this should print chevy 6388 03:41:23,359 --> 03:41:26,960 then we can do the same thing for model 6389 03:41:25,600 --> 03:41:29,120 year and color 6390 03:41:26,960 --> 03:41:30,720 so let's change some of these around 6391 03:41:29,120 --> 03:41:35,359 model 6392 03:41:30,720 --> 03:41:35,359 year and color 6393 03:41:35,920 --> 03:41:39,920 yep this object is a chevy corvette 2021 6394 03:41:38,720 --> 03:41:41,920 and the color is blue 6395 03:41:39,920 --> 03:41:42,960 and we should have two methods as well 6396 03:41:41,920 --> 03:41:44,720 so car one 6397 03:41:42,960 --> 03:41:46,399 i would like this car to use its drive 6398 03:41:44,720 --> 03:41:48,800 method 6399 03:41:46,399 --> 03:41:50,399 this car is driving and let's have this 6400 03:41:48,800 --> 03:41:54,479 car use it stop method 6401 03:41:50,399 --> 03:41:54,479 car one dot stop 6402 03:41:55,040 --> 03:41:58,560 this car is stopped there's one thing 6403 03:41:56,880 --> 03:42:00,080 that i should mention real quick because 6404 03:41:58,560 --> 03:42:02,319 i forgot to mention it earlier 6405 03:42:00,080 --> 03:42:03,520 so within our init method you can see 6406 03:42:02,319 --> 03:42:05,520 that we need five 6407 03:42:03,520 --> 03:42:06,720 arguments in order to construct a car 6408 03:42:05,520 --> 03:42:09,439 object self 6409 03:42:06,720 --> 03:42:10,000 make model year in color but when we 6410 03:42:09,439 --> 03:42:12,160 pass in 6411 03:42:10,000 --> 03:42:13,920 our arguments we're not passing in 6412 03:42:12,160 --> 03:42:16,000 anything for self we only have 6413 03:42:13,920 --> 03:42:18,640 four arguments here that's because with 6414 03:42:16,000 --> 03:42:20,960 python we do not need to pass itself 6415 03:42:18,640 --> 03:42:22,640 that's done automatically for us we're 6416 03:42:20,960 --> 03:42:23,439 referring to the object that we're 6417 03:42:22,640 --> 03:42:25,439 dealing with 6418 03:42:23,439 --> 03:42:26,640 as you can see with our drive and stop 6419 03:42:25,439 --> 03:42:29,520 method we need 6420 03:42:26,640 --> 03:42:30,160 to pass in self in order to execute our 6421 03:42:29,520 --> 03:42:31,920 method 6422 03:42:30,160 --> 03:42:34,000 but when we call this method we do not 6423 03:42:31,920 --> 03:42:36,239 need to do so so just remember with 6424 03:42:34,000 --> 03:42:38,239 python with self we do not need to pass 6425 03:42:36,239 --> 03:42:38,720 in anything for this argument that is 6426 03:42:38,239 --> 03:42:40,720 all 6427 03:42:38,720 --> 03:42:42,239 now the nice thing about this is that we 6428 03:42:40,720 --> 03:42:43,840 can reuse this class 6429 03:42:42,239 --> 03:42:45,279 as a blueprint to create more car 6430 03:42:43,840 --> 03:42:47,520 objects we just call that 6431 03:42:45,279 --> 03:42:49,120 init method that constructor so this 6432 03:42:47,520 --> 03:42:49,600 time let's create a second car called 6433 03:42:49,120 --> 03:42:51,680 car 6434 03:42:49,600 --> 03:42:53,199 2 and this will be a different kind of 6435 03:42:51,680 --> 03:42:55,439 car 6436 03:42:53,199 --> 03:42:58,720 what kinds of arguments should we pass 6437 03:42:55,439 --> 03:43:01,760 it let's say this is a ford 6438 03:42:58,720 --> 03:43:03,359 mustang the year will be 2022 and the 6439 03:43:01,760 --> 03:43:05,040 color will be red 6440 03:43:03,359 --> 03:43:07,840 all right let's check card to use 6441 03:43:05,040 --> 03:43:07,840 attributes 6442 03:43:08,160 --> 03:43:12,640 and let's have car 2 use its drive and 6443 03:43:10,640 --> 03:43:15,120 stop method 6444 03:43:12,640 --> 03:43:15,920 this is a ford mustang the year is 2022 6445 03:43:15,120 --> 03:43:17,520 the color is red 6446 03:43:15,920 --> 03:43:19,439 this car is driving and this car is 6447 03:43:17,520 --> 03:43:20,000 stopped one other thing that we can do 6448 03:43:19,439 --> 03:43:22,399 too 6449 03:43:20,000 --> 03:43:23,680 is that within our methods here let's 6450 03:43:22,399 --> 03:43:25,199 replace car with 6451 03:43:23,680 --> 03:43:27,680 the name of the model that we're working 6452 03:43:25,199 --> 03:43:31,279 with so this 6453 03:43:27,680 --> 03:43:34,880 plus self dot model 6454 03:43:31,279 --> 03:43:37,920 plus is driving now this self keyword 6455 03:43:34,880 --> 03:43:39,760 think of it as you're replacing self 6456 03:43:37,920 --> 03:43:41,600 with the name of the object that we're 6457 03:43:39,760 --> 03:43:44,479 working on if car one 6458 03:43:41,600 --> 03:43:45,600 is using its drive method replace itself 6459 03:43:44,479 --> 03:43:47,359 with car one 6460 03:43:45,600 --> 03:43:50,160 if this is car two that called this 6461 03:43:47,359 --> 03:43:51,760 method then replace itself with car 2. 6462 03:43:50,160 --> 03:43:54,560 think of it that way and let's do the 6463 03:43:51,760 --> 03:43:58,479 same thing for stop 6464 03:43:54,560 --> 03:44:01,520 this plus self dot model 6465 03:43:58,479 --> 03:44:03,279 plus is stopped all right 6466 03:44:01,520 --> 03:44:04,560 probably don't need these anymore so 6467 03:44:03,279 --> 03:44:07,600 let's have car one 6468 03:44:04,560 --> 03:44:09,359 use its drive and stop method this 6469 03:44:07,600 --> 03:44:09,920 corvette is driving this corvette is 6470 03:44:09,359 --> 03:44:13,279 stopped 6471 03:44:09,920 --> 03:44:13,840 let's try this with car two this mustang 6472 03:44:13,279 --> 03:44:16,239 is driving 6473 03:44:13,840 --> 03:44:18,080 this mustang is stopped now you can have 6474 03:44:16,239 --> 03:44:20,239 them do things independently too let's 6475 03:44:18,080 --> 03:44:22,239 have car 1 use its drive method and car 6476 03:44:20,239 --> 03:44:24,080 2 uses stop method 6477 03:44:22,239 --> 03:44:26,160 this corvette is driving and this 6478 03:44:24,080 --> 03:44:28,560 mustang is stopped in conclusion 6479 03:44:26,160 --> 03:44:29,680 a class can function as a blueprint to 6480 03:44:28,560 --> 03:44:32,080 create objects 6481 03:44:29,680 --> 03:44:33,279 we can assign attributes what describe 6482 03:44:32,080 --> 03:44:36,319 an object is or 6483 03:44:33,279 --> 03:44:38,800 has and methods what each object 6484 03:44:36,319 --> 03:44:41,439 can do and then within our class we have 6485 03:44:38,800 --> 03:44:44,160 a special method called the init method 6486 03:44:41,439 --> 03:44:45,600 we can pass in some arguments and assign 6487 03:44:44,160 --> 03:44:47,920 these arguments to each 6488 03:44:45,600 --> 03:44:49,920 object's attributes and then we can 6489 03:44:47,920 --> 03:44:50,640 reuse this class as if it was a 6490 03:44:49,920 --> 03:44:53,520 blueprint 6491 03:44:50,640 --> 03:44:53,840 so we can create more objects out of it 6492 03:44:53,520 --> 03:44:56,000 so 6493 03:44:53,840 --> 03:44:58,000 that is the basics of object oriented 6494 03:44:56,000 --> 03:45:00,080 programming in python if you would like 6495 03:44:58,000 --> 03:45:01,920 a copy of all this code i will post this 6496 03:45:00,080 --> 03:45:04,160 in the comment section down below 6497 03:45:01,920 --> 03:45:05,279 but yeah that's the basics of object 6498 03:45:04,160 --> 03:45:08,720 oriented programming 6499 03:45:05,279 --> 03:45:10,640 in python hey what's going on everybody 6500 03:45:08,720 --> 03:45:12,000 it's bro hope you're doing well and in 6501 03:45:10,640 --> 03:45:14,560 this video i'm going to explain 6502 03:45:12,000 --> 03:45:15,840 the basics of class variables in python 6503 03:45:14,560 --> 03:45:19,760 so sit back 6504 03:45:15,840 --> 03:45:21,520 relax and enjoy the show 6505 03:45:19,760 --> 03:45:23,439 here's a super quick video on the 6506 03:45:21,520 --> 03:45:24,160 differences between class and instance 6507 03:45:23,439 --> 03:45:26,560 variables 6508 03:45:24,160 --> 03:45:27,520 for this example let's say we have two 6509 03:45:26,560 --> 03:45:29,840 car objects 6510 03:45:27,520 --> 03:45:31,840 named car one and car two and i'm just 6511 03:45:29,840 --> 03:45:33,439 passing in a bunch of values to each of 6512 03:45:31,840 --> 03:45:36,239 these car constructors 6513 03:45:33,439 --> 03:45:36,800 now within my class car i have my init 6514 03:45:36,239 --> 03:45:38,720 method 6515 03:45:36,800 --> 03:45:40,319 also known as the constructor and what 6516 03:45:38,720 --> 03:45:40,960 i'm doing is assigning all of these 6517 03:45:40,319 --> 03:45:43,199 values 6518 03:45:40,960 --> 03:45:44,319 to the variables found within my car 6519 03:45:43,199 --> 03:45:46,479 constructor 6520 03:45:44,319 --> 03:45:47,439 these variables declared inside the 6521 03:45:46,479 --> 03:45:50,319 constructor 6522 03:45:47,439 --> 03:45:50,960 are known as instance variables and each 6523 03:45:50,319 --> 03:45:53,120 object 6524 03:45:50,960 --> 03:45:54,000 can have their own unique values 6525 03:45:53,120 --> 03:45:56,239 assigned to each 6526 03:45:54,000 --> 03:45:57,439 of these variables now where a class 6527 03:45:56,239 --> 03:46:00,479 variable is different 6528 03:45:57,439 --> 03:46:02,239 is that a class variable is declared 6529 03:46:00,479 --> 03:46:04,000 inside the class but outside of the 6530 03:46:02,239 --> 03:46:06,160 constructor and what we can do 6531 03:46:04,000 --> 03:46:07,040 is set some default values for some 6532 03:46:06,160 --> 03:46:08,800 variables 6533 03:46:07,040 --> 03:46:10,800 let's say we would like to add an 6534 03:46:08,800 --> 03:46:13,439 additional variable let's say wheels 6535 03:46:10,800 --> 03:46:14,080 wheels equals and we can set a default 6536 03:46:13,439 --> 03:46:16,399 value 6537 03:46:14,080 --> 03:46:18,160 for all instances of this class for each 6538 03:46:16,399 --> 03:46:20,640 car object that we create 6539 03:46:18,160 --> 03:46:22,560 so we set wheels to equal four this is 6540 03:46:20,640 --> 03:46:25,199 an example of a class 6541 03:46:22,560 --> 03:46:26,640 variable whenever we create a car object 6542 03:46:25,199 --> 03:46:29,600 we can pass in a unique 6543 03:46:26,640 --> 03:46:30,720 make model year in color but by default 6544 03:46:29,600 --> 03:46:33,279 they will all have 6545 03:46:30,720 --> 03:46:34,239 four wheels now if i was to print the 6546 03:46:33,279 --> 03:46:37,520 amount of wheels 6547 03:46:34,239 --> 03:46:38,080 that car 1 and car 2 have they will both 6548 03:46:37,520 --> 03:46:40,399 have 6549 03:46:38,080 --> 03:46:41,279 four wheels it's as if we're setting a 6550 03:46:40,399 --> 03:46:43,840 default value 6551 03:46:41,279 --> 03:46:45,279 if i change this to two wheels well then 6552 03:46:43,840 --> 03:46:47,600 these cars have two wheels 6553 03:46:45,279 --> 03:46:48,399 maybe their motorcycles or something now 6554 03:46:47,600 --> 03:46:50,640 you can actually 6555 03:46:48,399 --> 03:46:51,520 change these values each object will 6556 03:46:50,640 --> 03:46:53,600 have their own 6557 03:46:51,520 --> 03:46:54,960 copy of this variable but you can set it 6558 03:46:53,600 --> 03:46:58,399 to a default value 6559 03:46:54,960 --> 03:47:01,439 so let's say car one 6560 03:46:58,399 --> 03:47:04,319 is a motorcycle car one dot wheels 6561 03:47:01,439 --> 03:47:06,319 equals two so if i were to print car 6562 03:47:04,319 --> 03:47:09,680 one's wheels along with card two 6563 03:47:06,319 --> 03:47:11,359 car one will have two wheels but car two 6564 03:47:09,680 --> 03:47:12,880 is still using that default amount of 6565 03:47:11,359 --> 03:47:14,640 wheels which is four 6566 03:47:12,880 --> 03:47:16,399 now there's another way in which you can 6567 03:47:14,640 --> 03:47:18,080 access a class variable 6568 03:47:16,399 --> 03:47:19,920 you don't necessarily need to create an 6569 03:47:18,080 --> 03:47:20,880 object to do so you can just use the 6570 03:47:19,920 --> 03:47:23,120 name of the class 6571 03:47:20,880 --> 03:47:25,040 so what i'm going to do is turn all of 6572 03:47:23,120 --> 03:47:28,239 these lines into comments 6573 03:47:25,040 --> 03:47:31,040 and i will print the amount of wheels 6574 03:47:28,239 --> 03:47:33,520 that our class has so type in the name 6575 03:47:31,040 --> 03:47:35,359 of the class car with a capital c 6576 03:47:33,520 --> 03:47:37,359 it should be the same name as your class 6577 03:47:35,359 --> 03:47:38,399 name followed by the name of the class 6578 03:47:37,359 --> 03:47:41,359 variable 6579 03:47:38,399 --> 03:47:42,080 car dot wheels but make sure you spell 6580 03:47:41,359 --> 03:47:44,399 it right 6581 03:47:42,080 --> 03:47:45,199 all right so this will display four 6582 03:47:44,399 --> 03:47:47,600 wheels 6583 03:47:45,199 --> 03:47:48,800 now what if we decide to change the 6584 03:47:47,600 --> 03:47:51,520 class variable 6585 03:47:48,800 --> 03:47:51,840 through our class well that will affect 6586 03:47:51,520 --> 03:47:55,439 all 6587 03:47:51,840 --> 03:47:59,199 instances of our class so let's say car 6588 03:47:55,439 --> 03:48:01,120 dot wheels equals two it's as if we 6589 03:47:59,199 --> 03:48:03,279 change our mind and we have decided to 6590 03:48:01,120 --> 03:48:06,319 create motorcycles instead of cars 6591 03:48:03,279 --> 03:48:08,479 well if we were to change the wheels 6592 03:48:06,319 --> 03:48:09,439 class variable through the use of our 6593 03:48:08,479 --> 03:48:11,279 car class 6594 03:48:09,439 --> 03:48:13,199 well then it's going to affect it for 6595 03:48:11,279 --> 03:48:15,680 all instances of this class 6596 03:48:13,199 --> 03:48:16,960 you can see that both car 1 and car 2 6597 03:48:15,680 --> 03:48:19,199 now have two wheels 6598 03:48:16,960 --> 03:48:20,080 when we just changed car 1's wheels it 6599 03:48:19,199 --> 03:48:22,239 did not affect 6600 03:48:20,080 --> 03:48:24,160 car 2 at all so that's the main 6601 03:48:22,239 --> 03:48:25,520 difference between a class variable and 6602 03:48:24,160 --> 03:48:27,840 an instance variable 6603 03:48:25,520 --> 03:48:28,880 an instance variable is declared inside 6604 03:48:27,840 --> 03:48:31,520 of constructor 6605 03:48:28,880 --> 03:48:32,560 and they can be given unique values with 6606 03:48:31,520 --> 03:48:34,640 class variables 6607 03:48:32,560 --> 03:48:35,920 they are declared within a class but 6608 03:48:34,640 --> 03:48:38,399 outside of the constructor 6609 03:48:35,920 --> 03:48:40,000 and you can set a default value for all 6610 03:48:38,399 --> 03:48:42,399 instances of this class 6611 03:48:40,000 --> 03:48:43,920 for all unique objects that are created 6612 03:48:42,399 --> 03:48:44,880 and then you can change those values 6613 03:48:43,920 --> 03:48:46,399 later too 6614 03:48:44,880 --> 03:48:48,000 so if you would like a copy of all this 6615 03:48:46,399 --> 03:48:49,520 code i will post all of this in the 6616 03:48:48,000 --> 03:48:51,840 comment section down below 6617 03:48:49,520 --> 03:48:52,640 but yeah that is the basics of class 6618 03:48:51,840 --> 03:48:56,080 variables 6619 03:48:52,640 --> 03:48:57,920 in python hey what's going on everybody 6620 03:48:56,080 --> 03:48:59,279 it's you bro hope you're doing well and 6621 03:48:57,920 --> 03:49:01,040 in this video i'm going to explain 6622 03:48:59,279 --> 03:49:05,920 inheritance in python so 6623 03:49:01,040 --> 03:49:05,920 sit back relax and enjoy the show 6624 03:49:06,080 --> 03:49:10,000 let's talk about irritants the term 6625 03:49:08,000 --> 03:49:11,600 inherit means to receive derive or 6626 03:49:10,000 --> 03:49:13,439 be left with and we can apply this 6627 03:49:11,600 --> 03:49:14,399 concept to programming classes can 6628 03:49:13,439 --> 03:49:16,000 inherit something 6629 03:49:14,399 --> 03:49:18,479 usually attributes and methods from 6630 03:49:16,000 --> 03:49:21,040 another class these classes can form 6631 03:49:18,479 --> 03:49:22,160 parent-child relationships where a child 6632 03:49:21,040 --> 03:49:24,479 will receive everything 6633 03:49:22,160 --> 03:49:26,560 that the parent class has much like you 6634 03:49:24,479 --> 03:49:28,399 inherit jeans from your parents 6635 03:49:26,560 --> 03:49:30,640 and classes can have children and give 6636 03:49:28,399 --> 03:49:32,239 whatever they own to their children 6637 03:49:30,640 --> 03:49:33,840 and in today's video we'll be creating a 6638 03:49:32,239 --> 03:49:35,520 parent class called animal 6639 03:49:33,840 --> 03:49:37,040 and children of the animal class will 6640 03:49:35,520 --> 03:49:37,600 inherit the common attributes and 6641 03:49:37,040 --> 03:49:39,680 methods 6642 03:49:37,600 --> 03:49:41,040 that all animals might have to kick off 6643 03:49:39,680 --> 03:49:42,720 this video i think what we'll do 6644 03:49:41,040 --> 03:49:44,399 is keep all of our classes within the 6645 03:49:42,720 --> 03:49:45,760 same file just so that it's easier for 6646 03:49:44,399 --> 03:49:47,680 us to read and understand 6647 03:49:45,760 --> 03:49:49,199 let's begin by creating a class called 6648 03:49:47,680 --> 03:49:51,520 animal so at the top 6649 03:49:49,199 --> 03:49:53,359 type class animal and what are some 6650 03:49:51,520 --> 03:49:53,920 attributes and methods that all animals 6651 03:49:53,359 --> 03:49:55,920 should have 6652 03:49:53,920 --> 03:49:58,239 let's say we have a class variable 6653 03:49:55,920 --> 03:50:00,080 called alive and we'll set this to true 6654 03:49:58,239 --> 03:50:02,000 if you're an animal you begin by being 6655 03:50:00,080 --> 03:50:04,399 alive and what are some methods 6656 03:50:02,000 --> 03:50:05,439 how about an eat and sleep method so 6657 03:50:04,399 --> 03:50:08,319 let's define those 6658 03:50:05,439 --> 03:50:09,120 def eat what are we gonna do when we 6659 03:50:08,319 --> 03:50:12,000 call this method 6660 03:50:09,120 --> 03:50:12,720 let's print something let's print this 6661 03:50:12,000 --> 03:50:15,040 animal 6662 03:50:12,720 --> 03:50:16,239 is eating let's also create a sleep 6663 03:50:15,040 --> 03:50:18,080 method def 6664 03:50:16,239 --> 03:50:19,680 sleep what are we gonna do when we call 6665 03:50:18,080 --> 03:50:20,880 this method let's print something as 6666 03:50:19,680 --> 03:50:24,319 well 6667 03:50:20,880 --> 03:50:25,279 this animal is sleeping and we now have 6668 03:50:24,319 --> 03:50:27,840 our animal class 6669 03:50:25,279 --> 03:50:30,080 now let's create separate classes for 6670 03:50:27,840 --> 03:50:32,880 specific types of animals let's say 6671 03:50:30,080 --> 03:50:33,520 a class for rabbit a fish and a hawk so 6672 03:50:32,880 --> 03:50:36,399 we can make 6673 03:50:33,520 --> 03:50:36,960 objects of those classes so let's begin 6674 03:50:36,399 --> 03:50:40,399 with a 6675 03:50:36,960 --> 03:50:41,359 rabbit class class rabbit now to use 6676 03:50:40,399 --> 03:50:43,199 inheritance 6677 03:50:41,359 --> 03:50:45,520 with the class that you intend to be the 6678 03:50:43,199 --> 03:50:46,800 child class after the class name add a 6679 03:50:45,520 --> 03:50:48,479 set of parentheses 6680 03:50:46,800 --> 03:50:49,840 and then pass in the name of the parent 6681 03:50:48,479 --> 03:50:50,479 class in this case it's going to be 6682 03:50:49,840 --> 03:50:53,439 animal 6683 03:50:50,479 --> 03:50:54,160 so we will add that to the parentheses 6684 03:50:53,439 --> 03:50:56,960 so now 6685 03:50:54,160 --> 03:50:57,920 rabbit is the child class and animal is 6686 03:50:56,960 --> 03:51:00,000 the parent class 6687 03:50:57,920 --> 03:51:02,080 so the child class is going to inherit 6688 03:51:00,000 --> 03:51:04,000 everything that the animal class has 6689 03:51:02,080 --> 03:51:05,760 so for the time being i'm going to type 6690 03:51:04,000 --> 03:51:07,760 pass so this rabbit class 6691 03:51:05,760 --> 03:51:08,800 will have access to a class variable 6692 03:51:07,760 --> 03:51:11,680 called alive 6693 03:51:08,800 --> 03:51:12,239 and an eat and sleep method so let's 6694 03:51:11,680 --> 03:51:14,800 define 6695 03:51:12,239 --> 03:51:16,720 a fish and hawk class and i'm just going 6696 03:51:14,800 --> 03:51:19,840 to copy what i have here and just change 6697 03:51:16,720 --> 03:51:21,040 rabbit to fish and then do so again and 6698 03:51:19,840 --> 03:51:24,080 change rabbit to 6699 03:51:21,040 --> 03:51:26,160 hawk this is our family tree animal 6700 03:51:24,080 --> 03:51:27,840 is the parent class and it has three 6701 03:51:26,160 --> 03:51:29,920 children a rabbit class 6702 03:51:27,840 --> 03:51:31,279 a fish class and a hot class and for the 6703 03:51:29,920 --> 03:51:32,960 time being we're just going to write 6704 03:51:31,279 --> 03:51:35,040 pass as a placeholder 6705 03:51:32,960 --> 03:51:36,000 so i bet we can create objects from 6706 03:51:35,040 --> 03:51:37,760 these classes 6707 03:51:36,000 --> 03:51:39,120 so let's do so let's say we have an 6708 03:51:37,760 --> 03:51:41,760 object called rabbit 6709 03:51:39,120 --> 03:51:42,319 rabbit equals rabbit let's do the same 6710 03:51:41,760 --> 03:51:45,840 thing for 6711 03:51:42,319 --> 03:51:45,840 fish and hawk 6712 03:51:47,520 --> 03:51:50,560 all right we now have three objects 6713 03:51:50,080 --> 03:51:53,439 rabbit 6714 03:51:50,560 --> 03:51:53,920 fish and hawk and let's take a look at 6715 03:51:53,439 --> 03:51:56,080 their 6716 03:51:53,920 --> 03:51:58,319 class variables i bet they have an alive 6717 03:51:56,080 --> 03:51:59,600 variable let's check that so i would 6718 03:51:58,319 --> 03:52:02,399 like to print 6719 03:51:59,600 --> 03:52:03,439 rabbit dot oh would you look at that 6720 03:52:02,399 --> 03:52:05,600 this rabbit class 6721 03:52:03,439 --> 03:52:07,359 has a class variable called alive even 6722 03:52:05,600 --> 03:52:09,439 though there's nothing within this class 6723 03:52:07,359 --> 03:52:10,479 well that's because we used inheritance 6724 03:52:09,439 --> 03:52:12,560 so each of these 6725 03:52:10,479 --> 03:52:14,319 three children classes inherit 6726 03:52:12,560 --> 03:52:16,560 everything from their parents 6727 03:52:14,319 --> 03:52:18,160 all of these attributes and methods i 6728 03:52:16,560 --> 03:52:19,920 bet they have eaten sleep functions as 6729 03:52:18,160 --> 03:52:22,399 well so let's check that 6730 03:52:19,920 --> 03:52:24,080 with fish let's have fish use its eat 6731 03:52:22,399 --> 03:52:26,640 method 6732 03:52:24,080 --> 03:52:29,359 and hawk will use its sleep method hawk 6733 03:52:26,640 --> 03:52:29,359 dot sleep 6734 03:52:29,760 --> 03:52:32,800 so the rabbit is alive the fish is 6735 03:52:31,920 --> 03:52:34,800 eating and 6736 03:52:32,800 --> 03:52:36,319 the hawk is sleeping now even though we 6737 03:52:34,800 --> 03:52:36,720 have nothing written within each of 6738 03:52:36,319 --> 03:52:39,359 these 6739 03:52:36,720 --> 03:52:41,439 specific types of animal classes all of 6740 03:52:39,359 --> 03:52:43,199 these animals have access to these 6741 03:52:41,439 --> 03:52:44,239 attributes and methods because of 6742 03:52:43,199 --> 03:52:46,479 inheritance 6743 03:52:44,239 --> 03:52:48,239 and another benefit of inheritance is 6744 03:52:46,479 --> 03:52:50,080 that we don't need to keep on 6745 03:52:48,239 --> 03:52:51,760 copying and pasting this code for 6746 03:52:50,080 --> 03:52:53,520 example let's say we were not using 6747 03:52:51,760 --> 03:52:54,880 inheritance so we would not need an 6748 03:52:53,520 --> 03:52:56,880 animal class 6749 03:52:54,880 --> 03:52:59,199 we could write our code this way where 6750 03:52:56,880 --> 03:53:01,600 we would just copy and paste everything 6751 03:52:59,199 --> 03:53:03,520 underneath each of our classes so what 6752 03:53:01,600 --> 03:53:04,560 if we need to make a change to one of 6753 03:53:03,520 --> 03:53:06,160 these methods 6754 03:53:04,560 --> 03:53:07,600 as in your boss says you know what let's 6755 03:53:06,160 --> 03:53:10,399 change sleep to 6756 03:53:07,600 --> 03:53:11,760 slumber okay well we would have to go to 6757 03:53:10,399 --> 03:53:12,960 each of these classes and make the 6758 03:53:11,760 --> 03:53:14,720 change manually 6759 03:53:12,960 --> 03:53:16,560 it's not too bad if you have like three 6760 03:53:14,720 --> 03:53:18,560 classes but what if you have hundreds 6761 03:53:16,560 --> 03:53:20,160 that's going to take a lot of effort so 6762 03:53:18,560 --> 03:53:21,840 wouldn't it be nice if we just 6763 03:53:20,160 --> 03:53:23,439 list everything that each of these 6764 03:53:21,840 --> 03:53:25,600 classes has in common 6765 03:53:23,439 --> 03:53:26,479 and then have all of these classes 6766 03:53:25,600 --> 03:53:28,560 inherit from 6767 03:53:26,479 --> 03:53:29,520 one common class and then if you need to 6768 03:53:28,560 --> 03:53:31,680 make any changes 6769 03:53:29,520 --> 03:53:33,199 you can do so just within this class and 6770 03:53:31,680 --> 03:53:35,199 this change will apply to 6771 03:53:33,199 --> 03:53:37,279 all of the different child classes so 6772 03:53:35,199 --> 03:53:39,120 that's another benefit of inheritance 6773 03:53:37,279 --> 03:53:40,720 and not only does it make any changes 6774 03:53:39,120 --> 03:53:42,960 easier but each class 6775 03:53:40,720 --> 03:53:43,920 can have their own unique attributes and 6776 03:53:42,960 --> 03:53:45,600 methods as well 6777 03:53:43,920 --> 03:53:47,359 along with the attributes and methods 6778 03:53:45,600 --> 03:53:49,520 that they inherit from their parents 6779 03:53:47,359 --> 03:53:50,800 so for rabbit fish and hawk let's have 6780 03:53:49,520 --> 03:53:53,040 each of these classes have 6781 03:53:50,800 --> 03:53:54,239 their own unique method as well so for 6782 03:53:53,040 --> 03:53:57,040 rabbit let's define 6783 03:53:54,239 --> 03:53:57,840 a run method if you're a rabbit then you 6784 03:53:57,040 --> 03:54:01,120 can run 6785 03:53:57,840 --> 03:54:04,000 so let's print this rabbit 6786 03:54:01,120 --> 03:54:05,680 is running and i'm just going to fix 6787 03:54:04,000 --> 03:54:08,239 some of the spacing here 6788 03:54:05,680 --> 03:54:09,120 all right if you're a fish then you can 6789 03:54:08,239 --> 03:54:13,600 swim 6790 03:54:09,120 --> 03:54:17,680 def swim let's print 6791 03:54:13,600 --> 03:54:21,040 this fish is swimming 6792 03:54:17,680 --> 03:54:24,880 and lastly we have hawk if you're a hawk 6793 03:54:21,040 --> 03:54:28,560 then you can fly def fly 6794 03:54:24,880 --> 03:54:32,560 let's print this hawk 6795 03:54:28,560 --> 03:54:35,040 is flying like i said before not only 6796 03:54:32,560 --> 03:54:35,840 do these specific types of animals have 6797 03:54:35,040 --> 03:54:37,680 access to 6798 03:54:35,840 --> 03:54:39,040 all of the attributes and methods that 6799 03:54:37,680 --> 03:54:40,880 they inherit from their parents 6800 03:54:39,040 --> 03:54:42,800 but they can have their own attributes 6801 03:54:40,880 --> 03:54:43,680 and methods as well so let's test some 6802 03:54:42,800 --> 03:54:46,160 of these methods 6803 03:54:43,680 --> 03:54:46,960 so we have our rabbit fish and hawk 6804 03:54:46,160 --> 03:54:51,120 objects 6805 03:54:46,960 --> 03:54:54,560 let's have rabbit use its run method 6806 03:54:51,120 --> 03:54:58,000 fish we'll use its swim method and hawk 6807 03:54:54,560 --> 03:54:58,000 will use its fly method 6808 03:54:59,520 --> 03:55:03,279 and here we go this rabbit is running 6809 03:55:02,160 --> 03:55:06,960 this fish is swimming 6810 03:55:03,279 --> 03:55:09,279 and this hawk is flying so in conclusion 6811 03:55:06,960 --> 03:55:11,279 classes can have children the children 6812 03:55:09,279 --> 03:55:12,640 classes will inherit everything that 6813 03:55:11,279 --> 03:55:14,319 their parent class has 6814 03:55:12,640 --> 03:55:16,479 all of the parents attributes and 6815 03:55:14,319 --> 03:55:17,760 methods but not only that the children 6816 03:55:16,479 --> 03:55:19,840 classes can implement 6817 03:55:17,760 --> 03:55:20,880 their own unique attributes and methods 6818 03:55:19,840 --> 03:55:22,880 as well 6819 03:55:20,880 --> 03:55:23,920 so that is inheritance if you would like 6820 03:55:22,880 --> 03:55:25,520 a copy of this code 6821 03:55:23,920 --> 03:55:27,760 i will post all of this in the comment 6822 03:55:25,520 --> 03:55:31,040 section down below but yeah that's the 6823 03:55:27,760 --> 03:55:33,040 basics of inheritance using python 6824 03:55:31,040 --> 03:55:34,399 yo what's going on everybody it's you 6825 03:55:33,040 --> 03:55:36,080 bro hope you're doing well 6826 03:55:34,399 --> 03:55:38,319 and in this video i'm going to explain 6827 03:55:36,080 --> 03:55:39,279 how multi-level inheritance works in 6828 03:55:38,319 --> 03:55:43,680 python so 6829 03:55:39,279 --> 03:55:45,760 sit back relax and enjoy the show 6830 03:55:43,680 --> 03:55:47,760 all right welcome back everybody let's 6831 03:55:45,760 --> 03:55:50,479 talk about multi-level inheritance 6832 03:55:47,760 --> 03:55:52,399 this is a concept where a derived class 6833 03:55:50,479 --> 03:55:54,800 also known as a child class 6834 03:55:52,399 --> 03:55:56,080 inherits from another derived class 6835 03:55:54,800 --> 03:55:58,560 here's an example let's say 6836 03:55:56,080 --> 03:56:00,080 we have a hierarchy of classes there 6837 03:55:58,560 --> 03:56:02,560 will be three levels to this 6838 03:56:00,080 --> 03:56:03,520 we'll create a family tree of living 6839 03:56:02,560 --> 03:56:05,359 organisms 6840 03:56:03,520 --> 03:56:06,560 so at the very top we're going to have a 6841 03:56:05,359 --> 03:56:09,040 class called 6842 03:56:06,560 --> 03:56:10,880 organism any class that inherits from 6843 03:56:09,040 --> 03:56:13,439 the organism parent class 6844 03:56:10,880 --> 03:56:14,479 will receive one attribute one variable 6845 03:56:13,439 --> 03:56:16,960 named alive 6846 03:56:14,479 --> 03:56:19,439 and this will be set to true so if you 6847 03:56:16,960 --> 03:56:21,359 are an organism you begin by being alive 6848 03:56:19,439 --> 03:56:23,359 now let's say we have a child class that 6849 03:56:21,359 --> 03:56:24,080 will inherit from the organism parent 6850 03:56:23,359 --> 03:56:27,199 class 6851 03:56:24,080 --> 03:56:30,479 let's say animal class animal 6852 03:56:27,199 --> 03:56:31,439 inherits from organism so any animal 6853 03:56:30,479 --> 03:56:33,600 that we create 6854 03:56:31,439 --> 03:56:34,560 will have this alive attribute set the 6855 03:56:33,600 --> 03:56:37,680 true 6856 03:56:34,560 --> 03:56:39,120 and all animals eat so let's define a 6857 03:56:37,680 --> 03:56:40,960 function that does that 6858 03:56:39,120 --> 03:56:42,239 at least i believe all animals eat let's 6859 03:56:40,960 --> 03:56:46,239 pretend that they do 6860 03:56:42,239 --> 03:56:48,160 so this animal is eating 6861 03:56:46,239 --> 03:56:50,640 and now why stop there let's create a 6862 03:56:48,160 --> 03:56:53,120 class for a specific type of animal 6863 03:56:50,640 --> 03:56:54,080 i like dogs so let's create a dog class 6864 03:56:53,120 --> 03:56:57,279 class dog 6865 03:56:54,080 --> 03:56:59,279 inherits from the animal class 6866 03:56:57,279 --> 03:57:02,640 and all dogs should be able to bark so 6867 03:56:59,279 --> 03:57:02,640 let's create a function to do that 6868 03:57:04,000 --> 03:57:10,960 and we'll print this dog 6869 03:57:07,279 --> 03:57:11,359 is barking okay so what we just did here 6870 03:57:10,960 --> 03:57:15,040 is 6871 03:57:11,359 --> 03:57:17,680 multi-level inheritance we had a derived 6872 03:57:15,040 --> 03:57:18,319 child class inherit from another child 6873 03:57:17,680 --> 03:57:20,399 class 6874 03:57:18,319 --> 03:57:22,800 so it's kind of like this dog class is 6875 03:57:20,399 --> 03:57:25,040 the child animal class is the parent 6876 03:57:22,800 --> 03:57:25,840 and then the parent of the parent class 6877 03:57:25,040 --> 03:57:27,600 would be like 6878 03:57:25,840 --> 03:57:29,040 a grandparent right and then if you 6879 03:57:27,600 --> 03:57:30,000 added another layer on top of that to 6880 03:57:29,040 --> 03:57:31,840 this hierarchy 6881 03:57:30,000 --> 03:57:33,520 well then the parent of the grandparent 6882 03:57:31,840 --> 03:57:35,040 would be like a great grandparent 6883 03:57:33,520 --> 03:57:37,040 that's how i think of it at least now 6884 03:57:35,040 --> 03:57:38,800 let's create a dog object just to verify 6885 03:57:37,040 --> 03:57:39,760 that everything's working so let's call 6886 03:57:38,800 --> 03:57:42,319 our dog 6887 03:57:39,760 --> 03:57:44,960 dog and our dog object should have 6888 03:57:42,319 --> 03:57:47,279 access to an attribute named alive 6889 03:57:44,960 --> 03:57:49,760 and our dog object receives that 6890 03:57:47,279 --> 03:57:51,520 attribute from the organism class 6891 03:57:49,760 --> 03:57:53,840 and this will print true if i were to 6892 03:57:51,520 --> 03:57:55,680 print whatever this attribute contains 6893 03:57:53,840 --> 03:57:57,840 and our dog should be able to eat 6894 03:57:55,680 --> 03:58:00,640 because it receives this method from the 6895 03:57:57,840 --> 03:58:02,319 animal parent class 6896 03:58:00,640 --> 03:58:04,080 and lastly our dog should be able to 6897 03:58:02,319 --> 03:58:06,800 bark and that method is defined within 6898 03:58:04,080 --> 03:58:06,800 its own class 6899 03:58:07,680 --> 03:58:12,560 so that's how multi-level inheritance 6900 03:58:09,760 --> 03:58:14,960 works it's a concept where a child class 6901 03:58:12,560 --> 03:58:17,520 will inherit from another child class 6902 03:58:14,960 --> 03:58:20,080 i like to think of it like a family tree 6903 03:58:17,520 --> 03:58:22,160 where a child class will have a parent 6904 03:58:20,080 --> 03:58:24,479 and the parent of that parent would be 6905 03:58:22,160 --> 03:58:26,399 like a grandparent basically 6906 03:58:24,479 --> 03:58:28,160 so if you would like a copy of this code 6907 03:58:26,399 --> 03:58:29,359 i will post all of this to the comment 6908 03:58:28,160 --> 03:58:31,120 section down below 6909 03:58:29,359 --> 03:58:33,279 but yeah that is how multi-level 6910 03:58:31,120 --> 03:58:35,199 inheritance works in python 6911 03:58:33,279 --> 03:58:36,720 hey what's going on everybody it's you 6912 03:58:35,199 --> 03:58:38,160 bro hope you're doing well and in this 6913 03:58:36,720 --> 03:58:40,720 video i'm going to show you all how 6914 03:58:38,160 --> 03:58:41,439 multiple inheritance works in python so 6915 03:58:40,720 --> 03:58:45,840 sit back 6916 03:58:41,439 --> 03:58:48,319 relax and enjoy the show 6917 03:58:45,840 --> 03:58:50,479 all right people multiple inheritance 6918 03:58:48,319 --> 03:58:52,160 multiple inheritance is the concept 6919 03:58:50,479 --> 03:58:54,640 where a child class 6920 03:58:52,160 --> 03:58:55,279 is derived from more than one parent 6921 03:58:54,640 --> 03:58:57,199 class 6922 03:58:55,279 --> 03:58:58,960 here's an example let's say that all of 6923 03:58:57,199 --> 03:59:01,199 our children classes are going to be 6924 03:58:58,960 --> 03:59:03,120 certain types of animals and we would 6925 03:59:01,199 --> 03:59:05,600 like our animal classes 6926 03:59:03,120 --> 03:59:06,399 to inherit from either the prepared 6927 03:59:05,600 --> 03:59:08,640 class 6928 03:59:06,399 --> 03:59:10,640 the predator parrot class or both 6929 03:59:08,640 --> 03:59:12,479 depending on what type of animal it is 6930 03:59:10,640 --> 03:59:14,319 because some animals in the animal 6931 03:59:12,479 --> 03:59:16,560 kingdom can be both prey 6932 03:59:14,319 --> 03:59:17,920 and predators and one example that comes 6933 03:59:16,560 --> 03:59:20,239 to mind would be fish 6934 03:59:17,920 --> 03:59:21,120 fish will eat smaller fish but also can 6935 03:59:20,239 --> 03:59:23,760 get eaten by 6936 03:59:21,120 --> 03:59:25,040 larger fish so here we have two classes 6937 03:59:23,760 --> 03:59:27,680 prey and predator 6938 03:59:25,040 --> 03:59:28,560 each has a dedicated method if a class 6939 03:59:27,680 --> 03:59:30,560 inherits from 6940 03:59:28,560 --> 03:59:32,239 the prey parent class they will have 6941 03:59:30,560 --> 03:59:35,120 access to a flea method 6942 03:59:32,239 --> 03:59:37,040 which will print this animal please if a 6943 03:59:35,120 --> 03:59:37,760 class inherits from the predator parent 6944 03:59:37,040 --> 03:59:39,920 class 6945 03:59:37,760 --> 03:59:41,680 that class will have access to a hunt 6946 03:59:39,920 --> 03:59:43,920 method that will print 6947 03:59:41,680 --> 03:59:45,600 this animal is hunting and it's possible 6948 03:59:43,920 --> 03:59:47,920 that some animals can do both 6949 03:59:45,600 --> 03:59:48,800 they can both flee and hunt but they'll 6950 03:59:47,920 --> 03:59:50,720 need to inherit 6951 03:59:48,800 --> 03:59:52,479 both of these classes both prey and 6952 03:59:50,720 --> 03:59:53,680 predator and that's where multiple 6953 03:59:52,479 --> 03:59:55,520 inheritance comes in 6954 03:59:53,680 --> 03:59:56,720 now let's create some classes based on 6955 03:59:55,520 --> 03:59:59,279 certain types of animals 6956 03:59:56,720 --> 04:00:01,520 let's say we have class rabbit and 6957 03:59:59,279 --> 04:00:02,080 rabbit we'll inherit from the prey class 6958 04:00:01,520 --> 04:00:03,920 because 6959 04:00:02,080 --> 04:00:05,600 rabbits are typically prey and not 6960 04:00:03,920 --> 04:00:07,199 predators but i don't know there could 6961 04:00:05,600 --> 04:00:09,439 be some killer rabbits out there 6962 04:00:07,199 --> 04:00:12,319 haven't run into any yet let's have 6963 04:00:09,439 --> 04:00:16,080 class hawk inherit from the 6964 04:00:12,319 --> 04:00:18,720 predator class and lastly fish 6965 04:00:16,080 --> 04:00:20,800 and fish can be both prey and predators 6966 04:00:18,720 --> 04:00:22,640 so they will inherit from both classes 6967 04:00:20,800 --> 04:00:24,080 and we just separate each class with a 6968 04:00:22,640 --> 04:00:26,880 comma so prey 6969 04:00:24,080 --> 04:00:27,840 comma predator and it's as simple as 6970 04:00:26,880 --> 04:00:29,600 that 6971 04:00:27,840 --> 04:00:31,680 now let's create an object from each of 6972 04:00:29,600 --> 04:00:36,000 these classes we have rabbit 6973 04:00:31,680 --> 04:00:39,279 equals rabbit hawk 6974 04:00:36,000 --> 04:00:42,960 equals hawk and fish 6975 04:00:39,279 --> 04:00:44,640 equals fish now let's just verify 6976 04:00:42,960 --> 04:00:46,239 all of the different methods that each 6977 04:00:44,640 --> 04:00:48,640 of these objects inherited 6978 04:00:46,239 --> 04:00:49,439 so our rabbit should have access to a 6979 04:00:48,640 --> 04:00:52,800 flea method 6980 04:00:49,439 --> 04:00:53,279 but no hunt method and let's just test 6981 04:00:52,800 --> 04:00:57,279 that 6982 04:00:53,279 --> 04:00:59,520 yep this animal flees and my hawk object 6983 04:00:57,279 --> 04:01:01,840 should have access to a hunt method but 6984 04:00:59,520 --> 04:01:04,960 no flea method 6985 04:01:01,840 --> 04:01:08,239 this animal is hunting and lastly 6986 04:01:04,960 --> 04:01:10,399 my fish object has access to both a flea 6987 04:01:08,239 --> 04:01:11,920 and a hunt method and if you take a look 6988 04:01:10,399 --> 04:01:13,680 on the right hand side of this pop-up 6989 04:01:11,920 --> 04:01:15,520 window it shows the class that it 6990 04:01:13,680 --> 04:01:17,760 inherited this method from 6991 04:01:15,520 --> 04:01:19,920 so fleet comes from the parent class of 6992 04:01:17,760 --> 04:01:21,279 prey and hunt comes from the apparent 6993 04:01:19,920 --> 04:01:24,160 class of predator 6994 04:01:21,279 --> 04:01:26,319 so my fish can both flee and hunt 6995 04:01:24,160 --> 04:01:29,359 because it's typically considered both 6996 04:01:26,319 --> 04:01:31,920 prey and predators 6997 04:01:29,359 --> 04:01:33,760 this animal flees this animal is hunting 6998 04:01:31,920 --> 04:01:34,720 so that's basically what multiple 6999 04:01:33,760 --> 04:01:37,520 inheritance is 7000 04:01:34,720 --> 04:01:40,000 it's a concept where a child class is 7001 04:01:37,520 --> 04:01:42,160 derived from more than one parent class 7002 04:01:40,000 --> 04:01:43,840 so that's basically multiple inheritance 7003 04:01:42,160 --> 04:01:45,359 i'll post all of this code to the 7004 04:01:43,840 --> 04:01:47,279 comment section down below 7005 04:01:45,359 --> 04:01:48,479 and well yeah that's how multiple 7006 04:01:47,279 --> 04:01:51,439 inheritance works 7007 04:01:48,479 --> 04:01:53,120 in python hey what's going on everybody 7008 04:01:51,439 --> 04:01:54,479 it's bro hope you're doing well 7009 04:01:53,120 --> 04:01:56,640 and in this video i'm going to teach you 7010 04:01:54,479 --> 04:01:57,040 guys about method overriding in python 7011 04:01:56,640 --> 04:02:01,680 so 7012 04:01:57,040 --> 04:02:03,760 sit back relax and enjoy the show 7013 04:02:01,680 --> 04:02:06,000 here's a super quick video on method 7014 04:02:03,760 --> 04:02:08,000 overriding i have two classes a class 7015 04:02:06,000 --> 04:02:09,760 called animal and a class called rabbit 7016 04:02:08,000 --> 04:02:11,600 rabbit inherits from the animal class 7017 04:02:09,760 --> 04:02:13,600 therefore rabbit is the child class 7018 04:02:11,600 --> 04:02:15,760 an animal is the parent class within the 7019 04:02:13,600 --> 04:02:16,960 animal class there is one method a 7020 04:02:15,760 --> 04:02:18,720 method called eat 7021 04:02:16,960 --> 04:02:21,040 and when you call this method all this 7022 04:02:18,720 --> 04:02:22,560 will do is print this animal is eating 7023 04:02:21,040 --> 04:02:24,080 the rabbit class inherits from the 7024 04:02:22,560 --> 04:02:26,720 animal class therefore 7025 04:02:24,080 --> 04:02:28,720 rabbit has access to this eat method and 7026 04:02:26,720 --> 04:02:31,040 if i were to create a rabbit object 7027 04:02:28,720 --> 04:02:32,720 and this rabbit used its eat method well 7028 04:02:31,040 --> 04:02:35,199 then this is going to print 7029 04:02:32,720 --> 04:02:37,199 this animal is eating now method of 7030 04:02:35,199 --> 04:02:38,800 writing is the ability of an object 7031 04:02:37,199 --> 04:02:41,760 oriented programming language 7032 04:02:38,800 --> 04:02:42,800 to allow a subclass also known as a 7033 04:02:41,760 --> 04:02:45,439 child class 7034 04:02:42,800 --> 04:02:46,080 to provide a specific implementation of 7035 04:02:45,439 --> 04:02:48,319 a method 7036 04:02:46,080 --> 04:02:50,080 that is already provided by one of its 7037 04:02:48,319 --> 04:02:50,640 parents in this case we're going to 7038 04:02:50,080 --> 04:02:52,640 override 7039 04:02:50,640 --> 04:02:54,239 the eat method and what we can do is 7040 04:02:52,640 --> 04:02:57,040 provide a more specific 7041 04:02:54,239 --> 04:02:59,040 implementation for the rabbit class and 7042 04:02:57,040 --> 04:02:59,920 now to override a method what we'll do 7043 04:02:59,040 --> 04:03:02,000 is that within 7044 04:02:59,920 --> 04:03:03,279 the child class we need to define a 7045 04:03:02,000 --> 04:03:05,439 method with the same 7046 04:03:03,279 --> 04:03:07,359 matching method signature that is the 7047 04:03:05,439 --> 04:03:09,120 combination of a method's name 7048 04:03:07,359 --> 04:03:10,640 plus its parameters both of these 7049 04:03:09,120 --> 04:03:13,040 together are known as a 7050 04:03:10,640 --> 04:03:14,000 method signature within the child class 7051 04:03:13,040 --> 04:03:16,000 we will define 7052 04:03:14,000 --> 04:03:17,359 and eat method with the same parameters 7053 04:03:16,000 --> 04:03:19,359 def eat 7054 04:03:17,359 --> 04:03:21,199 and in this case the only parameter is 7055 04:03:19,359 --> 04:03:23,279 self just to keep this simple 7056 04:03:21,199 --> 04:03:24,319 and what we'll do is that will provide a 7057 04:03:23,279 --> 04:03:26,319 more specific 7058 04:03:24,319 --> 04:03:28,000 implementation of the eat method 7059 04:03:26,319 --> 04:03:29,760 specifically for rabbits 7060 04:03:28,000 --> 04:03:32,080 so let's print something more closely 7061 04:03:29,760 --> 04:03:35,279 associated with rabbits such as 7062 04:03:32,080 --> 04:03:37,680 this rabbit is eating a 7063 04:03:35,279 --> 04:03:38,319 carrot now if i were to run this program 7064 04:03:37,680 --> 04:03:40,800 again 7065 04:03:38,319 --> 04:03:41,439 we will instead use this implementation 7066 04:03:40,800 --> 04:03:42,960 of the 7067 04:03:41,439 --> 04:03:44,880 method instead of the one that it 7068 04:03:42,960 --> 04:03:47,359 inherits from its parent class 7069 04:03:44,880 --> 04:03:49,680 of animal basically speaking an object 7070 04:03:47,359 --> 04:03:51,920 will use a method that is more closely 7071 04:03:49,680 --> 04:03:54,080 associated with itself first before 7072 04:03:51,920 --> 04:03:55,600 relying on a method that it may inherit 7073 04:03:54,080 --> 04:03:57,760 from a parent class 7074 04:03:55,600 --> 04:03:58,800 in this example we're using this version 7075 04:03:57,760 --> 04:04:00,880 of the eat method 7076 04:03:58,800 --> 04:04:02,800 specifically for rabbits instead of the 7077 04:04:00,880 --> 04:04:03,359 version that we inherit from the parent 7078 04:04:02,800 --> 04:04:05,760 class 7079 04:04:03,359 --> 04:04:07,760 of animal that is the basics of method 7080 04:04:05,760 --> 04:04:09,279 overriding i will post this code in the 7081 04:04:07,760 --> 04:04:11,680 comment section down below 7082 04:04:09,279 --> 04:04:14,080 and well yeah that's how to override a 7083 04:04:11,680 --> 04:04:16,080 method using python 7084 04:04:14,080 --> 04:04:17,359 hey what's going on everybody it's you 7085 04:04:16,080 --> 04:04:18,800 bro hope you're doing well 7086 04:04:17,359 --> 04:04:21,199 and in this video i'm going to teach you 7087 04:04:18,800 --> 04:04:21,600 guys all about method chaining in python 7088 04:04:21,199 --> 04:04:26,239 so 7089 04:04:21,600 --> 04:04:26,239 sit back relax and enjoy the show 7090 04:04:26,479 --> 04:04:30,239 all right everybody let's talk about 7091 04:04:28,160 --> 04:04:30,960 method training method chaining is used 7092 04:04:30,239 --> 04:04:33,680 to call 7093 04:04:30,960 --> 04:04:35,520 multiple methods sequentially and each 7094 04:04:33,680 --> 04:04:36,160 call performs an action on the same 7095 04:04:35,520 --> 04:04:38,560 object 7096 04:04:36,160 --> 04:04:40,399 and return self here's an example of how 7097 04:04:38,560 --> 04:04:43,120 method chaining would be useful 7098 04:04:40,399 --> 04:04:43,520 let's say we have a class named car car 7099 04:04:43,120 --> 04:04:46,560 has 7100 04:04:43,520 --> 04:04:47,199 four methods turn on drive break and 7101 04:04:46,560 --> 04:04:48,800 turn off 7102 04:04:47,199 --> 04:04:50,720 they will all just print a simple 7103 04:04:48,800 --> 04:04:53,040 message beginning with turn on 7104 04:04:50,720 --> 04:04:55,040 turn on what print you start the engine 7105 04:04:53,040 --> 04:04:56,960 drive will print you drive the car 7106 04:04:55,040 --> 04:04:58,239 brake will print you step on the brakes 7107 04:04:56,960 --> 04:05:00,160 and turn off will print 7108 04:04:58,239 --> 04:05:01,279 you turn off the engine let's say we 7109 04:05:00,160 --> 04:05:04,239 have a car object 7110 04:05:01,279 --> 04:05:04,880 car equals car and i would like my car 7111 04:05:04,239 --> 04:05:07,680 object 7112 04:05:04,880 --> 04:05:08,479 to use its turn on method immediately 7113 04:05:07,680 --> 04:05:10,319 followed by 7114 04:05:08,479 --> 04:05:12,319 the drive method with how we've been 7115 04:05:10,319 --> 04:05:13,359 coding things previously we may write 7116 04:05:12,319 --> 04:05:15,439 something such as 7117 04:05:13,359 --> 04:05:17,199 the name of the object in this case car 7118 04:05:15,439 --> 04:05:19,359 dot and then the method to use 7119 04:05:17,199 --> 04:05:21,040 i would like my card to use the turn on 7120 04:05:19,359 --> 04:05:23,920 method followed by 7121 04:05:21,040 --> 04:05:25,520 car dot drive so currently this will 7122 04:05:23,920 --> 04:05:27,760 take two lines of code 7123 04:05:25,520 --> 04:05:29,359 but this will print you start the engine 7124 04:05:27,760 --> 04:05:31,760 and you drive the car 7125 04:05:29,359 --> 04:05:32,880 now with method training we can call 7126 04:05:31,760 --> 04:05:34,960 multiple methods 7127 04:05:32,880 --> 04:05:36,319 sequentially now the format on this is 7128 04:05:34,960 --> 04:05:38,000 going to be a little bit different 7129 04:05:36,319 --> 04:05:39,840 and if we were to write the same code 7130 04:05:38,000 --> 04:05:40,960 using method chaining it might look a 7131 04:05:39,840 --> 04:05:43,760 little something like this 7132 04:05:40,960 --> 04:05:44,319 after the first method call we would add 7133 04:05:43,760 --> 04:05:46,080 dot 7134 04:05:44,319 --> 04:05:48,080 and then an additional method that we 7135 04:05:46,080 --> 04:05:50,399 would like to call so for example 7136 04:05:48,080 --> 04:05:51,359 after the turn on method we'll 7137 04:05:50,399 --> 04:05:53,279 immediately call 7138 04:05:51,359 --> 04:05:54,399 the drive method but there's one more 7139 04:05:53,279 --> 04:05:56,399 thing that we need to add when we're 7140 04:05:54,399 --> 04:05:59,120 using python to do method training 7141 04:05:56,399 --> 04:06:01,279 so normally this by itself will not work 7142 04:05:59,120 --> 04:06:03,279 when we call a method in python if there 7143 04:06:01,279 --> 04:06:03,920 is nothing that is returned python will 7144 04:06:03,279 --> 04:06:06,479 return 7145 04:06:03,920 --> 04:06:07,600 none so as you can see here attribute 7146 04:06:06,479 --> 04:06:10,319 error none type 7147 04:06:07,600 --> 04:06:12,720 object has no attribute of drive so we 7148 04:06:10,319 --> 04:06:14,560 need to return itself under each method 7149 04:06:12,720 --> 04:06:16,479 that we're using method training with 7150 04:06:14,560 --> 04:06:17,680 so let's add return self to each of 7151 04:06:16,479 --> 04:06:20,720 these methods 7152 04:06:17,680 --> 04:06:24,160 so let's do that for drive 7153 04:06:20,720 --> 04:06:26,800 break and the turn off method and this 7154 04:06:24,160 --> 04:06:28,640 should work now you start the engine 7155 04:06:26,800 --> 04:06:30,319 you drive the car so think of it like 7156 04:06:28,640 --> 04:06:32,720 this after we finish 7157 04:06:30,319 --> 04:06:33,680 calling this method python is going to 7158 04:06:32,720 --> 04:06:35,680 return self 7159 04:06:33,680 --> 04:06:37,040 so it's going to return car so then 7160 04:06:35,680 --> 04:06:39,680 we'll immediately use 7161 04:06:37,040 --> 04:06:41,120 car.drive now here's another example 7162 04:06:39,680 --> 04:06:43,600 let's say i would like to 7163 04:06:41,120 --> 04:06:44,640 call the break method followed by the 7164 04:06:43,600 --> 04:06:47,600 turn off method 7165 04:06:44,640 --> 04:06:48,399 so that would be car dot break 7166 04:06:47,600 --> 04:06:51,760 parentheses 7167 04:06:48,399 --> 04:06:55,040 followed by dot turn 7168 04:06:51,760 --> 04:06:55,520 off so this will print you step on the 7169 04:06:55,040 --> 04:06:57,439 brakes 7170 04:06:55,520 --> 04:06:59,120 you turn off the engine now here's an 7171 04:06:57,439 --> 04:07:01,439 extreme example let's say we would like 7172 04:06:59,120 --> 04:07:04,479 to call all four methods in order 7173 04:07:01,439 --> 04:07:08,720 so that would be car dot 7174 04:07:04,479 --> 04:07:11,840 turn on followed by dot drive 7175 04:07:08,720 --> 04:07:15,760 dot break dot 7176 04:07:11,840 --> 04:07:17,760 turn off so this will call 7177 04:07:15,760 --> 04:07:19,439 all four methods in order starting with 7178 04:07:17,760 --> 04:07:21,680 the leftmost method 7179 04:07:19,439 --> 04:07:22,880 you start the engine you drive the car 7180 04:07:21,680 --> 04:07:25,199 you step on the brakes 7181 04:07:22,880 --> 04:07:27,120 you turn off the engine now if you have 7182 04:07:25,199 --> 04:07:28,880 one long method chain like this 7183 04:07:27,120 --> 04:07:30,640 it might become difficult to read after 7184 04:07:28,880 --> 04:07:32,720 a while so what i would recommend 7185 04:07:30,640 --> 04:07:34,640 if you're doing a lot of method chaining 7186 04:07:32,720 --> 04:07:36,640 is that after each method call 7187 04:07:34,640 --> 04:07:38,159 just hit enter to move each method call 7188 04:07:36,640 --> 04:07:40,239 down to a new line 7189 04:07:38,159 --> 04:07:41,199 so it'll look something like that and 7190 04:07:40,239 --> 04:07:43,199 this backslash 7191 04:07:41,199 --> 04:07:44,880 might be inserted this is a line 7192 04:07:43,199 --> 04:07:47,279 continuation character 7193 04:07:44,880 --> 04:07:48,640 so this is a little more readable but 7194 04:07:47,279 --> 04:07:49,600 you know it still does the same thing 7195 04:07:48,640 --> 04:07:51,439 basically 7196 04:07:49,600 --> 04:07:53,040 all right everybody so that is method 7197 04:07:51,439 --> 04:07:55,600 training it's used to call 7198 04:07:53,040 --> 04:07:56,479 multiple methods sequentially and each 7199 04:07:55,600 --> 04:07:58,560 call performs an 7200 04:07:56,479 --> 04:08:00,479 action on the same object and returns 7201 04:07:58,560 --> 04:08:02,159 self if you would like a copy of this 7202 04:08:00,479 --> 04:08:04,479 code i will post all of this in the 7203 04:08:02,159 --> 04:08:06,720 comments section down below but yeah 7204 04:08:04,479 --> 04:08:08,319 that is how to do a method chaining in 7205 04:08:06,720 --> 04:08:10,720 python 7206 04:08:08,319 --> 04:08:11,840 hey what's going on everybody it's bro 7207 04:08:10,720 --> 04:08:13,359 hope you're doing well 7208 04:08:11,840 --> 04:08:15,359 and in this video i'm going to explain 7209 04:08:13,359 --> 04:08:19,840 the super function in python so 7210 04:08:15,359 --> 04:08:19,840 sit back relax and enjoy the show 7211 04:08:20,159 --> 04:08:23,359 well then since you clicked on this 7212 04:08:21,760 --> 04:08:25,279 video i should probably discuss 7213 04:08:23,359 --> 04:08:26,960 the super function now the super 7214 04:08:25,279 --> 04:08:29,840 function is used to give access 7215 04:08:26,960 --> 04:08:32,000 to the methods of a parent class it 7216 04:08:29,840 --> 04:08:34,560 returns a temporary object 7217 04:08:32,000 --> 04:08:36,560 of a parent class when used here's an 7218 04:08:34,560 --> 04:08:37,439 example of why the super function would 7219 04:08:36,560 --> 04:08:38,960 be useful 7220 04:08:37,439 --> 04:08:40,800 let's take this program that i've 7221 04:08:38,960 --> 04:08:43,040 written there's three classes 7222 04:08:40,800 --> 04:08:43,920 a class called rectangle which is the 7223 04:08:43,040 --> 04:08:47,279 parent class 7224 04:08:43,920 --> 04:08:49,199 to two children classes square and cube 7225 04:08:47,279 --> 04:08:50,960 and all we're doing is creating a square 7226 04:08:49,199 --> 04:08:53,040 object and a cube object 7227 04:08:50,960 --> 04:08:54,479 but we need to pass in a length and a 7228 04:08:53,040 --> 04:08:57,600 width for squares 7229 04:08:54,479 --> 04:08:59,520 and a length width and height for cubes 7230 04:08:57,600 --> 04:09:01,840 now with programming we don't like to 7231 04:08:59,520 --> 04:09:04,080 repeat code we like to reuse code and if 7232 04:09:01,840 --> 04:09:06,880 you take a look within my knit methods 7233 04:09:04,080 --> 04:09:08,640 for both square and cube we're repeating 7234 04:09:06,880 --> 04:09:11,760 self.length equals length 7235 04:09:08,640 --> 04:09:12,960 and self.width equals width twice for 7236 04:09:11,760 --> 04:09:14,720 our init methods 7237 04:09:12,960 --> 04:09:16,000 so one thing that we can do any 7238 04:09:14,720 --> 04:09:18,319 similarities between 7239 04:09:16,000 --> 04:09:19,120 the square and cube class we can place 7240 04:09:18,319 --> 04:09:21,520 within 7241 04:09:19,120 --> 04:09:23,439 the rectangle class and then reuse this 7242 04:09:21,520 --> 04:09:26,000 code so what i'm going to do 7243 04:09:23,439 --> 04:09:27,040 is copy this indent method within our 7244 04:09:26,000 --> 04:09:29,920 square class 7245 04:09:27,040 --> 04:09:30,800 and paste it within our rectangle parent 7246 04:09:29,920 --> 04:09:33,439 class 7247 04:09:30,800 --> 04:09:33,920 and then in order to access this init 7248 04:09:33,439 --> 04:09:36,720 method 7249 04:09:33,920 --> 04:09:37,520 we can use the super function so we no 7250 04:09:36,720 --> 04:09:39,279 longer need 7251 04:09:37,520 --> 04:09:40,640 these two lines of code what i'm going 7252 04:09:39,279 --> 04:09:42,720 to do is ask 7253 04:09:40,640 --> 04:09:44,720 the rectangle class to use its init 7254 04:09:42,720 --> 04:09:48,000 method so we're going to type 7255 04:09:44,720 --> 04:09:49,760 super parentheses dot and then the name 7256 04:09:48,000 --> 04:09:51,439 or the function of the parent class 7257 04:09:49,760 --> 04:09:53,199 that we would like to use and i would 7258 04:09:51,439 --> 04:09:55,359 like to use the init method 7259 04:09:53,199 --> 04:09:56,880 but i need to send a length and a width 7260 04:09:55,359 --> 04:09:58,560 because it requires that 7261 04:09:56,880 --> 04:10:00,159 so let's send these so we're going to 7262 04:09:58,560 --> 04:10:01,920 pass in our length and 7263 04:10:00,159 --> 04:10:03,920 our width and now we can do the same 7264 04:10:01,920 --> 04:10:06,159 thing for our cube init method 7265 04:10:03,920 --> 04:10:08,080 so i will copy this line of code we no 7266 04:10:06,159 --> 04:10:10,479 longer need these two lines of code 7267 04:10:08,080 --> 04:10:12,319 because we will be reusing our init 7268 04:10:10,479 --> 04:10:14,800 method within the parent class 7269 04:10:12,319 --> 04:10:16,960 of rectangle but we'll still need to 7270 04:10:14,800 --> 04:10:19,279 keep this line in to assign the height 7271 04:10:16,960 --> 04:10:20,880 because that's a difference between our 7272 04:10:19,279 --> 04:10:23,120 square and cube class 7273 04:10:20,880 --> 04:10:24,000 and now we can just reuse this init 7274 04:10:23,120 --> 04:10:26,080 method within 7275 04:10:24,000 --> 04:10:27,760 the rectangle parent class now i'm 7276 04:10:26,080 --> 04:10:28,399 thinking that we should probably test to 7277 04:10:27,760 --> 04:10:30,239 see if 7278 04:10:28,399 --> 04:10:31,840 length and width are assigned to these 7279 04:10:30,239 --> 04:10:33,439 attributes and one way 7280 04:10:31,840 --> 04:10:35,120 that i think would be fun would be to 7281 04:10:33,439 --> 04:10:37,199 create an area method 7282 04:10:35,120 --> 04:10:38,960 for squares and a volume method for 7283 04:10:37,199 --> 04:10:41,359 cubes so let's define 7284 04:10:38,960 --> 04:10:42,560 an area method within the square class 7285 04:10:41,359 --> 04:10:46,319 def 7286 04:10:42,560 --> 04:10:49,520 area and i would like to return self 7287 04:10:46,319 --> 04:10:52,640 dot length times self 7288 04:10:49,520 --> 04:10:54,239 dot width and we can create a volume 7289 04:10:52,640 --> 04:10:57,359 method within cubes 7290 04:10:54,239 --> 04:10:58,399 so let's do that def let's change this 7291 04:10:57,359 --> 04:11:00,960 to volume 7292 04:10:58,399 --> 04:11:02,319 we'll multiply it length times width 7293 04:11:00,960 --> 04:11:05,359 times height 7294 04:11:02,319 --> 04:11:07,920 but we need to type self dot pipe 7295 04:11:05,359 --> 04:11:08,880 and we can call these methods to get the 7296 04:11:07,920 --> 04:11:11,760 area 7297 04:11:08,880 --> 04:11:13,680 of a square or the volume of a cube 7298 04:11:11,760 --> 04:11:17,600 let's print 7299 04:11:13,680 --> 04:11:21,439 square dot area 7300 04:11:17,600 --> 04:11:25,120 and then cube dot 7301 04:11:21,439 --> 04:11:27,680 volume so if our init method 7302 04:11:25,120 --> 04:11:28,720 is actually being used we should have a 7303 04:11:27,680 --> 04:11:31,920 length and the width 7304 04:11:28,720 --> 04:11:33,040 assigned to these attributes and it 7305 04:11:31,920 --> 04:11:35,439 looks like we do 7306 04:11:33,040 --> 04:11:37,520 in conclusion the super function is used 7307 04:11:35,439 --> 04:11:38,560 to give access to the methods of a 7308 04:11:37,520 --> 04:11:40,960 parent class 7309 04:11:38,560 --> 04:11:43,040 it returns a temporary object of a 7310 04:11:40,960 --> 04:11:43,600 parent class when used in order to 7311 04:11:43,040 --> 04:11:46,080 access 7312 04:11:43,600 --> 04:11:46,880 the methods of the parent class also 7313 04:11:46,080 --> 04:11:49,920 known as 7314 04:11:46,880 --> 04:11:52,560 a super class and in this example within 7315 04:11:49,920 --> 04:11:55,040 the init methods of both square and cube 7316 04:11:52,560 --> 04:11:56,159 we immediately called the init method of 7317 04:11:55,040 --> 04:11:58,560 the parent class 7318 04:11:56,159 --> 04:11:59,520 to pass in some arguments that both of 7319 04:11:58,560 --> 04:12:01,600 these classes 7320 04:11:59,520 --> 04:12:03,520 have in common so if you would like a 7321 04:12:01,600 --> 04:12:05,520 copy of this code i will post all of 7322 04:12:03,520 --> 04:12:07,760 this in the comment section down below 7323 04:12:05,520 --> 04:12:09,840 but yeah that is how the super function 7324 04:12:07,760 --> 04:12:11,920 works in python 7325 04:12:09,840 --> 04:12:12,880 hey what's going on everybody it's bro 7326 04:12:11,920 --> 04:12:14,479 hope you're doing well 7327 04:12:12,880 --> 04:12:17,279 and in this video i'm going to explain 7328 04:12:14,479 --> 04:12:21,920 how abstract classes work in python so 7329 04:12:17,279 --> 04:12:23,600 sit back relax and enjoy the show 7330 04:12:21,920 --> 04:12:25,040 hey what's good everybody let's talk 7331 04:12:23,600 --> 04:12:27,760 about abstract classes 7332 04:12:25,040 --> 04:12:30,080 now abstract classes prevent a user from 7333 04:12:27,760 --> 04:12:32,479 creating an object of that class 7334 04:12:30,080 --> 04:12:33,359 think of an abstract class more as a 7335 04:12:32,479 --> 04:12:35,840 template 7336 04:12:33,359 --> 04:12:37,439 it's an idea it's not real it's like a 7337 04:12:35,840 --> 04:12:40,560 ghost class basically 7338 04:12:37,439 --> 04:12:42,960 plus an abstract class compels a user to 7339 04:12:40,560 --> 04:12:44,000 override any abstract methods within a 7340 04:12:42,960 --> 04:12:45,680 child class 7341 04:12:44,000 --> 04:12:47,279 so it's also a form of checks and 7342 04:12:45,680 --> 04:12:48,000 balances as well which we'll discuss 7343 04:12:47,279 --> 04:12:49,680 later 7344 04:12:48,000 --> 04:12:51,359 now here's a small program that i've 7345 04:12:49,680 --> 04:12:53,680 written let's say we have 7346 04:12:51,359 --> 04:12:54,800 three classes class vehicle car and 7347 04:12:53,680 --> 04:12:57,040 motorcycle 7348 04:12:54,800 --> 04:12:59,199 car and motorcycle are the children 7349 04:12:57,040 --> 04:13:01,520 classes of the vehicle class 7350 04:12:59,199 --> 04:13:02,479 now within this vehicle class we have a 7351 04:13:01,520 --> 04:13:04,319 go method 7352 04:13:02,479 --> 04:13:05,760 but i'm currently only defining it not 7353 04:13:04,319 --> 04:13:06,880 actually implementing it so i'm just 7354 04:13:05,760 --> 04:13:09,199 writing pass 7355 04:13:06,880 --> 04:13:10,880 now within car and motorcycle we're 7356 04:13:09,199 --> 04:13:12,880 overriding this method and creating our 7357 04:13:10,880 --> 04:13:13,760 own implementation of the skull method 7358 04:13:12,880 --> 04:13:16,080 that we inherit 7359 04:13:13,760 --> 04:13:16,960 the go method for the car will print you 7360 04:13:16,080 --> 04:13:18,479 drive the car 7361 04:13:16,960 --> 04:13:20,960 and the go method for the motorcycle 7362 04:13:18,479 --> 04:13:22,880 class will print you ride the motorcycle 7363 04:13:20,960 --> 04:13:24,000 now currently i'm creating an object of 7364 04:13:22,880 --> 04:13:26,640 each of these classes 7365 04:13:24,000 --> 04:13:27,120 i have a generic vehicle object car 7366 04:13:26,640 --> 04:13:29,439 object 7367 04:13:27,120 --> 04:13:30,319 and motorcycle object now after running 7368 04:13:29,439 --> 04:13:32,000 this program 7369 04:13:30,319 --> 04:13:34,239 currently the go method within our 7370 04:13:32,000 --> 04:13:36,560 vehicle class is not being implemented 7371 04:13:34,239 --> 04:13:38,800 so this won't print anything but the go 7372 04:13:36,560 --> 04:13:41,040 method within our car class will print 7373 04:13:38,800 --> 04:13:43,199 you drive the car and the go method 7374 04:13:41,040 --> 04:13:45,279 within our motorcycle class will print 7375 04:13:43,199 --> 04:13:47,279 you ride the motorcycle with that being 7376 04:13:45,279 --> 04:13:47,680 said let's pretend that we're coding the 7377 04:13:47,279 --> 04:13:49,600 next 7378 04:13:47,680 --> 04:13:52,239 need for speed game and we would like a 7379 04:13:49,600 --> 04:13:54,640 user to create an object from a specific 7380 04:13:52,239 --> 04:13:56,560 kind of vehicle whether it's a car from 7381 04:13:54,640 --> 04:13:57,840 the car class or a motorcycle from the 7382 04:13:56,560 --> 04:13:59,520 motorcycle class 7383 04:13:57,840 --> 04:14:02,159 we would like to prevent a user from 7384 04:13:59,520 --> 04:14:04,640 creating an object of the vehicle class 7385 04:14:02,159 --> 04:14:05,840 because the vehicle class is too generic 7386 04:14:04,640 --> 04:14:06,800 we do not have all of the 7387 04:14:05,840 --> 04:14:09,359 implementations 7388 04:14:06,800 --> 04:14:11,359 set up for a vehicle it would be like 7389 04:14:09,359 --> 04:14:12,399 somebody unlocked the ghost car or the 7390 04:14:11,359 --> 04:14:13,920 invisible car 7391 04:14:12,399 --> 04:14:16,319 which doesn't actually exist and is 7392 04:14:13,920 --> 04:14:17,520 missing a lot of features we need a user 7393 04:14:16,319 --> 04:14:19,760 to create an object 7394 04:14:17,520 --> 04:14:21,760 from a child class because these are 7395 04:14:19,760 --> 04:14:23,760 fully fleshed out and one way in which 7396 04:14:21,760 --> 04:14:25,040 we can prevent a user from creating an 7397 04:14:23,760 --> 04:14:27,040 object of this class 7398 04:14:25,040 --> 04:14:28,159 as well to turn this class into an 7399 04:14:27,040 --> 04:14:30,159 abstract class 7400 04:14:28,159 --> 04:14:32,239 which is what this video is all about go 7401 04:14:30,159 --> 04:14:34,159 figure now the first step to creating an 7402 04:14:32,239 --> 04:14:34,800 abstract class is that we'll need some 7403 04:14:34,159 --> 04:14:37,120 imports 7404 04:14:34,800 --> 04:14:37,840 so at the top of your program write this 7405 04:14:37,120 --> 04:14:41,279 from 7406 04:14:37,840 --> 04:14:44,560 abc abc is an acronym for abstract 7407 04:14:41,279 --> 04:14:48,399 based class abc from abc 7408 04:14:44,560 --> 04:14:51,520 import abc all capital comma 7409 04:14:48,399 --> 04:14:53,760 abstract method all right 7410 04:14:51,520 --> 04:14:56,000 now with our vehicle class the class 7411 04:14:53,760 --> 04:14:58,239 that you intend to be the abstract class 7412 04:14:56,000 --> 04:14:59,040 add us out of parentheses and our 7413 04:14:58,239 --> 04:15:02,080 vehicle class 7414 04:14:59,040 --> 04:15:04,000 will inherit from the abc class 7415 04:15:02,080 --> 04:15:05,920 and now with any methods within your 7416 04:15:04,000 --> 04:15:09,920 vehicle class you're going to add 7417 04:15:05,920 --> 04:15:12,159 this decorator at the top at abstract 7418 04:15:09,920 --> 04:15:13,680 method all right we should now be 7419 04:15:12,159 --> 04:15:16,239 prevented from creating a 7420 04:15:13,680 --> 04:15:17,600 vehicle object our vehicle class is now 7421 04:15:16,239 --> 04:15:19,600 an abstract class 7422 04:15:17,600 --> 04:15:21,040 and we cannot give it a physical form a 7423 04:15:19,600 --> 04:15:22,640 physical manifestation 7424 04:15:21,040 --> 04:15:24,319 and if we attempt to you can see here 7425 04:15:22,640 --> 04:15:26,800 that we have a type error 7426 04:15:24,319 --> 04:15:28,800 can't instantiate abstract class from 7427 04:15:26,800 --> 04:15:31,680 vehicle with abstract methods 7428 04:15:28,800 --> 04:15:32,640 go so an abstract class is a class which 7429 04:15:31,680 --> 04:15:35,359 contains 7430 04:15:32,640 --> 04:15:36,319 one or more abstract methods and an 7431 04:15:35,359 --> 04:15:38,960 abstract method 7432 04:15:36,319 --> 04:15:40,960 is a method that has a declaration but 7433 04:15:38,960 --> 04:15:43,359 does not have an implementation 7434 04:15:40,960 --> 04:15:44,000 now by definition an abstract class 7435 04:15:43,359 --> 04:15:47,040 contains 7436 04:15:44,000 --> 04:15:49,199 one or more abstract methods if i remove 7437 04:15:47,040 --> 04:15:50,319 this one abstract method within our 7438 04:15:49,199 --> 04:15:52,319 vehicle class 7439 04:15:50,319 --> 04:15:54,560 well we could still create a vehicle 7440 04:15:52,319 --> 04:15:56,560 object and i'm just going to turn this 7441 04:15:54,560 --> 04:15:58,479 vehicle.go line into a comment because 7442 04:15:56,560 --> 04:16:00,159 we don't have a go method anymore 7443 04:15:58,479 --> 04:16:02,239 as you can see we can still create a 7444 04:16:00,159 --> 04:16:04,080 vehicle object which we would like to 7445 04:16:02,239 --> 04:16:06,960 prevent a user from doing 7446 04:16:04,080 --> 04:16:08,479 so we need at least one abstract method 7447 04:16:06,960 --> 04:16:10,239 within our vehicle class 7448 04:16:08,479 --> 04:16:11,520 so i'm going to go ahead and add this go 7449 04:16:10,239 --> 04:16:13,279 method back in 7450 04:16:11,520 --> 04:16:15,040 an additional feature of abstract 7451 04:16:13,279 --> 04:16:17,680 classes is that they compel 7452 04:16:15,040 --> 04:16:19,359 a user to override any abstract methods 7453 04:16:17,680 --> 04:16:21,040 within a child class 7454 04:16:19,359 --> 04:16:22,640 a method that is abstract has a 7455 04:16:21,040 --> 04:16:25,279 declaration but does not have 7456 04:16:22,640 --> 04:16:26,720 an implementation so let's say that we 7457 04:16:25,279 --> 04:16:29,120 create our vehicle class 7458 04:16:26,720 --> 04:16:29,840 our abstract vehicle class and now we're 7459 04:16:29,120 --> 04:16:31,439 creating 7460 04:16:29,840 --> 04:16:33,600 the children classes of car and 7461 04:16:31,439 --> 04:16:35,920 motorcycle and let's say i'm not paying 7462 04:16:33,600 --> 04:16:36,880 attention and i forget to add a go 7463 04:16:35,920 --> 04:16:38,640 method 7464 04:16:36,880 --> 04:16:40,640 so if i just write pass within my 7465 04:16:38,640 --> 04:16:43,279 motorcycle class i'm currently missing a 7466 04:16:40,640 --> 04:16:45,439 go method within my motorcycle class 7467 04:16:43,279 --> 04:16:47,920 well python is going to prevent me from 7468 04:16:45,439 --> 04:16:50,319 running this because we have a type air 7469 04:16:47,920 --> 04:16:52,399 can't instantiate abstract class vehicle 7470 04:16:50,319 --> 04:16:55,120 with abstract methods of go 7471 04:16:52,399 --> 04:16:57,439 so by inheriting from an abstract class 7472 04:16:55,120 --> 04:16:59,680 it's a good form of checks and balances 7473 04:16:57,439 --> 04:17:00,560 to be sure that your children classes 7474 04:16:59,680 --> 04:17:02,880 are not missing 7475 04:17:00,560 --> 04:17:04,000 any implementations of any methods that 7476 04:17:02,880 --> 04:17:05,520 they inherit 7477 04:17:04,000 --> 04:17:07,120 here's an additional way of thinking of 7478 04:17:05,520 --> 04:17:08,880 this our vehicle class 7479 04:17:07,120 --> 04:17:10,640 is telling its children if you're going 7480 04:17:08,880 --> 04:17:11,279 to inherit from me then you need to 7481 04:17:10,640 --> 04:17:13,359 override 7482 04:17:11,279 --> 04:17:14,880 this abstract method of mine and if you 7483 04:17:13,359 --> 04:17:16,080 don't well i'm not going to let you be 7484 04:17:14,880 --> 04:17:18,479 instantiated 7485 04:17:16,080 --> 04:17:19,600 so in order for us to create a cart and 7486 04:17:18,479 --> 04:17:22,000 motorcycle class 7487 04:17:19,600 --> 04:17:23,760 we need to override the go method that 7488 04:17:22,000 --> 04:17:24,479 they inherit from its parent class of 7489 04:17:23,760 --> 04:17:26,880 vehicle 7490 04:17:24,479 --> 04:17:29,040 and provide its own implementation now 7491 04:17:26,880 --> 04:17:30,000 let's say we create an additional method 7492 04:17:29,040 --> 04:17:32,880 let's say we have 7493 04:17:30,000 --> 04:17:33,520 a stop method so let's define that def 7494 04:17:32,880 --> 04:17:35,279 stop 7495 04:17:33,520 --> 04:17:37,040 for the time being i'll write pass and 7496 04:17:35,279 --> 04:17:40,560 to make this an abstract method 7497 04:17:37,040 --> 04:17:44,479 add this decorator at abstract 7498 04:17:40,560 --> 04:17:44,800 method now car and motorcycle both need 7499 04:17:44,479 --> 04:17:46,880 to 7500 04:17:44,800 --> 04:17:47,920 implement that method of stop it's 7501 04:17:46,880 --> 04:17:49,920 currently missing it 7502 04:17:47,920 --> 04:17:51,680 as you can see here class car must 7503 04:17:49,920 --> 04:17:52,800 implement all abstract methods 7504 04:17:51,680 --> 04:17:54,800 and the same thing applies for a 7505 04:17:52,800 --> 04:17:56,880 motorcycle as well so we need to 7506 04:17:54,800 --> 04:17:58,080 override this method and provide its own 7507 04:17:56,880 --> 04:18:01,040 implementation 7508 04:17:58,080 --> 04:18:02,239 so let's remove that decorator and let's 7509 04:18:01,040 --> 04:18:06,880 print something 7510 04:18:02,239 --> 04:18:08,239 let's print this car is stopped 7511 04:18:06,880 --> 04:18:10,640 and i'll do the same thing for 7512 04:18:08,239 --> 04:18:14,399 motorcycle 7513 04:18:10,640 --> 04:18:18,239 so def stop let's say 7514 04:18:14,399 --> 04:18:21,600 this motorcycle 7515 04:18:18,239 --> 04:18:22,880 is stopped and we'll call the stop 7516 04:18:21,600 --> 04:18:26,239 method for each of these 7517 04:18:22,880 --> 04:18:29,760 types of vehicles car dot stop 7518 04:18:26,239 --> 04:18:32,159 and motorcycle dot stop 7519 04:18:29,760 --> 04:18:33,520 all right and we can now create a car 7520 04:18:32,159 --> 04:18:36,080 and motorcycle object 7521 04:18:33,520 --> 04:18:38,000 because we are overriding both of these 7522 04:18:36,080 --> 04:18:39,520 abstract methods found within the parent 7523 04:18:38,000 --> 04:18:41,279 class of vehicle 7524 04:18:39,520 --> 04:18:42,800 all right everybody so that's the basics 7525 04:18:41,279 --> 04:18:45,359 of abstract classes 7526 04:18:42,800 --> 04:18:46,720 it's a class which contains one or more 7527 04:18:45,359 --> 04:18:48,720 abstract methods 7528 04:18:46,720 --> 04:18:50,000 and an abstract method is a method that 7529 04:18:48,720 --> 04:18:52,640 has a declaration 7530 04:18:50,000 --> 04:18:54,960 but does not have an implementation and 7531 04:18:52,640 --> 04:18:56,560 the benefits of using an abstract class 7532 04:18:54,960 --> 04:18:58,800 is that they prevent a user from 7533 04:18:56,560 --> 04:19:01,040 creating an object of that class 7534 04:18:58,800 --> 04:19:02,960 plus it compels a user to override any 7535 04:19:01,040 --> 04:19:03,920 abstract methods found within a child 7536 04:19:02,960 --> 04:19:05,680 class 7537 04:19:03,920 --> 04:19:07,279 so if you would like a copy of this code 7538 04:19:05,680 --> 04:19:08,479 i will post all of this in the comments 7539 04:19:07,279 --> 04:19:10,560 section down below 7540 04:19:08,479 --> 04:19:12,399 but yeah that is how abstract classes 7541 04:19:10,560 --> 04:19:14,319 work in python 7542 04:19:12,399 --> 04:19:15,680 hey what's going on everybody it's you 7543 04:19:14,319 --> 04:19:17,199 bro hope you're doing well 7544 04:19:15,680 --> 04:19:18,960 and in this video i'm going to explain 7545 04:19:17,199 --> 04:19:22,239 how we can pass objects as 7546 04:19:18,960 --> 04:19:24,640 arguments so sit back relax and enjoy 7547 04:19:22,239 --> 04:19:24,640 the show 7548 04:19:24,800 --> 04:19:28,399 ladies and gentlemen in today's video 7549 04:19:26,880 --> 04:19:28,880 i'm going to be demonstrating how we can 7550 04:19:28,399 --> 04:19:31,199 pass 7551 04:19:28,880 --> 04:19:32,560 objects as arguments for this example 7552 04:19:31,199 --> 04:19:35,279 let's say we have a class 7553 04:19:32,560 --> 04:19:36,159 car and there is one class variable 7554 04:19:35,279 --> 04:19:38,640 called color 7555 04:19:36,159 --> 04:19:40,720 color equals none what we'll be doing is 7556 04:19:38,640 --> 04:19:43,040 calling a function that accepts an 7557 04:19:40,720 --> 04:19:46,399 object as an argument as well as a color 7558 04:19:43,040 --> 04:19:48,399 so let's create a function named change 7559 04:19:46,399 --> 04:19:50,560 color now make sure when you define this 7560 04:19:48,399 --> 04:19:52,640 function it's not within the cart class 7561 04:19:50,560 --> 04:19:53,920 then technically this would be a method 7562 04:19:52,640 --> 04:19:55,520 of the car class 7563 04:19:53,920 --> 04:19:57,600 what we would like is a separate 7564 04:19:55,520 --> 04:19:59,600 function outside of the car class 7565 04:19:57,600 --> 04:20:01,680 now we need to set up some parameters we 7566 04:19:59,600 --> 04:20:02,880 would like to accept a car object as 7567 04:20:01,680 --> 04:20:05,040 well as a color 7568 04:20:02,880 --> 04:20:06,720 so for the first parameter let's say a 7569 04:20:05,040 --> 04:20:08,640 car and make sure this is in 7570 04:20:06,720 --> 04:20:10,640 uppercase or capital because python 7571 04:20:08,640 --> 04:20:11,600 doesn't like that argument names should 7572 04:20:10,640 --> 04:20:13,520 be lowercase 7573 04:20:11,600 --> 04:20:15,120 so when we pass in a car we will give it 7574 04:20:13,520 --> 04:20:17,120 a nickname of car 7575 04:20:15,120 --> 04:20:19,120 and we would also like to pass in a 7576 04:20:17,120 --> 04:20:19,760 color so let's set up a parameter called 7577 04:20:19,120 --> 04:20:22,720 color 7578 04:20:19,760 --> 04:20:24,800 now when we pass in a car we will assign 7579 04:20:22,720 --> 04:20:26,159 that car's color with whatever color 7580 04:20:24,800 --> 04:20:29,520 that we receive so 7581 04:20:26,159 --> 04:20:31,439 car dot color equals color 7582 04:20:29,520 --> 04:20:33,040 and now let's create some car objects so 7583 04:20:31,439 --> 04:20:36,720 let's say car one 7584 04:20:33,040 --> 04:20:40,000 equals car car two equals car 7585 04:20:36,720 --> 04:20:40,880 and car three equals cart currently if i 7586 04:20:40,000 --> 04:20:43,199 were to print 7587 04:20:40,880 --> 04:20:44,560 their colors this is what we'll see 7588 04:20:43,199 --> 04:20:47,279 print car one 7589 04:20:44,560 --> 04:20:48,640 dot color and repeat the steps for two 7590 04:20:47,279 --> 04:20:50,239 and three 7591 04:20:48,640 --> 04:20:52,399 and we're not calling this function yet 7592 04:20:50,239 --> 04:20:53,040 so all of these colors should be set to 7593 04:20:52,399 --> 04:20:56,080 none 7594 04:20:53,040 --> 04:20:58,479 they are basically cars with no color 7595 04:20:56,080 --> 04:21:00,319 they're i don't know iron or something 7596 04:20:58,479 --> 04:21:02,560 whatever cars are made out of aluminum 7597 04:21:00,319 --> 04:21:03,680 plastic stuff like that okay so now 7598 04:21:02,560 --> 04:21:06,399 let's call this function 7599 04:21:03,680 --> 04:21:08,000 change color perhaps right here so in 7600 04:21:06,399 --> 04:21:09,040 order to call this function we need to 7601 04:21:08,000 --> 04:21:11,600 pass in 7602 04:21:09,040 --> 04:21:12,319 a car object as well as a color so let's 7603 04:21:11,600 --> 04:21:15,120 pass in 7604 04:21:12,319 --> 04:21:17,840 car one and a color perhaps red and 7605 04:21:15,120 --> 04:21:21,199 we'll repeat the steps for two and three 7606 04:21:17,840 --> 04:21:24,880 so let's say car two will be white 7607 04:21:21,199 --> 04:21:27,359 and car 3 will be blue 7608 04:21:24,880 --> 04:21:28,080 now after calling this function we have 7609 04:21:27,359 --> 04:21:30,080 a car 7610 04:21:28,080 --> 04:21:32,080 object and a color and we're assigning 7611 04:21:30,080 --> 04:21:33,439 the color of our car to whatever color 7612 04:21:32,080 --> 04:21:36,640 that we receive 7613 04:21:33,439 --> 04:21:39,199 now these cars in order car 1 2 and 3 7614 04:21:36,640 --> 04:21:39,920 are red white and blue now one thing 7615 04:21:39,199 --> 04:21:41,760 that you should know 7616 04:21:39,920 --> 04:21:42,960 is that the name of this parameter that 7617 04:21:41,760 --> 04:21:45,040 accepts our object 7618 04:21:42,960 --> 04:21:46,239 doesn't necessarily need the same name 7619 04:21:45,040 --> 04:21:47,600 as the class name 7620 04:21:46,239 --> 04:21:49,359 that created the object that we're 7621 04:21:47,600 --> 04:21:51,040 passing in we could name this something 7622 04:21:49,359 --> 04:21:52,880 else entirely perhaps 7623 04:21:51,040 --> 04:21:54,239 vehicle you'll just want to be sure that 7624 04:21:52,880 --> 04:21:55,760 this is at least descriptive 7625 04:21:54,239 --> 04:21:58,239 of the kind of object that you would 7626 04:21:55,760 --> 04:22:00,880 like to pass in and we're not limited to 7627 04:21:58,239 --> 04:22:02,319 just passing in car objects we can pass 7628 04:22:00,880 --> 04:22:04,800 in all sorts of objects 7629 04:22:02,319 --> 04:22:05,520 but we need to pass in an object as well 7630 04:22:04,800 --> 04:22:07,600 as a color 7631 04:22:05,520 --> 04:22:08,560 so this time let's create a class called 7632 04:22:07,600 --> 04:22:12,239 motorcycle 7633 04:22:08,560 --> 04:22:14,319 class motorcycle and this class will 7634 04:22:12,239 --> 04:22:16,560 also have a color set to none 7635 04:22:14,319 --> 04:22:18,319 so what we're doing is actually reusing 7636 04:22:16,560 --> 04:22:19,439 this function for all sorts of different 7637 04:22:18,319 --> 04:22:21,359 kinds of vehicles 7638 04:22:19,439 --> 04:22:22,880 now let's create a vehicle i'll just 7639 04:22:21,359 --> 04:22:26,399 name this bike one for short 7640 04:22:22,880 --> 04:22:27,040 bike one equals motorcycle now let's 7641 04:22:26,399 --> 04:22:30,479 call the 7642 04:22:27,040 --> 04:22:33,600 change color function and we can pass in 7643 04:22:30,479 --> 04:22:34,479 an object as well as color this time we 7644 04:22:33,600 --> 04:22:37,520 will pass in 7645 04:22:34,479 --> 04:22:37,840 our bike object as well as a color i 7646 04:22:37,520 --> 04:22:42,000 think 7647 04:22:37,840 --> 04:22:46,080 black counts as a color and let's print 7648 04:22:42,000 --> 04:22:48,880 bike once color 7649 04:22:46,080 --> 04:22:50,319 and this should work we have red white 7650 04:22:48,880 --> 04:22:52,800 blue and black 7651 04:22:50,319 --> 04:22:54,319 in conclusion we can pass objects as 7652 04:22:52,800 --> 04:22:55,920 arguments to a function 7653 04:22:54,319 --> 04:22:58,640 much like what we've been doing with 7654 04:22:55,920 --> 04:23:01,279 variables however the type of objects 7655 04:22:58,640 --> 04:23:03,680 that we pass in may be limited based on 7656 04:23:01,279 --> 04:23:05,760 the required attributes and methods 7657 04:23:03,680 --> 04:23:08,000 that that given class or object might 7658 04:23:05,760 --> 04:23:09,040 have and we'll get more into this on the 7659 04:23:08,000 --> 04:23:11,359 next video on 7660 04:23:09,040 --> 04:23:13,359 duct typing so if you would like a copy 7661 04:23:11,359 --> 04:23:15,040 of all this code i will post all of this 7662 04:23:13,359 --> 04:23:17,760 in the comment section down below 7663 04:23:15,040 --> 04:23:18,560 but yeah that is how to pass objects as 7664 04:23:17,760 --> 04:23:21,040 arguments 7665 04:23:18,560 --> 04:23:21,600 using python hey what's going on 7666 04:23:21,040 --> 04:23:23,600 everybody 7667 04:23:21,600 --> 04:23:25,199 it's bro hope you're doing well and in 7668 04:23:23,600 --> 04:23:26,640 this video i'm going to explain duck 7669 04:23:25,199 --> 04:23:31,359 typing in python so 7670 04:23:26,640 --> 04:23:33,520 sit back relax and enjoy the show 7671 04:23:31,359 --> 04:23:34,880 i hope you all like ducks because that's 7672 04:23:33,520 --> 04:23:37,199 the topic of this video 7673 04:23:34,880 --> 04:23:39,680 duct typing is the concept where the 7674 04:23:37,199 --> 04:23:41,680 class of an object is less important 7675 04:23:39,680 --> 04:23:43,199 than the methods and or attributes that 7676 04:23:41,680 --> 04:23:45,439 that class might have 7677 04:23:43,199 --> 04:23:47,760 the class type is not checked if the 7678 04:23:45,439 --> 04:23:50,080 minimum methods and or attributes 7679 04:23:47,760 --> 04:23:50,880 are present it's based off of a popular 7680 04:23:50,080 --> 04:23:52,720 phrase 7681 04:23:50,880 --> 04:23:54,800 if it walks like a duck and it quacks 7682 04:23:52,720 --> 04:23:57,199 like a duck then it must be a duck 7683 04:23:54,800 --> 04:23:59,120 here's an example i have two classes 7684 04:23:57,199 --> 04:24:01,760 class duck and class chicken 7685 04:23:59,120 --> 04:24:02,399 both duck and chicken have walk and talk 7686 04:24:01,760 --> 04:24:04,159 methods 7687 04:24:02,399 --> 04:24:06,239 but they print something different with 7688 04:24:04,159 --> 04:24:06,880 the duck class the walk method will 7689 04:24:06,239 --> 04:24:09,359 print 7690 04:24:06,880 --> 04:24:10,000 this duck is walking and the duck talk 7691 04:24:09,359 --> 04:24:12,640 method will 7692 04:24:10,000 --> 04:24:13,120 print this duck is quacking the chicken 7693 04:24:12,640 --> 04:24:15,520 class 7694 04:24:13,120 --> 04:24:16,720 has the same named methods walk and talk 7695 04:24:15,520 --> 04:24:17,199 but they print something slightly 7696 04:24:16,720 --> 04:24:19,439 different 7697 04:24:17,199 --> 04:24:20,640 a slightly different variation the print 7698 04:24:19,439 --> 04:24:22,560 method will print 7699 04:24:20,640 --> 04:24:23,760 this chicken is walking and the talk 7700 04:24:22,560 --> 04:24:25,920 method will print 7701 04:24:23,760 --> 04:24:28,080 this chicken is clucking now let's say 7702 04:24:25,920 --> 04:24:30,960 we have a third class a class 7703 04:24:28,080 --> 04:24:30,960 called person 7704 04:24:32,159 --> 04:24:35,600 now there is one method within our 7705 04:24:34,080 --> 04:24:37,520 person class 7706 04:24:35,600 --> 04:24:39,520 let's say we're attempting to catch a 7707 04:24:37,520 --> 04:24:41,680 duck 7708 04:24:39,520 --> 04:24:42,560 we'll pass in self and we need to pass 7709 04:24:41,680 --> 04:24:45,920 in a 7710 04:24:42,560 --> 04:24:48,960 duck object as an argument so let's type 7711 04:24:45,920 --> 04:24:53,199 duck now within this method 7712 04:24:48,960 --> 04:24:56,720 we'll have our duck use its walk method 7713 04:24:53,199 --> 04:24:58,560 as well as its talk method duck.walk and 7714 04:24:56,720 --> 04:25:02,000 duck.talk 7715 04:24:58,560 --> 04:25:02,000 and then maybe we'll print something 7716 04:25:02,159 --> 04:25:09,040 such as you caught the critter 7717 04:25:07,279 --> 04:25:10,960 now let's create an object from each of 7718 04:25:09,040 --> 04:25:14,399 these classes let's create a 7719 04:25:10,960 --> 04:25:17,760 duck object duck equals duck 7720 04:25:14,399 --> 04:25:21,279 a chicken object chicken equals chicken 7721 04:25:17,760 --> 04:25:23,520 and lastly person person equals person 7722 04:25:21,279 --> 04:25:24,720 now if we would like our person to use 7723 04:25:23,520 --> 04:25:27,520 the catch method 7724 04:25:24,720 --> 04:25:29,680 we need to pass in a duck as an argument 7725 04:25:27,520 --> 04:25:32,159 so with our person let's type person 7726 04:25:29,680 --> 04:25:34,159 dot catch and we need to send a duck 7727 04:25:32,159 --> 04:25:35,120 object because that's a required 7728 04:25:34,159 --> 04:25:37,120 parameter here 7729 04:25:35,120 --> 04:25:39,199 so we're passing in our duck object that 7730 04:25:37,120 --> 04:25:39,680 we created and as soon as we catch the 7731 04:25:39,199 --> 04:25:41,680 duck 7732 04:25:39,680 --> 04:25:43,199 it's going to use its walk and talk 7733 04:25:41,680 --> 04:25:45,279 method and then we'll print 7734 04:25:43,199 --> 04:25:46,560 you caught the critter so as you would 7735 04:25:45,279 --> 04:25:48,319 expect this will print 7736 04:25:46,560 --> 04:25:50,399 this duck is walking the stuck is 7737 04:25:48,319 --> 04:25:53,439 quacking you caught the critter 7738 04:25:50,399 --> 04:25:56,399 now with duct typing we can pass in 7739 04:25:53,439 --> 04:25:56,720 a different type of object as long as it 7740 04:25:56,399 --> 04:25:59,359 has 7741 04:25:56,720 --> 04:26:00,239 the same methods and or attributes as 7742 04:25:59,359 --> 04:26:02,000 our duck 7743 04:26:00,239 --> 04:26:03,520 we could pass in a chicken because a 7744 04:26:02,000 --> 04:26:06,239 chicken can also walk 7745 04:26:03,520 --> 04:26:06,800 and talk so this time let's pass in our 7746 04:26:06,239 --> 04:26:09,279 chicken 7747 04:26:06,800 --> 04:26:10,960 as an argument and this will still work 7748 04:26:09,279 --> 04:26:13,680 even though our parameter is set up to 7749 04:26:10,960 --> 04:26:15,439 take a duck object 7750 04:26:13,680 --> 04:26:17,120 this chicken is walking this chicken is 7751 04:26:15,439 --> 04:26:19,359 clucking you caught the critter 7752 04:26:17,120 --> 04:26:21,840 in layman's terms think of it like this 7753 04:26:19,359 --> 04:26:23,600 python is examining this chicken object 7754 04:26:21,840 --> 04:26:25,439 it's using its walk method it's using 7755 04:26:23,600 --> 04:26:27,439 its talk method which are required 7756 04:26:25,439 --> 04:26:29,439 and python is saying well that's close 7757 04:26:27,439 --> 04:26:30,640 enough remember if it walks like a duck 7758 04:26:29,439 --> 04:26:33,279 and it quacks like a duck 7759 04:26:30,640 --> 04:26:35,920 then it must be a duck so duct typing is 7760 04:26:33,279 --> 04:26:38,000 the concept where the class of an object 7761 04:26:35,920 --> 04:26:39,840 is less important than the methods and 7762 04:26:38,000 --> 04:26:41,920 or attributes that it might have 7763 04:26:39,840 --> 04:26:44,000 the class type is not checked if the 7764 04:26:41,920 --> 04:26:46,000 minimum methods and or attributes 7765 04:26:44,000 --> 04:26:48,239 are present now let's say with our 7766 04:26:46,000 --> 04:26:50,239 chicken object our chicken can no longer 7767 04:26:48,239 --> 04:26:51,920 walk 7768 04:26:50,239 --> 04:26:53,279 well we cannot pass in our chicken 7769 04:26:51,920 --> 04:26:55,120 object anymore 7770 04:26:53,279 --> 04:26:57,199 we have an attribute error chicken 7771 04:26:55,120 --> 04:26:59,120 object has no attribute walk 7772 04:26:57,199 --> 04:27:01,120 so python took a look at this chicken 7773 04:26:59,120 --> 04:27:01,760 and noticed that it does not have a walk 7774 04:27:01,120 --> 04:27:03,760 method 7775 04:27:01,760 --> 04:27:05,199 well it's not walking like a duck but 7776 04:27:03,760 --> 04:27:07,040 it's talking like a duck 7777 04:27:05,199 --> 04:27:08,880 so it doesn't count as a duck because it 7778 04:27:07,040 --> 04:27:10,800 doesn't have its walk method 7779 04:27:08,880 --> 04:27:12,880 in conclusion the class type of an 7780 04:27:10,800 --> 04:27:14,720 object is not as important as the 7781 04:27:12,880 --> 04:27:16,159 methods and or attributes that that 7782 04:27:14,720 --> 04:27:18,399 class might have when using 7783 04:27:16,159 --> 04:27:19,680 duct typing the class type will not be 7784 04:27:18,399 --> 04:27:22,239 checked if the minimum 7785 04:27:19,680 --> 04:27:22,720 methods and or attributes are present 7786 04:27:22,239 --> 04:27:24,720 since 7787 04:27:22,720 --> 04:27:25,760 chickens can both walk and talk like 7788 04:27:24,720 --> 04:27:27,760 ducks then 7789 04:27:25,760 --> 04:27:29,760 they can be a substitute for ducks when 7790 04:27:27,760 --> 04:27:30,640 we pass in our duck object to this 7791 04:27:29,760 --> 04:27:32,319 method 7792 04:27:30,640 --> 04:27:33,920 so if you would like a copy of this code 7793 04:27:32,319 --> 04:27:35,040 i will post all of this in the comment 7794 04:27:33,920 --> 04:27:37,040 section down below 7795 04:27:35,040 --> 04:27:38,960 but yeah that is the basics of duct 7796 04:27:37,040 --> 04:27:40,960 typing in python 7797 04:27:38,960 --> 04:27:42,319 hey what's going on everybody it's you 7798 04:27:40,960 --> 04:27:44,000 bro hope you're doing well 7799 04:27:42,319 --> 04:27:46,159 and in this video i'm going to discuss 7800 04:27:44,000 --> 04:27:51,279 the walrus operator in python 7801 04:27:46,159 --> 04:27:52,880 so sit back relax and enjoy the show 7802 04:27:51,279 --> 04:27:54,800 all right people i hope you like 7803 04:27:52,880 --> 04:27:56,319 walruses because that's the topic of 7804 04:27:54,800 --> 04:27:58,960 this video we'll be discussing 7805 04:27:56,319 --> 04:28:00,319 the walrus operator also known as an 7806 04:27:58,960 --> 04:28:02,800 assignment expression 7807 04:28:00,319 --> 04:28:04,479 it's a colon followed by an equal sign 7808 04:28:02,800 --> 04:28:05,120 and it kind of looks like a walrus on 7809 04:28:04,479 --> 04:28:08,319 its side 7810 04:28:05,120 --> 04:28:10,800 now this is a new feature for python 3.8 7811 04:28:08,319 --> 04:28:11,760 what it does is that it assigns values 7812 04:28:10,800 --> 04:28:14,159 to variables 7813 04:28:11,760 --> 04:28:16,080 as part of a larger expression here's an 7814 04:28:14,159 --> 04:28:16,880 example of why the walrus operator would 7815 04:28:16,080 --> 04:28:19,359 be useful 7816 04:28:16,880 --> 04:28:19,920 let's say we have a variable named happy 7817 04:28:19,359 --> 04:28:21,920 happy 7818 04:28:19,920 --> 04:28:23,760 equals true if you're not happy or 7819 04:28:21,920 --> 04:28:24,960 you're feeling so so you can set this to 7820 04:28:23,760 --> 04:28:27,520 false if you want 7821 04:28:24,960 --> 04:28:28,800 so let's print the value contained 7822 04:28:27,520 --> 04:28:30,960 within happy 7823 04:28:28,800 --> 04:28:31,920 so this will print true to the console 7824 04:28:30,960 --> 04:28:33,520 window 7825 04:28:31,920 --> 04:28:35,120 now wouldn't it be cool if we could 7826 04:28:33,520 --> 04:28:37,840 combine both of these 7827 04:28:35,120 --> 04:28:39,279 statements together so we would want to 7828 04:28:37,840 --> 04:28:42,159 say something such as 7829 04:28:39,279 --> 04:28:44,080 happy equals true and then print it to 7830 04:28:42,159 --> 04:28:44,720 the console window using one line of 7831 04:28:44,080 --> 04:28:47,040 code 7832 04:28:44,720 --> 04:28:49,120 well normally we can't do this using the 7833 04:28:47,040 --> 04:28:50,399 standard assignment operator now one 7834 04:28:49,120 --> 04:28:53,199 thing that we can do is use 7835 04:28:50,399 --> 04:28:54,319 the walrus operator this assigns a value 7836 04:28:53,199 --> 04:28:56,880 to a variable 7837 04:28:54,319 --> 04:28:57,840 as part of a larger expression so this 7838 04:28:56,880 --> 04:29:00,159 would technically 7839 04:28:57,840 --> 04:29:01,040 be allowed if this assignment operator 7840 04:29:00,159 --> 04:29:03,199 was instead 7841 04:29:01,040 --> 04:29:04,880 an assignment expression the walrus 7842 04:29:03,199 --> 04:29:07,520 operator happy 7843 04:29:04,880 --> 04:29:08,479 walrus true and when they're all within 7844 04:29:07,520 --> 04:29:11,760 a print statement 7845 04:29:08,479 --> 04:29:14,080 we can assign a value to a variable and 7846 04:29:11,760 --> 04:29:15,279 use it as a part of a larger expression 7847 04:29:14,080 --> 04:29:17,840 so as you can see 7848 04:29:15,279 --> 04:29:19,920 this new variable of happy was assigned 7849 04:29:17,840 --> 04:29:22,080 and used in one expression 7850 04:29:19,920 --> 04:29:24,399 now here's a more practical example of 7851 04:29:22,080 --> 04:29:26,560 why the walrus operator would be useful 7852 04:29:24,399 --> 04:29:28,880 let's say we have the smell program what 7853 04:29:26,560 --> 04:29:30,880 this will do is consistently ask you 7854 04:29:28,880 --> 04:29:32,239 what food you like and put it into a 7855 04:29:30,880 --> 04:29:35,680 list called foods 7856 04:29:32,239 --> 04:29:38,399 foods equals list while true food equals 7857 04:29:35,680 --> 04:29:39,359 input what food do you like if food 7858 04:29:38,399 --> 04:29:42,560 equals quit 7859 04:29:39,359 --> 04:29:44,319 break then append our list of foods with 7860 04:29:42,560 --> 04:29:47,120 this new food item that we have 7861 04:29:44,319 --> 04:29:47,439 okay let's run this then so this while 7862 04:29:47,120 --> 04:29:49,439 loop 7863 04:29:47,439 --> 04:29:51,920 will continue as long as i don't type in 7864 04:29:49,439 --> 04:29:54,159 quit and it will consistently ask me 7865 04:29:51,920 --> 04:29:55,600 what food do i like and place that food 7866 04:29:54,159 --> 04:29:58,319 item into a list 7867 04:29:55,600 --> 04:30:00,560 named foods so let's begin what food do 7868 04:29:58,319 --> 04:30:04,319 i like well i do like pizza 7869 04:30:00,560 --> 04:30:06,000 i like sushi and i like ice cream 7870 04:30:04,319 --> 04:30:07,760 and that should be enough for me i'm 7871 04:30:06,000 --> 04:30:08,560 going to type in quit to exit this 7872 04:30:07,760 --> 04:30:10,720 program 7873 04:30:08,560 --> 04:30:11,680 now let's write the same program but 7874 04:30:10,720 --> 04:30:13,680 instead use 7875 04:30:11,680 --> 04:30:15,040 a walrus operator so that we can write 7876 04:30:13,680 --> 04:30:17,359 this program using 7877 04:30:15,040 --> 04:30:19,439 less lines of code now if i were to use 7878 04:30:17,359 --> 04:30:20,560 the walrus operator this is how i would 7879 04:30:19,439 --> 04:30:22,800 write the same program 7880 04:30:20,560 --> 04:30:23,600 we would still need our list foods 7881 04:30:22,800 --> 04:30:26,960 equals 7882 04:30:23,600 --> 04:30:28,399 list now for my while loop it's going to 7883 04:30:26,960 --> 04:30:32,720 look something like this 7884 04:30:28,399 --> 04:30:36,159 while food walrus operator 7885 04:30:32,720 --> 04:30:40,239 input what 7886 04:30:36,159 --> 04:30:42,800 food do you like 7887 04:30:40,239 --> 04:30:43,920 all right this is the assignment portion 7888 04:30:42,800 --> 04:30:46,479 now we can use 7889 04:30:43,920 --> 04:30:47,439 this assignment portion within a larger 7890 04:30:46,479 --> 04:30:50,720 expression 7891 04:30:47,439 --> 04:30:54,000 so let's also check to see if our input 7892 04:30:50,720 --> 04:30:56,560 does not equal quit alright 7893 04:30:54,000 --> 04:30:57,520 so then after each iteration of this 7894 04:30:56,560 --> 04:31:00,720 while loop 7895 04:30:57,520 --> 04:31:04,159 we will take our list foods dot 7896 04:31:00,720 --> 04:31:06,319 append food and 7897 04:31:04,159 --> 04:31:08,399 that is it so we have written the same 7898 04:31:06,319 --> 04:31:10,239 program in about half the lines of code 7899 04:31:08,399 --> 04:31:12,560 that we did previously so 7900 04:31:10,239 --> 04:31:13,920 let's test it what food do you like 7901 04:31:12,560 --> 04:31:17,199 pizza 7902 04:31:13,920 --> 04:31:20,000 sushi ice cream 7903 04:31:17,199 --> 04:31:22,479 and quit well everybody that is the 7904 04:31:20,000 --> 04:31:23,520 basics of the walrus operator also known 7905 04:31:22,479 --> 04:31:25,920 as an assignment 7906 04:31:23,520 --> 04:31:26,720 expression it's a new feature for python 7907 04:31:25,920 --> 04:31:28,640 3.8 7908 04:31:26,720 --> 04:31:30,399 and beyond so if this doesn't work for 7909 04:31:28,640 --> 04:31:31,760 you i would check to see what version of 7910 04:31:30,399 --> 04:31:34,319 python you're using first 7911 04:31:31,760 --> 04:31:36,000 it assigns values to variables as part 7912 04:31:34,319 --> 04:31:37,680 of a larger expression 7913 04:31:36,000 --> 04:31:39,120 if you would like a copy of this code i 7914 04:31:37,680 --> 04:31:40,479 will post all of this in the comment 7915 04:31:39,120 --> 04:31:42,880 section down below 7916 04:31:40,479 --> 04:31:45,120 but yeah that is how the walrus operator 7917 04:31:42,880 --> 04:31:47,279 works in python 7918 04:31:45,120 --> 04:31:48,880 hello what's going on everybody it's bro 7919 04:31:47,279 --> 04:31:50,000 hope you're doing well and in this video 7920 04:31:48,880 --> 04:31:52,080 i'm going to show you guys how we can 7921 04:31:50,000 --> 04:31:52,720 assign a function to a variable in 7922 04:31:52,080 --> 04:31:57,680 python 7923 04:31:52,720 --> 04:31:59,840 so sit back relax and enjoy the show 7924 04:31:57,680 --> 04:32:01,760 hey all in today's video i will show you 7925 04:31:59,840 --> 04:32:02,720 all how we can assign a function to a 7926 04:32:01,760 --> 04:32:04,640 variable 7927 04:32:02,720 --> 04:32:06,239 this video will be a prerequisite for a 7928 04:32:04,640 --> 04:32:06,960 few videos i have coming up regarding 7929 04:32:06,239 --> 04:32:08,960 functions 7930 04:32:06,960 --> 04:32:11,040 now let's say we have a function named 7931 04:32:08,960 --> 04:32:11,520 hello and all we'll do is print the word 7932 04:32:11,040 --> 04:32:13,279 hello 7933 04:32:11,520 --> 04:32:14,640 and as you know to call this function 7934 04:32:13,279 --> 04:32:16,399 you would type the name of the function 7935 04:32:14,640 --> 04:32:17,760 followed by a set of parentheses and 7936 04:32:16,399 --> 04:32:19,840 this will print the word 7937 04:32:17,760 --> 04:32:21,439 hello the set of parenthesis that comes 7938 04:32:19,840 --> 04:32:23,040 after a function's name 7939 04:32:21,439 --> 04:32:24,800 is the portion that will call the 7940 04:32:23,040 --> 04:32:26,800 function if you were to remove 7941 04:32:24,800 --> 04:32:29,040 that set of parenthesis afterwards we 7942 04:32:26,800 --> 04:32:31,199 would not in fact call that function 7943 04:32:29,040 --> 04:32:32,640 now with python python will pretty much 7944 04:32:31,199 --> 04:32:34,560 treat everything as objects 7945 04:32:32,640 --> 04:32:35,760 including functions so there's something 7946 04:32:34,560 --> 04:32:37,840 i want to show you guys 7947 04:32:35,760 --> 04:32:39,279 if i was to print the name of my 7948 04:32:37,840 --> 04:32:41,600 function hello 7949 04:32:39,279 --> 04:32:43,600 what will be displayed is the memory 7950 04:32:41,600 --> 04:32:45,680 address of this function 7951 04:32:43,600 --> 04:32:47,520 this is the memory address of where this 7952 04:32:45,680 --> 04:32:48,159 function is located within my computer's 7953 04:32:47,520 --> 04:32:50,159 memory 7954 04:32:48,159 --> 04:32:51,760 and it's in hexadecimal think of it like 7955 04:32:50,159 --> 04:32:53,600 a street address such as 7956 04:32:51,760 --> 04:32:55,359 one two three fake street this is the 7957 04:32:53,600 --> 04:32:56,800 address of where this function is within 7958 04:32:55,359 --> 04:32:58,560 my computer's memory 7959 04:32:56,800 --> 04:33:00,399 and each time that i run this program 7960 04:32:58,560 --> 04:33:01,040 this number can change as you can see 7961 04:33:00,399 --> 04:33:02,560 here 7962 04:33:01,040 --> 04:33:04,799 now one thing that we could do we could 7963 04:33:02,560 --> 04:33:06,959 assign this address to a variable 7964 04:33:04,799 --> 04:33:08,000 let's say hi equals hello and be sure 7965 04:33:06,959 --> 04:33:09,439 that you're not adding that set of 7966 04:33:08,000 --> 04:33:11,119 parentheses afterwards because 7967 04:33:09,439 --> 04:33:12,959 then you would be calling the hello 7968 04:33:11,119 --> 04:33:13,520 function and returning the result to 7969 04:33:12,959 --> 04:33:16,879 high 7970 04:33:13,520 --> 04:33:20,000 so high equals hello no parentheses 7971 04:33:16,879 --> 04:33:22,080 and if i was to print hi well 7972 04:33:20,000 --> 04:33:23,760 the address of hello and hi will be at 7973 04:33:22,080 --> 04:33:26,400 the same memory address 7974 04:33:23,760 --> 04:33:27,760 both of these numbers are the same now 7975 04:33:26,400 --> 04:33:30,080 what do you imagine would happen 7976 04:33:27,760 --> 04:33:32,240 if i was to call the high function after 7977 04:33:30,080 --> 04:33:33,199 we assigned the memory address of hello 7978 04:33:32,240 --> 04:33:34,879 to hi 7979 04:33:33,199 --> 04:33:36,480 well then what we'll end up doing is 7980 04:33:34,879 --> 04:33:38,240 calling the hello function 7981 04:33:36,480 --> 04:33:39,680 even though we're listing that we would 7982 04:33:38,240 --> 04:33:41,119 like to call the 7983 04:33:39,680 --> 04:33:43,840 high function even though it doesn't 7984 04:33:41,119 --> 04:33:44,799 exist so it's as if this hello function 7985 04:33:43,840 --> 04:33:46,959 has two names 7986 04:33:44,799 --> 04:33:49,039 you can either use hello or you can use 7987 04:33:46,959 --> 04:33:52,400 hi and let me demonstrate that 7988 04:33:49,039 --> 04:33:53,439 so you can call either hello hi or both 7989 04:33:52,400 --> 04:33:55,039 for this example 7990 04:33:53,439 --> 04:33:57,279 that's because we're assigning the 7991 04:33:55,039 --> 04:33:58,000 memory address of hello to this variable 7992 04:33:57,279 --> 04:33:59,920 of high 7993 04:33:58,000 --> 04:34:01,119 so we could treat high as a function 7994 04:33:59,920 --> 04:34:03,199 it's kind of like 7995 04:34:01,119 --> 04:34:05,760 an alias where this function has two 7996 04:34:03,199 --> 04:34:07,920 names now here's another demonstration 7997 04:34:05,760 --> 04:34:10,080 let's say that we would like to assign 7998 04:34:07,920 --> 04:34:13,359 our built-in print function to 7999 04:34:10,080 --> 04:34:16,879 a variable so let's say maybe say 8000 04:34:13,359 --> 04:34:18,561 say equals print and when i say print 8001 04:34:16,879 --> 04:34:19,760 i'm referring to the print function and 8002 04:34:18,561 --> 04:34:20,959 be sure you're not adding that set of 8003 04:34:19,760 --> 04:34:22,480 parentheses afterwards 8004 04:34:20,959 --> 04:34:24,879 now if i need to print something to the 8005 04:34:22,480 --> 04:34:26,719 console window i can either use 8006 04:34:24,879 --> 04:34:28,561 print like what we've previously been 8007 04:34:26,719 --> 04:34:30,240 doing or i could use 8008 04:34:28,561 --> 04:34:32,160 say because say has the same memory 8009 04:34:30,240 --> 04:34:35,439 address let's say 8010 04:34:32,160 --> 04:34:39,279 perhaps whoa i can't 8011 04:34:35,439 --> 04:34:42,320 believe this works 8012 04:34:39,279 --> 04:34:44,639 surprise face all right so 8013 04:34:42,320 --> 04:34:45,680 we can call the print function by saying 8014 04:34:44,639 --> 04:34:48,240 the word say 8015 04:34:45,680 --> 04:34:49,199 say all of this text and this will print 8016 04:34:48,240 --> 04:34:51,439 all of this text 8017 04:34:49,199 --> 04:34:52,400 well say all of this text to the console 8018 04:34:51,439 --> 04:34:54,480 window 8019 04:34:52,400 --> 04:34:55,520 under normal circumstances i'm not sure 8020 04:34:54,480 --> 04:34:57,039 why or when 8021 04:34:55,520 --> 04:34:58,879 you would need to assign the print 8022 04:34:57,039 --> 04:35:00,959 function to a variable but 8023 04:34:58,879 --> 04:35:02,561 you can and well that's kind of cool so 8024 04:35:00,959 --> 04:35:04,639 i thought i'd show you guys that 8025 04:35:02,561 --> 04:35:06,000 so everybody that's how to assign a 8026 04:35:04,639 --> 04:35:07,840 function to a variable 8027 04:35:06,000 --> 04:35:09,359 like i said we'll be building upon this 8028 04:35:07,840 --> 04:35:10,480 topic so you want to be sure that you 8029 04:35:09,359 --> 04:35:12,080 understand this 8030 04:35:10,480 --> 04:35:14,000 all right then well if you would like a 8031 04:35:12,080 --> 04:35:15,760 copy of this code i will post all of 8032 04:35:14,000 --> 04:35:18,400 this to the comments section down below 8033 04:35:15,760 --> 04:35:19,520 but yeah that's how to assign a function 8034 04:35:18,400 --> 04:35:22,080 to a variable 8035 04:35:19,520 --> 04:35:22,639 in python hey uh what's going on 8036 04:35:22,080 --> 04:35:24,561 everybody 8037 04:35:22,639 --> 04:35:26,240 it's bro hope you're doing well and in 8038 04:35:24,561 --> 04:35:28,400 today's video i'm going to explain 8039 04:35:26,240 --> 04:35:33,199 higher order functions in python so 8040 04:35:28,400 --> 04:35:34,959 sit back relax and enjoy the show 8041 04:35:33,199 --> 04:35:36,799 all right ladies and gentlemen let's 8042 04:35:34,959 --> 04:35:38,561 talk about higher order functions 8043 04:35:36,799 --> 04:35:40,879 these are functions that do one of two 8044 04:35:38,561 --> 04:35:42,000 things one they either accept a function 8045 04:35:40,879 --> 04:35:44,561 as an argument 8046 04:35:42,000 --> 04:35:46,400 or two they return a function as output 8047 04:35:44,561 --> 04:35:48,240 and in python this is totally allowed 8048 04:35:46,400 --> 04:35:48,959 because functions are also treated as 8049 04:35:48,240 --> 04:35:50,400 objects 8050 04:35:48,959 --> 04:35:52,240 i'll give you guys an example of both of 8051 04:35:50,400 --> 04:35:54,240 these but let's begin with number one 8052 04:35:52,240 --> 04:35:55,840 here's an example of number one let's 8053 04:35:54,240 --> 04:35:57,680 say that i have two functions 8054 04:35:55,840 --> 04:35:59,760 named loud and quiet both of these 8055 04:35:57,680 --> 04:36:00,480 functions will accept a string as an 8056 04:35:59,760 --> 04:36:02,561 argument 8057 04:36:00,480 --> 04:36:05,039 loud will return that string all 8058 04:36:02,561 --> 04:36:06,879 uppercase as if we're shouting something 8059 04:36:05,039 --> 04:36:08,879 quiet we'll return that text i'll 8060 04:36:06,879 --> 04:36:09,520 lowercase as if we're whispering 8061 04:36:08,879 --> 04:36:11,840 something 8062 04:36:09,520 --> 04:36:13,359 now i'm going to create a third function 8063 04:36:11,840 --> 04:36:14,959 called hello 8064 04:36:13,359 --> 04:36:17,039 and this will be the higher order 8065 04:36:14,959 --> 04:36:17,760 function it accepts a function as an 8066 04:36:17,039 --> 04:36:19,119 argument 8067 04:36:17,760 --> 04:36:21,119 i'm not sure if we're going to be 8068 04:36:19,119 --> 04:36:22,959 passing in loud or quiet 8069 04:36:21,119 --> 04:36:24,160 whatever it is i'm going to give it a 8070 04:36:22,959 --> 04:36:26,320 name of funk 8071 04:36:24,160 --> 04:36:28,000 short for function each of these 8072 04:36:26,320 --> 04:36:30,719 functions will return some text 8073 04:36:28,000 --> 04:36:32,160 either all uppercase or lowercase i'm 8074 04:36:30,719 --> 04:36:34,959 going to assign that to 8075 04:36:32,160 --> 04:36:36,400 text text equals the name of our 8076 04:36:34,959 --> 04:36:37,039 function we're not sure what it's going 8077 04:36:36,400 --> 04:36:39,600 to be 8078 04:36:37,039 --> 04:36:41,039 and i will pass in some text in order to 8079 04:36:39,600 --> 04:36:42,879 call one of these functions 8080 04:36:41,039 --> 04:36:44,400 i will pass in just the word hello 8081 04:36:42,879 --> 04:36:46,160 that's capitalized 8082 04:36:44,400 --> 04:36:47,439 and at the end i will print whatever 8083 04:36:46,160 --> 04:36:49,199 text that we have 8084 04:36:47,439 --> 04:36:51,039 in order to call this function of hello 8085 04:36:49,199 --> 04:36:51,600 i need to pass in a function as an 8086 04:36:51,039 --> 04:36:54,400 argument 8087 04:36:51,600 --> 04:36:55,039 do i want to pass in loud or quiet do i 8088 04:36:54,400 --> 04:36:57,279 want the 8089 04:36:55,039 --> 04:36:58,639 loud variety of hello where all of the 8090 04:36:57,279 --> 04:37:00,719 text will be uppercase 8091 04:36:58,639 --> 04:37:02,639 or the quiet variety of flow where all 8092 04:37:00,719 --> 04:37:05,920 the text will be lowercase 8093 04:37:02,639 --> 04:37:08,320 i would like to pass in loud because i 8094 04:37:05,920 --> 04:37:10,480 want to make my text all uppercase 8095 04:37:08,320 --> 04:37:12,000 so i'm typing the name of the higher 8096 04:37:10,480 --> 04:37:14,240 order function and passing in 8097 04:37:12,000 --> 04:37:16,000 a function as an argument and the result 8098 04:37:14,240 --> 04:37:17,199 is that the word hello will be printed 8099 04:37:16,000 --> 04:37:19,359 to the console window 8100 04:37:17,199 --> 04:37:20,400 all uppercase now here's a rundown of 8101 04:37:19,359 --> 04:37:21,920 what just happened 8102 04:37:20,400 --> 04:37:23,680 we're calling the hello function and 8103 04:37:21,920 --> 04:37:26,719 we're passing in loud 8104 04:37:23,680 --> 04:37:28,639 as an argument we're naming loud as 8105 04:37:26,719 --> 04:37:29,920 funk while we're within this hello 8106 04:37:28,639 --> 04:37:33,359 function 8107 04:37:29,920 --> 04:37:34,719 text equals loud and we're sending a 8108 04:37:33,359 --> 04:37:37,199 string of text 8109 04:37:34,719 --> 04:37:38,799 that says hello we're returning that 8110 04:37:37,199 --> 04:37:41,119 text all uppercase 8111 04:37:38,799 --> 04:37:42,320 assigning it to a variable called text 8112 04:37:41,119 --> 04:37:44,561 and printing that text 8113 04:37:42,320 --> 04:37:45,520 to the console window now if i would 8114 04:37:44,561 --> 04:37:47,920 like to 8115 04:37:45,520 --> 04:37:48,799 use the quiet variety of hello then i'm 8116 04:37:47,920 --> 04:37:52,080 going to pass in 8117 04:37:48,799 --> 04:37:53,119 quiet as an argument so quiet will make 8118 04:37:52,080 --> 04:37:55,520 all of this text 8119 04:37:53,119 --> 04:37:56,160 lowercase alright people so that's 8120 04:37:55,520 --> 04:37:58,400 example 8121 04:37:56,160 --> 04:37:59,279 one of a higher order function a higher 8122 04:37:58,400 --> 04:38:02,080 order function 8123 04:37:59,279 --> 04:38:03,039 is a function that accepts a function as 8124 04:38:02,080 --> 04:38:05,279 an argument 8125 04:38:03,039 --> 04:38:06,561 this hello function is an example of a 8126 04:38:05,279 --> 04:38:09,199 higher order function 8127 04:38:06,561 --> 04:38:10,879 because we're accepting either loud or 8128 04:38:09,199 --> 04:38:12,400 quiet as arguments 8129 04:38:10,879 --> 04:38:14,240 all right people let's move on to the 8130 04:38:12,400 --> 04:38:15,439 second part of this definition for 8131 04:38:14,240 --> 04:38:17,520 higher order functions 8132 04:38:15,439 --> 04:38:18,879 a higher order function is a function 8133 04:38:17,520 --> 04:38:20,561 that returns a function 8134 04:38:18,879 --> 04:38:22,639 i'm going to give a different example 8135 04:38:20,561 --> 04:38:24,959 this time let's say we have a pair of 8136 04:38:22,639 --> 04:38:26,320 nested functions the outer function will 8137 04:38:24,959 --> 04:38:28,320 be named divisor 8138 04:38:26,320 --> 04:38:30,160 and we will accept a number as an 8139 04:38:28,320 --> 04:38:32,561 argument that we will call x 8140 04:38:30,160 --> 04:38:33,199 a divisor is a number that is used to 8141 04:38:32,561 --> 04:38:35,600 divide 8142 04:38:33,199 --> 04:38:37,520 another number when using division and 8143 04:38:35,600 --> 04:38:38,080 inside this function we have an inner 8144 04:38:37,520 --> 04:38:41,039 function 8145 04:38:38,080 --> 04:38:42,639 named dividend dividend is the number 8146 04:38:41,039 --> 04:38:44,240 that's going to be divided 8147 04:38:42,639 --> 04:38:45,680 and we will call the argument that is 8148 04:38:44,240 --> 04:38:49,199 passed in y 8149 04:38:45,680 --> 04:38:51,359 all we'll do is return y divided by x 8150 04:38:49,199 --> 04:38:52,480 we're dividing the dividend by the 8151 04:38:51,359 --> 04:38:55,760 divisor y 8152 04:38:52,480 --> 04:38:57,520 divided by x now within the 8153 04:38:55,760 --> 04:39:00,080 outer function but not within the inner 8154 04:38:57,520 --> 04:39:02,879 function we're going to return 8155 04:39:00,080 --> 04:39:04,879 our dividend function so a higher order 8156 04:39:02,879 --> 04:39:05,680 function is a function that returns a 8157 04:39:04,879 --> 04:39:07,760 function 8158 04:39:05,680 --> 04:39:09,199 divisor is a higher order function 8159 04:39:07,760 --> 04:39:11,760 because we're returning 8160 04:39:09,199 --> 04:39:12,639 dividend now if i would like to access 8161 04:39:11,760 --> 04:39:14,879 this nested 8162 04:39:12,639 --> 04:39:15,760 dividend function i first need to call 8163 04:39:14,879 --> 04:39:18,320 the outside 8164 04:39:15,760 --> 04:39:20,480 divisor function and pass in a number as 8165 04:39:18,320 --> 04:39:22,799 an argument to serve as the divisor 8166 04:39:20,480 --> 04:39:24,639 the divisor function is going to return 8167 04:39:22,799 --> 04:39:26,480 my dividend function which we can then 8168 04:39:24,639 --> 04:39:29,760 assign to a variable 8169 04:39:26,480 --> 04:39:32,959 so the variable i'm going to name divide 8170 04:39:29,760 --> 04:39:35,760 divide equals and i need to call divisor 8171 04:39:32,959 --> 04:39:37,360 and set a divisor let's say that i would 8172 04:39:35,760 --> 04:39:40,878 like to divide all numbers 8173 04:39:37,360 --> 04:39:44,400 by two so i will set x to be two 8174 04:39:40,878 --> 04:39:47,440 and now for the dividend i will 8175 04:39:44,400 --> 04:39:50,480 print call the 8176 04:39:47,440 --> 04:39:52,080 divide variable and pass in a number as 8177 04:39:50,480 --> 04:39:52,878 the dividend the number that's going to 8178 04:39:52,080 --> 04:39:55,120 be divided 8179 04:39:52,878 --> 04:39:56,400 and let's say i would like to divide 10 8180 04:39:55,120 --> 04:39:58,320 by two 8181 04:39:56,400 --> 04:40:00,240 and this will work as you can see we 8182 04:39:58,320 --> 04:40:01,760 have five in the console window 8183 04:40:00,240 --> 04:40:03,520 so let me explain what just happened 8184 04:40:01,760 --> 04:40:06,480 here so our program 8185 04:40:03,520 --> 04:40:07,360 begins here divisor and we're passing in 8186 04:40:06,480 --> 04:40:09,840 two 8187 04:40:07,360 --> 04:40:12,000 x will be two and it will stay that way 8188 04:40:09,840 --> 04:40:13,520 until we finish this program or until we 8189 04:40:12,000 --> 04:40:16,240 reassign x 8190 04:40:13,520 --> 04:40:18,560 now x equals two we're skipping this 8191 04:40:16,240 --> 04:40:21,040 function because we did not call it yet 8192 04:40:18,560 --> 04:40:22,480 we're returning dividend and assigning 8193 04:40:21,040 --> 04:40:24,560 it to a variable 8194 04:40:22,480 --> 04:40:26,480 and we can call a variable if it has the 8195 04:40:24,560 --> 04:40:28,400 memory address of a function 8196 04:40:26,480 --> 04:40:29,760 which is what we're doing in this line 8197 04:40:28,400 --> 04:40:32,958 now we're calling 8198 04:40:29,760 --> 04:40:33,440 dividend and passing in 10 so y equals 8199 04:40:32,958 --> 04:40:36,718 10 8200 04:40:33,440 --> 04:40:38,798 and x still equals to we're returning 10 8201 04:40:36,718 --> 04:40:40,000 divided by 2 and printing it to the 8202 04:40:38,798 --> 04:40:42,240 console window 8203 04:40:40,000 --> 04:40:43,040 well everyone in conclusion a higher 8204 04:40:42,240 --> 04:40:46,320 order function 8205 04:40:43,040 --> 04:40:47,920 is a function that either one accepts a 8206 04:40:46,320 --> 04:40:51,040 function as an argument 8207 04:40:47,920 --> 04:40:52,638 or two they return a function the format 8208 04:40:51,040 --> 04:40:53,680 is a little bit strange and we're not 8209 04:40:52,638 --> 04:40:55,440 quite used to it yet 8210 04:40:53,680 --> 04:40:57,440 but in future videos we'll have more 8211 04:40:55,440 --> 04:40:58,878 practice with high order functions 8212 04:40:57,440 --> 04:41:00,480 if you would like a copy of all this 8213 04:40:58,878 --> 04:41:02,160 code i will post all of this to the 8214 04:41:00,480 --> 04:41:03,920 comment section down below 8215 04:41:02,160 --> 04:41:06,400 but yeah that is how higher order 8216 04:41:03,920 --> 04:41:08,878 functions work in python 8217 04:41:06,400 --> 04:41:10,400 yo what's going on everybody it's bro 8218 04:41:08,878 --> 04:41:11,440 hope you're doing well in today's video 8219 04:41:10,400 --> 04:41:14,240 i'm going to explain 8220 04:41:11,440 --> 04:41:15,120 lambda functions in python so sit back 8221 04:41:14,240 --> 04:41:19,040 relax 8222 04:41:15,120 --> 04:41:20,878 and enjoy the show all right 8223 04:41:19,040 --> 04:41:22,878 ladies and gentlemen let's talk about 8224 04:41:20,878 --> 04:41:24,718 lambda functions these are functions 8225 04:41:22,878 --> 04:41:26,240 that are written in one line and use 8226 04:41:24,718 --> 04:41:28,240 this lambda keyword 8227 04:41:26,240 --> 04:41:30,160 they accept any number of arguments but 8228 04:41:28,240 --> 04:41:32,240 they only have one expression 8229 04:41:30,160 --> 04:41:34,160 think of it as a shortcut they're useful 8230 04:41:32,240 --> 04:41:35,840 if you need a function for only one use 8231 04:41:34,160 --> 04:41:36,878 or a short period of time 8232 04:41:35,840 --> 04:41:38,878 and then you're just planning on 8233 04:41:36,878 --> 04:41:40,958 throwing away that function afterwards 8234 04:41:38,878 --> 04:41:42,240 here's the syntax for a lambda function 8235 04:41:40,958 --> 04:41:44,000 you type lambda 8236 04:41:42,240 --> 04:41:45,920 you add the parameters followed by a 8237 04:41:44,000 --> 04:41:47,600 colon and then your expression 8238 04:41:45,920 --> 04:41:49,760 let's say we have this function named 8239 04:41:47,600 --> 04:41:50,400 double double accepts one argument that 8240 04:41:49,760 --> 04:41:52,718 we will name 8241 04:41:50,400 --> 04:41:54,240 x we're returning x times two 8242 04:41:52,718 --> 04:41:56,080 effectively doubling it 8243 04:41:54,240 --> 04:41:57,280 so if i need to call this function i 8244 04:41:56,080 --> 04:41:59,280 would type double 8245 04:41:57,280 --> 04:42:01,360 and then pass in a number as an argument 8246 04:41:59,280 --> 04:42:03,440 so the result if i pass in 5 would be 8247 04:42:01,360 --> 04:42:04,958 10. now let's write the same function 8248 04:42:03,440 --> 04:42:07,120 but instead write it as a 8249 04:42:04,958 --> 04:42:09,040 lambda function so let me turn these 8250 04:42:07,120 --> 04:42:12,560 lines into comments 8251 04:42:09,040 --> 04:42:14,320 now the first step is to type lambda 8252 04:42:12,560 --> 04:42:16,000 then our parameters we only have one 8253 04:42:14,320 --> 04:42:18,000 parameter and that is x 8254 04:42:16,000 --> 04:42:19,120 colon and then our expression we would 8255 04:42:18,000 --> 04:42:21,760 like to return x 8256 04:42:19,120 --> 04:42:23,760 times two now this lambda function is 8257 04:42:21,760 --> 04:42:25,200 going to return a function object and we 8258 04:42:23,760 --> 04:42:26,240 can assign that much like what we do 8259 04:42:25,200 --> 04:42:29,600 with variables 8260 04:42:26,240 --> 04:42:32,320 let's say double equals lambda x colon 8261 04:42:29,600 --> 04:42:33,280 x times two and in order to call this 8262 04:42:32,320 --> 04:42:35,840 lambda function 8263 04:42:33,280 --> 04:42:36,560 we're going to type double then a set of 8264 04:42:35,840 --> 04:42:38,560 parentheses 8265 04:42:36,560 --> 04:42:39,600 to call this function and then pass in 8266 04:42:38,560 --> 04:42:42,000 our arguments 8267 04:42:39,600 --> 04:42:43,200 so let's say that x equals 5 and then 8268 04:42:42,000 --> 04:42:45,360 we're going to double it 8269 04:42:43,200 --> 04:42:47,600 and the result is 10. so that's the 8270 04:42:45,360 --> 04:42:48,400 syntax for a lambda function you type 8271 04:42:47,600 --> 04:42:50,480 lambda 8272 04:42:48,400 --> 04:42:52,480 your parameters colon and then your 8273 04:42:50,480 --> 04:42:53,600 expression this time let's say that we 8274 04:42:52,480 --> 04:42:55,360 have two parameters 8275 04:42:53,600 --> 04:42:56,798 we have two numbers that we want to 8276 04:42:55,360 --> 04:42:59,120 multiply by each other 8277 04:42:56,798 --> 04:43:00,480 so let's say that this function will be 8278 04:42:59,120 --> 04:43:03,840 called multiply 8279 04:43:00,480 --> 04:43:07,840 multiply equals lambda 8280 04:43:03,840 --> 04:43:10,080 x comma y because we have two parameters 8281 04:43:07,840 --> 04:43:11,280 colon followed by our expression we 8282 04:43:10,080 --> 04:43:14,480 would like to multiply 8283 04:43:11,280 --> 04:43:16,638 x times y and that is it 8284 04:43:14,480 --> 04:43:18,240 now in order to call multiply we need to 8285 04:43:16,638 --> 04:43:20,240 pass in two arguments 8286 04:43:18,240 --> 04:43:21,440 if i were to pass in just one well we'll 8287 04:43:20,240 --> 04:43:24,320 have a type error 8288 04:43:21,440 --> 04:43:24,958 so let's pass in five and maybe six and 8289 04:43:24,320 --> 04:43:28,638 the result 8290 04:43:24,958 --> 04:43:30,160 is thirty now let's try three parameters 8291 04:43:28,638 --> 04:43:31,680 let's say we would like to add three 8292 04:43:30,160 --> 04:43:35,040 numbers together 8293 04:43:31,680 --> 04:43:38,080 let's say add lambda x 8294 04:43:35,040 --> 04:43:38,958 comma y comma z so we have all of our 8295 04:43:38,080 --> 04:43:41,200 parameters 8296 04:43:38,958 --> 04:43:42,798 we have our colon here and then we need 8297 04:43:41,200 --> 04:43:46,240 our expression 8298 04:43:42,798 --> 04:43:49,680 x plus y plus z and 8299 04:43:46,240 --> 04:43:52,718 let's add five six and seven together 8300 04:43:49,680 --> 04:43:54,160 the result is 18. now let's try 8301 04:43:52,718 --> 04:43:56,400 something a little more complex let's 8302 04:43:54,160 --> 04:43:58,878 pass in some strings this time 8303 04:43:56,400 --> 04:43:59,840 i'll create a function named full name 8304 04:43:58,878 --> 04:44:02,958 full name 8305 04:43:59,840 --> 04:44:06,240 equals lambda 8306 04:44:02,958 --> 04:44:09,440 first name comma 8307 04:44:06,240 --> 04:44:12,878 last name the expression is going to be 8308 04:44:09,440 --> 04:44:16,878 first name plus perhaps a space 8309 04:44:12,878 --> 04:44:19,760 plus last name now i need to pass in a 8310 04:44:16,878 --> 04:44:22,240 first name and a last name 8311 04:44:19,760 --> 04:44:23,600 full name and you can use your own name 8312 04:44:22,240 --> 04:44:29,280 for this example if you want 8313 04:44:23,600 --> 04:44:31,280 let's say bro comma code 8314 04:44:29,280 --> 04:44:33,200 yep this will print your first name and 8315 04:44:31,280 --> 04:44:34,878 your last name to the console window 8316 04:44:33,200 --> 04:44:36,480 now let's take it a step further let's 8317 04:44:34,878 --> 04:44:38,560 say we would like to check somebody's 8318 04:44:36,480 --> 04:44:40,160 age once using a lambda function we 8319 04:44:38,560 --> 04:44:41,760 would like to check to see if they're 18 8320 04:44:40,160 --> 04:44:43,200 or older because they're signing up for 8321 04:44:41,760 --> 04:44:43,840 something perhaps let's say a credit 8322 04:44:43,200 --> 04:44:47,040 card 8323 04:44:43,840 --> 04:44:50,560 so let's create a function object age 8324 04:44:47,040 --> 04:44:51,440 check equals lambda we have one 8325 04:44:50,560 --> 04:44:53,440 parameter 8326 04:44:51,440 --> 04:44:55,280 somebody's edge colon and then our 8327 04:44:53,440 --> 04:44:59,120 expression 8328 04:44:55,280 --> 04:45:00,080 let's type true if age is greater than 8329 04:44:59,120 --> 04:45:03,680 or equal to 8330 04:45:00,080 --> 04:45:07,040 18 else 8331 04:45:03,680 --> 04:45:10,240 false to call this age track function 8332 04:45:07,040 --> 04:45:11,920 we need to pass in an age let's say the 8333 04:45:10,240 --> 04:45:13,120 user is 12 8334 04:45:11,920 --> 04:45:15,600 and they're trying to sign up for a 8335 04:45:13,120 --> 04:45:18,480 credit card while this will return false 8336 04:45:15,600 --> 04:45:19,040 let's say they're 18 now and this will 8337 04:45:18,480 --> 04:45:22,160 return 8338 04:45:19,040 --> 04:45:22,638 true so that's a lambda function it's a 8339 04:45:22,160 --> 04:45:24,718 function 8340 04:45:22,638 --> 04:45:25,840 that is written in one line using this 8341 04:45:24,718 --> 04:45:27,840 lambda keyword 8342 04:45:25,840 --> 04:45:29,440 they accept any number of arguments but 8343 04:45:27,840 --> 04:45:31,200 they only have one expression 8344 04:45:29,440 --> 04:45:32,798 think of it like a shortcut and they're 8345 04:45:31,200 --> 04:45:34,160 useful if you need to use a function 8346 04:45:32,798 --> 04:45:36,160 only once and then you're just planning 8347 04:45:34,160 --> 04:45:37,600 on throwing it away afterwards 8348 04:45:36,160 --> 04:45:39,280 so if you would like a copy of all this 8349 04:45:37,600 --> 04:45:40,798 code i will post all of this to the 8350 04:45:39,280 --> 04:45:43,120 comment section down below 8351 04:45:40,798 --> 04:45:44,480 but yeah those are lambda functions in 8352 04:45:43,120 --> 04:45:46,798 python 8353 04:45:44,480 --> 04:45:47,760 hey what's going on everybody it's bro 8354 04:45:46,798 --> 04:45:49,200 hope you're doing well 8355 04:45:47,760 --> 04:45:51,520 and in this video i'm going to show you 8356 04:45:49,200 --> 04:45:52,798 all how we can sort iterables in python 8357 04:45:51,520 --> 04:45:56,638 so sit back 8358 04:45:52,798 --> 04:45:56,638 relax and enjoy the show 8359 04:45:57,040 --> 04:46:00,480 well well well welcome back everybody in 8360 04:45:59,120 --> 04:46:02,240 this video i'm going to show you all how 8361 04:46:00,480 --> 04:46:04,160 we can sort data using python we'll 8362 04:46:02,240 --> 04:46:06,000 begin with the sort method which is used 8363 04:46:04,160 --> 04:46:07,760 with lists and the sort function which 8364 04:46:06,000 --> 04:46:09,680 is used with iterables and that would 8365 04:46:07,760 --> 04:46:11,360 include lists let's start with something 8366 04:46:09,680 --> 04:46:13,280 very basic and then we'll crank up the 8367 04:46:11,360 --> 04:46:15,680 difficulty one step at a time 8368 04:46:13,280 --> 04:46:17,200 i have a list named students and i have 8369 04:46:15,680 --> 04:46:17,680 a bunch of student names within this 8370 04:46:17,200 --> 04:46:19,920 list 8371 04:46:17,680 --> 04:46:21,680 we have squidward sandy patrick 8372 04:46:19,920 --> 04:46:23,520 spongebob mr krabs 8373 04:46:21,680 --> 04:46:25,120 let's say that all of these students are 8374 04:46:23,520 --> 04:46:27,200 taking a college course 8375 04:46:25,120 --> 04:46:28,798 perhaps on marine biology what i would 8376 04:46:27,200 --> 04:46:29,840 like to do is to sort this list in 8377 04:46:28,798 --> 04:46:31,920 alphabetical order 8378 04:46:29,840 --> 04:46:33,040 and to do that we have a method that is 8379 04:46:31,920 --> 04:46:35,280 built in with lists 8380 04:46:33,040 --> 04:46:36,320 so type the name of your list students 8381 04:46:35,280 --> 04:46:38,718 dot 8382 04:46:36,320 --> 04:46:40,240 sort and it's easy as that and to 8383 04:46:38,718 --> 04:46:40,718 display this list i think i'll use a for 8384 04:46:40,240 --> 04:46:44,400 loop 8385 04:46:40,718 --> 04:46:48,320 for i in students 8386 04:46:44,400 --> 04:46:50,000 print i 8387 04:46:48,320 --> 04:46:52,080 and there you go that's how to sort a 8388 04:46:50,000 --> 04:46:52,638 list starting with the top we have mr 8389 04:46:52,080 --> 04:46:54,560 krabs 8390 04:46:52,638 --> 04:46:57,120 then patrick sandy spongebob and 8391 04:46:54,560 --> 04:46:58,718 squidward the sort method of lists can 8392 04:46:57,120 --> 04:47:00,560 accept keyword arguments 8393 04:46:58,718 --> 04:47:02,080 there are two optional keyword arguments 8394 04:47:00,560 --> 04:47:05,840 that we can pass in we can pass 8395 04:47:02,080 --> 04:47:06,878 in key and or reverse we'll cover key a 8396 04:47:05,840 --> 04:47:09,920 little bit later 8397 04:47:06,878 --> 04:47:12,240 if we set reverse to true then our list 8398 04:47:09,920 --> 04:47:13,520 will be sorted by reverse alphabetical 8399 04:47:12,240 --> 04:47:16,160 order beginning with 8400 04:47:13,520 --> 04:47:17,520 squidward spongebob sandy patrick then 8401 04:47:16,160 --> 04:47:19,600 mr krabs 8402 04:47:17,520 --> 04:47:21,840 now the sort method does not work with 8403 04:47:19,600 --> 04:47:22,958 other iterables it's a built-in method 8404 04:47:21,840 --> 04:47:25,280 for lists 8405 04:47:22,958 --> 04:47:26,718 if our list of students was instead 8406 04:47:25,280 --> 04:47:28,560 let's say a tuple 8407 04:47:26,718 --> 04:47:30,000 well then the sort method is not going 8408 04:47:28,560 --> 04:47:32,240 to work you can see here that 8409 04:47:30,000 --> 04:47:34,400 we have an attribute error tuple object 8410 04:47:32,240 --> 04:47:36,000 has no attribute of sort 8411 04:47:34,400 --> 04:47:38,080 that's where the sort function would 8412 04:47:36,000 --> 04:47:38,798 come in because that's useful for other 8413 04:47:38,080 --> 04:47:40,320 iterables 8414 04:47:38,798 --> 04:47:42,958 you can also use this for a list 2 i 8415 04:47:40,320 --> 04:47:45,760 suppose the sort function will return a 8416 04:47:42,958 --> 04:47:47,120 sorted list so let's use the sort 8417 04:47:45,760 --> 04:47:50,240 function this time 8418 04:47:47,120 --> 04:47:53,680 and we'll assign the result to a list 8419 04:47:50,240 --> 04:47:57,360 called sorted students 8420 04:47:53,680 --> 04:47:57,600 equals sorted and then we need to pass 8421 04:47:57,360 --> 04:47:59,360 in 8422 04:47:57,600 --> 04:48:01,200 an iterable and we have the option of 8423 04:47:59,360 --> 04:48:03,840 passing in a key 8424 04:48:01,200 --> 04:48:06,000 and or reverse the iterable that we'll 8425 04:48:03,840 --> 04:48:06,480 pass in as an argument is our tuple 8426 04:48:06,000 --> 04:48:09,200 named 8427 04:48:06,480 --> 04:48:10,000 students so this line will be sorted 8428 04:48:09,200 --> 04:48:13,040 students 8429 04:48:10,000 --> 04:48:14,718 equals sorted students sorted students 8430 04:48:13,040 --> 04:48:17,280 is a list the sorted function will 8431 04:48:14,718 --> 04:48:18,400 return a list but it accepts an iterable 8432 04:48:17,280 --> 04:48:20,638 as an argument 8433 04:48:18,400 --> 04:48:21,440 and to display this we should change 4i 8434 04:48:20,638 --> 04:48:25,280 and students 8435 04:48:21,440 --> 04:48:27,520 to 4i in sorted students and now 8436 04:48:25,280 --> 04:48:28,480 this function will accept our iterable 8437 04:48:27,520 --> 04:48:30,958 our tuple 8438 04:48:28,480 --> 04:48:32,798 and create a sorted list and all of 8439 04:48:30,958 --> 04:48:35,120 these are in alphabetical order 8440 04:48:32,798 --> 04:48:36,560 and to reverse this we can pass in the 8441 04:48:35,120 --> 04:48:39,920 keyword argument of 8442 04:48:36,560 --> 04:48:40,718 reverse equals true and now our list is 8443 04:48:39,920 --> 04:48:42,958 sorted in 8444 04:48:40,718 --> 04:48:44,240 reverse alphabetical order all right 8445 04:48:42,958 --> 04:48:46,638 we're gonna take it up a level 8446 04:48:44,240 --> 04:48:48,878 welcome to level two sometimes data 8447 04:48:46,638 --> 04:48:51,440 isn't always as simple here we have a 8448 04:48:48,878 --> 04:48:53,200 list of tuples each tuple has a 8449 04:48:51,440 --> 04:48:55,600 corresponding student record 8450 04:48:53,200 --> 04:48:56,480 we have a name a letter grade for their 8451 04:48:55,600 --> 04:48:58,718 college course 8452 04:48:56,480 --> 04:48:59,840 and the student's age now how can we 8453 04:48:58,718 --> 04:49:02,638 sort this 8454 04:48:59,840 --> 04:49:03,360 list of tuples by either the student's 8455 04:49:02,638 --> 04:49:05,520 name 8456 04:49:03,360 --> 04:49:06,638 their grade or their age well that's 8457 04:49:05,520 --> 04:49:08,400 where the key 8458 04:49:06,638 --> 04:49:10,320 keyword argument's going to come in with 8459 04:49:08,400 --> 04:49:12,320 sorting if you take a look at this list 8460 04:49:10,320 --> 04:49:14,320 of tuples it somewhat resembles a 8461 04:49:12,320 --> 04:49:15,360 spreadsheet there's rows and then 8462 04:49:14,320 --> 04:49:17,520 there's columns 8463 04:49:15,360 --> 04:49:19,200 the first column corresponds to student 8464 04:49:17,520 --> 04:49:21,440 names the second column 8465 04:49:19,200 --> 04:49:23,360 are grades and the third column are all 8466 04:49:21,440 --> 04:49:25,920 the ages of the students 8467 04:49:23,360 --> 04:49:27,520 so by default sorting by the first 8468 04:49:25,920 --> 04:49:28,320 column is actually really easy that's 8469 04:49:27,520 --> 04:49:30,400 the default 8470 04:49:28,320 --> 04:49:31,760 so if we need to sort alphabetically 8471 04:49:30,400 --> 04:49:33,760 that would be the same process 8472 04:49:31,760 --> 04:49:35,200 as before we would type the name of the 8473 04:49:33,760 --> 04:49:38,798 list students 8474 04:49:35,200 --> 04:49:42,480 dot sort but if we were to print 8475 04:49:38,798 --> 04:49:44,400 this iterable for i in students 8476 04:49:42,480 --> 04:49:45,840 instead of just the individual student 8477 04:49:44,400 --> 04:49:48,878 names we're going to get 8478 04:49:45,840 --> 04:49:50,718 each tuple that we have so now all of 8479 04:49:48,878 --> 04:49:52,560 these tuples are arranged in 8480 04:49:50,718 --> 04:49:54,718 alphabetical order starting with 8481 04:49:52,560 --> 04:49:56,000 the first column that we have which are 8482 04:49:54,718 --> 04:49:58,560 all the student names 8483 04:49:56,000 --> 04:49:59,760 now how can we sort these iterables by 8484 04:49:58,560 --> 04:50:01,840 their second column 8485 04:49:59,760 --> 04:50:03,840 for this case it would be student grades 8486 04:50:01,840 --> 04:50:04,958 or even the third column which would be 8487 04:50:03,840 --> 04:50:07,040 the student ages 8488 04:50:04,958 --> 04:50:08,400 well that's where the key keyword 8489 04:50:07,040 --> 04:50:10,798 argument's going to come in 8490 04:50:08,400 --> 04:50:13,040 this is a keyword argument and we set 8491 04:50:10,798 --> 04:50:13,920 key equal to a function that's going to 8492 04:50:13,040 --> 04:50:16,638 return 8493 04:50:13,920 --> 04:50:17,440 the index of that specific column that 8494 04:50:16,638 --> 04:50:20,638 we have 8495 04:50:17,440 --> 04:50:22,878 so let's say key equals grid grade is 8496 04:50:20,638 --> 04:50:26,000 going to be a function object 8497 04:50:22,878 --> 04:50:29,120 grade equals and we can easily use a 8498 04:50:26,000 --> 04:50:32,480 lambda expression for this lambda 8499 04:50:29,120 --> 04:50:36,320 let's say grades colon 8500 04:50:32,480 --> 04:50:39,040 grades and we will set an index of one 8501 04:50:36,320 --> 04:50:39,520 now the first index is zero that's the 8502 04:50:39,040 --> 04:50:41,200 first 8503 04:50:39,520 --> 04:50:44,000 column because computers always start 8504 04:50:41,200 --> 04:50:44,798 with zero column two would have an index 8505 04:50:44,000 --> 04:50:47,200 of one 8506 04:50:44,798 --> 04:50:48,560 and then column three would have an 8507 04:50:47,200 --> 04:50:51,760 index of two 8508 04:50:48,560 --> 04:50:54,240 so grade equals lambda grids 8509 04:50:51,760 --> 04:50:55,760 colon grids index one you can also 8510 04:50:54,240 --> 04:50:58,638 rename these if you want 8511 04:50:55,760 --> 04:50:59,120 so students dot sort we're setting the 8512 04:50:58,638 --> 04:51:01,520 key 8513 04:50:59,120 --> 04:51:02,240 equal to grade and grade is a function 8514 04:51:01,520 --> 04:51:05,040 object 8515 04:51:02,240 --> 04:51:05,840 via a lambda function and now all of 8516 04:51:05,040 --> 04:51:08,000 these students 8517 04:51:05,840 --> 04:51:09,040 will be sorted by their grades starting 8518 04:51:08,000 --> 04:51:11,840 with sandy 8519 04:51:09,040 --> 04:51:12,798 then spongebob mr krabs patrick then 8520 04:51:11,840 --> 04:51:15,040 squidward 8521 04:51:12,798 --> 04:51:16,958 if this needs to be in reverse order we 8522 04:51:15,040 --> 04:51:17,920 can pass in that other keyword argument 8523 04:51:16,958 --> 04:51:21,440 of reverse 8524 04:51:17,920 --> 04:51:23,440 equals true and for practice if you 8525 04:51:21,440 --> 04:51:24,400 wanted to sort all of this data by each 8526 04:51:23,440 --> 04:51:26,480 student's age 8527 04:51:24,400 --> 04:51:28,480 then we would change grade to let's say 8528 04:51:26,480 --> 04:51:31,760 age age equals lambda 8529 04:51:28,480 --> 04:51:34,878 we'll change grades to maybe ages colon 8530 04:51:31,760 --> 04:51:37,600 ages and the index would then be two 8531 04:51:34,878 --> 04:51:38,480 because the first column here is zero 8532 04:51:37,600 --> 04:51:41,200 then one 8533 04:51:38,480 --> 04:51:42,000 then two and change key to our function 8534 04:51:41,200 --> 04:51:45,440 object 8535 04:51:42,000 --> 04:51:47,920 of edge now each student is 8536 04:51:45,440 --> 04:51:48,958 sorted numerically beginning with the 8537 04:51:47,920 --> 04:51:50,480 smallest edge 8538 04:51:48,958 --> 04:51:52,878 well the youngest person starting with 8539 04:51:50,480 --> 04:51:53,840 spongebob sandy patrick squidward then 8540 04:51:52,878 --> 04:51:56,080 mr krabs 8541 04:51:53,840 --> 04:51:58,400 and like i said before if you want your 8542 04:51:56,080 --> 04:51:58,718 data arranged in reverse order you can 8543 04:51:58,400 --> 04:52:01,360 set 8544 04:51:58,718 --> 04:52:02,958 that keyword argument of reverse to true 8545 04:52:01,360 --> 04:52:05,040 within the sort method 8546 04:52:02,958 --> 04:52:07,120 and all of our student data is sorted 8547 04:52:05,040 --> 04:52:08,320 beginning with the eldest student which 8548 04:52:07,120 --> 04:52:10,560 is mr krabs 8549 04:52:08,320 --> 04:52:11,520 then squidward patrick sandy and then 8550 04:52:10,560 --> 04:52:13,040 spongebob 8551 04:52:11,520 --> 04:52:14,958 and for the last part of this video 8552 04:52:13,040 --> 04:52:17,360 let's say that we're working with 8553 04:52:14,958 --> 04:52:19,200 some other iterable let's say we have a 8554 04:52:17,360 --> 04:52:21,520 tuple of tuples instead 8555 04:52:19,200 --> 04:52:23,280 well we can no longer use this sort 8556 04:52:21,520 --> 04:52:24,160 method because that only belongs to 8557 04:52:23,280 --> 04:52:25,840 lists 8558 04:52:24,160 --> 04:52:27,840 you can also use the sort function with 8559 04:52:25,840 --> 04:52:29,680 lists and it will generate a new sorted 8560 04:52:27,840 --> 04:52:30,638 list without changing the placement of 8561 04:52:29,680 --> 04:52:33,120 the original 8562 04:52:30,638 --> 04:52:34,240 so let's say we have a tuple of tuples 8563 04:52:33,120 --> 04:52:37,040 and this will create a 8564 04:52:34,240 --> 04:52:38,480 new sorted list let's call this list 8565 04:52:37,040 --> 04:52:41,680 sorted 8566 04:52:38,480 --> 04:52:43,920 students equals then we'll use the 8567 04:52:41,680 --> 04:52:46,320 sorted function we need to pass in an 8568 04:52:43,920 --> 04:52:48,958 iterable and we can pass in 8569 04:52:46,320 --> 04:52:49,920 both a key and or the reverse keyword 8570 04:52:48,958 --> 04:52:53,360 argument 8571 04:52:49,920 --> 04:52:56,878 so the iterable would be students 8572 04:52:53,360 --> 04:52:58,718 and let's set the key equal to edge 8573 04:52:56,878 --> 04:53:00,480 and i will not reverse it we can keep it 8574 04:52:58,718 --> 04:53:04,320 as it is but let's change 8575 04:53:00,480 --> 04:53:06,080 for i in students to sorted students 8576 04:53:04,320 --> 04:53:07,680 and that's how to use the sorted 8577 04:53:06,080 --> 04:53:10,400 function to sort 8578 04:53:07,680 --> 04:53:11,280 an iterable including a list well all 8579 04:53:10,400 --> 04:53:13,680 right everyone 8580 04:53:11,280 --> 04:53:15,040 that's how to sort iterables in python 8581 04:53:13,680 --> 04:53:16,718 if you would like a copy of all this 8582 04:53:15,040 --> 04:53:18,320 code i will post all of this to the 8583 04:53:16,718 --> 04:53:20,638 comments section down below 8584 04:53:18,320 --> 04:53:21,360 but yeah that's a basic way to sort 8585 04:53:20,638 --> 04:53:24,560 iterables 8586 04:53:21,360 --> 04:53:26,240 in python hey what's going on everybody 8587 04:53:24,560 --> 04:53:27,680 it's your bro hope you're doing well in 8588 04:53:26,240 --> 04:53:30,160 today's video i'm going to show you all 8589 04:53:27,680 --> 04:53:34,798 how the map function works in python so 8590 04:53:30,160 --> 04:53:36,878 sit back relax and enjoy the show 8591 04:53:34,798 --> 04:53:39,120 what up everybody let's discuss the map 8592 04:53:36,878 --> 04:53:41,520 function the map function will apply a 8593 04:53:39,120 --> 04:53:43,840 function to each item in an interval 8594 04:53:41,520 --> 04:53:44,560 that would include lists tuples things 8595 04:53:43,840 --> 04:53:46,400 like that 8596 04:53:44,560 --> 04:53:48,160 and the map function accepts two 8597 04:53:46,400 --> 04:53:50,000 arguments are iterable 8598 04:53:48,160 --> 04:53:52,638 as well as the function we would like to 8599 04:53:50,000 --> 04:53:54,240 apply to each item within our iterable 8600 04:53:52,638 --> 04:53:56,798 for example let's say that we have an 8601 04:53:54,240 --> 04:53:59,920 online store to represent this i have 8602 04:53:56,798 --> 04:54:01,040 a list of tuples named store each tuple 8603 04:53:59,920 --> 04:54:03,200 within this list 8604 04:54:01,040 --> 04:54:04,080 corresponds to an item for sale within 8605 04:54:03,200 --> 04:54:06,638 our store 8606 04:54:04,080 --> 04:54:07,840 there's shirts pants jackets and socks 8607 04:54:06,638 --> 04:54:10,958 and each item has 8608 04:54:07,840 --> 04:54:12,560 a price in us dollars let's say now what 8609 04:54:10,958 --> 04:54:14,400 i would like to do is to convert all of 8610 04:54:12,560 --> 04:54:16,560 the prices within my store 8611 04:54:14,400 --> 04:54:18,878 to euros and currently the conversion 8612 04:54:16,560 --> 04:54:22,000 rate as of the filming of this video is 8613 04:54:18,878 --> 04:54:24,000 one us dollar to 0.82 euros 8614 04:54:22,000 --> 04:54:25,520 so let's begin we can easily convert 8615 04:54:24,000 --> 04:54:29,520 dollars to euros with a 8616 04:54:25,520 --> 04:54:34,240 lambda function so let's say two euros 8617 04:54:29,520 --> 04:54:36,160 equals lambda the parameter will be data 8618 04:54:34,240 --> 04:54:38,080 colon and the next part's a little bit 8619 04:54:36,160 --> 04:54:39,920 tricky within a set of parenthesis to 8620 04:54:38,080 --> 04:54:42,878 represent a tuple 8621 04:54:39,920 --> 04:54:45,600 we'll take data at index 0 that 8622 04:54:42,878 --> 04:54:49,120 corresponds to the first column 8623 04:54:45,600 --> 04:54:52,000 comma data at index 1. 8624 04:54:49,120 --> 04:54:52,798 this represents the index of our prices 8625 04:54:52,000 --> 04:54:57,200 we're going to take 8626 04:54:52,798 --> 04:54:59,440 data at index 1 times 0.82 8627 04:54:57,200 --> 04:55:00,480 and that's it the first column will be 8628 04:54:59,440 --> 04:55:03,840 left untouched 8629 04:55:00,480 --> 04:55:05,840 but the data in column 1 will be 8630 04:55:03,840 --> 04:55:07,840 multiplied by 0.82 8631 04:55:05,840 --> 04:55:10,480 now our map function is going to create 8632 04:55:07,840 --> 04:55:12,560 a map object but we can easily cast that 8633 04:55:10,480 --> 04:55:16,080 to a different type of iterable 8634 04:55:12,560 --> 04:55:19,760 so let's say that store underscore 8635 04:55:16,080 --> 04:55:21,840 euros equals map 8636 04:55:19,760 --> 04:55:23,760 parenthesis and then we can pass in our 8637 04:55:21,840 --> 04:55:27,120 function as well as our iterable 8638 04:55:23,760 --> 04:55:31,520 our function is our two euros function 8639 04:55:27,120 --> 04:55:33,680 comma and our iterable is our store 8640 04:55:31,520 --> 04:55:36,160 and that is it now if you want to 8641 04:55:33,680 --> 04:55:38,160 convert this map object to an iterable 8642 04:55:36,160 --> 04:55:40,878 you can easily surround this with a cast 8643 04:55:38,160 --> 04:55:42,000 i would like to convert this map object 8644 04:55:40,878 --> 04:55:44,878 to a list 8645 04:55:42,000 --> 04:55:45,520 and we will now have a new store named 8646 04:55:44,878 --> 04:55:47,360 store 8647 04:55:45,520 --> 04:55:49,680 euros which will have all of the 8648 04:55:47,360 --> 04:55:52,000 different prices of our store in euros 8649 04:55:49,680 --> 04:55:52,958 and to display this i'll use a for loop 8650 04:55:52,000 --> 04:55:56,560 for i 8651 04:55:52,958 --> 04:56:00,000 in store euros 8652 04:55:56,560 --> 04:56:03,520 print i and 8653 04:56:00,000 --> 04:56:05,600 let's try it yep that seems about right 8654 04:56:03,520 --> 04:56:06,638 all of the prices in our store are now 8655 04:56:05,600 --> 04:56:08,560 in euros 8656 04:56:06,638 --> 04:56:10,240 let's pretend that these prices are 8657 04:56:08,560 --> 04:56:10,958 already in euros and we would like to 8658 04:56:10,240 --> 04:56:13,280 convert these 8659 04:56:10,958 --> 04:56:15,120 to american dollars so we will use the 8660 04:56:13,280 --> 04:56:18,000 same conversion rate but divide 8661 04:56:15,120 --> 04:56:19,280 by 0.82 so let's create a separate 8662 04:56:18,000 --> 04:56:22,400 lambda function 8663 04:56:19,280 --> 04:56:25,520 two dollars equals 8664 04:56:22,400 --> 04:56:26,638 i think i'll just copy this but we are 8665 04:56:25,520 --> 04:56:30,958 going to divide 8666 04:56:26,638 --> 04:56:34,718 the first index of data by 0.82 8667 04:56:30,958 --> 04:56:35,360 and let's say store dollars to represent 8668 04:56:34,718 --> 04:56:37,200 our store 8669 04:56:35,360 --> 04:56:40,480 in dollars because we're pretending now 8670 04:56:37,200 --> 04:56:40,480 that it's currently in euros 8671 04:56:41,680 --> 04:56:48,560 and we will display 4i in store dollars 8672 04:56:45,840 --> 04:56:50,400 so if all of these prices are in euros 8673 04:56:48,560 --> 04:56:51,280 we are now converting them to american 8674 04:56:50,400 --> 04:56:52,718 dollars 8675 04:56:51,280 --> 04:56:54,480 but you may want to do some rounding as 8676 04:56:52,718 --> 04:56:56,638 opposed to the nearest descent 8677 04:56:54,480 --> 04:56:58,320 well everyone that is the map function 8678 04:56:56,638 --> 04:57:00,638 it applies a function to 8679 04:56:58,320 --> 04:57:01,760 each item in an iterable those include 8680 04:57:00,638 --> 04:57:04,718 lists tuples 8681 04:57:01,760 --> 04:57:07,040 the like you pass in your iterable and 8682 04:57:04,718 --> 04:57:09,200 your function as an argument to the map 8683 04:57:07,040 --> 04:57:11,040 function so if you would like a copy of 8684 04:57:09,200 --> 04:57:12,638 all this code i will post all of this to 8685 04:57:11,040 --> 04:57:14,798 the comment section down below 8686 04:57:12,638 --> 04:57:17,840 but yeah that is how the map function 8687 04:57:14,798 --> 04:57:19,760 works in python 8688 04:57:17,840 --> 04:57:21,360 what's going on everybody it's you bro 8689 04:57:19,760 --> 04:57:22,958 hope you're doing well and in today's 8690 04:57:21,360 --> 04:57:24,878 video i'm going to show you all how the 8691 04:57:22,958 --> 04:57:29,920 filter function works in python 8692 04:57:24,878 --> 04:57:29,920 so sit back relax and enjoy the show 8693 04:57:30,000 --> 04:57:33,440 all right all right let's discuss the 8694 04:57:32,320 --> 04:57:35,600 filter function 8695 04:57:33,440 --> 04:57:36,878 the filter function creates a collection 8696 04:57:35,600 --> 04:57:39,680 of elements from an 8697 04:57:36,878 --> 04:57:40,400 iterable for which a function returns 8698 04:57:39,680 --> 04:57:42,320 true 8699 04:57:40,400 --> 04:57:44,958 now what the heck does that mean let me 8700 04:57:42,320 --> 04:57:45,840 explain let's say that we have a list of 8701 04:57:44,958 --> 04:57:48,080 tuples named 8702 04:57:45,840 --> 04:57:49,920 friends each tuple has the name of a 8703 04:57:48,080 --> 04:57:51,360 friend as well as their age 8704 04:57:49,920 --> 04:57:53,120 i just made up some ages for all of 8705 04:57:51,360 --> 04:57:53,840 these friends what i would like to do is 8706 04:57:53,120 --> 04:57:55,920 to create a 8707 04:57:53,840 --> 04:57:57,360 separate list for all the friends that 8708 04:57:55,920 --> 04:57:59,200 are 18 or older 8709 04:57:57,360 --> 04:58:01,120 so let's say that we're all going out 8710 04:57:59,200 --> 04:58:02,638 drinking and i would like to create a 8711 04:58:01,120 --> 04:58:04,718 list of my drinking buddies 8712 04:58:02,638 --> 04:58:06,240 because they're of drinking age i'm in 8713 04:58:04,718 --> 04:58:07,520 the united states and the drinking age 8714 04:58:06,240 --> 04:58:09,760 is currently 21 8715 04:58:07,520 --> 04:58:11,360 but i think worldwide is 18 so let's go 8716 04:58:09,760 --> 04:58:13,440 with 18 for this example since 8717 04:58:11,360 --> 04:58:15,120 i have a worldwide audience all right 8718 04:58:13,440 --> 04:58:17,520 now with this filter function 8719 04:58:15,120 --> 04:58:18,560 we have to pass in our iterable as well 8720 04:58:17,520 --> 04:58:21,200 as a function 8721 04:58:18,560 --> 04:58:22,798 to get anybody's edge that is over 18. 8722 04:58:21,200 --> 04:58:24,958 so let's begin by writing a 8723 04:58:22,798 --> 04:58:26,160 lambda function for this expression here 8724 04:58:24,958 --> 04:58:30,560 our filter function 8725 04:58:26,160 --> 04:58:34,400 so let's say age equals lambda 8726 04:58:30,560 --> 04:58:38,080 data will be the parameter colon data 8727 04:58:34,400 --> 04:58:40,240 at index one so the first index 8728 04:58:38,080 --> 04:58:42,480 the first column in this chart here 8729 04:58:40,240 --> 04:58:45,280 would be zero for other names the next 8730 04:58:42,480 --> 04:58:48,320 index of one would be all the ages 8731 04:58:45,280 --> 04:58:51,600 data at index one is greater than 8732 04:58:48,320 --> 04:58:53,680 or equal to eighteen and that is it 8733 04:58:51,600 --> 04:58:55,040 that is our lambda function and now 8734 04:58:53,680 --> 04:58:57,840 we're going to filter 8735 04:58:55,040 --> 04:58:59,120 our iterable by this function of age so 8736 04:58:57,840 --> 04:59:02,240 let's write 8737 04:58:59,120 --> 04:59:06,000 filter pass in our function 8738 04:59:02,240 --> 04:59:08,878 which is edge comma and our iterable 8739 04:59:06,000 --> 04:59:10,000 friends now this filter function will 8740 04:59:08,878 --> 04:59:11,840 return a filter 8741 04:59:10,000 --> 04:59:13,280 object you can easily cast this i would 8742 04:59:11,840 --> 04:59:17,360 like to cast 8743 04:59:13,280 --> 04:59:19,520 my friends list back into a list 8744 04:59:17,360 --> 04:59:20,400 and i will assign this to a new list 8745 04:59:19,520 --> 04:59:24,240 let's say 8746 04:59:20,400 --> 04:59:27,600 drinking buddies 8747 04:59:24,240 --> 04:59:30,240 equals list filter age 8748 04:59:27,600 --> 04:59:30,718 and friends and i should be able to 8749 04:59:30,240 --> 04:59:33,280 print 8750 04:59:30,718 --> 04:59:34,400 all of the friends that are 18 or older 8751 04:59:33,280 --> 04:59:38,160 for i 8752 04:59:34,400 --> 04:59:42,080 in drinking bodies 8753 04:59:38,160 --> 04:59:44,878 print i 8754 04:59:42,080 --> 04:59:45,840 and we have rachel monica chandler and 8755 04:59:44,878 --> 04:59:47,600 ross 8756 04:59:45,840 --> 04:59:49,600 well everyone that is the filter 8757 04:59:47,600 --> 04:59:50,240 function it creates a collection of 8758 04:59:49,600 --> 04:59:52,400 elements 8759 04:59:50,240 --> 04:59:53,520 from an iterable for which a function 8760 04:59:52,400 --> 04:59:55,520 returns true 8761 04:59:53,520 --> 04:59:57,760 i like to think of it as the search 8762 04:59:55,520 --> 05:00:00,160 results we're searching for any results 8763 04:59:57,760 --> 05:00:02,160 that meet this criteria where age is 8764 05:00:00,160 --> 05:00:03,600 greater than or equal to 18 8765 05:00:02,160 --> 05:00:05,280 and then you can create a separate 8766 05:00:03,600 --> 05:00:06,718 collection or other list if you want 8767 05:00:05,280 --> 05:00:08,718 from the results 8768 05:00:06,718 --> 05:00:10,480 but yeah that is how the filter function 8769 05:00:08,718 --> 05:00:12,958 works in python 8770 05:00:10,480 --> 05:00:13,920 yo what's going on everybody it's bro 8771 05:00:12,958 --> 05:00:15,360 hope you're doing well 8772 05:00:13,920 --> 05:00:17,200 and in today's video i'm going to show 8773 05:00:15,360 --> 05:00:18,160 you how the reduce function works in 8774 05:00:17,200 --> 05:00:22,560 python so 8775 05:00:18,160 --> 05:00:24,400 sit back relax and enjoy the show 8776 05:00:22,560 --> 05:00:26,320 well i suppose we're on the reduce 8777 05:00:24,400 --> 05:00:28,400 function today the reduce function 8778 05:00:26,320 --> 05:00:31,520 applies a function of our choosing 8779 05:00:28,400 --> 05:00:34,240 to an iterable and reduces that iterable 8780 05:00:31,520 --> 05:00:36,000 to a single cumulative value the way 8781 05:00:34,240 --> 05:00:37,440 this works is that the reduce function 8782 05:00:36,000 --> 05:00:39,280 performs our function 8783 05:00:37,440 --> 05:00:41,360 on the first two elements of our 8784 05:00:39,280 --> 05:00:44,320 iterable and repeats the process 8785 05:00:41,360 --> 05:00:45,440 until only one value remains i kind of 8786 05:00:44,320 --> 05:00:47,200 like to think of it like 8787 05:00:45,440 --> 05:00:49,120 we're recycling elements within an 8788 05:00:47,200 --> 05:00:51,440 iterable until a single 8789 05:00:49,120 --> 05:00:53,600 value remains our finished product so 8790 05:00:51,440 --> 05:00:55,360 how is this useful let's say that 8791 05:00:53,600 --> 05:00:57,440 we're playing a game scramble and we 8792 05:00:55,360 --> 05:00:58,400 have a bunch of different letters within 8793 05:00:57,440 --> 05:01:01,120 an iterable 8794 05:00:58,400 --> 05:01:03,200 a list is a type of iterable so i have a 8795 05:01:01,120 --> 05:01:05,200 list named letters and we have all of 8796 05:01:03,200 --> 05:01:07,520 the letters we need to spell the word 8797 05:01:05,200 --> 05:01:09,600 what i would like to do is to reduce all 8798 05:01:07,520 --> 05:01:12,638 of these individual elements into a 8799 05:01:09,600 --> 05:01:14,958 single cumulative value until only one 8800 05:01:12,638 --> 05:01:16,718 value remains so we can do that using 8801 05:01:14,958 --> 05:01:18,160 the reduce function you can also do this 8802 05:01:16,718 --> 05:01:19,360 with the for loop too there's multiple 8803 05:01:18,160 --> 05:01:21,840 ways of doing this 8804 05:01:19,360 --> 05:01:23,440 so using the reduce function we're going 8805 05:01:21,840 --> 05:01:28,320 to import 8806 05:01:23,440 --> 05:01:30,718 func tools and now let's say we have 8807 05:01:28,320 --> 05:01:32,320 our cumulative value stored within a 8808 05:01:30,718 --> 05:01:36,718 variable named word 8809 05:01:32,320 --> 05:01:38,400 word equals func tools dot reduce 8810 05:01:36,718 --> 05:01:40,798 now the reduce function has two 8811 05:01:38,400 --> 05:01:41,280 arguments the function we would like to 8812 05:01:40,798 --> 05:01:43,520 apply 8813 05:01:41,280 --> 05:01:44,400 to the first two elements and then our 8814 05:01:43,520 --> 05:01:45,920 iterable 8815 05:01:44,400 --> 05:01:48,000 the interval is easy that is just 8816 05:01:45,920 --> 05:01:50,000 letters so let's put that in 8817 05:01:48,000 --> 05:01:52,480 letters and for the function we can 8818 05:01:50,000 --> 05:01:55,520 easily write a lambda function for this 8819 05:01:52,480 --> 05:01:56,718 lambda and now this function needs two 8820 05:01:55,520 --> 05:02:00,320 parameters 8821 05:01:56,718 --> 05:02:02,240 let's say x and y now what kind of 8822 05:02:00,320 --> 05:02:04,718 function do we want to perform on the 8823 05:02:02,240 --> 05:02:07,120 first two elements of our iterable 8824 05:02:04,718 --> 05:02:07,760 let's combine them together so let's say 8825 05:02:07,120 --> 05:02:10,878 x 8826 05:02:07,760 --> 05:02:14,638 plus y and now 8827 05:02:10,878 --> 05:02:18,320 let's print our single cumulative value 8828 05:02:14,638 --> 05:02:21,040 of word so this should print 8829 05:02:18,320 --> 05:02:22,718 the word hello i'm fairly confident that 8830 05:02:21,040 --> 05:02:24,798 the word hello is worth like 8831 05:02:22,718 --> 05:02:26,000 20 points in scrabble now let me explain 8832 05:02:24,798 --> 05:02:28,560 what happened step by 8833 05:02:26,000 --> 05:02:29,120 step our reduce function applies our 8834 05:02:28,560 --> 05:02:31,760 function 8835 05:02:29,120 --> 05:02:32,638 to the first two elements within our 8836 05:02:31,760 --> 05:02:35,360 iterable 8837 05:02:32,638 --> 05:02:37,040 it performs this expression that we set 8838 05:02:35,360 --> 05:02:38,160 x plus y but really we can write 8839 05:02:37,040 --> 05:02:41,040 anything here 8840 05:02:38,160 --> 05:02:43,360 so we perform this expression on the 8841 05:02:41,040 --> 05:02:46,160 first two elements 8842 05:02:43,360 --> 05:02:46,958 then we repeat the process again using 8843 05:02:46,160 --> 05:02:48,560 the result 8844 05:02:46,958 --> 05:02:50,718 from the first time we use this 8845 05:02:48,560 --> 05:02:54,798 expression and the next element 8846 05:02:50,718 --> 05:02:56,480 so we're combining x plus y again 8847 05:02:54,798 --> 05:02:58,240 and then we're just going to repeat this 8848 05:02:56,480 --> 05:03:00,400 process until only 8849 05:02:58,240 --> 05:03:01,360 one value remains it's kind of like 8850 05:03:00,400 --> 05:03:03,280 we're recycling 8851 05:03:01,360 --> 05:03:05,120 all of these letters and creating a 8852 05:03:03,280 --> 05:03:07,360 finished product out of all of these 8853 05:03:05,120 --> 05:03:08,958 that's how i think of it at least here's 8854 05:03:07,360 --> 05:03:10,080 a different example let's say that i 8855 05:03:08,958 --> 05:03:13,680 would like to find 8856 05:03:10,080 --> 05:03:16,560 the factorial of 5 so i have the numbers 8857 05:03:13,680 --> 05:03:17,600 5 through 1 within a list named 8858 05:03:16,560 --> 05:03:20,320 factorial 8859 05:03:17,600 --> 05:03:21,760 so if i was to find the factorial of 5 8860 05:03:20,320 --> 05:03:23,280 via the reduce function 8861 05:03:21,760 --> 05:03:25,280 i would probably write something like 8862 05:03:23,280 --> 05:03:28,480 this so let me change 8863 05:03:25,280 --> 05:03:31,840 word to let's say result result 8864 05:03:28,480 --> 05:03:32,878 equals functools reduce now our lambda 8865 05:03:31,840 --> 05:03:36,160 function will be 8866 05:03:32,878 --> 05:03:37,440 x times y because with factorials we 8867 05:03:36,160 --> 05:03:40,160 multiply 8868 05:03:37,440 --> 05:03:41,120 the next two numbers together then we 8869 05:03:40,160 --> 05:03:42,958 take that result 8870 05:03:41,120 --> 05:03:44,480 and then multiply the next one in this 8871 05:03:42,958 --> 05:03:47,840 list so the 8872 05:03:44,480 --> 05:03:51,200 iterable will be factorial and 8873 05:03:47,840 --> 05:03:55,040 let's print what our result is 8874 05:03:51,200 --> 05:03:56,798 so the factorial of 5 should be 120. now 8875 05:03:55,040 --> 05:03:59,360 let's walk through this we're taking 8876 05:03:56,798 --> 05:03:59,840 the first two elements of this iterable 8877 05:03:59,360 --> 05:04:02,080 five 8878 05:03:59,840 --> 05:04:05,040 and four multiplying together in this 8879 05:04:02,080 --> 05:04:07,040 case in the last example we concatenated 8880 05:04:05,040 --> 05:04:08,560 the strings together of x and y 8881 05:04:07,040 --> 05:04:10,240 in this example we're multiplying them 8882 05:04:08,560 --> 05:04:13,360 together so we take 8883 05:04:10,240 --> 05:04:15,760 the first two elements 5 times 4 8884 05:04:13,360 --> 05:04:17,120 we take the result and apply it to the 8885 05:04:15,760 --> 05:04:20,638 next iteration 8886 05:04:17,120 --> 05:04:24,240 of x times y again so 20 times 3 8887 05:04:20,638 --> 05:04:28,240 is 60. we repeat this process again 8888 05:04:24,240 --> 05:04:31,600 60 times 2 is 120 and 120 times 1 8889 05:04:28,240 --> 05:04:32,000 is 120 so that's kind of how the reduce 8890 05:04:31,600 --> 05:04:34,000 function 8891 05:04:32,000 --> 05:04:36,080 works it applies a function of our 8892 05:04:34,000 --> 05:04:38,560 choosing to the first 8893 05:04:36,080 --> 05:04:39,760 two elements of an iterable and repeats 8894 05:04:38,560 --> 05:04:43,040 that process until 8895 05:04:39,760 --> 05:04:44,958 only a single cumulative value remains 8896 05:04:43,040 --> 05:04:46,560 so that is the reduce function if you 8897 05:04:44,958 --> 05:04:48,160 would like a copy of all this code i 8898 05:04:46,560 --> 05:04:49,440 will post all of this to the comment 8899 05:04:48,160 --> 05:04:51,520 section down below 8900 05:04:49,440 --> 05:04:54,320 but yeah that is how the reduce function 8901 05:04:51,520 --> 05:04:56,400 works in python 8902 05:04:54,320 --> 05:04:58,160 hey what's going on everybody it's your 8903 05:04:56,400 --> 05:04:59,760 bro hope you're doing well and in this 8904 05:04:58,160 --> 05:05:02,000 video i'm going to show you all how list 8905 05:04:59,760 --> 05:05:06,560 comprehensions work in python so 8906 05:05:02,000 --> 05:05:08,080 sit back relax and enjoy the show 8907 05:05:06,560 --> 05:05:10,638 all right everybody so list 8908 05:05:08,080 --> 05:05:12,798 comprehensions a list comprehension is a 8909 05:05:10,638 --> 05:05:15,040 way to create a new list with 8910 05:05:12,798 --> 05:05:16,560 less syntax you can also use a list 8911 05:05:15,040 --> 05:05:18,958 comprehension to mimic 8912 05:05:16,560 --> 05:05:20,480 certain lambda functions such as in the 8913 05:05:18,958 --> 05:05:23,040 previous videos where we use 8914 05:05:20,480 --> 05:05:24,798 the map and the filter functions and not 8915 05:05:23,040 --> 05:05:27,440 only that but a list comprehension 8916 05:05:24,798 --> 05:05:29,120 is easier to read than a lambda function 8917 05:05:27,440 --> 05:05:29,920 but there is a formula that we're going 8918 05:05:29,120 --> 05:05:32,320 to follow 8919 05:05:29,920 --> 05:05:34,160 when we create our list within our list 8920 05:05:32,320 --> 05:05:37,120 we're going to write an expression 8921 05:05:34,160 --> 05:05:39,440 for item in iterable now let me give you 8922 05:05:37,120 --> 05:05:40,560 an example of where a list comprehension 8923 05:05:39,440 --> 05:05:42,638 would be useful 8924 05:05:40,560 --> 05:05:44,320 let's take this program that will create 8925 05:05:42,638 --> 05:05:46,878 a list of all the numbers 8926 05:05:44,320 --> 05:05:48,320 1 through 10 squared so if i were to 8927 05:05:46,878 --> 05:05:48,958 print this we'll get the numbers 1 8928 05:05:48,320 --> 05:05:50,798 through 10 8929 05:05:48,958 --> 05:05:52,638 whatever their square is right so we 8930 05:05:50,798 --> 05:05:55,360 have 1 through 10 8931 05:05:52,638 --> 05:05:56,160 and the square of 10 is 100 so we wrote 8932 05:05:55,360 --> 05:05:58,080 this program 8933 05:05:56,160 --> 05:06:00,320 in three lines of code we're creating an 8934 05:05:58,080 --> 05:06:01,840 empty list we're creating a for loop 8935 05:06:00,320 --> 05:06:04,160 and then we're writing an expression for 8936 05:06:01,840 --> 05:06:05,840 what we want each loop iteration to do 8937 05:06:04,160 --> 05:06:07,680 now let's write the same program but 8938 05:06:05,840 --> 05:06:09,840 used a list comprehension 8939 05:06:07,680 --> 05:06:10,958 and we can write the same program with 8940 05:06:09,840 --> 05:06:12,718 less syntax 8941 05:06:10,958 --> 05:06:14,878 so i would write something like this and 8942 05:06:12,718 --> 05:06:17,920 we're going to follow this formula 8943 05:06:14,878 --> 05:06:21,360 so our list is going to be named 8944 05:06:17,920 --> 05:06:22,400 squares equals then within a set of 8945 05:06:21,360 --> 05:06:24,160 straight brackets 8946 05:06:22,400 --> 05:06:27,840 we're going to write our expression 8947 05:06:24,160 --> 05:06:31,520 which is this portion i times i 8948 05:06:27,840 --> 05:06:34,638 for item so that would be 4i 8949 05:06:31,520 --> 05:06:38,080 in our iterable that would be range 8950 05:06:34,638 --> 05:06:39,280 1 through 11 because remember 11's 8951 05:06:38,080 --> 05:06:43,280 exclusive 8952 05:06:39,280 --> 05:06:47,040 and then let's print our squares print 8953 05:06:43,280 --> 05:06:49,440 squares and there you go 8954 05:06:47,040 --> 05:06:50,718 we made the exact same program using a 8955 05:06:49,440 --> 05:06:52,718 list comprehension 8956 05:06:50,718 --> 05:06:55,680 so basically speaking you just follow 8957 05:06:52,718 --> 05:06:58,000 this formula you set your list equal to 8958 05:06:55,680 --> 05:07:00,480 your expression and our expression for 8959 05:06:58,000 --> 05:07:03,680 this example is i times i 8960 05:07:00,480 --> 05:07:05,920 for item for i in 8961 05:07:03,680 --> 05:07:06,878 your interval in range one through 8962 05:07:05,920 --> 05:07:09,520 eleven 8963 05:07:06,878 --> 05:07:11,120 so this took three lines of code well if 8964 05:07:09,520 --> 05:07:14,160 you exclude us printing 8965 05:07:11,120 --> 05:07:14,958 our list and this program took one line 8966 05:07:14,160 --> 05:07:17,200 of code 8967 05:07:14,958 --> 05:07:19,200 now we can also use a list comprehension 8968 05:07:17,200 --> 05:07:20,080 to mimic certain lambda functions i 8969 05:07:19,200 --> 05:07:22,638 wrote a program 8970 05:07:20,080 --> 05:07:23,600 where we will be filtering a list of 8971 05:07:22,638 --> 05:07:25,360 student grades 8972 05:07:23,600 --> 05:07:26,638 let's pretend that all of these students 8973 05:07:25,360 --> 05:07:28,638 took some sort of exam 8974 05:07:26,638 --> 05:07:32,480 so we have one student that received a 8975 05:07:28,638 --> 05:07:34,000 100 90 80 70 60 50 40 30 8976 05:07:32,480 --> 05:07:35,680 and then one student got a zero because 8977 05:07:34,000 --> 05:07:36,958 they didn't show up so what i would like 8978 05:07:35,680 --> 05:07:39,760 to do is to filter 8979 05:07:36,958 --> 05:07:40,718 all of these student grades into a list 8980 05:07:39,760 --> 05:07:42,878 that only contains 8981 05:07:40,718 --> 05:07:44,798 students that passed and the criteria 8982 05:07:42,878 --> 05:07:48,000 for a passing grade will be 8983 05:07:44,798 --> 05:07:50,560 a 60 or above so i would like to filter 8984 05:07:48,000 --> 05:07:52,080 all passing students and if i was to 8985 05:07:50,560 --> 05:07:53,280 write this program using the filter 8986 05:07:52,080 --> 05:07:54,400 function i would probably write 8987 05:07:53,280 --> 05:07:56,718 something like this 8988 05:07:54,400 --> 05:07:57,760 so i need a function and then my list 8989 05:07:56,718 --> 05:08:01,040 that i'm working with 8990 05:07:57,760 --> 05:08:02,320 lambda x colon x is greater than or 8991 05:08:01,040 --> 05:08:04,240 equal to 60 8992 05:08:02,320 --> 05:08:06,958 will filter all of the results and then 8993 05:08:04,240 --> 05:08:08,480 convert it to a list named past students 8994 05:08:06,958 --> 05:08:11,280 so the result will look something like 8995 05:08:08,480 --> 05:08:14,400 this i have five student grades in here 8996 05:08:11,280 --> 05:08:16,400 with 100 90 80 70 and 60. 8997 05:08:14,400 --> 05:08:17,520 now let's write the same program using a 8998 05:08:16,400 --> 05:08:19,280 list comprehension 8999 05:08:17,520 --> 05:08:20,958 now there's just one portion to this 9000 05:08:19,280 --> 05:08:21,840 formula for list comprehension that 9001 05:08:20,958 --> 05:08:23,920 we're going to add 9002 05:08:21,840 --> 05:08:25,440 at the end we're going to add if 9003 05:08:23,920 --> 05:08:27,120 conditional we can check 9004 05:08:25,440 --> 05:08:28,560 some sort of condition after each 9005 05:08:27,120 --> 05:08:30,320 iteration so 9006 05:08:28,560 --> 05:08:31,920 let's write the same program using a 9007 05:08:30,320 --> 05:08:34,560 list comprehension following 9008 05:08:31,920 --> 05:08:34,958 this new updated formula that we have so 9009 05:08:34,560 --> 05:08:38,718 we need 9010 05:08:34,958 --> 05:08:41,200 our list which will be named past 9011 05:08:38,718 --> 05:08:41,840 students equals first comes our 9012 05:08:41,200 --> 05:08:44,400 expression 9013 05:08:41,840 --> 05:08:45,040 and it's just i for this example for 9014 05:08:44,400 --> 05:08:48,080 item 9015 05:08:45,040 --> 05:08:51,680 i in our iterable 9016 05:08:48,080 --> 05:08:52,638 of students and then if we can check our 9017 05:08:51,680 --> 05:08:56,878 conditional 9018 05:08:52,638 --> 05:08:59,120 if i is greater than or equal to 60 9019 05:08:56,878 --> 05:09:01,360 and this will have the same effect as 9020 05:08:59,120 --> 05:09:03,120 our previous program but instead it uses 9021 05:09:01,360 --> 05:09:06,080 a list comprehension 9022 05:09:03,120 --> 05:09:07,440 now if your program has multiple output 9023 05:09:06,080 --> 05:09:08,400 if you need an else statement within 9024 05:09:07,440 --> 05:09:09,760 your conditional 9025 05:09:08,400 --> 05:09:12,000 you're actually going to move this 9026 05:09:09,760 --> 05:09:13,520 portion of the if conditional within 9027 05:09:12,000 --> 05:09:15,360 our formula to right after the 9028 05:09:13,520 --> 05:09:18,480 expression so let's write 9029 05:09:15,360 --> 05:09:20,320 an if else statement here 9030 05:09:18,480 --> 05:09:22,638 so let's say that instead of just 9031 05:09:20,320 --> 05:09:24,718 excluding any student that didn't pass 9032 05:09:22,638 --> 05:09:26,000 we'll replace their grade with the word 9033 05:09:24,718 --> 05:09:28,320 failed and we can do that 9034 05:09:26,000 --> 05:09:30,000 using an if else statement so let's do 9035 05:09:28,320 --> 05:09:31,440 this again i'm going to copy what we 9036 05:09:30,000 --> 05:09:33,600 have here 9037 05:09:31,440 --> 05:09:35,600 paste it and we'll move our conditional 9038 05:09:33,600 --> 05:09:39,200 from the end 9039 05:09:35,600 --> 05:09:41,760 to just after the expression portion i 9040 05:09:39,200 --> 05:09:43,680 if i is greater than or equal to 60 and 9041 05:09:41,760 --> 05:09:46,878 you can see here that we need to add an 9042 05:09:43,680 --> 05:09:49,840 else statement else will return 9043 05:09:46,878 --> 05:09:49,840 the word failed 9044 05:09:51,280 --> 05:09:56,400 so we're replacing each instance of 9045 05:09:54,480 --> 05:09:58,240 a failing grade with the word failed 9046 05:09:56,400 --> 05:10:00,160 instead so if your condition 9047 05:09:58,240 --> 05:10:01,280 returns whatever i is you can just add 9048 05:10:00,160 --> 05:10:02,878 that to the end 9049 05:10:01,280 --> 05:10:04,320 if you need an if else statement you're 9050 05:10:02,878 --> 05:10:05,520 going to add it right after the 9051 05:10:04,320 --> 05:10:07,680 expression 9052 05:10:05,520 --> 05:10:10,000 so everybody in conclusion a list 9053 05:10:07,680 --> 05:10:10,400 comprehension is a way to create a new 9054 05:10:10,000 --> 05:10:13,040 list 9055 05:10:10,400 --> 05:10:14,878 with less syntax you can even mimic 9056 05:10:13,040 --> 05:10:15,520 certain lambda functions which we did 9057 05:10:14,878 --> 05:10:17,360 with the 9058 05:10:15,520 --> 05:10:19,840 filter function and it has the added 9059 05:10:17,360 --> 05:10:21,040 benefit of being easier to read than a 9060 05:10:19,840 --> 05:10:22,798 lambda function 9061 05:10:21,040 --> 05:10:24,480 so you just follow one of these formulas 9062 05:10:22,798 --> 05:10:27,120 depending on what you're trying to do 9063 05:10:24,480 --> 05:10:28,080 so you need at least list equals your 9064 05:10:27,120 --> 05:10:30,560 expression 9065 05:10:28,080 --> 05:10:31,760 for item and your interval if you have a 9066 05:10:30,560 --> 05:10:33,120 condition that you want to check 9067 05:10:31,760 --> 05:10:35,280 you just add that to the end of your 9068 05:10:33,120 --> 05:10:36,240 list comprehension if you have an if 9069 05:10:35,280 --> 05:10:38,080 else statement 9070 05:10:36,240 --> 05:10:40,160 then you'll add that to right after the 9071 05:10:38,080 --> 05:10:42,080 expression so you just follow one of 9072 05:10:40,160 --> 05:10:43,520 these formulas depending on how you want 9073 05:10:42,080 --> 05:10:45,520 to write your program 9074 05:10:43,520 --> 05:10:47,280 so that is a list comprehension if you 9075 05:10:45,520 --> 05:10:48,958 would like a copy of this code i will 9076 05:10:47,280 --> 05:10:50,480 post all of this to the comments section 9077 05:10:48,958 --> 05:10:52,480 down below and well 9078 05:10:50,480 --> 05:10:54,240 yeah that's how list comprehensions work 9079 05:10:52,480 --> 05:10:56,718 in python 9080 05:10:54,240 --> 05:10:57,760 yo what's going on everybody it's bro 9081 05:10:56,718 --> 05:10:59,520 hope you're doing well 9082 05:10:57,760 --> 05:11:01,840 and in this video i'm going to explain 9083 05:10:59,520 --> 05:11:06,400 dictionary comprehensions in python so 9084 05:11:01,840 --> 05:11:06,400 sit back relax and enjoy the show 9085 05:11:06,718 --> 05:11:10,560 okay people dictionary comprehensions 9086 05:11:09,280 --> 05:11:11,840 they're very similar to list 9087 05:11:10,560 --> 05:11:13,360 comprehensions except 9088 05:11:11,840 --> 05:11:15,280 they're with dictionaries that's it 9089 05:11:13,360 --> 05:11:17,200 video over okay but seriously a 9090 05:11:15,280 --> 05:11:19,360 dictionary comprehension is a way to 9091 05:11:17,200 --> 05:11:21,120 create dictionaries using an expression 9092 05:11:19,360 --> 05:11:22,560 and they can replace for loops and 9093 05:11:21,120 --> 05:11:24,240 certain lambda functions 9094 05:11:22,560 --> 05:11:26,080 and all we have to do is follow this 9095 05:11:24,240 --> 05:11:29,600 formula dictionary equals 9096 05:11:26,080 --> 05:11:31,600 key colon our expression for key value 9097 05:11:29,600 --> 05:11:33,600 in iterable so let's go over a few 9098 05:11:31,600 --> 05:11:34,080 examples our first example we're going 9099 05:11:33,600 --> 05:11:37,040 to take 9100 05:11:34,080 --> 05:11:38,320 this dictionary named cities in f f is 9101 05:11:37,040 --> 05:11:41,200 short for fahrenheit 9102 05:11:38,320 --> 05:11:42,878 i have different city names as keys and 9103 05:11:41,200 --> 05:11:46,400 relative temperatures as 9104 05:11:42,878 --> 05:11:48,958 values in fahrenheit so new york is 32 9105 05:11:46,400 --> 05:11:51,920 degrees fahrenheit boston is 75. 9106 05:11:48,958 --> 05:11:53,680 los angeles is 100 and chicago is 50. 9107 05:11:51,920 --> 05:11:55,680 and what we'll be doing is creating a 9108 05:11:53,680 --> 05:11:57,680 separate dictionary where all of these 9109 05:11:55,680 --> 05:12:00,160 temperatures will be in celsius 9110 05:11:57,680 --> 05:12:02,160 using a dictionary comprehension so 9111 05:12:00,160 --> 05:12:03,600 let's follow this formula let's create a 9112 05:12:02,160 --> 05:12:07,680 new name for this dictionary 9113 05:12:03,600 --> 05:12:10,160 let's say cities in c short for celsius 9114 05:12:07,680 --> 05:12:11,520 equals and we will follow this pattern 9115 05:12:10,160 --> 05:12:13,360 first our key 9116 05:12:11,520 --> 05:12:15,040 golden then our expression we'll go back 9117 05:12:13,360 --> 05:12:18,160 to this in just a moment 9118 05:12:15,040 --> 05:12:21,520 for key comma 9119 05:12:18,160 --> 05:12:25,280 value in our iterable 9120 05:12:21,520 --> 05:12:27,120 of cities in f and since we're working 9121 05:12:25,280 --> 05:12:29,520 with a dictionary we're going to use 9122 05:12:27,120 --> 05:12:31,120 the items method now we just need to 9123 05:12:29,520 --> 05:12:33,360 fill in this expression 9124 05:12:31,120 --> 05:12:34,878 there is a formula to convert fahrenheit 9125 05:12:33,360 --> 05:12:36,240 to celsius and it should be on the 9126 05:12:34,878 --> 05:12:38,480 screen right about now 9127 05:12:36,240 --> 05:12:40,160 so let's follow this formula so we'll 9128 05:12:38,480 --> 05:12:43,200 take our value 9129 05:12:40,160 --> 05:12:45,680 minus 32 and just for good measure i'm 9130 05:12:43,200 --> 05:12:49,440 going to put these within parentheses 9131 05:12:45,680 --> 05:12:52,958 and we will multiply all of this by 9132 05:12:49,440 --> 05:12:54,320 5 divided by 9 and that is it so we have 9133 05:12:52,958 --> 05:12:56,878 our key 9134 05:12:54,320 --> 05:12:58,160 our expression to convert fahrenheit to 9135 05:12:56,878 --> 05:13:01,600 celsius 9136 05:12:58,160 --> 05:13:04,798 for key value in our iterable of 9137 05:13:01,600 --> 05:13:07,600 cities in f our previous dictionary and 9138 05:13:04,798 --> 05:13:08,480 let's test this by printing our new 9139 05:13:07,600 --> 05:13:12,638 dictionary 9140 05:13:08,480 --> 05:13:12,638 cities in c 9141 05:13:12,878 --> 05:13:16,160 actually i think i'm going to round 9142 05:13:14,080 --> 05:13:19,920 these numbers just so it's more readable 9143 05:13:16,160 --> 05:13:21,840 so let's round all this there we go so 9144 05:13:19,920 --> 05:13:24,878 new york and celsius is zero 9145 05:13:21,840 --> 05:13:26,320 boston is 24. los angeles is 38 and 9146 05:13:24,878 --> 05:13:29,120 chicago is 10. 9147 05:13:26,320 --> 05:13:30,798 so we created a new dictionary using a 9148 05:13:29,120 --> 05:13:32,718 dictionary comprehension 9149 05:13:30,798 --> 05:13:34,798 now with these dictionary comprehensions 9150 05:13:32,718 --> 05:13:35,280 you can add an if conditional to the end 9151 05:13:34,798 --> 05:13:37,040 of this 9152 05:13:35,280 --> 05:13:38,638 so let's say that we have a separate 9153 05:13:37,040 --> 05:13:40,160 dictionary of weather 9154 05:13:38,638 --> 05:13:42,240 like a description of the weather in 9155 05:13:40,160 --> 05:13:44,718 each city new york is snowing 9156 05:13:42,240 --> 05:13:46,160 boston is sunny los angeles is sunny and 9157 05:13:44,718 --> 05:13:47,920 chicago is cloudy 9158 05:13:46,160 --> 05:13:50,160 let's say that we would like to create a 9159 05:13:47,920 --> 05:13:51,440 separate dictionary with a dictionary 9160 05:13:50,160 --> 05:13:53,920 comprehension 9161 05:13:51,440 --> 05:13:54,798 that only has cities where the weather 9162 05:13:53,920 --> 05:13:57,120 is sunny 9163 05:13:54,798 --> 05:13:58,160 so i would write something like this 9164 05:13:57,120 --> 05:13:59,360 let's say sunny 9165 05:13:58,160 --> 05:14:02,000 weather will be the name of our 9166 05:13:59,360 --> 05:14:04,160 dictionary equals and we'll follow 9167 05:14:02,000 --> 05:14:06,240 this formula and then we just tack on 9168 05:14:04,160 --> 05:14:08,000 our conditional to the end so it's kind 9169 05:14:06,240 --> 05:14:11,760 of the same as before really 9170 05:14:08,000 --> 05:14:14,240 so key colon and we don't really have an 9171 05:14:11,760 --> 05:14:18,958 expression here so let's just say 9172 05:14:14,240 --> 05:14:18,958 value then for 9173 05:14:19,280 --> 05:14:25,440 key comma value 9174 05:14:22,400 --> 05:14:28,878 in our iterable of 9175 05:14:25,440 --> 05:14:33,840 weather dot items 9176 05:14:28,878 --> 05:14:35,840 then our conditional if value 9177 05:14:33,840 --> 05:14:37,600 sunny because we are only looking for 9178 05:14:35,840 --> 05:14:40,638 sunny weather 9179 05:14:37,600 --> 05:14:44,080 and let's print this print 9180 05:14:40,638 --> 05:14:45,840 our dictionary of sunny weather 9181 05:14:44,080 --> 05:14:47,840 and let me just make this more readable 9182 05:14:45,840 --> 05:14:48,718 for you guys and our new dictionary 9183 05:14:47,840 --> 05:14:51,520 comprehension 9184 05:14:48,718 --> 05:14:52,480 will create a dictionary of key values 9185 05:14:51,520 --> 05:14:55,360 where the value 9186 05:14:52,480 --> 05:14:57,280 is sunny using an if conditional at the 9187 05:14:55,360 --> 05:14:59,120 end of our dictionary comprehension 9188 05:14:57,280 --> 05:15:00,958 here's a third example for you well if 9189 05:14:59,120 --> 05:15:02,638 you have an if else condition you can 9190 05:15:00,958 --> 05:15:04,878 add that to where the expression is 9191 05:15:02,638 --> 05:15:06,878 within your dictionary comprehension 9192 05:15:04,878 --> 05:15:08,798 so i'm going to reuse the previous 9193 05:15:06,878 --> 05:15:09,280 dictionary for the first example where 9194 05:15:08,798 --> 05:15:11,920 we have 9195 05:15:09,280 --> 05:15:13,760 cities and their temperatures as values 9196 05:15:11,920 --> 05:15:15,440 so we're going to replace each 9197 05:15:13,760 --> 05:15:16,160 temperature with a description of the 9198 05:15:15,440 --> 05:15:18,638 weather 9199 05:15:16,160 --> 05:15:19,280 is it warm or is it cold so let's do 9200 05:15:18,638 --> 05:15:21,280 that using 9201 05:15:19,280 --> 05:15:22,958 a dictionary comprehension that contains 9202 05:15:21,280 --> 05:15:24,718 an if else statement 9203 05:15:22,958 --> 05:15:27,280 so let's say we have a new dictionary 9204 05:15:24,718 --> 05:15:29,920 named desk short for description 9205 05:15:27,280 --> 05:15:31,120 cities it's a brief description of each 9206 05:15:29,920 --> 05:15:34,878 city's temperature 9207 05:15:31,120 --> 05:15:37,520 so we follow this formula key 9208 05:15:34,878 --> 05:15:38,160 then our if else conditional for key 9209 05:15:37,520 --> 05:15:40,718 value 9210 05:15:38,160 --> 05:15:41,920 and iterable actually i probably can 9211 05:15:40,718 --> 05:15:44,798 copy all this 9212 05:15:41,920 --> 05:15:45,840 just to save some time okay so our 9213 05:15:44,798 --> 05:15:49,520 iterable 9214 05:15:45,840 --> 05:15:53,120 will be cities dot items 9215 05:15:49,520 --> 05:15:56,560 and our conditional is going to be 9216 05:15:53,120 --> 05:16:00,000 we will return warm if 9217 05:15:56,560 --> 05:16:04,080 our value is greater than or equal to 9218 05:16:00,000 --> 05:16:07,600 let's say 40. else 9219 05:16:04,080 --> 05:16:11,040 cold and we will print 9220 05:16:07,600 --> 05:16:14,240 our new dictionary of desk cities 9221 05:16:11,040 --> 05:16:17,360 description of cities so new york is 9222 05:16:14,240 --> 05:16:19,760 cold because it's 32 that's below 40. 9223 05:16:17,360 --> 05:16:21,200 boston is warm los angeles is warm and 9224 05:16:19,760 --> 05:16:22,878 chicago is warm 9225 05:16:21,200 --> 05:16:25,040 now if your condition gets somewhat 9226 05:16:22,878 --> 05:16:26,320 complex you can even call a separate 9227 05:16:25,040 --> 05:16:27,120 function to keep your code more 9228 05:16:26,320 --> 05:16:29,120 organized 9229 05:16:27,120 --> 05:16:30,320 so key then we can call a function and 9230 05:16:29,120 --> 05:16:32,958 pass in a value 9231 05:16:30,320 --> 05:16:33,760 for key value in iterable so i'm going 9232 05:16:32,958 --> 05:16:35,840 to again 9233 05:16:33,760 --> 05:16:36,798 reuse our city names and our 9234 05:16:35,840 --> 05:16:39,040 temperatures 9235 05:16:36,798 --> 05:16:40,320 and this time we are going to call a 9236 05:16:39,040 --> 05:16:41,920 function instead 9237 05:16:40,320 --> 05:16:43,520 i think i'll reuse this code from the 9238 05:16:41,920 --> 05:16:44,878 previous example where we have a 9239 05:16:43,520 --> 05:16:47,360 separate dictionary named 9240 05:16:44,878 --> 05:16:48,878 desk cities and i'm going to replace 9241 05:16:47,360 --> 05:16:51,120 this if else statement 9242 05:16:48,878 --> 05:16:52,718 with a function so let's say we have a 9243 05:16:51,120 --> 05:16:56,320 function that is named 9244 05:16:52,718 --> 05:16:58,400 check temp and we will pass in our value 9245 05:16:56,320 --> 05:17:00,160 and we just need to define this function 9246 05:16:58,400 --> 05:17:03,520 so let's define that here 9247 05:17:00,160 --> 05:17:06,798 so let's define check 9248 05:17:03,520 --> 05:17:08,480 temp and our parameter is our value that 9249 05:17:06,798 --> 05:17:09,440 we're currently working on within our 9250 05:17:08,480 --> 05:17:12,878 dictionary 9251 05:17:09,440 --> 05:17:16,718 value then we'll write if 9252 05:17:12,878 --> 05:17:19,840 value is greater than or equal to 9253 05:17:16,718 --> 05:17:24,480 let's say anything above how about 70 9254 05:17:19,840 --> 05:17:27,520 will return the word hot 9255 05:17:24,480 --> 05:17:32,080 then else if 9256 05:17:27,520 --> 05:17:35,200 let's say 69 greater than or equal to 9257 05:17:32,080 --> 05:17:39,600 value and then 9258 05:17:35,200 --> 05:17:43,040 value is greater than or equal to 9259 05:17:39,600 --> 05:17:46,878 how about 40 then we will 9260 05:17:43,040 --> 05:17:51,680 return the word warm 9261 05:17:46,878 --> 05:17:53,840 and lastly else return 9262 05:17:51,680 --> 05:17:56,400 cold and let me just fix some of the 9263 05:17:53,840 --> 05:17:56,400 spacing 9264 05:17:56,958 --> 05:18:00,878 now depending on the temperature that is 9265 05:17:58,958 --> 05:18:01,680 stored within each value of our 9266 05:18:00,878 --> 05:18:04,400 dictionary 9267 05:18:01,680 --> 05:18:06,320 we will call a function that will return 9268 05:18:04,400 --> 05:18:08,798 one of a few different outputs 9269 05:18:06,320 --> 05:18:11,040 so it's kind of like we're using the map 9270 05:18:08,798 --> 05:18:14,080 function the map lambda function 9271 05:18:11,040 --> 05:18:16,878 so let's print our description 9272 05:18:14,080 --> 05:18:18,798 of cities and we should get a short 9273 05:18:16,878 --> 05:18:19,360 description of the temperature in each 9274 05:18:18,798 --> 05:18:21,760 city 9275 05:18:19,360 --> 05:18:23,600 after we call the check temperature 9276 05:18:21,760 --> 05:18:24,878 function and then pass in each value 9277 05:18:23,600 --> 05:18:27,040 from our dictionary 9278 05:18:24,878 --> 05:18:28,160 so new york is cold because it's 32 9279 05:18:27,040 --> 05:18:31,200 degrees fahrenheit 9280 05:18:28,160 --> 05:18:32,000 boston is hot because it's above 70. los 9281 05:18:31,200 --> 05:18:34,480 angeles is hot 9282 05:18:32,000 --> 05:18:36,958 because it's 100 and then chicago is 9283 05:18:34,480 --> 05:18:40,000 warm because that is 50 which is between 9284 05:18:36,958 --> 05:18:42,480 69 and 40 within our function 9285 05:18:40,000 --> 05:18:44,240 so that is a dictionary comprehension 9286 05:18:42,480 --> 05:18:46,320 it's a shortcut where you can create 9287 05:18:44,240 --> 05:18:48,160 dictionaries using an expression 9288 05:18:46,320 --> 05:18:49,840 and they can replace for loops and 9289 05:18:48,160 --> 05:18:51,360 certain lambda functions 9290 05:18:49,840 --> 05:18:53,040 so if you would like a copy of all this 9291 05:18:51,360 --> 05:18:54,560 code i will post all of this to the 9292 05:18:53,040 --> 05:18:56,480 comment section down below 9293 05:18:54,560 --> 05:18:57,840 and well yeah that's how dictionary 9294 05:18:56,480 --> 05:19:00,958 comprehensions work 9295 05:18:57,840 --> 05:19:02,798 in python hey what's going on everybody 9296 05:19:00,958 --> 05:19:04,320 it's bro hope you're doing well and in 9297 05:19:02,798 --> 05:19:06,718 today's video i'm going to show you how 9298 05:19:04,320 --> 05:19:11,520 the zip function works in python so 9299 05:19:06,718 --> 05:19:11,520 sit back relax and enjoy the show 9300 05:19:11,840 --> 05:19:14,958 all right ladies and gentlemen welcome 9301 05:19:13,600 --> 05:19:16,160 back we're talking about the zip 9302 05:19:14,958 --> 05:19:18,400 function today 9303 05:19:16,160 --> 05:19:20,480 the zip function will aggregate elements 9304 05:19:18,400 --> 05:19:23,120 from two or more iterables 9305 05:19:20,480 --> 05:19:23,760 iterables are those things like lists 9306 05:19:23,120 --> 05:19:26,240 tuples 9307 05:19:23,760 --> 05:19:26,878 sets etc and the zip function will 9308 05:19:26,240 --> 05:19:29,760 create a 9309 05:19:26,878 --> 05:19:30,798 zip object with paired elements from 9310 05:19:29,760 --> 05:19:33,120 each iterable 9311 05:19:30,798 --> 05:19:34,638 stored in a tuple for each element 9312 05:19:33,120 --> 05:19:37,040 within our zip object 9313 05:19:34,638 --> 05:19:38,878 here's an example let's say that we have 9314 05:19:37,040 --> 05:19:41,600 two different types of iterables 9315 05:19:38,878 --> 05:19:43,120 i have a list of usernames and a few 9316 05:19:41,600 --> 05:19:46,638 usernames within here are 9317 05:19:43,120 --> 05:19:48,878 dude bro and mr and i have a 9318 05:19:46,638 --> 05:19:49,920 tuple of passwords and i have some very 9319 05:19:48,878 --> 05:19:53,280 secure passwords 9320 05:19:49,920 --> 05:19:55,840 such as password abc123 9321 05:19:53,280 --> 05:19:58,400 and guest what i would like to do is to 9322 05:19:55,840 --> 05:20:00,638 zip elements from each iterable together 9323 05:19:58,400 --> 05:20:03,040 so that they're in pairs and each pair 9324 05:20:00,638 --> 05:20:05,280 is going to be stored as a tuple within 9325 05:20:03,040 --> 05:20:07,760 a zip object and here's how we can do 9326 05:20:05,280 --> 05:20:09,200 that let's say that we will create a zip 9327 05:20:07,760 --> 05:20:11,920 object called users 9328 05:20:09,200 --> 05:20:13,120 and we will use the zip function the zip 9329 05:20:11,920 --> 05:20:14,878 function will take a 9330 05:20:13,120 --> 05:20:16,638 varying amount of iterables we're going 9331 05:20:14,878 --> 05:20:18,878 to pass in our usernames 9332 05:20:16,638 --> 05:20:20,560 and passwords and zip them together so 9333 05:20:18,878 --> 05:20:23,200 let's pass in usernames 9334 05:20:20,560 --> 05:20:24,558 as well as passwords and now our zip 9335 05:20:23,200 --> 05:20:26,558 object of users 9336 05:20:24,558 --> 05:20:28,320 is actually iterable zip objects are 9337 05:20:26,558 --> 05:20:28,798 iterable so we can use them within a for 9338 05:20:28,320 --> 05:20:32,480 loop 9339 05:20:28,798 --> 05:20:36,480 so let's type for i in users 9340 05:20:32,480 --> 05:20:36,480 and print i 9341 05:20:36,638 --> 05:20:39,840 and what we get is that we have a zip 9342 05:20:39,120 --> 05:20:42,718 object 9343 05:20:39,840 --> 05:20:44,400 of tuples and each tuple is storing each 9344 05:20:42,718 --> 05:20:47,600 pair of elements from our 9345 05:20:44,400 --> 05:20:49,360 two iterables now users is a 9346 05:20:47,600 --> 05:20:51,520 zip object and if you don't believe me 9347 05:20:49,360 --> 05:20:54,958 let me prove it i'm going to print 9348 05:20:51,520 --> 05:20:58,000 the type of users and this will print 9349 05:20:54,958 --> 05:21:00,320 that users is indeed a zip object 9350 05:20:58,000 --> 05:21:02,400 but you can easily convert this to a 9351 05:21:00,320 --> 05:21:03,120 different type of iterable by using a 9352 05:21:02,400 --> 05:21:04,878 cast 9353 05:21:03,120 --> 05:21:07,120 let's say that we would like to convert 9354 05:21:04,878 --> 05:21:09,120 our zip object into a list 9355 05:21:07,120 --> 05:21:10,958 so we'll surround the zip function with 9356 05:21:09,120 --> 05:21:13,520 a cast to a list 9357 05:21:10,958 --> 05:21:14,080 and now the data type of users is now a 9358 05:21:13,520 --> 05:21:16,798 list 9359 05:21:14,080 --> 05:21:18,400 what we have is a list of tuples and 9360 05:21:16,798 --> 05:21:21,120 each tuple is storing a 9361 05:21:18,400 --> 05:21:22,958 pair of elements from our two iterables 9362 05:21:21,120 --> 05:21:24,718 of usernames and passwords 9363 05:21:22,958 --> 05:21:26,878 now currently since we're passing in 9364 05:21:24,718 --> 05:21:27,840 only two different iterables we can 9365 05:21:26,878 --> 05:21:30,878 easily make this 9366 05:21:27,840 --> 05:21:31,520 a dictionary so that these are key value 9367 05:21:30,878 --> 05:21:33,840 pairs 9368 05:21:31,520 --> 05:21:34,718 so let's cast our zip object as a 9369 05:21:33,840 --> 05:21:36,400 dictionary 9370 05:21:34,718 --> 05:21:38,240 and to display all of the elements 9371 05:21:36,400 --> 05:21:38,798 within our dictionary all the key value 9372 05:21:38,240 --> 05:21:40,558 pairs 9373 05:21:38,798 --> 05:21:43,040 we're going to change our for loop to 9374 05:21:40,558 --> 05:21:46,878 this for key value 9375 05:21:43,040 --> 05:21:50,558 in users dot and we will use the 9376 05:21:46,878 --> 05:21:53,280 items method print key 9377 05:21:50,558 --> 05:21:54,798 comma value actually i think i'm going 9378 05:21:53,280 --> 05:21:58,320 to separate each of these with 9379 05:21:54,798 --> 05:22:00,638 a colon just to make it more readable 9380 05:21:58,320 --> 05:22:01,600 and now when we zip these two intervals 9381 05:22:00,638 --> 05:22:04,878 together 9382 05:22:01,600 --> 05:22:06,798 we end up with a dictionary of usernames 9383 05:22:04,878 --> 05:22:08,958 and passwords and the name of this 9384 05:22:06,798 --> 05:22:11,040 dictionary is users 9385 05:22:08,958 --> 05:22:12,240 now you're not limited to just two 9386 05:22:11,040 --> 05:22:14,718 iterables you can add 9387 05:22:12,240 --> 05:22:16,320 a third iterable or more so this time 9388 05:22:14,718 --> 05:22:19,280 let's create a 9389 05:22:16,320 --> 05:22:20,240 maybe a list a list of last login dates 9390 05:22:19,280 --> 05:22:23,760 and i'll just call this 9391 05:22:20,240 --> 05:22:24,798 login date equals and why not make a 9392 05:22:23,760 --> 05:22:27,040 list 9393 05:22:24,798 --> 05:22:27,840 and let's make up some dates let's say 1 9394 05:22:27,040 --> 05:22:31,840 1 9395 05:22:27,840 --> 05:22:31,840 dash 20 21 9396 05:22:32,600 --> 05:22:38,958 12-20-21 9397 05:22:34,320 --> 05:22:42,958 and 1-3-20 9398 05:22:38,958 --> 05:22:46,718 21 okay so let's create 9399 05:22:42,958 --> 05:22:48,000 a zip object of users and we're going to 9400 05:22:46,718 --> 05:22:52,160 zip 9401 05:22:48,000 --> 05:22:56,638 user names passwords 9402 05:22:52,160 --> 05:22:56,638 and login date 9403 05:22:56,958 --> 05:23:03,920 and let's iterate over this for i 9404 05:23:00,320 --> 05:23:08,638 in users print 9405 05:23:03,920 --> 05:23:10,160 i now we have a tuple for each element 9406 05:23:08,638 --> 05:23:13,280 and instead of a pair we now 9407 05:23:10,160 --> 05:23:14,080 have a trio i guess of all of the 9408 05:23:13,280 --> 05:23:17,200 different elements 9409 05:23:14,080 --> 05:23:19,440 from each iterable so in conclusion 9410 05:23:17,200 --> 05:23:20,558 the zip function will aggregate elements 9411 05:23:19,440 --> 05:23:23,760 from two or more 9412 05:23:20,558 --> 05:23:25,920 iterables and create a zip object with 9413 05:23:23,760 --> 05:23:26,798 paired or grouped together elements 9414 05:23:25,920 --> 05:23:29,440 stored in a 9415 05:23:26,798 --> 05:23:30,480 tuple for each element within our zip 9416 05:23:29,440 --> 05:23:32,400 object 9417 05:23:30,480 --> 05:23:34,400 so that is the zip function if you would 9418 05:23:32,400 --> 05:23:36,480 like a copy of this code i will post 9419 05:23:34,400 --> 05:23:38,240 this to the comment section down below 9420 05:23:36,480 --> 05:23:41,280 and well yeah that's how the zip 9421 05:23:38,240 --> 05:23:43,120 function works in python 9422 05:23:41,280 --> 05:23:44,480 hey what's going on everybody it's you 9423 05:23:43,120 --> 05:23:46,480 bro hope you're doing well 9424 05:23:44,480 --> 05:23:48,000 and i'm going to explain the purpose of 9425 05:23:46,480 --> 05:23:51,040 if name equals main 9426 05:23:48,000 --> 05:23:54,400 in python so sit back relax 9427 05:23:51,040 --> 05:23:56,160 and enjoy the show 9428 05:23:54,400 --> 05:23:58,320 during your programming journey you may 9429 05:23:56,160 --> 05:23:58,878 have encountered this strange statement 9430 05:23:58,320 --> 05:24:01,200 of 9431 05:23:58,878 --> 05:24:02,320 if name equals main now what the heck 9432 05:24:01,200 --> 05:24:05,520 does that mean 9433 05:24:02,320 --> 05:24:08,000 so with python files also referred to as 9434 05:24:05,520 --> 05:24:10,320 modules by including this statement it 9435 05:24:08,000 --> 05:24:12,638 gives our modules some flexibility 9436 05:24:10,320 --> 05:24:15,040 one a module that has the statement can 9437 05:24:12,638 --> 05:24:18,160 be run as a standalone program 9438 05:24:15,040 --> 05:24:20,320 or two this module can be imported 9439 05:24:18,160 --> 05:24:22,160 and used by other modules if there's 9440 05:24:20,320 --> 05:24:24,638 some sort of useful functions 9441 05:24:22,160 --> 05:24:25,360 or other resources within this module to 9442 05:24:24,638 --> 05:24:27,920 be imported 9443 05:24:25,360 --> 05:24:30,080 by including the statement of if name 9444 05:24:27,920 --> 05:24:30,558 equals main we're checking to see if a 9445 05:24:30,080 --> 05:24:32,718 user 9446 05:24:30,558 --> 05:24:34,160 is running this module as either a 9447 05:24:32,718 --> 05:24:36,320 standalone program 9448 05:24:34,160 --> 05:24:38,000 or they're importing it from another 9449 05:24:36,320 --> 05:24:40,558 module behind the scenes 9450 05:24:38,000 --> 05:24:41,360 the python interpreter will set special 9451 05:24:40,558 --> 05:24:44,160 variables 9452 05:24:41,360 --> 05:24:45,440 one of which is double underscore name 9453 05:24:44,160 --> 05:24:48,400 it's a variable 9454 05:24:45,440 --> 05:24:49,280 and python will assign the name variable 9455 05:24:48,400 --> 05:24:52,320 a value 9456 05:24:49,280 --> 05:24:52,798 of main if it's the initial module being 9457 05:24:52,320 --> 05:24:54,480 run 9458 05:24:52,798 --> 05:24:56,320 and we can actually test that by 9459 05:24:54,480 --> 05:24:59,440 printing double 9460 05:24:56,320 --> 05:25:01,200 underscore name so since this is the 9461 05:24:59,440 --> 05:25:04,240 initial module being run 9462 05:25:01,200 --> 05:25:05,760 module one for my example name is going 9463 05:25:04,240 --> 05:25:09,760 to be assigned a value 9464 05:25:05,760 --> 05:25:12,080 a string of main now check this out i 9465 05:25:09,760 --> 05:25:14,798 have two modules what if i were to 9466 05:25:12,080 --> 05:25:16,718 import module 2 and check the name 9467 05:25:14,798 --> 05:25:20,878 variable of module 2. 9468 05:25:16,718 --> 05:25:23,120 let's try it import module 9469 05:25:20,878 --> 05:25:23,120 two 9470 05:25:23,920 --> 05:25:28,160 in order to access a variable from 9471 05:25:26,320 --> 05:25:29,200 another module i need to type the name 9472 05:25:28,160 --> 05:25:32,958 of the module 9473 05:25:29,200 --> 05:25:35,760 module two dot and i would like to check 9474 05:25:32,958 --> 05:25:37,600 the special name variable of module 2 9475 05:25:35,760 --> 05:25:40,240 and print it 9476 05:25:37,600 --> 05:25:41,840 so when you import a module this name 9477 05:25:40,240 --> 05:25:43,840 variable is going to be assigned 9478 05:25:41,840 --> 05:25:44,958 the name of the module in this case 9479 05:25:43,840 --> 05:25:46,718 module 2. 9480 05:25:44,958 --> 05:25:48,798 this time what if we change the roles 9481 05:25:46,718 --> 05:25:49,440 around i'm going to copy all of this 9482 05:25:48,798 --> 05:25:52,000 text 9483 05:25:49,440 --> 05:25:52,718 and paste it within module two we're 9484 05:25:52,000 --> 05:25:56,080 going to 9485 05:25:52,718 --> 05:25:59,280 import from module one we will 9486 05:25:56,080 --> 05:26:02,638 print that special name variable of 9487 05:25:59,280 --> 05:26:03,760 this module module 2 and then print the 9488 05:26:02,638 --> 05:26:06,798 name variable 9489 05:26:03,760 --> 05:26:08,638 of module 1 which is being imported now 9490 05:26:06,798 --> 05:26:09,360 when i run this program i'm going to 9491 05:26:08,638 --> 05:26:12,878 instead 9492 05:26:09,360 --> 05:26:15,920 run from module 2. 9493 05:26:12,878 --> 05:26:19,360 now this time that special name variable 9494 05:26:15,920 --> 05:26:21,360 of module 2 is main and the special name 9495 05:26:19,360 --> 05:26:24,638 variable of module 1 9496 05:26:21,360 --> 05:26:27,680 is module 1 the name of the module 9497 05:26:24,638 --> 05:26:30,480 so by including a statement such as 9498 05:26:27,680 --> 05:26:32,638 if name equals main we're checking to 9499 05:26:30,480 --> 05:26:36,240 see if this module is being run 9500 05:26:32,638 --> 05:26:39,040 directly or indirectly so let's test it 9501 05:26:36,240 --> 05:26:39,600 within this statement of if name equals 9502 05:26:39,040 --> 05:26:43,200 main 9503 05:26:39,600 --> 05:26:47,120 let's print running this 9504 05:26:43,200 --> 05:26:49,440 module directly 9505 05:26:47,120 --> 05:26:50,320 else that must mean that we're running 9506 05:26:49,440 --> 05:26:54,240 this module 9507 05:26:50,320 --> 05:26:54,240 indirectly so let's print 9508 05:26:54,840 --> 05:27:00,480 running other 9509 05:26:57,120 --> 05:27:03,600 module indirectly 9510 05:27:00,480 --> 05:27:07,040 okay so i'm going to go to module 1 9511 05:27:03,600 --> 05:27:08,400 and run from here running this module 9512 05:27:07,040 --> 05:27:10,480 directly 9513 05:27:08,400 --> 05:27:12,320 now let's do the same thing with module 9514 05:27:10,480 --> 05:27:16,320 two but we need to 9515 05:27:12,320 --> 05:27:24,320 import module 9516 05:27:16,320 --> 05:27:27,280 one and let's run for module two 9517 05:27:24,320 --> 05:27:28,958 running other module indirectly for the 9518 05:27:27,280 --> 05:27:29,600 time being i'm just going to write pass 9519 05:27:28,958 --> 05:27:32,000 within 9520 05:27:29,600 --> 05:27:33,680 our if name equals main as a placeholder 9521 05:27:32,000 --> 05:27:36,080 and get rid of our if statement 9522 05:27:33,680 --> 05:27:38,160 so let's say within module 1 there's a 9523 05:27:36,080 --> 05:27:39,120 useful function or resource we would 9524 05:27:38,160 --> 05:27:41,600 like to access 9525 05:27:39,120 --> 05:27:43,440 from module 2. so let's say we have a 9526 05:27:41,600 --> 05:27:46,000 function named hello 9527 05:27:43,440 --> 05:27:47,840 and all this will do is print the word 9528 05:27:46,000 --> 05:27:49,440 hello 9529 05:27:47,840 --> 05:27:50,958 let's just pretend that it's a useful 9530 05:27:49,440 --> 05:27:54,320 function not really but 9531 05:27:50,958 --> 05:27:56,638 let's pretend so in order to use this 9532 05:27:54,320 --> 05:27:58,798 function from module two i need to 9533 05:27:56,638 --> 05:28:02,080 import module one 9534 05:27:58,798 --> 05:28:04,160 then type module one dot and 9535 05:28:02,080 --> 05:28:05,120 then the name of the function so i'm 9536 05:28:04,160 --> 05:28:08,798 going to run 9537 05:28:05,120 --> 05:28:11,600 from module 2 and this will print 9538 05:28:08,798 --> 05:28:13,200 hello but what if i run this program 9539 05:28:11,600 --> 05:28:16,080 from module 1. 9540 05:28:13,200 --> 05:28:18,320 so right now module 1 cannot be run 9541 05:28:16,080 --> 05:28:21,120 directly as a standalone program 9542 05:28:18,320 --> 05:28:22,160 so i cannot print the word hello so what 9543 05:28:21,120 --> 05:28:25,600 i could do 9544 05:28:22,160 --> 05:28:28,638 is within if name equals main i can 9545 05:28:25,600 --> 05:28:31,920 call that hello function directly 9546 05:28:28,638 --> 05:28:34,320 so let's run from module one 9547 05:28:31,920 --> 05:28:35,360 hello one other thing that you might see 9548 05:28:34,320 --> 05:28:37,840 too within 9549 05:28:35,360 --> 05:28:39,440 the if statement if name equals main 9550 05:28:37,840 --> 05:28:41,440 there might be a call for 9551 05:28:39,440 --> 05:28:43,600 a main function and you can write the 9552 05:28:41,440 --> 05:28:46,240 main body of your program within 9553 05:28:43,600 --> 05:28:47,120 a main function so everybody in 9554 05:28:46,240 --> 05:28:49,200 conclusion 9555 05:28:47,120 --> 05:28:50,638 the reason that people may include this 9556 05:28:49,200 --> 05:28:52,558 statement if name 9557 05:28:50,638 --> 05:28:54,558 equals main is that it allows our 9558 05:28:52,558 --> 05:28:57,200 modules to have some flexibility 9559 05:28:54,558 --> 05:28:59,280 they can be run as a standalone program 9560 05:28:57,200 --> 05:29:00,480 or they can be imported and used by 9561 05:28:59,280 --> 05:29:02,240 other modules 9562 05:29:00,480 --> 05:29:03,520 and this is because the python 9563 05:29:02,240 --> 05:29:06,480 interpreter sets 9564 05:29:03,520 --> 05:29:07,280 that special variable of name with a 9565 05:29:06,480 --> 05:29:10,160 value 9566 05:29:07,280 --> 05:29:10,798 of main if it's the initial module being 9567 05:29:10,160 --> 05:29:12,958 run 9568 05:29:10,798 --> 05:29:14,878 if you would like a copy of my code and 9569 05:29:12,958 --> 05:29:16,320 my notes i will post all of this to the 9570 05:29:14,878 --> 05:29:19,040 comment section down below 9571 05:29:16,320 --> 05:29:19,920 but yeah that's the purpose of if name 9572 05:29:19,040 --> 05:29:23,200 equals main 9573 05:29:19,920 --> 05:29:25,280 in python hey what's going on everybody 9574 05:29:23,200 --> 05:29:26,798 it's bro hope you're doing well and in 9575 05:29:25,280 --> 05:29:28,638 this video i'm going to show you all a 9576 05:29:26,798 --> 05:29:31,120 few useful functions related to 9577 05:29:28,638 --> 05:29:32,080 times and dates using the time module in 9578 05:29:31,120 --> 05:29:36,798 python so 9579 05:29:32,080 --> 05:29:36,798 sit back relax and enjoy the show 9580 05:29:37,120 --> 05:29:41,440 alright everybody so the time module 9581 05:29:39,440 --> 05:29:42,798 let's begin by importing time 9582 05:29:41,440 --> 05:29:44,558 and the first thing that i'm going to 9583 05:29:42,798 --> 05:29:46,160 explain to you while is how we can find 9584 05:29:44,558 --> 05:29:49,200 our computer's epic 9585 05:29:46,160 --> 05:29:50,160 also pronounced as epoch so this is a 9586 05:29:49,200 --> 05:29:52,958 date and time 9587 05:29:50,160 --> 05:29:54,480 in which your computer thinks time began 9588 05:29:52,958 --> 05:29:58,080 think of it that way at least 9589 05:29:54,480 --> 05:29:59,920 so we use our epic as a reference point 9590 05:29:58,080 --> 05:30:01,600 so to find your computer's epic it's 9591 05:29:59,920 --> 05:30:02,958 going to vary based on your computer and 9592 05:30:01,600 --> 05:30:04,958 your operating system 9593 05:30:02,958 --> 05:30:06,320 so to find your computer's epic type 9594 05:30:04,958 --> 05:30:08,878 time dot 9595 05:30:06,320 --> 05:30:10,000 c time and as an argument we will pass 9596 05:30:08,878 --> 05:30:12,080 in zero 9597 05:30:10,000 --> 05:30:14,320 now what this method will do is that 9598 05:30:12,080 --> 05:30:16,958 this method will convert a time 9599 05:30:14,320 --> 05:30:18,160 expressed in seconds and convert it to a 9600 05:30:16,958 --> 05:30:20,878 readable string 9601 05:30:18,160 --> 05:30:21,440 so if i was to pass in 0 and print it 9602 05:30:20,878 --> 05:30:23,920 well then 9603 05:30:21,440 --> 05:30:25,200 this will display the date and time 9604 05:30:23,920 --> 05:30:28,718 which is my epic 9605 05:30:25,200 --> 05:30:31,878 our reference point so for me my epic is 9606 05:30:28,718 --> 05:30:34,958 wednesday december 31st 6 p.m 9607 05:30:31,878 --> 05:30:37,280 1969 so i've just added a note 9608 05:30:34,958 --> 05:30:39,680 that the c time method will convert a 9609 05:30:37,280 --> 05:30:42,638 time expressed in seconds 9610 05:30:39,680 --> 05:30:44,240 since epic and convert it to a readable 9611 05:30:42,638 --> 05:30:47,120 string a readable 9612 05:30:44,240 --> 05:30:47,920 date and time so for practice let's pass 9613 05:30:47,120 --> 05:30:50,000 in perhaps 9614 05:30:47,920 --> 05:30:51,520 1 million seconds and see what date and 9615 05:30:50,000 --> 05:30:54,798 time that we receive 9616 05:30:51,520 --> 05:30:55,760 so our c time method will return a date 9617 05:30:54,798 --> 05:30:59,200 and time 9618 05:30:55,760 --> 05:31:02,320 one million seconds past this epic here 9619 05:30:59,200 --> 05:31:02,798 so that would be for me monday january 9620 05:31:02,320 --> 05:31:06,798 12th 9621 05:31:02,798 --> 05:31:09,520 about 7 a.m 1970. our next method is 9622 05:31:06,798 --> 05:31:10,000 the time method of the time module let's 9623 05:31:09,520 --> 05:31:12,958 print 9624 05:31:10,000 --> 05:31:13,760 time dot time method and what this 9625 05:31:12,958 --> 05:31:16,240 method will do 9626 05:31:13,760 --> 05:31:16,958 is return the current seconds that have 9627 05:31:16,240 --> 05:31:19,360 passed 9628 05:31:16,958 --> 05:31:20,160 since our epic using our computer's 9629 05:31:19,360 --> 05:31:23,200 clock 9630 05:31:20,160 --> 05:31:23,760 so the number that i end up with is just 9631 05:31:23,200 --> 05:31:27,200 over 9632 05:31:23,760 --> 05:31:30,320 1.6 billion and this is in seconds 9633 05:31:27,200 --> 05:31:33,120 so for me 1.6 billion seconds 9634 05:31:30,320 --> 05:31:33,600 has passed since that date which is my 9635 05:31:33,120 --> 05:31:37,120 epic 9636 05:31:33,600 --> 05:31:39,760 i believe it was december 31st 1969 9637 05:31:37,120 --> 05:31:41,840 and every time that i rerun this program 9638 05:31:39,760 --> 05:31:44,638 you can see that the amount of seconds 9639 05:31:41,840 --> 05:31:45,040 that has passed is actually increasing 9640 05:31:44,638 --> 05:31:47,840 so 9641 05:31:45,040 --> 05:31:49,120 that is the time method it will return 9642 05:31:47,840 --> 05:31:51,280 the current seconds 9643 05:31:49,120 --> 05:31:52,798 since you're epic using your computer's 9644 05:31:51,280 --> 05:31:55,120 clock and you know what 9645 05:31:52,798 --> 05:31:57,520 just for fun let's change the date and 9646 05:31:55,120 --> 05:32:00,718 time under clock and see what happens 9647 05:31:57,520 --> 05:32:04,320 so let's change the year to 9648 05:32:00,718 --> 05:32:07,040 how about the year 2000 change 9649 05:32:04,320 --> 05:32:09,840 and close so let's see what number we 9650 05:32:07,040 --> 05:32:14,400 get with the time method now 9651 05:32:09,840 --> 05:32:16,160 so we get just under 1 billion 948 9652 05:32:14,400 --> 05:32:18,400 million seconds have passed 9653 05:32:16,160 --> 05:32:19,520 and now if you need to retrieve the 9654 05:32:18,400 --> 05:32:21,040 current date and time 9655 05:32:19,520 --> 05:32:23,520 well there's one of a few ways in which 9656 05:32:21,040 --> 05:32:24,798 you could retrieve that but one way is 9657 05:32:23,520 --> 05:32:27,120 that we can combine 9658 05:32:24,798 --> 05:32:29,200 both of these methods of the see time 9659 05:32:27,120 --> 05:32:30,080 method and the time method of the time 9660 05:32:29,200 --> 05:32:33,200 module 9661 05:32:30,080 --> 05:32:35,520 so let's print time dot c 9662 05:32:33,200 --> 05:32:36,558 time and we're going to pass in an 9663 05:32:35,520 --> 05:32:39,760 amount of seconds 9664 05:32:36,558 --> 05:32:40,240 into the c time method as an argument so 9665 05:32:39,760 --> 05:32:42,400 within 9666 05:32:40,240 --> 05:32:44,000 the c time method we're going to call 9667 05:32:42,400 --> 05:32:46,400 the time method 9668 05:32:44,000 --> 05:32:47,120 so the time method will return an amount 9669 05:32:46,400 --> 05:32:49,680 of seconds 9670 05:32:47,120 --> 05:32:50,400 since our epic and the c time method 9671 05:32:49,680 --> 05:32:52,400 will convert 9672 05:32:50,400 --> 05:32:53,680 that amount of seconds to a readable 9673 05:32:52,400 --> 05:32:57,120 date and time 9674 05:32:53,680 --> 05:33:00,558 so the current date and time is saturday 9675 05:32:57,120 --> 05:33:02,240 january 23rd about 3 p.m 20 21 9676 05:33:00,558 --> 05:33:03,680 now there is more than one way to get 9677 05:33:02,240 --> 05:33:06,718 the current date and time 9678 05:33:03,680 --> 05:33:08,958 another way is to use the local time 9679 05:33:06,718 --> 05:33:09,840 method and the local time method will 9680 05:33:08,958 --> 05:33:12,878 create a 9681 05:33:09,840 --> 05:33:14,000 time object based on the current time so 9682 05:33:12,878 --> 05:33:17,440 what i'm going to do is 9683 05:33:14,000 --> 05:33:19,360 create a variable called time object 9684 05:33:17,440 --> 05:33:20,480 and i just need to explain a few things 9685 05:33:19,360 --> 05:33:22,558 about time objects 9686 05:33:20,480 --> 05:33:23,600 so to best explain this i'm going to 9687 05:33:22,558 --> 05:33:26,718 print our time 9688 05:33:23,600 --> 05:33:27,840 object now a time object is also 9689 05:33:26,718 --> 05:33:30,798 referred to as a 9690 05:33:27,840 --> 05:33:32,718 struct time object it is made up of 9691 05:33:30,798 --> 05:33:35,840 different keyword arguments 9692 05:33:32,718 --> 05:33:39,200 there's a year a month day 9693 05:33:35,840 --> 05:33:41,440 hour minutes seconds day of the week 9694 05:33:39,200 --> 05:33:42,958 day of the year and this keyword 9695 05:33:41,440 --> 05:33:44,400 argument here has something to do with 9696 05:33:42,958 --> 05:33:46,958 daylight savings time 9697 05:33:44,400 --> 05:33:47,840 so there's quite a few uses with time 9698 05:33:46,958 --> 05:33:50,160 objects 9699 05:33:47,840 --> 05:33:52,480 and one way is that we can format them 9700 05:33:50,160 --> 05:33:54,798 however we want because right now 9701 05:33:52,480 --> 05:33:55,520 this time object is not in a readable 9702 05:33:54,798 --> 05:33:58,320 format 9703 05:33:55,520 --> 05:34:00,400 so to convert this time object into a 9704 05:33:58,320 --> 05:34:01,680 readable string we'll need the help of a 9705 05:34:00,400 --> 05:34:05,600 separate function 9706 05:34:01,680 --> 05:34:08,798 and that is the strf time function 9707 05:34:05,600 --> 05:34:09,200 str is short for string f4 format and 9708 05:34:08,798 --> 05:34:12,480 time 9709 05:34:09,200 --> 05:34:15,600 well for time i guess so this function 9710 05:34:12,480 --> 05:34:18,958 needs two arguments a format and a time 9711 05:34:15,600 --> 05:34:19,520 object so our strf time function will 9712 05:34:18,958 --> 05:34:22,558 accept 9713 05:34:19,520 --> 05:34:23,440 a format and a time object as an 9714 05:34:22,558 --> 05:34:26,798 argument 9715 05:34:23,440 --> 05:34:28,878 so our format is really just a string 9716 05:34:26,798 --> 05:34:30,558 of different directives and to best 9717 05:34:28,878 --> 05:34:32,798 explain these i'm going to head to 9718 05:34:30,558 --> 05:34:33,520 python's official documentation on the 9719 05:34:32,798 --> 05:34:35,680 subject 9720 05:34:33,520 --> 05:34:38,080 so here i am on python's website 9721 05:34:35,680 --> 05:34:41,360 regarding the time module and underneath 9722 05:34:38,080 --> 05:34:43,360 this section on the strf time function 9723 05:34:41,360 --> 05:34:45,760 there are different directives that we 9724 05:34:43,360 --> 05:34:47,440 can embed within our format string that 9725 05:34:45,760 --> 05:34:49,840 we pass in as an argument 9726 05:34:47,440 --> 05:34:50,798 depending on the directive that we add 9727 05:34:49,840 --> 05:34:54,480 we can display 9728 05:34:50,798 --> 05:34:57,360 a certain format of our date and time 9729 05:34:54,480 --> 05:34:57,840 so for example if i was to pass in let's 9730 05:34:57,360 --> 05:35:01,200 say 9731 05:34:57,840 --> 05:35:04,000 percent lowercase a then we will display 9732 05:35:01,200 --> 05:35:05,920 the time object's weekday name and 9733 05:35:04,000 --> 05:35:07,520 you're not limited to just one directive 9734 05:35:05,920 --> 05:35:08,558 you can add any combination of 9735 05:35:07,520 --> 05:35:11,040 directives 9736 05:35:08,558 --> 05:35:12,160 so if i was to add percent m well we 9737 05:35:11,040 --> 05:35:14,718 would display 9738 05:35:12,160 --> 05:35:15,200 the month of our date time object as a 9739 05:35:14,718 --> 05:35:17,360 number 9740 05:35:15,200 --> 05:35:18,958 1 through 12. so there's a bunch of 9741 05:35:17,360 --> 05:35:20,160 directives here and i'm going to be 9742 05:35:18,958 --> 05:35:22,638 using some of these 9743 05:35:20,160 --> 05:35:24,400 so within a string for the format 9744 05:35:22,638 --> 05:35:27,200 argument i'm going to pass in 9745 05:35:24,400 --> 05:35:28,400 percent capital b for the name of the 9746 05:35:27,200 --> 05:35:31,760 month 9747 05:35:28,400 --> 05:35:35,120 percent d for the day 9748 05:35:31,760 --> 05:35:38,000 percent y for the year 9749 05:35:35,120 --> 05:35:40,000 percent h for the hour and to format 9750 05:35:38,000 --> 05:35:43,600 this i'm going to add a colon 9751 05:35:40,000 --> 05:35:48,320 to separate hours and minutes percent 9752 05:35:43,600 --> 05:35:51,040 m for minutes colon percent s 9753 05:35:48,320 --> 05:35:51,520 for seconds and then we are going to 9754 05:35:51,040 --> 05:35:54,320 assign 9755 05:35:51,520 --> 05:35:55,360 all of this to a variable let's say 9756 05:35:54,320 --> 05:35:58,878 local 9757 05:35:55,360 --> 05:36:03,120 time and local time will be a string 9758 05:35:58,878 --> 05:36:06,558 so let's print our local time 9759 05:36:03,120 --> 05:36:10,160 and the current time is january 9760 05:36:06,558 --> 05:36:12,320 23rd 2021 about 3 p.m 9761 05:36:10,160 --> 05:36:13,760 oh and i almost forgot you can also get 9762 05:36:12,320 --> 05:36:16,480 the utc 9763 05:36:13,760 --> 05:36:18,240 time that is the coordinated universal 9764 05:36:16,480 --> 05:36:20,320 time if you know how that works 9765 05:36:18,240 --> 05:36:21,600 so if you need that you would just use 9766 05:36:20,320 --> 05:36:25,120 the gm 9767 05:36:21,600 --> 05:36:26,558 time method for the utc time coordinated 9768 05:36:25,120 --> 05:36:29,680 universal time 9769 05:36:26,558 --> 05:36:32,240 okay next up we have the strp 9770 05:36:29,680 --> 05:36:32,718 time function and this function will 9771 05:36:32,240 --> 05:36:36,080 parse 9772 05:36:32,718 --> 05:36:36,638 a string representation of a time and or 9773 05:36:36,080 --> 05:36:39,360 date 9774 05:36:36,638 --> 05:36:39,920 and return a time object so we need to 9775 05:36:39,360 --> 05:36:42,000 pass in 9776 05:36:39,920 --> 05:36:44,000 a string representing the date and or 9777 05:36:42,000 --> 05:36:47,120 time as well as a format 9778 05:36:44,000 --> 05:36:49,120 string so let's create a time string and 9779 05:36:47,120 --> 05:36:50,240 this variable is going to be a string 9780 05:36:49,120 --> 05:36:54,080 representation of 9781 05:36:50,240 --> 05:36:56,000 a date let's say 20th of april 2020 9782 05:36:54,080 --> 05:36:58,718 and what we can do is take this string 9783 05:36:56,000 --> 05:37:01,440 representation of a time and or date 9784 05:36:58,718 --> 05:37:02,958 and parse it to a time object so we're 9785 05:37:01,440 --> 05:37:05,760 going to pass in our time 9786 05:37:02,958 --> 05:37:06,480 string variable as well as a format 9787 05:37:05,760 --> 05:37:10,080 string 9788 05:37:06,480 --> 05:37:12,558 so let's say i would like to parse 9789 05:37:10,080 --> 05:37:13,520 the day so that would be percent d for 9790 05:37:12,558 --> 05:37:17,680 day 9791 05:37:13,520 --> 05:37:21,840 then percent b for name of the month 9792 05:37:17,680 --> 05:37:23,840 and then comma percent y for year 9793 05:37:21,840 --> 05:37:26,080 this function will create a time object 9794 05:37:23,840 --> 05:37:29,680 so let's assign that to a variable 9795 05:37:26,080 --> 05:37:31,200 time object equals time dot strp time 9796 05:37:29,680 --> 05:37:32,878 we're passing in our string 9797 05:37:31,200 --> 05:37:35,920 representation of a time 9798 05:37:32,878 --> 05:37:38,558 or date as well as a format string 9799 05:37:35,920 --> 05:37:40,080 and we can print our time object using a 9800 05:37:38,558 --> 05:37:41,920 print statement 9801 05:37:40,080 --> 05:37:43,840 however this is going to be in a form 9802 05:37:41,920 --> 05:37:45,440 that is somewhat difficult to read but 9803 05:37:43,840 --> 05:37:46,160 you can see at least we have a time 9804 05:37:45,440 --> 05:37:48,000 object 9805 05:37:46,160 --> 05:37:49,520 with all of these keywords filled in 9806 05:37:48,000 --> 05:37:52,160 with anything that we passed in 9807 05:37:49,520 --> 05:37:53,360 via these format directives that we have 9808 05:37:52,160 --> 05:37:56,718 now the next function 9809 05:37:53,360 --> 05:37:59,520 is the asc time function 9810 05:37:56,718 --> 05:38:01,680 and this function accepts a time object 9811 05:37:59,520 --> 05:38:04,160 or a tuple representation 9812 05:38:01,680 --> 05:38:04,798 of a relative time so this time let's 9813 05:38:04,160 --> 05:38:08,160 create a 9814 05:38:04,798 --> 05:38:09,680 time tuple and we're going to follow 9815 05:38:08,160 --> 05:38:12,798 this order we can pass in 9816 05:38:09,680 --> 05:38:14,480 up to nine values the first value is a 9817 05:38:12,798 --> 05:38:17,520 year so let's pass in perhaps 9818 05:38:14,480 --> 05:38:20,718 2020 a month let's say four 9819 05:38:17,520 --> 05:38:24,000 a day how about 20 four hours 9820 05:38:20,718 --> 05:38:26,320 let's say four minutes 20 and seconds 9821 05:38:24,000 --> 05:38:27,760 maybe zero let me just fix some of the 9822 05:38:26,320 --> 05:38:30,160 spacing here 9823 05:38:27,760 --> 05:38:31,120 okay you can also pass in a numbered day 9824 05:38:30,160 --> 05:38:33,600 of the week 9825 05:38:31,120 --> 05:38:35,040 um i'm just going to say zero i don't 9826 05:38:33,600 --> 05:38:38,160 think it's really that important 9827 05:38:35,040 --> 05:38:39,920 a day of the year zero and 9828 05:38:38,160 --> 05:38:41,360 negative one or zero for daylight 9829 05:38:39,920 --> 05:38:44,000 savings time so 9830 05:38:41,360 --> 05:38:44,718 we created a time tuple and we can pass 9831 05:38:44,000 --> 05:38:47,760 in a time 9832 05:38:44,718 --> 05:38:48,240 object or a tuple representation of a 9833 05:38:47,760 --> 05:38:51,120 time 9834 05:38:48,240 --> 05:38:52,320 following this formula so let's pass in 9835 05:38:51,120 --> 05:38:55,360 our time tuple 9836 05:38:52,320 --> 05:38:57,440 and this will create a time string a 9837 05:38:55,360 --> 05:38:58,400 string representation of the time that 9838 05:38:57,440 --> 05:39:02,480 we create 9839 05:38:58,400 --> 05:39:02,480 and let's print our time string 9840 05:39:02,798 --> 05:39:08,638 and we should have april 20th 9841 05:39:05,840 --> 05:39:11,760 about four in the morning the year 2020. 9842 05:39:08,638 --> 05:39:15,040 so that is asc time it will convert a 9843 05:39:11,760 --> 05:39:17,440 tuple representation of a time and date 9844 05:39:15,040 --> 05:39:18,160 or a time object and convert it to a 9845 05:39:17,440 --> 05:39:21,520 readable 9846 05:39:18,160 --> 05:39:24,878 string now another option is to use m 9847 05:39:21,520 --> 05:39:28,160 k time and mk time will take a 9848 05:39:24,878 --> 05:39:28,878 tuple representation of a time or a time 9849 05:39:28,160 --> 05:39:32,160 object 9850 05:39:28,878 --> 05:39:35,440 and convert it to seconds since epic 9851 05:39:32,160 --> 05:39:38,558 so april 20th of the year 2020 9852 05:39:35,440 --> 05:39:40,958 is about 1.5 billion seconds 9853 05:39:38,558 --> 05:39:44,558 since our epic date and for me that was 9854 05:39:40,958 --> 05:39:47,920 december 31st the year 1969 9855 05:39:44,558 --> 05:39:49,120 so that is a few useful functions of the 9856 05:39:47,920 --> 05:39:50,798 time module 9857 05:39:49,120 --> 05:39:52,320 if you would like a copy of this code i 9858 05:39:50,798 --> 05:39:53,520 will post all this to the comment 9859 05:39:52,320 --> 05:39:56,240 section down below 9860 05:39:53,520 --> 05:39:58,000 and well yeah that's the time module in 9861 05:39:56,240 --> 05:40:00,400 python 9862 05:39:58,000 --> 05:40:01,360 yo what's going on everybody it's bro 9863 05:40:00,400 --> 05:40:03,040 hope you're doing well 9864 05:40:01,360 --> 05:40:04,878 and in this video i'm going to explain 9865 05:40:03,040 --> 05:40:09,360 multi-threading in python so 9866 05:40:04,878 --> 05:40:09,360 sit back relax and enjoy the show 9867 05:40:09,440 --> 05:40:13,120 think of a thread as a flow of execution 9868 05:40:12,080 --> 05:40:15,600 like a river 9869 05:40:13,120 --> 05:40:17,280 and each thread can carry out its own 9870 05:40:15,600 --> 05:40:18,878 separate order of instructions 9871 05:40:17,280 --> 05:40:21,120 if we use this process of 9872 05:40:18,878 --> 05:40:21,600 multi-threading we can have our program 9873 05:40:21,120 --> 05:40:23,680 run 9874 05:40:21,600 --> 05:40:24,878 different parts of its program at 9875 05:40:23,680 --> 05:40:27,040 different times 9876 05:40:24,878 --> 05:40:29,520 they all run concurrently but not truly 9877 05:40:27,040 --> 05:40:31,840 in parallel that is a concept for later 9878 05:40:29,520 --> 05:40:34,480 referred to as multi-processing 9879 05:40:31,840 --> 05:40:36,558 so with reds they each take a turn 9880 05:40:34,480 --> 05:40:38,958 running to achieve concurrency 9881 05:40:36,558 --> 05:40:40,638 this is due to a notorious feature known 9882 05:40:38,958 --> 05:40:43,440 as the gil 9883 05:40:40,638 --> 05:40:45,440 the global interpreter lock only one 9884 05:40:43,440 --> 05:40:47,440 thread can be running at one time 9885 05:40:45,440 --> 05:40:48,798 but they can all take turns when one 9886 05:40:47,440 --> 05:40:50,958 thread is idle 9887 05:40:48,798 --> 05:40:53,040 so this allows one thread to hold 9888 05:40:50,958 --> 05:40:55,920 control of the python interpreter 9889 05:40:53,040 --> 05:40:57,760 at any one time so they run concurrently 9890 05:40:55,920 --> 05:40:59,000 but not truly in parallel 9891 05:40:57,760 --> 05:41:01,600 which is what we do with 9892 05:40:59,000 --> 05:41:02,958 multi-processing now programs and tasks 9893 05:41:01,600 --> 05:41:05,280 can be divided into 9894 05:41:02,958 --> 05:41:08,080 two different categories they can be cpu 9895 05:41:05,280 --> 05:41:10,480 bound that is a program or a task 9896 05:41:08,080 --> 05:41:11,680 that spends most of its time waiting for 9897 05:41:10,480 --> 05:41:14,878 internal events 9898 05:41:11,680 --> 05:41:17,360 such as a task that is cpu intensive 9899 05:41:14,878 --> 05:41:19,600 it is better to use multi-processing for 9900 05:41:17,360 --> 05:41:22,798 tasks that are cpu bound 9901 05:41:19,600 --> 05:41:24,798 now tasks that are io bound means that 9902 05:41:22,798 --> 05:41:26,878 the program will spend most of its time 9903 05:41:24,798 --> 05:41:29,280 waiting for external events 9904 05:41:26,878 --> 05:41:30,878 such as waiting for user input or if 9905 05:41:29,280 --> 05:41:31,520 you're doing activities like web 9906 05:41:30,878 --> 05:41:33,840 scraping 9907 05:41:31,520 --> 05:41:35,680 you do a lot of sitting around so with i 9908 05:41:33,840 --> 05:41:36,718 o bound tasks it's better to use 9909 05:41:35,680 --> 05:41:38,798 multi-threading 9910 05:41:36,718 --> 05:41:40,080 because we can have multiple threads 9911 05:41:38,798 --> 05:41:42,080 running concurrently 9912 05:41:40,080 --> 05:41:43,920 but not truly in parallel like what we 9913 05:41:42,080 --> 05:41:46,160 do with multi-processing 9914 05:41:43,920 --> 05:41:48,160 after importing the threading module we 9915 05:41:46,160 --> 05:41:48,958 can count the number of threads that are 9916 05:41:48,160 --> 05:41:50,798 currently running 9917 05:41:48,958 --> 05:41:52,798 in the background whenever we run a 9918 05:41:50,798 --> 05:41:54,400 program we have one thread that is 9919 05:41:52,798 --> 05:41:56,958 running that is in charge of 9920 05:41:54,400 --> 05:41:58,878 executing our program and we can print 9921 05:41:56,958 --> 05:41:59,520 the active count of threads running in 9922 05:41:58,878 --> 05:42:01,920 our program 9923 05:41:59,520 --> 05:42:03,120 using the active count function of the 9924 05:42:01,920 --> 05:42:05,680 threading module 9925 05:42:03,120 --> 05:42:06,958 so this will print one we have one 9926 05:42:05,680 --> 05:42:09,760 thread that is running 9927 05:42:06,958 --> 05:42:11,040 and we can print a list of all the 9928 05:42:09,760 --> 05:42:14,080 threads that are running 9929 05:42:11,040 --> 05:42:16,080 by using the enumerate function so 9930 05:42:14,080 --> 05:42:17,360 the one thread that is in charge of 9931 05:42:16,080 --> 05:42:20,080 running our program 9932 05:42:17,360 --> 05:42:22,400 is referred to as the main thread by 9933 05:42:20,080 --> 05:42:22,958 using this concept of multi-threading we 9934 05:42:22,400 --> 05:42:24,718 can have 9935 05:42:22,958 --> 05:42:27,120 more than one thread running 9936 05:42:24,718 --> 05:42:28,958 concurrently not truly in parallel 9937 05:42:27,120 --> 05:42:30,400 all the threads will take turns while 9938 05:42:28,958 --> 05:42:32,638 one of them is idle 9939 05:42:30,400 --> 05:42:34,000 so we can have more than one thread 9940 05:42:32,638 --> 05:42:35,840 running more than just the main 9941 05:42:34,000 --> 05:42:37,360 thread which is in charge of running the 9942 05:42:35,840 --> 05:42:39,520 main body of our program 9943 05:42:37,360 --> 05:42:41,600 so while our main thread is in charge of 9944 05:42:39,520 --> 05:42:43,280 running the main body of our program 9945 05:42:41,600 --> 05:42:45,280 we can have another thread that's in 9946 05:42:43,280 --> 05:42:47,120 charge of a separate part of it 9947 05:42:45,280 --> 05:42:48,400 maybe like a countdown timer or 9948 05:42:47,120 --> 05:42:50,718 something so 9949 05:42:48,400 --> 05:42:52,878 one good example take that quiz game 9950 05:42:50,718 --> 05:42:55,040 that we made some number of videos ago 9951 05:42:52,878 --> 05:42:56,000 while we were waiting for user input 9952 05:42:55,040 --> 05:42:58,080 which is a 9953 05:42:56,000 --> 05:42:59,520 i o bound task we could have had a 9954 05:42:58,080 --> 05:43:01,200 countdown timer going 9955 05:42:59,520 --> 05:43:03,200 like you only have so many seconds to 9956 05:43:01,200 --> 05:43:05,200 answer this question we could have had 9957 05:43:03,200 --> 05:43:06,878 one thread in charge of waiting for user 9958 05:43:05,200 --> 05:43:09,120 input and another thread 9959 05:43:06,878 --> 05:43:11,200 in charge of the countdown timer so 9960 05:43:09,120 --> 05:43:14,000 that's an example of multi-threading 9961 05:43:11,200 --> 05:43:15,680 we had two threads running concurrently 9962 05:43:14,000 --> 05:43:17,520 and what we'll be doing in this video is 9963 05:43:15,680 --> 05:43:19,120 creating a program that involves 9964 05:43:17,520 --> 05:43:21,040 multi-threading we can have 9965 05:43:19,120 --> 05:43:22,878 different threads in charge of different 9966 05:43:21,040 --> 05:43:23,200 parts of our program and they can all 9967 05:43:22,878 --> 05:43:25,200 run 9968 05:43:23,200 --> 05:43:26,958 concurrently they'll all take turns 9969 05:43:25,200 --> 05:43:29,600 while one of them is idle 9970 05:43:26,958 --> 05:43:30,080 so let's say that we're running late for 9971 05:43:29,600 --> 05:43:32,480 school 9972 05:43:30,080 --> 05:43:34,320 or work in the morning and we have three 9973 05:43:32,480 --> 05:43:35,360 different tasks that we need to complete 9974 05:43:34,320 --> 05:43:38,000 before we can leave 9975 05:43:35,360 --> 05:43:38,558 for school or work so think of three 9976 05:43:38,000 --> 05:43:40,320 different 9977 05:43:38,558 --> 05:43:41,600 things you do in the morning such as 9978 05:43:40,320 --> 05:43:44,958 maybe eat 9979 05:43:41,600 --> 05:43:48,240 breakfast some people drink coffee or 9980 05:43:44,958 --> 05:43:51,280 maybe a beverage of your choice so drink 9981 05:43:48,240 --> 05:43:51,680 coffee and some people like me they like 9982 05:43:51,280 --> 05:43:53,520 to 9983 05:43:51,680 --> 05:43:54,878 do their homework last minute so i'll 9984 05:43:53,520 --> 05:43:57,520 say study 9985 05:43:54,878 --> 05:43:58,320 before i leave for work or school in the 9986 05:43:57,520 --> 05:44:00,000 morning 9987 05:43:58,320 --> 05:44:01,760 so what we'll do in each of these 9988 05:44:00,000 --> 05:44:03,600 functions each of these functions 9989 05:44:01,760 --> 05:44:06,000 should take some amount of time to 9990 05:44:03,600 --> 05:44:07,840 complete so we can have our main thread 9991 05:44:06,000 --> 05:44:10,240 sleep for a given number of seconds 9992 05:44:07,840 --> 05:44:12,558 using the sleep function 9993 05:44:10,240 --> 05:44:14,080 time dot sleep and let's say that in 9994 05:44:12,558 --> 05:44:16,638 order for me to eat breakfast 9995 05:44:14,080 --> 05:44:18,240 this task will take me three seconds 9996 05:44:16,638 --> 05:44:19,200 let's just pretend that instead of 9997 05:44:18,240 --> 05:44:23,120 minutes this will be 9998 05:44:19,200 --> 05:44:26,320 in seconds so drink coffee will take me 9999 05:44:23,120 --> 05:44:30,240 four seconds and study will take me 10000 05:44:26,320 --> 05:44:31,360 five seconds then when we finish 10001 05:44:30,240 --> 05:44:34,080 sleeping 10002 05:44:31,360 --> 05:44:35,440 let's print a confirmation message let's 10003 05:44:34,080 --> 05:44:38,718 say 10004 05:44:35,440 --> 05:44:41,760 you eat breakfast 10005 05:44:38,718 --> 05:44:43,440 as in you finish eating breakfast with 10006 05:44:41,760 --> 05:44:48,000 drink coffee 10007 05:44:43,440 --> 05:44:50,878 you drink coffee 10008 05:44:48,000 --> 05:44:50,878 and with study 10009 05:44:51,760 --> 05:44:58,798 you finish studying 10010 05:44:56,080 --> 05:45:00,160 now each of these tasks are i o bound 10011 05:44:58,798 --> 05:45:01,520 they're going to be spending a lot of 10012 05:45:00,160 --> 05:45:03,920 time just waiting around 10013 05:45:01,520 --> 05:45:06,160 for external events they're waiting for 10014 05:45:03,920 --> 05:45:07,680 the sleep function to expire before they 10015 05:45:06,160 --> 05:45:09,440 can finish their task 10016 05:45:07,680 --> 05:45:12,240 so we're going to have all of these 10017 05:45:09,440 --> 05:45:13,840 three functions run on our main thread 10018 05:45:12,240 --> 05:45:15,840 and we'll see how long it takes for us 10019 05:45:13,840 --> 05:45:18,160 to complete our morning ritual 10020 05:45:15,840 --> 05:45:19,440 these three tasks so let's call these 10021 05:45:18,160 --> 05:45:22,558 three functions within 10022 05:45:19,440 --> 05:45:26,798 our main thread so let's call the 10023 05:45:22,558 --> 05:45:30,558 eat breakfast function first followed by 10024 05:45:26,798 --> 05:45:33,680 drink coffee and then study 10025 05:45:30,558 --> 05:45:35,200 in that order so this program is going 10026 05:45:33,680 --> 05:45:38,240 to take approximately 10027 05:45:35,200 --> 05:45:40,958 let's see 12 seconds to complete so 10028 05:45:38,240 --> 05:45:44,958 there's going to be a pause for a second 10029 05:45:40,958 --> 05:45:50,718 you eat breakfast then followed by 10030 05:45:44,958 --> 05:45:52,878 you drank coffee and then study 10031 05:45:50,718 --> 05:45:53,760 you finished studying so this program 10032 05:45:52,878 --> 05:45:56,480 took about 10033 05:45:53,760 --> 05:45:57,280 12 seconds overall so if this were 10034 05:45:56,480 --> 05:45:59,280 realistic 10035 05:45:57,280 --> 05:46:01,040 what we would have done is we would sit 10036 05:45:59,280 --> 05:46:03,040 down and eat breakfast for 10037 05:46:01,040 --> 05:46:04,400 three minutes well three seconds in this 10038 05:46:03,040 --> 05:46:06,878 case and then 10039 05:46:04,400 --> 05:46:09,360 once we finish eating breakfast only 10040 05:46:06,878 --> 05:46:12,240 then are we allowed to drink our coffee 10041 05:46:09,360 --> 05:46:13,520 and once we finish our coffee only then 10042 05:46:12,240 --> 05:46:16,480 can we study 10043 05:46:13,520 --> 05:46:19,040 so we completed these tasks sequentially 10044 05:46:16,480 --> 05:46:19,760 and not concurrently for us to move down 10045 05:46:19,040 --> 05:46:21,680 to the next 10046 05:46:19,760 --> 05:46:23,440 function we need to complete the 10047 05:46:21,680 --> 05:46:24,558 previous functions because we're doing 10048 05:46:23,440 --> 05:46:27,440 this in order 10049 05:46:24,558 --> 05:46:28,480 but realistically us human beings we 10050 05:46:27,440 --> 05:46:30,878 would probably 10051 05:46:28,480 --> 05:46:32,000 eat breakfast drink coffee and study 10052 05:46:30,878 --> 05:46:34,400 altogether because 10053 05:46:32,000 --> 05:46:36,400 we can multitask and we can complete 10054 05:46:34,400 --> 05:46:38,240 these three functions in less time 10055 05:46:36,400 --> 05:46:40,000 and that's kind of the same process as 10056 05:46:38,240 --> 05:46:42,320 multi-threading we can have 10057 05:46:40,000 --> 05:46:43,040 these three separate functions running 10058 05:46:42,320 --> 05:46:45,200 concurrently 10059 05:46:43,040 --> 05:46:46,798 as if we're multitasking we're eating 10060 05:46:45,200 --> 05:46:49,440 breakfast drinking coffee 10061 05:46:46,798 --> 05:46:50,000 and studying all at once now currently 10062 05:46:49,440 --> 05:46:52,558 we have 10063 05:46:50,000 --> 05:46:54,160 one thread that is in charge of these 10064 05:46:52,558 --> 05:46:56,240 three separate functions 10065 05:46:54,160 --> 05:46:57,680 what we could do is that we can create 10066 05:46:56,240 --> 05:47:00,000 three additional threads 10067 05:46:57,680 --> 05:47:02,240 each thread will be in charge of each 10068 05:47:00,000 --> 05:47:03,520 task and then we'll have our main thread 10069 05:47:02,240 --> 05:47:05,120 running in the background that will 10070 05:47:03,520 --> 05:47:06,878 complete the rest of the program 10071 05:47:05,120 --> 05:47:08,958 so this is how to create an additional 10072 05:47:06,878 --> 05:47:12,320 thread let's say that 10073 05:47:08,958 --> 05:47:15,920 x equals threading 10074 05:47:12,320 --> 05:47:20,080 dot thread we need to pass in a target 10075 05:47:15,920 --> 05:47:21,520 target equals and then the name of the 10076 05:47:20,080 --> 05:47:23,680 function so let's say 10077 05:47:21,520 --> 05:47:24,798 thread x will be in charge of eating 10078 05:47:23,680 --> 05:47:26,638 breakfast 10079 05:47:24,798 --> 05:47:28,638 and then you can pass in arguments too 10080 05:47:26,638 --> 05:47:29,680 if your function has parameters by 10081 05:47:28,638 --> 05:47:31,600 typing args 10082 05:47:29,680 --> 05:47:33,600 and then you pass in a tuple so let's 10083 05:47:31,600 --> 05:47:35,600 say you have one argument to pass in 10084 05:47:33,600 --> 05:47:37,680 you'll type your argument followed by 10085 05:47:35,600 --> 05:47:38,958 a comma but we don't have any arguments 10086 05:47:37,680 --> 05:47:40,638 in this example but 10087 05:47:38,958 --> 05:47:42,080 you'll need to be sure to enter them in 10088 05:47:40,638 --> 05:47:44,638 if you do have any 10089 05:47:42,080 --> 05:47:45,600 okay then to begin this thread you type 10090 05:47:44,638 --> 05:47:48,878 the name of the thread 10091 05:47:45,600 --> 05:47:50,798 in this case x dot start so 10092 05:47:48,878 --> 05:47:52,320 we now have an additional thread and 10093 05:47:50,798 --> 05:47:53,200 this thread is in charge of eating 10094 05:47:52,320 --> 05:47:55,840 breakfast 10095 05:47:53,200 --> 05:47:57,280 now let's create another thread to drink 10096 05:47:55,840 --> 05:48:00,558 coffee 10097 05:47:57,280 --> 05:48:00,558 and we'll call this y 10098 05:48:01,760 --> 05:48:08,840 and the target will be drink coffee 10099 05:48:05,360 --> 05:48:10,638 and lastly we have a thread in charge of 10100 05:48:08,840 --> 05:48:13,840 studying 10101 05:48:10,638 --> 05:48:13,840 and this will be z 10102 05:48:14,798 --> 05:48:19,920 and the target is our study function 10103 05:48:17,920 --> 05:48:21,520 okay now let's see how long it takes for 10104 05:48:19,920 --> 05:48:23,600 us to complete our program 10105 05:48:21,520 --> 05:48:25,200 oh and be sure to comment out these 10106 05:48:23,600 --> 05:48:26,558 function calls within the main thread 10107 05:48:25,200 --> 05:48:27,760 because we don't want the main thread in 10108 05:48:26,558 --> 05:48:30,240 charge of those anymore 10109 05:48:27,760 --> 05:48:32,798 okay now we can run the program so we 10110 05:48:30,240 --> 05:48:34,878 have four threads running this time 10111 05:48:32,798 --> 05:48:35,840 you eat breakfast you drink coffee you 10112 05:48:34,878 --> 05:48:38,638 finish studying 10113 05:48:35,840 --> 05:48:39,840 so this program took approximately five 10114 05:48:38,638 --> 05:48:41,600 seconds to finish 10115 05:48:39,840 --> 05:48:43,200 and the reason that this program took 10116 05:48:41,600 --> 05:48:45,440 five seconds instead of 12 10117 05:48:43,200 --> 05:48:46,558 is because before our main thread was in 10118 05:48:45,440 --> 05:48:49,360 charge of running 10119 05:48:46,558 --> 05:48:50,958 these tasks sequentially in order but 10120 05:48:49,360 --> 05:48:53,600 now since we have a thread 10121 05:48:50,958 --> 05:48:54,080 dedicated to each task we can run them 10122 05:48:53,600 --> 05:48:56,400 all 10123 05:48:54,080 --> 05:48:57,520 concurrently instead of sequentially so 10124 05:48:56,400 --> 05:48:59,920 that this program 10125 05:48:57,520 --> 05:49:01,120 now took about five seconds to complete 10126 05:48:59,920 --> 05:49:03,200 and you may have noticed too 10127 05:49:01,120 --> 05:49:04,480 that the activecount function as well as 10128 05:49:03,200 --> 05:49:07,440 the enumerate function 10129 05:49:04,480 --> 05:49:09,520 were called before threads 1 2 and 3 10130 05:49:07,440 --> 05:49:11,360 finished their respective tasks 10131 05:49:09,520 --> 05:49:12,638 that's because the main thread is not 10132 05:49:11,360 --> 05:49:14,798 going to wait around for 10133 05:49:12,638 --> 05:49:16,878 these three threads to complete it has 10134 05:49:14,798 --> 05:49:19,920 its own set of instructions to do 10135 05:49:16,878 --> 05:49:21,280 so it is no longer in charge of these 10136 05:49:19,920 --> 05:49:23,120 three functions 10137 05:49:21,280 --> 05:49:25,280 the program is going to handle those 10138 05:49:23,120 --> 05:49:27,280 three functions to our three threads 10139 05:49:25,280 --> 05:49:28,878 and our main thread is going to continue 10140 05:49:27,280 --> 05:49:31,760 its own set of instructions 10141 05:49:28,878 --> 05:49:32,638 its job is to print the active count as 10142 05:49:31,760 --> 05:49:35,040 well as 10143 05:49:32,638 --> 05:49:36,638 call the enumerate function which it did 10144 05:49:35,040 --> 05:49:39,680 and it finished its tasks 10145 05:49:36,638 --> 05:49:40,878 before threads one two and three now one 10146 05:49:39,680 --> 05:49:43,120 trick that you can do too 10147 05:49:40,878 --> 05:49:43,920 is that you can use the time modules 10148 05:49:43,120 --> 05:49:46,400 performance 10149 05:49:43,920 --> 05:49:47,120 counter function and this function will 10150 05:49:46,400 --> 05:49:49,120 return 10151 05:49:47,120 --> 05:49:50,480 how long it takes our calling thread as 10152 05:49:49,120 --> 05:49:53,600 in our main thread 10153 05:49:50,480 --> 05:49:55,920 to finish its set of instructions so our 10154 05:49:53,600 --> 05:49:56,638 main thread is not in charge of 10155 05:49:55,920 --> 05:49:59,120 executing 10156 05:49:56,638 --> 05:50:00,558 these three functions our main thread is 10157 05:49:59,120 --> 05:50:02,718 in charge of creating 10158 05:50:00,558 --> 05:50:04,798 three additional threads and then 10159 05:50:02,718 --> 05:50:06,958 calling the activecount function 10160 05:50:04,798 --> 05:50:09,360 and the enumerate function as well as 10161 05:50:06,958 --> 05:50:11,280 the performance counter function 10162 05:50:09,360 --> 05:50:13,240 so our main thread will take 10163 05:50:11,280 --> 05:50:15,600 approximately 10164 05:50:13,240 --> 05:50:17,280 0.075 seconds to complete 10165 05:50:15,600 --> 05:50:19,680 and our three threads are still running 10166 05:50:17,280 --> 05:50:22,798 in the background our main threads job 10167 05:50:19,680 --> 05:50:24,958 its order of instructions is to create 10168 05:50:22,798 --> 05:50:25,920 three additional threads and then 10169 05:50:24,958 --> 05:50:28,400 immediately 10170 05:50:25,920 --> 05:50:29,840 print whatever is returned via the 10171 05:50:28,400 --> 05:50:31,840 activecount function 10172 05:50:29,840 --> 05:50:33,120 enumerate and then your performance 10173 05:50:31,840 --> 05:50:35,520 counter so 10174 05:50:33,120 --> 05:50:36,920 our main thread says that it finished 10175 05:50:35,520 --> 05:50:40,000 its tasks in about 10176 05:50:36,920 --> 05:50:42,160 .07 seconds but threads one 10177 05:50:40,000 --> 05:50:44,080 through three were still trying to catch 10178 05:50:42,160 --> 05:50:45,680 up that's why the main thread 10179 05:50:44,080 --> 05:50:47,200 finished before are three additional 10180 05:50:45,680 --> 05:50:49,440 threads and once 10181 05:50:47,200 --> 05:50:50,558 all active threads have completed their 10182 05:50:49,440 --> 05:50:52,958 tasks 10183 05:50:50,558 --> 05:50:54,718 then your program will finish and exit 10184 05:50:52,958 --> 05:50:56,798 there's also this concept called 10185 05:50:54,718 --> 05:50:58,638 thread synchronization we can have a 10186 05:50:56,798 --> 05:50:59,280 calling thread in this case our main 10187 05:50:58,638 --> 05:51:01,680 thread 10188 05:50:59,280 --> 05:51:02,878 wait around for another thread to finish 10189 05:51:01,680 --> 05:51:05,280 before it can move on 10190 05:51:02,878 --> 05:51:06,400 with its own instruction set so let's 10191 05:51:05,280 --> 05:51:08,798 say we would like 10192 05:51:06,400 --> 05:51:10,798 our main thread to wait around for 10193 05:51:08,798 --> 05:51:13,440 thread 1 also known as 10194 05:51:10,798 --> 05:51:14,080 x so we're going to use the join 10195 05:51:13,440 --> 05:51:17,120 function 10196 05:51:14,080 --> 05:51:19,600 of thread x and now our main thread 10197 05:51:17,120 --> 05:51:20,240 has to wait around for thread x also 10198 05:51:19,600 --> 05:51:22,480 known as 10199 05:51:20,240 --> 05:51:24,160 thread 1 to finish before it can move on 10200 05:51:22,480 --> 05:51:27,200 with its instruction set 10201 05:51:24,160 --> 05:51:30,160 so let's do the same with y and 10202 05:51:27,200 --> 05:51:32,080 z so now our main thread before it can 10203 05:51:30,160 --> 05:51:33,760 move on with the rest of the program has 10204 05:51:32,080 --> 05:51:36,638 to wait for all of these threads 10205 05:51:33,760 --> 05:51:37,200 to synchronize and join and then and 10206 05:51:36,638 --> 05:51:39,040 only then 10207 05:51:37,200 --> 05:51:40,240 can it move on with the rest of its own 10208 05:51:39,040 --> 05:51:42,080 instruction set 10209 05:51:40,240 --> 05:51:44,080 so this time our program is going to 10210 05:51:42,080 --> 05:51:46,480 look a little something like this 10211 05:51:44,080 --> 05:51:47,600 you eat breakfast you drink coffee you 10212 05:51:46,480 --> 05:51:50,000 finish studying 10213 05:51:47,600 --> 05:51:50,958 so by the time we reach our active count 10214 05:51:50,000 --> 05:51:52,878 these threads 10215 05:51:50,958 --> 05:51:54,320 are already joined and synchronized 10216 05:51:52,878 --> 05:51:56,320 they're no longer active 10217 05:51:54,320 --> 05:51:57,680 when we enumerate over our active 10218 05:51:56,320 --> 05:51:59,840 threads we only have 10219 05:51:57,680 --> 05:52:00,718 our main thread and our main thread 10220 05:51:59,840 --> 05:52:03,600 completed in 10221 05:52:00,718 --> 05:52:04,958 about 5.1 seconds this time because it 10222 05:52:03,600 --> 05:52:08,000 did a lot of waiting around 10223 05:52:04,958 --> 05:52:10,878 it was waiting for threads x y and z to 10224 05:52:08,000 --> 05:52:11,200 join and finish in conclusion a thread 10225 05:52:10,878 --> 05:52:14,080 is 10226 05:52:11,200 --> 05:52:16,320 a flow of execution like a separate 10227 05:52:14,080 --> 05:52:16,958 order of instructions that a program can 10228 05:52:16,320 --> 05:52:19,360 follow 10229 05:52:16,958 --> 05:52:20,320 and when we run a program we always have 10230 05:52:19,360 --> 05:52:22,080 at least one 10231 05:52:20,320 --> 05:52:24,240 thread running initially and that is 10232 05:52:22,080 --> 05:52:26,400 referred to as the main thread 10233 05:52:24,240 --> 05:52:28,798 however if you have multiple threads 10234 05:52:26,400 --> 05:52:30,718 using the concept of multi-threading 10235 05:52:28,798 --> 05:52:33,200 we can have multiple threads running 10236 05:52:30,718 --> 05:52:34,480 concurrently but not truly in parallel 10237 05:52:33,200 --> 05:52:36,958 which is what we'll cover 10238 05:52:34,480 --> 05:52:37,680 during the video on multi-processing 10239 05:52:36,958 --> 05:52:40,718 this is due 10240 05:52:37,680 --> 05:52:43,520 to a feature known as the gil 10241 05:52:40,718 --> 05:52:43,920 the global interpreter lock which allows 10242 05:52:43,520 --> 05:52:46,240 only 10243 05:52:43,920 --> 05:52:47,120 one thread to hold control of the python 10244 05:52:46,240 --> 05:52:50,240 interpreter 10245 05:52:47,120 --> 05:52:53,680 at any one time and tasks can be either 10246 05:52:50,240 --> 05:52:55,600 cpu bound or io bound a cpu bound task 10247 05:52:53,680 --> 05:52:58,160 is a program or task 10248 05:52:55,600 --> 05:52:59,200 that spends most of its time waiting for 10249 05:52:58,160 --> 05:53:02,638 internal events 10250 05:52:59,200 --> 05:53:05,200 such as a task that is cpu intensive 10251 05:53:02,638 --> 05:53:06,638 and o bound tasks spend most of their 10252 05:53:05,200 --> 05:53:09,120 time waiting for 10253 05:53:06,638 --> 05:53:10,400 external events such as waiting around 10254 05:53:09,120 --> 05:53:13,360 for user input 10255 05:53:10,400 --> 05:53:15,440 or web scraping with i o bound tasks 10256 05:53:13,360 --> 05:53:18,120 it's better to use multi-threading 10257 05:53:15,440 --> 05:53:19,360 with cpu bound tasks it's better to use 10258 05:53:18,120 --> 05:53:21,680 multi-processing 10259 05:53:19,360 --> 05:53:23,920 well everybody that is a quick overview 10260 05:53:21,680 --> 05:53:25,520 of multi-threading i will post all of 10261 05:53:23,920 --> 05:53:26,080 this code to the comment section down 10262 05:53:25,520 --> 05:53:28,160 below 10263 05:53:26,080 --> 05:53:29,120 and well yeah that's how to achieve 10264 05:53:28,160 --> 05:53:32,638 multi-threading 10265 05:53:29,120 --> 05:53:33,280 using python hey what's going on 10266 05:53:32,638 --> 05:53:35,200 everybody 10267 05:53:33,280 --> 05:53:36,958 it's you bro hope you're doing well and 10268 05:53:35,200 --> 05:53:38,480 in this video i'm going to explain demon 10269 05:53:36,958 --> 05:53:43,040 threads in python so 10270 05:53:38,480 --> 05:53:46,638 sit back relax and enjoy the show 10271 05:53:43,040 --> 05:53:49,200 okay so demon threads also pronounced as 10272 05:53:46,638 --> 05:53:51,200 daemon threads a demon thread is a 10273 05:53:49,200 --> 05:53:52,958 thread that runs in the background and 10274 05:53:51,200 --> 05:53:54,558 they are normally not important for your 10275 05:53:52,958 --> 05:53:56,480 program in order to run 10276 05:53:54,558 --> 05:53:57,920 your program will not wait for demon 10277 05:53:56,480 --> 05:54:00,878 threads to complete before 10278 05:53:57,920 --> 05:54:02,558 exiting whereas non-demon threads they 10279 05:54:00,878 --> 05:54:04,718 cannot normally be killed 10280 05:54:02,558 --> 05:54:05,760 they will stay alive until their task is 10281 05:54:04,718 --> 05:54:08,320 complete so 10282 05:54:05,760 --> 05:54:08,958 a few common uses of demon threads would 10283 05:54:08,320 --> 05:54:11,520 be 10284 05:54:08,958 --> 05:54:12,718 background tasks garbage collection 10285 05:54:11,520 --> 05:54:15,200 waiting for input 10286 05:54:12,718 --> 05:54:17,200 or other long-running processes so 10287 05:54:15,200 --> 05:54:18,160 here's an example of why demon threads 10288 05:54:17,200 --> 05:54:20,240 would be useful 10289 05:54:18,160 --> 05:54:22,080 let's say that we'll have two threads 10290 05:54:20,240 --> 05:54:24,160 our main thread will be in charge of 10291 05:54:22,080 --> 05:54:24,638 waiting around for some user input we'll 10292 05:54:24,160 --> 05:54:26,558 ask 10293 05:54:24,638 --> 05:54:28,638 do you wish to exit and in the 10294 05:54:26,558 --> 05:54:29,440 background we'll have a timer going that 10295 05:54:28,638 --> 05:54:32,160 will display 10296 05:54:29,440 --> 05:54:32,958 how long somebody is logged in so let's 10297 05:54:32,160 --> 05:54:36,240 create 10298 05:54:32,958 --> 05:54:38,638 a function named timer 10299 05:54:36,240 --> 05:54:40,000 and what we'll do is just display how 10300 05:54:38,638 --> 05:54:43,040 long somebody's logged in 10301 05:54:40,000 --> 05:54:46,400 so let's say we have a count variable 10302 05:54:43,040 --> 05:54:49,280 while true we'll use 10303 05:54:46,400 --> 05:54:51,520 the sleep function of the time module to 10304 05:54:49,280 --> 05:54:53,520 sleep for approximately one second 10305 05:54:51,520 --> 05:54:56,320 then we'll increment our count variable 10306 05:54:53,520 --> 05:54:59,360 by one and we'll print a message 10307 05:54:56,320 --> 05:55:01,600 so let's say logged in 10308 05:54:59,360 --> 05:55:01,600 for 10309 05:55:03,440 --> 05:55:05,760 count 10310 05:55:06,638 --> 05:55:11,680 seconds and let's create a thread that 10311 05:55:09,600 --> 05:55:13,280 will be in charge of this timer and run 10312 05:55:11,680 --> 05:55:16,320 it in the background so let's say 10313 05:55:13,280 --> 05:55:19,360 x equals threading 10314 05:55:16,320 --> 05:55:19,920 dot thread the target will be our 10315 05:55:19,360 --> 05:55:22,240 function 10316 05:55:19,920 --> 05:55:23,120 of timer and we need to start this 10317 05:55:22,240 --> 05:55:26,160 thread 10318 05:55:23,120 --> 05:55:29,040 x dot start so our thread 10319 05:55:26,160 --> 05:55:30,160 x will be in charge of our timer and run 10320 05:55:29,040 --> 05:55:32,080 it concurrently 10321 05:55:30,160 --> 05:55:34,000 while we're waiting for some user input 10322 05:55:32,080 --> 05:55:36,160 our main thread will be in charge of 10323 05:55:34,000 --> 05:55:38,000 waiting for this user input do you wish 10324 05:55:36,160 --> 05:55:40,718 to exit now here's the issue 10325 05:55:38,000 --> 05:55:42,878 of us using a non-demon thread for a 10326 05:55:40,718 --> 05:55:43,600 background task such as a countdown 10327 05:55:42,878 --> 05:55:45,120 timer 10328 05:55:43,600 --> 05:55:46,958 so our main thread is in charge of 10329 05:55:45,120 --> 05:55:49,680 waiting around for some user input 10330 05:55:46,958 --> 05:55:50,320 and thread x our additional thread is in 10331 05:55:49,680 --> 05:55:52,400 charge of 10332 05:55:50,320 --> 05:55:54,160 running this countdown timer so if i 10333 05:55:52,400 --> 05:55:55,040 wish to exit this program i'm just going 10334 05:55:54,160 --> 05:55:57,840 to type 10335 05:55:55,040 --> 05:56:00,080 okay and hit enter well my background 10336 05:55:57,840 --> 05:56:02,400 timer is still going but my main thread 10337 05:56:00,080 --> 05:56:03,840 is complete so i would like to exit this 10338 05:56:02,400 --> 05:56:06,480 program but 10339 05:56:03,840 --> 05:56:08,638 my program will not exit as long as 10340 05:56:06,480 --> 05:56:09,520 there are non-demon threads that are 10341 05:56:08,638 --> 05:56:11,440 still alive 10342 05:56:09,520 --> 05:56:13,360 we cannot normally close this program 10343 05:56:11,440 --> 05:56:15,520 unless we do like a brute force 10344 05:56:13,360 --> 05:56:17,040 you know close and exit of the program 10345 05:56:15,520 --> 05:56:20,400 so we can turn 10346 05:56:17,040 --> 05:56:22,480 this thread of x into a demon thread 10347 05:56:20,400 --> 05:56:24,000 so that when we wish to exit the program 10348 05:56:22,480 --> 05:56:26,878 when there are no more 10349 05:56:24,000 --> 05:56:28,638 non-demon threads alive and to change a 10350 05:56:26,878 --> 05:56:30,400 thread to a demon thread it's actually 10351 05:56:28,638 --> 05:56:31,040 fairly easy when you create your thread 10352 05:56:30,400 --> 05:56:33,840 you can set 10353 05:56:31,040 --> 05:56:34,638 this flag of demon equal to true and 10354 05:56:33,840 --> 05:56:37,040 that's it 10355 05:56:34,638 --> 05:56:38,480 so when all non-demon threads have 10356 05:56:37,040 --> 05:56:41,360 finished their tasks 10357 05:56:38,480 --> 05:56:43,280 your program will complete and exit but 10358 05:56:41,360 --> 05:56:43,920 if there are demon tasks running in the 10359 05:56:43,280 --> 05:56:45,840 background 10360 05:56:43,920 --> 05:56:47,440 they will be killed automatically so 10361 05:56:45,840 --> 05:56:49,600 let's try this program again 10362 05:56:47,440 --> 05:56:50,638 so do you wish to exit my program is 10363 05:56:49,600 --> 05:56:52,080 going to continue running 10364 05:56:50,638 --> 05:56:53,920 my demon thread is in charge of the 10365 05:56:52,080 --> 05:56:54,638 timer and it's going to continue running 10366 05:56:53,920 --> 05:56:57,200 until 10367 05:56:54,638 --> 05:56:58,320 all non-daemon threads have completed 10368 05:56:57,200 --> 05:57:02,000 their tasks 10369 05:56:58,320 --> 05:57:04,080 so once i type ok my program will 10370 05:57:02,000 --> 05:57:05,280 finish running and my countdown timer 10371 05:57:04,080 --> 05:57:07,840 will stop because 10372 05:57:05,280 --> 05:57:09,520 all demons are killed when your program 10373 05:57:07,840 --> 05:57:11,040 is finished running there's also two 10374 05:57:09,520 --> 05:57:11,920 additional methods that you might be 10375 05:57:11,040 --> 05:57:14,958 interested in 10376 05:57:11,920 --> 05:57:16,958 you can use the set daemon method 10377 05:57:14,958 --> 05:57:18,798 of a thread and you can set it to false 10378 05:57:16,958 --> 05:57:21,920 or true if you would like to change 10379 05:57:18,798 --> 05:57:23,600 a thread to a non-demon or a demon 10380 05:57:21,920 --> 05:57:25,760 however if your thread is currently 10381 05:57:23,600 --> 05:57:26,638 running well you cannot actually change 10382 05:57:25,760 --> 05:57:28,798 it from 10383 05:57:26,638 --> 05:57:30,240 a non-demon thread to a demon thread 10384 05:57:28,798 --> 05:57:31,680 while it's currently running 10385 05:57:30,240 --> 05:57:34,240 so you would have to do that before you 10386 05:57:31,680 --> 05:57:36,080 actually use the start function 10387 05:57:34,240 --> 05:57:37,360 and you can also check to see if a 10388 05:57:36,080 --> 05:57:40,480 thread is a demon or not 10389 05:57:37,360 --> 05:57:41,120 by using the is demon method this will 10390 05:57:40,480 --> 05:57:44,400 return 10391 05:57:41,120 --> 05:57:46,878 true or false so everybody in conclusion 10392 05:57:44,400 --> 05:57:47,600 a demon thread is a thread that runs in 10393 05:57:46,878 --> 05:57:49,040 the background 10394 05:57:47,600 --> 05:57:51,200 and they're not important for your 10395 05:57:49,040 --> 05:57:53,280 program in order to continue running 10396 05:57:51,200 --> 05:57:55,200 your program will not wait for demon 10397 05:57:53,280 --> 05:57:56,000 threads to complete before exiting the 10398 05:57:55,200 --> 05:57:58,480 program 10399 05:57:56,000 --> 05:57:59,600 whereas non-demon threads cannot 10400 05:57:58,480 --> 05:58:01,760 normally be killed 10401 05:57:59,600 --> 05:58:04,160 they will stay alive until their task is 10402 05:58:01,760 --> 05:58:04,878 complete and demon threads are commonly 10403 05:58:04,160 --> 05:58:07,120 used for 10404 05:58:04,878 --> 05:58:08,558 background tasks garbage collection 10405 05:58:07,120 --> 05:58:11,520 waiting around for input 10406 05:58:08,558 --> 05:58:13,120 or other long running processes so if 10407 05:58:11,520 --> 05:58:14,638 you would like a copy of this code i 10408 05:58:13,120 --> 05:58:15,840 will post all of this to the comment 10409 05:58:14,638 --> 05:58:18,080 section down below 10410 05:58:15,840 --> 05:58:19,760 but yeah those are demon threads in 10411 05:58:18,080 --> 05:58:22,240 python 10412 05:58:19,760 --> 05:58:23,200 yo what's going on everybody it's bro 10413 05:58:22,240 --> 05:58:24,798 hope you're doing well 10414 05:58:23,200 --> 05:58:26,798 and in this video i'm going to explain 10415 05:58:24,798 --> 05:58:31,600 multi-processing in python so 10416 05:58:26,798 --> 05:58:31,600 sit back relax and enjoy the show 10417 05:58:31,760 --> 05:58:35,600 what's up let's talk about 10418 05:58:33,280 --> 05:58:38,958 multi-processing multi-processing 10419 05:58:35,600 --> 05:58:41,520 is the act of running tasks in parallel 10420 05:58:38,958 --> 05:58:42,638 on a different cpu cores it's different 10421 05:58:41,520 --> 05:58:44,638 from multi-threading 10422 05:58:42,638 --> 05:58:45,680 although it sounds similar because with 10423 05:58:44,638 --> 05:58:48,320 multi-threading 10424 05:58:45,680 --> 05:58:48,798 we're limited to running one thread at a 10425 05:58:48,320 --> 05:58:51,920 time 10426 05:58:48,798 --> 05:58:53,840 because of the gil that lock which is 10427 05:58:51,920 --> 05:58:54,798 used for threading we can run threads 10428 05:58:53,840 --> 05:58:57,000 concurrently 10429 05:58:54,798 --> 05:58:58,240 but not in parallel however with 10430 05:58:57,000 --> 05:59:01,120 multi-processing 10431 05:58:58,240 --> 05:59:02,080 we can create processes and we can run 10432 05:59:01,120 --> 05:59:05,200 each process 10433 05:59:02,080 --> 05:59:06,718 in parallel on a different cpu course so 10434 05:59:05,200 --> 05:59:09,920 with multi-processing 10435 05:59:06,718 --> 05:59:13,200 it's better for tasks that are cpu bound 10436 05:59:09,920 --> 05:59:15,760 where you need heavy cpu usage whereas 10437 05:59:13,200 --> 05:59:17,200 multi-threading that is better for i o 10438 05:59:15,760 --> 05:59:18,160 bound tasks where you do a lot of 10439 05:59:17,200 --> 05:59:20,638 waiting around 10440 05:59:18,160 --> 05:59:21,520 so before we begin i recommend these two 10441 05:59:20,638 --> 05:59:24,320 imports 10442 05:59:21,520 --> 05:59:26,160 multi-processing and time let's begin 10443 05:59:24,320 --> 05:59:28,160 quick note if you're running the windows 10444 05:59:26,160 --> 05:59:28,480 operating system you'll probably need to 10445 05:59:28,160 --> 05:59:31,600 add 10446 05:59:28,480 --> 05:59:34,798 this line if name is equal to 10447 05:59:31,600 --> 05:59:37,040 main so when we run a program we have a 10448 05:59:34,798 --> 05:59:38,718 main process that is running and if we 10449 05:59:37,040 --> 05:59:41,200 create a child process 10450 05:59:38,718 --> 05:59:43,280 from that process it's going to copy the 10451 05:59:41,200 --> 05:59:46,160 module that we're currently working with 10452 05:59:43,280 --> 05:59:48,160 and that child process will create its 10453 05:59:46,160 --> 05:59:49,360 own children processes and it's going to 10454 05:59:48,160 --> 05:59:51,760 be a problem 10455 05:59:49,360 --> 05:59:52,878 so we're going to add this line if name 10456 05:59:51,760 --> 05:59:54,798 is equal to main 10457 05:59:52,878 --> 05:59:56,878 so when we create a child process it 10458 05:59:54,798 --> 05:59:58,080 will copy our module but it's not going 10459 05:59:56,878 --> 06:00:00,718 to execute it 10460 05:59:58,080 --> 06:00:02,480 so let's create a main function and a 10461 06:00:00,718 --> 06:00:04,160 majority of our code is going to be 10462 06:00:02,480 --> 06:00:05,440 within our main function 10463 06:00:04,160 --> 06:00:07,040 if you're running a different operating 10464 06:00:05,440 --> 06:00:08,638 system you probably don't have to do 10465 06:00:07,040 --> 06:00:10,480 this but if you're running windows 10466 06:00:08,638 --> 06:00:12,718 you probably will now with 10467 06:00:10,480 --> 06:00:14,718 multi-processing multi-processing is 10468 06:00:12,718 --> 06:00:17,200 better for tasks or functions 10469 06:00:14,718 --> 06:00:19,120 that are cpu bound where they require 10470 06:00:17,200 --> 06:00:21,120 heavy cpu usage 10471 06:00:19,120 --> 06:00:22,240 let's say that we have a function named 10472 06:00:21,120 --> 06:00:24,480 counter 10473 06:00:22,240 --> 06:00:26,638 and we'll pass in a number to count up 10474 06:00:24,480 --> 06:00:28,878 to but the number we're going to pass in 10475 06:00:26,638 --> 06:00:30,958 is a ridiculously large number like a 10476 06:00:28,878 --> 06:00:31,520 billion so let's create a function that 10477 06:00:30,958 --> 06:00:35,040 will count 10478 06:00:31,520 --> 06:00:38,080 from zero so count equals zero and 10479 06:00:35,040 --> 06:00:40,240 while count is less than 10480 06:00:38,080 --> 06:00:42,240 our number that we pass in we will 10481 06:00:40,240 --> 06:00:45,200 increment our counter 10482 06:00:42,240 --> 06:00:46,000 by one so that is the function that 10483 06:00:45,200 --> 06:00:48,798 we'll call with 10484 06:00:46,000 --> 06:00:49,760 our processes that we create now within 10485 06:00:48,798 --> 06:00:52,000 our main function 10486 06:00:49,760 --> 06:00:54,638 we'll create a process and to do that 10487 06:00:52,000 --> 06:00:57,600 you'll need the multi-processing module 10488 06:00:54,638 --> 06:00:59,280 so import process and cpu account and 10489 06:00:57,600 --> 06:01:01,200 we'll save this for a little bit later 10490 06:00:59,280 --> 06:01:02,558 to create a process let's say we have 10491 06:01:01,200 --> 06:01:06,320 process a 10492 06:01:02,558 --> 06:01:08,400 a equals process this step is very 10493 06:01:06,320 --> 06:01:10,798 similar to creating a thread 10494 06:01:08,400 --> 06:01:13,760 we have a target our target will be our 10495 06:01:10,798 --> 06:01:16,160 function of counter 10496 06:01:13,760 --> 06:01:16,878 and if we have arguments we will pass 10497 06:01:16,160 --> 06:01:18,958 those in 10498 06:01:16,878 --> 06:01:20,718 so remember with our arguments we have 10499 06:01:18,958 --> 06:01:23,120 to pass in a tuple 10500 06:01:20,718 --> 06:01:23,840 since we only have one argument to pass 10501 06:01:23,120 --> 06:01:25,920 in 10502 06:01:23,840 --> 06:01:27,760 to differentiate this from an expression 10503 06:01:25,920 --> 06:01:31,520 we have to add a comma at the end 10504 06:01:27,760 --> 06:01:35,200 so our number let's say is 1 billion 10505 06:01:31,520 --> 06:01:35,680 so that's a million 10 million 100 10506 06:01:35,200 --> 06:01:38,558 million 10507 06:01:35,680 --> 06:01:39,840 1 billion let me just verify that okay 10508 06:01:38,558 --> 06:01:42,080 so that's 1 billion 10509 06:01:39,840 --> 06:01:42,878 we're going to count from zero to a 10510 06:01:42,080 --> 06:01:46,558 billion 10511 06:01:42,878 --> 06:01:50,320 and in order to start this process a 10512 06:01:46,558 --> 06:01:52,878 my process use the start function 10513 06:01:50,320 --> 06:01:54,480 and then i will use some process 10514 06:01:52,878 --> 06:01:57,920 synchronization by using the 10515 06:01:54,480 --> 06:01:58,638 join function my main process is going 10516 06:01:57,920 --> 06:02:01,360 to wait around 10517 06:01:58,638 --> 06:02:03,040 for my child process of a to finish 10518 06:02:01,360 --> 06:02:06,400 before continuing 10519 06:02:03,040 --> 06:02:12,000 and let's print our performance counter 10520 06:02:06,400 --> 06:02:17,520 so we'll print finished in 10521 06:02:12,000 --> 06:02:17,520 let's say time dot performance counter 10522 06:02:19,760 --> 06:02:25,520 seconds okay so let's see how long it'll 10523 06:02:23,120 --> 06:02:26,080 take to count from zero to a billion 10524 06:02:25,520 --> 06:02:28,558 using 10525 06:02:26,080 --> 06:02:30,120 one process i fast forwarded this video 10526 06:02:28,558 --> 06:02:33,440 but it took my program 10527 06:02:30,120 --> 06:02:34,080 56.7 seconds to count from zero to a 10528 06:02:33,440 --> 06:02:35,878 billion 10529 06:02:34,080 --> 06:02:38,240 but we can speed this up by using 10530 06:02:35,878 --> 06:02:40,878 multi-processing let's create a second 10531 06:02:38,240 --> 06:02:42,798 process named b 10532 06:02:40,878 --> 06:02:44,160 and i'm going to divide the amount of 10533 06:02:42,798 --> 06:02:47,200 work in half so 10534 06:02:44,160 --> 06:02:52,000 each process will count to 500 million 10535 06:02:47,200 --> 06:02:53,600 half of a billion so let's change a to b 10536 06:02:52,000 --> 06:02:56,480 and i'm gonna group these start 10537 06:02:53,600 --> 06:02:56,480 functions together 10538 06:02:57,600 --> 06:03:02,718 just so it's easier to read and then b 10539 06:02:59,600 --> 06:03:06,080 dot join just to synchronize everything 10540 06:03:02,718 --> 06:03:09,360 okay and then let's change 1 billion to 10541 06:03:06,080 --> 06:03:12,000 500 million for each it's the same 10542 06:03:09,360 --> 06:03:14,718 amount of work but divided among workers 10543 06:03:12,000 --> 06:03:16,400 so that is 500 million let's see how 10544 06:03:14,718 --> 06:03:19,520 well it runs this time 10545 06:03:16,400 --> 06:03:20,080 so it took my program about 40.3 seconds 10546 06:03:19,520 --> 06:03:22,558 to finish 10547 06:03:20,080 --> 06:03:24,080 counting from zero to a billion but i 10548 06:03:22,558 --> 06:03:26,240 divided that task among 10549 06:03:24,080 --> 06:03:27,520 different processes each of my two 10550 06:03:26,240 --> 06:03:30,558 processes counted from 10551 06:03:27,520 --> 06:03:31,600 zero to 500 million this time so this 10552 06:03:30,558 --> 06:03:35,840 time let's create 10553 06:03:31,600 --> 06:03:39,520 four processes so we have a 10554 06:03:35,840 --> 06:03:42,958 b c and d 10555 06:03:39,520 --> 06:03:42,958 and we'll need to start them 10556 06:03:44,400 --> 06:03:50,320 and i'm going to join them now this 10557 06:03:48,480 --> 06:03:52,080 might not actually speed it up for you 10558 06:03:50,320 --> 06:03:54,240 and i'll explain why later 10559 06:03:52,080 --> 06:03:55,520 it depends on the amount of cpu cores 10560 06:03:54,240 --> 06:03:59,520 that you have 10561 06:03:55,520 --> 06:04:02,638 okay so oh let's change this to so 250 10562 06:03:59,520 --> 06:04:06,000 million for each 10563 06:04:02,638 --> 06:04:08,878 okay see you in a couple seconds 10564 06:04:06,000 --> 06:04:09,440 okay welcome back so on my computer when 10565 06:04:08,878 --> 06:04:11,760 i ran 10566 06:04:09,440 --> 06:04:14,000 four processes and each was counting up 10567 06:04:11,760 --> 06:04:17,280 to 250 million 10568 06:04:14,000 --> 06:04:20,480 i could finish my program in 27.3 10569 06:04:17,280 --> 06:04:22,558 seconds now for me if i created 10570 06:04:20,480 --> 06:04:24,638 more processes than this it would 10571 06:04:22,558 --> 06:04:27,280 probably take longer and here's why 10572 06:04:24,638 --> 06:04:27,920 so you can print the cpu count of your 10573 06:04:27,280 --> 06:04:31,200 computer 10574 06:04:27,920 --> 06:04:31,760 using this function of cpu count so if i 10575 06:04:31,200 --> 06:04:34,080 were to 10576 06:04:31,760 --> 06:04:35,280 print whatever this function returns i 10577 06:04:34,080 --> 06:04:37,440 can get the count 10578 06:04:35,280 --> 06:04:38,638 of the number of additional processes 10579 06:04:37,440 --> 06:04:40,080 that i can run 10580 06:04:38,638 --> 06:04:42,718 for the time being i'm just going to 10581 06:04:40,080 --> 06:04:44,638 comment out this start function because 10582 06:04:42,718 --> 06:04:47,840 i just want to print whatever this value 10583 06:04:44,638 --> 06:04:51,280 is so my cpu count on my computer 10584 06:04:47,840 --> 06:04:52,160 is four so i can run four additional 10585 06:04:51,280 --> 06:04:55,280 processes 10586 06:04:52,160 --> 06:04:57,840 but let's attempt to run eight processes 10587 06:04:55,280 --> 06:04:59,360 so i'm going to copy all these and let's 10588 06:04:57,840 --> 06:05:02,958 say that we have 10589 06:04:59,360 --> 06:05:06,400 e f g 10590 06:05:02,958 --> 06:05:10,558 and h and we will start all of those two 10591 06:05:06,400 --> 06:05:10,558 so give me a second just to start them 10592 06:05:10,718 --> 06:05:18,080 so a b c d e 10593 06:05:14,558 --> 06:05:22,878 f g 10594 06:05:18,080 --> 06:05:22,878 h and then i'm going to join all of them 10595 06:05:23,920 --> 06:05:31,840 e f g 10596 06:05:28,798 --> 06:05:34,240 and h okay so 10597 06:05:31,840 --> 06:05:35,920 let's run this now oh and change the 10598 06:05:34,240 --> 06:05:36,400 arguments to i keep forgetting to do 10599 06:05:35,920 --> 06:05:40,480 that 10600 06:05:36,400 --> 06:05:43,680 so let's say 125 for each 10601 06:05:40,480 --> 06:05:47,360 so 125 million times eight is 10602 06:05:43,680 --> 06:05:49,520 one billion okay see you in a bit 10603 06:05:47,360 --> 06:05:51,680 all right welcome back this time when i 10604 06:05:49,520 --> 06:05:52,160 had eight processes working it actually 10605 06:05:51,680 --> 06:05:54,240 took me 10606 06:05:52,160 --> 06:05:56,240 longer than when i had four processes 10607 06:05:54,240 --> 06:05:58,798 working when i had four processes 10608 06:05:56,240 --> 06:06:01,120 working it took me about 27 seconds 10609 06:05:58,798 --> 06:06:02,638 this time it took 30 seconds when i had 10610 06:06:01,120 --> 06:06:05,040 eight processes working 10611 06:06:02,638 --> 06:06:06,400 that's because i had more processes than 10612 06:06:05,040 --> 06:06:08,320 my cpu count 10613 06:06:06,400 --> 06:06:09,680 and that's partially because whenever 10614 06:06:08,320 --> 06:06:11,920 you create a process 10615 06:06:09,680 --> 06:06:12,798 there's significant overhead with 10616 06:06:11,920 --> 06:06:15,200 beginning and 10617 06:06:12,798 --> 06:06:16,000 destroying a process and if i can only 10618 06:06:15,200 --> 06:06:18,638 run four 10619 06:06:16,000 --> 06:06:20,400 processes on my computer at one time 10620 06:06:18,638 --> 06:06:21,120 well then i'm creating additional 10621 06:06:20,400 --> 06:06:23,520 processes 10622 06:06:21,120 --> 06:06:24,798 to no extra benefit it's actually 10623 06:06:23,520 --> 06:06:25,520 hindering the performance of the 10624 06:06:24,798 --> 06:06:27,120 computer 10625 06:06:25,520 --> 06:06:28,958 because i'm creating all of these 10626 06:06:27,120 --> 06:06:30,160 additional processes when it really 10627 06:06:28,958 --> 06:06:32,638 doesn't help me 10628 06:06:30,160 --> 06:06:33,280 in conclusion multi-processing is the 10629 06:06:32,638 --> 06:06:35,600 concept 10630 06:06:33,280 --> 06:06:37,200 of running tasks in parallel on 10631 06:06:35,600 --> 06:06:38,798 different cpu cores 10632 06:06:37,200 --> 06:06:40,240 it's similar but different from 10633 06:06:38,798 --> 06:06:41,280 multi-threading because with 10634 06:06:40,240 --> 06:06:43,680 multi-threading 10635 06:06:41,280 --> 06:06:45,760 we can run tasks concurrently but 10636 06:06:43,680 --> 06:06:46,718 they're all taking turns because of the 10637 06:06:45,760 --> 06:06:49,120 gil 10638 06:06:46,718 --> 06:06:50,240 with multi-processing we can run all of 10639 06:06:49,120 --> 06:06:53,200 these different tasks 10640 06:06:50,240 --> 06:06:53,920 together in parallel multi-processing is 10641 06:06:53,200 --> 06:06:56,958 better for 10642 06:06:53,920 --> 06:07:00,080 cpu bound tasks where a task has 10643 06:06:56,958 --> 06:07:02,798 heavy cpu usage and multi-threading 10644 06:07:00,080 --> 06:07:04,480 is better for i o bound tasks tasks that 10645 06:07:02,798 --> 06:07:06,558 involve a lot of waiting around 10646 06:07:04,480 --> 06:07:08,638 so that's multi-processing if you would 10647 06:07:06,558 --> 06:07:10,240 like a copy of all this code i will post 10648 06:07:08,638 --> 06:07:10,798 all of this to the comment section down 10649 06:07:10,240 --> 06:07:12,958 below 10650 06:07:10,798 --> 06:07:15,040 but yeah that is how multi-processing 10651 06:07:12,958 --> 06:07:17,040 works in python 10652 06:07:15,040 --> 06:07:18,558 hey what's going on everybody two bro 10653 06:07:17,040 --> 06:07:19,920 here hope you're doing well and in this 10654 06:07:18,558 --> 06:07:20,638 video we're going to be creating our 10655 06:07:19,920 --> 06:07:22,958 very first 10656 06:07:20,638 --> 06:07:26,718 graphical user interface in python so 10657 06:07:22,958 --> 06:07:26,718 sit back relax and enjoy the show 10658 06:07:27,040 --> 06:07:30,160 welcome one and all today we'll be 10659 06:07:28,638 --> 06:07:32,638 creating our very first 10660 06:07:30,160 --> 06:07:33,520 graphical user interface in python also 10661 06:07:32,638 --> 06:07:35,600 known as a 10662 06:07:33,520 --> 06:07:38,400 gui for short we're going to be using 10663 06:07:35,600 --> 06:07:40,080 the tk inter gui which is a module that 10664 06:07:38,400 --> 06:07:42,400 is included with python 10665 06:07:40,080 --> 06:07:44,400 our first step in order to use tk enter 10666 06:07:42,400 --> 06:07:45,520 is to import this module so that we can 10667 06:07:44,400 --> 06:07:48,878 begin using it 10668 06:07:45,520 --> 06:07:49,760 our first line of code will be from tk 10669 06:07:48,878 --> 06:07:52,320 enter 10670 06:07:49,760 --> 06:07:54,480 import asterisk this will import 10671 06:07:52,320 --> 06:07:55,440 everything related to the tk inter 10672 06:07:54,480 --> 06:07:57,360 module 10673 06:07:55,440 --> 06:07:59,520 and with this import we can now use all 10674 06:07:57,360 --> 06:08:00,400 of the gui features that this module has 10675 06:07:59,520 --> 06:08:02,160 to offer 10676 06:08:00,400 --> 06:08:04,080 now there is one important distinction 10677 06:08:02,160 --> 06:08:05,440 that we need to make we need to discuss 10678 06:08:04,080 --> 06:08:09,200 the differences between 10679 06:08:05,440 --> 06:08:11,040 windows and widgets here's a few quick 10680 06:08:09,200 --> 06:08:12,480 definitions that i definitely did not 10681 06:08:11,040 --> 06:08:15,680 copy from the internet 10682 06:08:12,480 --> 06:08:18,878 widgets are gui elements such as buttons 10683 06:08:15,680 --> 06:08:21,920 text boxes labels images you name it 10684 06:08:18,878 --> 06:08:24,878 and windows they serve as a container 10685 06:08:21,920 --> 06:08:26,558 to hold or contain these widgets for 10686 06:08:24,878 --> 06:08:28,480 this topic we'll be creating and 10687 06:08:26,558 --> 06:08:30,958 customizing our own window 10688 06:08:28,480 --> 06:08:32,638 and in future topics we'll be exploring 10689 06:08:30,958 --> 06:08:33,120 different widgets that are available to 10690 06:08:32,638 --> 06:08:35,280 us 10691 06:08:33,120 --> 06:08:37,520 so let's begin by creating a simple 10692 06:08:35,280 --> 06:08:38,480 window and we should give our window a 10693 06:08:37,520 --> 06:08:40,958 unique name 10694 06:08:38,480 --> 06:08:42,958 such as window that's pretty creative 10695 06:08:40,958 --> 06:08:43,920 and in order to instantiate this window 10696 06:08:42,958 --> 06:08:46,958 we're going to follow 10697 06:08:43,920 --> 06:08:48,480 this window name with equals tk make 10698 06:08:46,958 --> 06:08:49,120 sure you get the capitalization right 10699 06:08:48,480 --> 06:08:51,680 this is a 10700 06:08:49,120 --> 06:08:53,360 uppercase t and a set of parentheses to 10701 06:08:51,680 --> 06:08:54,718 serve as the constructor 10702 06:08:53,360 --> 06:08:57,920 what this will do and i'm just going to 10703 06:08:54,718 --> 06:09:02,080 add a comment is instantiate 10704 06:08:57,920 --> 06:09:04,320 an instance of a window for us 10705 06:09:02,080 --> 06:09:05,360 and when we compile and run this nothing 10706 06:09:04,320 --> 06:09:07,600 appears to happen 10707 06:09:05,360 --> 06:09:09,840 that's because we only instantiated our 10708 06:09:07,600 --> 06:09:10,160 window and we're not actually displaying 10709 06:09:09,840 --> 06:09:11,840 it 10710 06:09:10,160 --> 06:09:13,200 in order to display our window we're 10711 06:09:11,840 --> 06:09:16,638 going to follow this with 10712 06:09:13,200 --> 06:09:19,280 the name of the window dot main loop 10713 06:09:16,638 --> 06:09:20,320 and this will display our window and 10714 06:09:19,280 --> 06:09:22,400 this is our first 10715 06:09:20,320 --> 06:09:23,680 graphical user interface and we'll be 10716 06:09:22,400 --> 06:09:25,760 adding widgets 10717 06:09:23,680 --> 06:09:27,040 to this window so i'm going to add a 10718 06:09:25,760 --> 06:09:30,718 comment that 10719 06:09:27,040 --> 06:09:34,320 this will place window on 10720 06:09:30,718 --> 06:09:36,638 computer screen and it will also listen 10721 06:09:34,320 --> 06:09:37,600 for events but we'll get to that in 10722 06:09:36,638 --> 06:09:39,600 future videos 10723 06:09:37,600 --> 06:09:41,120 now what we'll be doing is customizing 10724 06:09:39,600 --> 06:09:43,520 the appearance of this window 10725 06:09:41,120 --> 06:09:46,000 let's begin by changing the size and we 10726 06:09:43,520 --> 06:09:48,958 can do that with the geometry function 10727 06:09:46,000 --> 06:09:51,280 so we're going to first begin by typing 10728 06:09:48,958 --> 06:09:53,440 the name of our window which is window 10729 06:09:51,280 --> 06:09:55,840 follow this by using the geometry 10730 06:09:53,440 --> 06:09:56,320 function and we pass in a width and a 10731 06:09:55,840 --> 06:09:58,400 height 10732 06:09:56,320 --> 06:09:59,840 but make sure this is within quotes and 10733 06:09:58,400 --> 06:10:00,558 set this to whatever width and height 10734 06:09:59,840 --> 06:10:03,440 that you want 10735 06:10:00,558 --> 06:10:04,558 so if i want 420 by 420 that would be 10736 06:10:03,440 --> 06:10:07,680 420 10737 06:10:04,558 --> 06:10:09,680 x 420 and the size of our window 10738 06:10:07,680 --> 06:10:11,280 is now the size that we set with the 10739 06:10:09,680 --> 06:10:13,200 geometry function 10740 06:10:11,280 --> 06:10:14,718 now take a look at the top left of this 10741 06:10:13,200 --> 06:10:17,840 window we have 10742 06:10:14,718 --> 06:10:18,638 this feather icon as well as a title of 10743 06:10:17,840 --> 06:10:21,040 tk 10744 06:10:18,638 --> 06:10:21,840 that's kind of lame so let's change that 10745 06:10:21,040 --> 06:10:24,320 let's first 10746 06:10:21,840 --> 06:10:25,760 change the window title and there is a 10747 06:10:24,320 --> 06:10:28,480 function to do that 10748 06:10:25,760 --> 06:10:29,520 so we type in the name of our window 10749 06:10:28,480 --> 06:10:33,200 window 10750 06:10:29,520 --> 06:10:35,200 dot title function and we can pass in a 10751 06:10:33,200 --> 06:10:38,878 new title that we want to set 10752 06:10:35,200 --> 06:10:42,558 and i will set the title to be maybe bro 10753 06:10:38,878 --> 06:10:46,558 code first gui 10754 06:10:42,558 --> 06:10:48,718 program that'll work and we now changed 10755 06:10:46,558 --> 06:10:51,840 the title of this window and it says 10756 06:10:48,718 --> 06:10:53,840 bro code first gui program 10757 06:10:51,840 --> 06:10:54,958 the next thing that we can do although 10758 06:10:53,840 --> 06:10:57,520 not necessary 10759 06:10:54,958 --> 06:10:58,878 is that we can change the icon of our 10760 06:10:57,520 --> 06:11:00,798 window bar at the top 10761 06:10:58,878 --> 06:11:03,040 i would like to replace this feather 10762 06:11:00,798 --> 06:11:05,200 icon with an image of my choosing 10763 06:11:03,040 --> 06:11:07,280 here's my desktop don't mind all of the 10764 06:11:05,200 --> 06:11:09,600 clutter and i would like to use this 10765 06:11:07,280 --> 06:11:10,878 image of the logo for my channel and set 10766 06:11:09,600 --> 06:11:13,680 this as the icon 10767 06:11:10,878 --> 06:11:15,840 for my window so what i'm going to do is 10768 06:11:13,680 --> 06:11:17,680 copy 10769 06:11:15,840 --> 06:11:19,600 and then go to my project folder and 10770 06:11:17,680 --> 06:11:22,878 then paste it 10771 06:11:19,600 --> 06:11:24,798 click ok and i now have this image 10772 06:11:22,878 --> 06:11:27,040 within my project folder 10773 06:11:24,798 --> 06:11:28,878 but it's in a format that we cannot use 10774 06:11:27,040 --> 06:11:30,718 currently we need to convert this to 10775 06:11:28,878 --> 06:11:34,000 what's called a photo image 10776 06:11:30,718 --> 06:11:36,480 that is a format that tk enter can use 10777 06:11:34,000 --> 06:11:37,600 so we'll need to convert our image to a 10778 06:11:36,480 --> 06:11:39,760 photo image 10779 06:11:37,600 --> 06:11:42,400 so do that anywhere within the window 10780 06:11:39,760 --> 06:11:42,878 between window equals tk and window.main 10781 06:11:42,400 --> 06:11:44,718 loop 10782 06:11:42,878 --> 06:11:46,320 right here is good i suppose we're going 10783 06:11:44,718 --> 06:11:48,718 to create a photo image 10784 06:11:46,320 --> 06:11:50,638 from this image and this is a png file 10785 06:11:48,718 --> 06:11:53,200 but yours might be something different 10786 06:11:50,638 --> 06:11:54,878 so let's give our photo image a unique 10787 06:11:53,200 --> 06:11:56,400 name like maybe icon 10788 06:11:54,878 --> 06:11:58,798 and in order to create a photo image 10789 06:11:56,400 --> 06:12:01,760 we'll follow this with equals 10790 06:11:58,798 --> 06:12:02,798 photo image and within the constructor 10791 06:12:01,760 --> 06:12:05,200 of our photo image 10792 06:12:02,798 --> 06:12:05,840 there is an option for file we can 10793 06:12:05,200 --> 06:12:08,000 either list 10794 06:12:05,840 --> 06:12:09,920 the file name or the file path if it's 10795 06:12:08,000 --> 06:12:12,160 somewhere else on your computer 10796 06:12:09,920 --> 06:12:14,160 since this image is within my same 10797 06:12:12,160 --> 06:12:15,120 project folder i only have to list the 10798 06:12:14,160 --> 06:12:18,320 file name 10799 06:12:15,120 --> 06:12:19,920 and this is called logo.png 10800 06:12:18,320 --> 06:12:21,440 and this is probably going to be named 10801 06:12:19,920 --> 06:12:24,000 something else for you though 10802 06:12:21,440 --> 06:12:24,638 so we now have this photo image called 10803 06:12:24,000 --> 06:12:27,520 icon 10804 06:12:24,638 --> 06:12:28,160 that we can use and i need to set the 10805 06:12:27,520 --> 06:12:31,360 icon 10806 06:12:28,160 --> 06:12:33,040 of my window to this icon and luckily 10807 06:12:31,360 --> 06:12:33,600 there is a function to take care of that 10808 06:12:33,040 --> 06:12:35,600 for us 10809 06:12:33,600 --> 06:12:36,718 type in the name of the window which is 10810 06:12:35,600 --> 06:12:39,680 window 10811 06:12:36,718 --> 06:12:40,400 dot and we're going to use the icon 10812 06:12:39,680 --> 06:12:43,600 photo 10813 06:12:40,400 --> 06:12:46,160 function there are two arguments the 10814 06:12:43,600 --> 06:12:48,240 first we're going to set to true 10815 06:12:46,160 --> 06:12:49,200 and the second is the photo image that 10816 06:12:48,240 --> 06:12:51,920 we want to use 10817 06:12:49,200 --> 06:12:53,120 and this photo image is called icon so 10818 06:12:51,920 --> 06:12:55,520 we're going to pass 10819 06:12:53,120 --> 06:12:56,240 this photo image as an argument to this 10820 06:12:55,520 --> 06:12:58,958 function 10821 06:12:56,240 --> 06:12:59,920 of icon photo and now when we compile 10822 06:12:58,958 --> 06:13:02,480 and run this 10823 06:12:59,920 --> 06:13:05,040 the image that we wanted to use is now 10824 06:13:02,480 --> 06:13:05,680 set for the top left icon of this window 10825 06:13:05,040 --> 06:13:07,680 bar 10826 06:13:05,680 --> 06:13:09,600 however it loses a lot of the quality 10827 06:13:07,680 --> 06:13:11,280 since the dimensions are so small 10828 06:13:09,600 --> 06:13:13,600 but you can still tell that it's my logo 10829 06:13:11,280 --> 06:13:15,760 though so that's how to replace 10830 06:13:13,600 --> 06:13:17,680 the icon of your window bar in the top 10831 06:13:15,760 --> 06:13:18,718 left corner now let's change the 10832 06:13:17,680 --> 06:13:20,718 background color 10833 06:13:18,718 --> 06:13:22,798 of this window and one way in which we 10834 06:13:20,718 --> 06:13:24,798 can do that is to use the config 10835 06:13:22,798 --> 06:13:26,240 function you can use the config function 10836 06:13:24,798 --> 06:13:27,680 anytime that you want to make any 10837 06:13:26,240 --> 06:13:29,360 changes to this window 10838 06:13:27,680 --> 06:13:31,760 so let's change the background color 10839 06:13:29,360 --> 06:13:34,160 here and there is an option called 10840 06:13:31,760 --> 06:13:34,798 background and we can set this equal to 10841 06:13:34,160 --> 06:13:37,760 either 10842 06:13:34,798 --> 06:13:39,760 a color name or a hexadecimal value 10843 06:13:37,760 --> 06:13:41,360 let's begin with just some color names 10844 06:13:39,760 --> 06:13:42,878 let's say that i want the background 10845 06:13:41,360 --> 06:13:44,638 color to be black 10846 06:13:42,878 --> 06:13:46,160 i'll just type in the word black here 10847 06:13:44,638 --> 06:13:48,638 and this will change the background 10848 06:13:46,160 --> 06:13:51,840 color of my window to black 10849 06:13:48,638 --> 06:13:52,798 alternatively you can set a hex value as 10850 06:13:51,840 --> 06:13:55,520 the color 10851 06:13:52,798 --> 06:13:56,480 so if you were to look up or google hex 10852 06:13:55,520 --> 06:13:58,558 color picker 10853 06:13:56,480 --> 06:13:59,840 you can select whatever color you want 10854 06:13:58,558 --> 06:14:03,280 let's say that i want 10855 06:13:59,840 --> 06:14:05,520 a sky blue color like this well this is 10856 06:14:03,280 --> 06:14:07,120 the hex value i'm going to copy this 10857 06:14:05,520 --> 06:14:09,520 value 10858 06:14:07,120 --> 06:14:10,480 and then paste it within the option for 10859 06:14:09,520 --> 06:14:12,480 my background 10860 06:14:10,480 --> 06:14:14,798 make sure you have this hashtag as well 10861 06:14:12,480 --> 06:14:17,760 because that represents that this is a 10862 06:14:14,798 --> 06:14:20,080 hex value so now the background color is 10863 06:14:17,760 --> 06:14:21,920 that sky blue color that i picked 10864 06:14:20,080 --> 06:14:24,400 well everybody that's the basics of 10865 06:14:21,920 --> 06:14:25,840 creating a window in python and in 10866 06:14:24,400 --> 06:14:26,558 future videos we're going to be 10867 06:14:25,840 --> 06:14:28,160 exploring 10868 06:14:26,558 --> 06:14:30,480 different widgets that we can add to 10869 06:14:28,160 --> 06:14:32,000 this window that have some functionality 10870 06:14:30,480 --> 06:14:33,600 so if you would like a copy of all this 10871 06:14:32,000 --> 06:14:34,718 code i'll post all of this in the 10872 06:14:33,600 --> 06:14:36,480 comments down below 10873 06:14:34,718 --> 06:14:38,958 but yeah that's how to create your very 10874 06:14:36,480 --> 06:14:40,558 first gui in python 10875 06:14:38,958 --> 06:14:42,320 hey what's going on everybody it's your 10876 06:14:40,558 --> 06:14:43,520 bro here hope you're doing well and in 10877 06:14:42,320 --> 06:14:44,400 this video i'm going to teach you guys 10878 06:14:43,520 --> 06:14:47,040 how we can create 10879 06:14:44,400 --> 06:14:49,760 labels in python so sit back relax and 10880 06:14:47,040 --> 06:14:49,760 enjoy the show 10881 06:14:49,840 --> 06:14:52,958 what is a label well i'm glad you asked 10882 06:14:52,160 --> 06:14:55,920 that question 10883 06:14:52,958 --> 06:14:56,400 it's an area widget that holds text and 10884 06:14:55,920 --> 06:14:58,958 or 10885 06:14:56,400 --> 06:15:00,958 an image within a window so to begin 10886 06:14:58,958 --> 06:15:02,000 we'll need to create a window to act as 10887 06:15:00,958 --> 06:15:04,160 a container 10888 06:15:02,000 --> 06:15:05,280 to hold this label we can create a 10889 06:15:04,160 --> 06:15:07,480 window with 10890 06:15:05,280 --> 06:15:08,718 window equals tk followed by 10891 06:15:07,480 --> 06:15:10,878 window.mainloop 10892 06:15:08,718 --> 06:15:12,638 and this will give us a window in which 10893 06:15:10,878 --> 06:15:15,120 we can add a label to 10894 06:15:12,638 --> 06:15:16,480 now let's give our label a unique name 10895 06:15:15,120 --> 06:15:18,558 such as label 10896 06:15:16,480 --> 06:15:20,000 and in order to instantiate a label 10897 06:15:18,558 --> 06:15:23,200 we'll follow this label name 10898 06:15:20,000 --> 06:15:24,638 with equals label with a capital l and a 10899 06:15:23,200 --> 06:15:26,480 set of parentheses 10900 06:15:24,638 --> 06:15:28,240 the parentheses are acting as the 10901 06:15:26,480 --> 06:15:30,798 constructor for this widget 10902 06:15:28,240 --> 06:15:31,440 our label and we can pass in arguments 10903 06:15:30,798 --> 06:15:33,520 into 10904 06:15:31,440 --> 06:15:34,480 these parentheses the first argument 10905 06:15:33,520 --> 06:15:37,760 that we're going to pass 10906 06:15:34,480 --> 06:15:38,240 in is a master the container for this 10907 06:15:37,760 --> 06:15:40,000 label 10908 06:15:38,240 --> 06:15:42,080 we're going to pass in the name of our 10909 06:15:40,000 --> 06:15:43,440 window because our window is acting as 10910 06:15:42,080 --> 06:15:45,440 the container 10911 06:15:43,440 --> 06:15:47,360 with widgets in python we can 10912 06:15:45,440 --> 06:15:48,080 de-eliminate the arguments that we're 10913 06:15:47,360 --> 06:15:50,958 passing in 10914 06:15:48,080 --> 06:15:52,160 with a comma and there are options 10915 06:15:50,958 --> 06:15:54,558 available to us 10916 06:15:52,160 --> 06:15:56,798 options are keyword arguments that we 10917 06:15:54,558 --> 06:15:57,600 can pass in to the constructor for this 10918 06:15:56,798 --> 06:16:00,400 widget 10919 06:15:57,600 --> 06:16:02,638 one option available to us is text and 10920 06:16:00,400 --> 06:16:02,958 we can set the text of this label equal 10921 06:16:02,638 --> 06:16:05,440 to 10922 06:16:02,958 --> 06:16:07,120 a string that we set so let's say that 10923 06:16:05,440 --> 06:16:09,600 we want this label to say 10924 06:16:07,120 --> 06:16:10,878 hello world well we're going to pass in 10925 06:16:09,600 --> 06:16:14,160 this option 10926 06:16:10,878 --> 06:16:16,400 text and it's a keyword argument 10927 06:16:14,160 --> 06:16:17,280 so if we were to do this we're going to 10928 06:16:16,400 --> 06:16:20,320 set the text 10929 06:16:17,280 --> 06:16:21,920 of the label but this label currently 10930 06:16:20,320 --> 06:16:23,520 doesn't appear within our window 10931 06:16:21,920 --> 06:16:25,840 what gives bro i thought you knew how to 10932 06:16:23,520 --> 06:16:27,200 code hold up wait a minute there's one 10933 06:16:25,840 --> 06:16:29,520 more thing that we need to do 10934 06:16:27,200 --> 06:16:30,320 so we're going to actually add this 10935 06:16:29,520 --> 06:16:33,600 label 10936 06:16:30,320 --> 06:16:35,760 to the window now we set the master 10937 06:16:33,600 --> 06:16:38,160 of the label to be our window but now we 10938 06:16:35,760 --> 06:16:40,878 actually have to add it as step two 10939 06:16:38,160 --> 06:16:41,360 one way which we can do that is to use a 10940 06:16:40,878 --> 06:16:44,320 pack 10941 06:16:41,360 --> 06:16:46,080 function of our label so follow label 10942 06:16:44,320 --> 06:16:46,798 equals label and pass in all of your 10943 06:16:46,080 --> 06:16:48,558 arguments 10944 06:16:46,798 --> 06:16:51,280 and then follow this with the name of 10945 06:16:48,558 --> 06:16:53,920 the label dot pack 10946 06:16:51,280 --> 06:16:55,760 so with the pack option by default this 10947 06:16:53,920 --> 06:16:58,718 will place our widget in the top 10948 06:16:55,760 --> 06:17:00,240 center of our window another way in 10949 06:16:58,718 --> 06:17:02,958 which we can add a label 10950 06:17:00,240 --> 06:17:03,920 to a container or window is to use the 10951 06:17:02,958 --> 06:17:05,600 place function 10952 06:17:03,920 --> 06:17:08,240 and we can set some coordinates of where 10953 06:17:05,600 --> 06:17:08,798 we want this label or other widget to 10954 06:17:08,240 --> 06:17:10,638 appear 10955 06:17:08,798 --> 06:17:12,480 within the window if we want this to 10956 06:17:10,638 --> 06:17:15,520 appear in the top left corner 10957 06:17:12,480 --> 06:17:17,200 we can set x to equal zero and y t 10958 06:17:15,520 --> 06:17:18,958 equals zero and these are options 10959 06:17:17,200 --> 06:17:22,240 available to us too that we can pass 10960 06:17:18,958 --> 06:17:25,840 in so this place function will place 10961 06:17:22,240 --> 06:17:27,600 our label at some position or 10962 06:17:25,840 --> 06:17:28,958 some set of coordinates within the 10963 06:17:27,600 --> 06:17:31,440 window and with 10964 06:17:28,958 --> 06:17:33,680 x equals 0 and y equals 0 that will 10965 06:17:31,440 --> 06:17:34,558 place our widget in the top left corner 10966 06:17:33,680 --> 06:17:36,400 of the window 10967 06:17:34,558 --> 06:17:38,240 and if i were to change these to let's 10968 06:17:36,400 --> 06:17:40,878 say x equals 100 10969 06:17:38,240 --> 06:17:42,240 and y equals 100 well this is going to 10970 06:17:40,878 --> 06:17:45,280 be placed 10971 06:17:42,240 --> 06:17:46,480 down by 100 pixels and to the right by 10972 06:17:45,280 --> 06:17:48,878 100 pixels 10973 06:17:46,480 --> 06:17:49,600 so you can use pack or place there's a 10974 06:17:48,878 --> 06:17:51,200 few others 10975 06:17:49,600 --> 06:17:53,120 like grid but they're a little more 10976 06:17:51,200 --> 06:17:53,600 complex so we'll get to that in a future 10977 06:17:53,120 --> 06:17:55,600 video 10978 06:17:53,600 --> 06:17:57,200 for the time being i'm going to use the 10979 06:17:55,600 --> 06:17:59,360 pack function of the label 10980 06:17:57,200 --> 06:18:01,280 and turn this place function into a 10981 06:17:59,360 --> 06:18:04,878 comment 10982 06:18:01,280 --> 06:18:07,040 dot pack all right now 10983 06:18:04,878 --> 06:18:08,718 how can we customize this label we can 10984 06:18:07,040 --> 06:18:10,958 pass in different options 10985 06:18:08,718 --> 06:18:12,638 when we instantiate this label let's 10986 06:18:10,958 --> 06:18:13,360 change the font and that's another 10987 06:18:12,638 --> 06:18:16,638 option 10988 06:18:13,360 --> 06:18:18,958 we can change the font with font equals 10989 06:18:16,638 --> 06:18:19,760 and then we can pass in a font let's 10990 06:18:18,958 --> 06:18:22,958 begin with a 10991 06:18:19,760 --> 06:18:26,958 font family i will say maybe ariel 10992 06:18:22,958 --> 06:18:28,080 a size perhaps 40 and then a style if 10993 06:18:26,958 --> 06:18:30,558 you so choose 10994 06:18:28,080 --> 06:18:31,440 like bold there's also italic and 10995 06:18:30,558 --> 06:18:33,520 underline 10996 06:18:31,440 --> 06:18:35,520 uh but this will work just fine then so 10997 06:18:33,520 --> 06:18:38,000 this will change the font 10998 06:18:35,520 --> 06:18:39,760 of the text of our label and it says 10999 06:18:38,000 --> 06:18:43,280 hello world still 11000 06:18:39,760 --> 06:18:46,320 now let's change the foreground color 11001 06:18:43,280 --> 06:18:47,680 so that is another option the foreground 11002 06:18:46,320 --> 06:18:50,798 is the font color 11003 06:18:47,680 --> 06:18:51,920 and the option is fg for foreground it's 11004 06:18:50,798 --> 06:18:54,718 short for foreground 11005 06:18:51,920 --> 06:18:55,440 foreground equals we can state a color 11006 06:18:54,718 --> 06:18:58,478 name 11007 06:18:55,440 --> 06:19:00,478 such as green and our text 11008 06:18:58,478 --> 06:19:02,160 should now be green you can select 11009 06:19:00,478 --> 06:19:05,840 something more specific by 11010 06:19:02,160 --> 06:19:06,558 passing in a hex value so a custom color 11011 06:19:05,840 --> 06:19:10,478 that i like 11012 06:19:06,558 --> 06:19:12,478 is hashtag zero zero ff00 11013 06:19:10,478 --> 06:19:14,400 this is bright green you can always look 11014 06:19:12,478 --> 06:19:16,000 up what different hex values are 11015 06:19:14,400 --> 06:19:17,520 and we can also change the background 11016 06:19:16,000 --> 06:19:20,400 color too 11017 06:19:17,520 --> 06:19:22,240 that is bg short for background color 11018 06:19:20,400 --> 06:19:24,718 and we can pass in a color name 11019 06:19:22,240 --> 06:19:26,558 or a hex value i'm just going to set 11020 06:19:24,718 --> 06:19:27,920 this to black just to demonstrate that 11021 06:19:26,558 --> 06:19:30,798 we can pass in both 11022 06:19:27,920 --> 06:19:31,520 either hex values or color names and 11023 06:19:30,798 --> 06:19:34,558 what we have 11024 06:19:31,520 --> 06:19:37,200 is green text on a black background 11025 06:19:34,558 --> 06:19:38,240 and this is the limits of our label 11026 06:19:37,200 --> 06:19:41,040 within the window 11027 06:19:38,240 --> 06:19:42,718 because when we expand this window the 11028 06:19:41,040 --> 06:19:43,360 label is still going to stay the same 11029 06:19:42,718 --> 06:19:46,000 size 11030 06:19:43,360 --> 06:19:46,878 if we were to change the text within the 11031 06:19:46,000 --> 06:19:49,040 label 11032 06:19:46,878 --> 06:19:50,240 our label is only going to take up the 11033 06:19:49,040 --> 06:19:52,160 room that it needs 11034 06:19:50,240 --> 06:19:54,080 and you can see that our label is now 11035 06:19:52,160 --> 06:19:55,840 smaller now we have all of these 11036 06:19:54,080 --> 06:19:57,360 arguments within our constructor and 11037 06:19:55,840 --> 06:19:58,240 it's getting a little bit difficult to 11038 06:19:57,360 --> 06:20:00,400 actually read 11039 06:19:58,240 --> 06:20:02,478 so i'm going to separate each of these 11040 06:20:00,400 --> 06:20:03,600 arguments line by line just so that this 11041 06:20:02,478 --> 06:20:05,280 is more organized 11042 06:20:03,600 --> 06:20:07,680 but it'll work just the same as it did 11043 06:20:05,280 --> 06:20:10,160 before now how do we add a 11044 06:20:07,680 --> 06:20:11,280 border around our label one we can set 11045 06:20:10,160 --> 06:20:14,160 the border style 11046 06:20:11,280 --> 06:20:14,798 with the relief option and one border 11047 06:20:14,160 --> 06:20:18,878 that i like 11048 06:20:14,798 --> 06:20:21,120 is raised but right now the border width 11049 06:20:18,878 --> 06:20:22,638 is one so it's a little difficult for us 11050 06:20:21,120 --> 06:20:24,718 to actually see the border 11051 06:20:22,638 --> 06:20:26,160 let's increase the border width with the 11052 06:20:24,718 --> 06:20:29,680 option bd 11053 06:20:26,160 --> 06:20:32,638 and i'll set this equal to maybe 10. 11054 06:20:29,680 --> 06:20:33,920 so this border is now more visible 11055 06:20:32,638 --> 06:20:36,798 another relief 11056 06:20:33,920 --> 06:20:38,558 which is the border style is sunken and 11057 06:20:36,798 --> 06:20:40,400 this will give our label a 11058 06:20:38,558 --> 06:20:42,320 sunken appearance but i think i'll 11059 06:20:40,400 --> 06:20:45,040 change this back to raised 11060 06:20:42,320 --> 06:20:45,520 we can also add some padding around the 11061 06:20:45,040 --> 06:20:47,680 text 11062 06:20:45,520 --> 06:20:49,440 between the text and the border we can 11063 06:20:47,680 --> 06:20:52,320 pad x and pad y 11064 06:20:49,440 --> 06:20:53,200 pad x will add some padding some space 11065 06:20:52,320 --> 06:20:55,360 between 11066 06:20:53,200 --> 06:20:57,840 the x-axis of our text between the 11067 06:20:55,360 --> 06:21:00,878 border so let's set this to maybe 20 11068 06:20:57,840 --> 06:21:01,360 and we now have 20 pixels worth of space 11069 06:21:00,878 --> 06:21:06,320 between 11070 06:21:01,360 --> 06:21:06,320 the text and the border let's also pad y 11071 06:21:06,958 --> 06:21:13,680 this will add some space above and below 11072 06:21:10,320 --> 06:21:16,400 our text between the text and the border 11073 06:21:13,680 --> 06:21:18,000 now how do we add an image to a label we 11074 06:21:16,400 --> 06:21:21,120 first need to create what's called 11075 06:21:18,000 --> 06:21:21,760 a photo image and we will create this 11076 06:21:21,120 --> 06:21:24,400 within 11077 06:21:21,760 --> 06:21:25,680 the lines of window equals tk and 11078 06:21:24,400 --> 06:21:27,840 window.main loop 11079 06:21:25,680 --> 06:21:28,878 and let's give this photo image a unique 11080 06:21:27,840 --> 06:21:31,280 name like 11081 06:21:28,878 --> 06:21:33,040 photo and in order to create a photo 11082 06:21:31,280 --> 06:21:36,558 image we're going to follow this with 11083 06:21:33,040 --> 06:21:38,478 photo equals photo image 11084 06:21:36,558 --> 06:21:40,400 and within the constructor of our photo 11085 06:21:38,478 --> 06:21:43,120 image there's an option called 11086 06:21:40,400 --> 06:21:44,080 file and we can list a file path or a 11087 06:21:43,120 --> 06:21:46,558 file name 11088 06:21:44,080 --> 06:21:48,320 right now on my desktop there is a image 11089 06:21:46,558 --> 06:21:50,080 that i want to use and i need to convert 11090 06:21:48,320 --> 06:21:52,320 this to a photo image 11091 06:21:50,080 --> 06:21:54,558 here's my desktop and this is an image 11092 06:21:52,320 --> 06:21:56,638 that i want to use to add to my label 11093 06:21:54,558 --> 06:21:58,798 i need to get the file path and i can 11094 06:21:56,638 --> 06:22:01,840 get that by going to properties 11095 06:21:58,798 --> 06:22:03,840 then underneath location i can copy this 11096 06:22:01,840 --> 06:22:07,520 location address 11097 06:22:03,840 --> 06:22:09,840 and paste it within the quotes for file 11098 06:22:07,520 --> 06:22:12,320 and then i need to add the image name 11099 06:22:09,840 --> 06:22:14,400 and this is person.png 11100 06:22:12,320 --> 06:22:16,878 and with these backslashes these need to 11101 06:22:14,400 --> 06:22:18,718 be double backslashes because 11102 06:22:16,878 --> 06:22:20,558 that is the escape character for a 11103 06:22:18,718 --> 06:22:23,360 backslash 11104 06:22:20,558 --> 06:22:23,840 but what would be a better option is to 11105 06:22:23,360 --> 06:22:25,840 add 11106 06:22:23,840 --> 06:22:27,440 this image to my project folder that 11107 06:22:25,840 --> 06:22:28,400 contains the python file that i'm 11108 06:22:27,440 --> 06:22:31,280 working with 11109 06:22:28,400 --> 06:22:31,840 so in place of listing the entire file 11110 06:22:31,280 --> 06:22:33,760 path 11111 06:22:31,840 --> 06:22:36,240 i'm going to copy the image that i want 11112 06:22:33,760 --> 06:22:40,400 to use go to my project folder 11113 06:22:36,240 --> 06:22:42,878 and then paste it and i no longer need 11114 06:22:40,400 --> 06:22:44,798 this entire file path i can just list 11115 06:22:42,878 --> 06:22:46,958 the image name that i have 11116 06:22:44,798 --> 06:22:48,958 and now what we need to do is add this 11117 06:22:46,958 --> 06:22:49,680 photo to the label and there's an option 11118 06:22:48,958 --> 06:22:52,000 to do so 11119 06:22:49,680 --> 06:22:53,440 that is the image option and we'll place 11120 06:22:52,000 --> 06:22:54,000 this within the constructor for the 11121 06:22:53,440 --> 06:22:56,558 label 11122 06:22:54,000 --> 06:22:58,080 we're going to set image equal to the 11123 06:22:56,558 --> 06:23:00,798 photo image that we have 11124 06:22:58,080 --> 06:23:02,478 and our photo image is called photo this 11125 06:23:00,798 --> 06:23:04,638 will add our photo image 11126 06:23:02,478 --> 06:23:06,718 to our label or other widget depending 11127 06:23:04,638 --> 06:23:09,280 on what we're working with 11128 06:23:06,718 --> 06:23:10,080 so we have our image but it replaced all 11129 06:23:09,280 --> 06:23:12,478 of the text 11130 06:23:10,080 --> 06:23:13,280 that we had for this label how do we add 11131 06:23:12,478 --> 06:23:16,160 both 11132 06:23:13,280 --> 06:23:16,958 text and an image to a window well we 11133 06:23:16,160 --> 06:23:19,840 can follow this 11134 06:23:16,958 --> 06:23:21,040 with the compound option we can set a 11135 06:23:19,840 --> 06:23:23,680 direction of where 11136 06:23:21,040 --> 06:23:25,520 we want this image to be placed relative 11137 06:23:23,680 --> 06:23:28,000 to the text that we have 11138 06:23:25,520 --> 06:23:28,558 so i'm going to set compound equal to 11139 06:23:28,000 --> 06:23:31,040 bottom 11140 06:23:28,558 --> 06:23:31,920 so that the image appears on the bottom 11141 06:23:31,040 --> 06:23:35,200 or underneath 11142 06:23:31,920 --> 06:23:35,920 our text so we can both have text and an 11143 06:23:35,200 --> 06:23:37,600 image 11144 06:23:35,920 --> 06:23:39,760 or if i want this to be in a different 11145 06:23:37,600 --> 06:23:41,920 location i could set this to maybe top 11146 06:23:39,760 --> 06:23:42,958 left or right if i were to set this to 11147 06:23:41,920 --> 06:23:46,400 top we have 11148 06:23:42,958 --> 06:23:48,400 our image on top of our text and one 11149 06:23:46,400 --> 06:23:50,240 thing to know about labels and many 11150 06:23:48,400 --> 06:23:52,638 other widgets in python is that 11151 06:23:50,240 --> 06:23:54,320 the size of the widget will actually 11152 06:23:52,638 --> 06:23:56,160 increase to accommodate 11153 06:23:54,320 --> 06:23:58,558 the size of all the components that's 11154 06:23:56,160 --> 06:24:01,440 contained within if i were to give the 11155 06:23:58,558 --> 06:24:02,160 text a longer string of characters such 11156 06:24:01,440 --> 06:24:05,440 as 11157 06:24:02,160 --> 06:24:07,680 bro do you even code then 11158 06:24:05,440 --> 06:24:08,798 the width of our label is actually going 11159 06:24:07,680 --> 06:24:11,280 to expand 11160 06:24:08,798 --> 06:24:13,280 to accommodate the size of the longer 11161 06:24:11,280 --> 06:24:15,680 string of text that we have 11162 06:24:13,280 --> 06:24:17,040 so that's the basics of labels everyone 11163 06:24:15,680 --> 06:24:18,798 if you would like a copy of all this 11164 06:24:17,040 --> 06:24:20,080 code i will post all of this in the 11165 06:24:18,798 --> 06:24:22,558 comments down below 11166 06:24:20,080 --> 06:24:24,400 but yeah that's the basics of labels in 11167 06:24:22,558 --> 06:24:26,000 python 11168 06:24:24,400 --> 06:24:27,600 hey what's going on everybody it's your 11169 06:24:26,000 --> 06:24:28,958 bro here hope you're doing well 11170 06:24:27,600 --> 06:24:31,200 and in this video i'm going to teach you 11171 06:24:28,958 --> 06:24:35,840 guys how we can create buttons in python 11172 06:24:31,200 --> 06:24:35,840 so sit back relax and enjoy the show 11173 06:24:35,920 --> 06:24:39,920 buttons you click them and then they do 11174 06:24:38,160 --> 06:24:41,680 stuff that's pretty much it 11175 06:24:39,920 --> 06:24:43,280 so this is how to create a button in 11176 06:24:41,680 --> 06:24:46,000 python we'll need a window 11177 06:24:43,280 --> 06:24:46,638 window equals tk followed by window.main 11178 06:24:46,000 --> 06:24:49,360 loop 11179 06:24:46,638 --> 06:24:51,120 let's call our button just button and in 11180 06:24:49,360 --> 06:24:51,680 order to create this we'll follow this 11181 06:24:51,120 --> 06:24:55,280 with 11182 06:24:51,680 --> 06:24:56,798 equals button and within the constructor 11183 06:24:55,280 --> 06:24:59,200 for this button we're going to list 11184 06:24:56,798 --> 06:25:00,400 what we want to add this button to what 11185 06:24:59,200 --> 06:25:03,440 is the master 11186 06:25:00,400 --> 06:25:04,798 the master is the window and in order to 11187 06:25:03,440 --> 06:25:07,280 display this button 11188 06:25:04,798 --> 06:25:07,840 we're going to follow this with button 11189 06:25:07,280 --> 06:25:11,440 dot 11190 06:25:07,840 --> 06:25:13,840 pack and we should have a small 11191 06:25:11,440 --> 06:25:16,320 rectangular button within our window 11192 06:25:13,840 --> 06:25:17,280 but it doesn't display any text nor does 11193 06:25:16,320 --> 06:25:19,280 it do anything 11194 06:25:17,280 --> 06:25:20,400 so let's add some text and there is an 11195 06:25:19,280 --> 06:25:22,240 option to do that 11196 06:25:20,400 --> 06:25:23,840 within the constructor we're going to 11197 06:25:22,240 --> 06:25:25,600 list the option for text 11198 06:25:23,840 --> 06:25:26,958 and set this to whatever text you want 11199 06:25:25,600 --> 06:25:30,000 to display let's 11200 06:25:26,958 --> 06:25:32,400 say click me and then when we run 11201 06:25:30,000 --> 06:25:33,040 this our button now says the text that 11202 06:25:32,400 --> 06:25:35,840 we set 11203 06:25:33,040 --> 06:25:37,200 and it says click me however it 11204 06:25:35,840 --> 06:25:39,600 currently doesn't do anything though 11205 06:25:37,200 --> 06:25:40,400 we need to set a command that is another 11206 06:25:39,600 --> 06:25:43,520 option 11207 06:25:40,400 --> 06:25:46,240 command equals and we list a function 11208 06:25:43,520 --> 06:25:47,840 name this is what's known as a callback 11209 06:25:46,240 --> 06:25:50,000 so let's create a function 11210 06:25:47,840 --> 06:25:51,920 called click and then outside of the 11211 06:25:50,000 --> 06:25:55,040 window let's define what our click 11212 06:25:51,920 --> 06:25:58,638 function does def click 11213 06:25:55,040 --> 06:26:02,478 and let's print a message you 11214 06:25:58,638 --> 06:26:05,200 clicked the button 11215 06:26:02,478 --> 06:26:05,920 now with the command we're going to list 11216 06:26:05,200 --> 06:26:08,638 the 11217 06:26:05,920 --> 06:26:10,160 function name without the parentheses so 11218 06:26:08,638 --> 06:26:12,718 make sure you're writing it as 11219 06:26:10,160 --> 06:26:14,160 this and not that this is what's known 11220 06:26:12,718 --> 06:26:16,718 as a callback 11221 06:26:14,160 --> 06:26:19,280 so when we click on this button it's 11222 06:26:16,718 --> 06:26:21,680 going to perform our callback 11223 06:26:19,280 --> 06:26:22,320 and perform whatever is within our 11224 06:26:21,680 --> 06:26:25,680 function 11225 06:26:22,320 --> 06:26:27,360 click just like that let's customize the 11226 06:26:25,680 --> 06:26:28,240 appearance of this button i'm going to 11227 06:26:27,360 --> 06:26:31,600 change the font 11228 06:26:28,240 --> 06:26:34,478 that is another option font equals 11229 06:26:31,600 --> 06:26:35,360 and let's pick a professional font such 11230 06:26:34,478 --> 06:26:38,798 as comic 11231 06:26:35,360 --> 06:26:41,120 sans it's very professional and i'll set 11232 06:26:38,798 --> 06:26:43,440 a size of 30. 11233 06:26:41,120 --> 06:26:46,160 and now you can actually read what's on 11234 06:26:43,440 --> 06:26:48,000 this button it's large enough 11235 06:26:46,160 --> 06:26:49,920 let's set the foreground color that is 11236 06:26:48,000 --> 06:26:52,400 the font color fg 11237 06:26:49,920 --> 06:26:53,360 you can place a color name or a hex 11238 06:26:52,400 --> 06:26:57,520 value here 11239 06:26:53,360 --> 06:27:00,320 i'll pick green that is 0 0 ff00 11240 06:26:57,520 --> 06:27:01,600 and our text should now be green which 11241 06:27:00,320 --> 06:27:04,240 it is 11242 06:27:01,600 --> 06:27:05,600 let's also change the background color 11243 06:27:04,240 --> 06:27:08,638 that is bg 11244 06:27:05,600 --> 06:27:09,600 i will pick the color name of black and 11245 06:27:08,638 --> 06:27:12,320 now we have 11246 06:27:09,600 --> 06:27:14,000 green text on a black background notice 11247 06:27:12,320 --> 06:27:16,958 that when i click on this button 11248 06:27:14,000 --> 06:27:18,798 or hold my mouse button down the color 11249 06:27:16,958 --> 06:27:21,200 scheme has changed that's because 11250 06:27:18,798 --> 06:27:22,718 there is a different active foreground 11251 06:27:21,200 --> 06:27:24,958 and active background 11252 06:27:22,718 --> 06:27:26,638 and we can change those too so that this 11253 06:27:24,958 --> 06:27:27,360 won't flash every time you click on the 11254 06:27:26,638 --> 06:27:29,360 button 11255 06:27:27,360 --> 06:27:30,958 so let's change the active foreground 11256 06:27:29,360 --> 06:27:33,200 and active background 11257 06:27:30,958 --> 06:27:34,558 those are other options active 11258 06:27:33,200 --> 06:27:36,558 foreground 11259 06:27:34,558 --> 06:27:38,320 and i'll set this to the same color as 11260 06:27:36,558 --> 06:27:41,280 my foreground 11261 06:27:38,320 --> 06:27:42,320 which is green so the text color is 11262 06:27:41,280 --> 06:27:44,160 going to stay the same 11263 06:27:42,320 --> 06:27:46,718 and i'll also change the active 11264 06:27:44,160 --> 06:27:46,718 background 11265 06:27:47,120 --> 06:27:50,400 and i will also set this to black to 11266 06:27:49,600 --> 06:27:53,600 match my 11267 06:27:50,400 --> 06:27:54,400 background and then this should no 11268 06:27:53,600 --> 06:27:57,520 longer flash 11269 06:27:54,400 --> 06:27:59,520 when we click on our button 11270 06:27:57,520 --> 06:28:00,638 if you need to disable somebody from 11271 06:27:59,520 --> 06:28:02,718 clicking on this button 11272 06:28:00,638 --> 06:28:04,638 there is an option to do so and that 11273 06:28:02,718 --> 06:28:07,280 option is the state 11274 06:28:04,638 --> 06:28:08,558 option and normally this is active but 11275 06:28:07,280 --> 06:28:11,120 you can set this to 11276 06:28:08,558 --> 06:28:12,558 disabled and we can no longer click on 11277 06:28:11,120 --> 06:28:14,080 this button 11278 06:28:12,558 --> 06:28:16,240 and you can see that the color scheme 11279 06:28:14,080 --> 06:28:17,200 has changed too it's all grayed out and 11280 06:28:16,240 --> 06:28:19,360 whatnot 11281 06:28:17,200 --> 06:28:21,200 so i'm going to replace this back with 11282 06:28:19,360 --> 06:28:22,160 active because we still have a few more 11283 06:28:21,200 --> 06:28:24,718 things to do 11284 06:28:22,160 --> 06:28:25,360 now how do we add an image to a button 11285 06:28:24,718 --> 06:28:28,000 there is an 11286 06:28:25,360 --> 06:28:28,958 image option and we set this equal to a 11287 06:28:28,000 --> 06:28:30,798 photo image 11288 06:28:28,958 --> 06:28:32,958 but first we need to create a photo 11289 06:28:30,798 --> 06:28:34,878 image i have this photo of 11290 06:28:32,958 --> 06:28:37,440 a like button a thumbs up that i want to 11291 06:28:34,878 --> 06:28:38,638 use i need to create a photo image from 11292 06:28:37,440 --> 06:28:40,718 this image file 11293 06:28:38,638 --> 06:28:42,160 so within the window let's call this 11294 06:28:40,718 --> 06:28:46,080 photo image just 11295 06:28:42,160 --> 06:28:49,280 photo photo equals 11296 06:28:46,080 --> 06:28:51,360 photo image then i'm going to list the 11297 06:28:49,280 --> 06:28:53,600 file path or the file name 11298 06:28:51,360 --> 06:28:54,638 since this image is within my same 11299 06:28:53,600 --> 06:28:57,280 project folder 11300 06:28:54,638 --> 06:28:58,120 i only have to list the file name and 11301 06:28:57,280 --> 06:29:00,798 this is called 11302 06:28:58,120 --> 06:29:01,840 like.png but if you use your own image 11303 06:29:00,798 --> 06:29:02,878 it's probably going to be named 11304 06:29:01,840 --> 06:29:05,360 something else 11305 06:29:02,878 --> 06:29:07,040 then i need to set the image equal to my 11306 06:29:05,360 --> 06:29:11,760 photo image which i named 11307 06:29:07,040 --> 06:29:14,080 photo but the photo replaces the text on 11308 06:29:11,760 --> 06:29:16,400 this button but it still works the same 11309 06:29:14,080 --> 06:29:17,600 so we would like to display some text as 11310 06:29:16,400 --> 06:29:21,360 well as our image 11311 06:29:17,600 --> 06:29:24,718 we need to use the compound option 11312 06:29:21,360 --> 06:29:27,840 and we list a direction we can list 11313 06:29:24,718 --> 06:29:28,320 top bottom left right we want this image 11314 06:29:27,840 --> 06:29:31,520 to be 11315 06:29:28,320 --> 06:29:32,080 on the perhaps bottom so let's list the 11316 06:29:31,520 --> 06:29:34,160 bottom 11317 06:29:32,080 --> 06:29:36,000 and now when we run this we have our 11318 06:29:34,160 --> 06:29:37,840 text as well as our image 11319 06:29:36,000 --> 06:29:39,040 and if we changed compound to a 11320 06:29:37,840 --> 06:29:41,600 different direction like 11321 06:29:39,040 --> 06:29:42,240 top then the image is going to be 11322 06:29:41,600 --> 06:29:45,920 displayed 11323 06:29:42,240 --> 06:29:47,920 on the top relative to the text 11324 06:29:45,920 --> 06:29:49,840 what if we wanted to count the amount of 11325 06:29:47,920 --> 06:29:52,080 times that we clicked this button 11326 06:29:49,840 --> 06:29:53,520 we could create a variable called count 11327 06:29:52,080 --> 06:29:56,718 and we'll set this equal to 11328 06:29:53,520 --> 06:29:58,080 zero but in order for us to access this 11329 06:29:56,718 --> 06:30:01,520 count variable 11330 06:29:58,080 --> 06:30:02,478 within the function of click we're going 11331 06:30:01,520 --> 06:30:06,000 to list 11332 06:30:02,478 --> 06:30:06,878 count as global so that this is a global 11333 06:30:06,000 --> 06:30:09,200 variable 11334 06:30:06,878 --> 06:30:10,958 every time we click this button let's 11335 06:30:09,200 --> 06:30:14,400 increment our variable count 11336 06:30:10,958 --> 06:30:16,400 by one by saying count plus equals one 11337 06:30:14,400 --> 06:30:18,160 and then we will print whatever the 11338 06:30:16,400 --> 06:30:20,400 value of count is 11339 06:30:18,160 --> 06:30:21,760 so within a print statement we'll just 11340 06:30:20,400 --> 06:30:23,760 say count 11341 06:30:21,760 --> 06:30:24,798 now when we click on this button we have 11342 06:30:23,760 --> 06:30:27,040 access to our count 11343 06:30:24,798 --> 06:30:28,798 variable that's outside of this function 11344 06:30:27,040 --> 06:30:31,200 and every time we click on this button 11345 06:30:28,798 --> 06:30:32,080 it's going to increase our count 11346 06:30:31,200 --> 06:30:34,878 variable by 11347 06:30:32,080 --> 06:30:35,600 one well everybody that's the basics of 11348 06:30:34,878 --> 06:30:37,680 creating 11349 06:30:35,600 --> 06:30:39,680 buttons in python if you want a copy of 11350 06:30:37,680 --> 06:30:40,878 all this code i'll post all of this in 11351 06:30:39,680 --> 06:30:43,200 the comments down below 11352 06:30:40,878 --> 06:30:44,718 but yeah that's the basics of buttons in 11353 06:30:43,200 --> 06:30:46,320 python 11354 06:30:44,718 --> 06:30:48,000 hey what's going on everybody it's your 11355 06:30:46,320 --> 06:30:49,280 bro here hope you're doing well and in 11356 06:30:48,000 --> 06:30:51,520 this video i'm going to teach you guys 11357 06:30:49,280 --> 06:30:52,000 how we can create a simple entry box in 11358 06:30:51,520 --> 06:30:56,160 python 11359 06:30:52,000 --> 06:30:56,160 so sit back relax and enjoy the show 11360 06:30:56,638 --> 06:30:59,760 if you find this video helpful please 11361 06:30:58,240 --> 06:31:02,160 remember to like 11362 06:30:59,760 --> 06:31:04,160 comment and subscribe your support will 11363 06:31:02,160 --> 06:31:06,240 help keep this channel running 11364 06:31:04,160 --> 06:31:07,840 alright my fellow bros in this video 11365 06:31:06,240 --> 06:31:08,320 we're going to be discussing the entry 11366 06:31:07,840 --> 06:31:10,718 widget 11367 06:31:08,320 --> 06:31:12,080 this is a text box that accepts a single 11368 06:31:10,718 --> 06:31:14,240 line of user input 11369 06:31:12,080 --> 06:31:15,120 as always we'll need a window window 11370 06:31:14,240 --> 06:31:17,440 equals tk 11371 06:31:15,120 --> 06:31:19,680 followed by window.main loop and let's 11372 06:31:17,440 --> 06:31:20,000 give our entry box a unique name let's 11373 06:31:19,680 --> 06:31:22,320 call 11374 06:31:20,000 --> 06:31:23,920 this entry for short and then to create 11375 06:31:22,320 --> 06:31:25,440 this entry widget we're going to follow 11376 06:31:23,920 --> 06:31:28,000 this with equals 11377 06:31:25,440 --> 06:31:29,920 entry and within the constructor we have 11378 06:31:28,000 --> 06:31:32,558 to state what we're adding this to 11379 06:31:29,920 --> 06:31:34,718 we're going to add this to our window 11380 06:31:32,558 --> 06:31:36,240 and we should finish this by packing it 11381 06:31:34,718 --> 06:31:39,280 or placing it 11382 06:31:36,240 --> 06:31:40,638 entry dot pack and we should have an 11383 06:31:39,280 --> 06:31:43,040 entry box within 11384 06:31:40,638 --> 06:31:45,680 our window however the text is somewhat 11385 06:31:43,040 --> 06:31:47,600 small so let's change that 11386 06:31:45,680 --> 06:31:50,400 we can set this as an option within the 11387 06:31:47,600 --> 06:31:53,120 constructor of our entry box 11388 06:31:50,400 --> 06:31:54,638 so let's set the font font equals and 11389 06:31:53,120 --> 06:31:57,840 pick whatever font you want 11390 06:31:54,638 --> 06:31:58,400 i'm going to pick maybe ariel and then a 11391 06:31:57,840 --> 06:32:00,878 size 11392 06:31:58,400 --> 06:32:02,798 it'd be 50. and this should be a little 11393 06:32:00,878 --> 06:32:06,320 bit larger 11394 06:32:02,798 --> 06:32:10,958 and we can type in some 11395 06:32:06,320 --> 06:32:13,120 text like this cool right 11396 06:32:10,958 --> 06:32:14,718 now we should add a submit button 11397 06:32:13,120 --> 06:32:16,718 because right now this currently doesn't 11398 06:32:14,718 --> 06:32:18,798 do anything if we type some text in 11399 06:32:16,718 --> 06:32:20,558 so let's create a button i'm going to 11400 06:32:18,798 --> 06:32:23,120 create this button maybe after 11401 06:32:20,558 --> 06:32:25,040 the entry box and let's say that this is 11402 06:32:23,120 --> 06:32:28,638 a submit button so let's call this 11403 06:32:25,040 --> 06:32:30,000 submit button equals button 11404 06:32:28,638 --> 06:32:32,718 what are we adding this to writing the 11405 06:32:30,000 --> 06:32:32,718 square window 11406 06:32:32,878 --> 06:32:36,240 and we'll set some text text equals 11407 06:32:35,440 --> 06:32:37,840 submit 11408 06:32:36,240 --> 06:32:39,280 we'll also need a command for this 11409 06:32:37,840 --> 06:32:42,000 button 11410 06:32:39,280 --> 06:32:43,520 command equals and we'll create a 11411 06:32:42,000 --> 06:32:46,000 function called submit 11412 06:32:43,520 --> 06:32:47,520 and we want to pack the submit button to 11413 06:32:46,000 --> 06:32:50,798 add it to the window 11414 06:32:47,520 --> 06:32:50,798 submit button.pack 11415 06:32:50,958 --> 06:32:55,280 all right now let's put our entry box on 11416 06:32:53,520 --> 06:32:56,718 the left hand side and our submit button 11417 06:32:55,280 --> 06:32:58,878 on the right hand side 11418 06:32:56,718 --> 06:33:00,638 when you pack these widgets you can set 11419 06:32:58,878 --> 06:33:04,000 these on a certain side 11420 06:33:00,638 --> 06:33:06,080 side equals left and our submit button 11421 06:33:04,000 --> 06:33:08,240 we want this on the right not necessary 11422 06:33:06,080 --> 06:33:10,478 but i think it looks better 11423 06:33:08,240 --> 06:33:12,320 and we also need to define our submit 11424 06:33:10,478 --> 06:33:16,160 function outside of the window 11425 06:33:12,320 --> 06:33:19,200 let's define what this does def submit 11426 06:33:16,160 --> 06:33:21,360 what's this going to do we need to get 11427 06:33:19,200 --> 06:33:22,400 the current value that's within our 11428 06:33:21,360 --> 06:33:24,478 entry box 11429 06:33:22,400 --> 06:33:26,478 one way in which we can do that is use 11430 06:33:24,478 --> 06:33:29,200 the entry boxes get 11431 06:33:26,478 --> 06:33:30,718 function this will return a string and 11432 06:33:29,200 --> 06:33:33,440 we can do something with that string 11433 06:33:30,718 --> 06:33:35,040 such as store this within a variable so 11434 06:33:33,440 --> 06:33:35,760 let's create a variable called maybe 11435 06:33:35,040 --> 06:33:38,240 username 11436 06:33:35,760 --> 06:33:40,320 let's pretend we're having a user submit 11437 06:33:38,240 --> 06:33:41,360 their username to login to something i 11438 06:33:40,320 --> 06:33:43,840 suppose 11439 06:33:41,360 --> 06:33:44,798 so we'll get the current value of the 11440 06:33:43,840 --> 06:33:46,478 entry box 11441 06:33:44,798 --> 06:33:47,840 store that within a variable and we'll 11442 06:33:46,478 --> 06:33:49,920 call this username 11443 06:33:47,840 --> 06:33:52,798 and then we will maybe print this for 11444 06:33:49,920 --> 06:33:52,798 fun print 11445 06:33:52,840 --> 06:33:58,878 hello plus user 11446 06:33:55,600 --> 06:34:00,798 name and let's try this 11447 06:33:58,878 --> 06:34:02,478 so here's our entry box here and our 11448 06:34:00,798 --> 06:34:03,920 submit button i'm just going to make up 11449 06:34:02,478 --> 06:34:06,840 something 11450 06:34:03,920 --> 06:34:09,440 click submit and it displays our message 11451 06:34:06,840 --> 06:34:10,798 hello ergo gerger gerger 11452 06:34:09,440 --> 06:34:12,878 i didn't say that this video was going 11453 06:34:10,798 --> 06:34:13,600 to be professional let's add a few other 11454 06:34:12,878 --> 06:34:15,840 buttons 11455 06:34:13,600 --> 06:34:18,400 let's say we want a delete button as 11456 06:34:15,840 --> 06:34:20,160 well as a backspace button 11457 06:34:18,400 --> 06:34:22,320 so let's copy what we have for our 11458 06:34:20,160 --> 06:34:24,478 submit button and create a delete button 11459 06:34:22,320 --> 06:34:26,400 and then a backspace button the delete 11460 06:34:24,478 --> 06:34:28,958 button is a little bit easier to code 11461 06:34:26,400 --> 06:34:31,440 so let's rename our submit button as 11462 06:34:28,958 --> 06:34:33,120 delete button 11463 06:34:31,440 --> 06:34:34,878 make sure to change the name to when you 11464 06:34:33,120 --> 06:34:38,160 pack this and the text 11465 06:34:34,878 --> 06:34:38,958 we will say delete and we'll create a 11466 06:34:38,160 --> 06:34:42,638 function called 11467 06:34:38,958 --> 06:34:44,718 delete and let's define what delete does 11468 06:34:42,638 --> 06:34:46,320 so after our submit function let's 11469 06:34:44,718 --> 06:34:50,000 define 11470 06:34:46,320 --> 06:34:52,558 our delete function and we need to 11471 06:34:50,000 --> 06:34:53,520 delete all of the text within our entry 11472 06:34:52,558 --> 06:34:55,200 box 11473 06:34:53,520 --> 06:34:57,360 one way in which we can do that is to 11474 06:34:55,200 --> 06:35:00,638 type in the name of our entry box 11475 06:34:57,360 --> 06:35:03,840 dot delete function 11476 06:35:00,638 --> 06:35:06,080 and this takes two positional arguments 11477 06:35:03,840 --> 06:35:07,280 which portion of our entry box do we 11478 06:35:06,080 --> 06:35:09,440 want to delete 11479 06:35:07,280 --> 06:35:10,320 so we want the very first character that 11480 06:35:09,440 --> 06:35:13,200 is index 11481 06:35:10,320 --> 06:35:13,760 0 followed by the last character and we 11482 06:35:13,200 --> 06:35:16,000 can say 11483 06:35:13,760 --> 06:35:16,798 end so this will delete all of the 11484 06:35:16,000 --> 06:35:19,840 characters 11485 06:35:16,798 --> 06:35:21,760 within our entry box and let's try it so 11486 06:35:19,840 --> 06:35:24,718 i'm just going to type in my name 11487 06:35:21,760 --> 06:35:27,040 bro i'm going to delete this and all of 11488 06:35:24,718 --> 06:35:29,280 the text is now gone 11489 06:35:27,040 --> 06:35:31,200 just like that let's create a backspace 11490 06:35:29,280 --> 06:35:32,798 button we'll copy what we have for our 11491 06:35:31,200 --> 06:35:36,558 delete button and change 11492 06:35:32,798 --> 06:35:36,558 the word delete to backspace 11493 06:35:37,040 --> 06:35:40,718 make sure you change that when you pack 11494 06:35:38,638 --> 06:35:41,280 this as well and for the text we'll set 11495 06:35:40,718 --> 06:35:44,000 this to 11496 06:35:41,280 --> 06:35:46,718 backspace and we'll create a function 11497 06:35:44,000 --> 06:35:49,280 called backspace 11498 06:35:46,718 --> 06:35:49,760 and let's define what backspace does so 11499 06:35:49,280 --> 06:35:52,798 after 11500 06:35:49,760 --> 06:35:55,920 our delete function let's define back 11501 06:35:52,798 --> 06:35:58,958 space and this is going to do something 11502 06:35:55,920 --> 06:36:02,320 very similar to our delete function 11503 06:35:58,958 --> 06:36:03,840 except we're going to change the first 11504 06:36:02,320 --> 06:36:06,400 positional argument 11505 06:36:03,840 --> 06:36:06,958 we need to place the second to last 11506 06:36:06,400 --> 06:36:09,600 character 11507 06:36:06,958 --> 06:36:10,798 within this positional argument one way 11508 06:36:09,600 --> 06:36:13,840 in which we can do that 11509 06:36:10,798 --> 06:36:16,160 is to first get the entire length of all 11510 06:36:13,840 --> 06:36:19,200 of the characters within our entry box 11511 06:36:16,160 --> 06:36:21,920 so let's begin by typing entry dot 11512 06:36:19,200 --> 06:36:22,558 get and we need to determine the length 11513 06:36:21,920 --> 06:36:25,600 of this 11514 06:36:22,558 --> 06:36:28,478 and we can surround this by placing this 11515 06:36:25,600 --> 06:36:29,040 within the length function so this will 11516 06:36:28,478 --> 06:36:31,360 return 11517 06:36:29,040 --> 06:36:33,280 the current length of all of the 11518 06:36:31,360 --> 06:36:36,160 characters within our entry box 11519 06:36:33,280 --> 06:36:36,638 and then we're going to add -1 so that 11520 06:36:36,160 --> 06:36:38,958 this 11521 06:36:36,638 --> 06:36:40,240 first positional argument is the second 11522 06:36:38,958 --> 06:36:42,240 to last character 11523 06:36:40,240 --> 06:36:44,080 and the second positional argument is 11524 06:36:42,240 --> 06:36:47,440 the end the last character 11525 06:36:44,080 --> 06:36:49,120 and then when we run this let's type in 11526 06:36:47,440 --> 06:36:50,798 our name i'm just going to type in draw 11527 06:36:49,120 --> 06:36:52,878 code this time i'm going to click 11528 06:36:50,798 --> 06:36:53,920 backspace and then it's only going to 11529 06:36:52,878 --> 06:36:55,760 delete the last 11530 06:36:53,920 --> 06:36:58,958 character and then if you were to click 11531 06:36:55,760 --> 06:37:01,520 delete it deletes everything 11532 06:36:58,958 --> 06:37:03,440 broy and then if you click submit it's 11533 06:37:01,520 --> 06:37:05,360 going to submit your name 11534 06:37:03,440 --> 06:37:06,798 all right let's customize the appearance 11535 06:37:05,360 --> 06:37:09,600 of this entry box 11536 06:37:06,798 --> 06:37:10,080 so within the constructor for this entry 11537 06:37:09,600 --> 06:37:13,040 box 11538 06:37:10,080 --> 06:37:14,718 let's change maybe the color maybe the 11539 06:37:13,040 --> 06:37:16,080 background color and foreground color 11540 06:37:14,718 --> 06:37:18,240 let's begin with the foreground color 11541 06:37:16,080 --> 06:37:18,798 you can pass in a color name or a hex 11542 06:37:18,240 --> 06:37:21,040 value 11543 06:37:18,798 --> 06:37:21,840 i want this to be green so the hex value 11544 06:37:21,040 --> 06:37:25,440 for that is 11545 06:37:21,840 --> 06:37:25,920 zero zero f zero ff00 and the foreground 11546 06:37:25,440 --> 06:37:28,240 color 11547 06:37:25,920 --> 06:37:29,680 of the text when we type it in is now 11548 06:37:28,240 --> 06:37:31,200 green 11549 06:37:29,680 --> 06:37:33,440 and we can also change the background 11550 06:37:31,200 --> 06:37:35,600 color that is bg 11551 06:37:33,440 --> 06:37:37,200 we could pass in a color name or a hex 11552 06:37:35,600 --> 06:37:38,638 value i'm just going to say black to 11553 06:37:37,200 --> 06:37:40,878 keep it simple 11554 06:37:38,638 --> 06:37:42,400 and we now have a black background and 11555 06:37:40,878 --> 06:37:45,680 the text color is now 11556 06:37:42,400 --> 06:37:47,200 green here's a few useful features that 11557 06:37:45,680 --> 06:37:49,680 you might be interested in 11558 06:37:47,200 --> 06:37:50,638 let's insert some default text for our 11559 06:37:49,680 --> 06:37:53,280 entry box 11560 06:37:50,638 --> 06:37:54,798 so we can use the insert function entry 11561 06:37:53,280 --> 06:37:56,878 dot insert 11562 06:37:54,798 --> 06:37:57,920 we place a positional argument as well 11563 06:37:56,878 --> 06:37:59,840 as some text 11564 06:37:57,920 --> 06:38:01,760 so for the positional argument let's say 11565 06:37:59,840 --> 06:38:04,400 zero so that's the very beginning 11566 06:38:01,760 --> 06:38:05,280 index zero and let's set the text to 11567 06:38:04,400 --> 06:38:08,320 maybe 11568 06:38:05,280 --> 06:38:10,240 spongebob and then the default text 11569 06:38:08,320 --> 06:38:12,878 within our entry box is now 11570 06:38:10,240 --> 06:38:15,200 spongebob another option available to us 11571 06:38:12,878 --> 06:38:17,440 is that we can set the state 11572 06:38:15,200 --> 06:38:19,040 of this entry box we can set this to 11573 06:38:17,440 --> 06:38:20,958 active or disabled 11574 06:38:19,040 --> 06:38:22,878 let's say that after somebody submits 11575 06:38:20,958 --> 06:38:25,840 our name we want to disable 11576 06:38:22,878 --> 06:38:27,120 this entry box so we can either pass in 11577 06:38:25,840 --> 06:38:30,080 this option 11578 06:38:27,120 --> 06:38:32,320 within our constructor or we can use the 11579 06:38:30,080 --> 06:38:35,360 config function to make any changes 11580 06:38:32,320 --> 06:38:37,600 to the state of our entry box so let's 11581 06:38:35,360 --> 06:38:39,760 say after we submit our username we want 11582 06:38:37,600 --> 06:38:41,520 to disable this entry box 11583 06:38:39,760 --> 06:38:44,558 so let's type in the name of our entry 11584 06:38:41,520 --> 06:38:48,080 box dot config 11585 06:38:44,558 --> 06:38:49,200 and for the option for state let's set 11586 06:38:48,080 --> 06:38:54,878 this to 11587 06:38:49,200 --> 06:38:57,920 disabled and then once we type in a name 11588 06:38:54,878 --> 06:38:58,958 and then click submit our entry box is 11589 06:38:57,920 --> 06:39:02,080 now disabled 11590 06:38:58,958 --> 06:39:04,160 there's also an option called show where 11591 06:39:02,080 --> 06:39:06,638 you can show a certain character in 11592 06:39:04,160 --> 06:39:09,040 place of the normal text on the screen 11593 06:39:06,638 --> 06:39:10,798 so this would be useful for a password 11594 06:39:09,040 --> 06:39:13,520 let's say that we're typing in 11595 06:39:10,798 --> 06:39:14,878 a password but we don't want to display 11596 06:39:13,520 --> 06:39:17,440 the password on the screen 11597 06:39:14,878 --> 06:39:19,200 we can use the show option to show a 11598 06:39:17,440 --> 06:39:22,000 certain character in place 11599 06:39:19,200 --> 06:39:23,040 of our text so you might see bullets or 11600 06:39:22,000 --> 06:39:24,798 an asterisk 11601 06:39:23,040 --> 06:39:27,040 so let's pretend that we're now typing 11602 06:39:24,798 --> 06:39:30,240 in a password so we're going to set 11603 06:39:27,040 --> 06:39:32,080 the option for show and place a 11604 06:39:30,240 --> 06:39:33,200 character that we want to replace each 11605 06:39:32,080 --> 06:39:35,280 character with 11606 06:39:33,200 --> 06:39:37,040 let's say we want to only display 11607 06:39:35,280 --> 06:39:39,920 asterisks when we type in 11608 06:39:37,040 --> 06:39:41,280 some characters and i'm going to type in 11609 06:39:39,920 --> 06:39:43,520 the word password 11610 06:39:41,280 --> 06:39:45,200 but it's only displaying asterisks now 11611 06:39:43,520 --> 06:39:48,240 but if i were to click submit 11612 06:39:45,200 --> 06:39:50,718 it displays our text and i would say 11613 06:39:48,240 --> 06:39:52,000 plain text instead of our hidden text 11614 06:39:50,718 --> 06:39:54,000 because we're only showing 11615 06:39:52,000 --> 06:39:55,760 whatever character that we set so that's 11616 06:39:54,000 --> 06:39:57,760 an option available to you as well 11617 06:39:55,760 --> 06:39:59,760 well everybody that's how to create an 11618 06:39:57,760 --> 06:40:00,478 entry box in python you can submit 11619 06:39:59,760 --> 06:40:03,360 information 11620 06:40:00,478 --> 06:40:04,878 such as a username for example or even a 11621 06:40:03,360 --> 06:40:07,200 password if you set the 11622 06:40:04,878 --> 06:40:09,200 show option available to you if you want 11623 06:40:07,200 --> 06:40:10,798 a copy of all this code i'll post all of 11624 06:40:09,200 --> 06:40:12,798 this in the comments down below 11625 06:40:10,798 --> 06:40:14,958 but yeah that's some of the uses of the 11626 06:40:12,798 --> 06:40:16,878 entry widget in python 11627 06:40:14,958 --> 06:40:18,400 hey how's it going everybody two bro 11628 06:40:16,878 --> 06:40:19,920 here hope you're doing well and in this 11629 06:40:18,400 --> 06:40:21,760 video i'm going to teach you guys how we 11630 06:40:19,920 --> 06:40:23,920 can create and customize our own 11631 06:40:21,760 --> 06:40:27,600 check buttons in python so sit back 11632 06:40:23,920 --> 06:40:30,000 relax and enjoy the show 11633 06:40:27,600 --> 06:40:31,120 all right my fellow bros in this video 11634 06:40:30,000 --> 06:40:33,360 we're going to be creating 11635 06:40:31,120 --> 06:40:35,520 and customizing our own check buttons in 11636 06:40:33,360 --> 06:40:36,558 python i sometimes refer to these as 11637 06:40:35,520 --> 06:40:38,160 check boxes 11638 06:40:36,558 --> 06:40:40,240 so in case i do that's what i'm 11639 06:40:38,160 --> 06:40:41,040 referring to as always we'll need a 11640 06:40:40,240 --> 06:40:44,000 window 11641 06:40:41,040 --> 06:40:44,718 window equals tk followed by window.main 11642 06:40:44,000 --> 06:40:47,840 loop 11643 06:40:44,718 --> 06:40:50,400 and let's call this button just check 11644 06:40:47,840 --> 06:40:51,040 button and in order to create this check 11645 06:40:50,400 --> 06:40:53,040 button 11646 06:40:51,040 --> 06:40:54,240 we type in the name of the check button 11647 06:40:53,040 --> 06:40:58,240 equals 11648 06:40:54,240 --> 06:41:00,160 check button and within the constructor 11649 06:40:58,240 --> 06:41:02,000 what are we adding this check button to 11650 06:41:00,160 --> 06:41:04,000 we're adding this to our window 11651 06:41:02,000 --> 06:41:05,760 and also within the constructor we can 11652 06:41:04,000 --> 06:41:07,840 set the different options 11653 06:41:05,760 --> 06:41:10,160 for how we want to customize this check 11654 06:41:07,840 --> 06:41:13,840 button let's begin with the text 11655 06:41:10,160 --> 06:41:17,520 text equals i agree 11656 06:41:13,840 --> 06:41:18,320 to something and last but not least we 11657 06:41:17,520 --> 06:41:20,718 need to add 11658 06:41:18,320 --> 06:41:22,000 this check button to the window one way 11659 06:41:20,718 --> 06:41:24,798 in which we can do that is 11660 06:41:22,000 --> 06:41:26,080 to use the pack function of this widget 11661 06:41:24,798 --> 06:41:28,558 so follow this with 11662 06:41:26,080 --> 06:41:30,558 check button dot pack when we run this 11663 06:41:28,558 --> 06:41:31,120 we have our check button within our 11664 06:41:30,558 --> 06:41:34,000 window 11665 06:41:31,120 --> 06:41:35,280 and we can toggle this on or off right 11666 06:41:34,000 --> 06:41:37,040 now i'm agreeing to something 11667 06:41:35,280 --> 06:41:38,558 i'm not sure what i'm agreeing to but 11668 06:41:37,040 --> 06:41:40,400 hopefully it's not anything bad 11669 06:41:38,558 --> 06:41:42,718 now currently this doesn't do anything 11670 06:41:40,400 --> 06:41:43,920 so we'll need to associate a variable 11671 06:41:42,718 --> 06:41:46,958 with this check button 11672 06:41:43,920 --> 06:41:49,200 so we'll add that as an option variable 11673 06:41:46,958 --> 06:41:50,240 equals and let's create a variable 11674 06:41:49,200 --> 06:41:52,080 called x 11675 06:41:50,240 --> 06:41:53,760 and we'll need to define this within the 11676 06:41:52,080 --> 06:41:55,360 window so 11677 06:41:53,760 --> 06:41:57,040 after we create our instance of our 11678 06:41:55,360 --> 06:42:00,080 window let's say 11679 06:41:57,040 --> 06:42:03,920 x equals now with check buttons 11680 06:42:00,080 --> 06:42:04,958 they store a 1 or a 0 by default within 11681 06:42:03,920 --> 06:42:08,000 our variable 11682 06:42:04,958 --> 06:42:12,160 our variable that we call x so with 11683 06:42:08,000 --> 06:42:15,280 x we're going to set this equal to a int 11684 06:42:12,160 --> 06:42:16,558 var if this were to return a string we 11685 06:42:15,280 --> 06:42:19,600 would say 11686 06:42:16,558 --> 06:42:20,718 string bar but normally by default this 11687 06:42:19,600 --> 06:42:23,360 returns a one 11688 06:42:20,718 --> 06:42:25,280 or a zero and we can actually change 11689 06:42:23,360 --> 06:42:28,878 that using the on value 11690 06:42:25,280 --> 06:42:31,440 and off value options so the on value 11691 06:42:28,878 --> 06:42:32,320 is what's going to be stored within our 11692 06:42:31,440 --> 06:42:35,360 variable 11693 06:42:32,320 --> 06:42:36,000 if it's toggled on so by default this is 11694 06:42:35,360 --> 06:42:38,958 one 11695 06:42:36,000 --> 06:42:40,160 and the off value is the opposite this 11696 06:42:38,958 --> 06:42:43,360 is what is stored within 11697 06:42:40,160 --> 06:42:44,958 our variable if this is toggled off 11698 06:42:43,360 --> 06:42:47,840 alright now let's have some fun with 11699 06:42:44,958 --> 06:42:49,200 this so let's associate a command with 11700 06:42:47,840 --> 06:42:51,440 this check button 11701 06:42:49,200 --> 06:42:52,240 command equals and let's create a 11702 06:42:51,440 --> 06:42:55,440 function called 11703 06:42:52,240 --> 06:43:00,240 maybe display and then let's define this 11704 06:42:55,440 --> 06:43:00,958 before the window def display and we're 11705 06:43:00,240 --> 06:43:04,000 going to check 11706 06:43:00,958 --> 06:43:07,040 the value of x to see if it's one 11707 06:43:04,000 --> 06:43:08,478 or zero so within our display function 11708 06:43:07,040 --> 06:43:11,600 we're going to check to see 11709 06:43:08,478 --> 06:43:14,080 if x and to get 11710 06:43:11,600 --> 06:43:15,360 the value of x we're going to use the 11711 06:43:14,080 --> 06:43:19,040 get function 11712 06:43:15,360 --> 06:43:20,878 so if x dot get is equal to one 11713 06:43:19,040 --> 06:43:23,040 that means that somebody clicked on the 11714 06:43:20,878 --> 06:43:26,320 check box or toggled it on 11715 06:43:23,040 --> 06:43:29,280 so if they check the check box 11716 06:43:26,320 --> 06:43:30,000 or check the check button then they 11717 06:43:29,280 --> 06:43:34,718 agree 11718 06:43:30,000 --> 06:43:36,798 so print you agree 11719 06:43:34,718 --> 06:43:37,920 if it's zero and we can do this with an 11720 06:43:36,798 --> 06:43:41,040 else statement 11721 06:43:37,920 --> 06:43:45,360 else print 11722 06:43:41,040 --> 06:43:48,638 you don't agree frowny face 11723 06:43:45,360 --> 06:43:49,040 and let's test this theory so we have 11724 06:43:48,638 --> 06:43:51,280 our 11725 06:43:49,040 --> 06:43:52,638 check button when we click on this it's 11726 06:43:51,280 --> 06:43:54,718 actually going to call 11727 06:43:52,638 --> 06:43:56,798 our display function i agree to 11728 06:43:54,718 --> 06:43:57,360 something and it says within our console 11729 06:43:56,798 --> 06:43:59,760 window 11730 06:43:57,360 --> 06:44:02,000 you agree and if i were to toggle this 11731 06:43:59,760 --> 06:44:03,520 off it says you don't agree 11732 06:44:02,000 --> 06:44:05,680 let's change the font of our check 11733 06:44:03,520 --> 06:44:08,478 button so we'll add that as an option 11734 06:44:05,680 --> 06:44:09,520 font equals and we can pass in a font 11735 06:44:08,478 --> 06:44:12,638 that we want 11736 06:44:09,520 --> 06:44:16,798 i'll say maybe ariel and then a size 11737 06:44:12,638 --> 06:44:19,680 of 20 and now this should be readable 11738 06:44:16,798 --> 06:44:20,000 i agree to something let's also color 11739 06:44:19,680 --> 06:44:22,558 this 11740 06:44:20,000 --> 06:44:24,240 because well i like coloring let's begin 11741 06:44:22,558 --> 06:44:27,920 with the foreground color 11742 06:44:24,240 --> 06:44:30,478 so that is fg for short fg equals 11743 06:44:27,920 --> 06:44:31,760 and then we can pass in a color name or 11744 06:44:30,478 --> 06:44:33,520 a hex value 11745 06:44:31,760 --> 06:44:35,680 i like hex values so i'm going to stick 11746 06:44:33,520 --> 06:44:39,520 with those i want a green 11747 06:44:35,680 --> 06:44:42,558 foreground color so that is zero zero f 11748 06:44:39,520 --> 06:44:44,080 zero zero but feel free to adjust these 11749 06:44:42,558 --> 06:44:47,200 values however you want 11750 06:44:44,080 --> 06:44:47,440 so our text should now be green that is 11751 06:44:47,200 --> 06:44:49,680 a 11752 06:44:47,440 --> 06:44:51,200 very bright green and it hurts my eyes 11753 06:44:49,680 --> 06:44:51,760 so i'm also going to change the 11754 06:44:51,200 --> 06:44:54,160 background 11755 06:44:51,760 --> 06:44:54,798 color which is bg and i'm going to set 11756 06:44:54,160 --> 06:44:57,680 this to 11757 06:44:54,798 --> 06:44:59,760 black so we'll have green text on a 11758 06:44:57,680 --> 06:45:01,760 black background 11759 06:44:59,760 --> 06:45:03,920 okay now when you click on this it 11760 06:45:01,760 --> 06:45:06,320 flashes that's kind of annoying 11761 06:45:03,920 --> 06:45:07,040 that's the active foreground and active 11762 06:45:06,320 --> 06:45:09,520 background 11763 06:45:07,040 --> 06:45:11,360 if we click on this or hold this down 11764 06:45:09,520 --> 06:45:13,760 the color scheme switches to 11765 06:45:11,360 --> 06:45:15,600 our active foreground slash background 11766 06:45:13,760 --> 06:45:19,200 and we can change those too 11767 06:45:15,600 --> 06:45:21,520 so i'm going to set those options active 11768 06:45:19,200 --> 06:45:25,280 foreground i'm going to set this the 11769 06:45:21,520 --> 06:45:27,840 same as my foreground color 11770 06:45:25,280 --> 06:45:28,558 and the active background will be the 11771 06:45:27,840 --> 06:45:31,680 same as 11772 06:45:28,558 --> 06:45:35,040 our normal background so that's active 11773 06:45:31,680 --> 06:45:38,400 background equals 11774 06:45:35,040 --> 06:45:41,280 black and this should no longer flash 11775 06:45:38,400 --> 06:45:43,520 when we click on it 11776 06:45:41,280 --> 06:45:45,680 not too bad not too bad let's add some 11777 06:45:43,520 --> 06:45:49,520 padding 11778 06:45:45,680 --> 06:45:52,558 pad x i'll set this to 25 11779 06:45:49,520 --> 06:45:57,200 and pad y i'll set this to 11780 06:45:52,558 --> 06:45:59,600 10. we have a little more padding around 11781 06:45:57,200 --> 06:46:00,558 our check button one feature that's 11782 06:45:59,600 --> 06:46:03,360 available to us 11783 06:46:00,558 --> 06:46:04,240 is that we can add an image next to our 11784 06:46:03,360 --> 06:46:07,360 check button 11785 06:46:04,240 --> 06:46:08,718 i downloaded an image of the python logo 11786 06:46:07,360 --> 06:46:12,320 that i want to add 11787 06:46:08,718 --> 06:46:13,200 so we'll use the image option to add a 11788 06:46:12,320 --> 06:46:15,600 photo image 11789 06:46:13,200 --> 06:46:16,638 to this check button but first we need 11790 06:46:15,600 --> 06:46:19,680 to create a 11791 06:46:16,638 --> 06:46:21,280 photo image from our image file so 11792 06:46:19,680 --> 06:46:24,478 within the window 11793 06:46:21,280 --> 06:46:25,920 let's call this python photo 11794 06:46:24,478 --> 06:46:27,680 but you'll probably use something 11795 06:46:25,920 --> 06:46:31,040 different for your project 11796 06:46:27,680 --> 06:46:33,920 python photo equals photo 11797 06:46:31,040 --> 06:46:36,000 image and within the constructor for our 11798 06:46:33,920 --> 06:46:38,558 photo image 11799 06:46:36,000 --> 06:46:39,280 we can set the file path or the file 11800 06:46:38,558 --> 06:46:41,760 name 11801 06:46:39,280 --> 06:46:44,478 since this image is within my project 11802 06:46:41,760 --> 06:46:49,200 folder i only have to list the file name 11803 06:46:44,478 --> 06:46:52,878 and this photo of mine is python.png 11804 06:46:49,200 --> 06:46:55,520 and now we set the image equal to 11805 06:46:52,878 --> 06:46:56,478 our photo image that we have image 11806 06:46:55,520 --> 06:46:59,600 equals 11807 06:46:56,478 --> 06:47:02,160 python photo but when we run this 11808 06:46:59,600 --> 06:47:04,000 it's going to overlap the text we still 11809 06:47:02,160 --> 06:47:06,878 have our image though 11810 06:47:04,000 --> 06:47:08,718 and this still works as intended so in 11811 06:47:06,878 --> 06:47:11,200 order to display 11812 06:47:08,718 --> 06:47:13,120 the image as well as the text we're 11813 06:47:11,200 --> 06:47:15,920 going to need to use the compound 11814 06:47:13,120 --> 06:47:17,200 option and let's add that at the end 11815 06:47:15,920 --> 06:47:19,600 compound 11816 06:47:17,200 --> 06:47:21,040 is where we're adding this photo 11817 06:47:19,600 --> 06:47:23,200 relative to the text 11818 06:47:21,040 --> 06:47:24,240 i want this photo on the left hand side 11819 06:47:23,200 --> 06:47:27,040 of the text 11820 06:47:24,240 --> 06:47:28,160 so i'm going to set this to left and now 11821 06:47:27,040 --> 06:47:30,400 when we run this 11822 06:47:28,160 --> 06:47:31,600 surprise surprise our image is on the 11823 06:47:30,400 --> 06:47:33,120 left hand side 11824 06:47:31,600 --> 06:47:35,520 one thing that you should know is that 11825 06:47:33,120 --> 06:47:37,440 with the on value and the off value data 11826 06:47:35,520 --> 06:47:40,160 type if you were to change this 11827 06:47:37,440 --> 06:47:42,400 to a non-integer value let's say we're 11828 06:47:40,160 --> 06:47:45,600 going to replace the on value with a 11829 06:47:42,400 --> 06:47:49,360 boolean value of true and the off value 11830 06:47:45,600 --> 06:47:52,000 is false with this variable make sure to 11831 06:47:49,360 --> 06:47:53,200 change the data type as well to reflect 11832 06:47:52,000 --> 06:47:56,478 what you're storing within 11833 06:47:53,200 --> 06:47:57,920 it so with our int var variable we want 11834 06:47:56,478 --> 06:48:01,360 to change this to a 11835 06:47:57,920 --> 06:48:04,080 boolean var value so replace int 11836 06:48:01,360 --> 06:48:04,798 with boolean and this will work much the 11837 06:48:04,080 --> 06:48:06,798 same 11838 06:48:04,798 --> 06:48:08,400 now with this display function we'll 11839 06:48:06,798 --> 06:48:10,718 need to account for that 11840 06:48:08,400 --> 06:48:11,840 so within the if statement here we'll 11841 06:48:10,718 --> 06:48:14,718 say if x 11842 06:48:11,840 --> 06:48:15,280 dot get we don't need to say is equal to 11843 06:48:14,718 --> 06:48:18,400 one 11844 06:48:15,280 --> 06:48:19,040 this will return true or false and this 11845 06:48:18,400 --> 06:48:21,360 will work 11846 06:48:19,040 --> 06:48:23,200 just the same as it did before and if 11847 06:48:21,360 --> 06:48:26,240 you replace this with a string 11848 06:48:23,200 --> 06:48:30,240 let's say the on value is 11849 06:48:26,240 --> 06:48:34,638 yes all caps and the off value is 11850 06:48:30,240 --> 06:48:36,798 no this is now a string bar 11851 06:48:34,638 --> 06:48:38,080 and we'll change our display function to 11852 06:48:36,798 --> 06:48:43,040 say if x 11853 06:48:38,080 --> 06:48:43,040 dot get is equal to yes 11854 06:48:43,760 --> 06:48:47,280 and this should work you agree you don't 11855 06:48:46,080 --> 06:48:50,558 agree you agree 11856 06:48:47,280 --> 06:48:51,680 you don't agree and i'll change that 11857 06:48:50,558 --> 06:48:53,920 back to one so 11858 06:48:51,680 --> 06:48:55,520 one and zero is the default for this but 11859 06:48:53,920 --> 06:48:56,478 you can edit these based on what you 11860 06:48:55,520 --> 06:48:58,718 need 11861 06:48:56,478 --> 06:49:00,878 all right so that's the basics of check 11862 06:48:58,718 --> 06:49:01,840 buttons in python if you want a copy of 11863 06:49:00,878 --> 06:49:03,360 all this code 11864 06:49:01,840 --> 06:49:05,920 i'll post all of this in the comments 11865 06:49:03,360 --> 06:49:08,638 down below but yeah that's the basics of 11866 06:49:05,920 --> 06:49:10,080 check buttons in python hey what's going 11867 06:49:08,638 --> 06:49:11,520 on everybody it's your bro here hope 11868 06:49:10,080 --> 06:49:12,958 you're doing well and in this video i'm 11869 06:49:11,520 --> 06:49:15,360 going to be teaching you guys how we can 11870 06:49:12,958 --> 06:49:15,680 create radio buttons in python so sit 11871 06:49:15,360 --> 06:49:19,200 back 11872 06:49:15,680 --> 06:49:21,200 relax and enjoy the show 11873 06:49:19,200 --> 06:49:23,600 okay everybody let's create some radio 11874 06:49:21,200 --> 06:49:25,840 buttons now these are similar to check 11875 06:49:23,600 --> 06:49:27,360 boxes but you can only select one from 11876 06:49:25,840 --> 06:49:29,040 any single grouping 11877 06:49:27,360 --> 06:49:30,798 so let's pretend that i'm buying you 11878 06:49:29,040 --> 06:49:33,120 lunch and you can only select 11879 06:49:30,798 --> 06:49:33,840 one item from a menu you can select 11880 06:49:33,120 --> 06:49:37,200 either a 11881 06:49:33,840 --> 06:49:39,600 slice of pizza a hamburger or a hot dog 11882 06:49:37,200 --> 06:49:40,558 so let's begin by creating a list i will 11883 06:49:39,600 --> 06:49:44,478 call this list 11884 06:49:40,558 --> 06:49:44,478 food food equals 11885 06:49:44,840 --> 06:49:48,878 pizza hamburger 11886 06:49:48,958 --> 06:49:55,920 hot dog and let's create a window 11887 06:49:52,558 --> 06:49:59,680 window equals tk followed by 11888 06:49:55,920 --> 06:50:01,600 window dot main loop 11889 06:49:59,680 --> 06:50:04,240 so to create a radio button we'll need a 11890 06:50:01,600 --> 06:50:08,320 name so i'll call this radio button 11891 06:50:04,240 --> 06:50:10,320 radio button equals radio button 11892 06:50:08,320 --> 06:50:12,320 and we're going to be adding this to our 11893 06:50:10,320 --> 06:50:12,878 window and we're going to put this 11894 06:50:12,320 --> 06:50:15,280 within 11895 06:50:12,878 --> 06:50:17,920 a for loop so we're going to iterate 11896 06:50:15,280 --> 06:50:20,558 through all of the items within our list 11897 06:50:17,920 --> 06:50:22,000 so let's create a for loop so we'll say 11898 06:50:20,558 --> 06:50:26,878 for 11899 06:50:22,000 --> 06:50:30,400 index in range 11900 06:50:26,878 --> 06:50:32,958 length of food 11901 06:50:30,400 --> 06:50:35,520 so this is going to iterate once through 11902 06:50:32,958 --> 06:50:38,240 all of the elements within our list 11903 06:50:35,520 --> 06:50:38,638 so when we run this it's going to create 11904 06:50:38,240 --> 06:50:40,958 three 11905 06:50:38,638 --> 06:50:42,080 radio buttons for us because we're 11906 06:50:40,958 --> 06:50:44,080 instantiating 11907 06:50:42,080 --> 06:50:45,200 one radio button for each item in the 11908 06:50:44,080 --> 06:50:48,718 list 11909 06:50:45,200 --> 06:50:51,760 now what i'm going to do is set the text 11910 06:50:48,718 --> 06:50:55,520 equal to 11911 06:50:51,760 --> 06:50:57,920 our list of food at a certain index 11912 06:50:55,520 --> 06:51:00,000 so when we first run this for loop it's 11913 06:50:57,920 --> 06:51:03,120 going to be zero for the index 11914 06:51:00,000 --> 06:51:06,400 then one and then two and then so on and 11915 06:51:03,120 --> 06:51:09,360 so forth for each item within our list 11916 06:51:06,400 --> 06:51:10,478 and then we just need to pack this so 11917 06:51:09,360 --> 06:51:14,160 radio 11918 06:51:10,478 --> 06:51:17,520 button dot pack 11919 06:51:14,160 --> 06:51:19,520 and then let's take a look at this 11920 06:51:17,520 --> 06:51:20,878 alright so here are all of the radio 11921 06:51:19,520 --> 06:51:22,718 buttons that we have 11922 06:51:20,878 --> 06:51:25,040 currently they're all selected though so 11923 06:51:22,718 --> 06:51:25,760 we'll need to link these within a single 11924 06:51:25,040 --> 06:51:28,160 grouping 11925 06:51:25,760 --> 06:51:29,600 the next thing we'll add is a variable 11926 06:51:28,160 --> 06:51:31,760 and we'll set this equal to 11927 06:51:29,600 --> 06:51:33,760 x and we'll need to declare this after 11928 06:51:31,760 --> 06:51:37,040 we create an instance of our window 11929 06:51:33,760 --> 06:51:40,558 so x equals int var 11930 06:51:37,040 --> 06:51:42,958 so this will hold an integer object 11931 06:51:40,558 --> 06:51:43,680 so these are all within the same 11932 06:51:42,958 --> 06:51:45,920 grouping 11933 06:51:43,680 --> 06:51:47,920 however if we try to run this again and 11934 06:51:45,920 --> 06:51:50,000 select one of these options 11935 06:51:47,920 --> 06:51:51,120 they all become selected so we're going 11936 06:51:50,000 --> 06:51:53,600 to need to give 11937 06:51:51,120 --> 06:51:54,400 each of these individual radio buttons 11938 06:51:53,600 --> 06:51:56,320 their own 11939 06:51:54,400 --> 06:51:57,920 value because right now they're all 11940 06:51:56,320 --> 06:52:01,280 sharing the same value 11941 06:51:57,920 --> 06:52:04,240 so we can add value equals 11942 06:52:01,280 --> 06:52:04,718 whatever the index is currently so the 11943 06:52:04,240 --> 06:52:07,280 first 11944 06:52:04,718 --> 06:52:07,920 item within our list of food is going to 11945 06:52:07,280 --> 06:52:11,120 have an 11946 06:52:07,920 --> 06:52:14,320 index value of zero then one 11947 06:52:11,120 --> 06:52:15,760 then two so let's try that again and you 11948 06:52:14,320 --> 06:52:18,320 can see by default it's 11949 06:52:15,760 --> 06:52:20,400 zero so pizza is already selected then 11950 06:52:18,320 --> 06:52:20,878 we can select either hamburger or hot 11951 06:52:20,400 --> 06:52:23,360 dog 11952 06:52:20,878 --> 06:52:25,120 but we can only select one so that's 11953 06:52:23,360 --> 06:52:27,360 kind of what we want then 11954 06:52:25,120 --> 06:52:29,280 so do you guys usually know how i call 11955 06:52:27,360 --> 06:52:30,638 the config function after each change 11956 06:52:29,280 --> 06:52:32,160 that i make for an object 11957 06:52:30,638 --> 06:52:34,400 well that's because i like to add 11958 06:52:32,160 --> 06:52:37,120 comments just for teaching purposes 11959 06:52:34,400 --> 06:52:38,718 for what each change does however it 11960 06:52:37,120 --> 06:52:40,558 might be easier for you instead of 11961 06:52:38,718 --> 06:52:41,200 calling the config function a bunch of 11962 06:52:40,558 --> 06:52:43,840 times 11963 06:52:41,200 --> 06:52:45,280 just to list all of the changes within 11964 06:52:43,840 --> 06:52:46,400 the parentheses when you create an 11965 06:52:45,280 --> 06:52:48,240 instance of the 11966 06:52:46,400 --> 06:52:50,320 object that you're working with however 11967 06:52:48,240 --> 06:52:52,080 just for teaching purposes i'm going to 11968 06:52:50,320 --> 06:52:54,080 put this all within 11969 06:52:52,080 --> 06:52:55,120 separate lines that allows me to add 11970 06:52:54,080 --> 06:52:56,798 some comments 11971 06:52:55,120 --> 06:52:59,280 so i think i'm going to start doing that 11972 06:52:56,798 --> 06:53:02,400 from now on uh so what this does 11973 06:52:59,280 --> 06:53:06,400 is that this adds text to 11974 06:53:02,400 --> 06:53:10,798 radio buttons and this 11975 06:53:06,400 --> 06:53:16,478 groups radio buttons together 11976 06:53:10,798 --> 06:53:18,478 if they share the same variable 11977 06:53:16,478 --> 06:53:20,400 so if you wanted a completely different 11978 06:53:18,478 --> 06:53:21,760 grouping of radio buttons you can give 11979 06:53:20,400 --> 06:53:25,440 them a different variable 11980 06:53:21,760 --> 06:53:28,478 like y and what this line does 11981 06:53:25,440 --> 06:53:32,718 is that this assigns 11982 06:53:28,478 --> 06:53:37,920 each radio button a 11983 06:53:32,718 --> 06:53:39,840 different value 11984 06:53:37,920 --> 06:53:42,558 all right so let's make some cosmetic 11985 06:53:39,840 --> 06:53:45,680 changes to our radio buttons 11986 06:53:42,558 --> 06:53:47,520 so right now they are centered let's 11987 06:53:45,680 --> 06:53:49,440 anchor these to the west 11988 06:53:47,520 --> 06:53:50,798 so we'll place this within the pack 11989 06:53:49,440 --> 06:53:54,718 function so 11990 06:53:50,798 --> 06:53:56,160 anchor equals w alternatively you can 11991 06:53:54,718 --> 06:53:58,478 just say capital w 11992 06:53:56,160 --> 06:54:00,000 without the quotes this would also work 11993 06:53:58,478 --> 06:54:01,840 too 11994 06:54:00,000 --> 06:54:03,680 so now they're all lined up let's add 11995 06:54:01,840 --> 06:54:06,958 some padding 11996 06:54:03,680 --> 06:54:10,160 so i'm going to add pad x 11997 06:54:06,958 --> 06:54:13,600 and i'll set this to 25 11998 06:54:10,160 --> 06:54:17,040 and this adds 11999 06:54:13,600 --> 06:54:19,520 padding on x 12000 06:54:17,040 --> 06:54:19,520 axis 12001 06:54:20,240 --> 06:54:23,440 let's also change the font 12002 06:54:23,840 --> 06:54:31,440 so we'll save font 12003 06:54:27,840 --> 06:54:32,558 equals maybe i'll pick impact for a font 12004 06:54:31,440 --> 06:54:36,718 style 12005 06:54:32,558 --> 06:54:38,558 and set the font size to 50 12006 06:54:36,718 --> 06:54:40,638 and now we can actually read these 12007 06:54:38,558 --> 06:54:43,440 because the font is large enough 12008 06:54:40,638 --> 06:54:44,958 now let's add some images so let's 12009 06:54:43,440 --> 06:54:47,200 create some photo images 12010 06:54:44,958 --> 06:54:49,120 you'll want to be sure to create these 12011 06:54:47,200 --> 06:54:50,240 after you create your instance of your 12012 06:54:49,120 --> 06:54:53,040 window 12013 06:54:50,240 --> 06:54:54,080 so let's create a pizza image first 12014 06:54:53,040 --> 06:54:58,160 pizza image 12015 06:54:54,080 --> 06:55:01,120 equals photo image 12016 06:54:58,160 --> 06:55:01,840 file equals the file path or the file 12017 06:55:01,120 --> 06:55:04,878 name 12018 06:55:01,840 --> 06:55:05,920 and my file is pizza.png and it kind of 12019 06:55:04,878 --> 06:55:10,120 looks like this 12020 06:55:05,920 --> 06:55:12,558 it's just the pizza emoji so 12021 06:55:10,120 --> 06:55:14,320 pizza.png 12022 06:55:12,558 --> 06:55:16,798 and then i'll do the same thing for 12023 06:55:14,320 --> 06:55:20,878 hamburger and hotdog 12024 06:55:16,798 --> 06:55:25,360 so this will be hamburger image 12025 06:55:20,878 --> 06:55:28,638 file equals hamburger.png 12026 06:55:25,360 --> 06:55:28,638 and then hotdog image 12027 06:55:35,280 --> 06:55:40,080 and this is what the other emojis look 12028 06:55:37,840 --> 06:55:40,080 like 12029 06:55:40,558 --> 06:55:44,240 and then let's create a list of the 12030 06:55:42,638 --> 06:55:47,600 photo images that we have 12031 06:55:44,240 --> 06:55:52,240 i'll call this food images 12032 06:55:47,600 --> 06:55:56,638 equals than the names of these images 12033 06:55:52,240 --> 06:55:59,840 pizza image hamburger image 12034 06:55:56,638 --> 06:55:59,840 and hot dog image 12035 06:56:01,040 --> 06:56:05,200 so then we can set the image for each 12036 06:56:03,600 --> 06:56:08,478 radio button 12037 06:56:05,200 --> 06:56:11,440 image equals 12038 06:56:08,478 --> 06:56:12,320 the name of the list food images 12039 06:56:11,440 --> 06:56:15,040 followed by 12040 06:56:12,320 --> 06:56:16,878 the index that we're currently on within 12041 06:56:15,040 --> 06:56:20,320 our for loop 12042 06:56:16,878 --> 06:56:23,520 so this adds image 12043 06:56:20,320 --> 06:56:27,920 to radio button and 12044 06:56:23,520 --> 06:56:30,160 let's test this cool here's our images 12045 06:56:27,920 --> 06:56:32,958 now if you want some images as well as 12046 06:56:30,160 --> 06:56:36,400 text you'll have to use compound 12047 06:56:32,958 --> 06:56:37,360 so let's add that next so i'm just going 12048 06:56:36,400 --> 06:56:41,840 to add a comma 12049 06:56:37,360 --> 06:56:41,840 followed by compound 12050 06:56:43,040 --> 06:56:47,760 equals let's say left so this will add 12051 06:56:46,878 --> 06:56:51,200 the image 12052 06:56:47,760 --> 06:56:51,200 to the left of the text 12053 06:56:52,638 --> 06:56:58,638 adds image and text i'll just say left 12054 06:56:56,400 --> 06:56:58,638 side 12055 06:56:59,120 --> 06:57:04,320 so this will display both an image and 12056 06:57:02,160 --> 06:57:06,400 text 12057 06:57:04,320 --> 06:57:07,600 so with these radio buttons you can 12058 06:57:06,400 --> 06:57:10,000 eliminate these 12059 06:57:07,600 --> 06:57:11,360 circle indicators and there's actually a 12060 06:57:10,000 --> 06:57:13,680 way to do that 12061 06:57:11,360 --> 06:57:14,718 so what we'll add if you want to get rid 12062 06:57:13,680 --> 06:57:18,320 of those 12063 06:57:14,718 --> 06:57:21,600 is indicator 12064 06:57:18,320 --> 06:57:27,600 on equals zero 12065 06:57:21,600 --> 06:57:31,520 so this will i should say eliminate 12066 06:57:27,600 --> 06:57:31,520 circle indicators 12067 06:57:32,000 --> 06:57:35,600 so it's going to change these to these 12068 06:57:34,160 --> 06:57:38,160 push buttons 12069 06:57:35,600 --> 06:57:40,080 however they are going to be of uneven 12070 06:57:38,160 --> 06:57:43,520 size you can set the width 12071 06:57:40,080 --> 06:57:47,040 if you want to so we'll do that 12072 06:57:43,520 --> 06:57:50,558 so we'll say width equals 12073 06:57:47,040 --> 06:57:52,638 what about 75 i'm not sure if that's a 12074 06:57:50,558 --> 06:57:57,840 good size or not 12075 06:57:52,638 --> 06:57:57,840 okay that's way too small let's try 375 12076 06:57:59,360 --> 06:58:02,478 so yeah you can have some push buttons 12077 06:58:00,878 --> 06:58:04,558 if you want to if you don't like the 12078 06:58:02,478 --> 06:58:07,840 circle indicators 12079 06:58:04,558 --> 06:58:11,200 so this sets width 12080 06:58:07,840 --> 06:58:14,240 of radio buttons 12081 06:58:11,200 --> 06:58:16,878 now let's actually set a function to 12082 06:58:14,240 --> 06:58:18,240 uh be called when we click one of these 12083 06:58:16,878 --> 06:58:20,558 buttons 12084 06:58:18,240 --> 06:58:24,000 so let's call this function maybe order 12085 06:58:20,558 --> 06:58:27,200 like we're ordering one of these items 12086 06:58:24,000 --> 06:58:29,760 so def we'll call this order like we're 12087 06:58:27,200 --> 06:58:31,440 ordering something 12088 06:58:29,760 --> 06:58:33,760 and what we'll do is just a few if 12089 06:58:31,440 --> 06:58:37,280 statements because i'm feeling lazy 12090 06:58:33,760 --> 06:58:40,320 so we'll just say if x that's 12091 06:58:37,280 --> 06:58:42,958 the value that's going to be stored 12092 06:58:40,320 --> 06:58:45,680 because each of these radio buttons is 12093 06:58:42,958 --> 06:58:48,798 grouped together by the variable x 12094 06:58:45,680 --> 06:58:52,320 if x dot get 12095 06:58:48,798 --> 06:58:57,040 is equal to zero that's our first index 12096 06:58:52,320 --> 06:59:01,280 for pizza what we'll display is 12097 06:58:57,040 --> 06:59:04,840 print you 12098 06:59:01,280 --> 06:59:08,558 oops you ordered 12099 06:59:04,840 --> 06:59:12,878 pizza then we'll just add 12100 06:59:08,558 --> 06:59:17,920 else if x dot get 12101 06:59:12,878 --> 06:59:23,120 is equal to one we will print 12102 06:59:17,920 --> 06:59:23,120 you ordered a hamburger 12103 06:59:25,760 --> 06:59:30,240 and then else if two 12104 06:59:30,798 --> 06:59:35,840 you ordered a hot dog 12105 06:59:37,280 --> 06:59:39,680 else 12106 06:59:40,718 --> 06:59:46,878 print huh 12107 06:59:44,718 --> 06:59:48,000 so there's probably a more efficient way 12108 06:59:46,878 --> 06:59:49,760 to write this however 12109 06:59:48,000 --> 06:59:51,760 i was just feeling lazy and this is 12110 06:59:49,760 --> 06:59:53,680 probably the best way to keep this 12111 06:59:51,760 --> 06:59:55,920 simple and easy to understand 12112 06:59:53,680 --> 06:59:57,920 so let's try this now oh however we need 12113 06:59:55,920 --> 07:00:00,958 to set the command 12114 06:59:57,920 --> 07:00:04,320 for our radio buttons so 12115 07:00:00,958 --> 07:00:07,520 command equals then the 12116 07:00:04,320 --> 07:00:10,080 function name which is order and make 12117 07:00:07,520 --> 07:00:13,440 sure you do not add the parentheses 12118 07:00:10,080 --> 07:00:16,478 so don't do this do that 12119 07:00:13,440 --> 07:00:19,520 and this will set 12120 07:00:16,478 --> 07:00:24,958 command of 12121 07:00:19,520 --> 07:00:24,958 radio button to function 12122 07:00:25,520 --> 07:00:30,878 all right let's test this so if we click 12123 07:00:28,320 --> 07:00:33,040 on pizza it says you ordered pizza 12124 07:00:30,878 --> 07:00:35,200 you ordered a hamburger and you ordered 12125 07:00:33,040 --> 07:00:37,440 a hot dog 12126 07:00:35,200 --> 07:00:39,040 okay everybody so that's the basics of 12127 07:00:37,440 --> 07:00:40,718 radio buttons if you would like a copy 12128 07:00:39,040 --> 07:00:42,160 of all this code i'll post all of this 12129 07:00:40,718 --> 07:00:44,400 in the comments down below 12130 07:00:42,160 --> 07:00:46,958 but yeah that's the basics of radio 12131 07:00:44,400 --> 07:00:48,478 buttons in python 12132 07:00:46,958 --> 07:00:50,320 hey what's going on everybody it's you 12133 07:00:48,478 --> 07:00:51,680 bro here hope you're doing well and in 12134 07:00:50,320 --> 07:00:52,558 this video i'm going to teach you guys 12135 07:00:51,680 --> 07:00:55,520 how we can create 12136 07:00:52,558 --> 07:00:59,200 a sliding scale in python so sit back 12137 07:00:55,520 --> 07:01:00,878 relax and enjoy the show 12138 07:00:59,200 --> 07:01:03,360 let's create a scale so we'll need a 12139 07:01:00,878 --> 07:01:06,558 window window equals tk 12140 07:01:03,360 --> 07:01:07,440 followed by window.mainloop we'll create 12141 07:01:06,558 --> 07:01:10,558 a scale 12142 07:01:07,440 --> 07:01:12,798 called scale scale equals scale 12143 07:01:10,558 --> 07:01:14,400 we need to add the scale to our window 12144 07:01:12,798 --> 07:01:17,360 so we'll put that here 12145 07:01:14,400 --> 07:01:18,240 we can list a from position and a 2 12146 07:01:17,360 --> 07:01:20,718 position 12147 07:01:18,240 --> 07:01:22,240 both of these values will be our range 12148 07:01:20,718 --> 07:01:25,840 of values that we can use 12149 07:01:22,240 --> 07:01:28,080 for our scale so from 12150 07:01:25,840 --> 07:01:30,638 sure to add an underscore after from it 12151 07:01:28,080 --> 07:01:34,878 won't work without this underscore 12152 07:01:30,638 --> 07:01:36,558 from equals let's say 0 and 2 equals 100 12153 07:01:34,878 --> 07:01:37,920 so we're going to create a scale for 12154 07:01:36,558 --> 07:01:41,120 temperature 12155 07:01:37,920 --> 07:01:43,360 and this will be in celsius so 0 will be 12156 07:01:41,120 --> 07:01:45,520 freezing and 100 will be the boiling 12157 07:01:43,360 --> 07:01:48,160 point uh you know for water 12158 07:01:45,520 --> 07:01:48,798 so then to display the scale we'll need 12159 07:01:48,160 --> 07:01:52,000 to add 12160 07:01:48,798 --> 07:01:54,638 scale.pack and let's run this 12161 07:01:52,000 --> 07:01:56,718 so the front position is zero so that's 12162 07:01:54,638 --> 07:01:58,958 at the top here and then if you go down 12163 07:01:56,718 --> 07:02:01,160 it increases so you can actually flip 12164 07:01:58,958 --> 07:02:04,878 this so we'll save from 12165 07:02:01,160 --> 07:02:06,798 102 will be zero 12166 07:02:04,878 --> 07:02:08,878 so now you can scroll up and the number 12167 07:02:06,798 --> 07:02:12,000 will increase 12168 07:02:08,878 --> 07:02:14,718 let's also get the current value so 12169 07:02:12,000 --> 07:02:14,718 we'll need a button 12170 07:02:14,878 --> 07:02:20,638 so button equals button 12171 07:02:18,400 --> 07:02:22,400 we'll add this to the window we'll add 12172 07:02:20,638 --> 07:02:26,240 some text 12173 07:02:22,400 --> 07:02:29,360 text equals submit and then a command 12174 07:02:26,240 --> 07:02:31,440 command equals submit but we'll have to 12175 07:02:29,360 --> 07:02:34,958 make this function 12176 07:02:31,440 --> 07:02:38,320 so let's do that outside the window def 12177 07:02:34,958 --> 07:02:39,920 submit and we will simply print the 12178 07:02:38,320 --> 07:02:43,840 current temperature 12179 07:02:39,920 --> 07:02:47,280 so let's display message the temperature 12180 07:02:43,840 --> 07:02:51,600 is plus 12181 07:02:47,280 --> 07:02:55,840 scale dot get 12182 07:02:51,600 --> 07:02:59,120 plus degrees 12183 07:02:55,840 --> 07:03:01,520 celsius we can just say degree c 12184 07:02:59,120 --> 07:03:05,840 so let's try this uh but we also have to 12185 07:03:01,520 --> 07:03:05,840 pack the button so button.pack 12186 07:03:07,120 --> 07:03:12,558 so if we were to try and submit this 12187 07:03:10,320 --> 07:03:13,360 right now scale.get is going to return 12188 07:03:12,558 --> 07:03:16,240 an integer 12189 07:03:13,360 --> 07:03:16,840 so we need to convert this to a string 12190 07:03:16,240 --> 07:03:20,400 so 12191 07:03:16,840 --> 07:03:22,000 str scale.get 12192 07:03:20,400 --> 07:03:23,760 and you have to do that with python if 12193 07:03:22,000 --> 07:03:26,638 you're displaying a 12194 07:03:23,760 --> 07:03:29,760 integer value along with another string 12195 07:03:26,638 --> 07:03:33,120 uh you need to convert it to a string 12196 07:03:29,760 --> 07:03:35,520 so now this should work so it is 82 12197 07:03:33,120 --> 07:03:37,760 degrees celsius 12198 07:03:35,520 --> 07:03:39,680 so let's do some cosmetic changes for 12199 07:03:37,760 --> 07:03:43,040 our temperature gauge 12200 07:03:39,680 --> 07:03:43,040 because right now it's kind of ugly 12201 07:03:43,120 --> 07:03:49,680 so the first thing we'll change is 12202 07:03:46,320 --> 07:03:54,478 let's say the length so length equals 12203 07:03:49,680 --> 07:03:54,478 600 so that should be fairly large 12204 07:03:56,000 --> 07:03:59,760 you can also set the orientation so that 12205 07:03:59,200 --> 07:04:04,320 is 12206 07:03:59,760 --> 07:04:06,558 orient spelt like that 12207 07:04:04,320 --> 07:04:09,120 so this can either be vertical which it 12208 07:04:06,558 --> 07:04:09,120 currently is 12209 07:04:09,200 --> 07:04:19,040 or horizontal 12210 07:04:15,440 --> 07:04:19,040 but i'll keep this as vertical 12211 07:04:19,840 --> 07:04:23,360 so i'll let a comment that this is 12212 07:04:22,000 --> 07:04:27,520 orientation 12213 07:04:23,360 --> 07:04:30,798 of scale let's also change the 12214 07:04:27,520 --> 07:04:34,080 font so font equals 12215 07:04:30,798 --> 07:04:34,080 pick whatever font you want 12216 07:04:34,320 --> 07:04:39,040 and a size so maybe 20. 12217 07:04:41,840 --> 07:04:45,680 now you can add some tick intervals 12218 07:04:46,798 --> 07:04:50,798 maybe i'll set this to 10. so these will 12219 07:04:50,160 --> 07:04:57,840 be 12220 07:04:50,798 --> 07:04:57,840 numeric indicators on the scale 12221 07:05:00,400 --> 07:05:04,718 and show value this hides the current 12222 07:05:04,240 --> 07:05:06,798 value 12223 07:05:04,718 --> 07:05:07,840 so if you set this to zero this will 12224 07:05:06,798 --> 07:05:11,840 hide 12225 07:05:07,840 --> 07:05:11,840 current value 12226 07:05:12,558 --> 07:05:17,200 so it no longer displays what the 12227 07:05:14,080 --> 07:05:19,920 current value is next to the slider 12228 07:05:17,200 --> 07:05:20,958 to actually retrieve it you'd have to 12229 07:05:19,920 --> 07:05:22,798 get the value 12230 07:05:20,958 --> 07:05:24,320 so right now it's 40 but it helps that 12231 07:05:22,798 --> 07:05:26,878 you have the tick intervals on the 12232 07:05:24,320 --> 07:05:31,040 left-hand side 12233 07:05:26,878 --> 07:05:31,040 i should probably add that this adds 12234 07:05:31,440 --> 07:05:38,000 uh numeric 12235 07:05:34,478 --> 07:05:41,920 indicators for 12236 07:05:38,000 --> 07:05:44,000 value good enough let's continue on 12237 07:05:41,920 --> 07:05:46,320 now let's use the set function we can 12238 07:05:44,000 --> 07:05:49,120 set the current value of the knob 12239 07:05:46,320 --> 07:05:50,878 on our scale and by default it is zero 12240 07:05:49,120 --> 07:05:53,680 so we can use the scale 12241 07:05:50,878 --> 07:05:54,558 dot set function and we can place a 12242 07:05:53,680 --> 07:05:57,200 number in here 12243 07:05:54,558 --> 07:05:58,080 a value so let's say we want this to be 12244 07:05:57,200 --> 07:06:00,718 100 12245 07:05:58,080 --> 07:06:01,840 so this is now at the 100 position 12246 07:06:00,718 --> 07:06:03,360 instead of zero 12247 07:06:01,840 --> 07:06:05,520 if we wanted this in the middle we could 12248 07:06:03,360 --> 07:06:07,600 set this to 50 12249 07:06:05,520 --> 07:06:09,280 and now it's in the middle however if 12250 07:06:07,600 --> 07:06:12,878 you change the 12251 07:06:09,280 --> 07:06:15,280 range of your skill of from minus 2 12252 07:06:12,878 --> 07:06:17,280 and set this to 50 now 50 is all the way 12253 07:06:15,280 --> 07:06:18,080 down here so it no longer appears in the 12254 07:06:17,280 --> 07:06:20,240 middle 12255 07:06:18,080 --> 07:06:21,440 so this isn't really necessary but if 12256 07:06:20,240 --> 07:06:22,400 you want this to be a little more 12257 07:06:21,440 --> 07:06:23,840 sophisticated 12258 07:06:22,400 --> 07:06:26,080 there's actually a formula that you 12259 07:06:23,840 --> 07:06:28,638 could use we can use scale 12260 07:06:26,080 --> 07:06:29,120 straight braces and then place the word 12261 07:06:28,638 --> 07:06:31,680 from 12262 07:06:29,120 --> 07:06:32,558 within here this will give us our max 12263 07:06:31,680 --> 07:06:35,600 basically 12264 07:06:32,558 --> 07:06:40,400 uh so this is from without an underscore 12265 07:06:35,600 --> 07:06:43,520 so this not this this 12266 07:06:40,400 --> 07:06:47,200 scale from minus scale 12267 07:06:43,520 --> 07:06:48,798 two and then we're going to divide this 12268 07:06:47,200 --> 07:06:50,000 by two 12269 07:06:48,798 --> 07:06:53,920 and then i'm just going to put this 12270 07:06:50,000 --> 07:06:57,600 within a extra set of parentheses 12271 07:06:53,920 --> 07:07:01,360 plus scale two just in case 12272 07:06:57,600 --> 07:07:03,600 our two value is greater than zero 12273 07:07:01,360 --> 07:07:05,600 we just want to account for that and 12274 07:07:03,600 --> 07:07:06,878 then i'm just going to 12275 07:07:05,600 --> 07:07:08,798 put these within another set of 12276 07:07:06,878 --> 07:07:10,400 parentheses so 12277 07:07:08,798 --> 07:07:14,718 this should always appear in the middle 12278 07:07:10,400 --> 07:07:14,718 then even if we were to change this 12279 07:07:14,840 --> 07:07:18,478 range 12280 07:07:16,478 --> 07:07:20,558 yeah it seems like it works uh like i 12281 07:07:18,478 --> 07:07:21,600 said it's not really necessary but 12282 07:07:20,558 --> 07:07:23,840 if you want this to be a little more 12283 07:07:21,600 --> 07:07:25,680 sophisticated that would work 12284 07:07:23,840 --> 07:07:28,080 now let's make some cosmetic changes to 12285 07:07:25,680 --> 07:07:30,958 this scale and make it look pretty 12286 07:07:28,080 --> 07:07:34,958 so first let's change the trough color 12287 07:07:30,958 --> 07:07:34,958 this is this gray portion right here 12288 07:07:35,280 --> 07:07:42,000 so let's set the trough color 12289 07:07:39,920 --> 07:07:43,440 equals whatever color you want you could 12290 07:07:42,000 --> 07:07:45,840 say like blue 12291 07:07:43,440 --> 07:07:47,440 or you can place a hex value here and i 12292 07:07:45,840 --> 07:07:50,718 actually picked one out already 12293 07:07:47,440 --> 07:07:54,638 so i'm going to use 6 9 e 12294 07:07:50,718 --> 07:07:58,240 a f f and that is this icy blue color 12295 07:07:54,638 --> 07:08:00,878 kind of like this is sliding on ice 12296 07:07:58,240 --> 07:08:03,200 and i'm also going to change the font 12297 07:08:00,878 --> 07:08:03,200 color 12298 07:08:03,440 --> 07:08:07,200 and i'm going to pick this fiery red 12299 07:08:06,160 --> 07:08:12,718 color 12300 07:08:07,200 --> 07:08:14,160 to symbolize heat 12301 07:08:12,718 --> 07:08:16,718 and then you can change the background 12302 07:08:14,160 --> 07:08:20,240 color too 12303 07:08:16,718 --> 07:08:22,878 so that is bg and i'll just set this to 12304 07:08:20,240 --> 07:08:22,878 like black 12305 07:08:24,718 --> 07:08:29,360 it's looking kind of cool now right and 12306 07:08:27,760 --> 07:08:31,200 for no good reason i'm going to add some 12307 07:08:29,360 --> 07:08:32,798 pictures 12308 07:08:31,200 --> 07:08:34,558 so feel free to pause the video if you'd 12309 07:08:32,798 --> 07:08:36,638 like to download some images i'm going 12310 07:08:34,558 --> 07:08:39,280 to use an image of a flame 12311 07:08:36,638 --> 07:08:40,000 for the hot side of our scale and a 12312 07:08:39,280 --> 07:08:43,440 snowflake 12313 07:08:40,000 --> 07:08:46,958 for the cold side so let's begin by 12314 07:08:43,440 --> 07:08:48,080 adding the hot image uh to the scale 12315 07:08:46,958 --> 07:08:50,638 before we actually 12316 07:08:48,080 --> 07:08:52,558 add the scale to the window so we'll do 12317 07:08:50,638 --> 07:08:53,920 this after we instantiate the window 12318 07:08:52,558 --> 07:08:55,040 because that's the only place that we 12319 07:08:53,920 --> 07:08:56,798 can do that 12320 07:08:55,040 --> 07:08:58,080 so we're going to create a image i'll 12321 07:08:56,798 --> 07:09:01,760 just call this hot image 12322 07:08:58,080 --> 07:09:04,798 for the fire icon so hot image equals 12323 07:09:01,760 --> 07:09:08,760 photo image file 12324 07:09:04,798 --> 07:09:12,080 equals this is hot.png so 12325 07:09:08,760 --> 07:09:13,760 hot.png we're going to add the image to 12326 07:09:12,080 --> 07:09:17,440 a label 12327 07:09:13,760 --> 07:09:20,638 i'll call this hot label hot label 12328 07:09:17,440 --> 07:09:24,160 label image equals 12329 07:09:20,638 --> 07:09:29,120 hot image and then we need to pack this 12330 07:09:24,160 --> 07:09:31,200 so hot label dot pack 12331 07:09:29,120 --> 07:09:32,320 and this should appear at the top let's 12332 07:09:31,200 --> 07:09:35,040 test it 12333 07:09:32,320 --> 07:09:37,760 yep there it is let's do the same thing 12334 07:09:35,040 --> 07:09:41,520 for the cold side 12335 07:09:37,760 --> 07:09:44,240 so i'll add this here before the button 12336 07:09:41,520 --> 07:09:45,958 and i'm going to replace hot with cold 12337 07:09:44,240 --> 07:09:48,958 same thing with the file name 12338 07:09:45,958 --> 07:09:48,958 cold.png 12339 07:09:49,360 --> 07:09:57,120 this will be cold label cold 12340 07:09:52,798 --> 07:09:59,920 image and cold label dot pack 12341 07:09:57,120 --> 07:10:02,160 and that should be it let's take a look 12342 07:09:59,920 --> 07:10:03,840 not too shabby it's looking pretty sweet 12343 07:10:02,160 --> 07:10:05,440 it has our submit button at the bottom 12344 07:10:03,840 --> 07:10:07,440 too uh so 12345 07:10:05,440 --> 07:10:09,040 right now the temperature is 100 degrees 12346 07:10:07,440 --> 07:10:11,600 celsius 12347 07:10:09,040 --> 07:10:14,160 the temperature is 0 degrees celsius the 12348 07:10:11,600 --> 07:10:16,240 temperature is 60 degrees celsius 12349 07:10:14,160 --> 07:10:17,840 well that's how to create a scale in 12350 07:10:16,240 --> 07:10:19,360 python if you would like a copy of all 12351 07:10:17,840 --> 07:10:20,478 this code i'll post all of this in the 12352 07:10:19,360 --> 07:10:22,478 comments down below 12353 07:10:20,478 --> 07:10:24,478 but yeah that's how to create a scale in 12354 07:10:22,478 --> 07:10:26,160 python 12355 07:10:24,478 --> 07:10:27,920 hey what's going on everybody it's your 12356 07:10:26,160 --> 07:10:29,280 bro here hope you're doing well and in 12357 07:10:27,920 --> 07:10:31,760 this video i'm going to teach you guys 12358 07:10:29,280 --> 07:10:32,080 how we can create a list box in python 12359 07:10:31,760 --> 07:10:35,920 so 12360 07:10:32,080 --> 07:10:35,920 sit back relax and enjoy the show 12361 07:10:36,638 --> 07:10:40,558 all right you wonderful people let's 12362 07:10:38,320 --> 07:10:43,200 create a list box a list box 12363 07:10:40,558 --> 07:10:44,958 is a listing of selectable text items 12364 07:10:43,200 --> 07:10:47,440 within its own container 12365 07:10:44,958 --> 07:10:48,478 so as always we'll need a window window 12366 07:10:47,440 --> 07:10:51,280 equals tk 12367 07:10:48,478 --> 07:10:52,558 followed by window dot main loop let's 12368 07:10:51,280 --> 07:10:55,120 create a list box 12369 07:10:52,558 --> 07:10:55,920 simply called list box big surprise 12370 07:10:55,120 --> 07:10:59,040 there right 12371 07:10:55,920 --> 07:11:00,478 so list box equals list box what are we 12372 07:10:59,040 --> 07:11:01,840 adding this list box to 12373 07:11:00,478 --> 07:11:03,600 we're going to place that here within 12374 07:11:01,840 --> 07:11:05,280 the parentheses of the constructor 12375 07:11:03,600 --> 07:11:07,718 we're adding this to our window and then 12376 07:11:05,280 --> 07:11:09,878 we're going to follow this with 12377 07:11:07,718 --> 07:11:11,840 listbox.pack you can also do 12378 07:11:09,878 --> 07:11:12,718 listbox.place as well and place some 12379 07:11:11,840 --> 07:11:14,400 coordinates but 12380 07:11:12,718 --> 07:11:16,240 we've always done pack let's just stick 12381 07:11:14,400 --> 07:11:18,400 with that for now all right so this is 12382 07:11:16,240 --> 07:11:20,718 what our list box looks like 12383 07:11:18,400 --> 07:11:22,718 it's a small rectangular box and we're 12384 07:11:20,718 --> 07:11:23,520 going to add some text items that are 12385 07:11:22,718 --> 07:11:25,600 selectable 12386 07:11:23,520 --> 07:11:27,600 and eventually we'll be able to submit a 12387 07:11:25,600 --> 07:11:30,000 choice so i have an idea 12388 07:11:27,600 --> 07:11:32,000 let's create an online menu like we're 12389 07:11:30,000 --> 07:11:34,320 ordering food from a restaurant 12390 07:11:32,000 --> 07:11:36,798 let's say it's bros italian restaurant 12391 07:11:34,320 --> 07:11:39,440 so let's add some food items to our menu 12392 07:11:36,798 --> 07:11:40,558 and somebody can submit an order so 12393 07:11:39,440 --> 07:11:42,558 we'll need to use the 12394 07:11:40,558 --> 07:11:43,920 insert function of the list box to add 12395 07:11:42,558 --> 07:11:47,200 some items so 12396 07:11:43,920 --> 07:11:50,478 list box dot insert we place 12397 07:11:47,200 --> 07:11:51,040 an index and a text item so let's say 12398 07:11:50,478 --> 07:11:54,080 for 12399 07:11:51,040 --> 07:11:56,878 index one we'll have pizza 12400 07:11:54,080 --> 07:11:58,000 and this is what we end up with we have 12401 07:11:56,878 --> 07:12:00,558 the item pizza 12402 07:11:58,000 --> 07:12:02,240 it's selectable and well that's really 12403 07:12:00,558 --> 07:12:04,558 it we'll need to create a submit button 12404 07:12:02,240 --> 07:12:06,558 and populate the rest of this list box 12405 07:12:04,558 --> 07:12:10,000 so let's add a few other items for our 12406 07:12:06,558 --> 07:12:13,120 menu so let's say we'll have five items 12407 07:12:10,000 --> 07:12:16,718 and we need an index for each so one two 12408 07:12:13,120 --> 07:12:18,798 three four and five so we have 12409 07:12:16,718 --> 07:12:20,878 pizza for the first item for the second 12410 07:12:18,798 --> 07:12:23,440 item let's have pasta 12411 07:12:20,878 --> 07:12:24,080 and third let's say garlic bread because 12412 07:12:23,440 --> 07:12:26,958 garlic bread 12413 07:12:24,080 --> 07:12:30,400 is awesome and restaurants usually have 12414 07:12:26,958 --> 07:12:31,600 appetizers so why not soup and salad 12415 07:12:30,400 --> 07:12:33,680 and that should be enough for this 12416 07:12:31,600 --> 07:12:36,080 example 12417 07:12:33,680 --> 07:12:37,520 and this is what we have we have our 12418 07:12:36,080 --> 07:12:40,638 menu and we can select 12419 07:12:37,520 --> 07:12:42,798 an item from this menu so let's 12420 07:12:40,638 --> 07:12:45,120 customize the appearance of this menu 12421 07:12:42,798 --> 07:12:46,958 well because we can so let's do that 12422 07:12:45,120 --> 07:12:48,320 let's change the background color so we 12423 07:12:46,958 --> 07:12:50,478 do that with the option 12424 07:12:48,320 --> 07:12:52,878 bg for background and you can set this 12425 07:12:50,478 --> 07:12:55,840 equal to a color of your choice 12426 07:12:52,878 --> 07:12:57,360 or a hex value so i have a hex color 12427 07:12:55,840 --> 07:13:01,360 value already picked out 12428 07:12:57,360 --> 07:13:02,320 it is f7fde and this has the appearance 12429 07:13:01,360 --> 07:13:04,798 of i would say 12430 07:13:02,320 --> 07:13:06,080 paper like it's kind of faded i think 12431 07:13:04,798 --> 07:13:08,400 that would fit some sort 12432 07:13:06,080 --> 07:13:10,400 of menu for a restaurant but pick 12433 07:13:08,400 --> 07:13:13,600 whatever background color you want 12434 07:13:10,400 --> 07:13:16,558 so let's change the font next so we can 12435 07:13:13,600 --> 07:13:21,840 do that with the option font 12436 07:13:16,558 --> 07:13:21,840 and i will pick the font 12437 07:13:22,638 --> 07:13:26,000 it's somewhat fancy i think it would fit 12438 07:13:24,638 --> 07:13:29,600 like a restaurant menu 12439 07:13:26,000 --> 07:13:32,798 and then a size of 35. 12440 07:13:29,600 --> 07:13:35,040 yeah we're getting somewhere we can also 12441 07:13:32,798 --> 07:13:39,680 change the width and the height 12442 07:13:35,040 --> 07:13:41,760 so width equals let's say 12. 12443 07:13:39,680 --> 07:13:43,360 that's not too bad for a width let's 12444 07:13:41,760 --> 07:13:46,400 also change the height 12445 07:13:43,360 --> 07:13:49,280 so we can do that with a function 12446 07:13:46,400 --> 07:13:50,958 of our list box so let's get the current 12447 07:13:49,280 --> 07:13:53,120 size of the list box 12448 07:13:50,958 --> 07:13:55,120 and let's actually do that after we 12449 07:13:53,120 --> 07:13:55,760 insert all of the items within our list 12450 07:13:55,120 --> 07:13:58,478 box 12451 07:13:55,760 --> 07:13:59,360 so to change the size of our list box 12452 07:13:58,478 --> 07:14:02,400 what we do 12453 07:13:59,360 --> 07:14:05,040 is type in list box 12454 07:14:02,400 --> 07:14:06,478 dot config you usually use config if you 12455 07:14:05,040 --> 07:14:09,600 need to change any uh 12456 07:14:06,478 --> 07:14:13,520 options and we want to change the 12457 07:14:09,600 --> 07:14:17,520 length of or the height of this list box 12458 07:14:13,520 --> 07:14:20,958 so we're going to say height equals 12459 07:14:17,520 --> 07:14:24,320 list box dot 12460 07:14:20,958 --> 07:14:25,680 size function so then this will adjust 821403

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