商业数据分析从入门到入职（6）Python程序结构和函数

CuterCorley 2021-04-05 22:06:12

一、Python程序结构

Python中，有3种常见的程序结构：

• Sequence顺序从上向下依次执行。
• Condition条件满足某个条件则执行。
• Loop循环重复执行某个动作。

1.if条件

``possibility_to_rain = 0.7print(possibility_to_rain > 0.8)print(possibility_to_rain > 0.3)possibility_to_rain = 1print(possibility_to_rain > 0.8)print(possibility_to_rain > 0.3)``

``FalseTrueTrueTrue``

``possibility_to_rain = 0.7if possibility_to_rain > 0.8: print("Do take your umberalla with you.") ## 这个地方标准格式是四个空格的缩进elif possibility_to_rain > 0.3: print("Take your umberalla just in case. hahaha") else: print("Enjoy the sunshine!")print('hello')``

``Take your umberalla just in case. hahaha``

``card_type = "debit"account_type = "checking"if card_type == "debit": if account_type == "checking": print("Checkings selectd.") else: print("Savings selected.")else: print("Credit card.")``

``Take your umberalla just in case. hahahahello``

2.循环

while循环

``count =1print(count)count+=1print(count)count+=1print(count)count+=1print(count)count+=1print(count)count+=1print(count)count+=1print(count)count+=1print(count)count+=1print(count)count+=1print(count)``

``12345678910``

``count = 1while count <= 10: print(count) count += 1``

``cnt = 1while True: print("cnt = %d" % cnt) ch = input('Do you want to continue? [y:n]: ') if ch == 'y': cnt += 1 else: break ``

``count = 1while count < 11: print(count) count = count + 1else: print('Counting complete.') print()count = 1while count < 11: print(count) count = count + 1 if count == 8: breakelse: print('Counting complete.')``

``12345678910Counting complete.1234567``

``count=0while count < 11: print("while count:",count) count = count + 1 if count == 11: breakelse: print("else:",count)``

``while count: 0while count: 1while count: 2while count: 3while count: 4while count: 5while count: 6while count: 7while count: 8while count: 9while count: 10``

for循环

``range(stop) -> range objectrange(start, stop[, step]) -> range object``

``for i in range(10): print(i)``

``0123456789``

``for i in range(4,10): print(i)print()for i in range(4,10,2): print(i)print()for i in range(5): print('Corley')print()for i in range(5): print('Corley'[i])``

``456789468CorleyCorleyCorleyCorleyCorleyCorle``

for循环中也可以使用break语句来终止循环，如下：

``for i in range(10): print(i) if i == 5: break``

``012345``

``result = 0for num in range(1,100): if num % 2 == 0: result = result + numprint(result)``

``2450``

3.案例-王者荣耀纯文本分析

``<ul class="herolist clearfix"><li><a href="herodetail/194.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg" width="91px" alt="苏烈">苏烈</a></li><li><a href="herodetail/195.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/195/195.jpg" width="91px" alt="百里玄策">百里玄策</a></li><li><a href="herodetail/196.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/196/196.jpg" width="91px" alt="百里守约">百里守约</a></li><li><a href="herodetail/193.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg" width="91px" alt="铠">铠</a></li></ul>``

``page_hero = '''<ul class="herolist clearfix"><li><a href="herodetail/194.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg" width="91px" alt="苏烈">苏烈</a></li><li><a href="herodetail/195.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/195/195.jpg" width="91px" alt="百里玄策">百里玄策</a></li><li><a href="herodetail/196.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/196/196.jpg" width="91px" alt="百里守约">百里守约</a></li><li><a href="herodetail/193.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg" width="91px" alt="铠">铠</a></li></ul>'''``

``start_link = page_hero.find('<img src="')print(start_link)start_quote = page_hero.find('"', start_link)end_quote = page_hero.find('"', start_quote+1)hero_link = page_hero[start_quote+1:end_quote]print(hero_link)``

``81http://game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg``

``# 第1个英雄start_link = page_hero.find('<a href="')start_quote = page_hero.find('"', start_link)end_quote = page_hero.find('"', start_quote+1)hero_info1 = page_hero[start_quote+1:end_quote]print(hero_info1)start_link = page_hero.find('<img src="', end_quote+1)start_quote = page_hero.find('"', start_link)end_quote = page_hero.find('"', start_quote+1)hero_link1 = page_hero[start_quote+1:end_quote]print(hero_link1)end_bracket = page_hero.find('>', end_quote+1)start_bracket = page_hero.find('<', end_bracket+1)hero_name1 = page_hero[end_bracket+1:start_bracket]print(hero_name1)``

