PYTHON basic
1. 关于has , is.
class Person(object): def __init__(self, name): ## has name self.name = name ## Person has-a pet of some kind self.pet = None class Employee(Person): def __init__(self, name, salary): super(Employee, self).__init__(name) ## emplyee has salary self.salary = salary
2.描述代码发生了什么
class song has a __init__ that takes self and lyrics parameters.
has function named sing_me_a_song that takes self parameters.
Set bulls_on_parade to an instance of class Song.
From bulls_on_parade, get the sing_me_a_song function.
class Song(object): def __init__(self, lyrics): self.lyrics = lyrics def sing_me_a_song(self): for line in self.lyrics: print(line)
bulls_on_parade = Song(["They rally around tha family",
"With pockets full of shells"])
bulls_on_parade.sing_me_a_song()
3. 继续描述代码发生了什么
ten_things = "Apples Oranges Crows Telephone Light Sugar"
# set a string to ten_things.
stuff = ten_things.split(' ')
# Call split with argument ' ' on ten_things, set it to stuff
' '.join(stuff)
# string has a attribute 'join', argument.
stuff.append(next_one)
# Call append with argument next_one on stuff
4. python 练习, string
str1 = 'abcefghxyzThis,is,the,target,string xyzlkdjf'
idx1 = str1.find('xyz') # get the position of 'xyz'
idx2 = str1.find( 'xyz' , idx1+1) # get the next 'xyz'
str1 = str1[idx1+3:idx2].replace(',','|' ) # replace ',' with '|'
str1 = str1.strip() # strip trailing spaces.