Public account: You and the cabin by: Peter Editor: Peter
Hello, I’m Peter
Today is an article on Python variables and assignment.
In fact, we have seen a lot of assignment statements in Python, such as a=1, which assigns the value 1 to the variable A.
Before getting into assignment statements, let’s look at variables in Python.
The environment
Peter: Yes, I do.
- System: MacOS
- Tool: Jupyter Notebook
- Python version: 3.7.5
- Document editor: Typora
variable
Have a good understanding of the memory location of variables
A variable is a container for data, so to speak. When you define variables in Python, you don’t need to declare them. When we first assign a value to a variable, the variable is automatically created and its type is specified.
Variables themselves are not typed, only objects (assigned data) are typed
a = 66
a
Copy the code
66
Copy the code
b = 66
b
Copy the code
66
Copy the code
type(a) # check that the value type is an integer int
Copy the code
int
Copy the code
type(b) # string type
Copy the code
int
Copy the code
We define two variables a and B, both of which are the number 66. Although the names are different, they represent the same element in the computer. Look at their memory address.
For example: Pig Eight Quit (value 66) this person, we can call “two shi Elder brother” (in variable A), can also be called “Tianpeng Marshal” (in variable B), but in essence they are pig eight Quit, just changed individual names, essentially the same
id(a) A and B have the same memory address
Copy the code
4387310752
Copy the code
id(b)
Copy the code
4387310752
Copy the code
Let’s define another variable assignment:
a = 77
a
Copy the code
77
Copy the code
id(a)
Copy the code
4387311104
Copy the code
We see that we assigned the value 66 to a and the memory address was 4430785696, and then we assigned the value 77 to A and the memory address became 4430786048.
Why is that? In fact, the numbers 66 and 77 are essentially two objects in the computer, but they just happen to have the same name. For example, journey to the West has a plot: the real Monkey King
The real Monkey King (number 66) and the fake Monkey King (number 77) are both called monkeys (label A), but they are actually two different monkeys (assigned different addresses).
Create a variable
The creation of a variable is achieved through an assignment statement
x = 99 # numeric
language = "python" # string
number = [1.3.5.7.9] # list
print(x)
print(language)
print(number)
Copy the code
99
python
[1, 3, 5, 7, 9]
Copy the code
Naming rules for variable names
Python variables can be named with short names, such as x, y, z, a, b, etc., or with descriptive names, such as age, name, sex, so that others can see what the variables mean. The usual Python naming rules for variables are:
- Variable names must start with a letter or underscore character, not a number
- Variable names can contain only alphanumeric characters and underscores (A-z, 0-9, and _)
- Variable names are case sensitive (name, name, and name are three different variables)
- Variable names must not conflict with Python keywords, or they will not be valid
Let’s look at common assignment methods in Python
Assignment statement
Conventional assignment
Assignment: To attach a Python data object to a variable, as if attaching a label to the object. Python uses the equal sign = as the assignment operator, in the form:
name = value
# variable = some value
Copy the code
list1 = ["python"."java"] # list assigned to variable B
list1
Copy the code
['python', 'java']
Copy the code
list2 = [1.2["python"."html"], (1.4.7)] # nested list
list2
Copy the code
[1, 2, ['python', 'html'], (1, 4, 7)]
Copy the code
age = 28 # numerical
age
Copy the code
28
Copy the code
information = "xiaoming is a boy" # string
information
Copy the code
'xiaoming is a boy'
Copy the code
# define a variable dic, dictionary type
dic = {"name":"xiaoming"."age":20."sex":"fale"}
dic
Copy the code
{'name': 'xiaoming', 'age': 20, 'sex': 'fale'}
Copy the code
Multivariable assignment
Assign three variables at once
m, n, o= 22."xiaoming"."Male" # Assign 3 variables at once
Copy the code
m
Copy the code
22
Copy the code
n
Copy the code
'xiaoming'
Copy the code
o
Copy the code
'male'Copy the code
The above example shows that 22 is assigned to m, the string object “xiaoming” is assigned to N, and “male” is assigned to the variable O
name, age = ("Peter".20) # Assign by tuple
Copy the code
name
Copy the code
'Peter'
Copy the code
age
Copy the code
20
Copy the code
The above example performs chained assignments in the form of Python tuples
Chain assignment
x1 = y1 = 33
Copy the code
x1
Copy the code
33
Copy the code
y1
Copy the code
33
Copy the code
In the example above we defined both variables x1 and y1 by chain assignment. They are the same object in memory.
id(x1)
Copy the code
4387309696
Copy the code
id(y1)
Copy the code
4387309696
Copy the code
It’s just the same Python object labeled differently, but essentially the same
Variable swap
k, j = 9.5
Copy the code
The above variable assignment is equivalent to:
k=9
j=5
Copy the code
print("k =",k)
print("j =",j)
Copy the code
k = 9
j = 5
Copy the code
print("id(k): ".id(k))
print("id(j): ".id(j))
Copy the code
id(k): 4387308928
id(j): 4387308800
Copy the code
Here we swap the values of the two variables kj:
k, j = j, k # Exchange of variable values
Copy the code
Assign the value of j (already 5) to k; Assign the value of the variable (already 9) to the variable j;
print("k =",k)
print("j =",j)
Copy the code
k = 5
j = 9
Copy the code
print("id(k): ".id(k))
print("id(j): ".id(j))
Copy the code
id(k): 4387308800
id(j): 4387308928
Copy the code
By comparing the memory addresses of the two variables before and after swapping, we find that the memory addresses are swapped, that is, the variables have been swapped
In other programming languages, an exchange of values is implemented (assuming two variables a and B are already defined) :
temp = a # a assigns the intermediate variable temp
a = b The value of # b is assigned to variable A
b = temp The value of # temp is assigned to variable B
Copy the code
Equality and sameness of variables
First, we must declare equality and sameness in Python, which are two different concepts, as illustrated by examples
number1 = 88
number2 = 88
Copy the code
id(number1)
Copy the code
4387311456
Copy the code
id(number2)
Copy the code
4387311456
Copy the code
Determine whether two variables are equal: use ==
number1 == number2
Copy the code
True
Copy the code
Determine if two variables are the same: use is
number1 is number2
Copy the code
True
Copy the code
The result is True, so number1 and number2 are the same object
list1 = "hello python"
list2 = "hello python"
Copy the code
list1 == list2 # is equal
Copy the code
True
Copy the code
list1 is list2 # is not the same
Copy the code
False
Copy the code
The above results show that list1 and list2 are equal, but not identical.
id(list1)
Copy the code
4444494000
Copy the code
id(list2)
Copy the code
4444495024
Copy the code
By looking at their memory addresses, I found that they really are different, so they must be two different objects.
Let’s look at one last case:
number3 = 1000
number4 = 1000
Copy the code
number3 == number4 # is equal
Copy the code
True
Copy the code
number3 is number4 # Not even!
Copy the code
False
Copy the code
When we look at the memory addresses of the two objects, we find that they are really different, so they must not be the same object
id(number3)
Copy the code
4444408880
Copy the code
id(number4)
Copy the code
4444409104
Copy the code
Why is that? We’ll find out later.