First, the 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
Variable assignments in Python do not require a type declaration.
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-*- # Assignment integer variable # floating-point # string print counter Print milesPrint name
Assigning values to multiple variables
Python allows you to assign values to multiple variables at the same time. For example:
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:
1
In 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.
Standard data types
There are several types of data that can be stored in memory.
For example, a person's age can be stored in numbers, and his name can be stored in characters.
Python defines a number of standard types for storing various types of data.
Python has five standard data types:
- Numbers (digital)
- String (String)
- List (lists)
- Tuple (tuple)
- Dictionary (dictionary)
Python numbers
Numeric data types are used to store numeric values.
They are immutable data types, which means that changing the numeric data type assigns a new object.
When you specify a value, the number object is created:
VAR1 = 1
You can also use the DEL statement to delete references to some objects.
The syntax for the DEL statement is:
You can delete a reference to a single or multiple objects by using the DEL statement. For example:
1 var 2
Python supports four different types of numbers:
- int (signed integral type)
- Long (longer integer [can also represent octal and hexadecimal])
- Float (float type)
- Complex (plural)
Instance
Examples of some numeric types:
int |
Long |
float |
Complex |
10 |
51924361L |
0.0 |
3.14j |
- Long integers can also use lowercase l, but it is recommended that you use uppercase L to avoid confusion with the number 1. Python uses L to display the long integer type.
- Python also supports complex numbers, which are composed of real and imaginary parts, and can be represented by a + BJ, or complex (a, b), where both the real and imaginary part of a complex number are floating-point types.
Python string
A string or series (string) is a string of characters consisting of numbers, letters, and underscores.
Generally recorded as:
1 s= "a1a2 An "(n>=0)
It is the data type that represents the text in the programming language.
The Python string list has 2 order of values:
- Left-to-right index starts at default 0, with a maximum range of 1 less string lengths
- Right-to-left index starts with default-1, the maximum range is the beginning of the string
If you are going to implement a string from a string, you can use the [header subscript: tail subscript] to intercept the corresponding string, where the subscript is calculated starting from 0, can be positive or negative, subscript can be empty to take the head or tail.
[head subscript: Tail subscript] Gets the substring that contains the character of the header subscript, but does not contain the trailing subscript character.
Like what:
1 >>> s = ' abcdef '2 >>> s[1:5]3 ' BCDE '
When using a colon-delimited string, Python returns a new object that contains the contiguous content identified with the offset, and the beginning of the left contains the bottom bounds.
The result above contains the value B of s[1], and the maximum range taken does not include the trailing subscript , or the value F of s[5].
The plus sign (+) is a string join operator, and an asterisk (*) is a repeating operation. The following example:
1 #!/usr/bin/python2 #-*-coding:utf-8-*-3 4str = ' Hello world! '5 6 PrintStr#Output Full String7 PrintSTR[0]#the first character in the output string8 PrintStr[2:5]#A string between the third and fifth in the output string9 PrintStr[2:]#outputs a string starting from the third characterTen PrintSTR * 2#output String two times One PrintSTR + "TEST"#string for output connection
The result of the above example output:
1 Hello world! 2 H 3 Llo 4 Llo world! 5 Hello world! Hello world! 6 Hello world! TEST
Python list
The list is the most frequently used data type in Python.
A list can accomplish the data structure implementation of most collection classes. It supports characters, numbers, and strings that can even contain lists (that is, nesting).
The list is identified by the [], internally separated by commas, and is the most versatile composite data type of Python.
The cut of the value in the list can also be used to the variable [head subscript: Tail subscript], you can intercept the corresponding list, from left to right index default 0, starting from right to left index default-1, subscript can be empty to take the head or tail.
The plus + is the list join operator, and the asterisk * is a repeating operation. The following example:
1 #!/usr/bin/python2 #-*-coding:utf-8-*-3 4 List= [' Runoob ', 786, 2.23, ' John ', 70.2 ]5Tinylist = [123, ' John ']6 7 Print List #Output Complete list8 Print List[0]#the first element of the output list9 Print List[1:3]#output second to third elementTen Print List[2:]#outputs all elements from the third start to the end of the list One PrintTinylist * 2#output List two times A Print List+ tinylist#print a list of combinations
The output of the above example is:
1 [' Runoob ', 786, 2.23, ' John ', 70.2]2runoob3 [786, 2.23]4 [2.23, ' John ', 70.2]5 [123, ' John ', 123, ' John ']6 [' Runoob ', 786, 2.23, ' Jo HN ', 70.2, 123, ' John ']
Python tuples
A tuple is another data type, similar to a list.
The tuple is identified with a "()". The inner elements are separated by commas. However, tuples cannot be assigned two times, which is equivalent to a read-only list.
1 #!/usr/bin/python2 #-*-coding:utf-8-*-3 4tuple = (' Runoob ', 786, 2.23, ' John ', 70.2 )5Tinytuple = (123, ' John ')6 7 PrintTuple#Output Full tuple8 PrintTUPLE[0]#the first element of an output tuple9 PrintTuple[1:3]#outputs the second to third elementTen PrintTuple[2:]#outputs all elements from the third start to the end of the list One PrintTinytuple * 2#output tuple two times A PrintTuple + tinytuple#Print a group of tuples
The result of the above example output:
1 (' Runoob ', 786, 2.23, ' John ', 70.2)2runoob3 (786, 2.23)4 (2.23, ' John ', 70.2)5 (123, ' John ', 123, ' John ')6 (' Runoob ', 786, 2.23, ' John ', 70.2, 123, ' John ')
The following operations on tuples are not valid because tuples are not allowed to be updated. And the list is allowed to be updated:
1 # !/usr/bin/python 2 # -*-coding:utf-8-*- 3 4 tuple = (' Runoob ', 786, 2.23, ' John ', 70.2 )5list = [' Runoob ', 786, 2.23, ' J Ohn ', 70.2 ]6 tuple[2] = $ # in the tuple is illegal application 7list [2] = the list is valid
Python Dictionary
The Dictionary (dictionary) is the most flexible built-in data structure type in Python, except for lists . A list is an ordered collection of objects, and a dictionary is an unordered collection of objects.
The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.
The dictionary is identified with "{}". A dictionary consists of an index (key) and a value corresponding to it.
1 #!/usr/bin/python2 #-*-coding:utf-8-*-3 4Dict = {}5dict[' One ' = "This is one"6DICT[2] = "This is the"7 8tinydict = {' name ': ' John ', ' Code ': 6734, ' dept ': ' Sales '}9 Ten One Printdict[' One ']#the value of the output key is ' one ' A PrintDICT[2]#the value of the output key is 2 - PrintTinydict#output a complete dictionary - PrintTinydict.keys ()#Output All keys the PrintTinydict.values ()#Output All Values
The output is:
1 This is one 2 This is the 3 {' dept ': ' Sales ', ' Code ': 6734, ' name ': ' John '}4 [' dept ', ' Code ', ' name ']5 [' Sales ', 6734, ' John ']
Python Data type conversions
Sometimes, we need to convert the data-built type into the data type, and you just need to use the data type as the function name.
The following several built-in functions can perform conversions between data types. These functions return a new object that represents the value of the transformation.
function |
Description |
int (x [, Base]) |
Convert x to an integer |
Long (x [, Base]) |
Convert x to a long integer |
Float (x) |
Convert x to a floating-point number |
Complex (real [, Imag]) |
Create a complex number |
STR (x) |
Convert an object x to a string |
REPR (x) |
Convert an object x to an expression string |
eval (str) |
Used to evaluate a valid Python expression in a string and return an object |
Tuple (s) |
Converting a sequence s to a tuple |
List (s) |
Convert the sequence s to a list |
Set (s) |
Convert to mutable Collection |
Dict (d) |
Create a dictionary. D must be a sequence (key,value) tuple. |
Frozenset (s) |
Convert to immutable Collection |
Chr (x) |
Converts an integer to one character |
UNICHR (x) |
Converts an integer to a Unicode character |
Ord (x) |
Converts a character to its integer value |
Hex (x) |
Converts an integer to a hexadecimal string |
Oct (x) |
Converts an integer to an octal string |
5. Python basic Syntax-variables