2. Operators and basic data types

Operators and basic data types

Arithmetic Operator

There are mainly several arithmetic operators in python

Operator description Instance
+ Add-Add two objects a + b outputs the result 30
- Subtract-get a negative number or subtract one number from another a-b output result -10
* Multiply-Multiply two numbers or return a string repeated several times a * b outputs the result 200
/ Divide-x divided by y b / a output result 2
% Modulo-returns the remainder of the division b% a output result 0
** Power-returns x to the power of y a**b is 10 to the 20th power, and the output is 100000000000000000000
// Divide-returns the integer part of the quotient ( rounded down ) >>> 9//2 <br />4<br /> >>> -9//2<br />-5

Example:

print(4+4)          #8
print(4-4)          #0
print(4*4)          #16
print(4/4)          #1.0
print(4-4*4)        #-12

print(4**4)         #254

print(10//3)        #3 整除 向下取整
print(10.0//3)      #3.0
print(-10//3)       #-4

print(10%3)         #1 余数  运算过程:10//3=3  3*3=9  10-9=1
print(-10%3)        #2 同样遵循向下取整  运算过程:-10//3=-4  -10-[3*(-4)]=2

注意:取整除的机制是向下取整。

​ means that if the original result is 1.3, then take 1; if the original result is -1.3, take -2

When the result is an infinite loop decimal, the result will look like this

>>> print(10/3)
3.3333333333333335

The reason why the last digit here is 5 is because of the finiteness of the binary system, which will produce corresponding errors.

When we calculate the following content, we will find that its result is 5.551115123125783e-17

print(0.1+0.1+0.1-0.3)

This is because scientific notation is used to calculate numbers in python. We can eliminate this error by referencing the decimal class

from decimal import Decimal
print(Decimal('0.1')+Decimal('0.1')+Decimal('0.1')-Decimal('0.3'))

Quotient and remainder

When we ask for the quotient of a number and its remainder, we can use the divmod method

>>> x = 10
>>> y = 3
>>> print(divmod(x,y))
(3, 1)

By looking at its help document, you can know that its calculation method is tuple (x//y,x%y)

Comparison operator

In comparison, what python compares is the value of the variable

E.g

>>> a = 3.0
>>> b = 3
>>> print(a == b)
True
Operator description Instance
== Equal to-compare whether the objects are equal (a == b) returns False.
!= Not equal-compares whether two objects are not equal (a != b) returns true.
<> Not Equal-Compare whether two objects are not equal. python3 is obsolete. (a <> b) returns true. This operator is similar to !=.
> Greater than-returns whether x is greater than y (a> b) returns False.
< Less than-Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. This is equivalent to the special variables True and False respectively. (a <b) returns true.
>= Greater than or equal to-returns whether x is greater than or equal to y. (a >= b) returns False.
<= Less than or equal to-returns whether x is less than or equal to y. (a <= b) returns true.

Multiple comparisons

Some things to be aware of when performing multiple comparisons

>>> print(3>2>1)
True
>>> print(3>2>2)
False
>>> print((3>2)>1)      #3>2所以结果为Ture,Ture>1?
False

Assignment operator

Operator description Instance
= Simple assignment operator c = a + b assign the result of a + b to c
+= Addition assignment operator c += a is equivalent to c = c + a
-= Subtraction assignment operator c -= a is equivalent to c = c-a
*= Multiplication assignment operator c = a is equivalent to c = c a
/= Division assignment operator c /= a is equivalent to c = c / a
%= Modulus assignment operator c %= a is equivalent to c = c% a
**= Power assignment operator c = a is equivalent to c = c a
//= Divide and Divide Assignment Operator c //= a is equivalent to c = c // a

Logical Operators

Operator Logical expression description Instance
and x and y Boolean "and"-If x is False, x and y return False, otherwise it returns the calculated value of y. (a and b) returns 20.
or x or y Boolean "or"-If x is non-zero, it returns the value of x, otherwise it returns the calculated value of y. (a or b) returns 10.
not not x Boolean "not"-If x is True, return False. If x is False, it returns True. not(a and b) returns False

In layman's terms, "and" means that it outputs False when it encounters False, or it outputs True when it encounters True.

Laziness mechanism

print(10 and 20)

The result is 20, why?

Through bool(), you can know that the bool values ​​of 10 and 20 are both True (all numbers except 0 are True)

and的懒惰机制是一个接一个看直到看到False为止

or的懒惰机制是一个接一个看直到看到True为止

所以

print(10 or 20)

输出结果为10

成员运算符

运算符 描述 实例
in 如果在指定的序列中找到值返回 True,否则返回 False。 x 在 y 序列中 , 如果 x 在 y 序列中返回 True。
not in 如果在指定的序列中没有找到值返回 True,否则返回 False。 x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。

可以判断值是否在列表中

li = [1,2,3,4]
if 5 in li:
    print("在")
else:
    print("不在")

身份运算符

用于查看两个东西的内存地址是否一样,也就是比较id()的值是否一样。

运算符 描述 实例
is is 是判断两个标识符是不是引用自一个对象 x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 False
is not is not 是判断两个标识符是不是引用自不同对象 x is not y , 类似 id(a) != id(b)。如果引用的不是同一个对象则返回结果 True,否则返回 False。

三目运算符

li = [1,2,3,4]
print("in" if 5 in li else "not in")

如果判断为是的执行内容 if 条件 else 判断为不是的执行内容

数据类型

进制

十进制

用0-9、正负号表示的证书为十进制整数

八进制

以数字0和字母o或O开头,用0-7、正负号表示的整数为八进制整数

如 0o27、0O36、-0o35等等

十六进制

以数字0和字母x或X开头,用数码0-9、a-f或A-F、正负号表示的整数为十六进制整数。

如 0X59、0xa8、-0X39等等

二进制

以数字0和字母b或B开头,用数码0和1、正负号表示的整数为二进制整数。

如 0b10111、-0B1101、0b0等等

数字类型

数字类型是不可变类型。也就是说当你重新赋予a的数字的时候,计算机会重新分配一个内存地址给它。

>>> a =1
>>> id(a)
2403117590832
>>> a =2
>>> id(a)
2403117590864

python中数字类型分为:整数、浮点数和复数

python中为了让内存的消耗不要那么大,所以在一开始就使用了一种叫做小整数对象池的一种方式,用来将频繁引用的数字分配上固定的内存地址

小整数对象池的范围是:-5到256 双闭合的区间,也就是包含-5和256

整数(int)

通常被称为整型,数值为正或者负,不带小数点。

通常用十进制表示数字,但有事我们也会用八进制或十六进制来表示

​ 十六进制用0x前缀和0-9,a-f表示,例如:0xff00

​ 八进制用0o前缀和0-7表示,例如:0o45

python的整数长度为32位,并且通常是连续分配内存空间的。例如上面代码块中的,a=1和a=2时相差了32位。

python中进制转换的方法:

​ 从十进制转十六进制是用hex

​ 从十进制转八进制是用oct

​ 从十进制转二进制是用bin

浮点数(float)

浮点数就是小数

对于很大或者很小的浮点数,一般使用科学计数法来表示。

复数(complex)

复数由实数部分和虚数部分构成,可以用a+bj,或者complex(a,b)表示,复数的实部a和虚部b都是浮点。

基本不用

Guess you like

Origin blog.51cto.com/14784139/2668715