Python For Beginners: Data Structures | Sara Al-Arab | Skillshare

Python For Beginners: Data Structures

Sara Al-Arab, Computer Engineer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
36 Lessons (3h 26m) View My Notes
    • 1. Introduction

      1:06
    • 2. Installing Python

      0:58
    • 3. Strings

      4:25
    • 4. Strings: Slicing

      4:52
    • 5. Application on Slicing

      4:55
    • 6. Strings are Immutable

      4:45
    • 7. Strings: Iterations

      6:53
    • 8. Strings: Compare

      7:58
    • 9. String Methods

      14:40
    • 10. Application on String Methods

      12:36
    • 11. Strings: Format Operator

      8:03
    • 12. Files

      7:18
    • 13. Application on File Reading

      7:54
    • 14. Writing Files

      10:04
    • 15. Lists

      4:36
    • 16. Lists: Accessing Items

      4:28
    • 17. Lists are Mutable

      3:07
    • 18. Lists: Slicing

      5:38
    • 19. Traversing a List

      5:24
    • 20. Lists: Adding, Removing, & Updating Items

      17:46
    • 21. List Operations

      5:10
    • 22. Sorting Lists

      3:58
    • 23. Lists & Functions

      6:50
    • 24. Strings & Lists

      7:11
    • 25. Objects & Values

      2:50
    • 26. Aliasing

      2:40
    • 27. Dictionaries

      3:30
    • 28. Dictionaries: Accessing Items

      4:52
    • 29. Dictionaries: Changing Values, and Adding & Removing Items

      8:50
    • 30. Nested Dictionaries

      3:56
    • 31. Looping Through a Dictionary

      3:02
    • 32. Tuples

      4:46
    • 33. Tuples: Accessing Items

      3:45
    • 34. Comparing Tuples

      1:10
    • 35. Dictionaries & Tuples

      1:36
    • 36. Project

      4:36

About This Class

Welcome to Python for Beginners: Data Structures! Data structures are the fundamental constructs around which you build your programs. Each data structure provides a particular way of organizing data so it can be accessed efficiently, depending on your use case. Python ships with an extensive set of data structures in its standard library. In this course, we'll be learning about the most basic and important Data Structures in Python, along with extensive examples for each Data Structure that will help you understand more their importance. If you're a Beginner in Programming, I advice you to start with the Python for Beginners course I have released on my page. Here's the link to the course: https://skl.sh/32KleZ5

Transcripts

