Python Full Course for free ๐ โ Transcript
Full transcript
- 0:00hey what's going on everybody it's bro
- 0:02hope you're doing well
- 0:03and in this video i'm going to help you
- 0:04get started with python so
- 0:06sit back relax and enjoy the show
- 0:11if you wouldn't mind please like comment
- 0:13and subscribe
- 0:14one like equals one prayer for the
- 0:16youtube algorithm
- 0:18i'm gonna tell you why you need to learn
- 0:20python it's the easiest programming
- 0:22language to learn as well as being the
- 0:24most popular in the world right now
- 0:26and according to glassdoor the salary
- 0:28for new python developers in the united
- 0:31states is
- 0:3264 000 if any of this sounds good to you
- 0:35well then
- 0:36let's get started if you need to
- 0:37download python go to
- 0:39python.org downloads and click this
- 0:42yellow download python
- 0:44button open when done check add
- 0:47python 3.9 or another version to path
- 0:51install now and then give it a little
- 0:53bit it states here that setup was
- 0:56successful so let's close out of this
- 0:58our next step is to download an ide
- 1:00that's an acronym for
- 1:02integrated development environment think
- 1:04of it as
- 1:05software that helps us write other
- 1:07software one ide that i recommend
- 1:09is pycharm you can find it at
- 1:11jetbrains.com
- 1:13pycharm go to download if you would like
- 1:17to pay for a professional version you
- 1:19can click
- 1:19this download button but since i don't
- 1:21like to pay for things i'm going to use
- 1:23the free community version at this point
- 1:26we just have to follow the standard
- 1:28installation procedure
- 1:29click next you can pick a destination
- 1:32folder but i'll keep the default
- 1:34next if you would like a desktop
- 1:37shortcut
- 1:38then check this i would also recommend
- 1:40updating the
- 1:41path variable next
- 1:44install and then give it a minute or two
- 1:48then after finishing i have a desktop
- 1:50shortcut so i can just click on this to
- 1:52run pycharm
- 1:53let's create a new project name this
- 1:56project whatever you like i'm going to
- 1:58name this project
- 1:59hello world and i recommend not creating
- 2:03a
- 2:03main dot pi welcome script right now
- 2:05i'll show you how to do that manually
- 2:07and let's create
- 2:09if you need a python file to work with
- 2:12this is how to create one go to
- 2:14file new python file and i'm going to
- 2:17name
- 2:18this main and then click python file
- 2:23all right ladies and gentlemen we now
- 2:25have an empty python script that we can
- 2:27use to begin coding
- 2:28now if you checked this box create a
- 2:31main.pi welcome script and then created
- 2:33a new project
- 2:34then your main python file will probably
- 2:37look a little something like this but
- 2:38that's completely fine we can just
- 2:40delete this
- 2:41i don't know about you guys but the font
- 2:43is very small for me
- 2:45so let's change that we can change that
- 2:47by going to file
- 2:49settings editor font and you can
- 2:52increase the font size
- 2:54let's say 25 you can also change the
- 2:56font too
- 2:57what's a good one uh how about
- 3:01that then click apply and
- 3:04okay much better we can actually see
- 3:06something
- 3:07to run a python script just click the
- 3:09screen play button in the corner
- 3:11alternatively you can go to the run tab
- 3:13and then go to run
- 3:15okay so this box that appeared at the
- 3:17bottom this is referred to as a terminal
- 3:20window this will display
- 3:21any output that your program has and
- 3:23this line at the end
- 3:25process finished with exit code zero
- 3:27that just means that there were no
- 3:29errors in this program
- 3:30so currently this program doesn't do
- 3:32anything but let's change that let's
- 3:34print something
- 3:35let's print a message so to print a
- 3:37message to the terminal window
- 3:39type print and then add a set of
- 3:42parentheses
- 3:42and then you can type a message but it
- 3:44has to be within quotes
- 3:46either double quotes or single quotes it
- 3:48doesn't matter
- 3:49so let's think of a message i don't know
- 3:52i
- 3:53love pizza then let me run this
- 3:57and in my terminal window it says i love
- 4:00pizza
- 4:01to print another line just add another
- 4:03print statement
- 4:04print and then your message within
- 4:06quotes
- 4:08it's really good
- 4:13and well what do you know it printed i
- 4:16love pizza
- 4:17it's really good so ladies and gentlemen
- 4:19you just wrote your first python program
- 4:21by the way you can change the font as
- 4:24well as the font color of your terminal
- 4:26window by going to
- 4:27file settings then go to editor
- 4:31color scheme console font and console
- 4:34colors
- 4:34let's change the font let's say i would
- 4:38like
- 4:39whatever this font is then click apply
- 4:43let's change the font color too by going
- 4:45to console colors
- 4:47for any output you can click number two
- 4:51and select a color let's say i would
- 4:54like
- 4:54bright green that should be decent
- 4:59and for anything else let's say this
- 5:02will be a dark color so i can barely see
- 5:04it
- 5:05so then let's click apply and okay
- 5:08oh yeah that's much better well
- 5:10everybody that was your very first
- 5:12python program i'll post
- 5:14any useful links and timestamps for this
- 5:16video in the comments section down below
- 5:18but yeah welcome to coding with python
- 5:22hey you yeah i'm talking to you if you
- 5:25learned something new
- 5:26then help me help you in three easy
- 5:28steps
- 5:29by smashing that like button drop a
- 5:31comment down below
- 5:32and subscribe if you'd like to become a
- 5:34fellow bro
- 5:38[Music]
- 5:56hey what's going on everybody it's bro
- 5:59hope you're doing well and in this video
- 6:01i'm going to explain how
- 6:02variables work in python so sit back
- 6:05relax
- 6:06and enjoy the show
- 6:09if you find this video helpful please
- 6:11remember to like
- 6:12comment and subscribe your support will
- 6:15help keep this channel running
- 6:17all right let's do this a variable is a
- 6:20container for a value it behaves as the
- 6:22value that it contains
- 6:24it's much like in algebra lessons back
- 6:26in the day where we had to solve for
- 6:28whatever value
- 6:29x was and then we can reuse x and it
- 6:31behaved exactly as that value
- 6:34well with programming we can do that
- 6:35plus we can assign variables of
- 6:37different data types
- 6:38so they are not limited to just numbers
- 6:40we can assign whole words
- 6:42numbers and even these things called
- 6:44booleans which are either true or false
- 6:46but i'll get more in depth into data
- 6:48types a little bit later on in this
- 6:49video
- 6:50so let's create a variable and this is
- 6:51how to do so we need a unique name for
- 6:54this variable
- 6:54let's say name name equals some value
- 6:57now the first data type that we'll cover
- 6:59is strings a string
- 7:01is a series of characters so to create a
- 7:03string
- 7:04we can either use single quotes or
- 7:06double quotes in python
- 7:08and we can assign this a value of
- 7:10whatever your own name is
- 7:11so i will assign this variable name a
- 7:14value of bro
- 7:15now this variable will behave exactly as
- 7:18if it was
- 7:18this value this name and then we can
- 7:20reuse this variable for something
- 7:22so let's print whatever our name is
- 7:25print
- 7:26name and this will print whatever your
- 7:28name is to the console window
- 7:30now when you print your variable make
- 7:32sure you don't put it within quotes
- 7:34because what we're doing then is
- 7:36literally printing
- 7:37the word name as you can see here so
- 7:41if you need to use your variable for
- 7:42something make sure it's not within
- 7:44quotes
- 7:45now we can combine our string variable
- 7:48with another string so within our print
- 7:50statement let's print
- 7:52a string literal such as hello space
- 7:55plus name so we are combining strings
- 7:58and then we're printing the results to
- 7:59the console window so this will print
- 8:02hello bro now you can actually check the
- 8:05data type of a variable
- 8:06so i'm going to turn this line into a
- 8:08comment so what you'll do
- 8:10is take the name of the variable
- 8:12surround this with a set of parentheses
- 8:14and then precede the set of parentheses
- 8:16with the type
- 8:17function and this will print the data
- 8:20type of this variable to the console
- 8:21window
- 8:22and you can see that it says class str
- 8:25short for string
- 8:26a string is a series of characters a
- 8:28name is a good example of a string
- 8:30a series of characters so that is how to
- 8:32check the data type
- 8:34of a variable just use the type function
- 8:37now you can actually combine variables
- 8:39together as long as they're of the same
- 8:41data type so let's change our name to
- 8:44maybe first name and we'll create a
- 8:45second variable called lastname
- 8:47now a common naming convention with
- 8:49variables if it has
- 8:51two words separate each word with an
- 8:53underscore
- 8:54it's not necessary but it's common
- 8:56practice for python and honestly i
- 8:58sometimes forget about that but don't
- 8:59tell anybody
- 9:00so let's create another variable called
- 9:02last name last
- 9:03name equals whatever your last name is
- 9:07and then let's create a third variable
- 9:09called full name
- 9:10full underscore name and let's combine
- 9:14first name plus last
- 9:18name and let us display full name
- 9:22along with hello plus full name
- 9:25now these two names were combined
- 9:26together to create a whole
- 9:28new variable actually what i think i'll
- 9:30do is add a space
- 9:32within my variable so i'm doing a bunch
- 9:35of string concatenation i'm combining
- 9:37the variable first name plus a space
- 9:39character plus
- 9:40last name so the result is hello
- 9:43whatever your first name and last name
- 9:44is
- 9:45so that is a variable of the string data
- 9:48type it is a series of characters
- 9:51now with strings we cannot normally use
- 9:53these for any sort of math
- 9:55that's where the next data type comes in
- 9:57for the time being i just turned all of
- 9:59these lines into comments and we are
- 10:00going to move on to the int
- 10:02data type short for integer this time
- 10:04let's create a variable called
- 10:06age and we will assign this a value of
- 10:08whatever your age is
- 10:09let's just say that i'm 21 now when you
- 10:12assign
- 10:12a value of the int data type make sure
- 10:15that it is
- 10:16not within quotes because then
- 10:18technically it would be a string then
- 10:19and you can see that the color scheme
- 10:21for this number actually changed
- 10:23so if this was a string we would treat
- 10:25it different
- 10:26than an int data type because with
- 10:28strings we cannot normally use them for
- 10:30any sort of math
- 10:31so we now have a variable called edge
- 10:34and we can actually
- 10:35increase or decrease or do whatever we
- 10:38want with the edge
- 10:38let's increase our age by one let's say
- 10:41it's our birthday to do so we would say
- 10:42age
- 10:43equals age plus one and then let's print
- 10:46our edge to the console window
- 10:48print edge and you can see that it says
- 10:51i am 22.
- 10:52now there's a shorthand way of writing
- 10:53this what you'll do is type
- 10:55age plus equals one so that's kind of
- 10:58like a shortcut and this would work the
- 11:00same too
- 11:01now let's print the data type of our
- 11:03inch variable because we can within a
- 11:05print statement i will print the edge
- 11:07and add my edge variable to this type
- 11:10function and this will print the data
- 11:13type of my edge variable to the console
- 11:15window
- 11:15and i will turn this line into a comment
- 11:17just so that it doesn't get in the way
- 11:19now if i were to print the data type of
- 11:21my age variable
- 11:23as you can see it is of the int data
- 11:25type short for integer
- 11:27it only stores a whole integer number
- 11:30now what if this was a string so what
- 11:33i'm going to do
- 11:34is surround my value within a set of
- 11:36quotes
- 11:37and i will turn this line into a comment
- 11:39so that it does not get in the way
- 11:41you can see that the data type of my age
- 11:43variable is now
- 11:44a string and with strings we cannot
- 11:46normally use them for any sort of math
- 11:48so let's attempt to increase our age
- 11:50variable by one
- 11:52and we ran into an error a type air can
- 11:54only concatenate string
- 11:56to string not end now the point being is
- 11:59that
- 12:00it's important to use the appropriate
- 12:02data type because
- 12:03with strings we cannot normally use them
- 12:05for any sort of math you'll want to use
- 12:06a data type of a numeric value
- 12:09an int or a float which we'll discuss a
- 12:11little bit later now here's a situation
- 12:13to consider
- 12:13what if we would like to display our
- 12:15edge variable along with a string
- 12:17literal much like what we did with this
- 12:19line involving a string literal such as
- 12:22hello
- 12:22plus the user's full name so let's
- 12:25attempt to do so and this is not going
- 12:26to work and i'll explain why
- 12:28so within our print statement let's say
- 12:30something such as
- 12:31your age is plus
- 12:35edge now we're going to run into a type
- 12:38error
- 12:38that's because we attempted to use
- 12:40string concatenation with a string
- 12:42literal
- 12:43and a variable of a different data type
- 12:46a variable of the
- 12:47int data type in order to display our
- 12:50edge along with the string we would need
- 12:52to convert our edge variable to the
- 12:54string
- 12:55data type and one way in which we can do
- 12:57that is by type casting
- 12:59and we'll cover this more in the next
- 13:00video so we're going to surround
- 13:02our age variable that is of the in data
- 13:05type with a set of parentheses
- 13:07and precede this with str short for
- 13:09string and this will convert our
- 13:11variable
- 13:12to a string that will allow us to
- 13:13display it with another string
- 13:15now if we were to run this program this
- 13:17way this will now display
- 13:19your age is and in this case 22 so if
- 13:22you need to display a variable of a
- 13:24different data type
- 13:26along with the string you would just
- 13:27need to use a stringcast to convert that
- 13:29data type to a string
- 13:31but we'll cover this more in the next
- 13:32video on typecasting
- 13:34so that is the int data type it stores a
- 13:37whole integer number and the next data
- 13:40type is the
- 13:41float data type and i'm going to turn
- 13:43these lines into comments and we can
- 13:44move on
- 13:45now the float data type is a numeric
- 13:47value that can store
- 13:48a number that includes a decimal portion
- 13:52an int data type cannot store a decimal
- 13:54portion that's why it's in
- 13:56short for integer it only stores a whole
- 13:58number this time let's say we have a
- 14:00height
- 14:00and i will assign this a perfectly
- 14:02normal height of maybe 250.5
- 14:06centimeters so this decimal portion is
- 14:08important
- 14:09that's indicating that this is a
- 14:11floating point number a float for short
- 14:13so let's print our height to the console
- 14:15window print
- 14:17height and this will print whatever my
- 14:19height is and now
- 14:20let's check the type of our height using
- 14:23the type function
- 14:25so within a print statement i'm going to
- 14:27add my height
- 14:28surround this with another set of
- 14:30parentheses and precede this
- 14:32with the type function and the data type
- 14:35of my variable height is a float short
- 14:38for a floating point number
- 14:39a numeric value that contains a decimal
- 14:42portion
- 14:42and now for practice let's print our
- 14:44height along with a string
- 14:46literal so let's create a string literal
- 14:48such as
- 14:50your height is
- 14:53plus height but guess what we're going
- 14:55to do some type casting
- 14:56so surround height with a set of
- 14:58parentheses and precede this with
- 15:00str so that we convert height to a
- 15:03string so that we can use
- 15:04string concatenation within this print
- 15:06statement and this will print the
- 15:07message
- 15:08your height is 250.5 actually let's add
- 15:12centimeters to the end so
- 15:14plus cm
- 15:17and there we go and that is what a
- 15:19floating point number is
- 15:20float for short it is a numeric value
- 15:22that contains a decimal portion
- 15:25now the last data type that we're going
- 15:26to cover is the boolean
- 15:28data type it is a variable that can only
- 15:30store true
- 15:31or false let's say we have a variable
- 15:34called human
- 15:35if we were to assign this a boolean
- 15:37value this would be either true or
- 15:40false and we can print whatever this
- 15:42value is let's print whatever
- 15:44human is and this will display false now
- 15:47the reason that you may want to use
- 15:49booleans instead of strings even though
- 15:51you can still store
- 15:52a string representation of the word
- 15:54false or true
- 15:56is that these are very useful when we
- 15:58get to if statements
- 15:59we can check to see if some statement is
- 16:01true so that is a good use of
- 16:04boolean values now let's check the data
- 16:07type of
- 16:08my human variable so let's print
- 16:11type human and this will print to the
- 16:15console window
- 16:16bool short for boolean now make sure
- 16:18you're not putting this within quotes
- 16:20because then it's technically a string
- 16:22and they behave differently
- 16:23now the data type is a string and you
- 16:26may have noticed as well that the font
- 16:27color has changed between
- 16:29strings and boolean values so that's
- 16:31another good indicator
- 16:32now what if you need to display your
- 16:35value along with a string using string
- 16:37concatenation
- 16:38well guess what we're going to typecast
- 16:39again so let's print something
- 16:44r u a human
- 16:48so we need to use string concatenation
- 16:50so i will convert
- 16:52my boolean variable of human to a string
- 16:55and i have been programmed to inform you
- 16:58that
- 16:59yes i am in fact a human that is true
- 17:01well everyone
- 17:02that is the basics of variables it is a
- 17:05container for a value and they behave
- 17:07as the value that they contain there are
- 17:09four basic data types
- 17:11strings which store a series of
- 17:13characters inc
- 17:14which store a whole integer floats which
- 17:18are floating point numbers a numeric
- 17:20value with a decimal
- 17:21and booleans which only store true or
- 17:24false
- 17:25and they're very useful with if
- 17:27statements so that is it for variables
- 17:29if you would like a copy of all this
- 17:31code i will copy and paste all of this
- 17:33to the comments section down below
- 17:35but yeah that is how variables work in
- 17:37python
- 17:38hey people what's going on it's bro hope
- 17:41you're doing well
- 17:41and in this video i'm going to explain
- 17:43multiple assignment in python so
- 17:45sit back relax and enjoy the show
- 17:50well then since you clicked on this
- 17:52video i should probably explain what
- 17:53multiple assignment is
- 17:55now multiple assignment allows us to
- 17:57assign multiple variables
- 17:59at the same time using one line of code
- 18:02here's an example of us
- 18:03using standard assignment let's say we
- 18:05have a variable
- 18:06name and i will set this to a value of
- 18:09my name but you can use your name for
- 18:11this example
- 18:12let's say edge equals 21 and how about a
- 18:15variable called
- 18:16attractive i think i'm going to set this
- 18:19to true
- 18:20okay so we have a bunch of variables and
- 18:22then we can print
- 18:23the value of these variables with some
- 18:25print statements
- 18:26so let's print name age and attractive
- 18:30so we have name age attractive
- 18:34and as you would expect this prints bro
- 18:3721 and true
- 18:38now another way in which we could write
- 18:40the same code is to use
- 18:42multiple assignment and this allows us
- 18:44to assign multiple variables
- 18:46at the same time using one line of code
- 18:48so i'm going to turn
- 18:50all of these lines into comments and
- 18:52this time
- 18:53we will only use one line of code but to
- 18:55do this we're going to list all of our
- 18:57variables
- 18:58separated with a comma so that would be
- 19:00name
- 19:01comma edge comma attractive
- 19:05and we will set them equal to those
- 19:08values
- 19:08but in the same order separated by
- 19:10commas so that would be
- 19:12bro comma 21 comma true
- 19:15and this would do the same thing only
- 19:17using one line of code
- 19:19here's another example let's say we have
- 19:21four variables
- 19:22spongebob patrick sandy and squidward
- 19:25and
- 19:25let's pretend that they're all around
- 19:27maybe 30 years old
- 19:28at least it seems like they're around 30
- 19:30years old but i could be wrong
- 19:31i'm not too familiar with spongebob lore
- 19:34so let's set them all equal to 30.
- 19:36now we could write this using four lines
- 19:39of code or
- 19:40we could use multiple assignment to
- 19:42write the same thing
- 19:43using one line of code so if all of
- 19:45these variables
- 19:46are all equal to the same value there's
- 19:49a little bit of different syntax here
- 19:51so let's begin with the first variable
- 19:53of spongebob
- 19:54equals patrick
- 19:59equals sandy equals
- 20:02squidward then we're going to set them
- 20:06all equal to 30 and
- 20:09this should work just fine using only
- 20:11one line of code
- 20:12well everybody that's pretty much it for
- 20:14multiple assignment it allows us to
- 20:15assign
- 20:16multiple variables at the same time
- 20:18using one line of code
- 20:20i will post all of this code in the
- 20:22comments section down below
- 20:23and well yeah that's how multiple
- 20:25assignment works in python
- 20:27hey what's going on everybody it's bro
- 20:29hope you're doing well
- 20:30and in this video i'm going to
- 20:31demonstrate some useful string methods
- 20:33in python so
- 20:34sit back relax and enjoy the show
- 20:39all right let's do this in this video
- 20:42i'm going to be explaining and
- 20:43demonstrating
- 20:44a few useful methods available to us for
- 20:47strings
- 20:47now let's create a variable called name
- 20:49for this example and you can use your
- 20:51own name if you want
- 20:52so we now have a variable called name
- 20:54and for my first example
- 20:56let's print the length of our name
- 20:59variable
- 21:00so to do that we can surround our name
- 21:03variable with the length method and this
- 21:05will print
- 21:06and return how long the length of our
- 21:09string is
- 21:09and for me the length of my name
- 21:11variable is three
- 21:13for three characters if i were to
- 21:15increase the size of my name
- 21:17well then that's going to change to a so
- 21:19that is
- 21:20the length method and i'm just going to
- 21:22turn this line into a comment
- 21:24and let's move on so next we have the
- 21:26find method
- 21:27i just copy and pasted print name so i
- 21:30can reuse it
- 21:31name dot and actually if you have
- 21:33intellisense setup
- 21:34if you add dot after your variable
- 21:37you'll get a suggestion or a few
- 21:39suggestions of
- 21:40different methods and other features
- 21:42that you have access to so
- 21:44let's find find which is
- 21:48uh screw this i'll just type find find
- 21:51then we can find the first index of
- 21:54where this character is
- 21:55let's say i would like to find where
- 21:57capital b is
- 21:58well that is at index zero because
- 22:01computers always start with zero
- 22:03so this first character would be zero
- 22:05then r
- 22:06would be one and then o would be
- 22:10two for this example all right so that
- 22:12is how to find a character
- 22:14within a string moving on so let's
- 22:17capitalize our name and i should
- 22:19probably make this lowercase
- 22:23so name dot capitalize
- 22:26and this will make my name capitalized
- 22:29but if there's any spaces
- 22:31and then additional words it's not going
- 22:33to capitalize that
- 22:34only the first letter and your string
- 22:37next we have upper this will make your
- 22:40string all uppercase
- 22:42name upper and now
- 22:45it is uppercase guess what's next
- 22:49lower name dot lower
- 22:52and i should probably make this capital
- 22:54again
- 22:57and everything is lowercase
- 23:00we're making some good progress today
- 23:02next we have
- 23:04is digit this will return true or false
- 23:07depending on if our string is a digit
- 23:11which it's not so this will print false
- 23:14if i changed my string to a bunch of
- 23:15numbers like 1 2
- 23:173 then this will return true let's
- 23:19change that back
- 23:20and move on next we have
- 23:25is alpha
- 23:28are these alphabetical characters
- 23:33oh make sure you add the parentheses
- 23:35afterwards my bad
- 23:37false because we added a space within
- 23:41here
- 23:42now it's true so you can use is alpha to
- 23:46check to see
- 23:46if your string contains only letters
- 23:49alphabetical letters
- 23:51next we have count
- 23:55we can count how many characters are
- 23:57within our string
- 23:58i would like to count how many o's are
- 24:00in here and this should return
- 24:02two that's right okay we're almost at
- 24:06the end
- 24:07we can replace characters within our
- 24:09string
- 24:10so we use the replace method
- 24:13and we pass in two arguments the
- 24:16character we would like to
- 24:18replace and the character we would like
- 24:20to replace our character with
- 24:21i would like to change all o's to a's
- 24:24and now
- 24:25my name is brock hod oh you guys are
- 24:28gonna like this next one
- 24:29this isn't technically a method but it's
- 24:31a really neat feature
- 24:33so one thing that we can do that's
- 24:34available to us with python is that we
- 24:36can display a string
- 24:38multiple times by multiplying that
- 24:40string by a given number
- 24:42so let's say i would like to display my
- 24:44name three times i can just take my
- 24:46string variable
- 24:47then add times three and this will print
- 24:49my name
- 24:50three times bro bro bro like i said not
- 24:53technically
- 24:54a method but it's a useful feature that
- 24:56you might be interested in that's
- 24:57related
- 24:58all right everybody so that is a few
- 25:01useful methods available to us for
- 25:02strings if you would like a copy of all
- 25:04this code
- 25:05i will post this in the comments section
- 25:07down below but yeah those are
- 25:09a few useful methods available to us for
- 25:12strings
- 25:13hey what's going on everybody it's you
- 25:14bro hope you're doing well and in this
- 25:16video i'm going to teach you guys all
- 25:18about typecasting in python so
- 25:19sit back relax and enjoy the show
- 25:24all right welcome back everybody i have
- 25:26a super quick video for you guys today
- 25:28on the subject of typecasting now
- 25:30typecasting is the ability to convert
- 25:32the data type of a value to another data
- 25:35type
- 25:35here's an example that i've written
- 25:36already i'm just going to paste it
- 25:38i have three variables for this example
- 25:41x y
- 25:42and z x contains one this is a whole
- 25:45integer
- 25:46into for short y contains two point zero
- 25:48this is a floating point number
- 25:50float four short you can tell it's a
- 25:52float because it ends with a decimal
- 25:53portion
- 25:54and z contains a string of three a
- 25:57string
- 25:58is a series of characters and you can
- 26:00tell it's a string because it's
- 26:01surrounded with a set of quotes
- 26:03and remember with strings we cannot
- 26:05normally perform math on strings
- 26:08so i have three print statements for x y
- 26:11and z and as you would expect they
- 26:13contain the values of
- 26:14one two point zero and three now let's
- 26:17begin by converting both y
- 26:18and z to the integer data type via
- 26:21typecasting in order to perform
- 26:23typecasting
- 26:24on a value or variable type that value
- 26:27or variable and surround this with a set
- 26:28of parentheses whoops i missed and then
- 26:32pre-save this
- 26:32with the data type you'd like to convert
- 26:34this value or variable to
- 26:36i would like to convert y to an integer
- 26:39and then you can use this for whatever
- 26:40you need
- 26:41so i'm going to replace y within my
- 26:43print statement
- 26:44with y after we typecast it as
- 26:47an integer and this will display 1 2
- 26:50not 2.0 it's the whole integer
- 26:54of 2 and then 3. so this is not a
- 26:57permanent change
- 26:58if you need to make this as a permanent
- 27:00change to y
- 27:02you'd have to reassign it so let's say y
- 27:05equals y and then we're going to
- 27:07typecast y
- 27:09so let's round this with a set of
- 27:10parentheses and precede this with the
- 27:12data type we'd like to convert
- 27:14y2 so now y is a whole
- 27:17integer and then we will print y without
- 27:20typecast this time and our result is
- 27:23one two three now z z is storing a
- 27:27string and with strings we cannot
- 27:29normally perform any math on strings
- 27:31for example within my print statement i
- 27:33will state z
- 27:35times three you would think that the
- 27:37result would be nine right
- 27:39well you're wrong it's going to print
- 27:41three three times
- 27:42if you need to convert a string to a int
- 27:46or a float you would just have to
- 27:47typecast it so this time let's type cast
- 27:50z as an integer z
- 27:53int and we will type cast z as an
- 27:56integer so then if we have z times three
- 28:00the result is now 9 instead of 333
- 28:04so that's the basic formula for type
- 28:06casting now let's convert these to
- 28:08floating point numbers but let's include
- 28:11x as well because
- 28:12x is feeling left out so let's convert x
- 28:14to
- 28:15a floating point number so float
- 28:17parenthesis and we will surround
- 28:19this with x and display x
- 28:22and actually let's convert y and z to
- 28:26floats as well because why not
- 28:29the results are now 1.0 2.0 and 9.0
- 28:33remember that we multiplied z times
- 28:35three
- 28:36and then if we need to convert these
- 28:38values to a string
- 28:41we would use the string cast
- 28:46so the results now are 1 2.0
- 28:49and 333 now you might be thinking to
- 28:52yourself
- 28:53when would you need to convert an
- 28:55integer or a float to a string
- 28:57here's a situation let's say within our
- 28:59print statements we need to print
- 29:02these variables along with another
- 29:04string so within our print statement
- 29:06let's say x is plus
- 29:09x we would like to display these
- 29:11together
- 29:12and let me copy this and do the same
- 29:14thing for y
- 29:15we'll say y is plus y
- 29:18you cannot normally display a string
- 29:21along with an integer
- 29:22or a float because we're using some
- 29:24string concatenation
- 29:26so this will result in a compilation
- 29:28error can only concatenate
- 29:30string not int to string so that's a
- 29:33situation
- 29:34in which you might need to use
- 29:36typecasting to convert an
- 29:38integer or float to a string so let's
- 29:40display
- 29:41x and y as strings via typecasting
- 29:45so we'll surround x and y with a set of
- 29:47parentheses and precede this with the
- 29:49data type we'd like to convert these
- 29:50values to
- 29:51we would like to convert x and y to both
- 29:53strings and now this will compile and
- 29:55run
- 29:56just fine well everybody that's the
- 29:58basics of typecasting
- 30:00if you would like a copy of all my code
- 30:02i will post everything in the comments
- 30:03down below
- 30:04if you found this video helpful don't be
- 30:06afraid to help me out by smashing that
- 30:08like button
- 30:09drop a comment down below and subscribe
- 30:11if you'd like to become a fellow bro
- 30:14hey what's going on everybody it's bro
- 30:16hope you're doing well
- 30:17and in this video i'm going to teach you
- 30:18guys how we can accept some user input
- 30:20in python so sit
- 30:22back relax and enjoy the show
- 30:26ladies and gentlemen this is where the
- 30:27fun begins in this video i'm going to
- 30:30explain how we can accept some user
- 30:31input in python
- 30:32and we do so by using the input function
- 30:35and then we type in our input to the
- 30:37console window
- 30:38but it would probably be good practice
- 30:40to let the user know what we want them
- 30:41to type in exactly
- 30:43so let's ask for somebody's name within
- 30:45quotes inside the input function i'm
- 30:47going to type
- 30:48what is your name and now
- 30:52our program will wait for us to enter in
- 30:54some user input
- 30:55so you can use your own name for this
- 30:57example i'm going to type bro
- 30:58now to submit some user input you just
- 31:00type or press
- 31:02enter now you can see that my process
- 31:04finished with exit code zero that means
- 31:06the program is done running
- 31:08so we should probably do something with
- 31:09this value we can actually assign it to
- 31:11a variable so we can use it elsewhere in
- 31:13our program
- 31:14so to do so we precede input with
- 31:17a variable like name works name equals
- 31:20input
- 31:20and when we accept some user input we
- 31:22will assign it to this variable that we
- 31:24called them
- 31:25and now we can do something with it so
- 31:27let's print a message
- 31:28involving our name variable that we have
- 31:31so how about
- 31:32hello plus name
- 31:35now when i run this program we can
- 31:37accept some user input
- 31:38and then do something with that input
- 31:40such as display message that says
- 31:42hello bro and then you can type in
- 31:44whatever you want for your name
- 31:45let's say i'm dude what is your name
- 31:48dude hello dude
- 31:49now this next part is very important
- 31:51when we accept user input it is always
- 31:54of the string
- 31:55data type a series of characters what if
- 31:58we need to accept a number
- 32:00well you can but you can't normally
- 32:01perform any math on it because it's of
- 32:04the string data type
- 32:05we would need to cast it as either of
- 32:07the integer data type
- 32:09or of the float data type so here's an
- 32:11example of why we can't perform any math
- 32:14on a string let's say we have a variable
- 32:16called
- 32:17edge age equals input and let's
- 32:20ask how old somebody is how old
- 32:23are you now i will add one to somebody's
- 32:28age let's pretend it's their birthday
- 32:30age equals age plus one this will add
- 32:32one to their age
- 32:33now let's try and run this what is your
- 32:35name bro
- 32:37how old are you let's say that i'm 21.
- 32:40okay we ran into an error a type error
- 32:42can only concatenate string to string
- 32:46with what i mentioned previously we
- 32:47cannot normally use
- 32:49strings for any sort of mathematical
- 32:51equation or operation
- 32:53we'll want to cast it to the integer or
- 32:55the float
- 32:56data type so let's cast our user input
- 32:59to the integer data type and we will
- 33:01surround our user input with a cast
- 33:04surround your input with a set of
- 33:06parenthesis and precede this with
- 33:08int and now we can use this user input
- 33:11in a mathematical operation
- 33:13so let's try it even though this won't
- 33:15display age yet so
- 33:16what is your name bro 21 and our program
- 33:20finished with
- 33:20an x a code of zero there were no
- 33:22problems now let's attempt to display
- 33:24this edge
- 33:25so i will print
- 33:28u r plus
- 33:31age plus years
- 33:35old we're doing some string
- 33:36concatenation here
- 33:38what is your name bro how old are you 21
- 33:42all right we ran into a type error again
- 33:44can only concatenate string to string
- 33:47you cannot normally display a variable
- 33:50of the integer data type
- 33:52along with strings because we're doing
- 33:54string concatenation we're adding
- 33:55strings together
- 33:57we would need to convert this variable
- 33:59back to the string
- 34:00data type so we can cast it we'll
- 34:02surround
- 34:03our edge variable with a cast now this
- 34:05is what's going to happen we will accept
- 34:07some user input it will be of the string
- 34:09data type and then we will
- 34:10immediately cast it to be of the integer
- 34:13data type so that we can treat it
- 34:15as a number instead of a character and
- 34:17then if we need to display this
- 34:19edge we will need to convert it back to
- 34:21a string so that we can use
- 34:23some string concatenation and display
- 34:25all of these strings together
- 34:27and now let's try this theory so what is
- 34:29your name bro
- 34:30how old are you let's say i'm 21 hello
- 34:33bro you
- 34:34are 22 years old here's a situation what
- 34:36if we enter
- 34:37a number that contains a decimal portion
- 34:39so let's try this again
- 34:41let's say i'm 21.5 years old
- 34:44well now we ran into a different issue a
- 34:47value error
- 34:48invalid literal for int with base 10
- 34:51that means if we cast a string as an
- 34:54integer
- 34:54it can only be a whole number a portion
- 34:57that does not contain
- 34:58a decimal and that is where the float
- 35:01data type would come in
- 35:02it is a data type that can contain a
- 35:05decimal portion
- 35:06for this example let's create a third
- 35:09variable called height and we will ask
- 35:11for somebody's height
- 35:12input we'll create a prompt that says
- 35:16how tall are you
- 35:21and we will cast this to be of the float
- 35:24data type
- 35:25so that our string our user input
- 35:28can contain a decimal portion and then
- 35:31we can treat it as a number instead of a
- 35:33series of characters
- 35:34and i think i will delete this line we
- 35:36won't really need it anymore
- 35:38and then let's print their height along
- 35:40with a message
- 35:42u r plus then
- 35:45height but we need to cast this back to
- 35:47being a string
- 35:50so we'll surround our height with a cast
- 35:54plus let's say cm
- 35:57tall all right let's test this
- 36:00what is your name bro how old are you
- 36:03let's say i'm 21 how tall are you let's
- 36:06pretend that i'm 250.5 centimeters tall
- 36:12i'm adding the 0.5 just to test to see
- 36:14if this will accept a floating point
- 36:15number
- 36:17hello bro you are 21 years old you are
- 36:21250.5 centimeters tall
- 36:23not really all right everybody so that
- 36:25is the basics of
- 36:26user input in python normally with user
- 36:29input it will give you
- 36:30a value of the string data type if you
- 36:33need to use that value for any sort of
- 36:35math
- 36:36you'll probably need to cast it to be of
- 36:38the integer or the float data
- 36:40type so if you would like a copy of this
- 36:42code i will post this in the comment
- 36:44section down below
- 36:45but yeah that's how to accept user input
- 36:47in python
- 36:49hey what's going on everybody it's bro
- 36:52hope you're doing well and in this video
- 36:53i'm going to demonstrate a few useful
- 36:55functions related to numbers in python
- 36:57so
- 36:58sit back relax and enjoy the show
- 37:03okay let's begin in this video i'm going
- 37:05to be demonstrating a bunch of useful
- 37:07functions related to numbers
- 37:09and a bunch of these functions are
- 37:11located within the math
- 37:13module now let's create a variable for
- 37:15this example let's say
- 37:17pi equals 3.14 just the first few digits
- 37:21of pi is fine for this example
- 37:23and now here's a bunch of useful
- 37:25functions that you guys would probably
- 37:26be interested in
- 37:28the first is round round is a built-in
- 37:30function and all we do is type round
- 37:32then add a set of parenthesis and then
- 37:35we can pass in
- 37:36a value or a variable as an argument and
- 37:39the round function
- 37:40will round that number for us so pi
- 37:43rounded to the nearest whole integer
- 37:45is you guessed it three now we can round
- 37:48a number up
- 37:49by using the seal function of the math
- 37:51class seal
- 37:52short for ceiling so let's print
- 37:55our result and in order to access this
- 37:58function within the math module
- 38:00we would type the name of the module dot
- 38:02and here's a bunch of functions that we
- 38:04have access to
- 38:05so we are looking for seal and it's
- 38:07actually at the top here for me
- 38:08and at seal we'll round a number up to
- 38:11the nearest whole integer
- 38:12so pi rounded up is four
- 38:16then on the flip side we can round a
- 38:18number down by using
- 38:19floor and that is also located within
- 38:21the math module
- 38:22so let's type math dot floor and
- 38:25pi rounded down would be three
- 38:29up next i have the absolute value
- 38:32function but it's abs
- 38:33for short abs will give you the absolute
- 38:36value of a number
- 38:38so the absolute value of pi actually
- 38:40let's make this negative 3.14
- 38:42the absolute value will tell you how far
- 38:45a number
- 38:46is away from zero so if you pass in a
- 38:48negative number
- 38:49it will give you a positive number so
- 38:52that is
- 38:52abs short for absolute value next we
- 38:55have
- 38:56the pow function pow will raise
- 38:59a base number to a power let's raise
- 39:03pi to the power of two so we pass in
- 39:06two arguments a base and an exponent
- 39:09pi to the power of two is nine point
- 39:11eight
- 39:12and some change so that is the pow
- 39:15function
- 39:16next we have square root
- 39:20so this is located within the math
- 39:22module we'll type
- 39:24math dot s q r
- 39:27t short for square root and the square
- 39:29root of pi
- 39:31is let's find out oh i guess we can't
- 39:34pass in pi
- 39:35uh let's round a different number like
- 39:38420
- 39:39oh i guess that's because i made pi
- 39:41negative okay so the square root of 420
- 39:43is
- 39:4420.4 and some change so that is the
- 39:47square
- 39:47root function all right we have two left
- 39:50we have the max function
- 39:52the max function will find the largest
- 39:54of a varying amount
- 39:56of values so let's create three
- 39:58variables for this example
- 40:00let's say x equals one y equals two
- 40:03and z equals three we can use the max
- 40:07function to find
- 40:08the largest of these values so within a
- 40:10print statement
- 40:11let's use the max function and pass in
- 40:15all of the different values or variables
- 40:17that you would like to compare
- 40:18so i would like to find the largest
- 40:21value between
- 40:22x y and z and the result in this example
- 40:26is three now the min function will find
- 40:29the lowest
- 40:30so min and then pass in your values or
- 40:34variables
- 40:35so i would like to find the minimum
- 40:36value between x
- 40:38y and z and in this case it is one
- 40:42well everybody those are a few useful
- 40:45functions that you guys might be
- 40:46interested in
- 40:47if you guys would like a copy of this
- 40:49code i will post this in the comments
- 40:50section down below
- 40:52but yeah those are a few useful
- 40:54functions related to numbers
- 40:56in python hey what's going on everybody
- 41:00it's bro hope you're doing well and in
- 41:02this video i'm going to explain string
- 41:04slicing in python so sit back
- 41:06relax and enjoy the show
- 41:10all right then let's talk about slicing
- 41:12slicing can be used to create a
- 41:14substring by extracting elements from
- 41:16another string
- 41:17the same principle can also be applied
- 41:19to different collections as well
- 41:21but in this video we're going to be
- 41:22focusing on slicing strings
- 41:24now to slice a string we can either use
- 41:27the indexing operator
- 41:29which is a set of square brackets or by
- 41:31use of the slice function
- 41:33to create a slice object this is a
- 41:35little more complex
- 41:36we'll discuss the slice function near
- 41:38the end of this video
- 41:39so with slicing there are three optional
- 41:43arguments
- 41:43three fields that we can fill in
- 41:45depending on where
- 41:46and how we want to slice our string a
- 41:49starting index
- 41:50a stopping index and a step let's begin
- 41:53with the starting index
- 41:54now for this example let's say we have a
- 41:57string
- 41:58variable called name and this will store
- 42:00a value of whatever your
- 42:02own full name is yes i legally change my
- 42:04name to bro code you can use your own
- 42:06name for this if you want
- 42:07so what i would like to do is to create
- 42:09a substring
- 42:10based off a sliced portion of my full
- 42:13name
- 42:14so what i would like to do is create a
- 42:16substring based off of just the first
- 42:18part of my name just bro
- 42:20so let's begin by creating a substring
- 42:23maybe i'll call this first
- 42:26name equals now to slice a string
- 42:30type in a string or the name of a
- 42:33variable that contains your string
- 42:34and follow this with the indexing
- 42:36operator and like i said we'll discuss
- 42:38the slice function a little bit later so
- 42:41we need
- 42:42a starting index and a stopping index
- 42:45but we'll begin with the starting index
- 42:47where do we want to begin
- 42:49our slice so computers they always start
- 42:51with zero
- 42:52so the first character in your string
- 42:54will have an index
- 42:56of zero i would like my slice to begin
- 42:58at index 0
- 42:59so i'll list that for the starting
- 43:01position the starting index
- 43:03so let's test this just to be sure it
- 43:05works i'll just print
- 43:07first name and let's see if this prints
- 43:10capital b
- 43:11which it does so following that same
- 43:13pattern the next character
- 43:15within my string would have an index of
- 43:18one that would be
- 43:19r then the next index two would be
- 43:22o and three should be an empty space
- 43:26all right so that is the starting index
- 43:29within string slicing if we would like
- 43:31to slice an entire portion of our string
- 43:34not just one letter we need a stopping
- 43:37index as well
- 43:38and both of these fields are separated
- 43:40with a colon
- 43:41so let's say i would like to slice the
- 43:43first three characters of my string
- 43:46so this would have an index of zero one
- 43:50two and this isn't going to work as you
- 43:52would imagine and i'll explain
- 43:54why so you would think that indexes
- 43:570 through 2 would give us three letters
- 43:59right but it only gives us two
- 44:01so this first index is inclusive
- 44:05the stopping index is exclusive so if i
- 44:08want the first
- 44:09three characters the first three letters
- 44:12in the string
- 44:12i would need the starting index to be
- 44:15zero
- 44:16and the stopping index to be three and
- 44:18now this should print
- 44:19my first name and not anything else
- 44:22there is a shorthand way of writing this
- 44:24too
- 44:24so if you were to leave this first index
- 44:27blank
- 44:28for start well python is going to assume
- 44:30that this is zero
- 44:31the beginning of your string so this
- 44:34would be
- 44:35no different than what we had previously
- 44:37just having colon
- 44:38three now let's try and create a
- 44:41separate substring
- 44:42for a last name so last
- 44:45name name now we need a
- 44:49starting index and a stopping index so
- 44:52we need the starting index at least so
- 44:54this would be
- 44:56zero one two three four
- 44:59four colon and where do we want this to
- 45:01stop
- 45:02zero one two three four five
- 45:05six seven and remember that this is
- 45:08exclusive
- 45:09so we're going to place eight here and
- 45:11now i would like to display
- 45:13last name and this should just display
- 45:16code
- 45:16and a shortcut is that if you were to
- 45:19leave
- 45:20stop blank but you still have that colon
- 45:22this is a shorthand way of writing
- 45:24i would like every character starting at
- 45:27index four
- 45:28and everything after until the end so
- 45:30there would be no change to this
- 45:32as well you can write it either way okay
- 45:34so we still have to discuss
- 45:36step step is an optional field that we
- 45:38can set a value to
- 45:40so step is how much we're increasing our
- 45:42index by
- 45:43between starting and stopping so it is
- 45:46entirely possible
- 45:48to create a substring that will count
- 45:50only
- 45:51every second character after the first
- 45:53normally step is one by default
- 45:55but if we were to set that to two we're
- 45:57going to count only
- 45:58every second character so let's try this
- 46:01maybe i'll create a new
- 46:02variable called funky name because i
- 46:05really don't know what else to call this
- 46:06for this example
- 46:07let's say funky name equals name and i'm
- 46:10going to count
- 46:11only every second character including
- 46:14the first
- 46:14so the indexes would be zero colon
- 46:18eight for my example but if you're using
- 46:20your own name that's probably going to
- 46:21be something different
- 46:23so the default first step is one and if
- 46:26i were to display funky name as it
- 46:28is currently this will just display my
- 46:30full name
- 46:31but if we set step to two this will only
- 46:33display
- 46:34every second character including the
- 46:36first so funky name is now
- 46:38for me at least b o c d now if step
- 46:42was three this will print only every
- 46:44third character including the first
- 46:46where it's b space d a shorthand way of
- 46:50writing this
- 46:50is that you could leave start and stop
- 46:53empty but the important thing is to have
- 46:55these two colons
- 46:57python is going to assume that if you
- 46:59leave your starting index
- 47:00empty you're referring to index 0 the
- 47:03beginning
- 47:04if your second index your stopping index
- 47:06is empty
- 47:07python will assume this will be the very
- 47:10end of your string
- 47:11so just having two colons and then step
- 47:13three would be
- 47:14no different than what we had previously
- 47:16or this next part i'm going to explain
- 47:19how we can reverse a string in python so
- 47:22let's create a new substring variable
- 47:24called
- 47:24reversed name equals name i would like
- 47:27to use the entire string
- 47:29so i can just use two colons python will
- 47:32fill in the rest
- 47:33so for the step this is going to be
- 47:35negative one
- 47:36it's kind of like we're counting
- 47:38backwards and we're going to create
- 47:40a new sub string based off my name but
- 47:43in reverse
- 47:44so if i were to print my reversed name
- 47:47this is now
- 47:48edok orb i think that's how you
- 47:50pronounce it
- 47:51so that's how to reverse a string in
- 47:53python using slicing
- 47:55now let's move on to part two of this
- 47:57video i'm going to be explaining the
- 47:59slice function and we can use the slice
- 48:01function to create a slice
- 48:03object which is actually reusable so
- 48:05that's somewhat convenient
- 48:07let's take a different scenario this
- 48:08time let's say we have a website url
- 48:11website equals http colon2forwardslashes
- 48:16google.com but use whatever website you
- 48:18want what i would like to do for this
- 48:20program
- 48:20is remove and create a substring based
- 48:23off the website name
- 48:24and not anything else i would like to
- 48:27exclude the http portion as well as the
- 48:30dot-com portion
- 48:31of this website url so let's break it
- 48:34down step by step
- 48:35our first step is to create a slice
- 48:37object so i will name the slice object
- 48:40as slice
- 48:40slice equals and i will invoke the slice
- 48:43function
- 48:44so within the parentheses of the slice
- 48:46function we can add up to
- 48:48three values a start a stop and a step
- 48:52exactly the same as what we did with
- 48:53indexing however with the slice function
- 48:56we separate each of these values with a
- 48:59comma
- 48:59instead of a colon but it works the same
- 49:01way so we would like
- 49:03our substring to begin where the name of
- 49:05the website begins
- 49:06so we would like our start to be zero
- 49:10one two three four five six seven
- 49:13so i will pass in seven for where my
- 49:15substring will begin
- 49:17and now each value within the slice
- 49:19function will be separated with
- 49:21a comma instead of a colon like what we
- 49:23did with the indexing operator
- 49:25now for the stop index that's going to
- 49:28be a little bit tricky
- 49:29and let me explain why one issue that
- 49:31we're going to run into
- 49:32is that not all website names are
- 49:34consistent with their length they can
- 49:36really vary
- 49:37so it is not possible for us to use the
- 49:40same stopping index
- 49:41for each of these website urls so one
- 49:44way in which we can account for that
- 49:46is to use what is called a negative
- 49:48index each character within a string
- 49:51has a positive index as well as a
- 49:53negative index
- 49:54and a negative index works almost
- 49:57exactly the same way
- 49:58except the character most on the right
- 50:01begins with a negative index
- 50:03of minus one and the character on the
- 50:06left
- 50:06of that would be negative two negative
- 50:09three and negative four
- 50:10it's as if you're counting backwards so
- 50:12with indexing we can use some
- 50:14combination of
- 50:15positive and negative indexing so i
- 50:18would like
- 50:19my substring to end where this dot is so
- 50:22this would have
- 50:23a index of minus one minus two
- 50:26minus three minus four and remember that
- 50:28this is exclusive
- 50:30so i would like my slice to begin at
- 50:32index seven
- 50:34and a negative index of four for the
- 50:37stopping position
- 50:38so that will give me just the website
- 50:40name and remove
- 50:41the http portion as well as the dot com
- 50:44portion of my website url
- 50:47and now we have a slice object and we
- 50:49can reuse this
- 50:50so to apply your slice object type in
- 50:53the name your string
- 50:54index operator and place your slice
- 50:57right within here
- 50:58and let's print this so print
- 51:01our website and apply the slice to it
- 51:03and now this should give us
- 51:05just the website name and now let's test
- 51:07this with maybe another website
- 51:09i'll rename this as website one and
- 51:11let's create website2
- 51:14and let's try a different website name
- 51:17for goodmeasure
- 51:18so let's try wikipedia so we can reuse
- 51:22our slice object
- 51:23and we will apply our slice to website
- 51:26two this time
- 51:27and this should remove the url portions
- 51:30http as well as
- 51:32com all right everybody so that is
- 51:34slicing strings
- 51:36in python you can either use the
- 51:38indexing operator
- 51:39or the slice function to create a slice
- 51:42object
- 51:43if you would like a copy of all this
- 51:44code i will post all of this in the
- 51:46comments section down below
- 51:48but yeah that is how string slicing
- 51:50works in python
- 51:52hey what's going on people it's you bro
- 51:54hope you're doing well and in this video
- 51:56i'm going to explain
- 51:57if statements else if statements and
- 51:59else statements and python so
- 52:01sit back relax and enjoy the show
- 52:06all right people let's talk about if
- 52:08statements an if statement is a block of
- 52:10code
- 52:11that will execute only if its condition
- 52:13is true
- 52:14it's a very basic form of decision
- 52:16making with programming
- 52:18for this example let's create a prompt
- 52:21where we will ask a user
- 52:22for their age and depending on what
- 52:24their age is we will print a response
- 52:26so let's say age equals input and then
- 52:29we will add a prompt
- 52:30how old are you now this will return a
- 52:33string
- 52:33so we should probably cast this as an
- 52:36integer data type
- 52:37so we now have a variable called age
- 52:39where we will accept some user input
- 52:42let's check somebody's age with a
- 52:43variety of if statements
- 52:45so let's check to see if somebody's age
- 52:47is greater than or equal to 18
- 52:49so to create an if statement type if and
- 52:51then some sort of condition
- 52:53if age is greater than or equal to 18
- 52:56to finish this if statement add a colon
- 52:58at the end
- 52:59now pay attention to this if i move down
- 53:01to the next line you'll notice that my
- 53:03cursor is now
- 53:04indented any indented code underneath an
- 53:07if statement is the block of code for
- 53:10that if statement
- 53:11so if this condition is true we will
- 53:13execute some
- 53:14block of code if it's false we'll skip
- 53:17over it so if somebody's age is greater
- 53:19than or equal to 18
- 53:20let's print you are an
- 53:24adult and let's try this program how old
- 53:27are you
- 53:28let's say i'm 21 so my condition when we
- 53:31get to the if statement
- 53:32is going to check to see if this
- 53:33condition is true if it is true we're
- 53:35going to
- 53:36execute this block of code if it's not
- 53:38we skip over it
- 53:39so how old are you let's say i'm 21 i'm
- 53:42going to hit enter
- 53:43and my if statement is true so we
- 53:45execute this block of code which prints
- 53:48you are an adult so what if this
- 53:49condition is false
- 53:51let's say i'm 12. well then we're just
- 53:53going to skip over this block of code
- 53:54and continue on with the rest of the
- 53:56program
- 53:56if you would like your program to take
- 53:58some other course of action
- 54:00if this condition is false we can add an
- 54:02else
- 54:03statement so after the if statement add
- 54:06else
- 54:06make sure you get the indentation right
- 54:08you don't want this within the block of
- 54:10code for the if statement
- 54:11so if this condition is false we can
- 54:14take
- 54:15some other course of action so if
- 54:17somebody's age
- 54:18is less than 18 let's print
- 54:22you are a child
- 54:26so let's try this again i'm going to
- 54:28enter that i
- 54:29am 12 years old not really but let's
- 54:31just pretend i am
- 54:33so this will now print you are a child
- 54:35if
- 54:36this condition is false perform this
- 54:38block of code for the else statement
- 54:40so with if statements we can check more
- 54:43than one condition before reaching the
- 54:45else statement by using an
- 54:46else if statement and we add that after
- 54:50an if statement so it's shortened to e l
- 54:53i f for else if so let's check to see if
- 54:56somebody's age
- 54:57is less than zero for some reason
- 55:00so let's print
- 55:03you haven't been born yet
- 55:08so with if statements we start at the
- 55:11top we first check
- 55:12the if statement if this is false we
- 55:14move down to the next
- 55:15statement to check then we check our
- 55:18else if statement
- 55:19if this is also false then we resort to
- 55:21the else statement
- 55:23the else statement is sort of like a
- 55:25last resort if all conditions
- 55:27evaluate to be false then we execute the
- 55:30else statement
- 55:31so let's try this how old are you let's
- 55:33say i'm negative
- 55:341 years old you haven't been born yet
- 55:38so with if statements we always begin
- 55:40with an if statement and we check
- 55:42that condition if that condition
- 55:44evaluates to be false
- 55:46then we will skip this block of code and
- 55:48move down to the else if statement
- 55:50if there is one it's optional if this
- 55:53condition
- 55:54is also false then we will use our else
- 55:56statement as a last resort
- 55:58if there is one if there is no else
- 56:00statement then we just skip over all of
- 56:02this then and continue on with the rest
- 56:04of the program
- 56:05so you can add more than one else if
- 56:07statement so let's check something else
- 56:10let's check to see else if age
- 56:14is equal to 100 so if you need to check
- 56:17to see
- 56:18if a value is equal to a particular
- 56:21value
- 56:21make sure you use double equal signs
- 56:24this is the comparison
- 56:25operator for equality if you use just
- 56:29one equal sign that's the assignment
- 56:31operator and
- 56:32python thinks you're attempting to set
- 56:34age equal to 100
- 56:35so if you want to check to see if age is
- 56:38equal to 100
- 56:39then use double equals so let's check to
- 56:42see if somebody's age is
- 56:44equal to 100 so let's print
- 56:48you are a century
- 56:52old now pay attention to this if i were
- 56:54to run this program
- 56:56how old are you let's say i'm 100. so
- 56:59this is printing you are an adult that's
- 57:02because we first check
- 57:03our if statement and well we're 100
- 57:06years old and 100
- 57:07is greater than or equal to 18. so we
- 57:10print
- 57:10this block of code we execute this block
- 57:12of code and then we skip
- 57:14everything else even though age is equal
- 57:17to 100 within
- 57:19our else if statement so the order of
- 57:22your if statements
- 57:23does matter so let's change this around
- 57:25let's add this
- 57:26to the very beginning and this will be
- 57:30our
- 57:30if statement and we will set age
- 57:33is greater than or equal to 18 to be an
- 57:35else if statement
- 57:36so we're first going to check to see if
- 57:38age is equal to 100
- 57:40if not we will check to see if age is
- 57:43greater than or equal to 18
- 57:45and we just follow that order so let's
- 57:47try this again how old are you
- 57:48i am 100 years old you are
- 57:52a century old so we first check our if
- 57:54statement
- 57:55if this condition is true we will
- 57:57execute this block of code
- 57:59if it's false we just go down the order
- 58:01till we reach our else statement
- 58:03well everybody that is the basics of if
- 58:06statements we have if statements
- 58:08else if statements and else statements
- 58:10so if you would like a copy of this code
- 58:12i will post this in the comments down
- 58:14below
- 58:15but yeah that is how if statements work
- 58:17in python
- 58:19hey what's going on everybody it's bro
- 58:21hope you're doing well in this video i'm
- 58:23going to explain
- 58:24logical operators in python so sit back
- 58:26relax
- 58:27and enjoy the show
- 58:30all right everybody let's talk about
- 58:32logical operators
- 58:33these are used to check if two or more
- 58:36conditional statements
- 58:37are true so we'll be discussing the and
- 58:40as well as the or
- 58:41logical operators there's also a third
- 58:44one called
- 58:44not but that works a little bit
- 58:46different and i'll explain why
- 58:48so let's create a program to demonstrate
- 58:50this let's create a program
- 58:51where we will ask somebody for the
- 58:53temperature outside and then depending
- 58:55on the temperature if it falls within a
- 58:56certain range
- 58:57we will print a message so we'll need to
- 58:59use and as well as or logical operators
- 59:01to do this
- 59:02so here i have a prompt temp for
- 59:04temperature equals input what is the
- 59:06temperature outside
- 59:08so i'm going to cast this as an integer
- 59:10data type
- 59:11so what if we want to check to see if
- 59:13our temperature is within a certain
- 59:15range
- 59:16we can use the and logical operator to
- 59:19check
- 59:19two or more conditional statements this
- 59:21is how we might do that
- 59:23let's check if temp
- 59:26is greater than or equal to zero zero
- 59:28degrees celsius
- 59:30and we can check another condition too
- 59:32if temp
- 59:34is less than or equal to 30 degrees
- 59:36celsius
- 59:38so we need to add a colon at the end to
- 59:40finish this if statement
- 59:42and let me just fix some of the spacing
- 59:44okay so
- 59:46with the and logical operator in order
- 59:48for this entire condition to be true
- 59:51both conditions must be true this needs
- 59:53to be true
- 59:54and this needs to be true so if our
- 59:57temperature
- 59:57is equal to or above 0 as well
- 1:00:01as being less than or equal to 30 that
- 1:00:04means our temperature
- 1:00:05falls within a certain range so let's
- 1:00:07print a message
- 1:00:08such as the temperature
- 1:00:12is good today
- 1:00:16print go
- 1:00:19outside all right let's try it
- 1:00:23so what is the temperature outside let's
- 1:00:25say it's 20 degrees celsius
- 1:00:27well the temperature is good today go
- 1:00:30outside
- 1:00:30this condition is true 20 is greater
- 1:00:33than or equal to zero
- 1:00:34and temperature is less than or equal to
- 1:00:3730.
- 1:00:38let's try a different temperature like
- 1:00:40negative 20 degrees celsius so it's
- 1:00:42really freaking cold outside right now
- 1:00:44as you can see nothing happened because
- 1:00:46our first condition
- 1:00:48was false our temperature is not greater
- 1:00:50than or equal to zero
- 1:00:52but our temperature is less than or
- 1:00:54equal to 30 so
- 1:00:56our second condition is true but our
- 1:00:58first condition was false
- 1:00:59and in order for this entire statement
- 1:01:01to be true since we're using the and
- 1:01:03logical operator
- 1:01:04both conditions must be true in order
- 1:01:06for this statement
- 1:01:07to be true now let's discuss the or
- 1:01:10logical operator
- 1:01:11we can check to see if our temperature
- 1:01:13is below
- 1:01:14or above a certain range so let's write
- 1:01:16else if
- 1:01:17temp is less than zero or
- 1:01:21temp is greater than 30. so that means
- 1:01:25it's going to be either
- 1:01:26very cold outside or very hot outside so
- 1:01:29let's print a message
- 1:01:31the temperature
- 1:01:35is bad today
- 1:01:39print stay
- 1:01:44inside all right let's try this again
- 1:01:49what is the temperature outside it is
- 1:01:50negative 20 degrees outside
- 1:01:53the temperature is bad today stay inside
- 1:01:55so with the
- 1:01:56orological operator as long as one of
- 1:01:59these conditions
- 1:02:00is true then the entire statement is
- 1:02:02true it doesn't matter if one of them is
- 1:02:04false as long as one of them is true
- 1:02:05then the entire statement
- 1:02:07is true last but not least we have the
- 1:02:09not logical operator this works a little
- 1:02:11bit different from the
- 1:02:12and as well as the or logical operator
- 1:02:14because we can check
- 1:02:16one or more conditional statements
- 1:02:18instead of two or more conditional
- 1:02:20statements
- 1:02:21so what the not logical operator will do
- 1:02:23is that it'll take
- 1:02:24a conditional statement if it's true
- 1:02:26it's going to flip it to false
- 1:02:28if it's normally false it's going to
- 1:02:29flip it to true so for this example we
- 1:02:31have another way in which we could write
- 1:02:33this
- 1:02:34although it's a little more complex so
- 1:02:36if our statement
- 1:02:37is true what we can do is that we can
- 1:02:39flip it to false using the
- 1:02:41not logical operator if it's normally
- 1:02:43false we can change it to true
- 1:02:45so what we'll do is surround one or more
- 1:02:47conditional statements with a set of
- 1:02:48parentheses
- 1:02:49and precede everything within the
- 1:02:51parentheses with the not
- 1:02:52logical operator so if the statement is
- 1:02:55normally true
- 1:02:56it's going to become false using the not
- 1:02:58logical operator
- 1:02:59if it's normally false it's going to be
- 1:03:01true then let's do the same with the
- 1:03:03else if statement
- 1:03:04so surround one or more conditional
- 1:03:06statements with the not
- 1:03:08logical operator so now these roles are
- 1:03:10going to be kind of reversed so let's
- 1:03:12try this again what is the temperature
- 1:03:13outside it is negative 20 degrees
- 1:03:15outside
- 1:03:16the temperature is good today go outside
- 1:03:18so what we should probably do
- 1:03:19is kind of reverse these rolls so i'm
- 1:03:22just going to swap
- 1:03:23everything within these if statements
- 1:03:25with each other
- 1:03:27and now this should work the same as it
- 1:03:29did before but we just wrote it a little
- 1:03:30bit different
- 1:03:31what is the temperature outside it is 15
- 1:03:34degrees outside
- 1:03:3515 degrees celsius the temperature is
- 1:03:37good today go outside
- 1:03:38so with the not logical operator you can
- 1:03:41surround one
- 1:03:42or more conditional statements with the
- 1:03:44not logical operator and what this will
- 1:03:46do
- 1:03:47is flip it from being false to true or
- 1:03:49from true to false
- 1:03:50well everybody that's the basics of
- 1:03:52logical operators in python if you would
- 1:03:55like a copy of all this code i will post
- 1:03:57this in the comments
- 1:03:58down below but yeah that's the basics of
- 1:04:00logical operators
- 1:04:02in python hey what's going on everybody
- 1:04:05it's bro hope you're doing well and in
- 1:04:07this video i'm going to explain while
- 1:04:08loops in python so sit back
- 1:04:10relax and enjoy the show
- 1:04:15so while loops a while loop is a
- 1:04:17statement that will execute its block of
- 1:04:19code
- 1:04:20as long as its condition remains true
- 1:04:23later on in this video for this example
- 1:04:25we're going to create
- 1:04:26a program where we will prompt a user to
- 1:04:28enter their name
- 1:04:29if they attempt to skip that prompt then
- 1:04:31we will continually ask the user to
- 1:04:33enter their name
- 1:04:34and they can't continue on with the rest
- 1:04:36of the program until they do so the key
- 1:04:37thing to keep in mind with while loops
- 1:04:39is that you'll want some way to
- 1:04:41eventually escape the while loop if you
- 1:04:42don't have any way to escape the while
- 1:04:44loop that's referred to as an
- 1:04:46infinite loop so let's create an example
- 1:04:48of an infinite loop because it's fun
- 1:04:50so to create a while loop type while and
- 1:04:52then some sort of condition
- 1:04:53so for our condition let's say while one
- 1:04:55is equal to one
- 1:04:56so this will always be true now what do
- 1:04:59we want to do
- 1:05:00when we execute this block of code let's
- 1:05:02print something
- 1:05:03print help i'm
- 1:05:06stuck in a loop all right so let's try
- 1:05:10this
- 1:05:10while one is equal to one print this
- 1:05:13statement
- 1:05:15and we will just continue on and on
- 1:05:17within this while loop because we have
- 1:05:19no way to escape this while loop
- 1:05:21so now let's use this concept and write
- 1:05:24a program where we will prompt a user to
- 1:05:26type in their name
- 1:05:27if they don't type in anything then we
- 1:05:29will continue to prompt them to type in
- 1:05:31at least something
- 1:05:32so let's say name is equal to
- 1:05:35and then to just keep this blank we'll
- 1:05:36just set this to a set of quotes
- 1:05:39and then let's write a while loop here
- 1:05:41while name
- 1:05:43and let's check the length of name if
- 1:05:45the length
- 1:05:46of our name is equal
- 1:05:49to zero then we will ask the user to
- 1:05:52type in their name
- 1:05:53name equals input
- 1:05:57enter your name
- 1:06:00so then once we escape the while loop
- 1:06:02let's print something
- 1:06:04print hello plus
- 1:06:07name whoops hello plus name
- 1:06:10alright so when i run this we are giving
- 1:06:12the user a way to escape the while loop
- 1:06:14they have to type in something for their
- 1:06:16name
- 1:06:16as long as the length of my name is
- 1:06:19equal to zero
- 1:06:20keep on printing this prompt enter your
- 1:06:23name
- 1:06:23so i'm just going to hit enter a bunch
- 1:06:25of times enter your name
- 1:06:26no enter your name no enter your name no
- 1:06:30so we are stuck within this loop until
- 1:06:32we enter something for our name
- 1:06:34so this time let me type in a name then
- 1:06:36hit enter
- 1:06:37so therefore our condition is now false
- 1:06:39the length
- 1:06:40of my name is now one two three so
- 1:06:43three is not equal to zero therefore our
- 1:06:46while statement our while loop is false
- 1:06:48and we continue on with the rest of the
- 1:06:49program there are a few variations in
- 1:06:51which you could write the same program
- 1:06:53so
- 1:06:54let's write this a different way what we
- 1:06:55could do is say
- 1:06:57name is equal to none while
- 1:07:00not name and this will pretty much do
- 1:07:03the same thing it's just another way of
- 1:07:05writing this
- 1:07:06well everybody that's the basics of
- 1:07:08while loops a while loop is a statement
- 1:07:10that will execute
- 1:07:11its block of code as long as its
- 1:07:13condition remains true
- 1:07:15for our example we asked the user to
- 1:07:17type in their name
- 1:07:18if they were to leave it empty or blank
- 1:07:20we would keep on asking the user to type
- 1:07:22in their name
- 1:07:23so if you would like a copy of this code
- 1:07:25i will post this in the comments section
- 1:07:26down below
- 1:07:27but yeah that's the basics of while
- 1:07:29loops in python
- 1:07:31hey what's going on everybody it's bro
- 1:07:33hope you're doing well and in this video
- 1:07:35i'm going to explain for loops in python
- 1:07:37and at the end of this video we're going
- 1:07:38to create a countdown timer so
- 1:07:40sit back relax and enjoy the show
- 1:07:45all right guys and gals let's talk about
- 1:07:47for loops a for loop is a statement that
- 1:07:49will execute its block of code a
- 1:07:52limited amount of times it's similar but
- 1:07:54different from a while loop because a
- 1:07:56while loop
- 1:07:57could iterate an unlimited or infinite
- 1:08:00amount of times depending on the
- 1:08:02condition a for loop
- 1:08:04will only iterate a limited amount of
- 1:08:06times and before we start executing
- 1:08:08its block of code we already know how
- 1:08:10many times we're going to repeat this
- 1:08:11block of code
- 1:08:12so for this example let's create a for
- 1:08:14loop that will simply count it to 10
- 1:08:16and then we'll create a few more
- 1:08:17sophisticated examples
- 1:08:19so to create a for loop that will count
- 1:08:20up to 10 this is what we'll type
- 1:08:22four and then we need some sort of
- 1:08:24counter people usually either write
- 1:08:26index or they shorten it to just i i for
- 1:08:29index
- 1:08:30four i in and we can set a range range
- 1:08:34ten so we will execute this for loop ten
- 1:08:36times
- 1:08:37and to finish this for loop just add a
- 1:08:39colon at the end
- 1:08:40so the next line or lines will be
- 1:08:43indented so that's the block of code for
- 1:08:45the for loop
- 1:08:46what do we want to do during each
- 1:08:47iteration let's just print what
- 1:08:49i is and let's take a look to see what
- 1:08:51would happen
- 1:08:52so we executed this for loop ten times
- 1:08:55except with computers computers always
- 1:08:57start with zero
- 1:08:58so when we said four i in range ten
- 1:09:00we're counting numbers
- 1:09:02zero through nine so it's as if this
- 1:09:04number
- 1:09:05is exclusive so there's two ways we can
- 1:09:07change this we could say range ten
- 1:09:09plus one or what i would do is say print
- 1:09:12i
- 1:09:12plus one so this will give us numbers
- 1:09:15one
- 1:09:15through ten so that's a for loop it will
- 1:09:18execute a limited amount of times
- 1:09:20this time let's count a range between
- 1:09:22two numbers
- 1:09:23not necessarily 0 to 10 but something
- 1:09:25else so let's say 4i
- 1:09:27in range maybe the numbers 50 through
- 1:09:31100
- 1:09:31so within my range function i will pass
- 1:09:34in two numbers
- 1:09:35the first number will be the starting
- 1:09:37point and the second number is the
- 1:09:38ending point
- 1:09:39this first number is inclusive and the
- 1:09:41second number is exclusive
- 1:09:43so let's print this print whatever i is
- 1:09:46so this will count
- 1:09:4850 through 99 actually if you wanted to
- 1:09:51include this last number since it's
- 1:09:53exclusive what we could do is just add
- 1:09:55one to the end so this would give
- 1:09:56us 50 through 100 except that
- 1:10:00this will iterate 51 times since we're
- 1:10:02counting 50.
- 1:10:04so you just have to pay attention to
- 1:10:05your range and the numbers that it's
- 1:10:06counting now one thing that you can do
- 1:10:08with the range function is that you can
- 1:10:10add a third argument and this will
- 1:10:12function as the step
- 1:10:13how much you want to count up or down by
- 1:10:15so this time let's count up by two
- 1:10:17so i'll add comma two we're passing in a
- 1:10:20third argument this time
- 1:10:22so this program will now count up by two
- 1:10:24starting at fifty and go all the way
- 1:10:27to one hundred a benefit of for loops is
- 1:10:30that we can iterate through anything
- 1:10:32that is considered
- 1:10:33iterable this could include a string the
- 1:10:35letters in a string
- 1:10:36or any sort of collection so this time
- 1:10:39let's create a for loop that will
- 1:10:40iterate
- 1:10:41once through each letter in maybe a name
- 1:10:44so for
- 1:10:44i in and let's type a name here type in
- 1:10:47whatever your first name and last name
- 1:10:49is
- 1:10:50and then we will print whatever i is
- 1:10:53for i in whatever your name is print
- 1:10:56each letter
- 1:10:57in the string so when i run this each
- 1:11:00letter
- 1:11:01within my name will be printed to a new
- 1:11:03line
- 1:11:04all right people for my last trick we're
- 1:11:06going to create a program
- 1:11:07where we will simulate a countdown
- 1:11:09starting at 10 and count down to zero
- 1:11:11and then maybe once we reach zero we can
- 1:11:13print something such as
- 1:11:15happy new year so to create this program
- 1:11:17we'll need an import so at the top
- 1:11:19we're going to import the time module
- 1:11:21because we'll be waiting
- 1:11:22one second after each iteration of this
- 1:11:24for loop
- 1:11:25so to create this program let's say four
- 1:11:27you could say i but you can really write
- 1:11:29anything here and
- 1:11:30to demonstrate that let's say four
- 1:11:32seconds four seconds
- 1:11:35in range so the starting point is going
- 1:11:38to be ten
- 1:11:38the ending point will be zero and we'll
- 1:11:41add a step
- 1:11:42what do we want to count up or down by
- 1:11:44let's set this to negative one so this
- 1:11:46will be a countdown
- 1:11:47starting at ten and ending at zero so
- 1:11:49after each iteration
- 1:11:51let's print whatever i is or in this
- 1:11:54case
- 1:11:55seconds and then we can sleep have our
- 1:11:58thread sleep
- 1:11:58for a number of seconds too so after we
- 1:12:01print seconds
- 1:12:03type time dot sleep and pass in
- 1:12:06how long you want to sleep for i would
- 1:12:08like to sleep for one second
- 1:12:10and then at the end let's print happy
- 1:12:12new year make sure this isn't within the
- 1:12:14for loop
- 1:12:15print happy new year and well
- 1:12:18that's it let's test this so we'll begin
- 1:12:21at 10 and count down to zero and you can
- 1:12:23see that my thread that is running this
- 1:12:25program
- 1:12:25is sleeping for one second after each
- 1:12:28iteration of this for loop
- 1:12:29then once we reach zero it will display
- 1:12:32happy new year
- 1:12:33well everybody that's the basics of for
- 1:12:35loops a for loop is a statement
- 1:12:37that will execute its block of code a
- 1:12:40limited amount of times
- 1:12:41it's different from a while loop because
- 1:12:43a while loop could execute
- 1:12:45an infinite or unlimited amount of times
- 1:12:47with for loops they execute limited
- 1:12:50amounts of times
- 1:12:51and before we enter the for loop we
- 1:12:53already know how many times we're going
- 1:12:54to iterate this for loop
- 1:12:56if you'd like a copy of all this code i
- 1:12:58will post this in the comments down
- 1:12:59below
- 1:13:00but yeah that's how for loops work in
- 1:13:02python
- 1:13:04hey what's going on everybody it's you
- 1:13:06bro hope you're doing well and in this
- 1:13:07video i'm going to explain nested loops
- 1:13:09in python so
- 1:13:10sit back relax and enjoy the show
- 1:13:15hey all you people let's talk about
- 1:13:17nested loops a nested loop is a general
- 1:13:19concept of having
- 1:13:20one loop inside of another loop and
- 1:13:22that's really it it doesn't matter if
- 1:13:24it's a for loop or a while loop
- 1:13:26so the inner loop will finish all of its
- 1:13:28iterations
- 1:13:29before we finish one iteration of the
- 1:13:31outer loop
- 1:13:32and to best demonstrate this we're going
- 1:13:34to create a program where we will draw a
- 1:13:36rectangle made out of a certain symbol
- 1:13:38that we choose
- 1:13:39we need to set a width and a height and
- 1:13:41to best do this we'll need to use a
- 1:13:43nested loop we'll create a few prompts
- 1:13:45one for rows
- 1:13:46columns and a symbol that we want to
- 1:13:48make a rectangle of
- 1:13:49so let's begin with rows rows equals
- 1:13:52input how many
- 1:13:56rows and we'll need to cast this as an
- 1:14:00integer data type
- 1:14:01because we're working with numbers okay
- 1:14:04let's do the same thing for columns
- 1:14:08so replace rows with columns for this
- 1:14:10next line
- 1:14:12input how many columns
- 1:14:15and we'll create a symbol variable
- 1:14:18symbol
- 1:14:18equals input and this we don't have to
- 1:14:21cast
- 1:14:23enter a symbol to
- 1:14:26use now it's time for the nested loops
- 1:14:29we're going to create an outer for loop
- 1:14:31as well as an inner for loop
- 1:14:33the outer for loop will be in charge of
- 1:14:35the rows the inner for loop will be in
- 1:14:37charge of the columns
- 1:14:38so let's begin by creating our outer
- 1:14:41loop so
- 1:14:42we'll write 4 i in range
- 1:14:46rows we would like our outer for loop to
- 1:14:49iterate as many times
- 1:14:50as we have rows and we're going to
- 1:14:52create an inner for loop that will
- 1:14:54iterate as many times as we have columns
- 1:14:56so within the block of code for the
- 1:14:58outer for loop we're going to create
- 1:15:00another for loop
- 1:15:01this time it'll be four and a common
- 1:15:04convention
- 1:15:05for inner loops is to write j as an
- 1:15:07index because
- 1:15:09j comes after i i guess so for j
- 1:15:12in range columns
- 1:15:18so our inner for loop will iterate for
- 1:15:21as many times
- 1:15:22as we have columns all we'll do within
- 1:15:24the
- 1:15:25inner for loop is print our symbol that
- 1:15:27we have
- 1:15:28except we'll have one issue using a
- 1:15:31print statement
- 1:15:32after we use the print statement we will
- 1:15:34enter a new line character and move down
- 1:15:36to the next line
- 1:15:37we can actually prevent that by adding
- 1:15:39comma
- 1:15:40and equals quotes so after using a print
- 1:15:45statement
- 1:15:45this will prevent our cursor from moving
- 1:15:47down to the next line
- 1:15:49so pay attention to the indentation too
- 1:15:52so there's kind of two levels of
- 1:15:53indentation
- 1:15:54we're currently within the inner for
- 1:15:56loop so that's pretty much it for the
- 1:15:58inner for loop now we're going to
- 1:16:00print a new line once we exit the inner
- 1:16:02for loop
- 1:16:03and you can see that the indentation is
- 1:16:05on
- 1:16:07this line right here we're within the
- 1:16:08outer for loop now
- 1:16:10and that's pretty much it so let's try
- 1:16:12this program
- 1:16:13how many rows let's say five rows how
- 1:16:16many columns perhaps six
- 1:16:19what symbol do we want to use let's say
- 1:16:21the dollar sign
- 1:16:22and let's try it there we go there's our
- 1:16:25rectangle
- 1:16:26so we have let's see six columns one
- 1:16:30two three four five six and five rows
- 1:16:32one
- 1:16:33two three four five in summary
- 1:16:36a nested loop is really just this
- 1:16:38concept of having
- 1:16:39one loop inside of another loop it
- 1:16:41doesn't matter if it's a while loop or a
- 1:16:43for a loop it's really situational
- 1:16:45the inner loop will finish all of its
- 1:16:47iterations before finishing
- 1:16:49one iteration of the outer loop so what
- 1:16:51we did to demonstrate that is that we
- 1:16:53had
- 1:16:54the outer loop in charge the rows and an
- 1:16:56inner loop in charge of the columns for
- 1:16:58our rectangle program
- 1:16:59so if you would like a copy of this
- 1:17:01program i will post this in the comment
- 1:17:03section down below
- 1:17:04but yeah that's the basics of nested
- 1:17:06loops in python
- 1:17:09hey what's going on everybody it's bro
- 1:17:11hope you're doing well
- 1:17:12and in this video i'm going to explain
- 1:17:14loop control statements in python
- 1:17:16so sit back relax and enjoy the show
- 1:17:21all right ladies and gentlemen let's
- 1:17:23talk about loop control statements
- 1:17:24these are used to change a loop's
- 1:17:26execution from its normal sequence
- 1:17:29and there are three we're going to
- 1:17:30discuss break continue
- 1:17:32and pass let's begin with break break is
- 1:17:35used to terminate the loop entirely
- 1:17:37now here's a good example of where a
- 1:17:39break would be useful
- 1:17:40let's say while true we're going to
- 1:17:43continually ask
- 1:17:44for somebody's name if they don't type
- 1:17:46in anything then we'll continue the
- 1:17:48while loop over and over again
- 1:17:49so let's say name equals input
- 1:17:55enter your name now
- 1:17:58if name does not equal
- 1:18:02a set of quotes that means they don't
- 1:18:04type in anything then
- 1:18:05we will break and break out of this
- 1:18:08while loop and let's test it
- 1:18:10enter your name nah i don't think so i'm
- 1:18:12too lazy to enter my name
- 1:18:14all right i can see that i'm not going
- 1:18:15to escape this while loop so i'm going
- 1:18:17to type in my name because i give up
- 1:18:19and hit enter therefore i will break and
- 1:18:22exit out of this while loop
- 1:18:23so a break is used to terminate the loop
- 1:18:26entirely when it's encountered
- 1:18:28next we have continue continue skips to
- 1:18:30the next iteration of the loop
- 1:18:32now let's say we have a phone number
- 1:18:35phone
- 1:18:35underscore number and i will set the
- 1:18:38sequel to
- 1:18:39let's say a random phone number
- 1:18:40including dashes 123-456-7890
- 1:18:45what i would like to do is to display
- 1:18:47this number within the console window
- 1:18:49without these dashes
- 1:18:51this is how to do that using a continue
- 1:18:53control statement
- 1:18:54for i in phone number
- 1:19:00so for each character within our string
- 1:19:03of phone number
- 1:19:04what we'll do is check to see if we
- 1:19:06encounter a dash character
- 1:19:08if i i is our index is
- 1:19:11equal to a dash then we want to
- 1:19:15continue and skip over this iteration of
- 1:19:18the loop
- 1:19:19so continue
- 1:19:23then i would like to print
- 1:19:26whatever our indexes whatever character
- 1:19:29we're working on
- 1:19:30so if i were to run this as it is it's
- 1:19:32going to print each digit
- 1:19:34on a new line so with print statements
- 1:19:37they will add a new line character to
- 1:19:39the end of your string
- 1:19:40and you can actually change that by
- 1:19:42adding to the end comma
- 1:19:44end equals and then a set of quotes so
- 1:19:47this will print my phone number without
- 1:19:49any dashes
- 1:19:50one two three four five six seven eight
- 1:19:51nine zero so that's a use of
- 1:19:54the continue control statement it skips
- 1:19:56to the next iteration of the loop
- 1:19:58last but not least we have pass pass
- 1:20:01does nothing
- 1:20:01it acts as a placeholder let's say that
- 1:20:04i would like to print the numbers
- 1:20:051 through 20 using a for loop for i
- 1:20:09in range 1 through 21 and remember that
- 1:20:13the second digit is
- 1:20:14exclusive so this will iterate once
- 1:20:16through the digits of 1
- 1:20:18through 20. let's pretend that i'm
- 1:20:20highly superstitious and i don't want to
- 1:20:22print
- 1:20:22the number 13 because 13 is considered
- 1:20:24an unlucky number
- 1:20:26if i is equal to 13
- 1:20:29what we'll do is pass and pass acts as a
- 1:20:31placeholder and it won't do anything
- 1:20:34else print
- 1:20:37i that's one i there we go
- 1:20:40this will print the numbers 1 through 20
- 1:20:42and skip 13 because we used
- 1:20:44the past control statement which does
- 1:20:47nothing it acts as a placeholder
- 1:20:49so everybody those are loop control
- 1:20:51statements they change
- 1:20:52a loop's execution from its normal
- 1:20:54sequence we have break
- 1:20:56continue and pass so if you would like a
- 1:20:59copy of all this code
- 1:21:00i will post all of this in the comments
- 1:21:01section down below but yeah those are a
- 1:21:03few
- 1:21:04loop control statements in python what's
- 1:21:07going on everybody
- 1:21:08it's bro hope you're doing well and in
- 1:21:10this video i'm going to explain
- 1:21:12lists in python so sit back relax
- 1:21:15and enjoy the show
- 1:21:19all right ladies and gentlemen let's get
- 1:21:21down to business i got to explain
- 1:21:23lists to you all now a list is used to
- 1:21:25store
- 1:21:26multiple items within a single variable
- 1:21:29for example let's say we have a variable
- 1:21:32called food
- 1:21:33and i will store a string value of pizza
- 1:21:36because i like pizza
- 1:21:37what we could do is that we can store
- 1:21:40multiple items within this
- 1:21:42variable by turning this variable into a
- 1:21:44list
- 1:21:45and in order to do so we're going to
- 1:21:47surround all of the values that we would
- 1:21:49like to add
- 1:21:50to our list with a set of square
- 1:21:52brackets
- 1:21:53and boom there you have it we now have a
- 1:21:56list called food it is no longer
- 1:21:58a variable so what we could do is add
- 1:22:01multiple items to this list of food so
- 1:22:04let's add some other food items that we
- 1:22:06might like
- 1:22:07let's see i also like maybe some
- 1:22:09hamburgers
- 1:22:11and a hot dog
- 1:22:15and maybe some spaghetti i think i
- 1:22:18smelled spaghetti right
- 1:22:19okay so we now have a list called food
- 1:22:22now what happens if we attempt to print
- 1:22:25this list of food
- 1:22:26so print food so this will
- 1:22:30print all of the elements found in this
- 1:22:34list
- 1:22:34each item in a list is referred to as an
- 1:22:37element
- 1:22:38if we need to access a certain element
- 1:22:41of
- 1:22:41this list we have to list the index so
- 1:22:44next to our list we'll add a set of
- 1:22:46square brackets again
- 1:22:47and we need to list the numbered index
- 1:22:49of the element that we're trying to
- 1:22:51access
- 1:22:52now computers they always start with
- 1:22:53zero so the first
- 1:22:55element in our list would be element
- 1:22:58number zero
- 1:22:59the next one would be one two and three
- 1:23:02so if we need to access element zero
- 1:23:06which is in the first position we would
- 1:23:07say food square brackets
- 1:23:10and then within here list the index so
- 1:23:12the first position
- 1:23:13is zero and this will now print the
- 1:23:15first element of our list
- 1:23:17which is pizza let's attempt to access
- 1:23:19some of these other elements
- 1:23:20so the element at index one would be
- 1:23:24you guessed it hamburger two
- 1:23:28would be hot dog and three would be
- 1:23:30spaghetti
- 1:23:31now what happens if we attempt to access
- 1:23:34the element at index four
- 1:23:36well currently that's out of range
- 1:23:38because we did not assign
- 1:23:40a value so we're experiencing an error
- 1:23:42an index
- 1:23:43out of range error but if i were to add
- 1:23:46another element
- 1:23:47let's say pudding alright well then
- 1:23:51the element at index 4 would be putting
- 1:23:54then
- 1:23:55one important concept with lists is that
- 1:23:57you can always
- 1:23:58update and change the elements found
- 1:24:00within a list
- 1:24:01later on in the program after you
- 1:24:03declare one so let's say we would like
- 1:24:05to
- 1:24:05immediately change one of these elements
- 1:24:08let's say
- 1:24:09food at index zero i want to
- 1:24:12replace with maybe sushi because sushi
- 1:24:15is great
- 1:24:16so if i were to print the element at
- 1:24:18index
- 1:24:190 this would no longer be pizza because
- 1:24:22we updated element 0 to be
- 1:24:25sushi then and this prints sushi now if
- 1:24:28you need to display
- 1:24:29all of the elements found within a list
- 1:24:31you can easily do so with a standard for
- 1:24:34loop
- 1:24:34what we'll do is say for x in
- 1:24:38the name of our list which is food
- 1:24:41print x and let me turn this line into a
- 1:24:45comment
- 1:24:46all right so this for loop will print
- 1:24:49all of the elements found within our
- 1:24:51list
- 1:24:51of food and the results are sushi
- 1:24:55hamburger hot dog spaghetti and pudding
- 1:24:57all right people for this next portion
- 1:24:59i'm going to demonstrate a few useful
- 1:25:01functions of
- 1:25:02lists and to access some of these
- 1:25:04functions type the name of your list
- 1:25:06dot and there's a bunch to choose from
- 1:25:08here let's begin with
- 1:25:10event we can add an element to this list
- 1:25:12let's say at the end i would like to add
- 1:25:14ice cream so then if i were to display
- 1:25:17my list we now have ice cream at the end
- 1:25:20because we appended
- 1:25:22this value to my list of food so let's
- 1:25:24go over a few others
- 1:25:26so we can also remove a value food
- 1:25:29dot remove let's say i would like to
- 1:25:32remove
- 1:25:32hotdog so i'm going to type that within
- 1:25:36my function and this will remove hotdog
- 1:25:39and that is no longer here
- 1:25:42next we can pop food.pop
- 1:25:46pop will remove the last element so this
- 1:25:50will remove
- 1:25:52pudding
- 1:25:54we can insert a value at a given index
- 1:25:57food dot insert we need to list an index
- 1:26:01let's say zero that would be the first
- 1:26:03position technically that's where pizza
- 1:26:05is currently
- 1:26:06and i would like to add cake
- 1:26:10and at index 0 we now have cake
- 1:26:14so next up we have sort
- 1:26:17food dot sort this will sort a list
- 1:26:20alphabetically
- 1:26:22so in the first position we have
- 1:26:24hamburger hot dog pudding spaghetti
- 1:26:26then sushi we can also clear a list
- 1:26:31food dot clear and this will remove
- 1:26:34all of the elements of a list so this
- 1:26:37will not print anything because we
- 1:26:39cleared the list
- 1:26:40well everyone in conclusion a list is
- 1:26:42really just a variable that can store
- 1:26:45multiple values think of it that way so
- 1:26:48if you would like a copy
- 1:26:50of all the code that we have written
- 1:26:51here today i will post all of this in
- 1:26:53the comments down below
- 1:26:54but yeah that's how lists work in python
- 1:26:58how's it going everybody it's bro hope
- 1:27:00you're doing well
- 1:27:01and in this video i'm going to explain
- 1:27:022d lists in python so sit back
- 1:27:05relax and enjoy the show
- 1:27:10i have a super quick video for you all
- 1:27:12today on
- 1:27:132d lists also referred to as
- 1:27:15multi-dimensional lists
- 1:27:17all it is is a list of separate lists
- 1:27:20so let's begin by creating a few
- 1:27:22separate lists let's say we have a list
- 1:27:24called drinks and let's assign a few
- 1:27:27values
- 1:27:28let's say we have coffee as well
- 1:27:32as soda and maybe some tea
- 1:27:35all right so we currently have one list
- 1:27:37let's make maybe two more let's make a
- 1:27:39list called
- 1:27:40dinner and maybe in here we'll place
- 1:27:44pizza then hamburger
- 1:27:50and hot dog okay
- 1:27:53then let's create one more for this
- 1:27:55example let's say dessert
- 1:27:59and maybe we'll add two items
- 1:28:02they all don't need the same amount of
- 1:28:03items or elements so we have cake
- 1:28:06and ice cream
- 1:28:09all right so we have three lists one
- 1:28:12called drinks
- 1:28:13dinner and dessert so what we can do is
- 1:28:16add
- 1:28:16all of these lists to one list
- 1:28:19and let's create a list called food food
- 1:28:22equals and for each of these elements
- 1:28:24we're going to place
- 1:28:25our lists so we have drinks dinner
- 1:28:29and dessert check this out what if i
- 1:28:32were to
- 1:28:33print my 2d list of food
- 1:28:36so what ends up happening is that this
- 1:28:38will print all of the elements
- 1:28:40found within each individual list and
- 1:28:42they're all grouped together
- 1:28:44so this first portion is my drinks list
- 1:28:48which contains coffee soda tea this next
- 1:28:51element
- 1:28:51is my dinner list which contains pizza
- 1:28:54hamburger hot dog and the last element
- 1:28:57dessert contains cake and ice cream if i
- 1:29:00need to access
- 1:29:02just one of these lists i will add an
- 1:29:04index
- 1:29:05after my food 2d list and
- 1:29:08set an index number so index zero is
- 1:29:11referring to my first
- 1:29:13list of drinks and this will display all
- 1:29:15of the elements found within my first
- 1:29:17list
- 1:29:18and if i need just one of these elements
- 1:29:21i will add
- 1:29:22a second set of square brackets and list
- 1:29:24the index of the item that i'm trying to
- 1:29:27access
- 1:29:27so this very first item coffee would be
- 1:29:31index zero and then index zero again so
- 1:29:33we need
- 1:29:34two sets of square brackets and if i
- 1:29:36were to print this
- 1:29:37this will display coffee so if i go down
- 1:29:40the list
- 1:29:40the next element would be soda
- 1:29:44and then t if i were to change the index
- 1:29:47within
- 1:29:47this first set of square brackets well
- 1:29:50now we're working with
- 1:29:51a different list this time we're working
- 1:29:53with our dinner list
- 1:29:54so the element at index 1
- 1:29:58index 2 that would be our dinner list
- 1:30:01and this will display the element of hot
- 1:30:03dog
- 1:30:04if i change this to the next list which
- 1:30:06would be dessert
- 1:30:08well we're going to receive an index out
- 1:30:10of range error because
- 1:30:11we only have two values within this list
- 1:30:14cake and ice cream there is no element
- 1:30:18at index 2 because we only added two
- 1:30:21elements
- 1:30:22to this list of dessert all right
- 1:30:24everybody so that
- 1:30:26is 2d lists also referred to as
- 1:30:29multi-dimensional lists
- 1:30:30it's a list of separate lists if you
- 1:30:33need to access
- 1:30:34one of the elements within your 2d list
- 1:30:36you need
- 1:30:37two sets of square brackets so if you
- 1:30:40would like a copy of this code
- 1:30:41i will post this in the comments down
- 1:30:43below but yeah that's how
- 1:30:442d lists work in python hey what's going
- 1:30:48on people
- 1:30:48it's you bro hope you're doing well and
- 1:30:50in this video i'm going to explain
- 1:30:51tuples in python so
- 1:30:53sit back relax and enjoy the show
- 1:30:58so tuples they are collections which are
- 1:31:01ordered
- 1:31:01and unchangeable they're very similar to
- 1:31:03lists but they're ordered
- 1:31:05and we can't change them they're useful
- 1:31:07for grouping together related data
- 1:31:09let's say we would like to create some
- 1:31:10sort of student record
- 1:31:12so we can create a tuple to take care of
- 1:31:14that for us so let's say we would like
- 1:31:16to create a tuple called student
- 1:31:18the process of creating a tuple is very
- 1:31:20similar to lists
- 1:31:22but instead of using a set of square
- 1:31:24brackets to place all of the values
- 1:31:26within
- 1:31:26we're going to use a set of parentheses
- 1:31:29and now we can add a bunch of values
- 1:31:31related to this collection of student
- 1:31:33so let's add some student information
- 1:31:35let's say that this student's name is
- 1:31:37bro
- 1:31:38let's give him an age as well as a
- 1:31:40gender
- 1:31:41all right so there you have it we now
- 1:31:43have a tuple called student
- 1:31:45and let's take a look at some of the
- 1:31:46functions related to tuples
- 1:31:48and you can access them by typing
- 1:31:49student dot
- 1:31:51and then there are a few here not as
- 1:31:53many as lists we have count and we have
- 1:31:55index
- 1:31:56so we can find the count of how many
- 1:31:58times a value appears
- 1:32:00let's count how many times the value of
- 1:32:03bro appears
- 1:32:04so we'll need to put this within a print
- 1:32:06statement so we can actually see it
- 1:32:08so bro appears
- 1:32:12one time within this tuple there is also
- 1:32:14an index method as well
- 1:32:16so type the name of your tuple dot
- 1:32:19and we will use the index method we can
- 1:32:22find the index of a certain value
- 1:32:24let's say we would like to find the
- 1:32:26index of mail
- 1:32:27and then i will just print this to the
- 1:32:29console window
- 1:32:31so the index of the value mail would be
- 1:32:34at two zero one
- 1:32:37two that's it for the methods available
- 1:32:40to tuples
- 1:32:41a few tricks that you can do with tuples
- 1:32:42is that you can display
- 1:32:44all of the contents within a tuple using
- 1:32:47a for loop
- 1:32:48let's say 4x in student
- 1:32:51we will print x so
- 1:32:55this will iterate once through all the
- 1:32:57values found within our tuple
- 1:32:59of student and we can also check to see
- 1:33:02if a certain value
- 1:33:03exists within our tuple using an if
- 1:33:05statement let's say if
- 1:33:07bro in student
- 1:33:11if this is true then what do we want to
- 1:33:13do let's say
- 1:33:14print bro is here
- 1:33:17all right so since bro is within here
- 1:33:21this will be true and this will print
- 1:33:23our
- 1:33:24statement of bro is here all right
- 1:33:27everybody like i said this is a super
- 1:33:29quick video on tuples
- 1:33:31they're very similar to lists except
- 1:33:34they are ordered and unchangeable if
- 1:33:36you'd like a copy of all this code i
- 1:33:38will post all of this in the comments
- 1:33:39down below
- 1:33:40and don't forget to smash that like
- 1:33:42button drop a comment down below
- 1:33:44and subscribe if you'd like to become a
- 1:33:46fellow bro
- 1:33:47hey what's going on everybody it's you
- 1:33:49bro hope you're doing well
- 1:33:51and in this video i'm going to explain
- 1:33:52what a set is in python so
- 1:33:55sit back relax and enjoy the show
- 1:34:00a set is a collection which is unordered
- 1:34:03as well as being
- 1:34:04unindexed they do not allow any
- 1:34:06duplicate values
- 1:34:07for example let's create a set of
- 1:34:10silverware
- 1:34:10so in order to create a set we need a
- 1:34:12name for this set
- 1:34:14let's call it utensils equals
- 1:34:17and in order to create a set we need to
- 1:34:19surround all values with a set
- 1:34:21of curly braces now let's add a few
- 1:34:23utensils
- 1:34:24to the set called utensils let's add a
- 1:34:27fork
- 1:34:29a spoon and maybe a knife and that
- 1:34:32should be good
- 1:34:33and let's display all of the values for
- 1:34:36x in utensils
- 1:34:41print x now with sets
- 1:34:44they're different from lists because
- 1:34:46they're unordered as well as being
- 1:34:48unindexed
- 1:34:49if i were to print all of the elements
- 1:34:51in utensils
- 1:34:52they might not necessarily be in the
- 1:34:54same order in which we placed them
- 1:34:56for example when we printed all of the
- 1:34:59elements found within utensils
- 1:35:00first we had a knife a fork and then a
- 1:35:03spoon if i were to run this again
- 1:35:05they might be in a different order kind
- 1:35:07of like that
- 1:35:08so a set is actually faster than a list
- 1:35:11if you need to check to see if something
- 1:35:14is within a set compared to a list and
- 1:35:16they do not allow any duplicate values
- 1:35:19let me add a bunch of knives and let's
- 1:35:21see what happens
- 1:35:22so i'll have a total of three knives but
- 1:35:26when we print all of the elements
- 1:35:28of the set only one knife appears so
- 1:35:30here's a few useful methods of sets
- 1:35:34the first method is that we can add an
- 1:35:36item to our set
- 1:35:37so type in the name of the set in this
- 1:35:40case it's utensils dot and here's a
- 1:35:42bunch of methods that you can use
- 1:35:44i am looking for add and we can add an
- 1:35:47element to the set
- 1:35:48i would like to add a napkin
- 1:35:52and then if we were to print the set we
- 1:35:54do have a napkin
- 1:35:56within the set we can also remove an
- 1:35:58element
- 1:35:59utensils dot remove
- 1:36:02and i would like to remove the fork
- 1:36:08and now our fork is gone we can also
- 1:36:11clear
- 1:36:12utensils dot clear
- 1:36:16and all the elements within our set
- 1:36:18should be gone
- 1:36:20so let me turn these lines into a
- 1:36:22comment
- 1:36:23all right for this next example we'll
- 1:36:25need a second set
- 1:36:26let's create a second set called dishes
- 1:36:30and then we need a set of curly braces
- 1:36:33and
- 1:36:33let's add a bowl
- 1:36:36a plate and a cup
- 1:36:40our next method is that we're going to
- 1:36:43add one set to another by using the
- 1:36:45update method
- 1:36:46let's say we would like to add our
- 1:36:48dishes set to our utensil set
- 1:36:51so let's type utensils dot and use the
- 1:36:54update method
- 1:36:54and within this method we will pass in
- 1:36:57the set that we would like to add
- 1:36:59so utensils update dishes this will add
- 1:37:02all of the elements found within dishes
- 1:37:04to our utensil set and you can see that
- 1:37:07when we print our utensils
- 1:37:08we have a bowl a plate and a cup in here
- 1:37:12as well
- 1:37:12and then if i were to switch these
- 1:37:14around let's say dishes
- 1:37:16update utensils and then i will
- 1:37:20print everything found within dishes now
- 1:37:23there's a fork a spoon
- 1:37:25and a knife in here as well we can also
- 1:37:28join
- 1:37:28two sets together and create a new set
- 1:37:31entirely
- 1:37:32so let's create a set called dinner
- 1:37:34table like we're setting up a dinner
- 1:37:36table and we need a fork a spoon a knife
- 1:37:39a bowl a plate and a cup
- 1:37:40so we have a dinner table set
- 1:37:44we're going to set the sequel to either
- 1:37:46utensils
- 1:37:48dot union
- 1:37:51dishes or we could do dishes
- 1:37:54union utensils either way would work and
- 1:37:57let's display all the elements found
- 1:37:59within dinner table
- 1:38:00and we should now have elements from
- 1:38:02both sets utensils
- 1:38:04and dishes as well there are also some
- 1:38:07methods in which we can compare
- 1:38:09the similarities as well as the
- 1:38:11differences between
- 1:38:12the elements found within two sets let's
- 1:38:14say i would like to check to see
- 1:38:16what utensils has that dishes doesn't
- 1:38:19and for this example i think i'll add a
- 1:38:21knife
- 1:38:22to my dishes set just so that they have
- 1:38:24at least one thing in common
- 1:38:26so i would like to see what utensils has
- 1:38:29that dishes doesn't so we can do so
- 1:38:31using the
- 1:38:32difference method and i will print the
- 1:38:35results with a print statement
- 1:38:36so i'm going to compare utensils against
- 1:38:40dishes utensils
- 1:38:45dishes and this will print what utensils
- 1:38:48has
- 1:38:49that dishes doesn't which is a fork as
- 1:38:52well as a spoon
- 1:38:54and we can reverse the rolls too we
- 1:38:56could say dishes difference
- 1:38:58utensils what do dishes have that
- 1:39:01utensils doesn't
- 1:39:02and dishes has a cup a bowl and a plate
- 1:39:06they both have knives so that's why
- 1:39:08knife isn't appearing
- 1:39:10we can also check to see if there's
- 1:39:12anything that they have in common
- 1:39:13using the intersection method so i'll
- 1:39:16turn this line into a comment
- 1:39:20we'll print utensils
- 1:39:23dot intersection
- 1:39:27dishes and this will return whatever
- 1:39:30element that they have in common
- 1:39:32which is a knife all right everybody in
- 1:39:35conclusion a set
- 1:39:36is a collection which is unordered as
- 1:39:39well as being
- 1:39:40unindexed they do not allow any
- 1:39:42duplicate values
- 1:39:43and you can do things like compare two
- 1:39:45sets see if there's anything that they
- 1:39:47have in common any differences
- 1:39:49and we can add or remove elements to a
- 1:39:51set if you'd like a copy of all this
- 1:39:53code i will post all of this in the
- 1:39:55comments down below
- 1:39:56don't be afraid to smash that like
- 1:39:58button drop a comment down below
- 1:40:00and subscribe if you'd like to become a
- 1:40:02fellow bro hey what's going on everybody
- 1:40:05it's you bro hope you're doing well and
- 1:40:06in this video i'm going to explain
- 1:40:08how dictionaries work in python so sit
- 1:40:11back
- 1:40:11relax and enjoy the show
- 1:40:15all right let's get down to business a
- 1:40:17dictionary is a
- 1:40:19changeable unordered collection of
- 1:40:22unique key value pairs they're fast
- 1:40:25because they use hashing
- 1:40:27and they allow us to access a value
- 1:40:29quickly
- 1:40:30now to create a dictionary it's very
- 1:40:32similar to creating a
- 1:40:33set based on the last video except we're
- 1:40:36going to store
- 1:40:36unique key value pairs let's create a
- 1:40:39dictionary
- 1:40:40of countries and their capitals we can
- 1:40:43store those
- 1:40:44as key value pairs so let's call this
- 1:40:46dictionary
- 1:40:48capitals capitals equals then we need a
- 1:40:51set of curly braces much like what we
- 1:40:53use
- 1:40:53with sets now we need a key
- 1:40:56and a value let's say we have the usa as
- 1:40:59a key
- 1:41:00and the capital of the usa would be
- 1:41:03washington dc
- 1:41:04so in order to associate a value with
- 1:41:06the key we'll follow the key with a
- 1:41:08colon
- 1:41:09and then add some value and the data
- 1:41:11type really doesn't matter but
- 1:41:12in this case we're just using strings so
- 1:41:14the capital of the usa
- 1:41:16would be washington dc
- 1:41:19and to add another key value pair we'll
- 1:41:21separate each with a comma
- 1:41:23and let's add a few others for good
- 1:41:25measure so let's say we have india next
- 1:41:28as a key and the value would be
- 1:41:31new delhi and we'll add two more
- 1:41:35let's say we have china
- 1:41:38and the capital of china is beijing
- 1:41:41and lastly let's add russia
- 1:41:46and the capital of russia is moscow
- 1:41:49and there you have it ladies and
- 1:41:51gentlemen we now have a dictionary
- 1:41:53called capitals
- 1:41:54that has unique key value pairs so
- 1:41:57remember with
- 1:41:58dictionaries they are unordered in order
- 1:42:00to access one of these values
- 1:42:02instead of using a numbered index we're
- 1:42:04going to use the associated key with
- 1:42:06that value
- 1:42:07here's an example let's say i would like
- 1:42:09to print the capital
- 1:42:10of russia so i need to use this key of
- 1:42:14russia
- 1:42:14so in order to do this type in the name
- 1:42:16of the dictionary
- 1:42:18followed by a set of straight brackets
- 1:42:20and then instead of an
- 1:42:21index number like 0 or 1 2 3
- 1:42:25i'm going to use the key so i would like
- 1:42:27to print
- 1:42:28the value that's stored or associated
- 1:42:31with the key
- 1:42:31of russia so when i run this this will
- 1:42:35print
- 1:42:36that value which is moscow so this isn't
- 1:42:39always safe
- 1:42:40let's say we have a key that doesn't
- 1:42:41exist like germany
- 1:42:43germany is not currently within my
- 1:42:44dictionary so if i were to run this
- 1:42:47well then my program is going to
- 1:42:49encounter an error
- 1:42:51and this will interrupt the normal flow
- 1:42:52of my program
- 1:42:54a much safer way to access a key
- 1:42:57to check to see if it's there or not is
- 1:42:59to use the get method
- 1:43:01of dictionaries so i'm going to turn
- 1:43:03this line into a comment and let's try
- 1:43:04this again but this time we'll use the
- 1:43:06get method
- 1:43:07so type in the name of the dictionary
- 1:43:09dot get
- 1:43:12and list the key that you want to use
- 1:43:15so i would like to see if germany
- 1:43:18is within my dictionary currently there
- 1:43:21isn't so this will return
- 1:43:23none and we will not encounter an error
- 1:43:25so this is a much
- 1:43:26safer way of checking to see if there is
- 1:43:28a key within your dictionary
- 1:43:30here's a few other useful methods there
- 1:43:32is a method to print only the keys
- 1:43:35so we type in the name of the dictionary
- 1:43:38capitals
- 1:43:39and here's a listing of all of the
- 1:43:41methods that we have access to
- 1:43:42i'm looking for the keys method and this
- 1:43:45will print
- 1:43:46only the keys and not the values we can
- 1:43:49also print
- 1:43:50just the values and not the keys so type
- 1:43:53in the name of the dictionary again
- 1:43:55capitals dot values
- 1:43:59and this will print only the values or
- 1:44:02you could print everything
- 1:44:03both the keys and the values and you do
- 1:44:06so by using
- 1:44:07the items method capitals
- 1:44:10dot items
- 1:44:15and this will print your entire
- 1:44:16dictionary one other way in which we can
- 1:44:18display
- 1:44:19all of the key value pairs in a
- 1:44:21dictionary is to use a for loop
- 1:44:23so at the end let's create a for loop
- 1:44:25for
- 1:44:26key comma value in
- 1:44:30capitals dot items
- 1:44:33this is going to iterate once for each
- 1:44:36key value pair
- 1:44:37in my dictionary for each key value in
- 1:44:40capitals
- 1:44:40dot items print
- 1:44:44key comma value
- 1:44:47and this will print my entire dictionary
- 1:44:50a feature of dictionaries
- 1:44:51is that they are mutable that means we
- 1:44:54can change them or alter them
- 1:44:55after the program is already running so
- 1:44:58one way in which we can do that
- 1:44:59is to use the update method of
- 1:45:01dictionaries
- 1:45:02so let's add germany as a key and give
- 1:45:06it a value
- 1:45:06of berlin so type in the name of the
- 1:45:09dictionary capitals
- 1:45:11and we'll use the update method and
- 1:45:13within the parentheses of this method
- 1:45:15we'll add a set of curly braces and now
- 1:45:18we can add a new
- 1:45:19key value pair so let's add germany
- 1:45:23and a value of berlin so if i were to
- 1:45:27run this and display
- 1:45:28all of the key value pairs found within
- 1:45:30my dictionary we now have
- 1:45:32germany as a key and berlin as its value
- 1:45:35not only can you use the update method
- 1:45:38to add a new
- 1:45:39key value pair but you can update an
- 1:45:41existing one let's say we would like to
- 1:45:43change the
- 1:45:44capital of usa so capitals
- 1:45:47dot update parentheses curly braces
- 1:45:51list the key usa colon
- 1:45:54and then we can give the usa a new value
- 1:45:57let's pretend that the united states
- 1:45:59government has decided to relocate
- 1:46:01their capital to las vegas
- 1:46:05so this will now update our key of usa
- 1:46:08with a new value and if i were to print
- 1:46:11all of the key value pairs found within
- 1:46:14my dictionary of capitals the usa has a
- 1:46:16new value
- 1:46:17of las vegas so the last two methods i'm
- 1:46:21going to cover are both pop
- 1:46:22and clear you can use the pop method to
- 1:46:25remove
- 1:46:25a key value pair so capitals
- 1:46:29dot pop parentheses and list the key
- 1:46:32of the key value pair that you would
- 1:46:34like to remove so let's say we would
- 1:46:36like to remove china
- 1:46:37so we'll list that key and then when we
- 1:46:39use the pop method
- 1:46:41that will remove this key value pair
- 1:46:44from my dictionary
- 1:46:45and then of course we have clear as well
- 1:46:47which will just remove everything
- 1:46:49capitals dot clear
- 1:46:52and this will clear my dictionary all
- 1:46:55right everybody
- 1:46:56so in conclusion a dictionary is a
- 1:46:58changeable
- 1:46:59unordered collection of unique key value
- 1:47:02pairs
- 1:47:03they're fast because they use hashing
- 1:47:05and they allow us to access a value
- 1:47:07quickly
- 1:47:08well that's the basics of dictionaries
- 1:47:10in python
- 1:47:11if you would like a copy of all this
- 1:47:13code i will post all of this in the
- 1:47:15comments down below
- 1:47:16but yeah that's how dictionaries work in
- 1:47:18python
- 1:47:20hey what's going on everybody it's bro
- 1:47:22hope you're doing well and in this video
- 1:47:24i'm going to teach you guys all about
- 1:47:25the index
- 1:47:26operator in python so sit back relax
- 1:47:29and enjoy the show
- 1:47:32all right people let's begin i'm going
- 1:47:34to be explaining the index operator
- 1:47:36which is represented by
- 1:47:38a set of square brackets now they give
- 1:47:41access
- 1:47:41to a sequences elements they include but
- 1:47:44are not limited to
- 1:47:46strings lists and tuples for this
- 1:47:48example we're going to be working with
- 1:47:50strings because strings are easy to work
- 1:47:52with
- 1:47:52let's say we have a name and name equals
- 1:47:55whatever your first
- 1:47:56and last name is we can use the index
- 1:47:58operator to access
- 1:48:00an element of the sequence our string
- 1:48:03so let's check to see if the first
- 1:48:05letter in our name
- 1:48:07is lowercase and i should probably
- 1:48:09change that to lowercase for this
- 1:48:11example
- 1:48:11so within an if statement if name and we
- 1:48:14can add a set
- 1:48:16of square brackets after our sequence
- 1:48:19which is a string and we can check
- 1:48:22a given element of our sequence let's
- 1:48:25check to see
- 1:48:26if the first letter is lowercase
- 1:48:29now with computers they always start at
- 1:48:31zero if you need to access the first
- 1:48:33element
- 1:48:33in a sequence that would be index zero
- 1:48:36and then the next sequence well the next
- 1:48:38element in the sequence would be one
- 1:48:40then two then you followed that pattern
- 1:48:42so i would like to check to see if
- 1:48:44name at index zero that would be the
- 1:48:47first character
- 1:48:48is lowercase so there is a method to
- 1:48:51check that
- 1:48:52dot is lower this will return
- 1:48:55true or false if the first letter is
- 1:48:58lowercase
- 1:48:59then our if statement is going to be
- 1:49:00resolved to true
- 1:49:03and what i could do is that i can
- 1:49:06perhaps change it
- 1:49:07to uppercase then so let's reassign
- 1:49:10name to equal name dot
- 1:49:14and there is a capitalize function here
- 1:49:17and then let's print our name print
- 1:49:21name so when we run this
- 1:49:24the first letter in my name is now
- 1:49:27capitalized
- 1:49:28so if you need to access an element
- 1:49:30within a sequence a string or
- 1:49:32list or a tuple you would add a set
- 1:49:36of square brackets afterwards and then
- 1:49:38you list an integer
- 1:49:39of the element that you're trying to
- 1:49:41access so here's a few other examples
- 1:49:43i'm going to turn
- 1:49:44this as well as this into a comment
- 1:49:47and let's say we would like to create
- 1:49:49some substrings and we can do so
- 1:49:51using our index operator so i would like
- 1:49:54to
- 1:49:54create a substring from the first part
- 1:49:57of my name
- 1:49:57so what i'll do is create a new variable
- 1:50:00first
- 1:50:01name equals name index operator
- 1:50:05so you can specify a range for an index
- 1:50:08operator
- 1:50:09so you would set the starting position
- 1:50:12on the left hand side followed by a
- 1:50:14colon and then your ending position
- 1:50:16so i would like the substring to begin
- 1:50:18at index 0 and
- 1:50:19end at 3 so that would be 0 colon
- 1:50:22then three all right and then i would
- 1:50:25like to make this all
- 1:50:26uppercase for some reason and then let's
- 1:50:29print my first name
- 1:50:33so with the index operator we specified
- 1:50:35a range
- 1:50:36we would like to access elements 0
- 1:50:39through
- 1:50:393. turn them up your case and assign
- 1:50:42them to a new
- 1:50:42variable of first name here's a shortcut
- 1:50:45that you might find useful
- 1:50:46if the index for your range begins with
- 1:50:49zero you can actually delete that you
- 1:50:50don't even necessarily need that zero
- 1:50:52now we just have a colon three and this
- 1:50:55would work the same as it did before
- 1:50:57so that's a useful shortcut and we're
- 1:50:59going to bring it up later if we need to
- 1:51:00access the last element in a sequence
- 1:51:03this time let's create a
- 1:51:05substring of last name so last
- 1:51:09name equals name index operator
- 1:51:13this time i would like to access the
- 1:51:16element at index let's see 0 1
- 1:51:202 3 4. all right so i'm going to say
- 1:51:24for colon and we're not really sure
- 1:51:27where our name our last name is going to
- 1:51:29end
- 1:51:30so what you could do is just say colon
- 1:51:32and then leave
- 1:51:33the next index blank so that will be
- 1:51:36the index beginning at four and
- 1:51:38everything afterwards
- 1:51:40and let's say two lower to make this all
- 1:51:42lowercase for some reason
- 1:51:44so let's print our last name print
- 1:51:48last name so this should
- 1:51:52take everything starting at index 4
- 1:51:56and after that and turn it into its own
- 1:51:58substring of last name
- 1:52:01now you can access the last element in a
- 1:52:03sequence by using what's called
- 1:52:05negative indexing so let's add
- 1:52:08a exclamation point to the end of our
- 1:52:11name so let's say
- 1:52:13we have a new variable called last
- 1:52:16character and i would like to store
- 1:52:21the last character in my name to this
- 1:52:23new
- 1:52:24variable this new substring so to access
- 1:52:27the last
- 1:52:27element in a sequence within the index
- 1:52:31operator we can use
- 1:52:32negative indexing so the last element
- 1:52:35would be
- 1:52:36negative one all right and then let's
- 1:52:38print
- 1:52:39whatever our last character is so this
- 1:52:42should print an exclamation point
- 1:52:45yep which did so then if this was
- 1:52:48negative 2
- 1:52:48that would be the second to last element
- 1:52:51in a sequence
- 1:52:52so now that would be e because e is in
- 1:52:55the second to last
- 1:52:56element in my sequence all right
- 1:52:59everybody so that's the basics of the
- 1:53:01index operator
- 1:53:02you can add a set of square brackets
- 1:53:04after a string
- 1:53:05a list or a tuple and then list an
- 1:53:08integer or a range
- 1:53:10of the elements that you're trying to
- 1:53:11access so if you would like a copy of
- 1:53:13all this code i will post everything in
- 1:53:15the comments down below
- 1:53:16don't be afraid to help me out and smash
- 1:53:18that like button drop a comment down
- 1:53:20below
- 1:53:20and subscribe if you'd like to become a
- 1:53:22fellow bro
- 1:53:24hey what's going on everybody it's you
- 1:53:26bro hope you're doing well
- 1:53:27and in this video i'm going to explain
- 1:53:28how functions work in python so
- 1:53:31sit back relax and enjoy the show
- 1:53:36if you find this video helpful please
- 1:53:37remember to like
- 1:53:39comment and subscribe your support will
- 1:53:41help keep this channel running
- 1:53:43okay let's do this thing a function
- 1:53:46is a block of code which is executed
- 1:53:49only when it is called
- 1:53:50this process is also known as invoking a
- 1:53:53function
- 1:53:54with programming we try not to repeat
- 1:53:56code if we don't have to
- 1:53:57that's why functions are useful because
- 1:53:59our function can perform
- 1:54:01some specific task for us some block of
- 1:54:03code for us
- 1:54:04whenever it is called so we only need to
- 1:54:06write that block of code
- 1:54:08once and if we need to repeat it we just
- 1:54:10call this function one more time
- 1:54:12so let's define and create a function
- 1:54:13we'll create a function called hello
- 1:54:15and in order to define a function we'll
- 1:54:17type def
- 1:54:19and then a unique function name so let's
- 1:54:21call this function hello
- 1:54:23and with functions they always end with
- 1:54:25a set of parentheses
- 1:54:26and lastly to finish defining this
- 1:54:29function we'll add
- 1:54:30a colon at the end so if you go down to
- 1:54:33the next line
- 1:54:33take notice that my cursor is now
- 1:54:36indented
- 1:54:37any code underneath a function that is
- 1:54:39indented
- 1:54:40belongs to that specific function and
- 1:54:42will only execute
- 1:54:44whenever that function is called so for
- 1:54:47now we need to type in something
- 1:54:48if you don't know what you want your
- 1:54:50function to do you can always type pass
- 1:54:52for now
- 1:54:52but let's print something print the word
- 1:54:56hello and in order to call this function
- 1:55:00all we need to do within our program is
- 1:55:02type the name of the function which is
- 1:55:04hello
- 1:55:05and then add a set of parentheses
- 1:55:07because functions always end with a set
- 1:55:09of parentheses
- 1:55:10so what this will do is execute my
- 1:55:13function
- 1:55:13once and perform this block of code
- 1:55:15whenever it is called
- 1:55:17if i were to remove this well our
- 1:55:19program currently isn't going to do
- 1:55:21anything
- 1:55:21because functions only execute their
- 1:55:24block of code
- 1:55:25only when it is called so this time
- 1:55:27let's print the word
- 1:55:28hello three times so i will call this
- 1:55:31function
- 1:55:32three separate times hello hello hello
- 1:55:35so when i run this our hello function
- 1:55:38will be called
- 1:55:39three separate times now with functions
- 1:55:41they are not limited to just one line
- 1:55:44this is an entire block of code so maybe
- 1:55:46we can do something else too
- 1:55:48let's print have a nice
- 1:55:52day now if i were to run this my hello
- 1:55:55function is going to execute
- 1:55:56this entire block of code three separate
- 1:55:59times hello
- 1:56:00have a nice day now an important feature
- 1:56:03of functions
- 1:56:04is that we can send our function some
- 1:56:06information
- 1:56:07and our function can do something with
- 1:56:08that information that it receives
- 1:56:10let's say this time i would like to
- 1:56:11print hello plus somebody's name
- 1:56:14currently name is an unresolved
- 1:56:16reference what we can do
- 1:56:17when we call this function is send our
- 1:56:19function some information
- 1:56:21this can be a value a variable a
- 1:56:23collection
- 1:56:24all sorts of different things so if you
- 1:56:26need to send your function some
- 1:56:27information
- 1:56:28within the parentheses when you call
- 1:56:30that function just list the data that
- 1:56:32you want to send your function
- 1:56:34so let's say i would like to send my
- 1:56:36function a string
- 1:56:37value of bro whatever your first name is
- 1:56:40when you send information
- 1:56:41to a function these are called arguments
- 1:56:45they are the information that you're
- 1:56:46sending to a function
- 1:56:48and when you define that function you
- 1:56:50need a matching
- 1:56:52set of what is known as parameters
- 1:56:55so our hello function is going to
- 1:56:57receive one argument a string value
- 1:57:00we need a matching number of parameters
- 1:57:03currently this isn't going to work you
- 1:57:05can see here that hello
- 1:57:06takes zero positional arguments but one
- 1:57:09was given
- 1:57:10our function needs a matching number of
- 1:57:13parameters
- 1:57:14so think of it this way when we receive
- 1:57:17this value
- 1:57:17we're going to give a nickname to this
- 1:57:20value
- 1:57:21so when we receive the string value what
- 1:57:24do we want to call it
- 1:57:25well let's call it name so in order to
- 1:57:27add parameters
- 1:57:29to your function just list the
- 1:57:31parameters within
- 1:57:32the parentheses of that function and
- 1:57:35well this should work now
- 1:57:36we have a matching set of arguments and
- 1:57:40parameters when we call the hello
- 1:57:42function this time we are sending
- 1:57:44one argument over and when our hello
- 1:57:46function
- 1:57:47receives this argument we're going to
- 1:57:49give it a temporary nickname of
- 1:57:52name or you can call this whatever you
- 1:57:53want to and then we can use
- 1:57:55this value for whatever we want within
- 1:57:58our function
- 1:57:58now if we were to run this this will
- 1:58:00print hello bro
- 1:58:02have a nice day now i can send maybe a
- 1:58:04different value
- 1:58:05let's say hello and then i will send a
- 1:58:09string value of dude
- 1:58:11hello dude have a nice day so this isn't
- 1:58:14limited to
- 1:58:15just values we can send variables to
- 1:58:18let's say we have a
- 1:58:19variable called name equals
- 1:58:23bro and this time i'm going to send
- 1:58:26this variable over hello and then send
- 1:58:30name so these don't need to be the
- 1:58:34exact same let's say this is
- 1:58:37my name and i'm going to send
- 1:58:40my name over and i will temporarily give
- 1:58:45this value a nickname of just name
- 1:58:48and then i can use this variable for
- 1:58:50something hello bro
- 1:58:52have a nice day now with arguments you
- 1:58:55can send
- 1:58:55more than one value over let's say this
- 1:58:58time i would like to send
- 1:58:59two values over so let's send pro
- 1:59:02maybe for a first name and code for a
- 1:59:05second name but
- 1:59:06you can use your first name and last
- 1:59:08name so this isn't going to work because
- 1:59:10we do not have a matching set
- 1:59:13of arguments and parameters we're
- 1:59:15sending
- 1:59:16two arguments over but our function is
- 1:59:19only designed
- 1:59:20to accept one argument because we have
- 1:59:23one parameter so let's rename
- 1:59:26name as first name and we need
- 1:59:30two parameters this time so what are we
- 1:59:32going to call
- 1:59:33this value when we receive it let's call
- 1:59:35this last name
- 1:59:38so this time we are going to receive two
- 1:59:40values
- 1:59:42they will be nicknamed first name and
- 1:59:44last name so this time
- 1:59:45let's print hello first name
- 1:59:48maybe i'll add a space plus
- 1:59:52last name and this is now valid
- 1:59:56hello bro code have a nice day with
- 1:59:59arguments
- 1:59:59you can mix and match the data types
- 2:00:02that you're sending
- 2:00:03as arguments so currently we're sending
- 2:00:05two
- 2:00:06string values as arguments this time
- 2:00:08let's send an integer value
- 2:00:10let's say we would like to send an edge
- 2:00:13over to our hello function
- 2:00:15so i'll just send maybe the value of 21.
- 2:00:18so currently this isn't going to work
- 2:00:20because the hello function takes two
- 2:00:22positional arguments
- 2:00:23but three were given when we call this
- 2:00:26hello function we're sending
- 2:00:27this argument over but our function
- 2:00:30doesn't know what to do with this
- 2:00:31argument
- 2:00:32so we need a matching parameter for this
- 2:00:34value that it receives
- 2:00:36so let's call this value maybe edge as a
- 2:00:39temporary nickname
- 2:00:40and then we can do something with this
- 2:00:42value when we receive it
- 2:00:43so let's print an additional message
- 2:00:47perhaps u r plus
- 2:00:50age plus years
- 2:00:54old so if you need to display an
- 2:00:57integer or number along with the string
- 2:00:59you do need to convert it to a string
- 2:01:01so let's just cast our edge as a string
- 2:01:04when we display this
- 2:01:06so this should work now we now have a
- 2:01:08matching number
- 2:01:09of arguments and parameters so this will
- 2:01:13display
- 2:01:14hello bro code you are 21 years old have
- 2:01:16a nice day
- 2:01:17so ladies and gentlemen in conclusion a
- 2:01:20function
- 2:01:20is a block of code which is executed
- 2:01:23only when it is called
- 2:01:24if i were to remove this function call
- 2:01:27well then
- 2:01:28our program is just going to completely
- 2:01:30ignore this function and
- 2:01:31this block of code would not be executed
- 2:01:34when we call this function we can pass
- 2:01:36information known as arguments but we
- 2:01:39need a
- 2:01:40matching number of parameters set up to
- 2:01:42receive
- 2:01:43these arguments there are a few advanced
- 2:01:46features of parameters but
- 2:01:48i'll hopefully get to that in future
- 2:01:50videos we'll just stick with the basics
- 2:01:51for now of functions
- 2:01:53so that's the basics of functions if you
- 2:01:55would like a copy of this code
- 2:01:57i will post this in the comments down
- 2:01:59below but yeah that is how functions
- 2:02:01work in python hey what's going on
- 2:02:04everybody it's you bro hope you're doing
- 2:02:06well
- 2:02:06and in this video i'm going to explain
- 2:02:08the return statement in python so
- 2:02:10sit back relax and enjoy the show
- 2:02:15all right people let's begin the return
- 2:02:18statement is used within functions
- 2:02:19to send python values or objects back to
- 2:02:23the caller
- 2:02:23these values or objects are known as the
- 2:02:26function's
- 2:02:26return value so do you know how with
- 2:02:29functions we can pass
- 2:02:30arguments to a function and then our
- 2:02:32function can do something with those
- 2:02:34arguments
- 2:02:34well not only that but our function can
- 2:02:36pass some value or
- 2:02:38object back to the caller so what we'll
- 2:02:41do for this example is create
- 2:02:43a function that will simply multiply two
- 2:02:45numbers together
- 2:02:46and return the result back to the caller
- 2:02:49so let's define
- 2:02:50a function called multiply and we'll set
- 2:02:52up two parameters for this function
- 2:02:54let's say number one and number two
- 2:02:58and what we'll do is say result
- 2:03:01equals number one times number two
- 2:03:05and then at the end we can return
- 2:03:07something what do we want to return
- 2:03:08let's return whatever the result is now
- 2:03:11when we call this function we will pass
- 2:03:13in two values as arguments let's say we
- 2:03:16would like to multiply
- 2:03:17six times eight so when we run this well
- 2:03:20we can't actually see anything
- 2:03:22when we return the result after
- 2:03:24multiplying numbers
- 2:03:25one and two it's going to return the
- 2:03:27result back to the caller
- 2:03:29in order to see the value that is
- 2:03:31returned back to the caller
- 2:03:32one way in which we can do this is to
- 2:03:35print
- 2:03:35the value that is returned within a
- 2:03:38print statement
- 2:03:39so what we'll do is print the value that
- 2:03:41is returned after calling the multiply
- 2:03:44function
- 2:03:44so 6 times 8 is really great 6 times 8
- 2:03:47is 48
- 2:03:48now one other thing that we can do is
- 2:03:51store the returned value within a
- 2:03:53variable
- 2:03:54so let's say x equals multiply
- 2:03:586 times 8 and then we will print
- 2:04:00whatever x is
- 2:04:02so the result is still the same of 48
- 2:04:05now with the return value there is
- 2:04:07another way in which we could write this
- 2:04:09that uses
- 2:04:10less lines of code so we'll delete this
- 2:04:12first line and instead of returning
- 2:04:14a variable called result what we'll do
- 2:04:16is just return
- 2:04:18number one times number two and this
- 2:04:20would work the same as it does before
- 2:04:23but it's written in less lines of code
- 2:04:25so that's another way in which you could
- 2:04:26write this
- 2:04:27all right everybody so that is the
- 2:04:29return statement functions can send
- 2:04:31python values or objects back to the
- 2:04:34caller
- 2:04:34these values or objects are known as the
- 2:04:36functions return
- 2:04:38value you can either print this value
- 2:04:40directly to the console window
- 2:04:42or you can store it within a variable or
- 2:04:44some other location
- 2:04:46and well ladies and gentlemen that is
- 2:04:48how the return statement works in python
- 2:04:51what's going on everybody it's bro hope
- 2:04:53you're doing well
- 2:04:54and in this video i'm going to explain
- 2:04:56keyword arguments in python so
- 2:04:58sit back relax and enjoy the show
- 2:05:02all right everybody let's discuss
- 2:05:05keyword arguments
- 2:05:06these are arguments that are preceded by
- 2:05:09an identifier when we pass them to a
- 2:05:11function
- 2:05:12the order of the arguments doesn't
- 2:05:14matter unlike positional arguments
- 2:05:16which do matter and we've been working
- 2:05:18with positional arguments already
- 2:05:20and python knows the names of the
- 2:05:21arguments that our function receives
- 2:05:24when we use keyword arguments here's an
- 2:05:26example of us
- 2:05:27using positional arguments and then i'll
- 2:05:29demonstrate the benefits of
- 2:05:30keyword arguments let's say we have a
- 2:05:32function called hello
- 2:05:34that accepts three arguments a first
- 2:05:36name a middle name and a last name
- 2:05:38and the output will be hello first name
- 2:05:40middle name last name
- 2:05:41so if i were to run this this will
- 2:05:43display hello bro
- 2:05:45dude code so we're using positional
- 2:05:47arguments
- 2:05:48the order of the arguments does matter
- 2:05:50what do you think's gonna happen if i
- 2:05:52were to replace some of these values
- 2:05:54let's say i will swap the first argument
- 2:05:56with the
- 2:05:57third argument so let's try it again
- 2:06:01hello code dude bro so obviously the
- 2:06:04order of the arguments matters
- 2:06:05and now if we were to use keyword
- 2:06:07arguments then the order of the
- 2:06:09arguments
- 2:06:10doesn't matter but with each argument we
- 2:06:12need to precede each argument with
- 2:06:15a unique identifier and that identifier
- 2:06:18is the name of the parameter we want to
- 2:06:20associate each argument with
- 2:06:22so for my intended last name i will use
- 2:06:25an identifier of
- 2:06:26last to match this parameter so
- 2:06:29dude will be middle middle equals dude
- 2:06:32and bro
- 2:06:33will be first even though they're not in
- 2:06:35the correct order so let's try this
- 2:06:36again
- 2:06:37and my output is hello bro dude code
- 2:06:40so that's all about keyword arguments
- 2:06:43they are arguments
- 2:06:44preceded with an identifier when we pass
- 2:06:46them to a function
- 2:06:48the order of the arguments doesn't
- 2:06:49matter unlike positional arguments
- 2:06:51which do matter and then python knows
- 2:06:53the names of the arguments
- 2:06:54that our function receives when we use
- 2:06:56keyword arguments
- 2:06:58so that's it for keyword arguments if
- 2:07:00you would like a copy of this code
- 2:07:02i will post this in the comment section
- 2:07:03down below but yeah that's how keyword
- 2:07:06arguments
- 2:07:06work in python hey what's going on
- 2:07:10everybody it's you bro hope you're doing
- 2:07:12well and in this video i'm going to
- 2:07:13explain
- 2:07:14nested function calls in python so sit
- 2:07:16back
- 2:07:17relax and enjoy the show
- 2:07:21all right people nested function calls
- 2:07:24these are function calls
- 2:07:25inside of other function calls and this
- 2:07:27is possible because
- 2:07:29certain functions will return a value
- 2:07:31and we can immediately use that value as
- 2:07:33an argument for the next function
- 2:07:35so here's an example let's say that we
- 2:07:37have the small program
- 2:07:39where we will ask a user to type in a
- 2:07:41whole positive number
- 2:07:43and let's say i don't let's say i type
- 2:07:45in negative 3.14
- 2:07:47and then i'm storing that value within a
- 2:07:50variable
- 2:07:50named num what i'm going to do is
- 2:07:52convert that number
- 2:07:54to a floating point number because
- 2:07:55currently it's a string when you accept
- 2:07:57user input
- 2:07:58i will find the absolute value of num
- 2:08:01and then round it to the nearest whole
- 2:08:03number
- 2:08:03and then print the value so we can
- 2:08:05actually do this using less lines of
- 2:08:07code with nested function calls
- 2:08:09so let's start with the first function
- 2:08:11which will be accepting some user input
- 2:08:13the input function so this will be in
- 2:08:16the very center
- 2:08:17of our nested function calls and then
- 2:08:19the next function we would like to apply
- 2:08:21to
- 2:08:22the variable that is returned the value
- 2:08:24that is returned
- 2:08:25we will pass in as an argument to our
- 2:08:27next function of casting
- 2:08:29this data type to a float so we'll
- 2:08:31surround
- 2:08:32our function with another function and
- 2:08:34use
- 2:08:35the float function and then we'll just
- 2:08:38keep on adding layers to this
- 2:08:39so we have the absolute value function
- 2:08:42next
- 2:08:42so we'll add another layer and we have
- 2:08:45the round function
- 2:08:47so we'll add a layer to that and
- 2:08:50lastly we're going to print and that'll
- 2:08:52be the outermost layer
- 2:08:55so this program will do the exact same
- 2:08:57thing
- 2:08:58let's say negative 3.14 except we've
- 2:09:01written this with
- 2:09:02one line of code so basically we start
- 2:09:05with the innermost function and resolve
- 2:09:07that first
- 2:09:08whatever value is returned we use as an
- 2:09:10argument to the next outermost function
- 2:09:13and in this case it would be float then
- 2:09:15we resolve that then move on to absolute
- 2:09:18value then round and then print
- 2:09:20so it's just another way of writing code
- 2:09:22that takes up less
- 2:09:23lines but this is more of an extreme
- 2:09:25example you probably won't see
- 2:09:27like four or five nested functions well
- 2:09:29everybody those are nested function
- 2:09:31calls
- 2:09:32if you would like a copy of this code i
- 2:09:34will post all of this to the comment
- 2:09:35section down below
- 2:09:36but yeah those are nested function calls
- 2:09:39in python
- 2:09:40hey what's going on people it's your bro
- 2:09:42hope you're doing well and in this video
- 2:09:44i'm going to show you how variable scope
- 2:09:46works in python so
- 2:09:47sit back relax and enjoy the show
- 2:09:52all right let's do a thing people let's
- 2:09:54discuss the scope of a variable
- 2:09:56now the scope of a variable is the
- 2:09:58region that a variable
- 2:10:00is recognized a variable is only
- 2:10:02available from inside the region
- 2:10:05that it is created let's say that we
- 2:10:06have a function and i just gave this
- 2:10:08function a name of display name
- 2:10:10i created a variable inside of this
- 2:10:12function named name
- 2:10:14and you can assign this a value of
- 2:10:16whatever your name is
- 2:10:17preferably a last name for this example
- 2:10:19and what i'll do is just print
- 2:10:21whatever my name is so this variable is
- 2:10:24known to have
- 2:10:25a local scope because it's declared
- 2:10:28inside of a function
- 2:10:29and a variable is only recognized inside
- 2:10:32of the region
- 2:10:33that it is created this variable of name
- 2:10:36since it's created inside
- 2:10:37of this function it is known as a local
- 2:10:40variable
- 2:10:41that has a local scope it's only
- 2:10:44available
- 2:10:44inside of this function that it's
- 2:10:46created now let's attempt to access this
- 2:10:49variable from
- 2:10:50outside of our function so i'm going to
- 2:10:52print my
- 2:10:53name variable but we'll have an issue
- 2:10:55though
- 2:10:56so name name is not defined so
- 2:10:59local variables are declared inside of a
- 2:11:02function
- 2:11:03and they have a local scope they are
- 2:11:05only
- 2:11:06available inside of this function in
- 2:11:07which they're created
- 2:11:09on the other hand a global variable is a
- 2:11:12variable that is declared
- 2:11:14outside of any function but within your
- 2:11:16module that you're working with
- 2:11:17a global variable is known to have a
- 2:11:20global scope
- 2:11:21they are available inside and outside of
- 2:11:23any functions
- 2:11:24now if i was to print my name variable
- 2:11:27this is going to print
- 2:11:29bro or whatever your first name is and
- 2:11:31it is entirely possible to have both
- 2:11:33a global and local version of the same
- 2:11:35named variable
- 2:11:37so we have both a global version of name
- 2:11:39as well as a local version of name
- 2:11:42if i was to print my name variable from
- 2:11:45outside of this function
- 2:11:46it's going to print my global version
- 2:11:48however if i print
- 2:11:50my version of name inside of that
- 2:11:52function in which it's created
- 2:11:54we're going to first use the local
- 2:11:56version before the
- 2:11:57global version of name and let's try
- 2:11:59that so i'm going to call
- 2:12:01my display name function
- 2:12:04and this will print my last name
- 2:12:08code and then my first name of bro so it
- 2:12:11is possible to have a global
- 2:12:12and local version of the same named
- 2:12:15variable
- 2:12:16now if i was to remove this line where
- 2:12:18we declare a local version
- 2:12:20of name well then the next best
- 2:12:22available name to us would be
- 2:12:24the global version of name so if you use
- 2:12:26a variable inside of a function
- 2:12:28you'll first use a local version of that
- 2:12:31variable
- 2:12:32if it's available if not you'll use a
- 2:12:34global version instead
- 2:12:36and python will follow this rule known
- 2:12:38as the legb
- 2:12:40rule you use any local variables first
- 2:12:42then
- 2:12:43enclosed variables then global and then
- 2:12:45any built-in variables in that order
- 2:12:48so that's the main difference between
- 2:12:50local scope and
- 2:12:51global scope the scope of a variable is
- 2:12:54the region in which it's available
- 2:12:56and it's limited to the region in which
- 2:12:58it's created a local scope
- 2:13:00is available only inside of the function
- 2:13:02in which that variable is created
- 2:13:04a global scoped variable is a variable
- 2:13:07that is available
- 2:13:08inside and outside of any functions but
- 2:13:10within your module that you're working
- 2:13:12with
- 2:13:13so that's variable scope in python if
- 2:13:15you would like a copy of this code i
- 2:13:17will post this code to the comment
- 2:13:18section down below
- 2:13:19and well yeah that's how variable scope
- 2:13:22works in python
- 2:13:24hey what's going on everybody it's bro
- 2:13:26hope you're doing well
- 2:13:27and in this video i'm going to explain
- 2:13:29the arcs parameter in python so sit back
- 2:13:32relax and enjoy the show
- 2:13:36all right guys and gals let's talk about
- 2:13:38args
- 2:13:39now args is a parameter that will pack
- 2:13:42all arguments into a tuple
- 2:13:44it's useful so that a function can
- 2:13:45accept a varying amount of arguments
- 2:13:48here's an example of why it would be
- 2:13:50useful let's say we have a function
- 2:13:52called add that will accept two numbers
- 2:13:54as arguments
- 2:13:55and add them together and return the sum
- 2:13:57so this works as intended right as long
- 2:13:59as we pass in
- 2:14:00only two numbers but what if we need to
- 2:14:02pass in three numbers as arguments
- 2:14:04well we can no longer use this add
- 2:14:06function because we have two parameters
- 2:14:08set up
- 2:14:08but we're passing in three arguments you
- 2:14:10can see here within my console window
- 2:14:12that we have a type error
- 2:14:14add takes two positional arguments but
- 2:14:16three were given
- 2:14:17and one way in which we can solve this
- 2:14:19issue is to use this
- 2:14:20args parameter so what we're going to do
- 2:14:22is replace all of our parameters
- 2:14:24with asterisk args and actually
- 2:14:27the args portion isn't as important as
- 2:14:30the asterisk
- 2:14:31args is short for arguments and you can
- 2:14:32really name this whatever you want just
- 2:14:34be sure that you have this asterisk
- 2:14:36because with this asterisk we're doing a
- 2:14:38form of packing
- 2:14:39what we're doing is passing all of these
- 2:14:41arguments and packing them
- 2:14:43into a tuple so let's rewrite this
- 2:14:45program so let's say maybe sum
- 2:14:47equals zero and then we will need to
- 2:14:51iterate through all of the items within
- 2:14:53our tuple because
- 2:14:54tuples are iterable so let's say for i
- 2:14:57in args what we'll do is say sum
- 2:15:00plus equals whatever i is and at the end
- 2:15:03when we escape the for loop we will
- 2:15:05return
- 2:15:06our sum so let's try it again and see if
- 2:15:08it works
- 2:15:09well would you look at that it works
- 2:15:11let's pass in a few other arguments for
- 2:15:13good measure
- 2:15:14let's pass in six arguments this time
- 2:15:16all right look at that
- 2:15:17this does in fact work so you can
- 2:15:19actually name this whatever you want the
- 2:15:20important thing is to have this asterisk
- 2:15:23so let's rename this as i don't know
- 2:15:25stuff
- 2:15:26and let's try this again yep
- 2:15:29it still works with tuples they are
- 2:15:32ordered
- 2:15:32and unchangeable let's attempt to edit
- 2:15:34one of these values
- 2:15:36so let's say stuff at
- 2:15:39index zero is equal to
- 2:15:42zero and let's try and change it so you
- 2:15:45can see that tuple object does not
- 2:15:47support
- 2:15:48item assignment if you need to change
- 2:15:50one of the values
- 2:15:51within your tuple after you pack all of
- 2:15:54these arguments
- 2:15:55you might need to convert this to a
- 2:15:56different collection one way in which we
- 2:15:58can do that is to cast it
- 2:16:00let's say stuff equals and we'll cast
- 2:16:03our tuple as a list because a list
- 2:16:06can be changeable it's mutable so let's
- 2:16:09attempt this again we'll cast
- 2:16:11our tuple as a list and then edit one of
- 2:16:13the values
- 2:16:14and then add all the values together so
- 2:16:16to change one of the values
- 2:16:18say the name your collection stuff in
- 2:16:20this case let's say at index zero
- 2:16:22this now equals zero so now if we were
- 2:16:25to add all these items together
- 2:16:26they should equal 20. there we go so
- 2:16:29yeah that's basically the args parameter
- 2:16:32the name isn't as important as the
- 2:16:34asterisk because you can really name it
- 2:16:36whatever you want
- 2:16:37a common convention is to just name this
- 2:16:39args it's a parameter that will pack
- 2:16:41all arguments into a tuple it's useful
- 2:16:44so that a function can accept
- 2:16:46a varying amount of arguments well that
- 2:16:48is all about args
- 2:16:50if you would like a copy of this code i
- 2:16:51will post this in the comments section
- 2:16:53down below
- 2:16:54but yeah that is how the args parameter
- 2:16:56works in python
- 2:16:58hey what's going on everybody it's bro
- 2:17:00hope you're doing well and in this video
- 2:17:02i'm going to be explaining quarks
- 2:17:04in python so sit back relax
- 2:17:07and enjoy the show
- 2:17:10all right everybody let's talk about
- 2:17:12quarkx now this is a parameter that will
- 2:17:14pack
- 2:17:15all arguments into a dictionary it's
- 2:17:17useful so that a function can accept a
- 2:17:20varying amount of keyword arguments it's
- 2:17:22identical
- 2:17:23to args except with args args will
- 2:17:26accept a varying amount of positional
- 2:17:28arguments
- 2:17:29and pack them into a tuple with quarks
- 2:17:31this will accept a varying amount of
- 2:17:33keyword arguments
- 2:17:34and pack them into a dictionary so
- 2:17:36here's an example of why this would be
- 2:17:38useful
- 2:17:38let's say we have a function called
- 2:17:40hello that accepts two keyword arguments
- 2:17:43a first name
- 2:17:44and a last name all this will do is
- 2:17:46output hello
- 2:17:47your first name and last name but what
- 2:17:49if somebody has more than two names
- 2:17:51let's say somebody has a middle name
- 2:17:53and for this example i'll just say my
- 2:17:54middle name is dude
- 2:17:56so if i were to run this well this hello
- 2:17:59function is no longer going to work
- 2:18:01as you can see in the console window we
- 2:18:03have a type error
- 2:18:04the hello function got an unexpected
- 2:18:06keyword argument middle
- 2:18:08one way in which we can solve this we
- 2:18:10can use this quark's
- 2:18:11parameter so we can accept a varying
- 2:18:14amount
- 2:18:15of keyword arguments so what we'll do is
- 2:18:17replace
- 2:18:18our parameters with two asterisks then
- 2:18:21quarks now if i would like the same
- 2:18:23output as i had before i'm going to need
- 2:18:25to change a few things around
- 2:18:26in order to access a value within a
- 2:18:29dictionary you type the name of the
- 2:18:30dictionary
- 2:18:31square brackets and then within quotes
- 2:18:34list your key
- 2:18:36so i would like to access the value at
- 2:18:38this key of first
- 2:18:40and let's repeat the process for last
- 2:18:42name
- 2:18:44so the key is going to be last we'll
- 2:18:46access this value
- 2:18:48at this key and this should work the
- 2:18:50same as it did before
- 2:18:52now let's take this a step further let's
- 2:18:54say we would like to display
- 2:18:55somebody's full name based on the amount
- 2:18:58of keyword arguments that they pass into
- 2:19:00this function
- 2:19:01well there's a few different ways in
- 2:19:02which we could do this one easy way is
- 2:19:05that we could write our program like
- 2:19:06this
- 2:19:08let's print the word hello
- 2:19:11and we will need to iterate once through
- 2:19:14each key value pair within this
- 2:19:16dictionary
- 2:19:17and we can use a for loop to do that so
- 2:19:19to iterate
- 2:19:20once through each key value pair in your
- 2:19:22dictionary
- 2:19:23we'll type for key value
- 2:19:26in the name of our dictionary quarks in
- 2:19:28this case
- 2:19:29dot items and then we will print
- 2:19:33each value
- 2:19:37so there's one more thing that we need
- 2:19:38to do this kind of works as intended but
- 2:19:40it's going to
- 2:19:41print each value including our word
- 2:19:44hello
- 2:19:44on a new line so with a standard print
- 2:19:48statement
- 2:19:48this will print a new line character
- 2:19:50after each print statement as if we're
- 2:19:52hitting enter
- 2:19:53we can actually change that so after
- 2:19:56your string
- 2:19:56add and equals and we can replace that
- 2:19:59new line character with something else
- 2:20:01maybe a space
- 2:20:02and let's do the same thing for our for
- 2:20:04loop so value
- 2:20:06comma end and then just add space now
- 2:20:08this will print
- 2:20:09all of your names on the same line hello
- 2:20:12bro
- 2:20:13dude code and now we can pass in a
- 2:20:15varying amount of keyword arguments
- 2:20:17based on how long our name is
- 2:20:19perhaps we have like a title title
- 2:20:22equals
- 2:20:23mr and we can print that as well
- 2:20:26hello mr bro dude code here's a fun
- 2:20:30fact of the day you don't necessarily
- 2:20:31need to name this parameter as quarks
- 2:20:34the important thing is to have
- 2:20:35these double asterisks that precede it
- 2:20:37quarks is just a
- 2:20:39common naming convention it's short for
- 2:20:41keyword arguments
- 2:20:42you could name this as something else
- 2:20:44perhaps something more descriptive like
- 2:20:45names since we're passing in a bunch of
- 2:20:47names so the name of our dictionary
- 2:20:49is now names and this would work the
- 2:20:52same as it did before
- 2:20:53the important thing is to have these
- 2:20:55double asterisks that precede the name
- 2:20:57of your dictionary
- 2:20:58but quarks is the common naming
- 2:21:00convention
- 2:21:01well everybody that's all about quarkx
- 2:21:03it's a parameter that will pack
- 2:21:05a varying amount of keyword arguments
- 2:21:07into a
- 2:21:08dictionary if you would like a copy of
- 2:21:10all this code i will post
- 2:21:12this in the comments section down below
- 2:21:13but yeah that is how quarkx
- 2:21:15works in python hey what's going on
- 2:21:18everybody
- 2:21:19it's your bro hope you're doing well and
- 2:21:20in this video i'm going to teach you
- 2:21:22guys all about the format method
- 2:21:23in python so sit back relax and enjoy
- 2:21:27the show
- 2:21:29okay ladies and gentlemen let's begin
- 2:21:32the format method
- 2:21:33is a method available to strings it's
- 2:21:35optional and it gives users
- 2:21:37more control when displaying output to
- 2:21:40best demonstrate this i'm going to use a
- 2:21:42line
- 2:21:42from a popular nursery rhyme titled hey
- 2:21:45diddle diddle because well
- 2:21:47you know i'm a mature adult like that
- 2:21:49i'll be printing
- 2:21:50this line to my console window the cow
- 2:21:53jumped over
- 2:21:54the moon let's do so using a standard
- 2:21:56print statement
- 2:21:57and then i'll show you the benefits of
- 2:21:59using the format method later
- 2:22:01for this example i have two variables a
- 2:22:04variable called animal and i'm going to
- 2:22:05store
- 2:22:06a string of cow and item and i'll be
- 2:22:09storing an item name
- 2:22:10which is moon when we compile and run
- 2:22:12this as you would expect
- 2:22:14the output is the cow jumped over the
- 2:22:16moon
- 2:22:17there's a more elegant way of writing
- 2:22:19this line of code
- 2:22:20and that is by using the format method
- 2:22:22available to strings
- 2:22:24i'm going to turn this line into a
- 2:22:26comment and we're instead
- 2:22:27going to produce the same output but use
- 2:22:29the format method
- 2:22:31instead so we need a standard print
- 2:22:33statement like usual
- 2:22:34and then a string that we would like to
- 2:22:36format so
- 2:22:37we're going to write the same thing but
- 2:22:39in place of the variables we're going to
- 2:22:41add a placeholder
- 2:22:42the instead of the variable name or
- 2:22:45value name i'm going to add
- 2:22:47a set of curly braces so that functions
- 2:22:50as a placeholder
- 2:22:51for a value or a variable the
- 2:22:55this should be cow jumped over
- 2:22:59the and i'm going to insert another
- 2:23:01placeholder
- 2:23:02now this is a string we can format the
- 2:23:04string by following
- 2:23:06this string with dot format parenthesis
- 2:23:10and within the parentheses of the format
- 2:23:12method we can insert
- 2:23:14some values or variables let's begin
- 2:23:16with some
- 2:23:17values i would like to insert cow
- 2:23:20as well as moon so this will produce
- 2:23:23the same output but it's written a
- 2:23:26little more elegantly
- 2:23:27we can also replace these with variables
- 2:23:30because
- 2:23:31they store these values so i'm going to
- 2:23:33replace cow
- 2:23:34with animal and moon with item
- 2:23:37and this will create the same output as
- 2:23:40it did before
- 2:23:41these curly braces are what's known as
- 2:23:43format fields
- 2:23:44they function as a placeholder for a
- 2:23:46value or
- 2:23:47a variable and they work in order the
- 2:23:50first format field will insert the first
- 2:23:52value
- 2:23:53at this location if you have another set
- 2:23:56or more sets of this format field you'll
- 2:23:59insert
- 2:24:00the next value that's listed within the
- 2:24:02format method
- 2:24:03so if we were to switch these around
- 2:24:06we'll switch the position of animal with
- 2:24:08item
- 2:24:09well then the first format field is
- 2:24:12going to insert
- 2:24:13our item and the second format field
- 2:24:15will insert our animal
- 2:24:17this time the moon jumped over the cow
- 2:24:20an additional way of inserting values at
- 2:24:22a given place holder would be to use
- 2:24:24what's referred to as a
- 2:24:26positional argument
- 2:24:29within these format fields we will list
- 2:24:32the index of the value that we would
- 2:24:33like to insert at this location
- 2:24:36if you need to insert the first value
- 2:24:38well that has an index
- 2:24:40of zero because computers always start
- 2:24:41with zero and following that pattern
- 2:24:44the next index would be one if you had
- 2:24:47let's say three values that you need to
- 2:24:49insert if you had another placeholder
- 2:24:51that would be two then so you would just
- 2:24:53follow that pattern
- 2:24:54so there's going to be no visible change
- 2:24:57but what we could do
- 2:24:58is reverse the positional arguments of
- 2:25:01these placeholders
- 2:25:02so what do you think would happen if i
- 2:25:04switched one with zero at these
- 2:25:06locations
- 2:25:07well this format field is going to
- 2:25:09insert the value found at index
- 2:25:11one this first value is index zero and
- 2:25:14the second one
- 2:25:15is index one so now the roles are
- 2:25:17reversed this time the moon
- 2:25:19jumped over the cow our last way of
- 2:25:22inserting some values at a given format
- 2:25:25field would be to use what's referred to
- 2:25:27as a
- 2:25:27keyword argument within
- 2:25:31the format method we're going to list
- 2:25:33some keyword argument pairs
- 2:25:35we need a keyword name followed by a
- 2:25:37value
- 2:25:38so let's say animal is going to be the
- 2:25:40keyword we're going to say equals
- 2:25:42some value animal equals cow and
- 2:25:45item equals moon and you may have
- 2:25:48noticed that the color of your text
- 2:25:50might have changed
- 2:25:51so we no longer need these variables
- 2:25:53actually um but i'm going to comment out
- 2:25:55these two print statements because we
- 2:25:57will get an error then
- 2:25:58so these are in keyword argument pairs
- 2:26:02and within our format field instead of
- 2:26:04keeping these empty
- 2:26:06or using an index we can use the keyword
- 2:26:08name at my first format field i'm going
- 2:26:10to insert my keyword
- 2:26:12of animal and at my second format field
- 2:26:15i would like to insert
- 2:26:17the item keyword so the result is going
- 2:26:20to be
- 2:26:20the cow jumped over the moon so let's
- 2:26:22reverse these now
- 2:26:23we'll place our item keyword here and
- 2:26:26then at the second format field we will
- 2:26:28insert
- 2:26:28our animal and this time the moon
- 2:26:32jumped over the cow here's an important
- 2:26:33concept that i'd like to bring to your
- 2:26:35attention
- 2:26:35with these values found within the
- 2:26:37format method you can actually reuse
- 2:26:39some of these
- 2:26:40more than once so let's say we would
- 2:26:42like to state
- 2:26:43the animal jumped over the animal we're
- 2:26:46going to use
- 2:26:46this animal keyword twice and our output
- 2:26:49is going to be
- 2:26:50the cow jumped over the cow if we're
- 2:26:52doing the same thing with positional
- 2:26:54arguments
- 2:26:55we can reuse the same index this time
- 2:26:58let's say
- 2:26:59that the moon jumped over the moon so
- 2:27:02our item has an
- 2:27:03index of one so within our first format
- 2:27:06field we'll state
- 2:27:08the index will be one and our second
- 2:27:10format field will also be one
- 2:27:12so our output's going to be the moon
- 2:27:14jumped over the moon
- 2:27:16all right let's take things up a level
- 2:27:17there's an even more elegant way in
- 2:27:19which we could write all of this
- 2:27:21what we'll do is store our string that
- 2:27:23we would like to format
- 2:27:25within a variable let's turn this line
- 2:27:27into a comment and let's create a
- 2:27:29variable called
- 2:27:30text text equals the string that we
- 2:27:32would like to format
- 2:27:33which is the format field
- 2:27:37jumped over the format field
- 2:27:41and if we need to format this variable
- 2:27:44we can just call the format method on it
- 2:27:47so within a print statement let's say
- 2:27:50text dot and then use the format method
- 2:27:54and pass
- 2:27:54in the values that we would like to
- 2:27:56insert at these placeholders
- 2:27:58so we're going to insert animal as well
- 2:28:02as item so our result as you would
- 2:28:05imagine
- 2:28:05is the cow jumped over the moon this
- 2:28:08next section i'm going to explain how we
- 2:28:10can add some padding
- 2:28:11to a string when we display it using the
- 2:28:13format method
- 2:28:14let's say we have a name variable and
- 2:28:16assign this a value of whatever your
- 2:28:18name is
- 2:28:19so i would like to display my name along
- 2:28:21with the custom message
- 2:28:22let's say hello my name
- 2:28:25is and then i'll use a format field here
- 2:28:28so let's follow this string by using the
- 2:28:30format method
- 2:28:31and i would like to insert my name at
- 2:28:33this location
- 2:28:34so currently this will display hello my
- 2:28:37name is bro
- 2:28:38or whatever else your name is so we can
- 2:28:40add some padding
- 2:28:41either before our name after or we can
- 2:28:44add some padding to the left and the
- 2:28:46right hand side
- 2:28:47of our value that we have so what we'll
- 2:28:49do to begin
- 2:28:51is let's add some padding to the right
- 2:28:53hand side of
- 2:28:54our name that we have so within our
- 2:28:56format field if we need to add some
- 2:28:58padding
- 2:28:59within the format field add a colon and
- 2:29:01then the amount of space you would like
- 2:29:03to allocate
- 2:29:04to displaying your value so let's say 10
- 2:29:07so i'm going to allocate 10 spaces worth
- 2:29:10of room
- 2:29:11to display my name you can't really see
- 2:29:13it right now but let's take a look
- 2:29:15so i can't move my cursor past this line
- 2:29:18at the end of my name
- 2:29:20but with my second line i have all of
- 2:29:22this room to work with
- 2:29:23so if i were to change my string that i
- 2:29:25have let's say
- 2:29:28nice to meet you well then
- 2:29:31all of this padding that i have is going
- 2:29:32to be more evident
- 2:29:34so we can add a number preceding with a
- 2:29:38colon to add some padding
- 2:29:40to a value and then you can left align
- 2:29:42this right align it or center align it
- 2:29:45so what i'll do is copy this line that i
- 2:29:47have
- 2:29:48if you need to left align it you use the
- 2:29:51less than sign
- 2:29:52but there's going to be no visible
- 2:29:53change because that's already the
- 2:29:55default
- 2:29:56if you need to write a line precede your
- 2:29:59number with a greater than sign
- 2:30:01so your value is going to be right
- 2:30:04aligned
- 2:30:05if you need to center this value then
- 2:30:07use
- 2:30:08the carrot so this will center align
- 2:30:11your value within the padding that you
- 2:30:14have allotted
- 2:30:15one question that you might have for me
- 2:30:16and i'll try and answer that now because
- 2:30:18it's relevant what if we need to add a
- 2:30:20positional argument or a
- 2:30:22keyword argument to our format field if
- 2:30:24there's already some text within here
- 2:30:26so what you would do before the colon
- 2:30:28you would just precede the colon
- 2:30:30with either your positional argument or
- 2:30:32your keyword argument
- 2:30:34followed by colon and then whatever you
- 2:30:36want to add afterwards to format
- 2:30:38your value that you have alright ladies
- 2:30:40and gentlemen we are near the end and i
- 2:30:42promise that this is the last section
- 2:30:44how can we format some numbers let's say
- 2:30:46we have a
- 2:30:47number variable and this will equal the
- 2:30:50first few digits of pi
- 2:30:523.14159 let's pretend that we would like
- 2:30:55to
- 2:30:56display only the first two digits after
- 2:30:59the decimal so we can do so using the
- 2:31:02format method
- 2:31:03let's print the number
- 2:31:06pi is and then i'll use a format field
- 2:31:09here
- 2:31:10and we will call the format method and
- 2:31:12insert our
- 2:31:13number that we have so if i need to
- 2:31:17display only the first two digits after
- 2:31:20the decimal
- 2:31:21within the format field i will add
- 2:31:24colon dot to f
- 2:31:28f is for floating point numbers that's
- 2:31:30anything with a decimal portion
- 2:31:32so the result is that this will display
- 2:31:35only the first
- 2:31:36two digits after my decimal because we
- 2:31:39placed two within
- 2:31:41the format field so if this was three
- 2:31:44point three f this would display three
- 2:31:47digits
- 2:31:47after the decimal portion oh and
- 2:31:50apparently this will round your number
- 2:31:51so that's something that you should keep
- 2:31:53in mind
- 2:31:53so this time let's change our number to
- 2:31:55something larger like
- 2:31:561000 and i will copy this line and make
- 2:31:59a few changes
- 2:32:00let's say the number is
- 2:32:04and i would like to add a comma at the
- 2:32:06thousands place
- 2:32:07so after the colon within the format
- 2:32:09field i will add a comma
- 2:32:11so this will automatically add a comma
- 2:32:13to all 1000s places
- 2:32:15another cool thing that you can do is
- 2:32:17that you can display your number as
- 2:32:19binary so within the format field add
- 2:32:21colon b
- 2:32:22this will display a binary
- 2:32:25representation of your number
- 2:32:27you can do the same thing with octal by
- 2:32:30adding colon
- 2:32:31o so our number is now being displayed
- 2:32:34as an octal number
- 2:32:35and you can do the same thing with
- 2:32:37hexadecimal lowercase x
- 2:32:39for lowercase or uppercase x for all
- 2:32:42uppercase
- 2:32:42so my number 1000 is now in hexadecimal
- 2:32:46and lastly we can display this number
- 2:32:49in scientific notation using either
- 2:32:53lowercase e for lowercase or capital e
- 2:32:55for uppercase
- 2:32:56and mynumber 1000 is now in scientific
- 2:33:00notation
- 2:33:01all right everybody so that's a few
- 2:33:03things that you can do with the format
- 2:33:05method it's an optional method that
- 2:33:07gives users
- 2:33:08more control when displaying output if
- 2:33:10you would like a copy of all the code
- 2:33:12that we've written here today i will
- 2:33:13post everything in the comments down
- 2:33:15below
- 2:33:16don't be afraid to smash that like
- 2:33:17button drop a comment down below and
- 2:33:20subscribe if you'd like to become a
- 2:33:21fellow bro
- 2:33:22hey what's going on everybody it's bro
- 2:33:24hope you're doing well and in this video
- 2:33:26i'm going to explain a few useful
- 2:33:27methods of the random module
- 2:33:29so sit back relax and enjoy the show
- 2:33:34okay people in this video i'm going to
- 2:33:36explain how we can generate some random
- 2:33:38numbers
- 2:33:39as well as a few other useful methods of
- 2:33:41the random module
- 2:33:43now we're not going to be creating true
- 2:33:45random numbers but something called
- 2:33:47pseudorandom numbers which are fairly
- 2:33:49darn close so the first step
- 2:33:51is to import the random module at the
- 2:33:53top import
- 2:33:54random we now have access to everything
- 2:33:57that the random module has to offer
- 2:33:59let's begin by generating a random
- 2:34:01number between one and six
- 2:34:02like we're rolling a dice so what i'm
- 2:34:04going to do is say x
- 2:34:06equals random dot rand
- 2:34:10and we can generate a random integer
- 2:34:12between a certain range
- 2:34:13if i want one through six i will state
- 2:34:16one
- 2:34:17comma six and then we can generate a
- 2:34:19random number between one and six like
- 2:34:21we're rolling a dice
- 2:34:22so if i were to print the value of x
- 2:34:26then we will get a random number between
- 2:34:27one and six for example i just got a one
- 2:34:30a two a three and a six all right
- 2:34:33we can also generate a random floating
- 2:34:35point number two
- 2:34:37let's say y equals random dot
- 2:34:40random so this will give us a random
- 2:34:43number between
- 2:34:440 and 1. this time i'm going to print
- 2:34:47y so we have 0.49 and some change
- 2:34:520.145 and 0.858
- 2:34:56we can also generate a random choice
- 2:34:58from a list or other collection
- 2:35:00let's say we're playing a game of rock
- 2:35:02paper scissors so let's create a list
- 2:35:04i'll just call this my list equals
- 2:35:08square brackets and we will have three
- 2:35:10choices
- 2:35:11rock paper and
- 2:35:14scissors
- 2:35:18so let's say z
- 2:35:21will be the random string that we
- 2:35:24generate
- 2:35:25z equals random dot
- 2:35:28choice and we will pass in my list
- 2:35:33and let's print z so this will generate
- 2:35:36a
- 2:35:37random choice from my list called my
- 2:35:40list
- 2:35:40so we got scissors paper and
- 2:35:43rock we can also use the shuffle method
- 2:35:46of the random module to
- 2:35:47shuffle a list or other collection let's
- 2:35:50say we're working with a deck of cards
- 2:35:52cards equals let's say one
- 2:35:56through nine
- 2:36:00and we have a jack a queen
- 2:36:04a king and an ace this won't be a full
- 2:36:08deck of cards it'll just be one suit
- 2:36:10so we can actually shuffle this list by
- 2:36:13using the shuffle method
- 2:36:14random dot shuffle what do we want to
- 2:36:18shuffle
- 2:36:18we want to shuffle our cards and i will
- 2:36:22display
- 2:36:23my cards with a print statement so the
- 2:36:26shuffle method will shuffle a list or
- 2:36:28other collection for you
- 2:36:29well everybody that's a few useful
- 2:36:31methods of the random module
- 2:36:33if you would like a copy of all this
- 2:36:34code i will post this in the comments
- 2:36:36down below
- 2:36:37don't be afraid to smash that like
- 2:36:39button drop a comment down below
- 2:36:40and subscribe if you'd like to become a
- 2:36:42fellow bro hey what's going on everybody
- 2:36:45it's
- 2:36:45bro hope you're doing well and in this
- 2:36:47video i'm going to explain exception
- 2:36:49handling in python
- 2:36:50so sit back relax and enjoy the show
- 2:36:55well everyone let's talk about
- 2:36:57exceptions an exception is an event
- 2:36:59detected
- 2:37:00during execution that interrupts the
- 2:37:02normal flow of a program
- 2:37:04here's a program that i've written and
- 2:37:06we're intentionally going to cause an
- 2:37:07exception
- 2:37:08so this program that i've written we
- 2:37:10will accept a numerator and a
- 2:37:12denominator from the user
- 2:37:13we'll divide the numerator by the
- 2:37:15denominator we'll get a result
- 2:37:17and print the result so what if i were
- 2:37:19to divide a number such as 5
- 2:37:21divided by 0 which we cannot
- 2:37:23mathematically do
- 2:37:24well this will cause an exception an
- 2:37:26event detected
- 2:37:27during execution that interrupts the
- 2:37:30flow of a program
- 2:37:31so our program was interrupted and
- 2:37:33within our console window we have this
- 2:37:35traceback message
- 2:37:36which states what went wrong and a name
- 2:37:39for this exception that occurred
- 2:37:41this specific exception that occurred is
- 2:37:43a zero division error
- 2:37:44so this video is all about handling
- 2:37:47these exceptions so they do not
- 2:37:48interrupt the normal flow of a program
- 2:37:51now a very basic form of exception
- 2:37:53handling
- 2:37:53is to surround any code that is
- 2:37:55considered dangerous as in it might
- 2:37:57cause an exception
- 2:37:58within a try block anytime you accept
- 2:38:01user input would be a good indicator too
- 2:38:03so this code is considered dangerous
- 2:38:05because we don't know what the user is
- 2:38:06going to type in
- 2:38:07so we will try all of this code if an
- 2:38:10exception occurs
- 2:38:11we can catch it and handle it so to do
- 2:38:13that
- 2:38:14we'll add an accept block accept and
- 2:38:16then we will catch
- 2:38:18exception normally having this block by
- 2:38:21itself
- 2:38:21isn't considered good practice but i'll
- 2:38:23explain why later so
- 2:38:25this accept exception block will catch
- 2:38:27all sorts of different exceptions
- 2:38:29and it will prevent our program from
- 2:38:31being interrupted and we can do
- 2:38:32something else entirely such as print a
- 2:38:34message
- 2:38:35something went wrong so let's try this
- 2:38:39again
- 2:38:40we will divide five by zero which we
- 2:38:42cannot do
- 2:38:43we no longer have that traceback message
- 2:38:45within our console window
- 2:38:46we caught the exception and we did
- 2:38:48something else we printed
- 2:38:50something went wrong like i said before
- 2:38:53it's not considered good practice to
- 2:38:55have a single
- 2:38:56accept block that will handle all
- 2:38:58exceptions it's much better to first
- 2:39:00handle specific exceptions when they
- 2:39:02occur
- 2:39:02and we can do so by writing additional
- 2:39:05accept blocks
- 2:39:06so let's create an additional accept
- 2:39:08block that will catch
- 2:39:10any zero division error exceptions
- 2:39:13that occurs when somebody attempts to
- 2:39:14divide by zero so if
- 2:39:16this exception occurs how do we want to
- 2:39:18handle it let's just print the message
- 2:39:20let's print you can't divide
- 2:39:24by zero idiot
- 2:39:27and let's try this again so five divided
- 2:39:30by zero
- 2:39:31you can't divide by zero idiot here's
- 2:39:33another one i'm going to turn
- 2:39:35this accept block into a comment so we
- 2:39:38can take a look at something
- 2:39:39what if i divide by something that's not
- 2:39:41a number like we take
- 2:39:425 and divide it by the word pizza well
- 2:39:45pizza is not a number
- 2:39:46and we ran into a value error exception
- 2:39:50so we should probably handle this too so
- 2:39:52let's create another accept block that
- 2:39:54will handle that
- 2:39:55accept value error
- 2:39:59and how do we want to handle this let's
- 2:40:02print something
- 2:40:03enter only numbers
- 2:40:06please okay let's try this again and see
- 2:40:10if
- 2:40:10this exception is caught five divided by
- 2:40:13the word
- 2:40:14pizza enter only numbers please
- 2:40:17not only that but you can also add the
- 2:40:20accept exception block at the very end
- 2:40:23just in case there's something you won't
- 2:40:24anticipate
- 2:40:25but it's always considered good practice
- 2:40:27to first catch any specific exceptions
- 2:40:30and let the user know exactly what went
- 2:40:31wrong an additional way in which we can
- 2:40:34handle these exceptions
- 2:40:35is that we could display the exception
- 2:40:38that occurs although it is completely
- 2:40:39optional
- 2:40:40so to do that what we'll do is that
- 2:40:42after each of these accept block
- 2:40:44definitions
- 2:40:45let's add as e so we're calling the
- 2:40:48exception as
- 2:40:49e that's just standard practice although
- 2:40:50it's not necessary
- 2:40:52so let's do that for each of these
- 2:40:54except blocks
- 2:40:55and then we can print whatever e is so
- 2:40:58let's add that
- 2:40:59to each of these blocks so
- 2:41:02it should now look something like this
- 2:41:04so when we encounter an exception
- 2:41:06we can display what exception occurred
- 2:41:09too as well
- 2:41:10so let's divide five by pizza invalid
- 2:41:13literal for int with base 10 pizza
- 2:41:16enter only numbers please so like i said
- 2:41:19this
- 2:41:20is an additional way in which you could
- 2:41:21handle an exception although it's
- 2:41:23entirely optional
- 2:41:24another thing that you can do is that
- 2:41:26you can add an else statement
- 2:41:28to the end of your accept blocks so
- 2:41:30what's happening here is that we're
- 2:41:32going to
- 2:41:32try all of this code if an exception
- 2:41:35happens
- 2:41:36then catch it and handle it if not then
- 2:41:38do something else
- 2:41:40so why don't we print our result only if
- 2:41:42there are no
- 2:41:43exceptions that occur so if there are no
- 2:41:46exceptions we will execute
- 2:41:48this else statement if not then we won't
- 2:41:51so
- 2:41:51let's divide five by an actual number
- 2:41:53this time like
- 2:41:54two so there are no exceptions that
- 2:41:57occurred so
- 2:41:58we executed this else statement whatever
- 2:42:00code is within
- 2:42:01our else block and this printed the
- 2:42:03result of 2.5
- 2:42:05but if an exception were to occur like
- 2:42:07we divide 5 by
- 2:42:080 then we will catch our exception and
- 2:42:11handle it
- 2:42:12and we do not execute whatever block of
- 2:42:15code is within
- 2:42:16our else statement there is one other
- 2:42:18clause that you should be aware of that
- 2:42:20is the
- 2:42:20final clause this is always at the end
- 2:42:22how this works
- 2:42:23is that whether or not we catch an
- 2:42:25exception we will always execute any
- 2:42:28code
- 2:42:28that is within the block of code for our
- 2:42:30final clause
- 2:42:32so this is a good opportunity if you
- 2:42:34open files
- 2:42:35to close them within the finally block
- 2:42:38but
- 2:42:38we haven't dealt with opening files yet
- 2:42:40so we won't do that
- 2:42:41all we'll do is just print something
- 2:42:43print this
- 2:42:45will always execute so
- 2:42:48whether or not we encounter an exception
- 2:42:50this finally block will always execute
- 2:42:52at the end
- 2:42:53so let's try this again so this time
- 2:42:55let's cause an exception
- 2:42:56let's divide five by zero you can't
- 2:42:59divide by
- 2:43:00zero this will always execute and this
- 2:43:02time let's take this seriously and
- 2:43:04divide
- 2:43:04five by another number such as three so
- 2:43:07this will give us a result
- 2:43:09and like i said the finally clause the
- 2:43:11code within the finally clause will
- 2:43:13always execute
- 2:43:14but we won't really be working with this
- 2:43:16until we get to the section on file
- 2:43:18handling
- 2:43:19well everybody those are exceptions in
- 2:43:21python they are events detected
- 2:43:24during execution that interrupt the flow
- 2:43:26of a program
- 2:43:27and we can handle these exceptions by
- 2:43:29using try and accept blocks
- 2:43:31so if you would like a copy of all this
- 2:43:33code i will post all of this in the
- 2:43:34comments down below
- 2:43:36but yeah that is how exceptions work in
- 2:43:38python
- 2:43:40hey what's going on everybody it's bro
- 2:43:42hope you're doing well
- 2:43:43and in this video we're going to be
- 2:43:45covering some basic file detection using
- 2:43:47python so
- 2:43:48sit back relax and enjoy the show
- 2:43:53welcome ladies and gentlemen to the
- 2:43:55beginning of a mini series involving
- 2:43:57doing stuff with files using python and
- 2:43:59in this video we're going to be doing
- 2:44:00some basic
- 2:44:01file detection and in order to do so i
- 2:44:04recommend
- 2:44:04importing the os module it's already
- 2:44:07included with the standard python
- 2:44:08library
- 2:44:09all you need to do is import it all
- 2:44:11we'll be doing is checking to see if a
- 2:44:13file
- 2:44:14exists someplace on our computer so
- 2:44:16we'll probably need a file to work with
- 2:44:17what i'm going to do is go to my desktop
- 2:44:20for convenience
- 2:44:21and just create a new text document i
- 2:44:23will call this test.txt
- 2:44:25now i probably need the file location so
- 2:44:28i'm going to copy that
- 2:44:30and then we can use this so let's create
- 2:44:32a variable called path
- 2:44:34that will include the path to the file
- 2:44:36that i would like to detect
- 2:44:38so that was test.txt now if you have
- 2:44:41backslashes in your file path you'll
- 2:44:44probably need
- 2:44:45double backslashes because that's the
- 2:44:47escape sequence for a backslash within a
- 2:44:49string
- 2:44:50so we now have this variable path that
- 2:44:53we can work with
- 2:44:54now i would like to check to see if this
- 2:44:56location
- 2:44:57exists on my computer so if os
- 2:45:00dot path dot exists and you can see that
- 2:45:04we can pass in
- 2:45:05our path as an argument so i will pass
- 2:45:07in my path
- 2:45:08and if this returns true if that
- 2:45:10location exists i will print
- 2:45:14that location exists
- 2:45:18so this will not tell me if this is a
- 2:45:21file or not
- 2:45:22we'll be covering that later else
- 2:45:25let's print that location
- 2:45:29doesn't exist
- 2:45:32all right so if this location exists
- 2:45:35this will return true
- 2:45:37if not this will execute our else
- 2:45:39statement
- 2:45:40that location exists so what if i were
- 2:45:43to delete this file
- 2:45:44and we run this program again that
- 2:45:46location doesn't exist
- 2:45:48now this won't tell you if the location
- 2:45:51you're dealing with
- 2:45:52is a file or not there is a separate
- 2:45:55function for that
- 2:45:56it is if os
- 2:45:59dot path dot is file
- 2:46:05what we'll do is pass in our path and
- 2:46:07print
- 2:46:09that is a file
- 2:46:12all right so let's create another file
- 2:46:16because i deleted the previous one
- 2:46:18test.txt it's in the same location let's
- 2:46:21run this again
- 2:46:22that location exists that is a file now
- 2:46:25what if this was a folder
- 2:46:27so i'm going to create a new folder i
- 2:46:29will call this folder
- 2:46:30i will delete my test document
- 2:46:34and let's change the location to folder
- 2:46:39that location exists but it is not a
- 2:46:41file you can check to see if a location
- 2:46:43is a directory
- 2:46:44so let's write an else if statement os
- 2:46:48dot path dot is directory and we will
- 2:46:52pass in our path
- 2:46:54and let's print that
- 2:46:57is a directory
- 2:47:01all right so we have our folder we're
- 2:47:03going to check to see
- 2:47:05if this location exists and if it's a
- 2:47:08file or
- 2:47:09a directory that location exists that is
- 2:47:12a directory
- 2:47:13well everyone that is the basics of file
- 2:47:15detection
- 2:47:16we'll be building upon this concept in
- 2:47:18future videos
- 2:47:19so if you would like a copy of this code
- 2:47:21i will post this in the comment section
- 2:47:23down below
- 2:47:24but yeah that is the basics of simple
- 2:47:26file detection using python
- 2:47:28hey what's going on everybody it's bro
- 2:47:31hope you're doing well and in this video
- 2:47:32we're going to be reading a file in
- 2:47:34python so
- 2:47:35sit back relax and enjoy the show
- 2:47:40welcome back guys and gals in this video
- 2:47:42i'm going to explain how we can read the
- 2:47:44contents of a file using python
- 2:47:46so we'll probably need some sort of file
- 2:47:48to work with i created a plain text file
- 2:47:50called
- 2:47:51test.txt it's a plain text file and all
- 2:47:54this says
- 2:47:55is omg you can read this have a nice day
- 2:47:58like comment and subscribe so what i
- 2:48:01would like to do is to read
- 2:48:02the contents of that file line by line
- 2:48:04and print it to the console window
- 2:48:06and here's how and honestly we only need
- 2:48:08two lines of code to do this
- 2:48:10so what we'll do is type with open
- 2:48:13then within the open function list the
- 2:48:15name of your file
- 2:48:17or the file path this file of mine is
- 2:48:20within my project folder
- 2:48:21so i only need to list the file name if
- 2:48:24this was someplace else you'll probably
- 2:48:26need
- 2:48:27the file path so let's say this was on
- 2:48:30my desktop
- 2:48:31well i'll probably need to list the file
- 2:48:33path of where that file is located
- 2:48:35and i'll need to use double backslashes
- 2:48:38because that's the escape sequence for a
- 2:48:40backslash
- 2:48:41but since this file is within my project
- 2:48:43folder i only need to list the file name
- 2:48:45so with open then the name your file or
- 2:48:49the file path
- 2:48:50as file then what we're going to do
- 2:48:54is print file dot
- 2:48:58read and that is it so let's test it
- 2:49:02and it works omg you can read this have
- 2:49:05a nice day
- 2:49:06like comment and subscribe so this will
- 2:49:09actually
- 2:49:09close files automatically after opening
- 2:49:12them
- 2:49:12so that's kind of convenient and let's
- 2:49:14just test that theory
- 2:49:15so i'm going to print file dot
- 2:49:19closed if my file is in fact closed this
- 2:49:22will print
- 2:49:22true if it's still open this will print
- 2:49:24false
- 2:49:26so normally when you read a file it's
- 2:49:28normally open and you need to
- 2:49:29close it manually but if you write your
- 2:49:31program this way
- 2:49:32with open this will close the file
- 2:49:35automatically for you
- 2:49:36so using with open will close any files
- 2:49:39automatically for you
- 2:49:41after they've been opened however this
- 2:49:43does not catch
- 2:49:44and handle any exceptions that might
- 2:49:46occur like if we cannot locate this file
- 2:49:49so let's say that i mistyped the
- 2:49:51extension for this file
- 2:49:53let's say that i type in test.tx
- 2:49:56that's kind of funny because tx is the
- 2:49:58abbreviation for
- 2:49:59texas it's a texas file so this will
- 2:50:02cause
- 2:50:03a file not found error and this will
- 2:50:05interrupt the flow of our program and we
- 2:50:08would probably like to prevent that
- 2:50:09so what we could do is write a try and
- 2:50:12an accept block
- 2:50:13so we will try all of this code if we
- 2:50:15cannot find
- 2:50:16this file we can handle this exception
- 2:50:19file not found error
- 2:50:20so we'll place all of this code within
- 2:50:22the try block then
- 2:50:23if an exception happens we will catch
- 2:50:25that exception
- 2:50:26except we would like to catch any file
- 2:50:29not found air
- 2:50:30exceptions and let's handle this by
- 2:50:33printing
- 2:50:35that file was not found
- 2:50:40so if i were to run this again we will
- 2:50:42catch and handle this exception and this
- 2:50:44will not
- 2:50:45interrupt the normal flow of our program
- 2:50:48well everybody that's how to open and
- 2:50:50read
- 2:50:50a file in python if you'd like a copy of
- 2:50:53all this code i will post this in the
- 2:50:55comment section down below
- 2:50:56but yeah that's how to open and read a
- 2:50:58file using python
- 2:51:00hey what's going on everybody it's bro
- 2:51:03hope you're doing well and in this video
- 2:51:04we're going to be writing files in
- 2:51:06python so sit back
- 2:51:07relax and enjoy the show
- 2:51:11so to begin it's kind of similar to the
- 2:51:13last video on reading files
- 2:51:15with open within the open function list
- 2:51:18the file name or the file path so what
- 2:51:21do you want to call this file
- 2:51:22i will call this file test dot txt
- 2:51:26now normally with the open function
- 2:51:28there is a mode
- 2:51:29and you can actually change that there's
- 2:51:31a second argument that you can pass in
- 2:51:33by default this is r for read but if you
- 2:51:36want to write a file this would be
- 2:51:38w so now we're going to write a file
- 2:51:41called
- 2:51:42test.txt so to finish with open
- 2:51:46as file what are we going to do file
- 2:51:49dot write and within the write function
- 2:51:52we can write some text
- 2:51:54so let's create a text variable and we
- 2:51:57will add a string
- 2:51:58so let's say text equals
- 2:52:03yo and if you need to go down to a new
- 2:52:06line
- 2:52:07be sure to use the new line character so
- 2:52:09after this new line character
- 2:52:11the text that comes after is going to be
- 2:52:13on the next line as if we're hitting
- 2:52:15enter
- 2:52:16so yo this is some
- 2:52:19text have
- 2:52:22a good one
- 2:52:26all right so after i run this program we
- 2:52:29should have
- 2:52:29a file a text file within our project
- 2:52:32folder that appears
- 2:52:34and it is right here and it says yo this
- 2:52:37is some text
- 2:52:38have a good one now this is in write
- 2:52:41mode
- 2:52:42if this text was something else it will
- 2:52:44actually
- 2:52:45overwrite your current file so if i were
- 2:52:47to change this to
- 2:52:49uh oh this text has
- 2:52:53been over written well then
- 2:52:56this will overwrite the text that we
- 2:52:59have
- 2:52:59uh oh this text has been overwritten so
- 2:53:02you can actually append a file by
- 2:53:04changing the mode
- 2:53:05to a for append so let me change this
- 2:53:08back
- 2:53:09we will rewrite this this will save over
- 2:53:12our current file
- 2:53:13now we can append some text so let's add
- 2:53:15maybe a new line
- 2:53:17uh have a nice
- 2:53:20day see ya so i would like to append
- 2:53:25some text to this file i'm going to
- 2:53:26change the mode to a
- 2:53:28for append and we can now append some
- 2:53:31text onto the end of this file
- 2:53:33well everybody that's how to write to a
- 2:53:35file and or append a file if you would
- 2:53:37like a copy of this code
- 2:53:38i will post this in the comment section
- 2:53:40down below but yeah that's how to
- 2:53:42write to and or append a file in python
- 2:53:46hey what's going on everybody it's bro
- 2:53:48hope you're doing well
- 2:53:49and in this video we're going to be
- 2:53:50copying files in python so
- 2:53:52sit back relax and enjoy the show
- 2:53:57welcome back to another video regarding
- 2:53:59doing stuff with files using python and
- 2:54:01in this video we're going to be
- 2:54:02copying some files and in order to do so
- 2:54:05i recommend
- 2:54:06importing the shuttle module there's
- 2:54:08other ways of doing this too
- 2:54:10but i tend to like using the shuttle
- 2:54:11module now
- 2:54:13with this module there are three basic
- 2:54:15functions
- 2:54:16to copy a file starting at the top they
- 2:54:19are copy file
- 2:54:20copy and copy to so copy file is what
- 2:54:24we'll be working with
- 2:54:25this will copy the contents of a file
- 2:54:27copy
- 2:54:28will do everything that copy file does
- 2:54:31plus copy
- 2:54:32the permissions mode and the destination
- 2:54:34can be a directory
- 2:54:36and lastly copy 2 this will do
- 2:54:38everything
- 2:54:39that copy does plus it copies the
- 2:54:41metadata
- 2:54:42of that file including the files
- 2:54:44creation and modification times
- 2:54:46so depending on the project or the
- 2:54:48program you're working with
- 2:54:49you may need to use copy or copy too but
- 2:54:52in this video we're going to be using
- 2:54:54copy file just to copy the contents of a
- 2:54:56file
- 2:54:57now after you import the shuttle module
- 2:55:00this is all you need to do to copy a
- 2:55:01file
- 2:55:02shuttle dot then use the appropriate
- 2:55:05function that you need to copy your file
- 2:55:07we're going to be using copy file copy
- 2:55:10file and within this function there are
- 2:55:12two arguments
- 2:55:13a source and a destination so within my
- 2:55:17project folder i have a plain
- 2:55:19text file called test test.txt
- 2:55:22all this says is yo this is some text
- 2:55:25see ya what i would like to do is copy
- 2:55:27this file because it's cool
- 2:55:29now since this file is within my project
- 2:55:32folder i only have to list the file name
- 2:55:34otherwise if this is someplace else i
- 2:55:36would list the file path here
- 2:55:38so this file is test.txt
- 2:55:42this is the source file and now i need a
- 2:55:45destination
- 2:55:46so the second argument will be separated
- 2:55:48with the comma i'm going to list
- 2:55:50a destination so you can actually rename
- 2:55:53this to
- 2:55:53i would like to name this as copy.txt
- 2:55:57so the destination is my project folder
- 2:56:00so when i create a copy of this file
- 2:56:02test.txt
- 2:56:04this copy will appear in my project
- 2:56:06folder and say the same thing
- 2:56:07so after running this we now have a
- 2:56:11file named copy.txt that says the same
- 2:56:13thing
- 2:56:14as my test file and remember what i said
- 2:56:16you can copy
- 2:56:17this file to a different location on
- 2:56:19your computer so you could list the file
- 2:56:22path
- 2:56:22so this file path would go to my desktop
- 2:56:25and then when i run this
- 2:56:26this will copy my original file my
- 2:56:28source and then copy it to
- 2:56:30wherever my destination is oh and i
- 2:56:32almost forgot so if you need to use copy
- 2:56:34or copy 2
- 2:56:35the arguments are exactly the same so if
- 2:56:38you needed to use copy
- 2:56:40this would be a shuttle dot copy instead
- 2:56:42of copy file
- 2:56:43then copy 2 is shuttle dot copy 2.
- 2:56:46the arguments are exactly the same but
- 2:56:48each of these functions will copy
- 2:56:50different things depending on what you
- 2:56:52need for your own program or project
- 2:56:54all right everybody so that's the basics
- 2:56:56of copying files and
- 2:56:58python i will post this in the comment
- 2:57:00section down below
- 2:57:01but yeah that is how to copy files using
- 2:57:04python
- 2:57:05hey what's going on everybody it's bro
- 2:57:08hope you're doing well and in this video
- 2:57:09i'm going to explain how we can move
- 2:57:11files using python so sit back relax
- 2:57:15and enjoy the show
- 2:57:18welcome back everybody to another video
- 2:57:20regarding that thing that we do called
- 2:57:22programming and in this video we're
- 2:57:23going to be moving files using python
- 2:57:26and in order to do so i recommend
- 2:57:28importing the os module
- 2:57:30this is included with the standard
- 2:57:32python library all you need to do is
- 2:57:34import it
- 2:57:35and now let's create two variables a
- 2:57:37variable called source
- 2:57:38that will hold the location of where our
- 2:57:41source file is located
- 2:57:42as well as a variable called destination
- 2:57:45to function as well
- 2:57:46the destination now for this video i'm
- 2:57:49going
- 2:57:50to move a file within my project folder
- 2:57:52to my desktop
- 2:57:53so to do so i'm going to create a new
- 2:57:56file
- 2:57:57i will name this test.txt but take the
- 2:58:00liberty to name this whatever you want
- 2:58:02so i'm going to list the location of
- 2:58:05where
- 2:58:05my test file is located since this is
- 2:58:08within my project folder i only need
- 2:58:10the file name if this was someplace else
- 2:58:12on your computer you would probably need
- 2:58:14the file path
- 2:58:15and now where would i like to move this
- 2:58:16file to i would like to move this file
- 2:58:19to my desktop
- 2:58:20so i just need the file path so i'm
- 2:58:22going to go to my desktop
- 2:58:24right click go to properties i'm going
- 2:58:27to copy
- 2:58:28this location and paste it within my
- 2:58:30destination
- 2:58:31and i will add slash desktop
- 2:58:34slash test.txt you can also rename this
- 2:58:38too if you want i will just keep it the
- 2:58:40same
- 2:58:40now if you have single backslashes
- 2:58:43you'll likely need to change these
- 2:58:45to double backslashes because that is
- 2:58:47the escape sequence
- 2:58:49to print a backslash within a string so
- 2:58:52we have a
- 2:58:52source location as well as a destination
- 2:58:55location
- 2:58:56now we can begin i would recommend
- 2:58:58writing our code within a try and accept
- 2:59:00block to
- 2:59:01handle any exceptions so i would like to
- 2:59:03handle any file
- 2:59:05not found error exceptions just in case
- 2:59:08our program cannot locate our source
- 2:59:10file so let's print a message if that is
- 2:59:12the case
- 2:59:13perhaps source plus was not
- 2:59:17found now within our try block let's
- 2:59:20check to see if there is already a file
- 2:59:22at this destination
- 2:59:23because i would not like to save over it
- 2:59:25if there's already a file there
- 2:59:27so let's do some basic file detection if
- 2:59:30os
- 2:59:31dot path dot exists
- 2:59:35and i will pass in my destination as an
- 2:59:37argument to the exists
- 2:59:39function so if there is already a file
- 2:59:42here i should probably let the user know
- 2:59:44print there is already a
- 2:59:47file there else
- 2:59:51we can replace our files honestly if you
- 2:59:53do not care about saving over any files
- 2:59:56you can omit this part so else os
- 2:59:59dot replace and we will pass
- 3:00:02in our source and our destination
- 3:00:06as arguments and let's print a message
- 3:00:09to the console window to let the user
- 3:00:11know
- 3:00:11that source plus was
- 3:00:16moved all right let's try this oh i
- 3:00:19accidentally deleted my
- 3:00:21file test.txt all right
- 3:00:24now this should disappear in three
- 3:00:27two one and it's gone
- 3:00:31test.txt was moved and it should now be
- 3:00:33on my desktop
- 3:00:35which it is now you can also use this to
- 3:00:37move a directory as well
- 3:00:39i'm going to create a new directory i
- 3:00:42will name this folder
- 3:00:44and i will change source to folder
- 3:00:47and the destination from test.txt to
- 3:00:50folder and now this should move my
- 3:00:53folder
- 3:00:55there is already a file there
- 3:00:59oh let's delete that okay
- 3:01:02folder was moved all right everybody so
- 3:01:05that is how
- 3:01:06to move a file and or directory using
- 3:01:08python
- 3:01:09if you would like a copy of this code i
- 3:01:11will post this in the comment section
- 3:01:13down below
- 3:01:14but yeah that is how to move a file and
- 3:01:17or directory
- 3:01:18using python hey what's going on
- 3:01:21everybody
- 3:01:21it's bro hope you're doing well and in
- 3:01:23this video i'm going to teach you guys
- 3:01:25how we can delete files using python so
- 3:01:27sit back relax and enjoy the show
- 3:01:32all right everybody so in this video
- 3:01:35we're going to be deleting files using
- 3:01:37python and in order to do so i recommend
- 3:01:39importing the os module
- 3:01:41this is already included with the
- 3:01:42standard python library all you need to
- 3:01:44do
- 3:01:45is import it and now we need a file to
- 3:01:47work with
- 3:01:48so let's right click on our projects
- 3:01:49folder and go to new
- 3:01:51file i will name this test.txt
- 3:01:54and we now have a file that we can work
- 3:01:56with but you know what i would like to
- 3:01:58delete this file because it's lame
- 3:02:00so let's do that in order to delete this
- 3:02:02file let's type os
- 3:02:03dot remove and we're going to list the
- 3:02:05file path here
- 3:02:07since this file is within my project
- 3:02:09folder i only need to list
- 3:02:10the file path test.txt if this was
- 3:02:14someplace else on my computer i would
- 3:02:16probably need the file path
- 3:02:17like that but since this file is within
- 3:02:19my project folder i only need
- 3:02:21the file name another thing that you can
- 3:02:23do too at least what i like to do
- 3:02:25is that i like to pass in a variable
- 3:02:27called path i will make this a variable
- 3:02:29and assign it a
- 3:02:31string value of whatever the path to my
- 3:02:33file is
- 3:02:34so i will assign path the name to my
- 3:02:37file
- 3:02:38test.txt now when i run this this will
- 3:02:40delete my file
- 3:02:42test.txt and you can see that it's no
- 3:02:44longer there
- 3:02:45now what if we attempt to delete a file
- 3:02:47that doesn't exist
- 3:02:49well we'll encounter an exception so if
- 3:02:51you want
- 3:02:52this is optional we can do some
- 3:02:53exception handling so let's write
- 3:02:56the remove function within a try block
- 3:02:58and we will catch
- 3:02:59this exception file not found error and
- 3:03:02let's print something
- 3:03:04print that file was not found
- 3:03:08now if you attempt to delete a file that
- 3:03:10doesn't exist
- 3:03:11your program will not be interrupted and
- 3:03:13instead you'll print something
- 3:03:15that file was not found now this
- 3:03:18function
- 3:03:18does not remove empty folders this is
- 3:03:21how to do so
- 3:03:22and actually let's test that so let's
- 3:03:23create a new directory
- 3:03:25i'll call this empty underscore folder
- 3:03:29now i will attempt to delete this empty
- 3:03:31folder
- 3:03:32empty folder
- 3:03:36we encountered a permission error access
- 3:03:38is denied so let's create an accept
- 3:03:40block for that
- 3:03:41accept permission error
- 3:03:44print i don't know you do
- 3:03:48not have permission to
- 3:03:51delete that right to delete
- 3:03:55an empty folder there is a different
- 3:03:57function
- 3:03:58os dot rmdir
- 3:04:02short for remove directory we're going
- 3:04:04to list the file path
- 3:04:05and pass that in as an argument and
- 3:04:07let's create an else block as well
- 3:04:09because we can
- 3:04:10else if there are no exceptions let's
- 3:04:12print
- 3:04:14path plus was
- 3:04:17deleted all right so rmdir
- 3:04:20remove directory will delete an empty
- 3:04:23directory
- 3:04:24empty folder was deleted now this
- 3:04:27function
- 3:04:27will not delete a folder that contains
- 3:04:30files and let's try that
- 3:04:31so let's create a new directory i'll
- 3:04:34just call this folder
- 3:04:36and within this folder i will create a
- 3:04:38new file
- 3:04:40test.txt
- 3:04:43all right let's attempt to delete this
- 3:04:45folder i will probably need to change
- 3:04:46the file name
- 3:04:48let's change that to folder and we
- 3:04:51encountered an
- 3:04:52os error that directory is not empty so
- 3:04:55let's handle this exception we don't
- 3:04:56need to but i like to
- 3:04:58accept os air print
- 3:05:03you cannot delete that
- 3:05:06using that function
- 3:05:10okay to delete a folder that contains
- 3:05:13files
- 3:05:13we'll need the shuttle module import
- 3:05:17shuttle and the function that we're
- 3:05:19looking for is
- 3:05:20shuttle dot rm tree
- 3:05:24short for remove tree and we will pass
- 3:05:26in our path
- 3:05:27now be careful with this function it is
- 3:05:29considered dangerous
- 3:05:30because it will delete a directory and
- 3:05:32all files contained within
- 3:05:34so now we can delete a folder that
- 3:05:36contains files
- 3:05:38folder was deleted in conclusion these
- 3:05:41are
- 3:05:41three basic functions to delete a file
- 3:05:44or directory they
- 3:05:45are remove which deletes a file rmdir
- 3:05:49which is an abbreviation for remove
- 3:05:52directory
- 3:05:52which will delete an empty directory and
- 3:05:55lastly
- 3:05:56rmtree which is an abbreviation for
- 3:05:58remove tree
- 3:05:59and this is of the shuttle module and
- 3:06:01this will delete a directory containing
- 3:06:04files
- 3:06:04so if you would like a copy of all this
- 3:06:06code i will post this in the comments
- 3:06:08section down below
- 3:06:10but yeah that is how to delete files and
- 3:06:12or directories
- 3:06:13using python yo what's going on
- 3:06:16everybody it's you bro hope you're doing
- 3:06:18well
- 3:06:18and in today's video i'm going to be
- 3:06:20discussing modules in python so
- 3:06:22sit back relax and enjoy the show
- 3:06:27all right well welcome back we're
- 3:06:29talking about modules today
- 3:06:31a module is a file containing python
- 3:06:33code
- 3:06:34it may contain functions classes etc
- 3:06:37it's used with modular programming which
- 3:06:39is this concept of separating a program
- 3:06:42into useful different parts we're
- 3:06:44currently working within our main module
- 3:06:46and mine is named hello youtube this is
- 3:06:48how to create a separate module
- 3:06:50right click on your project folder go to
- 3:06:52new python file
- 3:06:54i think i'll create a module filled with
- 3:06:56maybe message functions
- 3:06:58so i'll call this messages and click
- 3:07:00python file
- 3:07:01all right we have two tabs one for our
- 3:07:03main module mine is called hello youtube
- 3:07:05and a separate module
- 3:07:06for messages and i can write whatever i
- 3:07:09want within here since we're using
- 3:07:10modular programming i'll place a bunch
- 3:07:12of useful functions within this module
- 3:07:15and i'll import them to the main module
- 3:07:17that we're currently working with
- 3:07:18let's define a function called hello all
- 3:07:21this will do is print
- 3:07:24hello have a nice
- 3:07:27day and i'll create a second function
- 3:07:30for good measure
- 3:07:30let's call this pie
- 3:07:35let's say bye have
- 3:07:38a wonderful time
- 3:07:42now if we would like access to this
- 3:07:44module we need to import it
- 3:07:46i will head back to my main module and
- 3:07:47at the top write this
- 3:07:49import then the name of the module and
- 3:07:52for this example we used the name of
- 3:07:54messages
- 3:07:54for our separate module and to use a
- 3:07:57function from that module
- 3:07:58type the name of the module dot and then
- 3:08:01the name of the function
- 3:08:02so messages.hello will call the hello
- 3:08:05function
- 3:08:06found within the messages module and i
- 3:08:08can also call the
- 3:08:09buy function as well messages dot bye
- 3:08:13and that will print bye have a wonderful
- 3:08:16time
- 3:08:16no it can be somewhat tedious to have to
- 3:08:18write the name the module followed by
- 3:08:20the function name what we could do to
- 3:08:22shorten things up
- 3:08:22is to give our messages module an alias
- 3:08:25also known as a nickname
- 3:08:27so at the top write import messages
- 3:08:30as and what sort of nickname should we
- 3:08:32give messages
- 3:08:33let's say msg for short so we'll replace
- 3:08:37messages with
- 3:08:38msg msg hello and msg buy
- 3:08:41and this would work the same and it
- 3:08:42involves less typing there's also an
- 3:08:45additional way of
- 3:08:46importing a module and it goes a little
- 3:08:47something like this from
- 3:08:49the name of the module import and then
- 3:08:52list all of the functions or classes you
- 3:08:54would like to import
- 3:08:55i would like to import hello comma buy
- 3:08:58and we no longer need the module name
- 3:09:00before calling one of these functions
- 3:09:02i can simply just call hello and vi
- 3:09:04directly another way in which this could
- 3:09:06be written is
- 3:09:06from messages import asterisk that means
- 3:09:09to
- 3:09:10import all and i actually would not
- 3:09:11recommend using this if you're working
- 3:09:13on a large program or something that
- 3:09:15contains
- 3:09:16many modules because you can run into a
- 3:09:18naming conflict
- 3:09:19because some of these modules may have
- 3:09:21the same named function names or
- 3:09:22variables
- 3:09:24so if you're working on a small program
- 3:09:25i really don't think it's a big deal but
- 3:09:27if you have multiple modules i would
- 3:09:28actually refrain from using import alt
- 3:09:31it's much safer to write the name of the
- 3:09:33module followed by the name of the
- 3:09:35function that you want to use
- 3:09:36now another cool thing is that python
- 3:09:38has a bunch of pre-written modules that
- 3:09:40you have access to if you would like a
- 3:09:41comprehensive list of them type help
- 3:09:44and pass in the word modules and within
- 3:09:46my console window this will populate a
- 3:09:48listing of all the modules available to
- 3:09:50us
- 3:09:51here they are let's see if math is in
- 3:09:53here yep
- 3:09:54there it is so yeah feel free to take a
- 3:09:56look through some of these modules
- 3:09:58because there are some fairly useful
- 3:09:59tools within some of these modules
- 3:10:01i suppose you can also go to python's
- 3:10:03official documentation and search for
- 3:10:05the python module index and here's a
- 3:10:08listing of all the different modules too
- 3:10:09they have access to
- 3:10:10well everyone those are modules they are
- 3:10:13files containing python code and they
- 3:10:15may contain functions classes etc
- 3:10:17if you would like a copy of all this
- 3:10:18code i will post all of this to the
- 3:10:20comments section down below
- 3:10:22but yeah those are modules in python i
- 3:10:25guess there's a module called pickle
- 3:10:26hey what's going on everybody it's bro
- 3:10:28hope you're doing well
- 3:10:29and in this video we're going to be
- 3:10:31creating a basic game of rock paper
- 3:10:33scissors in python so
- 3:10:34sit back relax and enjoy the show
- 3:10:39let's begin by importing the random
- 3:10:42module
- 3:10:42because we're going to need the computer
- 3:10:44to pick a random choice and we'll create
- 3:10:46a
- 3:10:46list of possible choices so we have
- 3:10:49three strings within here
- 3:10:51rock paper and scissors
- 3:10:57i think i spelled scissors wrong there
- 3:11:00we go
- 3:11:01okay and we will have our computer pick
- 3:11:04a
- 3:11:04random dot choice from our list
- 3:11:08of choices and let's test it by printing
- 3:11:12whatever the computer picks
- 3:11:16so the computer picked paper paper
- 3:11:19scissors
- 3:11:20and scissors okay what about the player
- 3:11:23choice
- 3:11:23let's say player equals
- 3:11:27input rock
- 3:11:31paper or
- 3:11:34scissors
- 3:11:38and let's print what the player has
- 3:11:40print
- 3:11:41player and let's print the computer too
- 3:11:47maybe i'll add a string before these so
- 3:11:49let's say
- 3:11:50computer call in space
- 3:11:54whatever the computer chooses and same
- 3:11:57thing with the player
- 3:11:59player all right
- 3:12:02let's see if this works rock paper or
- 3:12:06scissors let's say rock
- 3:12:07the computer picks paper and i pick rock
- 3:12:10so i would technically lose this round
- 3:12:12now what if the player doesn't pick rock
- 3:12:14paper or scissors what if they type in
- 3:12:16gun so we would like some way to prevent
- 3:12:19that
- 3:12:19so let's put our input for our player
- 3:12:22within a while loop
- 3:12:24while player
- 3:12:27not in choices
- 3:12:32so we're going to need to initialize
- 3:12:34player because right now it's an
- 3:12:36unresolved reference
- 3:12:38so let's say player equals none
- 3:12:42for the time being okay now let's test
- 3:12:45our choices
- 3:12:46our player choices rock paper or
- 3:12:48scissors i pick
- 3:12:49the gun and it's going to keep on asking
- 3:12:52me until i pick
- 3:12:53something from my list of choices so
- 3:12:56if i type in that still won't accept it
- 3:12:58now let's type in
- 3:12:59rock again there we go now what if
- 3:13:02somebody types in one of these choices
- 3:13:04either all caps or
- 3:13:05capitalized well that technically
- 3:13:07wouldn't be in choices because
- 3:13:09these strings are case sensitive so why
- 3:13:12don't we take our user input and make it
- 3:13:14lowercase
- 3:13:14and i'll just add on dot lower and use
- 3:13:17the lower method
- 3:13:18so therefore we can now type in any
- 3:13:22input that is either all caps or
- 3:13:24uppercase
- 3:13:25and then that will still be accepted
- 3:13:27because we're going to take our player
- 3:13:28input
- 3:13:29and then make it lower case so that it
- 3:13:30matches one of these choices
- 3:13:33okay let's work on the win conditions
- 3:13:34next so first
- 3:13:36let's check to see if we encountered a
- 3:13:39tie
- 3:13:39if player is equal to computer that
- 3:13:42means that
- 3:13:43we both chose the same thing so let's
- 3:13:46print whatever the computer picked
- 3:13:48what we picked and we will print
- 3:13:52tie now let's write an else
- 3:13:56if statement e l i
- 3:13:59f player equals
- 3:14:03rock so if we pick rock and then we will
- 3:14:06create
- 3:14:07a nested if statement if computer
- 3:14:11equals paper
- 3:14:15that means we lose so let's print
- 3:14:18everything we have here except we're
- 3:14:20going to change
- 3:14:21tie to you
- 3:14:25lose now if the computer picks
- 3:14:29let's say scissors then we win
- 3:14:33so scissors and you
- 3:14:37win okay then let's just repeat this
- 3:14:40process
- 3:14:41for scissors and paper
- 3:14:45so this will be another else if
- 3:14:47statement
- 3:14:48if we pick
- 3:14:53scissors and the computer picks
- 3:14:56rock then we lose if the computer picks
- 3:15:00paper then you win
- 3:15:04and lastly we have paper as the player
- 3:15:07choice
- 3:15:12so else if player equals paper
- 3:15:16and the computer picks scissors then we
- 3:15:20lose
- 3:15:21if the computer picks rock well then
- 3:15:23paper covers rock
- 3:15:25and we win now let's test it
- 3:15:29rock paper or scissors again i'll try
- 3:15:32and pick the gun
- 3:15:33which i can't let's take this seriously
- 3:15:35i will pick rock
- 3:15:36the computer picked rock so it's a tie
- 3:15:40i'm feeling like paper today oh it's
- 3:15:43another tie i guess
- 3:15:44and how about scissors okay
- 3:15:48so i win this round i picked uh scissors
- 3:15:50and the computer picked paper
- 3:15:51therefore i win let's try and lose this
- 3:15:54time
- 3:15:55just to verify it so i won again let's
- 3:15:58try rock again
- 3:16:00okay so i lost that round so we know
- 3:16:02that the win conditions are working now
- 3:16:04how about we add a feature where we will
- 3:16:06ask the player if they would like to
- 3:16:08play again
- 3:16:08play another round i think the best way
- 3:16:10to do that would be to write
- 3:16:12all of this code within a while loop so
- 3:16:14at the top
- 3:16:15let's write while true and we'll need to
- 3:16:18indent everything so that it's within
- 3:16:20the while loop so let's just indent
- 3:16:22everything here
- 3:16:39now at the bottom we will ask the user
- 3:16:41if they would like to play again and i
- 3:16:43will store this within a variable
- 3:16:45named play again play again equals input
- 3:16:49and the prompt will be play again
- 3:16:54yes slash no and depending on their
- 3:16:58input
- 3:16:58let's just make it all lower case
- 3:17:02if play again
- 3:17:06does not equal yes
- 3:17:09that means they would like to quit so
- 3:17:11let's use a break statement to break out
- 3:17:14of our while loop that we're in
- 3:17:17and at the end let's print by
- 3:17:20and pay attention to the indentation
- 3:17:23this input and
- 3:17:24this if statement should be within the
- 3:17:25while loop so they should have at least
- 3:17:27one indentation and our print statement
- 3:17:30here we'll just say bye
- 3:17:32and you can see that there is no
- 3:17:33indentation so that is outside of the
- 3:17:35while loop
- 3:17:36okay let's test this one last time
- 3:17:40rock paper scissors i'm going to pick
- 3:17:42the
- 3:17:43cannon okay that's not valid input how
- 3:17:46about rock
- 3:17:47okay the computer picks rock i pick rock
- 3:17:49so it's a tie do i want to play again
- 3:17:52i'm going to select actually yes all
- 3:17:54caps
- 3:17:55that should still be acceptable elbow
- 3:17:57paper this time
- 3:17:59it's a tie again man the computer is
- 3:18:01really good at this game
- 3:18:02i'd like to play again i'll pick
- 3:18:04scissors
- 3:18:06okay so i lose i would like to play
- 3:18:08again and i want to win this time
- 3:18:11okay computer pick scissors i pick rock
- 3:18:14you win do you want to play again
- 3:18:16nah and it will print
- 3:18:19bye well everybody that's a simple game
- 3:18:22of rock paper scissors i'll post all of
- 3:18:24this code to the comment section down
- 3:18:26below
- 3:18:26and well yeah that's a basic game of
- 3:18:29rock paper scissors in python
- 3:18:32hey what's going on everybody it's you
- 3:18:34bro hope you're doing well
- 3:18:36and in this video i'm going to show you
- 3:18:37guys how we can build a basic quiz game
- 3:18:39in python
- 3:18:40so sit back relax and enjoy the show
- 3:18:45to begin this project i think it would
- 3:18:47be wise for us to create a skeletal
- 3:18:48structure for how this program is going
- 3:18:50to operate
- 3:18:51and then we'll fill in the gaps later as
- 3:18:52we go along so to begin
- 3:18:54let's define all of the functions that
- 3:18:56we'll need we'll need four
- 3:18:58we'll create a function that is named
- 3:19:00new game
- 3:19:01whenever we call this function it will
- 3:19:02create a new quiz game for us
- 3:19:05and for the time being let's write pass
- 3:19:07just as a placeholder
- 3:19:08we'll have a total of four functions new
- 3:19:10game
- 3:19:12a function to check our answer
- 3:19:16a function to display our score
- 3:19:21and lastly a function to
- 3:19:24play again
- 3:19:30all right now i'm just going to add some
- 3:19:33comments here
- 3:19:34just to separate each of these functions
- 3:19:36that we have just so that it's a little
- 3:19:37bit easier to read
- 3:19:38although this part is not really
- 3:19:40necessary so i just want to separate
- 3:19:42each of these functions for some
- 3:19:44readability
- 3:19:47okay those are all the functions that
- 3:19:49we'll need what we'll need now
- 3:19:51is some sort of collection to hold all
- 3:19:53of the questions and answers that we
- 3:19:55have and i think a dictionary would be
- 3:19:57perfect for this
- 3:19:58so what i did is that i created a
- 3:20:00dictionary named questions
- 3:20:02a dictionary has key value pairs
- 3:20:05each key is a question that i would like
- 3:20:07to ask and each question
- 3:20:08has an associated value we'll have the
- 3:20:11user guess between
- 3:20:12answers of a b c or d so these would be
- 3:20:16all of the correct answers all of the
- 3:20:17values within this dictionary
- 3:20:19so here are some of the questions that i
- 3:20:20want to ask feel free to come up with
- 3:20:22your own questions
- 3:20:23if you would like you can copy the
- 3:20:25questions that i have they should be
- 3:20:26posted in the comment section
- 3:20:28down below so the questions that i'm
- 3:20:30going to ask are
- 3:20:31who created python what year was python
- 3:20:33created
- 3:20:34python is attributed to which comedy
- 3:20:36group and lastly is the earth round
- 3:20:38i couldn't think of a fourth question so
- 3:20:40i just threw in a random stupid question
- 3:20:43and the correct answers to all of these
- 3:20:45questions in order would be
- 3:20:46a b c and a but feel free to come up
- 3:20:50with your own
- 3:20:51well we have our questions but we'll
- 3:20:53need some sort of collection to hold all
- 3:20:55of the different possible answers
- 3:20:57to each of these questions and i think a
- 3:20:592d list would work perfect for this
- 3:21:02so i have a list of lists a list of
- 3:21:05tuples could work too i suppose
- 3:21:07so here are all of the answers for the
- 3:21:09first question
- 3:21:10and the correct answer is a i believe
- 3:21:13his name is
- 3:21:14guido van rossum hey if you're listening
- 3:21:16man i'm sorry if i'm mispronouncing your
- 3:21:18name i'm really trying here
- 3:21:20the second list corresponds to the
- 3:21:21second question
- 3:21:23what year was python created it was
- 3:21:24created in 1991
- 3:21:26at least according to wikipedia python
- 3:21:29is attributed to which comedy group
- 3:21:31the answer is c monty python like monty
- 3:21:34python and the holy grail
- 3:21:35good movie by the way and lastly is the
- 3:21:38earth round
- 3:21:39this is a highly debated topic but the
- 3:21:41answer is hey
- 3:21:42the earth is in fact round so i have a
- 3:21:45list of lists
- 3:21:47each list corresponds to a key value
- 3:21:49pair within my dictionary of questions
- 3:21:52and this is a lot to type if you want
- 3:21:54you can just copy the code that i posted
- 3:21:56in the comments down below
- 3:21:57just copy and paste and you'll have all
- 3:21:59of this or if you want you can pause the
- 3:22:01video and type it
- 3:22:02i don't care so now that you have your
- 3:22:04questions and your options we're ready
- 3:22:05to begin
- 3:22:06so the first thing that we'll do within
- 3:22:07our program is to call the
- 3:22:09new game function to begin a new game so
- 3:22:12after
- 3:22:12we have our questions and options
- 3:22:14created let's create a
- 3:22:16new game by calling the new game
- 3:22:19function
- 3:22:20now when we run this program we'll
- 3:22:22generate our dictionary of questions
- 3:22:24and our 2d list of different options for
- 3:22:27each question
- 3:22:28and then we will call the new game
- 3:22:29function to begin a new game
- 3:22:31so let's head to the new game function
- 3:22:33and fill in everything within our new
- 3:22:35game function
- 3:22:36so at the top let's declare a few things
- 3:22:39let's say we have
- 3:22:40a list named guesses and guesses will be
- 3:22:43an empty list for now
- 3:22:45i will declare a variable called correct
- 3:22:48guesses set it equal to zero because we
- 3:22:51haven't guessed anything yet
- 3:22:52and we will set a current question
- 3:22:55number
- 3:22:56and set the sequel to one to represent
- 3:22:58the first question
- 3:23:00okay now we need to display all of the
- 3:23:03questions within our dictionary of
- 3:23:05questions
- 3:23:06and we can use a for loop for that so
- 3:23:09for
- 3:23:10key in questions
- 3:23:14i'm going to print my key
- 3:23:20and let's just test this okay yeah
- 3:23:23here's all my questions
- 3:23:25i think what i'll do is actually print
- 3:23:28something to separate each question
- 3:23:30let's say one of these lines i think it
- 3:23:34would look better with them
- 3:23:36okay i'll print this line and then we'll
- 3:23:38move on to a question
- 3:23:40all right now after re-running this we
- 3:23:42have each question and i added just a
- 3:23:44line break between each of these to make
- 3:23:46it look kind of fancy
- 3:23:47now i need to display all of the
- 3:23:49different options for each question
- 3:23:51and we can do that with a nested for
- 3:23:54loop
- 3:23:55so if i were to write i in
- 3:23:59options print i
- 3:24:02let me show you what happens now with
- 3:24:05the way this for loop is written now
- 3:24:07it's going to display
- 3:24:08all of the different options available
- 3:24:10to us for the entire quiz game
- 3:24:12what i would like instead is to display
- 3:24:14only the first
- 3:24:16list for the first question and then the
- 3:24:18second list
- 3:24:19for the second question and follow that
- 3:24:21pattern so we're going to change our for
- 3:24:23loop around
- 3:24:244i in options and we'll set an index for
- 3:24:27options
- 3:24:28the index is going to be our question
- 3:24:30number
- 3:24:31minus one now remember what i said in
- 3:24:34previous videos where different
- 3:24:36collections including
- 3:24:37lists tuples etc the first element in a
- 3:24:40collection
- 3:24:41has an index of zero because computers
- 3:24:43always start with zero
- 3:24:44then the next element would therefore
- 3:24:46have an index of one
- 3:24:48then two three then you follow that
- 3:24:50pattern we're using this question number
- 3:24:52variable
- 3:24:53as some sort of counter and since we
- 3:24:55initially set this to one
- 3:24:57i'm just going to subtract one so that
- 3:24:59we effectively
- 3:25:00receive zero as the index and now we
- 3:25:03just need to increment our question
- 3:25:04number
- 3:25:05after each iteration so let's do that
- 3:25:08i will add that to the end here after we
- 3:25:11finish displaying all of the options
- 3:25:13let's increment question number by one
- 3:25:16by typing question num plus equals one
- 3:25:20and if i were to run this currently
- 3:25:23we'll display all of the different
- 3:25:24options for each question
- 3:25:26who created python here's all of the
- 3:25:28associated options
- 3:25:30what year is python created python is
- 3:25:32attributed to which comedy group
- 3:25:34and lastly is the earth round now it's
- 3:25:37time for some user input
- 3:25:38so i'm going to create a variable called
- 3:25:41guess
- 3:25:42and make sure you don't put it within
- 3:25:44your inner for loop it should be within
- 3:25:46the outer for loop
- 3:25:48guess equals input and i'll create a
- 3:25:51prompt
- 3:25:53enter a b
- 3:25:56c or d
- 3:26:00here's something to consider we would
- 3:26:02like the user to type in either capital
- 3:26:04a
- 3:26:04b c or d with strings they're case
- 3:26:07sensitive what if the user typed in
- 3:26:09one of these letters but lowercase if
- 3:26:12they're correct we would still like to
- 3:26:13give them their point
- 3:26:14why don't we take our guess and make it
- 3:26:16uppercase
- 3:26:17so guess equals guess and we can make
- 3:26:20this uppercase
- 3:26:21using the upper method of strings at the
- 3:26:24end of this game i'm going to compare
- 3:26:26our guesses to the correct answers so we
- 3:26:29have an empty
- 3:26:30list named guesses and i'm going to
- 3:26:33append our current guest that we're
- 3:26:34working on
- 3:26:35to our list of guesses so guesses
- 3:26:39dot append and we will append our
- 3:26:42guess this round now that we have our
- 3:26:45guess
- 3:26:46and we've appended our guess to our list
- 3:26:48of guesses
- 3:26:49let's check to see if it's the correct
- 3:26:51answer or not so we're just about to
- 3:26:53fill in
- 3:26:53this check answer function next
- 3:26:57so we are going to call this function
- 3:26:59and pass in a few items as arguments
- 3:27:02so let's use the check answer function
- 3:27:07and we'll pass in the key for the
- 3:27:10current question that we're on
- 3:27:12so the key is the correct answer so that
- 3:27:14would be
- 3:27:15our questions
- 3:27:18dot get key
- 3:27:23this would be the answer and we'll also
- 3:27:25pass in
- 3:27:26our guess and we are going to fill in
- 3:27:30the check answer function but we need to
- 3:27:32set up the parameters
- 3:27:34we're receiving effectively our answer
- 3:27:37as well as our guess so i'm going to
- 3:27:41name these as
- 3:27:42answer and guess and we are going to
- 3:27:45check to see if
- 3:27:46our answer is equal to our guess
- 3:27:50if answer is equal
- 3:27:53to guess let's print something
- 3:27:57print correct
- 3:28:03and i think we should give the user a
- 3:28:04point we'll have our check answer
- 3:28:06function return
- 3:28:08one for one point return one
- 3:28:12else if this is not the correct answer
- 3:28:15let's print
- 3:28:17wrong and we will return
- 3:28:20zero they do not get a point
- 3:28:24and since this is returning a value we
- 3:28:26should assign that
- 3:28:27so let's assign the point we may or may
- 3:28:30not receive
- 3:28:31to our variable of correct guesses which
- 3:28:33is initially set to zero
- 3:28:35so we'll type correct guesses plus
- 3:28:38equals check answer and check answer
- 3:28:41will return
- 3:28:42one if we scored a point or zero if we
- 3:28:44did not score a point and make sure you
- 3:28:46have
- 3:28:47plus equals because if you just set this
- 3:28:49to equals
- 3:28:50then we cannot score more than one point
- 3:28:52it will either be
- 3:28:53zero or one so we are effectively
- 3:28:56incrementing our correct
- 3:28:57guesses by one for each point that we
- 3:29:00score
- 3:29:01and now we're going to work on the
- 3:29:03display score
- 3:29:04function and we'll call that at the end
- 3:29:06after we finish all of our questions
- 3:29:08that we have
- 3:29:09so make sure that you do not write this
- 3:29:10within the for loop it should be outside
- 3:29:13of it
- 3:29:13because once we finish iterating through
- 3:29:16all of our questions we're going to
- 3:29:17display a final score
- 3:29:19so let's call the display score
- 3:29:22function and we'll need to pass in some
- 3:29:25arguments
- 3:29:26our correct guesses as well
- 3:29:29as our list of guesses
- 3:29:33okay let's head to the display score
- 3:29:36function
- 3:29:37so we have as parameters
- 3:29:40correct guesses as well as
- 3:29:45guesses i need to remove this pass i'm
- 3:29:48going to add
- 3:29:49one of these fancy lines in just to
- 3:29:51separate the questions
- 3:29:52from the results and i will print
- 3:29:56results
- 3:29:59and maybe another one of these lines
- 3:30:02okay
- 3:30:02so i need to print all of the answers
- 3:30:08answers
- 3:30:11and i do not want to end on a new line
- 3:30:14so i'm going to set
- 3:30:15end equal to nothing and i need to
- 3:30:19display all of the values within
- 3:30:22our dictionary all of the answers i will
- 3:30:25do that using
- 3:30:26a for loop for i
- 3:30:29in questions
- 3:30:33print questions
- 3:30:37dot get i
- 3:30:41and i do not want to end on a new line
- 3:30:44so i'm going to set
- 3:30:45end equal to nothing effectively
- 3:30:48and then i'll print a new line okay
- 3:30:50let's work on the guesses i think i'm
- 3:30:52just going to copy all this
- 3:30:53and make some changes i'll replace
- 3:30:56answers
- 3:30:57with guesses for
- 3:31:00i in guesses
- 3:31:04print i
- 3:31:07okay let's just be sure that
- 3:31:09everything's working we're not
- 3:31:10calculating a score
- 3:31:12quite yet so i'm just going to answer a
- 3:31:16then b c and d
- 3:31:19on second thought after printing each of
- 3:31:22these answers and guesses
- 3:31:23i'm going to add a space after each of
- 3:31:25these so
- 3:31:26within each of these four loops at the
- 3:31:28end i will add a space
- 3:31:30let me try that again a b
- 3:31:33c d okay not too bad
- 3:31:37now let's calculate the final score and
- 3:31:39we're still within our display score
- 3:31:41function
- 3:31:43let's set score equal to
- 3:31:46correct
- 3:31:49guesses divided by
- 3:31:53the length of our questions
- 3:31:58and let me add a set of parentheses
- 3:32:00around here
- 3:32:03then i'm going to multiply our score by
- 3:32:06100
- 3:32:08and if you don't want a decimal portion
- 3:32:12because we'll display a percentage we
- 3:32:14can cast this as an end
- 3:32:16okay that all looks good and we'll print
- 3:32:19the final score
- 3:32:21print your score
- 3:32:25is plus
- 3:32:29we need to cast our score to a string
- 3:32:31because we're using string concatenation
- 3:32:34plus i'll add a percent sign
- 3:32:38all right let's try it i'm intentionally
- 3:32:40going to get the last question wrong
- 3:32:42so we should have a 75 if we have four
- 3:32:45questions
- 3:32:46so that would be a b c
- 3:32:50is the earth round what's earth i'm
- 3:32:52gonna guess d
- 3:32:54answers a b c a your guess is a b c
- 3:32:57d your score is 75 okay let's test it
- 3:33:01but getting all wrong answers d d d
- 3:33:04d your score is zero percent
- 3:33:07congratulations
- 3:33:08and we'll test it one more time but get
- 3:33:10all the right answers
- 3:33:12we just want to be sure that we have 100
- 3:33:14that's a
- 3:33:15b c a your score is 100
- 3:33:20now the last thing that you can add is
- 3:33:22that we can play again if we want
- 3:33:24and i have a separate function for that
- 3:33:26if you would like to add this option
- 3:33:27here's how to do so
- 3:33:28let's remove that pass
- 3:33:32let's set a variable named response
- 3:33:34equal to
- 3:33:36some input do you
- 3:33:40want to play again
- 3:33:45let's ask for yes or no
- 3:33:50actually let me remove that okay
- 3:33:55and depending on the user's response it
- 3:33:56might be a lowercase so let's make it
- 3:33:58uppercase
- 3:33:59response equals response
- 3:34:02and use the upper method
- 3:34:07if response is equal to
- 3:34:10yes all caps if response is equal to yes
- 3:34:14then return true
- 3:34:18else we will return
- 3:34:22false and that is it for this function
- 3:34:25the last thing we need to do
- 3:34:26is to create a while loop that will
- 3:34:28continue to ask the player if they want
- 3:34:30to play again
- 3:34:31this will be after we call the new game
- 3:34:33function
- 3:34:34while play again
- 3:34:38and remember this will return either
- 3:34:40true or
- 3:34:41false depending on the user's response
- 3:34:44while
- 3:34:44play again new game
- 3:34:48and call the new game function to create
- 3:34:50a new game for us
- 3:34:52if we escape the while loop that means
- 3:34:54the user doesn't want to play again
- 3:34:56so let's print the message
- 3:34:59such as by e
- 3:35:02all right let's test it i'm going to get
- 3:35:06all of the correct answers this time a b
- 3:35:10c a all right your score is 100
- 3:35:14do you want to play again i'm going to
- 3:35:16type
- 3:35:17yes all right then we can play again
- 3:35:22d d d d your score is zero percent do
- 3:35:26you want to play again
- 3:35:27no i think i mastered this game
- 3:35:30bye all right everybody so that's how
- 3:35:33you can use python to create a simple
- 3:35:36quiz game
- 3:35:37i will post all of this code to the
- 3:35:38comment section down below
- 3:35:40but well yeah that's a basic quiz game
- 3:35:43in python for you all
- 3:35:46hey what's going on everybody it's bro
- 3:35:48hope you're doing well
- 3:35:49and in this video i'm going to explain
- 3:35:51object-oriented programming in python so
- 3:35:53sit back relax and enjoy the show
- 3:35:58welcome to today's topic on python
- 3:36:01object oriented programming
- 3:36:03also known as pop and in today's video
- 3:36:06we're going to be creating objects
- 3:36:07an object is an instance of a class by
- 3:36:10using programming we can create
- 3:36:12representations of real-life objects so
- 3:36:15look around you wherever you're sitting
- 3:36:16or standing right now you are surrounded
- 3:36:19by objects next to me i have a phone
- 3:36:21a television some snacks and i'm talking
- 3:36:24into a microphone
- 3:36:25point being we can use programming to
- 3:36:27mimic real world objects by assigning a
- 3:36:30combination of attributes
- 3:36:32what an object is or has and methods
- 3:36:35what an object can do now in order to
- 3:36:37create an object we'll need to create a
- 3:36:39class
- 3:36:40a class can function as a blueprint that
- 3:36:41will describe what attributes and
- 3:36:43methods
- 3:36:44that our distinct type of object will
- 3:36:46have you can either create your class
- 3:36:48within your main module
- 3:36:49or you could create a separate file
- 3:36:51dedicated solely for your class
- 3:36:53now to create a class we would type
- 3:36:55class and then the name of the object
- 3:36:57that we would like to create so today i
- 3:36:58would like to create some car objects
- 3:37:00because i like cars
- 3:37:02so we would type car and a common naming
- 3:37:04convention
- 3:37:05with class names is that they should be
- 3:37:07capital so car is going to have a
- 3:37:09capital c
- 3:37:10and for the time being we need to type
- 3:37:11in something i'll just type in pass as a
- 3:37:13placeholder
- 3:37:14so if you have a small program it may be
- 3:37:16better to write your class within your
- 3:37:18main
- 3:37:19module but if your class is fairly large
- 3:37:22you may want to consider placing your
- 3:37:24class within a separate module
- 3:37:26so if you were to take that route we
- 3:37:28would go to file
- 3:37:29new python file and we would name this
- 3:37:32car
- 3:37:32click python file and we would declare
- 3:37:35our class within
- 3:37:36the separate module so class car and for
- 3:37:39now i'll type in pass
- 3:37:40then we just need to import this class
- 3:37:42so within our main module we would type
- 3:37:44from the name of the module import the
- 3:37:47name of the class
- 3:37:48from car import car now objects can have
- 3:37:51some combination of attributes and
- 3:37:53methods attributes describe what an
- 3:37:55object is or has
- 3:37:56so what are a few attributes that cars
- 3:37:58might have they might have a make so
- 3:38:00let's create a few variables
- 3:38:02for the time being i'm just going to use
- 3:38:04none as a placeholder
- 3:38:06cars can have a model a year
- 3:38:10and a color
- 3:38:14now objects can also have methods what
- 3:38:16kinds of methods could cars perform
- 3:38:18perhaps a drive method and a stop method
- 3:38:20so let's define those
- 3:38:22def let's say drive and we'll have one
- 3:38:25argument
- 3:38:26self self refers to the object that is
- 3:38:28using this method
- 3:38:30now what do we want to do when we call
- 3:38:31this method
- 3:38:33let's say this car
- 3:38:36is driving let's create a stop method as
- 3:38:39well
- 3:38:40def stop
- 3:38:43and we will print this
- 3:38:46car is stopped we now have all of the
- 3:38:49different attributes and methods we
- 3:38:51would like our car objects to have but
- 3:38:53there is one more thing that we need
- 3:38:54it is a special method called the init
- 3:38:57method that will construct
- 3:38:58objects for us in other programming
- 3:39:00languages this is known as the
- 3:39:02constructor
- 3:39:03so we need a special method that will
- 3:39:05create objects for us
- 3:39:06so we need to define this method def and
- 3:39:08the syntax on this is somewhat strange
- 3:39:10it is
- 3:39:11two underscores init it's short for
- 3:39:14initialize
- 3:39:14200 scores again and then we need at
- 3:39:16least self as an argument
- 3:39:18and then we can actually assign our car
- 3:39:21objects unique variables so take all of
- 3:39:24these attributes that we have
- 3:39:26and we're going to place them within
- 3:39:27this init method now we can receive
- 3:39:30arguments
- 3:39:30when we create car objects but we need
- 3:39:33to pass them in
- 3:39:34as arguments to our init method so we
- 3:39:36need to set up some parameters
- 3:39:38let's say in order to create a car
- 3:39:39object we need a make
- 3:39:42a model a year and a color
- 3:39:45then when we receive these arguments we
- 3:39:47can actually assign them to
- 3:39:49each car's specific attributes but we
- 3:39:52need to precede each of these with self
- 3:39:54self is referring to the object that
- 3:39:56we're currently working on
- 3:39:57or creating so self.make self.model
- 3:40:01self.ear and self.color
- 3:40:05and then when we assign these we're
- 3:40:07going to say self.make
- 3:40:08equals whatever make that we receive
- 3:40:11when it's passed in as an argument
- 3:40:12self.model equals model and continue
- 3:40:15that pattern for year
- 3:40:17and color all right that is all we need
- 3:40:20for our class car we have a constructor
- 3:40:23we're assigning
- 3:40:24arguments that we receive to the
- 3:40:26attributes of our car object
- 3:40:28and we also have two methods one for
- 3:40:30drive and one for stop
- 3:40:32now we can start creating some car
- 3:40:33objects so going back to my main file
- 3:40:36here
- 3:40:36to create an object we need a unique
- 3:40:38name for it let's call this first car
- 3:40:40just car one
- 3:40:41equals the name of the class car then a
- 3:40:44set of parentheses
- 3:40:46now in order to construct a car object
- 3:40:48we need to pass in a matching
- 3:40:50set of arguments you can see here that
- 3:40:52we need to pass in a make a model a year
- 3:40:54and a color
- 3:40:55so what kind of car should we make let's
- 3:40:57say that the make is going to be
- 3:40:59chevy we need a model
- 3:41:03perhaps corvette a year
- 3:41:072021 is good and a color let's say blue
- 3:41:10alright now let's actually access some
- 3:41:13of these cars attributes
- 3:41:15and i should probably put this one in a
- 3:41:16print statement so print car ones
- 3:41:19make and this should print chevy
- 3:41:23then we can do the same thing for model
- 3:41:25year and color
- 3:41:26so let's change some of these around
- 3:41:29model
- 3:41:30year and color
- 3:41:35yep this object is a chevy corvette 2021
- 3:41:38and the color is blue
- 3:41:39and we should have two methods as well
- 3:41:41so car one
- 3:41:42i would like this car to use its drive
- 3:41:44method
- 3:41:46this car is driving and let's have this
- 3:41:48car use it stop method
- 3:41:50car one dot stop
- 3:41:55this car is stopped there's one thing
- 3:41:56that i should mention real quick because
- 3:41:58i forgot to mention it earlier
- 3:42:00so within our init method you can see
- 3:42:02that we need five
- 3:42:03arguments in order to construct a car
- 3:42:05object self
- 3:42:06make model year in color but when we
- 3:42:09pass in
- 3:42:10our arguments we're not passing in
- 3:42:12anything for self we only have
- 3:42:13four arguments here that's because with
- 3:42:16python we do not need to pass itself
- 3:42:18that's done automatically for us we're
- 3:42:20referring to the object that we're
- 3:42:22dealing with
- 3:42:23as you can see with our drive and stop
- 3:42:25method we need
- 3:42:26to pass in self in order to execute our
- 3:42:29method
- 3:42:30but when we call this method we do not
- 3:42:31need to do so so just remember with
- 3:42:34python with self we do not need to pass
- 3:42:36in anything for this argument that is
- 3:42:38all
- 3:42:38now the nice thing about this is that we
- 3:42:40can reuse this class
- 3:42:42as a blueprint to create more car
- 3:42:43objects we just call that
- 3:42:45init method that constructor so this
- 3:42:47time let's create a second car called
- 3:42:49car
- 3:42:492 and this will be a different kind of
- 3:42:51car
- 3:42:53what kinds of arguments should we pass
- 3:42:55it let's say this is a ford
- 3:42:58mustang the year will be 2022 and the
- 3:43:01color will be red
- 3:43:03all right let's check card to use
- 3:43:05attributes
- 3:43:08and let's have car 2 use its drive and
- 3:43:10stop method
- 3:43:12this is a ford mustang the year is 2022
- 3:43:15the color is red
- 3:43:15this car is driving and this car is
- 3:43:17stopped one other thing that we can do
- 3:43:19too
- 3:43:20is that within our methods here let's
- 3:43:22replace car with
- 3:43:23the name of the model that we're working
- 3:43:25with so this
- 3:43:27plus self dot model
- 3:43:31plus is driving now this self keyword
- 3:43:34think of it as you're replacing self
- 3:43:37with the name of the object that we're
- 3:43:39working on if car one
- 3:43:41is using its drive method replace itself
- 3:43:44with car one
- 3:43:45if this is car two that called this
- 3:43:47method then replace itself with car 2.
- 3:43:50think of it that way and let's do the
- 3:43:51same thing for stop
- 3:43:54this plus self dot model
- 3:43:58plus is stopped all right
- 3:44:01probably don't need these anymore so
- 3:44:03let's have car one
- 3:44:04use its drive and stop method this
- 3:44:07corvette is driving this corvette is
- 3:44:09stopped
- 3:44:09let's try this with car two this mustang
- 3:44:13is driving
- 3:44:13this mustang is stopped now you can have
- 3:44:16them do things independently too let's
- 3:44:18have car 1 use its drive method and car
- 3:44:202 uses stop method
- 3:44:22this corvette is driving and this
- 3:44:24mustang is stopped in conclusion
- 3:44:26a class can function as a blueprint to
- 3:44:28create objects
- 3:44:29we can assign attributes what describe
- 3:44:32an object is or
- 3:44:33has and methods what each object
- 3:44:36can do and then within our class we have
- 3:44:38a special method called the init method
- 3:44:41we can pass in some arguments and assign
- 3:44:44these arguments to each
- 3:44:45object's attributes and then we can
- 3:44:47reuse this class as if it was a
- 3:44:49blueprint
- 3:44:50so we can create more objects out of it
- 3:44:53so
- 3:44:53that is the basics of object oriented
- 3:44:56programming in python if you would like
- 3:44:58a copy of all this code i will post this
- 3:45:00in the comment section down below
- 3:45:01but yeah that's the basics of object
- 3:45:04oriented programming
- 3:45:05in python hey what's going on everybody
- 3:45:08it's bro hope you're doing well and in
- 3:45:10this video i'm going to explain
- 3:45:12the basics of class variables in python
- 3:45:14so sit back
- 3:45:15relax and enjoy the show
- 3:45:19here's a super quick video on the
- 3:45:21differences between class and instance
- 3:45:23variables
- 3:45:24for this example let's say we have two
- 3:45:26car objects
- 3:45:27named car one and car two and i'm just
- 3:45:29passing in a bunch of values to each of
- 3:45:31these car constructors
- 3:45:33now within my class car i have my init
- 3:45:36method
- 3:45:36also known as the constructor and what
- 3:45:38i'm doing is assigning all of these
- 3:45:40values
- 3:45:40to the variables found within my car
- 3:45:43constructor
- 3:45:44these variables declared inside the
- 3:45:46constructor
- 3:45:47are known as instance variables and each
- 3:45:50object
- 3:45:50can have their own unique values
- 3:45:53assigned to each
- 3:45:54of these variables now where a class
- 3:45:56variable is different
- 3:45:57is that a class variable is declared
- 3:46:00inside the class but outside of the
- 3:46:02constructor and what we can do
- 3:46:04is set some default values for some
- 3:46:06variables
- 3:46:07let's say we would like to add an
- 3:46:08additional variable let's say wheels
- 3:46:10wheels equals and we can set a default
- 3:46:13value
- 3:46:14for all instances of this class for each
- 3:46:16car object that we create
- 3:46:18so we set wheels to equal four this is
- 3:46:20an example of a class
- 3:46:22variable whenever we create a car object
- 3:46:25we can pass in a unique
- 3:46:26make model year in color but by default
- 3:46:29they will all have
- 3:46:30four wheels now if i was to print the
- 3:46:33amount of wheels
- 3:46:34that car 1 and car 2 have they will both
- 3:46:37have
- 3:46:38four wheels it's as if we're setting a
- 3:46:40default value
- 3:46:41if i change this to two wheels well then
- 3:46:43these cars have two wheels
- 3:46:45maybe their motorcycles or something now
- 3:46:47you can actually
- 3:46:48change these values each object will
- 3:46:50have their own
- 3:46:51copy of this variable but you can set it
- 3:46:53to a default value
- 3:46:54so let's say car one
- 3:46:58is a motorcycle car one dot wheels
- 3:47:01equals two so if i were to print car
- 3:47:04one's wheels along with card two
- 3:47:06car one will have two wheels but car two
- 3:47:09is still using that default amount of
- 3:47:11wheels which is four
- 3:47:12now there's another way in which you can
- 3:47:14access a class variable
- 3:47:16you don't necessarily need to create an
- 3:47:18object to do so you can just use the
- 3:47:19name of the class
- 3:47:20so what i'm going to do is turn all of
- 3:47:23these lines into comments
- 3:47:25and i will print the amount of wheels
- 3:47:28that our class has so type in the name
- 3:47:31of the class car with a capital c
- 3:47:33it should be the same name as your class
- 3:47:35name followed by the name of the class
- 3:47:37variable
- 3:47:38car dot wheels but make sure you spell
- 3:47:41it right
- 3:47:42all right so this will display four
- 3:47:44wheels
- 3:47:45now what if we decide to change the
- 3:47:47class variable
- 3:47:48through our class well that will affect
- 3:47:51all
- 3:47:51instances of our class so let's say car
- 3:47:55dot wheels equals two it's as if we
- 3:47:59change our mind and we have decided to
- 3:48:01create motorcycles instead of cars
- 3:48:03well if we were to change the wheels
- 3:48:06class variable through the use of our
- 3:48:08car class
- 3:48:09well then it's going to affect it for
- 3:48:11all instances of this class
- 3:48:13you can see that both car 1 and car 2
- 3:48:15now have two wheels
- 3:48:16when we just changed car 1's wheels it
- 3:48:19did not affect
- 3:48:20car 2 at all so that's the main
- 3:48:22difference between a class variable and
- 3:48:24an instance variable
- 3:48:25an instance variable is declared inside
- 3:48:27of constructor
- 3:48:28and they can be given unique values with
- 3:48:31class variables
- 3:48:32they are declared within a class but
- 3:48:34outside of the constructor
- 3:48:35and you can set a default value for all
- 3:48:38instances of this class
- 3:48:40for all unique objects that are created
- 3:48:42and then you can change those values
- 3:48:43later too
- 3:48:44so if you would like a copy of all this
- 3:48:46code i will post all of this in the
- 3:48:48comment section down below
- 3:48:49but yeah that is the basics of class
- 3:48:51variables
- 3:48:52in python hey what's going on everybody
- 3:48:56it's you bro hope you're doing well and
- 3:48:57in this video i'm going to explain
- 3:48:59inheritance in python so
- 3:49:01sit back relax and enjoy the show
- 3:49:06let's talk about irritants the term
- 3:49:08inherit means to receive derive or
- 3:49:10be left with and we can apply this
- 3:49:11concept to programming classes can
- 3:49:13inherit something
- 3:49:14usually attributes and methods from
- 3:49:16another class these classes can form
- 3:49:18parent-child relationships where a child
- 3:49:21will receive everything
- 3:49:22that the parent class has much like you
- 3:49:24inherit jeans from your parents
- 3:49:26and classes can have children and give
- 3:49:28whatever they own to their children
- 3:49:30and in today's video we'll be creating a
- 3:49:32parent class called animal
- 3:49:33and children of the animal class will
- 3:49:35inherit the common attributes and
- 3:49:37methods
- 3:49:37that all animals might have to kick off
- 3:49:39this video i think what we'll do
- 3:49:41is keep all of our classes within the
- 3:49:42same file just so that it's easier for
- 3:49:44us to read and understand
- 3:49:45let's begin by creating a class called
- 3:49:47animal so at the top
- 3:49:49type class animal and what are some
- 3:49:51attributes and methods that all animals
- 3:49:53should have
- 3:49:53let's say we have a class variable
- 3:49:55called alive and we'll set this to true
- 3:49:58if you're an animal you begin by being
- 3:50:00alive and what are some methods
- 3:50:02how about an eat and sleep method so
- 3:50:04let's define those
- 3:50:05def eat what are we gonna do when we
- 3:50:08call this method
- 3:50:09let's print something let's print this
- 3:50:12animal
- 3:50:12is eating let's also create a sleep
- 3:50:15method def
- 3:50:16sleep what are we gonna do when we call
- 3:50:18this method let's print something as
- 3:50:19well
- 3:50:20this animal is sleeping and we now have
- 3:50:24our animal class
- 3:50:25now let's create separate classes for
- 3:50:27specific types of animals let's say
- 3:50:30a class for rabbit a fish and a hawk so
- 3:50:32we can make
- 3:50:33objects of those classes so let's begin
- 3:50:36with a
- 3:50:36rabbit class class rabbit now to use
- 3:50:40inheritance
- 3:50:41with the class that you intend to be the
- 3:50:43child class after the class name add a
- 3:50:45set of parentheses
- 3:50:46and then pass in the name of the parent
- 3:50:48class in this case it's going to be
- 3:50:49animal
- 3:50:50so we will add that to the parentheses
- 3:50:53so now
- 3:50:54rabbit is the child class and animal is
- 3:50:56the parent class
- 3:50:57so the child class is going to inherit
- 3:51:00everything that the animal class has
- 3:51:02so for the time being i'm going to type
- 3:51:04pass so this rabbit class
- 3:51:05will have access to a class variable
- 3:51:07called alive
- 3:51:08and an eat and sleep method so let's
- 3:51:11define
- 3:51:12a fish and hawk class and i'm just going
- 3:51:14to copy what i have here and just change
- 3:51:16rabbit to fish and then do so again and
- 3:51:19change rabbit to
- 3:51:21hawk this is our family tree animal
- 3:51:24is the parent class and it has three
- 3:51:26children a rabbit class
- 3:51:27a fish class and a hot class and for the
- 3:51:29time being we're just going to write
- 3:51:31pass as a placeholder
- 3:51:32so i bet we can create objects from
- 3:51:35these classes
- 3:51:36so let's do so let's say we have an
- 3:51:37object called rabbit
- 3:51:39rabbit equals rabbit let's do the same
- 3:51:41thing for
- 3:51:42fish and hawk
- 3:51:47all right we now have three objects
- 3:51:50rabbit
- 3:51:50fish and hawk and let's take a look at
- 3:51:53their
- 3:51:53class variables i bet they have an alive
- 3:51:56variable let's check that so i would
- 3:51:58like to print
- 3:51:59rabbit dot oh would you look at that
- 3:52:02this rabbit class
- 3:52:03has a class variable called alive even
- 3:52:05though there's nothing within this class
- 3:52:07well that's because we used inheritance
- 3:52:09so each of these
- 3:52:10three children classes inherit
- 3:52:12everything from their parents
- 3:52:14all of these attributes and methods i
- 3:52:16bet they have eaten sleep functions as
- 3:52:18well so let's check that
- 3:52:19with fish let's have fish use its eat
- 3:52:22method
- 3:52:24and hawk will use its sleep method hawk
- 3:52:26dot sleep
- 3:52:29so the rabbit is alive the fish is
- 3:52:31eating and
- 3:52:32the hawk is sleeping now even though we
- 3:52:34have nothing written within each of
- 3:52:36these
- 3:52:36specific types of animal classes all of
- 3:52:39these animals have access to these
- 3:52:41attributes and methods because of
- 3:52:43inheritance
- 3:52:44and another benefit of inheritance is
- 3:52:46that we don't need to keep on
- 3:52:48copying and pasting this code for
- 3:52:50example let's say we were not using
- 3:52:51inheritance so we would not need an
- 3:52:53animal class
- 3:52:54we could write our code this way where
- 3:52:56we would just copy and paste everything
- 3:52:59underneath each of our classes so what
- 3:53:01if we need to make a change to one of
- 3:53:03these methods
- 3:53:04as in your boss says you know what let's
- 3:53:06change sleep to
- 3:53:07slumber okay well we would have to go to
- 3:53:10each of these classes and make the
- 3:53:11change manually
- 3:53:12it's not too bad if you have like three
- 3:53:14classes but what if you have hundreds
- 3:53:16that's going to take a lot of effort so
- 3:53:18wouldn't it be nice if we just
- 3:53:20list everything that each of these
- 3:53:21classes has in common
- 3:53:23and then have all of these classes
- 3:53:25inherit from
- 3:53:26one common class and then if you need to
- 3:53:28make any changes
- 3:53:29you can do so just within this class and
- 3:53:31this change will apply to
- 3:53:33all of the different child classes so
- 3:53:35that's another benefit of inheritance
- 3:53:37and not only does it make any changes
- 3:53:39easier but each class
- 3:53:40can have their own unique attributes and
- 3:53:42methods as well
- 3:53:43along with the attributes and methods
- 3:53:45that they inherit from their parents
- 3:53:47so for rabbit fish and hawk let's have
- 3:53:49each of these classes have
- 3:53:50their own unique method as well so for
- 3:53:53rabbit let's define
- 3:53:54a run method if you're a rabbit then you
- 3:53:57can run
- 3:53:57so let's print this rabbit
- 3:54:01is running and i'm just going to fix
- 3:54:04some of the spacing here
- 3:54:05all right if you're a fish then you can
- 3:54:08swim
- 3:54:09def swim let's print
- 3:54:13this fish is swimming
- 3:54:17and lastly we have hawk if you're a hawk
- 3:54:21then you can fly def fly
- 3:54:24let's print this hawk
- 3:54:28is flying like i said before not only
- 3:54:32do these specific types of animals have
- 3:54:35access to
- 3:54:35all of the attributes and methods that
- 3:54:37they inherit from their parents
- 3:54:39but they can have their own attributes
- 3:54:40and methods as well so let's test some
- 3:54:42of these methods
- 3:54:43so we have our rabbit fish and hawk
- 3:54:46objects
- 3:54:46let's have rabbit use its run method
- 3:54:51fish we'll use its swim method and hawk
- 3:54:54will use its fly method
- 3:54:59and here we go this rabbit is running
- 3:55:02this fish is swimming
- 3:55:03and this hawk is flying so in conclusion
- 3:55:06classes can have children the children
- 3:55:09classes will inherit everything that
- 3:55:11their parent class has
- 3:55:12all of the parents attributes and
- 3:55:14methods but not only that the children
- 3:55:16classes can implement
- 3:55:17their own unique attributes and methods
- 3:55:19as well
- 3:55:20so that is inheritance if you would like
- 3:55:22a copy of this code
- 3:55:23i will post all of this in the comment
- 3:55:25section down below but yeah that's the
- 3:55:27basics of inheritance using python
- 3:55:31yo what's going on everybody it's you
- 3:55:33bro hope you're doing well
- 3:55:34and in this video i'm going to explain
- 3:55:36how multi-level inheritance works in
- 3:55:38python so
- 3:55:39sit back relax and enjoy the show
- 3:55:43all right welcome back everybody let's
- 3:55:45talk about multi-level inheritance
- 3:55:47this is a concept where a derived class
- 3:55:50also known as a child class
- 3:55:52inherits from another derived class
- 3:55:54here's an example let's say
- 3:55:56we have a hierarchy of classes there
- 3:55:58will be three levels to this
- 3:56:00we'll create a family tree of living
- 3:56:02organisms
- 3:56:03so at the very top we're going to have a
- 3:56:05class called
- 3:56:06organism any class that inherits from
- 3:56:09the organism parent class
- 3:56:10will receive one attribute one variable
- 3:56:13named alive
- 3:56:14and this will be set to true so if you
- 3:56:16are an organism you begin by being alive
- 3:56:19now let's say we have a child class that
- 3:56:21will inherit from the organism parent
- 3:56:23class
- 3:56:24let's say animal class animal
- 3:56:27inherits from organism so any animal
- 3:56:30that we create
- 3:56:31will have this alive attribute set the
- 3:56:33true
- 3:56:34and all animals eat so let's define a
- 3:56:37function that does that
- 3:56:39at least i believe all animals eat let's
- 3:56:40pretend that they do
- 3:56:42so this animal is eating
- 3:56:46and now why stop there let's create a
- 3:56:48class for a specific type of animal
- 3:56:50i like dogs so let's create a dog class
- 3:56:53class dog
- 3:56:54inherits from the animal class
- 3:56:57and all dogs should be able to bark so
- 3:56:59let's create a function to do that
- 3:57:04and we'll print this dog
- 3:57:07is barking okay so what we just did here
- 3:57:10is
- 3:57:11multi-level inheritance we had a derived
- 3:57:15child class inherit from another child
- 3:57:17class
- 3:57:18so it's kind of like this dog class is
- 3:57:20the child animal class is the parent
- 3:57:22and then the parent of the parent class
- 3:57:25would be like
- 3:57:25a grandparent right and then if you
- 3:57:27added another layer on top of that to
- 3:57:29this hierarchy
- 3:57:30well then the parent of the grandparent
- 3:57:31would be like a great grandparent
- 3:57:33that's how i think of it at least now
- 3:57:35let's create a dog object just to verify
- 3:57:37that everything's working so let's call
- 3:57:38our dog
- 3:57:39dog and our dog object should have
- 3:57:42access to an attribute named alive
- 3:57:44and our dog object receives that
- 3:57:47attribute from the organism class
- 3:57:49and this will print true if i were to
- 3:57:51print whatever this attribute contains
- 3:57:53and our dog should be able to eat
- 3:57:55because it receives this method from the
- 3:57:57animal parent class
- 3:58:00and lastly our dog should be able to
- 3:58:02bark and that method is defined within
- 3:58:04its own class
- 3:58:07so that's how multi-level inheritance
- 3:58:09works it's a concept where a child class
- 3:58:12will inherit from another child class
- 3:58:14i like to think of it like a family tree
- 3:58:17where a child class will have a parent
- 3:58:20and the parent of that parent would be
- 3:58:22like a grandparent basically
- 3:58:24so if you would like a copy of this code
- 3:58:26i will post all of this to the comment
- 3:58:28section down below
- 3:58:29but yeah that is how multi-level
- 3:58:31inheritance works in python
- 3:58:33hey what's going on everybody it's you
- 3:58:35bro hope you're doing well and in this
- 3:58:36video i'm going to show you all how
- 3:58:38multiple inheritance works in python so
- 3:58:40sit back
- 3:58:41relax and enjoy the show
- 3:58:45all right people multiple inheritance
- 3:58:48multiple inheritance is the concept
- 3:58:50where a child class
- 3:58:52is derived from more than one parent
- 3:58:54class
- 3:58:55here's an example let's say that all of
- 3:58:57our children classes are going to be
- 3:58:58certain types of animals and we would
- 3:59:01like our animal classes
- 3:59:03to inherit from either the prepared
- 3:59:05class
- 3:59:06the predator parrot class or both
- 3:59:08depending on what type of animal it is
- 3:59:10because some animals in the animal
- 3:59:12kingdom can be both prey
- 3:59:14and predators and one example that comes
- 3:59:16to mind would be fish
- 3:59:17fish will eat smaller fish but also can
- 3:59:20get eaten by
- 3:59:21larger fish so here we have two classes
- 3:59:23prey and predator
- 3:59:25each has a dedicated method if a class
- 3:59:27inherits from
- 3:59:28the prey parent class they will have
- 3:59:30access to a flea method
- 3:59:32which will print this animal please if a
- 3:59:35class inherits from the predator parent
- 3:59:37class
- 3:59:37that class will have access to a hunt
- 3:59:39method that will print
- 3:59:41this animal is hunting and it's possible
- 3:59:43that some animals can do both
- 3:59:45they can both flee and hunt but they'll
- 3:59:47need to inherit
- 3:59:48both of these classes both prey and
- 3:59:50predator and that's where multiple
- 3:59:52inheritance comes in
- 3:59:53now let's create some classes based on
- 3:59:55certain types of animals
- 3:59:56let's say we have class rabbit and
- 3:59:59rabbit we'll inherit from the prey class
- 4:00:01because
- 4:00:02rabbits are typically prey and not
- 4:00:03predators but i don't know there could
- 4:00:05be some killer rabbits out there
- 4:00:07haven't run into any yet let's have
- 4:00:09class hawk inherit from the
- 4:00:12predator class and lastly fish
- 4:00:16and fish can be both prey and predators
- 4:00:18so they will inherit from both classes
- 4:00:20and we just separate each class with a
- 4:00:22comma so prey
- 4:00:24comma predator and it's as simple as
- 4:00:26that
- 4:00:27now let's create an object from each of
- 4:00:29these classes we have rabbit
- 4:00:31equals rabbit hawk
- 4:00:36equals hawk and fish
- 4:00:39equals fish now let's just verify
- 4:00:42all of the different methods that each
- 4:00:44of these objects inherited
- 4:00:46so our rabbit should have access to a
- 4:00:48flea method
- 4:00:49but no hunt method and let's just test
- 4:00:52that
- 4:00:53yep this animal flees and my hawk object
- 4:00:57should have access to a hunt method but
- 4:00:59no flea method
- 4:01:01this animal is hunting and lastly
- 4:01:04my fish object has access to both a flea
- 4:01:08and a hunt method and if you take a look
- 4:01:10on the right hand side of this pop-up
- 4:01:11window it shows the class that it
- 4:01:13inherited this method from
- 4:01:15so fleet comes from the parent class of
- 4:01:17prey and hunt comes from the apparent
- 4:01:19class of predator
- 4:01:21so my fish can both flee and hunt
- 4:01:24because it's typically considered both
- 4:01:26prey and predators
- 4:01:29this animal flees this animal is hunting
- 4:01:31so that's basically what multiple
- 4:01:33inheritance is
- 4:01:34it's a concept where a child class is
- 4:01:37derived from more than one parent class
- 4:01:40so that's basically multiple inheritance
- 4:01:42i'll post all of this code to the
- 4:01:43comment section down below
- 4:01:45and well yeah that's how multiple
- 4:01:47inheritance works
- 4:01:48in python hey what's going on everybody
- 4:01:51it's bro hope you're doing well
- 4:01:53and in this video i'm going to teach you
- 4:01:54guys about method overriding in python
- 4:01:56so
- 4:01:57sit back relax and enjoy the show
- 4:02:01here's a super quick video on method
- 4:02:03overriding i have two classes a class
- 4:02:06called animal and a class called rabbit
- 4:02:08rabbit inherits from the animal class
- 4:02:09therefore rabbit is the child class
- 4:02:11an animal is the parent class within the
- 4:02:13animal class there is one method a
- 4:02:15method called eat
- 4:02:16and when you call this method all this
- 4:02:18will do is print this animal is eating
- 4:02:21the rabbit class inherits from the
- 4:02:22animal class therefore
- 4:02:24rabbit has access to this eat method and
- 4:02:26if i were to create a rabbit object
- 4:02:28and this rabbit used its eat method well
- 4:02:31then this is going to print
- 4:02:32this animal is eating now method of
- 4:02:35writing is the ability of an object
- 4:02:37oriented programming language
- 4:02:38to allow a subclass also known as a
- 4:02:41child class
- 4:02:42to provide a specific implementation of
- 4:02:45a method
- 4:02:46that is already provided by one of its
- 4:02:48parents in this case we're going to
- 4:02:50override
- 4:02:50the eat method and what we can do is
- 4:02:52provide a more specific
- 4:02:54implementation for the rabbit class and
- 4:02:57now to override a method what we'll do
- 4:02:59is that within
- 4:02:59the child class we need to define a
- 4:03:02method with the same
- 4:03:03matching method signature that is the
- 4:03:05combination of a method's name
- 4:03:07plus its parameters both of these
- 4:03:09together are known as a
- 4:03:10method signature within the child class
- 4:03:13we will define
- 4:03:14and eat method with the same parameters
- 4:03:16def eat
- 4:03:17and in this case the only parameter is
- 4:03:19self just to keep this simple
- 4:03:21and what we'll do is that will provide a
- 4:03:23more specific
- 4:03:24implementation of the eat method
- 4:03:26specifically for rabbits
- 4:03:28so let's print something more closely
- 4:03:29associated with rabbits such as
- 4:03:32this rabbit is eating a
- 4:03:35carrot now if i were to run this program
- 4:03:37again
- 4:03:38we will instead use this implementation
- 4:03:40of the
- 4:03:41method instead of the one that it
- 4:03:42inherits from its parent class
- 4:03:44of animal basically speaking an object
- 4:03:47will use a method that is more closely
- 4:03:49associated with itself first before
- 4:03:51relying on a method that it may inherit
- 4:03:54from a parent class
- 4:03:55in this example we're using this version
- 4:03:57of the eat method
- 4:03:58specifically for rabbits instead of the
- 4:04:00version that we inherit from the parent
- 4:04:02class
- 4:04:03of animal that is the basics of method
- 4:04:05overriding i will post this code in the
- 4:04:07comment section down below
- 4:04:09and well yeah that's how to override a
- 4:04:11method using python
- 4:04:14hey what's going on everybody it's you
- 4:04:16bro hope you're doing well
- 4:04:17and in this video i'm going to teach you
- 4:04:18guys all about method chaining in python
- 4:04:21so
- 4:04:21sit back relax and enjoy the show
- 4:04:26all right everybody let's talk about
- 4:04:28method training method chaining is used
- 4:04:30to call
- 4:04:30multiple methods sequentially and each
- 4:04:33call performs an action on the same
- 4:04:35object
- 4:04:36and return self here's an example of how
- 4:04:38method chaining would be useful
- 4:04:40let's say we have a class named car car
- 4:04:43has
- 4:04:43four methods turn on drive break and
- 4:04:46turn off
- 4:04:47they will all just print a simple
- 4:04:48message beginning with turn on
- 4:04:50turn on what print you start the engine
- 4:04:53drive will print you drive the car
- 4:04:55brake will print you step on the brakes
- 4:04:56and turn off will print
- 4:04:58you turn off the engine let's say we
- 4:05:00have a car object
- 4:05:01car equals car and i would like my car
- 4:05:04object
- 4:05:04to use its turn on method immediately
- 4:05:07followed by
- 4:05:08the drive method with how we've been
- 4:05:10coding things previously we may write
- 4:05:12something such as
- 4:05:13the name of the object in this case car
- 4:05:15dot and then the method to use
- 4:05:17i would like my card to use the turn on
- 4:05:19method followed by
- 4:05:21car dot drive so currently this will
- 4:05:23take two lines of code
- 4:05:25but this will print you start the engine
- 4:05:27and you drive the car
- 4:05:29now with method training we can call
- 4:05:31multiple methods
- 4:05:32sequentially now the format on this is
- 4:05:34going to be a little bit different
- 4:05:36and if we were to write the same code
- 4:05:38using method chaining it might look a
- 4:05:39little something like this
- 4:05:40after the first method call we would add
- 4:05:43dot
- 4:05:44and then an additional method that we
- 4:05:46would like to call so for example
- 4:05:48after the turn on method we'll
- 4:05:50immediately call
- 4:05:51the drive method but there's one more
- 4:05:53thing that we need to add when we're
- 4:05:54using python to do method training
- 4:05:56so normally this by itself will not work
- 4:05:59when we call a method in python if there
- 4:06:01is nothing that is returned python will
- 4:06:03return
- 4:06:03none so as you can see here attribute
- 4:06:06error none type
- 4:06:07object has no attribute of drive so we
- 4:06:10need to return itself under each method
- 4:06:12that we're using method training with
- 4:06:14so let's add return self to each of
- 4:06:16these methods
- 4:06:17so let's do that for drive
- 4:06:20break and the turn off method and this
- 4:06:24should work now you start the engine
- 4:06:26you drive the car so think of it like
- 4:06:28this after we finish
- 4:06:30calling this method python is going to
- 4:06:32return self
- 4:06:33so it's going to return car so then
- 4:06:35we'll immediately use
- 4:06:37car.drive now here's another example
- 4:06:39let's say i would like to
- 4:06:41call the break method followed by the
- 4:06:43turn off method
- 4:06:44so that would be car dot break
- 4:06:47parentheses
- 4:06:48followed by dot turn
- 4:06:51off so this will print you step on the
- 4:06:55brakes
- 4:06:55you turn off the engine now here's an
- 4:06:57extreme example let's say we would like
- 4:06:59to call all four methods in order
- 4:07:01so that would be car dot
- 4:07:04turn on followed by dot drive
- 4:07:08dot break dot
- 4:07:11turn off so this will call
- 4:07:15all four methods in order starting with
- 4:07:17the leftmost method
- 4:07:19you start the engine you drive the car
- 4:07:21you step on the brakes
- 4:07:22you turn off the engine now if you have
- 4:07:25one long method chain like this
- 4:07:27it might become difficult to read after
- 4:07:28a while so what i would recommend
- 4:07:30if you're doing a lot of method chaining
- 4:07:32is that after each method call
- 4:07:34just hit enter to move each method call
- 4:07:36down to a new line
- 4:07:38so it'll look something like that and
- 4:07:40this backslash
- 4:07:41might be inserted this is a line
- 4:07:43continuation character
- 4:07:44so this is a little more readable but
- 4:07:47you know it still does the same thing
- 4:07:48basically
- 4:07:49all right everybody so that is method
- 4:07:51training it's used to call
- 4:07:53multiple methods sequentially and each
- 4:07:55call performs an
- 4:07:56action on the same object and returns
- 4:07:58self if you would like a copy of this
- 4:08:00code i will post all of this in the
- 4:08:02comments section down below but yeah
- 4:08:04that is how to do a method chaining in
- 4:08:06python
- 4:08:08hey what's going on everybody it's bro
- 4:08:10hope you're doing well
- 4:08:11and in this video i'm going to explain
- 4:08:13the super function in python so
- 4:08:15sit back relax and enjoy the show
- 4:08:20well then since you clicked on this
- 4:08:21video i should probably discuss
- 4:08:23the super function now the super
- 4:08:25function is used to give access
- 4:08:26to the methods of a parent class it
- 4:08:29returns a temporary object
- 4:08:32of a parent class when used here's an
- 4:08:34example of why the super function would
- 4:08:36be useful
- 4:08:37let's take this program that i've
- 4:08:38written there's three classes
- 4:08:40a class called rectangle which is the
- 4:08:43parent class
- 4:08:43to two children classes square and cube
- 4:08:47and all we're doing is creating a square
- 4:08:49object and a cube object
- 4:08:50but we need to pass in a length and a
- 4:08:53width for squares
- 4:08:54and a length width and height for cubes
- 4:08:57now with programming we don't like to
- 4:08:59repeat code we like to reuse code and if
- 4:09:01you take a look within my knit methods
- 4:09:04for both square and cube we're repeating
- 4:09:06self.length equals length
- 4:09:08and self.width equals width twice for
- 4:09:11our init methods
- 4:09:12so one thing that we can do any
- 4:09:14similarities between
- 4:09:16the square and cube class we can place
- 4:09:18within
- 4:09:19the rectangle class and then reuse this
- 4:09:21code so what i'm going to do
- 4:09:23is copy this indent method within our
- 4:09:26square class
- 4:09:27and paste it within our rectangle parent
- 4:09:29class
- 4:09:30and then in order to access this init
- 4:09:33method
- 4:09:33we can use the super function so we no
- 4:09:36longer need
- 4:09:37these two lines of code what i'm going
- 4:09:39to do is ask
- 4:09:40the rectangle class to use its init
- 4:09:42method so we're going to type
- 4:09:44super parentheses dot and then the name
- 4:09:48or the function of the parent class
- 4:09:49that we would like to use and i would
- 4:09:51like to use the init method
- 4:09:53but i need to send a length and a width
- 4:09:55because it requires that
- 4:09:56so let's send these so we're going to
- 4:09:58pass in our length and
- 4:10:00our width and now we can do the same
- 4:10:01thing for our cube init method
- 4:10:03so i will copy this line of code we no
- 4:10:06longer need these two lines of code
- 4:10:08because we will be reusing our init
- 4:10:10method within the parent class
- 4:10:12of rectangle but we'll still need to
- 4:10:14keep this line in to assign the height
- 4:10:16because that's a difference between our
- 4:10:19square and cube class
- 4:10:20and now we can just reuse this init
- 4:10:23method within
- 4:10:24the rectangle parent class now i'm
- 4:10:26thinking that we should probably test to
- 4:10:27see if
- 4:10:28length and width are assigned to these
- 4:10:30attributes and one way
- 4:10:31that i think would be fun would be to
- 4:10:33create an area method
- 4:10:35for squares and a volume method for
- 4:10:37cubes so let's define
- 4:10:38an area method within the square class
- 4:10:41def
- 4:10:42area and i would like to return self
- 4:10:46dot length times self
- 4:10:49dot width and we can create a volume
- 4:10:52method within cubes
- 4:10:54so let's do that def let's change this
- 4:10:57to volume
- 4:10:58we'll multiply it length times width
- 4:11:00times height
- 4:11:02but we need to type self dot pipe
- 4:11:05and we can call these methods to get the
- 4:11:07area
- 4:11:08of a square or the volume of a cube
- 4:11:11let's print
- 4:11:13square dot area
- 4:11:17and then cube dot
- 4:11:21volume so if our init method
- 4:11:25is actually being used we should have a
- 4:11:27length and the width
- 4:11:28assigned to these attributes and it
- 4:11:31looks like we do
- 4:11:33in conclusion the super function is used
- 4:11:35to give access to the methods of a
- 4:11:37parent class
- 4:11:38it returns a temporary object of a
- 4:11:40parent class when used in order to
- 4:11:43access
- 4:11:43the methods of the parent class also
- 4:11:46known as
- 4:11:46a super class and in this example within
- 4:11:49the init methods of both square and cube
- 4:11:52we immediately called the init method of
- 4:11:55the parent class
- 4:11:56to pass in some arguments that both of
- 4:11:58these classes
- 4:11:59have in common so if you would like a
- 4:12:01copy of this code i will post all of
- 4:12:03this in the comment section down below
- 4:12:05but yeah that is how the super function
- 4:12:07works in python
- 4:12:09hey what's going on everybody it's bro
- 4:12:11hope you're doing well
- 4:12:12and in this video i'm going to explain
- 4:12:14how abstract classes work in python so
- 4:12:17sit back relax and enjoy the show
- 4:12:21hey what's good everybody let's talk
- 4:12:23about abstract classes
- 4:12:25now abstract classes prevent a user from
- 4:12:27creating an object of that class
- 4:12:30think of an abstract class more as a
- 4:12:32template
- 4:12:33it's an idea it's not real it's like a
- 4:12:35ghost class basically
- 4:12:37plus an abstract class compels a user to
- 4:12:40override any abstract methods within a
- 4:12:42child class
- 4:12:44so it's also a form of checks and
- 4:12:45balances as well which we'll discuss
- 4:12:47later
- 4:12:48now here's a small program that i've
- 4:12:49written let's say we have
- 4:12:51three classes class vehicle car and
- 4:12:53motorcycle
- 4:12:54car and motorcycle are the children
- 4:12:57classes of the vehicle class
- 4:12:59now within this vehicle class we have a
- 4:13:01go method
- 4:13:02but i'm currently only defining it not
- 4:13:04actually implementing it so i'm just
- 4:13:05writing pass
- 4:13:06now within car and motorcycle we're
- 4:13:09overriding this method and creating our
- 4:13:10own implementation of the skull method
- 4:13:12that we inherit
- 4:13:13the go method for the car will print you
- 4:13:16drive the car
- 4:13:16and the go method for the motorcycle
- 4:13:18class will print you ride the motorcycle
- 4:13:20now currently i'm creating an object of
- 4:13:22each of these classes
- 4:13:24i have a generic vehicle object car
- 4:13:26object
- 4:13:27and motorcycle object now after running
- 4:13:29this program
- 4:13:30currently the go method within our
- 4:13:32vehicle class is not being implemented
- 4:13:34so this won't print anything but the go
- 4:13:36method within our car class will print
- 4:13:38you drive the car and the go method
- 4:13:41within our motorcycle class will print
- 4:13:43you ride the motorcycle with that being
- 4:13:45said let's pretend that we're coding the
- 4:13:47next
- 4:13:47need for speed game and we would like a
- 4:13:49user to create an object from a specific
- 4:13:52kind of vehicle whether it's a car from
- 4:13:54the car class or a motorcycle from the
- 4:13:56motorcycle class
- 4:13:57we would like to prevent a user from
- 4:13:59creating an object of the vehicle class
- 4:14:02because the vehicle class is too generic
- 4:14:04we do not have all of the
- 4:14:05implementations
- 4:14:06set up for a vehicle it would be like
- 4:14:09somebody unlocked the ghost car or the
- 4:14:11invisible car
- 4:14:12which doesn't actually exist and is
- 4:14:13missing a lot of features we need a user
- 4:14:16to create an object
- 4:14:17from a child class because these are
- 4:14:19fully fleshed out and one way in which
- 4:14:21we can prevent a user from creating an
- 4:14:23object of this class
- 4:14:25as well to turn this class into an
- 4:14:27abstract class
- 4:14:28which is what this video is all about go
- 4:14:30figure now the first step to creating an
- 4:14:32abstract class is that we'll need some
- 4:14:34imports
- 4:14:34so at the top of your program write this
- 4:14:37from
- 4:14:37abc abc is an acronym for abstract
- 4:14:41based class abc from abc
- 4:14:44import abc all capital comma
- 4:14:48abstract method all right
- 4:14:51now with our vehicle class the class
- 4:14:53that you intend to be the abstract class
- 4:14:56add us out of parentheses and our
- 4:14:58vehicle class
- 4:14:59will inherit from the abc class
- 4:15:02and now with any methods within your
- 4:15:04vehicle class you're going to add
- 4:15:05this decorator at the top at abstract
- 4:15:09method all right we should now be
- 4:15:12prevented from creating a
- 4:15:13vehicle object our vehicle class is now
- 4:15:16an abstract class
- 4:15:17and we cannot give it a physical form a
- 4:15:19physical manifestation
- 4:15:21and if we attempt to you can see here
- 4:15:22that we have a type error
- 4:15:24can't instantiate abstract class from
- 4:15:26vehicle with abstract methods
- 4:15:28go so an abstract class is a class which
- 4:15:31contains
- 4:15:32one or more abstract methods and an
- 4:15:35abstract method
- 4:15:36is a method that has a declaration but
- 4:15:38does not have an implementation
- 4:15:40now by definition an abstract class
- 4:15:43contains
- 4:15:44one or more abstract methods if i remove
- 4:15:47this one abstract method within our
- 4:15:49vehicle class
- 4:15:50well we could still create a vehicle
- 4:15:52object and i'm just going to turn this
- 4:15:54vehicle.go line into a comment because
- 4:15:56we don't have a go method anymore
- 4:15:58as you can see we can still create a
- 4:16:00vehicle object which we would like to
- 4:16:02prevent a user from doing
- 4:16:04so we need at least one abstract method
- 4:16:06within our vehicle class
- 4:16:08so i'm going to go ahead and add this go
- 4:16:10method back in
- 4:16:11an additional feature of abstract
- 4:16:13classes is that they compel
- 4:16:15a user to override any abstract methods
- 4:16:17within a child class
- 4:16:19a method that is abstract has a
- 4:16:21declaration but does not have
- 4:16:22an implementation so let's say that we
- 4:16:25create our vehicle class
- 4:16:26our abstract vehicle class and now we're
- 4:16:29creating
- 4:16:29the children classes of car and
- 4:16:31motorcycle and let's say i'm not paying
- 4:16:33attention and i forget to add a go
- 4:16:35method
- 4:16:36so if i just write pass within my
- 4:16:38motorcycle class i'm currently missing a
- 4:16:40go method within my motorcycle class
- 4:16:43well python is going to prevent me from
- 4:16:45running this because we have a type air
- 4:16:47can't instantiate abstract class vehicle
- 4:16:50with abstract methods of go
- 4:16:52so by inheriting from an abstract class
- 4:16:55it's a good form of checks and balances
- 4:16:57to be sure that your children classes
- 4:16:59are not missing
- 4:17:00any implementations of any methods that
- 4:17:02they inherit
- 4:17:04here's an additional way of thinking of
- 4:17:05this our vehicle class
- 4:17:07is telling its children if you're going
- 4:17:08to inherit from me then you need to
- 4:17:10override
- 4:17:11this abstract method of mine and if you
- 4:17:13don't well i'm not going to let you be
- 4:17:14instantiated
- 4:17:16so in order for us to create a cart and
- 4:17:18motorcycle class
- 4:17:19we need to override the go method that
- 4:17:22they inherit from its parent class of
- 4:17:23vehicle
- 4:17:24and provide its own implementation now
- 4:17:26let's say we create an additional method
- 4:17:29let's say we have
- 4:17:30a stop method so let's define that def
- 4:17:32stop
- 4:17:33for the time being i'll write pass and
- 4:17:35to make this an abstract method
- 4:17:37add this decorator at abstract
- 4:17:40method now car and motorcycle both need
- 4:17:44to
- 4:17:44implement that method of stop it's
- 4:17:46currently missing it
- 4:17:47as you can see here class car must
- 4:17:49implement all abstract methods
- 4:17:51and the same thing applies for a
- 4:17:52motorcycle as well so we need to
- 4:17:54override this method and provide its own
- 4:17:56implementation
- 4:17:58so let's remove that decorator and let's
- 4:18:01print something
- 4:18:02let's print this car is stopped
- 4:18:06and i'll do the same thing for
- 4:18:08motorcycle
- 4:18:10so def stop let's say
- 4:18:14this motorcycle
- 4:18:18is stopped and we'll call the stop
- 4:18:21method for each of these
- 4:18:22types of vehicles car dot stop
- 4:18:26and motorcycle dot stop
- 4:18:29all right and we can now create a car
- 4:18:32and motorcycle object
- 4:18:33because we are overriding both of these
- 4:18:36abstract methods found within the parent
- 4:18:38class of vehicle
- 4:18:39all right everybody so that's the basics
- 4:18:41of abstract classes
- 4:18:42it's a class which contains one or more
- 4:18:45abstract methods
- 4:18:46and an abstract method is a method that
- 4:18:48has a declaration
- 4:18:50but does not have an implementation and
- 4:18:52the benefits of using an abstract class
- 4:18:54is that they prevent a user from
- 4:18:56creating an object of that class
- 4:18:58plus it compels a user to override any
- 4:19:01abstract methods found within a child
- 4:19:02class
- 4:19:03so if you would like a copy of this code
- 4:19:05i will post all of this in the comments
- 4:19:07section down below
- 4:19:08but yeah that is how abstract classes
- 4:19:10work in python
- 4:19:12hey what's going on everybody it's you
- 4:19:14bro hope you're doing well
- 4:19:15and in this video i'm going to explain
- 4:19:17how we can pass objects as
- 4:19:18arguments so sit back relax and enjoy
- 4:19:22the show
- 4:19:24ladies and gentlemen in today's video
- 4:19:26i'm going to be demonstrating how we can
- 4:19:28pass
- 4:19:28objects as arguments for this example
- 4:19:31let's say we have a class
- 4:19:32car and there is one class variable
- 4:19:35called color
- 4:19:36color equals none what we'll be doing is
- 4:19:38calling a function that accepts an
- 4:19:40object as an argument as well as a color
- 4:19:43so let's create a function named change
- 4:19:46color now make sure when you define this
- 4:19:48function it's not within the cart class
- 4:19:50then technically this would be a method
- 4:19:52of the car class
- 4:19:53what we would like is a separate
- 4:19:55function outside of the car class
- 4:19:57now we need to set up some parameters we
- 4:19:59would like to accept a car object as
- 4:20:01well as a color
- 4:20:02so for the first parameter let's say a
- 4:20:05car and make sure this is in
- 4:20:06uppercase or capital because python
- 4:20:08doesn't like that argument names should
- 4:20:10be lowercase
- 4:20:11so when we pass in a car we will give it
- 4:20:13a nickname of car
- 4:20:15and we would also like to pass in a
- 4:20:17color so let's set up a parameter called
- 4:20:19color
- 4:20:19now when we pass in a car we will assign
- 4:20:22that car's color with whatever color
- 4:20:24that we receive so
- 4:20:26car dot color equals color
- 4:20:29and now let's create some car objects so
- 4:20:31let's say car one
- 4:20:33equals car car two equals car
- 4:20:36and car three equals cart currently if i
- 4:20:40were to print
- 4:20:40their colors this is what we'll see
- 4:20:43print car one
- 4:20:44dot color and repeat the steps for two
- 4:20:47and three
- 4:20:48and we're not calling this function yet
- 4:20:50so all of these colors should be set to
- 4:20:52none
- 4:20:53they are basically cars with no color
- 4:20:56they're i don't know iron or something
- 4:20:58whatever cars are made out of aluminum
- 4:21:00plastic stuff like that okay so now
- 4:21:02let's call this function
- 4:21:03change color perhaps right here so in
- 4:21:06order to call this function we need to
- 4:21:08pass in
- 4:21:09a car object as well as a color so let's
- 4:21:11pass in
- 4:21:12car one and a color perhaps red and
- 4:21:15we'll repeat the steps for two and three
- 4:21:17so let's say car two will be white
- 4:21:21and car 3 will be blue
- 4:21:24now after calling this function we have
- 4:21:27a car
- 4:21:28object and a color and we're assigning
- 4:21:30the color of our car to whatever color
- 4:21:32that we receive
- 4:21:33now these cars in order car 1 2 and 3
- 4:21:36are red white and blue now one thing
- 4:21:39that you should know
- 4:21:39is that the name of this parameter that
- 4:21:41accepts our object
- 4:21:42doesn't necessarily need the same name
- 4:21:45as the class name
- 4:21:46that created the object that we're
- 4:21:47passing in we could name this something
- 4:21:49else entirely perhaps
- 4:21:51vehicle you'll just want to be sure that
- 4:21:52this is at least descriptive
- 4:21:54of the kind of object that you would
- 4:21:55like to pass in and we're not limited to
- 4:21:58just passing in car objects we can pass
- 4:22:00in all sorts of objects
- 4:22:02but we need to pass in an object as well
- 4:22:04as a color
- 4:22:05so this time let's create a class called
- 4:22:07motorcycle
- 4:22:08class motorcycle and this class will
- 4:22:12also have a color set to none
- 4:22:14so what we're doing is actually reusing
- 4:22:16this function for all sorts of different
- 4:22:18kinds of vehicles
- 4:22:19now let's create a vehicle i'll just
- 4:22:21name this bike one for short
- 4:22:22bike one equals motorcycle now let's
- 4:22:26call the
- 4:22:27change color function and we can pass in
- 4:22:30an object as well as color this time we
- 4:22:33will pass in
- 4:22:34our bike object as well as a color i
- 4:22:37think
- 4:22:37black counts as a color and let's print
- 4:22:42bike once color
- 4:22:46and this should work we have red white
- 4:22:48blue and black
- 4:22:50in conclusion we can pass objects as
- 4:22:52arguments to a function
- 4:22:54much like what we've been doing with
- 4:22:55variables however the type of objects
- 4:22:58that we pass in may be limited based on
- 4:23:01the required attributes and methods
- 4:23:03that that given class or object might
- 4:23:05have and we'll get more into this on the
- 4:23:08next video on
- 4:23:09duct typing so if you would like a copy
- 4:23:11of all this code i will post all of this
- 4:23:13in the comment section down below
- 4:23:15but yeah that is how to pass objects as
- 4:23:17arguments
- 4:23:18using python hey what's going on
- 4:23:21everybody
- 4:23:21it's bro hope you're doing well and in
- 4:23:23this video i'm going to explain duck
- 4:23:25typing in python so
- 4:23:26sit back relax and enjoy the show
- 4:23:31i hope you all like ducks because that's
- 4:23:33the topic of this video
- 4:23:34duct typing is the concept where the
- 4:23:37class of an object is less important
- 4:23:39than the methods and or attributes that
- 4:23:41that class might have
- 4:23:43the class type is not checked if the
- 4:23:45minimum methods and or attributes
- 4:23:47are present it's based off of a popular
- 4:23:50phrase
- 4:23:50if it walks like a duck and it quacks
- 4:23:52like a duck then it must be a duck
- 4:23:54here's an example i have two classes
- 4:23:57class duck and class chicken
- 4:23:59both duck and chicken have walk and talk
- 4:24:01methods
- 4:24:02but they print something different with
- 4:24:04the duck class the walk method will
- 4:24:06print
- 4:24:06this duck is walking and the duck talk
- 4:24:09method will
- 4:24:10print this duck is quacking the chicken
- 4:24:12class
- 4:24:13has the same named methods walk and talk
- 4:24:15but they print something slightly
- 4:24:16different
- 4:24:17a slightly different variation the print
- 4:24:19method will print
- 4:24:20this chicken is walking and the talk
- 4:24:22method will print
- 4:24:23this chicken is clucking now let's say
- 4:24:25we have a third class a class
- 4:24:28called person
- 4:24:32now there is one method within our
- 4:24:34person class
- 4:24:35let's say we're attempting to catch a
- 4:24:37duck
- 4:24:39we'll pass in self and we need to pass
- 4:24:41in a
- 4:24:42duck object as an argument so let's type
- 4:24:45duck now within this method
- 4:24:48we'll have our duck use its walk method
- 4:24:53as well as its talk method duck.walk and
- 4:24:56duck.talk
- 4:24:58and then maybe we'll print something
- 4:25:02such as you caught the critter
- 4:25:07now let's create an object from each of
- 4:25:09these classes let's create a
- 4:25:10duck object duck equals duck
- 4:25:14a chicken object chicken equals chicken
- 4:25:17and lastly person person equals person
- 4:25:21now if we would like our person to use
- 4:25:23the catch method
- 4:25:24we need to pass in a duck as an argument
- 4:25:27so with our person let's type person
- 4:25:29dot catch and we need to send a duck
- 4:25:32object because that's a required
- 4:25:34parameter here
- 4:25:35so we're passing in our duck object that
- 4:25:37we created and as soon as we catch the
- 4:25:39duck
- 4:25:39it's going to use its walk and talk
- 4:25:41method and then we'll print
- 4:25:43you caught the critter so as you would
- 4:25:45expect this will print
- 4:25:46this duck is walking the stuck is
- 4:25:48quacking you caught the critter
- 4:25:50now with duct typing we can pass in
- 4:25:53a different type of object as long as it
- 4:25:56has
- 4:25:56the same methods and or attributes as
- 4:25:59our duck
- 4:26:00we could pass in a chicken because a
- 4:26:02chicken can also walk
- 4:26:03and talk so this time let's pass in our
- 4:26:06chicken
- 4:26:06as an argument and this will still work
- 4:26:09even though our parameter is set up to
- 4:26:10take a duck object
- 4:26:13this chicken is walking this chicken is
- 4:26:15clucking you caught the critter
- 4:26:17in layman's terms think of it like this
- 4:26:19python is examining this chicken object
- 4:26:21it's using its walk method it's using
- 4:26:23its talk method which are required
- 4:26:25and python is saying well that's close
- 4:26:27enough remember if it walks like a duck
- 4:26:29and it quacks like a duck
- 4:26:30then it must be a duck so duct typing is
- 4:26:33the concept where the class of an object
- 4:26:35is less important than the methods and
- 4:26:38or attributes that it might have
- 4:26:39the class type is not checked if the
- 4:26:41minimum methods and or attributes
- 4:26:44are present now let's say with our
- 4:26:46chicken object our chicken can no longer
- 4:26:48walk
- 4:26:50well we cannot pass in our chicken
- 4:26:51object anymore
- 4:26:53we have an attribute error chicken
- 4:26:55object has no attribute walk
- 4:26:57so python took a look at this chicken
- 4:26:59and noticed that it does not have a walk
- 4:27:01method
- 4:27:01well it's not walking like a duck but
- 4:27:03it's talking like a duck
- 4:27:05so it doesn't count as a duck because it
- 4:27:07doesn't have its walk method
- 4:27:08in conclusion the class type of an
- 4:27:10object is not as important as the
- 4:27:12methods and or attributes that that
- 4:27:14class might have when using
- 4:27:16duct typing the class type will not be
- 4:27:18checked if the minimum
- 4:27:19methods and or attributes are present
- 4:27:22since
- 4:27:22chickens can both walk and talk like
- 4:27:24ducks then
- 4:27:25they can be a substitute for ducks when
- 4:27:27we pass in our duck object to this
- 4:27:29method
- 4:27:30so if you would like a copy of this code
- 4:27:32i will post all of this in the comment
- 4:27:33section down below
- 4:27:35but yeah that is the basics of duct
- 4:27:37typing in python
- 4:27:38hey what's going on everybody it's you
- 4:27:40bro hope you're doing well
- 4:27:42and in this video i'm going to discuss
- 4:27:44the walrus operator in python
- 4:27:46so sit back relax and enjoy the show
- 4:27:51all right people i hope you like
- 4:27:52walruses because that's the topic of
- 4:27:54this video we'll be discussing
- 4:27:56the walrus operator also known as an
- 4:27:58assignment expression
- 4:28:00it's a colon followed by an equal sign
- 4:28:02and it kind of looks like a walrus on
- 4:28:04its side
- 4:28:05now this is a new feature for python 3.8
- 4:28:08what it does is that it assigns values
- 4:28:10to variables
- 4:28:11as part of a larger expression here's an
- 4:28:14example of why the walrus operator would
- 4:28:16be useful
- 4:28:16let's say we have a variable named happy
- 4:28:19happy
- 4:28:19equals true if you're not happy or
- 4:28:21you're feeling so so you can set this to
- 4:28:23false if you want
- 4:28:24so let's print the value contained
- 4:28:27within happy
- 4:28:28so this will print true to the console
- 4:28:30window
- 4:28:31now wouldn't it be cool if we could
- 4:28:33combine both of these
- 4:28:35statements together so we would want to
- 4:28:37say something such as
- 4:28:39happy equals true and then print it to
- 4:28:42the console window using one line of
- 4:28:44code
- 4:28:44well normally we can't do this using the
- 4:28:47standard assignment operator now one
- 4:28:49thing that we can do is use
- 4:28:50the walrus operator this assigns a value
- 4:28:53to a variable
- 4:28:54as part of a larger expression so this
- 4:28:56would technically
- 4:28:57be allowed if this assignment operator
- 4:29:00was instead
- 4:29:01an assignment expression the walrus
- 4:29:03operator happy
- 4:29:04walrus true and when they're all within
- 4:29:07a print statement
- 4:29:08we can assign a value to a variable and
- 4:29:11use it as a part of a larger expression
- 4:29:14so as you can see
- 4:29:15this new variable of happy was assigned
- 4:29:17and used in one expression
- 4:29:19now here's a more practical example of
- 4:29:22why the walrus operator would be useful
- 4:29:24let's say we have the smell program what
- 4:29:26this will do is consistently ask you
- 4:29:28what food you like and put it into a
- 4:29:30list called foods
- 4:29:32foods equals list while true food equals
- 4:29:35input what food do you like if food
- 4:29:38equals quit
- 4:29:39break then append our list of foods with
- 4:29:42this new food item that we have
- 4:29:44okay let's run this then so this while
- 4:29:47loop
- 4:29:47will continue as long as i don't type in
- 4:29:49quit and it will consistently ask me
- 4:29:51what food do i like and place that food
- 4:29:54item into a list
- 4:29:55named foods so let's begin what food do
- 4:29:58i like well i do like pizza
- 4:30:00i like sushi and i like ice cream
- 4:30:04and that should be enough for me i'm
- 4:30:06going to type in quit to exit this
- 4:30:07program
- 4:30:08now let's write the same program but
- 4:30:10instead use
- 4:30:11a walrus operator so that we can write
- 4:30:13this program using
- 4:30:15less lines of code now if i were to use
- 4:30:17the walrus operator this is how i would
- 4:30:19write the same program
- 4:30:20we would still need our list foods
- 4:30:22equals
- 4:30:23list now for my while loop it's going to
- 4:30:26look something like this
- 4:30:28while food walrus operator
- 4:30:32input what
- 4:30:36food do you like
- 4:30:40all right this is the assignment portion
- 4:30:42now we can use
- 4:30:43this assignment portion within a larger
- 4:30:46expression
- 4:30:47so let's also check to see if our input
- 4:30:50does not equal quit alright
- 4:30:54so then after each iteration of this
- 4:30:56while loop
- 4:30:57we will take our list foods dot
- 4:31:00append food and
- 4:31:04that is it so we have written the same
- 4:31:06program in about half the lines of code
- 4:31:08that we did previously so
- 4:31:10let's test it what food do you like
- 4:31:12pizza
- 4:31:13sushi ice cream
- 4:31:17and quit well everybody that is the
- 4:31:20basics of the walrus operator also known
- 4:31:22as an assignment
- 4:31:23expression it's a new feature for python
- 4:31:253.8
- 4:31:26and beyond so if this doesn't work for
- 4:31:28you i would check to see what version of
- 4:31:30python you're using first
- 4:31:31it assigns values to variables as part
- 4:31:34of a larger expression
- 4:31:36if you would like a copy of this code i
- 4:31:37will post all of this in the comment
- 4:31:39section down below
- 4:31:40but yeah that is how the walrus operator
- 4:31:42works in python
- 4:31:45hello what's going on everybody it's bro
- 4:31:47hope you're doing well and in this video
- 4:31:48i'm going to show you guys how we can
- 4:31:50assign a function to a variable in
- 4:31:52python
- 4:31:52so sit back relax and enjoy the show
- 4:31:57hey all in today's video i will show you
- 4:31:59all how we can assign a function to a
- 4:32:01variable
- 4:32:02this video will be a prerequisite for a
- 4:32:04few videos i have coming up regarding
- 4:32:06functions
- 4:32:06now let's say we have a function named
- 4:32:08hello and all we'll do is print the word
- 4:32:11hello
- 4:32:11and as you know to call this function
- 4:32:13you would type the name of the function
- 4:32:14followed by a set of parentheses and
- 4:32:16this will print the word
- 4:32:17hello the set of parenthesis that comes
- 4:32:19after a function's name
- 4:32:21is the portion that will call the
- 4:32:23function if you were to remove
- 4:32:24that set of parenthesis afterwards we
- 4:32:26would not in fact call that function
- 4:32:29now with python python will pretty much
- 4:32:31treat everything as objects
- 4:32:32including functions so there's something
- 4:32:34i want to show you guys
- 4:32:35if i was to print the name of my
- 4:32:37function hello
- 4:32:39what will be displayed is the memory
- 4:32:41address of this function
- 4:32:43this is the memory address of where this
- 4:32:45function is located within my computer's
- 4:32:47memory
- 4:32:48and it's in hexadecimal think of it like
- 4:32:50a street address such as
- 4:32:51one two three fake street this is the
- 4:32:53address of where this function is within
- 4:32:55my computer's memory
- 4:32:56and each time that i run this program
- 4:32:58this number can change as you can see
- 4:33:00here
- 4:33:01now one thing that we could do we could
- 4:33:02assign this address to a variable
- 4:33:04let's say hi equals hello and be sure
- 4:33:06that you're not adding that set of
- 4:33:08parentheses afterwards because
- 4:33:09then you would be calling the hello
- 4:33:11function and returning the result to
- 4:33:12high
- 4:33:13so high equals hello no parentheses
- 4:33:16and if i was to print hi well
- 4:33:20the address of hello and hi will be at
- 4:33:22the same memory address
- 4:33:23both of these numbers are the same now
- 4:33:26what do you imagine would happen
- 4:33:27if i was to call the high function after
- 4:33:30we assigned the memory address of hello
- 4:33:32to hi
- 4:33:33well then what we'll end up doing is
- 4:33:34calling the hello function
- 4:33:36even though we're listing that we would
- 4:33:38like to call the
- 4:33:39high function even though it doesn't
- 4:33:41exist so it's as if this hello function
- 4:33:43has two names
- 4:33:44you can either use hello or you can use
- 4:33:46hi and let me demonstrate that
- 4:33:49so you can call either hello hi or both
- 4:33:52for this example
- 4:33:53that's because we're assigning the
- 4:33:55memory address of hello to this variable
- 4:33:57of high
- 4:33:58so we could treat high as a function
- 4:33:59it's kind of like
- 4:34:01an alias where this function has two
- 4:34:03names now here's another demonstration
- 4:34:05let's say that we would like to assign
- 4:34:07our built-in print function to
- 4:34:10a variable so let's say maybe say
- 4:34:13say equals print and when i say print
- 4:34:16i'm referring to the print function and
- 4:34:18be sure you're not adding that set of
- 4:34:19parentheses afterwards
- 4:34:20now if i need to print something to the
- 4:34:22console window i can either use
- 4:34:24print like what we've previously been
- 4:34:26doing or i could use
- 4:34:28say because say has the same memory
- 4:34:30address let's say
- 4:34:32perhaps whoa i can't
- 4:34:35believe this works
- 4:34:39surprise face all right so
- 4:34:42we can call the print function by saying
- 4:34:44the word say
- 4:34:45say all of this text and this will print
- 4:34:48all of this text
- 4:34:49well say all of this text to the console
- 4:34:51window
- 4:34:52under normal circumstances i'm not sure
- 4:34:54why or when
- 4:34:55you would need to assign the print
- 4:34:57function to a variable but
- 4:34:58you can and well that's kind of cool so
- 4:35:00i thought i'd show you guys that
- 4:35:02so everybody that's how to assign a
- 4:35:04function to a variable
- 4:35:06like i said we'll be building upon this
- 4:35:07topic so you want to be sure that you
- 4:35:09understand this
- 4:35:10all right then well if you would like a
- 4:35:12copy of this code i will post all of
- 4:35:14this to the comments section down below
- 4:35:15but yeah that's how to assign a function
- 4:35:18to a variable
- 4:35:19in python hey uh what's going on
- 4:35:22everybody
- 4:35:22it's bro hope you're doing well and in
- 4:35:24today's video i'm going to explain
- 4:35:26higher order functions in python so
- 4:35:28sit back relax and enjoy the show
- 4:35:33all right ladies and gentlemen let's
- 4:35:34talk about higher order functions
- 4:35:36these are functions that do one of two
- 4:35:38things one they either accept a function
- 4:35:40as an argument
- 4:35:42or two they return a function as output
- 4:35:44and in python this is totally allowed
- 4:35:46because functions are also treated as
- 4:35:48objects
- 4:35:48i'll give you guys an example of both of
- 4:35:50these but let's begin with number one
- 4:35:52here's an example of number one let's
- 4:35:54say that i have two functions
- 4:35:55named loud and quiet both of these
- 4:35:57functions will accept a string as an
- 4:35:59argument
- 4:36:00loud will return that string all
- 4:36:02uppercase as if we're shouting something
- 4:36:05quiet we'll return that text i'll
- 4:36:06lowercase as if we're whispering
- 4:36:08something
- 4:36:09now i'm going to create a third function
- 4:36:11called hello
- 4:36:13and this will be the higher order
- 4:36:14function it accepts a function as an
- 4:36:17argument
- 4:36:17i'm not sure if we're going to be
- 4:36:19passing in loud or quiet
- 4:36:21whatever it is i'm going to give it a
- 4:36:22name of funk
- 4:36:24short for function each of these
- 4:36:26functions will return some text
- 4:36:28either all uppercase or lowercase i'm
- 4:36:30going to assign that to
- 4:36:32text text equals the name of our
- 4:36:34function we're not sure what it's going
- 4:36:36to be
- 4:36:37and i will pass in some text in order to
- 4:36:39call one of these functions
- 4:36:41i will pass in just the word hello
- 4:36:42that's capitalized
- 4:36:44and at the end i will print whatever
- 4:36:46text that we have
- 4:36:47in order to call this function of hello
- 4:36:49i need to pass in a function as an
- 4:36:51argument
- 4:36:51do i want to pass in loud or quiet do i
- 4:36:54want the
- 4:36:55loud variety of hello where all of the
- 4:36:57text will be uppercase
- 4:36:58or the quiet variety of flow where all
- 4:37:00the text will be lowercase
- 4:37:02i would like to pass in loud because i
- 4:37:05want to make my text all uppercase
- 4:37:08so i'm typing the name of the higher
- 4:37:10order function and passing in
- 4:37:12a function as an argument and the result
- 4:37:14is that the word hello will be printed
- 4:37:16to the console window
- 4:37:17all uppercase now here's a rundown of
- 4:37:19what just happened
- 4:37:20we're calling the hello function and
- 4:37:21we're passing in loud
- 4:37:23as an argument we're naming loud as
- 4:37:26funk while we're within this hello
- 4:37:28function
- 4:37:29text equals loud and we're sending a
- 4:37:33string of text
- 4:37:34that says hello we're returning that
- 4:37:37text all uppercase
- 4:37:38assigning it to a variable called text
- 4:37:41and printing that text
- 4:37:42to the console window now if i would
- 4:37:44like to
- 4:37:45use the quiet variety of hello then i'm
- 4:37:47going to pass in
- 4:37:48quiet as an argument so quiet will make
- 4:37:52all of this text
- 4:37:53lowercase alright people so that's
- 4:37:55example
- 4:37:56one of a higher order function a higher
- 4:37:58order function
- 4:37:59is a function that accepts a function as
- 4:38:02an argument
- 4:38:03this hello function is an example of a
- 4:38:05higher order function
- 4:38:06because we're accepting either loud or
- 4:38:09quiet as arguments
- 4:38:10all right people let's move on to the
- 4:38:12second part of this definition for
- 4:38:14higher order functions
- 4:38:15a higher order function is a function
- 4:38:17that returns a function
- 4:38:18i'm going to give a different example
- 4:38:20this time let's say we have a pair of
- 4:38:22nested functions the outer function will
- 4:38:24be named divisor
- 4:38:26and we will accept a number as an
- 4:38:28argument that we will call x
- 4:38:30a divisor is a number that is used to
- 4:38:32divide
- 4:38:33another number when using division and
- 4:38:35inside this function we have an inner
- 4:38:37function
- 4:38:38named dividend dividend is the number
- 4:38:41that's going to be divided
- 4:38:42and we will call the argument that is
- 4:38:44passed in y
- 4:38:45all we'll do is return y divided by x
- 4:38:49we're dividing the dividend by the
- 4:38:51divisor y
- 4:38:52divided by x now within the
- 4:38:55outer function but not within the inner
- 4:38:57function we're going to return
- 4:39:00our dividend function so a higher order
- 4:39:02function is a function that returns a
- 4:39:04function
- 4:39:05divisor is a higher order function
- 4:39:07because we're returning
- 4:39:09dividend now if i would like to access
- 4:39:11this nested
- 4:39:12dividend function i first need to call
- 4:39:14the outside
- 4:39:15divisor function and pass in a number as
- 4:39:18an argument to serve as the divisor
- 4:39:20the divisor function is going to return
- 4:39:22my dividend function which we can then
- 4:39:24assign to a variable
- 4:39:26so the variable i'm going to name divide
- 4:39:29divide equals and i need to call divisor
- 4:39:32and set a divisor let's say that i would
- 4:39:35like to divide all numbers
- 4:39:37by two so i will set x to be two
- 4:39:40and now for the dividend i will
- 4:39:44print call the
- 4:39:47divide variable and pass in a number as
- 4:39:50the dividend the number that's going to
- 4:39:52be divided
- 4:39:52and let's say i would like to divide 10
- 4:39:55by two
- 4:39:56and this will work as you can see we
- 4:39:58have five in the console window
- 4:40:00so let me explain what just happened
- 4:40:01here so our program
- 4:40:03begins here divisor and we're passing in
- 4:40:06two
- 4:40:07x will be two and it will stay that way
- 4:40:09until we finish this program or until we
- 4:40:12reassign x
- 4:40:13now x equals two we're skipping this
- 4:40:16function because we did not call it yet
- 4:40:18we're returning dividend and assigning
- 4:40:21it to a variable
- 4:40:22and we can call a variable if it has the
- 4:40:24memory address of a function
- 4:40:26which is what we're doing in this line
- 4:40:28now we're calling
- 4:40:29dividend and passing in 10 so y equals
- 4:40:3210
- 4:40:33and x still equals to we're returning 10
- 4:40:36divided by 2 and printing it to the
- 4:40:38console window
- 4:40:40well everyone in conclusion a higher
- 4:40:42order function
- 4:40:43is a function that either one accepts a
- 4:40:46function as an argument
- 4:40:47or two they return a function the format
- 4:40:51is a little bit strange and we're not
- 4:40:52quite used to it yet
- 4:40:53but in future videos we'll have more
- 4:40:55practice with high order functions
- 4:40:57if you would like a copy of all this
- 4:40:58code i will post all of this to the
- 4:41:00comment section down below
- 4:41:02but yeah that is how higher order
- 4:41:03functions work in python
- 4:41:06yo what's going on everybody it's bro
- 4:41:08hope you're doing well in today's video
- 4:41:10i'm going to explain
- 4:41:11lambda functions in python so sit back
- 4:41:14relax
- 4:41:15and enjoy the show all right
- 4:41:19ladies and gentlemen let's talk about
- 4:41:20lambda functions these are functions
- 4:41:22that are written in one line and use
- 4:41:24this lambda keyword
- 4:41:26they accept any number of arguments but
- 4:41:28they only have one expression
- 4:41:30think of it as a shortcut they're useful
- 4:41:32if you need a function for only one use
- 4:41:34or a short period of time
- 4:41:35and then you're just planning on
- 4:41:36throwing away that function afterwards
- 4:41:38here's the syntax for a lambda function
- 4:41:40you type lambda
- 4:41:42you add the parameters followed by a
- 4:41:44colon and then your expression
- 4:41:45let's say we have this function named
- 4:41:47double double accepts one argument that
- 4:41:49we will name
- 4:41:50x we're returning x times two
- 4:41:52effectively doubling it
- 4:41:54so if i need to call this function i
- 4:41:56would type double
- 4:41:57and then pass in a number as an argument
- 4:41:59so the result if i pass in 5 would be
- 4:42:0110. now let's write the same function
- 4:42:03but instead write it as a
- 4:42:04lambda function so let me turn these
- 4:42:07lines into comments
- 4:42:09now the first step is to type lambda
- 4:42:12then our parameters we only have one
- 4:42:14parameter and that is x
- 4:42:16colon and then our expression we would
- 4:42:18like to return x
- 4:42:19times two now this lambda function is
- 4:42:21going to return a function object and we
- 4:42:23can assign that much like what we do
- 4:42:25with variables
- 4:42:26let's say double equals lambda x colon
- 4:42:29x times two and in order to call this
- 4:42:32lambda function
- 4:42:33we're going to type double then a set of
- 4:42:35parentheses
- 4:42:36to call this function and then pass in
- 4:42:38our arguments
- 4:42:39so let's say that x equals 5 and then
- 4:42:42we're going to double it
- 4:42:43and the result is 10. so that's the
- 4:42:45syntax for a lambda function you type
- 4:42:47lambda
- 4:42:48your parameters colon and then your
- 4:42:50expression this time let's say that we
- 4:42:52have two parameters
- 4:42:53we have two numbers that we want to
- 4:42:55multiply by each other
- 4:42:56so let's say that this function will be
- 4:42:59called multiply
- 4:43:00multiply equals lambda
- 4:43:03x comma y because we have two parameters
- 4:43:07colon followed by our expression we
- 4:43:10would like to multiply
- 4:43:11x times y and that is it
- 4:43:14now in order to call multiply we need to
- 4:43:16pass in two arguments
- 4:43:18if i were to pass in just one well we'll
- 4:43:20have a type error
- 4:43:21so let's pass in five and maybe six and
- 4:43:24the result
- 4:43:24is thirty now let's try three parameters
- 4:43:28let's say we would like to add three
- 4:43:30numbers together
- 4:43:31let's say add lambda x
- 4:43:35comma y comma z so we have all of our
- 4:43:38parameters
- 4:43:38we have our colon here and then we need
- 4:43:41our expression
- 4:43:42x plus y plus z and
- 4:43:46let's add five six and seven together
- 4:43:49the result is 18. now let's try
- 4:43:52something a little more complex let's
- 4:43:54pass in some strings this time
- 4:43:56i'll create a function named full name
- 4:43:58full name
- 4:43:59equals lambda
- 4:44:02first name comma
- 4:44:06last name the expression is going to be
- 4:44:09first name plus perhaps a space
- 4:44:12plus last name now i need to pass in a
- 4:44:16first name and a last name
- 4:44:19full name and you can use your own name
- 4:44:22for this example if you want
- 4:44:23let's say bro comma code
- 4:44:29yep this will print your first name and
- 4:44:31your last name to the console window
- 4:44:33now let's take it a step further let's
- 4:44:34say we would like to check somebody's
- 4:44:36age once using a lambda function we
- 4:44:38would like to check to see if they're 18
- 4:44:40or older because they're signing up for
- 4:44:41something perhaps let's say a credit
- 4:44:43card
- 4:44:43so let's create a function object age
- 4:44:47check equals lambda we have one
- 4:44:50parameter
- 4:44:51somebody's edge colon and then our
- 4:44:53expression
- 4:44:55let's type true if age is greater than
- 4:44:59or equal to
- 4:45:0018 else
- 4:45:03false to call this age track function
- 4:45:07we need to pass in an age let's say the
- 4:45:10user is 12
- 4:45:11and they're trying to sign up for a
- 4:45:13credit card while this will return false
- 4:45:15let's say they're 18 now and this will
- 4:45:18return
- 4:45:19true so that's a lambda function it's a
- 4:45:22function
- 4:45:22that is written in one line using this
- 4:45:24lambda keyword
- 4:45:25they accept any number of arguments but
- 4:45:27they only have one expression
- 4:45:29think of it like a shortcut and they're
- 4:45:31useful if you need to use a function
- 4:45:32only once and then you're just planning
- 4:45:34on throwing it away afterwards
- 4:45:36so if you would like a copy of all this
- 4:45:37code i will post all of this to the
- 4:45:39comment section down below
- 4:45:40but yeah those are lambda functions in
- 4:45:43python
- 4:45:44hey what's going on everybody it's bro
- 4:45:46hope you're doing well
- 4:45:47and in this video i'm going to show you
- 4:45:49all how we can sort iterables in python
- 4:45:51so sit back
- 4:45:52relax and enjoy the show
- 4:45:57well well well welcome back everybody in
- 4:45:59this video i'm going to show you all how
- 4:46:00we can sort data using python we'll
- 4:46:02begin with the sort method which is used
- 4:46:04with lists and the sort function which
- 4:46:06is used with iterables and that would
- 4:46:07include lists let's start with something
- 4:46:09very basic and then we'll crank up the
- 4:46:11difficulty one step at a time
- 4:46:13i have a list named students and i have
- 4:46:15a bunch of student names within this
- 4:46:17list
- 4:46:17we have squidward sandy patrick
- 4:46:19spongebob mr krabs
- 4:46:21let's say that all of these students are
- 4:46:23taking a college course
- 4:46:25perhaps on marine biology what i would
- 4:46:27like to do is to sort this list in
- 4:46:28alphabetical order
- 4:46:29and to do that we have a method that is
- 4:46:31built in with lists
- 4:46:33so type the name of your list students
- 4:46:35dot
- 4:46:36sort and it's easy as that and to
- 4:46:38display this list i think i'll use a for
- 4:46:40loop
- 4:46:40for i in students
- 4:46:44print i
- 4:46:48and there you go that's how to sort a
- 4:46:50list starting with the top we have mr
- 4:46:52krabs
- 4:46:52then patrick sandy spongebob and
- 4:46:54squidward the sort method of lists can
- 4:46:57accept keyword arguments
- 4:46:58there are two optional keyword arguments
- 4:47:00that we can pass in we can pass
- 4:47:02in key and or reverse we'll cover key a
- 4:47:05little bit later
- 4:47:06if we set reverse to true then our list
- 4:47:09will be sorted by reverse alphabetical
- 4:47:12order beginning with
- 4:47:13squidward spongebob sandy patrick then
- 4:47:16mr krabs
- 4:47:17now the sort method does not work with
- 4:47:19other iterables it's a built-in method
- 4:47:21for lists
- 4:47:22if our list of students was instead
- 4:47:25let's say a tuple
- 4:47:26well then the sort method is not going
- 4:47:28to work you can see here that
- 4:47:30we have an attribute error tuple object
- 4:47:32has no attribute of sort
- 4:47:34that's where the sort function would
- 4:47:36come in because that's useful for other
- 4:47:38iterables
- 4:47:38you can also use this for a list 2 i
- 4:47:40suppose the sort function will return a
- 4:47:42sorted list so let's use the sort
- 4:47:45function this time
- 4:47:47and we'll assign the result to a list
- 4:47:50called sorted students
- 4:47:53equals sorted and then we need to pass
- 4:47:57in
- 4:47:57an iterable and we have the option of
- 4:47:59passing in a key
- 4:48:01and or reverse the iterable that we'll
- 4:48:03pass in as an argument is our tuple
- 4:48:06named
- 4:48:06students so this line will be sorted
- 4:48:09students
- 4:48:10equals sorted students sorted students
- 4:48:13is a list the sorted function will
- 4:48:14return a list but it accepts an iterable
- 4:48:17as an argument
- 4:48:18and to display this we should change 4i
- 4:48:20and students
- 4:48:21to 4i in sorted students and now
- 4:48:25this function will accept our iterable
- 4:48:27our tuple
- 4:48:28and create a sorted list and all of
- 4:48:30these are in alphabetical order
- 4:48:32and to reverse this we can pass in the
- 4:48:35keyword argument of
- 4:48:36reverse equals true and now our list is
- 4:48:39sorted in
- 4:48:40reverse alphabetical order all right
- 4:48:42we're gonna take it up a level
- 4:48:44welcome to level two sometimes data
- 4:48:46isn't always as simple here we have a
- 4:48:48list of tuples each tuple has a
- 4:48:51corresponding student record
- 4:48:53we have a name a letter grade for their
- 4:48:55college course
- 4:48:56and the student's age now how can we
- 4:48:58sort this
- 4:48:59list of tuples by either the student's
- 4:49:02name
- 4:49:03their grade or their age well that's
- 4:49:05where the key
- 4:49:06keyword argument's going to come in with
- 4:49:08sorting if you take a look at this list
- 4:49:10of tuples it somewhat resembles a
- 4:49:12spreadsheet there's rows and then
- 4:49:14there's columns
- 4:49:15the first column corresponds to student
- 4:49:17names the second column
- 4:49:19are grades and the third column are all
- 4:49:21the ages of the students
- 4:49:23so by default sorting by the first
- 4:49:25column is actually really easy that's
- 4:49:27the default
- 4:49:28so if we need to sort alphabetically
- 4:49:30that would be the same process
- 4:49:31as before we would type the name of the
- 4:49:33list students
- 4:49:35dot sort but if we were to print
- 4:49:38this iterable for i in students
- 4:49:42instead of just the individual student
- 4:49:44names we're going to get
- 4:49:45each tuple that we have so now all of
- 4:49:48these tuples are arranged in
- 4:49:50alphabetical order starting with
- 4:49:52the first column that we have which are
- 4:49:54all the student names
- 4:49:56now how can we sort these iterables by
- 4:49:58their second column
- 4:49:59for this case it would be student grades
- 4:50:01or even the third column which would be
- 4:50:03the student ages
- 4:50:04well that's where the key keyword
- 4:50:07argument's going to come in
- 4:50:08this is a keyword argument and we set
- 4:50:10key equal to a function that's going to
- 4:50:13return
- 4:50:13the index of that specific column that
- 4:50:16we have
- 4:50:17so let's say key equals grid grade is
- 4:50:20going to be a function object
- 4:50:22grade equals and we can easily use a
- 4:50:26lambda expression for this lambda
- 4:50:29let's say grades colon
- 4:50:32grades and we will set an index of one
- 4:50:36now the first index is zero that's the
- 4:50:39first
- 4:50:39column because computers always start
- 4:50:41with zero column two would have an index
- 4:50:44of one
- 4:50:44and then column three would have an
- 4:50:47index of two
- 4:50:48so grade equals lambda grids
- 4:50:51colon grids index one you can also
- 4:50:54rename these if you want
- 4:50:55so students dot sort we're setting the
- 4:50:58key
- 4:50:59equal to grade and grade is a function
- 4:51:01object
- 4:51:02via a lambda function and now all of
- 4:51:05these students
- 4:51:05will be sorted by their grades starting
- 4:51:08with sandy
- 4:51:09then spongebob mr krabs patrick then
- 4:51:11squidward
- 4:51:12if this needs to be in reverse order we
- 4:51:15can pass in that other keyword argument
- 4:51:16of reverse
- 4:51:17equals true and for practice if you
- 4:51:21wanted to sort all of this data by each
- 4:51:23student's age
- 4:51:24then we would change grade to let's say
- 4:51:26age age equals lambda
- 4:51:28we'll change grades to maybe ages colon
- 4:51:31ages and the index would then be two
- 4:51:34because the first column here is zero
- 4:51:37then one
- 4:51:38then two and change key to our function
- 4:51:41object
- 4:51:42of edge now each student is
- 4:51:45sorted numerically beginning with the
- 4:51:47smallest edge
- 4:51:48well the youngest person starting with
- 4:51:50spongebob sandy patrick squidward then
- 4:51:52mr krabs
- 4:51:53and like i said before if you want your
- 4:51:56data arranged in reverse order you can
- 4:51:58set
- 4:51:58that keyword argument of reverse to true
- 4:52:01within the sort method
- 4:52:02and all of our student data is sorted
- 4:52:05beginning with the eldest student which
- 4:52:07is mr krabs
- 4:52:08then squidward patrick sandy and then
- 4:52:10spongebob
- 4:52:11and for the last part of this video
- 4:52:13let's say that we're working with
- 4:52:14some other iterable let's say we have a
- 4:52:17tuple of tuples instead
- 4:52:19well we can no longer use this sort
- 4:52:21method because that only belongs to
- 4:52:23lists
- 4:52:24you can also use the sort function with
- 4:52:25lists and it will generate a new sorted
- 4:52:27list without changing the placement of
- 4:52:29the original
- 4:52:30so let's say we have a tuple of tuples
- 4:52:33and this will create a
- 4:52:34new sorted list let's call this list
- 4:52:37sorted
- 4:52:38students equals then we'll use the
- 4:52:41sorted function we need to pass in an
- 4:52:43iterable and we can pass in
- 4:52:46both a key and or the reverse keyword
- 4:52:48argument
- 4:52:49so the iterable would be students
- 4:52:53and let's set the key equal to edge
- 4:52:56and i will not reverse it we can keep it
- 4:52:58as it is but let's change
- 4:53:00for i in students to sorted students
- 4:53:04and that's how to use the sorted
- 4:53:06function to sort
- 4:53:07an iterable including a list well all
- 4:53:10right everyone
- 4:53:11that's how to sort iterables in python
- 4:53:13if you would like a copy of all this
- 4:53:15code i will post all of this to the
- 4:53:16comments section down below
- 4:53:18but yeah that's a basic way to sort
- 4:53:20iterables
- 4:53:21in python hey what's going on everybody
- 4:53:24it's your bro hope you're doing well in
- 4:53:26today's video i'm going to show you all
- 4:53:27how the map function works in python so
- 4:53:30sit back relax and enjoy the show
- 4:53:34what up everybody let's discuss the map
- 4:53:36function the map function will apply a
- 4:53:39function to each item in an interval
- 4:53:41that would include lists tuples things
- 4:53:43like that
- 4:53:44and the map function accepts two
- 4:53:46arguments are iterable
- 4:53:48as well as the function we would like to
- 4:53:50apply to each item within our iterable
- 4:53:52for example let's say that we have an
- 4:53:54online store to represent this i have
- 4:53:56a list of tuples named store each tuple
- 4:53:59within this list
- 4:54:01corresponds to an item for sale within
- 4:54:03our store
- 4:54:04there's shirts pants jackets and socks
- 4:54:06and each item has
- 4:54:07a price in us dollars let's say now what
- 4:54:10i would like to do is to convert all of
- 4:54:12the prices within my store
- 4:54:14to euros and currently the conversion
- 4:54:16rate as of the filming of this video is
- 4:54:18one us dollar to 0.82 euros
- 4:54:22so let's begin we can easily convert
- 4:54:24dollars to euros with a
- 4:54:25lambda function so let's say two euros
- 4:54:29equals lambda the parameter will be data
- 4:54:34colon and the next part's a little bit
- 4:54:36tricky within a set of parenthesis to
- 4:54:38represent a tuple
- 4:54:39we'll take data at index 0 that
- 4:54:42corresponds to the first column
- 4:54:45comma data at index 1.
- 4:54:49this represents the index of our prices
- 4:54:52we're going to take
- 4:54:52data at index 1 times 0.82
- 4:54:57and that's it the first column will be
- 4:54:59left untouched
- 4:55:00but the data in column 1 will be
- 4:55:03multiplied by 0.82
- 4:55:05now our map function is going to create
- 4:55:07a map object but we can easily cast that
- 4:55:10to a different type of iterable
- 4:55:12so let's say that store underscore
- 4:55:16euros equals map
- 4:55:19parenthesis and then we can pass in our
- 4:55:21function as well as our iterable
- 4:55:23our function is our two euros function
- 4:55:27comma and our iterable is our store
- 4:55:31and that is it now if you want to
- 4:55:33convert this map object to an iterable
- 4:55:36you can easily surround this with a cast
- 4:55:38i would like to convert this map object
- 4:55:40to a list
- 4:55:42and we will now have a new store named
- 4:55:44store
- 4:55:45euros which will have all of the
- 4:55:47different prices of our store in euros
- 4:55:49and to display this i'll use a for loop
- 4:55:52for i
- 4:55:52in store euros
- 4:55:56print i and
- 4:56:00let's try it yep that seems about right
- 4:56:03all of the prices in our store are now
- 4:56:05in euros
- 4:56:06let's pretend that these prices are
- 4:56:08already in euros and we would like to
- 4:56:10convert these
- 4:56:10to american dollars so we will use the
- 4:56:13same conversion rate but divide
- 4:56:15by 0.82 so let's create a separate
- 4:56:18lambda function
- 4:56:19two dollars equals
- 4:56:22i think i'll just copy this but we are
- 4:56:25going to divide
- 4:56:26the first index of data by 0.82
- 4:56:30and let's say store dollars to represent
- 4:56:34our store
- 4:56:35in dollars because we're pretending now
- 4:56:37that it's currently in euros
- 4:56:41and we will display 4i in store dollars
- 4:56:45so if all of these prices are in euros
- 4:56:48we are now converting them to american
- 4:56:50dollars
- 4:56:51but you may want to do some rounding as
- 4:56:52opposed to the nearest descent
- 4:56:54well everyone that is the map function
- 4:56:56it applies a function to
- 4:56:58each item in an iterable those include
- 4:57:00lists tuples
- 4:57:01the like you pass in your iterable and
- 4:57:04your function as an argument to the map
- 4:57:07function so if you would like a copy of
- 4:57:09all this code i will post all of this to
- 4:57:11the comment section down below
- 4:57:12but yeah that is how the map function
- 4:57:14works in python
- 4:57:17what's going on everybody it's you bro
- 4:57:19hope you're doing well and in today's
- 4:57:21video i'm going to show you all how the
- 4:57:22filter function works in python
- 4:57:24so sit back relax and enjoy the show
- 4:57:30all right all right let's discuss the
- 4:57:32filter function
- 4:57:33the filter function creates a collection
- 4:57:35of elements from an
- 4:57:36iterable for which a function returns
- 4:57:39true
- 4:57:40now what the heck does that mean let me
- 4:57:42explain let's say that we have a list of
- 4:57:44tuples named
- 4:57:45friends each tuple has the name of a
- 4:57:48friend as well as their age
- 4:57:49i just made up some ages for all of
- 4:57:51these friends what i would like to do is
- 4:57:53to create a
- 4:57:53separate list for all the friends that
- 4:57:55are 18 or older
- 4:57:57so let's say that we're all going out
- 4:57:59drinking and i would like to create a
- 4:58:01list of my drinking buddies
- 4:58:02because they're of drinking age i'm in
- 4:58:04the united states and the drinking age
- 4:58:06is currently 21
- 4:58:07but i think worldwide is 18 so let's go
- 4:58:09with 18 for this example since
- 4:58:11i have a worldwide audience all right
- 4:58:13now with this filter function
- 4:58:15we have to pass in our iterable as well
- 4:58:17as a function
- 4:58:18to get anybody's edge that is over 18.
- 4:58:21so let's begin by writing a
- 4:58:22lambda function for this expression here
- 4:58:24our filter function
- 4:58:26so let's say age equals lambda
- 4:58:30data will be the parameter colon data
- 4:58:34at index one so the first index
- 4:58:38the first column in this chart here
- 4:58:40would be zero for other names the next
- 4:58:42index of one would be all the ages
- 4:58:45data at index one is greater than
- 4:58:48or equal to eighteen and that is it
- 4:58:51that is our lambda function and now
- 4:58:53we're going to filter
- 4:58:55our iterable by this function of age so
- 4:58:57let's write
- 4:58:59filter pass in our function
- 4:59:02which is edge comma and our iterable
- 4:59:06friends now this filter function will
- 4:59:08return a filter
- 4:59:10object you can easily cast this i would
- 4:59:11like to cast
- 4:59:13my friends list back into a list
- 4:59:17and i will assign this to a new list
- 4:59:19let's say
- 4:59:20drinking buddies
- 4:59:24equals list filter age
- 4:59:27and friends and i should be able to
- 4:59:30print
- 4:59:30all of the friends that are 18 or older
- 4:59:33for i
- 4:59:34in drinking bodies
- 4:59:38print i
- 4:59:42and we have rachel monica chandler and
- 4:59:44ross
- 4:59:45well everyone that is the filter
- 4:59:47function it creates a collection of
- 4:59:49elements
- 4:59:50from an iterable for which a function
- 4:59:52returns true
- 4:59:53i like to think of it as the search
- 4:59:55results we're searching for any results
- 4:59:57that meet this criteria where age is
- 5:00:00greater than or equal to 18
- 5:00:02and then you can create a separate
- 5:00:03collection or other list if you want
- 5:00:05from the results
- 5:00:06but yeah that is how the filter function
- 5:00:08works in python
- 5:00:10yo what's going on everybody it's bro
- 5:00:12hope you're doing well
- 5:00:13and in today's video i'm going to show
- 5:00:15you how the reduce function works in
- 5:00:17python so
- 5:00:18sit back relax and enjoy the show
- 5:00:22well i suppose we're on the reduce
- 5:00:24function today the reduce function
- 5:00:26applies a function of our choosing
- 5:00:28to an iterable and reduces that iterable
- 5:00:31to a single cumulative value the way
- 5:00:34this works is that the reduce function
- 5:00:36performs our function
- 5:00:37on the first two elements of our
- 5:00:39iterable and repeats the process
- 5:00:41until only one value remains i kind of
- 5:00:44like to think of it like
- 5:00:45we're recycling elements within an
- 5:00:47iterable until a single
- 5:00:49value remains our finished product so
- 5:00:51how is this useful let's say that
- 5:00:53we're playing a game scramble and we
- 5:00:55have a bunch of different letters within
- 5:00:57an iterable
- 5:00:58a list is a type of iterable so i have a
- 5:01:01list named letters and we have all of
- 5:01:03the letters we need to spell the word
- 5:01:05what i would like to do is to reduce all
- 5:01:07of these individual elements into a
- 5:01:09single cumulative value until only one
- 5:01:12value remains so we can do that using
- 5:01:14the reduce function you can also do this
- 5:01:16with the for loop too there's multiple
- 5:01:18ways of doing this
- 5:01:19so using the reduce function we're going
- 5:01:21to import
- 5:01:23func tools and now let's say we have
- 5:01:28our cumulative value stored within a
- 5:01:30variable named word
- 5:01:32word equals func tools dot reduce
- 5:01:36now the reduce function has two
- 5:01:38arguments the function we would like to
- 5:01:40apply
- 5:01:41to the first two elements and then our
- 5:01:43iterable
- 5:01:44the interval is easy that is just
- 5:01:45letters so let's put that in
- 5:01:48letters and for the function we can
- 5:01:50easily write a lambda function for this
- 5:01:52lambda and now this function needs two
- 5:01:55parameters
- 5:01:56let's say x and y now what kind of
- 5:02:00function do we want to perform on the
- 5:02:02first two elements of our iterable
- 5:02:04let's combine them together so let's say
- 5:02:07x
- 5:02:07plus y and now
- 5:02:10let's print our single cumulative value
- 5:02:14of word so this should print
- 5:02:18the word hello i'm fairly confident that
- 5:02:21the word hello is worth like
- 5:02:2220 points in scrabble now let me explain
- 5:02:24what happened step by
- 5:02:26step our reduce function applies our
- 5:02:28function
- 5:02:29to the first two elements within our
- 5:02:31iterable
- 5:02:32it performs this expression that we set
- 5:02:35x plus y but really we can write
- 5:02:37anything here
- 5:02:38so we perform this expression on the
- 5:02:41first two elements
- 5:02:43then we repeat the process again using
- 5:02:46the result
- 5:02:46from the first time we use this
- 5:02:48expression and the next element
- 5:02:50so we're combining x plus y again
- 5:02:54and then we're just going to repeat this
- 5:02:56process until only
- 5:02:58one value remains it's kind of like
- 5:03:00we're recycling
- 5:03:01all of these letters and creating a
- 5:03:03finished product out of all of these
- 5:03:05that's how i think of it at least here's
- 5:03:07a different example let's say that i
- 5:03:08would like to find
- 5:03:10the factorial of 5 so i have the numbers
- 5:03:135 through 1 within a list named
- 5:03:16factorial
- 5:03:17so if i was to find the factorial of 5
- 5:03:20via the reduce function
- 5:03:21i would probably write something like
- 5:03:23this so let me change
- 5:03:25word to let's say result result
- 5:03:28equals functools reduce now our lambda
- 5:03:31function will be
- 5:03:32x times y because with factorials we
- 5:03:36multiply
- 5:03:37the next two numbers together then we
- 5:03:40take that result
- 5:03:41and then multiply the next one in this
- 5:03:42list so the
- 5:03:44iterable will be factorial and
- 5:03:47let's print what our result is
- 5:03:51so the factorial of 5 should be 120. now
- 5:03:55let's walk through this we're taking
- 5:03:56the first two elements of this iterable
- 5:03:59five
- 5:03:59and four multiplying together in this
- 5:04:02case in the last example we concatenated
- 5:04:05the strings together of x and y
- 5:04:07in this example we're multiplying them
- 5:04:08together so we take
- 5:04:10the first two elements 5 times 4
- 5:04:13we take the result and apply it to the
- 5:04:15next iteration
- 5:04:17of x times y again so 20 times 3
- 5:04:20is 60. we repeat this process again
- 5:04:2460 times 2 is 120 and 120 times 1
- 5:04:28is 120 so that's kind of how the reduce
- 5:04:31function
- 5:04:32works it applies a function of our
- 5:04:34choosing to the first
- 5:04:36two elements of an iterable and repeats
- 5:04:38that process until
- 5:04:39only a single cumulative value remains
- 5:04:43so that is the reduce function if you
- 5:04:44would like a copy of all this code i
- 5:04:46will post all of this to the comment
- 5:04:48section down below
- 5:04:49but yeah that is how the reduce function
- 5:04:51works in python
- 5:04:54hey what's going on everybody it's your
- 5:04:56bro hope you're doing well and in this
- 5:04:58video i'm going to show you all how list
- 5:04:59comprehensions work in python so
- 5:05:02sit back relax and enjoy the show
- 5:05:06all right everybody so list
- 5:05:08comprehensions a list comprehension is a
- 5:05:10way to create a new list with
- 5:05:12less syntax you can also use a list
- 5:05:15comprehension to mimic
- 5:05:16certain lambda functions such as in the
- 5:05:18previous videos where we use
- 5:05:20the map and the filter functions and not
- 5:05:23only that but a list comprehension
- 5:05:24is easier to read than a lambda function
- 5:05:27but there is a formula that we're going
- 5:05:29to follow
- 5:05:29when we create our list within our list
- 5:05:32we're going to write an expression
- 5:05:34for item in iterable now let me give you
- 5:05:37an example of where a list comprehension
- 5:05:39would be useful
- 5:05:40let's take this program that will create
- 5:05:42a list of all the numbers
- 5:05:441 through 10 squared so if i were to
- 5:05:46print this we'll get the numbers 1
- 5:05:48through 10
- 5:05:48whatever their square is right so we
- 5:05:50have 1 through 10
- 5:05:52and the square of 10 is 100 so we wrote
- 5:05:55this program
- 5:05:56in three lines of code we're creating an
- 5:05:58empty list we're creating a for loop
- 5:06:00and then we're writing an expression for
- 5:06:01what we want each loop iteration to do
- 5:06:04now let's write the same program but
- 5:06:05used a list comprehension
- 5:06:07and we can write the same program with
- 5:06:09less syntax
- 5:06:10so i would write something like this and
- 5:06:12we're going to follow this formula
- 5:06:14so our list is going to be named
- 5:06:17squares equals then within a set of
- 5:06:21straight brackets
- 5:06:22we're going to write our expression
- 5:06:24which is this portion i times i
- 5:06:27for item so that would be 4i
- 5:06:31in our iterable that would be range
- 5:06:341 through 11 because remember 11's
- 5:06:38exclusive
- 5:06:39and then let's print our squares print
- 5:06:43squares and there you go
- 5:06:47we made the exact same program using a
- 5:06:49list comprehension
- 5:06:50so basically speaking you just follow
- 5:06:52this formula you set your list equal to
- 5:06:55your expression and our expression for
- 5:06:58this example is i times i
- 5:07:00for item for i in
- 5:07:03your interval in range one through
- 5:07:05eleven
- 5:07:06so this took three lines of code well if
- 5:07:09you exclude us printing
- 5:07:11our list and this program took one line
- 5:07:14of code
- 5:07:14now we can also use a list comprehension
- 5:07:17to mimic certain lambda functions i
- 5:07:19wrote a program
- 5:07:20where we will be filtering a list of
- 5:07:22student grades
- 5:07:23let's pretend that all of these students
- 5:07:25took some sort of exam
- 5:07:26so we have one student that received a
- 5:07:28100 90 80 70 60 50 40 30
- 5:07:32and then one student got a zero because
- 5:07:34they didn't show up so what i would like
- 5:07:35to do is to filter
- 5:07:36all of these student grades into a list
- 5:07:39that only contains
- 5:07:40students that passed and the criteria
- 5:07:42for a passing grade will be
- 5:07:44a 60 or above so i would like to filter
- 5:07:48all passing students and if i was to
- 5:07:50write this program using the filter
- 5:07:52function i would probably write
- 5:07:53something like this
- 5:07:54so i need a function and then my list
- 5:07:56that i'm working with
- 5:07:57lambda x colon x is greater than or
- 5:08:01equal to 60
- 5:08:02will filter all of the results and then
- 5:08:04convert it to a list named past students
- 5:08:06so the result will look something like
- 5:08:08this i have five student grades in here
- 5:08:11with 100 90 80 70 and 60.
- 5:08:14now let's write the same program using a
- 5:08:16list comprehension
- 5:08:17now there's just one portion to this
- 5:08:19formula for list comprehension that
- 5:08:20we're going to add
- 5:08:21at the end we're going to add if
- 5:08:23conditional we can check
- 5:08:25some sort of condition after each
- 5:08:27iteration so
- 5:08:28let's write the same program using a
- 5:08:30list comprehension following
- 5:08:31this new updated formula that we have so
- 5:08:34we need
- 5:08:34our list which will be named past
- 5:08:38students equals first comes our
- 5:08:41expression
- 5:08:41and it's just i for this example for
- 5:08:44item
- 5:08:45i in our iterable
- 5:08:48of students and then if we can check our
- 5:08:51conditional
- 5:08:52if i is greater than or equal to 60
- 5:08:56and this will have the same effect as
- 5:08:59our previous program but instead it uses
- 5:09:01a list comprehension
- 5:09:03now if your program has multiple output
- 5:09:06if you need an else statement within
- 5:09:07your conditional
- 5:09:08you're actually going to move this
- 5:09:09portion of the if conditional within
- 5:09:12our formula to right after the
- 5:09:13expression so let's write
- 5:09:15an if else statement here
- 5:09:18so let's say that instead of just
- 5:09:20excluding any student that didn't pass
- 5:09:22we'll replace their grade with the word
- 5:09:24failed and we can do that
- 5:09:26using an if else statement so let's do
- 5:09:28this again i'm going to copy what we
- 5:09:30have here
- 5:09:31paste it and we'll move our conditional
- 5:09:33from the end
- 5:09:35to just after the expression portion i
- 5:09:39if i is greater than or equal to 60 and
- 5:09:41you can see here that we need to add an
- 5:09:43else statement else will return
- 5:09:46the word failed
- 5:09:51so we're replacing each instance of
- 5:09:54a failing grade with the word failed
- 5:09:56instead so if your condition
- 5:09:58returns whatever i is you can just add
- 5:10:00that to the end
- 5:10:01if you need an if else statement you're
- 5:10:02going to add it right after the
- 5:10:04expression
- 5:10:05so everybody in conclusion a list
- 5:10:07comprehension is a way to create a new
- 5:10:10list
- 5:10:10with less syntax you can even mimic
- 5:10:13certain lambda functions which we did
- 5:10:14with the
- 5:10:15filter function and it has the added
- 5:10:17benefit of being easier to read than a
- 5:10:19lambda function
- 5:10:21so you just follow one of these formulas
- 5:10:22depending on what you're trying to do
- 5:10:24so you need at least list equals your
- 5:10:27expression
- 5:10:28for item and your interval if you have a
- 5:10:30condition that you want to check
- 5:10:31you just add that to the end of your
- 5:10:33list comprehension if you have an if
- 5:10:35else statement
- 5:10:36then you'll add that to right after the
- 5:10:38expression so you just follow one of
- 5:10:40these formulas depending on how you want
- 5:10:42to write your program
- 5:10:43so that is a list comprehension if you
- 5:10:45would like a copy of this code i will
- 5:10:47post all of this to the comments section
- 5:10:48down below and well
- 5:10:50yeah that's how list comprehensions work
- 5:10:52in python
- 5:10:54yo what's going on everybody it's bro
- 5:10:56hope you're doing well
- 5:10:57and in this video i'm going to explain
- 5:10:59dictionary comprehensions in python so
- 5:11:01sit back relax and enjoy the show
- 5:11:06okay people dictionary comprehensions
- 5:11:09they're very similar to list
- 5:11:10comprehensions except
- 5:11:11they're with dictionaries that's it
- 5:11:13video over okay but seriously a
- 5:11:15dictionary comprehension is a way to
- 5:11:17create dictionaries using an expression
- 5:11:19and they can replace for loops and
- 5:11:21certain lambda functions
- 5:11:22and all we have to do is follow this
- 5:11:24formula dictionary equals
- 5:11:26key colon our expression for key value
- 5:11:29in iterable so let's go over a few
- 5:11:31examples our first example we're going
- 5:11:33to take
- 5:11:34this dictionary named cities in f f is
- 5:11:37short for fahrenheit
- 5:11:38i have different city names as keys and
- 5:11:41relative temperatures as
- 5:11:42values in fahrenheit so new york is 32
- 5:11:46degrees fahrenheit boston is 75.
- 5:11:48los angeles is 100 and chicago is 50.
- 5:11:51and what we'll be doing is creating a
- 5:11:53separate dictionary where all of these
- 5:11:55temperatures will be in celsius
- 5:11:57using a dictionary comprehension so
- 5:12:00let's follow this formula let's create a
- 5:12:02new name for this dictionary
- 5:12:03let's say cities in c short for celsius
- 5:12:07equals and we will follow this pattern
- 5:12:10first our key
- 5:12:11golden then our expression we'll go back
- 5:12:13to this in just a moment
- 5:12:15for key comma
- 5:12:18value in our iterable
- 5:12:21of cities in f and since we're working
- 5:12:25with a dictionary we're going to use
- 5:12:27the items method now we just need to
- 5:12:29fill in this expression
- 5:12:31there is a formula to convert fahrenheit
- 5:12:33to celsius and it should be on the
- 5:12:34screen right about now
- 5:12:36so let's follow this formula so we'll
- 5:12:38take our value
- 5:12:40minus 32 and just for good measure i'm
- 5:12:43going to put these within parentheses
- 5:12:45and we will multiply all of this by
- 5:12:495 divided by 9 and that is it so we have
- 5:12:52our key
- 5:12:54our expression to convert fahrenheit to
- 5:12:56celsius
- 5:12:58for key value in our iterable of
- 5:13:01cities in f our previous dictionary and
- 5:13:04let's test this by printing our new
- 5:13:07dictionary
- 5:13:08cities in c
- 5:13:12actually i think i'm going to round
- 5:13:14these numbers just so it's more readable
- 5:13:16so let's round all this there we go so
- 5:13:19new york and celsius is zero
- 5:13:21boston is 24. los angeles is 38 and
- 5:13:24chicago is 10.
- 5:13:26so we created a new dictionary using a
- 5:13:29dictionary comprehension
- 5:13:30now with these dictionary comprehensions
- 5:13:32you can add an if conditional to the end
- 5:13:34of this
- 5:13:35so let's say that we have a separate
- 5:13:37dictionary of weather
- 5:13:38like a description of the weather in
- 5:13:40each city new york is snowing
- 5:13:42boston is sunny los angeles is sunny and
- 5:13:44chicago is cloudy
- 5:13:46let's say that we would like to create a
- 5:13:47separate dictionary with a dictionary
- 5:13:50comprehension
- 5:13:51that only has cities where the weather
- 5:13:53is sunny
- 5:13:54so i would write something like this
- 5:13:57let's say sunny
- 5:13:58weather will be the name of our
- 5:13:59dictionary equals and we'll follow
- 5:14:02this formula and then we just tack on
- 5:14:04our conditional to the end so it's kind
- 5:14:06of the same as before really
- 5:14:08so key colon and we don't really have an
- 5:14:11expression here so let's just say
- 5:14:14value then for
- 5:14:19key comma value
- 5:14:22in our iterable of
- 5:14:25weather dot items
- 5:14:28then our conditional if value
- 5:14:33sunny because we are only looking for
- 5:14:35sunny weather
- 5:14:37and let's print this print
- 5:14:40our dictionary of sunny weather
- 5:14:44and let me just make this more readable
- 5:14:45for you guys and our new dictionary
- 5:14:47comprehension
- 5:14:48will create a dictionary of key values
- 5:14:51where the value
- 5:14:52is sunny using an if conditional at the
- 5:14:55end of our dictionary comprehension
- 5:14:57here's a third example for you well if
- 5:14:59you have an if else condition you can
- 5:15:00add that to where the expression is
- 5:15:02within your dictionary comprehension
- 5:15:04so i'm going to reuse the previous
- 5:15:06dictionary for the first example where
- 5:15:08we have
- 5:15:09cities and their temperatures as values
- 5:15:11so we're going to replace each
- 5:15:13temperature with a description of the
- 5:15:15weather
- 5:15:16is it warm or is it cold so let's do
- 5:15:18that using
- 5:15:19a dictionary comprehension that contains
- 5:15:21an if else statement
- 5:15:22so let's say we have a new dictionary
- 5:15:24named desk short for description
- 5:15:27cities it's a brief description of each
- 5:15:29city's temperature
- 5:15:31so we follow this formula key
- 5:15:34then our if else conditional for key
- 5:15:37value
- 5:15:38and iterable actually i probably can
- 5:15:40copy all this
- 5:15:41just to save some time okay so our
- 5:15:44iterable
- 5:15:45will be cities dot items
- 5:15:49and our conditional is going to be
- 5:15:53we will return warm if
- 5:15:56our value is greater than or equal to
- 5:16:00let's say 40. else
- 5:16:04cold and we will print
- 5:16:07our new dictionary of desk cities
- 5:16:11description of cities so new york is
- 5:16:14cold because it's 32 that's below 40.
- 5:16:17boston is warm los angeles is warm and
- 5:16:19chicago is warm
- 5:16:21now if your condition gets somewhat
- 5:16:22complex you can even call a separate
- 5:16:25function to keep your code more
- 5:16:26organized
- 5:16:27so key then we can call a function and
- 5:16:29pass in a value
- 5:16:30for key value in iterable so i'm going
- 5:16:32to again
- 5:16:33reuse our city names and our
- 5:16:35temperatures
- 5:16:36and this time we are going to call a
- 5:16:39function instead
- 5:16:40i think i'll reuse this code from the
- 5:16:41previous example where we have a
- 5:16:43separate dictionary named
- 5:16:44desk cities and i'm going to replace
- 5:16:47this if else statement
- 5:16:48with a function so let's say we have a
- 5:16:51function that is named
- 5:16:52check temp and we will pass in our value
- 5:16:56and we just need to define this function
- 5:16:58so let's define that here
- 5:17:00so let's define check
- 5:17:03temp and our parameter is our value that
- 5:17:06we're currently working on within our
- 5:17:08dictionary
- 5:17:09value then we'll write if
- 5:17:12value is greater than or equal to
- 5:17:16let's say anything above how about 70
- 5:17:19will return the word hot
- 5:17:24then else if
- 5:17:27let's say 69 greater than or equal to
- 5:17:32value and then
- 5:17:35value is greater than or equal to
- 5:17:39how about 40 then we will
- 5:17:43return the word warm
- 5:17:46and lastly else return
- 5:17:51cold and let me just fix some of the
- 5:17:53spacing
- 5:17:56now depending on the temperature that is
- 5:17:58stored within each value of our
- 5:18:00dictionary
- 5:18:01we will call a function that will return
- 5:18:04one of a few different outputs
- 5:18:06so it's kind of like we're using the map
- 5:18:08function the map lambda function
- 5:18:11so let's print our description
- 5:18:14of cities and we should get a short
- 5:18:16description of the temperature in each
- 5:18:18city
- 5:18:19after we call the check temperature
- 5:18:21function and then pass in each value
- 5:18:23from our dictionary
- 5:18:24so new york is cold because it's 32
- 5:18:27degrees fahrenheit
- 5:18:28boston is hot because it's above 70. los
- 5:18:31angeles is hot
- 5:18:32because it's 100 and then chicago is
- 5:18:34warm because that is 50 which is between
- 5:18:3669 and 40 within our function
- 5:18:40so that is a dictionary comprehension
- 5:18:42it's a shortcut where you can create
- 5:18:44dictionaries using an expression
- 5:18:46and they can replace for loops and
- 5:18:48certain lambda functions
- 5:18:49so if you would like a copy of all this
- 5:18:51code i will post all of this to the
- 5:18:53comment section down below
- 5:18:54and well yeah that's how dictionary
- 5:18:56comprehensions work
- 5:18:57in python hey what's going on everybody
- 5:19:00it's bro hope you're doing well and in
- 5:19:02today's video i'm going to show you how
- 5:19:04the zip function works in python so
- 5:19:06sit back relax and enjoy the show
- 5:19:11all right ladies and gentlemen welcome
- 5:19:13back we're talking about the zip
- 5:19:14function today
- 5:19:16the zip function will aggregate elements
- 5:19:18from two or more iterables
- 5:19:20iterables are those things like lists
- 5:19:23tuples
- 5:19:23sets etc and the zip function will
- 5:19:26create a
- 5:19:26zip object with paired elements from
- 5:19:29each iterable
- 5:19:30stored in a tuple for each element
- 5:19:33within our zip object
- 5:19:34here's an example let's say that we have
- 5:19:37two different types of iterables
- 5:19:38i have a list of usernames and a few
- 5:19:41usernames within here are
- 5:19:43dude bro and mr and i have a
- 5:19:46tuple of passwords and i have some very
- 5:19:48secure passwords
- 5:19:49such as password abc123
- 5:19:53and guest what i would like to do is to
- 5:19:55zip elements from each iterable together
- 5:19:58so that they're in pairs and each pair
- 5:20:00is going to be stored as a tuple within
- 5:20:03a zip object and here's how we can do
- 5:20:05that let's say that we will create a zip
- 5:20:07object called users
- 5:20:09and we will use the zip function the zip
- 5:20:11function will take a
- 5:20:13varying amount of iterables we're going
- 5:20:14to pass in our usernames
- 5:20:16and passwords and zip them together so
- 5:20:18let's pass in usernames
- 5:20:20as well as passwords and now our zip
- 5:20:23object of users
- 5:20:24is actually iterable zip objects are
- 5:20:26iterable so we can use them within a for
- 5:20:28loop
- 5:20:28so let's type for i in users
- 5:20:32and print i
- 5:20:36and what we get is that we have a zip
- 5:20:39object
- 5:20:39of tuples and each tuple is storing each
- 5:20:42pair of elements from our
- 5:20:44two iterables now users is a
- 5:20:47zip object and if you don't believe me
- 5:20:49let me prove it i'm going to print
- 5:20:51the type of users and this will print
- 5:20:54that users is indeed a zip object
- 5:20:58but you can easily convert this to a
- 5:21:00different type of iterable by using a
- 5:21:02cast
- 5:21:03let's say that we would like to convert
- 5:21:04our zip object into a list
- 5:21:07so we'll surround the zip function with
- 5:21:09a cast to a list
- 5:21:10and now the data type of users is now a
- 5:21:13list
- 5:21:14what we have is a list of tuples and
- 5:21:16each tuple is storing a
- 5:21:18pair of elements from our two iterables
- 5:21:21of usernames and passwords
- 5:21:22now currently since we're passing in
- 5:21:24only two different iterables we can
- 5:21:26easily make this
- 5:21:27a dictionary so that these are key value
- 5:21:30pairs
- 5:21:31so let's cast our zip object as a
- 5:21:33dictionary
- 5:21:34and to display all of the elements
- 5:21:36within our dictionary all the key value
- 5:21:38pairs
- 5:21:38we're going to change our for loop to
- 5:21:40this for key value
- 5:21:43in users dot and we will use the
- 5:21:46items method print key
- 5:21:50comma value actually i think i'm going
- 5:21:53to separate each of these with
- 5:21:54a colon just to make it more readable
- 5:21:58and now when we zip these two intervals
- 5:22:00together
- 5:22:01we end up with a dictionary of usernames
- 5:22:04and passwords and the name of this
- 5:22:06dictionary is users
- 5:22:08now you're not limited to just two
- 5:22:11iterables you can add
- 5:22:12a third iterable or more so this time
- 5:22:14let's create a
- 5:22:16maybe a list a list of last login dates
- 5:22:19and i'll just call this
- 5:22:20login date equals and why not make a
- 5:22:23list
- 5:22:24and let's make up some dates let's say 1
- 5:22:271
- 5:22:27dash 20 21
- 5:22:3212-20-21
- 5:22:34and 1-3-20
- 5:22:3821 okay so let's create
- 5:22:42a zip object of users and we're going to
- 5:22:46zip
- 5:22:48user names passwords
- 5:22:52and login date
- 5:22:56and let's iterate over this for i
- 5:23:00in users print
- 5:23:03i now we have a tuple for each element
- 5:23:08and instead of a pair we now
- 5:23:10have a trio i guess of all of the
- 5:23:13different elements
- 5:23:14from each iterable so in conclusion
- 5:23:17the zip function will aggregate elements
- 5:23:19from two or more
- 5:23:20iterables and create a zip object with
- 5:23:23paired or grouped together elements
- 5:23:25stored in a
- 5:23:26tuple for each element within our zip
- 5:23:29object
- 5:23:30so that is the zip function if you would
- 5:23:32like a copy of this code i will post
- 5:23:34this to the comment section down below
- 5:23:36and well yeah that's how the zip
- 5:23:38function works in python
- 5:23:41hey what's going on everybody it's you
- 5:23:43bro hope you're doing well
- 5:23:44and i'm going to explain the purpose of
- 5:23:46if name equals main
- 5:23:48in python so sit back relax
- 5:23:51and enjoy the show
- 5:23:54during your programming journey you may
- 5:23:56have encountered this strange statement
- 5:23:58of
- 5:23:58if name equals main now what the heck
- 5:24:01does that mean
- 5:24:02so with python files also referred to as
- 5:24:05modules by including this statement it
- 5:24:08gives our modules some flexibility
- 5:24:10one a module that has the statement can
- 5:24:12be run as a standalone program
- 5:24:15or two this module can be imported
- 5:24:18and used by other modules if there's
- 5:24:20some sort of useful functions
- 5:24:22or other resources within this module to
- 5:24:24be imported
- 5:24:25by including the statement of if name
- 5:24:27equals main we're checking to see if a
- 5:24:30user
- 5:24:30is running this module as either a
- 5:24:32standalone program
- 5:24:34or they're importing it from another
- 5:24:36module behind the scenes
- 5:24:38the python interpreter will set special
- 5:24:40variables
- 5:24:41one of which is double underscore name
- 5:24:44it's a variable
- 5:24:45and python will assign the name variable
- 5:24:48a value
- 5:24:49of main if it's the initial module being
- 5:24:52run
- 5:24:52and we can actually test that by
- 5:24:54printing double
- 5:24:56underscore name so since this is the
- 5:24:59initial module being run
- 5:25:01module one for my example name is going
- 5:25:04to be assigned a value
- 5:25:05a string of main now check this out i
- 5:25:09have two modules what if i were to
- 5:25:12import module 2 and check the name
- 5:25:14variable of module 2.
- 5:25:16let's try it import module
- 5:25:20two
- 5:25:23in order to access a variable from
- 5:25:26another module i need to type the name
- 5:25:28of the module
- 5:25:29module two dot and i would like to check
- 5:25:32the special name variable of module 2
- 5:25:35and print it
- 5:25:37so when you import a module this name
- 5:25:40variable is going to be assigned
- 5:25:41the name of the module in this case
- 5:25:43module 2.
- 5:25:44this time what if we change the roles
- 5:25:46around i'm going to copy all of this
- 5:25:48text
- 5:25:49and paste it within module two we're
- 5:25:52going to
- 5:25:52import from module one we will
- 5:25:56print that special name variable of
- 5:25:59this module module 2 and then print the
- 5:26:02name variable
- 5:26:03of module 1 which is being imported now
- 5:26:06when i run this program i'm going to
- 5:26:08instead
- 5:26:09run from module 2.
- 5:26:12now this time that special name variable
- 5:26:15of module 2 is main and the special name
- 5:26:19variable of module 1
- 5:26:21is module 1 the name of the module
- 5:26:24so by including a statement such as
- 5:26:27if name equals main we're checking to
- 5:26:30see if this module is being run
- 5:26:32directly or indirectly so let's test it
- 5:26:36within this statement of if name equals
- 5:26:39main
- 5:26:39let's print running this
- 5:26:43module directly
- 5:26:47else that must mean that we're running
- 5:26:49this module
- 5:26:50indirectly so let's print
- 5:26:54running other
- 5:26:57module indirectly
- 5:27:00okay so i'm going to go to module 1
- 5:27:03and run from here running this module
- 5:27:07directly
- 5:27:08now let's do the same thing with module
- 5:27:10two but we need to
- 5:27:12import module
- 5:27:16one and let's run for module two
- 5:27:24running other module indirectly for the
- 5:27:27time being i'm just going to write pass
- 5:27:28within
- 5:27:29our if name equals main as a placeholder
- 5:27:32and get rid of our if statement
- 5:27:33so let's say within module 1 there's a
- 5:27:36useful function or resource we would
- 5:27:38like to access
- 5:27:39from module 2. so let's say we have a
- 5:27:41function named hello
- 5:27:43and all this will do is print the word
- 5:27:46hello
- 5:27:47let's just pretend that it's a useful
- 5:27:49function not really but
- 5:27:50let's pretend so in order to use this
- 5:27:54function from module two i need to
- 5:27:56import module one
- 5:27:58then type module one dot and
- 5:28:02then the name of the function so i'm
- 5:28:04going to run
- 5:28:05from module 2 and this will print
- 5:28:08hello but what if i run this program
- 5:28:11from module 1.
- 5:28:13so right now module 1 cannot be run
- 5:28:16directly as a standalone program
- 5:28:18so i cannot print the word hello so what
- 5:28:21i could do
- 5:28:22is within if name equals main i can
- 5:28:25call that hello function directly
- 5:28:28so let's run from module one
- 5:28:31hello one other thing that you might see
- 5:28:34too within
- 5:28:35the if statement if name equals main
- 5:28:37there might be a call for
- 5:28:39a main function and you can write the
- 5:28:41main body of your program within
- 5:28:43a main function so everybody in
- 5:28:46conclusion
- 5:28:47the reason that people may include this
- 5:28:49statement if name
- 5:28:50equals main is that it allows our
- 5:28:52modules to have some flexibility
- 5:28:54they can be run as a standalone program
- 5:28:57or they can be imported and used by
- 5:28:59other modules
- 5:29:00and this is because the python
- 5:29:02interpreter sets
- 5:29:03that special variable of name with a
- 5:29:06value
- 5:29:07of main if it's the initial module being
- 5:29:10run
- 5:29:10if you would like a copy of my code and
- 5:29:12my notes i will post all of this to the
- 5:29:14comment section down below
- 5:29:16but yeah that's the purpose of if name
- 5:29:19equals main
- 5:29:19in python hey what's going on everybody
- 5:29:23it's bro hope you're doing well and in
- 5:29:25this video i'm going to show you all a
- 5:29:26few useful functions related to
- 5:29:28times and dates using the time module in
- 5:29:31python so
- 5:29:32sit back relax and enjoy the show
- 5:29:37alright everybody so the time module
- 5:29:39let's begin by importing time
- 5:29:41and the first thing that i'm going to
- 5:29:42explain to you while is how we can find
- 5:29:44our computer's epic
- 5:29:46also pronounced as epoch so this is a
- 5:29:49date and time
- 5:29:50in which your computer thinks time began
- 5:29:52think of it that way at least
- 5:29:54so we use our epic as a reference point
- 5:29:58so to find your computer's epic it's
- 5:29:59going to vary based on your computer and
- 5:30:01your operating system
- 5:30:02so to find your computer's epic type
- 5:30:04time dot
- 5:30:06c time and as an argument we will pass
- 5:30:08in zero
- 5:30:10now what this method will do is that
- 5:30:12this method will convert a time
- 5:30:14expressed in seconds and convert it to a
- 5:30:16readable string
- 5:30:18so if i was to pass in 0 and print it
- 5:30:20well then
- 5:30:21this will display the date and time
- 5:30:23which is my epic
- 5:30:25our reference point so for me my epic is
- 5:30:28wednesday december 31st 6 p.m
- 5:30:311969 so i've just added a note
- 5:30:34that the c time method will convert a
- 5:30:37time expressed in seconds
- 5:30:39since epic and convert it to a readable
- 5:30:42string a readable
- 5:30:44date and time so for practice let's pass
- 5:30:47in perhaps
- 5:30:471 million seconds and see what date and
- 5:30:50time that we receive
- 5:30:51so our c time method will return a date
- 5:30:54and time
- 5:30:55one million seconds past this epic here
- 5:30:59so that would be for me monday january
- 5:31:0212th
- 5:31:02about 7 a.m 1970. our next method is
- 5:31:06the time method of the time module let's
- 5:31:09print
- 5:31:10time dot time method and what this
- 5:31:12method will do
- 5:31:13is return the current seconds that have
- 5:31:16passed
- 5:31:16since our epic using our computer's
- 5:31:19clock
- 5:31:20so the number that i end up with is just
- 5:31:23over
- 5:31:231.6 billion and this is in seconds
- 5:31:27so for me 1.6 billion seconds
- 5:31:30has passed since that date which is my
- 5:31:33epic
- 5:31:33i believe it was december 31st 1969
- 5:31:37and every time that i rerun this program
- 5:31:39you can see that the amount of seconds
- 5:31:41that has passed is actually increasing
- 5:31:44so
- 5:31:45that is the time method it will return
- 5:31:47the current seconds
- 5:31:49since you're epic using your computer's
- 5:31:51clock and you know what
- 5:31:52just for fun let's change the date and
- 5:31:55time under clock and see what happens
- 5:31:57so let's change the year to
- 5:32:00how about the year 2000 change
- 5:32:04and close so let's see what number we
- 5:32:07get with the time method now
- 5:32:09so we get just under 1 billion 948
- 5:32:14million seconds have passed
- 5:32:16and now if you need to retrieve the
- 5:32:18current date and time
- 5:32:19well there's one of a few ways in which
- 5:32:21you could retrieve that but one way is
- 5:32:23that we can combine
- 5:32:24both of these methods of the see time
- 5:32:27method and the time method of the time
- 5:32:29module
- 5:32:30so let's print time dot c
- 5:32:33time and we're going to pass in an
- 5:32:35amount of seconds
- 5:32:36into the c time method as an argument so
- 5:32:39within
- 5:32:40the c time method we're going to call
- 5:32:42the time method
- 5:32:44so the time method will return an amount
- 5:32:46of seconds
- 5:32:47since our epic and the c time method
- 5:32:49will convert
- 5:32:50that amount of seconds to a readable
- 5:32:52date and time
- 5:32:53so the current date and time is saturday
- 5:32:57january 23rd about 3 p.m 20 21
- 5:33:00now there is more than one way to get
- 5:33:02the current date and time
- 5:33:03another way is to use the local time
- 5:33:06method and the local time method will
- 5:33:08create a
- 5:33:09time object based on the current time so
- 5:33:12what i'm going to do is
- 5:33:14create a variable called time object
- 5:33:17and i just need to explain a few things
- 5:33:19about time objects
- 5:33:20so to best explain this i'm going to
- 5:33:22print our time
- 5:33:23object now a time object is also
- 5:33:26referred to as a
- 5:33:27struct time object it is made up of
- 5:33:30different keyword arguments
- 5:33:32there's a year a month day
- 5:33:35hour minutes seconds day of the week
- 5:33:39day of the year and this keyword
- 5:33:41argument here has something to do with
- 5:33:42daylight savings time
- 5:33:44so there's quite a few uses with time
- 5:33:46objects
- 5:33:47and one way is that we can format them
- 5:33:50however we want because right now
- 5:33:52this time object is not in a readable
- 5:33:54format
- 5:33:55so to convert this time object into a
- 5:33:58readable string we'll need the help of a
- 5:34:00separate function
- 5:34:01and that is the strf time function
- 5:34:05str is short for string f4 format and
- 5:34:08time
- 5:34:09well for time i guess so this function
- 5:34:12needs two arguments a format and a time
- 5:34:15object so our strf time function will
- 5:34:18accept
- 5:34:19a format and a time object as an
- 5:34:22argument
- 5:34:23so our format is really just a string
- 5:34:26of different directives and to best
- 5:34:28explain these i'm going to head to
- 5:34:30python's official documentation on the
- 5:34:32subject
- 5:34:33so here i am on python's website
- 5:34:35regarding the time module and underneath
- 5:34:38this section on the strf time function
- 5:34:41there are different directives that we
- 5:34:43can embed within our format string that
- 5:34:45we pass in as an argument
- 5:34:47depending on the directive that we add
- 5:34:49we can display
- 5:34:50a certain format of our date and time
- 5:34:54so for example if i was to pass in let's
- 5:34:57say
- 5:34:57percent lowercase a then we will display
- 5:35:01the time object's weekday name and
- 5:35:04you're not limited to just one directive
- 5:35:05you can add any combination of
- 5:35:07directives
- 5:35:08so if i was to add percent m well we
- 5:35:11would display
- 5:35:12the month of our date time object as a
- 5:35:14number
- 5:35:151 through 12. so there's a bunch of
- 5:35:17directives here and i'm going to be
- 5:35:18using some of these
- 5:35:20so within a string for the format
- 5:35:22argument i'm going to pass in
- 5:35:24percent capital b for the name of the
- 5:35:27month
- 5:35:28percent d for the day
- 5:35:31percent y for the year
- 5:35:35percent h for the hour and to format
- 5:35:38this i'm going to add a colon
- 5:35:40to separate hours and minutes percent
- 5:35:43m for minutes colon percent s
- 5:35:48for seconds and then we are going to
- 5:35:51assign
- 5:35:51all of this to a variable let's say
- 5:35:54local
- 5:35:55time and local time will be a string
- 5:35:58so let's print our local time
- 5:36:03and the current time is january
- 5:36:0623rd 2021 about 3 p.m
- 5:36:10oh and i almost forgot you can also get
- 5:36:12the utc
- 5:36:13time that is the coordinated universal
- 5:36:16time if you know how that works
- 5:36:18so if you need that you would just use
- 5:36:20the gm
- 5:36:21time method for the utc time coordinated
- 5:36:25universal time
- 5:36:26okay next up we have the strp
- 5:36:29time function and this function will
- 5:36:32parse
- 5:36:32a string representation of a time and or
- 5:36:36date
- 5:36:36and return a time object so we need to
- 5:36:39pass in
- 5:36:39a string representing the date and or
- 5:36:42time as well as a format
- 5:36:44string so let's create a time string and
- 5:36:47this variable is going to be a string
- 5:36:49representation of
- 5:36:50a date let's say 20th of april 2020
- 5:36:54and what we can do is take this string
- 5:36:56representation of a time and or date
- 5:36:58and parse it to a time object so we're
- 5:37:01going to pass in our time
- 5:37:02string variable as well as a format
- 5:37:05string
- 5:37:06so let's say i would like to parse
- 5:37:10the day so that would be percent d for
- 5:37:12day
- 5:37:13then percent b for name of the month
- 5:37:17and then comma percent y for year
- 5:37:21this function will create a time object
- 5:37:23so let's assign that to a variable
- 5:37:26time object equals time dot strp time
- 5:37:29we're passing in our string
- 5:37:31representation of a time
- 5:37:32or date as well as a format string
- 5:37:35and we can print our time object using a
- 5:37:38print statement
- 5:37:40however this is going to be in a form
- 5:37:41that is somewhat difficult to read but
- 5:37:43you can see at least we have a time
- 5:37:45object
- 5:37:46with all of these keywords filled in
- 5:37:48with anything that we passed in
- 5:37:49via these format directives that we have
- 5:37:52now the next function
- 5:37:53is the asc time function
- 5:37:56and this function accepts a time object
- 5:37:59or a tuple representation
- 5:38:01of a relative time so this time let's
- 5:38:04create a
- 5:38:04time tuple and we're going to follow
- 5:38:08this order we can pass in
- 5:38:09up to nine values the first value is a
- 5:38:12year so let's pass in perhaps
- 5:38:142020 a month let's say four
- 5:38:17a day how about 20 four hours
- 5:38:20let's say four minutes 20 and seconds
- 5:38:24maybe zero let me just fix some of the
- 5:38:26spacing here
- 5:38:27okay you can also pass in a numbered day
- 5:38:30of the week
- 5:38:31um i'm just going to say zero i don't
- 5:38:33think it's really that important
- 5:38:35a day of the year zero and
- 5:38:38negative one or zero for daylight
- 5:38:39savings time so
- 5:38:41we created a time tuple and we can pass
- 5:38:44in a time
- 5:38:44object or a tuple representation of a
- 5:38:47time
- 5:38:48following this formula so let's pass in
- 5:38:51our time tuple
- 5:38:52and this will create a time string a
- 5:38:55string representation of the time that
- 5:38:57we create
- 5:38:58and let's print our time string
- 5:39:02and we should have april 20th
- 5:39:05about four in the morning the year 2020.
- 5:39:08so that is asc time it will convert a
- 5:39:11tuple representation of a time and date
- 5:39:15or a time object and convert it to a
- 5:39:17readable
- 5:39:18string now another option is to use m
- 5:39:21k time and mk time will take a
- 5:39:24tuple representation of a time or a time
- 5:39:28object
- 5:39:28and convert it to seconds since epic
- 5:39:32so april 20th of the year 2020
- 5:39:35is about 1.5 billion seconds
- 5:39:38since our epic date and for me that was
- 5:39:40december 31st the year 1969
- 5:39:44so that is a few useful functions of the
- 5:39:47time module
- 5:39:49if you would like a copy of this code i
- 5:39:50will post all this to the comment
- 5:39:52section down below
- 5:39:53and well yeah that's the time module in
- 5:39:56python
- 5:39:58yo what's going on everybody it's bro
- 5:40:00hope you're doing well
- 5:40:01and in this video i'm going to explain
- 5:40:03multi-threading in python so
- 5:40:04sit back relax and enjoy the show
- 5:40:09think of a thread as a flow of execution
- 5:40:12like a river
- 5:40:13and each thread can carry out its own
- 5:40:15separate order of instructions
- 5:40:17if we use this process of
- 5:40:18multi-threading we can have our program
- 5:40:21run
- 5:40:21different parts of its program at
- 5:40:23different times
- 5:40:24they all run concurrently but not truly
- 5:40:27in parallel that is a concept for later
- 5:40:29referred to as multi-processing
- 5:40:31so with reds they each take a turn
- 5:40:34running to achieve concurrency
- 5:40:36this is due to a notorious feature known
- 5:40:38as the gil
- 5:40:40the global interpreter lock only one
- 5:40:43thread can be running at one time
- 5:40:45but they can all take turns when one
- 5:40:47thread is idle
- 5:40:48so this allows one thread to hold
- 5:40:50control of the python interpreter
- 5:40:53at any one time so they run concurrently
- 5:40:55but not truly in parallel
- 5:40:57which is what we do with
- 5:40:59multi-processing now programs and tasks
- 5:41:01can be divided into
- 5:41:02two different categories they can be cpu
- 5:41:05bound that is a program or a task
- 5:41:08that spends most of its time waiting for
- 5:41:10internal events
- 5:41:11such as a task that is cpu intensive
- 5:41:14it is better to use multi-processing for
- 5:41:17tasks that are cpu bound
- 5:41:19now tasks that are io bound means that
- 5:41:22the program will spend most of its time
- 5:41:24waiting for external events
- 5:41:26such as waiting for user input or if
- 5:41:29you're doing activities like web
- 5:41:30scraping
- 5:41:31you do a lot of sitting around so with i
- 5:41:33o bound tasks it's better to use
- 5:41:35multi-threading
- 5:41:36because we can have multiple threads
- 5:41:38running concurrently
- 5:41:40but not truly in parallel like what we
- 5:41:42do with multi-processing
- 5:41:43after importing the threading module we
- 5:41:46can count the number of threads that are
- 5:41:48currently running
- 5:41:48in the background whenever we run a
- 5:41:50program we have one thread that is
- 5:41:52running that is in charge of
- 5:41:54executing our program and we can print
- 5:41:56the active count of threads running in
- 5:41:58our program
- 5:41:59using the active count function of the
- 5:42:01threading module
- 5:42:03so this will print one we have one
- 5:42:05thread that is running
- 5:42:06and we can print a list of all the
- 5:42:09threads that are running
- 5:42:11by using the enumerate function so
- 5:42:14the one thread that is in charge of
- 5:42:16running our program
- 5:42:17is referred to as the main thread by
- 5:42:20using this concept of multi-threading we
- 5:42:22can have
- 5:42:22more than one thread running
- 5:42:24concurrently not truly in parallel
- 5:42:27all the threads will take turns while
- 5:42:28one of them is idle
- 5:42:30so we can have more than one thread
- 5:42:32running more than just the main
- 5:42:34thread which is in charge of running the
- 5:42:35main body of our program
- 5:42:37so while our main thread is in charge of
- 5:42:39running the main body of our program
- 5:42:41we can have another thread that's in
- 5:42:43charge of a separate part of it
- 5:42:45maybe like a countdown timer or
- 5:42:47something so
- 5:42:48one good example take that quiz game
- 5:42:50that we made some number of videos ago
- 5:42:52while we were waiting for user input
- 5:42:55which is a
- 5:42:56i o bound task we could have had a
- 5:42:58countdown timer going
- 5:42:59like you only have so many seconds to
- 5:43:01answer this question we could have had
- 5:43:03one thread in charge of waiting for user
- 5:43:05input and another thread
- 5:43:06in charge of the countdown timer so
- 5:43:09that's an example of multi-threading
- 5:43:11we had two threads running concurrently
- 5:43:14and what we'll be doing in this video is
- 5:43:15creating a program that involves
- 5:43:17multi-threading we can have
- 5:43:19different threads in charge of different
- 5:43:21parts of our program and they can all
- 5:43:22run
- 5:43:23concurrently they'll all take turns
- 5:43:25while one of them is idle
- 5:43:26so let's say that we're running late for
- 5:43:29school
- 5:43:30or work in the morning and we have three
- 5:43:32different tasks that we need to complete
- 5:43:34before we can leave
- 5:43:35for school or work so think of three
- 5:43:38different
- 5:43:38things you do in the morning such as
- 5:43:40maybe eat
- 5:43:41breakfast some people drink coffee or
- 5:43:44maybe a beverage of your choice so drink
- 5:43:48coffee and some people like me they like
- 5:43:51to
- 5:43:51do their homework last minute so i'll
- 5:43:53say study
- 5:43:54before i leave for work or school in the
- 5:43:57morning
- 5:43:58so what we'll do in each of these
- 5:44:00functions each of these functions
- 5:44:01should take some amount of time to
- 5:44:03complete so we can have our main thread
- 5:44:06sleep for a given number of seconds
- 5:44:07using the sleep function
- 5:44:10time dot sleep and let's say that in
- 5:44:12order for me to eat breakfast
- 5:44:14this task will take me three seconds
- 5:44:16let's just pretend that instead of
- 5:44:18minutes this will be
- 5:44:19in seconds so drink coffee will take me
- 5:44:23four seconds and study will take me
- 5:44:26five seconds then when we finish
- 5:44:30sleeping
- 5:44:31let's print a confirmation message let's
- 5:44:34say
- 5:44:35you eat breakfast
- 5:44:38as in you finish eating breakfast with
- 5:44:41drink coffee
- 5:44:43you drink coffee
- 5:44:48and with study
- 5:44:51you finish studying
- 5:44:56now each of these tasks are i o bound
- 5:44:58they're going to be spending a lot of
- 5:45:00time just waiting around
- 5:45:01for external events they're waiting for
- 5:45:03the sleep function to expire before they
- 5:45:06can finish their task
- 5:45:07so we're going to have all of these
- 5:45:09three functions run on our main thread
- 5:45:12and we'll see how long it takes for us
- 5:45:13to complete our morning ritual
- 5:45:15these three tasks so let's call these
- 5:45:18three functions within
- 5:45:19our main thread so let's call the
- 5:45:22eat breakfast function first followed by
- 5:45:26drink coffee and then study
- 5:45:30in that order so this program is going
- 5:45:33to take approximately
- 5:45:35let's see 12 seconds to complete so
- 5:45:38there's going to be a pause for a second
- 5:45:40you eat breakfast then followed by
- 5:45:44you drank coffee and then study
- 5:45:50you finished studying so this program
- 5:45:52took about
- 5:45:5312 seconds overall so if this were
- 5:45:56realistic
- 5:45:57what we would have done is we would sit
- 5:45:59down and eat breakfast for
- 5:46:01three minutes well three seconds in this
- 5:46:03case and then
- 5:46:04once we finish eating breakfast only
- 5:46:06then are we allowed to drink our coffee
- 5:46:09and once we finish our coffee only then
- 5:46:12can we study
- 5:46:13so we completed these tasks sequentially
- 5:46:16and not concurrently for us to move down
- 5:46:19to the next
- 5:46:19function we need to complete the
- 5:46:21previous functions because we're doing
- 5:46:23this in order
- 5:46:24but realistically us human beings we
- 5:46:27would probably
- 5:46:28eat breakfast drink coffee and study
- 5:46:30altogether because
- 5:46:32we can multitask and we can complete
- 5:46:34these three functions in less time
- 5:46:36and that's kind of the same process as
- 5:46:38multi-threading we can have
- 5:46:40these three separate functions running
- 5:46:42concurrently
- 5:46:43as if we're multitasking we're eating
- 5:46:45breakfast drinking coffee
- 5:46:46and studying all at once now currently
- 5:46:49we have
- 5:46:50one thread that is in charge of these
- 5:46:52three separate functions
- 5:46:54what we could do is that we can create
- 5:46:56three additional threads
- 5:46:57each thread will be in charge of each
- 5:47:00task and then we'll have our main thread
- 5:47:02running in the background that will
- 5:47:03complete the rest of the program
- 5:47:05so this is how to create an additional
- 5:47:06thread let's say that
- 5:47:08x equals threading
- 5:47:12dot thread we need to pass in a target
- 5:47:15target equals and then the name of the
- 5:47:20function so let's say
- 5:47:21thread x will be in charge of eating
- 5:47:23breakfast
- 5:47:24and then you can pass in arguments too
- 5:47:26if your function has parameters by
- 5:47:28typing args
- 5:47:29and then you pass in a tuple so let's
- 5:47:31say you have one argument to pass in
- 5:47:33you'll type your argument followed by
- 5:47:35a comma but we don't have any arguments
- 5:47:37in this example but
- 5:47:38you'll need to be sure to enter them in
- 5:47:40if you do have any
- 5:47:42okay then to begin this thread you type
- 5:47:44the name of the thread
- 5:47:45in this case x dot start so
- 5:47:48we now have an additional thread and
- 5:47:50this thread is in charge of eating
- 5:47:52breakfast
- 5:47:53now let's create another thread to drink
- 5:47:55coffee
- 5:47:57and we'll call this y
- 5:48:01and the target will be drink coffee
- 5:48:05and lastly we have a thread in charge of
- 5:48:08studying
- 5:48:10and this will be z
- 5:48:14and the target is our study function
- 5:48:17okay now let's see how long it takes for
- 5:48:19us to complete our program
- 5:48:21oh and be sure to comment out these
- 5:48:23function calls within the main thread
- 5:48:25because we don't want the main thread in
- 5:48:26charge of those anymore
- 5:48:27okay now we can run the program so we
- 5:48:30have four threads running this time
- 5:48:32you eat breakfast you drink coffee you
- 5:48:34finish studying
- 5:48:35so this program took approximately five
- 5:48:38seconds to finish
- 5:48:39and the reason that this program took
- 5:48:41five seconds instead of 12
- 5:48:43is because before our main thread was in
- 5:48:45charge of running
- 5:48:46these tasks sequentially in order but
- 5:48:49now since we have a thread
- 5:48:50dedicated to each task we can run them
- 5:48:53all
- 5:48:54concurrently instead of sequentially so
- 5:48:56that this program
- 5:48:57now took about five seconds to complete
- 5:48:59and you may have noticed too
- 5:49:01that the activecount function as well as
- 5:49:03the enumerate function
- 5:49:04were called before threads 1 2 and 3
- 5:49:07finished their respective tasks
- 5:49:09that's because the main thread is not
- 5:49:11going to wait around for
- 5:49:12these three threads to complete it has
- 5:49:14its own set of instructions to do
- 5:49:16so it is no longer in charge of these
- 5:49:19three functions
- 5:49:21the program is going to handle those
- 5:49:23three functions to our three threads
- 5:49:25and our main thread is going to continue
- 5:49:27its own set of instructions
- 5:49:28its job is to print the active count as
- 5:49:31well as
- 5:49:32call the enumerate function which it did
- 5:49:35and it finished its tasks
- 5:49:36before threads one two and three now one
- 5:49:39trick that you can do too
- 5:49:40is that you can use the time modules
- 5:49:43performance
- 5:49:43counter function and this function will
- 5:49:46return
- 5:49:47how long it takes our calling thread as
- 5:49:49in our main thread
- 5:49:50to finish its set of instructions so our
- 5:49:53main thread is not in charge of
- 5:49:55executing
- 5:49:56these three functions our main thread is
- 5:49:59in charge of creating
- 5:50:00three additional threads and then
- 5:50:02calling the activecount function
- 5:50:04and the enumerate function as well as
- 5:50:06the performance counter function
- 5:50:09so our main thread will take
- 5:50:11approximately
- 5:50:130.075 seconds to complete
- 5:50:15and our three threads are still running
- 5:50:17in the background our main threads job
- 5:50:19its order of instructions is to create
- 5:50:22three additional threads and then
- 5:50:24immediately
- 5:50:25print whatever is returned via the
- 5:50:28activecount function
- 5:50:29enumerate and then your performance
- 5:50:31counter so
- 5:50:33our main thread says that it finished
- 5:50:35its tasks in about
- 5:50:36.07 seconds but threads one
- 5:50:40through three were still trying to catch
- 5:50:42up that's why the main thread
- 5:50:44finished before are three additional
- 5:50:45threads and once
- 5:50:47all active threads have completed their
- 5:50:49tasks
- 5:50:50then your program will finish and exit
- 5:50:52there's also this concept called
- 5:50:54thread synchronization we can have a
- 5:50:56calling thread in this case our main
- 5:50:58thread
- 5:50:59wait around for another thread to finish
- 5:51:01before it can move on
- 5:51:02with its own instruction set so let's
- 5:51:05say we would like
- 5:51:06our main thread to wait around for
- 5:51:08thread 1 also known as
- 5:51:10x so we're going to use the join
- 5:51:13function
- 5:51:14of thread x and now our main thread
- 5:51:17has to wait around for thread x also
- 5:51:19known as
- 5:51:20thread 1 to finish before it can move on
- 5:51:22with its instruction set
- 5:51:24so let's do the same with y and
- 5:51:27z so now our main thread before it can
- 5:51:30move on with the rest of the program has
- 5:51:32to wait for all of these threads
- 5:51:33to synchronize and join and then and
- 5:51:36only then
- 5:51:37can it move on with the rest of its own
- 5:51:39instruction set
- 5:51:40so this time our program is going to
- 5:51:42look a little something like this
- 5:51:44you eat breakfast you drink coffee you
- 5:51:46finish studying
- 5:51:47so by the time we reach our active count
- 5:51:50these threads
- 5:51:50are already joined and synchronized
- 5:51:52they're no longer active
- 5:51:54when we enumerate over our active
- 5:51:56threads we only have
- 5:51:57our main thread and our main thread
- 5:51:59completed in
- 5:52:00about 5.1 seconds this time because it
- 5:52:03did a lot of waiting around
- 5:52:04it was waiting for threads x y and z to
- 5:52:08join and finish in conclusion a thread
- 5:52:10is
- 5:52:11a flow of execution like a separate
- 5:52:14order of instructions that a program can
- 5:52:16follow
- 5:52:16and when we run a program we always have
- 5:52:19at least one
- 5:52:20thread running initially and that is
- 5:52:22referred to as the main thread
- 5:52:24however if you have multiple threads
- 5:52:26using the concept of multi-threading
- 5:52:28we can have multiple threads running
- 5:52:30concurrently but not truly in parallel
- 5:52:33which is what we'll cover
- 5:52:34during the video on multi-processing
- 5:52:36this is due
- 5:52:37to a feature known as the gil
- 5:52:40the global interpreter lock which allows
- 5:52:43only
- 5:52:43one thread to hold control of the python
- 5:52:46interpreter
- 5:52:47at any one time and tasks can be either
- 5:52:50cpu bound or io bound a cpu bound task
- 5:52:53is a program or task
- 5:52:55that spends most of its time waiting for
- 5:52:58internal events
- 5:52:59such as a task that is cpu intensive
- 5:53:02and o bound tasks spend most of their
- 5:53:05time waiting for
- 5:53:06external events such as waiting around
- 5:53:09for user input
- 5:53:10or web scraping with i o bound tasks
- 5:53:13it's better to use multi-threading
- 5:53:15with cpu bound tasks it's better to use
- 5:53:18multi-processing
- 5:53:19well everybody that is a quick overview
- 5:53:21of multi-threading i will post all of
- 5:53:23this code to the comment section down
- 5:53:25below
- 5:53:26and well yeah that's how to achieve
- 5:53:28multi-threading
- 5:53:29using python hey what's going on
- 5:53:32everybody
- 5:53:33it's you bro hope you're doing well and
- 5:53:35in this video i'm going to explain demon
- 5:53:36threads in python so
- 5:53:38sit back relax and enjoy the show
- 5:53:43okay so demon threads also pronounced as
- 5:53:46daemon threads a demon thread is a
- 5:53:49thread that runs in the background and
- 5:53:51they are normally not important for your
- 5:53:52program in order to run
- 5:53:54your program will not wait for demon
- 5:53:56threads to complete before
- 5:53:57exiting whereas non-demon threads they
- 5:54:00cannot normally be killed
- 5:54:02they will stay alive until their task is
- 5:54:04complete so
- 5:54:05a few common uses of demon threads would
- 5:54:08be
- 5:54:08background tasks garbage collection
- 5:54:11waiting for input
- 5:54:12or other long-running processes so
- 5:54:15here's an example of why demon threads
- 5:54:17would be useful
- 5:54:18let's say that we'll have two threads
- 5:54:20our main thread will be in charge of
- 5:54:22waiting around for some user input we'll
- 5:54:24ask
- 5:54:24do you wish to exit and in the
- 5:54:26background we'll have a timer going that
- 5:54:28will display
- 5:54:29how long somebody is logged in so let's
- 5:54:32create
- 5:54:32a function named timer
- 5:54:36and what we'll do is just display how
- 5:54:38long somebody's logged in
- 5:54:40so let's say we have a count variable
- 5:54:43while true we'll use
- 5:54:46the sleep function of the time module to
- 5:54:49sleep for approximately one second
- 5:54:51then we'll increment our count variable
- 5:54:53by one and we'll print a message
- 5:54:56so let's say logged in
- 5:54:59for
- 5:55:03count
- 5:55:06seconds and let's create a thread that
- 5:55:09will be in charge of this timer and run
- 5:55:11it in the background so let's say
- 5:55:13x equals threading
- 5:55:16dot thread the target will be our
- 5:55:19function
- 5:55:19of timer and we need to start this
- 5:55:22thread
- 5:55:23x dot start so our thread
- 5:55:26x will be in charge of our timer and run
- 5:55:29it concurrently
- 5:55:30while we're waiting for some user input
- 5:55:32our main thread will be in charge of
- 5:55:34waiting for this user input do you wish
- 5:55:36to exit now here's the issue
- 5:55:38of us using a non-demon thread for a
- 5:55:40background task such as a countdown
- 5:55:42timer
- 5:55:43so our main thread is in charge of
- 5:55:45waiting around for some user input
- 5:55:46and thread x our additional thread is in
- 5:55:49charge of
- 5:55:50running this countdown timer so if i
- 5:55:52wish to exit this program i'm just going
- 5:55:54to type
- 5:55:55okay and hit enter well my background
- 5:55:57timer is still going but my main thread
- 5:56:00is complete so i would like to exit this
- 5:56:02program but
- 5:56:03my program will not exit as long as
- 5:56:06there are non-demon threads that are
- 5:56:08still alive
- 5:56:09we cannot normally close this program
- 5:56:11unless we do like a brute force
- 5:56:13you know close and exit of the program
- 5:56:15so we can turn
- 5:56:17this thread of x into a demon thread
- 5:56:20so that when we wish to exit the program
- 5:56:22when there are no more
- 5:56:24non-demon threads alive and to change a
- 5:56:26thread to a demon thread it's actually
- 5:56:28fairly easy when you create your thread
- 5:56:30you can set
- 5:56:31this flag of demon equal to true and
- 5:56:33that's it
- 5:56:34so when all non-demon threads have
- 5:56:37finished their tasks
- 5:56:38your program will complete and exit but
- 5:56:41if there are demon tasks running in the
- 5:56:43background
- 5:56:43they will be killed automatically so
- 5:56:45let's try this program again
- 5:56:47so do you wish to exit my program is
- 5:56:49going to continue running
- 5:56:50my demon thread is in charge of the
- 5:56:52timer and it's going to continue running
- 5:56:53until
- 5:56:54all non-daemon threads have completed
- 5:56:57their tasks
- 5:56:58so once i type ok my program will
- 5:57:02finish running and my countdown timer
- 5:57:04will stop because
- 5:57:05all demons are killed when your program
- 5:57:07is finished running there's also two
- 5:57:09additional methods that you might be
- 5:57:11interested in
- 5:57:11you can use the set daemon method
- 5:57:14of a thread and you can set it to false
- 5:57:16or true if you would like to change
- 5:57:18a thread to a non-demon or a demon
- 5:57:21however if your thread is currently
- 5:57:23running well you cannot actually change
- 5:57:25it from
- 5:57:26a non-demon thread to a demon thread
- 5:57:28while it's currently running
- 5:57:30so you would have to do that before you
- 5:57:31actually use the start function
- 5:57:34and you can also check to see if a
- 5:57:36thread is a demon or not
- 5:57:37by using the is demon method this will
- 5:57:40return
- 5:57:41true or false so everybody in conclusion
- 5:57:44a demon thread is a thread that runs in
- 5:57:46the background
- 5:57:47and they're not important for your
- 5:57:49program in order to continue running
- 5:57:51your program will not wait for demon
- 5:57:53threads to complete before exiting the
- 5:57:55program
- 5:57:56whereas non-demon threads cannot
- 5:57:58normally be killed
- 5:57:59they will stay alive until their task is
- 5:58:01complete and demon threads are commonly
- 5:58:04used for
- 5:58:04background tasks garbage collection
- 5:58:07waiting around for input
- 5:58:08or other long running processes so if
- 5:58:11you would like a copy of this code i
- 5:58:13will post all of this to the comment
- 5:58:14section down below
- 5:58:15but yeah those are demon threads in
- 5:58:18python
- 5:58:19yo what's going on everybody it's bro
- 5:58:22hope you're doing well
- 5:58:23and in this video i'm going to explain
- 5:58:24multi-processing in python so
- 5:58:26sit back relax and enjoy the show
- 5:58:31what's up let's talk about
- 5:58:33multi-processing multi-processing
- 5:58:35is the act of running tasks in parallel
- 5:58:38on a different cpu cores it's different
- 5:58:41from multi-threading
- 5:58:42although it sounds similar because with
- 5:58:44multi-threading
- 5:58:45we're limited to running one thread at a
- 5:58:48time
- 5:58:48because of the gil that lock which is
- 5:58:51used for threading we can run threads
- 5:58:53concurrently
- 5:58:54but not in parallel however with
- 5:58:57multi-processing
- 5:58:58we can create processes and we can run
- 5:59:01each process
- 5:59:02in parallel on a different cpu course so
- 5:59:05with multi-processing
- 5:59:06it's better for tasks that are cpu bound
- 5:59:09where you need heavy cpu usage whereas
- 5:59:13multi-threading that is better for i o
- 5:59:15bound tasks where you do a lot of
- 5:59:17waiting around
- 5:59:18so before we begin i recommend these two
- 5:59:20imports
- 5:59:21multi-processing and time let's begin
- 5:59:24quick note if you're running the windows
- 5:59:26operating system you'll probably need to
- 5:59:28add
- 5:59:28this line if name is equal to
- 5:59:31main so when we run a program we have a
- 5:59:34main process that is running and if we
- 5:59:37create a child process
- 5:59:38from that process it's going to copy the
- 5:59:41module that we're currently working with
- 5:59:43and that child process will create its
- 5:59:46own children processes and it's going to
- 5:59:48be a problem
- 5:59:49so we're going to add this line if name
- 5:59:51is equal to main
- 5:59:52so when we create a child process it
- 5:59:54will copy our module but it's not going
- 5:59:56to execute it
- 5:59:58so let's create a main function and a
- 6:00:00majority of our code is going to be
- 6:00:02within our main function
- 6:00:04if you're running a different operating
- 6:00:05system you probably don't have to do
- 6:00:07this but if you're running windows
- 6:00:08you probably will now with
- 6:00:10multi-processing multi-processing is
- 6:00:12better for tasks or functions
- 6:00:14that are cpu bound where they require
- 6:00:17heavy cpu usage
- 6:00:19let's say that we have a function named
- 6:00:21counter
- 6:00:22and we'll pass in a number to count up
- 6:00:24to but the number we're going to pass in
- 6:00:26is a ridiculously large number like a
- 6:00:28billion so let's create a function that
- 6:00:30will count
- 6:00:31from zero so count equals zero and
- 6:00:35while count is less than
- 6:00:38our number that we pass in we will
- 6:00:40increment our counter
- 6:00:42by one so that is the function that
- 6:00:45we'll call with
- 6:00:46our processes that we create now within
- 6:00:48our main function
- 6:00:49we'll create a process and to do that
- 6:00:52you'll need the multi-processing module
- 6:00:54so import process and cpu account and
- 6:00:57we'll save this for a little bit later
- 6:00:59to create a process let's say we have
- 6:01:01process a
- 6:01:02a equals process this step is very
- 6:01:06similar to creating a thread
- 6:01:08we have a target our target will be our
- 6:01:10function of counter
- 6:01:13and if we have arguments we will pass
- 6:01:16those in
- 6:01:16so remember with our arguments we have
- 6:01:18to pass in a tuple
- 6:01:20since we only have one argument to pass
- 6:01:23in
- 6:01:23to differentiate this from an expression
- 6:01:25we have to add a comma at the end
- 6:01:27so our number let's say is 1 billion
- 6:01:31so that's a million 10 million 100
- 6:01:35million
- 6:01:351 billion let me just verify that okay
- 6:01:38so that's 1 billion
- 6:01:39we're going to count from zero to a
- 6:01:42billion
- 6:01:42and in order to start this process a
- 6:01:46my process use the start function
- 6:01:50and then i will use some process
- 6:01:52synchronization by using the
- 6:01:54join function my main process is going
- 6:01:57to wait around
- 6:01:58for my child process of a to finish
- 6:02:01before continuing
- 6:02:03and let's print our performance counter
- 6:02:06so we'll print finished in
- 6:02:12let's say time dot performance counter
- 6:02:19seconds okay so let's see how long it'll
- 6:02:23take to count from zero to a billion
- 6:02:25using
- 6:02:26one process i fast forwarded this video
- 6:02:28but it took my program
- 6:02:3056.7 seconds to count from zero to a
- 6:02:33billion
- 6:02:34but we can speed this up by using
- 6:02:35multi-processing let's create a second
- 6:02:38process named b
- 6:02:40and i'm going to divide the amount of
- 6:02:42work in half so
- 6:02:44each process will count to 500 million
- 6:02:47half of a billion so let's change a to b
- 6:02:52and i'm gonna group these start
- 6:02:53functions together
- 6:02:57just so it's easier to read and then b
- 6:02:59dot join just to synchronize everything
- 6:03:02okay and then let's change 1 billion to
- 6:03:06500 million for each it's the same
- 6:03:09amount of work but divided among workers
- 6:03:12so that is 500 million let's see how
- 6:03:14well it runs this time
- 6:03:16so it took my program about 40.3 seconds
- 6:03:19to finish
- 6:03:20counting from zero to a billion but i
- 6:03:22divided that task among
- 6:03:24different processes each of my two
- 6:03:26processes counted from
- 6:03:27zero to 500 million this time so this
- 6:03:30time let's create
- 6:03:31four processes so we have a
- 6:03:35b c and d
- 6:03:39and we'll need to start them
- 6:03:44and i'm going to join them now this
- 6:03:48might not actually speed it up for you
- 6:03:50and i'll explain why later
- 6:03:52it depends on the amount of cpu cores
- 6:03:54that you have
- 6:03:55okay so oh let's change this to so 250
- 6:03:59million for each
- 6:04:02okay see you in a couple seconds
- 6:04:06okay welcome back so on my computer when
- 6:04:08i ran
- 6:04:09four processes and each was counting up
- 6:04:11to 250 million
- 6:04:14i could finish my program in 27.3
- 6:04:17seconds now for me if i created
- 6:04:20more processes than this it would
- 6:04:22probably take longer and here's why
- 6:04:24so you can print the cpu count of your
- 6:04:27computer
- 6:04:27using this function of cpu count so if i
- 6:04:31were to
- 6:04:31print whatever this function returns i
- 6:04:34can get the count
- 6:04:35of the number of additional processes
- 6:04:37that i can run
- 6:04:38for the time being i'm just going to
- 6:04:40comment out this start function because
- 6:04:42i just want to print whatever this value
- 6:04:44is so my cpu count on my computer
- 6:04:47is four so i can run four additional
- 6:04:51processes
- 6:04:52but let's attempt to run eight processes
- 6:04:55so i'm going to copy all these and let's
- 6:04:57say that we have
- 6:04:59e f g
- 6:05:02and h and we will start all of those two
- 6:05:06so give me a second just to start them
- 6:05:10so a b c d e
- 6:05:14f g
- 6:05:18h and then i'm going to join all of them
- 6:05:23e f g
- 6:05:28and h okay so
- 6:05:31let's run this now oh and change the
- 6:05:34arguments to i keep forgetting to do
- 6:05:35that
- 6:05:36so let's say 125 for each
- 6:05:40so 125 million times eight is
- 6:05:43one billion okay see you in a bit
- 6:05:47all right welcome back this time when i
- 6:05:49had eight processes working it actually
- 6:05:51took me
- 6:05:52longer than when i had four processes
- 6:05:54working when i had four processes
- 6:05:56working it took me about 27 seconds
- 6:05:58this time it took 30 seconds when i had
- 6:06:01eight processes working
- 6:06:02that's because i had more processes than
- 6:06:05my cpu count
- 6:06:06and that's partially because whenever
- 6:06:08you create a process
- 6:06:09there's significant overhead with
- 6:06:11beginning and
- 6:06:12destroying a process and if i can only
- 6:06:15run four
- 6:06:16processes on my computer at one time
- 6:06:18well then i'm creating additional
- 6:06:20processes
- 6:06:21to no extra benefit it's actually
- 6:06:23hindering the performance of the
- 6:06:24computer
- 6:06:25because i'm creating all of these
- 6:06:27additional processes when it really
- 6:06:28doesn't help me
- 6:06:30in conclusion multi-processing is the
- 6:06:32concept
- 6:06:33of running tasks in parallel on
- 6:06:35different cpu cores
- 6:06:37it's similar but different from
- 6:06:38multi-threading because with
- 6:06:40multi-threading
- 6:06:41we can run tasks concurrently but
- 6:06:43they're all taking turns because of the
- 6:06:45gil
- 6:06:46with multi-processing we can run all of
- 6:06:49these different tasks
- 6:06:50together in parallel multi-processing is
- 6:06:53better for
- 6:06:53cpu bound tasks where a task has
- 6:06:56heavy cpu usage and multi-threading
- 6:07:00is better for i o bound tasks tasks that
- 6:07:02involve a lot of waiting around
- 6:07:04so that's multi-processing if you would
- 6:07:06like a copy of all this code i will post
- 6:07:08all of this to the comment section down
- 6:07:10below
- 6:07:10but yeah that is how multi-processing
- 6:07:12works in python
- 6:07:15hey what's going on everybody two bro
- 6:07:17here hope you're doing well and in this
- 6:07:18video we're going to be creating our
- 6:07:19very first
- 6:07:20graphical user interface in python so
- 6:07:22sit back relax and enjoy the show
- 6:07:27welcome one and all today we'll be
- 6:07:28creating our very first
- 6:07:30graphical user interface in python also
- 6:07:32known as a
- 6:07:33gui for short we're going to be using
- 6:07:35the tk inter gui which is a module that
- 6:07:38is included with python
- 6:07:40our first step in order to use tk enter
- 6:07:42is to import this module so that we can
- 6:07:44begin using it
- 6:07:45our first line of code will be from tk
- 6:07:48enter
- 6:07:49import asterisk this will import
- 6:07:52everything related to the tk inter
- 6:07:54module
- 6:07:55and with this import we can now use all
- 6:07:57of the gui features that this module has
- 6:07:59to offer
- 6:08:00now there is one important distinction
- 6:08:02that we need to make we need to discuss
- 6:08:04the differences between
- 6:08:05windows and widgets here's a few quick
- 6:08:09definitions that i definitely did not
- 6:08:11copy from the internet
- 6:08:12widgets are gui elements such as buttons
- 6:08:15text boxes labels images you name it
- 6:08:18and windows they serve as a container
- 6:08:21to hold or contain these widgets for
- 6:08:24this topic we'll be creating and
- 6:08:26customizing our own window
- 6:08:28and in future topics we'll be exploring
- 6:08:30different widgets that are available to
- 6:08:32us
- 6:08:33so let's begin by creating a simple
- 6:08:35window and we should give our window a
- 6:08:37unique name
- 6:08:38such as window that's pretty creative
- 6:08:40and in order to instantiate this window
- 6:08:42we're going to follow
- 6:08:43this window name with equals tk make
- 6:08:46sure you get the capitalization right
- 6:08:48this is a
- 6:08:49uppercase t and a set of parentheses to
- 6:08:51serve as the constructor
- 6:08:53what this will do and i'm just going to
- 6:08:54add a comment is instantiate
- 6:08:57an instance of a window for us
- 6:09:02and when we compile and run this nothing
- 6:09:04appears to happen
- 6:09:05that's because we only instantiated our
- 6:09:07window and we're not actually displaying
- 6:09:09it
- 6:09:10in order to display our window we're
- 6:09:11going to follow this with
- 6:09:13the name of the window dot main loop
- 6:09:16and this will display our window and
- 6:09:19this is our first
- 6:09:20graphical user interface and we'll be
- 6:09:22adding widgets
- 6:09:23to this window so i'm going to add a
- 6:09:25comment that
- 6:09:27this will place window on
- 6:09:30computer screen and it will also listen
- 6:09:34for events but we'll get to that in
- 6:09:36future videos
- 6:09:37now what we'll be doing is customizing
- 6:09:39the appearance of this window
- 6:09:41let's begin by changing the size and we
- 6:09:43can do that with the geometry function
- 6:09:46so we're going to first begin by typing
- 6:09:48the name of our window which is window
- 6:09:51follow this by using the geometry
- 6:09:53function and we pass in a width and a
- 6:09:55height
- 6:09:56but make sure this is within quotes and
- 6:09:58set this to whatever width and height
- 6:09:59that you want
- 6:10:00so if i want 420 by 420 that would be
- 6:10:03420
- 6:10:04x 420 and the size of our window
- 6:10:07is now the size that we set with the
- 6:10:09geometry function
- 6:10:11now take a look at the top left of this
- 6:10:13window we have
- 6:10:14this feather icon as well as a title of
- 6:10:17tk
- 6:10:18that's kind of lame so let's change that
- 6:10:21let's first
- 6:10:21change the window title and there is a
- 6:10:24function to do that
- 6:10:25so we type in the name of our window
- 6:10:28window
- 6:10:29dot title function and we can pass in a
- 6:10:33new title that we want to set
- 6:10:35and i will set the title to be maybe bro
- 6:10:38code first gui
- 6:10:42program that'll work and we now changed
- 6:10:46the title of this window and it says
- 6:10:48bro code first gui program
- 6:10:51the next thing that we can do although
- 6:10:53not necessary
- 6:10:54is that we can change the icon of our
- 6:10:57window bar at the top
- 6:10:58i would like to replace this feather
- 6:11:00icon with an image of my choosing
- 6:11:03here's my desktop don't mind all of the
- 6:11:05clutter and i would like to use this
- 6:11:07image of the logo for my channel and set
- 6:11:09this as the icon
- 6:11:10for my window so what i'm going to do is
- 6:11:13copy
- 6:11:15and then go to my project folder and
- 6:11:17then paste it
- 6:11:19click ok and i now have this image
- 6:11:22within my project folder
- 6:11:24but it's in a format that we cannot use
- 6:11:27currently we need to convert this to
- 6:11:28what's called a photo image
- 6:11:30that is a format that tk enter can use
- 6:11:34so we'll need to convert our image to a
- 6:11:36photo image
- 6:11:37so do that anywhere within the window
- 6:11:39between window equals tk and window.main
- 6:11:42loop
- 6:11:42right here is good i suppose we're going
- 6:11:44to create a photo image
- 6:11:46from this image and this is a png file
- 6:11:48but yours might be something different
- 6:11:50so let's give our photo image a unique
- 6:11:53name like maybe icon
- 6:11:54and in order to create a photo image
- 6:11:56we'll follow this with equals
- 6:11:58photo image and within the constructor
- 6:12:01of our photo image
- 6:12:02there is an option for file we can
- 6:12:05either list
- 6:12:05the file name or the file path if it's
- 6:12:08somewhere else on your computer
- 6:12:09since this image is within my same
- 6:12:12project folder i only have to list the
- 6:12:14file name
- 6:12:15and this is called logo.png
- 6:12:18and this is probably going to be named
- 6:12:19something else for you though
- 6:12:21so we now have this photo image called
- 6:12:24icon
- 6:12:24that we can use and i need to set the
- 6:12:27icon
- 6:12:28of my window to this icon and luckily
- 6:12:31there is a function to take care of that
- 6:12:33for us
- 6:12:33type in the name of the window which is
- 6:12:35window
- 6:12:36dot and we're going to use the icon
- 6:12:39photo
- 6:12:40function there are two arguments the
- 6:12:43first we're going to set to true
- 6:12:46and the second is the photo image that
- 6:12:48we want to use
- 6:12:49and this photo image is called icon so
- 6:12:51we're going to pass
- 6:12:53this photo image as an argument to this
- 6:12:55function
- 6:12:56of icon photo and now when we compile
- 6:12:58and run this
- 6:12:59the image that we wanted to use is now
- 6:13:02set for the top left icon of this window
- 6:13:05bar
- 6:13:05however it loses a lot of the quality
- 6:13:07since the dimensions are so small
- 6:13:09but you can still tell that it's my logo
- 6:13:11though so that's how to replace
- 6:13:13the icon of your window bar in the top
- 6:13:15left corner now let's change the
- 6:13:17background color
- 6:13:18of this window and one way in which we
- 6:13:20can do that is to use the config
- 6:13:22function you can use the config function
- 6:13:24anytime that you want to make any
- 6:13:26changes to this window
- 6:13:27so let's change the background color
- 6:13:29here and there is an option called
- 6:13:31background and we can set this equal to
- 6:13:34either
- 6:13:34a color name or a hexadecimal value
- 6:13:37let's begin with just some color names
- 6:13:39let's say that i want the background
- 6:13:41color to be black
- 6:13:42i'll just type in the word black here
- 6:13:44and this will change the background
- 6:13:46color of my window to black
- 6:13:48alternatively you can set a hex value as
- 6:13:51the color
- 6:13:52so if you were to look up or google hex
- 6:13:55color picker
- 6:13:56you can select whatever color you want
- 6:13:58let's say that i want
- 6:13:59a sky blue color like this well this is
- 6:14:03the hex value i'm going to copy this
- 6:14:05value
- 6:14:07and then paste it within the option for
- 6:14:09my background
- 6:14:10make sure you have this hashtag as well
- 6:14:12because that represents that this is a
- 6:14:14hex value so now the background color is
- 6:14:17that sky blue color that i picked
- 6:14:20well everybody that's the basics of
- 6:14:21creating a window in python and in
- 6:14:24future videos we're going to be
- 6:14:25exploring
- 6:14:26different widgets that we can add to
- 6:14:28this window that have some functionality
- 6:14:30so if you would like a copy of all this
- 6:14:32code i'll post all of this in the
- 6:14:33comments down below
- 6:14:34but yeah that's how to create your very
- 6:14:36first gui in python
- 6:14:38hey what's going on everybody it's your
- 6:14:40bro here hope you're doing well and in
- 6:14:42this video i'm going to teach you guys
- 6:14:43how we can create
- 6:14:44labels in python so sit back relax and
- 6:14:47enjoy the show
- 6:14:49what is a label well i'm glad you asked
- 6:14:52that question
- 6:14:52it's an area widget that holds text and
- 6:14:55or
- 6:14:56an image within a window so to begin
- 6:14:58we'll need to create a window to act as
- 6:15:00a container
- 6:15:02to hold this label we can create a
- 6:15:04window with
- 6:15:05window equals tk followed by
- 6:15:07window.mainloop
- 6:15:08and this will give us a window in which
- 6:15:10we can add a label to
- 6:15:12now let's give our label a unique name
- 6:15:15such as label
- 6:15:16and in order to instantiate a label
- 6:15:18we'll follow this label name
- 6:15:20with equals label with a capital l and a
- 6:15:23set of parentheses
- 6:15:24the parentheses are acting as the
- 6:15:26constructor for this widget
- 6:15:28our label and we can pass in arguments
- 6:15:30into
- 6:15:31these parentheses the first argument
- 6:15:33that we're going to pass
- 6:15:34in is a master the container for this
- 6:15:37label
- 6:15:38we're going to pass in the name of our
- 6:15:40window because our window is acting as
- 6:15:42the container
- 6:15:43with widgets in python we can
- 6:15:45de-eliminate the arguments that we're
- 6:15:47passing in
- 6:15:48with a comma and there are options
- 6:15:50available to us
- 6:15:52options are keyword arguments that we
- 6:15:54can pass in to the constructor for this
- 6:15:56widget
- 6:15:57one option available to us is text and
- 6:16:00we can set the text of this label equal
- 6:16:02to
- 6:16:02a string that we set so let's say that
- 6:16:05we want this label to say
- 6:16:07hello world well we're going to pass in
- 6:16:09this option
- 6:16:10text and it's a keyword argument
- 6:16:14so if we were to do this we're going to
- 6:16:16set the text
- 6:16:17of the label but this label currently
- 6:16:20doesn't appear within our window
- 6:16:21what gives bro i thought you knew how to
- 6:16:23code hold up wait a minute there's one
- 6:16:25more thing that we need to do
- 6:16:27so we're going to actually add this
- 6:16:29label
- 6:16:30to the window now we set the master
- 6:16:33of the label to be our window but now we
- 6:16:35actually have to add it as step two
- 6:16:38one way which we can do that is to use a
- 6:16:40pack
- 6:16:41function of our label so follow label
- 6:16:44equals label and pass in all of your
- 6:16:46arguments
- 6:16:46and then follow this with the name of
- 6:16:48the label dot pack
- 6:16:51so with the pack option by default this
- 6:16:53will place our widget in the top
- 6:16:55center of our window another way in
- 6:16:58which we can add a label
- 6:17:00to a container or window is to use the
- 6:17:02place function
- 6:17:03and we can set some coordinates of where
- 6:17:05we want this label or other widget to
- 6:17:08appear
- 6:17:08within the window if we want this to
- 6:17:10appear in the top left corner
- 6:17:12we can set x to equal zero and y t
- 6:17:15equals zero and these are options
- 6:17:17available to us too that we can pass
- 6:17:18in so this place function will place
- 6:17:22our label at some position or
- 6:17:25some set of coordinates within the
- 6:17:27window and with
- 6:17:28x equals 0 and y equals 0 that will
- 6:17:31place our widget in the top left corner
- 6:17:33of the window
- 6:17:34and if i were to change these to let's
- 6:17:36say x equals 100
- 6:17:38and y equals 100 well this is going to
- 6:17:40be placed
- 6:17:42down by 100 pixels and to the right by
- 6:17:45100 pixels
- 6:17:46so you can use pack or place there's a
- 6:17:48few others
- 6:17:49like grid but they're a little more
- 6:17:51complex so we'll get to that in a future
- 6:17:53video
- 6:17:53for the time being i'm going to use the
- 6:17:55pack function of the label
- 6:17:57and turn this place function into a
- 6:17:59comment
- 6:18:01dot pack all right now
- 6:18:04how can we customize this label we can
- 6:18:07pass in different options
- 6:18:08when we instantiate this label let's
- 6:18:10change the font and that's another
- 6:18:12option
- 6:18:13we can change the font with font equals
- 6:18:16and then we can pass in a font let's
- 6:18:18begin with a
- 6:18:19font family i will say maybe ariel
- 6:18:22a size perhaps 40 and then a style if
- 6:18:26you so choose
- 6:18:28like bold there's also italic and
- 6:18:30underline
- 6:18:31uh but this will work just fine then so
- 6:18:33this will change the font
- 6:18:35of the text of our label and it says
- 6:18:38hello world still
- 6:18:39now let's change the foreground color
- 6:18:43so that is another option the foreground
- 6:18:46is the font color
- 6:18:47and the option is fg for foreground it's
- 6:18:50short for foreground
- 6:18:51foreground equals we can state a color
- 6:18:54name
- 6:18:55such as green and our text
- 6:18:58should now be green you can select
- 6:19:00something more specific by
- 6:19:02passing in a hex value so a custom color
- 6:19:05that i like
- 6:19:06is hashtag zero zero ff00
- 6:19:10this is bright green you can always look
- 6:19:12up what different hex values are
- 6:19:14and we can also change the background
- 6:19:16color too
- 6:19:17that is bg short for background color
- 6:19:20and we can pass in a color name
- 6:19:22or a hex value i'm just going to set
- 6:19:24this to black just to demonstrate that
- 6:19:26we can pass in both
- 6:19:27either hex values or color names and
- 6:19:30what we have
- 6:19:31is green text on a black background
- 6:19:34and this is the limits of our label
- 6:19:37within the window
- 6:19:38because when we expand this window the
- 6:19:41label is still going to stay the same
- 6:19:42size
- 6:19:43if we were to change the text within the
- 6:19:46label
- 6:19:46our label is only going to take up the
- 6:19:49room that it needs
- 6:19:50and you can see that our label is now
- 6:19:52smaller now we have all of these
- 6:19:54arguments within our constructor and
- 6:19:55it's getting a little bit difficult to
- 6:19:57actually read
- 6:19:58so i'm going to separate each of these
- 6:20:00arguments line by line just so that this
- 6:20:02is more organized
- 6:20:03but it'll work just the same as it did
- 6:20:05before now how do we add a
- 6:20:07border around our label one we can set
- 6:20:10the border style
- 6:20:11with the relief option and one border
- 6:20:14that i like
- 6:20:14is raised but right now the border width
- 6:20:18is one so it's a little difficult for us
- 6:20:21to actually see the border
- 6:20:22let's increase the border width with the
- 6:20:24option bd
- 6:20:26and i'll set this equal to maybe 10.
- 6:20:29so this border is now more visible
- 6:20:32another relief
- 6:20:33which is the border style is sunken and
- 6:20:36this will give our label a
- 6:20:38sunken appearance but i think i'll
- 6:20:40change this back to raised
- 6:20:42we can also add some padding around the
- 6:20:45text
- 6:20:45between the text and the border we can
- 6:20:47pad x and pad y
- 6:20:49pad x will add some padding some space
- 6:20:52between
- 6:20:53the x-axis of our text between the
- 6:20:55border so let's set this to maybe 20
- 6:20:57and we now have 20 pixels worth of space
- 6:21:00between
- 6:21:01the text and the border let's also pad y
- 6:21:06this will add some space above and below
- 6:21:10our text between the text and the border
- 6:21:13now how do we add an image to a label we
- 6:21:16first need to create what's called
- 6:21:18a photo image and we will create this
- 6:21:21within
- 6:21:21the lines of window equals tk and
- 6:21:24window.main loop
- 6:21:25and let's give this photo image a unique
- 6:21:27name like
- 6:21:28photo and in order to create a photo
- 6:21:31image we're going to follow this with
- 6:21:33photo equals photo image
- 6:21:36and within the constructor of our photo
- 6:21:38image there's an option called
- 6:21:40file and we can list a file path or a
- 6:21:43file name
- 6:21:44right now on my desktop there is a image
- 6:21:46that i want to use and i need to convert
- 6:21:48this to a photo image
- 6:21:50here's my desktop and this is an image
- 6:21:52that i want to use to add to my label
- 6:21:54i need to get the file path and i can
- 6:21:56get that by going to properties
- 6:21:58then underneath location i can copy this
- 6:22:01location address
- 6:22:03and paste it within the quotes for file
- 6:22:07and then i need to add the image name
- 6:22:09and this is person.png
- 6:22:12and with these backslashes these need to
- 6:22:14be double backslashes because
- 6:22:16that is the escape character for a
- 6:22:18backslash
- 6:22:20but what would be a better option is to
- 6:22:23add
- 6:22:23this image to my project folder that
- 6:22:25contains the python file that i'm
- 6:22:27working with
- 6:22:28so in place of listing the entire file
- 6:22:31path
- 6:22:31i'm going to copy the image that i want
- 6:22:33to use go to my project folder
- 6:22:36and then paste it and i no longer need
- 6:22:40this entire file path i can just list
- 6:22:42the image name that i have
- 6:22:44and now what we need to do is add this
- 6:22:46photo to the label and there's an option
- 6:22:48to do so
- 6:22:49that is the image option and we'll place
- 6:22:52this within the constructor for the
- 6:22:53label
- 6:22:54we're going to set image equal to the
- 6:22:56photo image that we have
- 6:22:58and our photo image is called photo this
- 6:23:00will add our photo image
- 6:23:02to our label or other widget depending
- 6:23:04on what we're working with
- 6:23:06so we have our image but it replaced all
- 6:23:09of the text
- 6:23:10that we had for this label how do we add
- 6:23:12both
- 6:23:13text and an image to a window well we
- 6:23:16can follow this
- 6:23:16with the compound option we can set a
- 6:23:19direction of where
- 6:23:21we want this image to be placed relative
- 6:23:23to the text that we have
- 6:23:25so i'm going to set compound equal to
- 6:23:28bottom
- 6:23:28so that the image appears on the bottom
- 6:23:31or underneath
- 6:23:31our text so we can both have text and an
- 6:23:35image
- 6:23:35or if i want this to be in a different
- 6:23:37location i could set this to maybe top
- 6:23:39left or right if i were to set this to
- 6:23:41top we have
- 6:23:42our image on top of our text and one
- 6:23:46thing to know about labels and many
- 6:23:48other widgets in python is that
- 6:23:50the size of the widget will actually
- 6:23:52increase to accommodate
- 6:23:54the size of all the components that's
- 6:23:56contained within if i were to give the
- 6:23:58text a longer string of characters such
- 6:24:01as
- 6:24:02bro do you even code then
- 6:24:05the width of our label is actually going
- 6:24:07to expand
- 6:24:08to accommodate the size of the longer
- 6:24:11string of text that we have
- 6:24:13so that's the basics of labels everyone
- 6:24:15if you would like a copy of all this
- 6:24:17code i will post all of this in the
- 6:24:18comments down below
- 6:24:20but yeah that's the basics of labels in
- 6:24:22python
- 6:24:24hey what's going on everybody it's your
- 6:24:26bro here hope you're doing well
- 6:24:27and in this video i'm going to teach you
- 6:24:28guys how we can create buttons in python
- 6:24:31so sit back relax and enjoy the show
- 6:24:35buttons you click them and then they do
- 6:24:38stuff that's pretty much it
- 6:24:39so this is how to create a button in
- 6:24:41python we'll need a window
- 6:24:43window equals tk followed by window.main
- 6:24:46loop
- 6:24:46let's call our button just button and in
- 6:24:49order to create this we'll follow this
- 6:24:51with
- 6:24:51equals button and within the constructor
- 6:24:55for this button we're going to list
- 6:24:56what we want to add this button to what
- 6:24:59is the master
- 6:25:00the master is the window and in order to
- 6:25:03display this button
- 6:25:04we're going to follow this with button
- 6:25:07dot
- 6:25:07pack and we should have a small
- 6:25:11rectangular button within our window
- 6:25:13but it doesn't display any text nor does
- 6:25:16it do anything
- 6:25:17so let's add some text and there is an
- 6:25:19option to do that
- 6:25:20within the constructor we're going to
- 6:25:22list the option for text
- 6:25:23and set this to whatever text you want
- 6:25:25to display let's
- 6:25:26say click me and then when we run
- 6:25:30this our button now says the text that
- 6:25:32we set
- 6:25:33and it says click me however it
- 6:25:35currently doesn't do anything though
- 6:25:37we need to set a command that is another
- 6:25:39option
- 6:25:40command equals and we list a function
- 6:25:43name this is what's known as a callback
- 6:25:46so let's create a function
- 6:25:47called click and then outside of the
- 6:25:50window let's define what our click
- 6:25:51function does def click
- 6:25:55and let's print a message you
- 6:25:58clicked the button
- 6:26:02now with the command we're going to list
- 6:26:05the
- 6:26:05function name without the parentheses so
- 6:26:08make sure you're writing it as
- 6:26:10this and not that this is what's known
- 6:26:12as a callback
- 6:26:14so when we click on this button it's
- 6:26:16going to perform our callback
- 6:26:19and perform whatever is within our
- 6:26:21function
- 6:26:22click just like that let's customize the
- 6:26:25appearance of this button i'm going to
- 6:26:27change the font
- 6:26:28that is another option font equals
- 6:26:31and let's pick a professional font such
- 6:26:34as comic
- 6:26:35sans it's very professional and i'll set
- 6:26:38a size of 30.
- 6:26:41and now you can actually read what's on
- 6:26:43this button it's large enough
- 6:26:46let's set the foreground color that is
- 6:26:48the font color fg
- 6:26:49you can place a color name or a hex
- 6:26:52value here
- 6:26:53i'll pick green that is 0 0 ff00
- 6:26:57and our text should now be green which
- 6:27:00it is
- 6:27:01let's also change the background color
- 6:27:04that is bg
- 6:27:05i will pick the color name of black and
- 6:27:08now we have
- 6:27:09green text on a black background notice
- 6:27:12that when i click on this button
- 6:27:14or hold my mouse button down the color
- 6:27:16scheme has changed that's because
- 6:27:18there is a different active foreground
- 6:27:21and active background
- 6:27:22and we can change those too so that this
- 6:27:24won't flash every time you click on the
- 6:27:26button
- 6:27:27so let's change the active foreground
- 6:27:29and active background
- 6:27:30those are other options active
- 6:27:33foreground
- 6:27:34and i'll set this to the same color as
- 6:27:36my foreground
- 6:27:38which is green so the text color is
- 6:27:41going to stay the same
- 6:27:42and i'll also change the active
- 6:27:44background
- 6:27:47and i will also set this to black to
- 6:27:49match my
- 6:27:50background and then this should no
- 6:27:53longer flash
- 6:27:54when we click on our button
- 6:27:57if you need to disable somebody from
- 6:27:59clicking on this button
- 6:28:00there is an option to do so and that
- 6:28:02option is the state
- 6:28:04option and normally this is active but
- 6:28:07you can set this to
- 6:28:08disabled and we can no longer click on
- 6:28:11this button
- 6:28:12and you can see that the color scheme
- 6:28:14has changed too it's all grayed out and
- 6:28:16whatnot
- 6:28:17so i'm going to replace this back with
- 6:28:19active because we still have a few more
- 6:28:21things to do
- 6:28:22now how do we add an image to a button
- 6:28:24there is an
- 6:28:25image option and we set this equal to a
- 6:28:28photo image
- 6:28:28but first we need to create a photo
- 6:28:30image i have this photo of
- 6:28:32a like button a thumbs up that i want to
- 6:28:34use i need to create a photo image from
- 6:28:37this image file
- 6:28:38so within the window let's call this
- 6:28:40photo image just
- 6:28:42photo photo equals
- 6:28:46photo image then i'm going to list the
- 6:28:49file path or the file name
- 6:28:51since this image is within my same
- 6:28:53project folder
- 6:28:54i only have to list the file name and
- 6:28:57this is called
- 6:28:58like.png but if you use your own image
- 6:29:00it's probably going to be named
- 6:29:01something else
- 6:29:02then i need to set the image equal to my
- 6:29:05photo image which i named
- 6:29:07photo but the photo replaces the text on
- 6:29:11this button but it still works the same
- 6:29:14so we would like to display some text as
- 6:29:16well as our image
- 6:29:17we need to use the compound option
- 6:29:21and we list a direction we can list
- 6:29:24top bottom left right we want this image
- 6:29:27to be
- 6:29:28on the perhaps bottom so let's list the
- 6:29:31bottom
- 6:29:32and now when we run this we have our
- 6:29:34text as well as our image
- 6:29:36and if we changed compound to a
- 6:29:37different direction like
- 6:29:39top then the image is going to be
- 6:29:41displayed
- 6:29:42on the top relative to the text
- 6:29:45what if we wanted to count the amount of
- 6:29:47times that we clicked this button
- 6:29:49we could create a variable called count
- 6:29:52and we'll set this equal to
- 6:29:53zero but in order for us to access this
- 6:29:56count variable
- 6:29:58within the function of click we're going
- 6:30:01to list
- 6:30:02count as global so that this is a global
- 6:30:06variable
- 6:30:06every time we click this button let's
- 6:30:09increment our variable count
- 6:30:10by one by saying count plus equals one
- 6:30:14and then we will print whatever the
- 6:30:16value of count is
- 6:30:18so within a print statement we'll just
- 6:30:20say count
- 6:30:21now when we click on this button we have
- 6:30:23access to our count
- 6:30:24variable that's outside of this function
- 6:30:27and every time we click on this button
- 6:30:28it's going to increase our count
- 6:30:31variable by
- 6:30:32one well everybody that's the basics of
- 6:30:34creating
- 6:30:35buttons in python if you want a copy of
- 6:30:37all this code i'll post all of this in
- 6:30:39the comments down below
- 6:30:40but yeah that's the basics of buttons in
- 6:30:43python
- 6:30:44hey what's going on everybody it's your
- 6:30:46bro here hope you're doing well and in
- 6:30:48this video i'm going to teach you guys
- 6:30:49how we can create a simple entry box in
- 6:30:51python
- 6:30:52so sit back relax and enjoy the show
- 6:30:56if you find this video helpful please
- 6:30:58remember to like
- 6:30:59comment and subscribe your support will
- 6:31:02help keep this channel running
- 6:31:04alright my fellow bros in this video
- 6:31:06we're going to be discussing the entry
- 6:31:07widget
- 6:31:08this is a text box that accepts a single
- 6:31:10line of user input
- 6:31:12as always we'll need a window window
- 6:31:14equals tk
- 6:31:15followed by window.main loop and let's
- 6:31:17give our entry box a unique name let's
- 6:31:19call
- 6:31:20this entry for short and then to create
- 6:31:22this entry widget we're going to follow
- 6:31:23this with equals
- 6:31:25entry and within the constructor we have
- 6:31:28to state what we're adding this to
- 6:31:29we're going to add this to our window
- 6:31:32and we should finish this by packing it
- 6:31:34or placing it
- 6:31:36entry dot pack and we should have an
- 6:31:39entry box within
- 6:31:40our window however the text is somewhat
- 6:31:43small so let's change that
- 6:31:45we can set this as an option within the
- 6:31:47constructor of our entry box
- 6:31:50so let's set the font font equals and
- 6:31:53pick whatever font you want
- 6:31:54i'm going to pick maybe ariel and then a
- 6:31:57size
- 6:31:58it'd be 50. and this should be a little
- 6:32:00bit larger
- 6:32:02and we can type in some
- 6:32:06text like this cool right
- 6:32:10now we should add a submit button
- 6:32:13because right now this currently doesn't
- 6:32:14do anything if we type some text in
- 6:32:16so let's create a button i'm going to
- 6:32:18create this button maybe after
- 6:32:20the entry box and let's say that this is
- 6:32:23a submit button so let's call this
- 6:32:25submit button equals button
- 6:32:28what are we adding this to writing the
- 6:32:30square window
- 6:32:32and we'll set some text text equals
- 6:32:35submit
- 6:32:36we'll also need a command for this
- 6:32:37button
- 6:32:39command equals and we'll create a
- 6:32:42function called submit
- 6:32:43and we want to pack the submit button to
- 6:32:46add it to the window
- 6:32:47submit button.pack
- 6:32:50all right now let's put our entry box on
- 6:32:53the left hand side and our submit button
- 6:32:55on the right hand side
- 6:32:56when you pack these widgets you can set
- 6:32:58these on a certain side
- 6:33:00side equals left and our submit button
- 6:33:04we want this on the right not necessary
- 6:33:06but i think it looks better
- 6:33:08and we also need to define our submit
- 6:33:10function outside of the window
- 6:33:12let's define what this does def submit
- 6:33:16what's this going to do we need to get
- 6:33:19the current value that's within our
- 6:33:21entry box
- 6:33:22one way in which we can do that is use
- 6:33:24the entry boxes get
- 6:33:26function this will return a string and
- 6:33:29we can do something with that string
- 6:33:30such as store this within a variable so
- 6:33:33let's create a variable called maybe
- 6:33:35username
- 6:33:35let's pretend we're having a user submit
- 6:33:38their username to login to something i
- 6:33:40suppose
- 6:33:41so we'll get the current value of the
- 6:33:43entry box
- 6:33:44store that within a variable and we'll
- 6:33:46call this username
- 6:33:47and then we will maybe print this for
- 6:33:49fun print
- 6:33:52hello plus user
- 6:33:55name and let's try this
- 6:33:58so here's our entry box here and our
- 6:34:00submit button i'm just going to make up
- 6:34:02something
- 6:34:03click submit and it displays our message
- 6:34:06hello ergo gerger gerger
- 6:34:09i didn't say that this video was going
- 6:34:10to be professional let's add a few other
- 6:34:12buttons
- 6:34:13let's say we want a delete button as
- 6:34:15well as a backspace button
- 6:34:18so let's copy what we have for our
- 6:34:20submit button and create a delete button
- 6:34:22and then a backspace button the delete
- 6:34:24button is a little bit easier to code
- 6:34:26so let's rename our submit button as
- 6:34:28delete button
- 6:34:31make sure to change the name to when you
- 6:34:33pack this and the text
- 6:34:34we will say delete and we'll create a
- 6:34:38function called
- 6:34:38delete and let's define what delete does
- 6:34:42so after our submit function let's
- 6:34:44define
- 6:34:46our delete function and we need to
- 6:34:50delete all of the text within our entry
- 6:34:52box
- 6:34:53one way in which we can do that is to
- 6:34:55type in the name of our entry box
- 6:34:57dot delete function
- 6:35:00and this takes two positional arguments
- 6:35:03which portion of our entry box do we
- 6:35:06want to delete
- 6:35:07so we want the very first character that
- 6:35:09is index
- 6:35:100 followed by the last character and we
- 6:35:13can say
- 6:35:13end so this will delete all of the
- 6:35:16characters
- 6:35:16within our entry box and let's try it so
- 6:35:19i'm just going to type in my name
- 6:35:21bro i'm going to delete this and all of
- 6:35:24the text is now gone
- 6:35:27just like that let's create a backspace
- 6:35:29button we'll copy what we have for our
- 6:35:31delete button and change
- 6:35:32the word delete to backspace
- 6:35:37make sure you change that when you pack
- 6:35:38this as well and for the text we'll set
- 6:35:40this to
- 6:35:41backspace and we'll create a function
- 6:35:44called backspace
- 6:35:46and let's define what backspace does so
- 6:35:49after
- 6:35:49our delete function let's define back
- 6:35:52space and this is going to do something
- 6:35:55very similar to our delete function
- 6:35:58except we're going to change the first
- 6:36:02positional argument
- 6:36:03we need to place the second to last
- 6:36:06character
- 6:36:06within this positional argument one way
- 6:36:09in which we can do that
- 6:36:10is to first get the entire length of all
- 6:36:13of the characters within our entry box
- 6:36:16so let's begin by typing entry dot
- 6:36:19get and we need to determine the length
- 6:36:21of this
- 6:36:22and we can surround this by placing this
- 6:36:25within the length function so this will
- 6:36:28return
- 6:36:29the current length of all of the
- 6:36:31characters within our entry box
- 6:36:33and then we're going to add -1 so that
- 6:36:36this
- 6:36:36first positional argument is the second
- 6:36:38to last character
- 6:36:40and the second positional argument is
- 6:36:42the end the last character
- 6:36:44and then when we run this let's type in
- 6:36:47our name i'm just going to type in draw
- 6:36:49code this time i'm going to click
- 6:36:50backspace and then it's only going to
- 6:36:52delete the last
- 6:36:53character and then if you were to click
- 6:36:55delete it deletes everything
- 6:36:58broy and then if you click submit it's
- 6:37:01going to submit your name
- 6:37:03all right let's customize the appearance
- 6:37:05of this entry box
- 6:37:06so within the constructor for this entry
- 6:37:09box
- 6:37:10let's change maybe the color maybe the
- 6:37:13background color and foreground color
- 6:37:14let's begin with the foreground color
- 6:37:16you can pass in a color name or a hex
- 6:37:18value
- 6:37:18i want this to be green so the hex value
- 6:37:21for that is
- 6:37:21zero zero f zero ff00 and the foreground
- 6:37:25color
- 6:37:25of the text when we type it in is now
- 6:37:28green
- 6:37:29and we can also change the background
- 6:37:31color that is bg
- 6:37:33we could pass in a color name or a hex
- 6:37:35value i'm just going to say black to
- 6:37:37keep it simple
- 6:37:38and we now have a black background and
- 6:37:40the text color is now
- 6:37:42green here's a few useful features that
- 6:37:45you might be interested in
- 6:37:47let's insert some default text for our
- 6:37:49entry box
- 6:37:50so we can use the insert function entry
- 6:37:53dot insert
- 6:37:54we place a positional argument as well
- 6:37:56as some text
- 6:37:57so for the positional argument let's say
- 6:37:59zero so that's the very beginning
- 6:38:01index zero and let's set the text to
- 6:38:04maybe
- 6:38:05spongebob and then the default text
- 6:38:08within our entry box is now
- 6:38:10spongebob another option available to us
- 6:38:12is that we can set the state
- 6:38:15of this entry box we can set this to
- 6:38:17active or disabled
- 6:38:19let's say that after somebody submits
- 6:38:20our name we want to disable
- 6:38:22this entry box so we can either pass in
- 6:38:25this option
- 6:38:27within our constructor or we can use the
- 6:38:30config function to make any changes
- 6:38:32to the state of our entry box so let's
- 6:38:35say after we submit our username we want
- 6:38:37to disable this entry box
- 6:38:39so let's type in the name of our entry
- 6:38:41box dot config
- 6:38:44and for the option for state let's set
- 6:38:48this to
- 6:38:49disabled and then once we type in a name
- 6:38:54and then click submit our entry box is
- 6:38:57now disabled
- 6:38:58there's also an option called show where
- 6:39:02you can show a certain character in
- 6:39:04place of the normal text on the screen
- 6:39:06so this would be useful for a password
- 6:39:09let's say that we're typing in
- 6:39:10a password but we don't want to display
- 6:39:13the password on the screen
- 6:39:14we can use the show option to show a
- 6:39:17certain character in place
- 6:39:19of our text so you might see bullets or
- 6:39:22an asterisk
- 6:39:23so let's pretend that we're now typing
- 6:39:24in a password so we're going to set
- 6:39:27the option for show and place a
- 6:39:30character that we want to replace each
- 6:39:32character with
- 6:39:33let's say we want to only display
- 6:39:35asterisks when we type in
- 6:39:37some characters and i'm going to type in
- 6:39:39the word password
- 6:39:41but it's only displaying asterisks now
- 6:39:43but if i were to click submit
- 6:39:45it displays our text and i would say
- 6:39:48plain text instead of our hidden text
- 6:39:50because we're only showing
- 6:39:52whatever character that we set so that's
- 6:39:54an option available to you as well
- 6:39:55well everybody that's how to create an
- 6:39:57entry box in python you can submit
- 6:39:59information
- 6:40:00such as a username for example or even a
- 6:40:03password if you set the
- 6:40:04show option available to you if you want
- 6:40:07a copy of all this code i'll post all of
- 6:40:09this in the comments down below
- 6:40:10but yeah that's some of the uses of the
- 6:40:12entry widget in python
- 6:40:14hey how's it going everybody two bro
- 6:40:16here hope you're doing well and in this
- 6:40:18video i'm going to teach you guys how we
- 6:40:19can create and customize our own
- 6:40:21check buttons in python so sit back
- 6:40:23relax and enjoy the show
- 6:40:27all right my fellow bros in this video
- 6:40:30we're going to be creating
- 6:40:31and customizing our own check buttons in
- 6:40:33python i sometimes refer to these as
- 6:40:35check boxes
- 6:40:36so in case i do that's what i'm
- 6:40:38referring to as always we'll need a
- 6:40:40window
- 6:40:41window equals tk followed by window.main
- 6:40:44loop
- 6:40:44and let's call this button just check
- 6:40:47button and in order to create this check
- 6:40:50button
- 6:40:51we type in the name of the check button
- 6:40:53equals
- 6:40:54check button and within the constructor
- 6:40:58what are we adding this check button to
- 6:41:00we're adding this to our window
- 6:41:02and also within the constructor we can
- 6:41:04set the different options
- 6:41:05for how we want to customize this check
- 6:41:07button let's begin with the text
- 6:41:10text equals i agree
- 6:41:13to something and last but not least we
- 6:41:17need to add
- 6:41:18this check button to the window one way
- 6:41:20in which we can do that is
- 6:41:22to use the pack function of this widget
- 6:41:24so follow this with
- 6:41:26check button dot pack when we run this
- 6:41:28we have our check button within our
- 6:41:30window
- 6:41:31and we can toggle this on or off right
- 6:41:34now i'm agreeing to something
- 6:41:35i'm not sure what i'm agreeing to but
- 6:41:37hopefully it's not anything bad
- 6:41:38now currently this doesn't do anything
- 6:41:40so we'll need to associate a variable
- 6:41:42with this check button
- 6:41:43so we'll add that as an option variable
- 6:41:46equals and let's create a variable
- 6:41:49called x
- 6:41:50and we'll need to define this within the
- 6:41:52window so
- 6:41:53after we create our instance of our
- 6:41:55window let's say
- 6:41:57x equals now with check buttons
- 6:42:00they store a 1 or a 0 by default within
- 6:42:03our variable
- 6:42:04our variable that we call x so with
- 6:42:08x we're going to set this equal to a int
- 6:42:12var if this were to return a string we
- 6:42:15would say
- 6:42:16string bar but normally by default this
- 6:42:19returns a one
- 6:42:20or a zero and we can actually change
- 6:42:23that using the on value
- 6:42:25and off value options so the on value
- 6:42:28is what's going to be stored within our
- 6:42:31variable
- 6:42:32if it's toggled on so by default this is
- 6:42:35one
- 6:42:36and the off value is the opposite this
- 6:42:38is what is stored within
- 6:42:40our variable if this is toggled off
- 6:42:43alright now let's have some fun with
- 6:42:44this so let's associate a command with
- 6:42:47this check button
- 6:42:49command equals and let's create a
- 6:42:51function called
- 6:42:52maybe display and then let's define this
- 6:42:55before the window def display and we're
- 6:43:00going to check
- 6:43:00the value of x to see if it's one
- 6:43:04or zero so within our display function
- 6:43:07we're going to check to see
- 6:43:08if x and to get
- 6:43:11the value of x we're going to use the
- 6:43:14get function
- 6:43:15so if x dot get is equal to one
- 6:43:19that means that somebody clicked on the
- 6:43:20check box or toggled it on
- 6:43:23so if they check the check box
- 6:43:26or check the check button then they
- 6:43:29agree
- 6:43:30so print you agree
- 6:43:34if it's zero and we can do this with an
- 6:43:36else statement
- 6:43:37else print
- 6:43:41you don't agree frowny face
- 6:43:45and let's test this theory so we have
- 6:43:48our
- 6:43:49check button when we click on this it's
- 6:43:51actually going to call
- 6:43:52our display function i agree to
- 6:43:54something and it says within our console
- 6:43:56window
- 6:43:57you agree and if i were to toggle this
- 6:43:59off it says you don't agree
- 6:44:02let's change the font of our check
- 6:44:03button so we'll add that as an option
- 6:44:05font equals and we can pass in a font
- 6:44:08that we want
- 6:44:09i'll say maybe ariel and then a size
- 6:44:12of 20 and now this should be readable
- 6:44:16i agree to something let's also color
- 6:44:19this
- 6:44:20because well i like coloring let's begin
- 6:44:22with the foreground color
- 6:44:24so that is fg for short fg equals
- 6:44:27and then we can pass in a color name or
- 6:44:30a hex value
- 6:44:31i like hex values so i'm going to stick
- 6:44:33with those i want a green
- 6:44:35foreground color so that is zero zero f
- 6:44:39zero zero but feel free to adjust these
- 6:44:42values however you want
- 6:44:44so our text should now be green that is
- 6:44:47a
- 6:44:47very bright green and it hurts my eyes
- 6:44:49so i'm also going to change the
- 6:44:51background
- 6:44:51color which is bg and i'm going to set
- 6:44:54this to
- 6:44:54black so we'll have green text on a
- 6:44:57black background
- 6:44:59okay now when you click on this it
- 6:45:01flashes that's kind of annoying
- 6:45:03that's the active foreground and active
- 6:45:06background
- 6:45:07if we click on this or hold this down
- 6:45:09the color scheme switches to
- 6:45:11our active foreground slash background
- 6:45:13and we can change those too
- 6:45:15so i'm going to set those options active
- 6:45:19foreground i'm going to set this the
- 6:45:21same as my foreground color
- 6:45:25and the active background will be the
- 6:45:27same as
- 6:45:28our normal background so that's active
- 6:45:31background equals
- 6:45:35black and this should no longer flash
- 6:45:38when we click on it
- 6:45:41not too bad not too bad let's add some
- 6:45:43padding
- 6:45:45pad x i'll set this to 25
- 6:45:49and pad y i'll set this to
- 6:45:5210. we have a little more padding around
- 6:45:57our check button one feature that's
- 6:45:59available to us
- 6:46:00is that we can add an image next to our
- 6:46:03check button
- 6:46:04i downloaded an image of the python logo
- 6:46:07that i want to add
- 6:46:08so we'll use the image option to add a
- 6:46:12photo image
- 6:46:13to this check button but first we need
- 6:46:15to create a
- 6:46:16photo image from our image file so
- 6:46:19within the window
- 6:46:21let's call this python photo
- 6:46:24but you'll probably use something
- 6:46:25different for your project
- 6:46:27python photo equals photo
- 6:46:31image and within the constructor for our
- 6:46:33photo image
- 6:46:36we can set the file path or the file
- 6:46:38name
- 6:46:39since this image is within my project
- 6:46:41folder i only have to list the file name
- 6:46:44and this photo of mine is python.png
- 6:46:49and now we set the image equal to
- 6:46:52our photo image that we have image
- 6:46:55equals
- 6:46:56python photo but when we run this
- 6:46:59it's going to overlap the text we still
- 6:47:02have our image though
- 6:47:04and this still works as intended so in
- 6:47:06order to display
- 6:47:08the image as well as the text we're
- 6:47:11going to need to use the compound
- 6:47:13option and let's add that at the end
- 6:47:15compound
- 6:47:17is where we're adding this photo
- 6:47:19relative to the text
- 6:47:21i want this photo on the left hand side
- 6:47:23of the text
- 6:47:24so i'm going to set this to left and now
- 6:47:27when we run this
- 6:47:28surprise surprise our image is on the
- 6:47:30left hand side
- 6:47:31one thing that you should know is that
- 6:47:33with the on value and the off value data
- 6:47:35type if you were to change this
- 6:47:37to a non-integer value let's say we're
- 6:47:40going to replace the on value with a
- 6:47:42boolean value of true and the off value
- 6:47:45is false with this variable make sure to
- 6:47:49change the data type as well to reflect
- 6:47:52what you're storing within
- 6:47:53it so with our int var variable we want
- 6:47:56to change this to a
- 6:47:57boolean var value so replace int
- 6:48:01with boolean and this will work much the
- 6:48:04same
- 6:48:04now with this display function we'll
- 6:48:06need to account for that
- 6:48:08so within the if statement here we'll
- 6:48:10say if x
- 6:48:11dot get we don't need to say is equal to
- 6:48:14one
- 6:48:15this will return true or false and this
- 6:48:18will work
- 6:48:19just the same as it did before and if
- 6:48:21you replace this with a string
- 6:48:23let's say the on value is
- 6:48:26yes all caps and the off value is
- 6:48:30no this is now a string bar
- 6:48:34and we'll change our display function to
- 6:48:36say if x
- 6:48:38dot get is equal to yes
- 6:48:43and this should work you agree you don't
- 6:48:46agree you agree
- 6:48:47you don't agree and i'll change that
- 6:48:50back to one so
- 6:48:51one and zero is the default for this but
- 6:48:53you can edit these based on what you
- 6:48:55need
- 6:48:56all right so that's the basics of check
- 6:48:58buttons in python if you want a copy of
- 6:49:00all this code
- 6:49:01i'll post all of this in the comments
- 6:49:03down below but yeah that's the basics of
- 6:49:05check buttons in python hey what's going
- 6:49:08on everybody it's your bro here hope
- 6:49:10you're doing well and in this video i'm
- 6:49:11going to be teaching you guys how we can
- 6:49:12create radio buttons in python so sit
- 6:49:15back
- 6:49:15relax and enjoy the show
- 6:49:19okay everybody let's create some radio
- 6:49:21buttons now these are similar to check
- 6:49:23boxes but you can only select one from
- 6:49:25any single grouping
- 6:49:27so let's pretend that i'm buying you
- 6:49:29lunch and you can only select
- 6:49:30one item from a menu you can select
- 6:49:33either a
- 6:49:33slice of pizza a hamburger or a hot dog
- 6:49:37so let's begin by creating a list i will
- 6:49:39call this list
- 6:49:40food food equals
- 6:49:44pizza hamburger
- 6:49:48hot dog and let's create a window
- 6:49:52window equals tk followed by
- 6:49:55window dot main loop
- 6:49:59so to create a radio button we'll need a
- 6:50:01name so i'll call this radio button
- 6:50:04radio button equals radio button
- 6:50:08and we're going to be adding this to our
- 6:50:10window and we're going to put this
- 6:50:12within
- 6:50:12a for loop so we're going to iterate
- 6:50:15through all of the items within our list
- 6:50:17so let's create a for loop so we'll say
- 6:50:20for
- 6:50:22index in range
- 6:50:26length of food
- 6:50:30so this is going to iterate once through
- 6:50:32all of the elements within our list
- 6:50:35so when we run this it's going to create
- 6:50:38three
- 6:50:38radio buttons for us because we're
- 6:50:40instantiating
- 6:50:42one radio button for each item in the
- 6:50:44list
- 6:50:45now what i'm going to do is set the text
- 6:50:48equal to
- 6:50:51our list of food at a certain index
- 6:50:55so when we first run this for loop it's
- 6:50:57going to be zero for the index
- 6:51:00then one and then two and then so on and
- 6:51:03so forth for each item within our list
- 6:51:06and then we just need to pack this so
- 6:51:09radio
- 6:51:10button dot pack
- 6:51:14and then let's take a look at this
- 6:51:17alright so here are all of the radio
- 6:51:19buttons that we have
- 6:51:20currently they're all selected though so
- 6:51:22we'll need to link these within a single
- 6:51:25grouping
- 6:51:25the next thing we'll add is a variable
- 6:51:28and we'll set this equal to
- 6:51:29x and we'll need to declare this after
- 6:51:31we create an instance of our window
- 6:51:33so x equals int var
- 6:51:37so this will hold an integer object
- 6:51:40so these are all within the same
- 6:51:42grouping
- 6:51:43however if we try to run this again and
- 6:51:45select one of these options
- 6:51:47they all become selected so we're going
- 6:51:50to need to give
- 6:51:51each of these individual radio buttons
- 6:51:53their own
- 6:51:54value because right now they're all
- 6:51:56sharing the same value
- 6:51:57so we can add value equals
- 6:52:01whatever the index is currently so the
- 6:52:04first
- 6:52:04item within our list of food is going to
- 6:52:07have an
- 6:52:07index value of zero then one
- 6:52:11then two so let's try that again and you
- 6:52:14can see by default it's
- 6:52:15zero so pizza is already selected then
- 6:52:18we can select either hamburger or hot
- 6:52:20dog
- 6:52:20but we can only select one so that's
- 6:52:23kind of what we want then
- 6:52:25so do you guys usually know how i call
- 6:52:27the config function after each change
- 6:52:29that i make for an object
- 6:52:30well that's because i like to add
- 6:52:32comments just for teaching purposes
- 6:52:34for what each change does however it
- 6:52:37might be easier for you instead of
- 6:52:38calling the config function a bunch of
- 6:52:40times
- 6:52:41just to list all of the changes within
- 6:52:43the parentheses when you create an
- 6:52:45instance of the
- 6:52:46object that you're working with however
- 6:52:48just for teaching purposes i'm going to
- 6:52:50put this all within
- 6:52:52separate lines that allows me to add
- 6:52:54some comments
- 6:52:55so i think i'm going to start doing that
- 6:52:56from now on uh so what this does
- 6:52:59is that this adds text to
- 6:53:02radio buttons and this
- 6:53:06groups radio buttons together
- 6:53:10if they share the same variable
- 6:53:16so if you wanted a completely different
- 6:53:18grouping of radio buttons you can give
- 6:53:20them a different variable
- 6:53:21like y and what this line does
- 6:53:25is that this assigns
- 6:53:28each radio button a
- 6:53:32different value
- 6:53:37all right so let's make some cosmetic
- 6:53:39changes to our radio buttons
- 6:53:42so right now they are centered let's
- 6:53:45anchor these to the west
- 6:53:47so we'll place this within the pack
- 6:53:49function so
- 6:53:50anchor equals w alternatively you can
- 6:53:54just say capital w
- 6:53:56without the quotes this would also work
- 6:53:58too
- 6:54:00so now they're all lined up let's add
- 6:54:01some padding
- 6:54:03so i'm going to add pad x
- 6:54:06and i'll set this to 25
- 6:54:10and this adds
- 6:54:13padding on x
- 6:54:17axis
- 6:54:20let's also change the font
- 6:54:23so we'll save font
- 6:54:27equals maybe i'll pick impact for a font
- 6:54:31style
- 6:54:32and set the font size to 50
- 6:54:36and now we can actually read these
- 6:54:38because the font is large enough
- 6:54:40now let's add some images so let's
- 6:54:43create some photo images
- 6:54:44you'll want to be sure to create these
- 6:54:47after you create your instance of your
- 6:54:49window
- 6:54:50so let's create a pizza image first
- 6:54:53pizza image
- 6:54:54equals photo image
- 6:54:58file equals the file path or the file
- 6:55:01name
- 6:55:01and my file is pizza.png and it kind of
- 6:55:04looks like this
- 6:55:05it's just the pizza emoji so
- 6:55:10pizza.png
- 6:55:12and then i'll do the same thing for
- 6:55:14hamburger and hotdog
- 6:55:16so this will be hamburger image
- 6:55:20file equals hamburger.png
- 6:55:25and then hotdog image
- 6:55:35and this is what the other emojis look
- 6:55:37like
- 6:55:40and then let's create a list of the
- 6:55:42photo images that we have
- 6:55:44i'll call this food images
- 6:55:47equals than the names of these images
- 6:55:52pizza image hamburger image
- 6:55:56and hot dog image
- 6:56:01so then we can set the image for each
- 6:56:03radio button
- 6:56:05image equals
- 6:56:08the name of the list food images
- 6:56:11followed by
- 6:56:12the index that we're currently on within
- 6:56:15our for loop
- 6:56:16so this adds image
- 6:56:20to radio button and
- 6:56:23let's test this cool here's our images
- 6:56:27now if you want some images as well as
- 6:56:30text you'll have to use compound
- 6:56:32so let's add that next so i'm just going
- 6:56:36to add a comma
- 6:56:37followed by compound
- 6:56:43equals let's say left so this will add
- 6:56:46the image
- 6:56:47to the left of the text
- 6:56:52adds image and text i'll just say left
- 6:56:56side
- 6:56:59so this will display both an image and
- 6:57:02text
- 6:57:04so with these radio buttons you can
- 6:57:06eliminate these
- 6:57:07circle indicators and there's actually a
- 6:57:10way to do that
- 6:57:11so what we'll add if you want to get rid
- 6:57:13of those
- 6:57:14is indicator
- 6:57:18on equals zero
- 6:57:21so this will i should say eliminate
- 6:57:27circle indicators
- 6:57:32so it's going to change these to these
- 6:57:34push buttons
- 6:57:35however they are going to be of uneven
- 6:57:38size you can set the width
- 6:57:40if you want to so we'll do that
- 6:57:43so we'll say width equals
- 6:57:47what about 75 i'm not sure if that's a
- 6:57:50good size or not
- 6:57:52okay that's way too small let's try 375
- 6:57:59so yeah you can have some push buttons
- 6:58:00if you want to if you don't like the
- 6:58:02circle indicators
- 6:58:04so this sets width
- 6:58:07of radio buttons
- 6:58:11now let's actually set a function to
- 6:58:14uh be called when we click one of these
- 6:58:16buttons
- 6:58:18so let's call this function maybe order
- 6:58:20like we're ordering one of these items
- 6:58:24so def we'll call this order like we're
- 6:58:27ordering something
- 6:58:29and what we'll do is just a few if
- 6:58:31statements because i'm feeling lazy
- 6:58:33so we'll just say if x that's
- 6:58:37the value that's going to be stored
- 6:58:40because each of these radio buttons is
- 6:58:42grouped together by the variable x
- 6:58:45if x dot get
- 6:58:48is equal to zero that's our first index
- 6:58:52for pizza what we'll display is
- 6:58:57print you
- 6:59:01oops you ordered
- 6:59:04pizza then we'll just add
- 6:59:08else if x dot get
- 6:59:12is equal to one we will print
- 6:59:17you ordered a hamburger
- 6:59:25and then else if two
- 6:59:30you ordered a hot dog
- 6:59:37else
- 6:59:40print huh
- 6:59:44so there's probably a more efficient way
- 6:59:46to write this however
- 6:59:48i was just feeling lazy and this is
- 6:59:49probably the best way to keep this
- 6:59:51simple and easy to understand
- 6:59:53so let's try this now oh however we need
- 6:59:55to set the command
- 6:59:57for our radio buttons so
- 7:00:00command equals then the
- 7:00:04function name which is order and make
- 7:00:07sure you do not add the parentheses
- 7:00:10so don't do this do that
- 7:00:13and this will set
- 7:00:16command of
- 7:00:19radio button to function
- 7:00:25all right let's test this so if we click
- 7:00:28on pizza it says you ordered pizza
- 7:00:30you ordered a hamburger and you ordered
- 7:00:33a hot dog
- 7:00:35okay everybody so that's the basics of
- 7:00:37radio buttons if you would like a copy
- 7:00:39of all this code i'll post all of this
- 7:00:40in the comments down below
- 7:00:42but yeah that's the basics of radio
- 7:00:44buttons in python
- 7:00:46hey what's going on everybody it's you
- 7:00:48bro here hope you're doing well and in
- 7:00:50this video i'm going to teach you guys
- 7:00:51how we can create
- 7:00:52a sliding scale in python so sit back
- 7:00:55relax and enjoy the show
- 7:00:59let's create a scale so we'll need a
- 7:01:00window window equals tk
- 7:01:03followed by window.mainloop we'll create
- 7:01:06a scale
- 7:01:07called scale scale equals scale
- 7:01:10we need to add the scale to our window
- 7:01:12so we'll put that here
- 7:01:14we can list a from position and a 2
- 7:01:17position
- 7:01:18both of these values will be our range
- 7:01:20of values that we can use
- 7:01:22for our scale so from
- 7:01:25sure to add an underscore after from it
- 7:01:28won't work without this underscore
- 7:01:30from equals let's say 0 and 2 equals 100
- 7:01:34so we're going to create a scale for
- 7:01:36temperature
- 7:01:37and this will be in celsius so 0 will be
- 7:01:41freezing and 100 will be the boiling
- 7:01:43point uh you know for water
- 7:01:45so then to display the scale we'll need
- 7:01:48to add
- 7:01:48scale.pack and let's run this
- 7:01:52so the front position is zero so that's
- 7:01:54at the top here and then if you go down
- 7:01:56it increases so you can actually flip
- 7:01:58this so we'll save from
- 7:02:01102 will be zero
- 7:02:04so now you can scroll up and the number
- 7:02:06will increase
- 7:02:08let's also get the current value so
- 7:02:12we'll need a button
- 7:02:14so button equals button
- 7:02:18we'll add this to the window we'll add
- 7:02:20some text
- 7:02:22text equals submit and then a command
- 7:02:26command equals submit but we'll have to
- 7:02:29make this function
- 7:02:31so let's do that outside the window def
- 7:02:34submit and we will simply print the
- 7:02:38current temperature
- 7:02:39so let's display message the temperature
- 7:02:43is plus
- 7:02:47scale dot get
- 7:02:51plus degrees
- 7:02:55celsius we can just say degree c
- 7:02:59so let's try this uh but we also have to
- 7:03:01pack the button so button.pack
- 7:03:07so if we were to try and submit this
- 7:03:10right now scale.get is going to return
- 7:03:12an integer
- 7:03:13so we need to convert this to a string
- 7:03:16so
- 7:03:16str scale.get
- 7:03:20and you have to do that with python if
- 7:03:22you're displaying a
- 7:03:23integer value along with another string
- 7:03:26uh you need to convert it to a string
- 7:03:29so now this should work so it is 82
- 7:03:33degrees celsius
- 7:03:35so let's do some cosmetic changes for
- 7:03:37our temperature gauge
- 7:03:39because right now it's kind of ugly
- 7:03:43so the first thing we'll change is
- 7:03:46let's say the length so length equals
- 7:03:49600 so that should be fairly large
- 7:03:56you can also set the orientation so that
- 7:03:59is
- 7:03:59orient spelt like that
- 7:04:04so this can either be vertical which it
- 7:04:06currently is
- 7:04:09or horizontal
- 7:04:15but i'll keep this as vertical
- 7:04:19so i'll let a comment that this is
- 7:04:22orientation
- 7:04:23of scale let's also change the
- 7:04:27font so font equals
- 7:04:30pick whatever font you want
- 7:04:34and a size so maybe 20.
- 7:04:41now you can add some tick intervals
- 7:04:46maybe i'll set this to 10. so these will
- 7:04:50be
- 7:04:50numeric indicators on the scale
- 7:05:00and show value this hides the current
- 7:05:04value
- 7:05:04so if you set this to zero this will
- 7:05:06hide
- 7:05:07current value
- 7:05:12so it no longer displays what the
- 7:05:14current value is next to the slider
- 7:05:17to actually retrieve it you'd have to
- 7:05:19get the value
- 7:05:20so right now it's 40 but it helps that
- 7:05:22you have the tick intervals on the
- 7:05:24left-hand side
- 7:05:26i should probably add that this adds
- 7:05:31uh numeric
- 7:05:34indicators for
- 7:05:38value good enough let's continue on
- 7:05:41now let's use the set function we can
- 7:05:44set the current value of the knob
- 7:05:46on our scale and by default it is zero
- 7:05:49so we can use the scale
- 7:05:50dot set function and we can place a
- 7:05:53number in here
- 7:05:54a value so let's say we want this to be
- 7:05:57100
- 7:05:58so this is now at the 100 position
- 7:06:00instead of zero
- 7:06:01if we wanted this in the middle we could
- 7:06:03set this to 50
- 7:06:05and now it's in the middle however if
- 7:06:07you change the
- 7:06:09range of your skill of from minus 2
- 7:06:12and set this to 50 now 50 is all the way
- 7:06:15down here so it no longer appears in the
- 7:06:17middle
- 7:06:18so this isn't really necessary but if
- 7:06:20you want this to be a little more
- 7:06:21sophisticated
- 7:06:22there's actually a formula that you
- 7:06:23could use we can use scale
- 7:06:26straight braces and then place the word
- 7:06:28from
- 7:06:29within here this will give us our max
- 7:06:31basically
- 7:06:32uh so this is from without an underscore
- 7:06:35so this not this this
- 7:06:40scale from minus scale
- 7:06:43two and then we're going to divide this
- 7:06:47by two
- 7:06:48and then i'm just going to put this
- 7:06:50within a extra set of parentheses
- 7:06:53plus scale two just in case
- 7:06:57our two value is greater than zero
- 7:07:01we just want to account for that and
- 7:07:03then i'm just going to
- 7:07:05put these within another set of
- 7:07:06parentheses so
- 7:07:08this should always appear in the middle
- 7:07:10then even if we were to change this
- 7:07:14range
- 7:07:16yeah it seems like it works uh like i
- 7:07:18said it's not really necessary but
- 7:07:20if you want this to be a little more
- 7:07:21sophisticated that would work
- 7:07:23now let's make some cosmetic changes to
- 7:07:25this scale and make it look pretty
- 7:07:28so first let's change the trough color
- 7:07:30this is this gray portion right here
- 7:07:35so let's set the trough color
- 7:07:39equals whatever color you want you could
- 7:07:42say like blue
- 7:07:43or you can place a hex value here and i
- 7:07:45actually picked one out already
- 7:07:47so i'm going to use 6 9 e
- 7:07:50a f f and that is this icy blue color
- 7:07:54kind of like this is sliding on ice
- 7:07:58and i'm also going to change the font
- 7:08:00color
- 7:08:03and i'm going to pick this fiery red
- 7:08:06color
- 7:08:07to symbolize heat
- 7:08:12and then you can change the background
- 7:08:14color too
- 7:08:16so that is bg and i'll just set this to
- 7:08:20like black
- 7:08:24it's looking kind of cool now right and
- 7:08:27for no good reason i'm going to add some
- 7:08:29pictures
- 7:08:31so feel free to pause the video if you'd
- 7:08:32like to download some images i'm going
- 7:08:34to use an image of a flame
- 7:08:36for the hot side of our scale and a
- 7:08:39snowflake
- 7:08:40for the cold side so let's begin by
- 7:08:43adding the hot image uh to the scale
- 7:08:46before we actually
- 7:08:48add the scale to the window so we'll do
- 7:08:50this after we instantiate the window
- 7:08:52because that's the only place that we
- 7:08:53can do that
- 7:08:55so we're going to create a image i'll
- 7:08:56just call this hot image
- 7:08:58for the fire icon so hot image equals
- 7:09:01photo image file
- 7:09:04equals this is hot.png so
- 7:09:08hot.png we're going to add the image to
- 7:09:12a label
- 7:09:13i'll call this hot label hot label
- 7:09:17label image equals
- 7:09:20hot image and then we need to pack this
- 7:09:24so hot label dot pack
- 7:09:29and this should appear at the top let's
- 7:09:31test it
- 7:09:32yep there it is let's do the same thing
- 7:09:35for the cold side
- 7:09:37so i'll add this here before the button
- 7:09:41and i'm going to replace hot with cold
- 7:09:44same thing with the file name
- 7:09:45cold.png
- 7:09:49this will be cold label cold
- 7:09:52image and cold label dot pack
- 7:09:57and that should be it let's take a look
- 7:09:59not too shabby it's looking pretty sweet
- 7:10:02it has our submit button at the bottom
- 7:10:03too uh so
- 7:10:05right now the temperature is 100 degrees
- 7:10:07celsius
- 7:10:09the temperature is 0 degrees celsius the
- 7:10:11temperature is 60 degrees celsius
- 7:10:14well that's how to create a scale in
- 7:10:16python if you would like a copy of all
- 7:10:17this code i'll post all of this in the
- 7:10:19comments down below
- 7:10:20but yeah that's how to create a scale in
- 7:10:22python
- 7:10:24hey what's going on everybody it's your
- 7:10:26bro here hope you're doing well and in
- 7:10:27this video i'm going to teach you guys
- 7:10:29how we can create a list box in python
- 7:10:31so
- 7:10:32sit back relax and enjoy the show
- 7:10:36all right you wonderful people let's
- 7:10:38create a list box a list box
- 7:10:40is a listing of selectable text items
- 7:10:43within its own container
- 7:10:44so as always we'll need a window window
- 7:10:47equals tk
- 7:10:48followed by window dot main loop let's
- 7:10:51create a list box
- 7:10:52simply called list box big surprise
- 7:10:55there right
- 7:10:55so list box equals list box what are we
- 7:10:59adding this list box to
- 7:11:00we're going to place that here within
- 7:11:01the parentheses of the constructor
- 7:11:03we're adding this to our window and then
- 7:11:05we're going to follow this with
- 7:11:07listbox.pack you can also do
- 7:11:09listbox.place as well and place some
- 7:11:11coordinates but
- 7:11:12we've always done pack let's just stick
- 7:11:14with that for now all right so this is
- 7:11:16what our list box looks like
- 7:11:18it's a small rectangular box and we're
- 7:11:20going to add some text items that are
- 7:11:22selectable
- 7:11:23and eventually we'll be able to submit a
- 7:11:25choice so i have an idea
- 7:11:27let's create an online menu like we're
- 7:11:30ordering food from a restaurant
- 7:11:32let's say it's bros italian restaurant
- 7:11:34so let's add some food items to our menu
- 7:11:36and somebody can submit an order so
- 7:11:39we'll need to use the
- 7:11:40insert function of the list box to add
- 7:11:42some items so
- 7:11:43list box dot insert we place
- 7:11:47an index and a text item so let's say
- 7:11:50for
- 7:11:51index one we'll have pizza
- 7:11:54and this is what we end up with we have
- 7:11:56the item pizza
- 7:11:58it's selectable and well that's really
- 7:12:00it we'll need to create a submit button
- 7:12:02and populate the rest of this list box
- 7:12:04so let's add a few other items for our
- 7:12:06menu so let's say we'll have five items
- 7:12:10and we need an index for each so one two
- 7:12:13three four and five so we have
- 7:12:16pizza for the first item for the second
- 7:12:18item let's have pasta
- 7:12:20and third let's say garlic bread because
- 7:12:23garlic bread
- 7:12:24is awesome and restaurants usually have
- 7:12:26appetizers so why not soup and salad
- 7:12:30and that should be enough for this
- 7:12:31example
- 7:12:33and this is what we have we have our
- 7:12:36menu and we can select
- 7:12:37an item from this menu so let's
- 7:12:40customize the appearance of this menu
- 7:12:42well because we can so let's do that
- 7:12:45let's change the background color so we
- 7:12:46do that with the option
- 7:12:48bg for background and you can set this
- 7:12:50equal to a color of your choice
- 7:12:52or a hex value so i have a hex color
- 7:12:55value already picked out
- 7:12:57it is f7fde and this has the appearance
- 7:13:01of i would say
- 7:13:02paper like it's kind of faded i think
- 7:13:04that would fit some sort
- 7:13:06of menu for a restaurant but pick
- 7:13:08whatever background color you want
- 7:13:10so let's change the font next so we can
- 7:13:13do that with the option font
- 7:13:16and i will pick the font
- 7:13:22it's somewhat fancy i think it would fit
- 7:13:24like a restaurant menu
- 7:13:26and then a size of 35.
- 7:13:29yeah we're getting somewhere we can also
- 7:13:32change the width and the height
- 7:13:35so width equals let's say 12.
- 7:13:39that's not too bad for a width let's
- 7:13:41also change the height
- 7:13:43so we can do that with a function
- 7:13:46of our list box so let's get the current
- 7:13:49size of the list box
- 7:13:50and let's actually do that after we
- 7:13:53insert all of the items within our list
- 7:13:55box
- 7:13:55so to change the size of our list box
- 7:13:58what we do
- 7:13:59is type in list box
- 7:14:02dot config you usually use config if you
- 7:14:05need to change any uh
- 7:14:06options and we want to change the
- 7:14:09length of or the height of this list box
- 7:14:13so we're going to say height equals
- 7:14:17list box dot
- 7:14:20size function so then this will adjust
- 7:14:24the size
- 7:14:25of our list box dynamically even if we
- 7:14:28were to remove an item
- 7:14:30then the size of our list box is going
- 7:14:32to shrink
- 7:14:34just like that but let's keep salad in
- 7:14:37there for now because we need salad
- 7:14:40all right so how do we actually submit
- 7:14:43one of these options we'll need to
- 7:14:44create a submit button
- 7:14:46so let's do that after we create our
- 7:14:48list box so near the bottom
- 7:14:50we'll call this submit button
- 7:14:53submit button equals button we're adding
- 7:14:56this to our window
- 7:14:58let's set the text to submit
- 7:15:01and we'll need a command so we'll create
- 7:15:03a function called
- 7:15:05submit and then we need to pack
- 7:15:08this submit button
- 7:15:11submit button.pack now at the top of
- 7:15:14this program
- 7:15:15let's create a submit function
- 7:15:19so at the top def submit
- 7:15:24so to retrieve or get the current
- 7:15:27selected item
- 7:15:28of a list box there is a certain
- 7:15:30function that you can use
- 7:15:31so we'll begin by saying
- 7:15:35list box dot get
- 7:15:38what are we getting we want to get
- 7:15:39anything that is currently selected and
- 7:15:41there is a function
- 7:15:43for that so what we're going to do
- 7:15:45within the parentheses of the get
- 7:15:47function is type in
- 7:15:49listbox dot current selection
- 7:15:54it's shortened to cur selection all
- 7:15:57right and then we can either store this
- 7:15:59within a variable or we can just print
- 7:16:01this but i think we'll just print this
- 7:16:02to keep it simple
- 7:16:06now when we select an item and
- 7:16:09click on the submit button it's going to
- 7:16:11submit our order
- 7:16:12actually better yet let's actually uh
- 7:16:16print you have
- 7:16:20ordered whatever item that
- 7:16:25we select so garlic bread you have
- 7:16:27ordered garlic bread
- 7:16:29cool what we're going to work on now is
- 7:16:32a way to submit
- 7:16:33or insert an item to our list box after
- 7:16:36this program is compiled and already
- 7:16:38running
- 7:16:39so we'll create an entry box within our
- 7:16:42window
- 7:16:42where we can submit a custom item to
- 7:16:44this menu
- 7:16:45so let's create an entry box and let's
- 7:16:49do that before the submit button that
- 7:16:50would be a good spot for this
- 7:16:52so let's call this entry box where
- 7:16:54somebody can
- 7:16:55type in something to add to the menu so
- 7:16:58entry box equals entry and we're adding
- 7:17:01this
- 7:17:02to our window and then entry box
- 7:17:05dot pack then we're going to create a
- 7:17:09button
- 7:17:10called add and i'm just going to copy
- 7:17:12everything from the
- 7:17:13submit button and we'll change submit to
- 7:17:16add
- 7:17:17we're going to add an item to the menu
- 7:17:20so we'll change the text
- 7:17:21to add and command add
- 7:17:24add button dot pack then we're going to
- 7:17:28create
- 7:17:29a function called add and we'll do that
- 7:17:31at the top
- 7:17:32def add parentheses
- 7:17:35colon so to add an item to the list box
- 7:17:39first we need to
- 7:17:40get what is currently in the entry box
- 7:17:43so we can do that by typing in
- 7:17:47list box dot insert
- 7:17:50then within the parentheses we need to
- 7:17:52get
- 7:17:53an index number as well as
- 7:17:56a text item so for the number what we
- 7:17:59can type here
- 7:18:00is the listbox dot size
- 7:18:03so this will give us the current
- 7:18:05position of the
- 7:18:07index that we're currently on for the
- 7:18:08list box and then we need to get the
- 7:18:11text
- 7:18:12in the entry box so we type in the name
- 7:18:14of the entry box
- 7:18:15entry box dot get
- 7:18:19and this will insert a new item within
- 7:18:22our menu our list box so let's try it
- 7:18:24again
- 7:18:27so we can type in an item let's say soda
- 7:18:29we want to add soda to the menu
- 7:18:31and then we click add all right there's
- 7:18:34our soda but we have to scroll down to
- 7:18:36see it
- 7:18:37so let's change the size of our list box
- 7:18:41so there's actually a line of code that
- 7:18:43we did that already
- 7:18:44listbox.config height equals listbox
- 7:18:47size so let's add that within the add
- 7:18:50function at the very end
- 7:18:52so then the height of our list box will
- 7:18:55adjust
- 7:18:56so currently there's no soda within our
- 7:18:57menu let's type in soda
- 7:18:59or some other item click add and then
- 7:19:02that item will appear and the size of
- 7:19:04the list box will change to accommodate
- 7:19:06any new items
- 7:19:07let's say now that we serve sushi we're
- 7:19:10now an italian
- 7:19:11and japanese restaurant now we have
- 7:19:14sushi on the menu
- 7:19:15you know what why stop there let's
- 7:19:17create a delete button to delete an item
- 7:19:19from this menu
- 7:19:21so we'll create a delete button
- 7:19:24i'll just copy everything for add and
- 7:19:26change this to
- 7:19:27delete so delete button text equals
- 7:19:30delete
- 7:19:32command equals delete as well and
- 7:19:35delete button dot pack and then at the
- 7:19:38very top we'll create a delete
- 7:19:40function so we'll do that here
- 7:19:44d e f delete
- 7:19:48and to delete an item from a list box
- 7:19:51there's actually a function to do that
- 7:19:52so
- 7:19:53list box dot delete
- 7:19:56and then within the parenthesis of the
- 7:19:58delete function we can pass
- 7:20:00in list box dot current selection or
- 7:20:04just
- 7:20:04cur selection for short and then we want
- 7:20:07to
- 7:20:08readjust the height of this list box
- 7:20:10after we delete an item
- 7:20:12so we'll just add listbox.config height
- 7:20:15equals listbox.size because the size
- 7:20:18of all the items within the list box is
- 7:20:20going to shrink after we delete an item
- 7:20:23so let's try this so here's our menu
- 7:20:26let's say we no longer want salad
- 7:20:28because salad is healthy well we can
- 7:20:30delete it
- 7:20:31same thing with soup and you know what
- 7:20:33let's also delete the
- 7:20:34pasta and the pizza so we're only left
- 7:20:37with sweet sweet garlic bread
- 7:20:39well ladies and gentlemen we're going to
- 7:20:40get into some more advanced stuff with
- 7:20:42list boxes
- 7:20:43what if you want to select more than one
- 7:20:45item from this list box
- 7:20:47we're going to need to change a few
- 7:20:48things so when we create our list box
- 7:20:51there is a select mode select mode
- 7:20:54and we are going to set this equal to
- 7:20:56multiple
- 7:20:57so now we can actually select multiple
- 7:21:00items
- 7:21:01from this list box like i want to select
- 7:21:03one of everything
- 7:21:04but what happens when we submit this
- 7:21:07well we're going to run into some
- 7:21:08problems so we're going to need to
- 7:21:09change this program around
- 7:21:11so let's begin with the submit option so
- 7:21:14what we're going to do within the submit
- 7:21:16option
- 7:21:16is first get rid of this line let's say
- 7:21:20we
- 7:21:20no longer need this anymore uh and then
- 7:21:23before we print out the items that we
- 7:21:26ordered
- 7:21:26let's create a list called food
- 7:21:30and we'll just leave this empty for now
- 7:21:31we'll fill this later
- 7:21:33so we'll create a for loop for index
- 7:21:37in list box
- 7:21:40dot current selection
- 7:21:44what we're going to do after each
- 7:21:46iteration
- 7:21:48food dot insert
- 7:21:52our index number as well as
- 7:21:55listbox dot get
- 7:21:59the item name at this index what this
- 7:22:02for loop is doing is that it's going to
- 7:22:04iterate once for each item that we
- 7:22:07select
- 7:22:08so since we have three items that are
- 7:22:10currently selected this is going to
- 7:22:12iterate three times
- 7:22:13get the index number as well as the item
- 7:22:15at that index number
- 7:22:17and now we just need to display the
- 7:22:18contents of what we have
- 7:22:20within our list of food so we can do
- 7:22:23that with
- 7:22:23another for loop so let's add that after
- 7:22:26you have ordered
- 7:22:27then for
- 7:22:30index in our list
- 7:22:34food all we'll do is print
- 7:22:39index so this will print out everything
- 7:22:42that we ordered
- 7:22:43so let's say that we want one of
- 7:22:45everything and submit this
- 7:22:46you have ordered pizza pasta garlic
- 7:22:49bread soup and salad
- 7:22:51now what happens if we try to delete
- 7:22:53multiple items
- 7:22:54so let's say we want to delete soup and
- 7:22:56salad and we click delete
- 7:22:58well we're going to run into an error
- 7:23:00because our function is not set up to
- 7:23:02accommodate for
- 7:23:03multiple items so we'll need to change a
- 7:23:05few things around so let's get rid of
- 7:23:07this line for now
- 7:23:08list box dot delete list box dot current
- 7:23:10selection
- 7:23:11and we're going to place a for loop here
- 7:23:13so for
- 7:23:14index in list box
- 7:23:18dot current selection then after each
- 7:23:21iteration
- 7:23:22what we're going to do is use list box
- 7:23:25dot
- 7:23:26delete and we're deleting the item at
- 7:23:29whatever index we're currently on so you
- 7:23:32would think this would work
- 7:23:33at first glance let's say we want to
- 7:23:36delete pizza and pasta
- 7:23:38we're going to click delete so pizza was
- 7:23:40deleted but not
- 7:23:41pasta that's because the indexes are
- 7:23:44changing
- 7:23:45after we delete an item so when we use
- 7:23:48this for loop
- 7:23:49we should actually reverse this so with
- 7:23:53listbox.currentselection
- 7:23:54we are going to enclose this with
- 7:23:56reversed
- 7:23:59so then we're going to start at the last
- 7:24:01index and work our way to zero
- 7:24:03in that order so now if we want to
- 7:24:06delete
- 7:24:06pizza and pasta i don't know why we
- 7:24:09would because
- 7:24:10they're amazing and we click delete it
- 7:24:12deletes
- 7:24:13pizza and pasta but we still have garlic
- 7:24:16bread soup and salad then we can add an
- 7:24:18item
- 7:24:19let's say we want to add sushi against
- 7:24:21the menu we can do that
- 7:24:22and then we can order one of everything
- 7:24:24because we're really hungry
- 7:24:26you have ordered garlic bread soup salad
- 7:24:28and sushi
- 7:24:29so that's the basics of creating a list
- 7:24:32box in python if you want a copy of all
- 7:24:34this code
- 7:24:35i'll post all of this in the comments
- 7:24:36down below but yeah that's the basics
- 7:24:38of list boxes in python
- 7:24:41hey what's going on everybody it's your
- 7:24:43bro here hope you're doing well and in
- 7:24:45this video i'm going to teach you guys
- 7:24:46how we can create
- 7:24:47message boxes in python so sit back
- 7:24:49relax and enjoy the show
- 7:24:54okay people let's create some message
- 7:24:56boxes but first we'll need an import so
- 7:24:58from
- 7:24:59tk enter import
- 7:25:02message box and what this will do
- 7:25:06is import the message box
- 7:25:09library so it's not necessary to add
- 7:25:12this comment uh so we'll need a window
- 7:25:15window equals tk followed by window
- 7:25:18dot main loop let's create a button
- 7:25:22so button equals button we're going to
- 7:25:26add this to the window
- 7:25:27we're going to set the command to
- 7:25:29execute a
- 7:25:30click function that will have a message
- 7:25:33box appear when we click on the button
- 7:25:35and let's set the text so text equals
- 7:25:39click me and then we will need to pack
- 7:25:43this button
- 7:25:43so button.pack now we'll need to create
- 7:25:46a click function
- 7:25:47so let's do that at the top def
- 7:25:50click and what do we want to do
- 7:25:54so there's a few different uh message
- 7:25:56dialog boxes that you can use
- 7:25:58so let's begin with a basic show info
- 7:26:01message box
- 7:26:02so message box dot and there's a few
- 7:26:05options
- 7:26:06let's begin with show info show
- 7:26:09info this just displays a simple message
- 7:26:12so if we were to run this we have a
- 7:26:15button we click on the button and it's
- 7:26:16going to create a message box for us
- 7:26:19and it just shows some info but we don't
- 7:26:21really have any info to show yet
- 7:26:23so we can set a title and we can set a
- 7:26:26message
- 7:26:26so let's set the title title equals
- 7:26:31this is an info
- 7:26:34message box
- 7:26:39although part of the title got cut off
- 7:26:42let's also set the message message
- 7:26:45equals
- 7:26:47you are a person
- 7:26:53click me you are a person thanks i
- 7:26:56didn't know that so there's a few other
- 7:26:59message boxes that we can use too
- 7:27:01so for now i'm just going to turn this
- 7:27:03into a comment and i'll just copy this
- 7:27:05just to save some time
- 7:27:09and the next one that we have is
- 7:27:12show warning
- 7:27:15so this will have a different icon so
- 7:27:18the title
- 7:27:19let's say warning and the message
- 7:27:24you have a virus
- 7:27:31so now we get this annoying warning
- 7:27:33message but you can just click ok to get
- 7:27:35rid of it
- 7:27:36so one thing that you might see with
- 7:27:38tech support scams
- 7:27:39they'll have a message box like this
- 7:27:42that shows a warning
- 7:27:43but they'll put this within a while loop
- 7:27:45that's set to true
- 7:27:47so this will just continue on and on
- 7:27:52kind of like this so if we were to try
- 7:27:55and close out of this
- 7:27:56it's just going to reappear while
- 7:28:00true is true basically so that's just
- 7:28:03something funny that i wanted to show
- 7:28:05you
- 7:28:05so let's move on i'm going to turn this
- 7:28:07into a comment
- 7:28:10then the next one we have is show error
- 7:28:17so show error
- 7:28:21title set it to whatever you want
- 7:28:24error message
- 7:28:27something went wrong
- 7:28:36there's a few more sophisticated ones
- 7:28:38too we can ask for some user input
- 7:28:42so this next one is message box
- 7:28:45dot ask and there's a few to choose from
- 7:28:49let's go with ask okay cancel first
- 7:28:53we'll set the title title equals
- 7:28:56ask okay cancel
- 7:29:00and a message message
- 7:29:05do you want to
- 7:29:08do the thing
- 7:29:15so we can actually put this within an if
- 7:29:18statement
- 7:29:19so this returns either true or false
- 7:29:21depending on what you click
- 7:29:23so let's put this within an if statement
- 7:29:26if this is true
- 7:29:28print you did
- 7:29:31a thing
- 7:29:34if it returns false we can use an else
- 7:29:36statement
- 7:29:38else what else do we want to do we'll
- 7:29:40say else
- 7:29:42print you
- 7:29:45cancelled a thing
- 7:29:49and let's test this
- 7:29:56but i am forgetting a colon there
- 7:30:01do you want to do the thing okay you did
- 7:30:04a thing
- 7:30:04let's try it again do you want to do the
- 7:30:07thing
- 7:30:08cancel you cancelled a thing cool
- 7:30:11there's a few others
- 7:30:12so i'll turn all of these into comments
- 7:30:17and the next one we can go over is ask
- 7:30:20retry cancel
- 7:30:22and this is fairly similar to what we
- 7:30:23had before i'm going to copy this
- 7:30:28let's change this to ask
- 7:30:32retry cancel ask retry
- 7:30:36cancel do you want to
- 7:30:40retry the thing
- 7:30:44you retried a thing
- 7:30:48or you cancelled a thing so it's very
- 7:30:51similar to what we had before
- 7:30:52do you want to retry the thing retry you
- 7:30:55retried a thing
- 7:30:58and cancel you cancelled a thing
- 7:31:03okay the next one we have is ask yes
- 7:31:06no so we can get some user input
- 7:31:11if it's a yes or a no
- 7:31:14so this returns a boolean value a true
- 7:31:17or false
- 7:31:18value so message box
- 7:31:21dot ask
- 7:31:24yes no
- 7:31:28and we'll set the title title equals
- 7:31:31ask yes or no
- 7:31:35and a message
- 7:31:40do you like
- 7:31:44cake
- 7:31:47so let's put this within an if statement
- 7:31:50because this returns
- 7:31:51a true or false answer print
- 7:31:57i like cake too
- 7:32:01else they answered no then if this is
- 7:32:05false
- 7:32:06print why do you
- 7:32:09not like cake
- 7:32:15and i'm missing a colon there we go
- 7:32:19so if they click yes this returns true
- 7:32:22and we execute this line of code i like
- 7:32:25cake
- 7:32:25too if they answer no it's going to
- 7:32:28execute this
- 7:32:29else statement do you like cake no why
- 7:32:32do you not like
- 7:32:33cake now what we have next is ask
- 7:32:36question and this doesn't return a
- 7:32:38boolean value of true or false
- 7:32:40it actually returns a string of yes or
- 7:32:43no
- 7:32:43so this is message box
- 7:32:47dot ask question
- 7:32:50and we'll set the title to ask
- 7:32:54question and a message
- 7:32:57message equals do
- 7:33:00you like pi
- 7:33:05so let's just print the result what this
- 7:33:08returns
- 7:33:09so print everything within here
- 7:33:13so like i said this doesn't return it
- 7:33:15true or false
- 7:33:16but yes or no
- 7:33:20so what we could do is assign the
- 7:33:23response to
- 7:33:24a variable such as answer
- 7:33:27so answer equals whatever the answer is
- 7:33:30whatever they
- 7:33:31click whatever button they click on so
- 7:33:33if
- 7:33:34our answer is equal to yes
- 7:33:38will print a message print
- 7:33:42i like pie 2.
- 7:33:47then else the answer is no
- 7:33:51will print why do you
- 7:33:54not like pie
- 7:34:00so do you like pie yes i like pie too do
- 7:34:03you like pie
- 7:34:04no why do you not like pie
- 7:34:07so this next one is probably the most
- 7:34:10difficult
- 7:34:10it's ask yes no cancel
- 7:34:15so it's message box dot
- 7:34:18ask yes no cancel
- 7:34:21it's this last one so let's set the
- 7:34:23title
- 7:34:24title equals whatever you want yes
- 7:34:28no cancel and a message message
- 7:34:35equals do you
- 7:34:38like to code
- 7:34:42so let's just print the results what
- 7:34:45this returns
- 7:34:46so print everything just so we can take
- 7:34:50a look
- 7:34:51so the possible answers are
- 7:34:55true false
- 7:34:58or none so we can just run some if
- 7:35:01statements for this
- 7:35:04so if
- 7:35:07answer is equal to true
- 7:35:11what we can do is print
- 7:35:16you like to code
- 7:35:20oh but we need to assign this to an
- 7:35:22answer so answer
- 7:35:23equals all of this
- 7:35:29else if
- 7:35:33answer is equal to false
- 7:35:37let's print then
- 7:35:41why are you watching a video
- 7:35:44on coding
- 7:35:48and then else the response is none so we
- 7:35:51can just use an
- 7:35:52else statement so else
- 7:35:56you have dodged the question
- 7:36:02okay let's try this
- 7:36:05do you like to code yes you like to code
- 7:36:08uh but this should really be a smiley
- 7:36:10face right
- 7:36:12do you like to code no then why are you
- 7:36:15watching a video on coding
- 7:36:17do you like to code cancel you have
- 7:36:20dodged the question
- 7:36:22no one other option available to you is
- 7:36:24that you can change the icon that you're
- 7:36:26using
- 7:36:27so let's say that we want to change this
- 7:36:29icon so right now it is currently
- 7:36:32this question mark so we can set this to
- 7:36:34one
- 7:36:35of a few preset icons so icon
- 7:36:38equals and let's set this to warning
- 7:36:42so instead of that question mark we get
- 7:36:44this yellow exclamation point
- 7:36:46and you can also use info
- 7:36:54it's just an eye and error
- 7:37:02so do it fits best for you all right
- 7:37:05well
- 7:37:06that's the basics on message boxes in
- 7:37:08python if you'd like a copy of all this
- 7:37:10code
- 7:37:10i'll post all of this in the comments
- 7:37:12down below but yeah that's the basics of
- 7:37:14message boxes in python hey what's going
- 7:37:18on everybody it's your bro here hope
- 7:37:19you're doing well and in this video i'm
- 7:37:21going to teach you guys about the color
- 7:37:23chooser module in python so
- 7:37:25sit back relax and enjoy the show
- 7:37:29okay everybody so before we begin it
- 7:37:31we'll need a second import so let's add
- 7:37:33this at the top
- 7:37:34so from tk enter import
- 7:37:37color chooser and the reason that we
- 7:37:40need a second import and it's not
- 7:37:42included with this first import
- 7:37:44is because this is a sub module
- 7:37:47it's not necessary to add this comment i
- 7:37:49just like to add comments for teaching
- 7:37:51purposes
- 7:37:52so make sure you have these two imports
- 7:37:54before we begin
- 7:37:55now we'll need a window and a button
- 7:37:58that we can add to the window
- 7:37:59and when we click on the button we're
- 7:38:00going to select a color and change the
- 7:38:03background color
- 7:38:04of the window so let's create a window
- 7:38:07window equals tk followed by window.main
- 7:38:11loop and then let's set the geometry so
- 7:38:14we're going to change
- 7:38:15the background color of the window after
- 7:38:17we select a color
- 7:38:18so let's select a fairly large size for
- 7:38:21a window
- 7:38:22maybe 420 by 420 so this is the width
- 7:38:25and the height
- 7:38:26and then let's create a button so button
- 7:38:29equals button
- 7:38:31let's set some text text equals
- 7:38:34click me and we're going to
- 7:38:37set a command for this button that is
- 7:38:39called so we're going to create a
- 7:38:41function maybe called click
- 7:38:43and then we just need to pack this
- 7:38:45button so we can add this to the window
- 7:38:47so button.pack
- 7:38:48now we're going to need to create a
- 7:38:50function and we'll call it click
- 7:38:53so when we click on the button this
- 7:38:55function is going to be called
- 7:38:56so def click and what do we want to do
- 7:39:00after clicking this button
- 7:39:01what we're going to do is take color
- 7:39:05chooser and we're going to use the ask
- 7:39:09color function so currently this is what
- 7:39:12this does
- 7:39:13we can select a color after clicking
- 7:39:15this button and pick whatever color you
- 7:39:17want
- 7:39:18then you hit ok however we need to store
- 7:39:21this color within
- 7:39:22let's say a variable so we'll create a
- 7:39:23variable called color color equals
- 7:39:26color chooser dot ask color and then we
- 7:39:28can do something with this color
- 7:39:30but for now let's just print this color
- 7:39:32just to take a look to see what it
- 7:39:33contains
- 7:39:34so print color
- 7:39:39so let's say that i want this green
- 7:39:42color
- 7:39:43click ok this is what this prints this
- 7:39:46is our color
- 7:39:47so this is the rgb values that we have
- 7:39:51in this
- 7:39:51first element so this is the amount of
- 7:39:53red
- 7:39:54then green and then blue and the second
- 7:39:58element
- 7:39:58is the hexadecimal representation of
- 7:40:01these values
- 7:40:03so what we want is this hexadecimal
- 7:40:06value
- 7:40:07so what we can do is create a second
- 7:40:09variable to store this
- 7:40:11so let's just call this color hex
- 7:40:14color hex equals color
- 7:40:18and we're going to get the first element
- 7:40:21well technically it's the second element
- 7:40:22but it's the
- 7:40:24element at position one so this one
- 7:40:27this is zero index zero so we're going
- 7:40:30to
- 7:40:31basically extract this hexadecimal value
- 7:40:33and let's just print this
- 7:40:35just to be sure that it's working fine
- 7:40:36so print color
- 7:40:38hex and let's try this again but select
- 7:40:41a different color
- 7:40:42uh maybe i want this purple color then
- 7:40:45click ok
- 7:40:46so this is cb42ff
- 7:40:50and here we have cb42ff
- 7:40:54now let's change the background color so
- 7:40:56since we have this hex value that's
- 7:40:58actually fairly simple
- 7:40:59so we're going to take window dot config
- 7:41:05bg for background color equals
- 7:41:08color hex
- 7:41:11and this will change background
- 7:41:15color
- 7:41:18and let's set this to a sky blue color
- 7:41:22maybe like this click ok and it changes
- 7:41:25the color
- 7:41:26however it's not really necessary to
- 7:41:28continue printing these values you can
- 7:41:30take these out if you want
- 7:41:33and this would work like just the same
- 7:41:35then
- 7:41:37now this part isn't necessary but if you
- 7:41:40want
- 7:41:40you can condense these three lines of
- 7:41:42code that we have for our click function
- 7:41:44into less lines of code so what you
- 7:41:47could do
- 7:41:48instead of assigning the value of color
- 7:41:52at index 1
- 7:41:53to a separate variable we can take color
- 7:41:56at index 1
- 7:41:57get rid of this line and assign bg
- 7:42:00equals color
- 7:42:01at index one so now this is done with
- 7:42:03just
- 7:42:04two lines of code and why stop there we
- 7:42:06can write this with just
- 7:42:08one line of code so now we're going to
- 7:42:10take color chooser dot ask
- 7:42:12color get rid of this line bg equals
- 7:42:17color chooser dot ask color at index
- 7:42:20one and now this is done
- 7:42:24with just one line of code however it's
- 7:42:27a
- 7:42:27i would say a little more difficult to
- 7:42:29teach
- 7:42:30so that's why i kind of like to separate
- 7:42:32this line by line
- 7:42:34so if you want you can condense all of
- 7:42:36this into just one line of code
- 7:42:38so here's some ideas of where the color
- 7:42:40chooser sub module could be helpful
- 7:42:43so of course you can change the
- 7:42:44background color too but let's say that
- 7:42:46you're creating a game and you can
- 7:42:48customize your character or avatar
- 7:42:51well it would be kind of nice if you
- 7:42:52could have some way for the user to
- 7:42:54actually change maybe the character's
- 7:42:56hair color eye color something like this
- 7:42:58would actually work fairly well
- 7:43:00so yeah that's the basics of color
- 7:43:02chooser if you'd like a copy of all this
- 7:43:04code
- 7:43:04i'll post this in the comments down
- 7:43:06below but that's the basics of
- 7:43:08color chooser in python hey what's going
- 7:43:11on everybody it's bro here hope you're
- 7:43:12doing well and in this video i'm going
- 7:43:14to teach you guys how we can create a
- 7:43:15text area in python so sit back relax
- 7:43:18and enjoy the show
- 7:43:21all right my fellow bros let's discuss
- 7:43:24the text
- 7:43:24widget this functions like a text area
- 7:43:27you can enter multiple lines of text i'm
- 7:43:30not sure why
- 7:43:31it's called a text widget instead of a
- 7:43:33text area widget text area would make
- 7:43:36more sense
- 7:43:37but idk though so let's begin by
- 7:43:39creating a window
- 7:43:41window equals tk followed by window.main
- 7:43:44loop
- 7:43:46so to create a text widget also known as
- 7:43:49a text area
- 7:43:51let's call this text text equals text
- 7:43:54and we're going to add this text to our
- 7:43:56window and then we need to
- 7:43:58pack this text text dot pack
- 7:44:03so we should have a text area
- 7:44:06and you can enter in
- 7:44:09stuff like this
- 7:44:13all right but what good is a text area
- 7:44:16if you can't actually submit
- 7:44:18some text so let's create a button to do
- 7:44:20that for us
- 7:44:21so button equals button
- 7:44:24we're going to add this button to the
- 7:44:26window
- 7:44:28and button dot pack
- 7:44:32so we need a command for this button
- 7:44:36and a function as well so let's create a
- 7:44:39function called submit it's going to be
- 7:44:40a submit button
- 7:44:42and let's change the text to because we
- 7:44:44can
- 7:44:45text equals submit
- 7:44:50cool now let's create a function called
- 7:44:53submit
- 7:44:54and we'll do this at the top d e f
- 7:44:56submit
- 7:44:58and what is this going to do well it's
- 7:45:01going to
- 7:45:02get the text from the text area and
- 7:45:05let's assign this to a variable and we
- 7:45:07can call this maybe input or
- 7:45:09whatever you want so to get the text
- 7:45:12from the text area
- 7:45:14let's say text that's the name of this
- 7:45:16text
- 7:45:17area dot get but we need
- 7:45:20a starting index as well as an ending
- 7:45:23index
- 7:45:24so we're going to place 1.0 here
- 7:45:27for the beginning index this is the
- 7:45:29first line
- 7:45:30we want to get everything so for the
- 7:45:33second index we can just put
- 7:45:34end then we can do anything we want with
- 7:45:37this input so let's just print this to
- 7:45:39the console window
- 7:45:40so print input
- 7:45:44and this should work
- 7:45:49hey you drink plenty
- 7:45:52of water love you
- 7:45:55and click submit and then you can do
- 7:45:58whatever you want with this
- 7:45:59input well that's how a text area
- 7:46:02functions but now let's customize the
- 7:46:04appearance of this text area and i have
- 7:46:06a few ideas that you might be interested
- 7:46:08in
- 7:46:09so let's change the background color of
- 7:46:11this text area
- 7:46:12so bg equals and i will pick
- 7:46:15light yellow and you will see why in
- 7:46:17just a second
- 7:46:19so now this text area kind of resembles
- 7:46:22maybe a piece of paper
- 7:46:23a notebook a post-it note however
- 7:46:27this font is really small so let's
- 7:46:30change the font
- 7:46:31styling and the font size so
- 7:46:35font equals and i will pick the font
- 7:46:39ink free this somewhat resembles
- 7:46:42handwriting so
- 7:46:43i kind of like this and it kind of fits
- 7:46:45our notebook theme
- 7:46:46and then a size maybe 25. one thing you
- 7:46:49should know
- 7:46:50the text area size corresponds directly
- 7:46:53with
- 7:46:53the font size so 25 is a fairly
- 7:46:57large font so if we were to compile this
- 7:47:00and run this
- 7:47:01the size of our text area is now massive
- 7:47:05because it corresponds directly with the
- 7:47:07font size
- 7:47:08so we might want to limit the width and
- 7:47:10the height
- 7:47:11so the height is the amount of
- 7:47:14characters that this is tall
- 7:47:16so let's say 8 and the width is the
- 7:47:19amount of characters
- 7:47:20that this is long and let's say 20.
- 7:47:24and now this should be much smaller and
- 7:47:27it kind of resembles a post-it note
- 7:47:28which is kind of cool let's also change
- 7:47:31the amount of padding that we have
- 7:47:34we can do that with pad x and pad y so
- 7:47:36pad x
- 7:47:37let's say 20 and pad
- 7:47:40y also 20. so the text shouldn't
- 7:47:43actually touch any of the borders
- 7:47:47this is some sample
- 7:47:50text cool let's finish this by changing
- 7:47:54the font color so that is
- 7:47:56fg for foreground foreground equals
- 7:47:59and i'll pick purple because purple is a
- 7:48:02pretty color
- 7:48:03and let's run this one last time
- 7:48:07roses are red
- 7:48:10violets are blue
- 7:48:14booty booty booty booty
- 7:48:19rockin everywhere
- 7:48:25well that's the basics of the text
- 7:48:27widget in python if you would like a
- 7:48:29copy of all this code
- 7:48:30i'll post all of this in the comments
- 7:48:32down below but yeah that's the basics of
- 7:48:34the text widget in python
- 7:48:37hey what's going on everybody it's your
- 7:48:39bro here hope you're doing well and in
- 7:48:41this video i'm going to teach you guys
- 7:48:42how we can use a file dialog
- 7:48:44in python to open and read the contents
- 7:48:46of a file so sit back
- 7:48:47relax and enjoy the show
- 7:48:51okay people so this is how you can use
- 7:48:54file dialog
- 7:48:55to open and read the contents of a file
- 7:48:57but first we'll need a second import
- 7:48:59along with this import at the top
- 7:49:01so what we're going to be importing is
- 7:49:03from tk
- 7:49:05enter import file
- 7:49:08dialog and then we'll need a window
- 7:49:11like what we usually do so window equals
- 7:49:15tk followed by window dot
- 7:49:18main loop now we'll need a button that
- 7:49:21we're going to add to the window and
- 7:49:22when we click on this button
- 7:49:23it's going to launch our file dialog so
- 7:49:26we can select a file
- 7:49:27so we'll call this button button equals
- 7:49:30button
- 7:49:31let's set some text maybe to just open
- 7:49:34like we're opening a file and we'll
- 7:49:36associate a function to our command
- 7:49:39for this button so let's create a
- 7:49:41function called
- 7:49:43open file that's fine and then we need
- 7:49:46to
- 7:49:47pack this button so we can add it to the
- 7:49:49screen so button.pack
- 7:49:51now we need to create a function called
- 7:49:53open file and we'll do this before we
- 7:49:55create our window let's type
- 7:49:57def open file
- 7:50:00and when we call this function we're
- 7:50:03going to
- 7:50:04take file dialog and use the
- 7:50:07ask open file name function
- 7:50:11so this is going to return a string
- 7:50:14and that string is the file path of
- 7:50:16where your file is located
- 7:50:18so what we could do is store this within
- 7:50:21a variable
- 7:50:22so let's store this within a variable
- 7:50:24called file path
- 7:50:26and to test this let's just print this
- 7:50:29just to take a look at the contents of
- 7:50:31this variable
- 7:50:32so let's just print the file path now i
- 7:50:35have a file on my desktop that i want to
- 7:50:37use called test file
- 7:50:39so let's click the open button and this
- 7:50:42might not necessarily bring you to your
- 7:50:44desktop it might bring you someplace
- 7:50:45else within your
- 7:50:46file directory so this just happens to
- 7:50:49be
- 7:50:50bring me to my desktop so i'm going to
- 7:50:52click the test file that i have and
- 7:50:54click
- 7:50:54open so this prints the file path
- 7:50:57so it's going to return a string and
- 7:51:00it's the file path basically
- 7:51:02so now we want to open and read the
- 7:51:04contents of this file
- 7:51:05so we don't really need this print line
- 7:51:07anymore so
- 7:51:09what we'll do is create a file
- 7:51:12variable and we're going to use open
- 7:51:16we're going to list the file path and
- 7:51:18then we're going to use
- 7:51:20r for read so the default is normally rt
- 7:51:23for read text otherwise you can read
- 7:51:25binary uh but we can just set this to r
- 7:51:28r is the same as rt basically uh so then
- 7:51:30we're going to open this file
- 7:51:32and then we're going to read it so let's
- 7:51:35just print this to the console window
- 7:51:37so print the name of the file which is
- 7:51:41file
- 7:51:41where this is located file dot
- 7:51:45read function and then it's a good idea
- 7:51:47to close your files
- 7:51:49after you're done with them so file dot
- 7:51:51close
- 7:51:52and let's test this so i'm going to
- 7:51:55click
- 7:51:56open find the file that i want
- 7:52:00click it and then open
- 7:52:05and that text file says my name is bro i
- 7:52:07like pizza
- 7:52:08now let's say you want to make this a
- 7:52:10little more advanced so what i'm going
- 7:52:12to do
- 7:52:12is that i'm going to set the initial
- 7:52:14directory of where we begin
- 7:52:16looking for a file when we launch our
- 7:52:18file dialog
- 7:52:19so with that test file i'm going to move
- 7:52:21this to my project folder so i'm just
- 7:52:23going to paste it
- 7:52:26so now this is within my project folder
- 7:52:28now i'm going to set the initial
- 7:52:30directory within
- 7:52:31the function of ask open file name
- 7:52:35so we're going to set the initial d
- 7:52:38i r for initial directory and then we
- 7:52:40can list a file path
- 7:52:42so one thing you could do i want to get
- 7:52:45this
- 7:52:46path here this file path so if you're
- 7:52:47using pycharm what you could do is right
- 7:52:49click this
- 7:52:50copy path and then get the absolute path
- 7:52:53and then i'm just going to paste this
- 7:52:55and then uh with these backslashes
- 7:52:58you'll have to do double backslashes
- 7:52:59because
- 7:53:00this is the escape character for a
- 7:53:02backslash
- 7:53:04alright so this should open my project
- 7:53:06folder
- 7:53:08which it does and here's that test file
- 7:53:12another thing we could do is that we can
- 7:53:13change the title of our file dialog
- 7:53:16right now the default is just open so we
- 7:53:18can change that to something else
- 7:53:20so i'm going to add a comma maybe put
- 7:53:22this on a different line
- 7:53:24uh so this is still within the function
- 7:53:27of ask open file name
- 7:53:28so i'm going to set the title to
- 7:53:30something else i'll say
- 7:53:32open file
- 7:53:35okay so if we were to run this again
- 7:53:40instead of just saying open it says open
- 7:53:43file okay and we can also limit the
- 7:53:46file types that we accept and look for
- 7:53:49so we can use file types
- 7:53:55so let's say that we want to initially
- 7:53:57look for a
- 7:53:58txt file so what we'll do is type in
- 7:54:04like a name for the file that we're
- 7:54:06looking for so this is what's going to
- 7:54:07appear to the user
- 7:54:09uh text files followed by an extension
- 7:54:12so we want asterisk
- 7:54:16dot txt
- 7:54:19and then we can also have an option for
- 7:54:21all files
- 7:54:22so i'm going to add a comma for a second
- 7:54:24option
- 7:54:26and then let's say all
- 7:54:29files and then an extension so comma
- 7:54:34asterisk dot asterisk this will look for
- 7:54:37all files
- 7:54:38uh but i think i am missing a
- 7:54:40parenthesis
- 7:54:42there that should be good so then when
- 7:54:44we open our file dialog
- 7:54:47it's going to initially be looking for
- 7:54:49plain text files
- 7:54:50and with this drop down menu it says
- 7:54:52text files and
- 7:54:54all files so that's what we have listed
- 7:54:56within our file types of what we want to
- 7:54:58search for
- 7:55:00so you can see that if i switch to all
- 7:55:02files
- 7:55:03our python file actually appears because
- 7:55:06we don't have any limits on the files
- 7:55:08that we're trying to open or search for
- 7:55:10so if i change this back to text files
- 7:55:13only folders and
- 7:55:14plain text files are visible so then we
- 7:55:16can open our test file again
- 7:55:19but yeah that's the basics of file
- 7:55:22dialog in the next video we're going to
- 7:55:23be saving
- 7:55:24a file so if you'd like a copy of all
- 7:55:26this code i'll post
- 7:55:28all of this in the comments down below
- 7:55:29but yeah that's the basics of opening a
- 7:55:31file
- 7:55:32using python hey what's going on
- 7:55:34everybody it's your bro here hope you're
- 7:55:36doing well and in this video i'm going
- 7:55:37to teach you guys how we can use
- 7:55:39python's file dialog to save a file
- 7:55:42someplace on your computer
- 7:55:43so sit back relax and enjoy the show
- 7:55:48okay everybody so this is how we can
- 7:55:50save a file
- 7:55:52we're going to be reusing a file dialog
- 7:55:54again kind of like what we did with the
- 7:55:56video on opening and reading a file so
- 7:55:59from
- 7:56:00tk enter import file dialog we're going
- 7:56:03to be creating a window and adding a
- 7:56:05button as well as a text area to this
- 7:56:07window
- 7:56:08so let's create our window window equals
- 7:56:10tk
- 7:56:11followed by window.main loop
- 7:56:15let's create a button so button equals
- 7:56:18button
- 7:56:19let's set the text of the button so text
- 7:56:22equals
- 7:56:22save it's going to be a save button and
- 7:56:25then a command
- 7:56:26so we'll need to link a function to this
- 7:56:28command so let's create a
- 7:56:30save file function and then we need to
- 7:56:34pack
- 7:56:34this button so button dot pack
- 7:56:38now let's create a text area this is
- 7:56:40just called
- 7:56:41text so text equals text we're going to
- 7:56:44add this to the window
- 7:56:46and we need to pack this text so text
- 7:56:49dot pack and now let's create
- 7:56:52a save file function so at the top of
- 7:56:56our program
- 7:56:57d e f save file
- 7:57:02and then we're going to take file dialog
- 7:57:05and use the
- 7:57:06ask save as file function and we're
- 7:57:09going to store
- 7:57:11the contents within file file equals
- 7:57:14file dialog dot ask save as file
- 7:57:18let's run this just to test it out so we
- 7:57:21have
- 7:57:21our window a save button and this
- 7:57:25is our text area
- 7:57:28we can type in multiple
- 7:57:32lines of text
- 7:57:35yay and let's try and save this just to
- 7:57:39see what we have so far
- 7:57:41so this will bring me to my desktop it
- 7:57:43might bring you someplace else for
- 7:57:46your computer so i'm just going to save
- 7:57:48this as
- 7:57:50test file now with this drop down menu
- 7:57:53save as type there's nothing set up so
- 7:57:56we'll need to do that manually
- 7:57:58uh if we were to save this and here's
- 7:58:00that test file on my desktop
- 7:58:02so let's take a look at the properties
- 7:58:04we did not list a
- 7:58:06file extension so the type of file just
- 7:58:09says
- 7:58:09file so we'll get to that later on in
- 7:58:11this video
- 7:58:12and if we were to open this with a
- 7:58:14program like notepad
- 7:58:16well it's currently empty that's because
- 7:58:18we need to write
- 7:58:19some text to this file so let's do that
- 7:58:22next
- 7:58:23let's store all of the text from our
- 7:58:26text area to a variable
- 7:58:28let's call this variable file text
- 7:58:32file text equals and then we need to get
- 7:58:35all of the text from our text area so
- 7:58:38text dot get and we're going to list the
- 7:58:42starting index as well as the ending
- 7:58:44index
- 7:58:45for this function so we're going to
- 7:58:47begin at
- 7:58:481.0 that is the beginning of this text
- 7:58:51area
- 7:58:51and the ending we can just put end and
- 7:58:54then we need to convert
- 7:58:55all of this into a string so let's
- 7:58:57surround this with
- 7:58:59str and then we can store all of the
- 7:59:02text
- 7:59:03from our text area within this variable
- 7:59:06file text and now we just need to write
- 7:59:09our text to this file so file dot
- 7:59:12write and we're going to pass in file
- 7:59:15text
- 7:59:16and then it's a good idea to close your
- 7:59:18files when you're done using them so
- 7:59:20file
- 7:59:21dot close and let's try this again
- 7:59:26hello my name
- 7:59:30is bro let's save this
- 7:59:34i'll call this hello and click save
- 7:59:38and we still can't save s-type so we'll
- 7:59:40work on that next
- 7:59:42let's go to my desktop here's the test
- 7:59:44file that we created i'm going to open
- 7:59:46this with notepad and it has all of the
- 7:59:48text
- 7:59:49that we added to that text area now
- 7:59:52let's list
- 7:59:53some available file extensions that we
- 7:59:55can have the user
- 7:59:56save this file as so let's begin by
- 7:59:59setting
- 8:00:00the default extension so default
- 8:00:04extension and let's set this to
- 8:00:07a text file that is dot
- 8:00:11txt and then we can list
- 8:00:14all of the available file types
- 8:00:17so file types
- 8:00:21and we're going to place these within a
- 8:00:23set of straight brackets
- 8:00:25and i'll just separate these line by
- 8:00:26line so let's begin with a plain
- 8:00:28text file so this will take two strings
- 8:00:32the first is the message that will
- 8:00:36appear within
- 8:00:37that save as type drop down menu so
- 8:00:39let's place
- 8:00:41text file and the second string
- 8:00:44the file extension and that is
- 8:00:48dot txt
- 8:00:51then let's add another let's say html
- 8:00:54so i'm going to copy this and paste it
- 8:00:57and
- 8:00:58let's say html next
- 8:01:02dot html and then lastly you can do
- 8:01:06just all files so
- 8:01:09all files and that is dot
- 8:01:13asterisk and let's try this again
- 8:01:17so hello my
- 8:01:21name is bro i
- 8:01:25like pizza and
- 8:01:28ice cream and this time i think i'll
- 8:01:32save this
- 8:01:32as an html file
- 8:01:35and this is we'll call this
- 8:01:39pizza and ice cream i guess
- 8:01:43and click save now let's go back to my
- 8:01:46desktop
- 8:01:47here's that file pizza and ice cream and
- 8:01:49it is an
- 8:01:51file let's just verify that yep.html for
- 8:01:56the type of file
- 8:01:57so we can open this with notepad
- 8:02:01and it has all of the text i could even
- 8:02:04open this with sublime text
- 8:02:06as well kind of like that
- 8:02:11all right let's change the initial
- 8:02:14directory
- 8:02:15for saving a file let's change it to
- 8:02:18my project folder for uh this python
- 8:02:22file that we have
- 8:02:23so that is and i'm just going to
- 8:02:25organize this a little bit
- 8:02:28initial d i r
- 8:02:31equals and then we can list a file path
- 8:02:34so i'm just going to copy this
- 8:02:37so copy path paste it
- 8:02:42then i want to make sure that these are
- 8:02:45double backslashes
- 8:02:49and i need to add a comma at the end
- 8:02:52okay let's try this again
- 8:02:54so testing
- 8:02:57one two three and this should go to my
- 8:03:01project folder
- 8:03:02which it does testing123
- 8:03:07and let's save this as a plain text file
- 8:03:10click save
- 8:03:12and here's that plain text file
- 8:03:15testing123
- 8:03:16another option available to you is that
- 8:03:18you don't necessarily need to use
- 8:03:20a text area to get some text to write to
- 8:03:23a file
- 8:03:24you could use the console window to
- 8:03:26accept some text
- 8:03:28so to demonstrate this i'm going to turn
- 8:03:30this line into a comment
- 8:03:32and we're instead going to use the
- 8:03:34console window this time
- 8:03:36so we'll say file text equals
- 8:03:40input and then let's just have a prompt
- 8:03:43here
- 8:03:44enter some text i
- 8:03:47guess and then let's run this again so
- 8:03:50we still need to
- 8:03:51click this save button and select a file
- 8:03:54location i'll just save over our file
- 8:03:57testing123 i'll click save
- 8:04:00yeah i'll replace it alright so we need
- 8:04:02to enter some text
- 8:04:04i guess so i will write a message
- 8:04:08such as the cake
- 8:04:12is a lie and hit enter
- 8:04:16so then our file testing123 now says
- 8:04:19the cake is a lie so you don't
- 8:04:21necessarily need to use
- 8:04:23the get function for a text area you can
- 8:04:26use the console window as well
- 8:04:29here's one last thing to consider before
- 8:04:30you go so what if we begin the process
- 8:04:33of
- 8:04:34finding a file location to save a file
- 8:04:37to
- 8:04:37and then we exit out of this window well
- 8:04:40we're going to encounter an exception
- 8:04:42so one way that we can prevent this is
- 8:04:45to add
- 8:04:46this line if file is
- 8:04:49none return
- 8:04:52so then if we were to try this again
- 8:04:56begin saving a file and then exit out of
- 8:04:58this
- 8:04:59there is no exception that we encounter
- 8:05:02so that's just one little extra i
- 8:05:04thought i would throw in
- 8:05:05but yeah that's the basics of saving
- 8:05:07files in python if you would like a copy
- 8:05:09of all this code
- 8:05:10i'll post all of this in the comments
- 8:05:12down below but yeah that's how to save a
- 8:05:15file using python
- 8:05:17hey what's going on everybody it's your
- 8:05:18bro here hope you're doing well and in
- 8:05:20this video i'm going to teach you guys
- 8:05:21how we can create a menu bar
- 8:05:23in python so sit back relax and enjoy
- 8:05:26the show
- 8:05:28all right people let's create a menu bar
- 8:05:31not too different from the menu bar that
- 8:05:32you see at the top of many programs
- 8:05:34there's usually a tab
- 8:05:36for file edit view help stuff like that
- 8:05:39except we're going to create one that
- 8:05:41doesn't have as many options
- 8:05:43or features it's going to be the generic
- 8:05:45brand menu bar like the walmart variety
- 8:05:48so let's begin by creating a window so
- 8:05:50window equals tk followed by
- 8:05:53window.mainloop then we're going to
- 8:05:54create a menu bar
- 8:05:56and add menus to the menu bar and each
- 8:05:59menu is going to function like a
- 8:06:01dropdown menu where you hover over one
- 8:06:03of these tabs
- 8:06:04and there's a drop down so we need a
- 8:06:07menu bar
- 8:06:08let's just call this menu bar menu bar
- 8:06:10equals
- 8:06:11menu and we're adding this to our window
- 8:06:15so then we're going to take window dot
- 8:06:17config
- 8:06:18and we can set the menu of this window
- 8:06:21to equal the menu bar that we just
- 8:06:24created
- 8:06:25with each of these tabs for your menu
- 8:06:26bar you need to create a separate menu
- 8:06:29and add each menu to your menu bar and
- 8:06:31the menu bar is added to the window
- 8:06:33there's different levels to the stuff so
- 8:06:36let's create
- 8:06:37a file menu because well this one has a
- 8:06:40file menu
- 8:06:40why not our program so file
- 8:06:44menu equals menu
- 8:06:47kind of like what we did with our menu
- 8:06:49bar except instead of adding this to our
- 8:06:52window we're going to add this
- 8:06:53to our menu bar so menu
- 8:06:57bar like that there's one other step two
- 8:07:02we need to use this function menu
- 8:07:05bar dot add
- 8:07:08underscore cascade this will have a drop
- 8:07:12down
- 8:07:12menu sort of effect so let's create a
- 8:07:16label
- 8:07:16just called file to mimic the file tab
- 8:07:19that you see at the top of many programs
- 8:07:21so label file and we're going to set the
- 8:07:25menu
- 8:07:26to equal the file menu that we created
- 8:07:30so file menu and let's take a look just
- 8:07:33to see what we have
- 8:07:35so here's our menu bar and here's our
- 8:07:37menu there's a drop down
- 8:07:39effect for this but we need to add
- 8:07:41actual options
- 8:07:42to this file menu so let's do that
- 8:07:46so let's begin by creating let's see we
- 8:07:49got
- 8:07:50maybe open save and exit
- 8:07:54that should be good so file
- 8:07:58menu dot add
- 8:08:01underscore command so this is a
- 8:08:03clickable option
- 8:08:05and let's create a label called open
- 8:08:08and we can open a file and maybe two
- 8:08:12more so we got open
- 8:08:14save and exit open save
- 8:08:17exit
- 8:08:21and let's take a look at this
- 8:08:25so here's the drop down menu we can open
- 8:08:27save exit
- 8:08:29now you might notice that there is this
- 8:08:31annoying line
- 8:08:32this is called a tear off you can
- 8:08:34actually get rid of this if you want
- 8:08:36so when we create this file menu we're
- 8:08:39going to
- 8:08:40set tear off to equal
- 8:08:43zero and that should get rid of that
- 8:08:47and it's gone so sometimes you might see
- 8:08:51a line to separate sections well
- 8:08:54you can do that with a separator so
- 8:08:57let's separate
- 8:08:58the exit command that we have
- 8:09:01from open and save and to add that
- 8:09:03separator we just type in the name of
- 8:09:05our menu
- 8:09:06file menu dot add
- 8:09:10separator so this will separate your
- 8:09:14different commands from each other
- 8:09:16within a menu you can see that there is
- 8:09:18a line between
- 8:09:19save and exit because we placed a
- 8:09:21separator here
- 8:09:22now if you want each of these file
- 8:09:24commands to actually do something you
- 8:09:26need to associate
- 8:09:27a command with each of these commands
- 8:09:30it's a command within a command
- 8:09:32so command equals for our open file
- 8:09:35command
- 8:09:36we'll create a function called open file
- 8:09:40and we'll need to declare that function
- 8:09:42and then let's create a command called
- 8:09:45save file and then another called
- 8:09:48exit actually one shortcut if you want
- 8:09:51to exit something you can just say
- 8:09:53quit all right let's create an
- 8:09:56open file and a save file function so
- 8:09:59def open file
- 8:10:03let's just print something just to keep
- 8:10:05this simple
- 8:10:07i already have another video on opening
- 8:10:09files so
- 8:10:11print file has
- 8:10:14been opened and let's do the same thing
- 8:10:18for
- 8:10:18save file
- 8:10:25file has been saved
- 8:10:28and then our last command exit we'll
- 8:10:30just quit
- 8:10:31so let's try this so let's go to file
- 8:10:34here's the drop down menu we can
- 8:10:36open a file file has been opened we can
- 8:10:39save a file
- 8:10:41and then exit and exit will close out of
- 8:10:43our window
- 8:10:44let's create a second menu called edit
- 8:10:46to mimic the one that we have at the top
- 8:10:48of our ide
- 8:10:49so let's create a edit menu so it's the
- 8:10:52same process that we did with our file
- 8:10:54menu
- 8:10:55so let's call this edit menu equals menu
- 8:10:59we're adding this to our menu bar and i
- 8:11:02do not want a tear
- 8:11:03off so tear off equals zero then for the
- 8:11:07next line we need to add
- 8:11:09cascade so i'm going to copy this
- 8:11:12menu bar dot add cascade the label name
- 8:11:15will be edit and the file i mean the
- 8:11:18menu
- 8:11:19will be edit menu and we're setting
- 8:11:22this menu that we're adding to edit menu
- 8:11:26and let's try this so we have file and
- 8:11:28edit
- 8:11:29but we need to add commands to this edit
- 8:11:31menu
- 8:11:32so it's the same process that we did
- 8:11:34with our file menu i'm just going to
- 8:11:35copy this
- 8:11:36change file to edit and let's
- 8:11:40make a command called cut copy and paste
- 8:11:44so the label will be cut we'll create a
- 8:11:48command called
- 8:11:49cut we'll copy this
- 8:11:54and we'll create a another command
- 8:11:56called
- 8:11:58copy and then paste
- 8:12:08all right then we just need to create
- 8:12:10some functions for this so cut
- 8:12:11copy paste let's do that at the top
- 8:12:18so cut
- 8:12:24you cut some text
- 8:12:29so cut copy and then paste
- 8:12:35you copied some text
- 8:12:38and paste you
- 8:12:42pasted some text
- 8:12:45all right let's try this so here's our
- 8:12:47edit menu
- 8:12:49here's the drop down we can cut we can
- 8:12:51copy
- 8:12:52and we can paste not actually but it's
- 8:12:55going to call a function that can
- 8:12:57do something like that if you want to
- 8:12:59set that up maybe that's a video for a
- 8:13:01different day
- 8:13:02let's customize these menus well because
- 8:13:04we can
- 8:13:05so let's begin by changing the font so
- 8:13:08we can just type in
- 8:13:09within each of these menus font equals
- 8:13:12pick a font that you want and then a
- 8:13:14size so i'm going to add this font
- 8:13:16for both of these menus that we have and
- 8:13:19then when we
- 8:13:20hover over and click on these menus you
- 8:13:23can see that the font has changed
- 8:13:25did you guys know that you can actually
- 8:13:27add images next to each of these
- 8:13:28commands
- 8:13:29well you can and this is how we can do
- 8:13:31that first you'll need some images
- 8:13:33here's a few that i'm using so add these
- 8:13:35to your project folder i plan on using a
- 8:13:38floppy disk for save
- 8:13:39and open folder for open and for some
- 8:13:42reason a stop sign for exit because i
- 8:13:44couldn't really think of anything at the
- 8:13:46moment
- 8:13:46i guess an arrow would have worked too i
- 8:13:48guess so let's create some photo images
- 8:13:50out of these images
- 8:13:53so i already did this i'm just going to
- 8:13:54copy and paste the text that i had
- 8:13:57so create a unique name for each image i
- 8:14:00have
- 8:14:00open image save image and exit image
- 8:14:03they're all
- 8:14:04photo images and here is the file path
- 8:14:07or the file name for each of these
- 8:14:09pictures that i'm using so to add an
- 8:14:12image
- 8:14:12to each of these commands when you add
- 8:14:14the command we can just say
- 8:14:16image and then the photo image that you
- 8:14:18want to use
- 8:14:19so with my open command i'm going to use
- 8:14:22this
- 8:14:22open image photo image that i have and
- 8:14:25this is the
- 8:14:26open folder so i'm going to do the same
- 8:14:28thing for save
- 8:14:29and exit so we have save image
- 8:14:33and exit image
- 8:14:38but when we run and compile this let's
- 8:14:40take a look
- 8:14:42yeah so all the text is gone that's
- 8:14:44going to be a problem
- 8:14:46so we'll need to compound views so with
- 8:14:48compound we can add
- 8:14:50an image on a certain side of the text
- 8:14:53and keep the text so compound
- 8:14:56let's say we want the image on the left
- 8:14:59so
- 8:15:00compound left and i'm just going to add
- 8:15:02this to each of these commands
- 8:15:06and now we should have an image as well
- 8:15:09as some text
- 8:15:11well that's the basics of creating a
- 8:15:13menu bar in python if you want a copy of
- 8:15:15all this code
- 8:15:16i'll post all of this in the comments
- 8:15:18down below but yeah that's how you can
- 8:15:19create a simple
- 8:15:20menu bar in python what's going on
- 8:15:24everybody it's bro here hope you're
- 8:15:25doing well and in this video i'm going
- 8:15:27to teach you guys how
- 8:15:28frames work in python so sit back relax
- 8:15:31and enjoy the show
- 8:15:34what's going on people we're going to be
- 8:15:36discussing frames today
- 8:15:38a frame is really just a rectangular
- 8:15:40container to group and hold widgets
- 8:15:42together
- 8:15:43so we're going to practice using frames
- 8:15:45today but first we'll need a window
- 8:15:47we're going to create a frame and add a
- 8:15:49frame to the window
- 8:15:51so window equals tk followed by
- 8:15:54window.main loop
- 8:15:56well we're going to need some widgets to
- 8:15:58add to our frame
- 8:15:59so let's create a bunch of buttons
- 8:16:01because why not
- 8:16:03so i'm going to show you guys a shortcut
- 8:16:05too so let's say
- 8:16:06button equals button
- 8:16:10and then for now we're going to add this
- 8:16:12to our window but we're going to replace
- 8:16:14this eventually with
- 8:16:16frame when we create this and i'm just
- 8:16:18going to set the text
- 8:16:20to equal let's say w
- 8:16:23we're going to have four different
- 8:16:24buttons wasd
- 8:16:26like the buttons on the keyboard if
- 8:16:28you're playing a game and you want to
- 8:16:29move forward
- 8:16:30left back or right so we're adding this
- 8:16:33to the window
- 8:16:34for now let's set the text to w
- 8:16:38and what else can we do uh let's change
- 8:16:40the font so font
- 8:16:41equals i'll pick the font consoles
- 8:16:45because
- 8:16:46that tends to be even uh with buttons
- 8:16:50the size of the button adjusts to uh the
- 8:16:52size of the
- 8:16:54uh letter that you're using or the size
- 8:16:56of the text
- 8:16:57uh so let's set this to 25
- 8:17:00and then a width so maybe
- 8:17:04width equals three all right and then
- 8:17:07button.pack and i just want to take a
- 8:17:10look at this
- 8:17:14so here's our button w so i'd like to
- 8:17:16show you guys a
- 8:17:17shortcut if you don't plan on using this
- 8:17:20button by name
- 8:17:22you don't really need to give it a name
- 8:17:23you can just say button
- 8:17:25and then have all your uh options here
- 8:17:28and then instead of having button.pack
- 8:17:31just add
- 8:17:32dot pack to the end so this will do the
- 8:17:34same thing
- 8:17:35however you can't adjust this button by
- 8:17:38its name though because it doesn't have
- 8:17:39a name anymore
- 8:17:40so let's create a few other buttons one
- 8:17:42for a s
- 8:17:44and d so we have w
- 8:17:47a s d
- 8:17:52and by default they're all top and
- 8:17:55center
- 8:17:56so with pack we can actually put these
- 8:17:59on a certain side
- 8:18:00so let's say we want uh w on the top
- 8:18:04and then the other three will be all
- 8:18:07left
- 8:18:07so side equals left and then after i
- 8:18:11show this to you
- 8:18:12you'll see the benefits of using a frame
- 8:18:15all right so right now they're all at
- 8:18:18least it appears that they're all within
- 8:18:19a container
- 8:18:20so what if i were to expand this well w
- 8:18:23is just drifting off
- 8:18:24it's sailing away by w so
- 8:18:27what if we could put these within a
- 8:18:29frame or container
- 8:18:30to actually contain all of these widgets
- 8:18:33well we can
- 8:18:34so let's create a frame frame
- 8:18:38equals frame
- 8:18:41and instead of adding these buttons to
- 8:18:43the window we're going to add
- 8:18:44these buttons to our frame
- 8:18:51and now we need to add the frame to the
- 8:18:54window
- 8:18:56and this is what this looks like however
- 8:18:59we need to
- 8:19:00actually add this by using pack or place
- 8:19:03so frame
- 8:19:04dot pack so now what this looks
- 8:19:08like is that all of these buttons all of
- 8:19:10these widgets are now contained within a
- 8:19:12frame
- 8:19:13a container let's change the background
- 8:19:16color of this frame just so that we can
- 8:19:17see
- 8:19:18the width and the height of this frame
- 8:19:20so
- 8:19:21when you create your frame there is an
- 8:19:24option for background color it's just bg
- 8:19:27and let's set this to a color we haven't
- 8:19:29done pink yet
- 8:19:30pink is a very masculine color so now
- 8:19:33the background of our frame is pink and
- 8:19:36that might be
- 8:19:37a better visual cue to the balance of
- 8:19:40the frame
- 8:19:41let's also give this frame a border so
- 8:19:43let's
- 8:19:44set a width of 5 using bd
- 8:19:47and then the type of border that we want
- 8:19:50and we can
- 8:19:51specify that with relief so relief
- 8:19:53equals
- 8:19:54let's say raised so this will have a 3d
- 8:19:57pop effect
- 8:19:58kind of like that let's try sunken
- 8:20:00there's a few other ones as well but i
- 8:20:02don't feel like it's necessary to
- 8:20:04go over all the border types let's stick
- 8:20:07with sunken for now that'll work
- 8:20:09alright so with this frame we are
- 8:20:12currently packing this
- 8:20:13we can set this on a certain side within
- 8:20:16our window
- 8:20:17so currently it's at the top let's set
- 8:20:19this to maybe the bottom so
- 8:20:21side equals bottom
- 8:20:25now this frame is sticking to the bottom
- 8:20:28of the window
- 8:20:29and it still contains all of the buttons
- 8:20:31all of the widgets
- 8:20:32that we added another function available
- 8:20:34to you is
- 8:20:35the place function we can place this
- 8:20:38frame
- 8:20:38at certain coordinates within our window
- 8:20:41so let's set
- 8:20:43x to equal zero and y to equal zero
- 8:20:46so then when we place this frame within
- 8:20:48our window and set some coordinates
- 8:20:51this entire frame is going to stick at
- 8:20:54these coordinates even if we were to
- 8:20:56resize this window and we can change
- 8:20:59these too
- 8:21:00let's say x is now 100 and y is also 100
- 8:21:05so the frame along with all of the
- 8:21:08components all of the widgets that it
- 8:21:10contains
- 8:21:11are sticking within the container and
- 8:21:13they move along with this
- 8:21:15so that's the basics of frames it's
- 8:21:17really just a rectangular container to
- 8:21:19group and hold widgets together
- 8:21:22so if you want a copy of all this code
- 8:21:24i'll post all of this in the comments
- 8:21:25down below
- 8:21:26and well yeah that's how frames work in
- 8:21:29python
- 8:21:30hey what's going on everybody it's your
- 8:21:32bro here hope you're doing well and in
- 8:21:33this video i'm going to teach you guys a
- 8:21:35few different ways in which we can
- 8:21:36create new windows using python so sit
- 8:21:38back relax and enjoy the show
- 8:21:42let's get started everybody so we're
- 8:21:44going to create a window and we'll add a
- 8:21:45button to this window
- 8:21:46when we click on this button it'll
- 8:21:48create a new separate window for us
- 8:21:50so window equals tk followed by
- 8:21:52window.main loop
- 8:21:54we'll need to add a button so let's do
- 8:21:55that button we're adding this to the
- 8:21:57window
- 8:21:58let's add some text to the button let's
- 8:22:00say create
- 8:22:02new window and we'll set a command
- 8:22:06command equals and we'll need to define
- 8:22:08a function
- 8:22:09let's create a function called create
- 8:22:12underscore window
- 8:22:14and let's pack this as well so we will
- 8:22:16define
- 8:22:17this function create window and let's do
- 8:22:19so at the top
- 8:22:21def create window and what do we want
- 8:22:23this to do
- 8:22:24well we're going to create a new window
- 8:22:26and let's call this next window
- 8:22:28new underscore window equals and there's
- 8:22:31two popular ways of creating a window
- 8:22:33one is we can use tk and the other is
- 8:22:36that we can use
- 8:22:37something called top level and let me
- 8:22:40explain the difference between the two
- 8:22:41here's a definition for a top level
- 8:22:43window this is a
- 8:22:45new window on top of other windows
- 8:22:48and it's usually linked to a bottom
- 8:22:51window
- 8:22:52our main window is serving as the bottom
- 8:22:54window
- 8:22:55and our new window is serving as the top
- 8:22:57window the top level window
- 8:23:00so let's try this and just experiment
- 8:23:02here is our main window
- 8:23:03the bottom window and if we were to
- 8:23:05click on this button it's going to
- 8:23:07create a
- 8:23:08new window for us a new top level window
- 8:23:11if we were to close out of the bottom
- 8:23:13level window
- 8:23:14any top level windows associated with
- 8:23:17this would also close
- 8:23:18kind of like what you just saw now
- 8:23:20however it's not the same case
- 8:23:22the other way around if we have a bottom
- 8:23:25window and a top level window
- 8:23:26and we close out of the top level window
- 8:23:29our bottom level window
- 8:23:31is just fine it didn't close out of that
- 8:23:33so think of these like
- 8:23:34jenga pieces if you were to pull one of
- 8:23:37the pieces
- 8:23:38one of the windows out from underneath
- 8:23:40the bottom any
- 8:23:41pieces or windows on the top of it will
- 8:23:44close out as well
- 8:23:45or collapse kind of like in the game
- 8:23:46jenga so
- 8:23:48that's one way to think of top level
- 8:23:50windows they're stacked on top of any
- 8:23:52bottom level windows
- 8:23:53if you remove a bottom piece it removes
- 8:23:55any from the top
- 8:23:56on the flip side we have tk this is a
- 8:23:59new independent window and we've
- 8:24:01actually been doing this since
- 8:24:03day one of our gui programming tk is a
- 8:24:06new independent window and our main
- 8:24:09window and the new window that we create
- 8:24:10are not linked whatsoever they're
- 8:24:12completely independent
- 8:24:13so if we were to create a new window
- 8:24:16this is our new window and close out of
- 8:24:17our old window
- 8:24:18well this new window still persists so
- 8:24:21this could be useful for
- 8:24:22let's say a login form or a login screen
- 8:24:25after you log in you'll be brought to
- 8:24:26your main program which could be within
- 8:24:28the new window
- 8:24:29one other thing that you can do too you
- 8:24:31can also destroy the old window after
- 8:24:33creating a new window
- 8:24:35so we need to type in the name of the
- 8:24:37window that we want to destroy
- 8:24:39actually let's call the main window just
- 8:24:41old underscore window just to clarify
- 8:24:43things
- 8:24:44so let's rename that here here and here
- 8:24:47as well
- 8:24:48so we want to destroy our old window as
- 8:24:51if we're closing out of it
- 8:24:52so we type in the name of the window old
- 8:24:55underscore window
- 8:24:56and we use the destroy function and that
- 8:24:58sounds pretty sweet
- 8:25:00so we're going to destroy our old window
- 8:25:02after creating
- 8:25:03our new window so let me add a comment
- 8:25:05that this will
- 8:25:06close out of old window
- 8:25:10and let's test it so we have our old
- 8:25:13window
- 8:25:14our main window we'll create a new
- 8:25:16window it creates our new window and
- 8:25:18then closes out of the old window
- 8:25:20so that's one of a few ways in which you
- 8:25:22can create a new window for your program
- 8:25:24so if you want to copy of all this code
- 8:25:26i'll post all of this in the comments
- 8:25:28down below
- 8:25:28but yeah that's how to create a new
- 8:25:30window using python
- 8:25:32hey what's going on everybody too bro
- 8:25:34here hope you're doing well and in this
- 8:25:36video i'm going to teach you guys how we
- 8:25:37can create separate tabs for our gui
- 8:25:39applications in python so
- 8:25:41sit back relax and enjoy the show
- 8:25:45okay let's begin so in order to create
- 8:25:47tabs for our python gui application
- 8:25:50we're going to need access to a widget
- 8:25:52called notebook
- 8:25:53and that's found in a different module
- 8:25:56so this time we're going to need
- 8:25:57two imports so at the top from tk enter
- 8:26:00import
- 8:26:01just everything but the notebook widget
- 8:26:03is found within a separate module
- 8:26:05so we'll need a second import from tk
- 8:26:09enter
- 8:26:10import ttk so this ttk import
- 8:26:14gives us access to several different
- 8:26:16widgets that are normally not available
- 8:26:18to us
- 8:26:18so once you have these two imports
- 8:26:20you're ready to begin so we'll need a
- 8:26:22window
- 8:26:23window equals tk followed by window.main
- 8:26:26loop
- 8:26:26and we need to create a notebook widget
- 8:26:29so
- 8:26:30right after you create your window we're
- 8:26:31going to create a notebook
- 8:26:33we'll call this notebook equals ttk
- 8:26:37dot notebook and we want to add
- 8:26:40this notebook to our window so let me
- 8:26:43add a comment here what this widget does
- 8:26:45so this is a widget that manages
- 8:26:49a collection of windows
- 8:26:52and displays and that's really all there
- 8:26:55is to it
- 8:26:56so in order to create different tabs
- 8:26:58we're going to create some frames
- 8:27:00so for the first tab i'll call this tab
- 8:27:021 equals frame
- 8:27:04and we want to add this to the notebook
- 8:27:08and with our notebook we're adding this
- 8:27:10notebook to the window
- 8:27:12all right so this will be a new frame
- 8:27:16for tab one and let's create a second
- 8:27:19tab
- 8:27:19and we'll call this tab two and you can
- 8:27:22create more tabs if you want but that
- 8:27:24might be overkill
- 8:27:25so this is a new frame for tab two
- 8:27:28after you finish these two lines of code
- 8:27:30we're going to follow this up with
- 8:27:32notebook.ad so notebook.ad
- 8:27:36what widget are we adding we're adding
- 8:27:39our tab
- 8:27:391 frame and we can set some text for
- 8:27:43this tab so text equals and i'll type in
- 8:27:46tab one and we'll repeat the process for
- 8:27:49tab two
- 8:27:51so notebook dot add tab two
- 8:27:54and we'll change the text to tab two and
- 8:27:57then we need to
- 8:27:58pack this notebook notebook dot pack
- 8:28:02and let's take a look to see what we
- 8:28:04have all right we have
- 8:28:06tab one and tab two but we'll probably
- 8:28:09want to actually add something
- 8:28:11to each of these tabs so that the
- 8:28:13content is visible so let's create some
- 8:28:15labels
- 8:28:15so after we pack our notebook let's
- 8:28:18create some labels
- 8:28:19i don't really feel like giving these
- 8:28:21names because we don't really plan on
- 8:28:22using them for anything else
- 8:28:24so we'll just say label tab
- 8:28:27one that's going to be the parent widget
- 8:28:30and let's set some text text equals
- 8:28:33hello this is tab
- 8:28:36number one then i think i'll give this a
- 8:28:40width and a height too
- 8:28:41just so it's more visible so for the
- 8:28:44width i'll make this 50
- 8:28:45and the height 25 so this will be
- 8:28:48roughly a square shape
- 8:28:51and then we need to pack this so i'm
- 8:28:52just going to add pack
- 8:28:55and let's create a second label for tab
- 8:28:57two
- 8:28:58so label tab two maybe i'll say
- 8:29:02goodbye this is tab number two
- 8:29:05okay we should have some actual content
- 8:29:08for these labels now for these tabs
- 8:29:10so here's the label this is tab one and
- 8:29:13this is tab two and you can switch
- 8:29:15between these and they display
- 8:29:16each of these separate labels however
- 8:29:19with the way that this window is
- 8:29:20arranged
- 8:29:21if we were to expand the size of this
- 8:29:23window well
- 8:29:25our frames and our tabs kind of stay in
- 8:29:27the top
- 8:29:28and in the center so i'm going to add a
- 8:29:30few lines within
- 8:29:32the pack function of our notebook the
- 8:29:34first thing i'm going to do
- 8:29:35is use the expand option and set this to
- 8:29:38true
- 8:29:40so what this does and i'm just going to
- 8:29:41add a comment that expand
- 8:29:44this will expand
- 8:29:47to fill any space
- 8:29:50not otherwise
- 8:29:53used so what happens now
- 8:29:57is that this will actually expand
- 8:30:00and it's staying in the center of the
- 8:30:02window but there's one more thing we're
- 8:30:04going to add
- 8:30:05fill equals both
- 8:30:08normally this is x or y or you can do
- 8:30:11both
- 8:30:12so i'll add a comment as to what this
- 8:30:14option does
- 8:30:16so fill will fill
- 8:30:19space on x and
- 8:30:22y axis so if you combine
- 8:30:26both of these together what you get is
- 8:30:28that these tabs will stay
- 8:30:30in the top left corner and if you were
- 8:30:32to expand the size of this window
- 8:30:34the tabs aren't going to move they're
- 8:30:36going to stick to the top left
- 8:30:38but the size of the window will expand
- 8:30:41all right so that's the basics of
- 8:30:42creating tabs in python if you want a
- 8:30:44copy of all this code
- 8:30:45i'll post all of this in the comments
- 8:30:47down below but yeah that's the basics of
- 8:30:49creating tabs in python
- 8:30:52hey what's going on everybody it's your
- 8:30:54bro here hope you're doing well and in
- 8:30:56this video i'm going to teach you guys
- 8:30:57all about the grid geometry manager in
- 8:30:59python so sit back relax and enjoy the
- 8:31:02show
- 8:31:04all right everybody let's talk about the
- 8:31:06grid geometry manager
- 8:31:07well because we have to this is a useful
- 8:31:10way to organize widgets within a
- 8:31:12container
- 8:31:12imagine our windows being arranged into
- 8:31:14a series of
- 8:31:16rows and columns similar to an excel
- 8:31:18spreadsheet
- 8:31:19i tend to use that example a lot the top
- 8:31:21left most cell
- 8:31:22is row zero column zero computers always
- 8:31:25begin with zero so that's why our grid
- 8:31:27doesn't begin with
- 8:31:28row one column one and we can list which
- 8:31:31row and column
- 8:31:32we would like our widgets to be placed
- 8:31:34within and by default we only have
- 8:31:36one row and one column to work with at
- 8:31:38the start but we can explicitly state
- 8:31:41which row and column this widget should
- 8:31:43be placed within
- 8:31:44relative to the container that it's in
- 8:31:46and python will create new relative rows
- 8:31:48and columns for us if we state where
- 8:31:50these widgets
- 8:31:50should be placed so let's create a small
- 8:31:53submission form
- 8:31:54and then organize all of our widgets
- 8:31:55using our new grid system that we have
- 8:31:58so let's begin by creating a window
- 8:32:00window equals tk
- 8:32:01followed by window.mainloop now that we
- 8:32:04have our window we'll need to create
- 8:32:05some widgets to add to this window
- 8:32:07so let's say that we want an entry box
- 8:32:10and a label for a first name last name
- 8:32:12and email address as well as a submit
- 8:32:15button
- 8:32:15so let's create a label and an entry box
- 8:32:18for a first name just to demonstrate
- 8:32:20this
- 8:32:21so let's call this label first name
- 8:32:24label equals label we're adding this to
- 8:32:27the window
- 8:32:27and let's set the text to first
- 8:32:31name for now i'm just going to use that
- 8:32:34pack to demonstrate the differences
- 8:32:36between
- 8:32:36pack and grid and let's create an entry
- 8:32:40box
- 8:32:41let's call this first name
- 8:32:45entry equals entry
- 8:32:48we are adding this to the window and for
- 8:32:51now we're just going to
- 8:32:52pack this and let's take a look
- 8:32:57so here's our label on the top it says
- 8:32:59first name and then it has
- 8:33:01our entry box right underneath so what
- 8:33:03if we want to have
- 8:33:05this label on the left hand side and our
- 8:33:08entry box on the right hand side of the
- 8:33:09label
- 8:33:10we're better off using the grid geometry
- 8:33:13manager
- 8:33:14so we're going to replace pack with grid
- 8:33:21and we have to set which row and column
- 8:33:23that we want each of these widgets to be
- 8:33:24placed within
- 8:33:26normally if you use the pack geometry
- 8:33:29manager
- 8:33:29as you create new widgets they're just
- 8:33:31going to be placed directly underneath
- 8:33:33in one long column so
- 8:33:36let's say that for our name label we
- 8:33:39want to place this where
- 8:33:41the row is zero and the column is zero
- 8:33:43that's the top left corner
- 8:33:45so within the grid geometry manager when
- 8:33:48we call it
- 8:33:49we're going to set row to equal zero as
- 8:33:52well as column
- 8:33:53to equal zero and then for the first
- 8:33:56name
- 8:33:56entry we want this to be on the right
- 8:33:58hand side of our label
- 8:34:00so we'll place this within the same row
- 8:34:03row
- 8:34:03equals zero but in a different column
- 8:34:06column
- 8:34:07equals one so column one is directly to
- 8:34:10the right of column zero
- 8:34:12and then when we compile and run this we
- 8:34:14now have our entry box
- 8:34:16to the right of our label that we have
- 8:34:18so let's repeat the process
- 8:34:19for a last name an email address and
- 8:34:22then a submit button
- 8:34:23so i'm just going to copy what we have
- 8:34:25here and change first name to
- 8:34:27last name so last name label
- 8:34:31and we'll change the text to last name
- 8:34:35last name entry and we'll keep that the
- 8:34:38same
- 8:34:39now we want this last name label to be
- 8:34:42underneath
- 8:34:43the first name label because right now
- 8:34:45they're going to overlap because we have
- 8:34:47them in the same
- 8:34:48row and columns so you can see that we
- 8:34:50can no longer see
- 8:34:51the first name label or the first name
- 8:34:53entry
- 8:34:54so let's place this directly underneath
- 8:34:56we're going to put these underneath by
- 8:34:58one row
- 8:34:59so we'll keep the columns the same but
- 8:35:01we're going to
- 8:35:02put this underneath by one row so row
- 8:35:05one
- 8:35:05and row one for the last name entry and
- 8:35:08what you get is that
- 8:35:10these two labels and entry boxes are
- 8:35:12arranged neatly into
- 8:35:14rows and columns and let's do the same
- 8:35:16thing for an email
- 8:35:18so i'm going to copy what we have and
- 8:35:20we're going to change
- 8:35:22last name to email so
- 8:35:25email label and email entry and then
- 8:35:28change
- 8:35:28for the text last name to just email
- 8:35:31and we're going to place these where row
- 8:35:34equals 2
- 8:35:35and we'll keep the column the same and
- 8:35:37for email entry
- 8:35:39row 2 column one
- 8:35:43so doesn't that look a lot better now
- 8:35:45let's create a submit button
- 8:35:48so let's call this submit button
- 8:35:52equals button and then we're adding this
- 8:35:54to the window
- 8:35:56and we should set some text text equals
- 8:35:59submit i guess dot grid
- 8:36:03and we want to place this underneath
- 8:36:06currently kind of just shoves it
- 8:36:07underneath for now
- 8:36:08let's say that we want this between
- 8:36:11these two columns
- 8:36:13so we'll want to set the row and the
- 8:36:15column to begin with
- 8:36:17row is going to equal three
- 8:36:20and column we'll set this equal to zero
- 8:36:22for now
- 8:36:23there isn't going to be any apparent
- 8:36:24change it's still going to be in the
- 8:36:26same place
- 8:36:27but one thing that we can do is use an
- 8:36:29option called column
- 8:36:30span we can have a widget take up more
- 8:36:32than one column
- 8:36:34and it's going to place this widget in
- 8:36:36between both of these columns
- 8:36:38in between the combined width of both of
- 8:36:40them
- 8:36:41so we'll add column span
- 8:36:44equals two so this widget is going to
- 8:36:47take up
- 8:36:48the next two available columns including
- 8:36:50the one that it's currently in and you
- 8:36:52can see that our submit button
- 8:36:54is now between these two columns that we
- 8:36:56have
- 8:36:57one thing that you should know is that
- 8:36:59the column width is dependent on the
- 8:37:01width
- 8:37:01of the largest widget that is contained
- 8:37:03within that particular column
- 8:37:05let's say that we increase the width of
- 8:37:07our first name
- 8:37:08label well our column is going to expand
- 8:37:11to actually fit
- 8:37:12that widget that we have so for our
- 8:37:14first name label
- 8:37:15let's set the width to maybe 20
- 8:37:19and this is going to increase the width
- 8:37:22of column 0
- 8:37:23and to even better demonstrate this
- 8:37:25let's color
- 8:37:26each label a certain color just for a
- 8:37:28visual
- 8:37:29for our first name label let's say this
- 8:37:31is red and then for
- 8:37:34our last name label let's set this to
- 8:37:36green
- 8:37:38and then for email maybe blue
- 8:37:46now all of these are color coordinated
- 8:37:48and you can see that
- 8:37:49the background color of our first name
- 8:37:51label
- 8:37:52is taking up the entire column space now
- 8:37:55what if we changed the size of our
- 8:37:58email label to something even larger
- 8:38:00like 30
- 8:38:01so width equals 30.
- 8:38:06and now the column 0 has expanded to
- 8:38:09accommodate the size of our largest
- 8:38:11widget which is our email
- 8:38:13which has a width of 30 and before we
- 8:38:15finish this video let's add a
- 8:38:17title to the submission form right on
- 8:38:19top of the first name label and the
- 8:38:22entry label so let's call this title
- 8:38:25label equals label writing this to the
- 8:38:28window
- 8:38:28let's set some text let's say enter
- 8:38:32your info and let's pick font
- 8:38:36font equals
- 8:38:40what am i feeling like today maybe just
- 8:38:42arial
- 8:38:43and then a size maybe 25 we're going to
- 8:38:46use the
- 8:38:47grid geometry manager
- 8:38:50and we want this at the top so we want
- 8:38:53row zero at least
- 8:38:55i want this to take up two columns so
- 8:38:57we'll say column
- 8:38:59equals zero and then set a column span
- 8:39:02column span equals two
- 8:39:05now a few things are going to overlap
- 8:39:07here
- 8:39:09so we're going to move everything down
- 8:39:11by one row
- 8:39:13besides our title label so we want
- 8:39:16this first name label to be directly
- 8:39:18underneath our title label
- 8:39:20so let's move everything down by one row
- 8:39:32and our title is going to be directly on
- 8:39:34top of our first name label as well as
- 8:39:37the first name
- 8:39:38entry box that we have all right so
- 8:39:40that's the very basics of the grid
- 8:39:42geometry manager
- 8:39:43if you want a copy of all this code i'll
- 8:39:45post all of this in the comments down
- 8:39:47below
- 8:39:47but yeah that's the basics of the grid
- 8:39:49geometry manager
- 8:39:50in python hey what's going on everybody
- 8:39:53it's your bro here hope you're doing
- 8:39:55well and in this video i'm going to
- 8:39:56teach you guys how we can create a
- 8:39:57progress bar in python so
- 8:39:59sit back relax and enjoy the show
- 8:40:03okay let's create a progress bar but
- 8:40:05we'll need a few imports to begin
- 8:40:07the progress bar widget is found within
- 8:40:09that ttk module so we'll need a separate
- 8:40:12import from that
- 8:40:13from tk enter dot ttk
- 8:40:16import everything and we'll import
- 8:40:19everything from time as well let's
- 8:40:22create a window
- 8:40:23window equals tk followed by
- 8:40:25window.mainloop
- 8:40:26we're going to add a progress bar and a
- 8:40:28button to this window
- 8:40:30when we click on the button it's going
- 8:40:31to fill our progress bar
- 8:40:33let's begin by creating a button to
- 8:40:35begin with let's call this button
- 8:40:37equals button we're adding this to the
- 8:40:40window
- 8:40:41let's set some text text equals maybe
- 8:40:43download
- 8:40:45and then a command maybe i'll call this
- 8:40:48start
- 8:40:50finish by adding this button to the
- 8:40:52window and we'll just use pack to do
- 8:40:54that
- 8:40:55let's define what our start function
- 8:40:57does
- 8:40:58def start we'll fill this in later i'm
- 8:41:01just going to
- 8:41:02print nothing just as a placeholder and
- 8:41:05we should just have a button
- 8:41:07now let's add a progress bar before this
- 8:41:09button
- 8:41:11and we'll call this progress bar just
- 8:41:12bar for short bar
- 8:41:14equals progress bar
- 8:41:18we're adding this to the window
- 8:41:22we can set an orientation
- 8:41:25this can be horizontal or vertical later
- 8:41:28on in this video i'll show you the
- 8:41:29differences between the two
- 8:41:30but for now let's set this to horizontal
- 8:41:34and then i'm going to pack this bar bar
- 8:41:37dot pack
- 8:41:38and add some padding pad y
- 8:41:41equals 10 and let's see what we have
- 8:41:46okay this is our progress bar at the top
- 8:41:48and this is the button
- 8:41:49when we click on this button it's going
- 8:41:51to fill our progress bar
- 8:41:53but i think i'm going to increase the
- 8:41:55length of this progress bar
- 8:41:57let's set this to maybe 300 i believe
- 8:41:59100 is the default
- 8:42:01okay that's not too bad of a size now
- 8:42:04let's define what our start function
- 8:42:06does
- 8:42:07we can actually increase the value of
- 8:42:10our progress bar by typing in the name
- 8:42:11of this progress bar here
- 8:42:14value plus equals some amount let's say
- 8:42:1810. so what's going to happen right now
- 8:42:22is that every time we click on this
- 8:42:24button it's going to fill our progress
- 8:42:26bar by
- 8:42:2710 out of the total which is 100
- 8:42:32let's pretend that we're waiting for a
- 8:42:34certain amount of tasks
- 8:42:35to complete so let's define a few
- 8:42:38variables
- 8:42:39let's say we have a variable called
- 8:42:41tasks and let's set this to 10 just to
- 8:42:43keep it simple
- 8:42:45and x will represent the current task
- 8:42:47that we're on
- 8:42:48and we'll say 0 to begin with let's
- 8:42:51create a while loop
- 8:42:53while x is less than the amount of
- 8:42:57tasks that we have we will
- 8:43:00increment the value of our bar by 10.
- 8:43:0310 10 for each test that we have which
- 8:43:06is 10 for a total of 100
- 8:43:09and we'll want to increment the task
- 8:43:11that we're on as well
- 8:43:12which is represented by x so let's
- 8:43:15increment x by 1 after each iteration
- 8:43:18of this while loop now when we run this
- 8:43:21and click download this spills instantly
- 8:43:25let's add a delay just to simulate
- 8:43:29each task waiting to be completed so
- 8:43:32let's add that here let's add a delay by
- 8:43:35using time
- 8:43:36dot sleep and let's set this to one
- 8:43:38second
- 8:43:40we're going to wait one second and then
- 8:43:41complete a task
- 8:43:45what ends up happening now is that
- 8:43:46nothing appears to be updating
- 8:43:48well what gives bro you lie to me well
- 8:43:50that's because
- 8:43:51the window is waiting to have this
- 8:43:53progress bar complete
- 8:43:55before it's actually going to display it
- 8:43:57being filled
- 8:43:58so we would want to refresh this window
- 8:44:01after each iteration of the while loop
- 8:44:04so at the end of this while loop let's
- 8:44:06add one thing
- 8:44:07window dot update underscore idle
- 8:44:11tasks after each iteration of this while
- 8:44:14loop it's going to update
- 8:44:16the window that we have so then when we
- 8:44:18download something
- 8:44:20it's going to update our progress bar
- 8:44:22after each iteration of this while loop
- 8:44:24until it's completed
- 8:44:26right about now let's take this a step
- 8:44:29further by adding a label that displays
- 8:44:31the current percent of the progress bar
- 8:44:33that is complete
- 8:44:34let's add this label after the progress
- 8:44:36bar but before the button
- 8:44:38sandwiched right between these two
- 8:44:39widgets so let's add that here
- 8:44:42we'll call this label percent label
- 8:44:45equals label we'll add this to the
- 8:44:47window
- 8:44:48and we'll need a text variable text
- 8:44:51variable equals let's call this percent
- 8:44:54so the reason that we use a text
- 8:44:56variable is so that we can update this
- 8:44:58label with some text
- 8:45:00after each iteration of our while loop
- 8:45:02and we'll need to
- 8:45:03pack this okay within the window
- 8:45:07near the top let's define percent
- 8:45:09percent
- 8:45:10equals string var this allows us to
- 8:45:14update percent with some new text and
- 8:45:16set the percent label with the new text
- 8:45:18that we have
- 8:45:20within the while loop but before we
- 8:45:22update idle tasks
- 8:45:25let's set what percent is
- 8:45:28percent dot set and let's say
- 8:45:32x divided by tasks and then we'll
- 8:45:35multiply this by
- 8:45:36100 i'm just going to put this within
- 8:45:39its own set of
- 8:45:40parentheses times 100
- 8:45:43and then we'll convert all of this to a
- 8:45:44string
- 8:45:46so we can display this along with some
- 8:45:48text
- 8:45:50there are a lot of parentheses here so
- 8:45:51it might be difficult to keep track of
- 8:45:53all this
- 8:45:54all right and then we're just going to
- 8:45:56tack on
- 8:45:57percent and that's it let's try it
- 8:46:03so we got 10 20 30
- 8:46:0740 50. i'll shut up now one change that
- 8:46:10i'm gonna make is that i'm going to cast
- 8:46:13all of this
- 8:46:13as an integer before we convert it to a
- 8:46:15string
- 8:46:18because i do not like that decimal at
- 8:46:20the end of our percentage
- 8:46:22so yeah it says 10 percent 20 so on and
- 8:46:25so forth
- 8:46:26let's add a label that displays the
- 8:46:28current task that we're on
- 8:46:29so let's copy what we have for our
- 8:46:31percent label
- 8:46:32and let's rename this as maybe task
- 8:46:35label
- 8:46:38task label text variable will equal
- 8:46:41maybe text
- 8:46:43and let's define what text is so text
- 8:46:46is also a string var and within our
- 8:46:50while loop
- 8:46:51let's update the value of our text
- 8:46:54text dot set
- 8:46:57will display x plus maybe a forward
- 8:47:01slash
- 8:47:05plus our tasks
- 8:47:08and since we're displaying integers
- 8:47:10along with some strings
- 8:47:12we need to convert these to strings and
- 8:47:14we can use the str function to do that
- 8:47:16for us
- 8:47:19so surround x and tasks with str to
- 8:47:22convert these to strings
- 8:47:25and then we'll add just plus tasks
- 8:47:28completed so the value of x will update
- 8:47:33after each iteration of this while loop
- 8:47:35let's try it
- 8:47:36and after clicking download this will
- 8:47:39display the percentage as well as the
- 8:47:41amount of tasks that have been completed
- 8:47:44all the way until it reaches the end
- 8:47:49let's change this into a more practical
- 8:47:51example
- 8:47:52we'll pretend that we're downloading a
- 8:47:53video game so let's replace or rename
- 8:47:56tasks as maybe gb4 gigabytes
- 8:48:00and this game will be 100 gigabytes
- 8:48:03we'll rename x as well so let's refactor
- 8:48:07rename and change x to maybe download to
- 8:48:09represent
- 8:48:10the amount of gigabytes that are
- 8:48:12downloaded
- 8:48:14and let's create a variable called speed
- 8:48:17and let's set this to one
- 8:48:19alright so while our download
- 8:48:22is less than the amount of gigabytes for
- 8:48:24this game
- 8:48:25we'll sleep for maybe a portion of a
- 8:48:28second
- 8:48:28.05 should be good we'll increase
- 8:48:32the value of our bar by
- 8:48:35this amount speed divided by
- 8:48:38gigabytes and then multiply this by 100
- 8:48:44for the download we're going to
- 8:48:46increment this by whatever
- 8:48:47our speed is and right now it's one so
- 8:48:49that's the same
- 8:48:50we can keep percent the same for our
- 8:48:53text
- 8:48:53let's change tasks to gigabytes
- 8:48:57and that's all the changes that we have
- 8:48:58to make and
- 8:49:00now when we download something we're
- 8:49:02kind of pretending that we're
- 8:49:04downloading
- 8:49:05a large file or a game you can see that
- 8:49:07once it reaches 100 it says 100
- 8:49:10100 out of 100 gigabytes completed now
- 8:49:13we can change
- 8:49:14this to a different value let's say
- 8:49:15maybe 50
- 8:49:17and the speed will be maybe two
- 8:49:21so this will adjust to accommodate
- 8:49:23whatever the new file size is as well as
- 8:49:25the speed
- 8:49:26and before i forget you can change the
- 8:49:28orientation to a
- 8:49:30vertical bar if you so choose so we have
- 8:49:33this vertical download bar now
- 8:49:35which could be good for something all
- 8:49:37right so that's the basics
- 8:49:39of progress bars in python if you want
- 8:49:41to copy of all this code
- 8:49:42i'll post all of this in the comments
- 8:49:44down below but yeah that's the basics of
- 8:49:46progress bars
- 8:49:47in python what's going on everybody it's
- 8:49:50your bro here hope you're doing well and
- 8:49:52in this video i'm going to teach you
- 8:49:53guys how we can use the canvas widget to
- 8:49:55draw some simple shapes so
- 8:49:56sit back relax and enjoy the show
- 8:50:01welcome back everyone well in this video
- 8:50:03we're going to be creating a canvas
- 8:50:05a canvas is a widget that is used to
- 8:50:07draw simple graphs plots and images
- 8:50:09within a window
- 8:50:11we're going to be creating some simple
- 8:50:12shapes in this video not anything too
- 8:50:14sophisticated i might get more into the
- 8:50:16canvas widget in a separate video
- 8:50:18and at the end of this lesson we're
- 8:50:20going to practice by creating a pokeball
- 8:50:22so to begin we'll need a window window
- 8:50:25equals tk
- 8:50:26followed by window.main loop to create a
- 8:50:28canvas we can just simply call this
- 8:50:30canvas
- 8:50:31canvas equals canvas and we're adding
- 8:50:33this canvas
- 8:50:34to our window and then we need to follow
- 8:50:36this with canvas.pack or
- 8:50:38place but i'm just going to use pack
- 8:50:40because it's simple
- 8:50:41this is what our canvas looks like it's
- 8:50:43a rectangular area which we can draw
- 8:50:45graphics on
- 8:50:46we can also set the width and the height
- 8:50:48of this canvas so when we create this
- 8:50:50canvas
- 8:50:51we're going to set the options for a
- 8:50:53height and a width
- 8:50:54so let's begin with the height height
- 8:50:56equals let's say 500
- 8:50:58and the width will be the same 500 and
- 8:51:01now we have
- 8:51:02a square canvas that we can draw
- 8:51:04graphics on
- 8:51:05let's begin by creating some simple
- 8:51:07shapes maybe a line
- 8:51:08i think a line is a shape maybe a
- 8:51:10one-dimensional one i don't know
- 8:51:12i don't geometry so let's create a
- 8:51:14simple line
- 8:51:16so we type the name of the canvas dot
- 8:51:19create and there's a bunch of different
- 8:51:20shapes that we can create
- 8:51:21let's create a line
- 8:51:24now i'm just going to turn this into a
- 8:51:26comment for a moment we need to set some
- 8:51:28coordinates
- 8:51:28for this line this top left corner of
- 8:51:31our canvas
- 8:51:32is 0 0 and the bottom right corner of
- 8:51:35this canvas is
- 8:51:36500 by 500 depends on what you set the
- 8:51:39dimensions of the canvas to
- 8:51:41so when we create a line we need a
- 8:51:43starting point and an ending point
- 8:51:45so if we want this to start in the top
- 8:51:47left corner we're going to pass in
- 8:51:490 0 that is the starting position and we
- 8:51:52can place the coordinates for the ending
- 8:51:54position
- 8:51:54if we want this line to end in the
- 8:51:56bottom right corner that would be
- 8:51:58where x is 500 and y is 500 and this is
- 8:52:02what this looks like
- 8:52:03we just have a simple line going across
- 8:52:05the screen
- 8:52:06but that's not all we can also change
- 8:52:08the color of this line
- 8:52:10and the thickness let's begin by
- 8:52:11changing the color so
- 8:52:13we can do that with fill fill equals
- 8:52:16let's pick color blue
- 8:52:20and now we have a blue line going across
- 8:52:22the screen
- 8:52:23but we can also change the width of this
- 8:52:25as well so we can do that
- 8:52:26using the width option and let's set
- 8:52:28this to five
- 8:52:30now we get an extra thick line across
- 8:52:32the screen
- 8:52:34now let's make a red line
- 8:52:37so i'm just going to copy this
- 8:52:40canvas.createline and let's change
- 8:52:42some coordinates for this uh let's say
- 8:52:45we want this to start in the bottom left
- 8:52:47corner and go up to the top right
- 8:52:50so the starting x position would be zero
- 8:52:53y would actually be 500 x
- 8:52:56for the ending position would be 500 and
- 8:52:58y would be zero
- 8:53:00let's set this to red and we'll keep the
- 8:53:03width the same
- 8:53:06okay so here's our red line and notice
- 8:53:08that this red line
- 8:53:10is overlapping the blue line so when you
- 8:53:12create shapes
- 8:53:13on a canvas if there's any that overlap
- 8:53:16the one that you most recently created
- 8:53:18will appear on the top one option
- 8:53:20available to you is that you can
- 8:53:22actually
- 8:53:22give these graphics a unique name with
- 8:53:25our blue line we could call this
- 8:53:26blue line blue line equals all of this
- 8:53:30and then we can call our red line
- 8:53:32something different like
- 8:53:34red line this would be useful if you
- 8:53:35want to call or use a
- 8:53:37graphic by its name if you want to like
- 8:53:40move a graphic on the screen or have
- 8:53:41something appear
- 8:53:42you can use this by its name but we
- 8:53:44don't really need it for this lesson so
- 8:53:46i'll just revert this back to what we
- 8:53:48had previously
- 8:53:49i'm just letting you know that you can
- 8:53:50do that alright what do we have up next
- 8:53:52let's create a rectangle so canvas dot
- 8:53:55create
- 8:53:56rectangle we can set some starting
- 8:53:59coordinates and ending coordinates
- 8:54:01the starting coordinates are for the top
- 8:54:03left of the rectangle
- 8:54:04and the ending coordinates are for the
- 8:54:05bottom right so for the starting
- 8:54:07coordinates let's say
- 8:54:08we're going to place this where x is 50
- 8:54:11y is 50.
- 8:54:12for the ending coordinates we want this
- 8:54:14where
- 8:54:15x is 250 and y is 250 i'm just making up
- 8:54:18numbers
- 8:54:19put it whatever you want really and this
- 8:54:22is what this looks like
- 8:54:24but i think i'm going to comment out
- 8:54:26these lines for now
- 8:54:29and here is our rectangle well i guess
- 8:54:31it's also a square
- 8:54:33we can actually give this a fill color
- 8:54:35as well so we do that with the fill
- 8:54:37option
- 8:54:38fill equals purple
- 8:54:41i guess and now we have a
- 8:54:44purple rectangle next on our list
- 8:54:47is create polygon we can create many
- 8:54:50sorts of shapes using create polygon but
- 8:54:52it really depends on how many
- 8:54:53coordinates that we enter
- 8:54:55let's create a simple triangle so we
- 8:54:57need at least three coordinates for this
- 8:54:59to work
- 8:54:59so canvas dot create polygon
- 8:55:02and i have some coordinates picked out
- 8:55:04already so let's place the first set of
- 8:55:06coordinates where
- 8:55:07x is 250 y is zero for the second set
- 8:55:12where x is 500 and y is 500
- 8:55:15in the last set the third set we'll
- 8:55:17place this where x
- 8:55:18is zero and y is 500 and what this looks
- 8:55:22like
- 8:55:22is that we have this black triangle
- 8:55:25that's taking up most of the canvas
- 8:55:27we can also change the fill color as
- 8:55:29well so we do that with
- 8:55:31the option fill and we can set this to
- 8:55:33whatever color you want
- 8:55:35let's say yellow and we now have a
- 8:55:38yellow triangle on our screen and it
- 8:55:40kind of resembles the triforce from the
- 8:55:42legend of zelda series one of my
- 8:55:44personal favorites
- 8:55:45let's also add a border this is actually
- 8:55:48done with
- 8:55:48outline outline equals and then pick a
- 8:55:51color for the outline
- 8:55:52let's say black and we have a black
- 8:55:54outline around
- 8:55:56this graphic and you can also change the
- 8:55:58thickness of this too
- 8:55:59with width and let's set the width to
- 8:56:01five
- 8:56:02and now we have an extra thick line
- 8:56:04around our triangle
- 8:56:06with these shapes you can pass in a list
- 8:56:09of points too
- 8:56:10so i'm going to get rid of all these and
- 8:56:12we're going to pass in
- 8:56:14a list so let's create a list called
- 8:56:16points
- 8:56:17points equals all the points that we
- 8:56:20just entered in previously
- 8:56:21and then instead of passing in all these
- 8:56:24coordinates individually i'm just going
- 8:56:25to pass in our list of points
- 8:56:28and this will work exactly the same okay
- 8:56:31let's create an arc now so canvas dot
- 8:56:34create
- 8:56:34arc and arc is really just a
- 8:56:37curved line between at two points
- 8:56:40however when we list some coordinates
- 8:56:42we're not listing the coordinates of the
- 8:56:44starting position and ending position
- 8:56:45for this arc it's more or less for the
- 8:56:48amount of space that we want to allocate
- 8:56:50to actually draw this arc
- 8:56:52so let's say that we want this arc to
- 8:56:54take up the entire canvas we can pass in
- 8:56:56the dimensions
- 8:56:57for this canvas so the top left corner
- 8:56:59is zero zero
- 8:57:00and the height and the width are five
- 8:57:02hundred
- 8:57:07so this is our arc it resembles a pie
- 8:57:09slice it's not starting in the top left
- 8:57:12corner and ending down here
- 8:57:13that's the amount of space that we're
- 8:57:15giving this crate arc function to
- 8:57:17actually draw
- 8:57:18an arc it's really an entire circle but
- 8:57:21only a portion of it is visible
- 8:57:23let's also set a fill color for this so
- 8:57:25fill equals
- 8:57:26what color did we not pick yet let's say
- 8:57:29green
- 8:57:32and now we have a green pie slice
- 8:57:35there's different styles you can do too
- 8:57:38so we can change that with style equals
- 8:57:40the default is pi slice
- 8:57:42that's what we had previously let's
- 8:57:44change this to chord
- 8:57:46this will draw a line between these two
- 8:57:48points
- 8:57:49and this kind of resembles like a bow
- 8:57:51like a ball with arrows
- 8:57:52and there's also just arc
- 8:57:56where it's simply just a curved line
- 8:57:59let's change this back to pie slice well
- 8:58:02because i like pie
- 8:58:04we can change the starting position of
- 8:58:06where this arc begins with the option
- 8:58:08start
- 8:58:09so start equals 0 by default so there's
- 8:58:12going to be no apparent change
- 8:58:14let's adjust this by 90 degrees so this
- 8:58:17is in
- 8:58:18degrees so this shifted
- 8:58:22what is this direction counterclockwise
- 8:58:24by 90 degrees
- 8:58:26so 180 would theoretically flip this
- 8:58:31and now this is on the other side of
- 8:58:33what we had previously
- 8:58:34and then 270.
- 8:58:40all right we can also set an extent
- 8:58:44so the default is normally 90 degrees
- 8:58:46but let's change this to 180
- 8:58:49and now we have a what is that
- 8:58:51hemisphere half a circle
- 8:58:54all right people let's practice by
- 8:58:55creating a pokeball there's still one
- 8:58:57more function that we have to cover it's
- 8:58:59called create oval but we'll do that as
- 8:59:01we're making this
- 8:59:02so let's begin by creating the top red
- 8:59:04hemisphere of this pokeball so that's
- 8:59:06canvas
- 8:59:07dot create arc and we're going to set
- 8:59:11the extent of this
- 8:59:13to 0 0 500 500
- 8:59:16so that this is going to take up the
- 8:59:18entire canvas
- 8:59:20and then let's also set a fill color to
- 8:59:22red
- 8:59:23so fill equals red
- 8:59:26we need to change the extent of how far
- 8:59:28this extends
- 8:59:29so we'll do that with the extent
- 8:59:31function so extent
- 8:59:32equals 180 so that this is a half circle
- 8:59:36and let's also change the thickness of
- 8:59:39this outline as well
- 8:59:40using the width option so width equals
- 8:59:4410 would be good and we have
- 8:59:47a red hemisphere for the top of our
- 8:59:49pokeball let's work on the bottom half
- 8:59:51so that is a white hemisphere
- 8:59:54so canvas dot create arc these are all
- 8:59:57the same we'll change
- 8:59:58the fill to white the extent will be the
- 9:00:01same
- 9:00:02but we need a different starting
- 9:00:03position so start
- 9:00:05equals 180. so this is going to begin
- 9:00:08at the 180 degree mark so when you
- 9:00:11combine these both together we get
- 9:00:13two different colored hemispheres that
- 9:00:14form a perfect circle
- 9:00:16now let's create an oval within the
- 9:00:18center of this pokeball
- 9:00:21so we're going to do that with canvas
- 9:00:24dot create
- 9:00:26oval we're going to set the amount of
- 9:00:29space that we want to give
- 9:00:30the circle to be drawn so we'll place
- 9:00:33this where
- 9:00:33x is 190 where y is 190
- 9:00:37where x 2 or the second x position is
- 9:00:40310
- 9:00:41and y2 is 310
- 9:00:46let's set the fill color next so we do
- 9:00:48that with fill
- 9:00:49fill equals white
- 9:00:53and lastly let's just give this a
- 9:00:55thicker border
- 9:00:56so that is width
- 9:01:00equals 10 to match the width for the
- 9:01:03two arcs that we have and there you have
- 9:01:06it we have a simple
- 9:01:07pokeball all right well that's the
- 9:01:09basics of the canvas widget if you'd
- 9:01:11like a copy of all this code i'll post
- 9:01:13all of this in the comments down below
- 9:01:15but yeah that's the basics of the canvas
- 9:01:17widget in python
- 9:01:18what's going on everybody it's your bro
- 9:01:21here hope you're doing well and in this
- 9:01:22video i'm going to teach you guys how we
- 9:01:24can create key events in python so sit
- 9:01:26back relax and enjoy the show
- 9:01:30welcome back everyone in this video
- 9:01:33we're going to be discussing at key
- 9:01:34events
- 9:01:35we can bind a key event and a function
- 9:01:38to a widget or a window so that when we
- 9:01:41press a certain key
- 9:01:43or do something we can trigger a
- 9:01:45function to be called that will perform
- 9:01:47some sort of task for us
- 9:01:48so we can actually do this by using the
- 9:01:51bind function
- 9:01:52so widgets and windows have access to a
- 9:01:55bind function
- 9:01:57so when our window or our widget is
- 9:01:59selected
- 9:02:00and then you press a certain key or do
- 9:02:02something then it's going to call a
- 9:02:04certain function to perform a task for
- 9:02:06us
- 9:02:06so there's two arguments within the bind
- 9:02:08function this takes an event
- 9:02:10as well as a function name let's say
- 9:02:13that when we press the enter key we want
- 9:02:15to
- 9:02:16call this function to do something so
- 9:02:18for this event
- 9:02:20what we type for that is a set of quotes
- 9:02:23angle brackets and the name of the
- 9:02:25button
- 9:02:26so for example if you want to press w to
- 9:02:28move forward like you're playing a game
- 9:02:30this would be w
- 9:02:32a s d there's also up
- 9:02:35down and then let's say we want to press
- 9:02:38enter to do something that is actually a
- 9:02:40return and then we will call
- 9:02:42this function so let's create a function
- 9:02:44called do something
- 9:02:46and let's define this at the top def
- 9:02:50do something now pay attention to this
- 9:02:53part we need to set up
- 9:02:54one parameter for this do something
- 9:02:56function
- 9:02:58this needs an event so be sure to
- 9:03:00include that and it's easy to forget
- 9:03:02so what do we want to do after we press
- 9:03:04enter
- 9:03:05let's just print a message you
- 9:03:09did a thing and
- 9:03:12this should work now so let's try it
- 9:03:17so while this window has focus ability
- 9:03:20we can actually uh have this respond to
- 9:03:22events
- 9:03:23so right now i'm pressing all sorts of
- 9:03:24keys you can probably hear me right now
- 9:03:26going crazy on my keyboard but now i'm
- 9:03:28going to hit
- 9:03:29enter and it says you did a thing and
- 9:03:32you can bind all sorts of keys to
- 9:03:33this window let's say that i want to
- 9:03:35press q to quit
- 9:03:38so we'll just replace this with
- 9:03:39lowercase q
- 9:03:42so i'm typing all sorts of keys but now
- 9:03:44i'm going to press q
- 9:03:45and it says you did a thing kind of like
- 9:03:48we're quitting the game
- 9:03:49you can actually have this respond to
- 9:03:51all keys
- 9:03:52well almost all of them so to do that
- 9:03:55you just type in key with the capital k
- 9:03:58so i can pretty much press on any key
- 9:04:01then it's going to
- 9:04:02trigger or call our function
- 9:04:05kind of like that so one thing that we
- 9:04:08could do
- 9:04:09we can display the key event that was
- 9:04:11pressed so i'm just going to add that
- 9:04:13here
- 9:04:14maybe i'll change this message u
- 9:04:18pressed plus and to display
- 9:04:21what key was pressed that is event
- 9:04:24dot key sim i believe that's for
- 9:04:28key symbol so this will actually display
- 9:04:31what button you pressed
- 9:04:32so w-a-s-d
- 9:04:36here's a unique idea for practice let's
- 9:04:39say that we want to add
- 9:04:40a label to our window and we're going to
- 9:04:42change the text on the label
- 9:04:44to reflect whatever key that we press so
- 9:04:47let's create a label
- 9:04:48label equals label we're adding this to
- 9:04:50our window
- 9:04:52and let's change the font as well to
- 9:04:54something massive
- 9:04:55like helvetica
- 9:04:59and a size of 100 that should be good
- 9:05:02then we need to pack this label so
- 9:05:04label dot pack then within
- 9:05:07this function let's turn this into a
- 9:05:09comment
- 9:05:10and we're going to use label dot config
- 9:05:14and we're going to update our text
- 9:05:17and the text is going to be whatever key
- 9:05:19that we press so we can do that with
- 9:05:21event dot key sim so now
- 9:05:25this label is going to display whatever
- 9:05:28key that we press
- 9:05:29so for example w a s
- 9:05:32d backspace escape
- 9:05:36up up down down left right left right b
- 9:05:38a start
- 9:05:39all right so that's the basics of key
- 9:05:41events if you could do me a favor and in
- 9:05:43the comments down below
- 9:05:44just press f to pay respects so if you
- 9:05:47want a copy of this code i'll post this
- 9:05:49in the comments down below but yeah
- 9:05:50that's the basics of key events in
- 9:05:53python
- 9:05:54hey what's going on everybody it's your
- 9:05:55bro here hope you're doing well and in
- 9:05:57this video i'm going to teach you guys
- 9:05:58all about
- 9:05:59mouse events in python so sit back relax
- 9:06:01and enjoy the show
- 9:06:05hey everyone we're going to be
- 9:06:06discussing mouse events in python so
- 9:06:08before we begin we'll need a window so
- 9:06:10window equals tk followed by window.main
- 9:06:13loop
- 9:06:14and in the last video we learned that we
- 9:06:16can bind
- 9:06:17a widget or a window so that when a
- 9:06:19certain event occurs we can call a
- 9:06:21function
- 9:06:22so we're going to do the same thing for
- 9:06:24this video but we have
- 9:06:25various mouse events that we can cover
- 9:06:28so we're going to use the window.bind
- 9:06:31function
- 9:06:31and this takes two arguments an event as
- 9:06:34well as a function
- 9:06:36name so let's define a function let's
- 9:06:38call it the
- 9:06:39do something function so do something
- 9:06:42and we'll need to define this so at the
- 9:06:44top def
- 9:06:46do something and remember that we need
- 9:06:49to set up
- 9:06:49one parameter for this function the
- 9:06:52parameter
- 9:06:53is an event and then we'll just print
- 9:06:55something print
- 9:06:57you did a thing
- 9:07:01okay so there are various mouse events
- 9:07:04for a
- 9:07:04left button click within quotes and then
- 9:07:08within angle brackets
- 9:07:09a left button click is button dash one
- 9:07:13so then if we were to click within this
- 9:07:16window
- 9:07:16it's going to call our function you did
- 9:07:19a thing
- 9:07:20but if i were to right click it doesn't
- 9:07:22do anything
- 9:07:24so there are other mouse events too so
- 9:07:26button two
- 9:07:27is that scroll wheel not if you scroll
- 9:07:29up or down but if you press
- 9:07:31in on it so right now i'm clicking left
- 9:07:34i'm clicking right
- 9:07:35now i'm going to press in on the mouse
- 9:07:37wheel and it says you did a thing
- 9:07:40button three is a right mouse click
- 9:07:46so i'm clicking left nothing but i'm
- 9:07:49gonna click right
- 9:07:51and that seems to work all right so
- 9:07:53button one i'm just going to add a note
- 9:07:56left mouse click button two
- 9:08:00is the scroll wheel
- 9:08:04that's if you press in on it and then
- 9:08:07button three
- 9:08:08is a right mouse click
- 9:08:11so did you guys know we can actually get
- 9:08:13the x and y coordinates of where this
- 9:08:15event occurred
- 9:08:16so to do that we type in the name of the
- 9:08:19event
- 9:08:20dot x however since we're displaying
- 9:08:23this along with some text
- 9:08:24we'll need to convert this to a string
- 9:08:28so string event dot x then i'll add the
- 9:08:31y coordinates as well
- 9:08:35event.y i think i'll just add a comma
- 9:08:38between these
- 9:08:39to separate them and we'll change our
- 9:08:42text
- 9:08:44so we'll say mouse coordinates
- 9:08:48all right let's try this again so this
- 9:08:51is going to
- 9:08:52give us the coordinates of where we
- 9:08:54click and right now i'm just left
- 9:08:55clicking
- 9:08:58so the top left corner is zero zero
- 9:09:02well at least close to it not there
- 9:09:04exactly
- 9:09:06alright so here's a few other events
- 9:09:08that we can cover
- 9:09:11i'm going to copy this we have button
- 9:09:14release
- 9:09:15if we were to release a button it's
- 9:09:18going to trigger our event
- 9:09:21so right now i'm holding in on the left
- 9:09:23mouse button but i'm going to let go
- 9:09:25then it triggers our event if i were to
- 9:09:28right click
- 9:09:29and hold it it doesn't do anything but
- 9:09:31as soon as i let go
- 9:09:32then our event is triggered we also have
- 9:09:35enter
- 9:09:36not to be confused with the enter button
- 9:09:39just going to get rid of that so that
- 9:09:42is enter if we were to enter our binded
- 9:09:46window or widget
- 9:09:49so if i were to enter it's going to give
- 9:09:51the coordinates of where i entered this
- 9:09:56there is also leave
- 9:09:59so i'm going to say that enter
- 9:10:03the window
- 9:10:07and leave is leave the window
- 9:10:14so if i enter nothing happens but as
- 9:10:16soon as i leave it gives the coordinates
- 9:10:22and the last one is motion as long as
- 9:10:24the cursor is in motion
- 9:10:28so this one might be good for a game so
- 9:10:31that is motion
- 9:10:34where the mouse
- 9:10:40moved
- 9:10:42it's consistently giving me the
- 9:10:43coordinates if i'm moving the mouse but
- 9:10:45if i were
- 9:10:45stationary or stop moving it stops
- 9:10:50all right so that's the basics of mouse
- 9:10:52events in python if you want to copy of
- 9:10:54all this code
- 9:10:54i'll post all of this in the comments
- 9:10:56down below but yeah that's the basics of
- 9:10:58mouse events in python hey what's going
- 9:11:01on everybody it's bro here hope you're
- 9:11:03doing well and in this video i'm going
- 9:11:05to teach you guys how we can drag
- 9:11:06and drop widgets in python so sit back
- 9:11:09relax and enjoy the show
- 9:11:12all right people let's just dive into
- 9:11:15this so we'll need a window and then
- 9:11:16we're going to be adding widgets to the
- 9:11:18window
- 9:11:19so window equals tk followed by
- 9:11:21window.main loop
- 9:11:23now you can drag and drop all sorts of
- 9:11:25widgets but let's just stick with the
- 9:11:26label because creating a label is simple
- 9:11:29so let's define a label label equals
- 9:11:32label we're going to be adding this
- 9:11:35label to the window
- 9:11:36and let's set a background color so we
- 9:11:38can actually see this label
- 9:11:40pick i don't know your favorite color
- 9:11:42i'm going to pick red
- 9:11:43and then i'm going to set a width and a
- 9:11:45height because normally if you have a
- 9:11:47label without any text it's really small
- 9:11:50so let's set a width of maybe 10
- 9:11:53and then a height of 5.
- 9:11:57that should be good and we want to place
- 9:11:59this label
- 9:12:00someplace within the window so label dot
- 9:12:03place and we can pass in some x
- 9:12:05coordinates as well as y coordinates
- 9:12:07so x equals zero and y equals zero
- 9:12:11so that this is going to appear in the
- 9:12:13top left corner
- 9:12:14of our window so this is what we have
- 9:12:17this
- 9:12:18is our label and we want to be able to
- 9:12:20drag and drop
- 9:12:21this label someplace within the window
- 9:12:23what we're going to need to do
- 9:12:24is actually bind this label twice but
- 9:12:27let's begin with just
- 9:12:28the first binding so in order to bind a
- 9:12:31widget you type in the name of the
- 9:12:32widget
- 9:12:33followed by calling the bind function
- 9:12:36so this bind function can take two
- 9:12:39arguments
- 9:12:40it can take an event as well as a
- 9:12:42function
- 9:12:43name so for the function name let's call
- 9:12:45this
- 9:12:46drag underscore start but you don't
- 9:12:48really need to
- 9:12:49name it the same that i do and for the
- 9:12:52event that occurs
- 9:12:53this is going to be a left button click
- 9:12:55with the mouse
- 9:12:56so that event is within quotes and
- 9:12:59within angle brackets
- 9:13:01button dash one so if we were to click
- 9:13:04someplace within the label using the
- 9:13:06left mouse button
- 9:13:07it's going to call our drag start
- 9:13:10function but we need to define this
- 9:13:12so at the top of our program let's
- 9:13:13define this function
- 9:13:16def drag start and this takes one
- 9:13:19parameter
- 9:13:20the parameter we have is our event so
- 9:13:22what we're going to do
- 9:13:23is actually get the coordinates of where
- 9:13:26we click
- 9:13:27within this label and we're going to
- 9:13:29assign these
- 9:13:30to a new variable or attribute of our
- 9:13:33label
- 9:13:34so label dot let's create an attribute
- 9:13:37called
- 9:13:37start x equals our event
- 9:13:41dot x so this is where we click
- 9:13:44within the label and not necessarily the
- 9:13:47window
- 9:13:48and let's repeat the process for y so
- 9:13:50label
- 9:13:51dot start y equals event dot
- 9:13:54y so we're going to have some
- 9:13:57coordinates of where we click
- 9:13:58within this label and we can actually
- 9:14:01use that for our next
- 9:14:02function so we're going to bind this
- 9:14:04label a second time
- 9:14:06so i'm going to copy this line and paste
- 9:14:08it
- 9:14:09the second binding let's say is
- 9:14:12drag underscore motion for the function
- 9:14:15name
- 9:14:16and this event is going to occur if we
- 9:14:19were to hold down the left mouse button
- 9:14:21and then drag so that is b
- 9:14:25one dash motion
- 9:14:28and then we need to define this function
- 9:14:30so at the top
- 9:14:32d e f drag underscore motion and this
- 9:14:35takes our event as a parameter
- 9:14:37so this part is going to be a little
- 9:14:39funky we're going to
- 9:14:41get or create new x and y
- 9:14:44coordinates so let's begin with x x
- 9:14:47equals the name of our label or widget
- 9:14:50label dot w info
- 9:14:54underscore x this is a function so add
- 9:14:56some parentheses
- 9:14:58this will get the top left x coordinate
- 9:15:01of our label relative to the window that
- 9:15:04we're in
- 9:15:05minus label x well labeled.startx
- 9:15:11plus event dot x
- 9:15:14all right so we're going to do the same
- 9:15:17thing for y
- 9:15:19well we're going to replace x with y for
- 9:15:21the second line
- 9:15:25all right so this is the top
- 9:15:29left corner the x coordinate of our
- 9:15:32widget
- 9:15:33relative to the window this
- 9:15:37is the place where we click within
- 9:15:40the label itself and this is where we
- 9:15:43begin
- 9:15:44dragging our widget to it's a little
- 9:15:47difficult to explain but if you can find
- 9:15:48combine
- 9:15:49combine all of these together you'll get
- 9:15:51the new coordinates
- 9:15:52of where you want to drag this component
- 9:15:56this widget to
- 9:15:57so we need to replace this widget so
- 9:16:00label dot place x equals x
- 9:16:04and y equals y so then when you combine
- 9:16:07all of these together
- 9:16:09we have our widget and we can drag it
- 9:16:11and drop it someplace else within the
- 9:16:13window
- 9:16:15now wait a second what if we have more
- 9:16:17than one widget
- 9:16:18well with the way that our functions are
- 9:16:20written now they wouldn't be compatible
- 9:16:22with more than one widget
- 9:16:24just this label itself so what if we
- 9:16:27were to create a
- 9:16:28second label so i'm going to call the
- 9:16:30second label just
- 9:16:32label two so i'm just going to rename
- 9:16:34them and
- 9:16:35let's change the background color to
- 9:16:37maybe
- 9:16:38blue and i'm just going to place this
- 9:16:40somewhere else within our window so
- 9:16:41maybe where x
- 9:16:42is 100 and y is 100 and we'll need to
- 9:16:45bind
- 9:16:46label two as well so
- 9:16:49make sure to bind label two so we have
- 9:16:53label one binded it's just called label
- 9:16:55and the same thing with label two
- 9:16:57all right let's see what happens i'm
- 9:16:59going to attempt to drag
- 9:17:01label 2. the label 1 is moving that's
- 9:17:04because
- 9:17:04with our functions we stated we're
- 9:17:07getting the coordinates
- 9:17:09and we're storing them within the
- 9:17:10attributes of label
- 9:17:121 and not necessarily label two so in
- 9:17:14order to make
- 9:17:15these functions compatible with all
- 9:17:18widgets
- 9:17:19we can actually add one line of code
- 9:17:22widget equals event dot widget
- 9:17:26so this is going to get the widget of
- 9:17:28the event that we're dealing with
- 9:17:30and we're going to temporarily rename
- 9:17:32this as widget
- 9:17:34so now we're going to replace all
- 9:17:36instances of where we use
- 9:17:37label and replace it with widget
- 9:17:42be sure to do the same thing too with
- 9:17:43drag motion
- 9:17:46and then we will need to add this line
- 9:17:48at the top of
- 9:17:49drag motion so now
- 9:17:53these functions are now compatible with
- 9:17:55any widget that we create
- 9:17:59and we should be able to drag and drop
- 9:18:02both
- 9:18:03widgets all right well that's the basics
- 9:18:06of dragging and dropping widgets in
- 9:18:08python if you want a copy of all this
- 9:18:10code i'll post all of this in the
- 9:18:11comments down below
- 9:18:13but yeah that's the basics of dragging
- 9:18:14and dropping widgets in python
- 9:18:18hey what's going on everybody it's your
- 9:18:20bro here hope you're doing well and in
- 9:18:21this video i'm going to teach you guys
- 9:18:22how we can move an image
- 9:18:24on both a window and a canvas in python
- 9:18:27so sit back relax and enjoy the show
- 9:18:31all right my fellow bros i'm going to
- 9:18:33divide this video into
- 9:18:35two different sections in the first
- 9:18:37section we're going to move a widget
- 9:18:39within a window
- 9:18:40and in the next section we're going to
- 9:18:42move an image
- 9:18:43on a canvas so let's begin by moving a
- 9:18:46widget within a window
- 9:18:47so we'll need a window window equals tk
- 9:18:50followed by window.mainloop and we'll
- 9:18:52also need to set some geometry for this
- 9:18:54window
- 9:18:55window dot geometry let's make this
- 9:18:58500x500
- 9:19:01500 x 500 and let's take a look
- 9:19:06okay that should be a decent size let's
- 9:19:08add a label to this window
- 9:19:11so i actually have a race car image that
- 9:19:13i want to use if you would like an image
- 9:19:15feel free to pause the video and
- 9:19:16download one
- 9:19:17what i would like to do is create a
- 9:19:19photo image from this png file that i
- 9:19:21have
- 9:19:22i'm just going to call this my
- 9:19:25image equals photo
- 9:19:28image file equals since this is within
- 9:19:32my project folder i can just list the
- 9:19:34file name
- 9:19:36otherwise you might have to list the
- 9:19:37file path and for me this is called
- 9:19:40racecar.png
- 9:19:42i want to add this image to my label not
- 9:19:45necessary but
- 9:19:46it'll be helpful for this example i'm
- 9:19:48going to call my label just a label
- 9:19:50label equals label i'm adding this to
- 9:19:52the window and i'm going to set the
- 9:19:54image to equal
- 9:19:55my image that i created it's a photo
- 9:19:57image
- 9:19:58if you do not have an image that you
- 9:20:00want to use you can simply
- 9:20:02just change the background color you
- 9:20:03could say bg
- 9:20:05i don't know red
- 9:20:08oh then we need to place this so be sure
- 9:20:10to do that
- 9:20:11label dot place and i'm going to place
- 9:20:13this in the top left corner that is
- 9:20:15where x
- 9:20:15equals zero and y equals zero and then
- 9:20:18we should have a race car
- 9:20:20or whatever image you used and a red
- 9:20:22background you can get rid of the
- 9:20:24background color if you prefer
- 9:20:25that's just if you don't have an image
- 9:20:27to use you can set the background color
- 9:20:29just for a
- 9:20:30visual to show you where the label
- 9:20:32currently is
- 9:20:33let's set some key bindings for this
- 9:20:35window and let's add them maybe here
- 9:20:37before we create the label and our image
- 9:20:39so let's find the w key for up s for
- 9:20:43down
- 9:20:43a for left and d for right so we do that
- 9:20:46with
- 9:20:47window dot bind and we pass in an event
- 9:20:51as well as a function name so we want to
- 9:20:54tie the w key to a function and we'll
- 9:20:58call this
- 9:20:58move underscore up and then we will need
- 9:21:02to define this
- 9:21:04so outside of the window let's define
- 9:21:08move underscore up and this takes
- 9:21:12an event so we'll place that as a
- 9:21:14parameter
- 9:21:15and to move our label up we're going to
- 9:21:18take
- 9:21:18label dot place
- 9:21:22and we're going to place this at some
- 9:21:23new coordinates
- 9:21:25x equals the label's current position
- 9:21:28relative to the window that it's in so
- 9:21:31that is label
- 9:21:32dot w info
- 9:21:35underscore x
- 9:21:38all right so then for y it's similar
- 9:21:41y equals label dot w
- 9:21:45info underscore y
- 9:21:48but we're going to subtract maybe one
- 9:21:52all right so this is all we need
- 9:21:56although this is only going to move up
- 9:21:58so i'm going to press
- 9:21:59w and our label our race car or whatever
- 9:22:03image you used is going to move
- 9:22:04slowly up i'm going to increase this to
- 9:22:0810 so the change is more drastic
- 9:22:11so now our race car is drifting off into
- 9:22:13space
- 9:22:15let's find a few other keys s a
- 9:22:18and d sad all right so
- 9:22:21s will be down move underscore down
- 9:22:27a for left move left
- 9:22:31and d for right move
- 9:22:35right we'll just copy what we have here
- 9:22:38and make a few changes
- 9:22:39let's begin with move down
- 9:22:44the only thing we're going to change
- 9:22:45within here is in place of subtracting
- 9:22:4810
- 9:22:48from the y coordinate we're going to add
- 9:22:5010
- 9:22:52and with our race car oh i guess we have
- 9:22:54to define these so let's do that real
- 9:22:56quick
- 9:22:57uh so we have left
- 9:23:00move left we're going to subtract
- 9:23:0310 from the x coordinate and keep y the
- 9:23:07same
- 9:23:09and then the same thing for move right
- 9:23:11move
- 9:23:12underscore right and this time we are
- 9:23:15going to add 10
- 9:23:16to x and y will stay the same
- 9:23:21okay now with our image we can move up
- 9:23:25down right and then left
- 9:23:29and y stop there we combine these to our
- 9:23:32arrow keys
- 9:23:36so up is just up with a capital u
- 9:23:39down is down these are all capital by
- 9:23:42the way
- 9:23:43left is left and then right is right
- 9:23:49and now i can use my arrow keys to move
- 9:23:51this image
- 9:23:58now for this part of the video i'm going
- 9:24:00to teach you guys how we can move an
- 9:24:02image on a canvas this time and not just
- 9:24:04within a window
- 9:24:05but we'll need a window to begin with
- 9:24:07window equals tk followed by
- 9:24:09window.mainloop
- 9:24:10and we're going to create a canvas
- 9:24:12canvas equals canvas
- 9:24:15we're adding this canvas to the window
- 9:24:17and let's set a width and height
- 9:24:19for the width let's make this 500 and
- 9:24:21the height 500 as well
- 9:24:22so this is an even square and we'll need
- 9:24:25to
- 9:24:26pack or place this canvas canvas.pack
- 9:24:28should take care of that
- 9:24:30and let's run this and this is our
- 9:24:32canvas
- 9:24:33at this step we need to add an image to
- 9:24:36our canvas and i have an image of a
- 9:24:38racecar that i want to add
- 9:24:39so i need to create a photo image from
- 9:24:42this file
- 9:24:43so we can do that let's give this photo
- 9:24:46image a unique name i'm just going to
- 9:24:47call this photo image to keep it simple
- 9:24:50photo image equals photo image and i can
- 9:24:53list the file path or the file name
- 9:24:55since
- 9:24:56this image is within my project folder i
- 9:24:59only have to list the file name
- 9:25:00and my file is called racecar.png
- 9:25:04this next step we need to give our image
- 9:25:07a unique name
- 9:25:08the image that's being added to the
- 9:25:09canvas i'll call this
- 9:25:11my image equals
- 9:25:15and we need to turn this photo image
- 9:25:18into an image added to the canvas
- 9:25:20so we type in the name of the canvas
- 9:25:22canvas
- 9:25:23dot create underscore image
- 9:25:26we first need to list the coordinates of
- 9:25:28where we want to place this photo image
- 9:25:31so let's place this in the top left
- 9:25:32corner that is where
- 9:25:34x is 0 and y 0 and we need to set the
- 9:25:37image of the image that we're creating
- 9:25:40so we can do that with saying
- 9:25:41image equals the name of our photo image
- 9:25:46and let's see what we have so far so
- 9:25:49this image is somewhat cut off in the
- 9:25:51top left corner we can easily fix that
- 9:25:53by anchoring
- 9:25:54our image when we create it so let's at
- 9:25:56the end add
- 9:25:57anchor equals northwest
- 9:26:00so that should fix that problem and we
- 9:26:03now have our image that we want to use
- 9:26:06and to move our image we can set some
- 9:26:07key bindings
- 9:26:10so we type in the name of the window dot
- 9:26:12bind
- 9:26:13and we pass in two arguments an event as
- 9:26:16well as a function name
- 9:26:18let's tie pressing the w key to moving
- 9:26:21up
- 9:26:21so the first argument is going to be w
- 9:26:24and the second is going to be
- 9:26:25a function name so let's say move
- 9:26:28underscore up
- 9:26:29and let's find some other keys
- 9:26:33w for up s for down
- 9:26:39a for left and
- 9:26:42d for right
- 9:26:45we'll need to define these now so at the
- 9:26:48top outside of the window
- 9:26:50let's begin by defining move up
- 9:26:53def move up this has an event as a
- 9:26:56parameter
- 9:26:59and let's create the other definitions
- 9:27:02here
- 9:27:05so i'm just going to rename these so we
- 9:27:08have move
- 9:27:08up move down move left
- 9:27:12and move right okay now this next step
- 9:27:18we can use the canvases move function to
- 9:27:20move an image
- 9:27:21a certain amount of pixels on the x-axis
- 9:27:24and
- 9:27:24y-axis so let's begin with move up we
- 9:27:27type in the name of the canvas
- 9:27:29dot and use the move function and this
- 9:27:32has
- 9:27:32three arguments the image that we want
- 9:27:34to use
- 9:27:35and this image is called my image the
- 9:27:38one with the race car
- 9:27:41the next argument is the amount of
- 9:27:43pixels we're moving this on the
- 9:27:45x-axis with up we can keep this the same
- 9:27:50but on the y-axis let's say we want to
- 9:27:52move this
- 9:27:53up 10 pixels so that would be -10
- 9:27:57and let's repeat the process for move
- 9:27:59down move left and move right
- 9:28:01for move down we're going to change
- 9:28:04y to 10 for move left
- 9:28:08we'll keep y the same at zero but we're
- 9:28:11going to subtract
- 9:28:1210 and then move right
- 9:28:16we are going to add 10 to our x's
- 9:28:20and that should be it so we can move up
- 9:28:24down right left
- 9:28:28and if you want to bind these to some
- 9:28:30arrow keys
- 9:28:31all we have to do is just change the
- 9:28:34event
- 9:28:35so we can bind these to our wsad keys
- 9:28:38as well as up
- 9:28:42down left
- 9:28:46and right and this time i'm going to use
- 9:28:49my arrow keys
- 9:28:50up down right and left
- 9:28:56all right so that's a few of the basics
- 9:28:58of moving either widgets or
- 9:29:00images on a canvas or window if you
- 9:29:02would like a copy of all this code for
- 9:29:04both examples i'll post both of them in
- 9:29:06the comments down below
- 9:29:08but yeah that's the basics of moving
- 9:29:10widgets and images
- 9:29:11in python hey what's going on everybody
- 9:29:14it's your bro here hope you're doing
- 9:29:16well and in this video i'm going to
- 9:29:17teach you guys how we can create some
- 9:29:19simple 2d animations in python so sit
- 9:29:22back relax and enjoy the show
- 9:29:26alright my fellow bros in this video
- 9:29:28we're going to be animating an image on
- 9:29:30a canvas we're only going to be moving
- 9:29:33this image
- 9:29:33up down left and right feel free to
- 9:29:36download an image that you want to use
- 9:29:37i'm just going to use this
- 9:29:39ufo emoji that i found so we'll need two
- 9:29:42imports
- 9:29:43at the top from tk enter import
- 9:29:45everything that's what we usually have
- 9:29:47we'll also need to import time as well
- 9:29:50we'll need a window window equals tk
- 9:29:53followed by window.main loop
- 9:29:55we'll need a canvas i'm going to call
- 9:29:57the canvas canvas
- 9:29:58canvas equals canvas we're adding the
- 9:30:01canvas
- 9:30:01to the window i'm going to create
- 9:30:03something that's called a
- 9:30:05constant we're going to create a
- 9:30:07constant for the width and the height
- 9:30:09of our canvas so a constant is a
- 9:30:12variable or value that you don't plan on
- 9:30:14changing later at all
- 9:30:16and a common naming convention for a
- 9:30:18constant is that the name of the
- 9:30:20constant is
- 9:30:20all uppercase so i want the width and
- 9:30:23the height
- 9:30:24of this canvas to be constants so for
- 9:30:27the width this is going to be
- 9:30:28all uppercase it's not necessary to make
- 9:30:31it all uppercase
- 9:30:32but that's a common naming convention
- 9:30:34for the width i'm going to set this to
- 9:30:35500
- 9:30:36and the height set to 500 as well
- 9:30:39and for the width of this canvas i'm
- 9:30:41going to set the width
- 9:30:43to equal my constant which is 500
- 9:30:46and the height to my constant height
- 9:30:50so the reason that i'm making constants
- 9:30:52is that we'll be reusing these a lot
- 9:30:54later in this program so we might as
- 9:30:56well just declare them now and get it
- 9:30:58over with
- 9:30:59so for the canvas we're adding this to
- 9:31:01the window we're setting the width to
- 9:31:03our constant of width and the height to
- 9:31:05our constant of height
- 9:31:07and we need to follow this by packing
- 9:31:08this or place
- 9:31:10canvas dot pack pack to make it simple
- 9:31:13so we should have a canvas on our window
- 9:31:17now if you have an image that you want
- 9:31:18to use we're going to animate it
- 9:31:20i need to create a photo image from this
- 9:31:22file it's a png file it's called
- 9:31:25ufo but yours is probably going to be
- 9:31:27named something different so
- 9:31:28don't copy me exactly i need to create a
- 9:31:31photo image from this
- 9:31:32i'm going to call this photo underscore
- 9:31:35image but name it whatever you want this
- 9:31:38equals
- 9:31:39photo image and i need to list the file
- 9:31:42path or the file name
- 9:31:43since this file is within my project
- 9:31:46folder i only have to list the file name
- 9:31:48and for me this is called ufo.png
- 9:31:53our next step is that we're going to
- 9:31:54take our photo image and add this photo
- 9:31:57image to the canvas
- 9:31:58and we'll also give this image a unique
- 9:32:00name i'm going to call this my
- 9:32:03image equals we're going to use the
- 9:32:06canvases
- 9:32:07create image function so canvas dot
- 9:32:11create image there's a few arguments
- 9:32:13that we need to pass
- 9:32:14in where we want this image to appear we
- 9:32:17need a set of coordinates
- 9:32:18if we want this to appear in the top
- 9:32:20left corner that is where x
- 9:32:22is zero and y is zero for the image
- 9:32:25we're going to set this equal to
- 9:32:27our photo image whatever you named this
- 9:32:30and lastly we should anchor this i'm
- 9:32:33going to anchor this in the northwest
- 9:32:35corner to keep it simple
- 9:32:37and we should have our image on our
- 9:32:39canvas in the top left corner
- 9:32:41and here is my ufo that's being piloted
- 9:32:44by a green alien that's waving to me
- 9:32:46aliens are usually not that friendly
- 9:32:48unless they're abducting you our next
- 9:32:49step is to create a
- 9:32:50while loop so we'll say wow true this
- 9:32:53will continue forever until we close out
- 9:32:55of the program
- 9:32:56if you're looking at the code for a game
- 9:32:57this might say instead
- 9:32:59while running running will be a boolean
- 9:33:03variable that contains either true or
- 9:33:05false so if this is true while the game
- 9:33:07is running
- 9:33:08they're going to continue moving all of
- 9:33:10the characters on the screen
- 9:33:11and then if somebody pauses the game or
- 9:33:13closes out of the game
- 9:33:14then they'll flip this running variable
- 9:33:16to false but to keep things simple we'll
- 9:33:19just say wow
- 9:33:20true but that's something that you might
- 9:33:21see what we'll want to do is get the
- 9:33:24coordinates of our image
- 9:33:25within the canvas and this will return a
- 9:33:28list of coordinates
- 9:33:29so let's create a list called
- 9:33:31coordinates so
- 9:33:33coordinates equals canvas
- 9:33:37and we can use the chords function to
- 9:33:40get the coordinates
- 9:33:41i guess that's short for coordinates and
- 9:33:44we're getting the coordinates of
- 9:33:45my image so we'll pass this into this
- 9:33:48function
- 9:33:49and then let's print this just to see
- 9:33:51what this returns
- 9:33:52we'll print coordinates
- 9:33:57okay now to update the window after each
- 9:33:59cycle
- 9:34:00of this while loop we can use the update
- 9:34:02function of our window
- 9:34:04so window.update will also have this
- 9:34:07thread sleep for let's say 0.01 seconds
- 9:34:10so time dot sleep 0.01 one
- 9:34:15so here's a quick rundown of what we
- 9:34:17have going on so far within this while
- 9:34:19loop
- 9:34:19we're going to first get the coordinates
- 9:34:21of where our image
- 9:34:23is located we'll print the coordinates
- 9:34:26to the console window
- 9:34:27this part isn't required but it's going
- 9:34:29to help us for learning purposes
- 9:34:32we'll update this window for any changes
- 9:34:34and then our thread
- 9:34:36which is in charge of running the
- 9:34:37program is going to sleep
- 9:34:39for i believe that's a hundredth of a
- 9:34:41second
- 9:34:43so when we run this our image is staying
- 9:34:46in place
- 9:34:47and we know that our while loop is
- 9:34:48running because it's continuously
- 9:34:50printing the coordinates
- 9:34:51the first number is the x position
- 9:34:55the second number is the y position and
- 9:34:58now we're going to give our image a
- 9:34:59speed
- 9:35:00or velocity i like to call it velocity
- 9:35:03and in fact i'm going to give this image
- 9:35:05two velocities
- 9:35:06one for how fast this image is moving on
- 9:35:08the x-axis
- 9:35:09and another for how fast it's moving on
- 9:35:11the y axis
- 9:35:13so i'm going to create two variables one
- 9:35:15called x velocity
- 9:35:17and i'm going to set this equal to one
- 9:35:19and y velocity
- 9:35:22and i'll set this to one as well okay
- 9:35:26so what we need to do now is update
- 9:35:29the position of our image so let's do
- 9:35:32that before we update
- 9:35:33our window so i'm going to type in the
- 9:35:35name of the canvas
- 9:35:36and use the move function this move
- 9:35:39function takes three arguments
- 9:35:41what you want to move i want to move my
- 9:35:44image my ufo
- 9:35:46and how far i want to move this image on
- 9:35:49each axis each coordinate
- 9:35:51so what i want to do is update the
- 9:35:54x velocity the x position of my image
- 9:35:58so i'm going to pass in my x velocity
- 9:36:02variable and then y we'll just keep the
- 9:36:04same for now so we'll just say
- 9:36:06zero so after each iteration of this
- 9:36:08while loop
- 9:36:09my ufo is going to move one pixel to the
- 9:36:11right
- 9:36:12and it's just going to go off into the
- 9:36:13void forever and now
- 9:36:15if we replaced x velocity with zero
- 9:36:19and for y we set this to our y velocity
- 9:36:22this image is now going to travel down
- 9:36:26and it will continue into the void
- 9:36:27forever
- 9:36:29and then if we combine both of these
- 9:36:30together
- 9:36:33we're going to move diagonally down and
- 9:36:36to the right
- 9:36:37so what if we want this image to bounce
- 9:36:40off the walls we're going to add some
- 9:36:41if statements we're going to begin by
- 9:36:43just having this image bounce off the
- 9:36:45left and right walls
- 9:36:47for now let's set y velocity to zero so
- 9:36:50that this image is only moving
- 9:36:51left and right so before we move this
- 9:36:55image we'll add an if statement we're
- 9:36:56going to check to see
- 9:36:58if the x position of our image
- 9:37:01is either greater than the width of the
- 9:37:03canvas
- 9:37:04or if it's less than zero so we take
- 9:37:07our coordinates and we're going to get
- 9:37:09the first element from this list
- 9:37:11so that is zero that is x the x value
- 9:37:15if coordinates at element zero is
- 9:37:18greater than or equal to
- 9:37:20the width of this window and this is a
- 9:37:22constant
- 9:37:24so we can just place that here if
- 9:37:26coordinates
- 9:37:27well x within our coordinates is greater
- 9:37:29than or equal to width
- 9:37:32or coordinates at index
- 9:37:360 is less than 0 then we want to reverse
- 9:37:40the direction or flip the velocity
- 9:37:43so we want to change our x velocity and
- 9:37:45make this a negative number
- 9:37:47one easy way to do that is that we can
- 9:37:49take x velocity
- 9:37:51equals x velocity times negative one
- 9:37:55or another way of writing this is just
- 9:37:57to say x velocity
- 9:37:59minus negative velocity now what ends up
- 9:38:02happening
- 9:38:03is that when this image reaches the
- 9:38:05right border it's going to bounce back
- 9:38:08however it only bounces back after it
- 9:38:10reaches
- 9:38:11the top left corner of the image we need
- 9:38:13to factor in
- 9:38:14the width of the image as well so we're
- 9:38:17going to create
- 9:38:18two new variables and let's do this
- 9:38:19right before the while loop
- 9:38:21we'll want to determine what the height
- 9:38:23and the width are for our photo image
- 9:38:25that we have
- 9:38:26so this is what we're going to write
- 9:38:28let's call these two values
- 9:38:29image underscore width and
- 9:38:33image underscore height
- 9:38:37so we need to determine what the width
- 9:38:39and the height is
- 9:38:40for our photo image and there is a
- 9:38:42function to do that
- 9:38:44type in the name of the photo image dot
- 9:38:47and there is a width
- 9:38:48function and the same thing for height
- 9:38:53photo image dot height
- 9:38:56and then we can reuse these so i'm just
- 9:38:59going to add
- 9:39:00one line to the sift statement if
- 9:39:03the x position of our image is greater
- 9:39:06uh greater than
- 9:39:07or equal to the width of our window
- 9:39:10minus the width of our image and i'm
- 9:39:13just going to put these within
- 9:39:15parentheses just to keep these more
- 9:39:16organized
- 9:39:17and what ends up happening now is that
- 9:39:20when
- 9:39:22the right portion of our image hits the
- 9:39:25right wall it's going to bounce off
- 9:39:27and let's do the same thing for our y
- 9:39:29velocity
- 9:39:32we're going to replace coordinates at
- 9:39:35index 0 with 1 and change that here as
- 9:39:38well
- 9:39:39replace with with height
- 9:39:43and this is image underscore height
- 9:39:46and we're going to change the y velocity
- 9:39:49this time
- 9:39:54and when we move this image let's also
- 9:39:56add the
- 9:39:57y velocity
- 9:40:00and now our image is just going to
- 9:40:03bounce off the walls forever
- 9:40:04but it's kind of predictable since we
- 9:40:06set x and y
- 9:40:08to equal one now let's change the speeds
- 9:40:10to kind of spice things up
- 9:40:12let's say for the x velocity this is
- 9:40:14three and y velocity this is now two
- 9:40:16this is going to go in a completely
- 9:40:18different direction
- 9:40:21so you can mess with the speeds however
- 9:40:22you want so
- 9:40:24guys how about we add a background image
- 9:40:26to this well because we can
- 9:40:28i'm going to reuse this image of space
- 9:40:30from my java video on this same topic
- 9:40:33i need to create a photo image from this
- 9:40:35and i'm going to copy what we did for
- 9:40:37our normal image that we were moving and
- 9:40:40animating
- 9:40:41so this is called space.png well that's
- 9:40:44the name of my file
- 9:40:46and i'm going to give this image a
- 9:40:47different name maybe i'll call this
- 9:40:50background underscore photo
- 9:40:53and then i'm going to add this photo to
- 9:40:55the canvas
- 9:40:57and i'm going to call this a different
- 9:40:59name
- 9:41:01i'll just call this background all right
- 9:41:04so
- 9:41:04our window should have a background now
- 9:41:08and then our image moves on top of the
- 9:41:10background
- 9:41:10so make sure you add and create the
- 9:41:12background before you add any other
- 9:41:14images
- 9:41:15otherwise the background might overlap
- 9:41:17your image
- 9:41:18alright so that's the basics of some
- 9:41:21simple animations in
- 9:41:22python if you want to copy all this code
- 9:41:24i'll post all of this in the comments
- 9:41:26down below
- 9:41:26but yeah that's the basics of some
- 9:41:28animations in python
- 9:41:31hey what's going on everybody it's your
- 9:41:33bro here hope you're doing well and in
- 9:41:35this video i'm going to teach you guys
- 9:41:36how we can animate multiple objects in
- 9:41:38python so
- 9:41:39sit back relax and enjoy the show
- 9:41:43welcome back my fellow bros in this
- 9:41:45video we're going to be animating
- 9:41:47multiple objects within a window
- 9:41:49more specifically we'll animate some
- 9:41:50circles and each of these circles will
- 9:41:52have their own characteristics
- 9:41:54speeds and directions so we'll need two
- 9:41:57imports to begin
- 9:41:58from ck enter import everything we'll
- 9:42:01also be importing everything from the
- 9:42:02time module as well
- 9:42:04we'll need a window window equals tk
- 9:42:06followed by window.main loop
- 9:42:08i'm going to create two constants one
- 9:42:10for the width and one for the height
- 9:42:12let's set the width equal to 500 and the
- 9:42:14height equal to
- 9:42:15500 as well we'll create a canvas and
- 9:42:17we'll be animating
- 9:42:19the circles that we're going to create
- 9:42:20on the canvas so let's create a canvas
- 9:42:23canvas equals canvas
- 9:42:27we're going to be adding the canvas to
- 9:42:28the window and we'll set the width of
- 9:42:30our canvas equal to our constant width
- 9:42:33which is 500 and our height equal to our
- 9:42:36constant of height
- 9:42:37which is also 500 and we will add this
- 9:42:40canvas by using the pack function
- 9:42:42canvas.pack here's our canvas and we're
- 9:42:45going to be creating multiple circles
- 9:42:47let's pretend that they're all different
- 9:42:48sports balls like a tennis ball baseball
- 9:42:51volleyball bowling ball
- 9:42:53they'll all be bouncing off of the
- 9:42:55borders and once
- 9:42:56each of them encounters a border they
- 9:42:57will change direction
- 9:42:59since we're going to be creating
- 9:43:01multiple objects i think it's best if we
- 9:43:03do this with another class
- 9:43:05so let's create another class i'm going
- 9:43:07to go to file
- 9:43:08new python file and let's call this
- 9:43:12class
- 9:43:12ball and click python file and we need
- 9:43:16to define
- 9:43:17this class class ball
- 9:43:22and we also need a constructor for this
- 9:43:24ball class
- 9:43:25we can create one with def two
- 9:43:28underscores
- 9:43:28i-n-i-t two underscores again
- 9:43:32and we can set up the parameters of what
- 9:43:34we want to receive if we were to create
- 9:43:36a ball
- 9:43:37object so let's pretend that
- 9:43:40we're going to be creating a volleyball
- 9:43:42to begin with so
- 9:43:44let's call this ball volley
- 9:43:47underscore ball equals
- 9:43:50ball and we'll pass in some arguments so
- 9:43:53let's set up the parameters first
- 9:43:55what do we want to receive one we want
- 9:43:57to receive
- 9:43:58our canvas so that we can actually draw
- 9:44:00this oval we can draw this ball
- 9:44:03we should also have an x and y position
- 9:44:07a diameter
- 9:44:11x velocity y velocity
- 9:44:15and a color all right so
- 9:44:18when we create a ball like a volleyball
- 9:44:21we're going to pass
- 9:44:22in whatever the parameters require so we
- 9:44:25need a canvas
- 9:44:26x y a diameter an x velocity
- 9:44:30y velocity and a color so within the
- 9:44:32constructor for the ball
- 9:44:34we're passing in our canvas it's kind of
- 9:44:37like what we do with widgets
- 9:44:38we're specifying what we're going to be
- 9:44:40adding this widget to
- 9:44:42we're going to be adding this ball to
- 9:44:44our canvas we also need
- 9:44:46x y and a diameter let's make this
- 9:44:48simple by setting x and y to both zero
- 9:44:51for the diameter let's set this to maybe
- 9:44:54100 and we also need an x velocity and y
- 9:44:58velocity
- 9:44:59let's set this both to one and a color
- 9:45:02so let's pass in a string
- 9:45:04like white let's head back to our ball
- 9:45:07class and finish assigning
- 9:45:08all of these arguments that we're going
- 9:45:10to receive the first step is that we'll
- 9:45:12say
- 9:45:13self dot canvas equals
- 9:45:16the canvas that we receive when it's
- 9:45:18passed to us as an argument
- 9:45:21our next step is to draw this oval draw
- 9:45:24this ball
- 9:45:25and we'll say self dot image
- 9:45:29equals canvas dot create
- 9:45:33underscore oval and with creating ovals
- 9:45:36we can pass in x y
- 9:45:39and with circles the diameter is equal
- 9:45:42for both the width and the height
- 9:45:44so we'll pass in our diameter twice
- 9:45:48and lastly a fill color
- 9:45:52and we'll say this is equal to the color
- 9:45:54that we receive
- 9:45:55which is white
- 9:45:58we're going to set the x velocity and
- 9:46:00the y velocity
- 9:46:02this is related to the direction that
- 9:46:04it's initially going to head in
- 9:46:06so self dot x velocity
- 9:46:10equals the x velocity that we receive
- 9:46:15and we'll do the same thing for y
- 9:46:16velocity
- 9:46:19self.y velocity equals y velocity
- 9:46:23now since this is in another class we
- 9:46:25need to import this class
- 9:46:29from ball import
- 9:46:32everything and you can see that this red
- 9:46:34underline went away
- 9:46:36so let's see what happens now
- 9:46:39we have our volleyball but it's not
- 9:46:42currently moving
- 9:46:42so that's the next step so let's create
- 9:46:45a function
- 9:46:46within the ball class called move that
- 9:46:48we can call upon
- 9:46:49so we'll do this after the constructor
- 9:46:51cef
- 9:46:53move and this will take self as an
- 9:46:56argument
- 9:46:58the first thing that we should do is get
- 9:47:00the coordinates
- 9:47:02so we'll say coordinates and this is a
- 9:47:04list coordinates equals
- 9:47:06self dot canvas dot
- 9:47:09c o o rds
- 9:47:14and we'll pass in self dot image
- 9:47:18and we will print these coordinates just
- 9:47:20to see what's here
- 9:47:26all right now we'll create a while loop
- 9:47:29within our main class
- 9:47:30so let's add that near the end and we'll
- 9:47:33just say
- 9:47:35while true so while this is running
- 9:47:39let's move our volleyball so volleyball
- 9:47:43dot move so this will call the move
- 9:47:45function of our volleyball
- 9:47:48and then we will update the window
- 9:47:51window dot update so that this refreshes
- 9:47:54and we will sleep for maybe a hundredth
- 9:47:57of a second
- 9:47:58time dot sleep 0.01 but you can put
- 9:48:02whatever you want here
- 9:48:04so this is going to at first just print
- 9:48:06the coordinates of this ball
- 9:48:09so it's not currently moving and this
- 9:48:11time with our list we have
- 9:48:13four coordinates zero zero that's the
- 9:48:15top left corner and 100 100 that's the
- 9:48:18bottom
- 9:48:18right corner so this time we have four
- 9:48:21coordinates that we have to deal with
- 9:48:23the top left corner as well as the
- 9:48:25bottom right corner
- 9:48:26our next step is that we should move
- 9:48:28this image
- 9:48:30self dot canvas dot move
- 9:48:34what object are we going to move we're
- 9:48:36going to move self
- 9:48:38dot image and in what directions
- 9:48:41self.x velocity as well as
- 9:48:44self dot y velocity now
- 9:48:48our ball is going to move across the
- 9:48:50window and it right now it's displaying
- 9:48:52all of the coordinates
- 9:48:53we don't have any bounds set up so it's
- 9:48:55just going to continue into the void
- 9:48:56forever so we should set up some if
- 9:48:59statements to check to see if one of the
- 9:49:01borders
- 9:49:02touches one of the walls of the window
- 9:49:04if so it's going to change
- 9:49:05direction so let's check to see
- 9:49:09if the left or right side of our ball
- 9:49:11touches one of the left or right borders
- 9:49:13so let's write an if statement if
- 9:49:17and then let's take a look at these
- 9:49:18coordinates if
- 9:49:20coordinates at index 2
- 9:49:24that is this one 0 1
- 9:49:272. so this is the bottom right corner
- 9:49:31with these two sets of coordinates if
- 9:49:33coordinates at index 2
- 9:49:35is greater than or equal to
- 9:49:38i'm going to put these within
- 9:49:39parentheses self
- 9:49:41dot canvas and we need to get the width
- 9:49:44of the canvas
- 9:49:45one way which we can do that is to use
- 9:49:47the w
- 9:49:49info underscore with function
- 9:49:53so this is going to get the width of the
- 9:49:55canvas
- 9:49:57and then let's add one more thing
- 9:50:00or coordinates at index
- 9:50:03zero is less than zero
- 9:50:07so if this ball touches the left or
- 9:50:10right border
- 9:50:11we want to change direction so we can
- 9:50:14flip
- 9:50:14the x velocity self.x velocity
- 9:50:18equals negative self x
- 9:50:22velocity and let's do the same thing for
- 9:50:24the top and bottom borders
- 9:50:26so let's copy what we have
- 9:50:31and we'll replace coordinates at index
- 9:50:33two with
- 9:50:34three and we'll also replace
- 9:50:38w info underscore width with height
- 9:50:43and coordinates at index one
- 9:50:46change self dot x velocity with y
- 9:50:49velocity
- 9:50:50same thing here as well alright and
- 9:50:54that should be everything let's try it
- 9:50:58so let's see if this ball actually
- 9:50:59bounces off the borders
- 9:51:02which it does pretty cool so that's
- 9:51:04everything we need to do within our ball
- 9:51:06class
- 9:51:06now if we need to create multiple balls
- 9:51:09we can just construct them
- 9:51:10so let's say this time we would like a
- 9:51:12tennis ball
- 9:51:14i'm going to name this volleyball as
- 9:51:16tennis ball and
- 9:51:18let's make the diameter smaller like
- 9:51:20five but we'll make this a lot faster
- 9:51:22like
- 9:51:23for the velocity let's say four and
- 9:51:25three and the color
- 9:51:26let's make this yellow and we also need
- 9:51:30to call the
- 9:51:31move function of this tennis ball so
- 9:51:33within the while loop
- 9:51:34tennis ball dot move and then we should
- 9:51:38also have a tennis ball moving across
- 9:51:40the screen
- 9:51:40in a different direction now technically
- 9:51:43we don't need to print the coordinates
- 9:51:44but i tend to do that just for
- 9:51:46teaching purposes to better demonstrate
- 9:51:48all of this
- 9:51:49now our ball class is acting as a
- 9:51:52blueprint
- 9:51:53for how balls should be created and the
- 9:51:55behavior that they exhibit
- 9:51:57and if we need to create a ball we can
- 9:51:59just come up with a name for the ball
- 9:52:01and call the ball constructor to take
- 9:52:03care of that for us
- 9:52:05kind of like what we've been doing with
- 9:52:06widgets so all we need to do to create
- 9:52:09another ball
- 9:52:10is come up with a unique name for the
- 9:52:11ball and instantiate it using
- 9:52:13our ball class that we created so this
- 9:52:15time let's create
- 9:52:17a basketball i'll rename this as
- 9:52:19basketball
- 9:52:20and we'll pass in different arguments
- 9:52:22based on how we want to customize
- 9:52:25this unique ball so for the diameter
- 9:52:28let's say this is now 125
- 9:52:30and the x and y velocity let's set this
- 9:52:32to something very fast like
- 9:52:35eight and seven and with the color let's
- 9:52:38change this to orange
- 9:52:40and make sure you also call the move
- 9:52:42function
- 9:52:44so basketball dot move and we should
- 9:52:47have a basketball moving across the
- 9:52:49screen
- 9:52:50all right everybody so that's the basics
- 9:52:53of animating multiple objects in python
- 9:52:55if you want
- 9:52:56a copy of all this code i'll post all of
- 9:52:58this in the comments down below
- 9:52:59but yeah that's how to animate multiple
- 9:53:01objects in python
- 9:53:04hey what's going on everybody it's ibro
- 9:53:06here hope you're doing well and in this
- 9:53:08video i'm going to teach you guys how we
- 9:53:09can create a clock program in python so
- 9:53:12sit back relax and enjoy the show
- 9:53:16all right my fellow bros in this video
- 9:53:18we're going to be creating a clock
- 9:53:20gui program so we'll need a few imports
- 9:53:22from
- 9:53:23tk enter import everything
- 9:53:27and from time import everything as well
- 9:53:31we'll need a window window equals tk
- 9:53:33followed by window.main loop
- 9:53:35and in this program we're going to be
- 9:53:37creating three separate labels
- 9:53:39one for the time another for the day of
- 9:53:41the week and another for the date
- 9:53:43so let's begin with just displaying the
- 9:53:45time and work on the day of the week and
- 9:53:47the date later
- 9:53:49so let's create a label for the time
- 9:53:51called time
- 9:53:52label equals label we're adding this to
- 9:53:55the window
- 9:53:56and let's set the font because we can
- 9:53:59i'm going to choose
- 9:54:00maybe ariel and then a size of 50 so
- 9:54:03that this is somewhat large
- 9:54:05i'm going to change the foreground color
- 9:54:07that is the font color
- 9:54:09and i will pick a hex value of green so
- 9:54:12that is
- 9:54:120 0 f 0 0 but feel free to pick whatever
- 9:54:16color you want
- 9:54:17and i'm also going to set the background
- 9:54:19color and you can pick a hex value or a
- 9:54:21color name i'm just going to say black
- 9:54:24and then i'm going to pack this label
- 9:54:27time label dot pack
- 9:54:32now we're going to call a function and
- 9:54:35we'll call this update
- 9:54:37and we'll define what this does after
- 9:54:39every second has passed
- 9:54:40we will use a recursive function to keep
- 9:54:43on updating the
- 9:54:44time every one second or so so let's
- 9:54:47define what this function does
- 9:54:49def update
- 9:54:52there is a function within the time
- 9:54:54module called strf time
- 9:54:57which we can use to return the current
- 9:54:59time and format it however we want
- 9:55:01so we're going to actually place this
- 9:55:03within a string variable and we'll call
- 9:55:05this time
- 9:55:06string and this function is strf
- 9:55:10time i'm actually going to go to
- 9:55:12python's website just to give you an
- 9:55:14example of how this works
- 9:55:15here we are on python's website and this
- 9:55:18is the
- 9:55:18strf time function and this converts a
- 9:55:22tuple representing a time as returned by
- 9:55:26gm time or local time to a string
- 9:55:29as specified by the format arguments
- 9:55:32so when we call this function we can
- 9:55:35pass in
- 9:55:36directives as arguments based on what we
- 9:55:39want to display
- 9:55:40if we want to display the current time
- 9:55:42there is a few directives that we can
- 9:55:44pass in
- 9:55:45so let's say that we want a 12 hour
- 9:55:48clock as represented by a decimal number
- 9:55:50between
- 9:55:511 and 12 well we would pass in percent
- 9:55:54i as an argument if we want the minutes
- 9:55:57that would be
- 9:55:58right here percent m and if we want the
- 9:56:01seconds that is
- 9:56:02percent s so we're going to pass in
- 9:56:05these directives
- 9:56:06as if they are arguments so within
- 9:56:09this function we'll pass in those format
- 9:56:13specifiers or directives so i want
- 9:56:16percent i followed by
- 9:56:20percent m and percent
- 9:56:23s alright uh but these also have to be
- 9:56:27within quotes so don't forget that
- 9:56:28because i just did
- 9:56:32okay this will display the current time
- 9:56:35oh then we also have to update our time
- 9:56:38label so
- 9:56:39time label dot config
- 9:56:44text equals our time string because
- 9:56:47remember that this
- 9:56:48returns a string of text
- 9:56:52text equals time string so this will
- 9:56:54display the current time
- 9:56:56and right now it is six in the morning
- 9:56:59or so
- 9:56:59however we just get one long jumbled
- 9:57:02number
- 9:57:03so it would be a good idea if we
- 9:57:05separated some of these numbers
- 9:57:07so within our strf time function i'm
- 9:57:10just going to add some colons to
- 9:57:11separate these values
- 9:57:15and this will display the current time
- 9:57:17it is currently 6
- 9:57:1846 in the morning let's add am or pm to
- 9:57:22the end of this
- 9:57:23so the directive for that is percent
- 9:57:26lowercase p
- 9:57:296 46 am now how do we update this label
- 9:57:33every one second to reflect the current
- 9:57:35time
- 9:57:35well that's a good question and i'm glad
- 9:57:37you asked it so we're going to use
- 9:57:39the time labels after function
- 9:57:42we pass in a delay and then a function
- 9:57:45that we want to call
- 9:57:46after that delay so let's say we want to
- 9:57:49call this function update every 1000
- 9:57:52milliseconds
- 9:57:53or one second so we'll pass in the delay
- 9:57:56as an argument
- 9:57:571000 milliseconds and the function that
- 9:57:59we want to call
- 9:58:00and we're going to perform what is known
- 9:58:02as a recursive
- 9:58:04function we're going to call a function
- 9:58:07within itself so after 1000 seconds
- 9:58:10we're going to call
- 9:58:11update again and it's going to repeat
- 9:58:12the process it's going to return the
- 9:58:14time
- 9:58:15and format that time and update
- 9:58:18our label with the new time so what ends
- 9:58:21up happening now
- 9:58:22is that our clock is going to update
- 9:58:25every 1000 milliseconds
- 9:58:28now let's add a few other labels let's
- 9:58:30say the day of the week
- 9:58:32i'm going to copy what we have here and
- 9:58:34let's rename this as
- 9:58:36day label
- 9:58:40but i probably don't want this to be the
- 9:58:42same size for the font let's say
- 9:58:44ink free because i like that font and a
- 9:58:48size of maybe 25 and i'll get rid of the
- 9:58:50coloring
- 9:58:53it's the same process as before really
- 9:58:56so let's replace
- 9:59:00time string with day string
- 9:59:05the directive for strf time to display
- 9:59:09the day of the week
- 9:59:11is percent a
- 9:59:14and we're going to replace time label
- 9:59:17with
- 9:59:18day label text equals day string
- 9:59:23and this will display the day of the
- 9:59:25week and it is currently
- 9:59:27wednesday and if we want to display
- 9:59:31the current date we can create a label
- 9:59:34and we'll call this
- 9:59:36date label
- 9:59:40and i'll make the font slightly bigger
- 9:59:41maybe 30.
- 9:59:44we'll copy what we have for our day
- 9:59:46string
- 9:59:48and rename this as date string
- 9:59:54so we have a few options for the date i
- 9:59:56want to display
- 9:59:58the name of the month the day and then
- 10:00:00the year
- 10:00:01so that would be percent capital b
- 10:00:06percent lowercase d i'm going to add a
- 10:00:09comma to separate the day and the year
- 10:00:12and percent capital y
- 10:00:15and then i want to set the text of my
- 10:00:17date label
- 10:00:18to my date string so that this will now
- 10:00:22display the current date there's just
- 10:00:24one quick change that i think we should
- 10:00:26make
- 10:00:26right now we have three different labels
- 10:00:28a time label a day label
- 10:00:30and a date label and right now we're
- 10:00:33using the after function of the time
- 10:00:35label
- 10:00:36so since we have all of these different
- 10:00:37labels i think we should replace time
- 10:00:39label
- 10:00:40with our window because our window has
- 10:00:42an after function as well
- 10:00:44so that after 1000 milliseconds has
- 10:00:47passed
- 10:00:47we can update the time the day of the
- 10:00:50week
- 10:00:50and the date all right so that's the
- 10:00:53basics of creating a simple clock
- 10:00:55program in python if you want a copy of
- 10:00:57all this code
- 10:00:58i'll post all of this in the comments
- 10:00:59down below but yeah that's the basics of
- 10:01:01creating a clock in python
- 10:01:04hey what's going on everybody it's you
- 10:01:06bro hope you're doing well
- 10:01:07and in this video i'm going to show you
- 10:01:08all how we can send an email using
- 10:01:10python so
- 10:01:11sit back relax and enjoy the show
- 10:01:16oh a quick note for you all you're going
- 10:01:17to need a gmail account to sign into
- 10:01:20as well as your username and password so
- 10:01:22once you have those set you're ready to
- 10:01:24begin
- 10:01:24all right let's begin by importing the
- 10:01:27simple mail
- 10:01:28transfer protocol library we'll need a
- 10:01:31couple variables to hold strings
- 10:01:32let's say we have a sender a receiver
- 10:01:37a password
- 10:01:40a subject for your email as well as
- 10:01:44a body for the email okay sender is
- 10:01:46going to be you
- 10:01:47sending the email so enter your email in
- 10:01:49here i'm just going to make up one
- 10:01:51you at gmail.com
- 10:01:54actually sender would be better i would
- 10:01:56say let me change that sender at
- 10:01:58gmail.com
- 10:01:59let's say we have a receiver this is who
- 10:02:01you're going to send the email to
- 10:02:04receiver gmail.com
- 10:02:07don't actually enter in these emails use
- 10:02:09your owner the person that you intend to
- 10:02:10send the email to for your test
- 10:02:12because these will probably not work i'm
- 10:02:14assuming
- 10:02:15then enter the password for your gmail
- 10:02:17account so let's say
- 10:02:20password123 and a subject
- 10:02:23uh let's say python email
- 10:02:27test and a body what do you want to say
- 10:02:30within your email
- 10:02:32i wrote an email
- 10:02:36okay so that should be good so remember
- 10:02:38that you're going to want to change
- 10:02:40these to your actual
- 10:02:41gmail account and you'll want to send it
- 10:02:43to a valid email address too
- 10:02:45and make sure that you use your password
- 10:02:47for your gmail account
- 10:02:49okay now we're going to create a header
- 10:02:51for our email and the syntax on this is
- 10:02:53a little strange we're going to say
- 10:02:55message
- 10:02:56f and then triple quotes so what this
- 10:02:59triple quotes is
- 10:03:00is that a triple quote string can span
- 10:03:03multiple lines of text so the first
- 10:03:06thing we'll enter is
- 10:03:07from within our header from and then
- 10:03:10since we're using an f-string we can
- 10:03:12insert
- 10:03:13a variable at a given location when we
- 10:03:16insert
- 10:03:16a pair of these curly braces so from
- 10:03:20will be sender the next line
- 10:03:23to colon
- 10:03:27receiver then the next line
- 10:03:30subject colon
- 10:03:34subject this will be the title of the
- 10:03:36email then i'm going to add a new line
- 10:03:39and then insert the body
- 10:03:43and if you want you can give yourself a
- 10:03:45name when you send this email
- 10:03:47let's say maybe snoop dogg and if
- 10:03:50there's somebody you want to send the
- 10:03:51email to
- 10:03:52and give them a name you can say like
- 10:03:55nicholas
- 10:03:56cage but these aren't necessary
- 10:03:59okay so we have our header completed let
- 10:04:02me just add a note that this is our
- 10:04:03header
- 10:04:05okay we're going to create a server
- 10:04:07object
- 10:04:08server equals smtplib.smtp
- 10:04:17there are two arguments the first is
- 10:04:20smtp
- 10:04:25the other argument is the port number
- 10:04:28and that is going to be
- 10:04:29587 that is the default mail submission
- 10:04:32port
- 10:04:33587 now we're going to take our server
- 10:04:36object
- 10:04:37and start tls transport layer security
- 10:04:42okay now we need to log in so server
- 10:04:46dot login we will pass in
- 10:04:50our sender email that's us as well as
- 10:04:53our password
- 10:04:56password and let's print
- 10:05:02logged in
- 10:05:06we'll eventually place all this within a
- 10:05:08try and accept block later
- 10:05:10all right and then to send an email type
- 10:05:13server dot send mail
- 10:05:17and we pass in our self sender
- 10:05:20receiver and our message
- 10:05:23sender comma receiver
- 10:05:27comma message and let's print a
- 10:05:31confirmation
- 10:05:32print email has
- 10:05:36been sent now if you were to run this
- 10:05:39program
- 10:05:40you may encounter this error smtp
- 10:05:43authentication error that either means
- 10:05:45your username
- 10:05:46password combination was not correct or
- 10:05:49you need to turn on
- 10:05:50the less secure app access on your gmail
- 10:05:53account
- 10:05:54which you can find here however i would
- 10:05:56recommend turning that back off when
- 10:05:58you're done with this program just
- 10:05:59because
- 10:06:00your account will be less secure and i'm
- 10:06:02not liable if you get hacked hey you
- 10:06:03know what
- 10:06:04let's place this code within a try block
- 10:06:06we will try
- 10:06:07all of this code and then if we
- 10:06:10encounter
- 10:06:10this exception we can catch and handle
- 10:06:13it properly
- 10:06:14so we will try these four lines of code
- 10:06:17and we will handle
- 10:06:18our exception of smt lib dot
- 10:06:21smtp authentication error
- 10:06:25that means we could not sign in so let's
- 10:06:27print
- 10:06:28unable to sign in
- 10:06:32so i'm going to run this one more time
- 10:06:34and instead of getting that ugly error
- 10:06:36we should get a simple message that says
- 10:06:38unable to sign in once you have the
- 10:06:40proper username password combination for
- 10:06:42your gmail account
- 10:06:43and if you need to set less secure app
- 10:06:46access to on
- 10:06:47but make sure that you turn it back off
- 10:06:49when you're done with this you should be
- 10:06:50able to send an email
- 10:06:51let's try it logged in email has been
- 10:06:54sent
- 10:06:55here is a snapshot of the email that i
- 10:06:57received
- 10:06:58i blurred out my actual email because i
- 10:07:00don't want it public because
- 10:07:01some of you guys are weird so we have
- 10:07:04our sender
- 10:07:05our receiver and i gave myself a
- 10:07:07nickname of snoop dogg that's why it
- 10:07:09says snoop
- 10:07:10to nicholas the receiver but you don't
- 10:07:12actually need a name for these as long
- 10:07:14as you have the sender and receiver
- 10:07:16fields filled in you should be good
- 10:07:18we have our subject python email test
- 10:07:21as well as our body i wrote an email
- 10:07:24well everybody that's how to write and
- 10:07:26send a simple email using python if you
- 10:07:28would like a copy of this code i will
- 10:07:30post this to the comment section down
- 10:07:32below
- 10:07:32but yeah that's how to send a simple
- 10:07:34email using python
- 10:07:37hey what's going on everybody it's you
- 10:07:39bro hope you're doing well
- 10:07:40and in this video i'm going to show you
- 10:07:41all how we can run a python file using
- 10:07:44command prompt so
- 10:07:45sit back relax and enjoy the show
- 10:07:50hey y'all in today's video i'm going to
- 10:07:52show you all how we can run a python
- 10:07:53file with command prompt
- 10:07:55so first we'll need a script to work
- 10:07:57with let's just make a very simple
- 10:07:59program
- 10:08:00let's say print hello world and then
- 10:08:03maybe we will ask
- 10:08:04for some user input name equals input
- 10:08:08what's your name and then at the end
- 10:08:12we will print the word hello
- 10:08:16plus the user's name okay so
- 10:08:20let's save this to a place that will
- 10:08:22remember perhaps
- 10:08:23my desktop now to save this file to a
- 10:08:25separate location go to
- 10:08:27file save as you can pick a name for
- 10:08:30this python file
- 10:08:32then to directory click these three dots
- 10:08:34and pick a location you want to save
- 10:08:36this to
- 10:08:37to make this easy i'm going to save this
- 10:08:39python file to my desktop
- 10:08:40and then click ok so the name of this
- 10:08:43python file of mine
- 10:08:44is hello underscore world dot py then
- 10:08:48click ok
- 10:08:49now our second step is to open command
- 10:08:51prompt to open command prompt just
- 10:08:53search for
- 10:08:54cmd if you're using mac i believe it's
- 10:08:57terminal
- 10:08:58step number three is that we need to
- 10:09:00navigate to the directory that contains
- 10:09:02our python file i saved this python file
- 10:09:05to my desktop
- 10:09:06so if you right click on your file and
- 10:09:08go to location you can just copy this
- 10:09:10address
- 10:09:11now within command prompt type cd to
- 10:09:13change directory
- 10:09:15space and then paste that directory
- 10:09:18that contains your python file hit enter
- 10:09:21and for the last step we need to invoke
- 10:09:23the python interpreter by typing
- 10:09:25python space and then
- 10:09:29the name of the script and the name of
- 10:09:31my script is hello
- 10:09:32underscore world dot p y hit enter to
- 10:09:36run the script
- 10:09:37hello world what is your name bro hello
- 10:09:40bro
- 10:09:41so that is how to run a python file with
- 10:09:43command prompt i will post all of these
- 10:09:45notes to the comments section down below
- 10:09:47and well
- 10:09:48yeah that's how to run a python file
- 10:09:50with command prompt
- 10:09:52it's you bro hope you're all doing well
- 10:09:54and in this video i'm going to show you
- 10:09:55all how we can use pip for python to
- 10:09:58download packages and modules so sit
- 10:10:00back
- 10:10:01relax and enjoy the show
- 10:10:05alright people pip for python pip is a
- 10:10:08package manager for packages and modules
- 10:10:10from the python package index if you're
- 10:10:13using
- 10:10:14python version 3.4 or above pip should
- 10:10:17already be installed
- 10:10:18if you're using a python version below
- 10:10:213.4
- 10:10:22one option is that you can always update
- 10:10:24your version of python just click
- 10:10:26download open when done
- 10:10:30go to customize installation make sure
- 10:10:32that pip
- 10:10:33is checked go to next and add
- 10:10:36python to environment variables then
- 10:10:38install
- 10:10:40now to use pip open up command prompt
- 10:10:42and type pip
- 10:10:43for a help menu all sorts of different
- 10:10:47general options and commands will appear
- 10:10:49if you need to check
- 10:10:50your version of pip type this command
- 10:10:53and here's a cheat sheet that i wrote
- 10:10:55so to check the current version of pip
- 10:10:57type pip
- 10:10:58dash dash version enter
- 10:11:02and currently the version that i'm using
- 10:11:05is 21
- 10:11:06if you need to upgrade pip use this
- 10:11:08command pip
- 10:11:10install dash dash up
- 10:11:14grade pip mine should be the latest
- 10:11:18version
- 10:11:18yeah requirement already satisfied and
- 10:11:21if you need a list of installed packages
- 10:11:24type
- 10:11:24pip list
- 10:11:28so here's all of the packages that i
- 10:11:31currently have installed
- 10:11:32some of these you might recognize like
- 10:11:34numpy
- 10:11:35pie game pie installer those are just a
- 10:11:38few to check to see if any of your
- 10:11:40packages
- 10:11:41are outdated type pip list
- 10:11:45dash dash outdated
- 10:11:49and this might take a second here are
- 10:11:51four packages that are outdated on my
- 10:11:53computer
- 10:11:54babel pi game pi tz and setup tools
- 10:11:58if you need to upgrade one of your
- 10:11:59packages type pip
- 10:12:01install the name of the package let's
- 10:12:04say that i would like to update pygame
- 10:12:06then dash dash upgrade
- 10:12:10i just realized that in my notes i'm
- 10:12:12missing updated package so i just added
- 10:12:14that here
- 10:12:15now you can see that pie game has
- 10:12:17successfully
- 10:12:18updated and let's just check to see if
- 10:12:20it has by using
- 10:12:22pip list outdated pip
- 10:12:25list dash dash outdated
- 10:12:29and pi game is no longer in here so it
- 10:12:32is up to date
- 10:12:33and if you would like to install a
- 10:12:34package type hip
- 10:12:36install and the name of the package you
- 10:12:38would like to download
- 10:12:39let's say i would like to download
- 10:12:41pandas pip
- 10:12:42install the name of the package in this
- 10:12:44case pandas
- 10:12:47two valley boring minutes later and it's
- 10:12:50done
- 10:12:50so i should now have pandas installed
- 10:12:52and let me check
- 10:12:54pip list
- 10:12:57and here it is i have successfully
- 10:12:59downloaded the package of pandas
- 10:13:02so those are a few commands for pip and
- 10:13:05if you're interested in downloading more
- 10:13:06packages head to python's package index
- 10:13:09on second thought i should probably show
- 10:13:11you all where that is python's package
- 10:13:13index
- 10:13:14is located at pipi.org
- 10:13:17and you can do a search for all sorts of
- 10:13:19different projects and packages
- 10:13:21so everybody that is pip4 python i will
- 10:13:24post my notes in the comments section
- 10:13:25down below
- 10:13:26and well yeah that's how to use pip for
- 10:13:29python
- 10:13:30hey what's going on everybody it's bro
- 10:13:32hope you're doing well
- 10:13:33and in this video i'm going to show you
- 10:13:34all how we can convert a python file to
- 10:13:37an executable
- 10:13:38so sit back relax and enjoy the show
- 10:13:43here's a quick disclaimer before we
- 10:13:45begin windows defender and other
- 10:13:47anti-virus programs may prevent your
- 10:13:49executable from running but if you head
- 10:13:51to the settings section of your
- 10:13:52anti-virus you can disable that so that
- 10:13:54your python file can run
- 10:13:56also please be sure that pip and pi
- 10:13:58installer are both installed and up to
- 10:14:00date
- 10:14:01i have a separate video on that if
- 10:14:03you're interested now the program that
- 10:14:04i'm going to be converting to an
- 10:14:06executable
- 10:14:06has a gui portion to it i created a
- 10:14:09clock program in a previous video
- 10:14:11so i'm going to turn this clock program
- 10:14:13into an executable
- 10:14:15now i recommend creating a new folder on
- 10:14:17your desktop just because this process
- 10:14:19is normally somewhat messy so we'll
- 10:14:21create a folder to contain it all
- 10:14:23now copy any relevant python files and
- 10:14:25or images and paste them to within
- 10:14:27this folder if you have an image that
- 10:14:29you would like to set as an icon for
- 10:14:31your executable
- 10:14:32i recommend moving that to the same
- 10:14:33folder as well our next step is to open
- 10:14:36command prompt
- 10:14:38and we need to change the directory of
- 10:14:40command prompt so that it's pointing to
- 10:14:42that folder that contains our python
- 10:14:44file
- 10:14:44so on your python file right click on it
- 10:14:46go to properties and copy
- 10:14:48this location and in command prompt to
- 10:14:52change directory type
- 10:14:53cd space and then paste that location
- 10:14:56now here's the cheat code to convert a
- 10:14:58python file to an executable
- 10:15:00make sure that pi installer and pip are
- 10:15:02installed and both
- 10:15:03up to date so type pi installer
- 10:15:07dash capital f to make all of this one
- 10:15:10file
- 10:15:11if you need the terminal window in your
- 10:15:14program
- 10:15:14then omit this next part since i'm
- 10:15:16running a gui application i do not need
- 10:15:18that terminal window
- 10:15:20so to remove that window type dash w
- 10:15:23if you have an icon that you want to set
- 10:15:26type dash
- 10:15:27i and the name of your icon however it
- 10:15:30should be an ico file
- 10:15:32so if you have some other image that you
- 10:15:34would like to convert to an ico file
- 10:15:36here's how if you need to convert an
- 10:15:39image to an ico file
- 10:15:41you can just google convert to ico but
- 10:15:43one website that i commonly use
- 10:15:45is icoconvert.com just pick a file to
- 10:15:48upload
- 10:15:49so choose file i'm going to convert this
- 10:15:52png file of an alarm clock
- 10:15:54upload it there is a bunch of different
- 10:15:57settings you can use
- 10:15:58convert ico and then download my icon
- 10:16:02and once your ico file is downloaded i
- 10:16:04will drag and drop it to my folder
- 10:16:06and i will rename this let's rename this
- 10:16:09as
- 10:16:09icon dot ico and here's that cheat code
- 10:16:13one more time pi installer dash f
- 10:16:16dash w if you do not need the terminal
- 10:16:18window
- 10:16:19dash i if you would like to add an icon
- 10:16:22the name of your icon it should be an
- 10:16:23ico file
- 10:16:24followed by the name of the python file
- 10:16:27and mine is named
- 10:16:29clock.p.y then hit enter
- 10:16:36alright looks like building our
- 10:16:39executable has completed successfully so
- 10:16:41open that folder back up
- 10:16:43your executable is located within this
- 10:16:45dist folder
- 10:16:46i would recommend just dragging and
- 10:16:48dropping it to your desktop
- 10:16:50and we can get rid of this old folder
- 10:16:53but close out a command prompt and by
- 10:16:56clicking on this executable
- 10:16:58this executable will now run my python
- 10:17:00program
- 10:17:01well everyone that is how to convert a
- 10:17:03python file to
- 10:17:04an exe file i will post my notes to the
- 10:17:07comment section down below
- 10:17:08and well yeah that's how to convert a
- 10:17:10python file to an executable
- 10:17:13what's going on everybody it's you bro
- 10:17:15hope you're doing well
- 10:17:16and in this video i'm going to show you
- 10:17:17all how we can create a basic calculator
- 10:17:19using python so
- 10:17:21sit back relax and enjoy the show
- 10:17:26let's create a calculator after
- 10:17:27importing tkinter we'll need to define
- 10:17:30three separate functions our first
- 10:17:32function will be named
- 10:17:33button press and there is one parameter
- 10:17:37which we will call num we're not going
- 10:17:39to fill this in quite yet we'll get back
- 10:17:41to it later we'll use
- 10:17:42pass as a placeholder we'll also need an
- 10:17:45equals function to calculate our
- 10:17:47expression
- 10:17:50and a function to clear our calculator
- 10:17:55okay that's it for our three functions
- 10:17:57let's create a window
- 10:17:58window equals tk and at the end of our
- 10:18:02program
- 10:18:02we'll write window.mainloop
- 10:18:06let's set a title window
- 10:18:09dot title calculator
- 10:18:14programs good but make sure you spell
- 10:18:17title right
- 10:18:21and let's set a geometry of
- 10:18:25500 500 should be decent
- 10:18:29now we're going to create a string named
- 10:18:31equation
- 10:18:32text equals a set of quotes
- 10:18:36this will effectively be empty for the
- 10:18:38time being
- 10:18:40and we'll need to create a string var
- 10:18:42and i will call this
- 10:18:44equation label equals string
- 10:18:48var let's create a label
- 10:18:51label equals label we're adding our
- 10:18:54label
- 10:18:54to our window i will set our text
- 10:18:58variable equal to our equation label
- 10:19:04you can pick a font if you wish
- 10:19:11i'll set the background color equal to
- 10:19:13white
- 10:19:17a width of 24
- 10:19:20and a height of two
- 10:19:24and we need to finish this by using
- 10:19:26label dot pack
- 10:19:27and we should have a blank label within
- 10:19:30our calculator
- 10:19:31this will display our numbers that we're
- 10:19:33entering in or our expression
- 10:19:36okay this is the tedious part we have to
- 10:19:38create all of the buttons and i think we
- 10:19:40should place these all within a frame
- 10:19:42frame equals frame we're adding our
- 10:19:44frame to our window
- 10:19:46frame dot pack
- 10:19:50now let's create button one we'll do
- 10:19:52some copying and pasting later
- 10:19:53so it's not as tedious so button one
- 10:19:57equals button we're adding our button to
- 10:20:00our frame
- 10:20:01i'll set the text equal to 1
- 10:20:04the height equal to 4 and the width
- 10:20:07equal to 9 and a font size
- 10:20:11of 35
- 10:20:15okay we'll need a command command
- 10:20:18equals i'll write a lambda function here
- 10:20:22lambda we will call button press
- 10:20:28i think i'm going to put this on the
- 10:20:29next line just so it's easier to read
- 10:20:31button press and we will pass in the
- 10:20:33number one
- 10:20:35and we need to add our button to our
- 10:20:37frame
- 10:20:38button one dot grid
- 10:20:42and i'm going to set a row equal to zero
- 10:20:45and a column
- 10:20:46equal to zero and let's just test it
- 10:20:50okay let's add the other buttons i'm
- 10:20:53going to copy and paste everything that
- 10:20:54we have for button one
- 10:20:56and change button one to button two
- 10:21:00set the text equal to two pass into
- 10:21:03to button press and for the row and
- 10:21:06column
- 10:21:06i'm going to set the column equal to one
- 10:21:09but keep row the same
- 10:21:11okay button three
- 10:21:15it's gonna be the same process really so
- 10:21:19text equals three
- 10:21:20button press three row zero column two
- 10:21:27and button four
- 10:21:30text equals four button press
- 10:21:34four column
- 10:21:37actually let's set this to row one
- 10:21:40column zero and i need to change that
- 10:21:44too
- 10:21:47okay let's just double check this
- 10:21:52okay button five
- 10:21:57so button five text equals five
- 10:22:01button press five and
- 10:22:05let's set column equal to one
- 10:22:10okay number six i told you all this is
- 10:22:14going to be tedious
- 10:22:21okay so row one column two
- 10:22:29button seven
- 10:22:38okay so row two column two
- 10:22:44actually let's change that to column
- 10:22:46zero
- 10:22:48much better okay
- 10:22:51button eight
- 10:22:55eight eight eight eight
- 10:22:59row two column one
- 10:23:04number nine
- 10:23:14row two column two
- 10:23:20and lastly zero
- 10:23:26so button zero
- 10:23:30text equals zero button press pass in
- 10:23:33zero
- 10:23:34and this will be row three column
- 10:23:38zero okay
- 10:23:41not too bad now let's fill in some of
- 10:23:43the symbols
- 10:23:45so we need plus
- 10:23:49i'm just going to rename this as plus
- 10:23:54the text will equal the plus sign put
- 10:23:58that within quotes
- 10:24:00and button press will be plus
- 10:24:05oh and we should probably put this in a
- 10:24:07row and column so let's say
- 10:24:09row 0 column three
- 10:24:12so it should be on the right hand side
- 10:24:14here
- 10:24:15okay we need minus
- 10:24:25let's change plus to minus
- 10:24:30and place this at row one column three
- 10:24:36we need multiply
- 10:24:45that is an asterisk
- 10:24:51row two column three
- 10:24:55and divide
- 10:25:04that is a forward slash
- 10:25:12row three column three
- 10:25:18i promise we're near the end of creating
- 10:25:20these buttons we'll need an equal sign
- 10:25:23let's say equal
- 10:25:27equal text
- 10:25:31is the equal sign and our command
- 10:25:34is going to be
- 10:25:37equals the function
- 10:25:41and this will be row 3 column 2.
- 10:25:50we need a decimal
- 10:26:02let's change the text to a decimal
- 10:26:08row three column one
- 10:26:13okay and lastly we just need a clear
- 10:26:16button let's put this at the bottom
- 10:26:24so clear clear
- 10:26:28let's set the text equal to the word
- 10:26:31clear
- 10:26:33and the command will be our clear
- 10:26:36function
- 10:26:38and i think i'm going to pack this at
- 10:26:40the end
- 10:26:42but let's change frame to window
- 10:26:46okay let's check out all these buttons
- 10:26:50okay not too bad i think i'm going to
- 10:26:51change the width on our clear button so
- 10:26:53it's a little bit wider
- 10:26:55let's change the width to 12.
- 10:26:58that's a little bit better okay let's
- 10:27:01fill in some of these functions
- 10:27:03let's start with button press we're
- 10:27:06going to state
- 10:27:07global equation
- 10:27:10text then our equation text
- 10:27:16is equal to equation text plus
- 10:27:20we're converting whatever number or
- 10:27:22symbol that we receive
- 10:27:24to a string and our equation
- 10:27:29label which is a string var will be set
- 10:27:32to our equation text
- 10:27:36and let's test this
- 10:27:39so we can type in any combination of
- 10:27:42numbers or symbols
- 10:27:47okay let's work on our equals
- 10:27:50function next so let's say
- 10:27:53global equation
- 10:27:57text total
- 10:28:01equals
- 10:28:04eval now eval will parse the expression
- 10:28:07that we pass in
- 10:28:10equation
- 10:28:14text
- 10:28:17and our equation label
- 10:28:20will be set to whatever our total is
- 10:28:26and if we want to reuse our total let's
- 10:28:28set
- 10:28:29our equation text
- 10:28:33equal to total 2 plus 2
- 10:28:37equals four minus three equals one
- 10:28:41times five equals five divided by two
- 10:28:44equals two point five now what if we
- 10:28:47divide by zero
- 10:28:49well we'll run into that ugly zero
- 10:28:51division error
- 10:28:53so let's write our code within a try and
- 10:28:55accept block
- 10:28:56so i'm going to indent some of this
- 10:29:01and let's write try all of this code
- 10:29:04and we will catch a zero division error
- 10:29:06exception
- 10:29:08so accept zero division error
- 10:29:15and i will set our equation
- 10:29:19label set
- 10:29:22let's say arithmetic
- 10:29:26error equation text
- 10:29:30equals a set of quotes okay so we should
- 10:29:33be able to catch this exception now
- 10:29:37five divided by zero equals arithmetic
- 10:29:40error
- 10:29:41now what if we have a syntax here
- 10:29:45like we type in just a bunch of symbols
- 10:29:46and hit equals
- 10:29:49we'll probably want to catch the syntax
- 10:29:51error as well
- 10:29:52so let's write an additional accept
- 10:29:55block before
- 10:29:56zero division error or after i guess it
- 10:29:59doesn't matter
- 10:30:01accept syntax
- 10:30:04error let's change arithmetic to
- 10:30:09syntax and we can keep equation text the
- 10:30:11same
- 10:30:13so now our program won't crash if we
- 10:30:15type in a bunch of symbols
- 10:30:17syntax error now let's work on the clear
- 10:30:20button
- 10:30:22so that is within the clear function
- 10:30:28so we will set global
- 10:30:31equation text
- 10:30:36equation label
- 10:30:41dot set to a set of quotes to
- 10:30:43effectively clear it
- 10:30:46and our equation
- 10:30:49text equal to a set of quotes as well
- 10:30:54and that should be it for this program
- 10:30:55let's run this one last time
- 10:30:57let's say 3.14 times
- 10:31:012.1 equals 6.594
- 10:31:05divided by 2.1
- 10:31:08equals 3.14 minus one i'm just making up
- 10:31:12numbers here
- 10:31:13plus ten equals twelve point one four
- 10:31:16and let's clear this and start again one
- 10:31:19plus
- 10:31:19two plus three plus four equals ten
- 10:31:23i think that's good enough all right
- 10:31:24everybody so that's how to create a
- 10:31:26basic
- 10:31:27calculator in python i will post all of
- 10:31:29my code in the comments section down
- 10:31:31below if you would like a copy for
- 10:31:33yourself
- 10:31:34but yeah that's how to create a basic
- 10:31:36calculator using python
- 10:31:38hey what's going on everybody it's your
- 10:31:40bro hope you're doing well and in this
- 10:31:42video i'm going to explain how we can
- 10:31:43build a basic text editor program in
- 10:31:46python so
- 10:31:47sit back relax and enjoy the show
- 10:31:51okay people let's create a text editor
- 10:31:53here's all the different imports that
- 10:31:55you'll need and we'll begin by defining
- 10:31:57all of the different functions that
- 10:31:58we'll need
- 10:31:59so let's say we have a change color
- 10:32:01function to change the color
- 10:32:03of our font and for all of these
- 10:32:05functions i'm going to write pass as a
- 10:32:07placeholder
- 10:32:08so we have change font and
- 10:32:12this function will accept a varying
- 10:32:14amount of arguments
- 10:32:15so we'll use that args keyword
- 10:32:19we will need to open a file new
- 10:32:23file
- 10:32:27open file
- 10:32:34save file
- 10:32:40cut
- 10:32:44copy
- 10:32:49paste yeah there's a lot of functions
- 10:32:52here
- 10:32:54about
- 10:32:57this is for an about section
- 10:33:01and lastly quit
- 10:33:05so how many do we have one two three
- 10:33:09four five six seven eight
- 10:33:12nine ten ten functions let's create the
- 10:33:15main window that we're gonna work with
- 10:33:17window equals tk
- 10:33:20and at the very end window.main
- 10:33:24loop
- 10:33:27okay so let's create a title for this
- 10:33:28window window
- 10:33:30title notepad
- 10:33:34program i guess or maybe text editor
- 10:33:36program would be more appropriate
- 10:33:39text editor program
- 10:33:44okay and we will need a file to work
- 10:33:46with and i'll declare that now
- 10:33:48file equals none
- 10:33:51we'll need a window width and height
- 10:33:55window with equals 500 is good and a
- 10:34:00height
- 10:34:03okay so let's attempt to center this
- 10:34:05window
- 10:34:06because right now it just appears to the
- 10:34:08side
- 10:34:10so our next step is to get the screen
- 10:34:12width
- 10:34:16equals window dot w
- 10:34:20info screen width
- 10:34:24and i'll copy this for screen height
- 10:34:35okay so we need to figure out how much
- 10:34:37we're going to move our window on the x
- 10:34:39and y axis so let's say x
- 10:34:42equals screen
- 10:34:46width divided by 2
- 10:34:49minus
- 10:34:52window width divided by
- 10:34:56two and we're going to cast all of this
- 10:34:58to an
- 10:34:59end so add an inch cast around this
- 10:35:04let's copy this do the same thing for y
- 10:35:08except this will be screen height
- 10:35:11and window height
- 10:35:17okay let's set the geometry for the
- 10:35:19window window
- 10:35:21geometry i'm going to use a
- 10:35:24string format method so let's say
- 10:35:32these will be placeholders for our
- 10:35:34values
- 10:35:37so we have window width for the first
- 10:35:40value
- 10:35:42window height
- 10:35:46x and y so this should be near the
- 10:35:50middle
- 10:35:51cool
- 10:35:55we're going to create a string var
- 10:35:57variable to hold the
- 10:35:58font name font name
- 10:36:02equals string var
- 10:36:05and pass in your window and you can set
- 10:36:08this to
- 10:36:09what do you want your default font to be
- 10:36:12so let's say font
- 10:36:13name dot set and then pick a font
- 10:36:16i'll use ariel i think
- 10:36:19let's create a font size string bar as
- 10:36:22well
- 10:36:28we're going to add this to the window
- 10:36:32and we can set a default size
- 10:36:37perhaps 25.
- 10:36:40okay let's create a text area
- 10:36:43text area equals text
- 10:36:48we're adding this to the window i'm
- 10:36:51going to set a
- 10:36:52font equal to font
- 10:36:55name dot get the name of the font that
- 10:36:59we want to use
- 10:37:00will be the font that we set within our
- 10:37:03font
- 10:37:03name string var so now we'll use a size
- 10:37:07of font size dot get
- 10:37:11font size dot get
- 10:37:16okay now we need a scroll bar
- 10:37:20scroll bar equals
- 10:37:23scroll bar
- 10:37:26writing this to our text area so pass
- 10:37:29that in
- 10:37:31and i'm going to configure my window
- 10:37:34using grid row configure
- 10:37:39this will allow our text area to expand
- 10:37:42but we're going to set a weight equal to
- 10:37:441 so that it doesn't back expand
- 10:37:47and then we need column configure
- 10:37:55okay and text
- 10:37:59area dot grid
- 10:38:03i'm going to use the sticky keyword and
- 10:38:07this will stick north plus east
- 10:38:10plus south plus west
- 10:38:14so my text area should take most of the
- 10:38:16window
- 10:38:19this is some sample text
- 10:38:24hooray so it goes down to the next line
- 10:38:27once it runs out of room on one line
- 10:38:29unless you resize the window which is
- 10:38:31exactly what we wanted
- 10:38:33let's create a scroll bar while we
- 10:38:37already created it we just have to
- 10:38:39add a few details to it
- 10:38:42so that will be near the bottom
- 10:38:45and why don't we say scroll
- 10:38:49bar dot pack
- 10:38:53side equals right
- 10:38:56fill equals y
- 10:39:00and then we just have to configure our
- 10:39:03text area
- 10:39:04with the scroll bar so text
- 10:39:08area dot config
- 10:39:12y scroll command
- 10:39:16equals scroll bar
- 10:39:20dot set so that should work but
- 10:39:24let's test it to be sure okay i'm just
- 10:39:27going to type a bunch of f's
- 10:39:31okay look at that our scroll bar works
- 10:39:34cool okay let's head to this portion
- 10:39:39and we'll create a bunch of buttons well
- 10:39:42a button
- 10:39:43an option menu and a spin box for the
- 10:39:46font color the size and the font style
- 10:39:49so i'm going to place all of these
- 10:39:51different widgets within a frame
- 10:39:53frame equals frame add it to the window
- 10:39:58and then i'm going to use frame dot grid
- 10:40:02okay let's create a color button to
- 10:40:05change the fonts color
- 10:40:07color button equals button
- 10:40:10i'll add my button to my frame
- 10:40:13i'll set the text of the button equal to
- 10:40:16color
- 10:40:18and i will associate a command of
- 10:40:21change color which we have already
- 10:40:23defined at the top
- 10:40:25where is it at the top here change color
- 10:40:32and then actually that's it for this
- 10:40:35portion
- 10:40:36so then we need to add this button color
- 10:40:40button dot grid i'll set the row
- 10:40:44equal to zero and the column equal to
- 10:40:47zero
- 10:40:48so let's be sure that this appears make
- 10:40:51sure you spell grid right
- 10:40:53okay and we have a color button but this
- 10:40:56button doesn't do anything
- 10:40:58so let's head to the change color
- 10:41:00function
- 10:41:02within the change color function we're
- 10:41:04going to say color
- 10:41:05equals color chooser
- 10:41:10dot ask color
- 10:41:14and you can set a title to
- 10:41:17pick a color or
- 10:41:21else you don't technically need a title
- 10:41:25and then text area
- 10:41:28dot config oh i should probably print
- 10:41:32this color
- 10:41:32so let me do that um i'm just gonna
- 10:41:35explain something here
- 10:41:36so this color is going to be i believe a
- 10:41:39tuple
- 10:41:39so let's print it so let's say that i
- 10:41:43would like
- 10:41:45white okay so we have a tuple we just
- 10:41:49need
- 10:41:50one of these values not both of them
- 10:41:53so i'm going to say
- 10:41:56text area dot config
- 10:42:00foreground equals our color but we do
- 10:42:03not want to assign a tuple
- 10:42:05to our color we just need one of these
- 10:42:07values so let's say i would like this
- 10:42:09hex value
- 10:42:11so i'm going to say at index 1
- 10:42:15and let's try it color
- 10:42:19perhaps i would like my text pink for
- 10:42:22some reason so we're going with pink
- 10:42:24this is some text i suppose
- 10:42:28and we can change this back too by using
- 10:42:30this color button now i would like
- 10:42:32blue and we have blue text okay so that
- 10:42:36is the change color function i'm going
- 10:42:38to minimize this because we do not need
- 10:42:40it anymore
- 10:42:41okay we're going to change the font next
- 10:42:44so
- 10:42:44head to the portion of our code right
- 10:42:48after the
- 10:42:49color button and we'll create an option
- 10:42:51menu
- 10:42:52to list all of the different fonts
- 10:42:53available to us so let's say
- 10:42:55font box equals option
- 10:42:59menu we're adding this to our frame
- 10:43:04we need to set a variable a string var
- 10:43:08so that will be our font name that we
- 10:43:10declared up here
- 10:43:12now here's a trick to add all of the
- 10:43:14fonts as options to our option menu
- 10:43:17we can use the unpacking operator which
- 10:43:20is an asterisk
- 10:43:21followed by font dot families
- 10:43:25and then call this function so this will
- 10:43:28return
- 10:43:29all of the different fonts available to
- 10:43:31us and we just need to set a command
- 10:43:34and when we use our option menu we'll
- 10:43:37call
- 10:43:38change font and we need to add
- 10:43:41our font box to our grid
- 10:43:46row equals zero and column
- 10:43:49equals one so this should be on the
- 10:43:51right hand side of our color button
- 10:43:55yeah it's going to look something like
- 10:43:56that and you can see the default is
- 10:43:58arial that we set
- 10:43:59and we can change it to something else
- 10:44:02like
- 10:44:03comic sans or whatever
- 10:44:06uh however it's not going to change it
- 10:44:08quite yet so that'll be the change font
- 10:44:10function
- 10:44:11before we fill in the change font
- 10:44:13function there's another widget that's
- 10:44:15going to share the same command of
- 10:44:17change font so we should create that
- 10:44:18next
- 10:44:19so our next widget is a spin box which i
- 10:44:22will name
- 10:44:23sizebox this will be in charge of
- 10:44:26increasing or decreasing the font
- 10:44:28so size box equals spin box
- 10:44:32we're adding the spin box to our frame
- 10:44:35and we'll set a range
- 10:44:37so from underscore you have to add that
- 10:44:39underscore it's kind of weird
- 10:44:41this is the range of our spin box for
- 10:44:43the font size
- 10:44:44from 1 to let's say 100
- 10:44:49the text variable will be
- 10:44:53font size
- 10:44:57and our command equals change
- 10:45:01font so when
- 10:45:04we use our spin box and call this
- 10:45:08command of change font it's going to
- 10:45:10pass in
- 10:45:11our font size as an argument whereas
- 10:45:13with option menu
- 10:45:14it doesn't pass any arguments that's why
- 10:45:17with our
- 10:45:17change font function we're accepting a
- 10:45:20varying amount of arguments
- 10:45:21because one's going to pass in an
- 10:45:23argument and the other isn't and we just
- 10:45:26need to add our spin box
- 10:45:28so that would be size box
- 10:45:32dot grid row equals zero
- 10:45:36and column equals two
- 10:45:39so we should have a spin box right on
- 10:45:42the bottom
- 10:45:43and it's set to 25 that was our default
- 10:45:46for
- 10:45:47our font size string var which we set as
- 10:45:50the text variable
- 10:45:52okay so we can fill in this function of
- 10:45:54change font now
- 10:45:55which will apply both to our option menu
- 10:45:58and our spin box
- 10:46:00so that is all the way at the top within
- 10:46:03the change font function
- 10:46:04we're going to take our text area
- 10:46:08dot config set
- 10:46:11the font equal to font
- 10:46:14name dot get
- 10:46:18and the size will be size box
- 10:46:22dot get and that's it for this function
- 10:46:25i'll minimize it for now and let's test
- 10:46:28it
- 10:46:30this is some sample text
- 10:46:34hey how you doing good
- 10:46:38looking okay so let's change
- 10:46:41the font uh let's pick comic sans or
- 10:46:45whatever you want
- 10:46:46and we can increase or decrease the size
- 10:46:51and we can change the color how about
- 10:46:54red this time
- 10:46:56all right nice now let's add a menu bar
- 10:46:59at the top so we can save this file or
- 10:47:02open a file or create a new file and
- 10:47:04some other stuff
- 10:47:06on second thought i'm going to move
- 10:47:08these two last lines of code
- 10:47:09to this section here
- 10:47:12where we create our scroll bar just so
- 10:47:14that it's more organized
- 10:47:16okay so at the bottom let's create
- 10:47:19various drop-down menus so we'll need a
- 10:47:21menu bar to begin with
- 10:47:23so let's say menu bar
- 10:47:27equals menu we're adding this to our
- 10:47:30window
- 10:47:31make sure that's capital by the way
- 10:47:35and window dot config
- 10:47:39menu equals menu bar
- 10:47:44so we'll create a file menu edit menu
- 10:47:46and
- 10:47:47a help menu so let's begin with the file
- 10:47:50menu to add to the menu bar file menu
- 10:47:54equals menu we're adding this to our
- 10:47:57menu
- 10:47:58bar and set a tear off equal to zero
- 10:48:06we'll need a cascade for it to actually
- 10:48:09be a drop down menu
- 10:48:10so that will be menu bar
- 10:48:14dot add cascade
- 10:48:21label equals file
- 10:48:25comma menu equals
- 10:48:28file menu
- 10:48:32let's see if anything appears yet yep so
- 10:48:35we have a file menu
- 10:48:37but there's nothing to drop down yet
- 10:48:40so we need some options
- 10:48:43well commands technically file menu
- 10:48:47dot add command
- 10:48:54label equals new
- 10:49:00and the command will equal our
- 10:49:03new file function
- 10:49:06actually i'll put these together okay so
- 10:49:09let's copy this
- 10:49:10and paste it we have open
- 10:49:16the command will be open file
- 10:49:22we have save
- 10:49:25the command will be save
- 10:49:28file and i'll add a separator
- 10:49:33file menu dot add
- 10:49:37separator and lastly
- 10:49:40we will have exit
- 10:49:44command quit
- 10:49:47okay let me show you what this looks
- 10:49:49like
- 10:49:51so here's our drop down menu
- 10:49:54that separator is this bold line going
- 10:49:56across
- 10:49:57so if i was to remove this then we
- 10:50:00wouldn't have that bolt line
- 10:50:01you can keep it in or get rid of it do
- 10:50:03whatever you want
- 10:50:05and this is the tear off so i'm going to
- 10:50:07remove the tear off
- 10:50:11and you get this like tear off line at
- 10:50:13the top i guess it's the default
- 10:50:15i usually don't like it so i set tear
- 10:50:17off equal to zero
- 10:50:18so that is our file menu let's work on a
- 10:50:21edit menu
- 10:50:24edit menu equals
- 10:50:27menu menu
- 10:50:32bar make sure that's capital two
- 10:50:38tare off equals zero
- 10:50:42we'll need a cascade menu bar
- 10:50:46dot add cascade
- 10:50:55label equals
- 10:51:00edit
- 10:51:02menu equals edit
- 10:51:06menu
- 10:51:10edit menu dot add
- 10:51:14command
- 10:51:19label equals cut to cut some text
- 10:51:25command equals copy
- 10:51:30actually that's cut my bad
- 10:51:33okay next is copy so let's copy this
- 10:51:36and paste it
- 10:51:40cut copy
- 10:51:44we have paste
- 10:51:47command paste
- 10:51:51all right let's take a look
- 10:51:55oh make sure you spell label right
- 10:51:58okay there we go okay so we can cut
- 10:52:01copy and paste and lastly we should have
- 10:52:05a help menu
- 10:52:08help menu equals
- 10:52:12menu menu
- 10:52:15bar tear off
- 10:52:19equals zero
- 10:52:22i think i'll copy this for my cascade
- 10:52:26so menu bar dot add cascade
- 10:52:30help menu equals help
- 10:52:33menu
- 10:52:38what we'll be doing for a command is
- 10:52:40that we'll be creating
- 10:52:41an about section that will bring up a
- 10:52:44pop-up window if you need to display
- 10:52:46any like help information or anything
- 10:52:49uh so let's say about and the command
- 10:52:52will be
- 10:52:54about okay let's take a look
- 10:52:58so we have help and we'll have an about
- 10:53:00section that will tell you more about
- 10:53:02this program
- 10:53:03but we need to work on some of these
- 10:53:05functions let's head to the quit
- 10:53:08function
- 10:53:09this one's fairly easy this is found
- 10:53:11within the file menu
- 10:53:12window dot destroy that will close out
- 10:53:16of the window
- 10:53:18so go to file then exit to exit the
- 10:53:22program
- 10:53:23we don't need it anymore so let's
- 10:53:25minimize this
- 10:53:26okay let's go to the about function
- 10:53:31let's bring up a message box show info
- 10:53:36this first argument is the title so
- 10:53:38let's say
- 10:53:39about this program
- 10:53:43and the text will be whatever you want
- 10:53:45to
- 10:53:46tell the user about this program this
- 10:53:49is a program written
- 10:53:53by you
- 10:53:57okay so we should have a pop-up window
- 10:53:59when you go to help
- 10:54:00then about about this program this is a
- 10:54:03program
- 10:54:04written by you the next function is the
- 10:54:07paste function so we can paste some text
- 10:54:10text area dot event
- 10:54:14generate and the event is going to be
- 10:54:18within quotes
- 10:54:19and double angle brackets
- 10:54:22paste let's copy this
- 10:54:26and change paste to copy
- 10:54:31and then cut
- 10:54:37okay so we should be able to cut copy
- 10:54:39and paste
- 10:54:44hey how's it
- 10:54:47hanging okay
- 10:54:50let's highlight this edit
- 10:54:54copy let's move down to the next line
- 10:54:58and paste and we should be able to cut
- 10:55:01as well
- 10:55:02edit cut nice
- 10:55:06head to the new file function
- 10:55:09and we'll set the title of our window to
- 10:55:12maybe
- 10:55:13untitled until we save something so
- 10:55:16untitled
- 10:55:20and i need to delete any text within our
- 10:55:23text area
- 10:55:24text area dot delete and we need to set
- 10:55:28a range so this has to be a floating
- 10:55:30point number
- 10:55:31the first character until the end
- 10:55:34let's try it so i'll write some sample
- 10:55:40text
- 10:55:42and we are going to go to file new
- 10:55:45that changed the title and it deleted
- 10:55:48our text
- 10:55:49so that is the new file function
- 10:55:52let's work on open file we are now
- 10:55:55within
- 10:55:56the open file function and we'll say
- 10:55:58file
- 10:55:59equals ask open
- 10:56:03file name so we have a
- 10:56:07default extension if you would like one
- 10:56:11default i think i spelled that wrong
- 10:56:14extension
- 10:56:18equals let's say a txt file
- 10:56:23comma what are the accepted file types
- 10:56:28i'll set this equal to
- 10:56:32all files
- 10:56:35comma and then we need asterisk
- 10:56:38dot asterisk for all file types
- 10:56:43as well as text documents
- 10:56:46so these are what is accepted when we
- 10:56:49look for a file so we have either all
- 10:56:52files
- 10:56:53or all text documents text
- 10:56:57documents comma
- 10:57:02asterisk dot txt
- 10:57:08okay so then let's try some code
- 10:57:14we'll set our window title
- 10:57:18equal to os dot path
- 10:57:21dot base name of our file
- 10:57:24so that we will change the title of the
- 10:57:26window
- 10:57:27to match whatever the file is that we
- 10:57:29opened
- 10:57:32then let's take our text
- 10:57:35area delete what's currently here
- 10:57:38from one until the end
- 10:57:42take our file use the open function
- 10:57:47open our file and read it so pass in our
- 10:57:52text area dot insert
- 10:57:58beginning at index of 1.0
- 10:58:04file dot read
- 10:58:07that you can catch any exceptions to
- 10:58:11i'll just write accept exception but
- 10:58:13it's better to name specific
- 10:58:15exceptions i'll just print
- 10:58:20couldn't read
- 10:58:24file and we should close our file at the
- 10:58:28end within a finally block
- 10:58:31finally file dot close
- 10:58:35we'll need a sample file to read so i'm
- 10:58:37going to create a new file on my desktop
- 10:58:40i'll call this test.txt
- 10:58:44and let's write something whoa
- 10:58:47you can actually read this
- 10:58:53awesome okay let's save
- 10:58:56close out of it add run our program
- 10:59:01file open test.txt
- 10:59:05open whoa you can actually read this
- 10:59:08awesome nice so we know that the open
- 10:59:12function is working
- 10:59:13if we need to create a new file just go
- 10:59:15to file new
- 10:59:17and that will delete our old text
- 10:59:20so lastly we have save file
- 10:59:25underneath the save file function file
- 10:59:28equals file dialog
- 10:59:31dot ask save as
- 10:59:34file name so this will be the default
- 10:59:38name for the file but you can change
- 10:59:40that
- 10:59:41initial file equals
- 10:59:45let's say untitled
- 10:59:49dot
- 10:59:53you txt set a default extension default
- 10:59:57extension equals let's say txt
- 11:00:05file types equals
- 11:00:14all files
- 11:00:18comma then asterisk dot asterisk
- 11:00:22for all file types
- 11:00:30and any text documents text
- 11:00:34documents
- 11:00:38asterisk dot txt
- 11:00:46okay if file
- 11:00:49is none that's if they close out of our
- 11:00:52file dialog
- 11:00:56we'll just return
- 11:00:59else
- 11:01:03so we'll set the window title first
- 11:01:07window title os dot path
- 11:01:11dot base name
- 11:01:16file
- 11:01:18file equals open open
- 11:01:22our file and write
- 11:01:27file dot right
- 11:01:31take our text area and get
- 11:01:34the text from
- 11:01:37index one through the end
- 11:01:42catch any exceptions it's better to name
- 11:01:45them instead of just
- 11:01:46doing a general accept exception
- 11:01:49but i'm just going to print couldn't
- 11:01:53save file
- 11:01:57and finally close our file
- 11:02:00file dot close all right so
- 11:02:03let's test it so i'm going to change
- 11:02:07the font i wonder if impact is in here
- 11:02:11yes it is i'll change the font size
- 11:02:15maybe 30 and i'll pick a color
- 11:02:18how about this swamp green color okay so
- 11:02:22write something perhaps some lyrics to a
- 11:02:24song you like
- 11:02:40i should probably test copying and
- 11:02:42pasting
- 11:02:43edit copy
- 11:02:46edit paste
- 11:02:50nice
- 11:02:55all right let's actually try and save
- 11:02:56this now i'll go to
- 11:02:58file save the default file name is
- 11:03:02untitled because we set it to that
- 11:03:05i'll say all star
- 11:03:09lyrics
- 11:03:13and save now i should be able to open
- 11:03:17this
- 11:03:18so i'm going to go to my desktop here's
- 11:03:21that text file
- 11:03:21i'm going to open it and here are the
- 11:03:23lyrics that i wrote within my text
- 11:03:25editor
- 11:03:26and let's try and read this so let's go
- 11:03:29to
- 11:03:29file open find your text document
- 11:03:33open and would you look at that my
- 11:03:35lyrics are still here
- 11:03:37however this program doesn't save the
- 11:03:39font
- 11:03:40so i thought of a fun last minute
- 11:03:42project that we can do we can actually
- 11:03:44use our text editor to write a python
- 11:03:46file much like what we do with pycharm
- 11:03:49so let's write a program using our text
- 11:03:51editor program
- 11:03:53it'll be just a simple program let's
- 11:03:56print
- 11:03:58hello world and ask for some user input
- 11:04:02let's say name equals input
- 11:04:09what's your name
- 11:04:14and we will print
- 11:04:18hello plus our name
- 11:04:21variable and you can change the font and
- 11:04:24everything too if you want
- 11:04:31okay so let's save this file save
- 11:04:34i'll save this to my desktop but make
- 11:04:36sure you save it as a
- 11:04:38py file a python file i'll name this as
- 11:04:42hello dot py and save
- 11:04:47now to run a file using command prompt
- 11:04:49we need to find
- 11:04:50the directory that contains our python
- 11:04:52file mine is hello.py and it's located
- 11:04:56on my desktop
- 11:04:57so i need the location right click go to
- 11:05:00properties
- 11:05:01you can copy the location then within
- 11:05:03command prompt or terminal
- 11:05:05you can change directory by typing cd
- 11:05:08space
- 11:05:09and then paste your new directory the
- 11:05:11new location
- 11:05:12enter then to run a python program
- 11:05:15type python space and then the name of
- 11:05:19the program
- 11:05:20the python file mine is hello dot
- 11:05:23py hit enter hello world what is your
- 11:05:26name
- 11:05:27bro hello bro i just thought that would
- 11:05:31be a fun last minute project
- 11:05:32that we can do you don't even
- 11:05:33technically need an ide to write a
- 11:05:36python program
- 11:05:37screw you pycharm well everyone that is
- 11:05:39a basic
- 11:05:40text editor program i will post the code
- 11:05:43for this program
- 11:05:44in the comment section down below and
- 11:05:46well yeah that's a basic text editor
- 11:05:48program
- 11:05:49using python all right
- 11:05:52ladies and gentlemen let's create a game
- 11:05:54of tic-tac-toe to begin we'll need to
- 11:05:56import tkinter as well as random and
- 11:05:59let's begin by defining
- 11:06:00all of the different functions that
- 11:06:02we'll need let's define a function named
- 11:06:04next turn for the time being we'll just
- 11:06:08write pass we'll fill this in later on
- 11:06:10we'll need a function named check winner
- 11:06:16a function named empty spaces to check
- 11:06:21if there are any empty spaces left and
- 11:06:24lastly a function named new
- 11:06:26game that will launch a new game for us
- 11:06:29okay we have our four functions now
- 11:06:31let's create a window
- 11:06:32let's create a window window equals
- 11:06:35tk and at the end of our program we need
- 11:06:39to use window
- 11:06:40dot main loop and when we run this we
- 11:06:43should have just a small
- 11:06:45basic window let's set the title window
- 11:06:49dot title let's set this to
- 11:06:53tick tack toe
- 11:06:57we'll need a list of players players
- 11:07:00equals
- 11:07:02x comma o
- 11:07:05and with the way that we're writing this
- 11:07:07program we can swap
- 11:07:08these symbols with a different character
- 11:07:10for example we could say
- 11:07:12dollar sign is playing against the at
- 11:07:15symbol too
- 11:07:16but i'll demonstrate that later let's
- 11:07:18keep it as x and o for now
- 11:07:21now we need to select a random player to
- 11:07:24begin
- 11:07:25player equals random
- 11:07:28dot choice and pass in our list of
- 11:07:32players
- 11:07:33now we'll need nine buttons i'm going to
- 11:07:35create a 2d
- 11:07:36list of buttons named buttons
- 11:07:39and this will be the first row this will
- 11:07:42be the second row
- 11:07:44for the time being i'm just going to
- 11:07:45initialize these with
- 11:07:470 for everything and put these all
- 11:07:50within
- 11:07:51a list so we have a 2d list named
- 11:07:54buttons
- 11:07:56and to better visualize this i'm going
- 11:07:58to place each row on a new line
- 11:08:00so that's what our board is going to
- 11:08:01look like we have a 2d list
- 11:08:04named buttons we'll need a label to
- 11:08:06display whose turn it is
- 11:08:08label equals label
- 11:08:11i'll set the text equal to
- 11:08:15player plus the word
- 11:08:18turn and i'll set the font to
- 11:08:22font equals pick whatever font that you
- 11:08:25want
- 11:08:30and i am going to pack this label
- 11:08:33label dot pack and set the side
- 11:08:37equal to top and let's run this just to
- 11:08:40test it
- 11:08:42okay looks like it's x's turn let's try
- 11:08:45it again
- 11:08:46oh stern okay that's fine for now
- 11:08:49let's create a reset button
- 11:08:52reset underscore button
- 11:08:55equals button
- 11:08:59i'll set the text equal to
- 11:09:02restart it's going to be a restart
- 11:09:04button or reset button
- 11:09:07pick a font
- 11:09:14maybe i'll set the size to 20
- 11:09:17and we'll need a command command
- 11:09:21equals new game
- 11:09:24when we click on this button it's going
- 11:09:26to call this
- 11:09:27new game function for us and we need to
- 11:09:31pack this button reset
- 11:09:34button dot pack
- 11:09:37and i will set the side equal to top
- 11:09:40okay
- 11:09:41let's see if that appears alright so we
- 11:09:43have our
- 11:09:45turn order a label that will display
- 11:09:47whose turn it is as well as our reset
- 11:09:49button
- 11:09:50now we'll need to create all of the
- 11:09:51buttons to add to our 2d
- 11:09:53list named buttons but i'm going to
- 11:09:55place these all within
- 11:09:57a frame frame equals frame
- 11:10:00we're adding our frame to our window and
- 11:10:03i'm going to
- 11:10:04pack this frame frame dot pack
- 11:10:08now we'll take our 2d list of buttons
- 11:10:11and add a button to each spot
- 11:10:13and i think the best way to do this
- 11:10:14would be to use nested for loops
- 11:10:17we'll have an outer for loop in charge
- 11:10:19of the rows
- 11:10:20four row in range three
- 11:10:24remember we only have three rows and
- 11:10:26three columns
- 11:10:27the inner for loop will be in charge of
- 11:10:29the columns
- 11:10:30four column and range three
- 11:10:34and inside the inner for loop we're
- 11:10:36going to create a new button
- 11:10:39we'll say buttons at index
- 11:10:43row and column we have two indexes
- 11:10:46because
- 11:10:47we have a 2d list so buttons
- 11:10:50at row whatever column whatever
- 11:10:53depending on
- 11:10:54what iteration we are within our for
- 11:10:56loops
- 11:10:57we'll create a new button
- 11:11:00and we'll set the text oh first add this
- 11:11:03to the frame
- 11:11:04almost forgot about that we're adding
- 11:11:06our buttons to our frame and adding the
- 11:11:08frame to the window
- 11:11:10now we'll set the text equal to a blank
- 11:11:12set of quotes because we don't want any
- 11:11:14text
- 11:11:16we'll set the font i'll just copy what
- 11:11:19we have here
- 11:11:20for label
- 11:11:26for a width let's say five and a height
- 11:11:29let's say two i'm going to hit enter
- 11:11:32just to move down to the next line
- 11:11:33because we still have a few more things
- 11:11:35to fill in
- 11:11:36okay we'll need a command command
- 11:11:39equals and i'm going to set this equal
- 11:11:41to a lambda function
- 11:11:42so we need some arguments actually i
- 11:11:45think i'll pass in some keyword
- 11:11:46arguments
- 11:11:46row equals row and column
- 11:11:50equals column then our expression is
- 11:11:54next turn and we will pass in
- 11:11:57row and column now we also need to add
- 11:12:00our buttons to our
- 11:12:02frame buttons at
- 11:12:05index of row and column
- 11:12:09we'll use the grid function for this
- 11:12:12grid
- 11:12:13row equals and
- 11:12:16column equals column
- 11:12:20and it's always a good idea to test your
- 11:12:22program after making any major changes
- 11:12:24so we should have our label our reset
- 11:12:27button
- 11:12:28and our grid of buttons now that the
- 11:12:31main body of our program is complete
- 11:12:33let's head to the next turn function
- 11:12:36and remember that we're passing in row
- 11:12:38and column as
- 11:12:40arguments so we need to set up those
- 11:12:42parameters within the next turn
- 11:12:44function row and column and the first
- 11:12:47line within here
- 11:12:48we would like access to our player
- 11:12:52so let's say global player so that we
- 11:12:54have access to it
- 11:12:56and we're first going to check to see if
- 11:12:58the button
- 11:12:59that we click on is empty so buttons
- 11:13:03at index of row and index of column
- 11:13:07to access the text of a button just add
- 11:13:10a
- 11:13:10third index operator and type in text
- 11:13:14if the text of our button that we click
- 11:13:17is equal to a set of quotes that means
- 11:13:20it's empty
- 11:13:21and when we call check winner
- 11:13:25winner winner chicken dinner and it's
- 11:13:28false
- 11:13:29there is no winner then we will fill in
- 11:13:32that button with our player our player's
- 11:13:35character whatever it is
- 11:13:37x or o or whatever we decide
- 11:13:40so within here let's check to see if
- 11:13:44player is equal to players
- 11:13:47at index zero that is our first player
- 11:13:51and remember that we created
- 11:13:52a list of players and we're planning on
- 11:13:55swapping these later
- 11:13:56we could say like if player is equal to
- 11:13:59x
- 11:14:00but that would make our program less
- 11:14:01flexible if we want to pick like a
- 11:14:03different character instead of an x
- 11:14:05so if player is equal to player at index
- 11:14:08zero whatever symbol is there
- 11:14:11then we are going to take buttons
- 11:14:14at index of row index of column
- 11:14:19take our text and set it equal to
- 11:14:23our player
- 11:14:27and we should check to see if there's a
- 11:14:29winner after filling in this button
- 11:14:32if check
- 11:14:35winner and we still need to fill in this
- 11:14:37function
- 11:14:38returns false
- 11:14:42then we will switch players player
- 11:14:44equals
- 11:14:45players at index of one and
- 11:14:50label dot config
- 11:14:55text equals
- 11:14:59players at index 1
- 11:15:02plus the word turn
- 11:15:06so what we did here is that we're
- 11:15:08checking to see
- 11:15:09if after placing our text of our player
- 11:15:13on that button that we click if there is
- 11:15:16no winner
- 11:15:17then we're going to swap players player
- 11:15:20equals our next player
- 11:15:21and we're configuring our label so that
- 11:15:23it displays the next player's turn
- 11:15:26players at index 1 turn
- 11:15:29now what if there is a winner let's say
- 11:15:32else if
- 11:15:33check winner
- 11:15:36is true
- 11:15:40then we will take our label dot
- 11:15:44config and set the text
- 11:15:48equal to players
- 11:15:51at index 0
- 11:15:54plus the word wins
- 11:15:59now let's add another else if statement
- 11:16:02else if there's a tie let's say check
- 11:16:06winner is equal to
- 11:16:09the word tie because we are already
- 11:16:12using
- 11:16:12false and true then let's change our
- 11:16:16label
- 11:16:17and i'm just going to copy this
- 11:16:20text equals the word
- 11:16:23tie okay
- 11:16:26so let's create an else block
- 11:16:29so that goes right here
- 11:16:32so if it's not player one's turn player
- 11:16:35at index zero
- 11:16:37then it's our other player's turn player
- 11:16:39at index
- 11:16:40of one so oh then so we just need to
- 11:16:43mostly just copy this text and change a
- 11:16:46few things around
- 11:16:47so i'm going to copy all this
- 11:16:52and within here let's set
- 11:16:56player equals players at index
- 11:16:59zero then players at
- 11:17:02index zero's turn players
- 11:17:06at index one wins
- 11:17:09and that is it now our next turn
- 11:17:11function is now complete let's head down
- 11:17:13to the
- 11:17:14check winner function and we need to
- 11:17:16check all of the different win
- 11:17:18conditions
- 11:17:18and return it true if somebody won false
- 11:17:22if there is yet no winner and the word
- 11:17:24tie if it's a tie
- 11:17:26so let's check all of the horizontal win
- 11:17:28conditions
- 11:17:29so we can do this using a for loop for
- 11:17:32row in range three
- 11:17:37we need to check the text of each button
- 11:17:39in each row
- 11:17:40if buttons at index of row
- 11:17:44and column index of 0 we'll use the
- 11:17:48index operator to check the text
- 11:17:50is equal to the next button in our row
- 11:17:54so i will copy this and change zero to
- 11:17:57one
- 11:17:58check to see if that is equal to buttons
- 11:18:02at index of row index of two
- 11:18:06and i'm going to check to see if this is
- 11:18:08all not
- 11:18:10equal let me move this a little bit
- 11:18:13to a space
- 11:18:17so if that is the case that means
- 11:18:19somebody won
- 11:18:20so if all of these buttons are the same
- 11:18:23and they are not equal to
- 11:18:25an empty space that means they're all
- 11:18:27the same so let's return
- 11:18:29true that means that somebody won
- 11:18:32no we need to check the vertical win
- 11:18:34conditions
- 11:18:35if anybody has the same character all
- 11:18:38the way down
- 11:18:39a single column this next for loop is
- 11:18:41going to be four column in range three
- 11:18:44for the first button this will be button
- 11:18:47at index of zero
- 11:18:49and column then
- 11:18:52one and column let me just paste that
- 11:18:56and two and column
- 11:19:01now we need to check the diagonal win
- 11:19:03conditions
- 11:19:04so if buttons at
- 11:19:07index 0 0 that's the top left corner
- 11:19:11if the text of that button is equal to
- 11:19:14buttons at indexes of one one
- 11:19:19is equal to the indexes of
- 11:19:23button two two and if all of this
- 11:19:27does not equal an empty space
- 11:19:30then return true so there is a winner
- 11:19:35and we have one last win condition to
- 11:19:37check that is the other
- 11:19:39diagonal win condition so we just need
- 11:19:41to change some of these indexes around
- 11:19:44so we have zero two
- 11:19:47one one and two zero
- 11:19:50i'm going to change this statement to an
- 11:19:52else if statement
- 11:19:54no we need to check to see if there's
- 11:19:56any spaces remaining
- 11:19:58else if we will call the empty spaces
- 11:20:01function which we have yet to fill in if
- 11:20:05this returns false then we will
- 11:20:09return the word
- 11:20:12tie
- 11:20:15and lastly else else there is no winner
- 11:20:19and
- 11:20:20no tie so we will return false
- 11:20:24and that is it for the check winner
- 11:20:27function
- 11:20:28let's test this
- 11:20:31so this doesn't account for a tie quite
- 11:20:33yet but we can fill in
- 11:20:35some of these spaces looks like x wins
- 11:20:37and we can no longer fill in buttons
- 11:20:40and we cannot start a new game quite yet
- 11:20:42either
- 11:20:43within the empty spaces function let's
- 11:20:45create a local
- 11:20:47variable named spaces and set this equal
- 11:20:49to nine whenever we call this function
- 11:20:53four row in range
- 11:20:56three and we'll create a nested for loop
- 11:20:59for column in range
- 11:21:04three we will check the text of each
- 11:21:07button
- 11:21:08if buttons at indexes of
- 11:21:11row and column
- 11:21:15and we will check the text to see
- 11:21:21if it's not equal to an empty space
- 11:21:26if that is the case spaces
- 11:21:29minus equals one
- 11:21:33then we'll write an if statement if
- 11:21:36spaces
- 11:21:36as in spaces remaining is equal to zero
- 11:21:41that means we will return false and
- 11:21:44there are no
- 11:21:45spaces left else
- 11:21:49we will return
- 11:21:52true okay let's test it
- 11:21:56so this time i'm trying to get a tie
- 11:22:06yep looks like it's a tie this next
- 11:22:10part's optional but i would like to
- 11:22:11change
- 11:22:12the color of each button for the winning
- 11:22:14combination
- 11:22:15so within our check winner function
- 11:22:17underneath the first win condition
- 11:22:20i'm going to take buttons at index
- 11:22:23of row and index of zero
- 11:22:29use the config method
- 11:22:33and set the background color equal to
- 11:22:36let's say green
- 11:22:39and i'm going to repeat this process for
- 11:22:41the other buttons within this win
- 11:22:43condition
- 11:22:44so that is row
- 11:22:47zero row one and row two
- 11:22:50i'll copy all of this paste it
- 11:22:55and switch some of these around so these
- 11:22:57should all be matching
- 11:23:00so zero and column one and column and
- 11:23:03two in column
- 11:23:08okay so we have zero zero one
- 11:23:11one and two two
- 11:23:14and one more
- 11:23:17zero two one one and
- 11:23:20two zero now when we have a winning
- 11:23:24combination
- 11:23:26the color of the buttons involved in
- 11:23:28that combination are going to change to
- 11:23:30green
- 11:23:30now if there is a tie let's change all
- 11:23:33of the buttons to
- 11:23:34let's say yellow so i'm going to write
- 11:23:37nested for loops for this
- 11:23:39four row in range
- 11:23:43three for column
- 11:23:47in range three
- 11:23:51take our buttons at row
- 11:23:54and column and change
- 11:23:58the background color to yellow or some
- 11:24:00other color of your choosing
- 11:24:03so if there's a tie they should all be
- 11:24:06yellow
- 11:24:08i'm trying not to win here it's actually
- 11:24:10more complex than what i thought
- 11:24:16yep it looks like it's all a tie and the
- 11:24:18last thing that we need to do is to fill
- 11:24:20in
- 11:24:20the new game function so that we can
- 11:24:22begin a new game
- 11:24:24so let's say global player we would like
- 11:24:26access to our player
- 11:24:27from inside this function and set player
- 11:24:30equal
- 11:24:31to a new random choice random
- 11:24:35dot choice and pass in our list
- 11:24:38of players and we will change our label
- 11:24:41label dot config
- 11:24:44and set the text equal to player
- 11:24:48plus the word turn and we also need to
- 11:24:52reset all of our buttons
- 11:24:56for row in range
- 11:25:00three for
- 11:25:03column in range
- 11:25:07three buttons
- 11:25:10at index of row and index of column
- 11:25:16dot config
- 11:25:19text equals a set of quotes so it's
- 11:25:22empty
- 11:25:23and we'll change the background color
- 11:25:26factors previously
- 11:25:27so the default color for buttons is
- 11:25:30actually hexadecimal
- 11:25:32f 0 f 0 f 0
- 11:25:35so this function will begin a new game
- 11:25:37for us
- 11:25:38and let's test it to be sure i'm going
- 11:25:41to start a new game
- 11:25:42reset reset
- 11:25:46i'll actually try and win this time and
- 11:25:49reset
- 11:25:50okay so it looks like the new game
- 11:25:52function is working
- 11:25:54now another thing that you can do too is
- 11:25:56that you can change
- 11:25:57the players around let's say we would
- 11:25:59like to play as
- 11:26:01dollar sign and at sign so this program
- 11:26:04is flexible enough
- 11:26:06so that you can change the icons of the
- 11:26:08players
- 11:26:10all right everybody so that's a basic
- 11:26:12game of tic-tac-toe for
- 11:26:14python if you would like a copy of this
- 11:26:16code i will post all of this to the
- 11:26:17comments section down below
- 11:26:19but yeah that's how to code a basic game
- 11:26:21of tic-tac-toe
- 11:26:23for python hey what's going on everybody
- 11:26:26it's your bro hope you're doing well and
- 11:26:28in this video we're going to create a
- 11:26:30game of snake using python so
- 11:26:32sit back relax and enjoy the show
- 11:26:36first thing we should do is import from
- 11:26:39tkinter as well as the random module
- 11:26:41and let's define all of the different
- 11:26:43classes and functions that we'll need
- 11:26:45so let's create a class for our snake
- 11:26:48object
- 11:26:49as well as our food object so for the
- 11:26:52time being i'm just going to write pass
- 11:26:54for my classes we'll fill these in later
- 11:26:56so we have class snake and
- 11:26:58class food and
- 11:27:01let's define all of the different
- 11:27:02functions that we'll need so let's say
- 11:27:04we have a function named next
- 11:27:06turn
- 11:27:10change direction
- 11:27:18check collisions
- 11:27:22and lastly game over
- 11:27:29okay for change direction i'm going to
- 11:27:32have one parameter
- 11:27:34a new direction
- 11:27:37now when i create a game i like to place
- 11:27:39a bunch of constants at the top
- 11:27:41of my game constants are variables that
- 11:27:44you do not want to change
- 11:27:45later they're kind of like the settings
- 11:27:47however in python
- 11:27:49there are no constants compared to other
- 11:27:51programming languages
- 11:27:52so we're just going to create a bunch of
- 11:27:54variables that will behave like them
- 11:27:56so constants are values we do not want
- 11:27:58to change and they're kind of like the
- 11:27:59game settings
- 11:28:00and i placed them at the top for
- 11:28:02convenience and the naming convention
- 11:28:04for a constant is that
- 11:28:05all the letters are uppercase so these
- 11:28:08will be settings like
- 11:28:09the game width the speed etc
- 11:28:12so let's say the width of our game will
- 11:28:14be 700
- 11:28:15but feel free to take the liberty to
- 11:28:17pick whatever size you want
- 11:28:19so we have game width game height
- 11:28:22i'll set this to 700 as well so it's a
- 11:28:24square
- 11:28:26let's say we have a speed the speed of
- 11:28:28the snake how often will our canvas
- 11:28:30update
- 11:28:31so let's say 50 but the lower the number
- 11:28:33the faster the game
- 11:28:35and a space size how large are the items
- 11:28:38in our game like
- 11:28:39the food and body parts of the snake
- 11:28:42so i'm going to pick 50 but you can
- 11:28:44change this
- 11:28:46and body parts how many body parts does
- 11:28:50our snake have when we begin a game
- 11:28:52let's say three how about a snake
- 11:28:55color you can pick a color name
- 11:28:58you can use rgb values or you can use a
- 11:29:01hexadecimal value
- 11:29:02so i'm going to pick green 0 0 ff00 but
- 11:29:06you can pick any color you want
- 11:29:09how about food color
- 11:29:13equals red
- 11:29:16and that is ff000
- 11:29:20and what about a background color for
- 11:29:22the canvas background
- 11:29:25color i'll pick black so that is
- 11:29:28six zeros
- 11:29:31feel free to mess with some of these
- 11:29:32colors too so we have a green snake
- 11:29:35our food is going to be red and our
- 11:29:37background is going to be black
- 11:29:39so that is all of the constants for our
- 11:29:41game but you can feel free to adjust
- 11:29:43them if you like
- 11:29:44let's head down to the bottom and make
- 11:29:46our window so we have
- 11:29:48window equals tk and at the very end we
- 11:29:51should have
- 11:29:52window dot main loop
- 11:29:57let's set a title for this window window
- 11:30:01dot title
- 11:30:04snack game okay fine i'll spell it right
- 11:30:08snake game and if you do not want your
- 11:30:12window to be resizable
- 11:30:14you can use resizable
- 11:30:18and then you have to pass in false twice
- 11:30:20it's kind of strange but it's how it is
- 11:30:23okay we should have a small window and
- 11:30:26we cannot resize this even if we tried
- 11:30:30okay let's create a score label
- 11:30:33but we probably need a score first so
- 11:30:35let's say score equals zero
- 11:30:37and an initial direction direction
- 11:30:41equals let's say down
- 11:30:45now let's create a score label so let's
- 11:30:47say label
- 11:30:49equals label we're adding our
- 11:30:52label to our window let's set the text
- 11:30:56equal to score
- 11:30:59colon then i'm going to use the format
- 11:31:02method
- 11:31:04and we will pass in our score whatever
- 11:31:08it is
- 11:31:10and i'll set a font pick whatever font
- 11:31:12you prefer
- 11:31:17and 40 is a decent size
- 11:31:21and then i'm going to pack this label
- 11:31:25let's test it okay we're getting
- 11:31:28somewhere
- 11:31:29we'll need to create a canvas canvas
- 11:31:33equals canvas we're adding it to our
- 11:31:36window i'm going to set the background
- 11:31:39color
- 11:31:40equal to our background constant
- 11:31:45i'm going to set our height equal to the
- 11:31:47game
- 11:31:48height and the width equal
- 11:31:52to the game width that we set
- 11:31:56and we need to pack this canvas dot pack
- 11:32:02and we should have a game board
- 11:32:06i'm going to try and center this window
- 11:32:08when it appears
- 11:32:09so we can do that using a few lines of
- 11:32:11code the first thing we'll do is
- 11:32:13update our window so that it renders and
- 11:32:16then we need to find some dimensions
- 11:32:18so let's say the window width
- 11:32:22equals window dot w
- 11:32:26info width
- 11:32:31and window height
- 11:32:35equals window
- 11:32:38dot w info height
- 11:32:43we'll need our screen width
- 11:32:48equals window
- 11:32:53dot screen width
- 11:32:56and screen height
- 11:33:00equals window dot w
- 11:33:04info screen height
- 11:33:07where is it there it is
- 11:33:13okay then we need to see how much we're
- 11:33:15going to adjust the position of our
- 11:33:17window
- 11:33:19so let's say x equals
- 11:33:24screen width divided by 2
- 11:33:28minus window
- 11:33:32width divided by two
- 11:33:36and do the same thing for y
- 11:33:40except this will be screen height
- 11:33:45and window height
- 11:33:48and that will be why
- 11:33:52okay then we need to set the geometry
- 11:33:55window
- 11:33:56dot geometry
- 11:34:00we're going to use an f string
- 11:34:04so let's set the window
- 11:34:07width times
- 11:34:12window height
- 11:34:17and then add plus
- 11:34:21x plus y
- 11:34:24so when we pass in x and y
- 11:34:27these cannot be floats they have to be
- 11:34:30whole integers
- 11:34:31so let's add a cast around x and y
- 11:34:38okay now this should be fairly close to
- 11:34:40the center
- 11:34:42yeah that's not too bad now when we
- 11:34:44begin a new game we should create a
- 11:34:45snake object as well as a food object
- 11:34:48snake equals snake and called the snake
- 11:34:51constructor
- 11:34:52and food equals food
- 11:34:56okay let's fill in our food class first
- 11:34:58because i think that'll be easier than
- 11:35:00our snake class
- 11:35:01so let's head to the food class
- 11:35:04now let's create an init method so def
- 11:35:08init this will construct a food object
- 11:35:11for us
- 11:35:12and we need to place our food object
- 11:35:14randomly
- 11:35:16so for the x coordinate let's say x
- 11:35:18equals
- 11:35:19random dot rand int and we need a range
- 11:35:23the range is going to be zero comma now
- 11:35:27with our game board i view it like a
- 11:35:29chess board
- 11:35:30there's a given amount of spaces so
- 11:35:33700 divided by our space size
- 11:35:37is 14 possible spots on the x-axis
- 11:35:41and then 14 possible spots on the y-axis
- 11:35:45so i need to pick one of these spots
- 11:35:47randomly
- 11:35:49so let's say game
- 11:35:53width divided by our space
- 11:35:56size so we'll get a random number
- 11:35:58between 0 and 14
- 11:36:00however this should be exclusive so
- 11:36:02let's say -1
- 11:36:05and for good measure i'm just going to
- 11:36:07add a set of parentheses around this
- 11:36:09okay and then we'll convert this to
- 11:36:11pixels
- 11:36:12so let's multiply all of this by our
- 11:36:14space
- 11:36:16size the size of each item in the game
- 11:36:19and we'll do the same thing for
- 11:36:20y but change game width to height
- 11:36:30okay we should be good
- 11:36:35all right now let's set the coordinates
- 11:36:37let's say self dot coordinates
- 11:36:41equals a list of x and y
- 11:36:45and that's it but make sure you spell
- 11:36:47coordinates right
- 11:36:50now we need to draw our food object on
- 11:36:52our canvas
- 11:36:54canvas dot create
- 11:36:58oval you can pick a square two if you
- 11:37:00like
- 11:37:01we need a starting corner that will be
- 11:37:03where x
- 11:37:05and y is and an ending coordinator
- 11:37:08so that will be x and y plus
- 11:37:11our space size the size of an object in
- 11:37:14that game
- 11:37:15so x plus space
- 11:37:19size y plus
- 11:37:22space size
- 11:37:26and you can set a fill color too
- 11:37:29fill will be our food color that we
- 11:37:32declared
- 11:37:34and i recommend adding a tag as well
- 11:37:37that'll make it easy to delete this
- 11:37:39object
- 11:37:41so tag will equal the string food
- 11:37:44now let's just test this so we should
- 11:37:47have a random circle
- 11:37:48or a square if you picked a square
- 11:37:50appear someplace at some spot
- 11:37:52on my game board cool seems like it's
- 11:37:55working
- 11:37:56let's work on the snake class next our
- 11:37:59snake class we'll need a constructor
- 11:38:06and we will set a body size
- 11:38:10equal to our body parts
- 11:38:16we'll need a list of coordinates
- 11:38:24fill this in later and a list
- 11:38:27of square graphics
- 11:38:31so that will be a list okay
- 11:38:34so we need to create a list of
- 11:38:37coordinates
- 11:38:38we can use a for loop for that so let's
- 11:38:40say for
- 11:38:41i in range zero
- 11:38:45through body parts
- 11:38:50we will take self dot coordinates
- 11:38:54and append a new list
- 11:38:59and the coordinates for each body part
- 11:39:01at the start of the game will be
- 11:39:030 0 so that our snake will appear in the
- 11:39:06top left corner
- 11:39:09now we'll need to create some squares
- 11:39:11okay so
- 11:39:12for x y
- 11:39:15in self dot coordinates
- 11:39:18and remember that we have a list of
- 11:39:21lists so that's why we're using
- 11:39:22x y in self coordinates
- 11:39:27we'll create a square equals
- 11:39:31canvas dot create
- 11:39:35rectangle
- 11:39:38so we need a starting corner that will
- 11:39:40be x and y
- 11:39:45then x plus our
- 11:39:48space size the size of each object in
- 11:39:51the game
- 11:39:53let me use the constant though space
- 11:39:57size y plus
- 11:40:00space size
- 11:40:06and let's set a fill color equal to
- 11:40:09our snake color
- 11:40:14all right and let's set a tag
- 11:40:18for convenience tag equals
- 11:40:22a string of snake okay so we have a
- 11:40:25list of squares and we can append each
- 11:40:28square into our list
- 11:40:31self dot squares
- 11:40:36dot append and pass in whatever square
- 11:40:39that you create
- 11:40:42so we have a snake that has a body size
- 11:40:46a list of coordinates and a list of
- 11:40:49square graphics
- 11:40:50now after testing this we should have
- 11:40:52the head of our snake in the top left
- 11:40:54corner
- 11:40:55and now we just need the snake to move
- 11:40:57in a given direction every turn
- 11:40:59so the initial direction is down but
- 11:41:02we'll be able to change that later
- 11:41:03so let's head to the next turn function
- 11:41:06and there's actually two things we'll
- 11:41:08need as parameters that i forgot to fill
- 11:41:10in
- 11:41:11snake as well as food and we will call
- 11:41:14this function when we begin our game
- 11:41:17so let's unpack the head of the snake so
- 11:41:19that's
- 11:41:20x comma y equals
- 11:41:23snake dot coordinates
- 11:41:26at index of zero so that's the head of
- 11:41:30the snake
- 11:41:31the coordinates will be stored in x and
- 11:41:34y
- 11:41:35so let's check to see if our direction
- 11:41:39our initial direction equals
- 11:41:42up and we'll need some else if
- 11:41:46statements
- 11:41:46we'll fill this in just a moment
- 11:41:52else if our direction equals
- 11:41:55down
- 11:42:00then left
- 11:42:06and then right
- 11:42:11okay if our direction is up
- 11:42:14then let's take our y coordinate for the
- 11:42:17head of our snake
- 11:42:18minus equals our space
- 11:42:22size so that we move one space up
- 11:42:26and then down is plus equals
- 11:42:29space size left
- 11:42:33is x minus equals
- 11:42:36space size and lastly
- 11:42:39right is x plus equals space size
- 11:42:45okay then we need to call the next turn
- 11:42:47function again
- 11:42:48for the next turn so we can use window
- 11:42:52dot after we need the time
- 11:42:56so let's say our game speed
- 11:42:59we're going to call the next turn
- 11:43:01function and we need to pass in our
- 11:43:03arguments of snake and food
- 11:43:08snake food
- 11:43:12oh and make sure you're not actually
- 11:43:13calling the next turn function inside of
- 11:43:15the after method you just have to write
- 11:43:17the function name of next turn without
- 11:43:19the parentheses
- 11:43:21okay so let's update the coordinates for
- 11:43:23the height for the snake
- 11:43:24and write that before we move on to the
- 11:43:26next turn
- 11:43:28so snake dot coordinates
- 11:43:34and we will insert a new set of
- 11:43:36coordinates
- 11:43:37after updating one of them
- 11:43:41so zero will be the index the head of
- 11:43:44the snake
- 11:43:45and we will insert x and y coordinates
- 11:43:48at
- 11:43:48this new location now we're going to
- 11:43:52create a new graphic for the head of the
- 11:43:54snake
- 11:43:55square equals canvas dot create
- 11:43:59rectangle pass in x
- 11:44:02and y for the starting corner of our
- 11:44:05rectangle and
- 11:44:06the ending corner will be x plus
- 11:44:09our space size y
- 11:44:14plus our space size
- 11:44:17then i will add a fill color of
- 11:44:22snake color and then we need to update
- 11:44:26our snakes list of squares
- 11:44:30so that will be snake dot squares
- 11:44:35and insert at
- 11:44:39index 0 a new square that we create
- 11:44:44okay let's test this one last thing that
- 11:44:46we'll need to get this program to run
- 11:44:48is that after you create your snake and
- 11:44:50food object we should call it the
- 11:44:52next turn function and pass in our snake
- 11:44:55and food object so we should be able to
- 11:44:57test this
- 11:44:59so our snake is going to move but we
- 11:45:01need to delete the
- 11:45:02last body part in our snake within the
- 11:45:05next turn function
- 11:45:07but before we update to the next turn
- 11:45:10let's delete the last body part of our
- 11:45:12snake so delete
- 11:45:14snake dot coordinates
- 11:45:18at negative index of one that is the
- 11:45:21last set of coordinates
- 11:45:24we will update our canvas canvas dot
- 11:45:27delete
- 11:45:31snake dot squares
- 11:45:36an index of negative one
- 11:45:40and lastly delete snake
- 11:45:46the list of squares
- 11:45:49at index of negative one
- 11:45:52so it should appear that our snake is
- 11:45:54moving cool
- 11:45:56we need some controls for our snake so
- 11:45:59at the bottom of our program
- 11:46:01let's bind some keys let's do that here
- 11:46:06so window dot bind
- 11:46:10let's find the left arrow key
- 11:46:16and we will use a lambda
- 11:46:20the argument is event and we will call
- 11:46:23the
- 11:46:23change direction function and pass in
- 11:46:27the word left
- 11:46:31okay then we have to do the same thing
- 11:46:33for the other directions
- 11:46:40so we have right
- 11:46:44pass in right
- 11:46:48up pass in up
- 11:46:53down pass in down
- 11:46:59let's head to the change direction
- 11:47:01function
- 11:47:03we need to access our direction global
- 11:47:07direction this is the old direction
- 11:47:12if our new direction that is passed in
- 11:47:16is equal to left
- 11:47:22and if our old direction this direction
- 11:47:26does not equal right because we do not
- 11:47:28want to go backwards and do a 180 degree
- 11:47:31turn
- 11:47:34then we will set our direction
- 11:47:37equal to our new
- 11:47:40direction and we just need to repeat
- 11:47:42this for the other
- 11:47:44directions for new direction
- 11:47:48and i'm going to change this to else if
- 11:47:52else if new direction is right
- 11:47:56and our direction is not already
- 11:47:59left
- 11:48:05then we have up
- 11:48:09if our direction is not down
- 11:48:16and down if our
- 11:48:20direction is not up okay so we should be
- 11:48:23able to change
- 11:48:24the direction of our snake
- 11:48:28sweet okay we need to eat that pesky
- 11:48:31apple next so let's work on that
- 11:48:34there's nothing else that we need to
- 11:48:35change within the change direction
- 11:48:37function so i'm going to minimize
- 11:48:39this function and head to the
- 11:48:42next turn function
- 11:48:47we'll place an if statement here if
- 11:48:50x remember that we unpacked the
- 11:48:53coordinates
- 11:48:54for the head of the snake if x
- 11:48:57the x coordinate for the head of the
- 11:48:59snake is equal to
- 11:49:02our food objects coordinates
- 11:49:08at index of zero that's the x-coordinate
- 11:49:12for our food object and
- 11:49:16y is equal to
- 11:49:19food coordinates at index of one
- 11:49:23that means they're overlapping
- 11:49:27let's take our score
- 11:49:31and increment it by one
- 11:49:36and change our label label.config
- 11:49:44the text will equal
- 11:49:48score
- 11:49:52and then i'm going to use the format
- 11:49:54method
- 11:49:55and pass in my new score
- 11:50:01let's delete our food object
- 11:50:04and we gave our food object a tag so we
- 11:50:07can just use the name of the tag
- 11:50:09to delete it and create a new food
- 11:50:11object
- 11:50:16then i'm going to write this part of our
- 11:50:18program within an else statement
- 11:50:23we will only delete the last body part
- 11:50:26of our snake
- 11:50:28if we did not eat a food object
- 11:50:32okay let's test it again
- 11:50:36so i have three body parts now i have
- 11:50:38four
- 11:50:39five
- 11:50:43six seven eight nine
- 11:50:47ten you can see that the score is going
- 11:50:49up too
- 11:50:51okay let's work on collisions next
- 11:50:53because i should not be able to go off
- 11:50:55screen like this
- 11:50:56there's one thing that we're going to
- 11:50:58change so let's add an
- 11:51:00if statement that will check collisions
- 11:51:04this will return true or false if we
- 11:51:07detect a collision
- 11:51:08and pass in our snake object
- 11:51:13if there is a collision we'll call the
- 11:51:16game over function
- 11:51:20else we will update to the next turn
- 11:51:25okay let's fill in the check collisions
- 11:51:28function
- 11:51:29and we no longer need our function of
- 11:51:31next turn
- 11:51:32actually we don't need these classes
- 11:51:34either so i'll minimize them
- 11:51:36okay check collisions so it looks like
- 11:51:39we will need a parameter of snake
- 11:51:41so be sure to set that
- 11:51:44let's unpack the head of the snake x
- 11:51:47y equals snake dot coordinates
- 11:51:52at index of zero
- 11:51:57and let's check to see if we cross the
- 11:51:59left or
- 11:52:00right border of the game if x
- 11:52:03is less than zero or
- 11:52:07x is greater than or equal to
- 11:52:11our game width
- 11:52:15then return true i think for testing
- 11:52:19purposes i'm going to print something to
- 11:52:21the console window
- 11:52:24game over let's test it
- 11:52:29okay i'm going to go over to the right
- 11:52:30border game over and our game stopped
- 11:52:34let's go over the left game over
- 11:52:37cool so we know that it's working okay
- 11:52:39let's do the same thing for y
- 11:52:43we can use else if here
- 11:52:47if y is less than zero or
- 11:52:50y is greater than or equal to our game
- 11:52:54height let's print game over
- 11:52:59and return true and we should probably
- 11:53:01test it
- 11:53:03okay i'm going up game over and let's go
- 11:53:07down
- 11:53:08game over
- 11:53:12so what if our snake touches its tail or
- 11:53:15another body part
- 11:53:17so let's say four i actually i
- 11:53:20isn't too descriptive let's say for
- 11:53:22every body part
- 11:53:25in snake dot coordinates
- 11:53:34we're going to set this to everything
- 11:53:37after the head of the snake
- 11:53:40we're going to check to see if any of
- 11:53:41the coordinates are matching
- 11:53:45if x is equal to
- 11:53:48body part at index
- 11:53:520 and
- 11:53:55y is equal to body
- 11:53:59part at index 1
- 11:54:03then return true
- 11:54:07and i'll print game over to test it
- 11:54:10print game over
- 11:54:15otherwise we can return false there are
- 11:54:18no collisions
- 11:54:20i think i'm going to change the size of
- 11:54:23the snake
- 11:54:24to 10. okay let's run into each other
- 11:54:29cool game over and the last thing that
- 11:54:32we need to do
- 11:54:33is to fill in the game over function
- 11:54:36because it looks like everything else is
- 11:54:37fine
- 11:54:38so head to the game over function take
- 11:54:42your canvas and delete all
- 11:54:49and we'll create some game over text
- 11:54:52canvas
- 11:54:53create text
- 11:54:57i would like this in the center of my
- 11:54:59canvas
- 11:55:00i'll take canvas w info
- 11:55:04width divided by two comma
- 11:55:08canvas w info height
- 11:55:12divided by two i'm going to put some of
- 11:55:14this on the next line
- 11:55:16for readability i'll set a font
- 11:55:19pick whatever font that you want
- 11:55:26we'll need some text
- 11:55:30text equals game over
- 11:55:36pick a color red's decent
- 11:55:40i'll add a tag too for convenience game
- 11:55:44over and that should be it for the game
- 11:55:47over function
- 11:55:49i'm going to change the body parts of
- 11:55:51the snake back to what it was originally
- 11:55:53and we should probably test that game
- 11:55:55over screen
- 11:55:58all right it appeared okay so let's
- 11:56:01change some of these settings around
- 11:56:03you can create a larger game board let's
- 11:56:05say
- 11:56:061000 by 700
- 11:56:11that still works you can slow down the
- 11:56:14speed or speed it up
- 11:56:16so 100 will be about half as fast
- 11:56:19it's going pretty slow now but what if
- 11:56:22we set it to
- 11:56:2320.
- 11:56:28you can change the space size
- 11:56:32so everything is a lot smaller now
- 11:56:35including the food
- 11:56:36object
- 11:56:40you can change the body parts what about
- 11:56:4220 to begin with
- 11:56:46that's fairly excessive
- 11:56:50i'll change that back to three you can
- 11:56:53change the snake color
- 11:56:54let's say we would like a blue snake
- 11:56:57so that is for a hex color
- 11:57:01four zeros and then two f's
- 11:57:05you can change the food color so let's
- 11:57:07say we would like a yellow food
- 11:57:09object so that would be four f's and
- 11:57:11then two zeros
- 11:57:14kind of resembles the python logo that
- 11:57:16color scheme
- 11:57:20you can change the background color too
- 11:57:23so let's say
- 11:57:23all white that would be six apps
- 11:57:28i do not like that go back delete delete
- 11:57:32delete alright well
- 11:57:33that should be everything let's run this
- 11:57:35game one last time
- 11:57:46[Music]
- 11:58:14[Music]
- 11:58:44well everybody that is a very basic game
- 11:58:46of snake for beginners i will post all
- 11:58:48of this code to the comment section down
- 11:58:50below but yeah that's a basic game of
- 11:58:53snake
- 11:58:53using python hey you
- 11:58:57yeah i'm talking to you if you learned
- 11:58:58something new then help me
- 11:59:00help you in three easy steps by smashing
- 11:59:03that like button
- 11:59:04drop a comment down below and subscribe
- 11:59:06if you'd like to become a fellow bro
- 11:59:11[Music]
- 11:59:59you
About this transcript
This page contains the full transcript of Python Full Course for free ๐ by Bro Code, generated from the public captions YouTube serves with the video. The transcript has 111,769 words across 19,359 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator โ no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.