0:01 in this full course you will learn the
0:03 basics of python programming
0:06 i'm beau carnes with freecodecamp.org
0:07 i've previously created one of the most
0:10 popular javascript courses on youtube
0:13 and i've created many python tutorials
0:15 now i've created this complete python
0:17 course for beginners you don't need any
0:19 previous programming experience to
0:21 follow along and all you need to code in
0:23 python is a web browser in this course i
0:25 will teach you all the core aspects of
0:27 the python programming language and i
0:30 will simplify the more complex topics
0:31 python is considered one of the most
0:33 popular programming languages in the
0:35 world and it's only growing in
0:37 popularity python excels in a wide
0:39 variety of scenarios such as shell
0:42 scripting task automation and web
0:44 development and it's also the language
0:46 of choice for data analysis and machine
0:49 learning but it can also adapt to create
0:52 games and work with embedded devices
0:54 we're going to jump right into it so you
0:55 can start coding your first python
0:58 program as soon as possible to get
1:00 started quickly we'll use a replit which
1:02 is an online ide that allows users to
1:05 code and run programs in a variety of
1:08 different languages all in a web browser
1:09 and later i'll show you how to get
1:11 python set up on your local operating
1:14 system after the first project i'll go
1:16 into more detail about each of the main
1:18 features of python the section is
1:20 comprehensive and detailed and in the
1:22 final section you will use what you've
1:24 been learning to code a blackjack game
1:26 with me guiding you every step of the
1:28 way throughout the course there will be
1:30 a little repetition of some of the key
1:32 python programming concepts to make sure
1:33 you have a deep understanding of the
1:36 language so let's get started we're
1:38 going to start by creating a simple rock
1:41 paper scissors game and we'll start by
1:44 going to replit.com replied provided a
1:46 grant that made this course possible and
1:48 replie is going to make it super easy to
1:50 get up and running really quickly so you
1:53 can either sign up or log in
1:54 and create an account i'm just going to
1:56 use my google account
1:59 okay now that you're logged into replit
2:01 you can either just click the create
2:04 button or this plus button over here to
2:06 create a new replit and i'll make sure
2:10 to create a python replit but you can
2:12 see you can also select all sorts of
2:14 different programming languages oh these
2:15 are just the ones that start with the
2:18 word python but so there's there's tons
2:19 of different programming languages you
2:21 can select but in this case we are just
2:24 going to use python and then i'll click
2:31 okay so let me just kind of show off
2:33 replica a little bit
2:34 this is where we're going to create our
2:36 python code i'm going to zoom in just a
2:38 little bit so we're going to write the
2:40 code right here and then we can see some
2:42 output over on the right side and then
2:44 you can create different files over on
2:46 the left side here
2:48 and then there's some other things like
2:50 you can connect to version control and
2:51 and
2:52 if you have environment variables we're
2:54 not even going to be discussing those in
2:56 this course there's a debugger you can
2:58 connect to a database and just some
3:00 other things but we're mainly going to
3:02 just be using this main.pi program to
3:04 write our program and we're going to see
3:06 the results in the console so i'm just
3:09 going to close this files window so
3:11 it's a little bigger here
3:13 i'm going to start off by showing you
3:17 how to create a variable with python so
3:19 this is a rock paper scissors game and
3:21 there's going to be a players a player
3:23 is going to have a choice and a computer
3:25 is going to have a choice so i'm going
3:29 to create a variable called player choice
3:30 choice
3:33 and i'm going to set that equal to rock
3:34 rock
3:36 so let's look at a few components about
3:39 this this is the variable name player choice
3:40 choice
3:42 and you can see
3:43 if you
3:45 we use an underscore that's just kind of
3:48 the convention for python to use an
3:49 underscore if you're going to have a
3:51 space in the variable name and we're
3:53 going to assign it that's what this
3:55 equal sign this is the assign operator
3:58 and we're going to assign it to a string
4:00 a string is just a word or a collection
4:02 of characters like rock and we're going
4:04 to put quotation marks around it now we
4:06 could have also used a single quotes
4:08 instead of double quotes as long as you
4:10 use the same quote on each side that's
4:13 what's important so we've now created a variable
4:14 variable called
4:15 called
4:18 playerchoice and assigned it to rock and
4:20 now we can reference this variable later
4:22 and whenever we reference the variable
4:25 called playerchoice it's going to
4:27 the code is going to automatically
4:31 replace that player choice with rock
4:33 so this is going to be a very
4:36 interactive project i hope you're
4:38 following along i hope you have already
4:40 got replit loaded up like this now
4:42 throughout this project i'm going to
4:45 tell you what the next thing to do is
4:48 and i want you to try doing it yourself
4:52 before you watch what i'm going to do so
4:53 periodically you'll want to pause the video
4:55 video
4:57 based on and what i say you and try to
4:58 implement what i say
5:00 before you come back to the video and
5:02 watch me implement it and see if you've
5:05 implemented the the same way
5:06 so i'm just going to zoom in one more
5:08 time and
5:10 this is the first thing i want you to do
5:13 see if you can make another variable on
5:14 the next line so you're going to press
5:16 return or enter to go to the next line
5:18 and this variable should be called
5:21 computer choice and you should set it to equal
5:22 equal paper
5:24 paper
5:25 okay so you can pause the video and see
5:26 if you can make a variable called
5:28 computer choice and set it to equal paper
5:33 so here it's pretty simple here it's
5:35 going to start simple but it's going to
5:38 get harder as we go so computer choice equals
5:40 equals
5:42 paper okay so like i said it's starting
5:43 simple but it's going to get more
5:45 complex as we go along
5:47 if you've done that you've now written
5:49 your first line of python code in this course
5:50 course
5:52 okay now i'm going to talk about functions
5:53 functions
5:56 a function is a set of code which only
5:58 runs when it is called
6:00 so i'm going to show you how to put this
6:03 code into a function
6:05 now one thing about python is that
6:08 indentation is very important
6:11 so after we create a we define the name
6:14 of a function any line of code that's
6:16 indented the same amount is considered
6:18 within that function
6:21 so i'm going to create a new line of
6:24 code at the top and i'm going to call it get
6:26 get choices
6:35 okay so we define the function with def
6:37 and get choices and i'm going to select
6:39 all these these two lines of code at the
6:42 same time and just press the tab key and
6:44 that's going to indent all these the
6:46 same amount
6:48 and you can see sometimes they'll be
6:50 squiggly lines and if you hover over
6:52 some of the squiggly lines it will tell
6:54 you something in this case it just says
6:56 the local variable called player choice
6:59 is assigned to but never used that's not
7:01 necessarily bad it's just it's just
7:03 telling us that usually if you create a
7:04 variable you're going to want to use it
7:06 later well we are going to use it later
7:08 we just haven't gotten to it yet so
7:10 sometimes the squiggly lines will
7:12 indicate there's some sort of error in
7:13 the code
7:16 usually i think it's the color red will
7:17 indicate an error but if it's a
7:19 different color it just may mean that
7:21 there's something maybe not quite right but
7:22 but
7:25 it's not really that big of a deal so if
7:27 you have a variable that's assigned to
7:29 but never used that's not going to stop
7:30 your program
7:32 but it's just saying that it's not this
7:34 variable isn't really being used for
7:36 anything yet but we will change that
7:37 this function i'm going to show you how
7:39 to call a function later but we're
7:41 creating a function called getchoices
7:45 that assigns these two variables and
7:46 it's also i'm going to put another line
7:49 at the very end here and it's a return statement
7:51 statement
7:55 and i'm going to return player choice
7:56 choice
7:58 this will indicate what's returned when
8:00 this function is called so later we'll
8:02 call this function get choices and it
8:04 will return
8:06 something it will return the player
8:07 choice which is right here we'll turn in
8:10 this case rock that we can use somewhere
8:12 else in our code
8:14 and i did just happen to put an extra
8:16 line here
8:18 that's just that's optional i put a
8:20 blank line here just to kind of
8:22 make things easy to kind of organize the
8:24 code a little bit so
8:26 different sometimes i'll just put an
8:28 extra line between different sections of
8:30 code and it just makes it easier to
8:32 identify the different sections when
8:33 you're looking at the code
8:36 for the computer those extra lines don't
8:39 mean anything the indentation though
8:41 definitely does mean something
8:43 so as long as every line of code is
8:46 indented the same amount as the previous
8:48 line of code then it's all within the
8:49 same function
8:51 okay so this is what i want you to do
8:53 see if you can change the return
8:55 statement so instead of returning player
8:58 choice it's returning the computer choice
9:04 so that's pretty simple
9:06 it's now returning the computer choice here
9:07 here
9:08 now i'm going to create another function
9:11 down here this is just going to be an
9:12 example function just so i can
9:14 demonstrate something to you and then
9:15 we'll delete it it's not going to be
9:17 part of our rock paper scissors game but
9:19 i'm going to create a new function
9:20 called greeting
9:23 and then i am going to add what it's
9:25 going to do oh yeah put the semicolon
9:27 you also know a colon so a function
9:29 always has to have a colon at the end of
9:33 how we define it so i'm going to return
9:36 a string and it's just going to say hi
9:38 and one thing you'll notice is that
9:41 there's no you don't have to put any
9:42 anything at the end of each line some
9:45 programming languages such as javascript
9:46 you're going to put a semicolon at the
9:48 line at the end of the line
9:50 but in python it doesn't matter you
9:51 don't put anything at the end of each
9:54 line so now i'm going to call the
9:55 function to call the function i just
9:58 type the name and i put the parentheses
10:00 at the end so it's going to say so
10:03 greeting is now going to call it's going
10:04 to call this function and that's going
10:06 to return the string hi
10:08 now it's not going to do anything with
10:10 the string because our program doesn't
10:12 do anything with the string that's been
10:14 returned it's not going to go into the
10:16 console or anything but let's add
10:20 additional code so we will
10:23 make this string hi go on to the console
10:26 first let me create a variable that
10:28 takes what this greeting function
10:32 returns so i'm going to type in response
10:35 equals greeting so now we set the the
10:37 what greeting is being returned to this
10:40 variable called response and now i can
10:42 use the print function
10:45 and i can print the response and this
10:48 will print it to the console so now i
10:49 can we have this green button here this
10:51 runs the program i'll click this green
10:54 button here and now we can see it's high so
10:55 so
10:57 this response is getting this high from
10:59 here and then we're printing it right
11:02 there okay i'm actually going to delete
11:07 and what i want you to try to do now is
11:10 to call the get choices function and
11:12 store the response in a variable called
11:15 choices and then print the value of
11:17 choices to the console
11:18 so you can pause and try that really
11:20 quick and then i'm going to show you how
11:22 okay so remember the variable is called choices
11:24 choices
11:26 and we're going to set that equal to
11:29 the get choices variable now ins or the
11:31 get choices function now instead of
11:34 typing out get choices you can see that
11:37 this code editor is actually giving us
11:39 suggestions on what we want to put in
11:42 here so i just typed in get and you can
11:44 see it's now saying get choices right
11:45 here so i can instead of typing out the
11:48 whole thing i can either click there or
11:50 i can just press the tab key and it's
11:51 going to fill in the rest of that
11:53 function name so that's just something
11:56 little that makes it easier to write a
11:57 program and you don't have to type out
11:59 every single thing so if you've already
12:02 created a function or a variable then
12:05 the the code editor then reply will will
12:07 suggest what you may want to fill in
12:09 when you're typing and most other code
12:12 editors do something similar it's called
12:14 code completion so
12:15 i'm going to but i still have to add
12:18 parentheses at the end and now i'm going
12:20 to print
12:21 the choices [Music]
12:22 [Music]
12:25 okay so i'm gonna just click this play
12:27 button to make sure it does what we're
12:30 trying to do yep it clicked paper great
12:31 great
12:33 now let's talk about dictionaries
12:36 dictionaries in python are used to store
12:40 data values in key value pairs so let me
12:46 okay so a dictionary is going to have
12:48 these curly braces at the beginning and
12:51 end and then here's a key value pair
12:53 there's one it's separated by a comma
12:55 and then here's another key value pair
12:58 so we're setting name
13:00 we're setting that to equal bow and
13:01 we're saying the color
13:05 to equal blue so here's the key here's
13:07 the value here's the key
13:08 here's the value
13:11 and the key or the value in a dictionary
13:12 can be a variable
13:15 like the color we could set that we
13:17 already created this variable here i'm
13:19 just going to copy the word choices and
13:21 we can put choices and you can see now
13:23 it's not surrounded by the quotation
13:26 marks so if you surrounded by quotation
13:27 marks it's a string but if we don't
13:30 surround it by quotation marks it's
13:32 getting the the variable here choices so
13:35 this would be set to
13:37 paper because that's what choice is
13:38 going to is going to equal so we're
13:40 about to delete this line because that
13:41 was just an example
13:44 but now before the return statement in
13:46 this function we're going to add a new
13:48 line we're going to create a variable
13:50 named choices and make it equal to a
13:53 dictionary the first key should be
13:56 player with a value of the player choice
13:58 actually this variable here
14:01 the second key should be computer with a
14:03 value of computer choice
14:05 and then we're going to update the
14:07 return statement
14:10 to return the the choices dictionary
14:12 now i'm not going to keep telling you to
14:14 pause and try it yourself so just the
14:16 rest of this time whenever i say what
14:18 we're going to do you'll know that after
14:20 i get done explaining it you can pause
14:22 it and try it yourself but that's what
14:29 so i'm going to create choices
14:31 and it's going to be a dictionary and
14:33 then it's going to have
14:35 a player
14:37 that's the key and the value is going to
14:39 be player
14:40 choice and i was just able to press tab
14:43 to fill that in and then we're going to
14:44 have computer
14:46 and it's going to be
14:49 computer choice
14:51 and you can see sometimes if it goes to
14:53 the next line there just won't be a line
14:54 number right here
14:56 but i can
14:59 move this over for now
15:02 and then i am going to oh i forgot to
15:04 put the equal sign that's what that red
15:06 squiggly line means so because the red
15:08 means there is a problem in the program
15:09 so it's not going to run correctly now
15:11 the orange just means that we haven't
15:14 used choices yet which we will right now
15:15 because we're going to return choices
15:17 choices choices
15:23 okay so now we don't have any squiggly
15:25 lines because we're using every variable
15:26 that we created and we're returning
15:29 choices here
15:31 so you may have noticed that player choice
15:32 choice
15:34 it does not actually get set to the
15:36 player's actual choice
15:38 so let's fix that
15:41 the input function gets input from a
15:44 user and will use it to get the player's choice
15:45 choice
15:47 so instead of having players choice
15:49 equal rock we're going to make players
15:59 rock paper
16:01 paper scissors
16:06 okay so this is how you get input from a
16:09 user we use the input function and this
16:10 is going to be something that's going to
16:11 be printed
16:13 we'll print to the console here
16:16 and whatever the result of this input
16:18 will be that the the player that the
16:20 user entered will get stored to this
16:22 variable which we then can use later in
16:24 our program
16:26 so let's just try that out i'll click run
16:27 run and
16:31 enter
16:33 let me just stop and run it again
16:35 okay enter a choice rock paper scissors
16:37 i'll just put rock
16:39 and now you can see the player's choice
16:42 we're still printing that and it's going
16:44 to print as rock
16:47 great now let's just clean up this code
16:49 a little bit we still have this here we
16:50 don't need that dictionary that's not
16:52 going to be part of our final code
16:54 and now we'll make it so the computer
16:55 can actually make a choice
16:57 so we're going to learn about importing
17:00 libraries creating lists and calling methods
17:01 methods
17:03 python libraries are a set of useful
17:04 functions so you don't have to write
17:06 code from scratch when you import a
17:09 library to your program you get access
17:10 to more features without writing
17:12 additional code
17:14 with basic python it's challenging to
17:16 get your program to do something
17:18 randomly but it's easy to choose
17:20 something randomly using the random library
17:21 library
17:24 so let me show you how we can import the
17:26 random library
17:28 import statements are used to import
17:29 libraries and they're usually put at the
17:32 top of a program so i'm going to press
17:34 enter a few times here to add some lines
17:36 at the top and i'm going to do import random
17:38 random
17:40 so now we are going we we've
17:43 imported the random library
17:44 so we're going to use that random
17:47 library soon but now let's learn about lists
17:48 lists
17:50 a list in python is used to store
17:53 multiple items in a single variable
17:54 lists are surrounded by brackets and
17:56 each item is separated by a comma so
17:58 here's an example i could create a
18:00 variable called food and set it to this
18:02 list it's going to have three items pizza
18:07 carrots
18:09 and eggs [Music]
18:10 [Music]
18:15 so this is a list of strings
18:17 and then you can also get a random item
18:19 from the list
18:21 using we're going to use now we've
18:23 imported random we can get a random item
18:26 by using that that random library so i'm
18:28 going to put dinner [Music]
18:29 [Music] equals
18:30 equals
18:33 random dot choice
18:36 and then i'm going to pass in the the
18:37 list here
18:40 so using the random library we can call
18:43 choice and then we can pass in
18:45 a list and it's going to choose a random
18:47 item from that list and
18:52 and set it to equal this dinner variable
18:54 so right now the computer choice always
18:57 equals paper but we want it to be a
18:59 random choice between rock scissors and paper
19:01 paper
19:02 so before the computer choice variable
19:05 is created we'll create a new variable
19:07 called options and assign it to a list
19:10 of the possible options rock paper
19:11 scissors then we'll set the computer
19:14 choice variable to be a random choice of
19:18 one of the items in the options list
19:20 okay i hope you already tried this now
19:22 but let me show you how that's going to work
19:24 work
19:27 we'll create options
19:29 and we'll set it equal to this list of
19:31 of rock
19:36 [Music]
19:39 paper scissors
19:40 and you can see the code editor will
19:43 often pop up these boxes with more
19:45 information about what we're doing to
19:48 give us some some help with what we're
19:50 trying to do here so we're going to set
19:52 this computer choice to be random
19:54 random
19:56 dot choice
19:59 food not food i was looking at the food
20:04 there we go
20:07 and let's just um try running this
20:10 program and seeing what happens so i'm
20:12 going to put rock
20:14 and then we see the computer chose
20:16 scissor it should be scissors that's why
20:19 we're testing it out i guess i
20:22 spelled that wrong okay so scissors with
20:26 an s so now let me try running it again
20:28 and you can see it shows scissors but if
20:30 we run it enough time it should now it's
20:32 choosing paper because it's choosing it
20:33 at random okay
20:34 okay
20:36 that's working
20:39 okay so now let's just delete all this
20:41 code after the get choices function we
20:42 don't need to test that get choices
20:45 function anymore and now
20:48 let's create a new function called check when
20:50 when
20:51 so you shouldn't know enough how to you
20:53 should know enough about how to create a function
20:54 function
20:55 so def check
20:59 [Music] when
21:00 when
21:02 so this is just an empty function with
21:04 nothing inside it yet
21:07 but before we add oh with the
21:10 colon so before we add any code inside
21:12 the function we're going to create some arguments
21:15 arguments
21:17 function can re functions can receive
21:19 data when they're called the data are
21:20 called arguments so when creating a
21:22 function you can specify arguments
21:24 inside the parentheses so we've been
21:26 using this empty parentheses but i can
21:29 uh put in tooth i can put in things
21:31 within these parentheses
21:33 when this function is called we're going
21:36 to give it two pieces of data we're
21:38 going to pass two pieces of data into
21:41 the function the first piece of data is
21:43 going to be player the second piece of
21:45 data is going to be computer
21:47 so the you can basically call these
21:50 anything you want uh these are just
21:53 we're creating new variables but when we
21:55 call these functions we'll pass in two
21:57 pieces of information that will then be
22:00 assigned to the variable names player
22:03 and computer that we can use inside the function
22:04 function
22:06 so for now let's
22:08 finish off with to a for a complete
22:10 function the function has to have some
22:11 code within the function
22:13 so let's just add a return statement
22:16 that's just going to return a list
22:18 containing the elements player and computer
22:23 so this check when function shouldn't
22:25 actually return this
22:27 this is just to kind of get get it
22:29 quickly created it should actually
22:31 return different things depending on the
22:34 player and computer arguments
22:36 an if statement will allow a program to
22:38 do different things depending on certain conditions
22:39 conditions
22:41 so an if statement will first check a
22:43 condition and if the condition is true
22:46 then all the lines of code under the if
22:47 statement that are indented the same
22:50 amount will execute so as a quick
22:52 example i can say
22:54 a equals three
22:56 b equals five
22:58 and then we can create an if statement here
22:59 here if
23:00 if
23:03 a is greater than b
23:04 b
23:12 yes
23:14 or we can do if a is
23:17 less than b or we can do if we want to
23:20 check if a and b are equal we can do
23:23 eq we we'll put two equal signs now this
23:24 is very important you never want to use
23:27 one equal sign because a single equal
23:29 sign is the assignment operator that's
23:31 how you assign what variables are equal
23:34 to so if you use a single equal sign or
23:37 like then if i put if a single equal
23:40 sign b without a double equal sign that
23:42 is going to
23:44 set a to equal b which is not what we
23:47 want the double equal sign
23:50 is going to check if a and b are the
23:52 same value basically it checks if two
23:55 values values are equal now you can also do
23:55 do
23:57 a not equal so if you use the
24:00 exclamation point that's not equal or
24:02 you can use
24:05 less than or equal to or you can do more
24:06 than or equal to
24:08 so i'm just going to delete all this for now
24:09 now
24:11 so now we're going to update this risk
24:13 turn statement uh before the return
24:15 statement we want to we're going to
24:20 check if player if player equals computer
24:21 computer
24:25 and if so if true will return the string
24:28 it's a tie
24:31 so let's do that if player
24:33 equals computer and this is something
24:35 that maybe you figured out yourself
24:38 before you before you're watching this
24:40 will return a string and the string is
24:42 going to be
24:50 okay so now it's only going to return so
24:52 a function does not have to return something
24:53 something
24:55 and for this function it's only going to
24:56 return something
24:58 if this is true if player equals computer
24:59 computer
25:02 if not it won't return anything and just
25:05 to make make you notice this see this
25:06 this
25:09 line is indented within the if statement
25:13 which is indented within this function
25:15 and just really quick thing to note
25:17 for a return statement parentheses are
25:19 optional so i could also
25:22 add parentheses like that but you don't
25:24 need them but for now i'm just gonna get
25:26 rid of them [Music]
25:28 [Music]
25:30 so currently when there's a tie the
25:32 program now returns it's a tie
25:35 but how does the player know that's true
25:36 now let's have the program print which
25:39 options that the player and the computer chose
25:40 chose
25:43 you can concatenate strings with the
25:44 plus operator
25:47 that just means you can combine strings
25:49 with other strings or strings with variables
25:50 variables
25:53 so let me show you how you can print
25:56 which options were chosen so i'm going
26:07 and then i'll put player so you chose
26:09 and then we have to add a space here
26:10 because it's going to print a space and
26:13 then it will choose it will print this
26:16 so if the player chose rock we'll say
26:18 you chose rock
26:20 and then we can concave so we
26:22 concatenated these together but we can
26:24 add another plus sign
26:27 and put another string here and it's
26:28 going to say
26:30 computer chose oh it's like being
26:44 now i'm just about to type it in but see
26:46 if you can figure out what to add at the
26:47 end here to put in what the computer chose
26:53 so computer chose
26:55 computer or the the value of this
26:59 so
27:02 a lot of times when with programming
27:05 there's a bunch of ways to do the same
27:08 thing so this is one way to combine
27:10 strings and variables together there's
27:13 another way that's a little simpler
27:15 called an f string
27:17 so an f string will allow you to make
27:20 strings with and with variables and
27:21 other python code
27:25 so to do that you just put a variable or
27:26 you just put f at the beginning of a
27:29 string so let me just give you an
27:32 example so we do age equals 25 we're
27:33 going to create a variable and then i'm
27:35 going to make an f string we'll make it
27:37 a print statement and i'm going to put a
27:39 string in here but instead of starting
27:40 with a quotation mark it's going to
27:41 start with an f
27:44 and then i'm going to put gem is and
27:46 then whenever you want to put a variable
27:47 or any kind of python code we're going
27:50 to put some curly braces and i'm just
27:52 going to put the variable within the
27:53 curly braces
27:56 so years old and i'll put the end of the string
27:57 string
28:00 or let me put a
28:01 period here
28:04 okay so gem is
28:08 age which is going to be 25 years old so
28:10 the f string is just a slightly simpler
28:13 way to combine the strings and the
28:16 variables so what i want you to do see
28:17 if you can figure out how to update this
28:20 line right here so it uses an f string
28:22 and it uses these curly braces instead
28:30 so yeah we're going to put f and i can delete
28:32 delete
28:34 a lot of these
28:36 things here [Music]
28:40 [Music]
28:41 and then i'm going to put some curly
28:49 there we go okay you cho there was a
28:51 comma here you chose player computer chose
28:53 chose computer
28:58 okay so now we've been able to put in
29:01 the variable within this string with the
29:09 so we're going to test this out uh so
29:11 in a code
29:14 the code in this function never gets run
29:17 so when we press the run button it's not
29:18 going to run any of the code within the
29:21 function unless the function is called
29:23 within the program so
29:24 so
29:25 we're not going to test out this
29:27 function all right now what i want you
29:30 to do is see if you can add a line to
29:32 call the check when function and then
29:38 so we're going to just do
29:41 check when rock
29:42 rock
29:44 or actually we have to pass in strings
29:47 here rock paper
29:48 paper so
29:50 so
29:52 this is going to it's going to call this
29:54 function with rock in terms instead of
29:57 player and paper instead of computer
29:59 so let me try stride running this program
30:01 program
30:04 you chose rock computer chose paper and
30:05 just don't worry about these little
30:08 icons sometimes they just block what's
30:09 in there but it's still behind that
30:12 little search icon so it's to see it's
30:15 doing you chose rock computer chose paper
30:16 paper
30:18 now let's get back to checking the winner
30:19 winner
30:21 so far this function is only going to
30:23 check if there's a tie
30:25 now we'll start adding code to check
30:27 different wing conditions
30:30 so let's learn about else and elif statements
30:32 statements
30:34 okay so down here i'm just going to give
30:36 you i'm just going to paste an example
30:38 so so here's the if statement if age is
30:40 greater than or equal to 18 it's going
30:43 to print this else so anytime this is
30:45 not true then it's going to print you
30:47 are a child
30:50 okay now here i've combined it with
30:52 something else the ellis statement ls
30:55 just stands for else if it combines else
30:57 and if so you have to put a condition so
30:59 if age is greater than or equal to 18
31:02 print you're an adult else if now we're
31:04 going to check if age is more than
31:07 greater than 12 you were a teenager
31:10 else if age is greater than 1 print you
31:12 are a child
31:14 or else if none of these other things
31:22 so and it's it's only going to do one of these
31:23 these
31:24 it's going to get to the for once it
31:27 gets to the first one that's true then
31:28 it's not going to check the rest it's
31:30 just going to kind of go to the next
31:33 line of code after all these statements
31:35 so it's just going to choose one and
31:36 once it gets the first one that's true
31:38 then it's be done with this whole
31:44 you can also check for two conditions at
31:46 once let me give you an example i'm just
31:48 going to delete all this code and i'm
31:52 going to create an alif statement here elif
31:53 elif
31:55 else if and we're going to check if
32:00 player is equal to rock
32:02 and i'm just going to type in the word and
32:02 and
32:04 and computer
32:06 is equal to scissors
32:07 scissors
32:09 so now i'm checking if
32:11 both these conditions have to be true so
32:13 this condition and
32:15 this condition have to be true before
32:16 the following
32:18 statement will happen which we're going
32:24 rock smashes [Music]
32:26 [Music]
32:29 scissors you
32:31 you when [Music]
32:32 [Music]
32:33 okay now
32:34 the next thing we're going to do let's
32:35 see if we can figure out how to do this
32:38 we're going to add another lf statement
32:40 and this time we'll check if players
32:43 equal the rock and computer is equal to
32:46 paper and if so we'll return paper
32:47 covers rock
32:52 so we're going to make this kind of
32:55 easier by just copying this code and
32:57 then i'm just going to paste in this and
33:00 then i'll just change this so players
33:03 equal the rock and computer is equal to
33:06 paper instead of scissors paper
33:07 paper
33:14 paper
33:19 you lose
33:21 and we won't even have an exclamation
33:23 point anymore because it's not exciting
33:28 okay and you can kind of see there's a
33:30 few we could add a few more elif
33:33 statements to cover all the different situations
33:34 situations
33:36 but instead we're going to talk about refactoring
33:37 refactoring
33:39 refactoring is the process of
33:42 restructuring code while keeping the
33:44 original functionality when created pro
33:46 when creating a program it's common to
33:48 refactor code to make it simpler or more understandable
33:50 understandable
33:52 so we are actually going to refactor
33:54 this code that i've highlighted now
33:57 and we are going to instead use a nested
33:58 if statement
34:00 a nested if statement will make the code
34:03 more understandable at a quick glance like
34:03 like
34:07 so you can put an if statement inside
34:10 another if else or else statement
34:12 so you'll notice here the first elf
34:13 statement is if player equals rock and
34:16 the second l of statement is if player
34:18 equals rock so see if you can figure out
34:20 how to refactor this to
34:21 to
34:23 not have to use the and anymore we're
34:25 just going to use one if statement and
34:27 one else statement and then an if
34:29 statement under that
34:30 elif statement
34:32 if that doesn't make sense you can just
34:34 see what i do right now
34:36 so i'm going to just
34:39 move this down a little bit
34:40 i'm going to actually copy and paste
34:42 some of these items but we're going to
34:44 start with l if player equals rock we're
34:46 not going to have this
34:47 i'm going to put this on a new line and
34:51 say if computer equals scissors so first
34:53 we're going to check if player equals rock
34:54 rock
34:56 and then if so we're then going to check
34:58 it's going to be a nested if statement
35:01 if computer equals scissors and if
35:04 computer equals scissors then we're
35:06 going to use this this return statement here
35:07 here
35:09 so i have to make sure it's indented
35:11 correctly it's going to re so this we have
35:12 have
35:13 we have
35:15 player if player equals rock then if
35:17 computer equals scissors we'll return oh
35:19 this needs to be indented one more time
35:21 to be on inside that if statement
35:23 and now we don't even need this elf
35:26 statement this can just be an else
35:27 statement else
35:29 because if
35:31 the computer equals scissors there's
35:34 only one other option because we already
35:36 know if player equals rock and computer
35:38 equals rock will have already returned
35:39 it's a tie
35:42 and by the way once you return something
35:44 the rest of the code in a function does
35:46 not run so if we're returning it to tie
35:49 nothing else after that is going to run
35:52 so we know that computer can't equal the
35:53 rock at this point so it's either going
35:55 to be scissors or paper so i don't we
35:57 don't even have to check if computer
35:59 equals paper because at this point
36:01 computer has to equals paper and then
36:04 we'll just return this this line
36:06 let's see
36:09 there we go return paper covers rock
36:11 you lose
36:13 so now we just basically have to add two
36:15 more sections similar to this so this
36:17 one is if player equals rock
36:19 then we have to have another section if
36:22 player equals paper and then if player
36:24 equals scissors
36:26 and then we just have to have the the
36:28 stuff inside is going to be pretty
36:30 similar just corresponding to the
36:32 different relationships between rock
36:35 paper scissors
36:37 so i'm just going to copy that
36:40 and then i will paste it here and then
36:42 one thing is important to make sure this
36:44 elif statement
36:47 lines up with this else statement
36:49 and this is now going to be paper
36:50 paper
36:52 and then we are going to check if
36:54 computer equals rock
36:56 and a computer equals rock
36:59 then we will say that
37:05 you win
37:08 or we'll say scissors
37:12 cuts paper
37:14 paper
37:16 you lose and then the final one which at
37:18 this point i'm sure you can figure out
37:20 on your own we're going to add one more section
37:21 section
37:22 and this if
37:26 player equals scissors
37:28 and then first we're going to check if
37:30 computer equals paper
37:31 if so
37:32 we will do scissors
37:34 scissors cuts
37:35 cuts
37:37 paper we're just making sure we're just
37:39 making it so every time the first one is
37:42 you win the second one is you lose but
37:44 you could do it the other way around
37:48 rock smashes
37:50 smashes scissors
37:52 scissors
37:55 you lose
37:57 okay we're almost finished with this
37:58 whole program
38:00 so both
38:02 the get choices function and the check
38:05 win function they're both complete
38:07 now let code to call the functions and
38:08 play the game
38:11 so first let's remove this
38:13 this check when
38:14 now we'll create a variable called
38:18 choices and make it equal to the result
38:21 of calling the get choices function
38:22 and we just have to make sure it's not
38:24 indented it's on the the first
38:27 the first column i guess right here not
38:30 ended at all so we'll do choices
38:32 choices
38:34 equals get choices
38:35 choices [Music]
38:37 [Music]
38:40 and one thing about this is it's going
38:42 to return a dictionary so if we look at
38:44 the get choices
38:45 so it's returning choices and it's going
38:48 to be a dictionary like this now let me
38:49 just copy this i'm going to show you
38:51 something down here i'm going to just
38:53 paste it down here and we'll just make
38:55 an example of what it could look like it
38:58 could look like rock and paper i always
39:00 use rock and paper as examples because
39:01 scissors is a
39:03 little harder for me to spell
39:06 so so that's a little easier so so let's
39:09 one thing we haven't talked about is how to
39:10 to uh
39:11 uh
39:14 how to access a and specific element
39:16 within a dictionary so this is a
39:19 dictionary so let's say if i call this um
39:24 well i'll just call it choices even
39:26 because we're going to delete that and
39:27 so we're only going to have this choices
39:29 but if choices equals this and let's say
39:32 i just want to get the the choice of the player
39:33 player
39:36 let me show you how i would do that
39:38 i'll just do
39:40 p choice for player choice and i'm going
39:44 to put equal choices and to get just the
39:46 the value of the player i'm going to put
39:49 brackets so the brackets look like that
39:50 and then i have to
39:54 put the the key that i want the value of
39:56 so the key would be player
39:58 so if i
39:59 put the name of the dictionary which is this
40:00 this
40:02 and then i put some brackets we're going
40:05 to use brackets to identify what is the
40:06 key that we want the value of so here's
40:08 the key of the with the value of rock
40:11 and here's a key with a value of paper
40:13 so this is how we can get the player
40:16 choice and i'm sure you can under see
40:17 how you get the computer choices if
40:19 instead if
40:21 we take this computer word and put it
40:23 right here
40:25 uh computer
40:27 okay i'm just going to get rid of all
40:29 this here
40:30 and we're going to we have the get
40:32 choices here
40:34 and now listen carefully to what we're
40:35 going to do next
40:38 we are going to create a variable called
40:41 result and make it equal to the result
40:44 of calling the check win function and
40:46 when we call the check win function
40:49 we're going to pass in the value of the
40:50 player key
40:52 and the computer key
40:56 of the choices dictionary
40:58 so let's do that so then you'll see what
41:00 i mean a result
41:02 is going to be
41:03 we're going to call [Music]
41:05 [Music] check
41:06 check when
41:08 when
41:12 and then we are going to pass in
41:15 we're going to pass in we have choices player
41:17 player
41:25 computer
41:27 so because remember we had that we i
41:28 showed the example that dictionary so
41:30 we're getting the player key the value
41:32 of the player key and the value of the
41:34 computer key
41:36 so now we know who wins
41:39 we've now this result variable is going
41:41 to be one of these strings have been
41:43 returned either it's a tie rock space of
41:46 scissors paper covered rocks and so on
41:50 so now we just have to print the result
41:51 we're going to print
41:53 print
41:55 the result
41:58 okay we can try out this game
42:02 i'm going to click the run button
42:05 okay this is why it's sometimes better
42:08 to test a little earlier i just forgot the
42:09 the
42:12 semicolon on some of these so um semicolon
42:14 semicolon
42:16 and see there's a red arrow i should
42:18 have seen that semicolon
42:20 and semicolon
42:23 okay now let's try it i'm going to play
42:25 the program into a choice
42:27 i'm going to do
42:28 rock and i just noticed something else i
42:30 want to change so enter a choice here i
42:31 started there's a parenthesis here
42:33 there's no parentheses at the end so
42:34 again i'm just going to change that
42:37 really quick so enter a choice and we're
42:39 going to put it in parentheses here and
42:45 test again so rock okay you chose rock
42:46 computer chose rock
42:49 it's a tie okay i'm gonna play it again paper
42:50 paper
42:53 you chose paper computer chose scissors
42:56 scissors cuts paper you lose
43:00 okay we just created a python game
43:02 so hopefully this gives you a better
43:04 understanding of what it's like to
43:07 program in python and you you now know
43:09 about some of the basic concepts of
43:12 python now there's a lot more to learn
43:14 in python and we're going to be covering
43:16 a lot more in this course i just wanted
43:19 to start with a game and a full program
43:21 so just right off the bat you could go
43:22 to program
43:24 so in the next section i'm going to
43:28 start going over in detail all the most
43:30 common features of python
43:32 and we're going to cover some of the
43:34 features that we've already used in this
43:38 game plus a lot of additional features
43:39 additional common features that were not
43:41 used in this game
43:42 and then in the final section of the course
43:44 course
43:47 we're going to code a more complex game
43:49 a blackjack game
43:51 so let's get started with the next section
43:52 section
43:54 one of the quickest and easiest ways to
43:56 get started with python is by using
43:58 replit.com but you may also want to get
44:01 python running on your local computer so
44:03 if you want to do that you can start by
44:06 going on over to python.org
44:08 and these go to the downloads menu
44:11 and then you're going to just click
44:13 what you want to download for it for so
44:15 it's going to default to be for you the
44:17 operating system you're on but you can
44:20 also go to other platforms and make sure
44:22 you can and then just find the platform
44:24 that you want to download on and there's
44:26 going to be instructions on here that's
44:28 going to tell you how to go about
44:31 getting installed on your specific computer
44:32 computer
44:34 so there's a few different ways to run a
44:37 python programs and one of the ways is
44:39 with an interactive prompt so after you
44:40 get installed if you open up your
44:43 terminal and type in python or sometimes
44:46 it's going to be python 3 depending on
44:48 how you got it installed you're going to
44:50 see this interactive prompt and then you
44:53 can just this is called a python rebel
44:55 it's it's different from a rebel
44:57 creating with created with replit but
45:00 you can start coding in python right on
45:02 here so i could say
45:06 name equals bow
45:08 and then you have to make sure you
45:11 put the the quotation marks at the end
45:13 and now i've gotten a variable stored as
45:16 bow and then i can just type in the
45:17 variable name name and it's going to
45:20 show you show me the value of the
45:22 variable and you can type in most
45:24 different python commands right into
45:27 this inactive prompt here
45:28 and then i can just quit it when i'm
45:30 ready to quit
45:33 it's also common to run python using
45:35 visual studio code so if you just search
45:37 for visual studio code you can get to
45:39 the the download page and then you can
45:42 just download it for your system and
45:44 there's also you can download for
45:47 different systems and then once you open
45:49 up visual studio code to get python
45:50 working you're going to want to install
45:53 the python extension so i'm going to
45:55 click over here to extensions and you
45:57 can search for python or it may just be
46:00 listed here under popular extensions and
46:02 i'm just going to click install
46:04 and this is going to make it easier to
46:09 work with python on visual studio code
46:10 so now i can just kind of close some of
46:14 this stuff here
46:16 and i'm going to
46:19 create a new file
46:21 and i can just call the and i can just
46:23 type in name equals
46:25 equals bow
46:27 bow print
46:29 print name
46:30 name
46:32 and then if i save this as
46:34 as
46:37 test.pi it's going to now it's going to
46:40 add the colors that correspond to python
46:41 python
46:43 and then i'm just going to click this
46:44 play button here
46:46 and it's going to play it's going to
46:48 open up a terminal window here
46:51 and it's going to run the program and
46:54 it's going to print bow that's what my
46:55 program did if i zoom out a little bit
46:57 you'll be able to see the difference so
46:58 well we just print the name and it and
47:01 it runs the program just like that and
47:04 then you can see on the terminal how the
47:07 the command that was used to run that so
47:09 we could use the same command on any
47:11 terminal and instead of typing this
47:13 whole thing for the location of python 3
47:16 i can just do python
47:19 3 and then you this is where that file
47:22 is located so just copy that
47:24 and i can paste it in here and it's
47:26 going to run that program
47:28 in this section we'll learn about the
47:30 core features of python i'll go into
47:32 more detail about some of the things we
47:34 learned in the first project and they'll
47:36 be covering a lot more concepts this
47:38 section was heavily inspired by the
47:41 python handbook by flavio copes and you
47:43 can check that out on freecodecamp news
47:45 and like the first part of the course
47:47 i'll be running python in replit so once
47:49 you get logged into replit just like i
47:50 already showed you before you can hit
47:52 the plus button here or the create
47:55 button to create a new replit and then
47:56 you can search for the programming
47:58 language or you can just click it down
48:01 here python we'll just create a python repel
48:02 repel
48:05 and then we can instantly start creating
48:08 writing python code in replit
48:10 so like i showed before we got our
48:12 different files here we're just going to
48:14 start by using one file here and this
48:16 we're going to encode and this is where
48:17 it's going to
48:19 appear if we we run the code so i'm
48:20 going to
48:22 close off the list of files here
48:25 and let's just start at the beginning
48:27 again so you've gotten used to coding in
48:29 python through creating a rock paper
48:30 scissors game
48:32 but now we're going to
48:35 kind of do a deep dive into all the
48:37 basic commands of python so there's
48:39 going to be some review but we're going
48:42 to be going into more detail about each
48:44 of the elements and the first thing
48:47 we're going to talk about is variables
48:49 so we can create a new python variable
48:52 by assigning a value to a label using
48:54 the equal sign or the assignment
48:56 operator so let me give you an example
48:58 just like i was showing you before just
49:00 name equals bo
49:02 so let me just zoom in a little bit more
49:05 here and so now our variable name is
49:08 name and we've assigned it the value of bo
49:09 bo
49:12 and then we can also uh create we can
49:14 create a variable with a number so i
49:15 could do age equals
49:17 equals
49:20 39. so a variable name can be composed
49:23 of characters numbers and an underscore
49:26 character but and it cannot start with
49:27 the number
49:30 so it could be anything like name one
49:34 it could be all capital letters
49:35 it could
49:37 be it could have an underscore it can
49:39 start with an underscore like i said
49:40 like that and
49:41 and
49:44 you can see that these are all it's
49:45 putting these
49:47 red squiggly lines because it's showing
49:49 that that's not
49:50 actual python code if you're going to
49:51 create a variable you should be
49:55 assigning it to a value or you should be
49:58 using a variable that already exists but
49:59 i'm just showing you some different
50:01 examples of different variables now so
50:04 here's an example of an invalid variable
50:07 name if you just start with a number
50:08 like that that can't be a variable
50:10 because you can't start with a number
50:12 and i couldn't put something like test
50:14 exclamation point you can't use
50:16 exclamation points you can't use percent signs
50:17 signs
50:20 and other than that anything is valid
50:23 unless it's a python keyword so there
50:25 are some keywords a keyword is something
50:29 that's used to to write python like for if
50:31 if while
50:32 while
50:34 import these are all words that have
50:36 very specific meanings within python so
50:39 you cannot use them for a variable name
50:42 now there's no need to memorize them as
50:44 the python editor here will alert you if
50:47 you use one of those as a variable
50:48 so that was just like
50:52 if i say if equals hi
50:53 hi
50:54 and then you can see it's going to show
50:56 you right here with these squiggly lines
50:59 that we've done something wrong
51:03 invalid syntax because and then also you
51:04 can see that
51:06 it turns blue this word turns blue
51:09 because it's a keyword you can't use it
51:12 as a variable name
51:14 so like i said it's going to alert you
51:15 if you if you use a keyword as a
51:17 variable and you'll start to gradually
51:19 recognize them as part of the python
51:21 programming language syntax
51:23 now let's talk about talk about
51:26 expressions and statements in python so
51:29 an expression is any sort of code that
51:31 returns a value like for instance if you
51:35 do one plus one or if you just do a
51:36 string like
51:38 bow this is going to like this is going
51:41 to return to this is going to return the
51:44 string bow so a statement on the other
51:47 hand is an operation on a value
51:49 so for example these are this is a
51:51 statement here because we have an
51:53 operation we're assigning this to the
51:56 variable and then another statement
51:58 would be like this print name
51:59 name
52:01 so that's going to
52:03 be a statement because it's doing
52:06 something to the value now a program is
52:09 formed by a series of statements and
52:11 each statement is put on its own line
52:13 like we have these two lines here but
52:15 you can use a semicolon to have more
52:17 than one statement on a single line so i
52:20 i could put a semicolon here
52:22 and then if i run the program it's still
52:24 going to print the name and let's just
52:25 do that we already learned how to run a
52:27 program in replica click this button
52:30 right here but we can see it's going to
52:32 print bow and if i put these on two
52:34 different lines it's going to do the
52:36 same thing if i run the program it's
52:38 going to still do the same thing here
52:40 now let's talk about comments this is
52:42 something we haven't talked about in
52:45 this course yet so in the python program
52:48 everything after a hash mark is ignored
52:51 so if i put a hash mark i can say this
52:55 is a commented line and when we run the
52:57 program this line is going to be
53:00 completely ignored and then we can also
53:03 put in inline comment if i just put the
53:07 hash mark here this is an inline comment
53:09 and the cool thing about most code
53:11 editors including replit is it's going
53:14 to put comments going to make them gray
53:16 so you know that this isn't really part
53:18 of the program this is just some sort of
53:21 special note that the programmer wants
53:23 to put as part of the program
53:25 so i want to emphasize again how
53:28 important indentation is so it's very
53:31 meaningful in python so you can't
53:33 randomly indent things like you can't
53:35 just press tab here to indent here and
53:37 how this is kind of lined up here this
53:40 line up here you can see this red
53:43 squiggly line says unexpected expected indent
53:44 indent
53:46 so some other languages do not have
53:49 meaningful white space an indentation
53:52 doesn't matter but in python indentation matters
53:53 matters
53:55 so in this case if you try to run this
53:57 program we can run this and you'll see
54:00 an error right here showing up in red
54:02 here it says indentation error unexpected
54:04 unexpected indent
54:05 indent
54:07 because indentation has a special
54:10 meaning so i can just unindent that here
54:14 everything indented belongs to a block
54:15 like a control statement or a
54:17 conditional block or a function or a
54:20 class body and we'll be talking more
54:23 about those different blocks
54:26 now let's talk about data types python
54:29 has several built-in types
54:32 so for instance this is a string so
54:35 anything surrounded by quotation marks
54:38 is a string that's one data type and you
54:40 can check the type of a variable by
54:43 using the type function so i could say type
54:44 type
54:47 and then i'll put name and to make sure
54:49 to be able to see in the console i'm
54:52 going to print what the type is here
54:55 and if i press if i run the program
54:58 we'll see that the type is the class of
55:02 str which stands for string
55:04 and then we can test to see if something
55:08 is a string by comparing it to str so i
55:12 could do equals equals str
55:15 and then if i run that it's going to say
55:20 true because the type of name does equal
55:22 a string
55:24 and then we can also use is
55:26 instance so
55:28 i'm going to
55:31 uh so if we instead of doing type we do is
55:32 is
55:35 instance and then we
55:38 have to pass it two things so
55:40 the first thing we're going to pass it
55:42 so we have the is instance we're passing
55:44 it the name that's this variable and str
55:46 we're trying to see if name is an
55:49 instance of a string and if i run that
55:52 it should say true again
55:55 so we've been testing against the str
55:57 class the string class but it works the
56:00 same for other data types so so there
56:03 are some data types around numbers
56:05 so an integer number integer numbers are
56:08 represented using the int class or the
56:11 int class and floating point numbers or
56:14 fractions are a type of are the type
56:17 float so i can say age equals
56:19 equals 2
56:20 2
56:22 and then we can just check
56:30 and i can pass in the age and then i can
56:31 pass in an int
56:33 so if i run that
56:35 we're going to see that i've done
56:36 something wrong
56:40 oh i spelled that wrong there we go okay
56:41 now i'm going to try this and we'll see
56:50 true false it's not a false or it's not
56:53 a float because it doesn't have a
56:55 decimal point if i
56:57 did 2.9
56:59 then it should show that it is a float
57:02 because it has a decimal so python
57:04 automatically detects the type
57:07 from the value type so it automatically
57:08 knows this is a string it automatically
57:12 knows this is a float but you can also
57:15 create a variable of a specific type by
57:17 using the class constructor passing a
57:20 value literal or a variable name like
57:23 for instance we have this and we check
57:25 to see if this is a float and it's
57:28 saying false but i can make it a float
57:30 by typing in float
57:31 float
57:34 and just p putting the value
57:36 into the flow here so we're going to
57:39 make it a float so now it's true true
57:41 and you can do the same thing with
57:45 strings or integers or other data types
57:48 and you can also convert from one data
57:51 type to another by using the class
57:53 constructor so so that's basically what
57:54 we did we just converted this from an
57:58 integer to a float but i can also do
57:59 something else i can convert something from
58:00 from
58:02 a string
58:05 to an integer so for instance i'm going
58:07 to we'll just get rid of this one
58:09 completely here and we'll just use this
58:10 one so
58:13 a string is anything in quotation marks
58:15 so if i do
58:24 it's going to say
58:26 false it's not an n so i'm printing
58:28 whether it's an instance of an int but i
58:31 can convert this string
58:35 into an integer by just doing integer
58:37 and this let me run the program and it
58:39 says true
58:40 another thing about this is you don't
58:43 just have to pass in the actual data or
58:45 the actual string i can pass in a
58:49 variable so i can say number
58:52 equals and now i'm going to make it a an
58:54 x it says number but it's actually a
58:58 string but i can pass in the number here
59:00 and then it's going to set that to age
59:03 and it's going to be true so we create
59:05 the string we convert the string to an
59:09 integer and we tested that that age is
59:11 an integer
59:13 so when we do something like this this
59:15 is called casting
59:18 we are it's basically trying to extract
59:22 an integer from this string
59:23 of course the conversion might not
59:25 always work depending on the value
59:28 that's passed so for instance if we
59:31 write test here in the string
59:34 instead of the 20
59:35 we may get an air so let me just run
59:38 this and see now we have an error it
59:40 says invalid literal for
59:44 for int int with base 10 test so we
59:46 can't convert the word we can't convert
59:49 the string test to an integer so python
59:51 does its best to do the conversion but
59:54 it doesn't always work there are a few
59:56 other types so let me see are some other
59:58 common types of types so there's the
60:00 type of complex for complex numbers bool for booleans list for list tuple for
60:03 for booleans list for list tuple for tuples range for ranges
60:05 tuples range for ranges dict is our dictionaries and set
60:07 dict is our dictionaries and set is a type for sets
60:09 is a type for sets and we'll explore all these soon we'll
60:12 and we'll explore all these soon we'll go into more detail about about all
60:14 go into more detail about about all these different types of types well now
60:17 these different types of types well now let's talk about operators we've already
60:19 let's talk about operators we've already seen one operator that's this one that's
60:21 seen one operator that's this one that's the assignment operator but there's also
60:24 the assignment operator but there's also arithmetic operators comparison
60:26 arithmetic operators comparison operators logical operators bitwise
60:28 operators logical operators bitwise operators and plus some interesting ones
60:31 operators and plus some interesting ones like is and in so we're going to be
60:34 like is and in so we're going to be going over a lot of those right now
60:36 going over a lot of those right now so we talked about the assignment
60:37 so we talked about the assignment operator which is used to assign a value
60:39 operator which is used to assign a value to a variable or to assign a variable
60:42 to a variable or to assign a variable value to another variable now let's talk
60:45 value to another variable now let's talk about arithmetic operators
60:47 about arithmetic operators it's just what you use to do
60:49 it's just what you use to do math mathematics
60:51 math mathematics so here are the different
60:53 so here are the different arithmetic operators so we have plus one
60:56 arithmetic operators so we have plus one plus one equals two then minus
60:58 plus one equals two then minus multiplication division
61:01 multiplication division we have
61:02 we have remainder so four divided by three
61:05 remainder so four divided by three equals one but there's a remainder of
61:06 equals one but there's a remainder of one we have exponents four to the power
61:08 one we have exponents four to the power of 2 is 16 and floor division
61:12 of 2 is 16 and floor division so floor division does a
61:14 so floor division does a division problem and then just
61:18 division problem and then just basically rounds down
61:20 basically rounds down so floor division does the division and
61:22 so floor division does the division and rounds down to the nearest whole number
61:25 rounds down to the nearest whole number so actually this would be better seen if
61:27 so actually this would be better seen if we do four five divide by two five floor
61:30 we do four five divide by two five floor division divided by two is going to be
61:32 division divided by two is going to be two normally be 2.5 but floor division
61:35 two normally be 2.5 but floor division is rounding down to the nearest integer
61:37 is rounding down to the nearest integer the nearest whole number
61:39 the nearest whole number and then also note that that
61:41 and then also note that that the minus can also be a
61:44 the minus can also be a make something a negative number
61:47 make something a negative number so i could do 4 plus
61:50 so i could do 4 plus negative or i mean 1 plus negative 1 and
61:53 negative or i mean 1 plus negative 1 and then that's just going to equal 0. and
61:55 then that's just going to equal 0. and then the plus operator can also be used
61:58 then the plus operator can also be used to concatenate string values that's
62:02 to concatenate string values that's something we talked about earlier but i
62:04 something we talked about earlier but i could say
62:05 could say scamp
62:06 scamp and then put a plus
62:13 is a good dog and then like if i print this out
62:21 i put the the parentheses around it i can
62:22 can and then we'll see the whole string here
62:24 and then we'll see the whole string here sk scamp is a good dog
62:27 sk scamp is a good dog that was the name of my first dog when i
62:28 that was the name of my first dog when i was a kid so we can also combine these
62:32 was a kid so we can also combine these arithmetic operators with the assignment
62:35 arithmetic operators with the assignment operator let me show you what i mean so
62:37 operator let me show you what i mean so let me just get rid of this here and i'm
62:39 let me just get rid of this here and i'm going to do age
62:41 going to do age equals 8
62:42 equals 8 and age
62:44 and age plus equals
62:46 plus equals [Music]
62:47 [Music] 8
62:48 8 and i'll do print
62:50 and i'll do print age
62:51 age so we've so all these different
62:54 so we've so all these different operators can be assigned with it can be
62:56 operators can be assigned with it can be combined with the assignment operator
63:00 combined with the assignment operator and now it's going to add
63:03 and now it's going to add 8
63:04 8 to the age so if i
63:07 to the age so if i run this it's 16. so this actually just
63:11 run this it's 16. so this actually just means
63:13 means age
63:14 age equals age
63:17 equals age plus
63:21 eight so this age plus equals eight is the
63:24 so this age plus equals eight is the same as saying age equals age plus eight
63:28 same as saying age equals age plus eight so it's just going to add this number
63:31 so it's just going to add this number to the current age and set it equal to
63:34 to the current age and set it equal to the age and you can do that with any of
63:36 the age and you can do that with any of these like i could do
63:39 these like i could do times
63:41 times and that would be age equals age times 8
63:45 and that would be age equals age times 8 and
63:47 and 64. and so on with any of these
63:50 64. and so on with any of these arithmetic operators
63:53 arithmetic operators okay now let's talk about comparison
63:55 okay now let's talk about comparison operators
63:57 operators now we talked a little bit about them
63:59 now we talked a little bit about them before
64:00 before but let's see some examples again so
64:03 but let's see some examples again so this is to compare if two things are
64:05 this is to compare if two things are equal
64:07 equal and then we have not equal we're
64:08 and then we have not equal we're comparing them to see if they're not
64:10 comparing them to see if they're not equal or count this is if a is
64:13 equal or count this is if a is greater than b
64:15 greater than b or more than b and then this this is
64:18 or more than b and then this this is less than or equal to b
64:19 less than or equal to b now let me just tell you a trick of how
64:21 now let me just tell you a trick of how i keep the keep track of which one is
64:23 i keep the keep track of which one is greater than and which one is less than
64:25 greater than and which one is less than if you see this less than one if you
64:28 if you see this less than one if you kind of tilt your head a little bit it
64:29 kind of tilt your head a little bit it kind of looks like an l see like l and
64:32 kind of looks like an l see like l and this one doesn't look like as much of an
64:34 this one doesn't look like as much of an l so this
64:36 l so this less than operator kind of in some ways
64:39 less than operator kind of in some ways looks like a capital l that's kind of
64:41 looks like a capital l that's kind of just squished over and that's how i keep
64:43 just squished over and that's how i keep track of which one is less than which
64:45 track of which one is less than which one is greater than
64:47 one is greater than and so these are all going to give
64:49 and so these are all going to give either a false value or a true values
64:53 either a false value or a true values speaking of true and false
64:55 speaking of true and false true and false are examples of boolean
64:59 true and false are examples of boolean the boolean data type the boolean data
65:01 the boolean data type the boolean data type just means true false or true
65:04 type just means true false or true so a boolean is either going to be true
65:06 so a boolean is either going to be true or false there's only two options
65:08 or false there's only two options and there are a few boolean operators
65:15 so let me show you what the boolean the boolean operators are either not
65:18 boolean operators are either not and
65:18 and or or
65:22 or or so when working with two or false
65:23 so when working with two or false attributes
65:25 attributes those work like logical and or and not
65:29 those work like logical and or and not so when you're using uh not
65:32 so when you're using uh not it means it's not true you're checking
65:34 it means it's not true you're checking you're checking to see something is not
65:35 you're checking to see something is not true and means they both have to be true
65:38 true and means they both have to be true and or means either this one has to be
65:40 and or means either this one has to be true or this one has to be true in order
65:43 true or this one has to be true in order for the full thing to be evaluated as
65:46 for the full thing to be evaluated as true
65:47 true and let me show you something about or
65:50 and let me show you something about or now or using an expression returns the
65:54 now or using an expression returns the value of the first operator operand that
65:56 value of the first operator operand that is not a false value or a falsie value
66:00 is not a false value or a falsie value otherwise it returns the last operand so
66:04 otherwise it returns the last operand so it's going to return
66:06 it's going to return the first operand that is not a false
66:08 the first operand that is not a false value but since this is a false value
66:10 value but since this is a false value it's returning the second operand since
66:12 it's returning the second operand since this is a false value it returns the
66:14 this is a false value it returns the second one
66:15 second one since this is not a false value it will
66:19 since this is not a false value it will return the first one
66:21 return the first one and this is considered a false value if
66:24 and this is considered a false value if it's just an
66:25 it's just an empty bracket that's false so it's going
66:27 empty bracket that's false so it's going to return the second value which just
66:29 to return the second value which just happens to be false
66:31 happens to be false and since this is a false value it's
66:33 and since this is a false value it's going to return the second option which
66:35 going to return the second option which also happens to equal to false
66:39 also happens to equal to false so
66:40 so one way to think about it for the word
66:41 one way to think about it for the word or is so the pi this is how the python
66:44 or is so the pi this is how the python docs describe it if x is false
66:48 docs describe it if x is false then why
66:49 then why else x
66:50 else x so this would be like x this would be y
66:53 so this would be like x this would be y if else is false then why else x
66:57 if else is false then why else x and then for and down here let's look at
66:59 and then for and down here let's look at some examples for and and only evaluates
67:02 some examples for and and only evaluates the second argument if the first one is
67:05 the second argument if the first one is true so if the first argument is falsy
67:09 true so if the first argument is falsy such as false zero and empty string
67:11 such as false zero and empty string empty brackets it returns that argument
67:14 empty brackets it returns that argument otherwise it evaluates the second
67:17 otherwise it evaluates the second argument
67:18 argument so the way the way the python docs
67:20 so the way the way the python docs describe it is if x is false then x
67:24 describe it is if x is false then x else y
67:26 else y okay let's quickly discuss bitwise
67:28 okay let's quickly discuss bitwise operators they're very rarely used only
67:31 operators they're very rarely used only in very specific situations but it's
67:34 in very specific situations but it's worth knowing what these bitwise
67:37 worth knowing what these bitwise operators are just in case you're in the
67:39 operators are just in case you're in the very rare situation that you need to use
67:42 very rare situation that you need to use them
67:43 them and then two other types of operators
67:45 and then two other types of operators are is and in
67:48 are is and in now is is called the identity operator
67:51 now is is called the identity operator it's used to compare two objects and
67:54 it's used to compare two objects and returns true if both are the same
67:56 returns true if both are the same objects if both are the same object and
68:00 objects if both are the same object and i'll be talking more about that later in
68:02 i'll be talking more about that later in the section on objects and then in is
68:06 the section on objects and then in is called the membership operator this is
68:08 called the membership operator this is used to tell if a value is contained in
68:11 used to tell if a value is contained in a list or another sequence and we'll be
68:14 a list or another sequence and we'll be talking more about the in operator when
68:16 talking more about the in operator when we're discussing lists and other
68:19 we're discussing lists and other sequences later in this course
68:21 sequences later in this course and the final thing i want to talk to
68:23 and the final thing i want to talk to you about is the ternary operator now
68:26 you about is the ternary operator now the turn area operator in python allows
68:28 the turn area operator in python allows you to quickly define a conditional
68:32 you to quickly define a conditional so here will be like kind of the slow
68:34 so here will be like kind of the slow way to do it without a ternary operator
68:37 way to do it without a ternary operator so let's say you have a function that in
68:39 so let's say you have a function that in this function is comparing age with 18
68:43 this function is comparing age with 18 and it's going to return true or false
68:45 and it's going to return true or false depending on the risk result
68:47 depending on the risk result so instead of writing like this
68:50 so instead of writing like this we can implement it with a ternary
68:53 we can implement it with a ternary operator so let's do death
68:59 [Music] is
69:00 is adult i'll call it is adult 2 because
69:03 adult i'll call it is adult 2 because it's the second way of doing it and this
69:06 it's the second way of doing it and this time we're going to use the ternary
69:08 time we're going to use the ternary operator it's just going to be return
69:11 operator it's just going to be return true
69:13 true if age
69:15 if age is greater than 18
69:18 is greater than 18 else
69:27 so you can see first we define the result if the condition is true
69:29 result if the condition is true then we evaluate the condition
69:32 then we evaluate the condition and then we define the result if the
69:34 and then we define the result if the condition is false
69:36 condition is false it's basically an if else statement all
69:38 it's basically an if else statement all on a single line
69:40 on a single line okay let's talk more about strings in
69:44 okay let's talk more about strings in python so a string in python is a series
69:46 python so a string in python is a series of characters enclosed in quotes in
69:49 of characters enclosed in quotes in double quotes or it could be
69:52 double quotes or it could be single quotes
69:54 single quotes as long as the type of quote is the same
69:57 as long as the type of quote is the same on both sides and we already talked
69:59 on both sides and we already talked about how you can assign a string to a
70:03 about how you can assign a string to a variable
70:04 variable [Music]
70:06 [Music] and we already talked about how you can
70:08 and we already talked about how you can concatenate two strings using the plus
70:10 concatenate two strings using the plus operator like phrase
70:17 equals bo and then you can concatenate with the plus operator
70:19 with the plus operator is my
70:21 is my name
70:21 name [Music]
70:26 and then also instead of putting a string here
70:29 also instead of putting a string here you can put the variable so i could put
70:31 you can put the variable so i could put name is my name and we already have the
70:34 name is my name and we already have the variable here to equal bo so when you
70:36 variable here to equal bo so when you concatenate you can concatenate the
70:38 concatenate you can concatenate the strings or the variables
70:40 strings or the variables you can also append to a string using
70:43 you can also append to a string using the plus equal operator so let's say i
70:45 the plus equal operator so let's say i want to add to this name and i'm so i'm
70:48 want to add to this name and i'm so i'm going to say name
70:51 going to say name plus equals
70:59 is my name so we're adding is my name to the end of this so i can say
71:02 the end of this so i can say print
71:06 name and then we can see what it looks like
71:08 and then we can see what it looks like when you use the plus equal operator
71:11 when you use the plus equal operator so bo is my name
71:14 so bo is my name and then we already talked about how you
71:16 and then we already talked about how you can convert a number to a string using
71:19 can convert a number to a string using the
71:20 the str class constructor like if we had age
71:24 str class constructor like if we had age equals we could make this a string
71:28 equals we could make this a string but we passed this integer it converts
71:30 but we passed this integer it converts to a string and now it's going to be a
71:32 to a string and now it's going to be a string
71:33 string now here's something new a string can be
71:35 now here's something new a string can be a multi what can be multi-line when
71:38 a multi what can be multi-line when defined with a special syntax so if you
71:41 defined with a special syntax so if you enclose it if you enclose the string in
71:43 enclose it if you enclose the string in a set of three
71:45 a set of three quotes
71:46 quotes so let me show you an example get rid of
71:48 so let me show you an example get rid of all this
71:50 all this and i'm going to
71:52 and i'm going to print an entire
71:54 print an entire string here
71:56 string here so we're going to make this a multi-line
71:58 so we're going to make this a multi-line string i'm going to put three quotation
72:01 string i'm going to put three quotation marks
72:02 marks and then it's going to start with three
72:04 and then it's going to start with three quotation marks and end with three
72:06 quotation marks and end with three quotation marks and then i can make it
72:08 quotation marks and then i can make it multi-line so i can say bo is and then i
72:12 multi-line so i can say bo is and then i can
72:13 can put some extra lines
72:15 put some extra lines 39
72:17 39 years old now if i print that
72:21 years old now if i print that and you can see it's going to print
72:23 and you can see it's going to print the different lines here so we just made
72:25 the different lines here so we just made a multi-line string
72:27 a multi-line string and you can also instead of using the
72:29 and you can also instead of using the double quotes you can put a single quote
72:31 double quotes you can put a single quote as long as they're the same at the
72:34 as long as they're the same at the at the beginning and ending
72:36 at the beginning and ending now a string also has a set of built-in
72:39 now a string also has a set of built-in methods let me show you an example so if
72:41 methods let me show you an example so if i have this string
72:44 i have this string bow but i'm going to put at the end of
72:46 bow but i'm going to put at the end of the string i'm going to put dot
72:49 the string i'm going to put dot upper and i put parentheses at the end
72:52 upper and i put parentheses at the end so if i run this now it's going to print
72:54 so if i run this now it's going to print it in all capital letters
72:57 it in all capital letters and the same thing you can use with
72:59 and the same thing you can use with lower so if it if i had a string that
73:02 lower so if it if i had a string that had a few capital letters
73:06 had a few capital letters okay now it's all
73:07 okay now it's all lower now i can also type in a title
73:11 lower now i can also type in a title and this is going to
73:13 and this is going to make each lut so i can say
73:16 make each lut so i can say bow
73:17 bow person
73:18 person uh and i do this so it's going to cat
73:21 uh and i do this so it's going to cat it's going to a title that's going to
73:23 it's going to a title that's going to make the first letter of each string
73:26 make the first letter of each string a capital letter
73:28 a capital letter i can also check things like i can say
73:32 i can also check things like i can say is lower and it's going to check if it's
73:35 is lower and it's going to check if it's all lowercase letters false but if i
73:38 all lowercase letters false but if i make it so it is all lowercase letters
73:41 make it so it is all lowercase letters it's going to say true so here's just a
73:45 it's going to say true so here's just a list of a few common ones
73:48 list of a few common ones [Music]
73:51 [Music] you can do is alpha to check if it
73:53 you can do is alpha to check if it contains only characters
73:55 contains only characters is
73:56 is l num to
73:58 l num to check if a string contains characters or
74:00 check if a string contains characters or digits and is not empty is decimal
74:03 digits and is not empty is decimal lower to make it lower case is lower
74:06 lower to make it lower case is lower upper is upper title starts with to
74:08 upper is upper title starts with to check if it starts with a specific
74:09 check if it starts with a specific substring to check if it ends with you
74:11 substring to check if it ends with you can replace part of a string split a
74:14 can replace part of a string split a string you can strip the white space
74:17 string you can strip the white space from a string you can append new letters
74:20 from a string you can append new letters to a string you can find the position of
74:22 to a string you can find the position of a substring spring string and there's a
74:25 a substring spring string and there's a few more but these are some of the most
74:26 few more but these are some of the most common things you can do with a string
74:30 common things you can do with a string and then one thing to know about these
74:32 and then one thing to know about these is that they they all return a new
74:36 is that they they all return a new modified string they don't actually
74:39 modified string they don't actually alter the original string so let me show
74:42 alter the original string so let me show you what i mean by that so let's say we
74:44 you what i mean by that so let's say we have we'll do name equals bow again let
74:48 have we'll do name equals bow again let me zoom in a little bit
74:50 me zoom in a little bit and we're going to
74:52 and we're going to print
74:54 print name dot lower
75:01 now i'm going to print name
75:02 name so if i just run this
75:09 and we first figure out what went wrong here it looks like there's a few extra
75:12 here it looks like there's a few extra parentheses
75:14 parentheses [Music]
75:21 okay let's run this and you can see it's going to make it all lowercase but then
75:24 going to make it all lowercase but then when i print the name again
75:26 when i print the name again it's not still lowercase because this
75:29 it's not still lowercase because this just returns a brand new modified string
75:32 just returns a brand new modified string it doesn't actually change anything
75:34 it doesn't actually change anything in the original string
75:38 in the original string and then you can use some global
75:40 and then you can use some global functions with strings as well so one
75:43 functions with strings as well so one function we haven't discussed yet is the
75:45 function we haven't discussed yet is the l e n function which stands for length
75:48 l e n function which stands for length it can give the length of a string so
75:51 it can give the length of a string so i'm going to type an l-e-n
75:53 i'm going to type an l-e-n and then so there's just some global
75:55 and then so there's just some global functions that work with a lot of
75:56 functions that work with a lot of different types of data and the length
76:00 different types of data and the length of this is four you can see
76:03 of this is four you can see and then you can use the in operator now
76:07 and then you can use the in operator now i briefly mentioned the n operator
76:09 i briefly mentioned the n operator earlier so
76:11 earlier so let me show you one use case so we can
76:14 let me show you one use case so we can use the in operator to check if a string
76:17 use the in operator to check if a string contains a substring like for instance i
76:20 contains a substring like for instance i can say
76:22 can say a u
76:24 a u in
76:25 in name so let's check if name
76:29 name so let's check if name contains the letters a u
76:32 contains the letters a u well true it does but if if if it if it
76:37 well true it does but if if if it if it if it didn't if i just add an extra
76:38 if it didn't if i just add an extra string it's going to say
76:40 string it's going to say false
76:41 false okay another thing with strings um
76:45 okay another thing with strings um escaping is a way to add special
76:48 escaping is a way to add special characters into a string
76:51 characters into a string for example let's say we wanted to add a
76:54 for example let's say we wanted to add a double quote within the string how can i
76:57 double quote within the string how can i add a double quote into a string that's
76:59 add a double quote into a string that's wrapped in double quotes if i put a
77:02 wrapped in double quotes if i put a double quote like that that's not going
77:04 double quote like that that's not going to work because this is going to be the
77:06 to work because this is going to be the string and then it's not going to the
77:08 string and then it's not going to the code editor is not going to know what to
77:09 code editor is not going to know what to do with this last little bit here
77:12 do with this last little bit here so
77:12 so the way to go is to escape the double
77:15 the way to go is to escape the double quote inside the string with the
77:17 quote inside the string with the backslash character so right before this
77:20 backslash character so right before this quote i'm going to put the backslash
77:22 quote i'm going to put the backslash character
77:23 character and then you can see it now all is all
77:26 and then you can see it now all is all the same color as a string and if i
77:28 the same color as a string and if i print it it's going to it's not going to
77:31 print it it's going to it's not going to print the backslash character so putting
77:33 print the backslash character so putting a backslash is how you escape a
77:35 a backslash is how you escape a character and that just means
77:38 character and that just means this
77:39 this the the backslash character means that
77:40 the the backslash character means that the next character is not going to mean
77:42 the next character is not going to mean what it normally means
77:44 what it normally means it's going to actually just be the
77:47 it's going to actually just be the string of that character
77:49 string of that character and you can do the same thing with so
77:52 and you can do the same thing with so with in this particular example you may
77:55 with in this particular example you may not need to do it because you can always
77:57 not need to do it because you can always just put a single quote at the beginning
78:00 just put a single quote at the beginning and ending
78:01 and ending and as long as you have a different type
78:04 and as long as you have a different type of quote at the beginning and ending
78:06 of quote at the beginning and ending then
78:08 then you can put a double quote in the middle
78:10 you can put a double quote in the middle but let's say you want a string that
78:13 but let's say you want a string that contains both a single quote and double
78:15 contains both a single quote and double quote within the string
78:16 quote within the string then you will have to backslash like if
78:18 then you will have to backslash like if i just put a single quote there it's
78:19 i just put a single quote there it's going to mess it up but if i put it
78:21 going to mess it up but if i put it backslash
78:22 backslash now it's going to have the single the
78:25 now it's going to have the single the double quote and the single quote right
78:27 double quote and the single quote right within the string
78:29 within the string and
78:30 and you can also use the escape character
78:32 you can also use the escape character for
78:33 for special formatting characters like uh
78:37 special formatting characters like uh for instance what if i want there to be
78:39 for instance what if i want there to be a new line between the first two and the
78:42 a new line between the first two and the last two letters of the string if i do
78:44 last two letters of the string if i do slash n
78:46 slash n that is going to not actually just put a
78:48 that is going to not actually just put a slash in let's see what happens when i
78:50 slash in let's see what happens when i put that this is means new line you can
78:53 put that this is means new line you can see it says be new line a you
78:56 see it says be new line a you and then another way a reason why you
78:59 and then another way a reason why you may want to use an escape here like
79:01 may want to use an escape here like let's see what happens if i do this
79:03 let's see what happens if i do this this
79:04 this that's not looking how i want to look
79:06 that's not looking how i want to look because it's normally normally when
79:09 because it's normally normally when the code is running if it sees this
79:12 the code is running if it sees this backslash it thinks it's an escape
79:14 backslash it thinks it's an escape character so if you want to actually add
79:16 character so if you want to actually add a backslash to a string you have to
79:18 a backslash to a string you have to escape the backslash so now it's be
79:21 escape the backslash so now it's be backslash au
79:24 backslash au okay we're done talking about escape
79:26 okay we're done talking about escape characters now i'm going to tell you how
79:28 characters now i'm going to tell you how you can get a specific character in a
79:32 you can get a specific character in a string so given a string you can get its
79:35 string so given a string you can get its character using square brackets to get a
79:38 character using square brackets to get a specific item given its index starting
79:41 specific item given its index starting from zero
79:42 from zero so
79:43 so one thing to know about programming is
79:46 one thing to know about programming is that
79:47 that whenever you're counting from in most
79:49 whenever you're counting from in most programming languages you start counting
79:52 programming languages you start counting starting at zero so this is going to get
79:55 starting at zero so this is going to get the letter at index one so this is index
79:59 the letter at index one so this is index zero the b the e is at index one index
80:02 zero the b the e is at index one index two index three so if i run that we can
80:05 two index three so if i run that we can see we are getting the e that's at index
80:09 see we are getting the e that's at index one if i want to get the b i just put a
80:12 one if i want to get the b i just put a zero in the brackets and we get the b
80:15 zero in the brackets and we get the b and then we can use a negative number to
80:18 and then we can use a negative number to start counting at the end so if i put
80:21 start counting at the end so if i put negative one it's going to start here
80:23 negative one it's going to start here zero
80:24 zero one and that's going to be a oh you okay
80:27 one and that's going to be a oh you okay i guess when it's going backwards it's
80:29 i guess when it's going backwards it's not going to start zero because there is
80:31 not going to start zero because there is no negative zero that makes sense so
80:33 no negative zero that makes sense so negative one is going to be the last
80:35 negative one is going to be the last character in the string so negative one
80:37 character in the string so negative one negative two negative three and so on
80:40 negative two negative three and so on we can also use a range using what we
80:43 we can also use a range using what we call slicing so if i put 1 colon 2
80:47 call slicing so if i put 1 colon 2 this is going to be every character
80:50 this is going to be every character starting at index 1
80:52 starting at index 1 and ending before index 2. so it starts
80:56 and ending before index 2. so it starts at index 1 so it starts with that
80:58 at index 1 so it starts with that character and it ends before an x2 which
81:00 character and it ends before an x2 which is a so that's actually only going to
81:02 is a so that's actually only going to return an e
81:05 return an e but if i put 3 here
81:08 but if i put 3 here now we can return a u
81:11 now we can return a u and if i put bo is cool we can put one
81:15 and if i put bo is cool we can put one further down i'm going to put 7
81:17 further down i'm going to put 7 and we can see
81:18 and we can see it's going to return part of this string
81:24 it's going to return part of this string and then you can also start if you just
81:27 and then you can also start if you just put a blank before the colon then it's
81:30 put a blank before the colon then it's going to turn everything up to it's
81:32 going to turn everything up to it's going to start at the beginning and
81:33 going to start at the beginning and return everything up to character seven
81:36 return everything up to character seven and you can also do in the opposite
81:37 and you can also do in the opposite direction so if i put a blank after the
81:40 direction so if i put a blank after the colon it's going to go to the end of the
81:42 colon it's going to go to the end of the string so it's going to say is cool
81:45 string so it's going to say is cool so let's talk about
81:46 so let's talk about booleans well we already talked more
81:48 booleans well we already talked more about we already talked about booleans
81:50 about we already talked about booleans but we're going to talk a little bit
81:51 but we're going to talk a little bit more about boolean which is considered
81:54 more about boolean which is considered the bool type and this can have two
81:56 the bool type and this can have two values true or false so i can say done
82:01 values true or false so i can say done equals
82:03 equals true
82:04 true or you can do done
82:06 or you can do done equals false now
82:09 equals false now notice that it always has a capital t or
82:11 notice that it always has a capital t or a capital f so
82:13 a capital f so if you don't put a capital t or capital
82:15 if you don't put a capital t or capital f it won't be considered the the true
82:17 f it won't be considered the the true boolean value in python
82:19 boolean value in python and booleans can be especially useful
82:22 and booleans can be especially useful with the conditional co
82:24 with the conditional co with conditional control structures like
82:26 with conditional control structures like if statements
82:28 if statements well we already discussed if statements
82:30 well we already discussed if statements in the first part of the course and
82:31 in the first part of the course and we'll be discussing them more in detail
82:34 we'll be discussing them more in detail later but let me just show you an
82:35 later but let me just show you an example so if done and i'm going to
82:38 example so if done and i'm going to erase this done because we want to be
82:41 erase this done because we want to be true so if done
82:43 true so if done and then we'll say print
82:50 yes [Music]
82:53 [Music] else
82:54 else print
83:02 no okay so i can run that and it's going to
83:04 okay so i can run that and it's going to print yes because done
83:06 print yes because done equals true but if done equals false
83:10 equals true but if done equals false [Music]
83:13 [Music] then it's just going to say
83:15 then it's just going to say no
83:15 no so when evaluating a value for true or
83:18 so when evaluating a value for true or false if the value is not a bool or
83:22 false if the value is not a bool or boolean like if it's not true or false
83:24 boolean like if it's not true or false we have some rules depending on the type
83:26 we have some rules depending on the type we're checking so numbers are always
83:29 we're checking so numbers are always true
83:30 true except for the number zero
83:33 except for the number zero if i put 0 here it's going to evaluate
83:37 if i put 0 here it's going to evaluate to false but if i put any other number
83:40 to false but if i put any other number here it's going to be true even like
83:44 here it's going to be true even like negative 1 or anything like that it's
83:46 negative 1 or anything like that it's going to be true oh i guess
83:49 going to be true oh i guess that i didn't put negative 1 i put
83:50 that i didn't put negative 1 i put equals 1. so if it's negative 1 that's
83:53 equals 1. so if it's negative 1 that's going to be true
83:55 going to be true so strings are always false
83:59 so strings are always false oh
84:00 oh strings are false only when empty
84:04 strings are false only when empty so if i say
84:06 so if i say bow here this is going to be true
84:08 bow here this is going to be true because it's not an empty string but if
84:10 because it's not an empty string but if i make an empty string then it's going
84:12 i make an empty string then it's going to be
84:13 to be false
84:15 false lists tuples and sets and dictionaries
84:18 lists tuples and sets and dictionaries which we'll talk about more later are
84:20 which we'll talk about more later are false only when empty so it's going to
84:22 false only when empty so it's going to be if a list double star dictionary is
84:25 be if a list double star dictionary is is filled with something that is true
84:28 is filled with something that is true and then also you can
84:30 and then also you can check if a value is a boolean so if i
84:33 check if a value is a boolean so if i say done equals true
84:36 say done equals true i can do
84:38 i can do print
84:44 type we're going to check the type we're going to check if the type of done
84:47 going to check if the type of done equals
84:49 equals bool so let's check it does that equal
84:52 bool so let's check it does that equal boolean
84:53 boolean true it does now let's see let's
84:57 true it does now let's see let's change this to a different type and it's
85:00 change this to a different type and it's going to say false so it can still
85:02 going to say false so it can still evaluate whether this is true or false
85:04 evaluate whether this is true or false but the type is not a boolean the type
85:07 but the type is not a boolean the type is a string
85:08 is a string and let me show you another example code
85:14 so the global the any function it's a global function
85:17 the any function it's a global function it's very useful when working with
85:18 it's very useful when working with booleans
85:20 booleans it returns true if any of the values of
85:23 it returns true if any of the values of the iterable such as a list if any of
85:26 the iterable such as a list if any of them are true it's going to return true
85:28 them are true it's going to return true for all of them so
85:30 for all of them so for instance book one read that's true
85:32 for instance book one read that's true but book two read that's false but this
85:35 but book two read that's false but this is going to return true because it's
85:38 is going to return true because it's checking if any of them are true and
85:40 checking if any of them are true and then it's going to set this to true
85:44 then it's going to set this to true now the all function is is similar but
85:46 now the all function is is similar but returns true if
85:48 returns true if all of the values are true so
85:52 all of the values are true so we see we have this value as true
85:55 we see we have this value as true we have this value as false
85:58 we have this value as false whereas any would have returned true
86:00 whereas any would have returned true this is going to return false because it
86:02 this is going to return false because it only returns true if all of the values
86:05 only returns true if all of the values are true
86:08 are true okay now let's talk about more
86:10 okay now let's talk about more number data ties we already talked about
86:12 number data ties we already talked about int an integer whole number we've
86:14 int an integer whole number we've already talked about float which is any
86:18 already talked about float which is any number with a decimal point there's
86:20 number with a decimal point there's another type called complex
86:22 another type called complex complex numbers are an extension of the
86:25 complex numbers are an extension of the familiar real number system in which all
86:28 familiar real number system in which all numbers are expressed as a sum of a real
86:30 numbers are expressed as a sum of a real part
86:31 part and an imaginary part
86:33 and an imaginary part imaginary numbers are real multiples of
86:35 imaginary numbers are real multiples of the imaginary unit which is the square
86:38 the imaginary unit which is the square root of negative one often written
86:40 root of negative one often written i in mathematics or j in engineering
86:44 i in mathematics or j in engineering python is built in support for complex
86:46 python is built in support for complex numbers which are written with
86:48 numbers which are written with the the j notation so the imaginary part
86:52 the the j notation so the imaginary part is written with a s with a j suffix
86:55 is written with a s with a j suffix so you can combine it you can use a
86:58 so you can combine it you can use a literal value like complex
87:03 equals two plus
87:05 two plus three j so the the j means it's the
87:08 three j so the the j means it's the imaginary part of the number or you can
87:11 imaginary part of the number or you can use the complex constructor so i can put
87:14 use the complex constructor so i can put num
87:15 num equals
87:17 equals complex
87:18 complex and then i can pass in
87:20 and then i can pass in two comma three so
87:23 two comma three so the three part is the imaginary part the
87:26 the three part is the imaginary part the two is the the real part the integer
87:29 two is the the real part the integer part
87:30 part and then once you have a complex number
87:34 and then once you have a complex number you can get it's real or imaginary part
87:37 you can get it's real or imaginary part like this so i can say print
87:39 like this so i can say print [Music]
87:40 [Music] num
87:41 num dot real
87:43 dot real or num
87:45 or num dot match
87:47 dot match so this is going to be the 2 this is
87:50 so this is going to be the 2 this is going to be the 3. so if i just
87:52 going to be the 3. so if i just uh
87:54 uh let me
87:55 let me i think the problem was
88:00 i think the problem was num
88:00 num num one no so i'll do num one and num
88:03 num one no so i'll do num one and num two and we're gonna do this as num two
88:06 two and we're gonna do this as num two okay let's check this so
88:08 okay let's check this so this is the real part this is the
88:10 this is the real part this is the imaginary part you can see they're being
88:12 imaginary part you can see they're being returned as floats
88:15 returned as floats and you can use the type function to
88:18 and you can use the type function to check the type
88:20 check the type so now let's talk about some built-in
88:21 so now let's talk about some built-in functions that help with numbers so one
88:24 functions that help with numbers so one of them is abs abs will return the
88:27 of them is abs abs will return the absolute value of a number so if i say
88:31 absolute value of a number so if i say 5.5
88:33 5.5 that's just going to be
88:35 that's just going to be 5.5 but if i put negative 5.5 well
88:39 5.5 but if i put negative 5.5 well it will be 5.5 so
88:42 it will be 5.5 so [Music]
88:44 [Music] so if i print this see 5.5 basically it
88:47 so if i print this see 5.5 basically it just makes it so it's not negative
88:49 just makes it so it's not negative then you can also use
88:51 then you can also use round so if we do round
88:55 round so if we do round let's make this just 5.5 round is going
88:59 let's make this just 5.5 round is going to round to the nearest integer so if i
89:01 to round to the nearest integer so if i do this it's just gonna be six so point
89:04 do this it's just gonna be six so point five is going to round up but if we did
89:06 five is going to round up but if we did uh
89:07 uh four nine
89:09 four nine it's going to go down to five
89:11 it's going to go down to five you can also specify a second parameter
89:14 you can also specify a second parameter to set the decimal points precision so
89:18 to set the decimal points precision so i can
89:23 go to if i do one here and i round it it's going to
89:26 one here and i round it it's going to instead of rounding to the nearest
89:28 instead of rounding to the nearest integer it's now going to round to the
89:29 integer it's now going to round to the nearest tenths place value or one
89:33 nearest tenths place value or one decimal point
89:34 decimal point there are several other math utility
89:36 there are several other math utility functions and constants that are
89:37 functions and constants that are provided by the math standard library
89:39 provided by the math standard library like there's a math package a c math
89:41 like there's a math package a c math package decimal pages fractions package
89:44 package decimal pages fractions package that makes it easier to work with
89:47 that makes it easier to work with different types of numbers we'll explore
89:49 different types of numbers we'll explore some of those more later on
89:50 some of those more later on now let's talk about in nums and noms
89:53 now let's talk about in nums and noms are readable names that are bound to a
89:55 are readable names that are bound to a constant value so to use a noms we're
89:58 constant value so to use a noms we're going to have to import and numbs from
89:59 going to have to import and numbs from the inum standard library module
90:02 the inum standard library module like this from enum import enum and now
90:06 like this from enum import enum and now we'll be talking more about
90:08 we'll be talking more about importing
90:10 importing stuff from modules later but once you
90:13 stuff from modules later but once you import it then we can initialize a new
90:15 import it then we can initialize a new and nom in this way so do class
90:18 and nom in this way so do class state enum
90:27 and so we can have inactive [Music]
90:28 [Music] equals 0
90:30 equals 0 and active
90:31 and active [Music]
90:32 [Music] equals one
90:35 equals one so basically the the word state this can
90:37 so basically the the word state this can be anywhere any
90:39 be anywhere any word we like so we're setting
90:42 word we like so we're setting uh
90:44 uh basically a variable called
90:45 basically a variable called state.inactive
90:46 state.inactive which is going to equal 0 or
90:48 which is going to equal 0 or state.active to equal 1.
90:50 state.active to equal 1. so
90:51 so you can reference this how you would
90:52 you can reference this how you would reference it you can do print
90:55 reference it you can do print state
90:56 state dot
90:57 dot active
90:59 active and then if i just run the program we'll
91:02 and then if i just run the program we'll see
91:03 see now you can see it's just going to
91:04 now you can see it's just going to return state to active instead of one
91:07 return state to active instead of one so to actually get the value you use
91:09 so to actually get the value you use state to active dot value
91:11 state to active dot value and then we run that and then we'll see
91:13 and then we run that and then we'll see one here
91:15 one here if you we want to just return
91:16 if you we want to just return state.active that the sa that value can
91:19 state.active that the sa that value can be reached by the number assigned in the
91:21 be reached by the number assigned in the num so state we can do
91:24 num so state we can do one
91:25 one and if i print that it's now going to
91:27 and if i print that it's now going to say state.active
91:29 say state.active same for using the square brackets
91:31 same for using the square brackets notation so i could do states
91:35 notation so i could do states i'll do see square brackets and put
91:38 i'll do see square brackets and put active
91:39 active if i print that it's going to print
91:42 if i print that it's going to print state that active so this is basically
91:46 state that active so this is basically the only way to create constants in
91:49 the only way to create constants in python
91:50 python python is no way to enforce the variable
91:53 python is no way to enforce the variable should be a constant so some people use
91:56 should be a constant so some people use enums to create a constant
91:59 enums to create a constant and then nobody can reassign the value
92:01 and then nobody can reassign the value so
92:02 so when we do
92:03 when we do state
92:05 state or state active dot
92:08 or state active dot value
92:10 value so this is it's not going to be able to
92:11 so this is it's not going to be able to be reassigned so basically there's two
92:14 be reassigned so basically there's two ways to do we can do this bracket
92:15 ways to do we can do this bracket notation or we can go back to the other
92:18 notation or we can go back to the other way
92:19 way active
92:20 active now we can also list all the possible
92:22 now we can also list all the possible values for enum so
92:25 values for enum so our num is called state and we can now
92:27 our num is called state and we can now just print all the values oh i actually
92:30 just print all the values oh i actually did that wrong it's supposed to be a
92:31 did that wrong it's supposed to be a list
92:33 list so this is going to list the values of
92:35 so this is going to list the values of the state
92:37 the state and we can see we have inactive 0 and
92:40 and we can see we have inactive 0 and active is 1. and we can also count them
92:44 active is 1. and we can also count them using the length function so we're going
92:46 using the length function so we're going to print the result of a length state
92:49 to print the result of a length state and that's just going to give us 2.
92:51 and that's just going to give us 2. okay let's talk about more about user
92:53 okay let's talk about more about user input now we already discussed a little
92:54 input now we already discussed a little bit in our first program
92:56 bit in our first program but you can do get user input by using
93:00 but you can do get user input by using the input function so let's just get rid
93:02 the input function so let's just get rid of all this
93:04 of all this and we'll do
93:06 and we'll do age
93:07 age equals input
93:10 equals input and we can say
93:12 and we can say print
93:18 your age is and then we just can concatenate that
93:20 and then we just can concatenate that with the
93:22 with the variable age and then also if you want
93:25 variable age and then also if you want to
93:26 to so let's just do a quick test and right
93:28 so let's just do a quick test and right now it's looking for the age right now i
93:30 now it's looking for the age right now i can put five your age is five so there's
93:33 can put five your age is five so there's two ways to make it say what is your age
93:35 two ways to make it say what is your age we can do a print statement right before
93:37 we can do a print statement right before here
93:38 here and do what is
93:41 and do what is your age
93:43 your age and then
93:45 and then now we can put four your age is four
93:48 now we can put four your age is four now you can also ins instead of
93:51 now you can also ins instead of putting the print statement right before
93:52 putting the print statement right before here i'm going to copy this delete that
93:55 here i'm going to copy this delete that and we can put it right in this input
93:58 and we can put it right in this input function
93:59 function and then i'll say it'll still say what
94:01 and then i'll say it'll still say what is your age and i can
94:03 is your age and i can put an age here
94:11 so one thing to really realize about this is that it gets the input at
94:13 this is that it gets the input at runtime meaning the program will stop
94:16 runtime meaning the program will stop execution and will wait until the user
94:18 execution and will wait until the user types something and presses the enter
94:20 types something and presses the enter key
94:21 key you can also do more complex input
94:23 you can also do more complex input processing and accept input at program
94:26 processing and accept input at program invocation time and we'll see how to do
94:28 invocation time and we'll see how to do that later on
94:30 that later on if you want to get the input when the
94:32 if you want to get the input when the program is run that's going to work
94:33 program is run that's going to work better for command line applications
94:35 better for command line applications other kinds of applications will need a
94:37 other kinds of applications will need a different way of accepting input
94:39 different way of accepting input let's look more at control statements
94:42 let's look more at control statements this is another thing we've already
94:43 this is another thing we've already discussed earlier but we're going to
94:46 discussed earlier but we're going to review it and and look at it in a little
94:48 review it and and look at it in a little more detail so a control statement is
94:50 more detail so a control statement is like an if statement so if condition
94:52 like an if statement so if condition that's this variable here equals true
94:54 that's this variable here equals true then it's going to run everything in the
94:56 then it's going to run everything in the block a block is
94:58 block a block is the part that is indented one level
95:01 the part that is indented one level usually it's going to be either four or
95:03 usually it's going to be either four or two spaces in this case it's four spaces
95:05 two spaces in this case it's four spaces sometimes it's two spaces it doesn't
95:07 sometimes it's two spaces it doesn't matter it could even be one space as
95:08 matter it could even be one space as long as it's the same as long as every
95:11 long as it's the same as long as every line of code is indented the same amount
95:13 line of code is indented the same amount so if i just run that
95:15 so if i just run that the condition was true the block can be
95:18 the condition was true the block can be formed by a single line or multiple
95:19 formed by a single line or multiple lines and it ends whenever you move back
95:21 lines and it ends whenever you move back to the previous indentation level
95:24 to the previous indentation level so
95:25 so for instance if once we are not indented
95:28 for instance if once we are not indented i can say print
95:30 i can say print outside
95:32 outside if
95:32 if so then
95:34 so then that's always going to pres it's always
95:35 that's always going to pres it's always going to print this because it's not in
95:37 going to print this because it's not in that if statement
95:39 that if statement and then we have the if else statements
95:41 and then we have the if else statements where the else is if
95:43 where the else is if if this does not true then it's going to
95:45 if this does not true then it's going to do whatever is in here so if i just
95:48 do whatever is in here so if i just change this to false
95:49 change this to false then it's going to
95:51 then it's going to print whatever the condition was false
95:54 print whatever the condition was false and then we can have this series this is
95:56 and then we can have this series this is something that we showed in the per the
95:57 something that we showed in the per the program earlier but if and then elif
96:00 program earlier but if and then elif combines else and if so
96:02 combines else and if so if this is not true then i'll move on to
96:04 if this is not true then i'll move on to this line and else
96:06 this line and else if this is true then i'll do this else
96:08 if this is true then i'll do this else if this is true and it'll just keep
96:10 if this is true and it'll just keep going on and then it will always do the
96:13 going on and then it will always do the this is if none of the other ones were
96:14 this is if none of the other ones were true it's going to do this
96:18 true it's going to do this so since
96:19 so since it was testing this it's not even going
96:21 it was testing this it's not even going to evaluate anything later but if we
96:25 to evaluate anything later but if we move this to false
96:27 move this to false and we change this to bow
96:30 and we change this to bow then it's actually going to skip all the
96:32 then it's actually going to skip all the way down
96:33 way down all the way to this else here
96:36 all the way to this else here and if we do flavio
96:39 and if we do flavio you can print that and then it's going
96:41 you can print that and then it's going to say hello flavio from right here
96:44 to say hello flavio from right here okay that's all we're going to talk
96:45 okay that's all we're going to talk about for this for now since we've
96:46 about for this for now since we've already covered it earlier in the course
96:48 already covered it earlier in the course now i'm going to go into more detail
96:50 now i'm going to go into more detail about lists lists are an essential
96:53 about lists lists are an essential python data structure and so an example
96:57 python data structure and so an example of a list would be let's create a list
96:58 of a list would be let's create a list called dogs so we're going to create
97:02 called dogs so we're going to create the dog names we have roger we have sid
97:06 the dog names we have roger we have sid and this allows you to group together
97:08 and this allows you to group together multiple values and reference them all
97:10 multiple values and reference them all with a common name so we have a list of
97:13 with a common name so we have a list of dogs and this is just two strings so the
97:16 dogs and this is just two strings so the list always going to have the opening
97:17 list always going to have the opening closing brackets and each item in the
97:19 closing brackets and each item in the list is going to be separated with a
97:21 list is going to be separated with a comma and a list can hold different
97:23 comma and a list can hold different types of values so these are all strings
97:26 types of values so these are all strings but we can
97:27 but we can have a string
97:28 have a string an integer a string a
97:31 an integer a string a boolean
97:33 boolean and you can mix different types of data
97:36 and you can mix different types of data types
97:37 types in a single list
97:39 in a single list and then you can check if an item is
97:41 and then you can check if an item is contained in a list with the in operator
97:45 contained in a list with the in operator so we talked about the in operator
97:47 so we talked about the in operator earlier but let me show you how that
97:48 earlier but let me show you how that works so we're going to print here's
97:50 works so we're going to print here's where we can use the in operator we're
97:53 where we can use the in operator we're going to check if roger
97:55 going to check if roger is
97:56 is in
97:57 in dogs
97:58 dogs so let's see
98:00 so let's see so run that true but now let's check if
98:03 so run that true but now let's check if bo is in dogs well
98:05 bo is in dogs well false because it's checking
98:08 false because it's checking so this is how you can check if an item
98:10 so this is how you can check if an item is in a list
98:11 is in a list you can also define a list as an empty
98:14 you can also define a list as an empty string so i could actually just
98:16 string so i could actually just remove all this and now we just have an
98:19 remove all this and now we just have an empty list and this is obviously still
98:21 empty list and this is obviously still going to be false because there's
98:22 going to be false because there's nothing in that list
98:24 nothing in that list but let's go back to when we had some
98:27 but let's go back to when we had some items in the list
98:29 items in the list and you can reference items in a list by
98:32 and you can reference items in a list by their indexes starting with zero so i'm
98:36 their indexes starting with zero so i'm going to do dogs and then i can use
98:38 going to do dogs and then i can use these brackets so and now i'm going to
98:40 these brackets so and now i'm going to put the so this is where we're going to
98:42 put the so this is where we're going to reference the thing
98:43 reference the thing an item from the list and i'm going to
98:45 an item from the list and i'm going to type in 0 which will be this item right
98:48 type in 0 which will be this item right here roger or i could put 2 and it's
98:52 here roger or i could put 2 and it's going to do 0 1 2 and now we're going to
98:55 going to do 0 1 2 and now we're going to have sid
98:57 have sid and the same
99:00 and the same and you can use this same notation to
99:04 and you can use this same notation to update an item
99:06 update an item in a list so i'm going to add another
99:08 in a list so i'm going to add another line of code here and put dogs 2 is
99:11 line of code here and put dogs 2 is going to equal
99:19 bo and now i'm just going to print the
99:20 and now i'm just going to print the entire list here and now
99:23 entire list here and now instead of
99:25 instead of being roger 1 said true to roger 1 bow
99:28 being roger 1 said true to roger 1 bow true
99:29 true because we've updated the item at index
99:32 because we've updated the item at index 2 to be bo instead of sid
99:42 now you can also use the index method so
99:42 so um instead of
99:45 um instead of like if i want to find the first item in
99:47 like if i want to find the first item in the list i could do it like this
99:49 the list i could do it like this so you can also use a negative number
99:52 so you can also use a negative number here
99:53 here just how we saw on the string so
99:55 just how we saw on the string so negative 2 is going to start with one
99:56 negative 2 is going to start with one two
99:58 two actually let's do negative one so it
100:00 actually let's do negative one so it should return true here
100:02 should return true here okay true so it starts with this one if
100:04 okay true so it starts with this one if you put a negative number
100:07 you put a negative number you can also extract part of a list now
100:10 you can also extract part of a list now this is very similar to what we showed
100:13 this is very similar to what we showed using
100:14 using with the string let me just add another
100:16 with the string let me just add another item here
100:17 item here [Music]
100:21 [Music] now i am going to
100:25 now i am going to use the colon to do part of a list so
100:27 use the colon to do part of a list so i'm going to 2
100:28 i'm going to 2 4
100:29 4 and so this is a slice
100:32 and so this is a slice so it's going to start at the second or
100:35 so it's going to start at the second or zero
100:36 zero one
100:40 two which is now bow because we change it to
100:42 which is now bow because we change it to bow and it's going to go through four
100:45 bow and it's going to go through four it's going to go through four but not
100:46 it's going to go through four but not over pat not including four so it's
100:49 over pat not including four so it's going to be 2
100:50 going to be 2 3
100:51 3 and then not 4 so it's just sid and true
100:54 and then not 4 so it's just sid and true or bow and true because we updated 2 to
100:56 or bow and true because we updated 2 to bow
100:57 bow and you can also just leave this blank
100:59 and you can also just leave this blank so it's going to go through the end of
101:00 so it's going to go through the end of the list or if you
101:02 the list or if you leave the first number blank
101:05 leave the first number blank it's going to go it's going to start at
101:07 it's going to go it's going to start at the beginning of the list and we can go
101:09 the beginning of the list and we can go through for instance index three
101:12 through for instance index three and so that's a way to slice the list
101:15 and so that's a way to slice the list you can also
101:17 you can also use the the length function so let's
101:20 use the the length function so let's find out how many items are in the list
101:22 find out how many items are in the list i'll use the length the length of dogs
101:25 i'll use the length the length of dogs is six or six items in the list we can
101:28 is six or six items in the list we can also add items to the list by using the
101:31 also add items to the list by using the append method so i'm going to do dogs
101:35 append method so i'm going to do dogs dot append
101:36 dot append and then i can
101:39 and then i can add an item so i can say something like
101:42 add an item so i can say something like judah and now if we see the length
101:44 judah and now if we see the length there's now going to be 7 and if we just
101:47 there's now going to be 7 and if we just print
101:48 print the full list
101:50 the full list then we can see that we can see all the
101:52 then we can see that we can see all the items including the one that was just
101:54 items including the one that was just added we can also use the extend method
101:58 added we can also use the extend method the extend is another way to add an item
102:02 the extend is another way to add an item to a list so if i do instead of dogs
102:05 to a list so if i do instead of dogs append i can do dogs that extend
102:09 append i can do dogs that extend and then i'm going to pass in
102:11 and then i'm going to pass in instead of just passing in the string
102:13 instead of just passing in the string i'm gonna pass in
102:15 i'm gonna pass in the item as a list so i'm gonna and it's
102:19 the item as a list so i'm gonna and it's gonna add it just the same but now i can
102:21 gonna add it just the same but now i can actually
102:23 actually combine two lists together so i'm gonna
102:26 combine two lists together so i'm gonna put a five so if i do this we can see
102:29 put a five so if i do this we can see now we are taking this list and
102:32 now we are taking this list and extending it by adding this list on the
102:35 extending it by adding this list on the end this is a two item list and we have
102:37 end this is a two item list and we have that six m list and now we have the
102:38 that six m list and now we have the eight item list
102:40 eight item list you can also use the
102:42 you can also use the plus equals operator so
102:46 plus equals operator so to use the plus equals operator i'm
102:48 to use the plus equals operator i'm going to do dogs it's the same it's
102:50 going to do dogs it's the same it's going to do the same thing as extend
102:53 going to do the same thing as extend so plus equals
102:55 so plus equals and then we have this list take this
102:58 and then we have this list take this parentheses off here and it should look
103:00 parentheses off here and it should look exactly the same see it's showing up the
103:02 exactly the same see it's showing up the same thing up here
103:03 same thing up here so the plus equals is going to be the
103:06 so the plus equals is going to be the same thing as extend where it takes the
103:09 same thing as extend where it takes the list that's already exists and adds this
103:12 list that's already exists and adds this other list to the end
103:15 other list to the end and when you're using the extend or the
103:17 and when you're using the extend or the plus equals
103:19 plus equals you don't want to forget the square
103:21 you don't want to forget the square brackets
103:22 brackets here so if you if you forget the square
103:25 here so if you if you forget the square brackets and let's say i'm just going to
103:26 brackets and let's say i'm just going to add this iron to the list
103:29 add this iron to the list it's now actually going to put each
103:31 it's now actually going to put each letter individually here
103:33 letter individually here so if i you can kind of see it better if
103:35 so if i you can kind of see it better if i move over here
103:39 i move over here so it so that so you want to make sure
103:41 so it so that so you want to make sure you put the the brackets
103:44 you put the the brackets and another thing you can do is remove
103:47 and another thing you can do is remove you can remove an item using the remove
103:50 you can remove an item using the remove method so i'm gonna do
103:52 method so i'm gonna do dogs dot remove
103:55 dogs dot remove sid
103:57 sid okay now i'm gonna play it here and
104:01 okay now i'm gonna play it here and it's saying
104:14 oh obviously um i'm moving sid but we've already changed the element of sid to
104:16 already changed the element of sid to bow so let's remove
104:18 bow so let's remove quincy and let's try that
104:22 quincy and let's try that okay so now there's no quincy
104:25 okay so now there's no quincy so another common thing to do another
104:28 so another common thing to do another common list method is pop so if i do
104:32 common list method is pop so if i do dogs.pop
104:34 dogs.pop it's going to remove and return a single
104:37 it's going to remove and return a single item so first i'm going to
104:40 item so first i'm going to [Music]
104:43 [Music] do dot
104:44 do dot print dogs that pop and then i'm going
104:45 print dogs that pop and then i'm going to print dog so if i
104:47 to print dog so if i so first it's going to return 5 that was
104:50 so first it's going to return 5 that was the last item that we added onto the
104:52 the last item that we added onto the list and now when i print the list that
104:54 list and now when i print the list that item's not in the list so pop is going
104:57 item's not in the list so pop is going to remove the last item from the list
105:00 to remove the last item from the list and it's going to return the last item
105:03 and it's going to return the last item it's going to return and remove the last
105:04 it's going to return and remove the last item from the list and then it's not on
105:06 item from the list and then it's not on the list anymore
105:09 the list anymore now let's make this
105:11 now let's make this let's let's simplify this just go back
105:13 let's let's simplify this just go back to the initial list and i'm going to
105:14 to the initial list and i'm going to change this to items
105:16 change this to items now i'm going to show you how to add an
105:18 now i'm going to show you how to add an item in the middle of the list at a
105:20 item in the middle of the list at a specific index you can use the insert
105:23 specific index you can use the insert method so i'm going to do items dot
105:26 method so i'm going to do items dot insert
105:27 insert i'm going to put the index which is
105:28 i'm going to put the index which is going we're going to insert at the index
105:30 going we're going to insert at the index number two and the item is going to be
105:32 number two and the item is going to be test
105:33 test and then i'm just going to print that
105:35 and then i'm just going to print that print
105:37 print items
105:38 items and then i'll run that and then we can
105:40 and then i'll run that and then we can see it index number two we now see the
105:42 see it index number two we now see the item test
105:44 item test now to add multiple items at a specific
105:47 now to add multiple items at a specific index you need to use slices so
105:50 index you need to use slices so let me show you how you do that so we're
105:52 let me show you how you do that so we're going to do a slice
105:59 and i'm going to set that equal to test
106:00 test 1
106:06 test 2.
106:07 2. we print that
106:08 we print that so now you can see we have test 1 and
106:10 so now you can see we have test 1 and test 2 right here
106:12 test 2 right here right behind this search thing here
106:14 right behind this search thing here and we've inserted multiple items into
106:17 and we've inserted multiple items into the list starting at index 1.
106:21 the list starting at index 1. now you can also sort a list
106:24 now you can also sort a list so if i do
106:26 so if i do here we go um
106:28 here we go um items dot sort
106:31 items dot sort it will sort the list
106:38 but you have to make sure okay we have an error it's not supported between
106:40 an error it's not supported between we have a combination of ins and strings
106:44 we have a combination of ins and strings so let's
106:45 so let's make it so it's all strings in the list
106:51 and then it should
106:53 then it should be able to sort it
106:58 okay now the
106:59 now the strings are in alphabetical order and if
107:02 strings are in alphabetical order and if we're using integers or floats and they
107:03 we're using integers or floats and they would just be in numerical order
107:06 would just be in numerical order now one thing that's interesting about
107:08 now one thing that's interesting about that if i put change this to
107:11 that if i put change this to bow
107:13 bow you'll see now we have this at the
107:15 you'll see now we have this at the beginning and this at the end
107:18 beginning and this at the end so
107:19 so a the sort method orders uppercase
107:21 a the sort method orders uppercase letters first and then lowercase letters
107:24 letters first and then lowercase letters so to fix that actually to make it
107:27 so to fix that actually to make it make more sense we're going to change
107:28 make more sense we're going to change that to
107:29 that to bob and to fix that
107:32 bob and to fix that within the sort i'm going to i'm going
107:34 within the sort i'm going to i'm going to put key equals str dot lower
107:42 and now it's going to sort these correctly how
107:44 it's going to sort these correctly how you would imagine not caring about
107:46 you would imagine not caring about uppercase or lowercase letters
107:50 uppercase or lowercase letters sorting modifies the original list
107:52 sorting modifies the original list content so to avoid that you can copy
107:55 content so to avoid that you can copy the list content using
107:58 the list content using so let me show you if we do
108:06 items items
108:10 copy equals
108:11 equals items
108:12 items and then we make a slice
108:15 and then we make a slice with with nothing at the beginning and
108:16 with with nothing at the beginning and nothing at the end so it's going to
108:18 nothing at the end so it's going to start at the beginning of the list to
108:19 start at the beginning of the list to the end of the list and now we can have
108:22 the end of the list and now we can have a copy and i can print
108:26 a copy and i can print items copy
108:28 items copy so if i print that so now we see we have
108:30 so if i print that so now we see we have the sorted list that's that we ran the
108:33 the sorted list that's that we ran the sort on but we also still have
108:36 sort on but we also still have the original list
108:42 so it with all the words in the original order
108:48 and there is also a way to sort a list without returning a
108:51 without returning a new list
108:53 new list there is also a way to sort a list
108:56 there is also a way to sort a list without modifying the original list so
108:59 without modifying the original list so instead of copying a list
109:02 instead of copying a list um what i'm going to do
109:04 um what i'm going to do is
109:06 is i items
109:08 i items instead of doing items dot sort
109:11 instead of doing items dot sort we are going to do
109:13 we are going to do we're going to use a global function
109:14 we're going to use a global function called sorted
109:16 called sorted so in the sorted function
109:19 so in the sorted function we are going to pass in two parameters
109:22 we are going to pass in two parameters so first is the list items
109:28 and then the second is how we're sorting it so this just makes
109:30 it so this just makes sure that the key the case of the
109:33 sure that the key the case of the letters don't matter and then i'm just
109:35 letters don't matter and then i'm just going to print that
109:44 now if i run this you can see we it we print printed the sorted list
109:46 we print printed the sorted list and now we're printing the list and it's
109:49 and now we're printing the list and it's not it's no longer sorted because this
109:51 not it's no longer sorted because this creates a new list without modifying the
109:55 creates a new list without modifying the original list
109:58 original list okay now let's learn about another data
110:00 okay now let's learn about another data structure called
110:02 structure called couples
110:03 couples so
110:04 so this time i'm going to put
110:06 this time i'm going to put tuples
110:08 tuples so we're using a comment here so tuples
110:11 so we're using a comment here so tuples are another fundamental python data
110:12 are another fundamental python data structure they allow you to create
110:14 structure they allow you to create immutable groups of objects this means
110:17 immutable groups of objects this means that once a tuple is created it cannot
110:19 that once a tuple is created it cannot be modified so we already saw that we
110:21 be modified so we already saw that we could modify lists but tuple you can't
110:24 could modify lists but tuple you can't even add or remove items
110:26 even add or remove items they're created in a way similar to
110:28 they're created in a way similar to lists but using parentheses instead of
110:30 lists but using parentheses instead of square brackets so
110:33 square brackets so for instance i'm going to do names
110:35 for instance i'm going to do names equals instead of using square brackets
110:36 equals instead of using square brackets we're going to do parentheses roger
110:39 we're going to do parentheses roger and
110:42 and sid
110:43 sid okay so a tuple's order like a list so
110:45 okay so a tuple's order like a list so you can get its values by referencing an
110:47 you can get its values by referencing an index
110:49 index an index so i could say for instance
110:52 an index so i could say for instance names and if i do the bracket i could
110:54 names and if i do the bracket i could put a zero here to return
110:57 put a zero here to return roger
110:58 roger and then you can also use the index
111:00 and then you can also use the index method for instance names
111:03 method for instance names dot
111:05 dot index
111:07 index and then i can
111:09 and then i can pass in something like
111:11 pass in something like roger and then this is going to
111:14 roger and then this is going to return 0
111:16 return 0 because it's going to get the the index
111:18 because it's going to get the the index number of that so as with strings and
111:21 number of that so as with strings and lists using a negative index we'll start
111:23 lists using a negative index we'll start searching from the end so i could do
111:31 that i can do negative one to start not negative zero negative one to start
111:33 not negative zero negative one to start searching from the end here
111:35 searching from the end here and you can count the items in a tuple
111:37 and you can count the items in a tuple with the length function so i could do
111:39 with the length function so i could do if i do length and then do names it's if
111:43 if i do length and then do names it's if i printed that it would just print 2
111:45 i printed that it would just print 2 because there are two items in that
111:47 because there are two items in that tuple
111:48 tuple then you can also check if an item is
111:50 then you can also check if an item is contained in a tuple with the in
111:51 contained in a tuple with the in operators very similar to a list so i
111:54 operators very similar to a list so i can do this time i will print it i'll do
111:56 can do this time i will print it i'll do print
111:57 print roger
111:59 roger in
112:00 in names so if i print that and run it
112:02 names so if i print that and run it we'll see true because roger is in the
112:05 we'll see true because roger is in the names
112:06 names and then you can extract part of a tuple
112:08 and then you can extract part of a tuple using slices
112:10 using slices just like we could do with with strings
112:12 just like we could do with with strings and lists so i could do names and then i
112:16 and lists so i could do names and then i could do
112:17 could do 0 starting at the whoops
112:21 0 starting at the whoops 0
112:23 0 2
112:24 2 so that's just going to start at the
112:26 so that's just going to start at the zero index and be done at the index 2
112:29 zero index and be done at the index 2 and then
112:30 and then and then you can use the so you can get
112:32 and then you can use the so you can get a sorted version of the tuple using the
112:34 a sorted version of the tuple using the sorted global function so
112:36 sorted global function so remember when we were looking at lists
112:39 remember when we were looking at lists when we used the sorted function it
112:42 when we used the sorted function it created a new a new list or so when
112:46 created a new a new list or so when we're creating using the sorted function
112:48 we're creating using the sorted function for tuples it creates a new tuple so i
112:51 for tuples it creates a new tuple so i can say sorted
112:53 can say sorted [Music]
112:54 [Music] names
112:56 names and this would put them in alphabetical
112:58 and this would put them in alphabetical order they already in are in
112:59 order they already in are in alphabetical order but say there is one
113:01 alphabetical order but say there is one more
113:02 more word in this list
113:04 word in this list and then
113:05 and then i can print this
113:07 i can print this [Music]
113:09 [Music] to print the sorted version but it's not
113:12 to print the sorted version but it's not actually going to modify the list
113:14 actually going to modify the list because you cannot modify
113:16 because you cannot modify a tuple
113:18 a tuple and then you can create a new tuple from
113:22 and then you can create a new tuple from existing tuples using the plus operator
113:24 existing tuples using the plus operator so i could say something like
113:26 so i could say something like new tuple
113:27 new tuple [Music]
113:29 [Music] equals names and then i can use the plus
113:32 equals names and then i can use the plus operator
113:33 operator and then i would say i would just say
113:36 and then i would say i would just say tina
113:37 tina and
113:38 and quincy i could add a few i could add so
113:41 quincy i could add a few i could add so these will combine two tuples into a new
113:43 these will combine two tuples into a new tuple but you can never like i said you
113:45 tuple but you can never like i said you can't actually modify the original tuple
113:50 can't actually modify the original tuple now let's learn about dictionaries
113:52 now let's learn about dictionaries dictionaries are another very important
113:55 dictionaries are another very important python data structure
113:56 python data structure while lists allow you to create
113:58 while lists allow you to create collections of values dictionaries allow
114:00 collections of values dictionaries allow you to create key value pairs we already
114:03 you to create key value pairs we already discussed dictionaries a little bit but
114:05 discussed dictionaries a little bit but now we're going to discuss even more
114:06 now we're going to discuss even more about dictionaries so let me give you an
114:09 about dictionaries so let me give you an example of a dictionary with just one
114:11 example of a dictionary with just one key value pair so dog equals and then
114:14 key value pair so dog equals and then we're going to use the curly braces to
114:16 we're going to use the curly braces to create the dictionary and i'll put name
114:19 create the dictionary and i'll put name and then i will put
114:21 and then i will put roger and just like any type of strings
114:23 roger and just like any type of strings we could make these single quotes or
114:25 we could make these single quotes or they could be double quotes and the
114:28 they could be double quotes and the spaces here are not very important but
114:31 spaces here are not very important but it's common to put spaces in between
114:33 it's common to put spaces in between these things for better readability
114:36 these things for better readability and the key can be any immutable
114:39 and the key can be any immutable value so this is the key this is the
114:41 value so this is the key this is the value and the key can be any immutable
114:44 value and the key can be any immutable value
114:45 value such as a string a number or a tuple
114:48 such as a string a number or a tuple the value can be anything you want so a
114:50 the value can be anything you want so a dictionary can contain multiple key
114:52 dictionary can contain multiple key value pairs so like for instance we got
114:55 value pairs so like for instance we got the name we can have
114:57 the name we can have age
114:59 age and that's going to be
115:06 8. and you can access individual key values using the notation like this so i
115:09 values using the notation like this so i can say
115:10 can say dog
115:12 dog so i'm using the bracket notation i can
115:14 so i'm using the bracket notation i can do name so if i print this it's just
115:17 do name so if i print this it's just going to
115:19 going to print roger
115:24 and then again i can use the single quotes 2 if i want
115:27 quotes 2 if i want and it still prints roger
115:30 and it still prints roger and then you can use the same notation
115:32 and then you can use the same notation to change the value stored at a specific
115:35 to change the value stored at a specific index
115:36 index so let's say i want to change the name
115:39 so let's say i want to change the name so if i do dog
115:41 so if i do dog and i'm going to say that the name
115:43 and i'm going to say that the name is now going to equal
115:47 is now going to equal sid
115:48 sid now i'm just going to print the whole
115:50 now i'm just going to print the whole the whole thing
115:52 the whole thing and we can see the name is now sid
115:54 and we can see the name is now sid instead of roger
115:56 instead of roger so another way to get a specific element
115:59 so another way to get a specific element is to use the get method so if i do a
116:03 is to use the get method so if i do a dog dot
116:05 dog dot get
116:06 get and then i
116:08 and then i do name so i'm going to try to get the
116:10 do name so i'm going to try to get the name here
116:12 name here it's going to return roger so
116:14 it's going to return roger so one good thing about this is that you
116:16 one good thing about this is that you can add a default value like let's say
116:19 can add a default value like let's say i'm going i'm searching for color
116:24 i'm going i'm searching for color and it's saying none it's giving it none
116:27 and it's saying none it's giving it none because there is no color
116:28 because there is no color but what if i want a default value so
116:30 but what if i want a default value so i'm going to put comma and then i'll put
116:33 i'm going to put comma and then i'll put brown
116:39 so now if it cannot find the color in the dictionary it's going to return
116:41 the dictionary it's going to return brown but if it can find the color like
116:44 brown but if it can find the color like let's say
116:46 let's say color
116:48 color and this is a
116:50 and this is a green dog
116:53 green dog okay we'll return
116:54 okay we'll return green
116:55 green so with the bracket notation that was
116:57 so with the bracket notation that was showing you earlier you cannot have a
116:59 showing you earlier you cannot have a default value so that's one good thing
117:01 default value so that's one good thing about the get method
117:02 about the get method now you can also use the pop method to
117:05 now you can also use the pop method to retrieve the value of a key and delete
117:07 retrieve the value of a key and delete the item from the dictionary we also
117:09 the item from the dictionary we also showed the pop method for lists so for
117:13 showed the pop method for lists so for instance i can say
117:15 instance i can say get dot
117:17 get dot pop
117:18 pop and then i will pass in
117:20 and then i will pass in name
117:21 name and then right after that i'm just going
117:23 and then right after that i'm just going to print
117:28 dog that the the whole dictionary so first we're going to get roger and then
117:30 first we're going to get roger and then when i print the dictionary here it's
117:32 when i print the dictionary here it's not going to show roger anymore because
117:35 not going to show roger anymore because we we deleted it pop will return the
117:37 we we deleted it pop will return the item and delete the item
117:40 item and delete the item now you can also use a function a method
117:43 now you can also use a function a method called pop item the pop item method well
117:47 called pop item the pop item method well let me show you that one pop item
117:49 let me show you that one pop item it's going to retrieve and remove the
117:51 it's going to retrieve and remove the last key value pair inserted into the
117:54 last key value pair inserted into the dictionary
117:56 dictionary so
117:57 so in this case it should be color so if i
118:00 in this case it should be color so if i run this
118:02 run this it's going to return color green and now
118:05 it's going to return color green and now when i print out the dictionary it's not
118:07 when i print out the dictionary it's not going to show color green because that
118:08 going to show color green because that was already removed it removed the last
118:10 was already removed it removed the last item
118:12 item you can also check if a key is contained
118:14 you can also check if a key is contained in a dictionary by using the in operator
118:17 in a dictionary by using the in operator so we're going to say we're going to try
118:20 so we're going to say we're going to try to find out if
118:25 color is in
118:27 is in dog if there's a key called color in dog
118:29 dog if there's a key called color in dog we run that and it says
118:32 we run that and it says true another thing we can do is get a
118:35 true another thing we can do is get a list with the keys in the dictionary
118:37 list with the keys in the dictionary using the keys method so if i do dog dot
118:41 using the keys method so if i do dog dot keys
118:43 keys and then we'll run that it's going to
118:45 and then we'll run that it's going to show the keys so the keys are name age
118:48 show the keys so the keys are name age and
118:49 and color
118:51 color we can see that it's inside the thing
118:53 we can see that it's inside the thing called dick keys but we can also pass
118:56 called dick keys but we can also pass this into list so we return an actual
118:59 this into list so we return an actual just the list part so now we can see
119:03 just the list part so now we can see it's just an actual list name age and
119:06 it's just an actual list name age and color
119:07 color then we can do the same thing with
119:09 then we can do the same thing with values so
119:11 values so instead of dog.keys let's do
119:15 instead of dog.keys let's do values
119:17 values print that
119:18 print that and you can see we have roger 8 green we
119:21 and you can see we have roger 8 green we can pass it into a list
119:24 can pass it into a list to
119:25 to return the app just the list here
119:28 return the app just the list here roger 8 green
119:31 roger 8 green and then finally if we just do items
119:34 and then finally if we just do items [Music]
119:35 [Music] it's going to return all the items in
119:37 it's going to return all the items in the list or all the items in the
119:39 the list or all the items in the dictionary
119:42 dictionary and convert it into a list
119:48 so you can see this is the first item in the list this
119:50 this is the first item in the list this is the second item and then we have the
119:53 is the second item and then we have the third item here so
119:55 third item here so we can see each item of the list each
119:58 we can see each item of the list each item of the dictionary is now in a list
120:01 item of the dictionary is now in a list and then like a lot of the other things
120:03 and then like a lot of the other things we can use the length function and i'll
120:05 we can use the length function and i'll just put dog
120:10 and we can see that there are three items in dog now you can also add a new
120:14 items in dog now you can also add a new key value pair to the dictionary so
120:16 key value pair to the dictionary so let's say i want to do
120:19 let's say i want to do dog
120:24 food or i it doesn't even have to be a single
120:26 or i it doesn't even have to be a single word i could put favorite
120:28 word i could put favorite food
120:30 food and i'm going to say
120:32 and i'm going to say meet
120:34 meet and now we're going to print
120:37 and now we're going to print that
120:43 let's see what do we oh this was supposed to i did that a little
120:45 this was supposed to i did that a little wrong
120:49 there we go this is actually how you do it you put use the bracket notation
120:51 it you put use the bracket notation equals and let's put what it equals
120:53 equals and let's put what it equals there okay now you can see that we now
120:56 there okay now you can see that we now have a new item on the list favorite
120:58 have a new item on the list favorite food
120:59 food meet
121:00 meet then you can also delete an item from a
121:03 then you can also delete an item from a list or a delete a key value pair so i'm
121:06 list or a delete a key value pair so i'm going to d e l means delete dog
121:09 going to d e l means delete dog or
121:10 or dog there we go and this time i'm going
121:12 dog there we go and this time i'm going to delete
121:14 to delete color and i'm just going to use single
121:16 color and i'm just going to use single quotes instead of double quotes to show
121:17 quotes instead of double quotes to show you that doesn't really matter the type
121:19 you that doesn't really matter the type of quote and now you can see we don't
121:21 of quote and now you can see we don't know what color
121:22 know what color this dog is it's no longer a green dog
121:25 this dog is it's no longer a green dog and then you can also copy a dictionary
121:28 and then you can also copy a dictionary so if you want to make two copies of a
121:29 so if you want to make two copies of a dictionary you can do
121:32 dictionary you can do do like this dog copy that's the name of
121:34 do like this dog copy that's the name of the new dictionary i'll do dog dot copy
121:38 the new dictionary i'll do dog dot copy and that would be the new
121:40 and that would be the new copied version of the dictionary
121:42 copied version of the dictionary okay now we are going to talk about a
121:45 okay now we are going to talk about a new thing called sets
121:47 new thing called sets sets are another important python data
121:50 sets are another important python data structure
121:51 structure sets kind of work like tuples but
121:53 sets kind of work like tuples but they're not ordered and they are
121:55 they're not ordered and they are immutable so you can change them
121:58 immutable so you can change them you can also say that they kind of work
121:59 you can also say that they kind of work like dictionaries but they don't have
122:01 like dictionaries but they don't have keys
122:02 keys they're all they have an immutable
122:05 they're all they have an immutable version of a set called a frozen set so
122:08 version of a set called a frozen set so let me show you how you would create a
122:10 let me show you how you would create a set so let's do names
122:12 set so let's do names and you we're going to use
122:14 and you we're going to use curly brackets just like that
122:16 curly brackets just like that and
122:19 and just like that so we have two names so
122:21 just like that so we have two names so you can see the differences the a
122:24 you can see the differences the a dictionary
122:25 dictionary you use the curly brackets but there's
122:27 you use the curly brackets but there's going to be key value pairs but this
122:29 going to be key value pairs but this doesn't have key value pairs in a list
122:31 doesn't have key value pairs in a list it's just going to be a
122:33 it's just going to be a item after item like this but there's
122:35 item after item like this but there's going to be brackets instead of curly
122:37 going to be brackets instead of curly braces
122:38 braces so
122:40 so one thing about the sets is that they
122:42 one thing about the sets is that they are not ordered
122:44 are not ordered so sets work well when you think about
122:46 so sets work well when you think about them as mathematical sets
122:48 them as mathematical sets so for instance let's have we're going
122:50 so for instance let's have we're going to create a set 1
122:53 to create a set 1 with roger and sid and we're going to
122:55 with roger and sid and we're going to have a set
122:57 have a set 2
122:58 2 which is just going to have
123:01 which is just going to have roger
123:02 roger and so you can intersect two sets so
123:06 and so you can intersect two sets so uh the inner intersect
123:12 the intersection of these two sets will be set one
123:15 be set one and
123:16 and set
123:17 set two so if i just print that out print
123:22 two so if i just print that out print intersect
123:23 intersect and then we can just run that and we're
123:25 and then we can just run that and we're gonna see what so it's just roger so the
123:27 gonna see what so it's just roger so the intersection of these two sets are just
123:29 intersection of these two sets are just roger the all the items that they have
123:31 roger the all the items that they have in common you can also create a union of
123:34 in common you can also create a union of two sets so
123:36 two sets so instead of just calling this union i'll
123:39 instead of just calling this union i'll put
123:40 put mod for modification and so i can show a
123:43 mod for modification and so i can show a few different things with the same
123:44 few different things with the same variable name and the union symbol is
123:47 variable name and the union symbol is just the
123:48 just the straight line like this it's not an i
123:50 straight line like this it's not an i it's just the
123:51 it's just the straight line
123:53 straight line it's on the same key as a
123:55 it's on the same key as a as the was it the forward slash
123:57 as the was it the forward slash backslash one of the slashes
123:59 backslash one of the slashes now we're going to get every single item
124:01 now we're going to get every single item in both sets which happens to in this
124:03 in both sets which happens to in this case just happen to be the same as set
124:04 case just happen to be the same as set one but if we change this one to the
124:07 one but if we change this one to the word luna it's just a different name and
124:10 word luna it's just a different name and now we're going to get each item
124:12 now we're going to get each item in both sets or said luna roger for the
124:15 in both sets or said luna roger for the intersection and then we can also get
124:17 intersection and then we can also get the difference the difference between
124:19 the difference the difference between two sets so let me change this to back
124:21 two sets so let me change this to back to roger and for the difference between
124:24 to roger and for the difference between two sets i'll use a the minus
124:27 two sets i'll use a the minus and
124:28 and the difference is just going to be sid
124:30 the difference is just going to be sid that's the only thing that's different
124:31 that's the only thing that's different between the two sets
124:33 between the two sets you can also check if a set is a
124:35 you can also check if a set is a superset of another and if a set is a
124:38 superset of another and if a set is a subset of another so how you would do
124:40 subset of another so how you would do that is
124:42 that is so we're just saying like is this
124:44 so we're just saying like is this greater than that which means it has
124:46 greater than that which means it has everything of the in the other set true
124:49 everything of the in the other set true now if we put the other direction
124:51 now if we put the other direction is does this set have everything in this
124:53 is does this set have everything in this one no it doesn't
124:55 one no it doesn't you can also count items in a set with
124:58 you can also count items in a set with the length function that's pretty
125:00 the length function that's pretty self-explanatory i won't even show it to
125:01 self-explanatory i won't even show it to you we've seen the link function so many
125:03 you we've seen the link function so many times
125:04 times you can also get you can also get a list
125:06 you can also get you can also get a list from the items in a set by passing the
125:08 from the items in a set by passing the set to the list constructor so
125:12 set to the list constructor so i'm just going to remove this
125:14 i'm just going to remove this and we will do
125:16 and we will do list
125:18 list set
125:19 set one
125:20 one okay so now we have a list of the set
125:23 okay so now we have a list of the set and then you can check if an item is
125:25 and then you can check if an item is contained in a set with the in operator
125:27 contained in a set with the in operator just like the
125:28 just like the list and the other way we the other
125:32 list and the other way we the other places we use the in operator and then
125:34 places we use the in operator and then one more final thing about a set a set
125:37 one more final thing about a set a set cannot have two of the same item so
125:40 cannot have two of the same item so that's another thing that's useful about
125:42 that's another thing that's useful about sets so if i type in rogers now we have
125:44 sets so if i type in rogers now we have roger sid roger if i play this we'll see
125:47 roger sid roger if i play this we'll see it's only going to
125:49 it's only going to print sid roger it's not going to add
125:52 print sid roger it's not going to add the roger twice to the set
125:54 the roger twice to the set so that's another
125:56 so that's another useful thing about sets is that make
125:57 useful thing about sets is that make sure there's only one of each item in
126:00 sure there's only one of each item in the the set so if you have a list that
126:03 the the set so if you have a list that has multiple items you can convert it to
126:05 has multiple items you can convert it to a set
126:06 a set um and then i'll just make sure there's
126:07 um and then i'll just make sure there's only one of each thing in that set
126:10 only one of each thing in that set now let's talk more about functions
126:14 now let's talk more about functions we already talked about functions in the
126:16 we already talked about functions in the last section but we're going to do a
126:17 last section but we're going to do a review and then go into even more detail
126:20 review and then go into even more detail about functions so a function lets us
126:23 about functions so a function lets us create a set of instructions that we can
126:25 create a set of instructions that we can run when needed and i'm just going to
126:27 run when needed and i'm just going to paste in a function and again the
126:30 paste in a function and again the indentation it can be either four spaces
126:33 indentation it can be either four spaces two spaces
126:36 two spaces as long as it's indented the exact same
126:38 as long as it's indented the exact same amount
126:39 amount so functions are essential in python and
126:41 so functions are essential in python and in many other programming languages they
126:43 in many other programming languages they help us create meaningful programs
126:45 help us create meaningful programs because they allow us to decompose a
126:46 because they allow us to decompose a program into manageable parts and they
126:49 program into manageable parts and they promote readability and code reuse
126:52 promote readability and code reuse so this one is a function called hello
126:55 so this one is a function called hello that just prints hello this is the
126:58 that just prints hello this is the function definition
126:59 function definition so there's a name called hello this is
127:02 so there's a name called hello this is the name here and then the body of the
127:04 the name here and then the body of the function
127:06 function which is the the set of instructions
127:08 which is the the set of instructions and the body of the function is
127:10 and the body of the function is everything that's after the colon
127:12 everything that's after the colon and everything that's indented one level
127:15 and everything that's indented one level on the right
127:16 on the right so to run this function we must call it
127:19 so to run this function we must call it so i can just type in hello
127:22 so i can just type in hello hello
127:24 hello and then this is the syntax to to call
127:27 and then this is the syntax to to call the function and i can call it multiple
127:29 the function and i can call it multiple times so i can just copy this and paste
127:32 times so i can just copy this and paste it and now if i just run this program
127:34 it and now if i just run this program it's going to print hello
127:36 it's going to print hello three times
127:38 three times the name of the function is very
127:40 the name of the function is very important so the name of the function is
127:43 important so the name of the function is hello it should be the function name
127:46 hello it should be the function name should be descriptive so anyone calling
127:47 should be descriptive so anyone calling it can imagine what the function does
127:51 it can imagine what the function does a function can accept one or more
127:53 a function can accept one or more parameters this is something else that
127:55 parameters this is something else that we saw
127:56 we saw before
127:57 before but
127:58 but i can type in a parameter right here and
128:01 i can type in a parameter right here and this becomes a variable that we can use
128:03 this becomes a variable that we can use in the function so i can change this
128:06 in the function so i can change this instead of printing hello
128:08 instead of printing hello it's going to print hello
128:11 it's going to print hello and then we'll just put name
128:14 and then we'll just put name and now i can call the function with the
128:17 and now i can call the function with the name
128:22 and i can actually um call it with different names so we'll do bow
128:25 different names so we'll do bow and we'll do
128:26 and we'll do quincy
128:27 quincy and then if i just play that we see
128:29 and then if i just play that we see hello bo
128:30 hello bo hello quincy
128:32 hello quincy so
128:33 so as you can see we call the function by
128:36 as you can see we call the function by passing the argument
128:38 passing the argument and again you can use single quote or
128:41 and again you can use single quote or double quotes here it's better to be
128:44 double quotes here it's better to be consistent just always use single quotes
128:47 consistent just always use single quotes or always use double quotes
128:49 or always use double quotes but for teaching i like to switch it up
128:51 but for teaching i like to switch it up to just to emphasize that you can use
128:54 to just to emphasize that you can use either so let me tell you about the
128:55 either so let me tell you about the difference between parameters and
128:58 difference between parameters and arguments
128:59 arguments these two words parameters and arguments
129:01 these two words parameters and arguments are sometimes used interchangeably
129:04 are sometimes used interchangeably and it's common to get confused about
129:07 and it's common to get confused about the distinction
129:08 the distinction we call
129:10 we call parameters
129:11 parameters the values accepted by the function
129:14 the values accepted by the function inside the function definition
129:16 inside the function definition and arguments are the values we pass to
129:20 and arguments are the values we pass to the function when we call it also an
129:22 the function when we call it also an argument can have a default value
129:25 argument can have a default value that's applied if the argument is not
129:28 that's applied if the argument is not specified so let me show you how i would
129:30 specified so let me show you how i would do that so we have it name so right now
129:34 do that so we have it name so right now it always needs to be get a name well
129:36 it always needs to be get a name well first let me show you what would happen
129:38 first let me show you what would happen if i called the function without passing
129:40 if i called the function without passing the name
129:43 the name i'm just going to run that and we can
129:45 i'm just going to run that and we can see we're going to get an error hello
129:47 see we're going to get an error hello missing one required positional argument
129:50 missing one required positional argument name but we can make it so you can call
129:53 name but we can make it so you can call this function without passing in a name
129:54 this function without passing in a name where it's optional you can't if you
129:56 where it's optional you can't if you want so i'm going to put an equal sign
129:58 want so i'm going to put an equal sign and then i'm gonna type in my friend
130:02 and then i'm gonna type in my friend and just to make consistent make this
130:04 and just to make consistent make this consistent i'm gonna make this all
130:06 consistent i'm gonna make this all double quotes
130:15 okay so this is now an optional argument so it's it's you can pass in the name
130:17 so it's it's you can pass in the name but if you don't pass a name it's going
130:19 but if you don't pass a name it's going to default to my friend so i'll just run
130:21 to default to my friend so i'll just run this again with that default
130:23 this again with that default value and now it's hello bow hill quincy
130:26 value and now it's hello bow hill quincy hello my friend because we called this
130:28 hello my friend because we called this and we didn't specify any argument or
130:31 and we didn't specify any argument or parameter and then we can also accept
130:33 parameter and then we can also accept multiple parameters so i'm just going to
130:35 multiple parameters so i'm just going to get rid of this default value and i'll
130:37 get rid of this default value and i'll put 8 so now we're accepting a name and
130:40 put 8 so now we're accepting a name and an age so we can now use both parameters
130:44 an age so we can now use both parameters in our function so i can do plus
130:48 in our function so i can do plus hello
130:49 hello name
130:51 name [Music]
130:53 [Music] you are
130:57 you are and we're going to add
130:58 and we're going to add the age
131:01 the age and now it's going to be passed in as a
131:03 and now it's going to be passed in as a number but we're going to convert it to
131:05 number but we're going to convert it to a string
131:06 a string so you are
131:09 so you are [Music]
131:13 [Music] years old
131:15 years old and then we have to make sure we have to
131:17 and then we have to make sure we have to make sure to add a space in here so
131:19 make sure to add a space in here so there'll be a space space after this
131:21 there'll be a space space after this word then the number then a space and
131:24 word then the number then a space and then years old
131:26 then years old so i'm gonna now have to pass in the
131:29 so i'm gonna now have to pass in the number
131:30 number and now i can run this function
131:34 and now i can run this function now showing the red
131:36 now showing the red squiggly lines
131:39 squiggly lines i sometimes the the red squiggly lines
131:41 i sometimes the the red squiggly lines will appear when it's
131:43 will appear when it's actually correct
131:45 actually correct so let me what am i am i doing something
131:47 so let me what am i am i doing something wrong here
131:48 wrong here oh i need to put the parentheses
131:52 oh i need to put the parentheses if the red squiggly lines appears when
131:55 if the red squiggly lines appears when it's actually correct they'll it they'll
131:57 it's actually correct they'll it they'll go away
131:58 go away usually within a few seconds or if you
132:01 usually within a few seconds or if you hit enter
132:02 hit enter so that actually was a problem i forgot
132:04 so that actually was a problem i forgot the parentheses at the end um so
132:07 the parentheses at the end um so you can see this is what the whole
132:08 you can see this is what the whole function looks like
132:11 function looks like if it's all on one line but i'm just
132:13 if it's all on one line but i'm just going to
132:14 going to move that over so hello bo you are 39
132:18 move that over so hello bo you are 39 years old so we've used the name and the
132:20 years old so we've used the name and the age
132:21 age so parameters are passed by reference
132:25 so parameters are passed by reference all types in python are objects but some
132:28 all types in python are objects but some of them are immutable including integers
132:31 of them are immutable including integers booleans floats strings and tuples this
132:33 booleans floats strings and tuples this means that if you pass them as
132:35 means that if you pass them as parameters and you modify their value
132:38 parameters and you modify their value inside the function the new value is not
132:40 inside the function the new value is not reflected outside of the function
132:43 reflected outside of the function let me just give you an example of that
132:45 let me just give you an example of that so if i just i'm just going to paste in
132:47 so if i just i'm just going to paste in some new code here and we can see we
132:49 some new code here and we can see we have this function called change and
132:52 have this function called change and we're going to pass in this value if we
132:54 we're going to pass in this value if we pass in this this valve variable 1 to
132:57 pass in this this valve variable 1 to the change function and we set value to
133:00 the change function and we set value to 2
133:01 2 well then we're going to print the value
133:03 well then we're going to print the value and see what happens
133:05 and see what happens and you can see it's now just 1 here so
133:08 and you can see it's now just 1 here so so it didn't change the value
133:11 so it didn't change the value the value so what we change inside the
133:14 the value so what we change inside the function doesn't affect anything from
133:16 function doesn't affect anything from outside the function
133:18 outside the function and then you can see we have these
133:20 and then you can see we have these orange squiggly lines here local
133:22 orange squiggly lines here local variable value is assigned but never
133:24 variable value is assigned but never used it's just showing that actually
133:25 used it's just showing that actually this isn't really doing anything once
133:27 this isn't really doing anything once it's inside the function and we change
133:29 it's inside the function and we change it doesn't change anything outside the
133:31 it doesn't change anything outside the function
133:32 function so if you pass an object that's not
133:34 so if you pass an object that's not immutable you do change one of its
133:36 immutable you do change one of its properties and the change will be
133:38 properties and the change will be reflected outside so this was
133:40 reflected outside so this was mutable this is immutable an object that
133:43 mutable this is immutable an object that would be
133:44 would be mutable would be like a dictionary so if
133:47 mutable would be like a dictionary so if i change this to a dictionary and i put
133:49 i change this to a dictionary and i put name
133:54 and i set it to bow but then inside the change i do value
133:57 but then inside the change i do value dot name or not i'll put the the
134:00 dot name or not i'll put the the brackets value name
134:03 brackets value name so the key of this dictionary and i set
134:07 so the key of this dictionary and i set that to sid
134:09 that to sid and i run this we'll see now the name
134:12 and i run this we'll see now the name has changed to sin so we changed we use
134:15 has changed to sin so we changed we use the change function to change name to
134:17 the change function to change name to sid and now it actually is changed
134:19 sid and now it actually is changed because a dictionary is mutable
134:22 because a dictionary is mutable so a function can also return a value
134:26 so a function can also return a value using the return statement
134:29 using the return statement so i'm going to update this whole thing
134:30 so i'm going to update this whole thing and talk about return statements a
134:33 and talk about return statements a function can return a value using the
134:35 function can return a value using the return statement so it's going to return
134:38 return statement so it's going to return this name that we then can continue to
134:40 this name that we then can continue to use in our in our program it doesn't
134:43 use in our in our program it doesn't have to return name it can return
134:44 have to return name it can return anything that happens inside the
134:46 anything that happens inside the function
134:47 function and
134:48 and the when the function meets the return
134:51 the when the function meets the return statement the function ends so you can
134:53 statement the function ends so you can have a return statement have code after
134:55 have a return statement have code after it but it will just end like for
134:57 it but it will just end like for instance if you have the return
134:58 instance if you have the return statement in a conditional like in an if
135:00 statement in a conditional like in an if statement we can also omit
135:03 statement we can also omit the return the return value
135:06 the return the return value so it's just going to end the function
135:08 so it's just going to end the function and not return anything so i had
135:11 and not return anything so i had mentioned having the return statement in
135:13 mentioned having the return statement in a conditional so that's a common way to
135:16 a conditional so that's a common way to end a function if the starting condition
135:18 end a function if the starting condition is not met
135:19 is not met like for instance if we update the
135:21 like for instance if we update the function to
135:22 function to this so if not name return this so if i
135:27 this so if not name return this so if i mean if not name return
135:30 mean if not name return or
135:30 or else
135:31 else you don't even need an else because this
135:33 you don't even need an else because this will just in the function and you don't
135:35 will just in the function and you don't even need an else this will happen if
135:37 even need an else this will happen if there is a name now we just said that
135:40 there is a name now we just said that you have to pass in something if you
135:42 you have to pass in something if you don't have a default value so the way to
135:44 don't have a default value so the way to get to that would just be to call the
135:47 get to that would just be to call the function
135:48 function with false
135:51 with false so if we call with false then it's just
135:53 so if we call with false then it's just going to return it doesn't do anything
135:55 going to return it doesn't do anything but if we call it with
135:57 but if we call it with bo
135:58 bo then let's see
136:00 then let's see hello bo so you can also return multiple
136:04 hello bo so you can also return multiple values by using comma separated values
136:07 values by using comma separated values so
136:08 so for instance i can i'm just going to
136:11 for instance i can i'm just going to take this part off here and then add a
136:13 take this part off here and then add a return statement
136:15 return statement return and then i can return the name i
136:19 return and then i can return the name i can return
136:21 can return bo in case that's not the name i can
136:23 bo in case that's not the name i can return
136:24 return 8 and then i can call this and i'm just
136:27 8 and then i can call this and i'm just going to call this with sid
136:29 going to call this with sid and we can see what happens
136:31 and we can see what happens now
136:32 now oh
136:33 oh it doesn't it's not it doesn't actually
136:34 it doesn't it's not it doesn't actually print what's been returned but if i
136:37 print what's been returned but if i print this
136:39 print this here we go then we can really see what
136:40 here we go then we can really see what happens so
136:42 happens so in this function it's going to print
136:43 in this function it's going to print this but now we're also going to print
136:46 this but now we're also going to print what's returned so let's see what that
136:49 what's returned so let's see what that looks like so
136:50 looks like so this is what when we print what was
136:52 this is what when we print what was returned it looks like this so it's sid
136:55 returned it looks like this so it's sid bo
136:56 bo 8
136:57 8 so one thing related to functions and
136:59 so one thing related to functions and also related to other parts of python is
137:03 also related to other parts of python is variable scope
137:05 variable scope so let's look at this
137:07 so let's look at this we've declared a variable up here and
137:10 we've declared a variable up here and when you declare a variable that
137:12 when you declare a variable that variable is visible in parts of your
137:14 variable is visible in parts of your program depending on where you declare
137:16 program depending on where you declare it
137:17 it so if you declare a variable outside of
137:20 so if you declare a variable outside of a function the variable is visible to
137:22 a function the variable is visible to any code running after the declare after
137:25 any code running after the declare after the declaration including functions so
137:28 the declaration including functions so we call this a global variable so we've
137:30 we call this a global variable so we've declared this before the function so we
137:33 declared this before the function so we can now access it inside a function and
137:35 can now access it inside a function and also outside the function so if i
137:39 also outside the function so if i so we can see 8 and 8 and it shows right
137:42 so we can see 8 and 8 and it shows right here what's going to
137:43 here what's going to show in the
137:44 show in the console here
137:46 console here but if we declare a variable inside a
137:48 but if we declare a variable inside a function let me give you an example if
137:50 function let me give you an example if we declare
137:51 we declare this variable
137:53 this variable inside the function i'm just going to
137:54 inside the function i'm just going to move it down to here
137:56 move it down to here now it's a
137:58 now it's a local variable and it's only visible
138:01 local variable and it's only visible inside the function so let me just
138:03 inside the function so let me just delete with this because it's not
138:04 delete with this because it's not actually going to be
138:06 actually going to be doing that so if i run this we're going
138:08 doing that so if i run this we're going to see there's there's an error
138:12 to see there's there's an error name
138:12 name age is not defined we're trying to print
138:15 age is not defined we're trying to print the age here but since the age was
138:17 the age here but since the age was declared inside the function it's not
138:19 declared inside the function it's not available outside the function it's only
138:21 available outside the function it's only available
138:23 available inside the function
138:25 inside the function so you just have to be aware sometimes
138:27 so you just have to be aware sometimes there's local variables that only apply
138:29 there's local variables that only apply inside the function and there's global
138:31 inside the function and there's global variables if you that can apply inside a
138:33 variables if you that can apply inside a function and outside a function
138:35 function and outside a function okay now let's look at something else
138:36 okay now let's look at something else with functions and this is nested
138:39 with functions and this is nested functions
138:41 functions functions in python can be nested
138:44 functions in python can be nested inside other functions
138:46 inside other functions a function defined inside a function is
138:49 a function defined inside a function is visible only inside that function this
138:51 visible only inside that function this is useful to create utilities that are
138:53 is useful to create utilities that are useful to a function but not useful
138:55 useful to a function but not useful outside of it so you might ask why
138:57 outside of it so you might ask why should i be hiding this function if it
138:59 should i be hiding this function if it does no harm
139:00 does no harm well one because it's always best to
139:02 well one because it's always best to hide functionality that's local to a
139:04 hide functionality that's local to a function if it's not useful elsewhere
139:07 function if it's not useful elsewhere also because we can make use of closures
139:10 also because we can make use of closures which we'll talk more about later so so
139:12 which we'll talk more about later so so look at this example so we have this
139:15 look at this example so we have this function talk and inside the function we
139:18 function talk and inside the function we defined another function called say
139:22 defined another function called say and then
139:23 and then what we
139:24 what we what we do is that we can call that say
139:27 what we do is that we can call that say function
139:28 function inside the function and so the way this
139:31 inside the function and so the way this works is we pass in the phrase so here's
139:34 works is we pass in the phrase so here's the phrase and the phrase i'm going to
139:36 the phrase and the phrase i'm going to buy the milk
139:37 buy the milk and here we do we we split it so split
139:41 and here we do we we split it so split is a way to
139:43 is a way to create a list
139:45 create a list of out of this string so we have this
139:47 of out of this string so we have this string but it's going to split it on
139:49 string but it's going to split it on every space so it's going to create a
139:51 every space so it's going to create a list of each word individually and then
139:54 list of each word individually and then we're going to run this loop
139:57 we're going to run this loop more on loops later
139:59 more on loops later and we're going to for every word in the
140:02 and we're going to for every word in the the words list we're going to say
140:04 the words list we're going to say the word we're going to say the word
140:06 the word we're going to say the word it's just going to print the word so if
140:07 it's just going to print the word so if i just run that i am going to buy the
140:10 i just run that i am going to buy the milk and every time it prints it it
140:12 milk and every time it prints it it prints it on a new line so this would
140:15 prints it on a new line so this would just be an example because we're never
140:16 just be an example because we're never going to want to use this save function
140:18 going to want to use this save function outside the talk function so it's better
140:19 outside the talk function so it's better just to put it inside the talk function
140:23 just to put it inside the talk function and then i'll just paste in
140:25 and then i'll just paste in another example here if you want to
140:27 another example here if you want to access a variable defined in the outer
140:30 access a variable defined in the outer function from the inner function
140:32 function from the inner function you first need to declare it as
140:35 you first need to declare it as non-local
140:36 non-local so
140:37 so we're using non-local here at non-local
140:40 we're using non-local here at non-local count and this allows us to access this
140:44 count and this allows us to access this variable that was declared inside the
140:47 variable that was declared inside the out so this is the outer function count
140:49 out so this is the outer function count and we have this variable called count
140:52 and we have this variable called count and to be able to access that variable
140:53 and to be able to access that variable in the inner function we have to
140:56 in the inner function we have to call non-local or we already talked
140:58 call non-local or we already talked about variable scope and if we didn't
141:02 about variable scope and if we didn't call
141:03 call call this non-local then we could not
141:04 call this non-local then we could not access the count variable from inside
141:07 access the count variable from inside the function so like for instance i'm
141:09 the function so like for instance i'm going to run this it's going to print
141:11 going to run this it's going to print the count which is just count plus 1 is
141:13 the count which is just count plus 1 is just adding 1 to this number that's all
141:15 just adding 1 to this number that's all the function does but if i take off this
141:18 the function does but if i take off this word non-local here and i run this we're
141:20 word non-local here and i run this we're going to get an error because it doesn't
141:23 going to get an error because it doesn't know what count is it doesn't know that
141:25 know what count is it doesn't know that we're referring to this count in the
141:26 we're referring to this count in the inner function so we'll just put that
141:28 inner function so we'll just put that back on there and then it should work
141:31 back on there and then it should work again
141:32 again this is especially useful with closures
141:35 this is especially useful with closures which we're just about to talk about
141:37 which we're just about to talk about so closure is a special way of doing a
141:39 so closure is a special way of doing a function in python if you return a
141:41 function in python if you return a nested function from a function that
141:44 nested function from a function that nested function has access to the
141:45 nested function has access to the variables defined in that function even
141:48 variables defined in that function even if that function is not active anymore
141:51 if that function is not active anymore so let me show you an example i'm going
141:52 so let me show you an example i'm going to paste some code that'll be very
141:54 to paste some code that'll be very similar to this code but just a a little
141:56 similar to this code but just a a little different and then i'll explain it so
141:59 different and then i'll explain it so now instead of count it's a counter so
142:02 now instead of count it's a counter so we're returning count from this nested
142:04 we're returning count from this nested function and from the outer function
142:07 function and from the outer function we're returning the nested function
142:10 we're returning the nested function we're returning the increment function
142:12 we're returning the increment function and then here instead of just calling
142:14 and then here instead of just calling the function directory the outer
142:16 the function directory the outer function we're assigning it to this
142:18 function we're assigning it to this variable and now we're going to print
142:22 variable and now we're going to print we're just going to call we're going to
142:23 we're just going to call we're going to call this variable which is the returned
142:27 call this variable which is the returned inner function so we're basically
142:28 inner function so we're basically calling the inner function and it's
142:30 calling the inner function and it's still going it's because we're calling
142:32 still going it's because we're calling the inner function it's not going to
142:35 the inner function it's not going to reset the count to zero every time and
142:38 reset the count to zero every time and it can keep track of that value and we
142:40 it can keep track of that value and we have this
142:42 have this using a comment what it's going to
142:43 using a comment what it's going to return but we can also
142:45 return but we can also run the program and we can see one two
142:48 run the program and we can see one two three just like that so we return the
142:51 three just like that so we return the increment inner function and that still
142:54 increment inner function and that still has access to the state of the count
142:55 has access to the state of the count variable even though the counter
142:57 variable even though the counter function has ended
142:59 function has ended so let's move on to
143:02 so let's move on to objects
143:04 objects [Music]
143:06 [Music] everything in python is an object
143:09 everything in python is an object even values of basic prim of types like
143:12 even values of basic prim of types like integers strings floats are objects
143:14 integers strings floats are objects lists are objects as well as tuples
143:16 lists are objects as well as tuples dictionaries and pretty much everything
143:19 dictionaries and pretty much everything so objects have attributes and methods
143:22 so objects have attributes and methods that can be accessed using the dot
143:24 that can be accessed using the dot syntax for example let's define a new
143:27 syntax for example let's define a new variable of type int so i'm going to do
143:29 variable of type int so i'm going to do age equals eight
143:32 age equals eight age now has access to the properties and
143:34 age now has access to the properties and methods defined for all int objects
143:38 methods defined for all int objects this includes for example access to the
143:40 this includes for example access to the real and imaginary part of that number
143:42 real and imaginary part of that number so i can do
143:44 so i can do a print
143:46 a print age dot real
143:48 age dot real and then if i just run that
143:50 and then if i just run that the real part is eight i can also print
143:53 the real part is eight i can also print the imaginary
143:55 the imaginary part of the number
143:57 part of the number and manage
143:58 and manage and there is no imaginary part of the
144:01 and there is no imaginary part of the number so it just
144:02 number so it just does zero i can also get the the bit
144:06 does zero i can also get the the bit length age dot
144:08 length age dot bit
144:09 bit length
144:15 and if i run that we can see the bit length is four so the
144:18 we can see the bit length is four so the bit length method returns the number of
144:20 bit length method returns the number of bits necessary to represent this number
144:23 bits necessary to represent this number in binary notation
144:25 in binary notation so there's just a lot of
144:27 so there's just a lot of things that you can use for all int
144:29 things that you can use for all int objects and these are just a few of them
144:32 objects and these are just a few of them so a variable holding a list value has
144:35 so a variable holding a list value has access to a different set of methods so
144:38 access to a different set of methods so i'm going to
144:40 i'm going to update this again we're going to do
144:41 update this again we're going to do items
144:43 items equals and we're going to create a list
144:45 equals and we're going to create a list one
144:46 one two
144:47 two so
144:48 so i can do
144:49 i can do items dot
144:51 items dot append
144:52 append i can append a three i can append
144:54 i can append a three i can append another item i can do
144:56 another item i can do items dot pop
144:58 items dot pop which is going to
145:00 which is going to remove and return the last item which is
145:03 remove and return the last item which is the three
145:04 the three and the methods so so these are the
145:07 and the methods so so these are the methods of pin and pop and the methods
145:09 methods of pin and pop and the methods available to an object depend on the
145:12 available to an object depend on the type of value
145:14 type of value the id global function provided by
145:17 the id global function provided by python lets you inspect the location in
145:19 python lets you inspect the location in memory for a particular object so for
145:22 memory for a particular object so for instance i could do a print and i'm
145:25 instance i could do a print and i'm going to do id what's the id of the
145:28 going to do id what's the id of the items
145:29 items object
145:31 object and we can see this is the location in
145:33 and we can see this is the location in memory
145:35 memory so some val some objects are mutable
145:37 so some val some objects are mutable while others are immutable this is
145:39 while others are immutable this is something that we already talked about a
145:41 something that we already talked about a little bit that depends on the object
145:43 little bit that depends on the object itself if the object provides methods to
145:46 itself if the object provides methods to change its content then it's mutable
145:48 change its content then it's mutable otherwise it's immutable
145:50 otherwise it's immutable most most types defined by python are
145:52 most most types defined by python are immutable for example an int is
145:54 immutable for example an int is immutable there are no methods to change
145:56 immutable there are no methods to change its value so if you increment the value
145:59 its value so if you increment the value like with um
146:01 like with um age equals age
146:04 age equals age plus
146:05 plus one
146:06 one it's actually going to create an
146:08 it's actually going to create an entirely
146:09 entirely new value
146:11 new value so it it's not going to even be the same
146:13 so it it's not going to even be the same object at all because age you it has to
146:16 object at all because age you it has to create a whole new one to reassign it
146:18 create a whole new one to reassign it but something like a in in a dictionary
146:22 but something like a in in a dictionary it would actually be the same object but
146:24 it would actually be the same object but you could just change different parts of
146:25 you could just change different parts of it now let's talk more about
146:28 it now let's talk more about loops
146:30 loops so this song we already discussed a
146:31 so this song we already discussed a little bit in the previous section but
146:34 little bit in the previous section but loops are
146:35 loops are one essential part of programming
146:37 one essential part of programming and in python we have two kinds of loops
146:41 and in python we have two kinds of loops while loops and for loops
146:44 while loops and for loops so before i um show i'm going to paste
146:47 so before i um show i'm going to paste in this code but i just want to show
146:49 in this code but i just want to show something really quick see how there's a
146:52 something really quick see how there's a line like dotted line here and a dotted
146:54 line like dotted line here and a dotted line here this is showing the default
146:56 line here this is showing the default indentation which we can change so i'm
146:59 indentation which we can change so i'm actually going to go and change
147:01 actually going to go and change that really quick let me wait i think
147:03 that really quick let me wait i think i'm gonna have to yeah i'm gonna zoom
147:04 i'm gonna have to yeah i'm gonna zoom out so i can get to this and i'm gonna
147:06 out so i can get to this and i'm gonna change the indent to four and now it's
147:10 change the indent to four and now it's not going to have a little line right in
147:11 not going to have a little line right in there so now i'm going to go back here
147:14 there so now i'm going to go back here and let's zoom in again
147:21 okay so let's talk about while loops
147:22 while loops while loops are defined using the
147:25 while loops are defined using the while
147:26 while keyword
147:27 keyword and they repeat their block until the
147:30 and they repeat their block until the the condition is evaluated as false so
147:33 the condition is evaluated as false so while condition equals true so
147:36 while condition equals true so this particular example is an infinite
147:40 this particular example is an infinite loop it never ends because this
147:43 loop it never ends because this condition is always going to so if we
147:45 condition is always going to so if we run this program which i'm not going to
147:47 run this program which i'm not going to do right now because it just goes on
147:49 do right now because it just goes on forever while this condition is true
147:52 forever while this condition is true keep running the code inside the loop
147:55 keep running the code inside the loop all the lines of code that are indented
147:57 all the lines of code that are indented the same amount
147:58 the same amount so let's halt the loop right after the
148:00 so let's halt the loop right after the first iteration i can do condition
148:05 first iteration i can do condition equals
148:06 equals false
148:12 [Music] so now if i run it it just runs the loop
148:16 so now if i run it it just runs the loop one time
148:17 one time so in this case the first iteration is
148:19 so in this case the first iteration is run as the condition is evaluated true
148:22 run as the condition is evaluated true and then at the second iteration the
148:24 and then at the second iteration the condition test evaluates to false so the
148:27 condition test evaluates to false so the control goes to the next instruction
148:30 control goes to the next instruction after the loop which in this case there
148:32 after the loop which in this case there is no next instruction after the loop
148:36 is no next instruction after the loop it's common to have a counter to stop
148:37 it's common to have a counter to stop the iteration after some number of
148:40 the iteration after some number of cycles
148:46 so here's a while loop with a counter so you you start the counter at zero and
148:48 you you start the counter at zero and then while count is less than 10.
148:52 then while count is less than 10. we're we're gonna print this count
148:54 we're we're gonna print this count equals count plus one so it's going to
148:56 equals count plus one so it's going to increment the counter every time
148:58 increment the counter every time until we get to the end so it's gonna
149:01 until we get to the end so it's gonna see it's gonna print this until
149:03 see it's gonna print this until eventually the count is
149:09 greater than 10 so or 10 or 10 or greater while count is
149:12 so or 10 or 10 or greater while count is less than 10. so once it gets to 10 the
149:14 less than 10. so once it gets to 10 the loop will stop um again another way to
149:18 loop will stop um again another way to doing to do this we could have just done
149:20 doing to do this we could have just done plus equals plus equals one so if i run
149:24 plus equals plus equals one so if i run that it's going to do the exact same
149:25 that it's going to do the exact same thing
149:27 thing and other type is the for loop so using
149:30 and other type is the for loop so using for loops we can tell python to execute
149:32 for loops we can tell python to execute a block for a predetermined amount of
149:35 a block for a predetermined amount of times up front and without the need of a
149:37 times up front and without the need of a separate variable and conditional to
149:39 separate variable and conditional to check its value
149:41 check its value it's commonly used to iterate the items
149:44 it's commonly used to iterate the items in a list so we have this list there's
149:47 in a list so we have this list there's obviously four items here and then four
149:49 obviously four items here and then four item in items so items is this list and
149:51 item in items so items is this list and then for each item in the list we're
149:53 then for each item in the list we're going to print
149:54 going to print the item pretty straightforward and it
149:56 the item pretty straightforward and it prints each item in the list
149:59 prints each item in the list or you can iterate a specific amount of
150:01 or you can iterate a specific amount of times using the range function so let's
150:04 times using the range function so let's say we don't have we're not going to
150:06 say we don't have we're not going to define this here we're just going to do
150:08 define this here we're just going to do 4 item in and then here i'm going to
150:11 4 item in and then here i'm going to type in range
150:14 type in range and then i'm just going to type in a
150:15 and then i'm just going to type in a number how about
150:17 number how about 15
150:18 15 so i'm using the range function that
150:21 so i'm using the range function that basically just returns a list and then
150:23 basically just returns a list and then if i do that we can see it's going from
150:26 if i do that we can see it's going from 0 to 14. so the range function is going
150:29 0 to 14. so the range function is going to return a
150:31 to return a a list that goes from 0 to 14 so there's
150:33 a list that goes from 0 to 14 so there's 15 items and it's going to print the
150:35 15 items and it's going to print the items
150:36 items now if we just go back a few steps to
150:39 now if we just go back a few steps to when we had the list here
150:41 when we had the list here we can i can show you how to get the
150:43 we can i can show you how to get the index so right now it's just printing
150:47 index so right now it's just printing the items one two three four but what if
150:49 the items one two three four but what if we want the index of the list
150:52 we want the index of the list we can do that by using the by wrapping
150:55 we can do that by using the by wrapping the sequence in the enumerate function
150:58 the sequence in the enumerate function so for items in and then
151:02 so for items in and then i'm going to do
151:03 i'm going to do enumerate
151:13 so this is going to return each item and
151:15 each item and the index of the item and since there's
151:17 the index of the item and since there's going to be an item in an index
151:19 going to be an item in an index but it's actually the index and then the
151:20 but it's actually the index and then the item i'm going to type an index comma
151:24 item i'm going to type an index comma item so this enumerate is going to get
151:26 item so this enumerate is going to get the index and the item so now i can
151:29 the index and the item so now i can print the index and the item here and if
151:33 print the index and the item here and if i run that
151:35 i run that so index zero item one index one item
151:38 so index zero item one index one item two index two item three and so on and
151:41 two index two item three and so on and it doesn't even have to be numbers we
151:43 it doesn't even have to be numbers we can
151:48 do names [Music]
151:55 and if i just run that whoops not that one
151:57 one and then we can see the the index and
152:00 and then we can see the the index and the item then let me put in some more
152:02 the item then let me put in some more code here so i can talk about break and
152:05 code here so i can talk about break and continue
152:07 continue both while and for loops can be
152:09 both while and for loops can be interrupted inside the block using
152:11 interrupted inside the block using either break or continue
152:13 either break or continue continue stops the current iteration and
152:15 continue stops the current iteration and tells python to execute the next one
152:18 tells python to execute the next one and break stops the loop altogether and
152:21 and break stops the loop altogether and goes on with the next instruction after
152:23 goes on with the next instruction after the loop ends so i'm going to just play
152:26 the loop ends so i'm going to just play this so here we're saying if item equals
152:28 this so here we're saying if item equals two continues that means it's going to
152:30 two continues that means it's going to skip that iteration so if i play this
152:32 skip that iteration so if i play this one three four so it's not going to it's
152:35 one three four so it's not going to it's not going to get to the print item
152:37 not going to get to the print item because it's actually skipped that
152:38 because it's actually skipped that iteration it just
152:40 iteration it just doesn't run any code after the continue
152:43 doesn't run any code after the continue if this is true
152:45 if this is true and so it doesn't print 2. so if we
152:47 and so it doesn't print 2. so if we change this to break it will be very
152:49 change this to break it will be very it'll be a little different here this
152:52 it'll be a little different here this time it's going to just print one
152:55 time it's going to just print one because now it's breaking out of the
152:56 because now it's breaking out of the loop
152:57 loop entirely and it's not going to run any
152:59 entirely and it's not going to run any more iteration of the loop
153:02 more iteration of the loop okay let's talk about another thing
153:04 okay let's talk about another thing classes classes in python
153:08 classes classes in python so in addition to using the python
153:11 so in addition to using the python provided types we can declare our own
153:13 provided types we can declare our own classes
153:14 classes and from the classes we can instantiate
153:17 and from the classes we can instantiate objects
153:19 objects an object is an instance of a class
153:22 an object is an instance of a class a class is the type of an object so
153:25 a class is the type of an object so here's an example i'm going to create a
153:27 here's an example i'm going to create a class called
153:30 class called dog
153:32 dog so
153:33 so uh to create a class you just put the
153:35 uh to create a class you just put the word class and then put the the class
153:37 word class and then put the the class name
153:38 name and
153:39 and now
153:40 now i can i can
153:42 i can i can add a method for the class so to define
153:45 add a method for the class so to define a method i'll just do define bark
153:48 a method i'll just do define bark [Music]
153:50 [Music] and i'm going to put the word self here
153:53 and i'm going to put the word self here and inside this i'll print
154:01 [Music] woof
154:02 woof [Music]
154:08 okay so self as an argument of the method
154:10 method will point to the current object
154:12 will point to the current object instance and must be specified when
154:14 instance and must be specified when defining a method so when you're
154:17 defining a method so when you're creating a method inside a class you're
154:18 creating a method inside a class you're always going to start with self so we
154:21 always going to start with self so we create an instance of a class
154:24 create an instance of a class which is an object
154:25 which is an object like this
154:26 like this so i'm just going to put
154:28 so i'm just going to put roger
154:30 roger equals
154:32 equals dog
154:33 dog okay so i've created a dog just like
154:36 okay so i've created a dog just like this
154:37 this and then i can
154:40 and then i can print
154:42 print type
154:43 type roger so let's see what the type of this
154:46 roger so let's see what the type of this roger is we can see it's the class to
154:49 roger is we can see it's the class to dog class
154:51 dog class roger is a dog
154:54 roger is a dog a special type of method
154:57 a special type of method called in there's a special type of
154:59 called in there's a special type of method called init which is a
155:01 method called init which is a constructor
155:03 constructor so let me show you how to create a
155:05 so let me show you how to create a constructor
155:07 constructor deaf
155:16 so we can use this a constructor like this
155:18 a constructor like this to initialize one more properties when
155:20 to initialize one more properties when we create a new object from that class
155:23 we create a new object from that class so you can see we always have to add
155:25 so you can see we always have to add self but now these are the two
155:28 self but now these are the two variables we can pass in when we create
155:31 variables we can pass in when we create a dog and that will associate be
155:34 a dog and that will associate be associated with that
155:36 associated with that that
155:37 that object
155:38 object so down here i can call um i can call
155:42 so down here i can call um i can call dog but i can pass in roger for the name
155:46 dog but i can pass in roger for the name and
155:47 and the age
155:48 the age and now when we create this
155:51 and now when we create this dog it's going to assign the name to
155:54 dog it's going to assign the name to self.name and it's going to assign the
155:56 self.name and it's going to assign the age to self.age and let me show you how
155:59 age to self.age and let me show you how you can access that information
156:02 you can access that information so i'm going to print instead of
156:04 so i'm going to print instead of printing the type i'm going to do roger
156:07 printing the type i'm going to do roger dot name
156:09 dot name and now it's going to when i do
156:11 and now it's going to when i do roger.name that's self.name so self is
156:14 roger.name that's self.name so self is roger and we do self.name it's going to
156:17 roger and we do self.name it's going to be the name that was passed in
156:19 be the name that was passed in and then we can also
156:21 and then we can also do the age
156:23 do the age and then we can finally
156:25 and then we can finally call the bark method so we have bark
156:28 call the bark method so we have bark here now we can see what that does so
156:30 here now we can see what that does so i'm just going to run that
156:32 i'm just going to run that and we have roger
156:35 and we have roger we have eight
156:36 we have eight and then
156:38 and then this is because i should have put
156:40 this is because i should have put parentheses here so let me put
156:42 parentheses here so let me put parentheses after bark
156:44 parentheses after bark and so we have wolf here so roger 8
156:48 and so we have wolf here so roger 8 wolf and the reason why it says none
156:51 wolf and the reason why it says none here is because i didn't have to put the
156:54 here is because i didn't have to put the print see i i put pranks that was in
156:56 print see i i put pranks that was in this groove if you're playing print on
156:58 this groove if you're playing print on everything but calling bark
157:00 everything but calling bark already prints wolf
157:02 already prints wolf so when i do
157:04 so when i do it when it's when it's printing it's
157:06 it when it's when it's printing it's printing because since
157:07 printing because since roger.bark doesn't return anything
157:10 roger.bark doesn't return anything there's no return statement that's why
157:12 there's no return statement that's why it printed none so there'd be two ways
157:15 it printed none so there'd be two ways to fix that either instead of printing
157:17 to fix that either instead of printing wolf i could return wolf
157:20 wolf i could return wolf or i could just not do the print here so
157:23 or i could just not do the print here so let me just take that off
157:25 let me just take that off okay roger 8
157:27 okay roger 8 wolf
157:28 wolf so one important feature of class is
157:30 so one important feature of class is inheritance let me show you an example
157:33 inheritance let me show you an example of inheritance i'm going to create a new
157:36 of inheritance i'm going to create a new class before the dog class and this is
157:39 class before the dog class and this is going to be
157:40 going to be a class
157:42 a class called
157:43 called animal
157:45 animal and the animal class i'm going to put a
157:47 and the animal class i'm going to put a function called walk
157:50 function called walk and i'm going to always pass in self
157:53 and i'm going to always pass in self and this is going to print
157:55 and this is going to print [Music]
157:56 [Music] walking
157:58 walking [Music]
158:00 [Music] and then we can make the dog class
158:03 and then we can make the dog class inherent inherit from the animal class
158:06 inherent inherit from the animal class so we have class dog but if i put
158:08 so we have class dog but if i put parentheses here
158:09 parentheses here then i can type in animal
158:11 then i can type in animal and now the dog class is going to
158:14 and now the dog class is going to inherit from the animal class and now i
158:17 inherit from the animal class and now i can go down here and after roger.bark i
158:20 can go down here and after roger.bark i can do roger.walk
158:23 can do roger.walk and if i run that
158:24 and if i run that okay so roger 8wolf but now it's going
158:27 okay so roger 8wolf but now it's going to be able to do walking and you can see
158:29 to be able to do walking and you can see the dog class doesn't actually have a
158:32 the dog class doesn't actually have a walk method but it's getting it from
158:35 walk method but it's getting it from the animal class it's inheriting this
158:37 the animal class it's inheriting this method
158:38 method and in that way you're able to i could
158:42 and in that way you're able to i could create a class cat a class frog
158:46 create a class cat a class frog a class bird and each of them could
158:49 a class bird and each of them could inherit the walk
158:51 inherit the walk method and then it would have
158:53 method and then it would have walking
158:55 walking and we'll be doing a little more with
158:56 and we'll be doing a little more with classes in the the final project
158:59 classes in the the final project in this course
159:01 in this course because we'll be going a little more
159:03 because we'll be going a little more over object oriented programming
159:06 over object oriented programming but right now let's talk about something
159:09 but right now let's talk about something new i'm going to just delete all this
159:11 new i'm going to just delete all this and we're going to be talking about
159:13 and we're going to be talking about modules
159:14 modules so every python file is a module you can
159:18 so every python file is a module you can import a module from other files and
159:20 import a module from other files and that's the base of any program of
159:21 that's the base of any program of moderate complexity as it promotes a
159:23 moderate complexity as it promotes a sensible organization and code reuse so
159:26 sensible organization and code reuse so it's basically how you can create a
159:28 it's basically how you can create a software that has multiple python
159:30 software that has multiple python programs in the same piece of software
159:33 programs in the same piece of software so in the typical python program one
159:35 so in the typical python program one file acts as the entry point and the
159:38 file acts as the entry point and the other files are modules and exposed
159:40 other files are modules and exposed functions that we can call from
159:42 functions that we can call from other files so
159:44 other files so let me just show you an example i am
159:47 let me just show you an example i am going to
159:48 going to open up this
159:50 open up this files tab and i'm going to create a new
159:52 files tab and i'm going to create a new file and this is going to be called dog
159:54 file and this is going to be called dog dot pi
159:55 dot pi and now i have dog dot pi open i no
159:57 and now i have dog dot pi open i no longer have the main dot pi open and i'm
160:00 longer have the main dot pi open and i'm going to define
160:02 going to define bark
160:03 bark and what bark is going to do is just
160:07 and what bark is going to do is just print
160:09 print woof
160:10 woof [Music]
160:11 [Music] okay now i'm going to it's just going to
160:14 okay now i'm going to it's just going to automatically save for me i'm going to
160:15 automatically save for me i'm going to go back to the the python file and now
160:18 go back to the the python file and now i'm going to
160:20 i'm going to import
160:22 import dog
160:24 dog and let's see oh it's just saying it's
160:26 and let's see oh it's just saying it's unused i thought maybe this thing wrong
160:28 unused i thought maybe this thing wrong but that just means i import dog and i
160:29 but that just means i import dog and i haven't used it which i'm about to do
160:30 haven't used it which i'm about to do right now so dog
160:32 right now so dog dot
160:33 dot bark
160:35 bark so now if i run this program
160:37 so now if i run this program it's going to say wolf but that's not
160:40 it's going to say wolf but that's not from this file it's actually importing
160:44 from this file it's actually importing this function from from the dog file so
160:46 this function from from the dog file so that's a way you can
160:49 that's a way you can break up your code into multiple files
160:52 break up your code into multiple files we can also use the from import syntax
160:56 we can also use the from import syntax and call the function directly
160:58 and call the function directly let me show you what i mean so instead
161:00 let me show you what i mean so instead of import dog i'm going to say
161:04 of import dog i'm going to say from dog
161:06 from dog import
161:07 import bark
161:09 bark and then instead of calling it dog.bark
161:12 and then instead of calling it dog.bark i can just call
161:13 i can just call bark
161:14 bark because we're only importing bark well
161:17 because we're only importing bark well we've imported bark directly instead of
161:18 we've imported bark directly instead of the whole dog so i can run that and it
161:21 the whole dog so i can run that and it says
161:22 says wolf
161:23 wolf so
161:24 so the first strategy allows us to load
161:26 the first strategy allows us to load everything defined in a file when i just
161:28 everything defined in a file when i just said
161:29 said import dog that
161:31 import dog that allows everything defined in a file so i
161:34 allows everything defined in a file so i could have a bunch of function like bark
161:36 could have a bunch of function like bark or walk
161:39 or walk name or there could be a bunch of
161:40 name or there could be a bunch of functions if i just say import dog it
161:43 functions if i just say import dog it imports all of them but the second
161:45 imports all of them but the second strategy from dog import bark allows us
161:48 strategy from dog import bark allows us to just pick the things we need so we're
161:50 to just pick the things we need so we're only going to import the specific
161:52 only going to import the specific functions that we need
161:54 functions that we need those modules are specific to your
161:56 those modules are specific to your program and importing depends on the
161:58 program and importing depends on the location of the file in the file system
162:02 location of the file in the file system so
162:03 so suppose you put dog.pi in a
162:06 suppose you put dog.pi in a subfolder for instance let's say i
162:09 subfolder for instance let's say i create a folder
162:11 create a folder and i call it liv for library and let's
162:14 and i call it liv for library and let's say i put dog.pi in this subfolder like
162:18 say i put dog.pi in this subfolder like this
162:19 this now in this folder to make this work i'm
162:22 now in this folder to make this work i'm going to have to create an empty file
162:24 going to have to create an empty file named init.pie so i'm going to add file
162:28 named init.pie so i'm going to add file and i'll do init dot or under underscore
162:31 and i'll do init dot or under underscore underscore init underscore underscore
162:33 underscore init underscore underscore dot pi
162:34 dot pi and this tells python that
162:37 and this tells python that the folder contains modules
162:40 the folder contains modules now i'm going to go back to my main file
162:43 now i'm going to go back to my main file and i can
162:45 and i can i can import dog from
162:47 i can import dog from lib so i'm going to say from
162:50 lib so i'm going to say from lib
162:51 lib that's that subfolder import
162:54 that's that subfolder import dog
162:56 dog and then i can do
162:57 and then i can do dog
162:59 dog dot bark so let's run that to make sure
163:01 dot bark so let's run that to make sure there's no errors it worked
163:03 there's no errors it worked correctly so i was able to import this
163:06 correctly so i was able to import this file from the subfolder
163:09 file from the subfolder or you can reference the dog module
163:12 or you can reference the dog module specific function by importing from
163:14 specific function by importing from lib.dog so i can do from
163:17 lib.dog so i can do from lib.dog
163:18 lib.dog import
163:20 import bark and now instead of calling dog.bark
163:23 bark and now instead of calling dog.bark i can just call
163:25 i can just call bark
163:26 bark and it says
163:27 and it says wolf
163:28 wolf so i'm going to close this here
163:30 so i'm going to close this here and
163:31 and now let's talk about the python standard
163:34 now let's talk about the python standard library so basically there's all these
163:38 library so basically there's all these pre-built modules you can
163:41 pre-built modules you can you can load a lot of code from the
163:43 you can load a lot of code from the standard library python exposes a lot of
163:46 standard library python exposes a lot of built-in functionality through its
163:47 built-in functionality through its standard library
163:48 standard library the stand library is a huge collection
163:50 the stand library is a huge collection of all sorts of utilities ranging from
163:52 of all sorts of utilities ranging from math utilities to debugging to creating
163:55 math utilities to debugging to creating graphical user interfaces
163:57 graphical user interfaces so there's a bunch of them but here's
163:59 so there's a bunch of them but here's some of the more common ones we have
164:01 some of the more common ones we have math for math utilities re regular
164:03 math for math utilities re regular expressions json to work with json date
164:06 expressions json to work with json date time sqlite 3 os for operating system
164:09 time sqlite 3 os for operating system utilities random for random number
164:11 utilities random for random number generations
164:13 generations so
164:14 so statistics requests for http request
164:17 statistics requests for http request http to create servers url lib to manage
164:20 http to create servers url lib to manage urls so you can import these modules
164:24 urls so you can import these modules that allow you to get extra
164:26 that allow you to get extra functionality so
164:29 functionality so we already looked at a little bit at the
164:31 we already looked at a little bit at the math one we already looked a little bit
164:33 math one we already looked a little bit at random in the the first
164:35 at random in the the first uh project that we did but let's just
164:38 uh project that we did but let's just kind of look at a little more how you
164:40 kind of look at a little more how you would do this so now we are going to use
164:42 would do this so now we are going to use the the math one we're going to import
164:45 the the math one we're going to import math
164:47 math and
164:48 and so this is how you would introduce you
164:50 so this is how you would introduce you would use a module of the standard
164:52 would use a module of the standard library so we already saw how to import
164:55 library so we already saw how to import modules that we created it's very stan
164:57 modules that we created it's very stan it's very similar with the standard
164:59 it's very similar with the standard library so now that i've imported math i
165:02 library so now that i've imported math i can now use
165:04 can now use functions and methods from from the math
165:06 functions and methods from from the math module so i can do math dot
165:09 module so i can do math dot square root and i can pass in 4 and then
165:12 square root and i can pass in 4 and then i can just
165:13 i can just print that so we can see what the result
165:15 print that so we can see what the result is
165:20 okay 2.0 or we can
165:23 or we can just like we shall
165:24 just like we shall we saw before instead of importing math
165:27 we saw before instead of importing math i can say
165:29 i can say from math
165:32 from math import
165:33 import square root and then instead of just
165:35 square root and then instead of just doing math dot square root i can just
165:37 doing math dot square root i can just call
165:38 call this
165:39 this square root method here and it's going
165:41 square root method here and it's going to do the same thing so that's basically
165:44 to do the same thing so that's basically how it works for for all the modules in
165:46 how it works for for all the modules in the standard library okay now we're
165:48 the standard library okay now we're going to start going over a few kind of
165:50 going to start going over a few kind of miscellaneous
165:51 miscellaneous slightly more advanced topics in python
165:54 slightly more advanced topics in python so we're going to talk about how to
165:57 so we're going to talk about how to accept arguments from the command line
165:59 accept arguments from the command line in python
166:01 in python well first of all let's see how to run a
166:03 well first of all let's see how to run a program from the command line
166:05 program from the command line in replit so let's say we have a program
166:08 in replit so let's say we have a program it just says print
166:11 it just says print hello okay so we've been running it by
166:14 hello okay so we've been running it by just clicking this play button but
166:17 just clicking this play button but there's another way to run a program in
166:19 there's another way to run a program in replit and i go over to the shell so
166:21 replit and i go over to the shell so this is the command line in replica we
166:25 this is the command line in replica we can clear this
166:26 can clear this and now i'm just going to type in
166:29 and now i'm just going to type in python main dot pi
166:32 python main dot pi okay so what we call python to run the
166:34 okay so what we call python to run the python program and then we just put the
166:37 python program and then we just put the name of our file with main.pi
166:40 name of our file with main.pi so
166:41 so whether you're in replit or if you're
166:43 whether you're in replit or if you're running things locally
166:45 running things locally you should be able to
166:46 you should be able to run a program in the same way
166:48 run a program in the same way depending on how you install the program
166:50 depending on how you install the program locally instead of typing python you may
166:53 locally instead of typing python you may type in python 3.
166:56 type in python 3. sometimes the way people install python
166:59 sometimes the way people install python it will be python 3 because we're using
167:01 it will be python 3 because we're using version 3 of python
167:03 version 3 of python so now let's see how you can
167:06 so now let's see how you can call a python a program on the command
167:09 call a python a program on the command line and pass in some arguments right
167:12 line and pass in some arguments right when we run the program from the command
167:15 when we run the program from the command line
167:17 line so
167:18 so a basic way to handle arguments is to
167:21 a basic way to handle arguments is to use the sys module from the standard
167:24 use the sys module from the standard library so let me give you an example so
167:27 library so let me give you an example so first of all we're going to import sys
167:30 first of all we're going to import sys now just so you know usually you're
167:33 now just so you know usually you're always going to have import statements
167:34 always going to have import statements on the first line i'm just putting this
167:36 on the first line i'm just putting this comment on the first line to remind us
167:38 comment on the first line to remind us what we're working on right now so now
167:41 what we're working on right now so now i'm going to we're going to import the
167:43 i'm going to we're going to import the sys library
167:45 sys library now i'm going to
167:47 now i'm going to print
167:48 print and i'm going to first i'm going to
167:52 and i'm going to first i'm going to oh
167:54 oh we're going to print the argument
167:55 we're going to print the argument cis.arg
167:56 cis.arg the
167:58 the so this is how we can print all the
168:00 so this is how we can print all the arguments that were passed in
168:03 arguments that were passed in when we called the program so so i'm
168:05 when we called the program so so i'm going to see we have python main.pi and
168:08 going to see we have python main.pi and now i'm going to put
168:10 now i'm going to put bow
168:12 bow 39
168:13 39 okay so you can see it's printing the
168:16 okay so you can see it's printing the list of arguments so this is basically
168:19 list of arguments so this is basically just a list the first item is the name
168:21 just a list the first item is the name of file
168:22 of file then we have
168:24 then we have the the first word and then the second
168:26 the the first word and then the second one and you can see they're both strings
168:28 one and you can see they're both strings even though this is a number it's coming
168:31 even though this is a number it's coming in
168:31 in as a string so then we could do
168:34 as a string so then we could do something like this we could say name
168:37 something like this we could say name equals cis.arg v
168:41 equals cis.arg v and then i would
168:44 and then i would get the element at index 1 which is the
168:47 get the element at index 1 which is the name here and i could print
168:53 [Music] hello
168:55 hello and then we're going to do a name oh
168:58 and then we're going to do a name oh hello hello and then name so let's call
169:01 hello hello and then name so let's call this again
169:02 this again and instead of i'm not going to do 39
169:04 and instead of i'm not going to do 39 it's just going to be python main.pi bow
169:08 it's just going to be python main.pi bow hello bo so we've now been able to use
169:11 hello bo so we've now been able to use the argument that was passed in
169:14 the argument that was passed in now this is a simple way to do it but
169:16 now this is a simple way to do it but you
169:17 you really would have to do a lot of work
169:19 really would have to do a lot of work using this method because you really
169:21 using this method because you really should validate the arguments make sure
169:22 should validate the arguments make sure the type is correct and you need to
169:24 the type is correct and you need to print feedback to the user if they're
169:26 print feedback to the user if they're not using the program correctly
169:29 not using the program correctly so i got zoomed out a little bit and i'm
169:30 so i got zoomed out a little bit and i'm going to show you this other method
169:33 going to show you this other method so python provides another package in
169:35 so python provides another package in the standard library to help you called
169:36 the standard library to help you called arg parse so first you would insert
169:39 arg parse so first you would insert import arg parse
169:41 import arg parse [Music]
169:43 [Music] and then let me show you
169:45 and then let me show you how you would use it
169:47 how you would use it so you call arc parse dot argument
169:50 so you call arc parse dot argument parser
169:51 parser and then pass in the description of the
169:54 and then pass in the description of the program so the description of the
169:56 program so the description of the program is this program prints the name
169:58 program is this program prints the name of my dogs
169:59 of my dogs then
170:00 then you proceed to add arguments you want to
170:03 you proceed to add arguments you want to accept
170:05 accept so for this example program we are going
170:07 so for this example program we are going to accept the c option or it can be
170:09 to accept the c option or it can be slash
170:11 slash c or dash that's color
170:13 c or dash that's color and we are going to
170:16 and we are going to be calling it color and then later we
170:18 be calling it color and then later we can we do
170:20 can we do parser.parse args
170:22 parser.parse args and then we can access args
170:25 and then we can access args dot color to get the color that was
170:28 dot color to get the color that was passed in and then you can specify
170:30 passed in and then you can specify whether it's required and what help is
170:33 whether it's required and what help is going to go along with that so let me
170:35 going to go along with that so let me show you how you would do that
170:37 show you how you would do that we're going to do python
170:39 we're going to do python main dot pi i'm going to put dash c and
170:42 main dot pi i'm going to put dash c and then i'm just going to put red
170:44 then i'm just going to put red okay so
170:46 okay so you can see if i
170:48 you can see if i go this out a little more you can see
170:49 go this out a little more you can see this is the
170:51 this is the command i called this is the command i
170:53 command i called this is the command i run i pass in red and then it just
170:55 run i pass in red and then it just printed red that's what we have right
170:57 printed red that's what we have right here
170:58 here and so let me show you what would happen
171:00 and so let me show you what would happen if we if we
171:02 if we if we don't specify the argument so if i just
171:06 don't specify the argument so if i just run it without the red so it's now
171:09 run it without the red so it's now giving me
171:10 giving me some information usage well main.pi we
171:14 some information usage well main.pi we need to put dash c and then we have to
171:16 need to put dash c and then we have to put a color and then it says the
171:19 put a color and then it says the following
171:20 following arguments are required this dashi or
171:23 arguments are required this dashi or dash sc so it's it's showing us that we
171:28 dash sc so it's it's showing us that we need to
171:29 need to if we we've called the program wrong and
171:31 if we we've called the program wrong and we're going to need to call it with the
171:33 we're going to need to call it with the dash c
171:34 dash c you can also
171:36 you can also set this option
171:38 set this option we can set an option to have a specific
171:40 we can set an option to have a specific set of values using choices so after
171:44 set of values using choices so after required true after this comma i'm going
171:46 required true after this comma i'm going to type in choices and i'm going to set
171:49 to type in choices and i'm going to set this to equal
171:53 see i have this empty dictionary
171:55 dictionary but i'm just going to well not a
171:57 but i'm just going to well not a dictionary but
171:58 dictionary but because it's not going to key value
171:59 because it's not going to key value pairs i can do red
172:01 pairs i can do red and yellow
172:02 and yellow so now it's it can only accept
172:06 so now it's it can only accept two options so i can
172:09 two options so i can call it here
172:11 call it here with
172:13 with red
172:15 red but if i call with blue
172:17 but if i call with blue it will say
172:19 it will say invalid choice blue i need to choose
172:21 invalid choice blue i need to choose from red or yellow so
172:24 from red or yellow so using this arc parse makes it easier to
172:26 using this arc parse makes it easier to deal with arguments and also makes it
172:29 deal with arguments and also makes it easier to
172:30 easier to communicate information back to the user
172:33 communicate information back to the user about what we're trying to get
172:36 about what we're trying to get so there are more options with this but
172:37 so there are more options with this but those are those are the basics
172:40 those are those are the basics now let's talk about something
172:41 now let's talk about something completely different
172:43 completely different lambda
172:45 lambda lambda
172:46 lambda functions
172:49 functions so let me just give you a quick example
172:51 so let me just give you a quick example [Music]
172:57 lambda num num
172:57 num [Music]
172:59 [Music] times
173:00 times 2. so
173:02 2. so lambda functions
173:03 lambda functions also called anonymous functions are tiny
173:06 also called anonymous functions are tiny functions they have no name and only
173:08 functions they have no name and only have one expression as their body
173:11 have one expression as their body so they're defined using the lambda
173:12 so they're defined using the lambda keyword and so
173:14 keyword and so this is going to be the argument
173:17 this is going to be the argument and this is going to be the expression
173:20 and this is going to be the expression the body must be a single expression and
173:22 the body must be a single expression and it has to be an expression not a
173:23 it has to be an expression not a statement so this difference is
173:25 statement so this difference is important an expression returns a value
173:27 important an expression returns a value a statement does not so it has to return
173:31 a statement does not so it has to return a value so the value that's being
173:33 a value so the value that's being returned is the number times two the
173:35 returned is the number times two the number that was passed in going to
173:37 number that was passed in going to multiply it by 2 in this example
173:39 multiply it by 2 in this example so this is basically the simplest
173:41 so this is basically the simplest example of a lambda function it just
173:43 example of a lambda function it just doubles the value of a number and lambda
173:46 doubles the value of a number and lambda functions can accept more
173:49 functions can accept more arguments so
173:50 arguments so so for instance i could do
173:52 so for instance i could do [Music]
173:54 [Music] lambda
173:56 lambda a comma b
173:57 a comma b and then we can multiply a times b
174:02 and then we can multiply a times b lambda functions cannot be invoked
174:04 lambda functions cannot be invoked directly but you can't assign them to
174:06 directly but you can't assign them to variables so for instance i can assign
174:10 variables so for instance i can assign this to the variable called
174:12 this to the variable called multiply so multiply is going to this
174:15 multiply so multiply is going to this function is going to be assigned to this
174:18 function is going to be assigned to this variable here so then the way that i
174:20 variable here so then the way that i would use that i could print now i'll
174:23 would use that i could print now i'll print the result of calling multiply
174:27 print the result of calling multiply and then i pass in
174:29 and then i pass in two
174:30 two and four
174:31 and four so if i just run that okay 2 times 4 is
174:34 so if i just run that okay 2 times 4 is 8. we can see right in the console here
174:36 8. we can see right in the console here and then i'm going to just
174:38 and then i'm going to just zoom in just a little bit
174:40 zoom in just a little bit so the utility of lambda functions comes
174:42 so the utility of lambda functions comes when combined with other python
174:44 when combined with other python functionality for example in combination
174:47 functionality for example in combination with map filter and reduce so speaking
174:50 with map filter and reduce so speaking of map filter and reduce that's what
174:52 of map filter and reduce that's what we're going to talk about now map
174:55 we're going to talk about now map filter
174:56 filter reduce
174:58 reduce so python provides three useful global
175:00 so python provides three useful global functions we that we can use to work
175:02 functions we that we can use to work with collections so this is map filter
175:05 with collections so this is map filter reduce so first let's talk about
175:08 reduce so first let's talk about map and since their functions are going
175:10 map and since their functions are going to have the
175:12 to have the parentheses at the end so map is used to
175:15 parentheses at the end so map is used to run a function
175:16 run a function upon each item in an iterable item like
175:19 upon each item in an iterable item like a list and create a new list with the
175:21 a list and create a new list with the same number of items but the values of
175:23 same number of items but the values of each item can be changed so here's an
175:27 each item can be changed so here's an example we have this list
175:29 example we have this list and then here's the function
175:32 and then here's the function and then we are going to map through
175:35 and then we are going to map through each item in the list
175:37 each item in the list and so here's the function we're going
175:38 and so here's the function we're going to run we're going to run this function
175:40 to run we're going to run this function on each item in the list
175:43 on each item in the list and now we're going to get a new list so
175:45 and now we're going to get a new list so i can do print
175:47 i can do print result now if we print that i'll just
175:50 result now if we print that i'll just run that function and we can see
175:53 run that function and we can see okay we get a map object
176:01 so then we can always just pass it into the
176:03 the list function
176:05 list function and then we can
176:06 and then we can run the program again two four six so
176:09 run the program again two four six so one two three became two four
176:11 one two three became two four so yeah whenever you want to do run a
176:13 so yeah whenever you want to do run a function on each item in a list you can
176:15 function on each item in a list you can use map
176:17 use map and when the function is a one-liner
176:19 and when the function is a one-liner it's common to use a lambda function so
176:22 it's common to use a lambda function so we just talked about lambda functions so
176:24 we just talked about lambda functions so now let me show you how you would do
176:26 now let me show you how you would do this as a lambda function so
176:29 this as a lambda function so double
176:30 double is going this is going to be a variable
176:32 is going this is going to be a variable and we're going to assign it to a lambda
176:33 and we're going to assign it to a lambda function
176:35 function and i'm going to
176:41 so now this lambda function takes the number
176:42 number a
176:43 a and then does a times two so and this we
176:46 and then does a times two so and this we just keep the same because now we're
176:48 just keep the same because now we're using a lambda function here and we're
176:50 using a lambda function here and we're taking each number and passing it
176:52 taking each number and passing it through this function where we have the
176:54 through this function where we have the the this is each number in the list and
176:55 the this is each number in the list and we multiply it so if i run this program
176:57 we multiply it so if i run this program it should look exactly the same
177:00 it should look exactly the same and we can even simplify it even more so
177:03 and we can even simplify it even more so this is where lambda functions really
177:05 this is where lambda functions really shine instead of assign it assigning it
177:08 shine instead of assign it assigning it to double first i can copy the whole
177:11 to double first i can copy the whole function i can delete this completely
177:14 function i can delete this completely and now i can just put it right in here
177:17 and now i can just put it right in here so now we're mapping over this function
177:20 so now we're mapping over this function and we don't even have to create the
177:22 and we don't even have to create the function in a different line and assign
177:24 function in a different line and assign it to a variable first we can put the
177:25 it to a variable first we can put the lambda function right in the same line
177:28 lambda function right in the same line right within the map and now i run this
177:30 right within the map and now i run this and it's going to give us the same
177:32 and it's going to give us the same result so remember we started with
177:34 result so remember we started with when i first showed you this example we
177:37 when i first showed you this example we had a much longer piece of code now
177:38 had a much longer piece of code now we've simplified it with the
177:41 we've simplified it with the lambda function
177:42 lambda function so the original list the original list
177:46 so the original list the original list is left untouched in a new list with the
177:48 is left untouched in a new list with the updated values is returned by map
177:51 updated values is returned by map the result is a map object which is an
177:53 the result is a map object which is an iterable so
177:54 iterable so that's why we needed to cast it to list
177:57 that's why we needed to cast it to list to print its content
177:59 to print its content okay now let's talk about filter
178:03 okay now let's talk about filter let me put in let me just update the
178:05 let me put in let me just update the code here it's kind of similar but now
178:07 code here it's kind of similar but now we're using filter filter takes an
178:09 we're using filter filter takes an iterable and returns a filter object
178:13 iterable and returns a filter object which is another iterable but without
178:15 which is another iterable but without some of the original items so you can do
178:18 some of the original items so you can do so by returning true or false from the
178:21 so by returning true or false from the filtering
178:22 filtering the filtering function so here's the
178:25 the filtering function so here's the filtering function we are going to check
178:27 filtering function we are going to check if the item passed in is even so
178:31 if the item passed in is even so so here's the list here so
178:33 so here's the list here so you can see we're calling filter we pass
178:35 you can see we're calling filter we pass in the function the filtering function
178:37 in the function the filtering function and then the list
178:38 and then the list and we're going to return true or false
178:41 and we're going to return true or false from this function so if it can be if
178:43 from this function so if it can be if it's divisible by if when you divide it
178:46 it's divisible by if when you divide it by two we have zero remainder
178:49 by two we have zero remainder then it's even
178:51 then it's even so
178:52 so that would return true so this line
178:54 that would return true so this line would return true
178:56 would return true and then if not it would return false if
178:58 and then if not it would return false if it's odd so now any even number is going
179:02 it's odd so now any even number is going to be added to the result and any odd
179:04 to be added to the result and any odd number is not going to be added to the
179:06 number is not going to be added to the result so basically we're filtering the
179:07 result so basically we're filtering the list based on this function and then
179:10 list based on this function and then here we just print
179:12 here we just print we convert that result to a list and if
179:14 we convert that result to a list and if we run that it's two and obviously if we
179:17 we run that it's two and obviously if we can just put in
179:19 can just put in uh more numbers here
179:21 uh more numbers here and run that again
179:22 and run that again we have two four six
179:24 we have two four six and then we can just like before we can
179:26 and then we can just like before we can use a lambda function so
179:29 use a lambda function so i'm just going to
179:31 i'm just going to copy this here we can just delete this
179:33 copy this here we can just delete this whole thing and we are going to put a
179:35 whole thing and we are going to put a lambda function here so lamb
179:38 lambda function here so lamb duh
179:38 duh [Music]
179:41 [Music] so now you can see we're just putting
179:44 so now you can see we're just putting the lambda function in the in line here
179:46 the lambda function in the in line here and we are checking to see if it's this
179:49 and we are checking to see if it's this is going to turn true or false whether
179:50 is going to turn true or false whether it's even or not
179:52 it's even or not and so i run the program and it's going
179:54 and so i run the program and it's going to give me the exact same result here
179:56 to give me the exact same result here okay the final thing we're going to talk
179:58 okay the final thing we're going to talk about is reduce
180:00 about is reduce reduce is used to calculate a value out
180:03 reduce is used to calculate a value out of a sequence like a list so for example
180:06 of a sequence like a list so for example suppose we have this list of expenses
180:09 suppose we have this list of expenses stored as tuples
180:11 stored as tuples and so so we had dinner 80 car repair
180:13 and so so we had dinner 80 car repair 180 or 120 and we want to calculate the
180:18 180 or 120 and we want to calculate the sum
180:18 sum of this property
180:21 of this property in each tuple
180:22 in each tuple in this case the cost of the expense
180:25 in this case the cost of the expense so here's kind of the long way of doing
180:28 so here's kind of the long way of doing it without using reduce
180:31 it without using reduce we basically take every expense in
180:34 we basically take every expense in expenses and then we add to the sum here
180:37 expenses and then we add to the sum here and we add expense one that's going to
180:39 and we add expense one that's going to be the
180:41 be the the item at index one and then we get
180:43 the item at index one and then we get the sum and we can print the sum so
180:45 the sum and we can print the sum so that's kind of like the long way of
180:47 that's kind of like the long way of doing it without reduce but there's a
180:50 doing it without reduce but there's a quicker way so
180:52 quicker way so to use reduce reduce is a little
180:55 to use reduce reduce is a little different from map and filter where it's
180:57 different from map and filter where it's not available it's not it's not
181:00 not available it's not it's not available automatically we have to
181:02 available automatically we have to import it from the standard library func
181:04 import it from the standard library func tools so i'll do from
181:06 tools so i'll do from funk
181:08 funk tools or function tools
181:10 tools or function tools import
181:11 import [Music]
181:14 [Music] reduce
181:23 create a new i'm going to create a new variable called sum
181:25 variable called sum and we're going to set it to
181:28 and we're going to set it to reduce we're going to use reduce and now
181:30 reduce we're going to use reduce and now i'm just going to
181:31 i'm just going to pass in i'm going to go directly to the
181:33 pass in i'm going to go directly to the lambda function so
181:35 lambda function so lambda
181:43 let me just kind of explain this for a little bit so reduce the first is going
181:45 little bit so reduce the first is going to take a function the reduction
181:47 to take a function the reduction function
181:48 function and then
181:49 and then the iterable here
181:51 the iterable here and the function has to take two
181:54 and the function has to take two arguments
181:55 arguments so this the first argument is the
181:57 so this the first argument is the accumulated value and then the the right
182:00 accumulated value and then the the right argument is
182:02 argument is the updated the update value from the
182:05 the updated the update value from the iterable so we're going to
182:07 iterable so we're going to continue adding
182:09 continue adding these two item we're going to
182:11 these two item we're going to basically add every item together and
182:14 basically add every item together and reduce
182:16 reduce the
182:17 the i the numbers at the first index all
182:20 i the numbers at the first index all into down to one value by adding them
182:24 into down to one value by adding them all together so i'll just
182:26 all together so i'll just pray here play here and then we get the
182:28 pray here play here and then we get the same number 200 and you can see it's a
182:31 same number 200 and you can see it's a lot it's a lot quicker just to use the
182:33 lot it's a lot quicker just to use the reduce function compared to the other
182:36 reduce function compared to the other code we had previously
182:38 code we had previously okay next up we are going to talk about
182:40 okay next up we are going to talk about recursion
182:42 recursion in
182:42 in python not recursion error just
182:44 python not recursion error just recursion
182:46 recursion and a function in python can call itself
182:49 and a function in python can call itself that's what recursion is and it can be
182:52 that's what recursion is and it can be pretty useful in many scenarios a common
182:54 pretty useful in many scenarios a common way to explain recursion is by using the
182:56 way to explain recursion is by using the factorial calculation
182:58 factorial calculation so let me show you how you would
182:59 so let me show you how you would calculate factorial this isn't python
183:02 calculate factorial this isn't python code this is just an example here so a
183:04 code this is just an example here so a fact when you do 3 factorial that means
183:07 fact when you do 3 factorial that means you do 3
183:08 you do 3 you multiply every number between three
183:12 you multiply every number between three between this number and one together so
183:14 between this number and one together so three times two times one equals six
183:16 three times two times one equals six four factorial is four times three times
183:18 four factorial is four times three times two times one five factorial is you know
183:20 two times one five factorial is you know five through one and so on and then
183:22 five through one and so on and then every number you multiply every whole
183:24 every number you multiply every whole number down to one so using recursion we
183:28 number down to one so using recursion we can write a function that calculates the
183:30 can write a function that calculates the factorial of any number so let me show
183:33 factorial of any number so let me show you so here's the function you can see
183:35 you so here's the function you can see inside the function it's calling the
183:38 inside the function it's calling the same function so a recursive function
183:40 same function so a recursive function it's always going to have a base case
183:42 it's always going to have a base case that's this
183:44 that's this and the recursive case so the base case
183:47 and the recursive case so the base case is when we're going to leave the
183:50 is when we're going to leave the the recursive function so if n is equal
183:53 the recursive function so if n is equal to one we're going to return one and
183:56 to one we're going to return one and that's basically going to get out of the
183:58 that's basically going to get out of the recursive function uh but if n is not
184:01 recursive function uh but if n is not going to equal one then we have the
184:03 going to equal one then we have the recursive the recursive case where we're
184:06 recursive the recursive case where we're going to call the function so you always
184:09 going to call the function so you always need to have at least you always have
184:10 need to have at least you always have need to have a base case so eventually
184:12 need to have a base case so eventually the recursion can stop
184:15 the recursion can stop if the recursion doesn't ever stop
184:18 if the recursion doesn't ever stop then you're going to get a recursion
184:20 then you're going to get a recursion error
184:21 error basically python by default will halt
184:23 basically python by default will halt recursions at 1000 calls and that's when
184:25 recursions at 1000 calls and that's when you get the recursion error so this is
184:28 you get the recursion error so this is going to get the factorial three but
184:30 going to get the factorial three but let's just do this a few more times so
184:32 let's just do this a few more times so you can see the difference so three
184:34 you can see the difference so three four
184:34 four five and now we'll test this out
184:37 five and now we'll test this out 6 24
184:39 6 24 120.
184:40 120. okay now let's talk about decorators
184:45 okay now let's talk about decorators so decorators in python are a way to
184:47 so decorators in python are a way to change enhance or alter in any way how a
184:50 change enhance or alter in any way how a function works decorators are defined
184:52 function works decorators are defined with the at symbol followed by the
184:54 with the at symbol followed by the decorator name just before the function
184:56 decorator name just before the function definition
184:59 definition so for instance let's say we have a
185:01 so for instance let's say we have a function
185:02 function hello
185:03 hello and it's just going to be the simplest
185:05 and it's just going to be the simplest function we're just going to print
185:11 hello uh so
185:12 uh so to make that to add a decorator i'm
185:15 to make that to add a decorator i'm going to put like this an at sign and
185:18 going to put like this an at sign and then the decorator name in this
185:20 then the decorator name in this case we're going to type in log time so
185:23 case we're going to type in log time so the function has the log time decorator
185:27 the function has the log time decorator assigned so
185:29 assigned so whenever we call the hello function the
185:32 whenever we call the hello function the decorator is going to be called a
185:34 decorator is going to be called a decorator is a function that takes a
185:36 decorator is a function that takes a function as a parameter wraps the
185:38 function as a parameter wraps the function in an inner function that
185:40 function in an inner function that performs the job it has to do and
185:42 performs the job it has to do and returns that inner function
185:45 returns that inner function so for instance i'm going to create
185:46 so for instance i'm going to create another function here that's going to be
185:49 another function here that's going to be the log time function
185:51 the log time function and now we we can do something before
185:54 and now we we can do something before and after the function like for instance
185:58 and after the function like for instance we can say
186:00 we can say print
186:04 before and then after
186:06 and then after we are going to print
186:08 we are going to print after
186:10 after now if i run this
186:16 oh and we have to call the function that's always important
186:19 that's always important now if i run this
186:21 now if i run this before hello after
186:24 before hello after so you're going to often use decorator
186:26 so you're going to often use decorator functions when you want to change the
186:27 functions when you want to change the behavior of a function without modifying
186:30 behavior of a function without modifying the function itself so a few good
186:32 the function itself so a few good examples are when you want to add
186:33 examples are when you want to add logging test performance perform caching
186:36 logging test performance perform caching verify permissions and so on
186:38 verify permissions and so on you can also use one when you need to
186:41 you can also use one when you need to run the same code on multiple functions
186:44 run the same code on multiple functions okay now let's talk about doc strings so
186:47 okay now let's talk about doc strings so doc strings
186:50 doc strings [Music]
186:51 [Music] documentation is hugely important not
186:54 documentation is hugely important not just to communicate to other people what
186:55 just to communicate to other people what the goal of a function or class or
186:57 the goal of a function or class or method or module is but it's also it
186:59 method or module is but it's also it also communicates to yourself when you
187:01 also communicates to yourself when you come back to your code like many months
187:04 come back to your code like many months from now you might not remember all the
187:05 from now you might not remember all the knowledge you were holding in your head
187:07 knowledge you were holding in your head when you wrote the code so at that point
187:09 when you wrote the code so at that point reading your code and understanding what
187:11 reading your code and understanding what it's supposed to do
187:13 it's supposed to do so that at that point reading your code
187:15 so that at that point reading your code and understanding what it's supposed to
187:17 and understanding what it's supposed to do will be a lot more difficult so a lot
187:19 do will be a lot more difficult so a lot that's one of the reasons why people add
187:22 that's one of the reasons why people add comments so another way is to use a doc
187:26 comments so another way is to use a doc string so let me show you what a doc
187:29 string so let me show you what a doc string looks like
187:30 string looks like the utility of a doc strings is that
187:32 the utility of a doc strings is that they follow conventions
187:35 they follow conventions so they can be processed automatically
187:37 so they can be processed automatically so this is how you would define a doc
187:39 so this is how you would define a doc string for a function
187:41 string for a function basically you're putting the three
187:42 basically you're putting the three quotation marks here three quotation
187:44 quotation marks here three quotation marks there and then this is a
187:47 marks there and then this is a description of what the function is
187:50 description of what the function is this is how you would define a doc
187:52 this is how you would define a doc string for a
187:54 string for a a class and a method so
187:56 a class and a method so got the class this is what the class
187:58 got the class this is what the class does is this is what the method does
188:02 does is this is what the method does and then it's also common to
188:04 and then it's also common to add docs place a doc string at the top
188:07 add docs place a doc string at the top of the file so if you put a doc string
188:09 of the file so if you put a doc string at the top of the file it's going to
188:11 at the top of the file it's going to look like this
188:13 look like this and it's going to explain what the file
188:16 and it's going to explain what the file is all about
188:18 is all about and docs stock strings can also span
188:21 and docs stock strings can also span multiple lines just like this is a
188:22 multiple lines just like this is a multiple line docs string as long as it
188:25 multiple line docs string as long as it has the three quarts three quarters at
188:26 has the three quarts three quarters at the top three quarts at the bottom
188:30 the top three quarts at the bottom and then python will process the doc
188:33 and then python will process the doc strings and you can use the help global
188:36 strings and you can use the help global function to get the documentation for a
188:39 function to get the documentation for a class a method a function or a module
188:41 class a method a function or a module for example i'm going to go to the
188:42 for example i'm going to go to the bottom of this and i'm going to say
188:45 bottom of this and i'm going to say print
188:47 print help
188:48 help and then i'm just going to type in
188:50 and then i'm just going to type in dog now i'll run this
188:58 and let me just run it again so now you're going to get information
189:00 so now you're going to get information about the dog we know that the dog has a
189:02 about the dog we know that the dog has a name and age it's a class representing a
189:04 name and age it's a class representing a dog and the has these specific
189:07 dog and the has these specific methods
189:08 methods and then it says more we can get more
189:10 and then it says more we can get more information data descriptors defined
189:12 information data descriptors defined here we have
189:14 here we have and this is just going to give us all
189:15 and this is just going to give us all this information
189:16 this information about
189:17 about the dog and we and so that's why it's
189:20 the dog and we and so that's why it's good to use doc strings because there
189:23 good to use doc strings because there are specific standards and it makes it
189:27 are specific standards and it makes it easier to get information using
189:29 easier to get information using different helper methods
189:37 and standards allow and standards allow us to have tools to
189:39 and standards allow us to have tools to extract doc strings and automatically
189:41 extract doc strings and automatically generate documentation for your code so
189:44 generate documentation for your code so besides just this help functions there's
189:46 besides just this help functions there's a lot of other methods to pull out these
189:48 a lot of other methods to pull out these docs strings and get information about
189:50 docs strings and get information about your code and next we will learn about
189:54 your code and next we will learn about annotations
189:56 annotations python is dynamically typed so we do not
189:59 python is dynamically typed so we do not have to specify the type of a variable
190:01 have to specify the type of a variable or function parameter or a function
190:04 or function parameter or a function return value
190:05 return value annotations allow us to optionally do
190:09 annotations allow us to optionally do that so if we want to actually show what
190:11 that so if we want to actually show what type we're expecting for different
190:14 type we're expecting for different values so here's a function without
190:16 values so here's a function without annotations and then here's how we would
190:19 annotations and then here's how we would make it have annotations so uh we want
190:22 make it have annotations so uh we want to make this function only accept an int
190:25 to make this function only accept an int so i'm going to put colon int
190:27 so i'm going to put colon int and then after here
190:29 and then after here i'm going to put
190:31 i'm going to put actually before the colon here i'm going
190:33 actually before the colon here i'm going to put a little arrow here
190:36 to put a little arrow here and then i'm going to put in int so now
190:38 and then i'm going to put in int so now we're specifying that this function
190:41 we're specifying that this function receives an int and then it's also going
190:44 receives an int and then it's also going to
190:45 to return an end
190:46 return an end and you can do the same thing with
190:49 and you can do the same thing with variables so if we have a variable if i
190:52 variables so if we have a variable if i had a variable called count and was
190:54 had a variable called count and was equal to zero i can add an annotation to
190:57 equal to zero i can add an annotation to make it be an int like that so now
191:01 make it be an int like that so now we're specifying that this variable is
191:03 we're specifying that this variable is going to be an integer
191:06 going to be an integer python will actually ignore these
191:07 python will actually ignore these annotations a separate tool called mypi
191:10 annotations a separate tool called mypi can be run standalone or integrated by
191:13 can be run standalone or integrated by ides to automatically check for type
191:15 ides to automatically check for type errors statically while you're coding
191:18 errors statically while you're coding it'll also help you catch tight
191:20 it'll also help you catch tight mismatched bugs before even running the
191:22 mismatched bugs before even running the code
191:23 code a great help especially when your
191:25 a great help especially when your software becomes large and you need to
191:26 software becomes large and you need to refactor your code
191:28 refactor your code okay now we'll talk about exceptions
191:31 okay now we'll talk about exceptions [Music]
191:34 [Music] it's important to have a way to handle
191:36 it's important to have a way to handle errors and python gives us exception
191:39 errors and python gives us exception handling to do so so for exception
191:41 handling to do so so for exception handling you would wrap lines of code in
191:44 handling you would wrap lines of code in a try block
191:47 a try block and then inside this block you'll put
191:48 and then inside this block you'll put the lines of code and then if an error
191:51 the lines of code and then if an error occurs
191:53 occurs python will alert you and you can
191:55 python will alert you and you can determine which kind of error occurred
191:57 determine which kind of error occurred using an accept block so
192:00 using an accept block so we're we're trying some lines of code
192:02 we're we're trying some lines of code here and then we're checking for a
192:04 here and then we're checking for a specific error and then if that error
192:07 specific error and then if that error happens we would handle that error but
192:09 happens we would handle that error but if a different error happens then we
192:11 if a different error happens then we will handle the different error you can
192:15 will handle the different error you can also catch
192:17 also catch all exceptions
192:18 all exceptions using an accept without an error type so
192:21 using an accept without an error type so at the very end you could just do accept
192:24 at the very end you could just do accept and then if you don't have an error type
192:26 and then if you don't have an error type then it's going to handle the rest of
192:28 then it's going to handle the rest of the exceptions and just to make this
192:31 the exceptions and just to make this clear this is just an example where it
192:33 clear this is just an example where it says air one you have to put a specific
192:36 says air one you have to put a specific error in that spot
192:39 error in that spot you can also put an else block at the
192:41 you can also put an else block at the end to handle that that will run if the
192:44 end to handle that that will run if the no exceptions are found so if there are
192:46 no exceptions are found so if there are no errors in this code that's right up
192:48 no errors in this code that's right up here we can have an else and then run
192:51 here we can have an else and then run specific code at the bottom that that
192:54 specific code at the bottom that that runs if there's no errors
192:56 runs if there's no errors and then we can have a finally block so
192:59 and then we can have a finally block so anything
193:00 anything in a finally block is going to just
193:03 in a finally block is going to just always run at the end whether or not
193:05 always run at the end whether or not there are exceptions or no exceptions
193:08 there are exceptions or no exceptions the code in the final block is always
193:11 the code in the final block is always going to run the specific error that's
193:13 going to run the specific error that's going to occur depends on the operation
193:15 going to occur depends on the operation you're performing for example if you're
193:17 you're performing for example if you're reading a file you might get an eof
193:20 reading a file you might get an eof error would just look like this eof
193:23 error would just look like this eof error which means end of file
193:26 error which means end of file if you divide a number by zero you'll
193:28 if you divide a number by zero you'll get a zero division error if you have a
193:30 get a zero division error if you have a type conversion issue you might get a
193:32 type conversion issue you might get a type error so
193:34 type error so let's try this code
193:36 let's try this code so i'm going to just delete all this and
193:38 so i'm going to just delete all this and we'll do result
193:40 we'll do result equals 2 divided by
193:43 equals 2 divided by 0 which
193:45 0 which you cannot do
193:47 you cannot do so just print the result
193:49 so just print the result and if i run that
193:52 and if i run that we'll see this error over here
193:55 we'll see this error over here zero division error division by zero so
193:59 zero division error division by zero so it's going to get an error when we run
194:00 it's going to get an error when we run the code and then whenever there's an
194:02 the code and then whenever there's an error anything after the error occurs
194:05 error anything after the error occurs will not happen so we're not going to
194:07 will not happen so we're not going to print the result because there is
194:09 print the result because there is because the this
194:10 because the this this line resulted in error so we're not
194:12 this line resulted in error so we're not going to run the following line of code
194:15 going to run the following line of code so now let's try adding that operation
194:17 so now let's try adding that operation in a
194:18 in a try block
194:19 try block so i'm just going to paste it all in
194:21 so i'm just going to paste it all in here and so we're putting the operation
194:24 here and so we're putting the operation in a try block and then we're expecting
194:27 in a try block and then we're expecting a zero division error
194:29 a zero division error where we'll print cannot divide by zero
194:32 where we'll print cannot divide by zero finally we will set the result to one
194:36 finally we will set the result to one and then print the results so let me
194:38 and then print the results so let me just run that code see cannot divide by
194:40 just run that code see cannot divide by zero and then we print one we because we
194:43 zero and then we print one we because we set it in the final block here
194:46 set it in the final block here so this try block lets us recover
194:48 so this try block lets us recover gracefully and move on with the program
194:50 gracefully and move on with the program you can raise exceptions in your own
194:52 you can raise exceptions in your own code too using the raise statement so i
194:55 code too using the raise statement so i could type in raise
194:58 could type in raise and then we can raise an exception
195:00 and then we can raise an exception intentionally
195:03 intentionally and error
195:05 and error so if i just run this
195:08 so if i just run this it will say
195:09 it will say an error because that's what we typed in
195:11 an error because that's what we typed in so you can make it say anything you want
195:12 so you can make it say anything you want for your error
195:14 for your error and this raises a general exception and
195:17 and this raises a general exception and you can and you can intercept it
195:21 you can and you can intercept it just like this so i could say
195:22 just like this so i could say try
195:24 try and then we raise that exception
195:28 and then we raise that exception and then we can do accept
195:31 and then we can do accept exception
195:32 exception as error
195:35 as error and then we can print
195:38 and then we can print the error
195:42 okay if i run that so now instead of we don't see all that
195:45 so now instead of we don't see all that red anymore because it's not stopping
195:47 red anymore because it's not stopping our program because of the error but
195:49 our program because of the error but it's now printing the error message
195:51 it's now printing the error message right here
195:52 right here just like that
195:54 just like that you can also define your own exception
195:57 you can also define your own exception class extending from exception so i
196:01 class extending from exception so i could do class
196:03 could do class dog not
196:05 dog not found
196:06 found [Music]
196:07 [Music] exception
196:09 exception and then i will
196:11 and then i will extend from exception
196:13 extend from exception [Music]
196:16 [Music] and then i can just put
196:18 and then i can just put pass for this one here let me adjust
196:21 pass for this one here let me adjust that
196:23 that so
196:23 so pass here just means nothing and we must
196:26 pass here just means nothing and we must use it when we define a class without
196:28 use it when we define a class without methods or a function without code so if
196:31 methods or a function without code so if you're not going to put anything so this
196:33 you're not going to put anything so this is just an example so i can just put
196:35 is just an example so i can just put pass to mean that we're not going to
196:37 pass to mean that we're not going to have any code in this
196:39 have any code in this so now we can try it out so i'll just
196:42 so now we can try it out so i'll just paste that uh so we're going to raise
196:44 paste that uh so we're going to raise dog not found exception and then if
196:47 dog not found exception and then if we're we're going to
196:49 we're we're going to candle this exception and just print dog
196:51 candle this exception and just print dog not found so let's try that yep dog nut
196:54 not found so let's try that yep dog nut bound because it raised this exception
196:56 bound because it raised this exception here we can also actually do something
197:00 here we can also actually do something in the exception
197:01 in the exception so if i can say print
197:03 so if i can say print inside
197:04 inside and then i'm going to run that
197:06 and then i'm going to run that and i'll do inside and dog not bound the
197:09 and i'll do inside and dog not bound the with statement is very helpful to
197:11 with statement is very helpful to simplify working with exception handling
197:14 simplify working with exception handling for example when working with files each
197:16 for example when working with files each time we open a file we must remember to
197:18 time we open a file we must remember to close it with makes the process more
197:21 close it with makes the process more transparent so let me show you some
197:23 transparent so let me show you some example code without the with statement
197:27 example code without the with statement so
197:27 so we're not going to go into a lot of
197:29 we're not going to go into a lot of details about working with files here
197:31 details about working with files here but i just want to kind of just give
197:33 but i just want to kind of just give this one quick example so if we're going
197:36 this one quick example so if we're going to be working with files in python so we
197:39 to be working with files in python so we can open the file and then we can read
197:42 can open the file and then we can read the file we can print the content from
197:44 the file we can print the content from the file
197:45 the file and we
197:46 and we we're going to try that because there
197:48 we're going to try that because there could be an exception and then finally
197:50 could be an exception and then finally we're always going to make sure to close
197:52 we're always going to make sure to close the file
197:53 the file but
197:54 but an alternate way to do it would be like
197:56 an alternate way to do it would be like this
197:57 this um so with
197:59 um so with we're going to open the file as file and
198:01 we're going to open the file as file and then content file.read and then print
198:04 then content file.read and then print the content and with using with it's
198:07 the content and with using with it's going to make sure to automatically
198:09 going to make sure to automatically close the file at the end in other words
198:12 close the file at the end in other words we have built-in implicit exception
198:14 we have built-in implicit exception handling as close will be called
198:17 handling as close will be called automatically for us
198:18 automatically for us and with can do a lot more stuff as well
198:21 and with can do a lot more stuff as well this example is just meant to introduce
198:23 this example is just meant to introduce its capabilities
198:25 its capabilities now let's talk about third-party
198:27 now let's talk about third-party packages
198:28 packages and we're going to talk about pip so
198:31 and we're going to talk about pip so let's learn how to install third-party
198:33 let's learn how to install third-party packages in python using pip the python
198:36 packages in python using pip the python standard library contains a huge number
198:38 standard library contains a huge number of utilities that simplify our python
198:40 of utilities that simplify our python development needs but nothing can
198:42 development needs but nothing can satisfy everything
198:44 satisfy everything that's why individuals and companies
198:45 that's why individuals and companies create packages and make them available
198:48 create packages and make them available as open source software for the entire
198:49 as open source software for the entire community so the modules are all
198:52 community so the modules are all collected in a single place called the
198:55 collected in a single place called the python package index which is available
198:58 python package index which is available available at pipe.org that's
199:01 available at pipe.org that's pi
199:02 pi pi.org
199:04 pi.org and they can be installed on the system
199:07 and they can be installed on the system using
199:08 using pip there's over 270 000 packages freely
199:12 pip there's over 270 000 packages freely available
199:13 available most computers are already going to have
199:15 most computers are already going to have pip installed and it already has pip
199:17 pip installed and it already has pip installed so let me show you how you
199:19 installed so let me show you how you would install a package we'd have to go
199:22 would install a package we'd have to go over to the shell here if you're not on
199:25 over to the shell here if you're not on replit you can just do in your terminal
199:27 replit you can just do in your terminal and i'm going to clear this here
199:30 and i'm going to clear this here and i'm just going to do pip
199:33 and i'm just going to do pip install and then you can put the name of
199:36 install and then you can put the name of a package for instance one popular
199:38 a package for instance one popular package is called the request package
199:41 package is called the request package it's an http library so i can do
199:43 it's an http library so i can do requests
199:45 requests and let me just
199:46 and let me just so you can see i have to make sure i
199:48 so you can see i have to make sure i spelled that right
199:49 spelled that right [Music]
199:52 [Music] and it's going to install that package
199:55 and it's going to install that package right now so once the we install this
199:57 right now so once the we install this package it's going to be available for
199:59 package it's going to be available for all our python scripts because packages
200:02 all our python scripts because packages are installed globally and the exact
200:04 are installed globally and the exact location depends on the operating system
200:09 location depends on the operating system you can also upgrade a package to its
200:11 you can also upgrade a package to its latest version by doing pip install
200:15 latest version by doing pip install dash u and then i will just put the
200:18 dash u and then i will just put the package name so in this case we'll just
200:19 package name so in this case we'll just do request again
200:26 and then it's going to just update it to its
200:27 its latest version
200:28 latest version in this case
200:30 in this case it updated from
200:32 it updated from 2.28.0 to 2.28.1
200:36 2.28.0 to 2.28.1 you can also specify a specific
200:40 you can also specify a specific version when you're installing
200:43 version when you're installing and then you can also uninstall a
200:45 and then you can also uninstall a package so i'll do pip
200:48 package so i'll do pip uninstall
200:49 uninstall requests
200:57 and then i can say that yes i do want to uninstall that
200:58 uninstall that and then once when you have a package
201:00 and then once when you have a package installed
201:02 installed i'm just going to install request again
201:07 and then you always have to make sure you spell it right
201:11 you spell it right so once you have it installed you can do
201:12 so once you have it installed you can do pip show
201:14 pip show requests
201:16 requests and then it's going to show some
201:17 and then it's going to show some information about the package
201:20 information about the package so see we can see the name the version
201:23 so see we can see the name the version uh the summary and then a bunch of the
201:25 uh the summary and then a bunch of the author and a bunch of other information
201:27 author and a bunch of other information about the package
201:30 about the package okay i'll just clear this
201:32 okay i'll just clear this now we're actually gonna backtrack a
201:34 now we're actually gonna backtrack a little bit we already talked about lists
201:35 little bit we already talked about lists but i'm gonna talk about a more advanced
201:38 but i'm gonna talk about a more advanced way of using lists called
201:40 way of using lists called list
201:41 list compression
201:43 compression list compressions so list compressions
201:46 list compressions so list compressions are a way to create lists in a very
201:48 are a way to create lists in a very concise way so suppose you have this
201:51 concise way so suppose you have this list like this it's a list of numbers
201:54 list like this it's a list of numbers and we'll just do
201:55 and we'll just do one
201:56 one two three
201:57 two three four five
201:59 four five so we can create a new list using a list
202:01 so we can create a new list using a list compression
202:02 compression composed by the numbers list elements to
202:06 composed by the numbers list elements to the power of 2.
202:08 the power of 2. let me show you what i mean so let's get
202:10 let me show you what i mean so let's get make a new list numbers
202:13 make a new list numbers power
202:14 power [Music]
202:16 [Music] 2
202:17 2 equals
202:18 equals and let me just show you how you do this
202:20 and let me just show you how you do this list compression
202:26 so this is the list compression syntax and if i print this
202:30 and if i print this [Music]
202:31 [Music] we can see that now we have every
202:33 we can see that now we have every element in the list to the power of two
202:35 element in the list to the power of two list compressions are a syntax that's
202:38 list compressions are a syntax that's sometimes preferred over loops as it's
202:40 sometimes preferred over loops as it's more readable when the operation can be
202:42 more readable when the operation can be written on a single line
202:44 written on a single line so for instance this is how you would do
202:46 so for instance this is how you would do it uh with a loop so what we do in a
202:49 it uh with a loop so what we do in a single line up here we take a few lines
202:52 single line up here we take a few lines to do
202:53 to do in the method with a loop so list
202:55 in the method with a loop so list compression just makes it
202:57 compression just makes it simpler
202:58 simpler and then you can do the same thing with
203:00 and then you can do the same thing with map as well
203:01 map as well but again it's just a little more
203:03 but again it's just a little more complex sometimes it's just simpler to
203:06 complex sometimes it's just simpler to use a list compression using the syntax
203:08 use a list compression using the syntax here
203:09 here now let's talk about a few more advanced
203:11 now let's talk about a few more advanced topics in regards to functions
203:13 topics in regards to functions polymorphism
203:15 polymorphism polymorphism generalizes a functionality
203:18 polymorphism generalizes a functionality so it can work on different types
203:20 so it can work on different types it's an important concept in object
203:22 it's an important concept in object oriented programming so see in here
203:25 oriented programming so see in here we've defined the same method on
203:28 we've defined the same method on different classes so the dog has eat and
203:31 different classes so the dog has eat and the cat also has an eat method
203:34 the cat also has an eat method then we can generate objects and we can
203:35 then we can generate objects and we can call the eat method regardless of the
203:38 call the eat method regardless of the class the object belongs to and will get
203:40 class the object belongs to and will get different results so we create the two
203:43 different results so we create the two objects the dog and the cat here
203:45 objects the dog and the cat here and we're calling the eat method on both
203:48 and we're calling the eat method on both objects and if we run this you can see
203:50 objects and if we run this you can see what we're getting eating cat dog food
203:53 what we're getting eating cat dog food eating cat food and so you could do a
203:56 eating cat food and so you could do a lot of things with this like maybe you
203:58 lot of things with this like maybe you have a list of different animals and
204:01 have a list of different animals and then you can
204:02 then you can loop through that list and call the eat
204:04 loop through that list and call the eat function or the eat method on each
204:06 function or the eat method on each animal in that list and they don't have
204:08 animal in that list and they don't have to be the exact same class to be able to
204:11 to be the exact same class to be able to still run the eat method
204:13 still run the eat method so we build a generalized interface and
204:16 so we build a generalized interface and now we do not need to know that an
204:18 now we do not need to know that an animal is a cat or dog we just need to
204:20 animal is a cat or dog we just need to know that we can call eat on it
204:23 know that we can call eat on it now let's talk about
204:24 now let's talk about operator
204:26 operator overloading
204:27 overloading [Music]
204:29 [Music] operator overloading is an advanced
204:31 operator overloading is an advanced technique we can use to make classes
204:33 technique we can use to make classes comparable and to make them work with
204:35 comparable and to make them work with python operators so let's take this
204:38 python operators so let's take this class dog so here's a dog class and you
204:41 class dog so here's a dog class and you can create a dog with a name and age
204:44 can create a dog with a name and age then we'll create two dog objects we'll
204:47 then we'll create two dog objects we'll do roger equals dog
204:50 do roger equals dog and we can pass the name
204:56 and eight [Music]
204:59 [Music] and then i'll make another one
205:08 we can use operator overloading to add a custom way to compare these two objects
205:11 custom way to compare these two objects based on the age property
205:13 based on the age property so like how could you compare
205:16 so like how could you compare this dog and this dog well we can make
205:19 this dog and this dog well we can make it possible with operator overloading so
205:22 it possible with operator overloading so let me just show you this example here
205:24 let me just show you this example here so
205:25 so this
205:26 this function here gt
205:28 function here gt is going to compare things as to figure
205:31 is going to compare things as to figure out what what is greater than you can
205:33 out what what is greater than you can now we'll be able to compare
205:35 now we'll be able to compare two dog objects to see which one is
205:37 two dog objects to see which one is greater than the other and this is how
205:39 greater than the other and this is how we're going to figure out which is
205:41 we're going to figure out which is greater than return true if self.age is
205:45 greater than return true if self.age is greater than
205:46 greater than other dot age which is the other one
205:48 other dot age which is the other one you're comparing it to else false
205:52 you're comparing it to else false now we can
205:53 now we can do print
205:58 roger is greater than sid so we're trying to
206:01 is greater than sid so we're trying to figure out this is true or false if i
206:02 figure out this is true or false if i run this it's going to say true roger is
206:06 run this it's going to say true roger is greater than sid because 8 is bigger
206:09 greater than sid because 8 is bigger than 7. but if we like put 9 here run
206:12 than 7. but if we like put 9 here run that
206:13 that now it's going to be false
206:15 now it's going to be false so in the same way we define this
206:18 so in the same way we define this underscore underscore gt underscore
206:20 underscore underscore gt underscore which means greater than we can also
206:22 which means greater than we can also define methods for like less than
206:25 define methods for like less than lower or equal to greater equal to or
206:27 lower or equal to greater equal to or not equal
206:29 not equal and then you can also create methods to
206:32 and then you can also create methods to go with different arithmetic operators
206:35 go with different arithmetic operators so
206:36 so we can do add subtract multiply
206:38 we can do add subtract multiply division floor division mod power so you
206:41 division floor division mod power so you can see all these different ones you can
206:43 can see all these different ones you can make it respond to the different
206:44 make it respond to the different operators so the example was just a
206:47 operators so the example was just a greater than operator but we can
206:49 greater than operator but we can make functions to show how it's going to
206:51 make functions to show how it's going to respond to all these different
206:53 respond to all these different operators there's even a few more
206:55 operators there's even a few more methods to work with other operators but
206:57 methods to work with other operators but you get the idea we've learned a lot
206:59 you get the idea we've learned a lot about python and now we're going to
207:01 about python and now we're going to bring a lot of what we've learned
207:03 bring a lot of what we've learned together to code a blackjack card game
207:06 together to code a blackjack card game and in the process we'll learn about
207:08 and in the process we'll learn about object oriented programming in python so
207:10 object oriented programming in python so we'll start by creating a new python
207:13 we'll start by creating a new python project on replit
207:18 and i'm just going to close this tab here
207:19 here and i'll zoom in just a bit
207:23 and i'll zoom in just a bit and just like our first project
207:26 and just like our first project i'm going to
207:27 i'm going to say what i'm about to do and i want you
207:30 say what i'm about to do and i want you to see if you can do it on your own
207:32 to see if you can do it on your own before i show you how to do it
207:35 before i show you how to do it and with all you've learned so far a lot
207:38 and with all you've learned so far a lot of this you're probably going to be able
207:40 of this you're probably going to be able to figure out on your own as i give you
207:42 to figure out on your own as i give you the instructions without even seeing how
207:45 the instructions without even seeing how i how i do it but then you can come back
207:47 i how i do it but then you can come back to the video and see how i do it
207:50 to the video and see how i do it or i guess you can just watch and not
207:52 or i guess you can just watch and not even try to do it yourself but you're
207:53 even try to do it yourself but you're going to learn a lot more if you try to
207:55 going to learn a lot more if you try to code this by yourself along with me as i
207:58 code this by yourself along with me as i do it but right before i do the
208:01 do it but right before i do the different steps
208:02 different steps so the first thing we're going to do is
208:04 so the first thing we're going to do is create a
208:05 create a variable called a suit
208:07 variable called a suit and set it equal to hearts and then a
208:10 and set it equal to hearts and then a variable called rank and set it to equal
208:14 variable called rank and set it to equal k for king
208:15 k for king and then a variable called value and set
208:18 and then a variable called value and set to equal
208:20 to equal 10.
208:21 10. [Music]
208:22 [Music] okay simple two variables equal to
208:24 okay simple two variables equal to strings and one variable equal to an int
208:28 strings and one variable equal to an int so now we are going to add a print
208:30 so now we are going to add a print statement and print the the phrase your
208:34 statement and print the the phrase your card is with a colon at the end
208:38 card is with a colon at the end and then we'll add another print
208:39 and then we'll add another print statement and print the rank
208:42 statement and print the rank so now we're just printing the variable
208:45 so now we're just printing the variable here
208:46 here and we're going to be doing a lot of
208:48 and we're going to be doing a lot of refactoring as we create this program
208:50 refactoring as we create this program let's refactor this so it's just one
208:53 let's refactor this so it's just one print statement that's going to print
208:54 print statement that's going to print your card is colon space and then the
208:57 your card is colon space and then the rank
209:03 so we are going to be doing string concatenation
209:05 string concatenation [Music]
209:07 [Music] just like that
209:09 just like that so
209:10 so you can concatenate as many strings and
209:12 you can concatenate as many strings and variables as you want so let's update
209:14 variables as you want so let's update the code so that the print function
209:16 the code so that the print function print prints your card as
209:18 print prints your card as k
209:19 k of
209:20 of hearts
209:22 hearts so we just need to add
209:24 so we just need to add of and we have to make sure we put
209:26 of and we have to make sure we put spaces on each side of the word of
209:31 and then suit
209:34 suit and let me just
209:35 and let me just adjust this here okay as you know you
209:37 adjust this here okay as you know you can use a list in python to store
209:38 can use a list in python to store multiple values or items at a time so
209:41 multiple values or items at a time so above the suit variable
209:43 above the suit variable create a suits
209:45 create a suits variable and assign it to a list of
209:46 variable and assign it to a list of suits in this case spades clubs hearts
209:50 suits in this case spades clubs hearts diamonds
209:54 we learned about how you can use the bracket operator to access a specific
209:57 bracket operator to access a specific element in a list the number inside the
210:00 element in a list the number inside the bracket specifies the index of the list
210:02 bracket specifies the index of the list to access remember the indexes start at
210:04 to access remember the indexes start at zero
210:06 zero so you update the suit variable so that
210:08 so you update the suit variable so that the value of hearts come from comes from
210:11 the value of hearts come from comes from the suits list
210:14 the suits list now we'll practice a for loop so add a
210:17 now we'll practice a for loop so add a for loop to the end of the code that
210:19 for loop to the end of the code that prints each suit
210:22 prints each suit and then we'll just test this out
210:24 and then we'll just test this out i really hope you actually are following
210:26 i really hope you actually are following along and trying it out right before i
210:28 along and trying it out right before i show it to you that's how you're going
210:30 show it to you that's how you're going to learn the best here so spades clubs
210:32 to learn the best here so spades clubs hearts diamonds
210:34 hearts diamonds now this next thing is is just to see if
210:37 now this next thing is is just to see if we can do it so it's not going to be
210:38 we can do it so it's not going to be part of our final code but right before
210:40 part of our final code but right before the loop we just added see if you can
210:42 the loop we just added see if you can add another item to the suits list
210:45 add another item to the suits list that's the string snakes
210:53 there's a few different ways to do it but we will use append snakes so this is
210:56 but we will use append snakes so this is just going to append the word snakes at
210:58 just going to append the word snakes at the end
210:59 the end of the list so if i run this we can now
211:02 of the list so if i run this we can now see snakes at the bottom
211:05 see snakes at the bottom okay now we're going to start the
211:06 okay now we're going to start the process of representing a full deck of
211:08 process of representing a full deck of cards with python code
211:10 cards with python code so we're going to actually get rid of a
211:12 so we're going to actually get rid of a lot of this we're going to get rid of
211:13 lot of this we're going to get rid of all this we're just going to have the
211:14 all this we're just going to have the suits and then we're going to have this
211:16 suits and then we're going to have this for loop at the bottom we're going to do
211:18 for loop at the bottom we're going to do a lot of refactoring as we go mainly for
211:20 a lot of refactoring as we go mainly for educational purposes but also so we can
211:23 educational purposes but also so we can get the a really good blackjack game so
211:25 get the a really good blackjack game so we have a list of suits after that we're
211:27 we have a list of suits after that we're going to create a list of ranks that's a
211:31 going to create a list of ranks that's a 2 3 4 5 6 7 8 9 10 j q
211:34 2 3 4 5 6 7 8 9 10 j q k
211:35 k [Music]
211:38 [Music] now before the suits list
211:40 now before the suits list create a new variable called cards and
211:43 create a new variable called cards and assign an empty list to the variable
211:52 you can an empty list is just two brackets with nothing inside
211:55 brackets with nothing inside now in the cards list there should be an
211:57 now in the cards list there should be an item for each card in the deck each item
211:59 item for each card in the deck each item in the suits list should be combined
212:01 in the suits list should be combined with each item in the ranks list for a
212:03 with each item in the ranks list for a total of 52 items or cards
212:07 total of 52 items or cards let's work our way up to that so first
212:09 let's work our way up to that so first we'll update the print statement in the
212:11 we'll update the print statement in the for loop so that it prints a list with
212:14 for loop so that it prints a list with two elements the first element should be
212:16 two elements the first element should be suit and the second should be the first
212:18 suit and the second should be the first element of the ranks lists so this
212:21 element of the ranks lists so this should print
212:22 should print an ace in every suit
212:29 so i'm going to update this so it's going to be a list with
212:32 going to be a list with suit
212:33 suit and ranks
212:35 and ranks the first item is going to be at index
212:38 the first item is going to be at index zero now let's print that out
212:45 so we got them these four right here now instead of just printing an ace in
212:47 now instead of just printing an ace in every suit let's print every rank in
212:50 every suit let's print every rank in every suit
212:51 every suit this can be done easily with a for loop
212:53 this can be done easily with a for loop nested within another for loop so inside
212:56 nested within another for loop so inside the for loop add another for loop that
212:59 the for loop add another for loop that loops through the ranks
213:01 loops through the ranks then update the print statement so that
213:03 then update the print statement so that it's not just printing the first element
213:07 it's not just printing the first element in the ranks list but it's printing the
213:09 in the ranks list but it's printing the rank from the for other for loop
213:14 rank from the for other for loop so let me show you what i mean
213:16 so let me show you what i mean we're going to do four
213:19 we're going to do four for rank and ranks
213:22 for rank and ranks and then
213:24 and then we have to make sure to indent this
213:26 we have to make sure to indent this print statement so it's inside this
213:28 print statement so it's inside this other for loop
213:29 other for loop and this is now just going to be
213:32 and this is now just going to be rank so it's going to print the suit and
213:34 rank so it's going to print the suit and rank and i'll just run that
213:37 rank and i'll just run that and now we with this nested for loop we
213:39 and now we with this nested for loop we have every card at every rank in every
213:42 have every card at every rank in every suit all 52 cards are printed as two
213:45 suit all 52 cards are printed as two item lists
213:47 item lists an element in a list can be another list
213:50 an element in a list can be another list so instead of printing 52
213:52 so instead of printing 52 two item lists
213:54 two item lists let's append those 52 cards to the cards
213:57 let's append those 52 cards to the cards list
213:58 list so we already have the cards list here
214:00 so we already have the cards list here it's empty but i'm going to do
214:03 it's empty but i'm going to do cards
214:05 cards dot
214:06 dot append
214:08 append and so we're appending
214:10 and so we're appending this item
214:12 this item all these items to the cards list
214:15 all these items to the cards list so let's check what the cards list looks
214:17 so let's check what the cards list looks like by printing out printing it out at
214:19 like by printing out printing it out at the bottom remember make sure this is
214:21 the bottom remember make sure this is not indented at all and we'll do print
214:24 not indented at all and we'll do print cards
214:26 cards and i'll run that
214:36 and then here it is here so this is the list it's not
214:38 list it's not one there's just a comma between each
214:40 one there's just a comma between each item in the list here
214:42 item in the list here you may notice that all the cards are in
214:44 you may notice that all the cards are in order in the cards lists
214:46 order in the cards lists for a game like this though the cards
214:48 for a game like this though the cards must be shuffled so to help with this
214:51 must be shuffled so to help with this import the random module at the top of
214:54 import the random module at the top of your code so that's just we just do
214:58 your code so that's just we just do import
215:00 import random
215:01 random [Music]
215:03 [Music] now we'll be able to use the the random
215:05 now we'll be able to use the the random module so this is going to import the
215:06 module so this is going to import the random module which contains a variety
215:08 random module which contains a variety of things related to random number
215:10 of things related to random number generation
215:11 generation and as you probably remember when you
215:12 and as you probably remember when you import a python module it allows you to
215:14 import a python module it allows you to use additional commands in your code
215:17 use additional commands in your code specifically we're going to be using the
215:19 specifically we're going to be using the random.shuffle function
215:21 random.shuffle function so right before at the end where it says
215:23 so right before at the end where it says print cards we're going to call
215:24 print cards we're going to call random.shuffle and pass in the cards
215:28 random.shuffle and pass in the cards list to that function
215:31 list to that function and then if i play this here or run the
215:34 and then if i play this here or run the program we can see that these are not in
215:36 program we can see that these are not in order anymore see ace of spades
215:39 order anymore see ace of spades three of spades king of diamonds jack of
215:42 three of spades king of diamonds jack of hearts so these are no longer in order
215:44 hearts so these are no longer in order because they've been shuffled now let's
215:46 because they've been shuffled now let's remove a single element from the cards
215:48 remove a single element from the cards list this is similar to dealing a card
215:51 list this is similar to dealing a card from a deck and this can be done with
215:53 from a deck and this can be done with the pop method
215:55 the pop method so after the cards are shuffled
215:58 so after the cards are shuffled let's create another card variable and
216:00 let's create another card variable and just
216:01 just pop off a card from the cards list and
216:04 pop off a card from the cards list and put it into that variable called card
216:06 put it into that variable called card and just print that card
216:09 and just print that card so i'll do card
216:10 so i'll do card equals cards dot pop
216:14 equals cards dot pop and then instead of printing all the
216:16 and then instead of printing all the cards i'm just going to print a single
216:18 cards i'm just going to print a single card i'll run the program
216:20 card i'll run the program see every time i run the program you can
216:22 see every time i run the program you can see we're getting a different card we're
216:24 see we're getting a different card we're dealing a different card because it's
216:26 dealing a different card because it's been shuffled
216:27 been shuffled so we've already learned all about
216:28 so we've already learned all about functions and now we're going to create
216:30 functions and now we're going to create a function
216:32 a function so create a
216:33 so create a function called shuffle that just has
216:36 function called shuffle that just has the single line that shuffles the cards
216:40 the single line that shuffles the cards so it's just def shuffle
216:43 so it's just def shuffle and then i just have to make sure this
216:44 and then i just have to make sure this is indented so now when we call the
216:47 is indented so now when we call the shuffle function it will shuffle the
216:49 shuffle function it will shuffle the cards
216:50 cards so right before the print statement
216:52 so right before the print statement call the shuffle function and instead of
216:54 call the shuffle function and instead of just printing the single card print the
216:57 just printing the single card print the cards
216:58 cards so do shuffle
217:01 so do shuffle and then i will print all the cards and
217:04 and then i will print all the cards and let's just try out the program
217:06 let's just try out the program and we can see there was a problem it's
217:09 and we can see there was a problem it's because we didn't put the the colon here
217:12 because we didn't put the the colon here so that's an important part of creating
217:14 so that's an important part of creating a function is putting the colon there
217:16 a function is putting the colon there now we'll create another function called
217:18 now we'll create another function called deal and we'll put this line inside the
217:21 deal and we'll put this line inside the the deal function
217:23 the deal function so we're going to define deal
217:26 so we're going to define deal and i'll put the colon this time and
217:27 and i'll put the colon this time and make sure to
217:29 make sure to indent that
217:30 indent that and we can see this has a orange
217:33 and we can see this has a orange squiggly line underneath it because
217:34 squiggly line underneath it because variables can only be accessed in the
217:36 variables can only be accessed in the context that they were created so the
217:38 context that they were created so the card variable will not be available
217:40 card variable will not be available outside of the deal function
217:42 outside of the deal function you can get a value out of a function by
217:44 you can get a value out of a function by returning a result using the return
217:46 returning a result using the return statement so at the end we're going to
217:49 statement so at the end we're going to return the card
217:54 okay now we've taken care of that squiggly line there so after the shuffle
217:57 squiggly line there so after the shuffle function is called we'll call the deal
217:59 function is called we'll call the deal function and assign the return value to
218:01 function and assign the return value to a variable named card then we'll update
218:04 a variable named card then we'll update the print function to print card instead
218:06 the print function to print card instead of cards
218:08 of cards so card
218:09 so card equals
218:11 equals deal
218:12 deal and then we'll just print
218:15 and then we'll just print the card
218:20 and again we see a different card every time we run
218:22 see a different card every time we run the program
218:24 the program what if you want the deal function to
218:25 what if you want the deal function to deal more than one card well let's
218:28 deal more than one card well let's refactor the deal function to and accept
218:30 refactor the deal function to and accept to accept an argument so any number of
218:32 to accept an argument so any number of arguments can appear inside the
218:34 arguments can appear inside the parentheses when a function is created
218:36 parentheses when a function is created separated by commas inside the function
218:39 separated by commas inside the function the arguments are assigned to variables
218:41 the arguments are assigned to variables called parameters so start by making it
218:44 called parameters so start by making it so we'll start by making it so the deal
218:45 so we'll start by making it so the deal function takes an argument named number
218:48 function takes an argument named number then we'll make sure when we call the
218:49 then we'll make sure when we call the function we use the new parameter
218:52 function we use the new parameter by
218:53 by making it so we're gonna deal two
218:56 making it so we're gonna deal two so i'm just gonna put number here it's
218:58 so i'm just gonna put number here it's gonna it's gonna deal a number of cards
219:01 gonna it's gonna deal a number of cards we're gonna deal two and i just didn't
219:03 we're gonna deal two and i just didn't say this before but now instead this is
219:05 say this before but now instead this is not one card anymore so we're going to
219:07 not one card anymore so we're going to update this to be cards dealt but
219:10 update this to be cards dealt but there's a special shortcut you can
219:12 there's a special shortcut you can either it's going to be
219:15 either it's going to be command or control d
219:17 command or control d and now i'm actually selecting the card
219:19 and now i'm actually selecting the card two different times see i i now have
219:21 two different times see i i now have multiple cursors here so basically i
219:24 multiple cursors here so basically i selected the word i double clicked to
219:26 selected the word i double clicked to select the word then did command or
219:27 select the word then did command or control d now it's selecting two words
219:30 control d now it's selecting two words and now i can type in
219:32 and now i can type in cards
219:34 cards delt
219:35 delt so now i can type in two places at one
219:37 so now i can type in two places at one time so that's a cool thing that you can
219:39 time so that's a cool thing that you can do in replit and you can do it in many
219:41 do in replit and you can do it in many other code editors and i'll run the
219:43 other code editors and i'll run the program but it should still only deal
219:45 program but it should still only deal one card because even though we're
219:46 one card because even though we're passing this parameter into here we're
219:48 passing this parameter into here we're not doing anything with it yet here so
219:51 not doing anything with it yet here so we want to update the deal function so
219:53 we want to update the deal function so it's going to return a list of cards
219:55 it's going to return a list of cards instead of a single card
219:57 instead of a single card in the first line of the function create
219:59 in the first line of the function create an empty list named cards delt then
220:02 an empty list named cards delt then update the last line of the function to
220:05 update the last line of the function to return cards dealt instead of return
220:07 return cards dealt instead of return card so let's do that really quick we're
220:10 card so let's do that really quick we're going to do
220:11 going to do cards
220:13 cards dealt
220:14 dealt is going to equal an empty list and i'll
220:17 is going to equal an empty list and i'll just copy that and paste it
220:19 just copy that and paste it right here
220:20 right here now do you remember how to use a the
220:22 now do you remember how to use a the range function with a for loop we talked
220:24 range function with a for loop we talked about it earlier in the course we just
220:26 about it earlier in the course we just briefly touched on it but let's create a
220:28 briefly touched on it but let's create a for loop that's going to
220:31 for loop that's going to add a card from the deck
220:34 add a card from the deck for each
220:35 for each card dealt
220:37 card dealt so we can do that by creating a for loop
220:40 so we can do that by creating a for loop for x and range
220:43 for x and range number now this is a common thing you're
220:46 number now this is a common thing you're going to be doing in python creating a
220:49 going to be doing in python creating a for loop that's going to be in range
220:51 for loop that's going to be in range number because now it's going to loop
220:54 number because now it's going to loop this many times it's going to loop this
220:56 this many times it's going to loop this many times which is the number we passed
220:57 many times which is the number we passed in here and we're going to do a few
221:00 in here and we're going to do a few things in this for loop
221:01 things in this for loop first we are going to
221:04 first we are going to actually do this what we already have
221:05 actually do this what we already have card equal cards dot pop and then we'll
221:08 card equal cards dot pop and then we'll do cards
221:10 do cards delt
221:12 delt dot append
221:15 dot append card
221:16 card so now just this card that we popped off
221:19 so now just this card that we popped off the deck we are appending it to the
221:20 the deck we are appending it to the card's delt and then we're returning the
221:23 card's delt and then we're returning the cards dealt here
221:26 cards dealt here so down here in the code
221:28 so down here in the code let's separate out a single card from
221:31 let's separate out a single card from the two cards dealt
221:33 the two cards dealt so let's create a variable called card
221:35 so let's create a variable called card and set it equal to the first item in
221:37 and set it equal to the first item in the cards delt list
221:40 the cards delt list and then we'll just print that card
221:41 and then we'll just print that card instead of cards dealt
221:43 instead of cards dealt so we are going to do card
221:46 so we are going to do card equals cards
221:49 equals cards delt and then we just use the brackets
221:52 delt and then we just use the brackets and put 0 to get the first item in that
221:56 and put 0 to get the first item in that list
221:56 list and then we'll just print
221:58 and then we'll just print a card
222:00 a card now i'm just going to test out the
222:01 now i'm just going to test out the program we're still just seeing a single
222:03 program we're still just seeing a single card here but it's doing a lot more
222:06 card here but it's doing a lot more behind the scenes now
222:08 behind the scenes now so now let's separate out the rank part
222:10 so now let's separate out the rank part of a single card so after we create the
222:13 of a single card so after we create the card there let's create a variable named
222:16 card there let's create a variable named rank and assign it the rank from the
222:18 rank and assign it the rank from the card
222:20 card so we'll do rank
222:22 so we'll do rank equals card
222:25 equals card and then i have to get index one because
222:28 and then i have to get index one because the rank is this that's the nine here
222:31 the rank is this that's the nine here the second item in this card is the rank
222:35 the second item in this card is the rank so each rank has a different value in
222:37 so each rank has a different value in blackjack the value of an ace or an a in
222:40 blackjack the value of an ace or an a in this in this program is 11
222:44 this in this program is 11 or sometimes it can actually be one it's
222:45 or sometimes it can actually be one it's going to be 11 or 1 but we'll get to the
222:47 going to be 11 or 1 but we'll get to the one part later so jack j q and k which
222:52 one part later so jack j q and k which is jack queen and king have the value of
222:54 is jack queen and king have the value of 10 and then the numbers have the value
222:56 10 and then the numbers have the value of the number so we need to check what
222:58 of the number so we need to check what the rank is and set the value depending
223:01 the rank is and set the value depending on the rank
223:02 on the rank so this is the perfect time for a
223:05 so this is the perfect time for a conditional statement specifically an if
223:07 conditional statement specifically an if statement
223:09 statement before the final print statement or
223:11 before the final print statement or program we're going to add an if
223:13 program we're going to add an if statement to check if the rank equals a
223:17 statement to check if the rank equals a and if so we'll assign 11 to a variable
223:20 and if so we'll assign 11 to a variable named value
223:23 named value so we'll do if
223:25 so we'll do if rank and i hope you remember if you're
223:27 rank and i hope you remember if you're flying along i hope you remember to use
223:28 flying along i hope you remember to use two equal signs instead of one equal
223:30 two equal signs instead of one equal sign here so if rank equals a
223:34 sign here so if rank equals a then value is going to equal with a
223:37 then value is going to equal with a single equal sign is going to equal
223:40 single equal sign is going to equal 11.
223:41 11. now if rank does not equal a we'll want
223:43 now if rank does not equal a we'll want to check if it equals j q or k
223:47 to check if it equals j q or k that can be done with an elif statement
223:50 that can be done with an elif statement for now we'll just create an if
223:52 for now we'll just create an if statement to check if the rank equals j
223:54 statement to check if the rank equals j and then if so we will set the value to
223:57 and then if so we will set the value to 10.
224:01 so we talked about the three logical operators and or
224:04 operators and or and not you can use these three
224:06 and not you can use these three operators in conditional statements to
224:08 operators in conditional statements to check multiple conditions at once so we
224:11 check multiple conditions at once so we want to check if
224:13 want to check if rank is j or rank is q
224:16 rank is j or rank is q or rank is k so update the code with the
224:20 or rank is k so update the code with the the and with the ors
224:25 now there can be any number of ls statements after an if statement but at
224:27 statements after an if statement but at the end there can only be a single else
224:29 the end there can only be a single else statement
224:30 statement and like we discussed the else is just
224:32 and like we discussed the else is just going to be if none of the other ones
224:34 going to be if none of the other ones are true so let's add an else statement
224:37 are true so let's add an else statement and inside we'll just assign rank to
224:39 and inside we'll just assign rank to value because we've already gotten all
224:40 value because we've already gotten all the letters out of the way the rest are
224:42 the letters out of the way the rest are numbers and we can assign it directly to
224:44 numbers and we can assign it directly to the value
224:49 now we'll instead of printing the card at the end
224:52 instead of printing the card at the end let's print the rank and the value
224:56 let's print the rank and the value so i can just type in rank comma value
224:59 so i can just type in rank comma value and
225:00 and when that multiple values in a print
225:02 when that multiple values in a print statement are listed with a comma
225:04 statement are listed with a comma separating them both values are printed
225:06 separating them both values are printed with a space in between so let's test
225:08 with a space in between so let's test this out a few times q10 five five 6 6
225:12 this out a few times q10 five five 6 6 so we can see every time we press it
225:14 so we can see every time we press it it's going to be a random rank and value
225:17 it's going to be a random rank and value now we already talked about dictionaries
225:19 now we already talked about dictionaries in python it's like a list but more
225:21 in python it's like a list but more general you can think of a dictionary as
225:23 general you can think of a dictionary as a mapping between a set of indices which
225:25 a mapping between a set of indices which are called keys and values so key value
225:28 are called keys and values so key value pairs each key maps to a value so above
225:31 pairs each key maps to a value so above the print statement let's create a
225:32 the print statement let's create a variable called rank
225:34 variable called rank underscore dict for dictionary and
225:37 underscore dict for dictionary and create a dictionary with two items
225:40 create a dictionary with two items a key value pair for the rank and a key
225:43 a key value pair for the rank and a key value pair for the value
225:52 so we have the string rank here and then the actual rank variable string value
225:54 the actual rank variable string value and the actual value variable
225:57 and the actual value variable before we are
225:59 before we are printing the rank variable and the value
226:01 printing the rank variable and the value variable but let's update this code so
226:04 variable but let's update this code so we're actually getting the rank and
226:05 we're actually getting the rank and value from the rank dictionary right
226:07 value from the rank dictionary right here
226:08 here so i'm going to copy that and then i
226:11 so i'm going to copy that and then i just pasted that but now i'm going to
226:13 just pasted that but now i'm going to use bracket notation
226:15 use bracket notation and so i'll put two brackets but then i
226:17 and so i'll put two brackets but then i also have to surround this in quotation
226:20 also have to surround this in quotation marks
226:21 marks and then i'm gonna
226:23 and then i'm gonna put the rank dictionary the brackets
226:26 put the rank dictionary the brackets and then the quotation marks because
226:28 and then the quotation marks because we're accessing that key there
226:30 we're accessing that key there and then i can just run the program and
226:33 and then i can just run the program and it's still doing the same thing as
226:34 it's still doing the same thing as before just a lot more complicated as
226:37 before just a lot more complicated as far as the code goes but it's going to
226:39 far as the code goes but it's going to be good to have more complicated code as
226:42 be good to have more complicated code as our program is going to become more
226:44 our program is going to become more complicated as we go
226:46 complicated as we go so when writing a program there are many
226:48 so when writing a program there are many ways to do almost everything
226:51 ways to do almost everything now we're going to refactor the code to
226:53 now we're going to refactor the code to get the value of each rank without using
226:56 get the value of each rank without using an if statement
226:58 an if statement instead we'll store both the rank name
227:00 instead we'll store both the rank name and value in the ranks list using
227:03 and value in the ranks list using dictionaries so let's delete all the
227:05 dictionaries so let's delete all the code lines of code after where it says
227:08 code lines of code after where it says shuffle
227:09 shuffle so
227:11 so here i know we typed in a lot of stuff
227:12 here i know we typed in a lot of stuff there but it was just kind of to
227:15 there but it was just kind of to practice and now we're going to practice
227:16 practice and now we're going to practice a different method of doing this
227:19 a different method of doing this so now let's create a new card variable
227:22 so now let's create a new card variable a new variable called card at the end
227:24 a new variable called card at the end and let's assign to the card variable
227:27 and let's assign to the card variable a a single card that will deal from the
227:29 a a single card that will deal from the deck but we'll make sure that card is
227:31 deck but we'll make sure that card is not
227:32 not in a list so this is a little tricky i'm
227:35 in a list so this is a little tricky i'm gonna do deal and i'll deal one card
227:39 gonna do deal and i'll deal one card but now i have to get
227:41 but now i have to get the first item so this is going to deal
227:44 the first item so this is going to deal one card but the one card is going to
227:47 one card but the one card is going to deal is going to be in a list so i want
227:50 deal is going to be in a list so i want to get the first item in the list which
227:52 to get the first item in the list which is going to be the only item in the list
227:54 is going to be the only item in the list so i had to put the zero in brackets
227:56 so i had to put the zero in brackets here to get that card out of a list
227:59 here to get that card out of a list before it goes into the card variable
228:02 before it goes into the card variable now we're going to update the ranks list
228:04 now we're going to update the ranks list so here's the the ranks list each
228:07 so here's the the ranks list each element the list should now be a
228:09 element the list should now be a dictionary when lists or list elements
228:11 dictionary when lists or list elements are long it's common to put each element
228:13 are long it's common to put each element on its own line so we're going to put
228:14 on its own line so we're going to put each element on its own line and each
228:17 each element on its own line and each element is going to have the rank and
228:18 element is going to have the rank and the value so for instance it will be
228:20 the value so for instance it will be rank a value 11
228:23 rank a value 11 rank 2 value 2.
228:27 rank 2 value 2. so it's going to look like
228:29 so it's going to look like this
228:31 this and i'm now i'm actually going to zoom
228:32 and i'm now i'm actually going to zoom out just a little bit and we have all
228:36 out just a little bit and we have all these they're all these ranks and each
228:38 these they're all these ranks and each one in this list
228:40 one in this list is a dictionary each element in the list
228:42 is a dictionary each element in the list is a dictionary
228:43 is a dictionary okay now that this is updated let's go
228:45 okay now that this is updated let's go down and just print a card so we can see
228:48 down and just print a card so we can see now that we've updated that ranks list
228:50 now that we've updated that ranks list so print
228:52 so print card
228:54 card okay so this is what it's going to look
228:56 okay so this is what it's going to look like coming from
228:58 like coming from our list
228:59 our list so we got the suit
229:01 so we got the suit and then we have the rank that's also
229:03 and then we have the rank that's also going to have the value here the rank
229:05 going to have the value here the rank and the value
229:07 and the value we can see every time we click it we get
229:09 we can see every time we click it we get a random item
229:12 a random item now let's update the code so instead of
229:14 now let's update the code so instead of printing the whole card we just
229:17 printing the whole card we just print the value so in this example the
229:20 print the value so in this example the value is two so we just want to print
229:23 value is two so we just want to print this to just that that value so how can
229:26 this to just that that value so how can we update this see if you can figure out
229:28 we update this see if you can figure out how to update this line so only prints
229:31 how to update this line so only prints just the value number there
229:34 just the value number there so first of all we have to see that
229:36 so first of all we have to see that we're in a list and we need so this is
229:38 we're in a list and we need so this is the first element of the list this is
229:40 the first element of the list this is the second element so wait to start by
229:42 the second element so wait to start by getting the second element of the list
229:45 getting the second element of the list which is
229:46 which is index one
229:48 index one and then
229:49 and then we have an object here or a dictionary i
229:52 we have an object here or a dictionary i mean and we need to get
229:54 mean and we need to get so here we have this key value pair so
229:56 so here we have this key value pair so we need the value at that key so to get
229:59 we need the value at that key so to get the value of that key we are going to
230:00 the value of that key we are going to put more brackets and i'm going to put
230:02 put more brackets and i'm going to put value the key of value so now with that
230:06 value the key of value so now with that should work let's try it
230:08 should work let's try it okay nine
230:09 okay nine seven see every time it's gonna just
230:11 seven see every time it's gonna just give us the value of the card
230:14 give us the value of the card now we'll start defining classes that
230:16 now we'll start defining classes that will be used in order to separate out
230:18 will be used in order to separate out different aspects of the game
230:21 different aspects of the game so classes you may remember provide a
230:23 so classes you may remember provide a way of bundling data and functionality
230:25 way of bundling data and functionality together creating a new class creates a
230:27 together creating a new class creates a new type of object allowing new
230:29 new type of object allowing new instances of that type to be made an
230:31 instances of that type to be made an object can contain a number of functions
230:34 object can contain a number of functions which we call methods as well as data
230:35 which we call methods as well as data that is used by those functions called
230:37 that is used by those functions called attributes so
230:39 attributes so we're going to use classes to model
230:41 we're going to use classes to model three parts of the game a card a deck
230:44 three parts of the game a card a deck and a hand so far we've mainly worked on
230:46 and a hand so far we've mainly worked on the elements of the debt class
230:49 the elements of the debt class so right after this import statement at
230:52 so right after this import statement at the top
230:53 the top we're going to
230:55 we're going to make a class a class called
230:58 make a class a class called dec and we're going to put everything
231:00 dec and we're going to put everything that we've written so far in that class
231:04 that we've written so far in that class so we're just gonna do class
231:08 deck colon
231:09 colon okay now we just highlight
231:11 okay now we just highlight everything here and then i'm gonna press
231:14 everything here and then i'm gonna press tab
231:14 tab to put everything in the class of deck
231:17 to put everything in the class of deck because everything's
231:19 because everything's indented a little bit
231:20 indented a little bit and then these last few lines of code we
231:23 and then these last few lines of code we don't need so i'll just delete those
231:25 don't need so i'll just delete those those are just for testing out
231:27 those are just for testing out a class is like a template you can use
231:30 a class is like a template you can use that class to create an instance of the
231:32 that class to create an instance of the class called an object
231:33 class called an object then you can use the instance each
231:36 then you can use the instance each instance keeps track of its own state so
231:38 instance keeps track of its own state so you can update an instance created from
231:39 you can update an instance created from a class and it won't impact other
231:41 a class and it won't impact other objects created from the same class soon
231:44 objects created from the same class soon you'll see an example of all this to
231:45 you'll see an example of all this to make it easier to understand
231:47 make it easier to understand but first let's prepare a class to
231:50 but first let's prepare a class to create an instance from it
231:52 create an instance from it when you create an instance of a class
231:54 when you create an instance of a class python automatically calls a function
231:56 python automatically calls a function also called a method in the clast named
231:58 also called a method in the clast named init remember we already discussed this
232:01 init remember we already discussed this earlier in the course so the contents of
232:04 earlier in the course so the contents of this init method should be code that is
232:06 this init method should be code that is run one time to initialize the instance
232:10 run one time to initialize the instance so at the beginning of our class let's
232:13 so at the beginning of our class let's create this init function so we'll do
232:17 create this init function so we'll do def
232:18 def underscore underscore init
232:21 underscore underscore init underscore underscore and if you
232:22 underscore underscore and if you remember from before we always have to
232:24 remember from before we always have to pass in
232:25 pass in self
232:26 self to all of these
232:28 to all of these functions in a class because then it
232:30 functions in a class because then it gets itself is referring to the instance
232:34 gets itself is referring to the instance of the class that we've developed now
232:36 of the class that we've developed now we're going to indent all the code
232:37 we're going to indent all the code that's not part of the shuffle or deal
232:39 that's not part of the shuffle or deal function so the code will be part of
232:41 function so the code will be part of this new function so i'm just going to
232:43 this new function so i'm just going to highlight
232:44 highlight all this here
232:46 all this here including
232:47 including the suits here and then just press
232:50 the suits here and then just press tab
232:51 tab so like i said we just added self in
232:53 so like i said we just added self in here you should always
232:54 here you should always all the methods in a class or all the
232:56 all the methods in a class or all the functions should have self
232:59 functions should have self anything inside the parentheses remember
233:01 anything inside the parentheses remember is called an argument their variables
233:03 is called an argument their variables pass them from the color to the
233:04 pass them from the color to the functions
233:05 functions as i've said all functions in a class
233:07 as i've said all functions in a class should receive self as an argument and
233:10 should receive self as an argument and self represents the instance of the
233:12 self represents the instance of the class by using the self keyword the
233:14 class by using the self keyword the function can access the attributes and
233:16 function can access the attributes and methods of the class
233:18 methods of the class so let's make sure to add self as the
233:21 so let's make sure to add self as the first item in the parentheses of the
233:23 first item in the parentheses of the other functions
233:25 other functions so we are going to add self here
233:28 so we are going to add self here and then see how we already have number
233:30 and then see how we already have number here but we're going to hit self at the
233:31 here but we're going to hit self at the beginning
233:32 beginning and we so we can still
233:34 and we so we can still call this function with just
233:36 call this function with just a single number but it's going to also
233:39 a single number but it's going to also get a reference to the instance here now
233:42 get a reference to the instance here now i want you to notice that
233:44 i want you to notice that the cards here is underlying in
233:46 the cards here is underlying in red so before it wasn't when we were
233:49 red so before it wasn't when we were before we made this into a class we
233:51 before we made this into a class we could just access
233:53 could just access this cards variable but now we cannot so
233:56 this cards variable but now we cannot so let's fix that
233:58 let's fix that inside a class in order to access a
234:00 inside a class in order to access a variable in multiple functions also
234:03 variable in multiple functions also called methods the variable has to start
234:05 called methods the variable has to start with self dot
234:07 with self dot so we're going to change all instances
234:10 so we're going to change all instances of cards in every function to self.card
234:14 of cards in every function to self.card starting
234:15 starting with this so self dot cards
234:19 with this so self dot cards now this is going to make it so we can
234:20 now this is going to make it so we can access it in other places and then we'll
234:22 access it in other places and then we'll change this to self dot cards
234:26 change this to self dot cards and then this is self
234:31 dot cards and then self
234:33 and then self dot cards
234:39 so now this will be a variable that's specifically associated with the
234:41 specifically associated with the instance of the deck that's created and
234:44 instance of the deck that's created and then we can access it in all of these
234:47 then we can access it in all of these other methods
234:48 other methods okay we can now create an instance also
234:51 okay we can now create an instance also called an object of the deck class so at
234:54 called an object of the deck class so at the very end of the code
234:56 the very end of the code let's create a variable called deck 1
234:59 let's create a variable called deck 1 and make it an instance of the deck
235:01 and make it an instance of the deck class
235:06 so to make sure i'm not indented at all and i'll do deck
235:08 and i'll do deck 1
235:09 1 equals
235:15 deck there we go now since we created cards with
235:17 now since we created cards with self.cards
235:19 self.cards we can access that we can access cards
235:23 we can access that we can access cards from the instance of the class so let's
235:25 from the instance of the class so let's just print out the cards from our deck
235:29 just print out the cards from our deck one so do print
235:32 one so do print deck
235:33 deck one dot cards
235:40 and we can try that out now you can see the the list of all of these cards
235:43 the the list of all of these cards it has the suit
235:45 it has the suit and the rank and the value for each card
235:48 and the rank and the value for each card so underneath where we created deck one
235:50 so underneath where we created deck one let's create deck two we'll create
235:52 let's create deck two we'll create another instance of another deck
235:54 another instance of another deck so
235:56 so [Music]
236:00 [Music] so now we can call methods on these
236:03 so now we can call methods on these instances and you see some of the
236:06 instances and you see some of the methods we have we have shuffle and deal
236:08 methods we have we have shuffle and deal so on deck 2 right after we create the
236:10 so on deck 2 right after we create the deck 2 let's shuffle the deck so deck
236:15 deck 2 let's shuffle the deck so deck 2 dot shuffle
236:17 2 dot shuffle and then i have to make sure to put the
236:19 and then i have to make sure to put the parentheses
236:21 parentheses at the end here
236:23 at the end here right if we print deck one let's print
236:24 right if we print deck one let's print deck two or the cards of deck two so i'm
236:27 deck two or the cards of deck two so i'm gonna copy that and then we'll print
236:29 gonna copy that and then we'll print deck two cards so now we should see that
236:31 deck two cards so now we should see that the deck one cards are not shuffled and
236:33 the deck one cards are not shuffled and the deck two cards are shuffled so let
236:35 the deck two cards are shuffled so let me move this over here i'm gonna run the
236:37 me move this over here i'm gonna run the program
236:38 program and let's see if we can see that where
236:40 and let's see if we can see that where deck one so here's where here's deck one
236:43 deck one so here's where here's deck one and we can see how it's all diamonds
236:46 and we can see how it's all diamonds diamonds diamonds diamonds diamonds all
236:48 diamonds diamonds diamonds diamonds all the diamonds are in a row because
236:49 the diamonds are in a row because unshuffled but then if we go into deck 2
236:52 unshuffled but then if we go into deck 2 we can see we have diamonds clubs spades
236:55 we can see we have diamonds clubs spades diamonds hearts so these are shuffled in
236:58 diamonds hearts so these are shuffled in deck 2 they are shuffled
237:01 deck 2 they are shuffled okay the deck works
237:04 okay the deck works now let's add safeguards to prevent
237:06 now let's add safeguards to prevent errors every time the deal function is
237:08 errors every time the deal function is called a card is removed from the cards
237:11 called a card is removed from the cards list
237:12 list you can only remove a card if there are
237:15 you can only remove a card if there are cards to remove
237:16 cards to remove so before the program tries to pop a
237:19 so before the program tries to pop a card off self.cards is to check if the
237:23 card off self.cards is to check if the length of self.cards is greater than
237:25 length of self.cards is greater than zero
237:26 zero remember you can get the number of items
237:28 remember you can get the number of items in a list with length so see if you can
237:31 in a list with length so see if you can figure that out on your own and then i'm
237:33 figure that out on your own and then i'm about to show you how it's done
237:36 about to show you how it's done so when it's going to deal here
237:38 so when it's going to deal here right as we're dealing we're going to
237:40 right as we're dealing we're going to add an if statement here so if
237:43 add an if statement here so if the length
237:45 the length of self dot cards
237:52 is greater than zero and we do we don't need this parentheses
237:55 and we do we don't need this parentheses here so if the length of self.cards is
237:58 here so if the length of self.cards is greater than zero
238:00 greater than zero then we will
238:03 then we will do this we'll pop up a card and add it
238:06 do this we'll pop up a card and add it to the cards dealt if not we just won't
238:09 to the cards dealt if not we just won't do anything and then we'll return cards
238:11 do anything and then we'll return cards dealt which could be an empty array if
238:14 dealt which could be an empty array if there were no cards on the deck
238:16 there were no cards on the deck now let's add something to the shuffle
238:19 now let's add something to the shuffle function a deck with only one card does
238:22 function a deck with only one card does not need to be shuffled so let's add the
238:24 not need to be shuffled so let's add the appropriate if statement to the shuffle
238:26 appropriate if statement to the shuffle function
238:28 function so we'll do
238:29 so we'll do if the length of self
238:32 if the length of self dot cards
238:33 dot cards is
238:35 is greater than one
238:37 greater than one then we will shuffle and then make sure
238:39 then we will shuffle and then make sure i'll make sure to put the
238:41 i'll make sure to put the colon there
238:42 colon there okay
238:45 okay since a card is a separate concept than
238:47 since a card is a separate concept than a deck
238:48 a deck next we'll make a card class
238:51 next we'll make a card class so let's create a card class with an
238:54 so let's create a card class with an init function and in that init function
238:57 init function and in that init function we'll set self.suit to equal hearts
239:05 so hopefully you already tried this i'm going to do class
239:08 going to do class card
239:10 card and then i will do def
239:13 and then i will do def net
239:14 net [Music]
239:16 [Music] and then after the suit will lose self
239:17 and then after the suit will lose self dot rank and set it to a
239:23 so currently anytime a card is created it will be an ace of hearts let's
239:26 it will be an ace of hearts let's refactor the code so the suit and rank
239:28 refactor the code so the suit and rank are specified when a card object is
239:31 are specified when a card object is constructed so the init method can take
239:34 constructed so the init method can take additional parameters besides self that
239:36 additional parameters besides self that are passed into it as objects is
239:39 are passed into it as objects is constructed so we'll update it to take
239:41 constructed so we'll update it to take suit and rank
239:42 suit and rank [Music]
239:44 [Music] now we'll create a special method
239:47 now we'll create a special method that's underscore underscore str
239:50 that's underscore underscore str underscore underscore
239:56 when a class has this specific method it's called when print is invoked on an
239:59 it's called when print is invoked on an object from the class
240:01 object from the class so we want to make it so when we print
240:04 so we want to make it so when we print an object from the card class it will
240:07 an object from the card class it will print something like 10 of hearts or
240:10 print something like 10 of hearts or three of clubs or something like that
240:14 three of clubs or something like that so we don't do print here we do return
240:16 so we don't do print here we do return it's going to return this to the print
240:18 it's going to return this to the print statement it's going to turn self dot
240:20 statement it's going to turn self dot rank
240:22 rank and then we have to get
240:23 and then we have to get the rank
240:25 the rank [Music]
240:28 [Music] and we do plus
240:30 and we do plus and then of or to put a string there
240:33 and then of or to put a string there plus
240:35 plus self dot suit
240:38 self dot suit so now it's going to return the rank
240:40 so now it's going to return the rank which is like 2 or a of and then the
240:44 which is like 2 or a of and then the suit which is one of these
240:46 suit which is one of these so let's just try it out real quick and
240:48 so let's just try it out real quick and we go to the bottom we don't need any of
240:49 we go to the bottom we don't need any of these to test because we're testing
240:51 these to test because we're testing something completely different now let's
240:53 something completely different now let's do card one
240:55 do card one equals
240:56 equals [Music]
240:57 [Music] card i'm going to create a card and i
240:59 card i'm going to create a card and i have to pass in remember i have to first
241:01 have to pass in remember i have to first pass in the suit so how about hearts and
241:04 pass in the suit so how about hearts and then i have to pass in the rank but we
241:07 then i have to pass in the rank but we want to make it look like these ranks so
241:09 want to make it look like these ranks so i'm just going to
241:10 i'm just going to copy one of these here
241:16 and then after we create the card i can just
241:18 after we create the card i can just print
241:20 print card one
241:22 card one let me clear this and then i'll just run
241:24 let me clear this and then i'll just run that j of hearts
241:26 that j of hearts oh see i got the j of hearts and feel
241:29 oh see i got the j of hearts and feel free to add a few more cards like this
241:31 free to add a few more cards like this and test out a few more if you want
241:33 and test out a few more if you want okay now we're going to refactor this
241:35 okay now we're going to refactor this slightly you remember way toward the
241:37 slightly you remember way toward the beginning of this course we talked about
241:39 beginning of this course we talked about f strings so f strings allow us to put
241:43 f strings so f strings allow us to put variables right within a string do you
241:45 variables right within a string do you remember how to do that let's see if you
241:47 remember how to do that let's see if you can update this to use enough string
241:49 can update this to use enough string so first we're going to create a new
241:51 so first we're going to create a new string but we're going to start with the
241:52 string but we're going to start with the letter f
241:54 letter f and then
241:55 and then inside this string we put curly braces
241:59 inside this string we put curly braces around
242:00 around the
242:01 the python code and we don't need these
242:03 python code and we don't need these other strings here so
242:12 and then an ending string here
242:15 ending string here okay it's still showing these um red
242:18 okay it's still showing these um red squiggly lines because if i have a
242:20 squiggly lines because if i have a double quote around the strings and
242:22 double quote around the strings and anytime other quotes are in the middle i
242:23 anytime other quotes are in the middle i have to put a different type of quote so
242:24 have to put a different type of quote so we're going to use
242:26 we're going to use single quotes okay so now we can make
242:29 single quotes okay so now we can make this a whole a string but we use the
242:31 this a whole a string but we use the brackets to put the variables right
242:34 brackets to put the variables right within the string so now we've updated
242:37 within the string so now we've updated that to use an f string
242:39 that to use an f string so currently in the deck class the last
242:42 so currently in the deck class the last line of this init method
242:47 line of this init method appends a list as an item to the cards
242:50 appends a list as an item to the cards list
242:52 list instead of appending
242:53 instead of appending suit
242:54 suit comma rank
242:56 comma rank we'll create and append an instance of
242:59 we'll create and append an instance of the card class
243:01 the card class then afterwards when a deck is created
243:02 then afterwards when a deck is created it's filled with cards
243:04 it's filled with cards so it's just like this we're just going
243:06 so it's just like this we're just going to delete that i'll put card
243:09 to delete that i'll put card and then i'll pass in a suit and they
243:11 and then i'll pass in a suit and they rank so now we're passing in
243:14 rank so now we're passing in card instances
243:16 card instances so we're done with the deck and card
243:18 so we're done with the deck and card classes and we created them in such a
243:21 classes and we created them in such a way that they could basically be used
243:23 way that they could basically be used for any card game
243:25 for any card game now let's make a hand class
243:28 now let's make a hand class this will represent a hand in the game
243:30 this will represent a hand in the game of blackjack so create a hand class and
243:32 of blackjack so create a hand class and add an inet method and initialize a
243:35 add an inet method and initialize a variable called self.cards that is set
243:38 variable called self.cards that is set to an empty list so let's go down here
243:41 to an empty list so let's go down here and we can also get rid of all this test
243:43 and we can also get rid of all this test code here
243:44 code here so the new class is called hand
243:50 and we'll also make the hand keep track of the value of the hands a self.value
243:53 of the value of the hands a self.value will start it at zero
243:59 in this blackjack game there will be a human controlled player and a program
244:01 human controlled player and a program controlled dealer so let's add a dealer
244:04 controlled dealer so let's add a dealer parameter in the init constructor method
244:06 parameter in the init constructor method of the hand class
244:08 of the hand class and then when the hand classes create a
244:09 and then when the hand classes create a dealer should be set to true or false to
244:12 dealer should be set to true or false to keep track of what type of hand it is
244:14 keep track of what type of hand it is so i'll pass in the parameter dealer and
244:18 so i'll pass in the parameter dealer and then we just have to create a variable
244:20 then we just have to create a variable and call dealer and set it to dealer so
244:22 and call dealer and set it to dealer so self dot dealer
244:25 self dot dealer equals dealer
244:26 equals dealer [Music]
244:27 [Music] if you remember from before function
244:29 if you remember from before function parameters can have default values so we
244:31 parameters can have default values so we want to make it so the default value of
244:33 want to make it so the default value of dealer is false
244:35 dealer is false so then if
244:37 so then if we create a hand and we don't set the
244:39 we create a hand and we don't set the dealer value it will automatically be
244:41 dealer value it will automatically be false
244:42 false and i'm just going to take out these
244:43 and i'm just going to take out these spaces here to make it smaller here
244:46 spaces here to make it smaller here so now a hand can be created
244:49 so now a hand can be created let's give it some functionality we'll
244:51 let's give it some functionality we'll add an add card method and the method
244:53 add an add card method and the method should take a card list as a parameter
245:02 and then we need to add that card list to the cards
245:04 to the cards so we can use the extend function the
245:07 so we can use the extend function the extend method to append each item in
245:09 extend method to append each item in card list onto the cards list so it's
245:12 card list onto the cards list so it's just going to look like this self dot
245:14 just going to look like this self dot cards dot append no extend i mean dot
245:18 cards dot append no extend i mean dot extend
245:20 extend and then we pass in
245:22 and then we pass in card list
245:23 card list now let's just add some code to test out
245:26 now let's just add some code to test out what we have so far so let's create a
245:28 what we have so far so let's create a deck and then we will shuffle the deck
245:32 deck and then we will shuffle the deck deck.shuffle
245:34 deck.shuffle [Music]
245:37 [Music] now we'll create a hand
245:43 now we can add cards to the hand so hand dot
245:44 dot add
245:46 add card
245:52 and we will deck dot deal we'll deal two cards into the hand
245:54 cards into the hand and then we'll just print
245:57 and then we'll just print hand cards
246:04 okay so this is what how it printed out i was expecting this to look a little
246:06 i was expecting this to look a little different
246:07 different because of this function it print it
246:10 because of this function it print it should print like that
246:12 should print like that but i think the reason is
246:14 but i think the reason is because
246:15 because this is a list so it's printing a list
246:17 this is a list so it's printing a list not an individual card so let's change
246:20 not an individual card so let's change this to print an individual card i'll
246:22 this to print an individual card i'll print the first card so put zero in
246:24 print the first card so put zero in there i'll try it again nine of diamonds
246:27 there i'll try it again nine of diamonds and then we can also print the next card
246:31 and then we can also print the next card three of hearts
246:33 three of hearts and then we can also print both cards if
246:35 and then we can also print both cards if we just copy that and do
246:38 we just copy that and do hand that cards
246:39 hand that cards zero
246:40 zero handout cards
246:42 handout cards one
246:44 one okay ace of hearts and nine of spades so
246:46 okay ace of hearts and nine of spades so those are the two cards that were dealt
246:47 those are the two cards that were dealt to the hand
246:48 to the hand now we'll go back to the hand class and
246:51 now we'll go back to the hand class and we'll add the ability to calculate the
246:53 we'll add the ability to calculate the value of a hand so let's add a method
246:56 value of a hand so let's add a method called calculate value
246:58 called calculate value and inside the method we'll set
247:00 and inside the method we'll set self.value to zero
247:07 now we'll take this one step at a time first let's let's make a for loop that's
247:09 first let's let's make a for loop that's going to go through every single card
247:12 going to go through every single card and inside the for loop we'll just set
247:15 and inside the for loop we'll just set the value
247:16 the value of the card to a variable called card
247:19 of the card to a variable called card underscore value so i'll do four card
247:24 underscore value so i'll do four card and self.cards
247:30 okay so we're not doing anything with that yet but we're going to in a second
247:32 that yet but we're going to in a second here
247:32 here now we want to make sure that this is an
247:35 now we want to make sure that this is an integer
247:36 integer so let's convert that to an integer if
247:38 so let's convert that to an integer if you remember you just use int
247:40 you remember you just use int [Music]
247:43 [Music] and then put it in print int and then
247:45 and then put it in print int and then inside the parentheses we put this value
247:48 inside the parentheses we put this value not just getting the card value for each
247:50 not just getting the card value for each card is not enough something must be
247:52 card is not enough something must be done with the variable so let's add that
247:56 done with the variable so let's add that value
247:57 value to
247:58 to self.value
248:01 self.value so we'll do self
248:03 so we'll do self dot value
248:05 dot value and then if you remember from before we
248:06 and then if you remember from before we can use the plus equals to
248:09 can use the plus equals to add that to the current value those will
248:11 add that to the current value those will do a card
248:13 do a card value
248:15 value so as you may know in blackjack and ace
248:17 so as you may know in blackjack and ace can have the value of either 11 or 1
248:20 can have the value of either 11 or 1 depending on what is better for the
248:22 depending on what is better for the player so there's a few ways to
248:24 player so there's a few ways to implement that in code so we're going to
248:26 implement that in code so we're going to do one way that's relatively simple
248:29 do one way that's relatively simple first we'll check if the hand has an ace
248:32 first we'll check if the hand has an ace so let's first create a variable that
248:33 so let's first create a variable that will store whether the
248:36 will store whether the hand has an a so just be called hand has
248:38 hand has an a so just be called hand has underscore ace we'll set to false and
248:41 underscore ace we'll set to false and we'll put it right under here
248:42 we'll put it right under here so we'll do
248:44 so we'll do has ace and we'll set to false and since
248:48 has ace and we'll set to false and since we're only going to be using has ace
248:50 we're only going to be using has ace within
248:51 within this
248:52 this method we don't need to use self that
248:54 method we don't need to use self that has ace because we're only using it here
248:58 has ace because we're only using it here and now when we're going through the the
249:00 and now when we're going through the the list of cards let's check if the the
249:03 list of cards let's check if the the rank of a card is an ace and then set
249:05 rank of a card is an ace and then set has aced equals true
249:07 has aced equals true so i'll do it
249:09 so i'll do it if
249:11 if card dot rank
249:14 card dot rank the rank is going to be equal double
249:16 the rank is going to be equal double equal sign if it equals ace
249:23 after this entire for loop we're going to check if the card has an
249:25 we're going to check if the card has an ace
249:26 ace and if the value is over 21
249:30 and if the value is over 21 if so then we'll just subtract 10 from
249:32 if so then we'll just subtract 10 from the value
249:33 the value because that'll be the same as setting
249:36 because that'll be the same as setting the ace to equal one instead of 11.
249:39 the ace to equal one instead of 11. so we'll just do if has
249:41 so we'll just do if has ace
249:54 is greater than 21 to self.value
249:56 to self.value minus equals
249:59 minus equals 10.
250:00 10. okay and look at this this is something
250:02 okay and look at this this is something i don't think i've discussed yet
250:04 i don't think i've discussed yet you could say if has
250:07 you could say if has equals true and self.value is greater
250:10 equals true and self.value is greater than 21.
250:11 than 21. but you can also it's like a shorthand
250:14 but you can also it's like a shorthand you don't have to say if has ace equals
250:16 you don't have to say if has ace equals true
250:17 true if has a because has ace is just going
250:19 if has a because has ace is just going to equal true or false you can just say
250:21 to equal true or false you can just say if has ace so that's just the same as
250:23 if has ace so that's just the same as saying if true or if false
250:26 saying if true or if false and so we're seeing if both of these
250:29 and so we're seeing if both of these evaluate the true then we will subtract
250:32 evaluate the true then we will subtract 10 from the value okay now we'll just
250:34 10 from the value okay now we'll just add another method to get the value of a
250:37 add another method to get the value of a hand called getvalue and the function
250:40 hand called getvalue and the function will just return self.value
250:42 will just return self.value so we're going to make sure that we're
250:44 so we're going to make sure that we're not we're indented correctly and do def
250:47 not we're indented correctly and do def get
250:49 get value
250:52 value return
250:53 return self
250:54 self dot value
250:58 and then i have to make sure i put the parentheses here
251:01 parentheses here and then i have to remember to put self
251:03 and then i have to remember to put self since this is a self.value we could call
251:06 since this is a self.value we could call down here like we could call
251:08 down here like we could call hand
251:09 hand value to get the value but it's
251:12 value to get the value but it's generally better to make a function to
251:14 generally better to make a function to return the value so i can do get
251:16 return the value so i can do get value that way there may be some extra
251:20 value that way there may be some extra code you want to run in there like
251:22 code you want to run in there like depending on different conditions
251:24 depending on different conditions you may want to modify the value before
251:27 you may want to modify the value before you return it
251:28 you return it so it's best practice to create a method
251:31 so it's best practice to create a method that will get a value like this for you
251:40 so currently this value that's returned could be incorrect
251:42 could be incorrect because
251:44 because if someone's going to get the value the
251:46 if someone's going to get the value the value has to be calculated correctly
251:49 value has to be calculated correctly first and like checking for aces and and
251:51 first and like checking for aces and and other things so let's call
251:54 other things so let's call let's calculate the value before we
251:56 let's calculate the value before we return the value so i'm going to do self
251:59 return the value so i'm going to do self dot calculate value so
252:02 dot calculate value so this is something that i think is new
252:05 this is something that i think is new where to call calculate value from
252:08 where to call calculate value from within this we're going to have to call
252:10 within this we're going to have to call self.calculatevalue
252:12 self.calculatevalue and self will refer to the instance that
252:15 and self will refer to the instance that we're working with so we're calling the
252:16 we're working with so we're calling the calculate value on the instance that's
252:20 calculate value on the instance that's that is the the hand instance and we're
252:22 that is the the hand instance and we're getting the value and then we're
252:23 getting the value and then we're returning the value
252:26 returning the value okay let's create another method called
252:28 okay let's create another method called is blackjack and it'll return true if
252:31 is blackjack and it'll return true if there's a blackjack and false otherwise
252:34 there's a blackjack and false otherwise so it's a blackjack if the value is 21.
252:39 so it's a blackjack if the value is 21. so i'm gonna do
252:40 so i'm gonna do def
252:41 def get or is
252:45 get or is oh and put self here
252:48 oh and put self here okay so this is going to evaluate you to
252:50 okay so this is going to evaluate you to either true or false and return true or
252:53 either true or false and return true or false depending on whether there's a
252:54 false depending on whether there's a blackjack
252:55 blackjack now we'll create the final method in the
252:57 now we'll create the final method in the hand class that will display information
252:59 hand class that will display information about the hand so let's create a method
253:01 about the hand so let's create a method called display they'll
253:03 called display they'll to start with will just print your hand
253:06 to start with will just print your hand [Music]
253:08 [Music] okay now let's do a quick refactor
253:10 okay now let's do a quick refactor instead of saying your hand it should
253:12 instead of saying your hand it should either say dealer's hand or your hand
253:17 either say dealer's hand or your hand depending on whether
253:20 depending on whether self.dealer
253:21 self.dealer is
253:22 is true or not
253:23 true or not so we're going to you to to do this all
253:26 so we're going to you to to do this all in one line we're going to use a few
253:29 in one line we're going to use a few things that we learned about earlier
253:32 things that we learned about earlier including
253:33 including ternary operators
253:35 ternary operators f strings and going between double
253:38 f strings and going between double quotes and single quotes and then one
253:41 quotes and single quotes and then one other new thing we are going to make
253:44 other new thing we are going to make this into an f string
253:47 this into an f string and then we are going to be using
253:50 and then we are going to be using actually
253:51 actually single quotes and double quotes within
253:54 single quotes and double quotes within this f string so if you want to use
253:56 this f string so if you want to use single quotes and double quotes within a
253:59 single quotes and double quotes within a string
254:00 string then you can surround it with a triple
254:04 then you can surround it with a triple single quote so i'm going to delete this
254:07 single quote so i'm going to delete this quote and just do three single quotes
254:09 quote and just do three single quotes and then delete this quote and do three
254:12 and then delete this quote and do three single quotes and so
254:14 single quotes and so we got the
254:16 we got the double quote
254:17 double quote single quote and now this is a triple
254:20 single quote and now this is a triple quote so now we can use the double quote
254:22 quote so now we can use the double quote and single quotes within this string
254:25 and single quotes within this string so i'm going to um i'm just going to
254:28 so i'm going to um i'm just going to delete your right here and we are going
254:31 delete your right here and we are going to
254:32 to put a ternary operator to see if it's
254:35 put a ternary operator to see if it's going to say dealers or yours in the
254:37 going to say dealers or yours in the dealer's hand or your hand so
254:40 dealer's hand or your hand so to do some code i'm going to put these
254:42 to do some code i'm going to put these curly braces here
254:44 curly braces here and then to do this ternary operator
254:46 and then to do this ternary operator we're going to put dealer
254:49 we're going to put dealer and now here so here's the double quote
254:51 and now here so here's the double quote and here's the single quote so dealers
254:54 and here's the single quote so dealers it will
254:55 it will return dealers
254:56 return dealers if
254:58 if self.dealer
255:04 so basically if self.dealer equals true so return dealers if self.dealer
255:08 so return dealers if self.dealer else will return
255:10 else will return your
255:13 your okay that's the line so it's going to be
255:15 okay that's the line so it's going to be the dealer's hand or your hand and next
255:18 the dealer's hand or your hand and next we will add a for loop that will print
255:21 we will add a for loop that will print out each of the cards
255:24 out each of the cards so
255:25 so for card and self.cards
255:29 for card and self.cards print
255:31 print card
255:32 card and then finally if the player is not
255:34 and then finally if the player is not the dealer it should print value
255:37 the dealer it should print value and then a colon and then print the
255:39 and then a colon and then print the value of the cards so to do this we can
255:42 value of the cards so to do this we can actually use the the not operator so
255:44 actually use the the not operator so if not
255:46 if not self.dealer
255:47 self.dealer [Music]
255:54 then we will print and we'll print value
255:56 and we'll print value value
255:59 value and then i can just put a comma to print
256:01 and then i can just put a comma to print two different items so the string and
256:02 two different items so the string and it'll print self dot get
256:05 it'll print self dot get value
256:07 value and it's gonna when you put a comma and
256:09 and it's gonna when you put a comma and two different things it's gonna put a
256:10 two different things it's gonna put a space in between
256:12 space in between and then finally we'll just add an empty
256:14 and then finally we'll just add an empty a print statement that will print a
256:16 a print statement that will print a blank line
256:18 blank line okay let's test this out by
256:21 okay let's test this out by instead of printing this we are going to
256:24 instead of printing this we are going to print
256:25 print hand
256:26 hand dot display
256:28 dot display to see if this all works how we thought
256:31 to see if this all works how we thought it was going to work
256:33 it was going to work so your hand k of spades two of spades
256:36 so your hand k of spades two of spades value is 12. so it's actually
256:38 value is 12. so it's actually calculating that correctly because
256:39 calculating that correctly because that's 10 plus 2 is 12 and then it's
256:42 that's 10 plus 2 is 12 and then it's going to print none which indicates that
256:44 going to print none which indicates that we did something wrong which is that we
256:46 we did something wrong which is that we did not need to print this because hand
256:49 did not need to print this because hand display display already prints so now
256:52 display display already prints so now just call hand.display
256:54 just call hand.display okay so now it doesn't put none or
256:57 okay so now it doesn't put none or doesn't yeah it doesn't put none at the
256:58 doesn't yeah it doesn't put none at the end so that looks right
257:01 end so that looks right okay when you're playing blackjack you
257:03 okay when you're playing blackjack you don't get to see
257:04 don't get to see everyone else's cards
257:06 everyone else's cards so
257:07 so we're going to update this so when the
257:09 we're going to update this so when the dealer's cards are printed during the
257:11 dealer's cards are printed during the game
257:12 game only the second one should display
257:15 only the second one should display the first card should display as hidden
257:19 the first card should display as hidden so in this for loop
257:22 so in this for loop when we're displaying the cards
257:25 when we're displaying the cards we're going to need to get access to the
257:27 we're going to need to get access to the card index since that will determine
257:30 card index since that will determine which to display
257:32 which to display since we're only going to display the
257:33 since we're only going to display the second card
257:34 second card so let's start by updating this for loop
257:37 so let's start by updating this for loop so we can get access to both the card
257:40 so we can get access to both the card and the card index
257:42 and the card index we briefly touched on this earlier in
257:44 we briefly touched on this earlier in the course we're going to be using the
257:46 the course we're going to be using the enumerate function
257:47 enumerate function so
257:48 so when it's for card in
257:50 when it's for card in and now i'm going to type in enumerate
257:53 and now i'm going to type in enumerate and i'm going to pass in self.cards
257:56 and i'm going to pass in self.cards and this is going to return the index
257:59 and this is going to return the index and the card for each card so i'm going
258:01 and the card for each card so i'm going to type it index
258:03 to type it index comma
258:04 comma and so
258:07 and so in we're getting the index and the card
258:10 in we're getting the index and the card for all the items in self.cards
258:13 for all the items in self.cards and so now we just have to update what's
258:16 and so now we just have to update what's in the for loop
258:17 in the for loop to print
258:19 to print hidden
258:20 hidden if it's the first card and it's a dealer
258:23 if it's the first card and it's a dealer so we'll do
258:25 so we'll do if index
258:27 if index equals zero and self.dealer
258:34 then we will print
258:43 and then we can use an else any other time
258:45 any other time and let's make sure this lines up
258:46 and let's make sure this lines up correctly any other time we will print
258:48 correctly any other time we will print the card
258:49 the card so
258:51 so what we did wrong here is this should be
258:52 what we did wrong here is this should be double equal sign i did almost did the
258:56 double equal sign i did almost did the the main mistake you always have to
258:57 the main mistake you always have to watch out never use a single equal sign
259:00 watch out never use a single equal sign when you're checking equality
259:03 when you're checking equality because that's the single equal sign is
259:05 because that's the single equal sign is the assignment operator so if index
259:07 the assignment operator so if index equals zero and self and we it is the
259:09 equals zero and self and we it is the dealer then we'll print hidden
259:11 dealer then we'll print hidden so in our version of the game at the end
259:13 so in our version of the game at the end of the game
259:14 of the game that all the dealer's cards will be
259:17 that all the dealer's cards will be shown so you can see what the dealer had
259:20 shown so you can see what the dealer had so to do that we're going to create a
259:23 so to do that we're going to create a new parameter in this
259:25 new parameter in this display method and it's going to be
259:27 display method and it's going to be called show all dealer cards with
259:30 called show all dealer cards with underscores for spaces and we're going
259:32 underscores for spaces and we're going to set the default value to false
259:35 to set the default value to false show
259:36 show all
259:37 all dealer
259:39 dealer cards
259:40 cards and when the default value is going to
259:42 and when the default value is going to be false
259:43 be false now we'll add it to this if statement
259:49 so we'll add another
259:51 we'll add another and
259:52 and not
259:53 not show
259:55 show all
259:56 all dealer cards
259:58 dealer cards so
260:00 so it's going to be hidden if we're not
260:02 it's going to be hidden if we're not showing all the dealer cards
260:04 showing all the dealer cards but if we are showing all the dealer
260:06 but if we are showing all the dealer cards then this whole if statement will
260:08 cards then this whole if statement will be false so we'll just print the card
260:11 be false so we'll just print the card and there's going to be one other
260:13 and there's going to be one other scenario where we're not going to print
260:17 scenario where we're not going to print hidden if there's a blackjack then the
260:19 hidden if there's a blackjack then the game is over the person with the
260:21 game is over the person with the blackjack is just going to win and then
260:23 blackjack is just going to win and then we'll just print all the cards so we're
260:26 we'll just print all the cards so we're going to add that to this long if
260:28 going to add that to this long if statement here so we'll say
260:30 statement here so we'll say and not is
260:33 and not is black jack
260:35 black jack and it should be
260:37 and it should be self dot is blackjack to be able to call
260:41 self dot is blackjack to be able to call this method here
260:43 this method here and since this is such a long line is
260:45 and since this is such a long line is always going to go to this next line we
260:47 always going to go to this next line we can do this special thing
260:50 can do this special thing we can add a slash here
260:53 we can add a slash here and then just go to the next line
260:56 and then just go to the next line so
260:57 so this slash or it's a backslash i mean
260:59 this slash or it's a backslash i mean this backslash will indicate that the
261:01 this backslash will indicate that the line continues on the following line
261:05 line continues on the following line okay we're done creating the hand class
261:07 okay we're done creating the hand class so we'll delete
261:09 so we'll delete everything that we were using for
261:10 everything that we were using for testing before
261:12 testing before okay it's time to code the final and
261:15 okay it's time to code the final and longest class that runs the game so what
261:18 longest class that runs the game so what i want you to do is create a class
261:20 i want you to do is create a class called game and inside the class create
261:22 called game and inside the class create a method called play and inside the
261:25 a method called play and inside the method create a variable called
261:27 method create a variable called gamenumber with the underscore for the
261:30 gamenumber with the underscore for the space
261:31 space and set that to zero so class game
261:36 and set that to zero so class game and then we'll create another variable
261:38 and then we'll create another variable games to play and set that to zero
261:41 games to play and set that to zero now we're going to set games to play
261:44 now we're going to set games to play to be whatever the user inputs
261:47 to be whatever the user inputs after they're asked how many games do
261:49 after they're asked how many games do you want to play
261:51 you want to play so you may remember how to do
261:53 so you may remember how to do input from before
261:55 input from before so we just do input
261:58 so we just do input now we want to make sure the games to
262:00 now we want to make sure the games to play is an end so we just need to
262:02 play is an end so we just need to convert this to an end
262:08 [Music] okay now let's test things so far so at
262:11 okay now let's test things so far so at the end i will put g equals game i'm
262:13 the end i will put g equals game i'm going to create a new game
262:15 going to create a new game and then g dot play
262:28 how many games you want to play five
262:29 five okay well it's not going to play the
262:30 okay well it's not going to play the games yet we still have to create that
262:32 games yet we still have to create that so there is a potential for an error
262:35 so there is a potential for an error here if i do this again and i just put
262:37 here if i do this again and i just put how many games i put you or some letter
262:39 how many games i put you or some letter we're going to get an error
262:41 we're going to get an error so basically anytime someone puts
262:43 so basically anytime someone puts something that's not a number is going
262:45 something that's not a number is going to be an error
262:46 to be an error so let's create a try accept block to
262:50 so let's create a try accept block to handle the exception
262:52 handle the exception and
262:53 and if they put something that's not a
262:55 if they put something that's not a number we'll
262:56 number we'll print you must enter a number so let me
262:59 print you must enter a number so let me arrange this and we've already learned a
263:01 arrange this and we've already learned a little bit about try except blocks i'm
263:03 little bit about try except blocks i'm going to put try
263:05 going to put try and it's going to try this
263:09 and it's going to try this and then if that doesn't work if there's
263:11 and then if that doesn't work if there's an exception
263:13 an exception [Music]
263:14 [Music] it will print
263:16 it will print [Music]
263:19 [Music] you must enter a number
263:22 you must enter a number so currently the user gets only one
263:25 so currently the user gets only one chance to input a correct value let's
263:27 chance to input a correct value let's make the program keep asking the user
263:29 make the program keep asking the user for a value until the user enters a
263:31 for a value until the user enters a number this can be done with a while
263:33 number this can be done with a while loop the while loop just keeps looping
263:35 loop the while loop just keeps looping while something is true so keep looping
263:38 while something is true so keep looping until the user enters a number by
263:40 until the user enters a number by putting the entire tri-catch block into
263:43 putting the entire tri-catch block into a while loop that keeps looping while
263:45 a while loop that keeps looping while the game's a play is less than or equal
263:47 the game's a play is less than or equal to zero
263:49 to zero [Music]
263:51 [Music] oh and i have to make sure i spell
263:54 oh and i have to make sure i spell while correctly
263:56 while correctly okay now let's create the main game loop
263:59 okay now let's create the main game loop this is a new loop that will loop one
264:01 this is a new loop that will loop one time per game played it should loop
264:04 time per game played it should loop while game number is less than games to
264:07 while game number is less than games to play and the first line of loop should
264:09 play and the first line of loop should increment the game number by one
264:16 inside the loop we'll create a deck object in a deck variable and shuffle
264:19 object in a deck variable and shuffle the deck
264:21 the deck now we'll create a variable called
264:23 now we'll create a variable called playerhand and set it to a hand object
264:27 playerhand and set it to a hand object and then we'll create a variable called
264:28 and then we'll create a variable called dealerhand and set it to a hand object
264:31 dealerhand and set it to a hand object but this time we'll make sure to specify
264:33 but this time we'll make sure to specify that dealer equals true
264:38 okay this next part will be a little more complicated we'll create a for loop
264:41 more complicated we'll create a for loop that loops two times and each iteration
264:44 that loops two times and each iteration should add a card to the player's hand
264:46 should add a card to the player's hand that is dealt from the deck and add a
264:48 that is dealt from the deck and add a card to the dealer's hand that is also
264:50 card to the dealer's hand that is also dealt from the deck
264:52 dealt from the deck [Music]
264:55 [Music] okay we just dealt two cards each player
264:58 okay we just dealt two cards each player now information is going to be printed
265:00 now information is going to be printed to the console for each game so let's
265:02 to the console for each game so let's start by printing an empty line
265:05 start by printing an empty line now we'll print an asterisk 30 times to
265:08 now we'll print an asterisk 30 times to make a divider
265:10 make a divider there's a trick to printing something a
265:12 there's a trick to printing something a lot of times so i can put an asterisk
265:16 lot of times so i can put an asterisk in in quotation marks and then just do
265:18 in in quotation marks and then just do times 30.
265:21 times 30. so it's going to print it 30 times now
265:23 so it's going to print it 30 times now we'll print the current game number out
265:25 we'll print the current game number out of the total number of games
265:27 of the total number of games so it'll be something like game 4 of 10
265:36 and we'll use an f string and then we'll just print 30 more
265:38 and then we'll just print 30 more asterisks
265:46 now we'll display the player's hand and then the dealer's hand
265:49 and then the dealer's hand at this point in the game someone could
265:50 at this point in the game someone could already have won if they got a blackjack
265:53 already have won if they got a blackjack the code should check if there's a
265:54 the code should check if there's a winner let's put the code to check if
265:56 winner let's put the code to check if there's a winner in a separate method of
265:58 there's a winner in a separate method of the game class so create a method called
266:00 the game class so create a method called check winner for now the method should
266:03 check winner for now the method should just return false
266:08 and just make sure everything's indented correctly this should be less indented
266:10 correctly this should be less indented than the previous line here
266:12 than the previous line here the check winner function should take
266:14 the check winner function should take the playerhand and dealer hand as
266:16 the playerhand and dealer hand as arguments
266:23 now before this return statement we're going to check if
266:24 we're going to check if playerhand.getvalue is greater than 21.
266:27 playerhand.getvalue is greater than 21. if so we'll print you busted dealer wins
266:30 if so we'll print you busted dealer wins and then return true and remember once
266:32 and then return true and remember once the program gets to a return statement
266:34 the program gets to a return statement none of the following statements in the
266:36 none of the following statements in the block are run
266:38 block are run [Music]
266:46 now we'll use a few lf statements to check for various other conditions so
266:49 check for various other conditions so we'll add an lf statement to see if the
266:51 we'll add an lf statement to see if the dealer got over 21
266:54 dealer got over 21 and then we'll print dealer busted you
266:55 and then we'll print dealer busted you win and then return true
266:59 win and then return true [Music]
267:02 [Music] oh and i just copied all this but this
267:03 oh and i just copied all this but this should be an l if not if
267:06 should be an l if not if and then we'll add an lf statement to
267:08 and then we'll add an lf statement to check if both players have a blackjack
267:10 check if both players have a blackjack and then we'll print both players have a
267:11 and then we'll print both players have a blackjack tie
267:13 blackjack tie and then return true
267:21 [Music] and then we'll add an elf statement to
267:22 and then we'll add an elf statement to check if player hand has a blackjack and
267:24 check if player hand has a blackjack and then we'll print you have blackjack you
267:26 then we'll print you have blackjack you win
267:27 win and then return true
267:34 and then we'll check if the dealer hand has a blackjack and then say dealer has
267:36 has a blackjack and then say dealer has blackjack dealer wins
267:38 blackjack dealer wins and return true
267:41 and return true [Music]
267:42 [Music] okay we're done with all the hand when
267:44 okay we're done with all the hand when conditions but the game can also end if
267:47 conditions but the game can also end if both players choose not to get more
267:49 both players choose not to get more cards so we're going to add a new
267:51 cards so we're going to add a new argument to the check winner method with
267:53 argument to the check winner method with a default value it's going to be game
267:55 a default value it's going to be game over equals false
267:57 over equals false so we'll add game
268:00 so we'll add game over
268:02 over equals false
268:03 equals false if it's true that means both players
268:05 if it's true that means both players have chosen not to get more cards now
268:08 have chosen not to get more cards now we'll use the new argument
268:10 we'll use the new argument the string of if and lf statements
268:12 the string of if and lf statements should only be run
268:14 should only be run if it's not a game over and we'll make
268:16 if it's not a game over and we'll make sure the line returned false is not in
268:19 sure the line returned false is not in the if statement
268:21 the if statement so
268:23 so here we'll say
268:24 here we'll say if
268:26 if not game
268:28 not game over
268:29 over and then i'll just select all these and
268:32 and then i'll just select all these and put them
268:34 put them in here
268:35 in here so if game over is true we'll check if
268:37 so if game over is true we'll check if the player hand's value is more than the
268:40 the player hand's value is more than the deal hands value and if so we'll print
268:42 deal hands value and if so we'll print you in
268:43 you in so we can do this with an else here
268:47 so we can do this with an else here else
268:50 else if player
268:52 if player [Music]
268:55 [Music] and then we'll do an lf for if it's a
268:57 and then we'll do an lf for if it's a tie
269:02 so this is an lf and we'll say if these are
269:04 and we'll say if these are equal to each other
269:06 equal to each other and we'll print
269:08 and we'll print tie
269:12 and then make sure we have the correct emoji for a tie
269:14 emoji for a tie [Music]
269:16 [Music] and then else the dealer is one
269:18 and then else the dealer is one so we'll just do
269:20 so we'll just do else
269:22 else [Music]
269:23 [Music] and then at the exact same level of
269:25 and then at the exact same level of indentation as the else we just added
269:28 indentation as the else we just added we'll add return true this will make the
269:30 we'll add return true this will make the method return true if game over equals
269:32 method return true if game over equals true
269:34 true now let's go back to the play method
269:35 now let's go back to the play method inside the while loop
269:37 inside the while loop and then we'll do an if statement and
269:39 and then we'll do an if statement and we'll do if and then we'll call the
269:41 we'll do if and then we'll call the check winner function with the player
269:42 check winner function with the player hand and the dealer hand so let's go
269:45 hand and the dealer hand so let's go back up here
269:54 winner and then we'll enter the player hand and
269:56 and then we'll enter the player hand and the dealer hand
269:58 the dealer hand [Music]
269:59 [Music] so if this is true that means we should
270:01 so if this is true that means we should go on to the next game
270:03 go on to the next game to do that we do continue
270:07 to do that we do continue so continue is going to just go to the
270:09 so continue is going to just go to the next iteration of the loop and the loop
270:11 next iteration of the loop and the loop we're on is this loop so when we go to
270:14 we're on is this loop so when we go to the next iteration we start a new game
270:17 the next iteration we start a new game at this point in the game the player
270:19 at this point in the game the player will be able to choose hit or stand
270:22 will be able to choose hit or stand so inside the while loop but not inside
270:24 so inside the while loop but not inside the if statement we just added we'll
270:26 the if statement we just added we'll create a variable called choice and set
270:29 create a variable called choice and set it to be an empty string
270:34 the player should be able to keep choosing until the value of their hand
270:35 choosing until the value of their hand is over 21. so right under the choice
270:38 is over 21. so right under the choice variable we'll add a while loop that
270:41 variable we'll add a while loop that loops while player hand's value is less
270:43 loops while player hand's value is less than 21 and inside the loop we'll add a
270:46 than 21 and inside the loop we'll add a line to get the choice
270:48 line to get the choice that's either going to be hit or stand
271:00 and then we'll just add this to convert whatever the answer is whatever the user
271:02 whatever the answer is whatever the user put in we are going to convert it to
271:03 put in we are going to convert it to lowercase
271:05 lowercase the while loop we just added should also
271:07 the while loop we just added should also stop if the user's choice is stand or or
271:10 stop if the user's choice is stand or or s so we'll update the line that starts
271:12 s so we'll update the line that starts the while loop to also stop if the
271:15 the while loop to also stop if the choice isn't s or stand so just do and
271:20 choice isn't s or stand so just do and choice
271:21 choice not in
271:22 not in and this is there's a few ways to do it
271:24 and this is there's a few ways to do it but this is kind of a new way that i'm
271:27 but this is kind of a new way that i'm just showing you here
271:28 just showing you here [Music]
271:31 [Music] so we are checking if choice is not in
271:35 so we are checking if choice is not in this list and inside the list we have
271:37 this list and inside the list we have two elements s or stand so if choice is
271:40 two elements s or stand so if choice is not in that if the choice is not s or
271:43 not in that if the choice is not s or stand then we'll continue the loop
271:45 stand then we'll continue the loop and then after the input we'll print an
271:47 and then after the input we'll print an empty line
271:52 also we want the program to keep asking the user for a choice until the user
271:54 the user for a choice until the user enters a valid choice
271:56 enters a valid choice the valid choices are hs hit and stand
272:00 the valid choices are hs hit and stand so right after the last print statement
272:02 so right after the last print statement at the same indentation we'll add a
272:04 at the same indentation we'll add a while loop that will keep looping until
272:06 while loop that will keep looping until the user enters a valid choice
272:09 the user enters a valid choice and inside that while loop we'll ask for
272:11 and inside that while loop we'll ask for input again
272:13 input again but we'll specify it can be h or s as
272:15 but we'll specify it can be h or s as well
272:18 well [Music]
272:25 [Music] so this is going to look very similar to
272:26 so this is going to look very similar to this line but it's going to kind of
272:28 this line but it's going to kind of clarify things just a little bit
272:30 clarify things just a little bit and then we'll print another empty line
272:35 and then we'll print another empty line the last while loop we checked if choice
272:38 the last while loop we checked if choice was not in a list
272:39 was not in a list outside of the recently added a while
272:41 outside of the recently added a while loop but inside the loop we just added
272:43 loop but inside the loop we just added before that one we'll add an if
272:45 before that one we'll add an if statement to check if choice is in the
272:47 statement to check if choice is in the list hit or h and if so we'll add a card
272:50 list hit or h and if so we'll add a card to the player's hand that is dealt from
272:52 to the player's hand that is dealt from the deck
272:58 and then right below that will display the player's hand
273:03 outside all the while loops about the player making a choice we'll check for a
273:05 player making a choice we'll check for a winner we'll use the same if statement
273:07 winner we'll use the same if statement and continue statement that we use last
273:09 and continue statement that we use last time we checked for winner so i'll just
273:11 time we checked for winner so i'll just copy this
273:14 copy this and then
273:15 and then we have to make sure it's lined up
273:17 we have to make sure it's lined up correctly
273:18 correctly okay so this is outside of this while
273:20 okay so this is outside of this while loop so after this all is all done we
273:23 loop so after this all is all done we check for a winner let's just add an
273:25 check for a winner let's just add an empty line there to
273:26 empty line there to make it more clear that the while loop
273:28 make it more clear that the while loop is over
273:29 is over now we'll store the value of the
273:30 now we'll store the value of the player's hand in a variable named player
273:33 player's hand in a variable named player hand value with underscores for spaces
273:38 hand value with underscores for spaces [Music]
273:39 [Music] and we'll do the same thing with the
273:41 and we'll do the same thing with the dealer's hand
273:44 dealer's hand [Music]
273:50 [Music] remember i could use the command d or
273:53 remember i could use the command d or control d to select two words at once
273:55 control d to select two words at once and change them both at the same time
273:58 and change them both at the same time okay the dealer should keep drawing
274:00 okay the dealer should keep drawing cards until dealer hand value is more
274:03 cards until dealer hand value is more than 17 so we'll make this happen with a
274:06 than 17 so we'll make this happen with a while loop
274:08 while loop and inside the loop we'll make sure the
274:09 and inside the loop we'll make sure the dealer is dealt a card from the deck and
274:11 dealer is dealt a card from the deck and that dealer hand value is updated
274:15 that dealer hand value is updated so you can try that yourself but i'm
274:17 so you can try that yourself but i'm going to show you right now while
274:19 going to show you right now while dealer
274:20 dealer hand value
274:22 hand value is less than 17.
274:26 is less than 17. then we will do dealer
274:28 then we will do dealer hand dot
274:30 hand dot add card
274:37 okay and after this while loop will display the dealer's hand and when we
274:39 display the dealer's hand and when we call the display method we'll make sure
274:41 call the display method we'll make sure to set show all dealer cards to true
274:48 [Music] and since it's the end of the game
274:50 and since it's the end of the game that's why we're just showing all the
274:51 that's why we're just showing all the cards
274:53 cards now we'll check for a winner just like
274:54 now we'll check for a winner just like before
275:05 then we'll print your hand colon and then the player hand value
275:08 then the player hand value [Music]
275:09 [Music] and then the dealer's hand
275:17 now we'll call the check winner function one final time
275:18 one final time but this time it should not be an if
275:20 but this time it should not be an if statement and we'll pass in the hands
275:22 statement and we'll pass in the hands like before but this time we'll add a
275:24 like before but this time we'll add a third argument of true to indicate that
275:27 third argument of true to indicate that the game is over
275:34 and at this point in the code the game is over
275:35 is over so outside the outer while loop and in
275:38 so outside the outer while loop and in the play method we'll add the final line
275:40 the play method we'll add the final line of saying
275:42 of saying thanks for playing
275:49 so it's going to be outside that while loop and we'll put print
275:57 and just to demonstrate it i use an escape character to add a new line so
276:00 escape character to add a new line so this slash in is going to add a new line
276:03 this slash in is going to add a new line and then do thanks for playing and when
276:05 and then do thanks for playing and when i line this up for with the while loop i
276:08 i line this up for with the while loop i realize that this entire function should
276:10 realize that this entire function should not be lined up with the while loop
276:12 not be lined up with the while loop sometimes it gets tricky with um
276:16 sometimes it gets tricky with um figuring out the exact right indentation
276:18 figuring out the exact right indentation so if i
276:19 so if i kind of go up straight up here
276:22 kind of go up straight up here i should say see that this should be
276:23 i should say see that this should be lined up with this play function
276:26 lined up with this play function so i'm going to come back down to this
276:29 so i'm going to come back down to this function
276:30 function i'm going to
276:31 i'm going to copy this all
276:33 copy this all and i'm just going to do shift tab
276:36 and i'm just going to do shift tab to indent it all one less
276:40 to indent it all one less this happens sometimes when running
276:42 this happens sometimes when running python code sometimes the indentation
276:44 python code sometimes the indentation can get all mixed up but that should be
276:47 can get all mixed up but that should be correct now and i think the red squiggly
276:50 correct now and i think the red squiggly lines here on the return true are not a
276:52 lines here on the return true are not a mistake in the code but a mistake in the
276:55 mistake in the code but a mistake in the error checking because it comes after
276:58 error checking because it comes after that emoji and it doesn't know how to
276:59 that emoji and it doesn't know how to handle the emoji but it's perfectly fine
277:02 handle the emoji but it's perfectly fine for code to have emojis okay let's run
277:05 for code to have emojis okay let's run the program and try it out so i'll press
277:09 the program and try it out so i'll press play how many games i want to play i'll
277:10 play how many games i want to play i'll do three
277:11 do three so game of one of three so i can see i
277:14 so game of one of three so i can see i have 17 i don't know what the dealer has
277:16 have 17 i don't know what the dealer has but i'm going to s for stand
277:20 but i'm going to s for stand okay it's always good to test so it says
277:23 okay it's always good to test so it says deal is missing one required positional
277:26 deal is missing one required positional argument so let's go up to
277:29 argument so let's go up to it says line 139 so this can kind of
277:31 it says line 139 so this can kind of help us know where to go so let's go up
277:34 help us know where to go so let's go up to 139
277:42 and yeah i want to deal a single card so i'm going to deal one card here
277:44 i'm going to deal one card here and
277:45 and were there any other times i did use
277:47 were there any other times i did use deal
277:51 i want to deal one card here
277:56 and yeah i got the deal one up here so i
277:58 yeah i got the deal one up here so i just think i just forgot the deal one in
278:00 just think i just forgot the deal one in those places
278:02 those places so uh thanks to these error messages
278:05 so uh thanks to these error messages whenever you have a problem make sure to
278:07 whenever you have a problem make sure to read the error messages and it can often
278:10 read the error messages and it can often give you a very good idea of what you
278:12 give you a very good idea of what you need to do wrong because even says deal
278:15 need to do wrong because even says deal is missing one required positional
278:16 is missing one required positional argument the number so that can really
278:19 argument the number so that can really help figure out what's wrong with your
278:20 help figure out what's wrong with your code so let's try that again we'll do
278:23 code so let's try that again we'll do three games
278:25 three games and then this time i will hit
278:28 and then this time i will hit and i'm going to stand
278:30 and i'm going to stand okay so now we have another error
278:33 okay so now we have another error so it says 173
278:36 so it says 173 and oh this i can already see this is
278:38 and oh this i can already see this is spelled wrong so let's go to
278:41 spelled wrong so let's go to 173
278:47 and make sure i spell that correctly
278:51 make sure i spell that correctly and make sure i spell that correctly
278:53 and make sure i spell that correctly okay let's try again
278:56 okay let's try again how many games you want to play three
278:58 how many games you want to play three i'm going to hit
279:01 i'm going to hit and hit
279:03 and hit okay so the first game
279:06 okay so the first game you busted dealer wins and now we're on
279:09 you busted dealer wins and now we're on game number two i'll hit
279:12 game number two i'll hit and this time i will stand
279:15 and this time i will stand okay dealer busted you win now we're on
279:18 okay dealer busted you win now we're on game three of three
279:20 game three of three and i will hit
279:22 and i will hit and i will stand
279:24 and i will stand and final results your hand 20 dealer's
279:27 and final results your hand 20 dealer's hand 19
279:28 hand 19 you win
279:29 you win thanks for playing
279:32 thanks for playing we just completed this whole game
279:36 we just completed this whole game okay we've reached the end of the course
279:38 okay we've reached the end of the course so you've learned the basics of python
279:41 so you've learned the basics of python and if you've been coding along you've
279:43 and if you've been coding along you've written two python programs good luck on
279:45 written two python programs good luck on your programming journey thanks for
279:48 your programming journey thanks for watching and remember use your code for
279:51 watching and remember use your code for good bye bye bye
279:54 good bye bye bye bye bye