``herodetail/194.shtmlhttp://game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg苏烈``

``# 第2个英雄page_hero = page_hero[start_bracket:]start_link = page_hero.find('<a href="')start_quote = page_hero.find('"', start_link)end_quote = page_hero.find('"', start_quote+1)hero_info2 = page_hero[start_quote+1:end_quote]print(hero_info2)start_link = page_hero.find('<img src="', end_quote+1)start_quote = page_hero.find('"', start_link)end_quote = page_hero.find('"', start_quote+1)hero_link2 = page_hero[start_quote+1:end_quote]print(hero_link2)end_bracket = page_hero.find('>', end_quote+1)start_bracket = page_hero.find('<', end_bracket+1)hero_name2 = page_hero[end_bracket+1:start_bracket]print(hero_name2)``

``herodetail/195.shtmlhttp://game.gtimg.cn/images/yxzj/img201606/heroimg/195/195.jpg百里玄策``

``# 第3个英雄page_hero = page_hero[start_bracket:]start_link = page_hero.find('<a href="')start_quote = page_hero.find('"', start_link)end_quote = page_hero.find('"', start_quote+1)hero_info3 = page_hero[start_quote+1:end_quote]print(hero_info3)start_link = page_hero.find('<img src="', end_quote+1)start_quote = page_hero.find('"', start_link)end_quote = page_hero.find('"', start_quote+1)hero_link3 = page_hero[start_quote+1:end_quote]print(hero_link3)end_bracket = page_hero.find('>', end_quote+1)start_bracket = page_hero.find('<', end_bracket+1)hero_name3 = page_hero[end_bracket+1:start_bracket]print(hero_name3)# 第4个英雄page_hero = page_hero[start_bracket:]start_link = page_hero.find('<a href="')start_quote = page_hero.find('"', start_link)end_quote = page_hero.find('"', start_quote+1)hero_info4 = page_hero[start_quote+1:end_quote]print(hero_info4)start_link = page_hero.find('<img src="', end_quote+1)start_quote = page_hero.find('"', start_link)end_quote = page_hero.find('"', start_quote+1)hero_link4 = page_hero[start_quote+1:end_quote]print(hero_link4)end_bracket = page_hero.find('>', end_quote+1)start_bracket = page_hero.find('<', end_bracket+1)hero_name4 = page_hero[end_bracket+1:start_bracket]print(hero_name4)``

``herodetail/196.shtmlhttp://game.gtimg.cn/images/yxzj/img201606/heroimg/196/196.jpg百里守约herodetail/193.shtmlhttp://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg铠``

``# 使用循环简化代码page_hero = '''<ul class="herolist clearfix"><li><a href="herodetail/194.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg" width="91px" alt="苏烈">苏烈</a></li><li><a href="herodetail/195.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/195/195.jpg" width="91px" alt="百里玄策">百里玄策</a></li><li><a href="herodetail/196.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/196/196.jpg" width="91px" alt="百里守约">百里守约</a></li><li><a href="herodetail/193.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg" width="91px" alt="铠">铠</a></li></ul>'''for i in range(4): print('第%d个英雄：' % (i+1)) start_link = page_hero.find('<a href="') start_quote = page_hero.find('"', start_link) end_quote = page_hero.find('"', start_quote+1) hero_info = page_hero[start_quote+1:end_quote] print(hero_info) start_link = page_hero.find('<img src="', end_quote+1) start_quote = page_hero.find('"', start_link) end_quote = page_hero.find('"', start_quote+1) hero_link = page_hero[start_quote+1:end_quote] print(hero_link) end_bracket = page_hero.find('>', end_quote+1) start_bracket = page_hero.find('<', end_bracket+1) hero_name = page_hero[end_bracket+1:start_bracket] print(hero_name) page_hero = page_hero[start_bracket:]``

``第1个英雄：herodetail/194.shtmlhttp://game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg苏烈第2个英雄：herodetail/195.shtmlhttp://game.gtimg.cn/images/yxzj/img201606/heroimg/195/195.jpg百里玄策第3个英雄：herodetail/196.shtmlhttp://game.gtimg.cn/images/yxzj/img201606/heroimg/196/196.jpg百里守约第4个英雄：herodetail/193.shtmlhttp://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg铠``

二、函数的介绍和基本使用

``# An empty function that does nothingdef do_nothing(): passdo_nothing()type(do_nothing)``

``function``

``# A function without parameters and returns valuesdef greeting(): print("Hello Python")# Call the functiona = greeting()``

``Hello Python``

``# A function with a parameter that returns nothingdef greeting(name): print("Hello %s" % name)# Call the functiongreeting('Corley')``

