Python Tutorial for Beginners
python full course::
let's write our first python code we're going to write print all in lowercase then add parentheses
then add quotes either single quotes or double quotes and inside this code, we're going to
write hello world so this is what we call a string a string means a string or sequence of
characters in simple words that mean textual data so in python and in many other programming languages whenever we're dealing with textual data we should always surround our text with quotes in python we can use single or double quotes now this print you see here is a function built into python and we can use it to print a message on our application window so let me show you
how to run this code on the top we go to the run menu and then select run note that there is a shortcut associated with this and I always use shortcuts because they increase my productivity so
Now select the app and over here you can see this little window this is what we call the terminal window and it shows the output of our program so here's the hello world message printed in the terminal window now as you learn more python you will learn how to build applications that have a graphical user interface that's an advanced topic so for now let's not worry about it
alright now let's talk about variables we use variables to temporarily store data in a computer's memory, for example, we can store the price of a product or someone's name their email age and so on let me show you so to declare a variable we start by typing a name for that variable let's say age then we add an equal sign and then we type a value let's say 20.
so with this we're, storing the number 20 somewhere in our computer's memory and we're attaching this age as a label for that memory location so now we can read the value at this memory location and print it on the terminal so instead of printing hello world we want to print the value of the age variable so ism going to delete what we have inside parenthesis and type age note that i'm nI'madding quotes because if i run this program we'll see the text h on the terminal we don't want that we want the value of the age variable so let's remove the quote and print the value of the age variable
now here on the toolbar
so as you can see our program gets executed from top to bottom so this is how we can declare and use a variable now let's look at a few more
examples so i'm going to declare another variable called price and set it to 19.95
so in python we can use numbers with a decimal point or whole numbers we can also declare a variable and assign it a string value so let's say first underline name so if you
want to use multiple words in the name of a variable we should separate them using an underscore this makes our code more readable see what would happen if I didn't use this underline this is not easily readable so we always separate multiple words by an underscore now we set this to a string so we can use single quotes or double quotes let's say march we also have a special type of value called a boolean value which can be true or false that is like yes or no in English let me show you so i'm going to declare another variable called is
online and set it to true we could also set it to false what we have here is called a boolean value now note that python is a case sensitive language so it's sensitive to lowercase
and uppercase letters in this case if I use a lowercase f
we can see an error over here because this is not recognized in python so false with a capital f is a special keyword in python that represents the boolean false value so this is how we can declare and use
variables in python all right now here's a little exercise for you imagine we want to write a program for a hospital so we're going to check in a patient named john smith he's 20 years old and is a new patient i want you to declare a few variables to store these values use the comment box below to share your code with others
In this course I'm going to show you how to receive input from the user in python we have another built-in function called input we use this to read a value from the terminal window let me show you so we add parenthesis then we type in a string here we can type a message like what is your name we had a question mark followed by a space you will see why we need this space in a second so let's run this program
we get this message now we have to enter a value so we click over here now you can see that the carrot is separated from the question mark this is because of the white space that we added over here
this function will return the value that we entered in the terminal window so we can get that value and store it in a variable let's declare a variable called name and set it to the return
value of the input function now we can print a greeting message for this user so we use the print function we say hello we had space now after the string we want to add the
value of the name variable so we use a plus sign and then type name what we are doing here is called string concatenation so we're combining this string with another string.
now run our program and see what happens so what is your name mosh now
we get this message hello mosh so this is how we can use the input function in python
you'll learn about the three types of data in python we have numbers strings and booleans now there are times you want to convert the value of a variable from one type to another let me show you so we're going to use our input function to read the user's birth year so enter
your birth here now this input function is going to return a value so we can store it in a
variable called birth underline year okay
now write code to calculate the age of this user so we write an expression like this currently we are in the year 2020 so 2020 minus birth year
this expression or piece of code is going to produce a value so once again we can store that value in a variable let's call that variable age now let's print age on the terminal
run our program and see what happens so my birth year is 1982. enter oops our program crashed so whenever you see this red message that indicates an error so this error occurred in this file that is our app. pi on line two right below that you can see the piece of code that generated this error so that is this expression 2020 minus birth year now below that you can see the type of error so here we have unsupported types for subtraction we have int and stir what are these well this end is short for integer and that represents a
whole number in programming so 2020 is an example of an integer now the birth year is an example of a string because whenever we call the input function this function would return a value as a string even if we enter a number in other words when I entered 1982 this input function returned a string with these characters 1982 so this string is different from the number 1982 they're completely different types so in this case let me delete these lines the reason we got this error is that we try to subtract a string from an integer so our code looks like this 1982 now python doesn't know how to subtract a string from an integer
so to solve this problem we need to convert this string to an integer now in python we have a bunch of built-in functions for converting the types of our variables so we have this end function we can pass our burst here to it and this will return the new numeric representation of the birth year
so to solve this problem we need to replace the string with the end function so let's see what's going on here on the first line we call the input function which returns a string on the second line we pass the string to our end function the in function will return the numeric representation of the burst year then we subtract it from 2020 we get the age and store it in the age variable now run our program so
we also have another built-in function called float that is for converting a value to a floating-point number a floating-point number in python and other programming languages is a number with a decimal point so 10 is an integer and 10.1 is a float so we have int we have a float and we also have bool for converting a value to a boolean and finally we have stir for converting
a value to a string so these are the built-in functions for converting the type of our variables
and then our program will print the sum of these two number
the second number now we calculate the sum so that is first plus second now let's see what happens when we print sum on the terminal so
so over here we're going to pass first to our int function and here as well now run our program
what if we eenter floatipoint pointnt number so 10.1 and 20. we got an error so to solve this problem
we need to treat both these values as floats so instead of the in the function we're going to use the float function Now
run our program one more time 10 and 20.once again we got an error the error is saying that python can only concatenate strings not floats to strings
to a string to solve this problem we need to convert the sum to your string so this is where we use the stir function
now run the program again so 10 plus 20.1 and here's the result
and one last thing in this exaI'me i'm calling the float function at the time we want to calculate the sum of these two numbers but this is not a requirement we can call the float function over here so this input function returns a string we can pass that string to our float function take a look so float parenthesis like this so the value that we're passing to the float function is the value that is returned from the input function similarly we call the float function over here now we can change this expression to first plus second that is another way to write this piece of code so type conversion is important in python and other programming languages there are times you need to convert the type of variable to a different type
i'm going to show you a bunch of cool things you can do with strings in python so let's start by
declaring a variable called the course and set it to python for beginners now this string that we have over here is technically an object in python is like an object in the real world as a metaphor think of the remote control of your tv this remote control is an object and it has a bunch of capabilities it has a bunch of buttons for turning your tv on turning it off changing the volume and so on now in this program, this course variable is storing a string object this string object has a bunch of capabilities so if we type course dot you can see all the capabilities available in a string object these are basic functions that you can call just like the print or input functions the difference is that the print and input functions general-purpose pose functions they don't belong to a particular object but the functions you see over here are specific to strings now more accurately we refer to these as methods so when a function
is part of an object we refer to that function as a method so let's look at a few examples here we have a function or a method called upper and we use that to convert a string to uppercase so if we print cou rse. upper
our original string will return a new string so right after this if we print couthe rse you can see that
our course variable is not affected so the upper method returns a new string
now similarly we have another method called lower for converting a string to
Lowercase
we have a method called to find to see if our string contains a character or a sequence of characters
for exampl,e here we can pass y and this will return the index of the first occurrence of y in our string so in python the index of the first character in a string is 0. so here we have 0 1 2 3 4 and so on so when we run this program you're going to see one on the terminal because the index of y is 1.
run the program
there you go now as I told you before python is sensitive to lowercase and uppercase
letters so if I pass an uppercase y here this find method returns a negative 1
because we don't have an uppercase y in this string we can also pass a sequence of characters for example 4. so this
now there are times we want to replace something in a string with something else to do that we use the replace method replace so we can replace for with a string containing the number for.
look so python 4 beginners obviously
if you look for a character or a sequence of characters that don't exist in our string nothing is going to happen for example if we try to replace x with 4 obviously we don't have x here so
nothing is going to happen
also just like the upper method the replace method is not going to modify our original string so it's going to return a new string this is because strings in python and many other programming languages are immutable we cannot change them once we create them whenever we want to change your string we'll end up with a new string object in memory now one last thing I want to cover in this course there are times you want to see if your string contains a character or a sequence of characters one way to do that is using the find method that we talked about so let's see if our string contains python
that is the index of the first occurrence of the word python in our string now in python we can also use the in operator so we can write an expression like this we type a string python then we type in this is a special keyword in python this is what we call the in operator so after that, we type the name of our variable so with this expression we're checking to see if we have python in the course as you can see python code is very readable it's like plEnglishlish
so when we run this program instead of seeing the index of the first occurrence of python we see a boolean vawhichthis is more desirable in a lot of cases next we're going to look at arithmetic operations
in this course i'm going to show you the arithmetic operators that we have in python these are the same arithmetic operators that we have in math for example we can add numbers we can subtract them multiply them and so on so let's print 10 plus 3. let me run this program we have 13.
so this is the addition operator we also have subtraction we have multiplication and division now technically we have two different types of division operators we have a division with one slash and another with two slashes let's look at the differences if you use a single slash
we gefloating-point point number that is a number with a decimal point but if we use double slashes we get an integer a whole number we also have the modulus operator that is indicated by a percent sign and this returns the remainder of the division of ten by three so
that is one and finally we have the exponent operator that is indicated by two asterisks so this is 10 to the power of three so when we run this we get a thousand now for all these operators that you saw we have an augmented assignment operator let me explain what it means so let's say we have a variable called x and we set it to 10. now we want to increment the value of x by 3. so we have to write code like this
x equals x plus 3. when python executes this code it's going to evaluate this expression or this piece of code the result of this expression is 10 plus 3 which is 13. then it will store 13 in the x now there is another way to achieve the same result using less code we can type x plus equal three what we have on line three is exactly identical to what we have on line two so what we have here is called the augmented assignment operator so we have this assignment operator but we have augmented or enhanced it now here we can also use subtraction to decrease the value of x by 3 we can use multiplication and so on so these are the arithmetic operators in
Python
all right let me ask you a queI'mon i'm going to declare a variable called x and set it to 10 plus 3 times 2. what do you think is the result of this expression this is a basic math question that unfortunately a lot of people fail to answer the answer is 16. here's the reason in math we have this concept called operator precedence and that determines the order in which these operators are applied so multiplication and division havhigher-orderrder so this part of the expression gets evaluated first so 2 times 3 is 6 and then the result is added to 10. that is why the result of this expression is 16. now in python operator precedence is exactly like math but we can always change it using parenthesis for example in this expression if you want 10 plus 3 to be evaluated first we can wrap it in parenthesis so like this now when we execute this code we're going to see 26 because 10 plus 3 is 13 and that divided by 2 is 26. let's verify this so print x and we get 26.
so you learn about the arithmetic operators in python now in python we have another set of operators called comparison operators we use these operators to compare values let me show you so i'm going to declare a variable called x and set it to an expression like this 3 is greater than 2. so what we have here this piece of code this expression is called a boolean expression because it produces a boolean value so in this case because 3 is greater than 2 the result of this expression is the boolean true so if we print x we get true on the terminal so here is the greater than operator we also have greater than or equal to we have less than we have less than or equal to here is the equality operator which is indicated by two equal signs do not confuse this with the assignment operator so here we're comparing three and two for equality so if we run our program we see false because 3 does not equal to 2. so here's the equality operator we also havethe not equality operator that is indicated by an exclamation mark followed by an equal sign so let's quickly recap here are the comparison operators we have in python greater than greater than or equal to less than less than or equal to equal and not equal these operators are extremely important in real python programs because quite often we have to compare values to evaluate certain conditions you're going to see that soon
in python we have another set of operators called logical operators we use these operators to build complex rules and conditions let me show youI'm I'm going to declare a variable called price and set it to 25.
now let's print a boolean expression like this price is greater than 10. now let's say we want to check to see if the price is between 10 and 30. this is where we use the logical and operator so we type and right after that we type another boolean expression surprise less
than 30. so with this and operator if both these boolean expressions return true the result of this entire expression will be true take a look so in this case we get true because the price is between 10 and 30 dollars
we also have the or operator with the or operator if at least one of these boolean expressions returns true then the result of this entire expression will be true to demonstrate tI'mI'm going to change price to 5. let's see how python is going to execute this code so first, it's going to look at this boolean expression is price greater than 10 no it's not so it will keep going then it will look at the second boolean expression is price less than 30 it sure is so the result of this entire expression will be true
so let's quickly recap in python we have three logical operators we have logical and which returns true if both expressions return true we have logical or which returns true if at least one expression returns true and we have not which inverses any value that we give it
in this course we're going to talk about if statements in python we use if statements to make decisions in our programs
for example we can declare a variable called temperature and depending on the value of this variable we can print different messages on the terminal let me show you so here's our temperature variable we set it to 35. now let's say ifthe temperature is greater than 30 we want to print a message saying it's a hot day so we type if then we type a condition and this is where we use our comparison operators so we type temperature greater than 30. then so we add a colon and see what happens when press enter now the character is indented and this represents a block of code so the code that we write over here will be executed if this condition is true otherwise it's not going to be executed let me show you so we're going to print it's a hot day and by the way, note that here I've surrounded the string with double quotes because here we have a single quote as an apostrophe so I couldn't declare a string like this with single quotes if I typed it's a hot day look python gets confused because it thinks this single code represents the end of our string so it doesn't recognize the subsequent characters, okay so that's why we use double quotes here so we can have an apostrophe in our string so it's a hot day now if I press enter again the carrot is indented so the code that right here will be part of our if block and it will get executed if this condition is true so here we can print a second message drink plenty of water now to terminate this block we press enter and then press shift and tab the carrot is no longer indented so the code that we write here will always get executed no matter what whether this condition is true or not now in c based programming languages like c plus c sharp java and java script
we present a block of code using curly braces so you start a block of code using a left
brace and then end it using a right brace in python we don't have curly braces so we use indentation to represent a block of code okay so in this case these two lines are indented and that means they are part of this block of code
now let's run the program and see what happens so we see these two messages because the
temperature is greater than 30. now if I change the temperature to 25 and run the program again we don't see anything okay now
after this block let's print done because this code is not indented it will always get executed it's not part of our if block okay so take a look here's the down message now let's add a second condition so if temperature is not greater than 30 that means it's less than or equal to 30. so
i'm gonna add a second condition so if the temperature is between 20 and 30 I want to print it's a nice day so here we type l if that is short for else if and here we type a second condition
so temperature greater than 20. we add a colon press enter now we have a new block so here we can print it's a nice day so if this condition is true that means the temperature is greater than 20 and less than or equal to 30. now what we have here is called a comment that is why it's grayed out it's not real code it's just some note that we add to our program python is not going to execute this so whenever we type a pound sign what we have after is treated as a comment okay so if this condition is true then we're going to see this message on the terminal
let's run our program and verify this there you go the temperature is 25 that's why we see this message
here's a great exercise for you to practice what you have learned so far i want you to write a weight converter program like this so this program is asking me my weight i enter 170. next, it's asking me if the weight is in kilograms or pounds soIi can type k for kilograms or l for poundsIi can type a lowercase l or an uppercase l it doesn't matter so let's go with a lowercase l now it tells me weight in kilogram is 76.5 so
so first we call our input function to ask the first question wait we get the result and store it in a
variable called weight next we call the input function one more time to ask the second question is this in kilogram or pounds we get the result and store it in a variable called unit this is where we're going to use an if statement so we want to check to see if unit equals k then we should convert the weight to pounds and print it on a terminal however with this code if i type a lowercase k this condition is not going to be true because earlier i told you that python is a case sensitive language so we need to convert this string to uppercase earlier we talked about string methods so if we type dot we can see all the functions or methods available in a
string object so we use the upper method and this returns a new string in the case now if this condition is true first we need to convert the weight to pounds so we declare a new variable called converted get the weight and divide it by 0.45 and then we can print this on a terminal so we say weight in pounds is then we append converted now to terminate this block we press shift and tab else colon so if this condition is not true that means the weight was entered in pounds so we need to convert it to kilograms once again we declare a variable converted and set it to weight times
0.45 and then we print weight in kilograms and here we concatenate this string with converter all right now we need to terminate this block so we press enter then shift and tab good
now if we run this program we're going to see an error let me show you so
let's run it here i'm going to enter 170 tIen i type a lowercase l okay here's an error can't multiply sequence by non-end of type float so this is where we got this error when we try to multiply the weight by 0.45
so that is line seven in our code now the reason this is happening is because the wait variable is storing a string object because earlier I told you the input function always returns a string so here we need to convert the weight to a number we can either use the end or the float function let's run the program one more time
the result of this operation is going to be a float now on line eight we are trying to concatenate or combine a string with a float and python doesn't know how to execute this code to solve this problem we need to convert this flow to a string so here we use the built-in stir function
so run it one more time let's say 76 kilos here we get a familiar error can only
concatenate string to string not float
so this error occurred on line five where we try to concatenate a string to a float so once again we need to convert this to a string object now let's run the program one more time 76 kilos is equal to 168 pounds
we're going to talk about while loops in python we use while loops to repeat a block of code multiple times for example let's say we want to print the numbers one to five a poor way of doing this is writing code like this print one then print two print 3 print 4 and print 5.
now why is this a bad approach well what if we wanted to print the numbers 1 to 1 million we don't want to write 1 million lines of code each line printing a number.
we'll start off by declaring a variable like I and set it to our initial number let's say one then we type while and here we type a condition so once again we can use our comparison operators
let's say i less than 5 less than or equal to 5. as long as this condition is true then the code that we write inside of the while block will get executed so here we type a colon then press enter now we have a block of code in this block we can print i and then we need to increment I y one so we set i to i plus one if you don't do this I will always be one and this block of code will get executed indefinitely it will never terminate basically our program will continue running until it runs out of memory so in the first iteration i is 1 is less than 5 so python is going to execute this block of code it will print 1 on the terminal and then i becomes 2.
then the control moves back over here so python evaluates this condition 2 is less than or equal to 5 so the condition is true and once again python is going to execute this block one more time in the second iteration
we're going to see two on the terminal and then I will become three so this will continue until this condition is no longer true
run this program now we see the numbers one to five so
this is the beauty of while loops i can easily change five to one thousand and by the way, i'm separating these three digits using an underscore this makes my number more readable we
don't have to add this but it just makes our code more readable so let's run the program
now we see the numbers 1 to 1000. it's much easier than 1000 lines of code each printing a number okay
we see this triangle shape because in the first iteration i is one so we see one asterisk in every iteration i is incremented by one so we see one extra asterisk and finally in the last iteration i is 10 and we see 10 asterisks on the terminal so you have learned about three types of data in python we have numbers which can be integers or floats we have booleans and strings these are what we refer to as primitive or basic types in python now in python we have a bunch of complex types as well these complex types are very useful in building real applications
in this course we're going to talkabout lists we use lists whenever we want to represent a list of objects like a list of numbers or a list of names let me show you so i'm going to declare a variable
called names and set it to a list of names so here we add square brackets to represent a list and now we can add one or more objects inside this list in this list we add a bunch of names like john bob marsh sam and mary so we separate this element these items using a comma okay now let's print our list so print names take a look it comes out exactly like how we wrote it now we can also get individual elements in this list for example if we want to get the first element in this list here we type a pair of square brackets and inside the square brackets we type an index the index of the first element in this list is zero so now when we run this program
we're going to see john on the terminal there you go now in python we can also use a negative index this is a feature that I personally have not seen in other programming languages so if 0 represents the first element in this list what do you think negative 1 represents it represents the last element in this list let's verify it so let's run the program
so let's run the program and here's some then we can also change an object at a
given index
for example let's say here we made a mistake and this john should not be spelled with an h so we need to reset it we type names of zero now we treat this like a regular variable so we set it to a new value we set it to john without an h
now let's print our list and here's our updated list beautiful
we can also select a range of values
for example let's say we're only interested in the first three names so over here we type square brackets and here we need to type two indexes a start index and an end index our start index is zero because we want to start from here and our end index is going to be zero one two plus one that's going to be three so we add a colon three so python is going to return all the elements from the start index up to the end index but excluding the end index so it's going to return the elements at index 0 1 and 2. take a look here are the first three names and by the way t,his expression does not modify our original list it returns a new list so right after this print statement if we print our original list.
you can see that it's not changed so this is how we use lists in python
earlier i told you that strengths in python are objects objects in programming are kind of like objects in the real world like your mobile phone your bicycle the remote control of your tv and so on they have certain capabilities so if we type a string here and then press dot we can see all the functions or methods available in a string object in python now lists are also objects so they have a bunch of methods for adding items or removing them and so on let me show you so iI'mgoing to declare a list of numbers let's say 1 2 3 4 and 5. now to add a new element at the end of this list we can use the append method so we type numbers that append
and here we type 6. now let's print our list
so here's our updated list beautiful now what if you want to insert a number somewhere in the middle or at the beginning for that
we use the insert method so we're going to call the insert method now on the top go to the view menu and look at parameter info look at the shortcut on a mac computer it's command and p on windows it's probably control and p if we use this shortcut we can see the values that this method expects so the first value that this method expects is an index value and the type of this value is an integer so if i want to insert a value at the beginning of this list iIshould pass 0 as the index of the first element right so let's pass 0. now the second value is highlighted so the second value is an object and the type of this is t that basically means this can be any type we can pass a number we can pass a boolean we can pass a string we can pass a list or any type of objects in python so i'm going to pass negative 1.
now run our program you can see negative 1 appeared at the beginning of our list
we also have a method for removing items so let's call remove three let's run the program
three is gone we only have one two four and five now if you wanna remove all the items in the list we call the clear method so clear this method doesn't expect any values so let's run our program our list is empty now sometimes you want to know if a given item exists in our list or not to do that we use the in operator so instead of printing our numbers list i'm going to print an expression one in numbers so here we're using the in operator we're checking to see if one is in the numbers list so this is a boolean expression it returns a boolean value take a look
so we get true obviously if we search for a value that doesn't exist in this list like 10 we get false okay now finally there are times you want to know how many items you have in the list to do that you can use the built-in lend function
so let's print len of numbers so len is a built-in function just like the print function that is why it's highlighted as purple it returns the number of elements in a list take a look
so we have five elements in this list
when writing python programs there are times you want to iterate over a list and access each item individually let me
show you so i'm going to declare a list of numbers one two three four five
now if we print this list it comes out exactly like how we wrote it using the square bracket notation but
what if we wanted to print each item on a separate line that is where we use the for loop let me show you so we're not going to print the entire list instead we're going to type 4 now we declare a variable which is called a loop variable let's call it item then we type in numbers next we add a colon to start a block of code so this is what we call a for loop with this for loop we can iterate over all the items in this list in each iteration this item variable is going to hold one value so in the first iteration item is going to be equal to 1 and the second iteration is going to be equal to 2 and so on so now if we print item and run our program
we see each item on a new line so this is how we use a for a loop now
equals I plus one
run the program and see what we get so we get the numbers one to five these are coming out from our for loop and then we get the numbers one to five one more time these are coming out from our while loop now if you compare these two approaches you can definitely see that the implementation using the for loop is shorter and easier to understand we don't have to use the square bracket notation we don't have to call the len function we don't have to declare a variable a loop variable and then increment it explicitly so with the for loop in each iteration the item variable will automatically hold one value in this list
we're going to talk about the range function in python we use the range function to generate a
sequence of numbers let me show you
so you type range this is a built-in function just like the print and input functions here we can pass a value like 5 and this will return a range object a range object is an object that can store a sequence of numbers let me show you so let's call that numbers so this is a range object in this object we're going to have the numbers 0 to 5 but excluding 5. now if we print
numbers
we're going to see this message range of zero to five not the actual numbers because this is the default representation of a range object to see the actual numbers we need to iterate over this range object using a for loop we can also iterate over a range object using a for loop basically we can use the for loop with any object that represents a sequence of objects so instead of printing numbers we're going to use a for loop for number in numbers colon
we're going to print number now we see the numbers zero to four so range of five
generates a sequence of numbers starting from zero up to the number we specify here
now if we supply two values the first value is going to be considered the starting value
and the second value is going to be considered the ending value and it's going to be excluded so the range of 5 to 10 is going to generate the numbers 5 to 9.
take a look there you go now we can also supply a third value and that will be used as a step so let's say instead of having a sequence of numbers like five six seven eight nine we wanna jump two numbers at a time so 5 7 9 and so on so here we pass 2 so we get these odd numbers 5 7 and 9.
so this is the range function in python like quite often you see the range function used as part of a for loop because we don't really need to store the result in a separate variable we can call the range function right here where we are using the numbers variable so we can type range of five and this will return a range object holding the numbers zero to four
you go so we don't really need to store the result in a separate variable like numbers there you go
we're going to talk about tuples in python tuples are kind of like lists we use them to storthe e a
sequence of objects but tuples are immutable which means we cannot change them once we create them
so i'm going to start by defining a list of numbers 1 2 3. now we use square brackets to define a list and parenthesis to define a tuple so now this numbers variable is storing a tuple
if we try to reassign let's say the first element we're going to get an error topple object does not support item assignment so this is what i meant by tuples are immutable they're unchangeable also if you type numbers dot you don't see any methods like append insert remove and so on we only have count and index count returns the number of occurrences of an element for example if we have let's say two threes in this tulle and call count of three this will return 2. the other method we have here is index and this returns the index of the first occurrence of the given element now these other methods you see here that start with an underscore they're called magic methods it's an advanced toc..... .



























































































Comments
Post a Comment