Second, understand simple data types

Python simple data types

A numerical representation of the data type and operation

The data used in mathematical calculations we call numerical data, often used in the program, the numerical data Pytho There are three types: integer data, real data and complex data type.

1 , integer (int type):

Here that the data refers to data that integer without a decimal point.

Represents 1) of an integer

Refers to an integer without decimal points, including positive, negative and zero decimal, binary, octal and hexadecimal representation of four kinds, starting with 0b is a binary number, beginning with 0O is octal, beginning with 0x are hexadecimal, without tagging is a decimal number. As shown below, the first number is an octal number, the second number is the hexadecimal number, the third number is a binary number, the fourth decimal number. Here's upper or lower case letters.

0O1076      -0x2ad0      0b10110110     -123

We enter the Python interpreter after the prompt window and enter the number of these are, as a result, the system automatically convert a decimal number display. Can quickly convert this way binary, octal, hexadecimal to decimal. See the figure below

Learn simple Python data types

Python in the range of integer data is unlimited, which means that we can use in the program arbitrarily large integers.

2) the number of integer arithmetic sign

Calculating integer data symbols may be used include

+ (Plus), - (minus), * (multiplication), / (division), // divisible,% (remainder number), ** (power)

Wherein the division result is generally not an integer, for example, in the prompt window Python interpreter input 9/4, carriage return is displayed as 2.25, but if the input result display 9 @ 4 is 2, divided by 9 represents 2 is after 4 supplier (discarded decimals). If the input 9% 4, is displayed as 1, 9 represents the number is divided by 4 is 1, if the input result display 2 ** 4 16, 4 represents a power of 2 16, referring next to FIG.

Learn simple Python data types

Do not, power operator has the highest priority, addition and subtraction of lowest priority, and multiply, divide, divisible, the same as the number four operator remainder of priority. When different priorities arithmetic symbols appear in the same expression, to complete a high priority operation, when the operator of the same priority appear in the same expression, it is evaluated from left to right, such as the following Expression formula:

2+4*6/2**3

5.0 calculation result, where the first cubic determined 2 and found to be 8, simplified to the expression: 2 + 4 * 6/8, due to multiply, divide the same operation level, adjustment shall be evaluated from left to right , for the first multiplication, the result is: 2 + 24/8, then the result was 3.0 for the division, into an expression: 2 + 3.0, and finally for the addition operation, the result is 5.0

Note that in addition to the number of the operation result with a decimal point. Regardless of whether divisible, if you want decimal point, you need to be calculated using the integer division symbol.

2, floating-point type (float type):

We will all number with a decimal point are used in mathematics known as floating point data type.

Represents 1) float

There are two floating-point representations, one is the common representation, one is scientific notation. Common notation as follows:

-134.5098   2.4     -0.04

The following is a scientific notation:

 123E-2  1.2234e2    -1.23788E-1     6.244E-3 

In the above formula, the letter E with floating-point numbers in scientific notation represented by the table, wherein the back of the letter e represents the number index, expressed as 12.34 12.34e-3 multiplied by the negative power of 10 3.

Number of inputs to the above explanation in the Python window, the results are shown in FIG.

Learn simple Python data types

2) The floating-point operator

Floating-point operator comprises

+ (Plus), - (minus), * (multiplication), / (division), // divisible,% (remainder number), ** (power)

Wherein the floating-point operation symbol% ​​Although operations, but the actual rarely used. Two positive number with a decimal point divided by the actual number of remainder is to do subtraction, minus the dividend divisor repeatedly until the reduction is not so far, the last remaining difference is the result.

After a number with a decimal point is divisible by two, the division is actually done, remove the decimal part supplier as an operation result.

Note: computer floating point (i.e., a decimal point number) indicates when there is an error, it should be noted when writing a program, it is possible to display such as 1.2 or 1.20000000003 1.19999999999, it should be avoided by floating the programming points accurate size comparison.

3, a plurality of types (complex):

Complex representation format is: a + bj

Wherein a and b are floating point numbers, such as 1.2 + 0.7j, complex representation is correct. When a plurality of the display system will be enclosed in parentheses.

Calculating a plurality of symbols that can be used as follows:

+ (Plus), - (minus), * (multiplication), / (division), ** (power)

我们在Python解释器窗口的提示符号后面依次输入如下各行内容

(3+5j)**2

(30+5j)/5

30+5j+2+5j

运行结果如下图所示。

Learn simple Python data types

二、字符串型数据(str)

1、字符串型数据的表示方法

字符串型数据用来表示程序中用到的文本文字、字母、键盘上各种可显示的符号,比如姓名,电话,地址等信息均要使用字符串来表示。

字符串是用一对引号括起来的一系列文字符号,引号可以是单引号,双引号,但开始结束使用的引号要相同。比如:

'邮编300072'

"滨海市河北路34号"

字符串中可以含有引号,但要和字符串开始和结束时使用的最外层引号不同,比如:

   "'赵明', '男','汉'"

当字符串中含有多种引号时,可以使用连续的三个引号作为字符串的引号,比如

"""李丽丽  '女'  1960-9-9  "汉族"   滨海市大明路38号  邮编400090  """

当字符串较长要写在多行时,要使用三引号。

'''锄禾日当午,汗滴禾下土。

   谁知盘中餐,粒粒皆辛苦。 '''

