* Collection:
set: holds a series of elements, but the set element is not duplicated and is unordered
How to create:set () and pass in a list,thelist element will be set the element.
S=set ([' A ', ' B ', ' C ']) print (s)//set ([' A ', ' C ', ' B ']) print (Len (s))//3
How to access: Use inch operator to determine if there is
= ([,,,]) ()//true
set Features of:
1. The internal structure and the dictionary are very similar, the only difference is not to store the value
= ([,,,,,,]) X1=x2=x1: () (x2): ()//ok
Traverse set:
traversing a set through a for loop
= ([(,), (,), (,)]) x:x[],,x[]
Lisa:85adma:95bart:50
Update Set
Two things:1 Add a new element to the set
2 Remove an existing element from the set
add element sets.add ( new element
S=set ([+]) add
eg
s=Set([1,2,3])
S.Add(4)
Print (s)//set ([1, 2, 3, 4])
Remove (): remove elements from set
sets.remove ( deleted element)
Add () can be added directly, with remove () before you need to determine
* function
What is a function: a chunk of reusable code that is a named block of code that accepts input, provides output, and can be stored in a file for later use.
calling functions
functions are divided into two main categories: custom functions, built-in functions
A. Pow (x , y) calculates the y of X**y x Sub-party
to invoke a function by ()
Print (POW (2,5))//32
The function has no input (no arguments) and must be added after the function ()
If you do not add (), it points to a function.
B. cmp (x, y), ifX<y,return-1, ifx==y, return0, ifX>yreturn 1 [2,34,1,5,98]
C. sum (x, y,...) Print sum ([1,6])
d. append () to add a new object at the end of the list
list.append ( obj)-obj objects added to the end
E. functions that do not return a value Print
=[]x=x<=:. Append (X*x) x=x+ (())//42925 If the function does not return a value, it returns none
Assigning a value to a function in Python will result in the inability to access the function you originally pointed to, and you need to restart Python
Custom functions: Use the DEF statement to write down the function name, parentheses, parameters in parentheses, and: And then write in the indent block
function body, the return value of the function is returned with a return statement.
Function name: can only contain letters, numbers, and underscores, and cannot begin with a number, indicating the simple name of its purpose.
def names (x):
function body
Return
Names (parametric)
NOTE: When the statement inside the function body executes, once it executes to the return statement, the function is completed and the result
Return. Therefore, it is possible to achieve very complex logic through conditional judgment and loop inside the function.
If there is no return statement, the result will be returned when the function is finished, and the result will be none
Return also returns none of Return
Return can be placed anywhere in the function, usually at the end.
Scope of the variable:
An important problem with a function is the scope, the scope of the variable, and the location of the program where it can be accessed
or visible.
Divided into two types: local scope, global scope
Local variables, global variables
F/elif/else try/execpt/final for/while do not sink into variable scope changes, they are in the code block of the
variables, which can also be accessed externally.
Search paths for variables: Local variables >> global variables
To modify a local variable to be the global variable: global
Lifetime of local variables: Local variables can only be used in functions, and local variables are automatically deleted when the function ends
Global Volume: Variables declared outside the function, any code in the program and any function can be read, however, in the function
You need to be particularly careful when assigning values to global variables in duplicate
Key words:
Words with special meanings, also known as reserved words, are pre-defined and special identifiers in the computer.
The keywords in Python include the following: The keyword cannot be used as a function name
And Del form while as Elif global with assert else if pass break import
Print class in Exec raise continue finally are return for lambda try
Parameters: Formal parameter types that can be used when calling a function
1. Required Parameters
2. Keyword parameters
3. Default parameters
4. Indefinite length parameter
1. Required parameters: Pass in the function in the correct order, the number of calls must be the same as when declaring
Exercise: Define a function that displays name and age
2. Keyword parameters:
Close to the function call, the function call uses the keyword argument to determine the passed-in parameter value.
The order of the arguments is not consistent with the declaration when using the keyword argument to allow the function call, and the Python interpreter can use the parameter name
Match the parameter value.
3. Default parameters:
When a function is called, the value of the default parameter is considered to be the default value if it is not passed in
4. Indefinite length parameter
You may need to deal with a function that has more arguments than was originally declared, and these parameters are called indeterminate length parameters.
* All unnamed variable parameters are stored.
def function name (*info)
Anonymous functions:
Using lambda to create anonymous functions
The body of a lambda is an expression, not a block of code
Note: Anonymous functions can contain only one statement
Grammar:
var = lambda arg1,arg2 ...: (expression)
Python module
Save a Python code file, the module can define functions, classes and variables, the module can also contain executable code
Import statement: Introducing modules
Syntax: Import module (block name) ...
Note: A module will only be imported once, no matter how many times you import it. This prevents the import module from executing again and again
This article from "Liu Xiao Rabbit" blog, declined reprint!
Python collections, functions