Python Full Course 🐍 (𝙁𝙧𝙚𝙚)

Python Full Course 🐍 (𝙁𝙧𝙚𝙚)

SUBTITLE'S INFO:

Language: English

Type: Robot

Number of phrases: 19359

Number of words: 111769

Number of symbols: 477164

DOWNLOAD SUBTITLES:

DOWNLOAD AUDIO AND VIDEO:

SUBTITLES:

Subtitles generated by robot
00:00
hey what's going on everybody it's bro hope you're doing well and in this video i'm going to help you get started with python so sit back relax and enjoy the show if you wouldn't mind please like comment and subscribe one like equals one prayer for the youtube algorithm i'm gonna tell you why you need to learn python it's the easiest programming language to learn as well as being the most popular in the world right now and according to glassdoor the salary for new python developers in the united states is
00:32
64 000 if any of this sounds good to you well then let's get started if you need to download python go to python.org downloads and click this yellow download python button open when done check add python 3.9 or another version to path install now and then give it a little bit it states here that setup was successful so let's close out of this our next step is to download an ide that's an acronym for integrated development environment think
01:04
of it as software that helps us write other software one ide that i recommend is pycharm you can find it at jetbrains.com pycharm go to download if you would like to pay for a professional version you can click this download button but since i don't like to pay for things i'm going to use the free community version at this point we just have to follow the standard installation procedure click next you can pick a destination folder but i'll keep the default
01:34
next if you would like a desktop shortcut then check this i would also recommend updating the path variable next install and then give it a minute or two then after finishing i have a desktop shortcut so i can just click on this to run pycharm let's create a new project name this project whatever you like i'm going to name this project hello world and i recommend not creating a main dot pi welcome script right now
02:05
i'll show you how to do that manually and let's create if you need a python file to work with this is how to create one go to file new python file and i'm going to name this main and then click python file all right ladies and gentlemen we now have an empty python script that we can use to begin coding now if you checked this box create a main.pi welcome script and then created a new project then your main python file will probably
02:37
look a little something like this but that's completely fine we can just delete this i don't know about you guys but the font is very small for me so let's change that we can change that by going to file settings editor font and you can increase the font size let's say 25 you can also change the font too what's a good one uh how about that then click apply and okay much better we can actually see something
03:07
to run a python script just click the screen play button in the corner alternatively you can go to the run tab and then go to run okay so this box that appeared at the bottom this is referred to as a terminal window this will display any output that your program has and this line at the end process finished with exit code zero that just means that there were no errors in this program so currently this program doesn't do anything but let's change that let's print something let's print a message so to print a
03:37
message to the terminal window type print and then add a set of parentheses and then you can type a message but it has to be within quotes either double quotes or single quotes it doesn't matter so let's think of a message i don't know i love pizza then let me run this and in my terminal window it says i love pizza to print another line just add another print statement print and then your message within quotes
04:08
it's really good and well what do you know it printed i love pizza it's really good so ladies and gentlemen you just wrote your first python program by the way you can change the font as well as the font color of your terminal window by going to file settings then go to editor color scheme console font and console colors let's change the font let's say i would like whatever this font is then click apply
04:43
let's change the font color too by going to console colors for any output you can click number two and select a color let's say i would like bright green that should be decent and for anything else let's say this will be a dark color so i can barely see it so then let's click apply and okay oh yeah that's much better well everybody that was your very first python program i'll post
05:14
any useful links and timestamps for this video in the comments section down below but yeah welcome to coding with python hey you yeah i'm talking to you if you learned something new then help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro
05:38
[Music] hey what's going on everybody it's bro hope you're doing well and in this video i'm going to explain how variables work in python so sit back relax and enjoy the show if you find this video helpful please remember to like comment and subscribe your support will help keep this channel running all right let's do this a variable is a
06:20
container for a value it behaves as the value that it contains it's much like in algebra lessons back in the day where we had to solve for whatever value x was and then we can reuse x and it behaved exactly as that value well with programming we can do that plus we can assign variables of different data types so they are not limited to just numbers we can assign whole words numbers and even these things called booleans which are either true or false but i'll get more in depth into data types a little bit later on in this video so let's create a variable and this is
06:51
how to do so we need a unique name for this variable let's say name name equals some value now the first data type that we'll cover is strings a string is a series of characters so to create a string we can either use single quotes or double quotes in python and we can assign this a value of whatever your own name is so i will assign this variable name a value of bro now this variable will behave exactly as if it was this value this name and then we can reuse this variable for something
07:22
so let's print whatever our name is print name and this will print whatever your name is to the console window now when you print your variable make sure you don't put it within quotes because what we're doing then is literally printing the word name as you can see here so if you need to use your variable for something make sure it's not within quotes now we can combine our string variable with another string so within our print statement let's print a string literal such as hello space
07:55
plus name so we are combining strings and then we're printing the results to the console window so this will print hello bro now you can actually check the data type of a variable so i'm going to turn this line into a comment so what you'll do is take the name of the variable surround this with a set of parentheses and then precede the set of parentheses with the type function and this will print the data type of this variable to the console window and you can see that it says class str short for string
08:26
a string is a series of characters a name is a good example of a string a series of characters so that is how to check the data type of a variable just use the type function now you can actually combine variables together as long as they're of the same data type so let's change our name to maybe first name and we'll create a second variable called lastname now a common naming convention with variables if it has two words separate each word with an underscore it's not necessary but it's common practice for python and honestly i
08:58
sometimes forget about that but don't tell anybody so let's create another variable called last name last name equals whatever your last name is and then let's create a third variable called full name full underscore name and let's combine first name plus last name and let us display full name along with hello plus full name now these two names were combined together to create a whole
09:28
new variable actually what i think i'll do is add a space within my variable so i'm doing a bunch of string concatenation i'm combining the variable first name plus a space character plus last name so the result is hello whatever your first name and last name is so that is a variable of the string data type it is a series of characters now with strings we cannot normally use these for any sort of math that's where the next data type comes in for the time being i just turned all of
09:59
these lines into comments and we are going to move on to the int data type short for integer this time let's create a variable called age and we will assign this a value of whatever your age is let's just say that i'm 21 now when you assign a value of the int data type make sure that it is not within quotes because then technically it would be a string then and you can see that the color scheme for this number actually changed so if this was a string we would treat it different than an int data type because with strings we cannot normally use them for
10:30
any sort of math so we now have a variable called edge and we can actually increase or decrease or do whatever we want with the edge let's increase our age by one let's say it's our birthday to do so we would say age equals age plus one and then let's print our edge to the console window print edge and you can see that it says i am 22. now there's a shorthand way of writing this what you'll do is type age plus equals one so that's kind of like a shortcut and this would work the same too
11:01
now let's print the data type of our inch variable because we can within a print statement i will print the edge and add my edge variable to this type function and this will print the data type of my edge variable to the console window and i will turn this line into a comment just so that it doesn't get in the way now if i were to print the data type of my age variable as you can see it is of the int data type short for integer it only stores a whole integer number now what if this was a string so what
11:33
i'm going to do is surround my value within a set of quotes and i will turn this line into a comment so that it does not get in the way you can see that the data type of my age variable is now a string and with strings we cannot normally use them for any sort of math so let's attempt to increase our age variable by one and we ran into an error a type air can only concatenate string to string not end now the point being is that it's important to use the appropriate data type because
12:03
with strings we cannot normally use them for any sort of math you'll want to use a data type of a numeric value an int or a float which we'll discuss a little bit later now here's a situation to consider what if we would like to display our edge variable along with a string literal much like what we did with this line involving a string literal such as hello plus the user's full name so let's attempt to do so and this is not going to work and i'll explain why so within our print statement let's say something such as your age is plus
12:35
edge now we're going to run into a type error that's because we attempted to use string concatenation with a string literal and a variable of a different data type a variable of the int data type in order to display our edge along with the string we would need to convert our edge variable to the string data type and one way in which we can do that is by type casting and we'll cover this more in the next video so we're going to surround our age variable that is of the in data type with a set of parentheses
13:07
and precede this with str short for string and this will convert our variable to a string that will allow us to display it with another string now if we were to run this program this way this will now display your age is and in this case 22 so if you need to display a variable of a different data type along with the string you would just need to use a stringcast to convert that data type to a string but we'll cover this more in the next video on typecasting so that is the int data type it stores a
13:37
whole integer number and the next data type is the float data type and i'm going to turn these lines into comments and we can move on now the float data type is a numeric value that can store a number that includes a decimal portion an int data type cannot store a decimal portion that's why it's in short for integer it only stores a whole number this time let's say we have a height and i will assign this a perfectly normal height of maybe 250.5 centimeters so this decimal portion is
14:08
important that's indicating that this is a floating point number a float for short so let's print our height to the console window print height and this will print whatever my height is and now let's check the type of our height using the type function so within a print statement i'm going to add my height surround this with another set of parentheses and precede this with the type function and the data type of my variable height is a float short for a floating point number
14:39
a numeric value that contains a decimal portion and now for practice let's print our height along with a string literal so let's create a string literal such as your height is plus height but guess what we're going to do some type casting so surround height with a set of parentheses and precede this with str so that we convert height to a string so that we can use string concatenation within this print statement and this will print the message your height is 250.5 actually let's add
15:12
centimeters to the end so plus cm and there we go and that is what a floating point number is float for short it is a numeric value that contains a decimal portion now the last data type that we're going to cover is the boolean data type it is a variable that can only store true or false let's say we have a variable called human if we were to assign this a boolean value this would be either true or false and we can print whatever this
15:42
value is let's print whatever human is and this will display false now the reason that you may want to use booleans instead of strings even though you can still store a string representation of the word false or true is that these are very useful when we get to if statements we can check to see if some statement is true so that is a good use of boolean values now let's check the data type of my human variable so let's print type human and this will print to the
16:15
console window bool short for boolean now make sure you're not putting this within quotes because then it's technically a string and they behave differently now the data type is a string and you may have noticed as well that the font color has changed between strings and boolean values so that's another good indicator now what if you need to display your value along with a string using string concatenation well guess what we're going to typecast again so let's print something r u a human
16:48
so we need to use string concatenation so i will convert my boolean variable of human to a string and i have been programmed to inform you that yes i am in fact a human that is true well everyone that is the basics of variables it is a container for a value and they behave as the value that they contain there are four basic data types strings which store a series of characters inc which store a whole integer floats which are floating point numbers a numeric
17:20
value with a decimal and booleans which only store true or false and they're very useful with if statements so that is it for variables if you would like a copy of all this code i will copy and paste all of this to the comments section down below but yeah that is how variables work in python hey people what's going on it's bro hope you're doing well and in this video i'm going to explain multiple assignment in python so sit back relax and enjoy the show well then since you clicked on this
17:52
video i should probably explain what multiple assignment is now multiple assignment allows us to assign multiple variables at the same time using one line of code here's an example of us using standard assignment let's say we have a variable name and i will set this to a value of my name but you can use your name for this example let's say edge equals 21 and how about a variable called attractive i think i'm going to set this to true okay so we have a bunch of variables and
18:22
then we can print the value of these variables with some print statements so let's print name age and attractive so we have name age attractive and as you would expect this prints bro 21 and true now another way in which we could write the same code is to use multiple assignment and this allows us to assign multiple variables at the same time using one line of code so i'm going to turn all of these lines into comments and
18:52
this time we will only use one line of code but to do this we're going to list all of our variables separated with a comma so that would be name comma edge comma attractive and we will set them equal to those values but in the same order separated by commas so that would be bro comma 21 comma true and this would do the same thing only using one line of code here's another example let's say we have four variables
19:22
spongebob patrick sandy and squidward and let's pretend that they're all around maybe 30 years old at least it seems like they're around 30 years old but i could be wrong i'm not too familiar with spongebob lore so let's set them all equal to 30. now we could write this using four lines of code or we could use multiple assignment to write the same thing using one line of code so if all of these variables are all equal to the same value there's a little bit of different syntax here so let's begin with the first variable
19:53
of spongebob equals patrick equals sandy equals squidward then we're going to set them all equal to 30 and this should work just fine using only one line of code well everybody that's pretty much it for multiple assignment it allows us to assign multiple variables at the same time using one line of code i will post all of this code in the comments section down below and well yeah that's how multiple
20:25
assignment works in python hey what's going on everybody it's bro hope you're doing well and in this video i'm going to demonstrate some useful string methods in python so sit back relax and enjoy the show all right let's do this in this video i'm going to be explaining and demonstrating a few useful methods available to us for strings now let's create a variable called name for this example and you can use your own name if you want so we now have a variable called name and for my first example
20:56
let's print the length of our name variable so to do that we can surround our name variable with the length method and this will print and return how long the length of our string is and for me the length of my name variable is three for three characters if i were to increase the size of my name well then that's going to change to a so that is the length method and i'm just going to turn this line into a comment and let's move on so next we have the find method
21:27
i just copy and pasted print name so i can reuse it name dot and actually if you have intellisense setup if you add dot after your variable you'll get a suggestion or a few suggestions of different methods and other features that you have access to so let's find find which is uh screw this i'll just type find find then we can find the first index of where this character is let's say i would like to find where capital b is
21:58
well that is at index zero because computers always start with zero so this first character would be zero then r would be one and then o would be two for this example all right so that is how to find a character within a string moving on so let's capitalize our name and i should probably make this lowercase so name dot capitalize and this will make my name capitalized
22:29
but if there's any spaces and then additional words it's not going to capitalize that only the first letter and your string next we have upper this will make your string all uppercase name upper and now it is uppercase guess what's next lower name dot lower and i should probably make this capital again and everything is lowercase we're making some good progress today
23:02
next we have is digit this will return true or false depending on if our string is a digit which it's not so this will print false if i changed my string to a bunch of numbers like 1 2 3 then this will return true let's change that back and move on next we have is alpha are these alphabetical characters oh make sure you add the parentheses afterwards my bad
23:37
false because we added a space within here now it's true so you can use is alpha to check to see if your string contains only letters alphabetical letters next we have count we can count how many characters are within our string i would like to count how many o's are in here and this should return two that's right okay we're almost at the end we can replace characters within our
24:09
string so we use the replace method and we pass in two arguments the character we would like to replace and the character we would like to replace our character with i would like to change all o's to a's and now my name is brock hod oh you guys are gonna like this next one this isn't technically a method but it's a really neat feature so one thing that we can do that's available to us with python is that we can display a string multiple times by multiplying that
24:40
string by a given number so let's say i would like to display my name three times i can just take my string variable then add times three and this will print my name three times bro bro bro like i said not technically a method but it's a useful feature that you might be interested in that's related all right everybody so that is a few useful methods available to us for strings if you would like a copy of all this code i will post this in the comments section down below but yeah those are a few useful methods available to us for
25:12
strings hey what's going on everybody it's you bro hope you're doing well and in this video i'm going to teach you guys all about typecasting in python so sit back relax and enjoy the show all right welcome back everybody i have a super quick video for you guys today on the subject of typecasting now typecasting is the ability to convert the data type of a value to another data type here's an example that i've written already i'm just going to paste it i have three variables for this example x y and z x contains one this is a whole
25:45
integer into for short y contains two point zero this is a floating point number float four short you can tell it's a float because it ends with a decimal portion and z contains a string of three a string is a series of characters and you can tell it's a string because it's surrounded with a set of quotes and remember with strings we cannot normally perform math on strings so i have three print statements for x y and z and as you would expect they contain the values of one two point zero and three now let's
26:17
begin by converting both y and z to the integer data type via typecasting in order to perform typecasting on a value or variable type that value or variable and surround this with a set of parentheses whoops i missed and then pre-save this with the data type you'd like to convert this value or variable to i would like to convert y to an integer and then you can use this for whatever you need so i'm going to replace y within my print statement with y after we typecast it as
26:47
an integer and this will display 1 2 not 2.0 it's the whole integer of 2 and then 3. so this is not a permanent change if you need to make this as a permanent change to y you'd have to reassign it so let's say y equals y and then we're going to typecast y so let's round this with a set of parentheses and precede this with the data type we'd like to convert y2 so now y is a whole integer and then we will print y without
27:20
typecast this time and our result is one two three now z z is storing a string and with strings we cannot normally perform any math on strings for example within my print statement i will state z times three you would think that the result would be nine right well you're wrong it's going to print three three times if you need to convert a string to a int or a float you would just have to typecast it so this time let's type cast
27:50
z as an integer z int and we will type cast z as an integer so then if we have z times three the result is now 9 instead of 333 so that's the basic formula for type casting now let's convert these to floating point numbers but let's include x as well because x is feeling left out so let's convert x to a floating point number so float parenthesis and we will surround this with x and display x
28:22
and actually let's convert y and z to floats as well because why not the results are now 1.0 2.0 and 9.0 remember that we multiplied z times three and then if we need to convert these values to a string we would use the string cast so the results now are 1 2.0 and 333 now you might be thinking to yourself when would you need to convert an integer or a float to a string
28:57
here's a situation let's say within our print statements we need to print these variables along with another string so within our print statement let's say x is plus x we would like to display these together and let me copy this and do the same thing for y we'll say y is plus y you cannot normally display a string along with an integer or a float because we're using some string concatenation so this will result in a compilation
29:28
error can only concatenate string not int to string so that's a situation in which you might need to use typecasting to convert an integer or float to a string so let's display x and y as strings via typecasting so we'll surround x and y with a set of parentheses and precede this with the data type we'd like to convert these values to we would like to convert x and y to both strings and now this will compile and run just fine well everybody that's the basics of typecasting
30:00
if you would like a copy of all my code i will post everything in the comments down below if you found this video helpful don't be afraid to help me out by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro hey what's going on everybody it's bro hope you're doing well and in this video i'm going to teach you guys how we can accept some user input in python so sit back relax and enjoy the show ladies and gentlemen this is where the fun begins in this video i'm going to explain how we can accept some user
30:31
input in python and we do so by using the input function and then we type in our input to the console window but it would probably be good practice to let the user know what we want them to type in exactly so let's ask for somebody's name within quotes inside the input function i'm going to type what is your name and now our program will wait for us to enter in some user input so you can use your own name for this example i'm going to type bro now to submit some user input you just type or press
31:02
enter now you can see that my process finished with exit code zero that means the program is done running so we should probably do something with this value we can actually assign it to a variable so we can use it elsewhere in our program so to do so we precede input with a variable like name works name equals input and when we accept some user input we will assign it to this variable that we called them and now we can do something with it so let's print a message involving our name variable that we have so how about
31:32
hello plus name now when i run this program we can accept some user input and then do something with that input such as display message that says hello bro and then you can type in whatever you want for your name let's say i'm dude what is your name dude hello dude now this next part is very important when we accept user input it is always of the string data type a series of characters what if we need to accept a number well you can but you can't normally perform any math on it because it's of
32:04
the string data type we would need to cast it as either of the integer data type or of the float data type so here's an example of why we can't perform any math on a string let's say we have a variable called edge age equals input and let's ask how old somebody is how old are you now i will add one to somebody's age let's pretend it's their birthday age equals age plus one this will add one to their age now let's try and run this what is your
32:35
name bro how old are you let's say that i'm 21. okay we ran into an error a type error can only concatenate string to string with what i mentioned previously we cannot normally use strings for any sort of mathematical equation or operation we'll want to cast it to the integer or the float data type so let's cast our user input to the integer data type and we will surround our user input with a cast surround your input with a set of
33:06
parenthesis and precede this with int and now we can use this user input in a mathematical operation so let's try it even though this won't display age yet so what is your name bro 21 and our program finished with an x a code of zero there were no problems now let's attempt to display this edge so i will print u r plus age plus years old we're doing some string concatenation here
33:38
what is your name bro how old are you 21 all right we ran into a type error again can only concatenate string to string you cannot normally display a variable of the integer data type along with strings because we're doing string concatenation we're adding strings together we would need to convert this variable back to the string data type so we can cast it we'll surround our edge variable with a cast now this is what's going to happen we will accept some user input it will be of the string data type and then we will
34:10
immediately cast it to be of the integer data type so that we can treat it as a number instead of a character and then if we need to display this edge we will need to convert it back to a string so that we can use some string concatenation and display all of these strings together and now let's try this theory so what is your name bro how old are you let's say i'm 21 hello bro you are 22 years old here's a situation what if we enter a number that contains a decimal portion so let's try this again
34:41
let's say i'm 21.5 years old well now we ran into a different issue a value error invalid literal for int with base 10 that means if we cast a string as an integer it can only be a whole number a portion that does not contain a decimal and that is where the float data type would come in it is a data type that can contain a decimal portion for this example let's create a third variable called height and we will ask for somebody's height
35:12
input we'll create a prompt that says how tall are you and we will cast this to be of the float data type so that our string our user input can contain a decimal portion and then we can treat it as a number instead of a series of characters and i think i will delete this line we won't really need it anymore and then let's print their height along with a message u r plus then
35:45
height but we need to cast this back to being a string so we'll surround our height with a cast plus let's say cm tall all right let's test this what is your name bro how old are you let's say i'm 21 how tall are you let's pretend that i'm 250.5 centimeters tall i'm adding the 0.5 just to test to see if this will accept a floating point number
36:17
hello bro you are 21 years old you are 250.5 centimeters tall not really all right everybody so that is the basics of user input in python normally with user input it will give you a value of the string data type if you need to use that value for any sort of math you'll probably need to cast it to be of the integer or the float data type so if you would like a copy of this code i will post this in the comment section down below but yeah that's how to accept user input in python
36:49
hey what's going on everybody it's bro hope you're doing well and in this video i'm going to demonstrate a few useful functions related to numbers in python so sit back relax and enjoy the show okay let's begin in this video i'm going to be demonstrating a bunch of useful functions related to numbers and a bunch of these functions are located within the math module now let's create a variable for this example let's say pi equals 3.14 just the first few digits
37:21
of pi is fine for this example and now here's a bunch of useful functions that you guys would probably be interested in the first is round round is a built-in function and all we do is type round then add a set of parenthesis and then we can pass in a value or a variable as an argument and the round function will round that number for us so pi rounded to the nearest whole integer is you guessed it three now we can round a number up by using the seal function of the math class seal
37:52
short for ceiling so let's print our result and in order to access this function within the math module we would type the name of the module dot and here's a bunch of functions that we have access to so we are looking for seal and it's actually at the top here for me and at seal we'll round a number up to the nearest whole integer so pi rounded up is four then on the flip side we can round a number down by using floor and that is also located within the math module so let's type math dot floor and
38:25
pi rounded down would be three up next i have the absolute value function but it's abs for short abs will give you the absolute value of a number so the absolute value of pi actually let's make this negative 3.14 the absolute value will tell you how far a number is away from zero so if you pass in a negative number it will give you a positive number so that is abs short for absolute value next we have
38:56
the pow function pow will raise a base number to a power let's raise pi to the power of two so we pass in two arguments a base and an exponent pi to the power of two is nine point eight and some change so that is the pow function next we have square root so this is located within the math module we'll type math dot s q r t short for square root and the square
39:29
root of pi is let's find out oh i guess we can't pass in pi uh let's round a different number like 420 oh i guess that's because i made pi negative okay so the square root of 420 is 20.4 and some change so that is the square root function all right we have two left we have the max function the max function will find the largest of a varying amount of values so let's create three variables for this example
40:00
let's say x equals one y equals two and z equals three we can use the max function to find the largest of these values so within a print statement let's use the max function and pass in all of the different values or variables that you would like to compare so i would like to find the largest value between x y and z and the result in this example is three now the min function will find the lowest
40:30
so min and then pass in your values or variables so i would like to find the minimum value between x y and z and in this case it is one well everybody those are a few useful functions that you guys might be interested in if you guys would like a copy of this code i will post this in the comments section down below but yeah those are a few useful functions related to numbers in python hey what's going on everybody it's bro hope you're doing well and in
41:02
this video i'm going to explain string slicing in python so sit back relax and enjoy the show all right then let's talk about slicing slicing can be used to create a substring by extracting elements from another string the same principle can also be applied to different collections as well but in this video we're going to be focusing on slicing strings now to slice a string we can either use the indexing operator which is a set of square brackets or by use of the slice function
41:33
to create a slice object this is a little more complex we'll discuss the slice function near the end of this video so with slicing there are three optional arguments three fields that we can fill in depending on where and how we want to slice our string a starting index a stopping index and a step let's begin with the starting index now for this example let's say we have a string variable called name and this will store a value of whatever your own full name is yes i legally change my
42:04
name to bro code you can use your own name for this if you want so what i would like to do is to create a substring based off a sliced portion of my full name so what i would like to do is create a substring based off of just the first part of my name just bro so let's begin by creating a substring maybe i'll call this first name equals now to slice a string type in a string or the name of a variable that contains your string
42:34
and follow this with the indexing operator and like i said we'll discuss the slice function a little bit later so we need a starting index and a stopping index but we'll begin with the starting index where do we want to begin our slice so computers they always start with zero so the first character in your string will have an index of zero i would like my slice to begin at index 0 so i'll list that for the starting position the starting index so let's test this just to be sure it
43:05
works i'll just print first name and let's see if this prints capital b which it does so following that same pattern the next character within my string would have an index of one that would be r then the next index two would be o and three should be an empty space all right so that is the starting index within string slicing if we would like to slice an entire portion of our string not just one letter we need a stopping
43:37
index as well and both of these fields are separated with a colon so let's say i would like to slice the first three characters of my string so this would have an index of zero one two and this isn't going to work as you would imagine and i'll explain why so you would think that indexes 0 through 2 would give us three letters right but it only gives us two so this first index is inclusive the stopping index is exclusive so if i
44:08
want the first three characters the first three letters in the string i would need the starting index to be zero and the stopping index to be three and now this should print my first name and not anything else there is a shorthand way of writing this too so if you were to leave this first index blank for start well python is going to assume that this is zero the beginning of your string so this would be no different than what we had previously just having colon
44:38
three now let's try and create a separate substring for a last name so last name name now we need a starting index and a stopping index so we need the starting index at least so this would be zero one two three four four colon and where do we want this to stop zero one two three four five six seven and remember that this is exclusive
45:09
so we're going to place eight here and now i would like to display last name and this should just display code and a shortcut is that if you were to leave stop blank but you still have that colon this is a shorthand way of writing i would like every character starting at index four and everything after until the end so there would be no change to this as well you can write it either way okay so we still have to discuss step step is an optional field that we can set a value to
45:40
so step is how much we're increasing our index by between starting and stopping so it is entirely possible to create a substring that will count only every second character after the first normally step is one by default but if we were to set that to two we're going to count only every second character so let's try this maybe i'll create a new variable called funky name because i really don't know what else to call this for this example let's say funky name equals name and i'm going to count
46:11
only every second character including the first so the indexes would be zero colon eight for my example but if you're using your own name that's probably going to be something different so the default first step is one and if i were to display funky name as it is currently this will just display my full name but if we set step to two this will only display every second character including the first so funky name is now for me at least b o c d now if step
46:42
was three this will print only every third character including the first where it's b space d a shorthand way of writing this is that you could leave start and stop empty but the important thing is to have these two colons python is going to assume that if you leave your starting index empty you're referring to index 0 the beginning if your second index your stopping index is empty python will assume this will be the very end of your string so just having two colons and then step
47:13
three would be no different than what we had previously or this next part i'm going to explain how we can reverse a string in python so let's create a new substring variable called reversed name equals name i would like to use the entire string so i can just use two colons python will fill in the rest so for the step this is going to be negative one it's kind of like we're counting backwards and we're going to create a new sub string based off my name but in reverse
47:44
so if i were to print my reversed name this is now edok orb i think that's how you pronounce it so that's how to reverse a string in python using slicing now let's move on to part two of this video i'm going to be explaining the slice function and we can use the slice function to create a slice object which is actually reusable so that's somewhat convenient let's take a different scenario this time let's say we have a website url website equals http colon2forwardslashes
48:16
google.com but use whatever website you want what i would like to do for this program is remove and create a substring based off the website name and not anything else i would like to exclude the http portion as well as the dot-com portion of this website url so let's break it down step by step our first step is to create a slice object so i will name the slice object as slice slice equals and i will invoke the slice function so within the parentheses of the slice
48:46
function we can add up to three values a start a stop and a step exactly the same as what we did with indexing however with the slice function we separate each of these values with a comma instead of a colon but it works the same way so we would like our substring to begin where the name of the website begins so we would like our start to be zero one two three four five six seven so i will pass in seven for where my substring will begin
49:17
and now each value within the slice function will be separated with a comma instead of a colon like what we did with the indexing operator now for the stop index that's going to be a little bit tricky and let me explain why one issue that we're going to run into is that not all website names are consistent with their length they can really vary so it is not possible for us to use the same stopping index for each of these website urls so one way in which we can account for that is to use what is called a negative
49:48
index each character within a string has a positive index as well as a negative index and a negative index works almost exactly the same way except the character most on the right begins with a negative index of minus one and the character on the left of that would be negative two negative three and negative four it's as if you're counting backwards so with indexing we can use some combination of positive and negative indexing so i would like
50:19
my substring to end where this dot is so this would have a index of minus one minus two minus three minus four and remember that this is exclusive so i would like my slice to begin at index seven and a negative index of four for the stopping position so that will give me just the website name and remove the http portion as well as the dot com portion of my website url and now we have a slice object and we can reuse this
50:50
so to apply your slice object type in the name your string index operator and place your slice right within here and let's print this so print our website and apply the slice to it and now this should give us just the website name and now let's test this with maybe another website i'll rename this as website one and let's create website2 and let's try a different website name for goodmeasure so let's try wikipedia so we can reuse
51:22
our slice object and we will apply our slice to website two this time and this should remove the url portions http as well as com all right everybody so that is slicing strings in python you can either use the indexing operator or the slice function to create a slice object if you would like a copy of all this code i will post all of this in the comments section down below but yeah that is how string slicing works in python
51:52
hey what's going on people it's you bro hope you're doing well and in this video i'm going to explain if statements else if statements and else statements and python so sit back relax and enjoy the show all right people let's talk about if statements an if statement is a block of code that will execute only if its condition is true it's a very basic form of decision making with programming for this example let's create a prompt where we will ask a user for their age and depending on what
52:24
their age is we will print a response so let's say age equals input and then we will add a prompt how old are you now this will return a string so we should probably cast this as an integer data type so we now have a variable called age where we will accept some user input let's check somebody's age with a variety of if statements so let's check to see if somebody's age is greater than or equal to 18 so to create an if statement type if and then some sort of condition if age is greater than or equal to 18
52:56
to finish this if statement add a colon at the end now pay attention to this if i move down to the next line you'll notice that my cursor is now indented any indented code underneath an if statement is the block of code for that if statement so if this condition is true we will execute some block of code if it's false we'll skip over it so if somebody's age is greater than or equal to 18 let's print you are an adult and let's try this program how old
53:27
are you let's say i'm 21 so my condition when we get to the if statement is going to check to see if this condition is true if it is true we're going to execute this block of code if it's not we skip over it so how old are you let's say i'm 21 i'm going to hit enter and my if statement is true so we execute this block of code which prints you are an adult so what if this condition is false let's say i'm 12. well then we're just going to skip over this block of code and continue on with the rest of the program if you would like your program to take
53:58
some other course of action if this condition is false we can add an else statement so after the if statement add else make sure you get the indentation right you don't want this within the block of code for the if statement so if this condition is false we can take some other course of action so if somebody's age is less than 18 let's print you are a child so let's try this again i'm going to enter that i
54:29
am 12 years old not really but let's just pretend i am so this will now print you are a child if this condition is false perform this block of code for the else statement so with if statements we can check more than one condition before reaching the else statement by using an else if statement and we add that after an if statement so it's shortened to e l i f for else if so let's check to see if somebody's age is less than zero for some reason
55:00
so let's print you haven't been born yet so with if statements we start at the top we first check the if statement if this is false we move down to the next statement to check then we check our else if statement if this is also false then we resort to the else statement the else statement is sort of like a last resort if all conditions evaluate to be false then we execute the else statement
55:31
so let's try this how old are you let's say i'm negative 1 years old you haven't been born yet so with if statements we always begin with an if statement and we check that condition if that condition evaluates to be false then we will skip this block of code and move down to the else if statement if there is one it's optional if this condition is also false then we will use our else statement as a last resort if there is one if there is no else statement then we just skip over all of
56:02
this then and continue on with the rest of the program so you can add more than one else if statement so let's check something else let's check to see else if age is equal to 100 so if you need to check to see if a value is equal to a particular value make sure you use double equal signs this is the comparison operator for equality if you use just one equal sign that's the assignment operator and python thinks you're attempting to set
56:34
age equal to 100 so if you want to check to see if age is equal to 100 then use double equals so let's check to see if somebody's age is equal to 100 so let's print you are a century old now pay attention to this if i were to run this program how old are you let's say i'm 100. so this is printing you are an adult that's because we first check our if statement and well we're 100
57:06
years old and 100 is greater than or equal to 18. so we print this block of code we execute this block of code and then we skip everything else even though age is equal to 100 within our else if statement so the order of your if statements does matter so let's change this around let's add this to the very beginning and this will be our if statement and we will set age is greater than or equal to 18 to be an else if statement
57:36
so we're first going to check to see if age is equal to 100 if not we will check to see if age is greater than or equal to 18 and we just follow that order so let's try this again how old are you i am 100 years old you are a century old so we first check our if statement if this condition is true we will execute this block of code if it's false we just go down the order till we reach our else statement well everybody that is the basics of if statements we have if statements
58:08
else if statements and else statements so if you would like a copy of this code i will post this in the comments down below but yeah that is how if statements work in python hey what's going on everybody it's bro hope you're doing well in this video i'm going to explain logical operators in python so sit back relax and enjoy the show all right everybody let's talk about logical operators these are used to check if two or more conditional statements are true so we'll be discussing the and
58:40
as well as the or logical operators there's also a third one called not but that works a little bit different and i'll explain why so let's create a program to demonstrate this let's create a program where we will ask somebody for the temperature outside and then depending on the temperature if it falls within a certain range we will print a message so we'll need to use and as well as or logical operators to do this so here i have a prompt temp for temperature equals input what is the temperature outside so i'm going to cast this as an integer
59:10
data type so what if we want to check to see if our temperature is within a certain range we can use the and logical operator to check two or more conditional statements this is how we might do that let's check if temp is greater than or equal to zero zero degrees celsius and we can check another condition too if temp is less than or equal to 30 degrees celsius so we need to add a colon at the end to
59:40
finish this if statement and let me just fix some of the spacing okay so with the and logical operator in order for this entire condition to be true both conditions must be true this needs to be true and this needs to be true so if our temperature is equal to or above 0 as well as being less than or equal to 30 that means our temperature falls within a certain range so let's print a message such as the temperature
01:00:12
is good today print go outside all right let's try it so what is the temperature outside let's say it's 20 degrees celsius well the temperature is good today go outside this condition is true 20 is greater than or equal to zero and temperature is less than or equal to 30. let's try a different temperature like negative 20 degrees celsius so it's really freaking cold outside right now as you can see nothing happened because
01:00:46
our first condition was false our temperature is not greater than or equal to zero but our temperature is less than or equal to 30 so our second condition is true but our first condition was false and in order for this entire statement to be true since we're using the and logical operator both conditions must be true in order for this statement to be true now let's discuss the or logical operator we can check to see if our temperature is below or above a certain range so let's write else if
01:01:17
temp is less than zero or temp is greater than 30. so that means it's going to be either very cold outside or very hot outside so let's print a message the temperature is bad today print stay inside all right let's try this again what is the temperature outside it is negative 20 degrees outside the temperature is bad today stay inside
01:01:55
so with the orological operator as long as one of these conditions is true then the entire statement is true it doesn't matter if one of them is false as long as one of them is true then the entire statement is true last but not least we have the not logical operator this works a little bit different from the and as well as the or logical operator because we can check one or more conditional statements instead of two or more conditional statements so what the not logical operator will do is that it'll take a conditional statement if it's true
01:02:26
it's going to flip it to false if it's normally false it's going to flip it to true so for this example we have another way in which we could write this although it's a little more complex so if our statement is true what we can do is that we can flip it to false using the not logical operator if it's normally false we can change it to true so what we'll do is surround one or more conditional statements with a set of parentheses and precede everything within the parentheses with the not logical operator so if the statement is normally true
01:02:56
it's going to become false using the not logical operator if it's normally false it's going to be true then let's do the same with the else if statement so surround one or more conditional statements with the not logical operator so now these roles are going to be kind of reversed so let's try this again what is the temperature outside it is negative 20 degrees outside the temperature is good today go outside so what we should probably do is kind of reverse these rolls so i'm just going to swap everything within these if statements with each other
01:03:27
and now this should work the same as it did before but we just wrote it a little bit different what is the temperature outside it is 15 degrees outside 15 degrees celsius the temperature is good today go outside so with the not logical operator you can surround one or more conditional statements with the not logical operator and what this will do is flip it from being false to true or from true to false well everybody that's the basics of logical operators in python if you would like a copy of all this code i will post this in the comments
01:03:58
down below but yeah that's the basics of logical operators in python hey what's going on everybody it's bro hope you're doing well and in this video i'm going to explain while loops in python so sit back relax and enjoy the show so while loops a while loop is a statement that will execute its block of code as long as its condition remains true later on in this video for this example we're going to create a program where we will prompt a user to enter their name if they attempt to skip that prompt then
01:04:31
we will continually ask the user to enter their name and they can't continue on with the rest of the program until they do so the key thing to keep in mind with while loops is that you'll want some way to eventually escape the while loop if you don't have any way to escape the while loop that's referred to as an infinite loop so let's create an example of an infinite loop because it's fun so to create a while loop type while and then some sort of condition so for our condition let's say while one is equal to one so this will always be true now what do we want to do when we execute this block of code let's print something
01:05:03
print help i'm stuck in a loop all right so let's try this while one is equal to one print this statement and we will just continue on and on within this while loop because we have no way to escape this while loop so now let's use this concept and write a program where we will prompt a user to type in their name if they don't type in anything then we will continue to prompt them to type in at least something so let's say name is equal to
01:05:35
and then to just keep this blank we'll just set this to a set of quotes and then let's write a while loop here while name and let's check the length of name if the length of our name is equal to zero then we will ask the user to type in their name name equals input enter your name so then once we escape the while loop let's print something print hello plus
01:06:07
name whoops hello plus name alright so when i run this we are giving the user a way to escape the while loop they have to type in something for their name as long as the length of my name is equal to zero keep on printing this prompt enter your name so i'm just going to hit enter a bunch of times enter your name no enter your name no enter your name no so we are stuck within this loop until we enter something for our name so this time let me type in a name then hit enter so therefore our condition is now false
01:06:39
the length of my name is now one two three so three is not equal to zero therefore our while statement our while loop is false and we continue on with the rest of the program there are a few variations in which you could write the same program so let's write this a different way what we could do is say name is equal to none while not name and this will pretty much do the same thing it's just another way of writing this well everybody that's the basics of while loops a while loop is a statement
01:07:10
that will execute its block of code as long as its condition remains true for our example we asked the user to type in their name if they were to leave it empty or blank we would keep on asking the user to type in their name so if you would like a copy of this code i will post this in the comments section down below but yeah that's the basics of while loops in python hey what's going on everybody it's bro hope you're doing well and in this video i'm going to explain for loops in python and at the end of this video we're going to create a countdown timer so
01:07:40
sit back relax and enjoy the show all right guys and gals let's talk about for loops a for loop is a statement that will execute its block of code a limited amount of times it's similar but different from a while loop because a while loop could iterate an unlimited or infinite amount of times depending on the condition a for loop will only iterate a limited amount of times and before we start executing its block of code we already know how many times we're going to repeat this block of code
01:08:12
so for this example let's create a for loop that will simply count it to 10 and then we'll create a few more sophisticated examples so to create a for loop that will count up to 10 this is what we'll type four and then we need some sort of counter people usually either write index or they shorten it to just i i for index four i in and we can set a range range ten so we will execute this for loop ten times and to finish this for loop just add a colon at the end so the next line or lines will be
01:08:43
indented so that's the block of code for the for loop what do we want to do during each iteration let's just print what i is and let's take a look to see what would happen so we executed this for loop ten times except with computers computers always start with zero so when we said four i in range ten we're counting numbers zero through nine so it's as if this number is exclusive so there's two ways we can change this we could say range ten plus one or what i would do is say print i plus one so this will give us numbers
01:09:15
one through ten so that's a for loop it will execute a limited amount of times this time let's count a range between two numbers not necessarily 0 to 10 but something else so let's say 4i in range maybe the numbers 50 through 100 so within my range function i will pass in two numbers the first number will be the starting point and the second number is the ending point this first number is inclusive and the second number is exclusive so let's print this print whatever i is
01:09:46
so this will count 50 through 99 actually if you wanted to include this last number since it's exclusive what we could do is just add one to the end so this would give us 50 through 100 except that this will iterate 51 times since we're counting 50. so you just have to pay attention to your range and the numbers that it's counting now one thing that you can do with the range function is that you can add a third argument and this will function as the step how much you want to count up or down by so this time let's count up by two
01:10:17
so i'll add comma two we're passing in a third argument this time so this program will now count up by two starting at fifty and go all the way to one hundred a benefit of for loops is that we can iterate through anything that is considered iterable this could include a string the letters in a string or any sort of collection so this time let's create a for loop that will iterate once through each letter in maybe a name so for i in and let's type a name here type in whatever your first name and last name
01:10:49
is and then we will print whatever i is for i in whatever your name is print each letter in the string so when i run this each letter within my name will be printed to a new line all right people for my last trick we're going to create a program where we will simulate a countdown starting at 10 and count down to zero and then maybe once we reach zero we can print something such as happy new year so to create this program we'll need an import so at the top
01:11:19
we're going to import the time module because we'll be waiting one second after each iteration of this for loop so to create this program let's say four you could say i but you can really write anything here and to demonstrate that let's say four seconds four seconds in range so the starting point is going to be ten the ending point will be zero and we'll add a step what do we want to count up or down by let's set this to negative one so this will be a countdown starting at ten and ending at zero so
01:11:49
after each iteration let's print whatever i is or in this case seconds and then we can sleep have our thread sleep for a number of seconds too so after we print seconds type time dot sleep and pass in how long you want to sleep for i would like to sleep for one second and then at the end let's print happy new year make sure this isn't within the for loop print happy new year and well that's it let's test this so we'll begin
01:12:21
at 10 and count down to zero and you can see that my thread that is running this program is sleeping for one second after each iteration of this for loop then once we reach zero it will display happy new year well everybody that's the basics of for loops a for loop is a statement that will execute its block of code a limited amount of times it's different from a while loop because a while loop could execute an infinite or unlimited amount of times with for loops they execute limited amounts of times
01:12:51
and before we enter the for loop we already know how many times we're going to iterate this for loop if you'd like a copy of all this code i will post this in the comments down below but yeah that's how for loops work in python hey what's going on everybody it's you bro hope you're doing well and in this video i'm going to explain nested loops in python so sit back relax and enjoy the show hey all you people let's talk about nested loops a nested loop is a general concept of having one loop inside of another loop and
01:13:22
that's really it it doesn't matter if it's a for loop or a while loop so the inner loop will finish all of its iterations before we finish one iteration of the outer loop and to best demonstrate this we're going to create a program where we will draw a rectangle made out of a certain symbol that we choose we need to set a width and a height and to best do this we'll need to use a nested loop we'll create a few prompts one for rows columns and a symbol that we want to make a rectangle of so let's begin with rows rows equals
01:13:52
input how many rows and we'll need to cast this as an integer data type because we're working with numbers okay let's do the same thing for columns so replace rows with columns for this next line input how many columns and we'll create a symbol variable symbol equals input and this we don't have to cast enter a symbol to
01:14:26
use now it's time for the nested loops we're going to create an outer for loop as well as an inner for loop the outer for loop will be in charge of the rows the inner for loop will be in charge of the columns so let's begin by creating our outer loop so we'll write 4 i in range rows we would like our outer for loop to iterate as many times as we have rows and we're going to create an inner for loop that will iterate as many times as we have columns so within the block of code for the
01:14:58
outer for loop we're going to create another for loop this time it'll be four and a common convention for inner loops is to write j as an index because j comes after i i guess so for j in range columns so our inner for loop will iterate for as many times as we have columns all we'll do within the inner for loop is print our symbol that we have except we'll have one issue using a
01:15:31
print statement after we use the print statement we will enter a new line character and move down to the next line we can actually prevent that by adding comma and equals quotes so after using a print statement this will prevent our cursor from moving down to the next line so pay attention to the indentation too so there's kind of two levels of indentation we're currently within the inner for loop so that's pretty much it for the inner for loop now we're going to print a new line once we exit the inner
01:16:02
for loop and you can see that the indentation is on this line right here we're within the outer for loop now and that's pretty much it so let's try this program how many rows let's say five rows how many columns perhaps six what symbol do we want to use let's say the dollar sign and let's try it there we go there's our rectangle so we have let's see six columns one two three four five six and five rows one
01:16:33
two three four five in summary a nested loop is really just this concept of having one loop inside of another loop it doesn't matter if it's a while loop or a for a loop it's really situational the inner loop will finish all of its iterations before finishing one iteration of the outer loop so what we did to demonstrate that is that we had the outer loop in charge the rows and an inner loop in charge of the columns for our rectangle program so if you would like a copy of this program i will post this in the comment section down below
01:17:04
but yeah that's the basics of nested loops in python hey what's going on everybody it's bro hope you're doing well and in this video i'm going to explain loop control statements in python so sit back relax and enjoy the show all right ladies and gentlemen let's talk about loop control statements these are used to change a loop's execution from its normal sequence and there are three we're going to discuss break continue and pass let's begin with break break is used to terminate the loop entirely
01:17:37
now here's a good example of where a break would be useful let's say while true we're going to continually ask for somebody's name if they don't type in anything then we'll continue the while loop over and over again so let's say name equals input enter your name now if name does not equal a set of quotes that means they don't type in anything then we will break and break out of this while loop and let's test it
01:18:10
enter your name nah i don't think so i'm too lazy to enter my name all right i can see that i'm not going to escape this while loop so i'm going to type in my name because i give up and hit enter therefore i will break and exit out of this while loop so a break is used to terminate the loop entirely when it's encountered next we have continue continue skips to the next iteration of the loop now let's say we have a phone number phone underscore number and i will set the sequel to let's say a random phone number
01:18:40
including dashes 123-456-7890 what i would like to do is to display this number within the console window without these dashes this is how to do that using a continue control statement for i in phone number so for each character within our string of phone number what we'll do is check to see if we encounter a dash character if i i is our index is equal to a dash then we want to
01:19:15
continue and skip over this iteration of the loop so continue then i would like to print whatever our indexes whatever character we're working on so if i were to run this as it is it's going to print each digit on a new line so with print statements they will add a new line character to the end of your string and you can actually change that by adding to the end comma end equals and then a set of quotes so
01:19:47
this will print my phone number without any dashes one two three four five six seven eight nine zero so that's a use of the continue control statement it skips to the next iteration of the loop last but not least we have pass pass does nothing it acts as a placeholder let's say that i would like to print the numbers 1 through 20 using a for loop for i in range 1 through 21 and remember that the second digit is exclusive so this will iterate once through the digits of 1
01:20:18
through 20. let's pretend that i'm highly superstitious and i don't want to print the number 13 because 13 is considered an unlucky number if i is equal to 13 what we'll do is pass and pass acts as a placeholder and it won't do anything else print i that's one i there we go this will print the numbers 1 through 20 and skip 13 because we used the past control statement which does nothing it acts as a placeholder
01:20:49
so everybody those are loop control statements they change a loop's execution from its normal sequence we have break continue and pass so if you would like a copy of all this code i will post all of this in the comments section down below but yeah those are a few loop control statements in python what's going on everybody it's bro hope you're doing well and in this video i'm going to explain lists in python so sit back relax and enjoy the show all right ladies and gentlemen let's get
01:21:21
down to business i got to explain lists to you all now a list is used to store multiple items within a single variable for example let's say we have a variable called food and i will store a string value of pizza because i like pizza what we could do is that we can store multiple items within this variable by turning this variable into a list and in order to do so we're going to surround all of the values that we would like to add to our list with a set of square
01:21:52
brackets and boom there you have it we now have a list called food it is no longer a variable so what we could do is add multiple items to this list of food so let's add some other food items that we might like let's see i also like maybe some hamburgers and a hot dog and maybe some spaghetti i think i smelled spaghetti right okay so we now have a list called food now what happens if we attempt to print
01:22:25
this list of food so print food so this will print all of the elements found in this list each item in a list is referred to as an element if we need to access a certain element of this list we have to list the index so next to our list we'll add a set of square brackets again and we need to list the numbered index of the element that we're trying to access now computers they always start with zero so the first
01:22:55
element in our list would be element number zero the next one would be one two and three so if we need to access element zero which is in the first position we would say food square brackets and then within here list the index so the first position is zero and this will now print the first element of our list which is pizza let's attempt to access some of these other elements so the element at index one would be you guessed it hamburger two
01:23:28
would be hot dog and three would be spaghetti now what happens if we attempt to access the element at index four well currently that's out of range because we did not assign a value so we're experiencing an error an index out of range error but if i were to add another element let's say pudding alright well then the element at index 4 would be putting then one important concept with lists is that you can always
01:23:58
update and change the elements found within a list later on in the program after you declare one so let's say we would like to immediately change one of these elements let's say food at index zero i want to replace with maybe sushi because sushi is great so if i were to print the element at index 0 this would no longer be pizza because we updated element 0 to be sushi then and this prints sushi now if you need to display
01:24:29
all of the elements found within a list you can easily do so with a standard for loop what we'll do is say for x in the name of our list which is food print x and let me turn this line into a comment all right so this for loop will print all of the elements found within our list of food and the results are sushi hamburger hot dog spaghetti and pudding all right people for this next portion i'm going to demonstrate a few useful
01:25:01
functions of lists and to access some of these functions type the name of your list dot and there's a bunch to choose from here let's begin with event we can add an element to this list let's say at the end i would like to add ice cream so then if i were to display my list we now have ice cream at the end because we appended this value to my list of food so let's go over a few others so we can also remove a value food dot remove let's say i would like to
01:25:32
remove hotdog so i'm going to type that within my function and this will remove hotdog and that is no longer here next we can pop food.pop pop will remove the last element so this will remove pudding we can insert a value at a given index food dot insert we need to list an index let's say zero that would be the first
01:26:03
position technically that's where pizza is currently and i would like to add cake and at index 0 we now have cake so next up we have sort food dot sort this will sort a list alphabetically so in the first position we have hamburger hot dog pudding spaghetti then sushi we can also clear a list food dot clear and this will remove
01:26:34
all of the elements of a list so this will not print anything because we cleared the list well everyone in conclusion a list is really just a variable that can store multiple values think of it that way so if you would like a copy of all the code that we have written here today i will post all of this in the comments down below but yeah that's how lists work in python how's it going everybody it's bro hope you're doing well and in this video i'm going to explain 2d lists in python so sit back
01:27:05
relax and enjoy the show i have a super quick video for you all today on 2d lists also referred to as multi-dimensional lists all it is is a list of separate lists so let's begin by creating a few separate lists let's say we have a list called drinks and let's assign a few values let's say we have coffee as well as soda and maybe some tea all right so we currently have one list let's make maybe two more let's make a
01:27:39
list called dinner and maybe in here we'll place pizza then hamburger and hot dog okay then let's create one more for this example let's say dessert and maybe we'll add two items they all don't need the same amount of items or elements so we have cake and ice cream all right so we have three lists one called drinks dinner and dessert so what we can do is add
01:28:16
all of these lists to one list and let's create a list called food food equals and for each of these elements we're going to place our lists so we have drinks dinner and dessert check this out what if i were to print my 2d list of food so what ends up happening is that this will print all of the elements found within each individual list and they're all grouped together so this first portion is my drinks list
01:28:48
which contains coffee soda tea this next element is my dinner list which contains pizza hamburger hot dog and the last element dessert contains cake and ice cream if i need to access just one of these lists i will add an index after my food 2d list and set an index number so index zero is referring to my first list of drinks and this will display all of the elements found within my first list
01:29:18
and if i need just one of these elements i will add a second set of square brackets and list the index of the item that i'm trying to access so this very first item coffee would be index zero and then index zero again so we need two sets of square brackets and if i were to print this this will display coffee so if i go down the list the next element would be soda and then t if i were to change the index within this first set of square brackets well
01:29:50
now we're working with a different list this time we're working with our dinner list so the element at index 1 index 2 that would be our dinner list and this will display the element of hot dog if i change this to the next list which would be dessert well we're going to receive an index out of range error because we only have two values within this list cake and ice cream there is no element at index 2 because we only added two
01:30:21
elements to this list of dessert all right everybody so that is 2d lists also referred to as multi-dimensional lists it's a list of separate lists if you need to access one of the elements within your 2d list you need two sets of square brackets so if you would like a copy of this code i will post this in the comments down below but yeah that's how 2d lists work in python hey what's going on people it's you bro hope you're doing well and in this video i'm going to explain
01:30:51
tuples in python so sit back relax and enjoy the show so tuples they are collections which are ordered and unchangeable they're very similar to lists but they're ordered and we can't change them they're useful for grouping together related data let's say we would like to create some sort of student record so we can create a tuple to take care of that for us so let's say we would like to create a tuple called student the process of creating a tuple is very similar to lists but instead of using a set of square
01:31:24
brackets to place all of the values within we're going to use a set of parentheses and now we can add a bunch of values related to this collection of student so let's add some student information let's say that this student's name is bro let's give him an age as well as a gender all right so there you have it we now have a tuple called student and let's take a look at some of the functions related to tuples and you can access them by typing student dot and then there are a few here not as many as lists we have count and we have
01:31:55
index so we can find the count of how many times a value appears let's count how many times the value of bro appears so we'll need to put this within a print statement so we can actually see it so bro appears one time within this tuple there is also an index method as well so type the name of your tuple dot and we will use the index method we can find the index of a certain value let's say we would like to find the
01:32:26
index of mail and then i will just print this to the console window so the index of the value mail would be at two zero one two that's it for the methods available to tuples a few tricks that you can do with tuples is that you can display all of the contents within a tuple using a for loop let's say 4x in student we will print x so this will iterate once through all the
01:32:57
values found within our tuple of student and we can also check to see if a certain value exists within our tuple using an if statement let's say if bro in student if this is true then what do we want to do let's say print bro is here all right so since bro is within here this will be true and this will print our statement of bro is here all right everybody like i said this is a super
01:33:29
quick video on tuples they're very similar to lists except they are ordered and unchangeable if you'd like a copy of all this code i will post all of this in the comments down below and don't forget to smash that like button drop a comment down below and subscribe if you'd like to become a fellow bro hey what's going on everybody it's you bro hope you're doing well and in this video i'm going to explain what a set is in python so sit back relax and enjoy the show a set is a collection which is unordered
01:34:03
as well as being unindexed they do not allow any duplicate values for example let's create a set of silverware so in order to create a set we need a name for this set let's call it utensils equals and in order to create a set we need to surround all values with a set of curly braces now let's add a few utensils to the set called utensils let's add a fork a spoon and maybe a knife and that should be good
01:34:33
and let's display all of the values for x in utensils print x now with sets they're different from lists because they're unordered as well as being unindexed if i were to print all of the elements in utensils they might not necessarily be in the same order in which we placed them for example when we printed all of the elements found within utensils first we had a knife a fork and then a spoon if i were to run this again they might be in a different order kind
01:35:07
of like that so a set is actually faster than a list if you need to check to see if something is within a set compared to a list and they do not allow any duplicate values let me add a bunch of knives and let's see what happens so i'll have a total of three knives but when we print all of the elements of the set only one knife appears so here's a few useful methods of sets the first method is that we can add an item to our set
01:35:37
so type in the name of the set in this case it's utensils dot and here's a bunch of methods that you can use i am looking for add and we can add an element to the set i would like to add a napkin and then if we were to print the set we do have a napkin within the set we can also remove an element utensils dot remove and i would like to remove the fork and now our fork is gone we can also
01:36:11
clear utensils dot clear and all the elements within our set should be gone so let me turn these lines into a comment all right for this next example we'll need a second set let's create a second set called dishes and then we need a set of curly braces and let's add a bowl a plate and a cup our next method is that we're going to
01:36:43
add one set to another by using the update method let's say we would like to add our dishes set to our utensil set so let's type utensils dot and use the update method and within this method we will pass in the set that we would like to add so utensils update dishes this will add all of the elements found within dishes to our utensil set and you can see that when we print our utensils we have a bowl a plate and a cup in here as well and then if i were to switch these
01:37:14
around let's say dishes update utensils and then i will print everything found within dishes now there's a fork a spoon and a knife in here as well we can also join two sets together and create a new set entirely so let's create a set called dinner table like we're setting up a dinner table and we need a fork a spoon a knife a bowl a plate and a cup so we have a dinner table set
01:37:44
we're going to set the sequel to either utensils dot union dishes or we could do dishes union utensils either way would work and let's display all the elements found within dinner table and we should now have elements from both sets utensils and dishes as well there are also some methods in which we can compare the similarities as well as the differences between the elements found within two sets let's
01:38:14
say i would like to check to see what utensils has that dishes doesn't and for this example i think i'll add a knife to my dishes set just so that they have at least one thing in common so i would like to see what utensils has that dishes doesn't so we can do so using the difference method and i will print the results with a print statement so i'm going to compare utensils against dishes utensils
01:38:45
dishes and this will print what utensils has that dishes doesn't which is a fork as well as a spoon and we can reverse the rolls too we could say dishes difference utensils what do dishes have that utensils doesn't and dishes has a cup a bowl and a plate they both have knives so that's why knife isn't appearing we can also check to see if there's anything that they have in common using the intersection method so i'll
01:39:16
turn this line into a comment we'll print utensils dot intersection dishes and this will return whatever element that they have in common which is a knife all right everybody in conclusion a set is a collection which is unordered as well as being unindexed they do not allow any duplicate values and you can do things like compare two sets see if there's anything that they have in common any differences and we can add or remove elements to a
01:39:51
set if you'd like a copy of all this code i will post all of this in the comments down below don't be afraid to smash that like button drop a comment down below and subscribe if you'd like to become a fellow bro hey what's going on everybody it's you bro hope you're doing well and in this video i'm going to explain how dictionaries work in python so sit back relax and enjoy the show all right let's get down to business a dictionary is a changeable unordered collection of
01:40:22
unique key value pairs they're fast because they use hashing and they allow us to access a value quickly now to create a dictionary it's very similar to creating a set based on the last video except we're going to store unique key value pairs let's create a dictionary of countries and their capitals we can store those as key value pairs so let's call this dictionary capitals capitals equals then we need a set of curly braces much like what we use
01:40:53
with sets now we need a key and a value let's say we have the usa as a key and the capital of the usa would be washington dc so in order to associate a value with the key we'll follow the key with a colon and then add some value and the data type really doesn't matter but in this case we're just using strings so the capital of the usa would be washington dc and to add another key value pair we'll separate each with a comma and let's add a few others for good
01:41:25
measure so let's say we have india next as a key and the value would be new delhi and we'll add two more let's say we have china and the capital of china is beijing and lastly let's add russia and the capital of russia is moscow and there you have it ladies and gentlemen we now have a dictionary called capitals that has unique key value pairs so
01:41:57
remember with dictionaries they are unordered in order to access one of these values instead of using a numbered index we're going to use the associated key with that value here's an example let's say i would like to print the capital of russia so i need to use this key of russia so in order to do this type in the name of the dictionary followed by a set of straight brackets and then instead of an index number like 0 or 1 2 3 i'm going to use the key so i would like to print
01:42:28
the value that's stored or associated with the key of russia so when i run this this will print that value which is moscow so this isn't always safe let's say we have a key that doesn't exist like germany germany is not currently within my dictionary so if i were to run this well then my program is going to encounter an error and this will interrupt the normal flow of my program a much safer way to access a key to check to see if it's there or not is
01:42:59
to use the get method of dictionaries so i'm going to turn this line into a comment and let's try this again but this time we'll use the get method so type in the name of the dictionary dot get and list the key that you want to use so i would like to see if germany is within my dictionary currently there isn't so this will return none and we will not encounter an error so this is a much safer way of checking to see if there is a key within your dictionary
01:43:30
here's a few other useful methods there is a method to print only the keys so we type in the name of the dictionary capitals and here's a listing of all of the methods that we have access to i'm looking for the keys method and this will print only the keys and not the values we can also print just the values and not the keys so type in the name of the dictionary again capitals dot values and this will print only the values or
01:44:02
you could print everything both the keys and the values and you do so by using the items method capitals dot items and this will print your entire dictionary one other way in which we can display all of the key value pairs in a dictionary is to use a for loop so at the end let's create a for loop for key comma value in capitals dot items
01:44:33
this is going to iterate once for each key value pair in my dictionary for each key value in capitals dot items print key comma value and this will print my entire dictionary a feature of dictionaries is that they are mutable that means we can change them or alter them after the program is already running so one way in which we can do that is to use the update method of dictionaries so let's add germany as a key and give
01:45:06
it a value of berlin so type in the name of the dictionary capitals and we'll use the update method and within the parentheses of this method we'll add a set of curly braces and now we can add a new key value pair so let's add germany and a value of berlin so if i were to run this and display all of the key value pairs found within my dictionary we now have germany as a key and berlin as its value
01:45:35
not only can you use the update method to add a new key value pair but you can update an existing one let's say we would like to change the capital of usa so capitals dot update parentheses curly braces list the key usa colon and then we can give the usa a new value let's pretend that the united states government has decided to relocate their capital to las vegas
01:46:05
so this will now update our key of usa with a new value and if i were to print all of the key value pairs found within my dictionary of capitals the usa has a new value of las vegas so the last two methods i'm going to cover are both pop and clear you can use the pop method to remove a key value pair so capitals dot pop parentheses and list the key of the key value pair that you would like to remove so let's say we would like to remove china
01:46:37
so we'll list that key and then when we use the pop method that will remove this key value pair from my dictionary and then of course we have clear as well which will just remove everything capitals dot clear and this will clear my dictionary all right everybody so in conclusion a dictionary is a changeable unordered collection of unique key value pairs they're fast because they use hashing and they allow us to access a value quickly
01:47:08
well that's the basics of dictionaries in python if you would like a copy of all this code i will post all of this in the comments down below but yeah that's how dictionaries work in python hey what's going on everybody it's bro hope you're doing well and in this video i'm going to teach you guys all about the index operator in python so sit back relax and enjoy the show all right people let's begin i'm going to be explaining the index operator which is represented by a set of square brackets now they give
01:47:41
access to a sequences elements they include but are not limited to strings lists and tuples for this example we're going to be working with strings because strings are easy to work with let's say we have a name and name equals whatever your first and last name is we can use the index operator to access an element of the sequence our string so let's check to see if the first letter in our name is lowercase and i should probably change that to lowercase for this
01:48:11
example so within an if statement if name and we can add a set of square brackets after our sequence which is a string and we can check a given element of our sequence let's check to see if the first letter is lowercase now with computers they always start at zero if you need to access the first element in a sequence that would be index zero and then the next sequence well the next element in the sequence would be one
01:48:40
then two then you followed that pattern so i would like to check to see if name at index zero that would be the first character is lowercase so there is a method to check that dot is lower this will return true or false if the first letter is lowercase then our if statement is going to be resolved to true and what i could do is that i can perhaps change it to uppercase then so let's reassign
01:49:10
name to equal name dot and there is a capitalize function here and then let's print our name print name so when we run this the first letter in my name is now capitalized so if you need to access an element within a sequence a string or list or a tuple you would add a set of square brackets afterwards and then you list an integer of the element that you're trying to access so here's a few other examples
01:49:43
i'm going to turn this as well as this into a comment and let's say we would like to create some substrings and we can do so using our index operator so i would like to create a substring from the first part of my name so what i'll do is create a new variable first name equals name index operator so you can specify a range for an index operator so you would set the starting position on the left hand side followed by a
01:50:14
colon and then your ending position so i would like the substring to begin at index 0 and end at 3 so that would be 0 colon then three all right and then i would like to make this all uppercase for some reason and then let's print my first name so with the index operator we specified a range we would like to access elements 0 through 3. turn them up your case and assign them to a new variable of first name here's a shortcut that you might find useful
01:50:46
if the index for your range begins with zero you can actually delete that you don't even necessarily need that zero now we just have a colon three and this would work the same as it did before so that's a useful shortcut and we're going to bring it up later if we need to access the last element in a sequence this time let's create a substring of last name so last name equals name index operator this time i would like to access the
01:51:16
element at index let's see 0 1 2 3 4. all right so i'm going to say for colon and we're not really sure where our name our last name is going to end so what you could do is just say colon and then leave the next index blank so that will be the index beginning at four and everything afterwards and let's say two lower to make this all lowercase for some reason so let's print our last name print
01:51:48
last name so this should take everything starting at index 4 and after that and turn it into its own substring of last name now you can access the last element in a sequence by using what's called negative indexing so let's add a exclamation point to the end of our name so let's say we have a new variable called last character and i would like to store
01:52:21
the last character in my name to this new variable this new substring so to access the last element in a sequence within the index operator we can use negative indexing so the last element would be negative one all right and then let's print whatever our last character is so this should print an exclamation point yep which did so then if this was negative 2 that would be the second to last element in a sequence
01:52:52
so now that would be e because e is in the second to last element in my sequence all right everybody so that's the basics of the index operator you can add a set of square brackets after a string a list or a tuple and then list an integer or a range of the elements that you're trying to access so if you would like a copy of all this code i will post everything in the comments down below don't be afraid to help me out and smash that like button drop a comment down below and subscribe if you'd like to become a fellow bro
01:53:24
hey what's going on everybody it's you bro hope you're doing well and in this video i'm going to explain how functions work in python so sit back relax and enjoy the show if you find this video helpful please remember to like comment and subscribe your support will help keep this channel running okay let's do this thing a function is a block of code which is executed only when it is called this process is also known as invoking a function with programming we try not to repeat
01:53:56
code if we don't have to that's why functions are useful because our function can perform some specific task for us some block of code for us whenever it is called so we only need to write that block of code once and if we need to repeat it we just call this function one more time so let's define and create a function we'll create a function called hello and in order to define a function we'll type def and then a unique function name so let's call this function hello and with functions they always end with a set of parentheses
01:54:26
and lastly to finish defining this function we'll add a colon at the end so if you go down to the next line take notice that my cursor is now indented any code underneath a function that is indented belongs to that specific function and will only execute whenever that function is called so for now we need to type in something if you don't know what you want your function to do you can always type pass for now but let's print something print the word
01:54:56
hello and in order to call this function all we need to do within our program is type the name of the function which is hello and then add a set of parentheses because functions always end with a set of parentheses so what this will do is execute my function once and perform this block of code whenever it is called if i were to remove this well our program currently isn't going to do anything because functions only execute their block of code only when it is called so this time
01:55:27
let's print the word hello three times so i will call this function three separate times hello hello hello so when i run this our hello function will be called three separate times now with functions they are not limited to just one line this is an entire block of code so maybe we can do something else too let's print have a nice day now if i were to run this my hello function is going to execute this entire block of code three separate
01:55:59
times hello have a nice day now an important feature of functions is that we can send our function some information and our function can do something with that information that it receives let's say this time i would like to print hello plus somebody's name currently name is an unresolved reference what we can do when we call this function is send our function some information this can be a value a variable a collection all sorts of different things so if you need to send your function some information within the parentheses when you call
01:56:30
that function just list the data that you want to send your function so let's say i would like to send my function a string value of bro whatever your first name is when you send information to a function these are called arguments they are the information that you're sending to a function and when you define that function you need a matching set of what is known as parameters so our hello function is going to receive one argument a string value
01:57:00
we need a matching number of parameters currently this isn't going to work you can see here that hello takes zero positional arguments but one was given our function needs a matching number of parameters so think of it this way when we receive this value we're going to give a nickname to this value so when we receive the string value what do we want to call it well let's call it name so in order to add parameters to your function just list the
01:57:31
parameters within the parentheses of that function and well this should work now we have a matching set of arguments and parameters when we call the hello function this time we are sending one argument over and when our hello function receives this argument we're going to give it a temporary nickname of name or you can call this whatever you want to and then we can use this value for whatever we want within our function now if we were to run this this will print hello bro
01:58:02
have a nice day now i can send maybe a different value let's say hello and then i will send a string value of dude hello dude have a nice day so this isn't limited to just values we can send variables to let's say we have a variable called name equals bro and this time i'm going to send this variable over hello and then send name so these don't need to be the
01:58:34
exact same let's say this is my name and i'm going to send my name over and i will temporarily give this value a nickname of just name and then i can use this variable for something hello bro have a nice day now with arguments you can send more than one value over let's say this time i would like to send two values over so let's send pro maybe for a first name and code for a
01:59:05
second name but you can use your first name and last name so this isn't going to work because we do not have a matching set of arguments and parameters we're sending two arguments over but our function is only designed to accept one argument because we have one parameter so let's rename name as first name and we need two parameters this time so what are we going to call this value when we receive it let's call
01:59:35
this last name so this time we are going to receive two values they will be nicknamed first name and last name so this time let's print hello first name maybe i'll add a space plus last name and this is now valid hello bro code have a nice day with arguments you can mix and match the data types that you're sending as arguments so currently we're sending two
02:00:06
string values as arguments this time let's send an integer value let's say we would like to send an edge over to our hello function so i'll just send maybe the value of 21. so currently this isn't going to work because the hello function takes two positional arguments but three were given when we call this hello function we're sending this argument over but our function doesn't know what to do with this argument so we need a matching parameter for this value that it receives
02:00:36
so let's call this value maybe edge as a temporary nickname and then we can do something with this value when we receive it so let's print an additional message perhaps u r plus age plus years old so if you need to display an integer or number along with the string you do need to convert it to a string so let's just cast our edge as a string when we display this so this should work now we now have a
02:01:08
matching number of arguments and parameters so this will display hello bro code you are 21 years old have a nice day so ladies and gentlemen in conclusion a function is a block of code which is executed only when it is called if i were to remove this function call well then our program is just going to completely ignore this function and this block of code would not be executed when we call this function we can pass information known as arguments but we
02:01:39
need a matching number of parameters set up to receive these arguments there are a few advanced features of parameters but i'll hopefully get to that in future videos we'll just stick with the basics for now of functions so that's the basics of functions if you would like a copy of this code i will post this in the comments down below but yeah that is how functions work in python hey what's going on everybody it's you bro hope you're doing well and in this video i'm going to explain the return statement in python so
02:02:10
sit back relax and enjoy the show all right people let's begin the return statement is used within functions to send python values or objects back to the caller these values or objects are known as the function's return value so do you know how with functions we can pass arguments to a function and then our function can do something with those arguments well not only that but our function can pass some value or object back to the caller so what we'll do for this example is create a function that will simply multiply two
02:02:45
numbers together and return the result back to the caller so let's define a function called multiply and we'll set up two parameters for this function let's say number one and number two and what we'll do is say result equals number one times number two and then at the end we can return something what do we want to return let's return whatever the result is now when we call this function we will pass in two values as arguments let's say we
02:03:16
would like to multiply six times eight so when we run this well we can't actually see anything when we return the result after multiplying numbers one and two it's going to return the result back to the caller in order to see the value that is returned back to the caller one way in which we can do this is to print the value that is returned within a print statement so what we'll do is print the value that is returned after calling the multiply function so 6 times 8 is really great 6 times 8
02:03:47
is 48 now one other thing that we can do is store the returned value within a variable so let's say x equals multiply 6 times 8 and then we will print whatever x is so the result is still the same of 48 now with the return value there is another way in which we could write this that uses less lines of code so we'll delete this first line and instead of returning a variable called result what we'll do is just return
02:04:18
number one times number two and this would work the same as it does before but it's written in less lines of code so that's another way in which you could write this all right everybody so that is the return statement functions can send python values or objects back to the caller these values or objects are known as the functions return value you can either print this value directly to the console window or you can store it within a variable or some other location and well ladies and gentlemen that is
02:04:48
how the return statement works in python what's going on everybody it's bro hope you're doing well and in this video i'm going to explain keyword arguments in python so sit back relax and enjoy the show all right everybody let's discuss keyword arguments these are arguments that are preceded by an identifier when we pass them to a function the order of the arguments doesn't matter unlike positional arguments which do matter and we've been working with positional arguments already
02:05:20
and python knows the names of the arguments that our function receives when we use keyword arguments here's an example of us using positional arguments and then i'll demonstrate the benefits of keyword arguments let's say we have a function called hello that accepts three arguments a first name a middle name and a last name and the output will be hello first name middle name last name so if i were to run this this will display hello bro dude code so we're using positional arguments the order of the arguments does matter
02:05:50
what do you think's gonna happen if i were to replace some of these values let's say i will swap the first argument with the third argument so let's try it again hello code dude bro so obviously the order of the arguments matters and now if we were to use keyword arguments then the order of the arguments doesn't matter but with each argument we need to precede each argument with a unique identifier and that identifier is the name of the parameter we want to associate each argument with
02:06:22
so for my intended last name i will use an identifier of last to match this parameter so dude will be middle middle equals dude and bro will be first even though they're not in the correct order so let's try this again and my output is hello bro dude code so that's all about keyword arguments they are arguments preceded with an identifier when we pass them to a function the order of the arguments doesn't matter unlike positional arguments which do matter and then python knows
02:06:53
the names of the arguments that our function receives when we use keyword arguments so that's it for keyword arguments if you would like a copy of this code i will post this in the comment section down below but yeah that's how keyword arguments work in python hey what's going on everybody it's you bro hope you're doing well and in this video i'm going to explain nested function calls in python so sit back relax and enjoy the show all right people nested function calls these are function calls
02:07:25
inside of other function calls and this is possible because certain functions will return a value and we can immediately use that value as an argument for the next function so here's an example let's say that we have the small program where we will ask a user to type in a whole positive number and let's say i don't let's say i type in negative 3.14 and then i'm storing that value within a variable named num what i'm going to do is convert that number to a floating point number because currently it's a string when you accept
02:07:57
user input i will find the absolute value of num and then round it to the nearest whole number and then print the value so we can actually do this using less lines of code with nested function calls so let's start with the first function which will be accepting some user input the input function so this will be in the very center of our nested function calls and then the next function we would like to apply to the variable that is returned the value that is returned we will pass in as an argument to our
02:08:27
next function of casting this data type to a float so we'll surround our function with another function and use the float function and then we'll just keep on adding layers to this so we have the absolute value function next so we'll add another layer and we have the round function so we'll add a layer to that and lastly we're going to print and that'll be the outermost layer so this program will do the exact same thing
02:08:58
let's say negative 3.14 except we've written this with one line of code so basically we start with the innermost function and resolve that first whatever value is returned we use as an argument to the next outermost function and in this case it would be float then we resolve that then move on to absolute value then round and then print so it's just another way of writing code that takes up less lines but this is more of an extreme example you probably won't see like four or five nested functions well
02:09:29
everybody those are nested function calls if you would like a copy of this code i will post all of this to the comment section down below but yeah those are nested function calls in python hey what's going on people it's your bro hope you're doing well and in this video i'm going to show you how variable scope works in python so sit back relax and enjoy the show all right let's do a thing people let's discuss the scope of a variable now the scope of a variable is the region that a variable is recognized a variable is only
02:10:02
available from inside the region that it is created let's say that we have a function and i just gave this function a name of display name i created a variable inside of this function named name and you can assign this a value of whatever your name is preferably a last name for this example and what i'll do is just print whatever my name is so this variable is known to have a local scope because it's declared inside of a function and a variable is only recognized inside of the region
02:10:33
that it is created this variable of name since it's created inside of this function it is known as a local variable that has a local scope it's only available inside of this function that it's created now let's attempt to access this variable from outside of our function so i'm going to print my name variable but we'll have an issue though so name name is not defined so local variables are declared inside of a function and they have a local scope they are
02:11:05
only available inside of this function in which they're created on the other hand a global variable is a variable that is declared outside of any function but within your module that you're working with a global variable is known to have a global scope they are available inside and outside of any functions now if i was to print my name variable this is going to print bro or whatever your first name is and it is entirely possible to have both a global and local version of the same
02:11:35
named variable so we have both a global version of name as well as a local version of name if i was to print my name variable from outside of this function it's going to print my global version however if i print my version of name inside of that function in which it's created we're going to first use the local version before the global version of name and let's try that so i'm going to call my display name function
02:12:04
and this will print my last name code and then my first name of bro so it is possible to have a global and local version of the same named variable now if i was to remove this line where we declare a local version of name well then the next best available name to us would be the global version of name so if you use a variable inside of a function you'll first use a local version of that variable if it's available if not you'll use a global version instead
02:12:36
and python will follow this rule known as the legb rule you use any local variables first then enclosed variables then global and then any built-in variables in that order so that's the main difference between local scope and global scope the scope of a variable is the region in which it's available and it's limited to the region in which it's created a local scope is available only inside of the function in which that variable is created a global scoped variable is a variable
02:13:07
that is available inside and outside of any functions but within your module that you're working with so that's variable scope in python if you would like a copy of this code i will post this code to the comment section down below and well yeah that's how variable scope works in python hey what's going on everybody it's bro hope you're doing well and in this video i'm going to explain the arcs parameter in python so sit back relax and enjoy the show all right guys and gals let's talk about
02:13:38
args now args is a parameter that will pack all arguments into a tuple it's useful so that a function can accept a varying amount of arguments here's an example of why it would be useful let's say we have a function called add that will accept two numbers as arguments and add them together and return the sum so this works as intended right as long as we pass in only two numbers but what if we need to pass in three numbers as arguments well we can no longer use this add function because we have two parameters set up
02:14:08
but we're passing in three arguments you can see here within my console window that we have a type error add takes two positional arguments but three were given and one way in which we can solve this issue is to use this args parameter so what we're going to do is replace all of our parameters with asterisk args and actually the args portion isn't as important as the asterisk args is short for arguments and you can really name this whatever you want just be sure that you have this asterisk because with this asterisk we're doing a form of packing
02:14:39
what we're doing is passing all of these arguments and packing them into a tuple so let's rewrite this program so let's say maybe sum equals zero and then we will need to iterate through all of the items within our tuple because tuples are iterable so let's say for i in args what we'll do is say sum plus equals whatever i is and at the end when we escape the for loop we will return our sum so let's try it again and see if it works well would you look at that it works
02:15:11
let's pass in a few other arguments for good measure let's pass in six arguments this time all right look at that this does in fact work so you can actually name this whatever you want the important thing is to have this asterisk so let's rename this as i don't know stuff and let's try this again yep it still works with tuples they are ordered and unchangeable let's attempt to edit one of these values so let's say stuff at index zero is equal to
02:15:42
zero and let's try and change it so you can see that tuple object does not support item assignment if you need to change one of the values within your tuple after you pack all of these arguments you might need to convert this to a different collection one way in which we can do that is to cast it let's say stuff equals and we'll cast our tuple as a list because a list can be changeable it's mutable so let's attempt this again we'll cast our tuple as a list and then edit one of the values
02:16:14
and then add all the values together so to change one of the values say the name your collection stuff in this case let's say at index zero this now equals zero so now if we were to add all these items together they should equal 20. there we go so yeah that's basically the args parameter the name isn't as important as the asterisk because you can really name it whatever you want a common convention is to just name this args it's a parameter that will pack all arguments into a tuple it's useful so that a function can accept
02:16:46
a varying amount of arguments well that is all about args if you would like a copy of this code i will post this in the comments section down below but yeah that is how the args parameter works in python hey what's going on everybody it's bro hope you're doing well and in this video i'm going to be explaining quarks in python so sit back relax and enjoy the show all right everybody let's talk about quarkx now this is a parameter that will pack all arguments into a dictionary it's
02:17:17
useful so that a function can accept a varying amount of keyword arguments it's identical to args except with args args will accept a varying amount of positional arguments and pack them into a tuple with quarks this will accept a varying amount of keyword arguments and pack them into a dictionary so here's an example of why this would be useful let's say we have a function called hello that accepts two keyword arguments a first name and a last name all this will do is output hello your first name and last name but what
02:17:49
if somebody has more than two names let's say somebody has a middle name and for this example i'll just say my middle name is dude so if i were to run this well this hello function is no longer going to work as you can see in the console window we have a type error the hello function got an unexpected keyword argument middle one way in which we can solve this we can use this quark's parameter so we can accept a varying amount of keyword arguments so what we'll do is replace our parameters with two asterisks then
02:18:21
quarks now if i would like the same output as i had before i'm going to need to change a few things around in order to access a value within a dictionary you type the name of the dictionary square brackets and then within quotes list your key so i would like to access the value at this key of first and let's repeat the process for last name so the key is going to be last we'll access this value at this key and this should work the same as it did before
02:18:52
now let's take this a step further let's say we would like to display somebody's full name based on the amount of keyword arguments that they pass into this function well there's a few different ways in which we could do this one easy way is that we could write our program like this let's print the word hello and we will need to iterate once through each key value pair within this dictionary and we can use a for loop to do that so to iterate once through each key value pair in your dictionary
02:19:23
we'll type for key value in the name of our dictionary quarks in this case dot items and then we will print each value so there's one more thing that we need to do this kind of works as intended but it's going to print each value including our word hello on a new line so with a standard print statement this will print a new line character after each print statement as if we're hitting enter we can actually change that so after
02:19:56
your string add and equals and we can replace that new line character with something else maybe a space and let's do the same thing for our for loop so value comma end and then just add space now this will print all of your names on the same line hello bro dude code and now we can pass in a varying amount of keyword arguments based on how long our name is perhaps we have like a title title equals mr and we can print that as well
02:20:26
hello mr bro dude code here's a fun fact of the day you don't necessarily need to name this parameter as quarks the important thing is to have these double asterisks that precede it quarks is just a common naming convention it's short for keyword arguments you could name this as something else perhaps something more descriptive like names since we're passing in a bunch of names so the name of our dictionary is now names and this would work the same as it did before the important thing is to have these double asterisks that precede the name of your dictionary
02:20:58
but quarks is the common naming convention well everybody that's all about quarkx it's a parameter that will pack a varying amount of keyword arguments into a dictionary if you would like a copy of all this code i will post this in the comments section down below but yeah that is how quarkx works in python hey what's going on everybody it's your bro hope you're doing well and in this video i'm going to teach you guys all about the format method in python so sit back relax and enjoy the show
02:21:29
okay ladies and gentlemen let's begin the format method is a method available to strings it's optional and it gives users more control when displaying output to best demonstrate this i'm going to use a line from a popular nursery rhyme titled hey diddle diddle because well you know i'm a mature adult like that i'll be printing this line to my console window the cow jumped over the moon let's do so using a standard print statement and then i'll show you the benefits of using the format method later
02:22:01
for this example i have two variables a variable called animal and i'm going to store a string of cow and item and i'll be storing an item name which is moon when we compile and run this as you would expect the output is the cow jumped over the moon there's a more elegant way of writing this line of code and that is by using the format method available to strings i'm going to turn this line into a comment and we're instead going to produce the same output but use the format method instead so we need a standard print
02:22:33
statement like usual and then a string that we would like to format so we're going to write the same thing but in place of the variables we're going to add a placeholder the instead of the variable name or value name i'm going to add a set of curly braces so that functions as a placeholder for a value or a variable the this should be cow jumped over the and i'm going to insert another placeholder now this is a string we can format the
02:23:04
string by following this string with dot format parenthesis and within the parentheses of the format method we can insert some values or variables let's begin with some values i would like to insert cow as well as moon so this will produce the same output but it's written a little more elegantly we can also replace these with variables because they store these values so i'm going to replace cow
02:23:34
with animal and moon with item and this will create the same output as it did before these curly braces are what's known as format fields they function as a placeholder for a value or a variable and they work in order the first format field will insert the first value at this location if you have another set or more sets of this format field you'll insert the next value that's listed within the format method so if we were to switch these around
02:24:06
we'll switch the position of animal with item well then the first format field is going to insert our item and the second format field will insert our animal this time the moon jumped over the cow an additional way of inserting values at a given place holder would be to use what's referred to as a positional argument within these format fields we will list the index of the value that we would like to insert at this location
02:24:36
if you need to insert the first value well that has an index of zero because computers always start with zero and following that pattern the next index would be one if you had let's say three values that you need to insert if you had another placeholder that would be two then so you would just follow that pattern so there's going to be no visible change but what we could do is reverse the positional arguments of these placeholders so what do you think would happen if i switched one with zero at these locations
02:25:07
well this format field is going to insert the value found at index one this first value is index zero and the second one is index one so now the roles are reversed this time the moon jumped over the cow our last way of inserting some values at a given format field would be to use what's referred to as a keyword argument within the format method we're going to list some keyword argument pairs we need a keyword name followed by a value
02:25:38
so let's say animal is going to be the keyword we're going to say equals some value animal equals cow and item equals moon and you may have noticed that the color of your text might have changed so we no longer need these variables actually um but i'm going to comment out these two print statements because we will get an error then so these are in keyword argument pairs and within our format field instead of keeping these empty or using an index we can use the keyword
02:26:08
name at my first format field i'm going to insert my keyword of animal and at my second format field i would like to insert the item keyword so the result is going to be the cow jumped over the moon so let's reverse these now we'll place our item keyword here and then at the second format field we will insert our animal and this time the moon jumped over the cow here's an important concept that i'd like to bring to your attention with these values found within the format method you can actually reuse
02:26:39
some of these more than once so let's say we would like to state the animal jumped over the animal we're going to use this animal keyword twice and our output is going to be the cow jumped over the cow if we're doing the same thing with positional arguments we can reuse the same index this time let's say that the moon jumped over the moon so our item has an index of one so within our first format field we'll state the index will be one and our second
02:27:10
format field will also be one so our output's going to be the moon jumped over the moon all right let's take things up a level there's an even more elegant way in which we could write all of this what we'll do is store our string that we would like to format within a variable let's turn this line into a comment and let's create a variable called text text equals the string that we would like to format which is the format field jumped over the format field
02:27:41
and if we need to format this variable we can just call the format method on it so within a print statement let's say text dot and then use the format method and pass in the values that we would like to insert at these placeholders so we're going to insert animal as well as item so our result as you would imagine is the cow jumped over the moon this next section i'm going to explain how we can add some padding to a string when we display it using the
02:28:13
format method let's say we have a name variable and assign this a value of whatever your name is so i would like to display my name along with the custom message let's say hello my name is and then i'll use a format field here so let's follow this string by using the format method and i would like to insert my name at this location so currently this will display hello my name is bro or whatever else your name is so we can add some padding either before our name after or we can
02:28:44
add some padding to the left and the right hand side of our value that we have so what we'll do to begin is let's add some padding to the right hand side of our name that we have so within our format field if we need to add some padding within the format field add a colon and then the amount of space you would like to allocate to displaying your value so let's say 10 so i'm going to allocate 10 spaces worth of room to display my name you can't really see it right now but let's take a look
02:29:15
so i can't move my cursor past this line at the end of my name but with my second line i have all of this room to work with so if i were to change my string that i have let's say nice to meet you well then all of this padding that i have is going to be more evident so we can add a number preceding with a colon to add some padding to a value and then you can left align this right align it or center align it so what i'll do is copy this line that i
02:29:47
have if you need to left align it you use the less than sign but there's going to be no visible change because that's already the default if you need to write a line precede your number with a greater than sign so your value is going to be right aligned if you need to center this value then use the carrot so this will center align your value within the padding that you have allotted one question that you might have for me and i'll try and answer that now because
02:30:18
it's relevant what if we need to add a positional argument or a keyword argument to our format field if there's already some text within here so what you would do before the colon you would just precede the colon with either your positional argument or your keyword argument followed by colon and then whatever you want to add afterwards to format your value that you have alright ladies and gentlemen we are near the end and i promise that this is the last section how can we format some numbers let's say we have a number variable and this will equal the
02:30:50
first few digits of pi 3.14159 let's pretend that we would like to display only the first two digits after the decimal so we can do so using the format method let's print the number pi is and then i'll use a format field here and we will call the format method and insert our number that we have so if i need to display only the first two digits after the decimal
02:31:21
within the format field i will add colon dot to f f is for floating point numbers that's anything with a decimal portion so the result is that this will display only the first two digits after my decimal because we placed two within the format field so if this was three point three f this would display three digits after the decimal portion oh and apparently this will round your number so that's something that you should keep
02:31:53
in mind so this time let's change our number to something larger like 1000 and i will copy this line and make a few changes let's say the number is and i would like to add a comma at the thousands place so after the colon within the format field i will add a comma so this will automatically add a comma to all 1000s places another cool thing that you can do is that you can display your number as binary so within the format field add colon b
02:32:22
this will display a binary representation of your number you can do the same thing with octal by adding colon o so our number is now being displayed as an octal number and you can do the same thing with hexadecimal lowercase x for lowercase or uppercase x for all uppercase so my number 1000 is now in hexadecimal and lastly we can display this number in scientific notation using either
02:32:53
lowercase e for lowercase or capital e for uppercase and mynumber 1000 is now in scientific notation all right everybody so that's a few things that you can do with the format method it's an optional method that gives users more control when displaying output if you would like a copy of all the code that we've written here today i will post everything in the comments down below don't be afraid to smash that like button drop a comment down below and subscribe if you'd like to become a fellow bro hey what's going on everybody it's bro
02:33:24
hope you're doing well and in this video i'm going to explain a few useful methods of the random module so sit back relax and enjoy the show okay people in this video i'm going to explain how we can generate some random numbers as well as a few other useful methods of the random module now we're not going to be creating true random numbers but something called pseudorandom numbers which are fairly darn close so the first step is to import the random module at the top import
02:33:54
random we now have access to everything that the random module has to offer let's begin by generating a random number between one and six like we're rolling a dice so what i'm going to do is say x equals random dot rand and we can generate a random integer between a certain range if i want one through six i will state one comma six and then we can generate a random number between one and six like we're rolling a dice so if i were to print the value of x
02:34:26
then we will get a random number between one and six for example i just got a one a two a three and a six all right we can also generate a random floating point number two let's say y equals random dot random so this will give us a random number between 0 and 1. this time i'm going to print y so we have 0.49 and some change 0.145 and 0.858
02:34:56
we can also generate a random choice from a list or other collection let's say we're playing a game of rock paper scissors so let's create a list i'll just call this my list equals square brackets and we will have three choices rock paper and scissors so let's say z will be the random string that we generate z equals random dot
02:35:28
choice and we will pass in my list and let's print z so this will generate a random choice from my list called my list so we got scissors paper and rock we can also use the shuffle method of the random module to shuffle a list or other collection let's say we're working with a deck of cards cards equals let's say one through nine and we have a jack a queen
02:36:04
a king and an ace this won't be a full deck of cards it'll just be one suit so we can actually shuffle this list by using the shuffle method random dot shuffle what do we want to shuffle we want to shuffle our cards and i will display my cards with a print statement so the shuffle method will shuffle a list or other collection for you well everybody that's a few useful methods of the random module if you would like a copy of all this code i will post this in the comments
02:36:36
down below don't be afraid to smash that like button drop a comment down below and subscribe if you'd like to become a fellow bro hey what's going on everybody it's bro hope you're doing well and in this video i'm going to explain exception handling in python so sit back relax and enjoy the show well everyone let's talk about exceptions an exception is an event detected during execution that interrupts the normal flow of a program here's a program that i've written and we're intentionally going to cause an
02:37:07
exception so this program that i've written we will accept a numerator and a denominator from the user we'll divide the numerator by the denominator we'll get a result and print the result so what if i were to divide a number such as 5 divided by 0 which we cannot mathematically do well this will cause an exception an event detected during execution that interrupts the flow of a program so our program was interrupted and within our console window we have this traceback message which states what went wrong and a name
02:37:39
for this exception that occurred this specific exception that occurred is a zero division error so this video is all about handling these exceptions so they do not interrupt the normal flow of a program now a very basic form of exception handling is to surround any code that is considered dangerous as in it might cause an exception within a try block anytime you accept user input would be a good indicator too so this code is considered dangerous because we don't know what the user is going to type in so we will try all of this code if an
02:38:10
exception occurs we can catch it and handle it so to do that we'll add an accept block accept and then we will catch exception normally having this block by itself isn't considered good practice but i'll explain why later so this accept exception block will catch all sorts of different exceptions and it will prevent our program from being interrupted and we can do something else entirely such as print a message something went wrong so let's try this again
02:38:40
we will divide five by zero which we cannot do we no longer have that traceback message within our console window we caught the exception and we did something else we printed something went wrong like i said before it's not considered good practice to have a single accept block that will handle all exceptions it's much better to first handle specific exceptions when they occur and we can do so by writing additional accept blocks so let's create an additional accept block that will catch
02:39:10
any zero division error exceptions that occurs when somebody attempts to divide by zero so if this exception occurs how do we want to handle it let's just print the message let's print you can't divide by zero idiot and let's try this again so five divided by zero you can't divide by zero idiot here's another one i'm going to turn this accept block into a comment so we can take a look at something what if i divide by something that's not
02:39:41
a number like we take 5 and divide it by the word pizza well pizza is not a number and we ran into a value error exception so we should probably handle this too so let's create another accept block that will handle that accept value error and how do we want to handle this let's print something enter only numbers please okay let's try this again and see if
02:40:10
this exception is caught five divided by the word pizza enter only numbers please not only that but you can also add the accept exception block at the very end just in case there's something you won't anticipate but it's always considered good practice to first catch any specific exceptions and let the user know exactly what went wrong an additional way in which we can handle these exceptions is that we could display the exception that occurs although it is completely optional so to do that what we'll do is that
02:40:42
after each of these accept block definitions let's add as e so we're calling the exception as e that's just standard practice although it's not necessary so let's do that for each of these except blocks and then we can print whatever e is so let's add that to each of these blocks so it should now look something like this so when we encounter an exception we can display what exception occurred too as well so let's divide five by pizza invalid
02:41:13
literal for int with base 10 pizza enter only numbers please so like i said this is an additional way in which you could handle an exception although it's entirely optional another thing that you can do is that you can add an else statement to the end of your accept blocks so what's happening here is that we're going to try all of this code if an exception happens then catch it and handle it if not then do something else so why don't we print our result only if there are no exceptions that occur so if there are no
02:41:46
exceptions we will execute this else statement if not then we won't so let's divide five by an actual number this time like two so there are no exceptions that occurred so we executed this else statement whatever code is within our else block and this printed the result of 2.5 but if an exception were to occur like we divide 5 by 0 then we will catch our exception and handle it and we do not execute whatever block of code is within our else statement there is one other
02:42:18
clause that you should be aware of that is the final clause this is always at the end how this works is that whether or not we catch an exception we will always execute any code that is within the block of code for our final clause so this is a good opportunity if you open files to close them within the finally block but we haven't dealt with opening files yet so we won't do that all we'll do is just print something print this will always execute so
02:42:48
whether or not we encounter an exception this finally block will always execute at the end so let's try this again so this time let's cause an exception let's divide five by zero you can't divide by zero this will always execute and this time let's take this seriously and divide five by another number such as three so this will give us a result and like i said the finally clause the code within the finally clause will always execute but we won't really be working with this until we get to the section on file handling
02:43:19
well everybody those are exceptions in python they are events detected during execution that interrupt the flow of a program and we can handle these exceptions by using try and accept blocks so if you would like a copy of all this code i will post all of this in the comments down below but yeah that is how exceptions work in python hey what's going on everybody it's bro hope you're doing well and in this video we're going to be covering some basic file detection using python so
02:43:48
sit back relax and enjoy the show welcome ladies and gentlemen to the beginning of a mini series involving doing stuff with files using python and in this video we're going to be doing some basic file detection and in order to do so i recommend importing the os module it's already included with the standard python library all you need to do is import it all we'll be doing is checking to see if a file exists someplace on our computer so we'll probably need a file to work with what i'm going to do is go to my desktop for convenience
02:44:21
and just create a new text document i will call this test.txt now i probably need the file location so i'm going to copy that and then we can use this so let's create a variable called path that will include the path to the file that i would like to detect so that was test.txt now if you have backslashes in your file path you'll probably need double backslashes because that's the escape sequence for a backslash within a string so we now have this variable path that
02:44:53
we can work with now i would like to check to see if this location exists on my computer so if os dot path dot exists and you can see that we can pass in our path as an argument so i will pass in my path and if this returns true if that location exists i will print that location exists so this will not tell me if this is a file or not
02:45:22
we'll be covering that later else let's print that location doesn't exist all right so if this location exists this will return true if not this will execute our else statement that location exists so what if i were to delete this file and we run this program again that location doesn't exist now this won't tell you if the location you're dealing with is a file or not there is a separate
02:45:55
function for that it is if os dot path dot is file what we'll do is pass in our path and print that is a file all right so let's create another file because i deleted the previous one test.txt it's in the same location let's run this again that location exists that is a file now what if this was a folder
02:46:27
so i'm going to create a new folder i will call this folder i will delete my test document and let's change the location to folder that location exists but it is not a file you can check to see if a location is a directory so let's write an else if statement os dot path dot is directory and we will pass in our path and let's print that
02:46:57
is a directory all right so we have our folder we're going to check to see if this location exists and if it's a file or a directory that location exists that is a directory well everyone that is the basics of file detection we'll be building upon this concept in future videos so if you would like a copy of this code i will post this in the comment section down below but yeah that is the basics of simple file detection using python
02:47:28
hey what's going on everybody it's bro hope you're doing well and in this video we're going to be reading a file in python so sit back relax and enjoy the show welcome back guys and gals in this video i'm going to explain how we can read the contents of a file using python so we'll probably need some sort of file to work with i created a plain text file called test.txt it's a plain text file and all this says is omg you can read this have a nice day like comment and subscribe so what i
02:48:01
would like to do is to read the contents of that file line by line and print it to the console window and here's how and honestly we only need two lines of code to do this so what we'll do is type with open then within the open function list the name of your file or the file path this file of mine is within my project folder so i only need to list the file name if this was someplace else you'll probably need the file path so let's say this was on my desktop
02:48:31
well i'll probably need to list the file path of where that file is located and i'll need to use double backslashes because that's the escape sequence for a backslash but since this file is within my project folder i only need to list the file name so with open then the name your file or the file path as file then what we're going to do is print file dot read and that is it so let's test it
02:49:02
and it works omg you can read this have a nice day like comment and subscribe so this will actually close files automatically after opening them so that's kind of convenient and let's just test that theory so i'm going to print file dot closed if my file is in fact closed this will print true if it's still open this will print false so normally when you read a file it's normally open and you need to close it manually but if you write your program this way
02:49:32
with open this will close the file automatically for you so using with open will close any files automatically for you after they've been opened however this does not catch and handle any exceptions that might occur like if we cannot locate this file so let's say that i mistyped the extension for this file let's say that i type in test.tx that's kind of funny because tx is the abbreviation for texas it's a texas file so this will cause
02:50:03
a file not found error and this will interrupt the flow of our program and we would probably like to prevent that so what we could do is write a try and an accept block so we will try all of this code if we cannot find this file we can handle this exception file not found error so we'll place all of this code within the try block then if an exception happens we will catch that exception except we would like to catch any file not found air exceptions and let's handle this by printing
02:50:35
that file was not found so if i were to run this again we will catch and handle this exception and this will not interrupt the normal flow of our program well everybody that's how to open and read a file in python if you'd like a copy of all this code i will post this in the comment section down below but yeah that's how to open and read a file using python hey what's going on everybody it's bro hope you're doing well and in this video we're going to be writing files in python so sit back
02:51:07
relax and enjoy the show so to begin it's kind of similar to the last video on reading files with open within the open function list the file name or the file path so what do you want to call this file i will call this file test dot txt now normally with the open function there is a mode and you can actually change that there's a second argument that you can pass in by default this is r for read but if you want to write a file this would be w so now we're going to write a file
02:51:41
called test.txt so to finish with open as file what are we going to do file dot write and within the write function we can write some text so let's create a text variable and we will add a string so let's say text equals yo and if you need to go down to a new line be sure to use the new line character so after this new line character
02:52:11
the text that comes after is going to be on the next line as if we're hitting enter so yo this is some text have a good one all right so after i run this program we should have a file a text file within our project folder that appears and it is right here and it says yo this is some text have a good one now this is in write mode
02:52:42
if this text was something else it will actually overwrite your current file so if i were to change this to uh oh this text has been over written well then this will overwrite the text that we have uh oh this text has been overwritten so you can actually append a file by changing the mode to a for append so let me change this back we will rewrite this this will save over our current file
02:53:13
now we can append some text so let's add maybe a new line uh have a nice day see ya so i would like to append some text to this file i'm going to change the mode to a for append and we can now append some text onto the end of this file well everybody that's how to write to a file and or append a file if you would like a copy of this code i will post this in the comment section down below but yeah that's how to
02:53:42
write to and or append a file in python hey what's going on everybody it's bro hope you're doing well and in this video we're going to be copying files in python so sit back relax and enjoy the show welcome back to another video regarding doing stuff with files using python and in this video we're going to be copying some files and in order to do so i recommend importing the shuttle module there's other ways of doing this too but i tend to like using the shuttle module now with this module there are three basic
02:54:15
functions to copy a file starting at the top they are copy file copy and copy to so copy file is what we'll be working with this will copy the contents of a file copy will do everything that copy file does plus copy the permissions mode and the destination can be a directory and lastly copy 2 this will do everything that copy does plus it copies the metadata of that file including the files creation and modification times
02:54:46
so depending on the project or the program you're working with you may need to use copy or copy too but in this video we're going to be using copy file just to copy the contents of a file now after you import the shuttle module this is all you need to do to copy a file shuttle dot then use the appropriate function that you need to copy your file we're going to be using copy file copy file and within this function there are two arguments a source and a destination so within my
02:55:17
project folder i have a plain text file called test test.txt all this says is yo this is some text see ya what i would like to do is copy this file because it's cool now since this file is within my project folder i only have to list the file name otherwise if this is someplace else i would list the file path here so this file is test.txt this is the source file and now i need a destination so the second argument will be separated
02:55:48
with the comma i'm going to list a destination so you can actually rename this to i would like to name this as copy.txt so the destination is my project folder so when i create a copy of this file test.txt this copy will appear in my project folder and say the same thing so after running this we now have a file named copy.txt that says the same thing as my test file and remember what i said you can copy this file to a different location on
02:56:19
your computer so you could list the file path so this file path would go to my desktop and then when i run this this will copy my original file my source and then copy it to wherever my destination is oh and i almost forgot so if you need to use copy or copy 2 the arguments are exactly the same so if you needed to use copy this would be a shuttle dot copy instead of copy file then copy 2 is shuttle dot copy 2. the arguments are exactly the same but each of these functions will copy
02:56:50
different things depending on what you need for your own program or project all right everybody so that's the basics of copying files and python i will post this in the comment section down below but yeah that is how to copy files using python hey what's going on everybody it's bro hope you're doing well and in this video