Variable definition: variable is an area of computer memory, storing the value within the specified range, the value can be changed, the popular saying variable is to give the data a name.
Second, the variable naming rules:
1. Variable names consist of letters, numbers, underscores
2. Numbers cannot begin
3. Keywords cannot be used, example: a,a1,a_1
Three, the assignment of variables:
1. Variables in Python do not need to be declared, and the assignment of variables is the process of declaring and defining variables.
2. Each variable must be assigned before it is used, and the variable will be created after the variable is assigned;
3. The equal 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 (=) operator is the value stored in the variable. Example: A=1
Assign values to four or more variables:
Python allows assigning values to multiple variables at the same time.
For example, a = b = c = 1 You can also specify multiple variables for multiple objects.
For example: A2,B2,C2 = "zz", 18,1
V. Classification of variables:
1. Immutable variable: number, tuple (), string (when the value changes, it points to a new address)
2. mutable variables: list [], Dictionary {} (value changed, id unchanged)
VI. Standard data types:
Numbers (number): Shaping, long shaping (123L, beyond int automatically turns into long), floating-point, Complex Type (a=1j)
String (String): "" "" "" "" "
List: [], Variable value
Tuple (tuple): (), value is not variable
Dictionary (dictionary): {}
6.1. Numeric type: number
Numeric data types are used to store numeric values. is an immutable data type (immutable variable), which means that changing the numeric data type assigns a new object. When a value is specified, the number object is created: num = 123 Python supports four different numeric types:
1. Int (signed integral type)
2. Long (longer integer [also can represent octal and hexadecimal])
3. Float (float type)
4. Complex (plural)
6.2. Strings: String
A string is a series of characters consisting of numbers, letters, and underscores.
The plus sign (+) is a string join operator, and an asterisk (*) is a repeating operation.
6.3. String formatting:
Python supports the output of formatted strings.
The most basic usage is to insert a value into a string that has the string format of%s.
6.4. List: Lists
A list can accomplish the data structure implementation of most collection classes. It supports characters, numbers, and even strings that can contain lists (so-called nesting).
The plus sign (+) is the list join operator, and the asterisk (*) is a repeating operation.
Common list operations:
List.append (obj) adds a new object at the end of the list
List.insert (index, obj) inserts an object into the list
List.index (obj) find the index position of the first occurrence of a value from the list
List.count (obj) counts the number of occurrences of an element in a list
List.extend (SEQ) appends multiple values from another sequence at the end of the list (the original list is expanded with a new list)
Del (List[index]) Delete an element
List.remove (obj) deletes the first matching element
List.pop (index) removes an element from the list (the last element by default) and returns the value of the element
List.sort () sort the original list
List.reverse () reverse list of elements
6.5, Ganso: Tuple
A tuple is identified with a "()", similar to a list, but the tuple is an immutable variable, and the element cannot be assigned two times, which is equivalent to a read-only list.
The following example:
>>> list=[1,2,3]
>>> tuple= (4,5,6)
>>> list[0]=10
>>> list [10, 2, 3]
>>> tuple[0]=40
Traceback (most recent):
File "<pyshell#26>", line 1, in <module>
Tuple[0]=40
TypeError: ' Tuple ' object does not support item assignment
6.6. Dictionary: Dictionary
A dictionary consists of an index (key) and its corresponding value (value), which is a Python unique mapping type.
Dictionaries are also referred to as associative arrays or hash tables.
Keys () returns a list of values () that return a value list, items () returns a tuple of key-value pairs, and Has_key (key) returns whether the key is contained.
Two important points to keep in mind:
1. The keys in the dictionary must be unique, but the values do not have to be. When created, if the same key is assigned a value of two times, the latter value is remembered;
2. The key must be immutable and can be used as a number, string, or tuple, but it is not possible to use a list.
Two ways to create a dictionary:
1. Use the Factory Method Dict () to create a dictionary: Example: a=dict ([' Name ', ' zz '],[' age ', 18])
2. Built-in Method Fromkeys (), the element in the dictionary has the same value, the default is None: B={}.fromkeys ((' x ', ' Y '), 1)
View Traversal Dictionaries
View the values in the dictionary:
1. Dict[key]
2. Dict.get (Key,value)
Two ways to traverse a dictionary:
1. For key in Dict:
Print key, Dict[key]
2. For key, value in Dict.items ():
Print key, value
Add change to delete a dictionary:
Dict[key]=value
Dict1.update (DICT2) #更新dict2到dict1, no Add, repeat overrides
Del Dict[key] Dict.pop (key)
Dict.clear ()
7. JSON module
Import JSON
Json.dumps () #编码, the dictionary turns into a string
Json.loads () #解码, the string is converted into a dictionary
8. Data type Conversion
9. Sequence operation
Sequences include: The two characteristics of a string, list, and tuple sequence are index and slice indexes, get specific element slices based on index, get sequence fragments
Sequence Operation application:
Finally, for a small example of a job, the data exists in a list, such as:
list=[' 23.34% ', ' 30.88% ', ' 15.9% '] need to compare the values in the list with the known data, in order to facilitate comparisons, think of removing the last percent, and then converting the string to float, it is easy to implement with slices: >>> list =[' 23.34% ', ' 30.88% ', ' 15.9% ']
>>> float (list[0][:-1])
23.34
Common sequence Operations:
Len () to find the length
+ Connect two sequences
* Repeating sequence elements
In determines whether the element is in sequence
Max () returns the maximum value
MIN () returns the minimum value
CMP () Comparison of two sequences
Variables and data types in Python