# 算术运算符 /除   //整除  %取模  **幂次
3/4 , 3//4 , 3%4 ,3**4
(0.75, 0, 3, 81)
# 逻辑运算
3>2 and 5<4  ,  3>2 or 5<4  ,not(2>1)
(False, True, False)
# 按位运算
#二进制 # 按位取反
# 二进制数在内存中以补码的形式存储。补码按位取反:二进制每一位取反,0变1,1变0。
# 0000 1001 按位取反  1111 0110 读取数据时用补码(取反+1) 1000 1001+1=1000 1010
bin(9), bin(~9), 9,~9,bin(-9),bin(~(-9))
('0b1001', '-0b1010', 9, -10, '-0b1001', '0b1000')
# 按位运算
# &按位与   |按位或 ^异或 <<左移   >>右移
4&5 , 4|5 ,4^5 ,4<<2 ,4>>2
(4, 5, 1, 16, 1)
# 三元运算符
x,y=3,5
if x<y:
    small=x
else:
    small=y
    
small
3
x,y=4,5
small=x if x<y else y
small
4
# in 存在 not in  不存在  is  是 not is 不是
# is, is not 对比的是两个变量的内存地址
# ==, != 对比的是两个变量的值
letters=['a','b','c','d']
if 'a' in letters:
    print('a exists.')
if 'h' not in letters:
    print('h not exists.')

a1="hello"
a2="hello"
if a1 is a2:
    print(a1,'is',a2)
else:
    print(a1 ,'not is ',a2)
 
if a1 == a2:
    print(a1,'==',a2)
else:
    print(a1 ,'!=',a2)
    
a1=["hello"]
a2=["hello"]
if a1 is a2:
    print(a1,'is',a2)
else:
    print(a1 ,'not is ',a2)
 
if a1 == a2:
    print(a1,'==',a2)
else:
    print(a1 ,'!=',a2)
a exists.
h not exists.
hello is hello
hello == hello
['hello'] not is  ['hello']
['hello'] == ['hello']
# 运行一下结果就出来了
set_1 = {"欢迎", "学习","Python"}
print(set_1.pop())
Python
#找到一个整数,输出它二进制长度
x=125
print(x,bin(x),x.bit_length())
125 0b1111101 7
a = 0.00000023
b = 2.3e-7
print(a)  # 2.3e-07
print(b)  # 2.3e-07
2.3e-07
2.3e-07
# 有时候我们想保留浮点型的小数点后 n 位。可以用 decimal 包里的 Decimal 对象和 getcontext() 方法来实现。
import decimal
from decimal import Decimal
a=decimal.getcontext()
print(a)
b=Decimal(1)/Decimal(3)
print(b)
# 保留4位小数
decimal.getcontext().prec=4
c=Decimal(1)/Decimal(3)
print(c)
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[Inexact, Rounded], traps=[InvalidOperation, DivisionByZero, Overflow])
0.3333333333333333333333333333
0.3333
# 布尔 (boolean) 型变量只能取两个值,True 和 False。当把布尔型变量用在数字运算中,用 1 和 0 代表 True 和 False。
print(True+True)
print(True+False)
print(True*True)
2
1
1
# bool 作用在基本类型变量:X 只要不是整型 0、浮点型 0.0,bool(X) 就是 True,其余就是 False。
# bool 作用在容器类型变量:X 只要不是空的变量,bool(X) 就是 True,其余就是 False。
# 对于数值变量,0, 0.0 都可认为是空的。
# 对于容器变量,里面没元素就是空的。
type(0) , bool(1) ,bool(1) ,type('') ,bool(''),bool('python') ,type([]),bool([]),bool([2,4,6])
(int, True, True, str, False, True, list, False, True)
# type() 不会认为子类是一种父类类型,不考虑继承关系。
# isinstance() 会认为子类是一种父类类型,考虑继承关系。如果要判断两个类型是否相同推荐使用 isinstance()。
type(2.3) ,isinstance(2.3,float)
(float, True)
# 转换为整型 int(x, base=10)
# 转换为字符串 str(object='')
# 转换为浮点型 float(x)
type(str(10+10)) ,int('20') ,float('32')
(str, 20, 32.0)
# if语句支持嵌套,即在一个if语句中嵌入另一个if语句,从而构成不同层次的选择结构。
# 【例子】Python 使用缩进而不是大括号来标记代码块边界,因此要特别注意else的悬挂问题。
a=6
if a>2:
    if a>7:
        print("比7大")