在Python解释器窗口的提示符后面输入上面各字符串均可以得到正确的输出。参见下图。绿色文字是输入的内容,蓝色文字是输出的内容。其中的Learn simple Python data types代表字符中的换行标记。

Learn simple Python data types

我们有时需要在字符串中包含一些特殊字符,这时需要使用转义字符。转义字符是由反斜扛加一个字符来表示,参见下表:

Learn simple Python data types

下面举例来说明上面表中字符的使用方法

在解释器窗口输入下面各行命令:

Learn simple Python data types

输出结果如下图所示。

Learn simple Python data types

对上面输出结果的说明:

第一条语句中的 \n表示换行,字符串中含有这个符号,在输出时遇到\n就会产生一个换行。

第二条语句中的\t代表一个横向跳格,即光标位置跳到下一个水平制表位,所以输出时遇到这个符号,则光标跳过几个位置后再显示后面的内容。

第三条语句中的\"代表一个双引号,所以在输出的内容中该位置显示一个双引号。同理\'则代表一个单引号。

第四条语句中的\\代表一个反斜扛。所以在输出时显示为一个反斜扛。

第五条语句中的“\101”中的101是八进制,相当于十进制的65,正是大写字母A的ASCII码,所以在输出时“\101”显示为A,同理“\102”显示为B,符号“\x41”中的x41表示是十六进制数,十六进制的41相当于十进制的65,也就是字母A的ASCII码,所以也显示为字母A,同理,“\x42”显示为字母B。

注意上面表的最后一种表达方法中,反斜扛后面只能使用八进制数或以x开头的十六进制数来表示ASCII。

2、字符串型数据的运算符

能用于字符串运算的有四个运算符号,分别是“+”、“*”、“in”和“not in”,其中“+”用于连接字符串,“*”用于重复原有的字符串,比如:在Python解释器窗口的提示符后面分别输入下列内容,并按回车键,结果如下图所示。

"中国"+"人民解放军"

      "ABCD"*3

Learn simple Python data types

命令执行的结果一个是将字符串连接成一个字符串,一个是将字符串原有内容重复了三次。

运算符“in”用于检测一个字符串是否被包含在另一个字符串中,运算符“not in”用于检测一个字符串是否未被包含在另一个字符串中,我们在Pyhton解释器窗口中分别输入下面的两条语句:

"AB" in "LABEAL"

"AB" not in "LABEAL"

结果如下图所示。

Learn simple Python data types

第一条语句的输出结果为True,而第二条语句的输出结果为:False,参见上图。

True的含义是“真”,False的含义是“假”

第一条语句的含义是字符串"AB"被包含在字符串"LABEAL"中吗,如果显示为True,则表示左侧的字符串被包含在右侧的字符串中,如果显示为False则表示左侧的字符串未被包含在右侧的字符串中。

运算符“not in” 和运算符“in” 恰恰相反。

第二条语句的含义是字符串"AB"未被包含在字符串"LABEAL"中吗,如果显示为True,则表示左侧的字符串未被包含在右侧的字符串中,如果显示为False则表示左侧的字符串被包含在右侧的字符串中。

三、逻辑型数据(bool)

逻辑型数据是一种特殊的数据,只有两个值,True和Fales,这两个英文单词的意思是“是”和“否”也可翻译成“真”和“假”。

逻辑型数据的运算符号有 三个:分别是 “and”、"or"、"not",它们分别读作“与”、“或”、“非”。逻辑运算具有重要的实际意义,我们后面介绍,这里只记住其运算规则即可,逻辑运算的规则如下:

1)逻辑“与”运算(and)

“逻辑与”运算的规则用一句话来概括就是:有假则假,即参与“逻辑与”运算的两个逻辑值,只要其中有一个值是“False”则结果就是“False”。在Python解释器窗口提示符后输入下列各逻辑运算式,可得到其运算结果。如下图所示。

False and False

False and True

True and False

True and True

Learn simple Python data types

代表真和假的逻辑值输入时要小心,英文单词的第一个字母要大写,否则会出现错误。

2)逻辑“或”运算(or)

“逻辑或”运算的规则用一句话来概括就是:有真则真,即参与“逻辑或”运算的两个逻辑值,只要其中有一个值是“True”则结果就是“True”。在Python解释器窗口提示符后输入下列各逻辑运算式,可得到其运算结果。如下图所示。

False or False

False or True

True or  False

True or  True

Learn simple Python data types

3) Logical "not" operator (not)

The meaning of logical "not" operation is negation of the logic value of the current, thus requiring only a logical value calculation, calculation results, and currently involved in an arithmetic logic value opposite. Enter the following logical expression in the non-Python interpreter prompt window, as shown below can be obtained calculation result.

not False

not True

Learn simple Python data types

Python logic operation may also be used in a digital, the digital system as a logical 0 value False, than the number of considered True 0

E.g. Input: 0 and 1

          1 or 0

          not 1

          not 0

The results of running the program as shown in FIG.

Learn simple Python data types

Note: priority logic operator is not at the highest level, or the lowest level, such as the following logical expression:

False or not True and True

In the above formula should be completed first operation not True, the result is False, the logical expression simplifies to:

False or False and  True

And then complete the operation, False and True result is False, the logical expression simplifies to: False or False

 

False or False to finalize the operation. So the final result is False.

 

Guess you like

Origin www.cnblogs.com/liuyan0917/p/11645741.html