Contents
1. My own example of programme
#store the full name
def store(data,full_name):
# str.split()
fnlist=full_name.split()
if len(fnlist)==2:
# list.insert()
fnlist.insert(1,' ')
labels=['first','second','last']
name_ex=look_up(data,full_name)
if name_ex:
# whenever the function has been called,
# this statement will execute
print('The person is already existed.')
else:
for label,name in zip(labels,fnlist):# zip()
if data[label].get(name):#dic.get()
data[label][name].append(full_name)
else:
data[label][name]=[full_name]
print('The person has been stored')
#look_up test whether the fullname is in the book
def look_up(data,full_name):
fnlist=full_name.split()
if len(fnlist)==2:
fnlist.insert(1,' ')
labels=['first','second','last']
for label,name in zip(labels,fnlist):
if data[label].get(name):
if full_name in data[label][name]:
print('The person is existed')
return True
else:
print('The person is not existed')
return False
else:
print('The person is not existed')
return False
# look_up_list return the wanted list by label and name
def look_up_list(data):
s=input('please enter the label and part of the name seperated by comma:')
s1=s.split(',')
label,name=s1
if data[label].get(name):
print(data[label][name])
else:
print('N/A')
order=input('''What do you want to do?
store a name? enter s
look for a person? enter l
look for the name list? enter ll''')
data={}
data['first']={}
data['second']={}
data['last']={}
#determine which interface to show
if order=='s':
choice=input('please enter the fullname:')
store(data,choice)
elif order=='l':
choice=input('please enter the fullname:')
look_up(data,choice)
elif order=='ll':
look_up_list(data)
2. str.split()
str.split(str="",num=string.count(str))[n]
: this function returns a list of strings.
Parameter Description:
str: split character, defaulted as a space
num: split times. The string will be splited into num+1 substrings.
3. list.insert()
list.insert(index, oject)
: this function insert an object into the list at given position, and it change the list in place without any returning.
4. zip()
zip()
: A useful built-in function for parallel iteration, which “zips” together the sequences , returning a sequence of tuples.
5. dict.get()
dict.get(key, return_value=None)
: The get method is a forgiving way of accessing dictionary items. If the key exists, it will return the value. Otherwise, it returns the return_value whose defaluted value is None, and you can give other value like “N/A” to this parameter.
6. The statements inside a function
In the store function:
if name_ex:
# whenever the function has been called,
# this statement will execut
print('The person is already existed.')
```