Directory
- Variable
- Naming conventions
- Value types and reference types
- Operator
- Arithmetic operators
- Assignment operators
- Relational operators
- logical operators
- Member operators
- Identity operator
- Three major characteristics of objects
- Bitwise operators
Variable
A variable is an abstraction of a type of data entity, or a name for an entity of a type's data. The basic way to do this is var = xxx
. =
is an assignment operation, Note that Python does not require a variable declaration like Java
>>> [1,2,3,4,5,6]*3+[1,2,3]+[1,2,3,4,5,6][1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 2, 3, 4, 5, 6]>>> A = [1,2,3,4,5,6]>>> print(A)[1, 2, 3, 4, 5, 6]>>> B = [1,2,3]>>> A * 3 + B + A[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 2, 3, 4, 5, 6]>>> skill = [‘新月打击‘,‘苍白之瀑‘]
Variables can be convenient for our operation. variable names to be standardized
Naming conventions
- Python specifies that a variable can begin with a letter, an underscore, a letter, a number, an underscore
>>> 0a = 2SyntaxError: invalid syntax>>> a2 = ‘f‘>>> _2ab=‘1‘
- Python recognizes case, a differs from a
>>> bTraceback (most recent call last): File "<pyshell#12>", line 1, in <module> bNameError: name ‘b‘ is not defined>>> B[1, 2, 3]
- Python is a weakly typed language with no type restrictions for the same variable
>>> a = ‘1‘>>> a = 1>>> a = (1,2,3)>>> a = {1,2,3}
- System reserved keywords cannot be used as variable names
Value types and reference types
int, str, and tuple are value types that are immutable and each assignment opens up new space for storage. List, set, dict are reference types, each variable is similar to a "pointer", pointing to a fixed position, and modifying one affects the other.
>>> a = {1,2,3}>>> b = a>>> a = 3>>> print(b){1, 2, 3}>>> a = [1,2,3,4,5]>>> b = a>>> a[0] = ‘1‘>>> print(a)[‘1‘, 2, 3, 4, 5]>>> print(b)[‘1‘, 2, 3, 4, 5]
Further explain that the value type is immutable.
>>> a = ‘hello‘>>> id(a)2166410629392>>> a = a + ‘ python‘>>> id(a)2166410595440>>> ‘python‘[0]‘p‘>>> ‘python‘[0]=‘m‘Traceback (most recent call last): File "<pyshell#33>", line 1, in <module> ‘python‘[0]=‘m‘TypeError: ‘str‘ object does not support item assignment
function id()
to display the address of a variable in memory
>>> b = [1,2,3]>>> b.append(4)>>> print(b)[1, 2, 3, 4]>>> c = (1,2,3)>>> c.append(4)Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> c.append(4)AttributeError: ‘tuple‘ object has no attribute ‘append‘
Because tuples are immutable, there are no operations such as appending elements. List and so on can be added, deleted, modified.
>>> a = (1,2,3,[1,2,4])>>> a[2]3>>> a[3][1, 2, 4]>>> a[3][2]4>>> a = (1,2,3,[1,2,[‘a‘,‘b‘,‘c‘]])>>> a[3][2][1]‘b‘>>> a[2] = ‘3‘Traceback (most recent call last): File "<pyshell#26>", line 1, in <module> a[2] = ‘3‘TypeError: ‘tuple‘ object does not support item assignment>>> a[3][2] = ‘4‘>>> print(a)(1, 2, 3, [1, 2, ‘4‘])
In this combination example, the tuple is immutable and the list is variable, so a[3]
a constant address is stored and the contents of the address are variable
Operator
Arithmetic operators
Includes common +,-,*,/,%
, idempotent, and power-operated Python **
implementations. It should be noted +
, -
and in the *
processing of certain data structures meaning, such as stitching, difference sets, replication, etc.
>>> ‘hello‘+‘ world‘‘hello world‘>>> [1,2,3]*3[1, 2, 3, 1, 2, 3, 1, 2, 3]>>> 3 - 12>>> 3 / 21.5>>> 3 // 21>>> 5 % 21>>> 2 ** 24
Assignment operators
As the name implies, by some simple operation (not even), the data is assigned to a variable, the core of the operation must fall on the assignment
>>> c = 1>>> c = c + 1>>> print(c)2>>> c += 1>>> print(c)3>>> b = 2>>> a = 3>>> b += a>>> print(b)5>>> b -= a>>> print(b)2>>> b *= a>>> print(b)6
Relational operators
An operation symbol used to determine the size relationship of two elements. Python is not only a number type to make a size comparison
>>> 1 > 1False>>> 1 >= 1True>>> a = 1>>> b = 2>>> a != bTrue>>> ‘a‘ > ‘b‘False>>> ‘abc‘ < ‘abd‘True>>> [1,2,3] < [1,3,2]True>>> (1,2,3) > (4,5,6)False
A single character compares the ASCII code, and the string comparison size is a comparison of the size of each bit character. the focus of the relational operation is the Boolean value returned when the operation is completed
logical operators
Feature: operation Boolean value, return boolean value . There are only three types of operations: with, or, non-
>>> True and TrueTrue>>> True and FalseFalse>>> True or FalseTrue>>> False or FalseFalse>>> not FalseTrue
For non-Boolean types of decision methods:
- For numeric types such as int, float, non 0 means false for true,0
- For strings, non-NULL indicates true, NULL indicates false
- For "group", list, set and so on. Non-NULL indicates true, NULL indicates false
>>> 1 and 11>>> ‘a‘ and ‘b‘‘b‘>>> ‘a‘ or ‘b‘‘a‘>>> not ‘a‘False>>> not []True>>> not [1,2]False
not
The operation returns a dominant true and false. But and
and not or
necessarily
>>> not []True>>> not [1,2]False>>> [1] or [][1]>>> 1 and 00
and
and or
The result display follows the "scan principle", if the current bit is able to determine the result then return the current bit, otherwise continue
>>> 0 and 10>>> 1 and 22>>> 2 and 11>>> 0 or 11>>> 1 or 01>>> 1 or 21
Member operators
Includes in
and not in
two actions to determine whether a member belongs to a group
>>> a = 1>>> a in [1,2,3,4,5]True>>> b = 6>>> b in [1, 2,3,4]False>>> b not in (1,2,3,4,5)True>>> b not in {1,2,3,4,5}True
For the Dictionary (dict) type, the in
not in
key is a key to the operation
>>> b = ‘a‘>>> b in {‘c‘:1}False>>> b = 1>>> b in {‘c‘:1}False>>> b = ‘c‘>>> b in {‘c‘:1}True
Identity operator
Include is
and not is
. Used to determine whether the left and right sides of the symbol are "identity"
>>> a = 1>>> b = 1>>> a is bTrue>>> a = 2>>> a is bFalse>>> a = ‘hello‘>>> b = ‘world‘>>> a is bFalse
At first glance, these discriminant errors assume that he is the same as the relational operator (= =). But look at the meeting and find the difference
>>> a = 1>>> b = 1.0>>> a is bFalse>>> a == bTrue
identity operators so-called "identity", in fact, check whether the memory address is the same, do not care whether the value is the same , simple and rude to say is the id()
same as the value
>>> a = 1>>> b = 1>>> id(a)1429827040>>> id(b)1429827040>>> a is bTrue>>> a == bTrue>>> b = 1.0>>> id(b)2354290105056>>> a is bFalse>>> a == bTrue
You can also use the identity operator for set, tuple, and so on
>>> a = {1,2,3}>>> b = {2,1,3}>>> a is bFalse>>> a == bTrue>>> a = (1,2,3)>>> b = (2,1,3)>>> a is bFalse>>> a == bFalse
Because of the disorder of the set, the relationship comparison is the same, and the tuple has no such attribute False
. And because each of the memory addresses are specific, they are identity independent and different from each other.
Three major characteristics of objects
All objects in Python
- Value value,==
- Identity
id()
, is
- Type
type()
, isinstance
Values and identity judging above, here is a description of the type judgment
>>> a =‘hello‘>>> type(a) == intFalse>>> type(a) == strTrue>>> isinstance(a,str)True>>> isinstance(a,int)False>>> isinstance(a,(int,str,float))True>>> isinstance(a,(int,float))False
Type judgment is to determine the type, can be used in type(x)==bb
the way, but the more recommended isinstance(x,(aa,bb,...))
method, both return a Boolean value. But the latter can determine if the subclass of X is of a certain type, but type()
not
Bitwise operators
It is mainly used for bitwise and &
and bitwise OR |
as the representative, the number as a binary and then each bit as a separate unit for the operation (bitwise operation).
>>> a = 2>>> b = 3>>> a & b2>>> a = 2>>> b = 3>>> a | b3
Here with and OR operation with middle School mathematics, &
when there is one for 0 o'clock, the result is 0. |
when there is a 1 o'clock in the operation, the result is 1
>>> a = 1>>> ~a-2>>> a << 12>>> a = 3>>> a >> 11
~
The bitwise counter operation involves twos complement, as shown in the previous example. Negative numbers are expressed as the 符号位+补码
number of tokens that need to be taken out of the sign bit (0 is positive, 1 is negative), and the remaining part is reversed plus one.
>>
And <<
is for each of the shift operation, the rule is to move to the right, the high-fill sign bit, left to move, the low-fill 0
Python Learning path--02 variables and operators