``Hello Corley``

``print(a)``

``None``

``# A function with a parameter and return a stringdef greeting_str(name): return "Hello again " + name# Use the functions = greeting_str("Corley")print(s)``

``Hello again Corley``

``# A function with 3 parametersdef menu(wine, entree, dessert): return "wine:{},entree:{},dessert:{}".format(wine,entree,dessert)# Get a menumenu('chardonnay', 'chicken', 'cake')``

``'wine:chardonnay,entree:chicken,dessert:cake'``

``menu(entree='beef', dessert='cake', wine='bordeaux')``

``'wine:bordeaux,entree:beef,dessert:cake'``

``# default dessert is puddingdef menu(wine, entree, dessert='pudding'): return "wine:{},entree:{},dessert:{}".format(wine,entree,dessert)# Call menu without providing dessertmenu('chardonnay', 'chicken')``

``'wine:chardonnay,entree:chicken,dessert:pudding'``

``# Default value will be overwritten if caller provide a valuemenu('chardonnay', 'chicken', 'doughnut')``

``'wine:chardonnay,entree:chicken,dessert:doughnut'``

``x = 1def new_x(): x = 5 print(x)def old_x(): print(x)new_x()old_x()``

``51``

``x = 1def change_x(): global x print('before changing inside,', x) x = 3 print('after changing inside,', x)print('before changing outside,', x)change_x()print('after changing outside,', x)``

``before changing outside, 1before changing inside, 1after changing inside, 3after changing outside, 3``

``# 使用函数实现def extract_info(current_page): start_link = current_page.find('<a href="') start_quote = current_page.find('"', start_link) end_quote = current_page.find('"', start_quote+1) hero_info = current_page[start_quote+1:end_quote] print(hero_info) start_link = current_page.find('<img src="', end_quote+1) start_quote = current_page.find('"', start_link) end_quote = current_page.find('"', start_quote+1) hero_link = current_page[start_quote+1:end_quote] print(hero_link) end_bracket = current_page.find('>', end_quote+1) start_bracket = current_page.find('<', end_bracket+1) hero_name = current_page[end_bracket+1:start_bracket] print(hero_name) return start_bracketstart_bracket = 0page_hero = '''<ul class="herolist clearfix"><li><a href="herodetail/194.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg" width="91px" alt="苏烈">苏烈</a></li><li><a href="herodetail/195.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/195/195.jpg" width="91px" alt="百里玄策">百里玄策</a></li><li><a href="herodetail/196.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/196/196.jpg" width="91px" alt="百里守约">百里守约</a></li><li><a href="herodetail/193.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg" width="91px" alt="铠">铠</a></li></ul>'''for i in range(4): print('第%d个英雄：' % (i+1)) page_hero = page_hero[start_bracket:] start_bracket = extract_info(page_hero) ``

``第1个英雄：herodetail/194.shtmlhttp://game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg苏烈第2个英雄：herodetail/195.shtmlhttp://game.gtimg.cn/images/yxzj/img201606/heroimg/195/195.jpg百里玄策第3个英雄：herodetail/196.shtmlhttp://game.gtimg.cn/images/yxzj/img201606/heroimg/196/196.jpg百里守约第4个英雄：herodetail/193.shtmlhttp://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg铠``

``# 使用函数实现def extract_info(i, current_page): start_link = current_page.find('<a href="') start_quote = current_page.find('"', start_link) end_quote = current_page.find('"', start_quote+1) hero_info = current_page[start_quote+1:end_quote] start_link = current_page.find('<img src="', end_quote+1) start_quote = current_page.find('"', start_link) end_quote = current_page.find('"', start_quote+1) hero_link = current_page[start_quote+1:end_quote] end_bracket = current_page.find('>', end_quote+1) start_bracket = current_page.find('<', end_bracket+1) hero_name = current_page[end_bracket+1:start_bracket] if hero_info.startswith('hero'): print('第%d个英雄：' % i) print(hero_info) print(hero_link) print(hero_name) return start_bracket else: return -1start_bracket = 0i = 1page_hero = '''<ul class="herolist clearfix"><li><a href="herodetail/194.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg" width="91px" alt="苏烈">苏烈</a></li><li><a href="herodetail/195.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/195/195.jpg" width="91px" alt="百里玄策">百里玄策</a></li><li><a href="herodetail/196.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/196/196.jpg" width="91px" alt="百里守约">百里守约</a></li><li><a href="herodetail/193.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg" width="91px" alt="铠">铠</a></li></ul>'''while True: page_hero = page_hero[start_bracket:] start_bracket = extract_info(i, page_hero) i += 1 if start_bracket == -1: break``

