in this video I'm going to teach you as much python as I can in exactly 1 hour if you would like a quick introduction or you're transitioning from another programming language this video is for you if you would like to go more in depth I do have a full free 12-hour course on my channel otherwise let's get started I don't like boring introductions so I say we just jump right in there's two things we'll need to download the first is a python interpreter to convert our written code to machine code we're going to head to
python.org go to downloads and download the latest version we will open this executable if you're on Windows you'll want to check this checkbox add python exe to path and we will install now the setup was successful and that's all you need to do to download the python interpreter the second download we'll need is an IDE an integrated development environment basically a place where we can write code for idees there's two popular choices when writing python code py charm and vs code if you already use vs code you can stick with that just be sure to
download the python extension I find py charm more beginner friendly if you've never coded before if you would like to use pycharm go to Jet brains /py charm and we will click this green download button there's two versions of py charm the professional version and the community version the professional version is paid for I would not recommend using it only because there's plenty of free idees on the market we'll use the Community Edition the free one because I don't like to pay for things and I'm sure you don't either select the correct download for your
operating system I'm running Windows I will download pycharm we will open this executable click next you could select a destination folder I'll keep it as is next I'll create a desktop shortcut but you don't necessarily need to click next install and we'll just have to give it a moment okay the setup is now complete I'll check this checkbox to run py charm when we close this window after opening py charm we're going to create a new project you can rename your python project I'll keep it as is you can select a location again I won't
change that you can create a sample welcome script but for this tutorial we won't let's select the latest python version and create our new project in the menu to the left we're going to create a new python file file new python file let's name this file main but really you can name it anything and select python file python files end with the py file extension we should have our main python file within our python project folder now we're going to print something to the console window within our main python file we're going to write a
print statement so type print add a set of parentheses between the set of parentheses we will add a set of double quotes to print something or single quotes either one my own preference is double quotes normally in a programming tutorial the instructor would tell you to print something such as hello world but we like to be different here instead think of your favorite food in this case I like pizza I will print I like pizza to run our Python program we will click the screen Arrow to run our main python file we should have a
console window that displays our output I like pizza or whatever your favorite food is let's print another line of code let's print it's really good by adding a second print statement we are printing a second line of code now we'll discuss comments the python interpreter doesn't output comments to write a comment you use a pound sign I like to call this a hashtag my comment will be this is my first Python program comments are used as notes for yourself or for other people reading this code if I were to run this code again this comment
is not displayed to the output we still have I like pizza it's really good all right everybody so that is your very first Python program and in the next topic we'll discuss variables all right we're moving on to variables a variable is a reusable container for a value there's four data types that you need to know as a beginner strings integers floats and booleans the variable behaves as if it was the value that it contains to create a variable we need a unique name we'll include our full name so we will create a variable named
full name equals some value now a string is a series of characters this can be within double quotes or single quotes in Python personally I like to use double quotes so why don't you type in your full name I'll use my YouTube channel name now with a variable it will behave as if it was the string of characters I could use it within a print statement to print a variable the easiest way is to use an F string F then a set of double quotes f means format using an F string we can insert a
variable wherever we would like so I'm going to say hello to insert a variable you need a set of curly braces then we can place that variable name within that set of curly braces so now my output is Hello Bro Code that's a string it's a series of characters then we have integers integers are whole numbers an example of an integer would be an age let's say that my age is 25 I wouldn't be you know 25.5 integers are whole numbers then let's output our age print I'll use an FST string Ur at a placeholder
insert our variable age years old then let's run this hello bro code you are 25 years old floats are numbers but they contain a decimal a good example of this would be a GPA a great point average let's say that my GPA is 3.2 3.2 out of 4 let's print our GPA using an F string your GPA is at a placeholder insert our variable GPA your GPA is 3.2 then we have booleans booleans are either true or false let's create a Boolean variable is student this is either true or false do pay attention to the
capitalization true and false need to be capitalized so let's say that I am currently a student now with booleans we typically don't output them directly but we could I'm going to use an F string and let's say are you a student and then I will add a placeholder display our Boolean variable are you a student that is true or I could say that this is false if I am not a student maybe I've graduated you can output a Boolean directly but typically we use them internally within a program such as when using an if statement
we haven't reached if statements yet but I just want to show one use of booleans we might say if is student if this is true then we will print you are a student if a student is false then we would print something else such as you are not a student is student will be true this will print you are a student if it were false we would instead print you are not a student we'll cover if statements a little bit later but that's where booleans are useful we tend to use them internally within a program
all right everybody so those are variables they're a reusable container for a value and well everybody those are variables in Python okay everybody so now I'm going to show you some basic arithmetic in Python here's a few arithmetic operators we have Plus for addition minus for subtraction asterisk for multiplication a forward slash for division two forward slashes for integer Division and then the modulus operator which gives us the remainder of any division we'll begin with addition let's say we have a variable of friends I will set that to be five then I will print my
variable of friends now I'm not using an F string only because I don't have any additional text to WR I'm just outputting the variable so friends contains a value of five I can increment the amount of friends I have by one by using addition so let's take friends equals friends + one basically we're taking 5 + 1 then reassigning it to our variable of friends which gives me six I could increase it by two or even three there is a short cut to this because this can be a lot to write we can use an
augmented assignment operator where we would say friends plus equals 1 to increment by one that gives me six or two or three so unlike other programming languages python doesn't have an increment operator such as Plus+ here we have a syntax error so to increment by 1 we would have to say plus equals 1 or some other number if you're choosing depending on how much you want to add then we have subtraction let's subtract one friend friends equals friends minus one that would give me 4 minus 2 would be 3 or we could use the augmented
assignment operator which would be minus equals let's say 1 which gives us 4us - 2 would be 3 - 3 would give us 2 that's subtraction then we have multiplication let's double the amount of friends that we have friends asterisk 2 asterisk is used for multiplication so 5 * 2 is 10 friends * 3 is 15 the augmented assignment version would be time equals some number so friends time equals 2 would be 10 3 would be 15 4 would be 20 then we have division standard division returns a floating Point number a number that contains
a decimal let's divide the amount of friends that we have by two friends equals friends / two that gives me 2.5 and this is a float a floating Point number even if our friends divided evenly 10 / 2 we still have that point Z at the end it's a floating Point number and not an integer so the augmented assignment version of this would beward slash equals what are we dividing by we will divide friends by two which gives us five or three which gives us 3.33 3 3 3 3 3 33 repeating then we have
integer division which is two for slashes integer division division returns an integer rounded down let's take friends equals our friends variable use integer division divided by 3 we will round down to the nearest whole integer which is three because 10 does not divide by three evenly then using integer division divided two would give us five whole numbers then again the augmented assignment version of this would be 2/ es equals use integer division to divide by three that gives us a whole number of three then we have the modulus operator represented by a percent which would
instead give us the remainder let's say with all of our friends we're all in the same classroom and the teacher says to get into groups of three I'm going to create a new variable of remaining friends equals our group of friends modulus 3 the modulus operator gives us the remainder of any Division 10 does not divide by three evenly how many friends do we have remaining one that's John John knows what he did he's the one friend remaining that's not paired up all right everybody so those are some basic arithmetic operators in Python we have
addition subtraction multiplication division integer Division and the modulus operator which gives you the remainder of any division and well everybody that is basic arithmetic in Python all right everybody so we are moving on to typ Casting typc casting is the process of converting a variable from one data type to another we have various functions to convert a value or variable to a string an integer a float or a Boolean let's create some variables we will create a name variable type in your full name an age make up some age a GPA for grade point average
let's say minus 3.2 and a Boolean of a student are we currently a student let's say that's true now you actually could get the data type of a variable or a value by using the type function then pass in a value or variable however when I run this there's no output so I need a print statement we will print what is returned by the type function get the type of our name variable then print it so our name variable is a string St Str our age variable is an integer and int GPA is a float
is student is a Boolean using these typ cast functions we can convert from from one data type to another here's how let's start with something simple let's convert our GPA to an integer currently it's a float I will reassign GPA use the int function to typ cast to an integer then pass in my GPA at the end we will print our GPA if we typ cast 3.2 to a whole integer what would the result be a whole integer of three we truncate the decimal portion let's convert our age to a floating Point number we will
reassign our variable of age use the typ cast function of float then insert our age variable let's print our age variable and it should be a floating Point number 25.0 now we'll cover strings let's typ cast our age to be a string age equals called the typ cast function of string St Str pass in our age variable so the result is still going to appear the same 25 however it's a string not an integer and to prove that I will enclose my age variable with the type function the type of variable age is a string
it would be the same as if we're taking this number and enclosing it within quotes So this would make a difference because let's say that I add one to age age plus equals 1 well we would get a type error can only concatenate strings not integers to a string however if I were to add a string of one to the end we would be using string concatenation so let's say it's my birthday and I add one to 25 well since we're working with strings now the result would be 251 I am 251 years old so
strings and numbers behave differently with numbers we can use them within arithmetic Expressions strings not so much we will take our name variable and typ cast it to a Boolean name equals call the typ cast fun function of BU passing our name variable this has an interesting result so I'm going to print name booleans are either true or false if I typ cast my string of text into a Boolean that gives me true now it really doesn't matter what I write here if I were to change my name to a single character such as B
this would still be true if our string variable was empty there were no characters within it that would actually give us false we could use this to check to see if somebody enters in their name or not if somebody types in their name then we typ cast it to a Boolean if somebody skips entering in their name that would return false we could reprompt the user to enter in their name again all right everybody so that is typ casting it is the process of converting a variable from one data type to another this is especially
useful with handling user input because user input is always a string there may be at times where you want to convert it to an integer a float or a boan and well everybody that is typ casting in Python okay we're moving on everybody how can we accept user input in Python here's how we will use the input function we will enter in the input within the console window however we need a prompt what do we want the user to type in let's ask for the user's name enter your name this prompt will appear in the
console window enter your name and then we can type in our name although we're not doing anything with it currently when we accept user input let's assign it to something let's create a name variable name equals our user input enter your name type in your name name hit enter when we type in our user input it's going to be assigned to this variable of name let's print it to test it I will print my name to the console window and to your name I will type in my full name hit enter and it displays what's
within my name variable let's use an F string this time let's display hello our name variable and to your name type in your full name hit enter hello whatever your name is in my case bro code now when we accept user input it's always of the string data type to test that I will print the type of our name variable type in your name again so the data type of our name variable is a string you may want to typ cast it to an integer a float or a Boolean this time let's say that we
need an age variable age equals the input function we will prompt the user to enter your age then we will print the user's age using an F string you are insert age years old let's run this again enter your name type in your full name enter your age type in your age hit enter hello bro code you are 25 years old the data type of our age is going to be a string Let's test that I will print the type of our age variable again we have to follow the prompts our age variable is a
string let's say that it's my birthday I will add one to age age equal age + 1 or we could short this to Agee plus equals 1 as a shortcut here's the result enter your name enter your age and we get a type error can only concatenate strings not int to string if we accept user input and we have to use that input for any sort of arithmetic Expressions we'll have to convert it to either an integer or floating Point number since I'm working with whole integers we can typ cast our age as an integer
age equals use the int typ cast function then pass in our age then I can add one to age AG plus equals 1 so this should technically work type in your name type in your age and we will add one to age hello bro code you are 26 years old there is a way in which we can condense these steps rather than taking up a separate line where we're typ casting our agent variable let's enclose our input function with a typ cast function of int after receiving user input we'll typ cast it to an integer
ENT your name and your age and now we can add one to age because it's a whole integer hello bro code you are 26 years old all right everybody so that's how to accept user input you use the input function whatever you type into the console window you can assign to a variable with user input we are given a string value you may have to typ cast it as a beginner you can type cast to an integer a floating Point number or a Boolean value and well everybody that's how to accept user input in Python
okay everybody we are moving on to if statements with if statements they allow for basic decision making we can execute some code only if a condition is true in this demonstration let's say we have a variable of age we will accept some user input we will ask the user to enter your age then we will typ cast this input as an integer because when we accept user input it's of the string data type to write an if statement we will type if then we will check a condition let's check to see if the user's age
is greater than or equal to 18 colon then any block of code underneath an if statement should be indented if this condition is true then we will perform the following let's print the following message you are an adult let's perform a test run and to your age let's say that I'm 21 hit enter you are an adult if this condition were false here's what happens enter your age let's say that I'm 12 hit enter nothing happens if this condition is false we skip over this block of code like it never happened otherwise we could do
something else with an else Clause again the block of code underneath an else Clause is indented we will print the following instead you are a child let's perform a test run again and to your age let's say that I'm 12 we instead print whatever is within the else clause you are a child so by using IF statements they allow for basic decision making between if and else we could add else if e l if so let's check to see else if age is less than zero technically somebody shouldn't have a negative age let's instead print
the following you haven't been born yet and to your age let's say that I'm -1 years old you haven't been born yet with if statements else if Clauses and else Clauses we go through these statements one by one when one of these is true we skip the rest of the Clauses and continue with the rest of the program you can add as many else if Clauses as you would like let's add another else if age is equal to zero to check to see if a value is equal you use double equals this is the equals
comparison operator if you use one equals python thinks you're trying to assign something python in this case thinks I'm trying to set age to be zero so to see if a value is directly equal you use double equals if our age variable is zero then we will print the following you word just born and to your age let's say that I'm zero you were just born let's add one more else if Clause else if age is greater than or equal to 65 then we will print the following you are a senior citizen now check this
out let's say that I'm 7 70 years old you are an adult why do we execute this block of code where age is greater than or equal to 18 rather than this one where we're checking if age is greater than or equal to 65 we're going through these statements one by one beginning from the top technically the statement was true 70 is greater than or equal to 18 so we execute this block of code and skip the rest the order of your if and else if statements does matter let's check to see if our age
is greater than or equal to 65 first so let's put this in the front turn it into an if statement then convert this statement to be an else if Clause now it should work just fine let's say that I'm 70 you are a senior citizen the order of your if statements and else if Clauses does matter we also could check Boolean values too let's say we're going to a movie theater we will have a Boolean variable of has ticket have we pre-ordered a ticket for the movie this will be true or false a Boolean let's
say that we have a ticket I'll add another if statement to check so with a Boolean we will check if has ticket now we don't necessarily need to say is this equal to true we could just write that Boolean value or variable if has ticket if this contains true then do the following let's print you may enter you have a ticket else let's print something else you need to buy a ticket so again our variable of has ticket is true so first we have to enter our age let's say that I'm 25 you're an adult
you may enter you have a ticket I set the variable of has ticket to be true let's set this to be false enter your age 25 you are an adult you need to buy a ticket because we do not currently have a ticket let's add a few more lines to these if statements let's say that if we're a senior citizen we get a discount on the price of a ticket but first we should set the initial price let's say that the price of a ticket is $10 if we're considered an adult then let's print the
following I'll use an F string the ticket price for an adult is We'll add a placeholder and insert the price I will preedee this price variable with a unit of currency I'll pick American dollars so if we're a child let's let's say the price is 50% off we get a discount the ticket price for a child is price time 0.5 if we're a senior citizen we get a 25% discount the price for a senior is price times 0.75 these other LF statements are kind of distracting so let's delete them we'll just stick with these three
statements enter your age let's say that I'm 25 hit enter you are an adult the ticket price for an adult is $10 you need to buy a ticket because we do not currently have a ticket let's say that I'm 70 you are a senior citizen the ticket price for a senior is $7.50 you need to buy a ticket let's say that we do have a ticket and we are a child I am 13 years old you are a child the ticket price for a child is $5 you may enter you have a ticket all right
everybody so those are if statements we can execute some code only if a condition is true if statements allow for basic decision- making we have if statements else if Clauses and else Clauses and well everybody those are if statements in Python all right people we're talking about logical operators today logical operators allow us to evaluate multiple conditions we can link them together there's three we'll discuss or and not we'll begin with OR with or we can check more than one condition if at least one of those conditions is true then the entire statement is true
here's an example let's say we have an outdoor event and I will create two variables one temp meeing temperature let's say that this is in Celsius 25° C pick Fahrenheit if you would like and I will create a Boolean variable of is raining I will set that to be false it is currently not raining if the temperature is too hot too cold or it's raining then I will cancel this outdoor event we'll write an if statement to check that if our temp short for temperature is greater than let's say 35 35 degrees CSUS then I'll
use the or logical operator or if our temp is less than zero or if is raining is true if one of these conditions is true we're going to cancel our outdoor event so let's print the following the outdoor event is cancelled else we will will print something else the outdoor event is still scheduled the temperature is reasonable and is raining is false it's not raining so we print the else Clause the outdoor event is still scheduled what if the temperature was really hot like 36° C well the outdoor event is cancelled what if it's cold
-5° the outdoor event is canceled this condition was true therefore we execute the if statement or what if the temperature is reasonable but it's raining is raining is true well then the outdoor event is still cancelled so with the or logical operator at least one of these conditions needs to be true if one of these conditions is true you could consider the entire statement true now let's cover and with and we can link two conditions together both conditions must be true in order for that entire statement to be true so again let's say we have
temp short for temperature and we have a Boolean variable of is sunny I will set that to be true we will check if our temp is greater than or equal to 28° C and is it sunny is sunny if it's hot and if it's sunny if this is true let's print the following it is hot outside for fun I'm going to add an emoji but you don't have to I just think it's more entertaining that way but you do you and I will print it is sunny sometimes these emojis are formatted differently I'm just going
to copy it from somewhere else that's better currently the temperature is 25 25° C and it's sunny this condition was false but this one is true with the and logical operator both conditions must be true in order for us to execute this block of code if our temperature was 30 30° C well then both conditions are true it is hot outside and it is sunny let's write a few more let's add else if else if the temp is less than or equal to zero and is sunny we will print something else it is cold outside
I'll change the Emoji and it is sunny let's set the temperature to be5 5° it is cold outside and it is sunny both these conditions are true so we do this instead you can link as many conditions together as you would like let's see if our temperature is within a certain range else if temp is less than 28 and our temp is greater than zero and is sunny to check to see if something is within a certain range there is a shortcut too P charm is recommending this we can simplify change comparisons so this effectively
does the same thing if 28 is greater than our temp and our temp is greater than zero and it's sunny then we will print it is warm outside rather than hot and it's still sunny so let's say our temperature is 20° C and it's sunny it is warm outside and it is sunny now we have the not logical operator it inverts the condition we are checking to see if something is either not false or not true so let's check to see if it's not Sunny really I'll just copy what we have and paste it else
if nut is sunny then that means it's cloudy and let's use a cloud Emoji so basically not does the opposite of what you're looking for we are checking if not is sunny is sunny is false then this condition is true okay let's say our temp is 28 is sunny is now false it is hot outside it is cloudy what if our temperature was Zero it is cold outside it is cloudy what if the temperature was reasonable like 20° C it is warm outside it is cloudy so not it inverts the condition if it's true it's
now false if it's false it's now true all right everybody so those are logical operators they allow us to evaluate multiple conditions with or at least one condition must be true with and both conditions must be true and not not does the opposite it inverts the condition we check if something is not false or not true and well everybody those are logical operators in Python now we're covering while loops a while loop is used to repeat a block of code as long as a condition remains true we recheck the condition at the end of the
loop here's an example I have a condition if 1 is equal to 1 this would be true then we will print the following I am stuck in a loop if statements only execute once I am stuck in a loop let let's replace if with while and see what happens I wouldn't recommend doing this but you can watch me we're going to execute this code Forever at the end of our while loop we recheck the condition again there's no way for us to escape this Loop hence I am stuck in a loop we're repeating it forever
that's how a while loop and an if statement are different if statements are executed once while Loops could execute forever you should put in some way to escape the while loop let's go over another example let's say we accept some user input name equals input we will ask a user for their name enter your name now let's say that somebody doesn't type in their name I'm just going to hit enter at this prompt our name is an empty string an empty set of quotes if somebody skips an entering in their name let's write a while
loop while our name is equal to an empty string and empty set of quotes if a user doesn't enter in their name let's reprompt them again so in order to escape this while loop the name that the user enters in can't be empty and then once they do escape the while loop let's print the following I'll use an F string hello insert a placeholder name if I'm supposed to enter my name then hit enter we get that prompt again we are now stuck within the while loop and I can't escape until I type in something
so let's type in our full name hit enter and now we escape the while loop hello whatever your name is if we type in our name right away we actually don't enter the while loop at all we check check the while loop once and enter it if this condition is true since our name was not empty since it wasn't an empty string we skip it entirely so after our while loop let's ask for a user's age age equals input enter your age and then I'll typ cast this input as an integer since we're working with
whole numbers while our age variable is less than zero then we'll do the following let's perform two actions let's print age can't be less than zero and then we will ask for the user input again then when we escape the while loop we will print hello the user's name and the user's age you are at a placeholder age years old all right so in this program we have two while Loops I'm just going to change one thing after age I'm going to add a colon then a space just to make it look better so let's
enter our first name enter your age if I type in an age that's less than zero we'll be stuck within this while loop let's say I'm negative-1 years old age can't be less than zero and to your age2 age can't be less than zero negative kajillion age can't be less than zero okay let's say that I'm 25 hello bro you are 25 years old while Loops are really good for accepting user input if somebody enters in a value that's not valid you can reprompt them again that's a great benefit of wild Loops all right everybody
so those are wild Loops we can repeat a block of code as long as a condition remains true we recheck the condition at the end of the loop if that condition is still true we perform another iteration of that while loop and well everybody those are while Loops in Python all right everybody so we are moving on to for Loops a for Loop is used to iterate over a sequence such as a string list topple or set but we still need to talk about these three topics but we will soon or we could use a
for Loop to repeat a block of code an exact amount of times with the while loop we will execute some code possibly infinitely with a for loop it's limited so let's say we would like to count to 10 we can do that with the for Loop fairly easily here's the formula for we need an index the developers usually shorten this to I I meaning index in now what number do we want to count up to we will say range range is a function then pass in 10 we will do something 10 times so let's print
I I is our index our counter we will iterate this Loop 10 times so we begin at zero actually and go all the way to 9 but technically it's a total of 10 to set the first number to be something other than zero we can specify that let's say one then where would we like to end the second value is going to be comma separated so let's say 11 the second number is exclusive the first number is inclusive print the value of I our index from the range between 1 and 11 exclusive that would give
us 1- 10 I could change these around too let's say 91 through 101 that would be 91 through 100 so again the first number is inclusive the second number is exclusive let's count from 1 to 10 again so 1A 11 we could count by twos that would be the third value comma two we will count by twos 1 357 9 basically we're incrementing our index of I by two during each iteration or three we'll count by threes 1 4 7 10 and then four would be let's find out 159 we even could iterate over strings
as well anything that is considered iterable so let's say we have a string of name type in your full name let's change this for loop around rather than saying for I our index we could rename this to something let's say letter for every letter in our iterable of name print each letter then let's see what happens we get each letter on a new line at the end of each print statement we print a new line character rather than a new line character let's make one following change let's set the ending character to be something else
like a space we will print each letter but separate each letter with a space or we could even do a dash it's your choice now here's a mini project that we'll do we'll count from 10 down to zero then display happy new year so we need a for Loop to iterate backwards let's say for I our counter our index in range now we'll begin at 10 and end at zero zero is exclusive to count backwards we'll set the step the third value to be NE -1 then let's print I our index so then we should
count backwards starting at 10 ending at 1 now let's do this when we escape the for Loop let's print happy new year so we start at 10 count down to one then display Happy New Year to actually simulate a countdown we can import the time module this will be a mini project let's let's import the time module at the top after printing I let's access the time module call the Sleep Method then pass in one to sleep for 1 second now we'll have a countdown timer starting at 10 once we reach zero we will display
happy New Year all right everybody so those are four Loops they're used to iterate over a sequence such as a string list topple and set these three we're going to talk about next or we can repeat a block of code an exact or certain amount of times such as counting down from 10 and well everybody those are four Loops in Python all right people we have an important topic to discuss today and that is lists tles and sets if you come from a different programming language these three collections are all similar to arrays but in
Python we have three different varieties each has their own benefits let's start with the list I have a variable of fruit let's say fruit is an apple well with a variable a variable can only contain a single value with a list we can contain multiple values to create a list we will enclose all values within a set of square brackets each additional element within this list should be comma separated so let's let's include an orange a banana and a coconut that's good enough for this example let's rename fruit as fruits so people reading this may
recognize that this contains more than one fruit if I was to print my list of fruits here's what's returned to us we print out the entire list including the set of straight brackets at the beginning and the end to get one of the elements within this list you'll use the index of operator which is a set of straight brackets for the first element that's going to have an index of zero the element at index zero would be the first item within my list of apple one would be orange two banana three coconut if I attempt
to access an element that doesn't exist we get an index error list index is out of range you could use a for Loop to iterate and print each element within the list let's say for every fruit in my collection of fruits I will print each fruit that will give me Apple orange banana coconut now you don't have to do this but I'm going to end each character with a space rather than a new line just so it's easier for me to read lists are mutable and they're the most flexible out of the three I can
change one of these elements at a given index let's say fruits at index of zero is now a pineapple rather than an Apple so that should change the element at the first index to be a pineapple or let's say fruits at index 3 is a mango then we have apple orange banana mango go we can append elements using the append method take our list of fruits use the append method what would we like to append to the end let's append a mango then we have apple orange banana coconut mango we can remove elements take our
list of fruits called the remove method let's remove our banana apple orange coconut we can pop meaning we can remove an element at a given index so take fruits call the pop method let's pop the element at index zero that's the first now we have orange banana coconut let's pop at index one which gets rid of the orange two would be the banana three would be the coconut and then if we go out of bounds we get an index error pop index out of range so that's the pop method then if you ever need to
clear your list you'll take the list of fruits call the Clear method that will remove all the elements from your list so lists are mutable meaning we can change the elements and they're the most flexible we can change the element at a given index append elements remove elements pop elements and we can clear the elements our next type of collection is a tupple they behave similarly to lists however rather than enclosing all of your elements within a set of straight brackets you'll enclose them within a set of parentheses where topples differ is that they're immutable
meaning we can't change the elements once the tupple is created but this makes them faster it's faster to access the elements of a tupple rather than a list so py charm has even given me a warning tupples don't support item assignment if I attempt to change the element at index zero to be uh let's keep that as a mango well we get a type error tupple does not support item assignment once we create a tupple we can't change the elements they're immutable even if I was to append a new element fruits. append I will append
a mango again again we get that attribute error tupple has no attribute append I can't remove either I will attempt to remove an apple again tupple object has no attribute remove topples are good if you need a collection that can't be changed the benefit of this is that they're faster to access then we have sets which are probably the most complicated all your values will be enclosed within a set of curly braces we can add and remove elements but we can't replace any of the elements within it that's because sets are unordered so to demonstrate
this I'm going to print my set of fruits the order is going to change each time during this first run we have coconut then Apple orange then banana if I run this again we get apple coconut banana orange then coconut banana apple orange sets are unordered we can't access them by index so let's say fruits at index0 is now a pineapple while sets do not support item assignment that's because they're unordered whatever is at index zero within our set is going to change each time they're unordered however we can add and remove elements and we
would use the add method to do that rather than than append So within our set let's append a mango so we have a mango within our set but the position changes each time we can remove fruits. remove let's remove the coconut so the Coconut's gone let's attempt to pop fruits. pop at index zero again we can't work with indices type error pop takes no arguments and we can still clear fruits. CLE sets also don't allow for any duplicates what if I were to add just a bunch of coconuts to the end well we still only
have one coconut where sets are really good is for membership testing checking to see if there's a given value within your set here's how we can do that I'll write an if statement if the word apple is in our set of fruits if this statement is true then let's print Apple was found else we will print something else print Apple was not found so apple is currently within our set Apple was found but what about a pineapple let's change Apple to Pineapple currently there's no pineapple pineapple was not found we even could accept user input
let's say fruit equals input enter a fruit to search for if our fruit variable is in fruits let's convert these print statements to an F string add a placeholder fruit our fruit was found if this is true else our fruit was not found enter a fruit to search for is there an orange orange was found is there a mango main go was not found sets are efficient for membership test in meaning checking to see if there's a given element within that set art everybody so those are lists topples and sets lists are mutable meaning we
can change the elements and they're the most flexible tupples are immutable but they're faster than lists sets are mutable only when adding and removing elements they're unordered we can't access a set by an index they don't allow for duplicates and sets are the best for membership testing and well every everybody those are lists tles and sets in Python