python study notes 7.21 (54-punch in on time-QQ)

Task1
1. Variables, operators and data types
1. Comment
(1) Single-line comment: In Python, # represents a comment and acts on the entire line.
(2) Multi-line comments: Use'''''' or """ """ to indicate interval comments, and all
content between the triple quotation marks is commented.
2. Operator
(1) Arithmetic operator: addition, subtraction, multiplication, division, and division (//) remainder (%) power (**)
(2) comparison operator: greater than (>) less than (<) greater than or equal to (>=)
Less than or equal to (<=) Equal to (==) Not equal to (!=)
(3) Logical operator and (and) or (or) not (not)
(4) Bit operator: bitwise negation (~) Press Bitwise AND (&) Bitwise OR (|)
Bitwise XOR (^) Left shift (<<) Right shift (>>)
(5) Ternary operator:
if x <y:
small = x
else:
small = y is
equivalent to:
small = x if x <y else y
(6) Other operators: exists (in) does not exist (not in)
is (is) is not (is not)
is, is not compares two variables The memory address of
==, != compares the values
of two variables. If the two variables to be compared point to types with immutable addresses (str, etc.),
then is, is not and ==,! = Is completely equivalent.
The two contrasted variables point to variable address types (list, dict, tuple,
etc.), there is a difference between the two.

[Dictionary (dict) is the only mapping type in python. It is composed of key-value pairs enclosed in {}. The key in the dict is unique. When saving, calculate a memory address based on the key. Then Save the key-value in this address. This algorithm is called a hash algorithm]
[(tuple) the ancestor is an immutable list The
ancestor is represented by (), and the elements are separated by commas, and there is no restriction on the data type]

Operator precedence: unary operators are better than binary operators. Arithmetic operation first, shift operation second, bit operation last. Logic operations are finally combined.

3. Variables and assignment
Before using a variable, you need to assign a value to it.
Variable names can include letters, numbers, and underscores, but variable names cannot start with numbers.
Python variable names are case sensitive, foo != Foo.

4. Data type and conversion
Integer type (int): integer,
floating point type (float): with decimals
Boolean type (bool): true/false
[bin(a) means a in binary form]

Sometimes we want to keep the n digits after the decimal point of the floating point type. It can be achieved with Decimal object and getcontext() method in decimal package. The introduction method is as follows:

import decimal
from decimal import Decimal

Use as follows:

b = Decimal(1) / Decimal(3)
print(b)

# 0.3333333333333333333333333333

There are many packages with a wide range of uses in Python. You can import what you use. The package is also an object, and you can also use the dir(decimal) mentioned above to see its properties and methods.

Boolean (boolean) variables can only take two values, True and False. When using Boolean variables in digital operations, use 1 and 0 to represent True and False. In addition to directly assigning True and False values ​​to variables, you can also use bool(X) to create variables, where X can be a basic type: integer, floating point, boolean; container types: string, tuple, list, dictionary, and set. The details are as follows:
(1) Bool acts on basic type variables: As long as X is not integer 0, floating point 0.0, bool(X) is True, and the rest is False.
(2) Bool acts on container type variables: As long as X is not an empty variable, bool(X) is True, and the rest is False.

Ways to obtain type information:
(1) type(object) to obtain type information
(2) isinstance(object, classinfo) to determine whether an object is a known type.
[Note: type() does not consider the subclass to be a parent type, and does not consider the inheritance relationship. isinstance() will consider the subclass to be a parent class type, and consider the inheritance relationship.

Type conversion:
converted to integer int(x, base=10)
converted to string str(object='')
converted to floating point float(x)

5. The print() function
Structure: print(*objects, sep='', end='\n', file=sys.stdout, flush=False) The
features are as follows:
(1) Format the object as a string The output is output to the stream file object file. Among them, all non-keyword parameters are converted to string output according to the str() method;
(2) The keyword parameter sep is the realization separator, such as the separator character in the middle when multiple parameters are output;
(3) Keyword The parameter end is the character at the end of the output, and the default is a newline character \n;
(4) The keyword parameter file is the file that defines the stream output, which can be the standard system output sys.stdout, or it can be redefined as another file;
( 5) The keyword parameter flush is to immediately output the content to the stream file without caching.

Practice questions

1. (1) Single-line comment: In Python, # represents a comment, which acts on the entire line.
(2) Multi-line comments: use'''''' or """ """ to indicate interval comments, and all content between the triple quotation marks is annotated

2. Operators: arithmetic operators, comparison operators, logical operators, bitwise operators, ternary operators, other operators (in, is, etc.)
Priority: unary operators are better than binary operators. Arithmetic operation first, shift operation second, bit operation last. Logic operations are finally combined.

3. is, is not compares the memory addresses of two variables;
==, != compares the values
of two variables ; if the two variables to be compared, they all point to types with immutable addresses (str, etc.) , Then is, is not and ==,! = Is completely equivalent.
If the two variables to be compared point to a variable address type (list, dict, etc.), there is a difference between the two.

4. Integer (int), floating-point (float), Boolean (bool)
type conversion: converted to integer int(x, base=10); converted to string str(object=''); converted to Floating point type float(x)

2. Bit operation
1. Original code, one's complement, and one's complement
binary has three different representation forms: original code, one's complement and one's complement. The computer uses complement to represent. The highest bit is the sign bit, 0 means positive number, 1 means negative number. In the bit operation, the sign bit also participates in the operation.

  • Original code: is its binary representation (note that the highest bit is the sign bit).
  • Inverse code: The inverse code of a positive number is the original code, and the inverse code of a negative number is the sign bit unchanged, and the remaining bits are inverted (corresponding to the inversion of the positive number by bit).
  • Complement: The complement of a positive number is the original code, and the complement of a negative number is the complement +1.

2. The bitwise non-operation (~)
reverses all 0 and 1 in the complement of num (0 becomes 1, and 1 becomes 0).

3. Bitwise AND operation (&) is
only 1 when both corresponding bits are 1.

4. Bitwise OR operation (|)
as long as one of the two corresponding bits is 1, it is 1.

5. Bitwise XOR operation (^)
is 1 only when two corresponding bits are different.
The nature of the exclusive OR operation: satisfy the commutative law and associative law

A^B = 
A^B^A: = A^A^B = B 

6. Bitwise left shift operation (<<)
num << i Move the binary representation of num to the left by i bits.

7. Bitwise right shift operation (>>)
num >> i The value obtained by shifting the binary representation of num i bits to the right.

8. Use bit arithmetic to realize fast calculation
(1) Quickly calculate the multiple of 2 through <<, >>.

n << 1 -> 计算 n*2
n >> 1 -> 计算 n/2,负奇数的运算不可用
n << m -> 计算 n*(2^m),即乘以 2 的 m 次方
n >> m -> 计算 n/(2^m),即除以 2 的 m 次方

(2) Quickly swap two integers through ^.

a ^= b
b ^= a
a ^= b

which is:

(3) Use a & (-a) to quickly get the integer whose last is the 1 position.

00 00 01 01 -> 5
&
11 11 10 11 -> -5
---
00 00 00 01 -> 1

00 00 11 10 -> 14
&
11 11 00 10 -> -14
---
00 00 00 10 -> 2

9. Use bit operations to realize the set of integers
. The binary representation of a number can be regarded as a set (0 means not in the set, 1 means in the set).
For example, the set {1, 3, 4, 8} can be expressed as 01 00 01 10 10, that is, the 1.3.4.8 bit of the binary is 1 to indicate that it is in the set, and the bit that is 0 is not in the set.

Operation of elements and collections:

a | (1<<i)  -> 把 i 插入到集合中,即对应位变为1
a & ~(1<<i) -> 把 i 从集合中删除,即对应位变为1
a & (1<<i)  -> 判断 i 是否属于该集合(零不属于,非零属于)

[Integers in Python are stored in complement. Integers in Python are not limited in length and will not overflow.
【In order to obtain the complement of a negative number (decimal representation), you need to manually perform a bitwise AND operation with the hexadecimal number 0xffffffff, and then pass it to bin() for output, and the complement representation of the negative number is obtained.

Practice questions: The
code is as follows:

class Solution:
 public:
  def singleNumber(self, nums: List[int]) -> int:
  self = 0;
  for (int i = 0; i<nums.size(); ++i)
   self = self^nums[i];
  return self;
    
 
————————————————
版权声明:本文为CSDN博主「_kean」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/C1029323236/article/details/86653188

Guess you like

Origin blog.csdn.net/m0_45672993/article/details/107498900