三、函数进阶

1.可变位置参数

``def print_args(*args): print('Positonal args:', args)print('hello', 'Corley')print('hello', 'Corley','again')print('what', 'are','you','doing')``

``hello Corleyhello Corley againwhat are you doing``

``def print_args(*args): print(type(args)) print('Positonal args:', args)print_args('hello')``

``<class 'tuple'>Positonal args: ('hello',)``

``def print_args_with_required(req1, req2, *args): print('req1:', req1) print('req2:', req2) print('all other args:', args)print_args_with_required()``

``---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-79-0f8d1d1519ce> in <module> 4 print('all other args:', args) 5 ----> 6 print_args_with_required()TypeError: print_args_with_required() missing 2 required positional arguments: 'req1' and 'req2'``

``def print_args_with_required(req1, req2, *args): print('req1:', req1) print('req2:', req2) print('all other args:', args)print_args_with_required(1,2)print_args_with_required(1,2, 3, 'hello')``

``req1: 1req2: 2all other args: ()req1: 1req2: 2all other args: (3, 'hello')``

2.可变关键字参数

``def print_kwargs(**kwargs): print('Keyword args:', kwargs)print_kwargs(1,2)``

``---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-81-fbc2fa215023> in <module> 2 print('Keyword args:', kwargs) 3 ----> 4 print_kwargs(1,2)TypeError: print_kwargs() takes 0 positional arguments but 2 were given``

``def print_kwargs(**kwargs): print('Keyword args:', kwargs)print_kwargs(fst=1,scd=2)``

``Keyword args: {'fst': 1, 'scd': 2}``

``def print_all_args(req1, req2, *args, **kwargs): print('required args:', req1, req2) print('Positonal args:', args) print('Keyword args:', kwargs)print_all_args(1,2,3,4,s='hello')``

``required args: 1 2Positonal args: (3, 4)Keyword args: {'s': 'hello'}``

3.函数定义和查看文档字符串

``?print``

``Docstring:print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)Prints the values to a stream, or to sys.stdout by default.Optional keyword arguments:file: a file-like object (stream); defaults to the current sys.stdout.sep: string inserted between values, default a space.end: string appended after the last value, default a newline.flush: whether to forcibly flush the stream.Type: builtin_function_or_method``

``def odd_or_even(num): ''' Return True id num is even, or return False if num is odd ''' return num % 2 == 0?odd_or_even``

``Signature: odd_or_even(num)Docstring:Return True id num is even,or return False if num is oddFile: XXX\<ipython-input-85-b86074aa0e21>Type: function``

``help(odd_or_even)``

``Help on function odd_or_even in module __main__:odd_or_even(num) Return True id num is even, or return False if num is odd``

``print(odd_or_even.__doc__)``

``Return True id num is even,or return False if num is odd``

5.函数作为参数

``def ask(): print('Do you love Python?')def answer(): print('Yes, I do')def run_sth(func): func()run_sth(ask)run_sth(answer)``

``Do you love Python?Yes, I do``

``def bin_op(func, op1, op2): return func(op1, op2)def add(op1, op2): return op1 + op2def sub(op1, op2): return op1 - op2print('1 + 2 =', bin_op(add, 1,2))print('1 - 2 =', bin_op(sub, 1,2))``

``1 + 2 = 31 - 2 = -1``

``def exp_factory(n): def exp(a): return a ** n return expsqr = exp_factory(2)print(type(sqr))print(sqr(3))``

``<class 'function'>9``

``cube = exp_factory(3)cube(3)``

``27``

6.装饰器

``def should_log(func): def func_with_log(*args, **kwargs): print('Calling:', func.__name__) return func(*args, **kwargs) return func_with_logadd_with_log = should_log(add)add_with_log(2,3)``

``Calling: add5``

``@should_logdef add(op1, op2): return op1 + op2@should_logdef sub(op1, op2): return op1 * op2add(1, 2)sub(1, 2)``

``Calling: addCalling: sub2``

7.匿名函数-lambda表达式

``def mul(op1, op2): return op1 * op2``

``bin_op(lambda op1, op2:op1*op2, 2,4)``

``8``

lambda表达式一般用于功能不复杂且使用不多的地方。

8.异常处理

``def div(op1, op2): try: return op1 / op2 except ZeroDivisionError: print('Division by zero')div(5, 0)``

``Division by zero``

``def div(op1, op2): try: return op1 / op2 except: print('Division by zero') finally: print('finished')div(5, 0)``

``Division by zerofinished``

https://www.helloworld.net/p/Mne4cgaiw7Ij8