Read Catalogue
- A. Variable
- Two. Data type
- 2.1 What are data types and data type classifications
- 2.2 Standard data types:
- 2.2.1 Digital
- 2.2.1.1 Integral type:
- 2.2.1.2 Long Integer type:
- 2.2.1.3 Boolean bool:
- 2.2.1.4 Floating point float:
- 2.2.1.5 plural complex:
- 2.2.1.6 number-related built-in functions
- 2.2.2 String
- 2.2.2.1 String Creation
- 2.2.2.2 String Common operations
- 2.2.2.3 Character factory function str ()
- 2.2.3 List
- 2.2.3.1 List Creation
- 2.2.3.2 List Common operations
- 2.2.3.3 List Factory function lists ()
- 2.2.4 tuples
- 2.2.4.1-Tuple creation
- 2.2.4.2-tuple common operation
- 2.2.4.3 Tuples factory function tuple ()
- 2.2.5 Dictionary
- 2.2.5.1 Dictionary Creation
- 2.2.5.2 Dictionary Common Operations
- 2.2.5.3 Dictionary factory function dict ()
- 2.2.6 Collection
- 2.2.6.1 Collection Creation
- 2.2.6.2 Collection Common operations: relational operations
- 2.2.6.3 Set Factory function set ()
- 2.2.7 bytes Type
- 2.2.8 data type conversion built-in functions summary
- Three. Operators
- Four. Summary of standard data type features
A. Variable
1 What is a variable declaration variable
#变量名 = variable value age=18gender1= ' Male ' gender2= ' female '
2 Why should I have a variable
Variable action: "Change" = Change, "volume" and "metering/saving State"
The operation nature of the program is a series of state changes, the purpose of the variable is to save the state, the change of the value of the variable is the result of the program running different.
For example: CS Shootout, a person's life can be expressed as life=active expression of survival, when a certain condition is met after modifying the variable life=inactive represents death.
3 types and objects of variable values
The program needs to deal with a lot of states, so there are different types of variable values, x= ' Egon ', variable value ' Egon ' stored in memory, bound by a name X,Variable value isWe want to store theData。
All data in Python is built around the concept of objects that contain some basic data types: numbers, strings, lists, tuples, dictionaries, etc.
All the data stored in the program is an object,
an object (such as A=1) is:
an identity (ID)
a type
a value (viewed by variable name a)
1 The type of the object is also referred to as the category of the object, and Python customizes the type-specific method for each type, greatly facilitating the developer's handling of the data
2 Creating an object of a particular type is also known as creating the type of aExample,Factory FunctionsThe concept comes from this
4 mutable objects and immutable objects
Once the instance is created, the identity and type are immutable,
If the value is not modifiable, the object is immutable
If the value can be modified, it is a mutable object
5 Container Objects
An object contains a reference to another object, which is called a container or collection
6 properties and methods for objects
A property is the value of an object, which is a function that will perform certain operations on the object itself when called, using the. operator to access the properties and methods of the object, such as
A=3+4j
A.real
b=[1,2,3]
B.append (4)
7 Identity comparison, type comparison, value comparison
X=1
Y=1
X is y #x与y是同一个对象 and is a comparison of ID, which is the identity
Type (x) is type (y) #对象的类型本身也是一个对象, so you can compare the identities of two object types with IS
x = = y #== compares the values of two objects for equality
7 naming conventions for variables
- Variable naming rules follow the naming rules for identifiers, as described in the second article
8 Assigning operations to variables
- The difference from the C language is that the variable assignment operation has no return value
- Chained assignment: y=x=a=1
- Multi-value assignment: x,y=1,2 x,y=y,x
- Incremental assignment: X+=1
Two. Data type 2.1 What is data type and data type classification
The essence of the program is to drive the computer to deal with changes in various states, which are divided into many kinds
For example, the League of Legends Game, a character has a name, money, grade, equipment and other characteristics, everyone will think of the first time to say
Name: Demacia------------> string
Money: 10000 ------------> Digital
Rating: ------------> Digital
Equip: shoes, day cloak, Langton trillion----> list
(It is the variables that record these characters, the real existence of these attributes are the values of the variables, and the different attributes require different types of values)
Data types in Python
Python uses the object model to store data, each data type has a built-in class, and each new data is actually initialized to generate an object, that is, all the data is an object
Object Three Properties
- Identity: Memory address, can be obtained with ID ()
- Type: Determines what type of value the object can hold, what action to perform, what rules to follow, and the type () to get
- Value: Real data saved by the object
Note: We are defining the data type, just like this: X=1, internally generated 1 This memory object is automatically triggered and we don't have to care
strings, numbers, lists, and so on are all data types (used to describe a State or feature) and there's a lot of other data that you need to define different data types to handle different kinds of data.
Standard type |
Other types |
Digital |
Types type |
String |
Null |
List |
File |
Meta-group |
Collection |
Dictionary |
Functions/Methods |
|
Class |
|
Module |
2.2 Standard data type: 2.2.1 Digital
Definition: a=1
Characteristics:
1. Only one value can be stored
2. Once defined, cannot be changed
3. Direct Access
Categories: Integer, Long, Boolean, floating point, plural
2.2.1.1 Integral type:
Python's integer is equivalent to Long in C, and integers in Python can be expressed in decimal, octal, and hexadecimal notation.
>>> 1010 ---------> Default decimal >>> oct (Ten) ' 012 ' ---------> Octal represents an Integer, the value preceded by a prefix "0" > >> hex ' 0xa ' ---------> hexadecimal denotes integer, prefix 0X or 0x before the number
The difference between python2.* and python3.* about integral type
python2.*
On a 32-bit machine, the number of integers is 32 bits and the value range is -2**31~2**31-1, which is -2147483648~2147483647
On a 64-bit system, the number of integers is 64 bits and the value range is -2**63~2**63-1, which is -9223372036854775808~9223372036854775807
python3.* Plastic Length Unlimited
Integer factory function int ()
python2.7python3.52.2.1.2 Long Integer type:
python2.*:
Unlike the C language, Python's long integer does not refer to the positioning width, which means that Python does not limit the size of a long integer value.
But in fact, because the machine memory is limited, so we use the long integer value can not be infinitely large.
How do we distinguish between long integers and integer values in the process of use?
It is common practice to add a capital letter L or lowercase l to the trailing digits to indicate that the integer is long, for example:
A = 9223372036854775808L
Note that, since Python2, Python automatically converts integer data to a long integer if an overflow occurs.
So the fact that there is no letter L behind Long data is not going to cause serious consequences.
python3.*
Long integer, integral type Unified to integral type
View2.2.1.3 Boolean bool:
True and False
1 and 0
2.2.1.4 Floating point float:
Python's floating-point number is a decimal number in mathematics, similar to a double in the C language.
In operations, the result of an integer and floating-point operation is a floating-point number
Floating-point numbers, which are decimals, are called floating-point numbers because, when represented by scientific notation,
The decimal position of a floating-point number is variable, for example, 1.23*109 and 12.3*108 are equal.
Floating-point numbers can be written in mathematical notation, such as 1.23,3.14,-9.01, and so on. But for very large or very small floating-point numbers,
We have to use scientific notation to replace 10 with E, 1.23*109 is 1.23e9, or 12.3e8,0.000012
Can be written in 1.2e-5, and so on.
Integers and floating-point numbers are stored internally in different ways, and integer operations are always accurate, and floating-point arithmetic may have
The rounding error.
2.2.1.5 plural complex:
The complex number consists of real and imaginary parts, the general form is X+yj, where x is the real part of the complex, and Y is the imaginary part of the complex, where x and y are real numbers.
Note that the letter J of the imaginary part can be case-sensitive,
>>> 1.3 + 2.5j = = 1.3 + 2.5JTrue
2.2.1.6 number-related built-in functions
2.2.2 String
Definition: It is a collection of ordered characters used to store and represent basic textual information, "' or" "or" ' "" that contains what is called a string
Characteristics:
1. Only one value can be stored
2. Non-volatile
3. Define the character set in left-to-right order, the subscript is accessed sequentially from 0, ordered
Add:
1. Both single and double quotation marks cannot suppress the meaning of special characters, if you want all the characters in quotation marks to nonspacing special meaning, precede the quotation marks with R, such as Name=r ' L\THF '
2.2.2.1 String Creation
' Hello World '
2.2.2.2 String Common operations
Remove whitespace
Segmentation
Length
Index
Slice
2.2.2.3 Character factory function str ()String Factory functionsthe difference between str function isdigit, isdecimal and isnumeric in Python2.2.3 List
Definitions: [] separated by commas, by index, holding various data types, each position represents an element
Characteristics:
1. can store multiple values
2. Can modify the value of the specified index position, variable
3. define list elements in left-to-right order, order access from 0, ordered
2.2.3.1 List Creation
list_test=[' LHF ', ' OK '
Or
List_test=list (' abc ')
Or
List_test=list ([' LHF ', ' OK ')
2.2.3.2 List Common operations
Index
Slice
Additional
Delete
Length
Slice
Cycle
Contains
2.2.3.3 List Factory function lists ()View
2.2.4 tuples
Definition: Similar to a list, except [] Change to ()
Characteristics:
1. can store multiple values
2. Non-volatile
3. define the tuple elements in left-to-right order, with the subscript starting from 0 sequential access, ordered
2.2.4.1-Tuple creation
ages = (11, 22, 33, 44, 55)
Or
ages = tuple ((11, 22, 33, 44, 55))
2.2.4.2-tuple common operation
Index
Slice
Cycle
Length
Contains
2.2.4.3 Tuple factory function tuple () 2.2.5 dictionary
Definition: {key1:value1,key2:value2},key-value structure, key must be hash
Characteristics:
1. can store multiple values
2. Can modify the value of the specified key, variable
3. No Order
2.2.5.1 Dictionary Creation
person = {"Name": "SB", ' Age ': 18}
Or
person = dict (name= ' SB ', age=18)
Person = dict ({"Name": "SB", ' Age ': 18})
person = dict ([' Name ', ' SB '],[' age ', 18])
{}.fromkeys (seq,100) #不指定100默认为None
Attention:
>>> Dic={}.fromkeys ([' K1 ', ' K2 '],[]) >>> dic{' K1 ': [], ' K2 ': []}>>> dic[' K1 '].append (1) > >> dic{' K1 ': [1], ' K2 ': [1]}
2.2.5.2 Dictionary Common Operations
Index
New
Delete
Key, value, key-value pairs
Cycle
Length
2.2.5.3 Dictionary factory function dict () 2.2.6 Collection
Definition: A collection of different elements, a set of unordered hash values that can be used as a dictionary key
Characteristics:
1. The purpose of the collection is to store different values together, and to do relational operations between different sets, without having to dwell on a single value in the collection
2.2.6.1 Collection Creation
{1,2,3,1}
Or
Define a mutable collection set
>>> set_test=set (' Hello ')
>>> Set_test
{' L ', ' o ', ' e ', ' H '}
Change to immutable collection Frozenset
>>> F_set_test=frozenset (set_test)
>>> F_set_test
Frozenset ({' L ', ' e ', ' h ', ' O '})
2.2.6.2 Collection Common operations: relational operations
Inch
Not in
= =
! =
<,<=
>,>=
|,|=: The collection
&.&=: Intersection
-,-=: Difference Set
^,^=: Symmetric differential
2.2.6.3 Set Factory function set ()View
2.2.7 bytes Type
Definition: Storage 8bit integer, data based on network transmission or memory variables stored to the hard disk need to be converted to bytes type, string predecessor B for the bytes type
>>> x ' Hello sb ' >>> x.encode (' gb2312 ') b ' Hello SB '
2.2.8 data type conversion built-in functions summary
Note: True to acsii table UNICHR in python2.7 more than CHR scope, python3.* in CHR built in Unichar
Three. Operators
1. Arithmetic operation:
2. Comparison operation:
3. Assignment Operation:
4, bit operation:
Note: ~ Example: 6 Explanation: Multiply the binary number +1 by-1, i.e. ~x =-(X+1),-(101 + 1) = 110
A bitwise reversal can only be used in front of a number. So writing 3+~5 can get the result-3, it's wrong to write a.
5. Logical Operation:
and annotations:
- In Python, and and or to perform Boolean logic calculations, as you would expect, but they do not return Boolean values, but instead return one of the values they actually compare.
- The value of a left-to-right calculus expression in a Boolean context, and returns the last value if all the values in the Boolean context are true.
- If a value in the Boolean context is False, and returns the first false value
or annotations:
- When using or , the left-to-right calculus is evaluated in a Boolean context, just like and. If a value is true,or returns the value immediately
- If all values are false,or returns the last False value
- Note that or in a Boolean context, the expression calculus continues until the first true value is found, and then the remaining comparison values are ignored
And-or used in combination:
- Combining the two preceding grammars, reasoning can be.
- To enhance the readability of the program, it is best to be associated with parentheses, for example:
(1 and ' X ') or ' Y '
6. Member Arithmetic:
7. Identity operations
8. Operator Precedence: top-down, high-to-low priority
Four. Summary of standard data type features
Differentiate by number of stored values
Scalar/Atomic Type |
Number, string |
Container type |
list, tuple, dictionary |
By Variable immutable distinction
Variable |
List, dictionary |
Not variable |
Numbers, strings, tuples |
Differentiate by Access order
Direct access |
Digital |
Sequential access (sequence type) |
String, list, tuple |
Key value access (mapping type) |
Dictionary |
Day6 arrays, tuples, dictionaries