Python variable type
Python variable type
The value of the variable stored in the memory. This means that a space is opened in the memory when a variable is created.
For variable-based data types, the interpreter allocates the specified memory and determines what data can be stored in the memory.
Therefore, variables can specify different data types, which can store integers, decimals, or characters.
Variable assignment
Variables in Python do not need to be declared. Variable assignment is a process of variable declaration and definition.
Each variable created in the memory contains the variable identifier, name, and data.
Each variable must be assigned a value before it can be used.
Equals sign (=) is used to assign values to variables.
On the left side of the equal sign (=) operator is a variable name, and on the right side of the equal sign (=) operator is the value stored in the variable. For example:
#! /Usr/bin/python #-*-coding: UTF-8-*-counter = 100 # assign a value to the integer variable miles = 1000.0 # Floating Point name = "John" # string print counterprint milesprint name
Running instance?Download
In the above example, 100,100 0.0 and "John" are assigned to the counter, miles, and name variables respectively.
The following results are output when the above program is executed:
1001000.0John
Assign values to multiple variables
Python allows you to assign values to multiple variables at the same time. For example:
A = B = c = 1
For the above instance, create an integer object with a value of 1. The three variables are allocated to the same memory space.
You can also specify multiple variables for multiple objects. For example:
A, B, c = 1, 2, "john"
In the above example, the two integer objects 1 and 2 are allocated to variables a and B, and the string object "john" is allocated to the variable c.
Standard Data Type
Data stored in the memory can be of multiple types.
For example, the person. s age is stored as a numerical value and his or her addresses are stored as alphanumeric characters.
Python has some standard types used to define operations, and they are possible for each of them to store methods.
Python has five standard data types:
- Numbers (number)
- String (String)
- List)
- Tuple (tuples)
- Dictionary)
Python numbersThe numeric data type is used to store numeric values.
They are unchangeable data types, which means that changing the numeric data type will allocate a new object.
When you specify a value, the Number object will be created:
Var1 = 1
Var2 = 10You can also use the del statement to delete some object references.
The syntax of the del statement is:
Del var1 [, var2 [, var3 [..., varN]You can use the del statement to delete one or more objects. For example:
Del var
Del var_a, var_ BPython supports four different numeric types:
- Int (signed integer)
- Long (long integer [can also represent octal and hexadecimal])
- Float (float type)
- Complex (plural) instance
Some numeric instances:
Int |
Long |
Float |
Complex |
10 |
51924361L |
0.0 |
3.14j |
100 |
-0x19323L |
15.20 |
45. j |
-786 |
0122L |
-21.9 |
9.322e-36j |
080 |
0 xDEFABCECBDAECBFBAEl |
32.3 + e18 |
. 876j |
-0490 |
535633629843L |
-90. |
-. 6545 + 0J |
-0x260 |
-052318172735L |
-32.54e100 |
3e + 26J |
0x69 |
-4721885298529L |
70.2-E12 |
4.53e-7j |
- Long integers can also use lowercase "L", but we recommend that you use uppercase "L" to avoid confusion with numbers "1. Python uses "L" to display long integers.
- Python also supports the complex number. The complex number consists of the real number and the virtual number. It can be expressed by a + bj or complex (a, B). The real part a and virtual Part B of the complex number are float.
Python stringA String is a String of numbers, letters, and underscores.
It is generally recorded:
S = "a1a2 · an" (n> = 0)It is the data type that represents text in programming languages.
The python string list has two values:
- From left to right index starts from 0 by default, and the maximum range is 1 less String Length
- From right to left index start from-1 by default. The maximum range is the start of the string.
If you want to obtain a substring, you can use the variable [header Subscript: tail subscript] to extract the corresponding string, where the subscript starts from 0, it can be a positive or negative number, and the subscript can be null to get the header or tail.
For example:
S = 'ilovepython'S [] returns love.
When a string separated by a colon is used, python returns a new object. The result contains the continuous content identified by the offset, and the start on the left contains the lower boundary.
The above result contains the value l of s [1], and the maximum obtained range does not include the upper boundary, that is, the value p of s [5.
The plus sign (+) is a String concatenation operator, and the asterisk (*) is a repeated operation. Example:
#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
Str = 'Hello World! '
Print str # output complete string
Print str [0] # The first character in the output string
Print str [2: 5] # output string between the third and fifth strings
Print str [2:] # output string starting from the third character
Print str * 2 # output string twice
Print str + "TEST" # output the connected stringOutput result of the above instance:
Hello World!
H
Llo
Llo World!
Hello World! Hello World!
Hello World! TEST
Python list
List is the most frequently used data type in Python.
The list can implement the data structure of most collection classes. It supports characters, numbers, strings, and even lists (so-called nesting ).
The list is identified. Is the most common Composite data type in python. You can see this code.
The variable [header Subscript: tail subscript] can also be used to split the list. The corresponding list can be truncated, starting from left to right index with 0 by default, from right to left, the index starts from-1 by default. The subscript can be null to indicate that the header or tail is obtained.
The plus sign (+) is a list join operator, and the asterisk (*) is a repeated operation. Example:
#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
List = ['abc', 786, 2.23, 'john', 70.2]
Tinylist = [123, 'john']
Print list # output complete list
Print list [0] # The first element in the output list
Print list [] # output the second to third elements
Print list [2:] # output all elements starting from the third to the end of the list
Print tinylist * 2 # output list twice
Print list + tinylist # print the list of combinations
Output result of the above instance:
['Abc', 786, 2.23, 'john', 70.2]
Abcd
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['Abc', 786, 2.23, 'john', 70.2, 123, 'john']
Python tuplesTuples are another data type, similar to List ).
The tuples are identified. Internal elements are separated by commas. However, an element cannot be assigned a value twice, which is equivalent to a read-only list.
#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
Tuple = ('abc', 786, 2.23, 'john', 70.2)
Tinytuple = (123, 'john ')
Print tuple # output complete tuples
Print tuple [0] # The first element of the output tuples
Print tuple [] # output the second to third elements
Print tuple [2:] # output all elements from the third end to the end of the list
Print tinytuple * 2 # output tuples twice
Print tuple + tinytuple # print the combination of tuples
Output result of the above instance:
('Abc', 786, 2.23, 'john', 70.2)
Abcd
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john ')
('Abc', 786, 2.23, 'john', 70.2, 123, 'john ')
The following is invalid because the tuples cannot be updated. The list can be updated:
#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
Tuple = ('abc', 786, 2.23, 'john', 70.2)
List = ['abc', 786, 2.23, 'john', 70.2]
Tuple [2] = 1000 # illegal application in the tuples
List [2] = 1000 # The list is valid
Python dictionaryA dictionary is the most flexible built-in data structure type in python except the list. A list is a combination of ordered objects, and a dictionary is a collection of unordered objects.
The difference between the two is that the elements in the dictionary are accessed by keys, rather than by offset.
The dictionary is identified. A dictionary consists of an index (key) and its corresponding value.
#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
Dict = {}
Dict ['one'] = "This is one"
Dict [2] = "This is two"
Tinydict = {'name': 'john', 'code': 6734, 'dept': 'sales '}
Print dict ['one'] # value of the output key 'one'
Print dict [2] # value of output key 2
Print tinydict # output the complete dictionary
Print tinydict. keys () # output all keys
Print tinydict. values () # output all values
Output result:
This is one This is two {'dept': 'sales', 'codec': 6734, 'name': 'john'} ['dept', 'code ', 'name'] ['sales', 6734, 'john']
Python data type conversionSometimes, we need to convert the built-in data types and convert the data types. You only need to use the data type as the function name.
The following built-in functions can be used to convert data types. These functions return a new object, indicating the converted value.
Function |
Description |
Int (x [, base]) |
Converts x to an integer. |
Long (x [, base]) |
Converts x to a long integer. |
Float (x) |
Convert x to a floating point number. |
Complex (real [, imag]) |
Create a plural number |
Str (x) |
Convert object x to a string |
Repr (x) |
Convert object x to an expression string |
Eval (str) |
Used to calculate a valid Python expression in a string and return an object |
Tuple (s) |
Converts the sequence s into a single tuples. |
List (s) |
Converts sequence s to a list. |
Set (s) |
Convert to a variable set |
Dict (d) |
Create a dictionary. D must be a sequence (key, value) tuples. |
Frozenset (s) |
Convert to an unchangeable set |
Chr (x) |
Converts an integer to a character. |
Unichr (x) |
Converts an integer to a Unicode character. |
Ord (x) |
Converts a character to an integer. |
Hex (x) |
Converts an integer to a hexadecimal string. |
Oct (x) |
Converts an integer to an octal string. |