Python variable type
Variable values stored in memory. This means that will open up a space in the memory when you create a variable.
Based on the data type of the variable, the interpreter will allocate the specified memory, and decide what data can be stored in memory.
Thus, the variables can specify different data types, these variables can store integer, decimal, or character.
Variable assignment
Python variables do not need to type an assignment statement.
Each variable is created in memory, including all logos, names and data of variable information.
Each variable must be assigned before use, variable assignment after the variable will be created.
Equal sign (=) is used to assign values to variables.
Equal sign (=) operator on the left is a variable name, an equal sign (=) operator on the right is the value stored in the variable. E.g:
#!/usr/bin/python # -*- coding: UTF-8 -*- counter = 100 # 赋值整型变量 miles = 1000.0 # 浮点型 name = "John" # 字符串 print counter print miles print name
The above example, 100,1000.0 and "John" are assigned to the counter, miles, name variable.
The above program will output the following results:
100 1000.0 John
A plurality of variable assignment
Python allows you to assign multiple variables simultaneously. E.g:
The above examples, create an Integer object, and a value of 1, three variables are assigned to the same memory space.
You can also specify multiple variables into multiple objects. E.g:
The above examples, two integer objects 1 and 2 is assigned to the variable a and b, string object "john" is assigned to the variable c.
Standard data types
Stored data can be a variety of types in memory.
For example, person.s age as a value stored in his or her address is stored in alphanumeric characters.
Python has some standard used to define the type of operation, and they may be for each of them the storage method.
Python has five standard data types:
- Numbers (Digital)
- String (String)
- List (list)
- Tuple (tuple)
- Dictionary (dictionary)
Python figures
Numeric data type is used to store values.
They are immutable data types, which means changing the numeric data type will be assigned a new object.
When you specify a value, Number object is created:
var2 = 10
You can also use the del statement to delete some object.
del statement syntax is:
You can refer to by using the del statement to delete single or multiple objects. E.g:
del var_a, var_b
Python supports four different types of figures:
- int (a signed integer)
- long (long integers [can also represent octal and hexadecimal])
- float (float)
- complex (complex)
Some examples of numeric types:
int | long | float | complex |
---|---|---|---|
10 | 51924361L | 0.0 | 3.14j |
100 | -0x19323L | 15.20 | 45.j |
-786 | 0122L | -21.9 | 9.322e-36j |
080 | 0xDEFABCECBDAECBFBAEl | 32.3 + e18 | .876j |
-0490 | 535633629843L | -90. | -.6545 + 0J |
-0x260 | -052318172735L | -32.54e100 | 3e + 26J |
0x69 | -4721885298529L | 70.2-E12 | 4.53e-7j |
- You can also use a long integer lowercase "L", but it is recommended that you use an uppercase "L", to avoid the number "1" confusion. Python uses "L" to display the long integer.
- Python also supports complex numbers, complex numbers by the real and imaginary parts, you can use a + bj, or complex (a, b) that the real part and an imaginary part b is a floating-point
Python strings
String or strings (String) is a string of characters consists of numbers, letters, underscores thereof.
Usually referred to as:
It is a programming language indicates the data type of text.
Python string list are two kinds of values in order:
- From left to right index defaults to zero, the maximum range is less string length 1
- From right to left starting at -1 index default, the maximum range is the beginning of a string
If you want to get some real substring, you can use the variables [under the header: Last subscript], you can intercept the corresponding string, where the subscript 0 is measured from the beginning, which can be positive or negative, under mark can be empty means to take the head or tail.
such as:
s [1: 5] is the result of love.
When using a colon-separated strings, python returns a new object that contains the results in order to offset the contents of this continuous identification, beginning on the left is included under the border.
The above results include the s [1] the value of l, the maximum range is not taken to include the border is s [5] the value of p.
Plus sign (+) is the string concatenation operator, the asterisk (*) is repeated operations. The following examples:
# - * - Coding: UTF-8 - * -
str = 'Hello World!'
print str # Output the full string
print str [0] # output string of the first character
# String output string third to fifth between: print str [5 2]
print str [2:] # output from the beginning of the third character string
print str * 2 # twice the output string
print str + "TEST" # output connection string
Examples of the above output:
H
llo
llo World!
Hello World! Hello World!
Hello World! TEST
Python list
List (list) is the most frequently used Python data types.You can complete the list data structure to achieve most of the collection class. It supports characters, numbers, strings and even contains a list (called nesting).
List with a [] logo. Python is the most common type of complex data. See the code to understand.
List segmentation deserve to be used in variable [under the header: Last subscript], you can intercept the appropriate list, from left to right by default index of 0, default index from right to left starting at -1, the subscript may blank means to take the head or tail.
Plus sign (+) is a list of the concatenation operator, the asterisk (*) is repeated operations. The following examples:
# - * - Coding: UTF-8 - * -
list = [ 'abcd', 786, 2.23, 'john', 70.2]
tinylist = [123, 'john']
a complete list of output print list #
print list [0] # the first element of the list output
print list [1: 3] # output of the second to the third element
print list [2:] # output all elements from the beginning to the end of the third list
print tinylist * 2 # output list twice
print list + tinylist list # print portfolio
Examples of the above output:
abcd
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
[ 'Abcd', 786, 2.23, 'john', 70.2, 123, 'john']
Python tuple
Tuple is another data type, similar to the List (list).
Tuple with "()" logo. Internal elements separated by commas. But not the second tuple assignment, equivalent to read-only list.
# - * - Coding: UTF-8 - * -
tuple = ( 'abcd', 786, 2.23, 'john', 70.2)
tinytuple = (123, 'john')
print tuple # output complete tuple
print tuple [0] # the first element of the output tuple
print tuple [1: 3] # output of the second to the third element
print tuple [2:] # output all elements from the beginning to the end of the third list
print tinytuple * 2 # output tuple twice
print tuple + tinytuple # tuple combination of print
Examples of the above output:
abcd
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
( 'Abcd', 786, 2.23, 'john', 70.2, 123, 'john')
The following is a tuple invalid because tuples are not allowed to update. The list is updated to allow:
# - * - Coding: UTF-8 - * -
tuple = ( 'abcd', 786, 2.23, 'john', 70.2)
list = [ 'abcd', 786, 2.23, 'john', 70.2]
tuple [2] = 1000 # tuple is illegal Application
list [2] = 1000 # list is legitimate applications
Python element dictionary
Dictionary (dictionary) is in addition to python than among the list of the most flexible type of built-in data structures. The list is ordered binding objects, dictionaries are unordered collections of objects.
The difference between the two is: among the elements of the dictionary is accessed by a key, rather than through an offset access.
Dictionary use "{}" logo. Dictionary by the index (key) and its corresponding value value components.
# - * - Coding: UTF-8 - * -
dict = {}
dict [ 'one'] = "This is one"
dict [2] = "This is two"
tinydict = { 'name': 'john', 'code': 6734, 'dept': 'sales'}
print dict [ 'one'] # output key is 'one' value
print dict [2] # key output value of 2
print tinydict # full output dictionary
print tinydict.keys () # Output all keys
print tinydict.values () # output all values
The output is:
Python data type conversion
Sometimes, we need to built-in data type conversion, data type conversion, you only need to type the data as a function name.
Several built-in functions can perform conversions between data types. These functions return a new object that represents the converted value.
function | description |
---|---|
int (x [, base]) | Converts x to an integer |
long (x [, base]) | Converts x to a long integer |
float (x) | The transition to a floating-point number x |
complex (real [, imag]) | Create a complex |
str (x) | The object is converted to a string x |
repr (x) | The object x is converted to a string expression |
eval (str) | It used to calculate string valid Python expression and returns an object |
tuple (s) | The sequence s into a tuple |
list (s) | The sequence s is converted to a list |
set (s) | Converted to variable set |
dict (d) | Create a dictionary. d must be a sequence of (key, value) tuple. |
frozenset (s) | Converted to immutable collection |
chr (x) | Will convert an integer to a character |
unichr (x) | An integer is converted to Unicode characters |
ord (x) | Convert a character to its integer value |
hex (x) | An integer is converted to a hexadecimal string |
oct (x) | An integer is converted to an octal string |