Python Basics---Chapter6 Abstraction (1)


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.')
      ```




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值