Analysis of the role of a comma after a value assignment statement in Python
This example describes the role of a comma after a value assignment statement in Python. Share it with you for your reference. The specific analysis is as follows:
IDLE 2.6.2
?
1 2 3 4 5 6 7 8 9 10 11 12
>>> A = 1 >>> B = 2
This is a creation in
Article, where the information may have evolved or changed.
Usually write much is python, recently saw a little go, today encountered a problem, and share with you
package mainimport "fmt"type student struct { Name string Age int}func pase_student() { m := make(map[string]*student) stus := []student{ {Name: "zhou", Age: 24}, {Name: "li", Age: 23}, {Name: "wang", Age: 22}, } for _, stu := ra
Python assignmentThere are several types. Let's talk about them separately;
1 sequence assignment:X, Y, Z = 1, 2, 3
We can think of it as: x = 1, y = 2, Z = 3
2 link assignment:
X = y = 1Print ID (X)Print ID (y)
As you can see, the results of the two executions are the same. Both the X and Y variables store the reference address of integer object 1.
3. Incremental
Simply put, if object A is stored in a mutable object such as a list or dictionary, a shallow copy of B to A is just a copy of the first layer of the object, and modifying the second layer of B still affects two objects.A deep copy does not affect the original object.ImportCopy.copy () Shallow copyCopy.deepcopy () deep copyThe assignment operation is more like a reference, the new assignment creates a new m
The following table lists the assignment operators supported by all Python languages. Assuming that variable a holds 10 and variable B holds 20, then:
For example:
Try the following example to understand all the assignment operators available in the Python programming language:
#!/usr/bin/pythona = 21b = 10c
This example describes the role of a comma after a python assignment statement. Share to everyone for your reference. The specific analysis is as follows:
IDLE 2.6.2
>>> a = 1>>> b = 2,>>> print type (a)
>>> print type (b)
>>> c = []>>> d = [],>>> print type (c)
>>> print type (d)
After a comma is added to the
elements of elements .In this line of code, note that set is a recursive pass-through subset:# Set[j] + = (Elements[i]) #Why Elements? SET[J] = set[j] + According to the question 2,+= Set[j] is still the original set[j], it may be the element of elements. SoSET[J] + = Elements[i]may be equivalent toElements[*] + = Elements[i]Once the elements of the elements are changed, the result is naturally wrong.How to solve this problem? According to question 3, as long as the set and elements are "no
This example describes the role of commas after Python assignment statements. Share to everyone for your reference. The specific analysis is as follows:
IDLE 2.6.2
?
1 2 3 4 5 6 7 8 9 10 11-12
>>> A = 1 >>> b = 2, >>> print type (a)
After a comma is appended to an assignment expression, an tuple object is automatically obtained, and
parameter value changes the default parameter value of the function object, so the default parameter value of the next call is changed. The reason for this is because the default parameter value is a mutable object, causing each call to change the default parameter value .As a result, you can avoid the hassle of designing objects as immutable objects as possible when programming.Assignment manipulation Techniques---sequence unpacking (recursive unpacking)
The sequence that contains mul
) lock_user_file.close () #定义事先设定的username和password常亮_username = "Galen "_password=" Galen "inpu_user={} #创建一个空的字典, used to determine a user name entered several times count=0# The number of times to record input Whilecount Program two: #! usr/bin/evnpython#-*-codingutf-8-*-__author__= ' Galen ' Importgetpass asgp# introduces the Getpass module importpickleimportos# verifies that there is a file that holds the locked user, if not, creates an empty file and writes an empty list if Notos.path.ex
Assignment
In python, a value assignment is to create an object reference, instead of storing the object as another copy. For example:
>>> a=[1,2,3]>>> b=a>>> c=a
The object is [1, 2, 3], which is referenced by three variables a, B, and c respectively. The three variables do not exclusively occupy the object [1, 2, 3], or you can use any variable to modify the o
First, meaning:The local variable XXX reference was not defined before.ii. Causes of ErrorsIs that Python does not have a variable declaration, so it finds the scope of the variable by a simple rule: if there is a variable assignment inside a function, the variable is considered local, so if the value of the modified variable becomes a local variable.Iii. The scene that produced this errorPython code:val=9d
a sequence, nested sequences in a dictionary are sub-objects of complex objects. For sub-objects, Python stores it as a public image, and all copies of it are treated as a reference, so that when one of the references changes the mirror and the other reference uses the mirror, the image is changed.So look at the origin[2 here], that is [3, 4] this list. According to the definition of shallow copy, the cop1[2] points to the same list [3, 4]. Well, if
= =Len (relation): $ relation.append (flag)Panax Notoginseng Else: - relation.append (flag) the + #parameter, the value of the remaining variable is equal to the value of the first variable argument Aindex = 1 the whileIndex Len (ARG): +Globals () [arg[index]] =globals () [arg[0]] -Index + = 1 $ $ - #assigning values to variables - defassign (Var, value): the Globalrelation - ifLen (relation):Wuyii =0 the whileI Len (relation): - ifVarinchRelation[i]
Multiple assignment operations are performed simultaneously:>>> x, y , z = #元组赋值 >>> print (x, y, z) 1 2 3 >>> values = [] #列表赋值 >>> x, Y,z = values>>> print (x, y, z) 1 2 3Swap two variables at once>>> x, y, z = 1,2,3>>> print (x, y, z) 1 2 3>>> x, y = y,x>>> print (x, y) 2 1When a function returns a tuple, multiple attributes that are assigned at the same time are particularly useful>>> people = {"Name": "Alice", "age": "+", "Genda": "ma
Sticker problemNums = Range (5)#range is a built-in function that creates a list of integersPrint(nums)#prints "[0,1,2,3,4]"Print(Nums[2:4])#Get a slice from index 2 to 4 (exclusive); prints ' [2,3] 'Print(nums[2:])#Get a slice from index 2 to the end; prints "[2,3,4]"Print(Nums[:2])#Get a slice from the start to index 2 (exclusive); prints "[0,1]"Print(nums[:])#Get a slice of the whole list; prints "[0,1,2,3,4]"Print(Nums[:-1])#Slice indices can be negative; prints "[0,1,2,3]"Nums[2:4] = [8,9]#
In the process of executing a program python error: local variable ' totalcount ' referenced before assignment
phenomenon: The function defines a variable x, call the variable in the function body, and need to change the variable's worth time, it will error the local variable ' totalcount ' referenced before assignment
Solution: Define local variables as glo
The code is as follows:
MyVar = 1
Def myfunc ():MyVar + = 1
MyFunc ()
will prompt for error:
Unboundlocalerror:local variable ' myVar ' referenced before assignment
Python proposes the following hypothesis: If you assign a value to a variable anywhere within the body of the function, Python adds the name to the local namespace.
Statement MyVar + = 1 assi
When assigning values between objects in Python is passed by reference, the copy module in the standard library is required if the object needs to be copied.1. Copy.copy a shallow copy copies only the parent object and does not copy the inner sub-objects of the object.2. Copy.deepcopy deep copy Copy object and its sub-object Program:Import copy a = [1, 2, 3, 4, [' A ', ' B ']] #原始对象 b = A #赋值, reference to the object C = copy.copy (a) #对象拷贝, shal
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.