Class exercises on day4

Dictionary Applications

switch statement to achieve
the first method:
Here Insert Picture Description
Here Insert Picture DescriptionHere Insert Picture Description
The second method:
! [Here insert a picture description] ( https://img-blog.csdnimg.cn/20190523172504896.pnHere Insert Picture Description
Here Insert Picture Description

Topic Demand: Suppose the user has several names and a list of favorite movies, some existing users have read and liked some movies, now looking for a new movie to see, do not know what to look good. According to available data, the user is interested to find most similar to the user, that is, seen and liked the movie is closest to the user, and then select a current user have not seen the movie from the user's favorite movies, make recommendations .
Skill Points: Python built-in functions and use built-in dictionary and a collection of

generated movie recommendations require data
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
movies recommended to achieve Case
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
05 traversing the dictionary to understand
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
tuples and list of diverse assignments
Here Insert Picture Description
Here Insert Picture Description
a key multi-value dictionary: defaultdict
Solution:
1) the dictionary is a. species associated with the container, each key is mapped to a single value. If you want the key is mapped to a plurality of values, save those values to the plurality of containers (list or collection) of.
2) Using collections module defaultdict class automatically initialize the first value, so just focus on adding elements.

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Defaultdict word frequency statistics
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
07default Case Code
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

function

如果在开发程序时,需要某块代码多次,但是为了提高编写的效率以及代码的重用,所以把具有独立功能的代码块组织为一个小模块,这就是函数.
10函数创建和调用
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190523175337506.pngHere Insert Picture Description
Here Insert Picture Description
空函数

定义一个什么事也不做的空函数,可以用 pass 语句;
pass 可以用来作为占位符,还没想好怎么写函数的代码,就可以先放一个 pass ,让代码能运行起来
11温度转换
Here Insert Picture Description
Here Insert Picture Description
12猜数字游戏
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
变量作用域
局部变量:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
全局变量:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
注意:global的本质是声明可以修改全局变量的指向, 即变量可以指向新的数据。
1). 不可变类型的全局变量: 指向的数据不能修改, 不使用global时无法修改全局变量。
2). 可变类型的全局变量: 指向的数据可以修改, 不使用global时可以修改全局变量。

Parameters passed
argument in parentheses defining for receiving the parameters, referred to as "parameter"
parameter in the call parentheses, to be passed to a function called "arguments"
parameter check
function is called If the number of parameters right, Python interpreter will automatically check it out and throw TypeError;
if the parameter type is not correct, Python interpreter will not be able to help us check.
Data type checking may be implemented using built-in functions isinstance
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
! [Inserted here described image] ( https://img-blog.csdnimg.cn/20190523181238642.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MjgzNQ= =, size_16, color_FFFFFF, t_70
Here Insert Picture Description
default parameter
Here Insert Picture Description
Here Insert Picture Description
keyword arguments
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43592835/article/details/90485601