This python article is the details of python you missed in the past. Snowball season 4, Article 15

This article continues to look for loopholes and fill gaps. The python language involves operators and their priorities

python operation expression list

The priority of the following operation expressions decreases from top to bottom.

  • {}, [], (): braces {} are used in dictionary and set related knowledge, brackets [] are used in lists and list generators, and parentheses () are used in tuples and generator expressions;
  • Class. Attribute: dot. Operator;
  • Function (parameter), method (parameter) and class (parameter): their priorities are basically the same;
  • List [i:j:k]: slice operation;
  • List [i]: index operation;
  • num**x: power operator;
  • ~num: bit inverse operator;
  • -Num, + num: unary operator;
  • num1/num2, num1//num2: Division and integer division operators;
  • num1*num2, num1%num2: multiplication / repetition operator, remainder / format operator;
  • num1+num2, num1-num2: addition / connection operator, subtraction / difference operator;
  • Num < < 1, Num > > 2: shift left and shift right operators;
  • Num1 & num2: bitwise and operator, intersection operator;
  • num1^num2: bitwise XOR operator, symmetric difference set operator;
  • num1|num2: bitwise OR operator, set union operator;
  • num1==num2,num1!=num2: equal and unequal;
  • <, >, < =, > =: comparison operator;
  • obj is object, obj is not object: identity operator;
  • a in my_list,a not in my_list: member test operator;
  • not, or, and: logical non, logical or, logical and;
  • num1 if num2 else num3: conditional expression;
  • lambda: anonymous function expression;
  • yield: generator expression.

python built-in data type

In python, some commonly used data types have been built in. These have been systematically learned in snowball Season 1. Here, some missing knowledge points are supplemented, focusing on numeric types and string types.
value type
Numerical values are numbers. Here is only a supplement for learning. In order to make numbers easy to read in python code, multiple zeros or underscores can be added to integers and floating-point numbers, such as the following code.

num = 100_000
f_num = 000.005


Generally, an underline is added every 3 digits.

Integer and floating-point objects have some proprietary type methods and properties

as_integer_ratio: returns an integer ratio

a = (10.0).as_integer_ratio()
b = (0.0).as_integer_ratio()
c = (-.25).as_integer_ratio()

is_integer: returns True if the floating point is an integer

a = (10.0).is_integer()  # True

c = (-.25).is_integer()  # False

Integer also has some properties or methods

a = (2).numerator  # molecule
b = (2).denominator  # denominator
print(a, b)

c = (127).bit_length() # Bit bit

The existence of attributes and methods for integer or floating-point types indirectly proves that they are objects.

Decimals and fractions have exclusive modules in python

from decimal import Decimal
# Calculation by object
df1 = Decimal('0.5')
df2 = Decimal(5)
df3 = Decimal(0.3)


You can give a Decimal object an integer or string type parameter, but it cannot be floating-point data because floating-point data is inaccurate in the computer.
If you want to convert floating-point numbers, use the following code:

df4 = Decimal.from_float(10.4)

You can also fix the precision using the getcontext() function

from decimal import Decimal, getcontext

getcontext().prec = 3
# Calculation by object
df1 = Decimal('1') / Decimal('3')

Floating point numbers can be rounded:

from decimal import *

df1 = Decimal('20.2333').quantize(Decimal('0.00'))
df2 = Decimal('20.2383').quantize(Decimal('0.00'))
print(df1) # 20.23
print(df2) # 20.24

The demonstration code of fraction module is as follows:

from fractions import Fraction
a = Fraction(1,2)

String type

Contiguous string constants, auto join

my_str = "hello" "world" "eraser"

If the adjacent string is in a bracket, it can be used to implement python's convention of limiting each line of code to no more than 79 characters.


There are bytes and byte arrays in python. You can use the code to calculate the difference set of sets to see the differences in methods and properties between bytes and byte groups.

str_set = set(dir(str))
bytes_set = set(dir(bytes))
# Differences between str and bytes
print(str_set - bytes_set)
# The differences are as follows: {'isdecimal', 'format', 'isnumeric', 'encode', 'isidentifier', 'casefold', 'isprintable', 'format_map'}

Using the same method, differences between other types can also be calculated.

Syntax details in python

python sequence assignment

a, b, c = [1, 2, 3]
print(a, b, c)

Sequence assignment requires that the number of variables on the left is consistent with the length of the sequence on the right.

Extended sequence assignment

a, *b, c = [1, 2, 3, 4, 5, 6, 7]
print(a, b, c)

Use * to collect unmatched elements in the sequence.

Syntax in python

  • Control flow: by default, python code runs from top to bottom. When branch code occurs, the running order is changed;
  • Block: the same code block has the same indentation;
  • Statement: a line of code in python is a statement, and some statements have line breaks;

Statement and expression are two concepts often mentioned in python coding, which are difficult to distinguish.
Generally, any expression can appear in the form of a statement, but the statement cannot appear in any expression. Therefore, we can know that the scope of the statement is large.

Generally, statements are regarded as a line of code composed of expressions, while expressions are only composed of operators and operands.

Write it at the back

The above content is the whole content of this article. I hope it will help you on the way to study~

Today is the 232 / 365 day of continuous writing.
Look forward to attention, praise, comment and collection.

More wonderful

Keywords: Python

Added by zwxetlp on Sun, 03 Oct 2021 01:35:19 +0300