Python Variable Type
The value that the variable is stored in memory. This means that there is a space in memory when creating variables.
Based on the data type of the variable, the interpreter allocates the specified memory and determines what data can be stored in memory.
Therefore, variables can specify different data types, which can store integers, decimals, or characters.
Assigning values to variables
Variables in Python do not need to be declared, and the assignment of variables is the process of declaring and defining variables.
Each variable is created in memory and includes information about the identity, name, and data of the variable.
Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.
The equals sign (=) is used to assign a value to a variable.
The left side of the equals sign (=) operator is a variable name, and the right side of the equals sign (=) operator is the value stored in the variable. For example:
#!/usr/bin/python#-*-Coding:utf-8-*-counter = 100 # Assignment integer variable miles = 1000.0 # floating-point name = "John" # string print counterprint mi Lesprint Name
In the above example, 100,1000.0 and "John" are assigned to the COUNTER,MILES,NAME variable respectively.
Executing the above program will output the following results:
1001000.0John
Assigning values to multiple variables
Python allows you to assign values to multiple variables at the same time. For example:
A = b = c = 1
The above example creates an integer object with a value of 1 and three variables allocated to the same memory space.
You can also specify multiple variables for multiple objects. For example:
A, b, C = 1, 2, "John"
For the above example, two integer objects 1 and 2 are assigned to variables A and B, and the string object "John" is assigned to variable C.
Python assignment operator
The following assumes that the variable A is 10 and the variable B is 20:
The following example shows the operation of all Python assignment operators:
#!/usr/bin/pythona = 21b = 10c = 0c = a + bprint "line 1-value of C are", CC + = Aprint "line 2-value of C is", C C *= Aprint "line 3-value of C are", C C/= a print "line 4-value of C are", c C = 2c%= Aprint "line 5-value of C is", CC **= Aprint "line 6-value of C are", CC//= aprint "line 7-value of C is", C
The result of the above example output:
Line 1-value of C are 31Line 2-value of C is 52Line 3-value of C are 1092Line 4-value of C is 52Line 5-value of C is 2Line 6-value of C are 2097152Line 7-value of C is 99864