1. Introduction : Welcome to Python for beginners, data structures. Data structures are the fundamental constructs around which you build your programs. Each data structure provides a particular way of organizing data, so it can be accessed efficiently depending on your use case. Python ships with an extensive set of structures and its standard library. And in this course, we'll be learning about the most basic and most important data structures in Python, along with extensive examples for each data structure that will help you understand more their importance. These data structures includes strings, files, lists, and areas, and tuples. And if you're a beginner and programming, I advise you to start with the Python for Beginners course. I have released on my page and you can find the link in the class description. I hope you enjoy this course and learn all the basic data structure information you need to know in Python. 2. Installing Python: Before getting started in this course, you need to first install Python on your machine. And installing Python is really easy. And in my previous course by them for beginners, I've had a video that shows you how to download Python on your machine. And I'm going to show you briefly in this video how to download Python on your machine. So first we need to add integral and type install Python. And you go ahead and optimizing.org and download the latest version of Python 3.8.5. And whenever we downloaded on what do you make sure that your download less successful? Just go ahead cmd and type in Python. So you should return with the latest version of Python that you download, Python 3.8.5. And so that's all it is to download Python on your machine. 3. Strings: So first we will learn about one of Python programmings, fundamental data types, Python strings, including an overview of how to work with them, Parthenon stream methods and more. So let's get started. So first, a string is a data type in Python composed of a collection of characters. It can include letters, numbers, and special characters. So you can think of it as a sequence of characters that are indexed from 0 till the last strings. So let's say here we have the string that has five characters black. So 12345. So it is indexed from 0, not from one. So 01234. So it's like the length minus one. It ends with coordinate five. And so on. The expression in the bracket, you can see right here is called the index, as we said. And as we said, the index indicates which character so we want to access. Let's say the first character. We type in color 0, not color one. So when you type color 0, we all returned with me, which is the first character. Second character starts from one, and so on. If you try to type in decimal floating number, you'll be returned with a type error that the string indices must be integers and not a decimal number. If you want to print the last character, you just type in negative one. Yes, he can type negative indices that not allowed in Python. So, and the way that works is that minus one is the last letter in our string character. In our string. So k is my swan. Let's say I want the one before it. So minus two, minus 3y, minus four and minus five. So lets say you try to access an index that is not found. So you'll be returned with an index error list indexes out of reach, because we don't have five in our range. Our range is from 0 to four. So if you try to access something out of that range, we're going to be returned with an index error. Index is out of range. And in Python, and in strings, we have something that's called multiline strings, where the line breaks are inserted at the same position as in the code. So you don't have to add here a line break a slash n. In order to tell Python, please move on to the next slide. You just type it the way you want it to be. And when you print that multiline string, he'll be returned with the same exact position as you can see. We have a library card here. So you see we entered a new line without adding anything. It just write it the way you want it to be. And Python will return the same exact position as we wrote inside of our code. And we can get the length of our string using the length function that takes as a parameter variable, a variable string. So you can see this is an example. We were, we enter this variable color, which has a string of five characters. And when we use this function will return with the number five, which represents the number of characters inside of our string. And as we earlier said, if we try to access on a number that is outside of our range, we'll get an index error. And so here when we entered the length of our variable string, we will return with an error because the length gives us five hour. However, our indexes are numbered from 0 to four, as we said earlier. So it's length might s1. So if you want to access the last character in our string, you just have to empty the length minus one. It starts some boots. And now in the upcoming lectures we'll see how we can work with the clinics. 4. Strings: Slicing: So first of all, let's talk about slice. So what is slicing? A segment of a string is called a sludge. And selecting a slice is similar to selecting a character. However, we have a semicolon to indicate from where to where we want to slice our string. So first, we indicate which string variable we want to slice, and then followed by a bracket. And inside these brackets, we want to specify from where to where we want to spend to slice our variable string. And so the first index before the column is the starting index which is included. However, the last index which is after the column, is not included. So when we said we want to slice our variable from 0 to five, we're actually saying that we want to slice it from index 0 to four. So the last index is not included. So as you can see here, this is our variable string and you slice it from 0 to five inside the brackets. And as you can see, this is numbered index 0, so 012345. So you can see that five is the comma. However, when reprinted that sliced operation on the string variable, we did not have a common side our, of our string. And that is because as we said, we don't include the last index, so the comma is not included. And similarly here, our this slice is from seven to 13. So as we said, this comma, this comma is five. Spaces are also characters, so they also have an index, so 567. So as you can see it, because W is index 77 is included because it's the starting index. We start with w. And so W until 13. This indicates for us that the exclamation is not 1312 because as we said there tier Amazon included minus1 reminds one is 12. So having this in our print statement indicates for us that this is index 12. And we don't have to include something before the comma, the column, sorry, or after the cone. We can only have on the ending index or the starting index. What does that mean? So let's say I have this column and then five. So where do we start within decayed python? Hey, what are we going to start slicing our street? However, Python understands that when we don't add something before the column, Python understands that we wanted to start from index 0. So we wanted to start from H, and then we wanted to end at five. So five is the comma. We slice it before it so we don't include the comma. And you can see here. And similarly, if we don't add something after the column, That means that we want Python to go to the end of the string. So it starts at seven W and then continues until the end of the string. And if we ever added to similar ending and starting indexes, that means that we didn't, we didn't actually slice our string. We just have an empty string. As we can use negative integers and having characters as we saw earlier, like having the last character, we use boron swan. We can do that with slicing. So here we're trying to print this variable string be from minus six to minus one. So this is indexed minus1, minus two, minus three, minus four, minus five, minus six. So we start from minus six, which is W, As you can see. And then because minus one is the exclamation mark, and we said that the ending index is not included. So we don't include the exclamation mark, so we don't have it. So it ends at minus two, as you can see here. So this is one way we can work with strings. And now let's go ahead and open our Visual Studio code text editor and try to use on this slicing operation. On some examples. 5. Application on Slicing: So let's go ahead and create a file. Let's call it slicing the pie. And let's create a variable. Let's call it sentence and say hello, slash n, which gives us a new line. And let us see now my name. So let's go ahead and run oncologist trivialize what are we getting? And now, sorry, we didn't print that. So let's go ahead and print out in order to realize these region. So as can see, Hello, my name is Sarah. Now, let's say I don't want to print Hello, I just want to print my name is Sarah. And so in order to do so, we need to do some slicing. So we want to start from m. So, so let's count from the starting string 01234. And now this special expression, which is a line break, is considered as a space, as a character. It had, it has an index. So when we're counting 01234, this is five. And so this is a six. So we want to start from six. So the end of our string. So now let's go ahead and run our code. And now you can see that we started from m to six. And now let's go ahead and try to do that. How are using negative strings as are indicative indexes? So how could we do that? So, we want morbid, morbidly want our starting index to me if it, if it has to be negative. So in order to know that, we'll start counting from the ends of our strings. So this is minus one, minus two minus three minus four space, which is minus five minus six minus 789101112131415. So it's minus 15. And we wanted to also go to the end of the string. So let's go ahead and try to run our code. And we've got the same answer. Now let's say I want only to type hello. I don't want to type my sir. So how would we do that? Let's go ahead and say I want to start out my string from the beginning, and I want to end it on the line break. So 0123 or five. And why would we ended at a line break? Because as she said, the ending index is not included, so this line break is not included. So let's go ahead and print and see that we only are provided with hello. Now let's try and do that with a negative index, which would be a lot harder. And of course, I won't do that. Negative indexes are actually used in special cases when you think that mosquito cases but kids is that this thing that it's easier to use negative indexes then positive index. For example, when you want to slice always from the end of a sentence. So you use negative indexes, which would make it a lot easier. So as we said, that was minus 15. So let's try, I'm not sure, mice 123456789101112131415. So as I said, it would be not officially registered and use negative indexes, especially that I'm starting from the start of the string. So minus 15, minus 161718192021, so minus 21, so minus 15, which would make them sludge included because my screen is M, but that's socket. So you can see that we have the line break included because we wanted, we added mice 15, which would eliminate the m. However, it would include a slash and the library. So these are some examples of how you can use slicing in your string. 6. Strings are Immutable: One of the most important characteristics of strings in Python is that they are immutable. And so what does immutable means? So whenever you create a variable string and once you declared it, you cannot change it throughout your code later again. So let's say you've declared this variable string Hello world. And he decided later on that you want to change h to be as if you ever try to do that. So let's see, you're accessing the first character is 0 and you just adding an assignment which is as Python would return for you, a type error saying that this SCR object does not support item assignment. So that's what we mean by strings are immutable. You cannot change the variable wants, it's declared. If, if you type hello world, you cannot change it once again. However, you could try to do something else if you want to change something inside of your string. And to do so you declare a new variable. So let's say I have the string Hello world and I want to change it to buy world. So how would I do that? Well, I can, I can use the slicing operator that we discussed a while ago and added to this word by. So in order to have the same string will just buy instead of hello, I add by with this string starting from the comma. And so in order to start from the comma, I need to include the index of that column. And to do so we just add 012345. So we started from five, which is included because it's a starting index. And we don't add some editing at the right of our column because we wanted to until the end of a string. And so if we print it, you can see that we have by instead of hello comma world. So that's one way of changing the structure of our strings without changing the string itself by creating a new variable. So now you might be wondering what, how did we add this plus operator? I mean, how did we add two strings? And so that is called string concatenation. And string concatenation combines two or more necessary to strings using the plus operator. So let's say we have the string a, which says hello and NB, which says World. And I want to put it in one string. I wanted to say hello world. And in order to do so, we'll just say C. And you are will see as a plus B. And so I'm concatenating these two strings and we print C. We can see that we have hello world and can see that that space is not from anything from here. That space is from space inside of me. Ok? Whenever you concatenate, you're just adding the last character of that string to the first character of the second string. So o is the last character of a and the space is the first character of t. So you can see them here. And so when you concatenate, we don't have an extra space all by itself. No. If we took that space, we would see hello world as one word. It would be all. The letters would be stick to each other. There wouldn't know, sorry. There wouldn't be any space. And other operators that we're going to see regularly throughout this course are the end and the not in operator. And so these two operators are Boolean operators that take two strings. So let's say string a and string black. And this, this and operator will return for us true if that string is inside it, a substring of that string. So a is indeed a substring of that string, which is here found your a. And so it returned for us, true, not necessarily to be one character could be a word. So here is color, and we'll see if it's inside of the strings is black. And because, and you can see that you returned by false because this world is not found inside of that string. And we're going to see in the upcoming lectures, where do we regularly use these operators? 7. Strings: Iterations: So as we've seen in the previous course, Python for beginners, if you have watched it, we've introduced iterations that we can apply on sequences. And we've clearly seen previously said throughout this course that strings are considered as a sequence and so we can iterate through them. And in the previous course, if you've watched it, we try to go through a string. And we did so by initializing a variable, let's call it index. And as long as that index was less than the length of our string, we're going to lose. Add a wild loop. And so inside of that while loop, we're going to have a character which is at the index of our string. So it starts from the first character at 0 until it's at the end of the string. And each time we're iterating, we're looping through the character of our string. We're going to print them. As you can see the output here. And each time we increment our index by one. However, there is a simpler and a more organized code that we could do for same output. We could just add up for just two on statements, four and an operator. And that's a, so for every element inside of this sequence, so for every a inside of the sequence of this string variable color, we're going to print this element, this character. So as you can see here, it would iterate on each variable, on each character or element inside of this sequence, and it will print that character. So these two codes provide us with the same output. However, the second one is much simpler. The first one. Another example to iterate through a string is to, in order to find a character inside of myString to check if let's say a is inside my string variable. And so in order to do so, I iterate through every character inside of that string. So foreign and operators. And if that character that is N side of my variable string is equal to a, I'm going to increment my count. And if my count is one, that means that there is indeed this character a inside myString. And if this count is 0, then there is no character inside a inside of myString. Now let's go ahead and take an example on iterating through strings and Visual Studio code. So let's try to write a code that counts the number of our letters inside of our sentence. So let's call it count v dot. And in order to count the number of vowel letters inside of our string or sentence, we have to first onCreate a string that contains all of the bowel, bowel letters. So which are a, E, I, O, U. And let's also include that in Capital. And in case we have capitals. So let's create our sentence. Let's say contains hello. My name is Sarah, and I'm learning Python. And in order to count the number of power letters inside of our sentence, we first need to initialize a variable to 0, which is count. And let's say I want to count all of the characters of my sentence. However, I want to store them inside of another variable just to compare on these two numbers. So let's call it count to initialize it. Per now, we add our for loop. So for every character inside of our sentence, we're going to see, we're going to check, sorry. We're going to check if this character is inside of this v is a string that contains all of the letters B. And if indeed it is, we're going to increment a counter, we're going to increment count. And when we exit the if statement, we're going to increment counter because we want to count all of the literacies of inside of our street. So counter increments every time we iterate through every character in sidebar, Spring Harbor count is only incremented if this character is inside B. So now let's go ahead and check that. It's working. So vowels count and print characters for all the character string. And let's just put some space in order to visualize. And let's go ahead and run our code. And you can see that vowels is 0 because I miss type this it has to be int. So if character is NB, we're going to do that. So now let's go ahead and run it. And you can see that bubbles 13 characters is 45. So let's just make sure that this is right. So 12345678910111213. So it's correct. We have 13 bubbles out of 45 characters. So this is one way we could use declarations to iterate through strings from Python. And there are many other uses, four iterations of strings and many other data types. 8. Strings: Compare : Moving on to string comparison, Python string comparison is performed using the characters into strings and characters in both strings are compared one-by-one and one different characters are found, then the Unicode value is compared. The character would lower. Unicode value is considered to be smaller. So what is the required value? So capital letters from a dataset have encodes values ranging from 65 to capitally. So 92 capitals. And in between, let's say B, B is 6667, and so on until we reach said, which is 90. And similarly for small letters a to Z, a is 9798, ranging until we reach zed, which is one to 122. And you can see that those molars are greater than the capital on a political level. And here's an example where we ask the user to enter a word. And if this word is smaller than a block, so it's word comes before it. So if we're trying to sort, let's say some words. This word is going to be before black. And if this word is greater than black, then word is before, which means word comes after black, which means that blacks before. And if it is equal, then we say that only means that we've entered black. The exact same words. As an example, on this word would read the word pink. So let's say the user entered pink and we try to compare pink to black. So first let's go ahead. As we said, we compare them character by character. So we compare p to be. So automatically when we see this capital and this is not capitalized, we automatically say that b is greater than pink because and be a, sorry, black is greater than pink because b is greater than p. So that's why we printed that pink comes before black. So this is a way to compare strings and this is how actually sort functions do sort words. They use this string comparator. And then let's go ahead to Visual Studio Code and try to experiment with this strings compared to. So let's go ahead and create a file and compare. And let's say we have a sequence of words that say Sarah, Python, coding. Let's put some small case, lowercase, sorry, words to say lower. Let's say hello. And I think that's enough. And let's create a while loop or a for loop. So for every, let's call it sentences. And sentences, we're going to compare each element. So let us say S is greater than. Let's compare them all to the world. And if S is greater than Sarah, we're going to print. And I'm going to print it. And let's go ahead and run our code. So you can see the results. So Sarah, on, as you can see here, we got output greater, Which means that this string, Sarah is better than the capitalized version. Ok? And so why is that? So addToSet RECOMB per character by character. So as an us, nothing changed. Same characters. Move on to a smelly and big a. And we know that the lower a is greater than Unicode than the higher than the upper case a. So that's why we have greater and the same for R and a because they are greater than o and it remains greater. Now for our second element, Python, we got smaller and see why. So first of all, P is smaller than s And alphabetic and alphabetical order. And seeming Unicode because it comes after it, sorry, before it. So S is greater. And although we have small, smaller case, lowercase on elements here, however, psi, this, the sum of these is still greater than that of Python. And the same is for coding. If you go now to lower, we could see that slower is greater than. And you can see it here, is greater than Sarah. So L is indeed greater than S, o, all these debtors actually, all this lowercase elements are greater than Sarah and the number of letters is greater than the number of literals here. And that makes us really sure that this string is going to be greater than Sarah. Now as we move on to higher, we can see that S is greater than h. Unicode. And r also is great, is greater than many of these characters right here. So we can estimate, we can be sure unless we actually do it, but we could estimate before we start seeing the results that this is going to be smaller. And indeed, this because we have two characters that are greater than all the characters found here. And lastly, we have all this smaller case, sorry, lowercase, center word hello. And we can be sure without even looking at the results, that this is going to be greater because it's all lowercase and this is all uppercase. So automatically this is greater. So I hope this all makes it clearer for you on how comparison is done in Python. 9. String Methods: When we talk about strings, we definitely need to talk about the string methods that we use on the street. And so first, strings are an example by the object. And object contains both data, the actual string itself, and methods which are effectively functions that are built into the object and are available to any instance of the object. Button has a function called the IR. Which one it's called it list the methods available for an object. And the time function, as we saw and Python for beginners shows the type of an object. And as we said that the IRR function list all the available methods. So all the available methods for the string object are written here. We have, let's just, she's sorry, C. Some of them capitalized, replace split, grades, strip right. Split. There are many that we're going to see now as we go into the course. And so first of all, let's go ahead and see the capitalized function before we discuss it. If you ever want to use help to get some simple documentation on a method, you could use the help function. And so you just type the class followed by a dot and then followed by the function, the name of the function of that class. And whenever you just type in a and help, the help function will give you help on Methodist descriptor, which is the first sentence. And this how it functions like a descriptor. It will describe for you what this function is going to do. And at the last sentence could see that it explains to you that this function will return a capitalized version of As, which is variable string. By making the first character have uppercase and lowercase. So the capitalise only capitalize the first character. So this is an example. So here is a variable called color, and we have a string of blackness. And this is another variable, variable where we capitalized this variable color. And when we print color one, you can see that the first character as capitalize. And you can see that we did not say color, it will capitalize because as we said, strings are immutable. If we did say color and not create a new variable, we would have an error because an STR doesn't accept an assignment. So this is the capitalize function. Let's go ahead and see other functions. So we have the operand lower functions. So we saw earlier that the capitalise only makes the first character of uppercase. However, the upper function converts all lowercase letters and strings to uppercase and the lower. So not only the first character but all that lowercase characters. Letters and the lower, on the contrary, will convert all the uppercase letters in a string to lowercase. So here's an example where we have a variable called color, just black and a string. And when we say color dot upper and save it in a new variable, we could see that all our characters are upper case, not only the first one. And this is an another example where we have not only and upper and lowercase characters, but we mistake you have to have a character comma, so that's better and just edited. Okay, so here we have this string which has lower and uppercase characters. And when we type in lambda2 slower, you can see that all the uppercase letters, which are ancient, w are now lowercase. And tears and new method, which is the starts with, let's see it. So the startswith method, OMB determines if a string is a substring of a string and returns true if so and false otherwise. So here we have line equals to Hello World and want to see that line starts with hello. So it could be a string of words or a character. By itself. It doesn't need to be a character, could be word asthma. So here we're going to see line starts with this string which is hello. And indeed, Hello is a substring of that string. So it returns true. If it does. If it does not. You can see that the case here, so line that starts with age. So you can see that our line does not start with page starts with a capital H, So we are returned with holes. So going to the previous example here, when we did land clobber that starts with H on line is here. So firstly, when we change it to lower, so here is our new string we're working on, and then starts with H, and it is indeed starting with small lowercase h. So it gives us true. So that's what the startswith method does. So now moving on to the find method. So the find method determines strain, occurs a string or a substring, and returns the index if it's found and minus1 otherwise. So here's an example where we have a variable fruit which holds a string banana. And you want to see if that string has the character a. So we use the fruit, I'll use the variable first and then dot find, which is the function, and add a parameter of that. On character a. And so if it is found, the find function will return for us the index of this a that it just found. And if not, it's going to print my S1. So now we print index and you can see that we have one. So first of all, you can see that we have more than 18. So how does this function returns for us? The index doesn't return the first, the last index or out what does it do? So the find function start as follows. It goes to the first character c. If it is an a, and it doesn't see that is an a. So goes to the next character and then finds that it is equals to a. So it finds the character that you're looking for and it stops. It doesn't move on. So it stops and gives us the index of that character that you're looking for. And so it stops at the first one. And that's why we are returned with one. Let's say we enter a string that is not found. So as you can see, that will return with minus one. And don't forget, these are written in the Python interpreter. Okay? Not in the text editor lever you S3. These columns, that means that we are writing in the Python interpreter. And now let's say I don't want to fight to search for a character on a search for hours. Maybe two characters may be a word such as an a. So let's say I want to find if there is an a in my string, and you can see that it goes through the first string, doesn't find an m. Then it goes to the second, it doesn't find an n. Then go to the third, it finds an end. Now it going, it's going to search for the second character, which is eight. So now it goes to second character starting from n. And it seems that it is indeed a. And it found what we are looking for. And so it returns for us the index of the first character in our string. So 012, you can see that two is returned. And now, as you can see here, we have two patterns of NAB have this first_name and then the second ANY. What if you are looking for this and a how do we do so? So in order to do so, we can add another parameter to our Find function, which is going to indicate for the following function to, from where to start. So here I want my find function to start from the third index to search for this. And so the third index is 012 is here. And so it's going to start from starting from the character after here. Sorry, from a. So after N such as AB equals 0123, so add three on, it, sees that a is not an end. It goes to N, finds that they are saved first doctors, now yours to find if there's an a resided and yes, indeed, a is decided. So it returns for us index for, because 01234, it starts from four. And let's say I don't want it to. I wanted to give me that same answer. So we specify that we want to start from 0 and ends at three. So 0123, and it ends here. So here we are returned by two. So this is all that it is for the find function in strings. And now we reach that strip function. Step function returns a copy of the string that we're working on in which all the characters happened stripped from the string. So if we don't specify the character that can go into script on the string. That is similar to this example right here. By default, we're going to strip the white character, but we're going to strip the word characters only from the left and the right. And not necessarily the word kangaroos. Any character that we're going to strip from a string is going to be stripped from the left and the right, nothing in the middle. So here we're going to strip the white characters by default from line. And you can see that all the white characters that are before age and older word characters that are after they are. And let's say I want to only strip that white characters that are on the left or on the right. And so to do so we can use the right strip and the left strip. So here, the same example, however, we don't distribute, we write stupid. And you can see that from the right or the word characters out. Death, Strip. However, from the left they are same. And if you use a left strip, all the left white space characters are stripped Hubbard from the right. They are the same. And here's another example where we use a character and not the whitespace. So here let's say I want to remove all these files on integers from the left of my string. I use l strip and specify the file number. And you can see that all the five had been stripped from the left. And if I want to strip all the exclamation marks from the right, I just slip or strip them from the right. And you can see that there are stripped. So this is the strip function. On another useful function is there place function or method which returns a copy of the string where all the occurrences of a harpist by me. So here's an example. We have hello world, string, and we were going to replace all the occurrences of h by m. And you can see that HCl, which is right here, is replaced by m. And the last method we're going to see string method is the split, which splits the string into substrings. If it finds instances of separator and it returns them as, as, as a sequence. So here we have this string hello world, and we want to split it according to, we can specify the delimiter long space according to it. And if not, by default, we're going to split according to the space. So here we're going to split when you see a comma. So here, this is hello, it splits and the comma, you can see. And then after the comma space, whitespace and I'm World, and you can see the whitespace world. However, here we're going to split according to the whitespace another comma. And so it's going to take all this part with the comma. And then it sees the whitespace, splits it here and Gibbs On the second, which is worse. And so it returns them as sequences. And now we're going to see how we could use this string methods and exercises, which we're going to write on with also your code text editor. 10. Application on String Methods: So now that we've seen the string methods, let's try to use them. So let's go ahead and open matlab pi. So inside mental Pi, we have a string, a variable data that has, that carries information. And from its name mail. This data is male, so we have a bunch of mail that you want to read. So we have the Fromm slash to slash and subject slash and slash. N is the brake line, so it breaks new line. And last one is the body. And you can see now that we have a star indicating that now we have another male. So we repeat the process again. So from slash to slash and subjects slash and body, and now another star negating another mail and so on. And so we want to read this data in an organized way. And so how would we do that? So first, we'd like to separate these males from each other. We want to read each made at a time. So to do so, we're going to create a variable called names. And these males are going to be stored using the split method that we've seen. And so we're going to split up according to what? According to the star. Because as we've said, as we've seen, each male is separated from the other by a star. So we use that start to separate these males. So now we should, this, should, this method should return for us a sequence of males. So let's just go ahead and print in order to make sure that what we are doing is right. So we'll go ahead and run our function. And as you can see, we have series of males. So this is the first one from common, from all information on net come from. So these are three meals that we'd like to manipulate or let's say read that we'd like to work with. So in order to work at, with each meal at a time, I'm going to use the operator and the L. So for every meal in males will going to use that male. So now the second thing we'd like to do is to read on the parts of our mail and also one at a time. So in order to do so, let's also use another split. Ok, so let's go ahead and say parts of that email equals to mail dot split. And we're going to split them according to the line breaks. So each line break is going to be consist, is going to consist an element in that string. So let's go ahead and just print in order for you to understand what I mean, to print parts. And let's just remove that for now. And now, let's go ahead and run our code. Now as you can see, we have three series, sorry, sequences because as we said, we have premiums. So this for loop is going to loop three times. And so inside of this series sequence, we have the first line, which is from the second line to come on subject, come up buddy. So we have four parts in our code. So I'm sorry for parts in our series, sorry, I keep saying theory sequence. Okay? Now, now that we've visualized how our sequence has got this is looking like we'd like to use some information. So let's say we'd like to know what's the host of the email that we're receiving. So in order to do so, we have to in some way take this segment of the email. So how could we do that? So firstly, we've decided we want to work on this first item in our sequence. So let's go ahead and extract it. So we have, let's call it from because we're going to, sorry, from one. We're going to extract the first sequence, that item of the sequence. So our sequences coal parts, so port at index 0, y index 0 because we want the first and indexes start from 0. So that's from one ports at 0. And now we have our string. Let's go ahead and print it. To see that we took actually the correct string. So printf. And let's go ahead and just delete that. We've already made sure that you've got the right sequences. So let's go ahead and print. Now as you can see, we have the from, from RAM of each of these sequences. So now that we've got the first item, we have the From, and we'd like to extract the host, as we were saying. So how would you do that? So what's something special about emails? You might think that the app is something special. You don't see a always, you often see it only in emails, and especially here in our code. You can only see it when in that email. So we can use that in order to find our host. And by find, I mean find that method, find. So find, as you said, returns for us the index of a certain element searching for. So let's go ahead and call this index because we know that our find method is going to return for us an index to an order for not, for us, not to forget what this variable holds. Let's call it something similar to what holds. And So index is from one, is this right here. Each one of these 0.5c, we'd like to find where is that? We'd like on this phi function to find for us the index of app. And now that we've found the index which is right here, we'd like to extract on everything from after that, that until that's placed here. So how could we find that the index of that space here? So to do so, we need to use that find again. So Let's call this end as the ending index that we want to find. And so we're going to find friends valuable from one space. We want to find a space which is right here. However, we have a lot of spaces. How would this method know that we want that space here? We should return for us the space that immediately finds which would be here in that case. So how would we tell find to bring for us this space that is right after that email. And we already found the index of r. So we can use that to find that space. And so as we, if you remember in the previous lecture, we said that we could add two parameters to the app, to the fine one for the starting and the ending index. So we'd like to find the space that is, start searching right after that. And so we add the index. So now find method would start searching more. That's based right after that ad. And so we'd get the index of that space, which is really good. So now all we have to do is get that substring and so reduce the slicing. And so Let's call that host. And host is from one, from the index k And to the end. And so the end is the whitespace. And so when the end is not included, so we do not stop on the amazon.com. And in order not to print the ad will just add plus one here because the index is the index of AD. And as you said, the starting index is included. So in order to not included, we add one. Now, let's go ahead and see if our work is. And let's go ahead and print host. Let's go ahead and run. And now as you can see, let's just see, removed that in order to see it clearly. As you can see, we have the host names of the three emails, right, written right over here. So we've found the hosts. Now let's say I want to capitalize the first letter in order to make it seem better. And let's say I want to take the.com and we'll just strip it from the.com. So one step at a time, let's go ahead and capitalize it. So first of all, we have to create a new variable because as you said, hold, sorry, strings are immutable. So host dot capitalize. And the second thing we want to do is to strip it. So hose too, because again, strings are immutable so opposed to is host one dot. And we want to strip it from the right, so we add a right strip. And Charles would be this.com. Let's go ahead and try that. And so nothing happened because we didn't print or host. Let's go ahead and print toaster. And now as you can see, our first character is capitalized, and we removed that.com by split, right? Stripping it from, stripping it from the right. And now our host names are clear. So that's one way we could use this string methods. And you saw how they are really beneficial and useful when writing programs. This is just a small example of how important these methods are. They are much, much important and that when writing codes in the future, when you become experts. So that's, as I said, an example, I hope you understood these methods better now that we've done an exercise on them. And now we'll move on to something else. 11. Strings: Format Operator: One of Python's coolest features is the string format operator, which is unique to strings. And so the format method in string contains curly braces as placeholders, which can hold arguments according to position or keyword to specify the order. So here we have this correctly Bracelet. They have no arguments. As this is a default border. There are three types of orders. Lucidum, this is the first type, default order. And so here we have our first Killian bracelet, and this is our second curly bracelet. As I said, they don't hold arguments now in this type of order, which is the default. And each chronic bracelet will be a placeholder for the first argument and the dot format. So this curly brace, that will be for this string, and that would be for that. And so just by default. And when I print this one, you can see that this please holder, sorry, reset will take hello Intuit. And so you can see that Hello took the place of that curly brace it Kala. And my name is, and that is instead of that currently place that we're going to add that variable, that strength, which is 0. So this is our first type of orders. The default order is go ahead and see the positional order the second time. And so on the positional order. We have these coordinate bracelets. However, we have an argument that holds the index of on the or the order of our formats. So here we have this first done Bible inside of our format which has index 0. And then here is the second has index one. And I want my first girly preset to have the one indexed at one. So which is hello? So you can see that hello is here. And now my name is, and I want my curly blister to go to the first index 0, the first variable which is 0. And as you can see, Sarah is replaced by this codebase. Sorry, the curly brace, it was replaced by Sarah. And the third and the last type of order is the key word order, where our variables are named by keywords. So a hello equals to Sarah, and I just call them by their name. So here is a, I want a2 fill in here. So it goes away and seize the day is hello and just puts Hello setup and so on. And don't forget to add a dot format. Without the dot format. This has no meaning because the dot format is doing that job for us. Now. And our modern way of using the format, the format operator. There is a relatively old way. And the old way uses the percentage, which allows us to construct strings, replacing parts of the string with the data stored in the variable is similar, a similar concept, however, different styles. Okay, so when applied to integers, we know that this percentage means that it's a modulus operator. We've said, we've seen that in the Python for Beginners course, if you've seen it. But when the first operand is a string, this percentage means that this is a format operator. We'll see that in the next slide. And there are three types of percentages. Percentage d, percentage g, presented last for the format operator. And recited deer indicates that our variable must be a integer and a small percentage indicates that our variable must be a floating point, percentage S, which indicates that our variable must be a string. So let's go ahead and see an example in order to understand this much better. So here's a variable which holds the value 42. And now we have another new statement that says, I want percentage d decimal. And the variable to take this, take the place of this percentage d must be the valuable likes. So when I write this on, the likes will be replaced by 4242 will be here instead of percentage d. So when I print percentage g inside of these strings, and then followed by a percentage and the name of the variable we returned by the value of that variable. And this is another example. We have the same variable likes. And now I want to write a sentence. I don't want to just be a percentage d. So I have and percentage steel likes. And as I said, after I finished that strength, it's followed by a percentage on the name of the variable likes. So you can see here I have 42 likes. So the value of that variable is here. And of course, it's not only a percentage d We can use presented gn. So percentage deal, as I said, must be an integer. So three, in three days I have gained. And percentage d must be floating points. So here is a flow important, 33.1. So 2.2 k with the space and that person as indicating that the variable must be a string. And it's replaced by followers, which is the string. And so that's how we use for operators. But you have to be careful while using them. You can't use percentage G while having the number to be affording point because that would give us an error. And sorry, and as you can see here, when we add several variables, we just add a comma. And this form is called a tuple, which we'll talk about later. And the number of elements in a tuple match number of formats sequences in the string. And the types, as I said, of the elements also must match the format sequences. So let's say here we have 34 format sequences. However, the tuple only includes two, will be returned by a type error saying that not enough arguments for format string. We don't have three arguments, we only have two, and that is not enough. So you have to have the similar, identical number of numbers here and here. And the same if you add, let's say a percentage she. And so here, Python X6 is expecting you to add an integer. However, you added a string, which is dollar, so it returned for you type error saying that percentage steel format and number is required, not a string. So you have to be careful while using this format operators. And you can see how this format operators are really beneficial for you as a programmer when you become more advanced. And it would help you so much writing your code so fluently. And so that's one of the things that makes Python unique. As it make, it helps programmers write codes and simpler way than other languages. Other programming languages. 12. Files: Python also supports file handling and allows users to handle files, to read and write files, along with many other file handling options to operate on files. The concept of file handling has stretched over bias other languages, but the implementation is either complicated or lengthy. But unlike other concepts of Python, this concept here is also easy and short. Python treats files differently as texts or binary. And this is really very important, and we'll see that in a moment. Each line of code includes a sequence of characters that form a text file. And in order to read that text file, we have some rules. So each line of a file is terminated with a special character, which is called the end offline, AOL. And so the end-of-line characters like comma or the slash n character, which says that we need a new line and that current client and tells the interpreter that anyone has begun. So let's see what we mean by that. So first of all, there are four different methods for opening a file. And when we want to read or write a file, we must first open that file. And opening that file communicates with the operating system which knows where the data for each file is stored. And when you open a file, you are asking that operating system to find that file by name and make sure that that file exists. And that file must be stored in the same folder that you are in when you are starting Python. So it has to be in the same project you're working on. And so as we said, we have this open function which has two parameters. The finally, when the mode and the file name is the name of our file, and the mode is what we'd like to do with that file. So we have four modes. Let's say we want to read a file. In order to do so we add an AR parameter. So it opens the file and reads. And we have, if we want to write something into our file, we add the W character, which opens our file and create and write whatever we want to write in it. And if we do not have this while, it creates it for us. So let's say, I say that my file is called demoed and texts and the operating system does not find the swale. That's no, that's not a problem. It creates that file for us. And this while loop would be found in the same project you're working on. However, when you write into a file, let's say there is an existing file and you want to write into it. Overrides everything. So let's say you have a bunch of texts. It raises all these stacks and rewrites whatever you want to write for it. And let's say you want to do that. You want to write some text. However, you don't want to raise the original texts you want to add to it. And so to do so we append. So we add that a character. So it opens the file and appends on whatever you write to that file. And at the same as right, if there is no file at grease it for us. And last day, let's say we only want to create a file. We don't want to write into it. We want to append Intuit. So we just want to create, and for that there is the x character which creates the specified file. However, if there is a file that has the same name, the Python will return for us an error because that file already exists. So we cannot create a new file that has that same name of our file. And so we can also specify what should be handled. So our file, as I said earlier, could be handled as binary or texts. And that's very important because each has its own way of interpreting the file. And so if we want to be if you want our file to be handled as tabs, we add a tier two hour mode powder. If you want to handle it as binary, we just add the character b. So here's an example where we want to open that file and we want to read Intuit. And we wanted to be read as texts, not as binary. So we add our entity. And it's no, it's worth mentioning that if we do not add any parameter to the mode, the open function will by default read that file. So if you don't add anything here, it will understand that you want to only read that file so it opens it and treats it. So if they're open is successful, the operating system returns as a file handle. The file handle is not the actual data contained in the file, but instead, it is a handle that we can use to read that data. You are given a handle if the requested file exists and you have the proper permissions to read the file. So here is the, let say the handle, the file handler that we created. And we can do a bunch of things with that file handler, can either and read from it or write from it or close it or arpanet. And so let's say I want to write some, Sorry, I want to read that file handle. So let's say my file handle is called f, Let's say so f dot read would return for us the text that is inside of that file. However, if the file does not exist, open will fail with a prospect and you will not get a handle to access the contents of a file. So here is an example where we open a text file and save it in a file handler. Handler. Python sees that there is no, there is no such file in our project. And so it's a Trojans, whereas an error, a file not found error, no such file or directory. There is no file called texts. And you can add a path for your file. Let's say it's not in your project, so you can just add the path. And that would open our file handler. And now in the upcoming videos we are going to see how to read files, how to close files, how to write files, how to delete files. So we see that in a moment. 13. Application on File Reading: Let's go ahead and try to open and read files in Python. And so as you can see here, we have a text file inside of our project, Python. And it's called demo dot text, which contains some texts. And we'd like to read that text. So let's go ahead and open creative part. It's called a tree, it that pi. And I'd like to read that text file. So you might be wondering, why would I want to read that text file? I can just open it, click it and just read it. However, we're just seeing on beginning, beginner level on how to read files that are found in our project. However, when you get more advanced, you'll use these same mass methods, exact methods, however, to read files from the web. Maybe. So they are not. When you get advanced, these bars are not going to be found in your project while you want to find them and then read them and so on. So however, because we are just beginning to explore these methods, we're using files that are found in our project. So let's go ahead and first create a file handler. And 20 have to open our demo. That text file. We can specify that you want to read it. And it's not necessary because by default it's going to read it. And so let's go ahead and look inside that sequence of lines. And let's just bring up just in order to see what we are going to return with. So let's go ahead and print line. And you can see that these are texts work. However, you can see that the statements are really far away from each other. Which means one thing that might happened, which is that we're skipping two lines at a time. So what do you do that we're going to add the line to line right? Strip and older to remove the whitespace, which is causing us to skip more than once. So now, let's go ahead. And you can see the difference. They are a little bit closer to each other now, which makes us owe DEX file matter. Now, let's say I let's say I want to count the lines in myText. So let's initialize a variable. And then each time we are looping through a line, we're going to just count. And when we finish we'll print that count. So that now have 34 lines. Now, you can notice that each line in our text file either starts with a or B. And let's say I want print, not all, not all the slides. I only want to print the lines that start with a. And to do so, we're going to add an if statement. So the if statement says that, if, sorry. So our line starts. And that column was the author of that. So if that happens and we're going to do so, put it after we stripy line. And of course you have to indent. So actually in order to reduce some executions that just so our line starts with a, we're going to strip that line and strip it from space. And then we're going to print it, and then we're going to count it. So now let's go ahead and see what happens. Now. As you can see, we are only returned with the lines that are start with a and now the number of lines strength from 34 to seven. So that's how we try to minimize our search mechanisms. So here I'm only searching for words and sentences that start with a. Now, let's say we also want to add another condition. We also want our statements start with the specific, to have not stopped to have a specific word. So let's say, I want my sentences to have, let's say the word. Let's say I don't know Python. So I want all the statements that have that quality not Python executes. So how could reduce that? So first of all, we made sure that our son, our lines that starts with a now we want to make sure of something else which is online, are going to find the character string that we want to say execute. Execute equal to minus one, which means that we did not find it. We're going to continue. So we're going to go ahead and read the next line. Otherwise we're going to continue and we're going to skip that continue. And distributed line printed and counts. So let's go ahead and see how many lines do actually have the word execute. So let's go ahead. And you can see that you only have 11 line that has executed. This executes. Let's search for I don't know statement. We can see that we have many statements. However, do I believe that the number of lines will return to seven because all of them have that word. And that's true. So you can see using that, we can actually search for keywords in our sentences. And that's a really powerful mechanism when searching for, I don't know, keywords for something that you need. So that's an example on how to open and read files in Python. 14. Writing Files: Now we've reached the part where we write while some Python and writing to a file requires a few decisions. The name of the file in which to store data and the access mode file available are two modes, writing to a new file and overriding existing data and appending data at the end of the file that already exists. According abbreviations or WNBA. We'll look into both of them. So first, as we said, writer Farley had to open it with the mood. W has a second parameter. Here's an example. This is the name of our file. And when we want to write to our list the file handler. And so when we want to write for file handler, whatever was inside of that file has been deleted. So now all of the content that was already in the file is replaced by this line. If this file does not exist. So when you open it and the, the upper function says that, oops, there's no function, there is no file. I'm not finding a file of that name. And so it creates for us and you file because it's of mod w, so on and so for w and there is no such file. A new file is created with the same name. And the content. Materials inside of the right function will be inside of that textbook and to append to a file. So I know, let's say I don't want to delete the already existing content and my file. And so to do so, we have to open up to the mode a. And second parameter you can see here. So let's see, my file contains some information about something and I want to add to that information. So I add the afterlife write statement, write whatever I want inside of my own right function. And that will be added at the end of my text file. Maybe I want to create a file without, I'm writing something into it right away. I'm going to create it first. And then throughout my code, maybe I decided to write into it. And so to do so I can, whenever I want to open a file created, sorry, right then the name of my new file. And finally Parameter. And I add the x parameter beside that. And so here is my file handler. And when I read the statement and you file in, my project has been created with the name new file, a text. And let's say later on throughout my code, I decide to write inside of, inside of that file. I reopen it and read the same name of the file. And however, now second parameter is not x w. And I want to later on add to it, I can obtained. So the result of the first statement is that a new empty file is created. And then if I want to add anything else, I want to add some content to that empty file. I can add a W parameter while opening a file handler. So on. Let's go to Visual Studio Code and try to have an example of that. So let's go ahead and try to create our own file and write into it. So let's go ahead and create a new foreign Scott. You deploy. First of all, create a file handler which is going to oppor For us new file. Let's call it output text. I want to write to that file. And now I want to write the right function. And I want to add a string. So what do I want to write it, the that part. So it's not necessary to be a string. I could a variable that holds a string. So let's say I want to add what was found in our demo, that textbook that we seen previously. So how could we add the contents of that data text into our new file, output that text file. So to do so first we have to read what we have in that demo to texts. So we're going to create another file handler on handler C2. And we're going to open that demo dot txt file. However, here we want to read it Myanmar to write with. And now let's create a variable, let's call it S. And S is going to be then string that holds that content. So it's file handler T2 dot. And as we said previously read returns for us a string with all characters of our demoed text file. So here as holes on the strings. So instead of writing all of the contents of a text here, we just add s. So here we are going to write S into our file handler. And let's go ahead and then close. Both. Close both. To do. So, let's go ahead and run our program. So now as you can see, we have this newly created file. It's called output the texts. And inside of it is the same data that is inside the downloaded text. So you can think of it as in copying the contents of a file to another file. So here we are copying the contents of the text to the contents of output, the text. And you can see that we created a new file. And just Try to append to that new file. So if you want to append, we cannot attend after we close it. So everything we want to work on is before the close. Such that I want to, I want to add more information to my output, that text file. So to do so, I want to reopen that file however, I want to add an append. So to do so, I want to sorry, right after I close it, I cannot open it twice if I did not close it. So now let's call that file handler. I want to open that output, the text file. However, now I want to append to it. I don't want to overwrite my information because if I added w, alright, all their permission arterial be erased and replaced by whatever I write inside of my wrote function. So in that way, adding an append will make sure that my content is not deleted. So I want to add a statement which says that this file as a copy of demo dot text. So let's go ahead and close it. Not just a disclaimer on it's not necessary to close our files only in that project cause you're not using resources. So that's okay. Just in this simple project, however, when you start using resources, it's better if you do not forget. So let's go ahead. And now it's going to output and see. Now we have added to our file that this file is a couple, is a copy of them, older texts. So you can see that we added a new line to our file. And now lastly, let's try just to create a file to show you that you can create an empty file. And to do so, we are open. I, let's say I want to call it last dot txt X for creating a new file and run our program. And you can see that we have lasted NT textbook that almost everything you need to know about files and how to use them. And we'll see in the upcoming lectures, more data structures aside from strings and files that are really beneficial for us. 15. Lists: And now we reach our third data structures, less and less are ordered collections of data that allow us to access the sets, sets of data. And so, like a string is a sequence of values. In a string, the values are characters, as we've seen. However analysts, they can be any type. And the values in our list are called elements, or sometimes you can call them items. And by any type, I mean it can be an integer or a floating number, or a string. And a list, as I said, is a collection which is and changeable. Unlike strings, strings, you cannot change strings. And so to create a new list, we enclose the elements in square brackets as you can see here. So here are two examples where the first list, we have integers, lists of integers 10203040, which are enclosed by a bracket and are separated by a comma. So the comma indicates for us that this is the first item, the second item, the third and so on. And this is another example where we have a list called fruits. And inside of it we have a list of strings. So this is the first string, the second string and string. And a list can contain a string, LSAT, or a float, or an integer, or another list. So here is a list that contains a string and an integer, sorry, floating-point, because you have a decimal and an integer and a list. So it doesn't need to have a list of integers or only offloading numbers for only of strings. No, you can have a list that contains all of these and including another list. So a list within another list is nested, as you can see here. And a list that contains no element obviously is called an empty list. And here's an example. This is the example we've seen, the fruits. And this is the numbers which has 17153 as list numbers. And this is an empty list. And if you try to print this list, you can see that this is our first list. This is our second list, and this is the empty list we have. The, this is how we, they are represented when you print. So let's go ahead to Visual Studio Code and try to create our own list. So now let's go ahead and create a new file. Let's call it list.pop. And let's try to create our formless. So this is clearly an integer floating point. Strain, a list that has also been editor and another string. So this is the list that we created. Now, let's go ahead and try to print out s. So as you can see, as we print all lists, we are returned with them with that list. And this is how it's represented. And you can see that we have a nested list inside it. And you can also have an asset lists inside of that list. So another list inside the list. Here. Let's say I want to have 2-3 inside of it. And that's also fine. You have you can have as many as you want inside each other, and that would be no problem. Now let's go ahead and see how these lists, how we can manipulate these lists, how we can use them, and so on. 16. Lists: Accessing Items: Now let's go ahead and see how we can access item unless it's actually pretty simple because list indices work the same way as string indices that we've talked about. And so any integer expression can be used as an index. And if you try to read or write an element that does not exist, new, get an index error. The same way when you try to access a character in a string that is not found, you get and out of range index error. And if our index has a negative value, that's not a problem. It just counts backwards from the end of the list. The same way. When you use negative values for strings, they start counting from the end of the strip. So here's an example. This is proof which has three items, apple, banana, cherry. And if you try to print one, that would not give us apple. Of course not because as we always say, this is our start from 0. So Apple has the index 0, banana one, and Cherry has index two. So when we try to access the index one, we actually mean the second item in our list, another first. And as we said, negative indexing is not a problem. Let's say I want to access the last item in my list and I don't know the size of my list. So I just enter minus1 and that'll just give me the last item. And that's because once one starts from the last element of the list, so minus one, minus two, minus three. So you can see that when we enter fruits and in between brackets minus one, we are return with Cherry, which is the last item. So let's go ahead to Visual Studio Code and try to decide. And so here's the example that we've been working on. And the last, in the previous lecture. And let's say I want to access that list that is inside of our big list. So to do so, we're going to print and L, which is the name of our list here. And I want to access the last item. So minus1, which happens to be a list. So let's go ahead and try it. And you can see that the second print, print the line Suan, gave us Ls, which contains one Hello and another list. So let's try to access the first item. And it gives us the first, sorry, not the first or second. I mean, the index1 item, which gives us 5.25. And now maybe we want to store these items somewhere. And that's totally fine. Totally fine. We can. So maybe I want to that 0.55 and a variable called C S, S equals to L one. And that's, I want to save that list that amongst oneness and L2. So that gives us that. And now let's go ahead and try to print and print s. And let's just remove this riddle. Wonder. So let's go ahead. And you can see that S now has this, and sorry, L2 has this list and as has this floating number which is 5.55. And if you ever want to use that flowing number, we have to convert it to a float in order just to use it. So float. And that's me. And hearing you. So that's how we access items and lists. 17. Lists are Mutable: One of the main advantages of lists in Python is that they are mutable, unlike strings, which we saw previously, that they are immutable. So strings are immutable. However, lists allow you to change the order of items in a list or to assign an item in a list. For example, here we have this list that contains true numbers. And I'd like to change the second number, 25 and enlists. I can actually do that. I can call the number that is index one has 13 and you assign it to five. And as you can see here, when we print that list, you can see that the second number has changed from a 11325. And you can see here that there exists a relationship between indices and elements. And this relationship is called mapping. We are mapping the index to one of the elements. So each index has a map to one of the elements. So 0 is not 2131 is mapped to five. And let's go ahead to Visual C code. I just try to change some items or try to reorder some items in a list. So here's an example. Here's the example that we were working on previously. And let's just add to it some changes. So let's say I want my last item to be changed to something else. I don't want it to be this anymore and I wanted to know the split. And so to do so, let's just accept that last item which is n minus one and assign it. Let's say, to a list which says 12345. So now, if we go ahead and print our list, let's go ahead and remove this. And now if we are honest, before and after. So here is the before and here's the after. And let's go ahead and see if something changes. And you can see that the last item which was one Hello and sublist, is now replaced by this list which contains the numbers one to five. And so you can see that that does not causes an error. Unlike strings. If you try to do that with strings, that Python will give you an error saying that you cannot change the structure of the string because strings, as I said, are immutable. And we'll see throughout the lectures something that pool alas to reorder lists. And you see that in the upcoming lectures. 18. Lists: Slicing: And similar to strings, we can slice lists in Python. And so you can specify a range of indexes between brackets and specify the start index and the ending index. And this will return a new list with a specified items that will be returned. And so here's an example where we have this list and we specify that we want to slice it from two to five. This is the starting index, the ending index. And similar to strings, ending index is not included. So here we're actually saying that you want to slice our list. So 01234, so Cherry orange Achilles should, must be returned, and this is our output. And the cherry orange and QBR returned. Here, we're slicing prostrating file. So it's actually four and orange and q0 or returns. And also Similarly, we can keep the left side of the column empty or the right. Doesn't matter. Here, we're saying that we want to start slicing the list, starting from two til the end of the list. So two is 012. So from cherry till the end of the list. And you can see that here we have cherry to the end of the list, mango. And if you don't omit both, the slice is a copy of the whole list. So let's say you don't add anything to the right and you don't add sorry. You don't add anything to the left and you don't add anything and the right, you will be, you will return the same, sorry, the same list will be returned for you. A slice operator on the left of an assignment can update multiple elements. So the C, you don't want to update one element and you want to update multiple elements. You can do that by adding, specifying between brackets that let say this is our list. So from one to three, I want from o to three, that means we have two items, so we have to modify to Ireland. So I want 0123 is our encoder. So I want to replace b and c with these two new items, X at Y. And here if we print our list, you can see that they are indeed place. So a, same. However, B and C are replaced by x and y, and the rest are the same. So if you try to add three parameters, that will give us, so they have to be the same parameter. So here if we have two items inside of our brackets, then you should provide two items. So here is the example we've been working on. So let's try to print on a slice of our own list. Let's slice it. And so let's say I want to print everything that's formed between 13, that means on and to actually saw this too. And so let's try and print that. And you can see that PR received only with these elements. And because lists are mutable, we can actually slice the list itself. So l could be from one to four. So now if we print our list and see if you can see that now our list is modified because now our list does not contain one. It contains only these three elements. And that is actually approved doubtless are indeed mural, which means that the list itself can change. And let's say I want to change something in that list, multiple things, multiple elements in that list. So let's say I want to change 1.555 and hay. And so to do so from 0 to three, sorry, till 4013, because I said only these three items. So from 0 to three, I'm going to sign your variables, which are, let's say 123. Now let's go ahead and print our list. And now you can see that our new list is here. And these three items are replaced by 123, which also is another proof that our lists are mutable and we can change them the way we live. So this is an example on how we can slice this in Python. 19. Traversing a List: Now we're going to dive into traversing lists in Python. And before that, let's just take a look on on the and operator, which we're going to see while traversing lists. And the and operator returns for us true or false. If let's say this element is in that list, it will return for that, for us, true. And indeed, we can see that this element is string element is inside of our lives. And if not, it will return for us both. And we've seen that many times throughout the course, throughout that Python for beginners. So this is just an overview, or let's say a review. And now let's go ahead to traversing lists. And so the most common way to trappers the elements of a list is with a for loop, which is so easy. So for every, let's say element or we can name it anything. It doesn't have to be sister and sister as snow to be any need for every element which happens to be called SR. And this list, which happens to be called Sister. We're going to print this element. And so you can see that we printed each element in that list. And if we try to traverse over an empty list, the body is never executed. So let's say we have a list called MT, which is actually empty. It doesn't have any elements inside it. And we tried to traverse through it. This body is executed, this print statement will never be executed. And although a list can contain another lists and sided with, the nested lists still counts as a single element. So why we are traversing our list? And let's say we're printing every element in our list. This would be considered as an element of this list. On its own is an element. So it would print that list and it wouldn't traverse to the list the elements of that sublist. So nested lists are kept count as single elements. And so let's go ahead and try to traverse our lists in our Visual Studio code project. So this is our list that we've been working on. So let's now try to print each element at a time. So you don't want to print the whole list. We want to pass every element and print that element. And so as we said, we create a for loop for every element. And say L, because that's the name of our list. We're going to print this. So let's go ahead and try to see what's going to happen. And so you can see here remodel, we've modified our list 123, so these are overwritten, are replaced by 1-2-3. So here's our first element, second element, third element, and then our fourth element, which you've also overwritten, we've replaced, this was replaced by 12345. And so you can see that we have a list that is 12345. So it didn't return for us the elements inside of a list. It returns the list itself because as we said, the list itself is considered as an element. And we can see that if we have an empty list and say, this is an empty list, and this is how I would create an empty list. And let's say we're trying to reverse it. So for every i in going to print hello. And you'll see that just in order to see that it skips. Nothing happened that let's bring something here. So let's go ahead and print done just in order to see that our code does not. Go ahead and run our program. And you can see that we print the hello statement was never printed. Hello String Theory. So you can see that will predict the list four, we modify that, then printed the elements of the list after we've modified it, then you can see that this print statement was never executed and then Don was executed. So that is an example that shows us that empty lists. The body, is not executed when you try to traverse an empty list. So that's how we traverse lists in Python. 20. Lists: Adding, Removing, & Updating Items: Now let's go ahead and C hat and see how we can add, remove, and update items and lists. And we'll start by seeing how we can add items and lists and islands. We actually have two approaches. The first approach is to add an item to the end of the list using the append method. And so here's an example where we have a list having three items, apple, banana, cherry. And let's say I want to add another item to the end of the list. And so we use the append method and the same would add orange. And if we print our list again, we can see that now our list contains four items and actually with the new item at the end of our list. The second approach is to use the insert method to add an item at a specified index. So here we have the same list which contains a sensory items, apple, banana story. However, now I want to add my item, let's say here instead of banana. And I want to push to the right. So I want to push all the items that are to the right of this position to the right. And so banana has an index of one. So 01. And so I will, I will insert menu item on index one. So here is my index number one. And this is Orange. And now you can see that now Orange is at index one and banana is moved to, is pushed to two, and Sherry is pushed to 320123. So that's these are the two approaches to add items, either by the insert method or the append method. Now, to remove items, we actually have several approaches. And so let's go ahead and explore each one of these approaches. So first we have the remove method that removes the specified item. So you specify the item you want to remove. So here is the list that we have. Apple, banana, cherry are our items. And say I want to remove banner. And so to do so, I have to know that I have an item that's called banana. I'd have to write it inside of the parameters of remote. And this method will remove this item. And now you can see that if we print our list again, we don't have only have Apple. And so this is one approach. Another approach is to use the pop method which removes the specified index. So here we are specifying the index. If you don't specify an index, you just by default are removing the last index. So here we didn't specify the index. So I want to pop the last element by default. And so if we reprint our list, you can see that now chosen Arabic is not, is removed actually. So we only have two items. Let's say I want to pop 0. If i enter 0, then Apple would be removed, and so on. So this is our second approach. Let's go ahead and see that del keyword. So the del keyword removes the specified index two. However, here, it only type in del and then space fruits the name of the list and then just as phi index that you want to remove. So 012 and so on. So you can see here that when we deleted used Adele keyword and then used crude 0, Apple has been removed. And if you actually type in keywords, that keyword del and then fruits, you're actually dilute deleting all the items in that list. So here is our list. And then we've typed del fruits. And so now our list is totally empty. All the items in our list has been removed. And we also have the clear method. So the clear method empties the list. It's similar to del fruit. So they'll then the name of cliffs. However here you're just you start with the name of the list and then dot clear. You don't use the del keyword. And if you print fruits, you can see that the list is empty. And you can remove more than one element using the adult keyword with a slice index. So here is the del keyword. However, I don't want to delete only one element that I want to release more than one element. So say, I want to delete B, C, and D. And so B starts from index one and then 23. Let's say I want to remove also for support. And we use that slice operator. And as we said, the ending, the second index here, then the index is not included. So we're actually building from one to four. And so if we delete, use the del queued and then use the slice operator from 95 and reprint our list. We are returned by only a and all the elements that are in between me and a, Including B and E or deleted. And so these are the all the remove methods that we can use. So we mentioned that Dell, I'm keyword. We've mentioned the clear method, mentioned the pop method and movement. So 12344 methods to four different methods to remove items and lists. And now we've come marriage on the part where we update items in our list and update items in our list. We can We did the indexes to update items in our list. So here we have, let's say, a list called numbers. And we want to update each item in that list. So here's our list numbers. It contains 12345. And to update, let's say each element in that list. We can use the range function, which we've seen in Python for Beginners course if you've seen it. And we include in that range there length of our less. So here we have five items, so that is five. So the range is five. Power grid range starts from index 0, so 01234, and that's actually 55 items. So 01234 and so forth for every x in that range. And we're going to access an item in our list. So as we said in the indices and lists mapped to the items. So 0 maps to 11, maps to two, and so on. And so let's say I want to multiply each element by two. And then if we want to print it, you see the results. And so here we are traversing our list and multiplying each item in our list by two. And if you can see that when a reprint our lists, we are returned by the, by that same list. However, the items have been multiplied by two each. So this is an example on how we can update items in our list. So here's our previous example where we were working on this list. So let's go ahead and try to remove and add an update items in our list. So first of all, let's go ahead and try to append to our list. So on. So this is our final list here, 12345. I'm not just renewed. And that's actually comment them out. They remind actually need them later on. And you've got to actually run our code. And let's just run it. So here we have. Now, Let's go ahead and try to append something to our list. So my list is called L, so L dot append. And as we said, append adds something to the end of our list. So let's say I want to append. Well, so here we add something to the end of our list. So let's just go ahead and print out list. Let's go ahead and comment that out for now. You can see now that this is before and after. So after we appended, we have this new item will come. Let's say I want to also insert something to my list. So insert similar to append, however, not at the end of our list. We want to add it somewhere in the middle maybe. So this are listless, i want to add it here. So 012 on the next tool. So too, I want to add on 6.66. And it can be a floating number, doesn't have to be a string. So let's go ahead. And now you can see now that our list actually obtains this 6.66 which is at T2 and allows new item which is walking. So that's for adding items. Now let's go ahead and try to remove items. So let's say I want to remove 5.551. So let's go ahead and remove. So it has to be aldosterone rule on which lists we're working on. So L dot remove 5.5. Let's go ahead and see if it actually removes it. And you can see that it does remove 5.25. Let's try to add a string. So let's remove string with by 0.5. And you can see that we are returned with a value error because this is the element inside of our list is not a string. This is at floating numbers. So if you tried to, and as a string, we're going to be returned with an error that this x, this x is not in the list. So be careful wider removing stuff. So now go ahead and try to delete 6.66. So by using, let's say the delete words. So delete L and 6.66 is, let's say CA1. Let's go ahead and comment that out. So let's go ahead and comment that out just for you to say. So this is our list. Now, let's go ahead and try and delete 5.25. And you can see that 5.55 is actually deleted. So here we had 5.5516.66. Now after deleted, 5.55 actually been returned with a 6.66 and so on. Okay. Now, let's go ahead and try to. Pop something out of our list. So let's go ahead and move this just to make it clearer. Print. And let's go ahead and try to pop something out of our list. So here's our list is go ahead and try to add up. And first of all, let's just try to use the default. So popping the last element. And you can see that the will come has been popped. Now let's say I want to pop and this list, so 0123 at index three. Now let's go ahead. And you can see that the list has been gone and give added will come at the end of the list. Now let's go ahead and try to clear this. And you can see that when we reprint this list to have an empty brackets, nothing between them means empty list. And finally, let's go ahead and just tried to delete several items. So I want to leave it to four. And you can see that 0123 happen removed because four is not included. And then we added all come at the end of the list. So this is our new list. And finally, an order to give you an idea on how to update lists, list items. Let's try to create a for loop. Every X, let's say. And the range that changes between the length of my list. Because I want to traverse all the edges of my lists. So the range is the length of the list. So I want to update that list item. I want to update all the list items. And so let's say I want all my list items to start with a C E star. So we append that to our list and go ahead and print our list after that update. So when return with a type error that cannot concatenate STR, int, STR, sorry, can only. And so that's because some of our list items are actually floating numbers and integers. So in order to avoid any problems, we're going to add a function. So now we can see that all of our items in our list items are, sorry, start with a star. So this is an example on how we can update this. And this is actually an example that shows us on how we could update, delete, and remove, sorry, and add items to our list. 21. List Operations: Now we're going to see the list operations and so on list, we actually have the plus operator which concatenates smith. So here we have listed a and less B. And we'd like to join these two lists into one list. And to do so would create a third variable C and which holds a plus mi. And so you can see that we have all the elements of a and a followed by all the elements of me. There's also another method to do that. We can use the extend method. So using the extend method and you don't have to create a third variable. We only have these two lists. And we'd like to add at the end of a the elements of B. And so we're extending to a, all the elements of V. And so in that way, we don't have to create a new list. So we're just adding the lists, sorry, the items of the mirror to that of list. So when we reprint a, you can see that we have a, B, and C which are already found in this day. And at the end, we've extended the list 2456, which are that list items off beat. And also have the multiplication operator. And that would repeat a list at a given number of times. So similar to strings, we've seen that if we multiply a string by a number, that string will be repeated. 22. Sorting Lists: Turns out that in Python, you can also sort lists. And in order to sort a list, there's a sort function that arranges the elements of the list from low to high. So let's say we have a list of numbers. It's easy to arrange elements from low to high, just arranged from one to two, from lowest to highest. However, what if we have characters? So one thing, you should know, characters that in Python characters are encoded. They haven't coded numbers. And lowercase characters are greater, have coding numbers that are greater than that of the uppercase. So if you're trying to compare small a to capital a, small a is greater because it's encoded number is greater than that of capitally. And as you move from a to B to C, the encoded number increases by one. So let's say a is one, then b is two, and so on. And so when you try to sort of say this list of characters that are encoded, you start from low to high. And so here we have a which has the lowest among these, then b, then c, and then d. Because as I said, encode number increases in alphabetical order. Let's say we have a capital a. Capital a is first because characters are smaller than lowercase characters. And so if we go and do that to our previous example. So here's the example when working on. So let's say we have a list that has scrambled numbers, so not Increasingly. So 89 minus 17. And let's say I want to sort lists, lists. Let's just comment this out. So as I said, d dot sort method and the low head on our list. So if we run our code, you can see that it's sorted from smaller to higher. So mine swan 14789. So that's four integers. Let's try to have it with characters, so a, C. And let's just put here F, and let's just put here, here. And here is the a, just in order for you to see what's going on. And let's see here we have another small a. So a and C Here we have an f and c and d. And if we go ahead and sort this list, you can see that first we are given the uppercase characters because as we said, uppercase characters are smaller than lowercase. And so a is the smallest and then C and an F because on the encoded number increases as we go from a to Zed and as a matter of lower or upper. And then we are given after the uppercase R finished, we have the small case lowercase. So we have a and then d and then ten set. So I hope this makes it clear to you as to how characters are encoded in Python. And so that's one way we could sort lists in Python. 23. Lists & Functions: Now moving on to this part of the course, we're going to see how we can use some functions on lists. So we have the Lynne Maxmin and some functions that we can use our list. So obviously done then will give us the number of items in that list, which is an aeration for length. So length of that list, and you can see that we have six items. So when you print the length of this class is going to return for us six. We have max, which obviously when it's going to return for us, then the maximum number on that list, which happens to be 74. And the men will give us the minimum number, which is three. Here. We have some function which will bring good return for us, the sum of all the elements in our list and a 104. And let's say we want to calculate the average. We just divide the sum by the length of that list. Seeing a typical code, let's say we want the user to enter the same multiple numbers and we'd like to, whenever he enters done, we'd like to calculate the average of the numbers that he entered. And if we didn't use those functions, Glenn, maximum sum, our code would be something like that. So while this is true, we're going to keep on getting input from the user until he understand. And then we just break the loop. And these numbers are going to be saved in a floating number undervalue variable. And each time or unbelief first initialize the count and the total. So the total as in the sum and count as in the length. So at the end of our loop, we are going to have the total, which is equivalent to the sum of the elements then whatever the user enters and sludge divided by the count, and the count is equivalent to the length, and so this is the average. However, if we do use these functions, our code will be much, much simpler. So here, as that goal, we have a list, an empty list, and we're going to keep on getting input from the user until he enters done, and the loop would break. And each time we're going to save that value that the user entered and uploading number. And we're going to append it to that list.append will add this list to the end of our list at this value. So, so each time the user enters a number, we're going to add it to that list. And then we're going to calculate the average. Sum over the length of that list, and we just print the average. So let's go ahead and try to implement that code. So let's go ahead and create a file. Let's call it on average dot py. And so first I'm going to create an empty list. And that's one way of creating an empty list. And then we're going to have a while loop. While true. While true, we're going to have an input from the user, let's say, which is an integer. Let's say a number doesn't have to be an a. So please enter a number the user entered. And then we go and break our loop. Otherwise, we're going to save that and put in a value, in a floating value. And then we're going to append this value to our lives. Nonetheless. So list.append value. And after that, we're going to exit the loop. So whenever we're done with just exit the loop and we have our list ready. So if we want to calculate the average, it's pretty simple. Just on logistic paid the full print. So as we said, the sum of our list. So some of you over the length of our, nonetheless. And now let's go ahead and print average. And that's it. So let's try our code. If it works. So please enter a number. Let's say I enter one, then two, then three. And let's see this. The sum of these is six. So let's add four to that. And now done. So the average is 2.5. So ten over five is actually sorry. Six, I'm sorry that yes. So ten over four is 2.5. And so we can see that our code works. So that's one way we could make use of these functions to make our code simpler and more livable for with the programmers. 24. Strings & Lists: So now we're going to combine and talk about two of our data structures, lists and strings. So a string, as we said, is a sequence of characters and a list is somehow similar as it is a sequence of values. But a list of characters is not the same as a string. So to convert from a string to a list of characters, you can use the list function. So here is our string, which is a sequence of characters. However, this sequence of characters first from a sequence of values. So in order to visualize that sequence, we're going to put that string inside of our list. So we're going to turn our string to Alice. And then if you print that to, you know, to see that every character in our string is now and a value in our list. So the sequence of characters that are, that are in our string, and now a sequence of values. So each character is now a value in our list. And if you want to break string into two words, we can do that using the split method. So let's say we have a string and we'd like to split that string into characters. If we use that split method, this method will return for us a list that contains each of these words as a value. So it reads on these words in that string as values, and that whitespace is what separates each value from the other. So you can see that, for example, why is one value? Is it the second value, and so on. And you can also specify Eddie, sorry, a delimiter. So if we don't add another meter by default, what is separating our values is the space. And we can, as we've seen, can access values in this list. So say I want to access the third item on type in T2, so 012. So sometimes it's beneficial for us to split our sentences into words. And stories were journalists, and you'll see that they're wrong. And again, as I said, we can call the split with an optional argument called the delimiter that specified which characters to use as a word boundaries. So here, for example, we don't have spaces in our string and replied to separate the values by that dash. And so here the delimiter is a dash. When we split that string, we're going to split according to having that dash as our delimiter. So when we print our list, that is what will return with these three values that are separated by a dash. And as opposed to the split function, we have the join function, which takes a list of strings and concatenates the elements. So join is a string method and you have to invoke it on the delimiter and pastoralists as parameter. So here is our list, and we'd like to concatenate the items on our list into a string. And in order to do that, we first specify a delimiter on, let's say I want to have the delimiter to be a space between each of my elements. And so that delimiter dot join that list is going to give us that output. So we're basically saying I want to join the elements of my list with that delimiters. So I'm between each element, we're going to have that delimiter, which is the space. So here's a code that we've worked on previously, which was called me lit by, where we had a bunch of data and we wanted to work on that data and wanted to extract some information. And so we split our mail so each performance was printed with a star. So we use the dot split function to separate each made from the other. And then inside the for loop we did some stuff to that myth. So let's say that now after I've finished, I'd like to join that list. Here. I created a list by splitting the data according to the store. And so let's say at the end of the list, I want to rejoin that list to a string. So to do so, as we said first, we need to have a delimiter. So let's say our delimiter is the star. It has to be because as we've seen, each male is separated from the other by a star. And to use the join function, we use that delimiter and then don't join. And you'd like to join our mailing list that has mail. We would like to join us. And let's go ahead and save that last variable. And I'm going to print. And in order to visualize our results. And let's go ahead and just comment that out in order not to confuse ourselves. So let's print meal here just to see the difference. And let's just have one extra line between them. Space actually, just to see which one is which. Sorry about that. It cannot be added if it's a list. So we'll just add a print, another print statement. So now let's go ahead and run. And so you can see that this is our list males right here, which contains the meals, each one in a value. And now here is our rejoined string. Using the.join method. You can see that the sludge and means go hop on to a new line. And the star is visual to show us that this is the beginning of a new male. So this is one way we can use string methods with lists. 25. Objects & Values: When working with lists, we always need to keep in mind objects and values. And so what I mean by that, for example, in, let's first on C strings. So here we have two strings which hold the same value. However, we don't know yet whether they point to the same string or to two different strings. In one case, a and B might referred to two different string objects, as you can see here, that have the same value. And in the second case, they refer to the same object. So here we have two different objects with the same value. However, here we have the same object with one value. And you already know that objects can be identified with their unique identifier. We can also test whether two names refer to the same object using the is operator. So two C, f, a, and b, are pointing to the same object. We may use the is operator, and it will return true if the two references are to the same object. Otherwise. Sorry, in other words, these references would be the same. And the answer, as you can see here, is true. And this tells us that both a and b refer to the same object. And this is the second of the two reference diagrams that, sorry tear, that explains this relationship. Since strings are immutable, bison can't optimize resources by making two names that refer to the same string, literal value refer to the same object. However, this is not the case with lists. Consider the following example. Here we have a much has these three items, 123. And he would have been, which happens to have the same elements as a. So Ls if two, unless they have the same, let's say items, we say that these two lists are equivalent. So if you see a cortical B, this will return for us true. And that's because they have the same elements. However, they are not identical because they are not pointing to the same object. And that's why if we see FreeType and a is b, that will return for us false. So if two objects are identical, they are also equivalent. However, if they are equivalent, that does not mean that they are necessarily identical. 26. Aliasing: We've talked in the previous lecture about objects and values and how we can have multiple values that refer to the same object. And that brings us to aliasing. So aliasing is when two values refer to the same object. So if a, let's say this list a refers to an object, which is this list 123. And you assign B equals to a. And then these both variables are now aliases because they both refer to the same object. And so if we type in B is going to be returned by a true, Because both and these objects, sorry, these two variables are references to the same object. And so if the earliest object is mutable and so is the case and lists. And we've seen that lists are mutable. Changes made with one erroneous affect the other. And so here is our list, and this is b, which is equals to a certain. These lists are aliases. And so if you try to change the first item in b, so b now also 1-2-3. And we tried to change b two, sorry, one to 17. And now we print a. So now we're printing a, although we didn't modify anything in a, we're going to see that the first variable is modified. And the reason is because when, wherever we modify and B is going to be modified in a. Because as we said, these two variables are aliases. They refer to the same objects. So whenever this object is changed, it's going to change for both variables. However, for immutable objects like strengths, aliasing is not much of a problem. So, as we see in strings, are immutable objects. So let's say two variables point to the same object. Because this object is immutable. So we're not going to see on a case where two strings are changed because the object itself is not going to change. So it almost makes, Never, sorry, it almost never makes a difference whether a and b refer to the same string or not, because aliasing is not, as we said, Not, not much of a problem. So that's all you need to know about aliasing. 27. Dictionaries: Now we're going to explore another data structure in Python. And this data structure is called dictionary. And so a dictionary in Python is an unordered collection of data values used to store data values like a map, which unlike other data types that hold only single value as an element, dictionary holds key value pair, k value is provided and machinery to make it more optimized. So dictionaries are written with a curly brackets and they have, as I said, keys and values. The kids are the ones that are right for the column. And the values are and the ones after the column. And each key value pair is separated from another with a comma. And you can add as many key value pairs as warm, as long as they are inside of these curly brackets. And they can't be, the values can be strings, can be integers, can be floating numbers, they can be anything. And if you try to print this dictionary, you'll get the key value pairs. And they are separated by commas. So this is the first key and its value. Second key and its value, Turkey and its value. So let's go ahead and try to create our own dictionary. So this abandoned, far equated. We're going to create our own dictionary. So let's say I have a student and I want to add some information about that student. So as I added these curly braces, we've created our dictionary. Now, let's go ahead and let's say I want to add his first name. First name. Let's say his first name is Jack. And now I want to add lastName. Let's say last name is Rochelle. And as you suddenly need separate our key value pairs with a comma. Say I want to know his age and see his age and his height. So on. So you can see here that we've created our dictionary that has four key value pairs. Anomalies, go ahead and try to print our dictionary. Well, our code. You can see that we have a dictionary here with that has a key value, key value, key value, key value, pairs of key values in our dictionary. So that's how we create a dictionary in Python. There's actually another method you can ask, sorry, do you can create an empty dictionary by just stating the students, let's say. So that's another way you could create an empty dictionary. And we'll see later how we can add this empty dictionary. 28. Dictionaries: Accessing Items: Now let's go ahead and see how we can access items and dictionaries. And you can access item. And I am a dictionary by referring to its key name inside square brackets. So here we have a dictionary that has three key value pairs. And I'd like to access, let's say, the color of my genes. And to do so, I first begin by writing the name of my dictionary, followed by square brackets. And inside of the square brackets I'm going to write the name of my key. Here. The key is called color. If I tried to print that color, you can see that now, color valuable holds the value of my key. So which is black. So the color, I'm sorry. So whenever you try to write the statement is going to return for you the value and store it in whatever variable euro. Let's say you try to access an item that is not found in your dictionary. If it's not found that could blow up your code and Python would give you an error. So to avoid that, we can use the cat method. And so the gas takes a key and a default value if the key appears in the dictionary. So if it, if we find our key in a dictionary, in the dictionary, it's going to return the corresponding value. However, it does not find that key. It's going to return the default value that we specified. So let's take an example. Here we have this dictionary and we have these key-value pairs. And I tried to access, let's say that key, Taylor, I give it a default value 0, meaning that if I don't find that key in my dictionary, I'm going to return 0. So here if we try to print that statement, we are returned by 42, which is the value of that key. So that means that we found our key and we access its value. However, here, we are trying to access a key. And whenever we write that key and the default value in our method, our method is going to go to that dictionary and search for that key. And it's going to see that we do not have this key in our dictionary. And so it's going to return for us the default value, which is 0. So now if you go ahead and head to our example we've been working on. So here's our example. And first let's try accessing a key that's not found. So Julian. So let's try, let's say accessing any k that's not found here. And let's go out and try to print. And now you can see that we have a traceback error, a key error saying that we do not have this key in our dictionary. So you can see that our code bloat up, blew up and we didn't continue printing the another, the other statement. So to avoid that, we're going to use the get method. And so I'm going to say, get me that key which is G. And if you do not find it, return for me this default value 0. So now we go ahead and print. You can see that my, nonetheless, it returned from E 0 and the dictionary is returned and throws mentioning that if we ever have two k's that have the same name. So say I have another key, the same name. It's going to, Python's going to see this as one key and not two. And it's going to print them on that is the most up-to-date from the one that is below and not one that as above. So you can see here that printed 2200 AD a1. So on. This is one way we can access items in our list without having them to blow up. And that default value is not necessarily an integer, can be strings. So here you can have key not found. So it can be a string that doesn't have to be an integer. And print, you can see that it printed, borrows key not found. And so get methods or the chemical is really beneficial when we're using and dealing with dictionaries. 29. Dictionaries: Changing Values, and Adding & Removing Items: Now let's go ahead and see how we can change values in dictionaries and how to add and remove items. And we start by seeing how we can change values. And so you can change the value of a specific item by referring to its Keeney. And so here's the example that we've seen previously. Let's say I want to change the color of my genes. Two from black at say two blue. And first. To do so we need to access that key. And you've seen previously that access and key. We just added ml square brackets and the name of the key inside of it. And so previously we just type this statement and then stored in a variable, Harvard here we're going to access this Anki and change its value. So here if we go ahead and represent our dictionary, you can see that the value of color has been changed from black to blue. And now to add items to our list, we can add an item by adding square brackets, the name of our new key name inside of it. So here is our dictionary. Let's say I'd like to add to this dictionary the price of my genes. And to do so, we start by writing the name of our dictionary and add a square brackets. And inside of it I'm going to write the name of my new key, and I'll going, I'm going to assign for it. So 55 is the value of my key, which is price. And now if I go ahead and print my dictionary, we're going to see that a new key value pair is added to my dictionary, which is right here. And lastly, to remove items from our dictionaries, we actually have several methods to do that. And the most common one is the pop method that removes the item with the specified key name. So let's say I want to remove, let's say the size, the key with any size and my dictionary. So all I have to do is just say, start by seeding the name of my dictionary and then followed by the dot and the method which is pumped. And inside of that and pop method, I'm going to write the key that I want to remove. And so we wrote size. And now if we go ahead and print out the dictionary, we can see now that we have only 32 key value pairs as the third one has been removed. And there are several other methods we can use. And we've seen, actually, we've seen some of them previously while talking about strings and lists. And some of these include the pop item, which is similar to the pop. However, adjust removes the last inserted item. And we have the del keyword would just where you just write the del and then beside of a key that you want to remove. So here is the name of the dictionary and inside the square brackets the name of the key. So whenever you type it removes the item with a specified key size. And lastly, we have the clear method which just empties our dictionary. So you just call that dictionary dot Clear and adjust clears all your dictionary, it becomes an empty dictionary. And now all of these methods, removing, adding, changing values, could help us build meaningful codes. And one of these meaningful codes could be using a dictionary as a set of counters. So we could create a dictionary to help us count how many times each character and a string has been repealed. So we could create a dictionary with characters as keys, counters as the corresponding values. And so here's an example. Here we have a string that has characters inside of it I want, and I want to see how many times each of these characters has been repeated. And so first of all, we begin by creating our dictionary. This is an empty dictionary. And now we're going to look for between each of my characters in my string. And I'm going to see if this character is not in my dictionary. I'm going to add a new key to my dictionary, having that character. And I'm going to set it to one as it has been. Only ben found one. I've seen it only once in my string. However, if it is already in my dictionary, I'm just going to get that value that is inside of that key. So let's say it's repeated twice. I'm going to add plus one to it because I've just encountered it one more time. So the frequency increases to three. So now I have seen it three times. And so the loop goes on and on until we finish our string, and this is our output. So you can see that the character has been repeated twice, x has been repeated once, o ones, and so on. So this is a dictionary that's, that shows us the frequency of each character in our string. Now we could make this code much simpler by using the get method that we've seen previously. So the get method automatically handles the case where a k is not in a dictionary. So we can reduce four lines down to one and eliminate the if statement. So here is our strength and recreated anti dictionary. And we're just going to replace all the code that we've seen here, but just the statement. So for every character in our string, we're going to create a character, sorry, a key, having that character as a name of the key. And we're going to get that key from our dictionary if it is already there and add to it one. However, if it's not found in our dictionary, so let's say I encountered E for the first time. I'm going to see, say D and brand between brackets on d dot get 0s. So we don't have in our dictionary because this is our first capitalists, that the dictionary is empty. So we are going to be returned by the default value z 0 because we don't have that key. And so 0 plus one is one, meaning that we've only encountered E1. However, if we do encounter E another time, we already have one when we get that E. So we're going to add for it another one. And so the count raises up to two. So let's go ahead and try to implement this code in Visual Studio. Code Studio, sorry. So this is a string that I'd like to count the characters inside of it. I want to see the frequency of each character. And so we begin by creating a new dictionary, an empty dictionary. And now for every character and our sentence, we're going to create a new dictionary key, having its key name as the name of the character. And we are going to first see if we have it. It's already found in our dictionary. And so to do so, we're going to use the get method. And we're going to get that key, just char. And if it's not found, you're going to give it a 0 at the meaning that we've never encountered this character before. So giving, going to give an account of 0. And we're going to add to it one. Each time encountered. We're going to add to it another one. And that's it. So let's go ahead and just print our dictionary after we see the frequencies. So now go ahead and print out, sorry, run our code. We're going to see the characters in our string and how many times each of them is encountered. So here, for example, is the space character. And we can see that we've repeated nine times. The e is repeated five times, and so on. So you can see that dictionaries help us build many useful programs. And one of them is using them as a counter. And this is actually the role of data structures that are structures help us make codes and are really simple to use. 30. Nested Dictionaries: So we've seen lists that we could have nested list, so a list inside another list. And similarly, we could have nested dictionaries as and we could have a dictionary nested inside of a bigger dictionary. So for example, let's say I have this dictionary right here. So it starts with the main dictionary, my family. And then I added curly brackets. And now inside that curly brackets, let's say I want to add several, Let's say individual. So child one, child 23. So this is my first key in that dictionary, my family. And inside of that, I'd like to create another dictionary. So this key by itself has another dictionary which has a name and the year. And similarly, that other key tried two, has the same dictionary name, year, and the third one. So these are three dictionaries that are nested within a bigger dictionary, which is my family. So for example, this is on the code that we've written first. So let's say this is a student homeowner. I want to create a bigger dictionary that covers all the students. So this is the dictionary I want to add inside of it. Several students. Each of those students has firstname, lastname, age, and height. So I'm going to go ahead and put that here. And we need to adaptation to specify that this actually is the key. And this is our first. 31. Looping Through a Dictionary: So we've seen that we could loop through lists and strings, and dictionaries are no different. So we could actually loops through a. However, in dictionaries, the process quite a limiting at the front. And that's because, as we've seen, dictionaries have key value pairs. So we have pairs of Ireland. And so whenever we create a for loop, to loop through that dictionary, that for loop is going to only access the keys of our dictionary. So here, that key name is just a name that could be values and it would still access those keys. And so for every key in that dictionary, we're going to print that key and followed by the value of that key. And you can see the output is right here. And you can notice that the order of that and that was printed out was not the same order of our dictionary. And that's because, as we said, dictionaries are unordered collections. So whenever we print them, they can be printed and unordered nuts. And we can also print our dictionaries according to some conditions. So we could just say I want to print all my items that have a value greater than 20. And it would just print all the pairs that have values greater than 20. Maybe I want to print my dictionary in an ordered way, and there's actually a way to do that. So here's my list that I'd like to print an unordered array. And to do so, we're going to first create a list that has all the keys of my dictionary. And to add all the keys of my dictionary, there's a method called dot keys that will bring us extract for us all the keys of my dictionary. And you can see that is my list. And they are in an unordered manner because dictionaries are unordered collections. And now I go ahead and sort my list, and so it sorts them. We've seen the sort method in the list data structure. And for every key in that list, and for every item in my list, I'm going to print that keys are going to print that key which is here. And I'm going to print decided that the barry That's names not counts. So I'm going to call names. And between the brackets, square brackets, I'm going to call the key. So it goes to the dictionary and just gets the value. So we're just going to use the dictionary to get the values. However, the keys are in that list, sorted them. So that's one way to print my dictionary items in a sorted manner. 32. Tuples: Okay, now we in our last data structure, police corps, and the structure is tuples. So a tuple is a collection of objects which are ordered, and most importantly, immutable. And tuples are sequences just like lists. However, the difference between tuples and lists are that tuples cannot be changed. Unlike lists, because tuples are immutable and lists are mutable. And to create a tuple, you just need to have a comma separated list of values. So here it is a variable. And I add some values which are separated with a comma, no brackets, on no square brackets, no curly brackets. And that would create for us a tuple. And although it's not necessary, however, it's the most common way AMI programmers use it. It's most common to enclose those tuples with a parenthesis to help identify those tuples quickly when we look at our Python code. So if you've seen this alone, it's actually hard to recognize that this is a tuple. However, if you add these parenthesis, you could just easily recognize it throughout a bunch of code. To create a tuple with only one element, we have to include a comma after that elements. So here, for example, we have that variable t1 and we want to have a tuple inside of it, which only carries one element. And to do so, we add a comma. And why is that? So, because if we do not add a comma, Python, we'll see that as a string in a parenthesis that evaluates it to a string. So without that comma, Python, we'll see this as a string. And if we see the type of books, you can see that the type of the first is a tuple over the type of the second is a string. And another way to construct a tuple is through the built-in function tuple. And you've seen that previously while or creating a list or a dictionary. And obviously it will no argument creating an empty tuple. Similarly as creating an empty dictionary or creating an empty list. And if you go ahead and print that empty tuple, that's what you are returned with. And if we actually have an, an argument, it might be a string or a list or tuple. The result of the cold tuple is a tuple with the elements of the sequence. So string is actually a sequence of characters. List is actually a sequence of elements, and so is a tuple. And so if we enter inside of that tuple, bolt and function, let's say a string. That function will Tetris a tuple that has each character of these of the string as an element in our newly formed tuple. So if we go ahead and, let's say we have a word on that. And let's go ahead and put that inside the built-in function. And see what does that return for us? T. Let's go ahead and run. You can see that a tuple is returned for us. We have a space here, so that space also is considered as an element on, let's try having a lists so it equals two. And let's say. And now if we go ahead and enter that and our built-in toggle function, and go ahead and print out and see what that gives us. So you can see it gives us a tuple with the elements of that list as elements of that tuple. So that's how we create tuples in Python. 33. Tuples: Accessing Items: Now we're going to see how we can access items and tuples, and we're going to see couple assignments. So to access items similar to strings, we can just add a bracket and inside of it the index number. So 0 gives us a minus one, gives us the last, which is e. And you can also use the slice operator. So I want to get the elements b and c. And so I start from index one till three. And remember that two is not included, so we're actually taking only 12. And if we try to modify one of the elements, that's going to bring us, give us an error because as we said, tuples are immutable. So try to change one of the elements of that tuple. We are going to get an error object does not support item assignment. However, we could use another approach which is to create a new, replace it by a new tuple. So here is a new tuple t1, where we're going to add as a first element a. And of course don't forget the comma. And then we're going to add the rest of the elements of the 4-tuple. Without that a software, we're going to start from one to the end of the list. As you can see here, T1, we're going to get the tuple that we wanted in the first place. And as for tuple assignment, one of the unique syntax syntactic features of the Python language is that it has the ability to have a tuple on the left side of an assignment statement. So this allows you to assign more than one variable at a time when the left side is a sequence. And so what I mean by that, so here is a list, and here we have a tuple which is, which has x and y as elements. And so if we assign these two on elements of that list to these two tuples and reprint x, we're going to get that x is assigned to the first item in that list and y to the second. So pre-print acts we get don't undo I play. And if the number of variables on the left and the number of values on the right are different. We're going to have an error which is too many values to pack. So that's value error. We can't have two elements and a tuple. However, three elements in that sequence, both sides or the statements are tuples actually. But the left here has is a topos variable. This is a tuple expression, so we're assigning that tuple variable to that tuple expressions. So a is equal to one, b is equal to two. However, this is, we don't have a barely a tuple variable word three. So this gives us, gives us an arrow. They have to have the same number of items on both the left and the side. And more generally, the right side could be any sequence. It can be a string or a list or tuple. So here, for example, is a string. And I have a tuple which has name and domain as elements. And I want to assign to it that string, which uses the split. Split retails for us is a sequence, a list actually. So name will have Sarah because with the split has an ad as a delimiter and domain has example.org. So that's one another way to use the tuple assignment. 34. Comparing Tuples: Alright, then tuples, we can actually also compare them, sorry, convertibles. And the comparison operators works with tuples and other sequences. Python starts by comparing the first element from each sequence. If they are equal, it goes on to the next element and so on, until it finds an element that is different. Subsequent elements are not considered even if they are really big. So here, for example, we have two tuples that we're trying to compare. And we see that the first elements are equal. So we go ahead to the next elements and we see that three is greater than one. So automatically we are returned by true, even though here we have other elements, we don't see them, we just gone to truth. And another example here, also 0 is equal to 0, so we go ahead and C1 and three. Also automatically true, although this number is huge and is much greater than four, however, we stopped at the second elements, so it returns for us true. 35. Dictionaries & Tuples: Now we reach our last topic for this course, which is called dictionaries and tuples. Dictionaries have a method called items that returns a list of tuples where each tuple is a key value pair. So here is our dictionary. And restore inside a variable, a list in which we use the items method to get the list of tuples of these key value pairs. And if we go ahead and print that list, you can see that we have a list and set up a brackets. And inside of that this, we have multiple tuples, so this is our first, second, third. And because dictionaries are unordered, they are listed in an unordered way, so there's no particular order. And if we want to sort the DOM, because this is a list, we can use the sort function. So it sorts them in ascending alphabetical order by the key about you. So a, B, and C. And we can have multiple assignment with dictionaries. So whenever we get that list which has these tuples, these tuples have a key and a value. And so we can call this, though both the key and a value and the pore loops. So for every key and value in that list of tuples, we're going to print, let's see value and then 60x. So here we've just flipped our items. So first we have the integers and then the alphabetic. 36. Project: Now we reach the project part of this course. And the project should help you implement all the knowledge that you acquired throughout this course by learning these different data structures, including strings, lists, dictionaries, tuples, and so on. And so for this project, you are given a file that contains a text file and a Python file that are attached in as a fall under the project section. So you should find that under the project section through this course. And so your first task, after you open this file and open it in your text editor, of course, you need to open that text file and Python and write it. So the project is actually pretty simple. You have this text file and you need to open it on, on that Python file. And we've seen how to do that throughout this course and re-explained it thoroughly. And so after we opened and you need to read it. However, while reading it will want to do some tasks, some several things we need to do. So first of all, when you read that text file, count the number of lines in that file. So we know that whenever you read a file, there's a certain method that we've learned that could help you move through that file line by line. So whenever you move on line by line, just count the number of lines. Secondly, create, you're going to actually create three lists, okay? So the first list, you're going to be adding the lines that you are reading from that text file that begin with a star. And you're going to create a list. And second list that you're going to add two with the lines that we'll start with that. And the third list is the list that you're going to add with it, the lines that start with a dash. And ofcourse, all these lines that I'm talking about are the lines of that text file. And so now after being done with these steps, we're going to work with these three lists. So for the first list, we're going to create a dictionary. And we've seen how to create a dictionary. And we're going to add the characters for only the first item in that list. So far only the first sentence in our list, because this list is one, contains several items as in several sentences. And so for each sentence will go in to, sorry for only the first sentence we're going to. And these characters or that sentence as keys and a number of times repeated as their values. And we've done a similar exercise on that. So if you're finding difficulty, just watch the video and it should be so easy for you. And just after that, convert the dictionary, you will create it to a tuple using a specific method. And we've explained that specific method in the course. Now for L2, just similar to the store and just take the first item. So the first sentence, and we're going to split the sentence into words. And we know that the split function splits our sentence into words and saved. So just create a variable to save these words when using that method. And just after that salt that newly created list. And we've also seen how to sort a list. So it shouldn't be hard at all. And finally, for the last list, we're going to remove the items or sentences because the items are actually sentences. And that file that have a length greater than 30. So if there's a sentence that has a length greater than 30, just remove it from the atlas. And we've also seen how to calculate the length of an item. So this is basically the project. It's not that hard at all. If you understand everything, it should be very easy for you. And if you have any questions, please don't hesitate to contact. I hope you enjoyed this course and I'm so excited to continue with you this journey onto other courses, hopefully in the future. Thank you so much for watching this course and I hope that you enjoyed it. As I said earlier.