Python Basics-02 Operations

Conversion

representation of integers

Integers are integers, and the computer can only store binary 0 and 1.
In order to facilitate the representation of data, the computer also supports octal and hexadecimal

Binary octal hexadecimal decimal Python can represent

a = 98  # 默认数字都是十进制的数字

b = 0b10110110  # 以0b开头的数字都是二进制
print(b) # 当使用print语句打印一个数字的时候,默认也是用十进制打印输出的
# 二进制里最大的数是1

c = 0o34 # 以0o开头的数字是八进制的数字
#八进制里最大的数是7

d = 0x23 # 以0x开头的数字是十六进制
#十六进制是0-9,a-f

decimal to binary conversion

Use the method of decomposing prime factors to find the binary number of a certain value.
In Python, you can use the bin built-in function to convert

Binary, octal, decimal, hexadecimal conversion

binary<==>decimal
binary==>octal
binary==>hexadecimal

  1. Decimal: 23 -> 23
  2. Convert to binary using decomposed prime factors: 0001 0111 -> 0b00010111
  3. Binary to octal: convert to octal in groups of three: 010 111 -> 0o27
  4. Binary to hexadecimal: convert to hexadecimal in groups of four: 0001 0111 -> 0x17

Base conversion using built-in functions

binary -> bin
octal -> oct
hex -> hex

a = 23
print(bin(a))
print(oct(a))
print(hex(a))

# 0b10111
# 0o27
# 0x17

data type conversion

Reasons for data type conversion: Different data types have different calculation methods
Convert one type of data to other types of data

int() --> Convert the data type to int
int() When the string is legal, you can add a parameter to specify the converted base, 8, 16. The default is converted to decimal

str() --> Convert the data type to str

bool() --> Convert data type to bool
number, only number 0 is converted to Boolean value is False, other numbers are converted to Boolean value is True In
string, only empty string can be converted to False, other strings are converted to True
None is converted to Boolean value is False
Empty list, empty tuple, empty dictionary, empty collection is converted to Boolean value is False

float() --> Convert the data type to float

implicit type conversion

if 3:
    print('good')

operator

arithmetic operator

1. + 加号
2. - 减号
3. * 乘号
4. / 除号
5. // 取整 #向下取整
6. % 取余
7. ** 指数
8. () 小括号(提高运算优先级)

In Python, division of two integers results in a float

Use of Arithmetic Operators in Strings

Strings only support addition and multiplication operators

Addition operator:
It can only be used for two string types of data, used to splice multiple strings.
Addition operations cannot be performed between numbers and strings

print('hello' + 'world') # 将多个字符串拼接为一个字符串

Multiplication operation:
can be used between numbers and strings to repeat a string multiple times

print('hello' * 2) # hellohello

assignment operator

  • In computer programming, we call = the assignment operator, which is different from the equal sign in mathematics.
  • The function of the equal sign (assignment operator) is to assign the value on the right side of the equal sign to the left side of the equal sign
  • The left side of the equal sign cannot be a constant or an expression
符合赋值运算符
+= 
-=
*=
/=
**=
//=
%=
  • Variables connected by equal signs can be assigned
a = b = c = d = 'hello'
  • Using the following method to assign values ​​to multiple variables actually unpacks the tuples. When unpacking, the number of variables is inconsistent with the number of values, and an error will be reported.
  • Variables can be made variable length using *
m,n = 3,5 #拆包
m,*n,o = 1,2,3,4,5,6 #可变长度

comparison operator

> 大于
< 小于
>= 大于等于
<= 小于等于
!= 不等于
== 判断==号左右两边是否相等

Use of comparison operators in strings

  • Comparison operators are used between strings, which will be compared one by one according to the encoding value of each character
  • ASCII code table az AZ
  • When a result is compared between a certain bit, the comparison is no longer made.
print('a' > 'b') # False
print('abc' > 'b') # False
  • Between numbers and strings, the result of == operation is False, the result of != is True, and other comparison operations are not supported

Logical Operators

and 逻辑与
or 逻辑或
not 逻辑非
  • Logic and rules: As long as one operation result is False, the result is False; only when all operation results are True, the result is True
  • Logical OR rule: As long as one operation result is True, the result is True; only when all operation results are False, the result is False
  • Logical NOT Budget: Negate, True ==> False, False ==> True

Short circuit and value of logic operation

4 > 3 and print('hello world')  # hello world
4 < 3 and print('hello world')  # False(不执行print)
# 逻辑与运算的短路问题

4 > 3 or print('哈哈哈')  #True(不执行print)
4 < 3 or print('嘿嘿嘿')  # 嘿嘿嘿
# 逻辑或运算的短路问题
  • The result of a logical operation is not necessarily a Boolean value
  • When the logical AND operation is performed, the first value is False; if all operations are True, the last value is taken
print(3 and 3 and 0 and 'hello')  # 0(取第一个为False的值)
  • When the logical OR operation is used to obtain values, the first value that is True is taken; if all operations are False, the last value is taken
print(0 or [] or 'lisi' or 5 or 'ok')  # lisi(取第一个为True的值)

bitwise operator

& 按位与
| 按位或
^ 按位异或
<< 按位左移
>> 按位右移
~ 按位取反
  • Bitwise operations, first convert the ones that need to be compared to binary
  • Bitwise AND operation: 1 if both are 1, otherwise 0
  • Bitwise OR operation: as long as one is 1, it is 1
  • Bitwise XOR operation: same as 0, different as 1
  • Bitwise left shift: Complement the 0 of the X bit on the right side, which can be calculated by a << n ==> a*(2 ** n)
  • Bitwise right shift: delete X bits on the right, which can be calculated by rounding after a >> n ==> a/(2 ** n)
  • Bitwise inversion: turn 0 into 1, 1 into 0, complement form

bitwise operator practice

  • Use bitwise operators to get 0xF0384Ethe RGB value of the hexadecimal color and print it out in decimal form
color = 0xF0384E
red = color >> 16
green = (color >> 8) ^ (red << 8)
blue = ((red << 16) + (green << 8) ) ^ color

print(hex(red))
print(hex(green))
print(hex(blue))
print(red)
print(green)
print(blue)

# 0xf0
# 0x38
# 0x4e
# 240
# 56
# 78

operator precedence

Operational priority: **higher than * / % //higher + -, it is recommended to use () to handle operator priority.
The priority of logical operators: not > and > or

Guess you like

Origin blog.csdn.net/Lz__Heng/article/details/130104998