This article mainly introduces the in-depth parsing of variables and assignment operators in Python. It is a basic knowledge in Python learning. This means that a space is created in memory when a variable is created. Based on the variable’s data type, the interpreter allocates specified memory and decides what data can be stored in memory. Therefore, variables can specify different data types, and these variables can store integers, decimals, or characters.
Variable assignment in Python, variables do not need to be declared. Assignment of variables is a process of variable declaration and definition. Each variable is created in memory, including its identity, name, and data. Each variable must be assigned a value before it can be used, and then the variable can be created. The equal sign (=) is used to assign values to variables. The left side of the equal (=) operator is a variable name, and the right side of the equal (=) operator is the value stored in the variable. Such as:
#! /usr/bin/python
# -*- coding: UTF-8 -*-
counter = 100 # Assign integer variables
miles = 1000.0 # floating-point
name = "John" # string
print counter
print miles
print name
Copy the code
In the above example, 100,1000.0 and “John” are assigned to the counter, miles, and name variables, respectively. Need Python learning materials can 76 24 months 59 test 510 points remove the text plus my micro, information free to everyone! (There are too many information books, SO I just send some randomly!) Executing the above program produces the following results:
100 John 1000.0
Multiple Variable Assignment Python allows you to assign values to multiple variables simultaneously. Such as:
a = b = c = 1
In the example above, create an integer object with the value 1 and allocate the three variables to the same memory space. You can also specify multiple variables for multiple objects. Such as:
a, b, c = 1.2."john"
Copy the code
In the example above, two integer objects 1 and 2 are assigned to variables A and B, and the string object “John” is assigned to variable C.
The Python assignment operator assumes that variable A is 10 and variable B is 20:Finally, if your time is not very tight, and want to quickly improve, the most important thing is not afraid of hardship, I suggest you can price @762459510, that is really very good, many people progress quickly, need you not afraid of hardship oh! You can go to add a look at ~
The following example demonstrates all of Python’s assignment operators:
#! /usr/bin/python
a = 21
b = 10
c = 0
c = a + b
print "Line 1 - Value of c is ", c
c += a
print "Line 2 - Value of c is ", c
c *= a
print "Line 3 - Value of c is ", c
c /= a
print "Line 4 - Value of c is ", c
c = 2
c %= a
print "Line 5 - Value of c is ", c
c **= a
print "Line 6 - Value of c is ", c
c //= a
print "Line 7 - Value of c is ", c
Copy the code
The output of the above example is as follows:
Line 1 - Value of c is 31
Line 2 - Value of c is 52
Line 3 - Value of c is 1092
Line 4 - Value of c is 52
Line 5 - Value of c is 2
Line 6 - Value of c is 2097152
Line 7 - Value of c is 99864
Copy the code