else:
    print("切!")  # 结果没有输出
# 特别的可以连续运算
a=33
44>a>22 ,22<a<44
(True, True)
# 举例:输入成绩,判断等次
score=int(input("请输入考试成绩:"))
if 100>=score>=90:
    print("A")
elif 90>score>=80:
    print("B")    
    
elif 80>score>=60:
    print("C")
else:
    print("D")
请输入考试成绩:99
A
# bool表达式循环
str="hello world"
while str:
    print(str)
    str=str[1:]
hello world
ello world
llo world
lo world
o world
 world
world
orld
rld
ld
d
# while...else循环
# while 布尔表达式:
#     代码块
# else:
#     代码块
# 当while循环正常执行完的情况下,执行else输出,
# 如果while循环中执行了跳出循环的语句,比如 break,将不执行else代码块的内容。
# 从1数到5,到5输出结束
a=1
while a<5:
    print(a)
    a+=1
else:
    print(a,"结束了")
1
2
3
4
5 结束了
# for in 输出字符串,用空格间隔
str="helloworld"
for s in str:
    print(s,end=' ')
h e l l o w o r l d
# for in 列表,字典
a=[1,2,3,4,5]
for i in a:
    print(i)
for i in range(len(a)):
    print(a[i])
    
b={"a":2,"b":3,"c":6}
for key,value in b.items():
    print(key,":",value)
1
2
3
4
5
1
2
3
4
5
a : 2
b : 3
c : 6
# enumerate()函数
# enumerate(sequence, [start=0])
# sequence:一个序列、迭代器或其他支持迭代对象。
# start:下标起始位置。
# 返回 enumerate(枚举) 对象,  
s=['春','夏','秋','冬']
ls=list(enumerate(s))
ls   # [(0, '春'), (1, '夏'), (2, '秋'), (3, '冬')]
ls2=list(enumerate(s,start=1))
ls2 # [(1, '春'), (2, '夏'), (3, '秋'), (4, '冬')]
[(1, '春'), (2, '夏'), (3, '秋'), (4, '冬')]
languages = ['Python', 'R', 'Matlab', 'C++']
for language in languages:
    print('I love', language)

for i,language in enumerate(languages,2):
    print(i,"I love ",language)
I love Python
I love R
I love Matlab
I love C++
2 I love  Python
3 I love  R
4 I love  Matlab
5 I love  C++

def a_func():
pass
pass是空语句,不做任何操作,只起到占位的作用,其作用是为了保持程序结构的完整性。
尽管pass语句不做任何操作,但如果暂时不确定要在一个位置放上什么样的代码,可以先放置一个pass语句,让代码可以正常运行。

# 列表推导式
# [ expr for value in collection [if condition] ]
# 举例:10以内的偶数组成的列表
a=[x for x in range(1,10) if x%2==0]
print(a)
# 举例:元组列表
b=[(x,x**2) for x in range(1,10) if x%2==0]
print(b)
# 举例:元组列表  笛卡尔积处理
c=[(x,y,z) for x in range(1,10) if x%3==0 for y in range(11,20) if y%3==0 for z in range(21,30) if z%3==0]
print(c)
[2, 4, 6, 8]
[(2, 4), (4, 16), (6, 36), (8, 64)]
[(3, 12, 21), (3, 12, 24), (3, 12, 27), (3, 15, 21), (3, 15, 24), (3, 15, 27), (3, 18, 21), (3, 18, 24), (3, 18, 27), (6, 12, 21), (6, 12, 24), (6, 12, 27), (6, 15, 21), (6, 15, 24), (6, 15, 27), (6, 18, 21), (6, 18, 24), (6, 18, 27), (9, 12, 21), (9, 12, 24), (9, 12, 27), (9, 15, 21), (9, 15, 24), (9, 15, 27), (9, 18, 21), (9, 18, 24), (9, 18, 27)]
# 异常处理
d={"a":1,"b":2,"c":3,"d":4}
try:
    a=d['f']
except KeyError:
    print("key error")
else:
    print(a)
finally:
    print("总会执行的代码")
key error
总会执行的代码