吉林省住房建设安厅网站,网站推荐几个,公众号开发 网站建设,学院网站的作用位运算1、原码、反码和补码计算机内部使用补码来表示2、按位运算实现快速计算(1) 通过^(异或)快速交换两个整数。a^bb^aa^b(2) 通过a(-a)快速获取a的最后为1 位置的整数。00 00 01 01 - 511 11 10 11 - -5- - -00 00 00 01- 14、利用位运算实现整数集合一…位运算1、原码、反码和补码计算机内部使用补码来表示2、按位运算实现快速计算(1) 通过^(异或)快速交换两个整数。a^bb^aa^b(2) 通过a(-a)快速获取a的最后为1 位置的整数。00 00 01 01 - 511 11 10 11 - -5- - -00 00 00 01- 14、利用位运算实现整数集合一个数的二进制表示可以看作是一个集合(0表示不在集合中1表示在集合中)。例如集合{1348}可以表示成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 补 -~aa 交 b-aba 并 b-a|ba 差 b-a(~b)注意整数在内存中是以补码的形式存在的输出也是按照补码输出的。Python中整型是不限制长度的不会超范围溢出。得到负数(十进制数)的补码的方式将其与十六进制数0xffffffff进行按位与操作再用bin()进行输出。条件语句1、if语句if expression:expr_true_suiteexpression条件表达式可以通过布尔操作符andor和not实现多重条件判断if 2 1 and not 2 3:print(Correct Judgement!)# Correct Judgement!2、if-else语句if expression:expr_true_suiteelse:expr_false_suite3、if-elif-else语句if expression1:expr1_true_suiteelif expression2:expr2_true_suite..elif expressionN:exprN_true_suiteelse:expr_false_suiteelif即为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#A4、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 abcdwhile string:print(string)string string[1:]# abcd# bcd# cd# d字符串中的切割函数(slice(startstop[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 O4、for-else循环for 迭代变量 in 可迭代对象:代码块else:代码块5、range()函数range([start,] stop[, step1])start 起始地点默认从0开始stop 结束位置不包含stopstep 步长默认为1for i in range(1, 10, 2):print(i)# 1# 3# 5# 7# 96、enumerate()函数将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列同时列出数据和数据下标一般用在 for 循环当中。enumerate(sequence, [start0])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, start1)) # 下标从 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():passdef 用于函数定义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 0x0000025BE511CC48print(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 0x0000007A0B8D01B0print(next(e)) # 0print(next(e)) # 1for each in e:print(each, end )# 2 3 4 5 6 7 8 9