First, the collection of Python
Collections are inherently de-weighed and unordered, and because they are unordered, the collection cannot be evaluated by subscript
A new collection is created by:
s = set () #空集合
S2 = {' 1 ', ' 2 ', ' 3 '}
The elements are added in the following ways:
S.add (' 1 ')
The methods for deleting elements are:
S.remove (' 1 ')
S.pop () #随机删除一个值
S1 = {A-i}
S2 = {3,4,5}
Intersection:
S2.intersection (S2)
S2 & S1
and set:
S2.union (S1)
S2 | S1
Subtraction (S2 is different from the value of S1):
S2.difference (S1)
S3-s2
Ii. recursion for Python
Recursion, that is, self-invocation, can recursive up to 999 times
Count = 0
def test1 ():
num = Int (input (' Please enter a number: '))
Global Count
Count+=1
num = 8
If num% 2 = = 0: # To determine if the number entered is not even
Pass
Print (count)
Else
return test1 () # If it is not even then continue calling yourself, enter the value
Print (Test1 ()) # Call Test
Third, Python functions
It's a function. A bunch of code together, into a whole, will need to repeat the steps into a function, can be concise code, but also improve the reusability of code
It is important to note that functions are not invoked and are not executed
There is a difference between a global variable and a local variable in a function, and if there is a local variable inside the function, the defined variable needs to be made before the variable is called.
def hello (file_name,content= "): #形参, formal parameters
#函数里面的变量都是局部变量
f = open (file_name, ' A + ', encoding= ' utf-8 ')
If content:
F.write (content)
res = '
Else
F.seek (0)
res = F.read ()
F.close ()
return res
name = Hello (' aa.txt ', ' KK ') #调用 #这里的aa. Txt,hhhh is an argument
Print (name)
The above file_name belongs to the positional parameter, required, and the content because the default value has been set, not required
The function does not have to have a return value, and the return value is None if the function has no return value
Return in the function is equivalent to break in the loop, and if you run to return in the function, the function ends directly
In addition to the positional parameters, there are mutable parameters, and the following args form a tuple
def test (A,b=1,*args):
Print (a)
Print (b)
Print (args)
Test (' hh ', 2, ' KK ', ' ss ', ' ll ')
Test (A=5,B=10)
t = [1,2,3,4,5]
Test (*T)
Keyword argument is in dictionary form
def test2 (A,**kwargs):
Print (a)
Print (Kwargs)
Test2 (a=1,name= ' hhh ', sex= ' female ')
The variables inside the function are local variables, and to modify the global variables, you need to add global before the variables in the function
This defines a function that verifies whether a string is a valid decimal number.
1, the number of decimal point is 1 2, according to the decimal point division
def check_float (s):
s = str (s)
Print (s)
If S.count ('. ') ==1:
S_list = S.split ('. ')
#1.2 [1.2]
left = S_list[0]
right = S_list[1]
If Left.isdigit () and Right.isdigit ():
Return True
Elif left.startswith ('-') and Left.count ('-') ==1:
If Left.split ('-') [ -1].isdigit () and Right.isdigit ():
Return True
Return False
Print (Check_float (' +5.0 '))
Iv. python Module
The Python module is divided into three types,
1. Standard module
That is, Python comes with, no need to install itself
2. Third-party modules
Need to be installed, is provided by others
Use pip install XXX to automatically install modules
Manual Installation steps:
# Download the installation package first
# Unzip
# in the command line into the directory after the decompression
# Execute Python setup.py install
3. python files written by yourself
Add environment variable if not in directory of current PY file
Import XX Imports a file, the essence of the import file is to run this file again
Python collections, recursion, functions, and modules