April 22, 2019
1. Variable Advancement
-
Variable and Invariant Types
Invariant Types: Number Types, Strings, Tuples
Variable types: lists and dictionaries
2. Receiving function returns data (tuples)
It is inconvenient to operate the contents of tuples directly. There are other ways: you want to use multiple variables for acceptance, the first tuple content to a and the second to b, note that the number of variables in the tuple needs to be the same as the number of accepted variables.
a, b = measure()
3. Advancement of Functional Parameters
def demo(num, num_list): # Modifying parameters inside functions does not affect the content of arguments print("inner biock") num = 10 num_list = [1, 2, 3] print(num_list) print(num) glob = 100 giob_list = [4, 5, 5] demo(glob, giob_list) print(glob) print(giob_list)
inner biock [1, 2, 3] 10 100 [4, 5, 5]
Variable types affect the value of the argument by modifying the parameter by method
def demo(num_list): # Modifying parameters inside functions does not affect the content of arguments print("inner biock") num = 10 num_list.append(9) num_list = [1, 2, 3, 4] print(num_list) giob_list = [4, 5, 5] demo(giob_list) print(giob_list)
inner biock [1, 2, 3, 4] [4, 5, 5, 9]
4. List addition is not content addition but list integration
a = [1, 2] b = [3, 4] print(a + b)
[1, 2, 3, 4]
5. Function default parameters
You can learn the meaning of default parameters from the following examples, and simplify function calls by setting parameter values. In this case, the default value of revest is false
gl_list = [6, 3, 9] gl_list.sort() # Ascending sort print(gl_list) gl_list.sort(reverse=True) # Descending sort print(gl_list)
The following is an example of a function default:
def print_info(name, gender = True): gender_text = "Schoolboy" if not gender: gender_text = "Girl student" print("%s yes %s" %(name, gender_text)) print_info("Xiao Ming") print_info("Xiaomei", False)
Xiao Ming is a boy. Xiaomei is a girl
Note: The default parameter should be at the end (right), and no default should be at the left most of the function.
6. Multivalued parameters
When the transfer parameters are uncertain, we can use multi-valued parameters, which are mostly used in the framework of network Daniel development.
* args acceptance tuple
** kwargs Acceptance Dictionary
def demo(num, *nums, **persons): print(num) print(nums) print(persons) demo(1, 2, 3, 4, 5, name = "xiaoming", age = "18")
1 (2, 3, 4, 5) {'name': 'xiaoming', 'age': '18'}
Look at the following two pieces of code
def sun_numbers(args): num = 0 print(args) for n in args: num = num + n return num result = sun_numbers((1, 2, 3, 4, 5)) print(result)
def sun_numbers(*args): num = 0 print(args) for n in args: num = num + n return num result = sun_numbers(1, 2, 3, 4, 5) print(result)
E: pro 07 grammatical advancement venv Scripts py thon. exe E:/ pro/07 grammatical advancement/hm_17 summation of multivalued parameters. (1, 2, 3, 4, 5) 15
Note: Use * to make calls easier
7. Unwrapping tuples and dictionaries
def demo(*args, **kwargs): print(args) print(kwargs) gl_nums = (1, 2, 3) gl_dict = {"name": "xiaoming", "age": 18} demo(gl_nums, gl_dict) # They are passed to args, so they need to be unpacked demo(*gl_nums, **gl_dict) # Successful unpacking grammar demo(1, 2, 3, name="xiaoming", age=18) #Other methods
Operation result
((1, 2, 3), {'name': 'xiaoming', 'ade': 18}) {} (1, 2, 3) {'name': 'xiaoming', 'ade': 18} (1, 2, 3) {'name': 'xiaoming', 'age': 18}
8. Recursion of Functions
Essence: Solving recursive problems, code reuse, calling yourself, the most important thing is the recursive exit.
Example: Recursive summation
def sum_number(num): # 1. Recursive export if num == 1: return 1 # 2. Digital Accumulation # Assume that sun_number can handle 1... correctly. num - 1 temp = sum_number(num - 1) return temp + num result = sum_number(100) print(result)
9. Object-Oriented (OOP)
- Procedure-oriented: Sequential execution, only execution, no return value, is characterized by focusing on how to do, that is, calling different functions in the main program, focusing on steps and processes, not responsible for the division of responsibilities, not suitable for complex project development. It is not suitable for the increase of new demand.
- Object-Oriented: Object-Oriented is a greater encapsulation that focuses on the subject (who does it). It encapsulates content as a method. That is, different objects encapsulate different methods to complete different functions, which is very suitable for the development of large and complex projects.
- Core concept: class and object, class is abstract and can not be used directly, its mission is to create objects, objects are instantiations of classes. In program development, there are classes and objects. Classes only need one, there are many objects, and the attributes of different objects may be different. What attributes and methods are in the class, what attributes and methods are in the object?
- Class Design: (1) Requirement Analysis (== Class Name, Attribute and Method=== and meets the Naming Method of Big Hump). Features are described as attributes and behaviors are described as methods.
- A built-in function dir: Pass in identifier / data in dir to view all attributes and methods. The method name is python's built-in method / attribute
>>> s = [1,2] >>> s [1, 2] >>> dir(s) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>>
- Define simple classes (including methods only):
# Needs: Kittens like fish and kittens like drinking water class Cat: def eat(self): print("Kittens love fish") def drink(self): print("Kittens love to drink water") # Instance object tom = Cat() tom.eat() tom.drink()
- Object-Oriented Applications: By default, using print to output object variables, which class the object referenced by this variable was created by and the address in memory (expressed in hexadecimal form)
print(tom)
<__main__.Cat object at 0x000001CEDA2B2B70>