One, variable
A variable is a quantity that records a series of state changes
1. Variables are divided into mutable types and immutable types --mutable and immutable, depending on where the variables occupy in memory
Mutable type: List list[], dictionary dicta{}
Immutable types: String str, numeric int, tuple ()
2. Access can be divided into sequential access, mapped access and direct access
Direct access: Numeric int
Map access: Dictionary dict{}
Sequential access: String str, list list[], tuple ()
Two, set
Features: composed of different elements, elements unordered, elements in the collection must be immutable types
1. Set definition: Set ()
s = set (' Hello ')
Print (s)
#输出结果为 {' O ', ' h ', ' l ', ' e '}, unordered, no weight
where () must be an iterative type
2. Common methods
Add () #向集合中添加元素
Clear () #清空集合
- To delete an element in a collection
Pop () #随机删除某个元素remove () #指定某个值删除, compile error discard () #指定某个值删除 when there are no elements to delete, compile without error when no element to delete exists
3. Common operation
S1.intersection (S2) #求s1与s2的交集
S1.union (S2) #求s1与s2的并集
- Finding the difference set
S1.difference (S2) #s1中有, elements not in S2 s2.difference (S1) #反之
S1.difference_update (s2) #等价于s1 = s1-s2s1.isdisjoint (s2) #交集是否为空集s1. Issubset (S2) #s1是否为s2的子集, conversely, Issuperset () Determines whether the parent set
Third, string formatting
%s fixed type, used to pass values, pass multiple values (,,), separated by commas
%d--Digital,%s--can be used to transmit all data types
Print floating-point numbers:
%.2f #打印小数点后2位
Print percent sign:%
The value of the dictionary:
% (name) d % (age) S #传一个字典, key=name,key=age
Iv. functions
Form:
def test () keyword function name parameter code block return value
The definition of a function has the following main points:
- def: A keyword that represents a function
- Function name: the names of functions, which are called later by function name
- Function Body: A series of logical calculations in a function, such as sending a message, calculating the maximum number in [11,22,38,888,2], etc...
- Parameters: Providing data for the function body
- Return value: Once the function has finished executing, it can return data to the caller.
About the return value:
- No return value: Returns a none
- Has a return value: Returns an Object
- Multiple return values: Returns a tuple
There are three different parameters to the function:
- General parameters
- Default parameters
- Dynamic parameters
# ######### definition Function ######### # name is called function func formal parameter, abbreviation: Parameter def func (name): print name# ######### execute function ######### # ' Wupei Qi ' is called the actual parameter of function func, abbreviation: argument func (' nameless ') common parameter
def func (name, age =): print "%s:%s"% (name,age) # Specify parameter func (' nameless ', 19) # Use default parameter func (' famous ') Note: Default parameters need to be placed in the parameter list at the end of the default parameters
Parameters of the parameter when the position parameter, the use of the keyword parameter rules:
- Position parameters must be one by one corresponding, lack of one no, more than one can not
- One parameter cannot pass two values
- If the positional parameter is mixed with the keyword parameter, the positional parameter must be on the left side of the keyword argument
- Multiple sets of parameters (positional parameters, *args,**kwargs) correspond to the form (positional parameters, List, dictionary)
Five, local variables and global variables
A local variable; a variable defined in a subroutine.
Global variables, which can be called anywhere in the entire file
The nearest principle of variable use
About the Global keyword:
If there is no global keyword in the function, the local variable is read first, and if there is no local variable, only the global variable can be read, and it cannot be assigned or modified.
When the global keyword is in place, Globals is taken to the function as a variable, but for other functions it is still a global variable, but the value of the global variable can be modified in the previous function.
In order to avoid the problem of duplicate names, global variables are capitalized and local variables are lowercase.
function Execution Order
Vi. recursion
function calls itself, but must have a definite end condition
Python advanced "Fourth" function