Stupid way to learn the 39th verse of Python
This section is mainly about dictionaries, the first two differences between a dictionary and a list are:
1. The list can be found by numbers in the list of elements, the number as the index, the dictionary can be anything to find the desired element, that is, the dictionary can be an object and another thing associated.
2. The list is in order, and the dictionary is unordered. (Mentioned in the previous section)
The code for this section is as follows:
1 classSong (object):2 3 def _init_(self, lyrics):4Self.lyrics =Lyrics5 6 defSing_me_a_song (self):7 forLineinchSelf.lyrics:8 Print Line9 TenHappy_bday = Song (["Happy Birthday to you", One "I don ' t want to get sued", A "So I'll stop right there"]) - -Bulls_on_parade = Song (["They rally around the family", the "With pockets full of shells"]) - - Happy_bday.sing_me_a_song () - +Bulls_on_parade.sing_me_a_song ()
This time there was an error with the following error message :
I did not have the wrong place, then asked eight abs, he told me that this error is very small, but it is difficult to find, but also I am unfamiliar with the constructor function results, the solution is: def _init_ (self, lyrics) This function is a constructor, There should be two underscore "_" on both sides of Init, but I only played one separately, so there was an error.
Another problem I have is that this def __init__ (self, lyrics) function exists meaning , this function is a constructor, it has two parameters self and lyrics, wherein the self is fixed, Since the two function def in this function is inside the class, it is equivalent to the two functions within the class that call each other, so the self.lyrics is used later.
After the problem is solved I try to use the For-loop loop, using the items () function, I write the following code:
1Song = {2 'Happy_bday':'Happy Birthday to your, I don\ ' t want to get sued, so i\ ' ll stop right there',3 'Bulls_on_parade':'They rally around the family, with pockets full of shells'4 }5 6 forKey, lineinchSong.items ():7 PrintLine
The operating result is:
Can be seen to be unordered.
Python Learning 05--Dictionary