位运算

1、原码、反码和补码

计算机内部使用补码来表示

2、按位运算实现快速计算

(1) 通过^(异或)快速交换两个整数。

a^=b

b^=a

a^=b

(2) 通过a&(-a)快速获取a的最后为1 位置的整数。

00 00 01 01 -> 5

&

11 11 10 11 -> -5

- - -

00 00 00 01-> 1

4、利用位运算实现整数集合

一个数的二进制表示可以看作是一个集合(0表示不在集合中,1表示在集合中)。

例如:

集合{1,3,4,8},可以表示成01 00 01 10 10 二对应的位运算也就可以看作是对集合进行的操作。

例:

a=[01101001],从右边数起,第0、3、5、6位是1,所以就表示了0、3、5、6这4个数

元素与集合的操作:

a|(1<把i插入到集合中

a&~(1<把i从集合中删除

a&(1<判断i是否属于该集合(零不属于,非零属于)

集合之间的操作:

a 补 ->~a

a 交 b->a&b

a 并 b->a|b

a 差 b->a&(~b)

注意:

整数在内存中是以补码的形式存在的,输出也是按照补码输出的。

Python中整型是不限制长度的不会超范围溢出。

得到负数(十进制数)的补码的方式:将其与十六进制数0xffffffff进行按位与操作,再用bin()进行输出。

条件语句

1、if语句

if expression:

expr_true_suite

expression条件表达式可以通过布尔操作符and,or和not实现多重条件判断

if 2 > 1 and not 2 > 3:

print('Correct Judgement!')

# Correct Judgement!

2、if-else语句

if expression:

expr_true_suite

else:

expr_false_suite

3、if-elif-else语句

if expression1:

expr1_true_suite

elif expression2:

expr2_true_suite

.

.

elif expressionN:

exprN_true_suite

else:

expr_false_suite

elif即为else if,用来检查多个表达式是否为真,并在为真时执行特定代码块中的代码。

temp = input('请输入成绩:')

source = int(temp)

if 100 >= source >= 90:

print('A')

elif 90 > source >= 80:

print('B')

elif 80 > source >= 60:

print('C')

elif 60 > source >= 0:

print('D')

else:

print('输入错误!')

#请输入成绩:99

#A

4、assert关键词

asssert关键词又称为"断言",该关键词后边的条件为False时,程序自动崩溃并抛出AssertionError的异常。

例子:

my_list = ['lsgogroup']

my_list.pop(0)

assert len(my_list) > 0

# AssertionError

循环语句

1、while循环

while 布尔表达式:

代码块

while后的布尔表达式写入一个非零整数时,视为真值,执行循环体。写入0时视为假值,不执行循环体。

也可换做str、list或其他序列。长度非零则为真,否则为假。

string = 'abcd'

while string:

print(string)

string = string[1:]

# abcd

# bcd

# cd

# d

字符串中的切割函数(slice(start,stop[,step]))

str[起始地址:结束位置:间距]

2、while-else循环

while 布尔表达式:

代码块

else:

代码块

如果在while的代码块中有break语句,则不执行else中的内容。

3、for循环

for 迭代变量 in 可迭代对象:

代码块

例子:

for i in 'ILoveLSGO':

print(i, end=' ') # 不换行输出

# I L o v e L S G O

4、for-else循环

for 迭代变量 in 可迭代对象:

代码块

else:

代码块

5、range()函数

range([start,] stop[, step=1])

start :起始地点,默认从0开始

stop :结束位置,不包含stop

step :步长,默认为1

for i in range(1, 10, 2):

print(i)

# 1

# 3

# 5

# 7

# 9

6、enumerate()函数

将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

enumerate(sequence, [start=0])

sequence :一个序列、迭代器或者其他支持迭代对像。

start :下标起始位置。

返回enumerate(枚举)对象

例子:

seasons = ['Spring', 'Summer', 'Fall', 'Winter']

lst = list(enumerate(seasons))

print(lst)

# [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]

lst = list(enumerate(seasons, start=1)) # 下标从 1 开始

print(lst)

# [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

与for循环结合使用

for i, a in enumerate(A)

do something with a

例子:

languages = ['Python', 'R', 'Matlab', 'C++']

for language in languages:

print('I love', language)

print('Done!')

# I love Python

# I love R

# I love Matlab

# I love C++

# Done!

7、break语句

break语句可以跳出当前所在层的循环。

8、continue 语句

continue终止本轮循环并开始下一轮循环。

9、 pass 语句

pass 语句的意思是“不做任何事”,如果你在需要有语句的地方不写任何语句,那么解释器会提示出错,而 pass 语句就是用来解决这些问题的。

def a_func():

pass

def 用于函数定义

pass是空语句,不做任何操作,只起到占位的作用,其作用是为了保持程序结构的完整性。

10、推导式

列表推导式

[ expr for value in collection [if condition] ]

例子:

x = [-4, -2, 0, 2, 4]

y = [a * 2 for a in x]

print(y)

# [-8, -4, 0, 4, 8]

x = [i for i in range(100) if (i % 2) != 0 and (i % 3) == 0]

print(x)

# [3, 9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99]

a = [(i, j) for i in range(0, 3) for j in range(0, 3)]

print(a)

# [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

元组推导式

( expr for value in collection [if condition] )

例子:

a = (x for x in range(10))

print(a)

# at 0x0000025BE511CC48>

print(tuple(a))

# (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

字典推导式

{ key_expr: value_expr for value in collection [if condition] }

例子:

b = {i: i % 2 == 0 for i in range(10) if i % 3 == 0}

print(b)

# {0: True, 3: False, 6: True, 9: False}

集合推导式

{ expr for value in collection [if condition] }

例子:

c = {i for i in [1, 2, 3, 4, 5, 5, 6, 4, 3, 2, 1]}

print(c)

# {1, 2, 3, 4, 5, 6}

其它

next(iterator[, default]) Return the next item from the iterator. If default is given and the iterator is exhausted, it is returned instead of raising StopIteration.

例子:

e = (i for i in range(10))

print(e)

# at 0x0000007A0B8D01B0>

print(next(e)) # 0

print(next(e)) # 1

for each in e:

print(each, end=' ')

# 2 3 4 5 6 7 8 9