hello everybody and thank you so much for joining me in this course my name is heath adams and i'm going to be your instructor for the next three or so hours so a little bit about me i am the owner and ceo at tcm security we focus on cyber security and primarily what is known as ethical hacking so organizations pay us to hack into them and try to find vulnerabilities before the bad people do and a lot of that does involve using coding and being able to run our own scripts and manipulate code and use
that in exploitation and so i have taken my passion for teaching and put that into this course i've taught a lot of courses before i've taught python before but i've never taught a full introductory level python course and i love to give away education for free on youtube so here we are in this course if you're interested in learning more about cyber security or educational training in that space we have a ton of free content on our youtube channel and we also have our tcm security academy which you can see the link there below as
well we've got 12 or so courses at the time of this video ranging from entry-level ethical hacking courses to python courses for ethical hackers to malware analysis and forensics courses and some really cool stuff out there so if you're like me with a passion for coding and cyber security it's really the best of both worlds so please do consider checking out the sites but most importantly please do consider dropping a like and subscribing to this channel we are really trying to push and make it towards a million subscribers and we are well on our way
and i would love to have you as a subscriber if you find the content worthy so with that brief introduction out of the way let's go ahead and get started with this course so briefly here's what we're going to cover it's a lot of topics that we're going to go over in the next three hours we're going to talk about strings and variables and methods and functions and boolean expressions and different types of looping for loops while loops dictionaries lists sockets we're going to build a port scanner we're going to talk about classes and objects
and how to take user input and how to read and write files and all this fun stuff and maybe none of this makes sense to you so i'm not going to really harp on it right now but hopefully by the end of all this all these things will make sense to you so let's go ahead and move into installing the materials that we need in order to get python running and then we're going to start coding pretty soon all right so in order to run python we need to download it now if you're running on
linux or a mac chances are python's already installed for you so you should double check that before going out and downloading it but if you go to python.org and you go to the download section here you have the option to download it here so i am running on windows it automatically detects what your operating system is and you could just come in here and quite literally just hit download python so i've done that it downloaded an executable for me you actually have the options here for windows linux mac and other so depending on what operating
your system you're on then that is what you're going to run in this instance all i have to do is open up the executable that i downloaded and i'm going to come in here for windows and i'm going to say install the launcher and add python 3.10 to path and i'll just say installed now and that will install for me and it's going to ask me if i want to allow this to make changes i'm going to say yes you're going to follow very similar instructions if you're following along on a mac or other operating
system now i am going to show you how to set this up but throughout this course i'm actually going to be running on linux because that's how i prefer to use python but i'm going to show you how you can operate this without having to download a virtual machine or how to do it the way i'm doing it i'm just very familiar and used to running it in the way that i'm going to be showing in the course but you're going to be able to follow along with the instructions that i'm setting you up with
as well so while this is setting up you have a few options for text editors or what are called ides so an ide is an integrated development environment and it basically allows you to code in an environment that is intuitive and can auto auto-complete your code for you and can detect errors in your code it has a lot of great functionality automatically built into it because it's made for coding so pycharm is a great ide you can go to jetbrains.com forward slash pycharm or just google pycharm and go to the downloads page again there is
a windows mac os and linux options all you got to do is come here and download the community edition for this and then you're going to run that as well now my setup for python was complete so we're good there i'm going to go ahead and install pycharm that i've downloaded and i'm going to talk about a couple of other options as well so you do not have to use an ide and in this course i'm not going to be using an ide i'm just going to be using a text editor i'm actually going to
be using a text editor called mousepad which is colorful it does okay it's not too bad but i'm going to be using that but i would recommend using an ide if you can when you're going through this installation for pycharm by the way all you have to do is just click next and then when you come in here you can create associations if you want so like you can do dot pi associations which i do recommend you can create a community desktop or shortcut here for the community edition and you can add the bin to
the path i think that you should do that as well and then you just hit next and install and just let that run now these other text editors like sublime sublime's a great text editor very pretty it's got good colors it's just not an ide so it's something that is not intuitive but it still can detect that you're writing in code and is very very visually pleasing um another one is notepad plus plus i use this it's only for windows but i think it's pretty decent and then i'll show you my environment really quick so
when i'm running in linux and this is our final project that you're looking at here but when i run in linux i run like this and i just run a mouse pad and i just come in here and i type things out it's still very color organized and color coordinated so i can see where i'm making mistakes at but again it's not an ide so i will come in here i will open this up and then i will execute the code within this area so as you follow along and you see me typing and executing
code i'm using two specific areas to do that i'm going to show you how you can do that all in one location with pycharm so you might need to reboot your computer i'm going to go ahead and say i'll manually reboot later all right so now pycharm is opened i did not actually need to reboot by the way so we can go ahead and come in here and just hit new project and you can select where you want your project to be located and you can select your environment i'm just going to use the virtual
environment make sure that you're using your base interpreter of python 3 currently run 3.1 and so all of this looks pretty good i'm going to go ahead and just hit create all right and it's going to create this for us and now just to make sure this is working you can see in here that we have a main.pi project if you want to come in here and use this you can just right click say new and file and then you can just name the file what you want like you can name this test.pi and when
you want to run your file so once you save your file and you want to run it so let's say this main here all we have to do is come up here and hit run run main and down below it will say what it's doing so you can see it says hi pycharm we had a print statement in here that says hi pycharm so this is doing exactly what we wanted to do so as you follow along you have the option to follow along in here if you see me create a file just create a
file if you see me run the program all you have to do is come in here and run the program now if you are familiar with linux or familiar with mac and you want to run it the way that i'm doing it that's perfectly acceptable as well so just as a heads up don't freak out just because i'm in a different environment it's just what i'm the most comfortable with but the pycharm ide is probably the best way to go if you're a brand new beginner because it's all in one you don't have to do
anything back and forth or use any other operating system you're not comfortable with so i'm giving you the flexibility of options here so from here we're going to go ahead and get started we're going to get into coding and we're going to have a really great time so i'm so excited to get into this so i'll catch you over in the next lesson as we start working with strings okay on to our first lesson the first thing that we're going to cover is going to be strings and in order to do that i'm going to
go ahead and make a directory on my linux machine you can make a folder in your mac or your windows machine if you're using those operating systems i'm just going to go in here and just say make dir python and then i'm going to go ahead and go into that directory so from here i'm going to go ahead and make my first script so i'm going to clear my screen with ctrl l and i'm going to say mousepad and i'm just going to call this script first dot pi i'm going to give it the ampersand
here at the end that's just going to allow me to open up this process of mouse pad and also have the terminal available to me when i need it so you'll see why i do that here in a little bit now if you're not on linux that's okay you can use something like notepad plus plus or code runner for mac which we showed earlier in this course so from here what i'm going to do is i'm going to go ahead and enter in a shebang which is a hash and exclamation forward slash bin forward slash
python3 and this is important for linux here because we are calling out the directory if you're familiar with linux what's happening here is we have two options we can come in here and we can say python3 and we can say first dot pi and that will execute our script or in theory we can come in here we could just do a dot forward slash first dot pi and run it that way if we run it like this our script will not know where to go unless we declare that so up here we're giving the shebang
forward slash bin forward slash python3 that is where python 3 is located in this machine so we'll come up here and it will look for this first and say okay i'm going to execute this based on python 3. so that is a little bit of a linux nuance here and something you should know in case you are ever coding in linux or an environment that is similar like unix which macs do run off of so from here if you've ever taken a coding class the first thing you do in every single coding class is what
is known as hello world so we're going to print out hello world and print it to our terminal so in order to do that we're going to go ahead and do something like this we're going to say print and then we'll do a parentheses and we'll say hello world i'm going to go ahead and save this and if we run this and i'm going to run this with python3 if we run this python3 first dot pi you'll see that it says hello world now one thing that we can do in here is good practice for
being a developer is add some notes in so we're going to add a comment in here we're going to do a hashtag or a pound symbol and we're just going to say print string now when we run this again this isn't going to print out comments are just for us inside of the script and i can prove that to you by saving this and running it again you'll see all we have in here is hello world perfect so comments with a hash are just meant for whoever is reading your script or your code and that
could be you or bad scenario but what if you get hit by a bus tomorrow you're the main developer and nobody knows how to read your code because you didn't leave any comments that'd be very bad so good coding practice suggests to leave good comments and make sure that people can come in and read your code and understand what is going on in here so we're going to add comments along the way so that way we understand what we're doing and practice good habits as well so what you're seeing here is what's called a string
you're seeing a string in quotations and you can see a string printed out with single quotes as well so we can just do hello world i'll add the exclamation for consistency if we save this and print this you'll see that it also prints hello world so it really doesn't matter if you have single quotes or double quotes until we get into more advanced strings and we'll talk about those a little bit later on in the course but for now just know that you can use single quotes or double quotes when printing out a string another
thing that we can do with strings is print on multiple lines so if we say print and we do triple quotes like this we say this string runs and then we hit enter multiple lines with an exclamation and and add in three more double quotes there and close this off we can add a note here as well that says triple quote for multi-line and if we save this with ctrl s and we run this again you're going to see this string runs multiple lines awesome another thing that we can do is concatenate strings so we
can print out something like this string is and now we're going to add a space at the end of it make sure you have a space right here we're going to do a plus sign and then we're going to say awesome just like that and we can say we can also concat it's always hard for me to spell that what's going to happen here is it's going to take this string plus this string when it prints out so the reason we have a space is it's got to have the space because there's no space here
and this won't just add a space for us even though in our heads we might want it to we're going to go ahead and save this print it out one more time and you can see that it concatenated and said this string is awesome now one last thing we can also print out a new line so we can just do something like this and we can use a single quote this time just to do a proof of concept do a backslash n like this and this will print out a new line and we'll get familiar
with that later on as we get into functions we'll write our own function to make a new line but for now if we wanted to add another line we could and then we can just print test that new line out and let's see if it actually gave us a line printed in between this concatenation and this new print statement here let's go ahead and save that print it and you can see there is a line here so the backslash n will print out a new line for us and that is all we need to know
for basic string so we're going to go ahead and move on to math you can go ahead and leave this open we're going to run through this as we go and then you'll have a long script which will also make for very good notes let's go ahead and move on to math so math python actually has a built-in math interpreter so we can do a bunch of fun stuff with python and with math that's automatically built in so i'm going to go ahead and just call this section math and if you want to like keep
track of sections you can come up here and just call this strings and just capitalize it like that and then that way we kind of have an idea of where we're at you can even add an extra line here if we want to so from here let's go ahead and play around with math just a little bit so if i wanted to do a print of 50 plus 50 we can definitely add we can also subtract so if you want to do 50 minus 50 you could do that and i'll give you some time to
catch up here in just a second i'm just going to do a few print statements then we'll take a look at them and then we'll move on to some more math as well we can also do 50 times 50 and we'll just use the little star or the asterisk we'll just say multiply and we'll also do 50 divided by 50. we'll just say divide so if we save that you could take a second to catch up you could see we are just doing simple math here we should expect a hundred we should expect zero we
should expect one here and 2500 if my math is mental math is correct so i'm going to go ahead and print this out okay and we've got 100 0 2500 and 1.0 so this 1.0 is a little bit different this is what's known as a float everything else that has come out so far is just an integer and we'll talk a little bit about that here in just a few minutes but know that integers are on the left hand side and if we don't have a decimal point here we're looking at integers and on the
right hand side when we see anything on the right hand side of the decimal we're looking at what is called a float that becomes very important depending on when we need it for now we're going to keep worrying about math we'll get into integers and floats in just a little bit so another thing that math built into python can do is it can do pemdas if you've never heard of pemdas please excuse my dear aunt sally so parentheses exponents multiply divide add subtract you may have had something different within your educational experience but it will
do that for you so if you do 50 plus 50 minus 50 times 50 divided by 50 it will give you the answer i don't know that mental math off hand another thing that we can do are exponents so we can print say 50 to the second power which is same thing as 50 times 50 which would be 2500 as well and we can get exponents out of this and we'll actually go ahead and save this and run this here because the next stuff i want to be able to lay out clearly so you can
see that we have successfully done the math here did it for us awesome and it also did the exponent for us as well so now a couple of weird things with division so you saw the 1.0 before well we have a few things that we can do for example the 50 divided by 6 but using a percentage sign is what's called a medullo and that it just takes what is left over so 50 divided by 6 is not divisible there is a leftover so if we print that you'll see we get a leftover of 2
which makes sense because 6 goes into 50 8 times 6 times 8 is 48 and then we have 2 left over so if we want to know what the remainder is we can use a percentage sign here we can also do something like 50 divided by 6 like we saw earlier and this will have division with remainder so we can say or or float we'll save that and take a look at that really quick you can see we get 8.3 and then it rounds up eventually to a 4. and lastly what if we wanted no
remainder so if we just did 50 divided by 6 we say no remainder we could do this and we should get 8 here so depending on the situation and what we want we might want a situation where we only want the integer or we only might want what's left over or perhaps we actually want the whole number of what's being divided so there are a few different ways to divide with math in python and it's good to know all of them depending on the situation that you run into so for now just take notes on
this and know that there are a few ways to do math and division and you can do pemdas and exponents and all kinds of fun stuff and this is just scratching the surface of math with python so that's it for this lesson we're going to go ahead and move on to the next one which is going to start covering variables and methods so i'm going to come in here and i'm just going to call this section variables and methods and if we want to make our script a little bit clean we can just come in
here and copy this new line it'll be a lot easier once we write a function for this but we haven't gotten there quite yet so we're going to print out this new line and that way when we print this out you can see that we have kind of gaps in between the sections that we're working on so now we're going to work on variables and methods so variables variable means something can vary it can change and we're going to look at that here in a second so if we have something like quote and we make
quote equal to all is fair and love and war and this is what this is a string we are storing the string inside of this variable called quote and we can just come in here now we can print out quote and if we do that we should get this returned back to us you can see all is fair in love and war if we didn't print this just go ahead actually we'll just comment this out this is a great way to test the script if we just save this now with a comment and we hit
enter there's nothing here we haven't given it any instructions to actually print out so now we can print this out and it's printing whatever is stored inside of that variable in this instance we have a string stored in there now when we talk about methods methods are just functions that are available for a given object now you could think of functions as something built in to python that allows us to do something and it'll make sense here when we get into using methods so we have this quote all is fair in love and war well
what if we wanted to print this in a few different ways we can use methods to do that for example we can print quote in uppercase and just do upper like that and this is going to make it all uppercase because we're giving it this period upper and then open close parentheses right here and that is allowing us to use that method so we can say uppercase and we can also print in lowercase if we do lower and just for one more we can also do print and we'll say quote dot title and this is
what's known as title case and title case will capitalize every single letter like it is the title uh in this instance here we're going to get like the a capitalize which wouldn't really be true in a title but still just know that title case is meant to capitalize every first letter within your string or your sentence in this case we're going to call this lowercase here okay so let's go ahead and save this and we're going to print this out and you can see the differences that we have we have it here and it's normal
quote and then we have it in uppercase all lowercase and then every single first letter capitalized perfect these are methods these are just a small example of methods we're going to get into more a little bit later here's another example what if we wanted to print the length of quote let's say we wanted to know how many characters were within this sentence so this is going to count the characters and this will also count spaces so we're trying to get a total count of what's going on inside this string we print that you'll see that
we get 28 return so if you want to check that you can go count every single character within here and you'll get the length of that and that could become important as you're doing python later on and this is just yet another example of a method now let's go back to looking at variables and why they're called variables so let's say that we have a variable of a name and in this instance i'm going to use my name and i'm going to make it a string and i'm going to use my age i am 33
and here we're going to say string just so we can make sure we notate the differences this is an int or an integer and we're also going to give a gpa let's say i went to school i'm going to use the american rating system let's say i had a 3.7 gpa and this is what's known as a float and that has a decimal so make sure you notate that so if we print these out we print out the integer of age that'll still print 33. what if we print out the integer of 30.1 save that
take a look and you'll see that we just get 30. all right and you can see that this rounded down again integer just the first number just what's on the left side of the decimal point doesn't care about the right side what if we printed integer of 30.9 will it round will it round no so if we save that and we print again you'll see it's still 30. it doesn't care what's on the right side of the number it's only going to take what the integer is so anytime you print an integer you're only going
to get the first or what's on the left-hand side of the decimal point so make sure you know that now a few cool things that we can do let's say that we wanted to print something like this if we print my name is with a space and then we'll do plus name and we'll say space again and i am space age plus space years old make sure you have your spacing in there properly that way you can account for the spaces before and after these variables but what are we doing here we are concatenating variables
now if i try to run this i'm actually going to get an error run it we're getting a type error and this is a great example by the way of understanding what python is telling you i get emails all the time from students saying i don't understand why my script isn't working and the first thing i'll ask them is i will say did you read the output of the error because it tells you it says hey line 44 like ctrl tab look this is line 44 it tells you where your mistake is and then it
also says and only concatenate string not int to string so if you didn't know what this meant and maybe you don't you can copy this go to google and search it and i promise you somebody will have had this error before as much as if we wish to be special there's not many situations that will come up where there hasn't been this error or an issue that we run into that somebody hasn't already had before so with that in mind we can fix this so it's saying that it can only concatenate strings not integers well
name is a string but age is an integer so we need to make in this situation age a string so what we're going to do is just say str like this and give it like that and now if we print this save it print it you can see my name is heath and i am 33 years old perfect now what happens if i am a year older well i could take age and do something like plus equals 1 something like that and if i print age now hopefully we are seeing 34. you can see 34.
now this is what comes into play this is a variable variables can change at this point in the script we define that we are 33 and as we run through it i am still 33 when i print this statement out however i've had a birthday now age has increased by one printing age out now will say that i'm 34. so variables can change variables can store different numbers at different times that's why they are called variables because they vary now we could also do something like birthday and set birthday equal to one and i could
say age plus equals birthday and then print age and guess what now it's going to return 35. so just know that you can store a number within a variable we've shown that before already we can add two integers together and we can print them out so we're adding those together and it's taking that total and adding that to age and now it's saying we're 35 years old so that is it for this lesson we're going to go ahead and move on to functions and fix this new line issue that we've had plus write some pretty
cool stuff out and build some other cool functions out as well so i'll go ahead and see you over in the next lesson all right moving on to functions so let's go ahead and print out a new line really quick and we're going to call this functions now i like to think of functions as mini programs what they are is an organized block of code that you define and then you can call it later instead of repeatedly typing the code out we're going to take a look at some examples of that so let's go ahead
and write a function so let's do a define def and that's how we start a function and in this one i'm going to say who am i and i'm going to do closed parentheses and here we're going to say this is a function without parameters and we'll talk about that here in just a little bit now indentation is incredibly important in python if we do not indent we will throw an error here and you're going to see a lot of situations where indentation becomes incredibly important it's one of the most important things in python python
does not have a lot of rules that apply to it but indentation is one of them that you must follow so we're gonna go ahead and hit the tab to indent and i'm going to say my name is heath and this is what is known as a local variable we'll talk about that here in a second as well age is equal to 30. and we're going to go ahead and print same thing we did before we can actually just kind of copy this up here we'll say my name is just like this okay copy all
that paste it and then add the closed parentheses there i'll let you catch up and then we can just call down here who am i so what's going on in here well we have a function that we're defining we're calling it who am i and it has no parameters we'll get into parameters here in just a second now we're giving it some items within this in this instance what we're doing is we're giving this local variables we're saying hey in this instance when we have a variable of name it's going to be heath and the
age is going to be 30. well whatever is stored in here is only stored within the function that's why this is a local variable if we print age as well we'll see that so we know that age was 35 we called age over here as 30 and let's see what happens first of all we called our function here we said who am i called that and all it does is run our mini program our mini program says hey i'm going to print out these variables into this string here okay and that's exactly what it did
but when we printed age again you can see that we're actually still 35 so this variable or the variables that are within a function are local and do not apply outside of the function so make sure to remember that okay so i'm going to delete this print statement we don't need it and we're going to write some more functions so let's do one where we add some parameters so we're going to do add 100 and we're going to give this a parameter of num num and that's just going to stand for number okay we're going
to indent make sure we indent and all we're going to do is print num plus 100 so when we call our function we say add 100 just like last time this time we have to actually give it what is called an argument so our argument goes with our parameter here when we're calling it so our argument is going to be a hundred and when we print this out this is going to be 200 hopefully so we'll save this run it and you can see that we got 200 because we're doing print 100 plus 100 that's
all we're doing this mini program okay let's try another one let's say we want to have multiple parameters let's do define add and we're just going to add x and y this one's going to be easy we're just going to say print x plus y and now we can add whatever we want so let's add 7 and 7 and 1 will take the place of x the other one will take the place of y x plus y 7 plus 7 should be 14. let's go ahead and run that you'll see we get 14 here beautiful
all right let's make it a little bit more complicated then all right let's try adding in here another function this time we're gonna do multiply we're gonna do x and y again and instead of doing a print i wanna show you something a little bit different let's say we just return x times y now in this instance if we multiply 7 times seven do we get 49 we don't okay there's a reason for this this is just calling back so when this is saying return this isn't saying print remember this says print this is going
to return x times y so we can call this here this function and it knows that this function is equal to 49 and perhaps we can put that into something else like we can put that maybe into a variable or whatever it is we might want to do with it however if we wanted to return to the screen we actually have to call that so sometimes we're going to actually store something instead of printing it we do a lot of printing in python tutorials just so that you can see it on the screen and make
sure that you're seeing what you're doing and that everything's printing out okay so that's why we're doing this but please understand that the return option does exist and is used quite frequently so i'm going to save this and run this you can see now that we get 49. perfect okay two more let's say we want to do a square root okay so let's define square root and we're just going to give it one parameter and in this instance we're going to print out the square root so we're going to take x and a square root
in exponent format is just to the power of 0.5 that's all we're doing and we can test that theory by doing something like square root of 64 which we should return as 8 because 8 times 8 is 64. so if we save that come in here print it and you can see we get a float of 8.0 now i told you we would create our own function for a new line and we're going to do just that let's go ahead and define new line and we could call this like new line if we wanted to
or whatever but i think to make it simple as long as we understand what it is we can make comments and notes in here if we wanted to we could just say define nl and then we can just say that when we do that we're just going to print out our n for our new line and then we call a new line we'll get one very simple so we can just even say in here new line okay so now we know and we save that and we can print it you'll see it it's here but
we really don't have anything after it to really show it but we do have that space there so that is it for this lesson i'll go ahead and see you over in the next one next up we're going to learn about boolean expressions and also relational and boolean operators so let's go ahead and type in here boolean expressions and when we think of these we can just think of this as true or false that's really what it is so from here let's do a few variables so we could set bull one equal to true we're
just going to say hey this variable's true bull two we could set equal to three times three is equal to nine so the double equals means that something is equal to something so we have three times three that's 9 equal equal that means that equals 9. do not confuse that with setting up your variable this is saying hey my variable is equal to this but this is saying this is equal to this number and that is a true statement we could also say bool 3 and make that false or we can give it a statement
that makes it false so something like 3 times 3 does not equal 9 exclamation equals means does not equal 9. now if we print it out bool 1 bool 2 full three and bowl four just like that and save it and if we come in here you'll see that it says true true false false again boolean expressions is something true is something false why do we need to know this well we need to know if something is true then we might want to continue on or if something is false we might want to do something
and we'll get into that when we get into conditional statements like if something is true then go ahead and do this if something is false do this or there's something called while loops which will allow us to continue as long as something is true we're going to get there when we get to conditional statements as well or looping i should say and we'll talk about that but that is why we might want to know these things among other stuff we might want to know if something's true or set that parameter or that variable to true
until it becomes false etc etc so right now just understand that boolean expressions are true or false and we're going to come in here and we're going to print and we're going to say type and we're going to do bull okay and this will tell us now this is a nice thing so if we come in here and say type it'll give us the class this class is boolean awesome and another example of that is if we say like bool 5 is equal to true and we print the type so if you see true and
you're like i wonder what that is is that a boolean or is it actually just a string we come in here and say bool five we save that save that there and then you should see that that class is actually a string here so you can check with the type to see what is this okay so the type feature is very nice and we can see that we are dealing with a boolean here and this one is actually a straight so please note that if you put it in quotations that makes it a string you
leave it like this that makes it a boolean expression let's go ahead and make a new line and we're going to talk about relational and boolean operators okay so we can think of that as let's make a variable here we say greater than we say 7 is greater than 5. well that would be true and we are using an operator here so the operator is the greater than sign we're also using a boolean expression because it's going to result in a true return if we were to print that out now we can do a few
other things we can say like less than is equal to five is less than seven we can do greater than or equal to which would be seven is greater than or equal to seven that's true we can also do less than or equal to and in that case we can do 7 is less than or equal to 7 which is also true so all of these statements here are true now we can get into some other type of operators and statements here what if we did and so let's just call this test and we're going
to do test and we say seven is greater than five well that's true and five is less than seven that's also true both statements are true thus this is true do another one test and two and for some students this does take a little bit to get your head wrapped around i'm going to provide a table for you here just in a second but just follow along and i'll explain this as we go let's say we have seven is greater than five and five is greater than seven all right let me fix that syntax five
is greater than seven this now becomes false why well seven is greater than five and five is greater than seven that's not true so because it's not true because both statements are not true this becomes false there's an alternative to this what if we had an or we said or so seven is greater than five or five is less than seven well both statements are true thus this is true but we can have the same situation as before where we have seven is greater than five or five is greater than seven and guess what this
statement is actually true because all we need is one condition to be true for this to be true so in this situation this is true or if this is true then the whole thing becomes true so just because this is false this would also have to be false for this to be false hopefully that makes sense one other thing here before i show you the cool little table is we could do test not so if we said something was not true guess what that becomes false same thing if we said it was not false then
it's true not is just the opposite so not true in this situation is false now if we go out to the internet and we go to google we could come in here and we can just search something like python truth table and if we go to images we should be able to find one that works out pretty well in our favor here's a good example right here so if you ever take a coding class you might be quizzed on this this will tell you truth tables right here so if it's not false it's true it's
not true it's false we can look at or statements so true or false is true the only situation an or statement becomes false is when both are false now the and we looked at that if it's true and false it's false if it's false and true or false and false it's still false only situation for and where it can be true is when both are true there's also not or not and there is does not equal or equals so it's good to know these types of truth tables and you can come and just again google
python truth table and come look at this pretty straightforward once you get the hang of it but it's completely okay for right now for it to be confusing just wanted you to know that these are out there and exist in case you are a little bit confused by this lesson so we're going to go ahead and move on to the next lesson which is going to cover conditional statements okay we'll come in here we'll do a new line and we'll just say conditional statements and we can think of this as if then or if then
else or if else either way you want to write this this is fine so think about this if you go to the store and you want to buy a drink and that drink is two dollars if you have two dollars or more you can buy the drink if you do not have two dollars you will not be able to buy the drink so let's take a look at that so if we write a function and we call it define drink and we give it a parameter of money we come in here and we say if
money is greater than or equal to two then we're going to return look at the indentation again we need an indent here after our function and we need an indent after our if statement so make sure you're indenting correctly return you [Music] got yourself a drink and here we come back we can say else we're going to return no drink for you kind of like seinfeld no soup for you all right so if we come in here and we print drink of three meaning we had three dollars at the time and we print drink of
one meaning we had one dollar at the time we're gonna have two different results if we have two or more dollars we're going to return you've got yourself a drink otherwise if we have less than two dollars we're going to say no drink for you let's save that you've got yourself a drink when we print out three dollars because we had enough money here you could see with one dollar we did not have enough money thus no drink for us okay let's take a look at a little bit more of a complicated one we're going
to come in here we're going to say define now i'm going to make this alcoholic beverages if you do not drink that's okay you can just follow along still you can change it into whatever you want to be i think this is a good example because we have a couple parameters and we can use those to make different sort of conditional statements so let's define alcohol now in the united states we have two things that we need in order to purchase alcohol one we have to be old enough we also have to have enough money
similar to the drink before that was not alcoholic we assume well we have to actually be old enough to purchase this drink in the united states you have to be 21. so if our age is greater than or equal to 21 let me put a space here by the way sorry and uh look the and is coming into play money is greater than or equal to five then we're going to return we're getting a drink all right now we're going to say else if because there's a few situations that we can be in there's actually
four situations here so we're going to say e-l-i-f which stands for else if what happens if our age is greater than or equal to 21 and we don't have enough money so we say money is less than five well in that situation we're going to return come back with more money okay and then what if we have another situation where we say age is less than 21 and we actually have money we're just an underage person trying to buy a drink and we say five dollars well we're gonna return nice try kid and then lastly
if we have no monies and we're not old enough well then we can just return something like you're too young and too poor okay and then let's try these out so we've got these different situations here again if we have our age and money we meet both criteria we're getting a drink otherwise if we meet the age but no money let's come back with more money we don't meet the age but we have the money nice try kid and then lastly if you don't have the money and you're not old enough you're going to be
too young and too poor so we're going to print out a few different statements here of this function so let's give it 21 and 5. we'll print alcohol again of 21 and 4. print alcohol of 20 and 5. and these should all meet different criteria alcohol of 20 and 4. so we should expect to get these in order we meet what we need here we don't we don't we don't different criteria for each of these we'll save this go ahead and run this you can see it says no drink for you actually that's the last
one uh we're getting a drink come back with more money nice try kid and you're too young and too poor so you can see that we can make conditional statements based on everything that we're starting to put together here we have now used a function we've used multiple parameters we've used the conditional statement we've used relational operators okay we've got the boolean right this is true and true we're returning something this is true and true in this situation then we're going to return something else so we have different situations that can come back so we
need to meet those criteria and we're starting to use that look we're using the return instead of the print feature there's a bunch of different things that we're doing here that's all starting to tie in and hopefully it's starting to make sense why we do things we're starting to build upon it and it's starting to get a little bit more fun so from here we're going to go ahead and move on to lists and move on with our journey into python so i'll see you in the next lesson let's move on to lists so let's
go ahead and do our new line and we'll call it lists and the best way to think of lists in my opinion is that they have brackets something like this now lists are data structures they are changeable we can reorder them they are basically just a group of elements now everything within a list is called an item and as i mentioned the best way to think about lists is that they have brackets just like a string might have quotes around it this will have brackets around it now again we need to remember these are changeable
we'll talk about something here in a little bit that is not changeable which is called the tuple now here let's go ahead and declare a list we're going to say movies and i'm just going to list out some movies that i like and we can do a bracket and i can say when harry met sally we'll just give these as strings i like the hangover and i like the perks of being a wallflower now you can add whatever you like here and my hangover here by the way that i have to hang over here my
hangover on the line here is just because my notepad here is not wide enough if i made it wider it would actually just continue on so i don't confuse this if i hit enter you'll see it goes to 148. so my wrapping does not mean i went into a new line and lastly we'll just say the exorcist and we'll close the list off with a close bracket so if i wanted to print the first movie in the list i wanted to print when harry met sally i come in here and i print movies and i
wanted to say one do we think that would print the first item in the list go ahead and hit save print and you're going to see that it actually prints the hangover so what am i doing here i am calling an index and our index actually starts with the number zero so when we look at an index we need to think of it as zero one two three always count starting with zero not one otherwise you can return the wrong numbers and this can get very confusing let's make sure we add a note to this
returns the second item in the list okay we're looking at the second item with the index of one if we printed for example movies and we gave it zero then we would see that this will return the first item in the list okay we'll save that take a look real quick you could see that the first item is when harry met sally and that's exactly what we're getting returned which is what we want now let's look at a few tricks to splicing an index or indices we can say something like print movies and if we
printed movies one to three what would happen here well what's going to happen is it's going to return the first item given so this is going to be index number one which is item two so first i should say first index number given right until the last number given it's not going to include the last number will steven say but not include the last number so that's a lot let's just print it out take a look so one to three will print the hangover starting at one and the perks of being a wallflower which is
two it will not print three okay so make sure you understand if you go one to three you're only going to include two items not three items now you can also do one to four which would print all of those if you wanted to print everything from the beginning of a number so say we've wanted to print movies one and we wanted to go all the way to the end we could just do something like this we could start from a certain point in index print the hangover works of being a wall flower the exorcist
and we're skipping when harry met sally because we're not starting at zero okay we could also do in the opposite of that movies and remember if we go to one it's not going to include one it's only going to include the first item here everything before so when harry met sally everything up until this point is another way to think about it okay so we wanted to print two items we'd actually have to add a second or put the number two here and that would include the hangover that's what we wanted to do now another
thing that we can do is if we wanted to print the very last item we could do a movies negative one and you don't need to commit all this to memory right now especially if you're not working with lists but if you ever do work with lists you need to pull specific items down from the list this becomes very important so it's important to know that indexes or indices exist you start at zero and then you move forward and then the way that you can splice these just depends on how you call these now this
will return last item in list we save that take a look you'll see it'll return the exorcist here now we can apply methods to lists just like printing length of movies will print count the items in the list right in the list and we'll save that just take a quick look i never close this sorry so you can see it tells you your mistakes and even i make mistakes come in here try it again you see that we have four items within our list all right so and that is true we have four items in
the list we can also add to the list so we can use something like movies dot append we'll add to lists and if we wanted to add the movie jaws we could and if we printed movies you'll see that this appends to the end of the list let's go ahead and save that and you'll see now that jaws is here at the end now we could also insert a movie into the list so if we did movies dot insert and we wanted to put in a specific spot so say we want to put it in
index two we could say two and then we could give it a movie hustle the movie i just watched recently and really liked and we can come in here and print movies now we save that you can see that we can actually insert hustle here into position two on the index so pretty neat now we can also remove movies we did movies.pop come in here and that will remove the last item so if we come in here add a print statement really quick save that and then print this out you'll see that jaws is now
gone we can also specify specific spots we want to remove so if we want to remove index 0 we could and then this will remove the first item print movies save go ahead and when harry met sally is now gone from our list we can also combine two lists so if we come in here and let's say that my wife has favorite movies and she definitely does and we'll just go with a couple of her favorite movies she likes just go with it and she also likes 50 first dates come in here and close that
off well we can combine movies let's say we wanted to know our favorite movies what we can do we can combine lists i should say come in here and i could say i want to add movies plus amber movies and i can print our favorite movies and then i have now combined lists let's go ahead and take a look at that hopefully i did it right and you can see now the hangover hustle parks a big wildfire exorcist just go with it 51st days you could also just come in here if you copied this by
the way and just pasted this you don't have to put it into a variable you could totally do it like that but i think storing in variables is a better practice so i'm going to go ahead and just control z here and one more thing so we can also have what are called 2d lists so two dimensional lists so let's say for example we have grades and we have a list of grades and we're going to say that inside this list we have bob and his grade is an 82 and then we have alice and
she has a 90. okay and then we've got jeff jeff's not doing so hot just got a 73. okay and let's say we wanted to pull down bob's grade we could make a variable like it's called bob's grade and we could set that to grades and then do something like 0 1 like this and what are we doing here well we're saying i want to pull from the first index okay index one or zero right first index zero one two so i'm pulling from zero and within that i want to pull the second item which
is this zero one so we have a two dimensional index here and if we went ahead and printed bob's grade you can see now if we save this we get an 82. well that's correct what if we screwed up bob's grade and we wanted to fix it well we could also do something like grades 0 1 just kind of like we called and we could just set it equal to 83 maybe it was an 83. okay we come in here and we print out grades you should see that this has changed so let's go ahead
and print this you can see now bob has an 83 so we can modify our 2d list as well so that is it for lists remember lists have brackets lists our data structures we can change lists as you saw we were able to append pop insert remove okay we can modify many different ways we're going to move on here in a second to what are called tuples and tuples cannot be changed even though they look very much like lists they are different so i'll go ahead and see you over in the next lesson when we
cover tuples okay let's talk about tuples very quickly and then we're going to move on so do a new line say tuples and these are like lists but they do not change we'll say do not change and then they get parentheses as opposed to getting brackets okay so we might want to have something that doesn't change and when something can't change we say that they are not mutable m-u-t-a-b-l-e they're not mutable so that means that they're immutable and tuples are immutable meaning we can't change them so we think of lists lists mutable tuples not mutable
or immutable so if we have a tuple of grades we can say something like grades are equal to a b c this is the american grading system d and f in here if we did something like grades dot pop like we saw before or grades dot append something like that neither of these are not going to work or neither of these will work i should say proper english what's going to happen is they're not mutable we can't change them once we have this and we store it it does not change so if we want something
that is like a list but we don't want it to change it's better to use a tuple we can set something in stone like our grade letterings and then we can call those when we need to we can just say something like print grades and give it a one and save it and we should get a b back okay and we get a b back so that's it just know the difference between tuples and lists and that tuples do not change but they are very similar in how they can function and there are pros and
cons to each of those we're going to go ahead and move on to the next lesson which is going to be looping okay so let's come in here make a new line and we're going to call this looping so we're going to talk about two different types of loops the first type of loop that we're going to talk about is what's called a for loop and four loops are start to finish of and iterate and here's what a loop looks like and we'll take a look so let's go ahead and get a list we're going
to say vegetables and you can put whatever vegetables you want in here i'm going to go with a cucumber i'll do some spinach and then we'll do some cabbage as well and now what we're going to do is we're going to come in here and we're going to write a for loop we're going to say 4 x and x could be whatever you want it to be you could say for veggies it could be whatever you want to call it in this instance i'm just going to call it x in vegetables so for x in
this list we're going to go ahead and print out x so what do you think this is going to do all this is going to do is just loop through the iterates start to finish of an iterate so it's going to say cucumber spinach cabbage go ahead and give it a go save it print it cucumber spinach cabbage all we did was go through a list iterate through it until we reached the end of the list and then it stopped so that is a for loop another good example of a loop if you want to
think about it might be trying to ping an ip address so like a 192 168 1.1 or maybe you have one dot x maybe there's a whole slew of ips that you want to ping maybe it's one through 254. okay if you wanted to ping all of those you might want to do something like a for x in ip and where i p is just equal to like a sequence like say ip is like a sequence of 1 to 254 and don't worry about this i'm just logically writing this out so if you say ips
1 the 254 for x and ip and then you want to do something you might want to like ping 192.168.1 dot x and then x will be one then two then three then four okay so you could write something like this for a ping sweeper again it's a start to finish of an iterate one through 254 with stop at 254 when the iterate is done now another example of this or another example of a loop is what we call a while loop let me say while loops these execute as long as true i should probably
capitalize true so that way it makes sense that we're talking booleans so let's think about this situation what about we set i equal to one so we've got a variable of i equal to one while i is less than we're going to go ahead and print out i and then we're going to say i plus equals 1. so what's going to happen well while i is less than ten so right now i is equal to one one is less than ten that is true we're going to go ahead and print i increase the value of
i by one so the next time it loops through it's going to be 2 and it's going to continue on until this is no longer true so if we save this this should print out 1 through 9 because we're not looking for 10 just 1 through 9. so we're going to say that and enter one two three four five six seven eight nine and it looks like the loop actually did its job so again we have two types of loops all we need to know about right now is we have a start to finish of
an iterate that's a for loop then a while loop executes as long as true we may see these later on as we do our project work so keep these in mind as we go on through the course so from here we're going to go ahead and move on to advanced strings so i'll see you over in the next lesson okay let's take a look at some advanced strings so if we come in here again we do a new line go ahead and enter advanced strings and let's create a variable of my name i'm going to
say my name is heath now let's say we wanted to print my name and we wanted to grab the first letter of my name how do you think we might do that well if you're saying index of zero you are absolutely correct and we can say in here first letter what about printing the last letter of my name well if you say negative one you have remembered your index lessons great job that's going to be the last letter we can save this and we can go ahead and print and if i made this a string
that would be very helpful go ahead and save this you can see that we get the capital h and the lowercase h here because my name starts with an h and ends with an h so another thing to note about strings much like tuples they are also immutable you cannot change a string we cannot modify the string we can join strings we can split strings and we'll talk about that here in a second but once i have set this variable in this string this string is here or ever i can always change the value of
this variable but you cannot change this string so again strings are immutable now let's go ahead and say we have a sentence and we give that something like this is a sentence if i can type today and we print out sentence with something like this what do we think that's going to look like let's save this print it you can see that we get this what are we doing well we're just grabbing this first word however we have the benefit of knowing what the first word is and how long it is so we went zero
one two three remember we stopped at four so we grab the word this well what if we were in a situation where we didn't know what the word was or how long the first word was we could use something like a delimiter so we could say print we could do sentence dot split and if we do a dot split we can just do it like this and then this is a delimiter and delimiter says we're going to take something and we're going to split based on that now the default delimiter in python if we just
give it a split is a space so we save this come in here this is a sentence now look at it it's in a list we could pull the first item if we wanted to and know the first word instead of having to split it like this or pull it out like that so we could do something like this as well this is just a proof of concept that's a little bit fun we go sentence split and we say that is equal to sentence dot split okay and then we also create another variable we call
that sentence join and we say that is equal to space we're giving it a delimiter to join on actually we're telling you what to add in between the different words that we're pulling out so we're adding a space here we're going to say join split like this and if we print out sentence join what do you think we're going to actually have happen here let's save it guess what this is a sentence so all we did was deconstruct this sentence based on delimiter it came out to this is a sentence and then we joined it
back together with a space so every one of those got a space in between and that's it so that's just a nifty little way to show you that splitting and joining is possible and again this is still immutable even though we're messing around with the strings the strings are immutable so let's take a look at a few other things here i mentioned in the beginning of the first lesson when we were talking about strings initially that we might have different ways of using single quotes and double quotes and think about this like what if we
had a situation of quote and we wanted to say something like he said give me all your money well with this situation what are we gonna do what if we wanted to add quotes here and say give me all your money we do that because that is a quote well looks like it's kind of messed up we're not seeing it in green it kind of looks funny what we can do in this situation is we can use single quotes if we wanted to or we could flip this around by the way we could use single
quotes on the outside and then double quotes on the inside if we wanted to use double quotes here and i could save this let's print out quote just to show you and then we can save this another thing that we can do though is we can say something like quote and then we can just do he said and we can do character escaping so we can do give me all your money like this and now python knows that everything within these escaped characters is ignoring it's ignoring this double quote here and ignoring this double quote
here so we're escaping that and then we can print it and hopefully we did this right go ahead and print you can see now it says he said give me all your money and double quotes while we also use double quotes on the outside so we can use character escaping if we want to just know that we'll have to use this backslash in front of each of the characters that we want to escape okay let's take a few more examples here so if we say [Music] something like a variable of too much space what happens
in a situation where we have like you can just add as much space as you want in here it doesn't really matter we have a string with a lot of space in there well we can strip this out we can print too much space and then we can do a dot strip and the strip is going to take the delimiter of a space as default and that will also strip this out you can see there's just hello there nothing else so that's kind of nice a few more items what if we have the letter of
a and we have the word of apple okay well what if we wanted to know something like this let's actually go up a little bit and let's say we wanted to know like print a and apple okay and what is this going to return go ahead and if i could type sorry and this is going to return something let's go ahead and save that that's going to return true so we'll just say true now if we print a in apple you're going to see that this is going to return false save this y well this
here is a lowercase a and that does not exist with an apple so even though we're looking for a specific letter within a word we have to match case sensitivity in order for that to return true so if we wanted to know if the letter a was in the word apple and we weren't concerned about case sensitivity then what do we do in that situation well this brings something up that we did a little bit early on which is using lowercase remember the lowercase method we can say print and then we can come in here
and say letter.lower you could also do upper if you wanted to but what we're doing is we're converting all of this to lowercase so this is now going to be a lowercase a and then we could say in word dot lower and we can just say this is an approved way of doing this so if we print that out we save this this should come back as true so think about when you're trying to match a word or a string or anything and if somebody came in and you were expecting like i don't know like
think about school and we're thinking about uh a word maybe like manhattan i don't know and somebody types in manhattan like this okay we still want to be able to accept that answer is true even though they may have screwed up a little bit on the punctuation now there could be instances where we want a capitalized letter and it has to be very specific in that case this kind of syntax works if we're looking to just accept an answer like manhattan for manhattan then we might want to consider using lowercase or uppercase in that situation
just to match those words or letters or whatever it might be so one more thing let's look at what is called string formatting so if we do something like movie we'll pick the hangover again we say the hangover you can pick whatever movie you want here there's a few ways that we can print this out remember we concatenated early on and we said like print we did something like my favorite movie [Music] is space plus you know movie something like that right well we can actually write this in a certain way that is a little
bit easier so there's a few things that we can do for example we can do my favorite movie is add a couple brackets in here like that and then we can add period at the end just because we're going to close our sentence we'll say dot format and we'll say movie and close that off that's one way of doing it if we save that you can see my favorite movie is the hangover okay this is called using the string format method now what we can do there's another type of method we can use is we
could say print my favorite movie is percent s and we say percent movie just like this save that this is using percent formatting and look the same thing happens i just forgot my period in there so no big deal lastly if we print out an f in the front this is called an f string or what's known as a string literal we can do my favorite movie is and then just do a movie like this super easy and we have moved off of using the format method and the percent method into this f string or
string literal let me come in here and save this tab up print and you can see my favorite movie is the hangover so that worked out perfectly well you can use any of these to format but just know that the f strings are the latest and the greatest way of doing it in python 3. so that is it for this lesson we're going to go ahead and move on to dictionaries okay so we'll add new lines in here and we'll say dictionaries and we can think of these as key value pairs and also think of
these with the curly braces like this so we've seen lists like that we've seen tuples now we can think of dictionaries with the curly braces so let's go ahead and we're going to put some drinks and again i'm going to use alcoholic beverages you do not have to do that you can use whatever drinks in prices in here that you want but we're going to have a key value pair so our key is going to be the drink so first drink i'm gonna pick up is a white russian and the value is going to be
seven let's say that a white russian costs seven dollars if we do an old-fashioned we're going to say that this costs ten dollars and we'll do one more we'll do a lemon drop and we'll just say that this costs eight dollars okay and we can make a note here to ourselves that drink is the key price is the value remember key value pairs key value all right so we've got our drinks we can print our drinks save this go ahead print and you can see we print out our dictionary here now let's give it another
example let's say that we have employees and this could be a good example so we say we have employees and i really like the show bob's burger so i'm going to use bob's burgers here as an example let's say we have different departments we've got the finance department and in the finance department we actually have a list of people what if we've got we've got bob oops we've got linda and we've got tina all in finance we can also have another department let's have it so again that's the key and the value is going to
be this list that we're putting in there so gene louise and we'll throw teddy in there as well and that one and let's do one more we'll do hr so hr and we'll put in here jimmy jr and we'll also put in mort again you could put whatever you want in here and if you need to pause if you're trying to follow line for line word for word you need to pause and type this out feel free to do that i'm going to go ahead and print out our employees just to show this make sure
i type this all right because this is a lot of syntax and we've got finance i t and hr looks like we did a great job awesome so let's say i wanted to add a new key value pair i could do that in a couple of different ways we can do something like employees and we can just say something like we want to add a legal department all right and in the legal department we're gonna have just one person we'll just have mr frond he's our legal team okay we'll just say adds new key value
pair save that actually let's print employees so that way we can actually verify save print and you can see legal ads mr frond so we are adding to the end of our dictionary just like we added to the end of our list everything gets appended to the end unless we specify otherwise let's go ahead and come in here another way that we can do this is we can say something like employees dot update and we can say something like give it a sales department and in the sales department we'll have andy we'll have ollie close
that off we're also going to have to close off our curly braces and we'll close off our parentheses and if you have a decent notepad it'll show you where your items are so that way you know you're closing things off correctly so we've got three different types of syntax going on there and it sometimes can be hard to read so here we could say adds new key value pair as well so just another way of doing it we can print employees and we'll see that andy and ollie should be added provided we typed everything correct
which we did so here's sales with andy and ollie okay one more thing let's say that we wanted to update something in our dictionary we're going to go ahead and update a value we're going to go back to drinks for this one let's say that inflation has happened which it has and with inflation the price of a white russian has now gone up from seven dollars to eight dollars we can do something like that print drinks and you should see that hopefully this has been updated to eight dollars which it was seven before now it's
eight perfect we can also grab the value of that so if we wanted to do something like print drinks dot get and then we do white russian we could totally do something like this and save and hopefully it will return 8. and it does so that is it for this lesson we're going to go ahead and move into a new file here in the next video so let's go ahead and save this and close this and we'll prepare for the next video so i'll catch you over in the next lesson okay so i wanted to
talk about importing we're going to talk about importing modules so i wanted to make a new file for this let's just call this mousepad importing dot pi or whatever you want to call it and i'm going to do the ampersand here and again if you're using windows or mac just make sure you create a new file for this i'm gonna go in here and i'm going to add my shebang like before i'm gonna declare bin python3 and save this and now we're going to go ahead and talk about importing so importing let's type this importing
and we say importing is important now modules exist within python that are built in but not available to us without importing them so like you saw us being able to do math but we could import a more robust module of math and then use that to do more advanced calculations outside of adding subtracting exponents etc in this instance we're going to talk about a couple of different imports that we can do and that you're going to see again as we get into some of our hands-on stuff and we build out some projects for example we
might want to import something called sys now let's say if we tried to print out sys.version and we'll just save this and then we're going to python3 importing dot pi and you're going to see that i get an error back that says cis is not defined name cis not defined okay well cis is something that we're going to use quite often in python you're going to see it imported quite a bit and it does a lot of different unique items like it does something called rv for example which we'll use as an example later on
but if we want to specify a an amount of arguments say we're trying to run uh python3 we're gonna do a port scanner later so we'll just say port scanner.pi and we need to supply an ip address here right and this is argument zero this is argument one so if we supply no argument one then we're going to throw an error it's going to be an if else statement and that's going to use something called rv now we'll get into that later but that's very important and it's a part of cis another part of sis
is doing something like the version type of python that we're using or another thing is the sys.exit which you'll see later on as well which is going to allow us to gracefully exit when we encounter an error we tell the script to exit or whatever we're doing so we might need to import something like this or another one is os which is very common to be imported as well so let's talk about importing in order to import we can say something like import sys just like this and we can just say this is for system
functions and parameters save that and let's go ahead and just print this now and see what happens you can see now that this has been imported even though it wasn't built in automatically we can import it and you can see now it tells us hey we are running on version 3.10.5 of python and it was downloaded on june 8th 2022 or built on june 8th of 2022 so from here let's go ahead and import something else let's go ahead and import date time and we can import from something so i know this is going to
be confusing but we can import specifics from a module so we're just importing the date time feature from date time even though they're named the same just know that we can import specifics we don't have to import the whole thing even though we totally could just import the whole thing as well so here for example we can say print datetime.now like this and you know what this is going to do it's going to tell you what the date and the time is and i apologize this is backwards this is it's from date time import date
time so again your syntax will tell you where it's wrong and it is perfectly normal to make mistakes i leave these in the video so that you see that i am human too and it's completely normal to make mistakes like this so go ahead and do your script now and you'll see that it gives the date and the time so right now it is july 6 and it is 2 o'clock in the morning i am a night owl now one other thing that we can do is we can come in here and we can from
daytime import date time and we can also give this an alias we can say as dt what if we don't want to write date timeout we just want to do dt just like we didn't want to write a new line out we did nl same kind of concept so we can just say import with alias and instead of saying datetime.now we could just say dt.now save that go ahead and print it you can see it still works so absolutely the same thing we're just giving an alias making it a little bit easier to run okay
so that is it for this video let's go ahead and close this out and then i'm going to meet you in the next video we're going to talk about sockets before we get into building out a port scanner so i'll catch you over in the next video okay so let's make a new file and we're going to call this s dot pi and again create this however you want to if you're using linux you can follow along like this or just create a new file in linux or windows again we're going to shebang this up
here i'm going to give this a bit in python 3. now this isn't incredibly necessary if you're running on windows or mac but this is just best practice for me this is the location of my python even though i'm not actually calling it i am calling it with python 3 so this isn't incredibly relevant as long as you're using python 3. now we are going to deal with sockets so sockets now sockets are what can be used to connect two nodes together so we're going to use this to connect to ports and ip addresses if
you're unfamiliar with computer networking that's okay but what we're looking for is a port and we're going to look for that port on an ip address and on that ip address that that port is open we're going to make a connection to this we're going to build a port scanner in the next video or in the later on in the lesson and you'll see this in action right now i just want you to notate understand what we're doing and then i'm going to demonstrate an example that you do not have to follow along with and
you might not be able to follow along with so that's okay but just know that sockets are used to make a connection between ports and iap addresses and you'll see that here very shortly so you do not name this by the way socket.pi if for some reason you came in here and you name this socket.pi this will break python because we are going to import socket so make sure that you come in here and you rename this if you named it socket.pi it's very easy to do that but that will break the socket dot pi
which is what we're importing here so with this we're going to set a couple of variables we're going to say host is equal to 127.0.0.1 and this is our loopback address or our home address if you're not familiar with computer networking i do recommend that you go take a class on computer networking because it will become very important as you get into python development especially if you ever work with nodes of any sort so other thing is ports so we have ports on our machine that we can connect to we're going to be using tcp
if you do not know what tcp is or what the ports are that's okay you can still follow along but know that there are 65 535 available ports some ports are very common in what they use like port 80 for example is a web server over http you have 443 which is https 21 is ftp there's a lot of common ports and protocols that are out there we're going to pick a non-standard port non-common we're just going to say 7777 so what i'm going to tell this to do is i want to tell this to
reach out to this host which is just going to be us for this example and this port i want to make a connection on this port now i'm going to give a variable of s and this is going to be a very long statement so if you say socket dot socket i'm going to type this out and i'm going to explain everything okay so socket dot af inet then we're going to say socket dot sock stream and then just for your notes af inet is ipv4 sock stream is a port so we're giving it this
s because we are not wanting to type this whole long thing out all the time we're just shortening this what we're going to do is we're going out to make a connection to this afi net which is ipv4 ip address which is what this is and we're going to also make a connection on a port which is our sock stream we're going to give it this port we're going to say that by doing s dot connect and we're going to come in here and this is going to be a tuple so we're actually going to
use double parenthesis here hostport okay and we could come in here by the way and we could just put in one two seven zero zero one instead of using a variable but i like to use variables because it's best practice but you could hard code this in if you wanted to but here i just like using the variables easier to change especially if you're coming in here and you're writing in a bunch of host port calls to the variable it's easy just to have it in one place as opposed to hard coding this okay so
i'm going to go ahead and save this this is where i do not expect you to follow along because you might not have the architecture behind it now as i stated in the beginning of this video i am an ethical hacker by trade which means that this version of cali that i've been running on or this version of linux that i've been running on is actually cali linux and in kali linux we have a bunch of tools available to us for end testing and ethical hacking one of those tools is a tool called netcat now
netcat is nc and basically what it does is allows us to connect to open ports or establish a listener on an open port so what i'm going to do is actually i'm going to open up a new tab here and make this bigger and then i'm going to run this here just understand if you've never used netcap before that's fine i'm just opening up a listener on a port that's all just worry about the l and the p for now listen on port 7777 anybody makes a connection on that port we're listening so we are
going to connect to ourselves because we wrote the script out to one two seven zero zero one which is us and all we're going to do is python 3 s dot pi and enter nothing's going to happen we don't have any print statements anything crazy but you can see that a connection was made from one two seven zero zero one to one two seven zero zero one and it made a connection on this port and we connected on the other side on this other port here which don't worry about that too much if you're not
familiar with networking that's absolutely okay but there has to be two ports being connected um one on one side one on the other so here we made that connection we didn't tell this to do anything else we just said hey go out listen for this connection we made a connection we established it we did our job we used our socket and we are good to go here we closed the connection because there was nothing no instructions given or anything else and that's all we needed so what we're going to do now is we're going to
in the next video build out what i call a terrible port scanner and we'll talk about why it's terrible but we're going to tie everything we've learned so far together and build something out that is actually usable with python so in throwing all these lessons and terms and everything out to you now we're going to apply that you're going to see how we can use that all together and i'm very excited so i'll catch you over in the next lesson when we start building out a port scanner it is time to create our first project
and this project is going to be building out a terrible port scanner and so let's go ahead and create a new file i'm going to call this scanner.pi give the ampersand here if you're on linux and then we're just going to call this then python3 save it get the pretty colors going and what we're going to do is we are going to create a scanner that is going to be something like this we're going to type in python3 scanner.pi and then we're going to say ip address we want to provide an iep address and then
it's going to go out and do port scanning on the ip address so hopefully we can build that out for now let's go ahead and import a couple of things i should say a few things we're going to import sys talked about sys we're going to import socket talked about socket and from date time we're going to go ahead and import date time so none of this should be new to you so first thing that we're going to need to do is define our target so we need to set up an if statement i'm just
going to say define our target and what i want to say is if the length of cis argv and i'll explain this in a second is equal to 2 then we're going to set a target variable target is going to be equal to socket dot get host by name and we're going to say sys.arg v one and all this is going to do is translate hostname to ipv4 all right let me make the else statements i'm gonna come back i'm gonna explain everything okay so if we do else we're going to print out invalid amount
of arguments and also print out syntax python3 scanner dot pi ip address something like that okay go ahead and close that off okay so we are taking a method of length and we're saying sys.rgv we spoke a little bit about rb in the importing section rv is going to be the amount of arguments that we are giving so when we come in here and we type in python 3 okay our first argument argument zero in theory is going to be scanner.pi the second argument or second index index of one is going to be the ip
address that we give so 192 168 1.1 or whatever it might be so we need to have two arguments if we have a third argument it's going to break if we don't have a second argument it's going to break if we just type in scanner up high it'll break if we type in an ip address like 1.1 and then we type in something here it's going to break because there's too many arguments so if it doesn't meet this specific length then what we're going to do is print out invalid amount of arguments here's the syntax
give somebody some some help here if it does meet the length then we're going to do is we're going to set our target equal to socket dot get host by name and all this is saying is we're going to get host by name of sysarcv1 that's our ip address now this is going to translate a hostname to an ipv4 so in case for example if you did python3scanner.pi and you had a hostname like i have a host in my house called punisher like if i gave it punisher and my dns knew internally that that translated
to a specific ip address in my network that's fine this will translate okay easy enough but we could also just give it an ip address so that's what we're looking for here i highly recommend for this example using an ip address in previous lessons when i've taught this before somebody has tried typing in a host name and it doesn't always go as planned though we will talk about adding air exceptions for that here in a little bit so right now all we need to know is we're trying to set this up with these arguments now
this is not the best logic this is something that will work for our needs because we are building this out but if we're building it out for somebody else this logic is not great and this is why i call this a crappy port scanner is because it's not the best we're only doing it for proof of concept and to learn what we've learned so far but think about this in another way think about if we're running two arguments well i could give this an argument what if i if i come in here and i say
python3 scanner.pi and i give it the second argument but i give it like 192 or something like that well that's not a host name it's not going to translate to anything that's also not really an iep address so we should really add some other statements in here to say hey it needs to be a valid ip address it needs to be like have four octets like we expect and if we really want to get critical about this we can make sure that it is a valid i p address because i ip addresses cancer with like
two five six dot whatever so we could say it must be between like you know each octet must be between 1 and 254 and get really refined with that and say hey that's not a valid ip address thus we're not going to scan this but we don't have to worry about that right now i'm just saying the logic behind it we need to start thinking about well how can a user break this we know how to run it but if we gave this to somebody else how would they run it and how could they break
it or how could they typo something so these are things to think about as we go so for now we've added this in we've got this in here we could test this if we just save it we come in here and we just say python scanner.pine we don't give any any arguments you'll see that it says invalid amount of arguments python3 scanner.pi is the syntax perfect don't give an argument right now because it's going to break if you don't have dns translating properly so from here let's go ahead and add a pretty banner so let's
add a pretty banner and all i'm going to do is do a print command and i'm going to just do a bunch of dashes this is not really going to be that pretty of a banner but it's still going to be a banner and so when this kicks off if if we've got a valid ip that we're going to be scanning we're just going to do scanning target and then we'll just do a space and you could just do a concatenation here of plus target you could do f string if you want completely up to
you on how you want to do this so we just do scanning target plus target this will say what the ip address is we can do like scanning target of that along those lines and then we could say print time started and then we can give this something like a string so we want to give plus a string of date time dot now remember we can't concatenate numbers and strings so we're going to go ahead and do this and then we'll just print out this one more time okay i'm going to save this let's go
ahead and try running this with like i don't know 192 168 1.1 okay so this is what we get we can see that we have scanning target okay scanning target 192 168 1.1 here's the time that it started perfect we've got a pretty banner set up so now let's actually start making it do something so we're going to use what is called the try command we're going to try something and if it works perfect if it doesn't we're going to have exceptions for that so here's what we're going to do we're going to say try
4 we're going to do a for loop port in range remember we talked about a range earlier we talked about well what about a 4 ip in range of 1 through 254 we're doing the same concept here now we're just giving it a range now there are 65 535 potential ports i'm not going to force you to scan all those if you want to you can come in here and say 65 535 this is not threaded we are not doing any sort of speed runs here this is could potentially be a slow port scanner so
it is best in my opinion to do like a port scan between 50 and 85 and the reason i do this is because we're going to scan or attempt to scan our home router and usually dns is open on that and usually port 80 is open on that so i'm trying to look and see if we can find a couple of ports that might be open so for port in this range what we're going to do is we're going to set our s equal to that lovely socket dot socket socket dot af inet and socket
dot sock underscore stream just like that and then we're also going to come in here and we're going to do a socket dot set default timeout to one so if it responds back or doesn't respond back within a second we're just going to move on we don't want to stick to scanning a port waiting waiting waiting for not to respond back to us and then the script takes long and longer and longer so we just want to make sure that we set our default timeout to one there and then what we're going to do is
we're going to go ahead and set a variable result and result is going to equal to s dot connect underscore ex and i'll explain all this in a second target import all right so what this is doing here actually let me type out the rest and then we'll go back we're going to say if result is equal to 0 we're going to print out port we can do is open so now we can come in here we can do a format port if we want to you could use an f string here if you want
to so we can just use an f string i'm just showing you examples but you can say port and just do an f in front of this okay port whatever is open and then we're going to close the connection all right let's read through this make sure we understand what's going on so we're gonna do a for loop for a port in the range of 50 to 85 so we're going to start with 50 and 51 then 52 go through that process we are setting a variable of s equal to this because we're going to
gather the ipv4 address and we're going to gather the port that we're trying to connect to now our ipv4 address here's our tuple right here we are saying i want to connect on target and port target is going to be supplied by us we're going to use rb1 so we're going to supply the ip address and the port is going to be this range here so for port in range we're declaring port here now this s dot connect underscore ex this is an error indicator so if a port is open the error result returns zero
if a port is closed it returns a one so if the result is a zero we're going to say hey this port was open if it's not it's going to close out and we're going to close this then we're going to go back to the loop and come through and try it again so we're going to close out our socket connection on that port come back try 51 52 etc etc now before we run this there are some exceptions that we need to consider so the first one we're going to try this but we need
exceptions so except what if we have a keyboard interrupt so we'll say keyboard interrupt and that just means what if we hit control c while this is running we can stop this on our own so we're providing an exception of a keyboard interrupt and we're going to say exiting program on a new line and then we're going to sys exit remember i talked about sysexit and we're going to allow us to exit gracefully we can also do an exception of a socket.gai error now this is what happens when the hostname does not resolve so what
if we say hostname could not be resolved so if we typed in a bunch of mumble jumbo up here we say like python3 scanner.pi and we just typed in something and that doesn't resolve to an ip address well guess what this is going to throw an error now and we can exit we got to cover all of our bases lastly what if we have a socket.air so we have an error when we're doing this and we print out hey we could not connect to the server just it's just not online what if we try to
connect to an ip address and it doesn't talk back to us could not connect to the server so something to think about there again will assist.exit so let's go ahead and save this now you're going to need to run this against something so i'm going to show you an example of mine okay so in order to run this we need an ip address now if i type in ifconfig i'm actually on a virtual machine right now so 192 168 138.14 is not my true ip address i'm actually going to bring up my windows machine that
i'm on and you can see here that i have a default gateway of 192.168.4.1 so make sure you know your gateway or what your router's ip address or that you have a machine that you can scan that you know might have a port of 53 or 80 open or you need to modify your script to make sure that you can scan for something but i'm going to go ahead and scan my 4.1 because it should have some stuff open so if i do a python3 scanner.pi and i'm going to come in here and just do
a 4.1 and i hit enter this could take some time so what i'm going to do is i'm going to let this sit here actually it's already it's zooming through you can see it found port 53 pretty quick um and then this might take time to find port 80 if port 80 is open but basically what it's going to go through is go through 450 51 52 53 all the way through the process and has that one second timeout as it goes through and checks each and every one of these ports so it should take
maybe up to a minute for this to work so i'm going to go ahead and pause right here it looks like we're almost done actually or 80 came back as open too no need to pause it finished while i was yapping so you can see it came back and said 453 is open port 80 is open so our script worked it took forever to scan these ports but it is working so we were able to build out let's go back and look at this this is awesome we were able to build out a port scanner
that came in here and within two hours you now have the knowledge to build out a basic port scanner you didn't think you would be able to do that well here you are look at you you're awesome okay so we were able to give it an argument we set our if conditional statement make sure the length of the argument was two valid otherwise we have an else here we made a little pretty banner we imported some stuff got that all working we did a try statement which really was just a for loop in here and
we had some logic based on conditions that would happen within this for loop we had a for loop and an if statement together we used a f string to print this out pretty cool came through we had some exceptions as well for example like the keyboard interrupt just to show you if i come and run this again if i wanted to stop this scan in the middle of it i could just hit ctrl c and it'll say hey exiting program and that's exactly what we told it to do so you could say in here it
says exiting program so that's the keyboard interrupt same thing we had exceptions for not being able to connect or we had the hostname could not resolve we would also have those sorts of things as well so you could test out those errors see if they work for you but other than that we built a scanner so this is project number one we're gonna go ahead and move on to learning a little bit more python and then we'll go ahead and get into some more project work i'll see you over in the next video now we're
going to take a look at accepting user input in our program and then how we can manipulate data with that input so let's go ahead and make a new file i'm going to just say mousepad and we'll call this input dot pi and i'm going to bring this over we'll give this the shebang then python3 here save it get the pretty colors and in order to take input in python all we actually need to do is just use input something like this so we could say like enter your name for input like this and put
a space and we'll need to put this into a variable so if we come into the front of this and we just say name is equal to we could do something like that and then we can just print out like hello and give a space do name and then we'll add an exclamation at the end or period at the end doesn't really matter and then we can just come in here and save that and if we run this close this out i forgot to add an ampersand so we'll do that real quick and if we
run this now we just say python3 and we say input dot pi say enter your name i'll just say heath and you can see it says hello heath we can add upon this we can say like what's your favorite drink so if we just make a variable and we call it drink and we do input and say what's your favorite drink and do a question mark something like that we can come in here and then just add upon this if we want we can just say have a space plus drink plus period all right and
it should say welcome heath have a and then whatever drink we put in here so if we save this come back and run it okay i'm gonna put heath as my name and then white russian is my favorite drink and it says hello heath have a white russian so we can easily accept input like this so let's think about if we wanted to build out a calculator i'm going to go ahead and delete this what if we wanted to take input and make a mini calculator we could definitely do that so what if we had
two numbers and we've been taking x and y so if we said x is equal to input and we'll just say give me a number and then we'll do something like that and then we can also do y and also make that input give me yet another number you put whatever you want here by the way and if we do something like print x plus y and we do that let's save it and let's see what happens here so i'm going to print this out we'll give a number we'll do three and then we'll do
two and it gave us 32. why is it doing that well what it's doing is it is taking these and giving this to us as a string so we need to either do an integer here and put this in an int or we need to take this as a float so if we think about this from the past if we want to only take integers we can but if somebody were to put in like 8.7 or some number here and they gave that to us well we would only take the 8 and we would leave
off the 0.7 so i think if we're building out a calculator it's better to use floats so we can just come in here and say float and we'll put this all in parentheses and then we'll do the same thing right here so now when we come in here and we save this and run this again what should happen is this should work properly so if we did like 3 and then 3.2 you'll see that we get 6.2 which is proper that's awesome well what if we wanted to build this out further we could make this
into a situational thing what if we not only wanted to do an addition but what if we wanted to allow the user to say i want to add or subtract or divide or multiply or maybe use an exponent we could do that so what if we added another variable and we just called this o for operator and we just took that input and we just said something along the lines of input give me an operator right and maybe we put this in the middle here so that way we're not just getting two numbers maybe we
want to take that and we want to say something like this so that way you'll say give me a number give me an operator and then give me another number and we'll print that out so now let's think about how we want this to flow if we want this to work with a addition like we already have we might want to come in here and just say something like if o or operator is equal to a plus sign well then we'll come in and just print out x plus y and remember the indentation is important
here else if or l if o is equal to a minus guess what we're going to print x minus y and we can continue on here else if o is equal to division we'll divide and then we'll multiply as well so x divided by y else if o is equal to multiply here we'll go ahead and multiply that let's throw in an exponent for good measure so let's do l if o is equal to an exponent so that's two asterisks there and then we'll just print out x times y so we've got a mini little
calculator here and we need to end this with an else statement so what if we take input that doesn't belong here if it doesn't match one of these operators then we can just print something like unknown operator all right and period there save that and this should work so let's go ahead and give this a try what if we come in here and we just say give me a number we'll do three and let's say we want to multiply and we'll do by three and we get a nine we can do it again we can
try something like we'll do four and this time we'll do an exponent we'll do four so four to the fourth power is 256. so we have just quite easily built out a little calculator that we take input from a user and then utilize that to calculate that expression given what is given to us by the user now let's think of something else here just as an aside what if something is written differently like when i think of math with python yes we think of two asterisks as an exponent but what if we had a situation
where we wanted to maybe use the up arrow or the caret symbol which is how i've commonly seen it used online for math and exponents well we could do something like or o is equal to the up arrow like that and what if we save that and ran this now and if we gave a 4 to the up arrow of 4 guess what we still get 256 so we can use our statement like that here our and or or other operators that we have learned along the way so start thinking about it like that as
we start to piece these things together we're starting to build out little projects and we can use some of the logic that we have already learned throughout this course so that's it for this lesson we're gonna go ahead and move on to the next one okay now we're to talk about reading and writing files using python so let's go ahead and make a new file i'm just going to mousepad and we're going to call this filemonths.txt and in here let's go ahead and just type out the month so we'll do january february march april i
can type may june july august september october november and december if you need to take a break and catch up go ahead and do that save this and once you have it saved go ahead and close the file out so let's go ahead and create a new python file so call this mousepad and i think we'll just call this dot readrite.pi it'll do the ampersand here and for this what we're going to do is we're going to go ahead and look at how we can read and write files so the way that we do that
is with the open command so we'll say something like open and with open and actually let's add the shebang up here really quick and python 3 save it and with the open command we can say something like open months dot txt and i need to add this here so if we open months.txt what we need to do with this or what we typically do with this is we store this within a variable so we might just call this months equals openmonth.txt and if we want to read that file we can come in here and just
print out months and you'll see what this does here in a second now best practice says to also close this out anytime we open this so i'm going to go ahead and just add some lines in here because we're going to add some more stuff to this but we can just do months dot close and we'll save this let's go ahead and run and see what happens here so if we do python 3 read write dot pi you'll see we get this information back let me move this so it's readable and what this tells us
is hey this is the name of the file we're in a certain mode and this mode currently is read mode and we get the default encoding here of utf-8 so we're getting information back we're not actually reading the file that we wrote so some of the information that we can get by the way we can come in here and we can say to ourself well is this file readable so we have mode equals r and this is one way to check it there's a couple ways that we can check it as well we can print
out months dot mode and that will tell us what mode we're in we can also print out months dot readable and that should give us a boolean statement whether that is true or false so if we come in here and we save this and we do this again you'll see that we get months we print that out we get the whole shebang here and we get the mode is equal to r which is readable you can see if we print out the mode we can also see it's readable or if we do months readable we
can see that it is readable so we have the ability here to actually print this out and read this file so how do we do that well let's go ahead and delete these two lines and we can just come in here and do months dot read if we do that what happens here save it print this out and you can see that it reads through every single iteration january all the way through december perfect so we just read a file and we closed out working as intended if we wanted to read this line by line
we could we can do months.readline like this and if we save that in print we will read literally one line if we wanted to read a second line we could copy this and we could paste this and save it and come in here and guess what we'll get january and february this time so if we want to read this we have to read line by line now if we wanted to read all the lines at once we could do that in a different way we could do that with read lines and this will print out
an empty array or a list here actually and what you'll see is we get january new line february new line march etc so we put this into a list and now if we try to actually run this again let's actually tab over if we tried to run this twice look what happens i'm going to print this and save this so we have the first list here and then we have a second list here the second list is empty well that's because we've actually already read this so we read through it here first and then it's
empty the next time we try to read it so if we wanted to read it again what we need to do is actually use what is called the seek method we can come here and say months dot seek do 0 like that to go back to the very first line and print out again and now you can see we actually printed this out twice so that works out well we also have the ability to put this into a for loop to read through every iteration of the line so if we wanted to do something like
four month in months we could just print out month all right and that should iterate through everything in here so save that print this and you can see that it indeed does print through everything we can also add the dot strip at the end of this save that print and you can see that we strip out any of the new lines that are in there so you saw we got some spacing in here a little funky so if we came through we just added the dot strip that makes our for loop a little bit better
so again remember what we're defining here is whatever we want we can call this for x in months make this an x and it's the same thing but logically it's just easy to call this month in months and then we can just print those out so we can iterate through this so so far up until this point all we've done is been able to read a file what if we want to write to a file or append to a file well we can do that as well so if we wanted to for instance open a
file let's open a new file let's do something we haven't created yet let's do days of the week so let's do open days and if we try to do this let's change months to days and we're going to change days to close and we're just going to leave this open right here so we have our best practice now if we try to run this with an opendays.txt we're not going to be able to do that because currently we're just in read mode so we want to be able to turn over and put ourselves into write
mode in order to do that we can just do a w if we wanted to check that out we could just do print days dot mode like we saw before make sure we are actually operating in right mode so if we save this you'll see that we are in right mode so we're good to go you could also just again delete this save and print out days apologies you print out days save that you'll be able to see that we are also in write mode so both of these work now let's write to a file
so we've got days let's go ahead and write to that so we can do something like days oops days dot right and let's say we wanted to put monday into days save that close it and if we go ahead and execute that nothing happens but if we go ahead and cat out days.txt you'll see that monday has been put into days.txt perfect so now what if we wanted to continue on with this let's put tuesday in there and we're going to need a new line so let's go ahead and do a new line with that
and we'll do tuesday and hopefully when we write this out we'll see monday and tuesday so let's save that print this sorry let's run the script and then print this and you'll see we have the new line in there and we have tuesday but we actually overwrote it we don't we don't have monday anymore so what's happening here is we're actually writing to a file which is overwriting the file we're not appending to the file so if we wanted to append to the file what we need to do is change this w to an a
and we can come in here and we can write in wednesday if we wanted to save this and now let's run this and run that and now you can see we have tuesday and wednesday so understand the difference between r being read w being right which overwrites and a being append which allows us to append a file instead of overwriting a file so that is it for this lesson i'll go ahead and see you over in the next one all right let's talk about classes and objects so python is what is known as an object-oriented
programming language so pretty much everything in python is an object and we can use what is called a class to be what is like an object constructor and we can use that to help create objects and this is a little bit easier to explain once we actually demonstrate this so we're going to create a couple of files in this lesson and the first thing that we're going to do is we're going to do a mouse pad and i am going to create a file called employees dot pi in this we're going to go ahead and
declare a class i'm going to save this here so let's do class employees save it and we're going to go ahead and hit enter and just tab over and we're gonna do this define and two underscores init or init two underscores again and we're gonna put some parameters inside of this function and we'll talk about what the init function is here in a second let's go ahead and let's think about having employees so the first parameter we're going to define is called self and then for our employees we might want to know the employee's name
we might want to know what department they work in we might want to know their role in that department their salary and maybe the years that they've been employed we'll do years underscore employed and just like a function we'll add the colon here at the end and we're going to go ahead and tab and then we're going to define all of this so let's go ahead and say self dot name is equal to name so we're creating methods here which you'll see us use in just a bit we're going to do self.department equals department and
you should get the gist at this point self.roll is equal to roll self.salary is equal to salary self dot years employed is equal to years employed okay so let's save that so all classes have a function called the init function and this is always executed when the class is being initiated so we're going to use this init function to assign values to object properties so what we're doing here is we're creating these parameters within this function and we're also building out methods that we can use once we import this class so let's go ahead and
close this out and we're going to open up another mouse pad and we'll just call this something like r employees dot pi you can call whatever you want and i'm gonna go ahead and do my shebang at the top we'll do bin python3 save that and from here i want to actually import the class that we just built so what i'm going to do is i'm going to import this and we're going to say from employees import employees all right and we can make a couple of employees here so let's set a variable e1 for
employee one and we'll just do employees and i'm going to stick with the bob's burgers theme from earlier we'll just say we've got bob remember we're declaring bob's name his department will say he works in sales we'll say that his role within the department is the director of sales and we'll say his salary is a hundred thousand dollars a year and that he has been employed for 20 years and we'll do another employee and we'll say that this employee is linda she is an executive she is the chief information officer or the cio she makes
150 000 a year and she has been with the organization for 10 years so we can do some stuff with this we can go and say like print e1 dot name let's save this make sure everything works come in here actually let me close and reopen this because i forgot my ampersand on this and we'll come back and now we'll go ahead and do python3 our employees and you'll see that print e1.name is pulling down bob so we're using the name method that we created with our class so we define that within that class so
now we have the ability to pull down information based on the class that we built so we can also do something like print e2 dot role and we should get that role printed out for us so if we see e2 role we're getting that uh she is a cio so that is accurate statement here let's see if we can open both of these let's open also employees.pi and we have both of these here now so it's a little bit easier to see we've got our function our init function here and we have all of our
methods that we've defined now we can build upon this within this class so let's say if we wanted to make another function if we wanted to say something like define eligible for retirement and we just said self in here as the parameter what if we said something like if self dot years employed is greater than or equal to 20 then we return true because if the person has worked with us for 20 years then they are eligible for retirement otherwise if they haven't then they haven't earned their retirement yet so we can look at an
employee and see how long they've worked with us and we can save this here and then we can come in here we can do something like print and we'll say e1 and we'll ask if they are eligible for retirement okay save this print this out and you'll see that bob is indeed eligible for retirement because he has been with the company at least 20 years which is what we put here within the class so just know what we're doing is we're building out our own class and we're able to define these functions and these parameters
and these help us with creating our objects that we're using over here so we've got our classes and then we've got our objects so this can get really robust very quick and we'll build out a project to look at that here in the next lesson and that way we can tie this all together but this is a very important part of programming and this is very much a 101 class so we're just skimming the surface on what this is but this is actually incredibly useful when we start building out classes like this to help with
other things that we're writing and pulling information from so that's it for this lesson we're going to go ahead and move on to the next one we have reached the final project in this course and this is going to tie everything together that we have learned thus far so i am a bit of a sneaker head which means i really like shoes and so what we're going to do is we're going to build a budget app using classes and objects and we're going to see what shoes we can afford based on how much money we
have so we're going to go ahead and start with the class and we're going to just make a new file we'll do mousepad and we'll call this shoes i can type shoes.pie open that up and we'll also open up a new window as well or just a new tab here and we'll write in both of these so for this one we're going to go ahead and just define a class of shoes and i'm going to go ahead and just save that so i see the colors and here we're going to define our init statement and
we're going to just say self and then we're going to have a name our name of our shoe and the price of our shoes and within this we're just going to define self.name equals name this should all look familiar so far and self dot price is equal to price what we're going to do with the price because we'll run into an instance where this is going to be taken potentially as a string we're going to go ahead and just give this a float we want to make sure that we are assuming this is going to
be a float of a price here let's go ahead and define a few more things so i want to define a budget check and i want to make sure that we have the budget here so what we're going to do in this instance is we're just going to say self and then we'll also add in budget here and we'll come in here and we'll do a check first we want to make sure that we are given an integer or a float here when we are asked for the budget so we can say something like if
not is instance and what this means is we're saying if this type is not what we're specifying here then we're going to go ahead and reject this and close out of the application so the is instance looks for our type and returns a true value but if we say is not true then that's false remember our truth tables and thus becomes invalid and closes the app so what's going to happen is we're going to say budget and we're also going to look whether budget is going to be an int or a float and if it
is not one of those two things we're going to go ahead and print out invalid entry please enter a number something along those lines and then we'll exit the application so let's say that we have a budget and we want to also know how much money is left over if we buy the shoes so we can define something like change or what change is left over and we could say something like self budget on this as well and we can just return our budget minus our self.price so remember we declared self.price up here now we're
calling it we're just saying hey i want to return the value of the budget that we have minus the cost of the item that we're purchasing okay only a couple more things and we'll be done with our class here so we also need to purchase it right so let's define buy and we're going to do self budget again and here we're going to do a self dot budget check and we'll check our budget and what we'll say is if our budget is greater than or equal to self.price then we're going to print out we'll do
an f string here and we'll just say u and cop sum and we'll do self dot name so the name that we'll provide here this will all make sense once we tie it all together okay and then if our budget is equal to the self.price then we're going to just say print out you have exactly enough money for these shoes otherwise we can do else now we could go through this whole thing and and give a statement and say well what if we had no money or what if we didn't have enough money but in
this instance we're just going to print out the situation of you can buy these shoes and have we have a dollar sign here we'll say self dot change and we'll give a budget left over so we'll say left over just like this all right and then when we exit the application we can say something like exit and we'll just give a statement thanks for using our shoe budget app now this might not make any sense yet and that's okay let me make this a little bit bigger and so what we're doing here is we're defining
our init and in our init we have a name which is going to be the name of the shoe and price it's going to be the price of the shoe so self.name is equal to name self.price is going to be a float because we're expecting potentially it could be our price of our shoe could be 2.99 or 100.99 it might not just be a perfect number so we want to use a float we have our budget check here we're just checking the is instance we could in theory add to this budget check and make sure
we have enough funding before we come down and do the purchase would probably be the logical way to do this but in this instance we're just using really beginner logic and we're just coming through and we're just saying hey if this instance isn't an integer or float like somebody types in a string then we're just going to go ahead and reject this we we don't want any we have no interest if this is no integer or float here then we're going to define our change so if we have uh money left over like if we
have budget minus the price of the shoe then we're gonna go ahead and give some money back um and then we're gonna buy the shoe as well so we're gonna have our buy option as long as our budget is greater than the price of the shoe we can buy that shoe and if the budget is equal to the price then we have enough money if it's not then we'll actually have some money left over here unless it's the instance of not having enough money and then it'll just exit the application because we haven't told it
to do anything else and then it'll also thank us for using the shoe app so that's part one we have defined the class now could we build this out all in one thing yes we could just write a script out for this we could code this and make it into one easy script but what this will do is allow us flexibility this is where object oriented programming comes into play because we don't have to continue to repeat ourself and what we're following is what is called the dry approach the don't repeat yourself approach and so
what we're doing is maintaining everything in a class here and then we'll call that class and make it very easy on us instead of having one crazy long script that is potentially repetitive and a lot more difficult to go through so here we're gonna just open this up and i'm gonna call out a shebang on bin python3 i'm gonna save this and i'm going to call this uh shoe purchase dot pi save that now we're going to import from our class that we made so from shoes import shoes and let's define a few variables so
let's say low is equal to shoes and for the low price shoes we're just going to say we can buy some and ones and we can buy those for thirty dollars for medium price shoes maybe we can purchase some air force ones and for those maybe they're 120 and then for the high price shoes maybe we're interested in purchasing some off-whites and those might run us for a hundred dollars and from here what we're gonna do is we're going to do a try statement remember we've done a try statement before and we're going to say
try and we're going to say shoe budget is equal to a float of an input we're going to ask for an input here so this is all tying together we'll say what is your shoe budget do a question mark there tie this together and then we're going to do an exception so if we have a value error meaning we don't get a number back similar to what we were doing previously if we don't get a number back here we're going to say please enter a number now we can come in here and say for shoes
in and then we can just give this high medium low like this we can do a shoes dot buy and remember we came out with this buy method over in our class here so we're doing a shoes.buy gonna go and check all this for us and we're gonna say shoe budget so we're gonna take the input of shoe budget what's the value here we'll take that we'll come in here and we'll see what our budget is if it is greater than or equal to the price of the shoe we're defining our name of the shoe
and our price of the shoe from our parameters that we defined up here name and price so we're saying hey our the name and the price so is the price of the shoe is it at least the amount of our budget and if it is we can buy that and we'll check in the highest order and then go in the lowest order if you flip these and you put in 400 or just keep telling you that you can only buy the low price shoe so you need to make sure it's in the highest to the
lowest order in this this example and so it'll check do we have enough money to buy the 400 pair of shoes no okay and then do we have enough money to buy the 120 pair of shoes no do we have enough money to buy a 30 pair of shoes we'll go through that and iterate in this loop to see what we can or can't buy so let's go ahead and save this and we'll save shoes.pi and i'm going to go ahead and run this hopefully i have no typos i will do shoe budget shoe purchase
sorry and run this and i do have a typo and so if i come back in here i forgot to close off my f string so let me save that now let's try this what is our shoe budget let's give it a few different options if we said our shoe budget was 30 exactly well it's gonna say that we can cop some air some and ones sorry and we have exactly enough money for these shoes thanks for using the shoe app uh what if we had thirty one dollars well we could still cops demand ones
and then we can buy these shoes and have one dollar left over alright what if we had 450 well we can buy the off-whites and we can buy those and have 50 left over for our budget so hopefully this makes sense i know this is a lot of information and this again is just a 101 course so we're just kind of dabbling in the object oriented programming and if you continue on with python from here and you start getting into more complex material then you could build upon what you're learning here so try to think
of something that you might want to script out like this or write out like this and think of the logic behind it i highly recommend going and doing coding challenges or programming challenges and try to increase the logic and the ideas behind what you're trying to build and that just takes practice all this takes practice it's one thing to watch and follow along with somebody it's a whole nother game to go out and do this yourself so start thinking of some things that you want to build really use your google skills look at stack overflow
and look at some googling and some redditing and just try to piece together the logic that you have in what you want to build so highly recommend building upon this this is a great language to learn and very flexible and i hope that you continue on with this so i do thank you for joining me again if you enjoyed this experience please do consider subscribing to the channel we do all kinds of free courses and content we do cyber security and ethical hacking related material as well a lot of open source intelligence and operational security
materials so if you're interested in any of that please do consider subscribing to the channel leaving a like for this and sharing this with a friend word of mouth is the way that we grow and we are well on our way hopefully to a million subscribers at the time of this recording we're around 355 thousand so you're watching this and we're at a million we've done our job but we've got still quite a ways to go so thank you again for joining me for this video i hope it was informative for you and i hope
to see you in future courses