First, the collection
1. Definition
A collection of different elements, which is a set of unordered hash values that can be used as keys to a dictionary.
2. Features
The collection itself is a mutable type, but the elements in the collection must be immutable types and are non-repeating, unordered.
3. Role
Relationship test; go heavy.
Ii. Common methods of collection
1. Create a collection in one of two ways:
#s1 = {1,2,3,3,3,3,4} or S = set (' Hello ')
2. Adding elements
# S1.add (5)
# S1.add (6,7,8,9) #只能接收一个值, pass 4 values will be an error
3. Emptying the collection
# s.clear ()
4. Copy
# S1 = s.copy ()
5. Delete
# s = {' SB ', 1,2,3,4,5,6}
#随机删
# S.pop ()
#指定删除
# s.remove (' SB ')
# s.remove (' hellol ') #删除不存在的元素会报错
# S.discard (' sbbbb ') #删除不存在的元素不会报错
6. Update
# s1={1,2}
# s2={1,2,3}
# s1.update (S2) #update可以更新多个值, add elements from S2 that are not in S1 to S1
# print (S1) # S1 's elements have changed.
# s1.union (S2) # Find the combination of the two, not the update
# print (S1) # The elements in S1 have not changed
#s1. Update ((3,4)) #可以传进去, lists, tuples can be passed, as long as you can iterate
Third, the relationship test
# op=[' Zyh ', ' yyq ', ' ZJQ '
# ec=[' zjq ', ' cyh '
# P_s=set (OP) # for looping the elements in the list, generating a collection
# L_s=set (EC)
1. Finding the intersection
# Print (p_s,l_s)
# Print (P_s.intersection (l_s))
# Print (p_s&l_s)
2. Seek and set
# Print (P_s.union (l_s))
# Print (p_s|l_s)
3. Finding the difference set
# print (' difference set ', p_s-l_s) #存在于p_s中但不存在于l_s中的元素, is that I have elements that you do not have
# Print (P_s.difference (l_s))
# print (' difference set ', l_s-p_s) #存在于l_s中但不存在于p_s中的元素, is that I have elements that you do not have
# Print (L_s.difference (p_s))
4. Cross-complement (set first, then remove the two elements without duplicates)
# print (' Cross complement set ', P_s.symmetric_difference (l_s))
# print (' Cross complement set ', p_s^l_s)
S1 = {1,2,3,4}
S2 = {2,3,5}
Print (S1^S2) # {1, 4, 5}
5. Two sets have no intersection, return True (have) or false (none)
# s1={1,2}
# s2={2,3,5}
# Print (S1.isdisjoint (S2))
6. Subset, Parent set
s1={1,2}
s2={1,2,3}
Print (S1.issubset (S2)) #s1 is a subset of S2
Print (S2.issubset (S1)) #False
Print (S2.issuperset (S1)) #s1 is the parent set of S2
Four, string formatting
1. String formatting two methods: Percent semicolon and format,%s can accept everything
2. Percent-Semicolon form
Format:%[(name)][flags][width]. [Precision]typecode
(name) optional, used to select the specified key
Flags are optional, and the values to choose from are:
+ Right-aligned, positive plus, negative before plus minus;
-left-justified; no sign before positive number, plus minus sign before negative number;
Spaces are right-justified, plus a positive number before a negative number plus a minus sign;
0 Right alignment, no sign before positive number, minus sign before negative, and 0 padding in blank
Width selectable, occupied widths
. Precision optional, number of digits retained after the decimal point
TypeCode must choose
Simple example: Print ("Hello%s"% "Yyq")
# # #打印字符串 # #
# msg= ' I am%s My hobby is%s '% (' yyq ', ' zyh ')
# Print (msg)
#i am YYQ My hobby is zyh
# msg = "I am {0}, age {1}, really". Format ("Seven", 18)
# Print (msg)
# msg= ' I am%s My hobby is%s '% (' Yyq ', 1)
# Print (msg)
# I am seven, age, really
# msg= ' I am%s My hobby is%s '% (' yyq ', [up])
# Print (msg)
# I am yyq my hobby is [1, 2]
# name= ' Yyq '
# age=19
# msg= ' I am%s My hobby is%s '% (name,age)
# Print (msg)
# I am yyq my hobby is 19
# # #打印浮点数 # #
# TPL = "percent%.2f"% 99.976234444444444444 # print percent, retain two decimal places
# Print (TPL)
# percent 99.98
# # #打印百分比 # #
# TPL = ' percent%.2f%% '% 99.976234444444444444 #打印百分数, reserved two digits after decimal point, percent print percentage
# Print (TPL)
# percent 99.98%
# # #对齐 # #
# msg= ' I am% (name) +60s My hobby is zyh '%{' name ': ' Yyq '} right-aligned 60 positions
# Print (msg)
3.format format
Format: [[Fill]align][sign][#][0][width][,][.precision][type]
Fill [optional] white space filled with characters
Align [optional] alignment (required with width)
<, content left justified
Content right-aligned (default)
=, the content is right-aligned, the symbol is placed to the left of the padding character, and only valid for numeric types. Even: symbol + filler + number
^, content centered
sign [optional] have unsigned numbers
+, positive home Plus, minus plus negative;
-The plus sign does not change, minus is negative;
Spaces, plus spaces, minus signs and negative;
# [optional] for binary, octal, hex, if you add #, it will show 0b/0o/0x, otherwise it won't show
, [optional] adds a delimiter to the number, such as: 1,000,000
width [optional] format the size of the placeholder
. precision [optional] decimal digits reserved precision
Type [optional] formatting types
# # #示例 # #
# TPL = "I am {name}, age {age}, really {name}". Format (name= "Seven", age=18) takes in order
# TPL = "I am {0}, age {1}, really". Format ("Seven", 18) # does not correspond to the error, according to the tuple subscript method to take
# TPL = "I am {name}, age {age}, really {name}". Format (**{"name": "Seven", "Age": ()) #这种是字典的形式, so the value is also in the format of the dictionary, and to * *
# TPL = "I am {: s}, age {:d}". Format (*["Seven"): S is a string type,:d is a numeric type, * is a list to be passed in
# TPL = "I am {: s}, age {:d}". Format ("Seven", 18)
#
# l=["Seven", 18]
# TPL = "I am {: s}, age {:d}". Format (*L) equivalent to format ("seven", 18)
# Print (TPL)
TPL = "Numbers: {: b},{:o},{:d},{:x},{:x}, {:%},{}". Format (15, 15, 15, 15, 15, 15.87623, 2)
Print (TPL)
: b-Binary
: o octal
: X Hex abcdef
: X Hex ABCDE
:% is displayed as a percentage, the default is 6 digits after the decimal point
Five, function
1. Definition
Functions in Python are a programmatic method of logical structuring and process. The "process" is a function that has no return value, and there are no returns. But Python returns none by default, so forget the word "process"
2. Format
# def Test (x):
# y=2*x+1
# return Y
def: A keyword that defines a function
Test: Name of function
(): Internal definable parameters
"": Document description (not necessary, but it is strongly recommended to add descriptive information for your function)
Y=2*x+1: Generic code block or program processing logic
Return: Define the return value
Call run: can be with parameters or without
Function name ()
3. Benefits of using functions:
Code reuse
Maintain consistency and ease of maintenance
Scalability
4. Return value
Function: Gets the execution knot of the function. In the actual production, the subsequent code may perform different operations depending on the result.
# def test03 ():
# msg = ' test03 '
# Print (msg)
# return 1,2,3,4, ' a ', [' Alex '],{' name ': ' Alex '},none # when the return value is multiple values, the function returns a tuple
# def test04 ():
# msg = ' test03 '
# Print (msg)
# return {' name ': ' Alex '} # back to Dictionary
Summarize:
Number of return values = 0: Return None
Number of return values = 1: Return object
Number of return values >1: return tuple
5. Parameters
Parametric the memory unit is allocated only when called, releasing the allocated memory unit immediately at the end of the call. Therefore, the formal parameter is only valid inside the function. Function call ends when you return to the keynote function, you can no longer use the shape parametric
Arguments can be constants, variables, expressions, functions, and so on, regardless of the type of argument, and when a function call is made, they must have a definite value in order to pass these values to the parameter. It is therefore necessary to use the assignment, input and other methods to get the parameters to determine the value
Def calc (x, y): #x =2,y=3 #x, y is a formal parameter and only allocates memory units when called (values are passed)
Res=x**y
return res
A=calc (2,3) #2, 3 is an argument
Print (a)
# # #默认参数 # #
def stu_register (name,age,course,country= "CN"):
Often used to install the software, the default installation path, the default installation of a big push not needed things
# # #位置参数 # #
Stu_register (name,age,course,country= "USA") # Name,age,course This is the positional parameter
# # #关键字参数 # #
Under normal circumstances, to pass parameters to the function in order, do not want to order the key parameters can be used, just specify the parameter name, but remember a requirement is that the key parameters must be placed after the position parameter.
Stu_register (age=22,name= ' Yyq ', job= "op") # This type of key=value is the keyword argument
# # #参数组 (* * dictionary, * list) # # #
If your function is undefined, you can use non-fixed parameters when you are not sure how many parameters the user wants to pass in.
# *args (list)
# def Test (X,*args):
# Print (x)
# print (args)
# test (1,2,3,4,5) # returns 1 and (2,3,4,5)
# test (1,{' name ': ' Alex '}) # returns 1 and ({' Name ': ' Alex '},)
# Test (1, (' x ', ' y ', ' z ')) # returns 1 and ((' X ', ' y ', ' z '),)
# Test (1,[' x ', ' y ', ' z ') # to pass the list as a whole into args, return 1 and ([' X ', ' y ', ' z '],)
# Test (1,*[' x ', ' y ', ' z ']) # The elements inside the loop list are then passed to args, and the elements in the list are passed to args, returning 1 and (' x ', ' y ', ' z ')
# Test (1,* (' x ', ' y ', ' z ')) # The elements inside the loop tuple are then passed to args, passing elements of the tuple to args, returning 1 and (' x ', ' y ', ' z ')
Summary: *args: Make a list of the remaining non-key-value parameters, then pass in the function, and will not report an error if you do not give it a value
# **kwargs (dictionary)
# def Test (X,**kwargs):
# Print (x)
# print (Kwargs) #返回一个字典
# Test (1,y=2,z=3)
# test (1,1,2,2,2,2,2,y=2,z=3) will error because the unknown parameter has only one
# test (1,y=2,z=3,z=3) #会报错: One parameter cannot pass two values
# *args&**kwargs
# def Test (X,*args,**kwargs):
# Print (x)
# print (Args,args[-1])
# Print (Kwargs,kwargs.get (' y '))
# test (1,1,2,1,1,11,1,x=1,y=2,z=3) #报错
# Test (1,1,2,1,1,11,1,y=2,z=3)
# test (1,*[1,2,3],**{' y ': 1})
PYTHON04 basic data Types-collection/formatting output/functions/Parameters