qq

content

  ➤ Preliminary knowledge

  ➤ Data Types

  ➤ Extend knowledge

 

one

Preliminary knowledge

 

If the reader has the foundation of other programming languages, such as: C language. You will find that python does not need to declare its data type when using "variables". Even, in python, there is no concept of "variable"! Instead, it is called an object reference.

Most of the above features exist because python is an object-oriented programming language. In python, everything is an object ! So, data types are also objects. The following figure can deepen the understanding of this problem.

 

▼ Data type objects and object references in python:

 

It can be seen that the data types in python are for the data objects in the heap. The object reference is also equivalent to another name for the data storage address, so there is no need to declare its data type. In python, object references are also a type of identifiers, which can be named using identifier naming rules.

python identifier naming rules

 

1.   Begin with a letter or underscore, followed by a letter, number or underscore. Where letters or underscores can be letters or numbers in Unicode encoding.

2. The  identifier cannot be repeated with the keyword, and the length of the identifier is not limited.

3.  Identifiers should be made meaningful and readable. For example: "myName" or "my_name".

▼ View keywords in Python

 

two

type of data

 

When we change the value of an object stored in the heap, if its reference (address) also changes, then we call the data type of this object an immutable data type. Conversely, it is called a mutable data type.

>>>> immutable data types

1.  Integer type and floating point type

The above two data types represent integers and floating-point numbers, respectively. Also, python supports complex numbers. Integer types in python are represented by decimal by default, and other decimal representations are:

base binary Octal hex
prefix 0b 0o 0x

Operations on these two types of data are usually various arithmetic operations. python can provide good support. These operators include: "+", "-", "*", "/", "//", "**", "%". As you can see, in python, there are two divisions: "/" and "//". The former is exact division, and the result is of type float. The latter is a floor division, and the result is of type int (value quotient). Also in the interactive environment, the "_" symbol can be used to save the result of the last operation.

2.  String type

In python, a string type is what is enclosed in single or double quotes. Multiple lines can be enclosed in triple quotes (did you remember comments?).

For strings, common operations include case conversion, string concatenation, and so on.

 

 1# 字符大小写转换
2s1 = "hello World!"  # 示例字符串。
3s1 = s1.title()  # 首字母大写。
4s1 = s1.upper()  # 全部转换为大写。
5s1 = s1.lower()  # 全部转换为小写。
6
7# 字符串拼接
8s1 = "hello"
9s2 = "world!"
10s3 = s1 + s2  # s1,s2为待连接字符串,s3为拼接后的字符串。
11s4 = s1 * 2 + s2 * 2  # 同样乘法可将多次拼接同一字符串。
12
13# 删除字符串空白
14s1 = "   hello    world!   "
15s1 = s1.lstrip()  # 删除字符串开头空白字符。
16s1 = s1.rstrip()  # 删除字符串末尾空白字符。
17s1 = s1.strip()   # 删除字符串两端空白字符。

 

Since the quotes that define a string are not part of its content, what happens when its content contains quotes? In addition to using single quotes (double quotes) in strings represented by double quotes (single quotes), we can also use escape methods, that is, use \' and \" to represent single and double quotes. Commonly used escape characters It can be summarized as follows:

 

escape character \b \n \r \t \v \\ \ooo \xhh
meaning backspace newline Enter horizontal tab vertical tab backslash octal character hex character

Additionally, prefixing a string with "r" will invalidate the escape characters in the string and display it as-is. An example of escaping is shown below:

1# 打印内容:I’m fine, thanks.
2print("I'm fine, thanks.")
3print('I\'m fine, thanks.')  
4
5# 其他转义字符应用示例
6print("he\rllo\b wor\tld")     # 结果为:ll wor    ld
7print("\x68\x65\x6C\x6C\x6F")  # 结果为:hello
8print(r"he\rllo\b wor\tld")    # 结果为:he\rllo\b wor\tld

 

If you know the C language, you must be familiar with string formatting. Python has two ways of string formatting: printf-form string formatting and

printf-form string formatting is like the C language, using % for formatting operations. The syntax format is:

format % values

Among them, format is a string, % is a format operator, and values ​​correspond to the converters in format one-to-one. When the number of converters is more than one, values ​​is a tuple or dictionary.

 

 

 

 

 


>>>> Application layer: Provide services to users

▼ Four physical topologies:

 

 

 

 

 

 

converter in format

 

The complete converter consists of the following parts:

String type (str)

 

In Python, there are two ways to format strings: the % format operator and the format function .



  • % formatting operator

First, let's look at a simple example:

From the above example, we can see that:

  1. Strings can be formatted with tuple and dict variables and the result is still a string.

  2. The % in the string corresponds to each real value in the following tuple and dict, and is formatted by the content following the % .

For the selection of type code, please refer to the following table:

Type codes and their descriptions
s Get the return value of the __str__ method of the passed in object and format it to the specified location
r Get the return value of the __repr__ method of the passed in object and format it to the specified location
c Integer: Returns the Unicode character represented; Character: Adds the character to the specified position
O Converts an integer to an octal representation and formats it at the specified location
x Convert an integer to a hexadecimal representation and format it to the specified location
d Convert integer and floating point numbers to decimal representation and format them to the specified position
and and Convert integer and floating point numbers to scientific notation and format them to the specified position
f、F Convert integers and floating-point numbers to floating-point representations and format them to the specified position (retains 6 decimal places by default)
g、G Autoscale converts integers, floating-point numbers to floating-point or scientific notation and formats them to the specified location
%

When there is a formatting flag in the string, you need to use %% to represent a percent sign

When you don't know which type symbol to choose, you can use the universal s type symbol.

 



  • format function

Also look at a simple example

From the above example, we can see that:

  1. The format() function accepts several arguments to format a string. The use of parameters is related to the invocation of strings.

  2.  Use { }  and  :  in strings for format control. The ordinal numbers in { } represent the format() function parameter order.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325083187&siteId=291194637
qq