1. Get Python idle current working directory:
1 Import OS 2 os.getcwd ()
2. Change the current working directory ("R" is required in front of the directory, otherwise it will report a syntax error):
Os.chdir (R" working directory ")
3. Run a function within the xxx.py:
1 from Import func_name #xxx不需要包含扩展名. py2#直接调用即可
4. Empty function
def func_name (): Pass #pass是占位符 for easy handling of non-written functions
5, data type check you can use the function isinstance (), to check the parameter type, only allow the parameters of integer and floating-point type
def my_abs (x): if not isinstance (x, (int, float)): Raise TypeError (' badoperand type') if x >= 0: return x Else: return -X
6. Define functions that contain mutable parameters:
1 def # Add a "*" to the parameter before calling the function to enter Calc directly (3,4,1,2) 2 sum = 03for in numbers:4 sum = SUM + N * n
5 return sum
7. Keyword parameters
Variable parameters allow the passing of 0 or any parameter, which is automatically assembled as a tuple when the function is called, and the keyword parameter allows the passing of 0 or any parameter with parameter names, which are automatically organized into a dict within the function.
def person (name, age, * *kw) :print('name:') Age : ' ' Other : ', kw)
>>> person(‘Bob‘, 35, city=‘Beijing‘)name: Bob age: 35 other: {‘city‘: ‘Beijing‘}
>>> extra = {‘city‘: ‘Beijing‘, ‘job‘: ‘Engineer‘} #还可以这样调用>>> person(‘Jack‘, 24, **extra)name: Jack age: 24 other: {‘city‘: ‘Beijing‘, ‘job‘: ‘Engineer‘}
8, Python defines the function, you can combine the 5 parameters (parameter combination), the combination order must be: Required parameters, default parameters, variable parameters, named keyword parameters, keyword parameters.
9. Change the following product function to make it accept one or more numbers and calculate the product:
1 def Product (x, y): 2 return X*y 3 4 5 # 6 def product (*kw) : 7 Pro = 1 8 for i in kw: 9 Pro = Pro * i 10 return Pro
Be aware of the syntax for defining mutable parameters and keyword parameters:
*argsis a variable parameter, and args receives a tuple;
**kwis a keyword parameter, kw receives a dict.
And how to pass in the syntax for variable and keyword arguments when calling a function:
Variable parameters can be directly passed func(1, 2, 3) in:, you can first assemble a list or a tuple, and then pass in *args : func(*(1, 2, 3)) ;
Keyword parameters can be directly passed in: func(a=1, b=2) , you can first assemble dict, and then pass in **kw : func(**{‘a‘: 1, ‘b‘: 2}) .
10. Recursive function
Hanoi
1 To move a Hanoi using a recursive function:2 defMove (n, a, B, c):3 ifn = = 1:4 Print('Move'A' -', c)5 Else:6Move (n-1, A, C, b)7Move (1, A, B, c)8Move (n-1, B, A, c)9 TenMove (4,'A','B','C')
11. Iterative Dict:
1d={"a": 3,"b": 2,"C": 6}2 forKeyinchd: #迭代key值3 Print(Key)4 5 forValueinchd.values (): #迭代value值6 Print(value)7 8 forK,dinchD.items (): #同时迭代key, value9 Print(k,d)
12, Python built-in function enumerate can change list, tuple into index-element pair, so that the index and the element itself can be iterated in the for loop at the same time:
1 d=[(2,3), (4,5), (up)]2 for in enumerate (d):3 Print (I,value)
13, the list generation statement is very concise, in a word you can generate a list object:
for in range (1,11)]
14. Yang Hui triangle generator
1 deftriangles (lines):2L=[]3 ifLines = =0:4 yieldL5 Else:6L=[1]7I=18 yieldL9 whilei<lines:TenL=[sum (i) forIinchZip ([0]+l,l+[0]] OneI=i+1 A yieldL
Reference to 51173580 this zip function, a slight modification of their own.
The first thing you don't understand is the 10th line of code, the two lists add up, that is, merging, understanding this, in the reference link of the code, it is easy to understand.
15, the use of map() functions, the user entered the non-standard English name, the first letter capitalized, other lowercase canonical name.
1 def Normalize (name): 2 return (Name.capitalize ()) 3 4 5 l1=["xiaoming","Dagou"," John"]6 l2=list (Map (NORMALIZE,L1))
[' Xiaoming ', ' Dagou ', ' John '] #输出结果
Python Small Summary