One, Python variables
1. Python variables
- Variables in Python do not need to be declared.
- Each variable must be assigned a value before it is used, and the variable will be created after the variable is assigned.
- In Python, a variable is a variable. It has no type. What we call "type" is the type of the object in the memory that the variable refers to.
- Equal sign
=
is used to assign values to variables. - Equals
=
the left side is a variable name of the operator, the equal sign=
on the right operator is the value stored in the variable.
2. Multiple variable assignment
Two, standard data types
- Number
- String
- List
- Tuple (tuple)
- Set
- Dictionary
Is it ordered (whether there is a default digital index inside) :
Ordered : string, list, tuple
Unordered : collection, dictionary
Among the six standard data types of Python3 :
Immutable data (3) :
Number (number), String (string), Tuple (tuple);
Variable data (3) : List (list), Dictionary (dictionary), Set (collection).
Three, two common characteristics of ordered data types
1. Data slice
Variable [head subscript: tail subscript: step length]
The index value starts with 0, and -1 is the starting position from the end.
-
Single value type : take elements by index
-
Double value type : take interval by index
Note that the border on the right is not available.
If the value on the left is greater than the right, a space is output.
- Three-value type The
third number represents the step size, and the default is 1.
2. Data connection and duplication
-
Data connection : +
-
Data duplication : *
Four, Number (number)
- int
- float
- bool
- complex (plural)
The built-in type() function can be used to query the object type pointed to by the variable.
In Python3, True and False are defined as keywords, but their values are still 1 and 0, and they can be added to numbers.
Numerical operations
1. Python can assign values to multiple variables at the same time, such as a, b = 1, 2.
2. A variable can point to different types of objects by assignment.
3. Numerical division contains two operators: / returns a floating point number, // returns an integer.
4. In mixed calculations, Python will convert integers to floating point numbers.
Five, String string
1. Backslashes can be used to escape, and use r
can make backslashes not escaped.
2. Strings can be +
connected together with *
operators and repeated with operators.
3. There are two indexing methods for strings in Python, 0
starting from left to right, and starting from right to left -1
.
4. Strings in Python cannot be changed.
Python does not have a separate character type, a character is a string of length 1.
Unlike C strings, Python strings cannot be changed. Assigning a value to an index position, such as word[0] ='m' will cause an error.
Six, List list
The list is []
a list of elements written between square brackets and separated by commas.
1. List is written between square brackets, and the elements are separated by commas.
2. Like strings, lists can be indexed and sliced.
3. List can be spliced using + operator.
4. The elements in the List can be changed.
Unlike Python strings, the elements in the list can be changed.
Seven, Set (collection)
- A set is a sequence of unordered and non-repeating elements.
- The basic function is to test membership and delete duplicate elements.
- You can use braces
{}
orset()
create aggregate functions. - Note: Create an empty set must be used
set()
instead of{}
, as{}
is used to create an empty dictionary.
Eight, Dictionary (dictionary)
- Dictionaries are another very useful built-in data type in Python.
- 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 through keys, not offsets.
- A dictionary mapping type, a dictionary "{}" identifier, which is an unordered key (key): Value (value) pairs.
- The dictionary object requires that the key must use an immutable type.
- In the same dictionary, the key must be unique.
Dict constructor () can be constructed directly from the key-value dictionary of the sequence :
In addition, there are some types of dictionaries built-in functions, for example clear()
, keys()
, values()
and the like.
1. A dictionary is a mapping type, and its elements are key-value pairs.
2. The keywords of the dictionary must be immutable and cannot be repeated.
3. Create an empty dictionary for use {}
.
Nine, Tuple tuple
Tuples are similar to lists, except that the elements of tuples cannot be modified. Tuples are written in parentheses (), and the elements are separated by commas.
Although the elements of a tuple cannot be changed, it can contain mutable objects, such as lists.
Constructing tuples with 0 or 1 element is special, so there are some additional grammatical rules:
tup1 = () # 空元组
tup2 = (20,) # 一个元素,需要在元素后添加逗号
- Like strings, the elements of tuples cannot be modified.
- Tuples can also be indexed and sliced in the same way.
- Note the special grammatical rules for constructing tuples containing 0 or 1 elements.
- Tuples can also be spliced using the + operator.
Ten, Python data type conversion
Sometimes, we need to convert the built-in types of the data. For the conversion of data types, you only need to use the data type as the function name.
The following built-in functions can perform conversion between data types. These functions return a new object that represents the converted value.