运算符,for循环

运算符:
算术运算
比较运算 = >
赋值运算:a = b += -=
逻辑运算:and or not

优先级:
()> not > and > or

1,运算符两边全部为比较运算
1 > 2 and 3 < 4 or 2 > 6
2,运算符两边全部为数字
x or y x is True,return x

3,运算符两边是数字或者是比较运算
1 or 3 > 2 and 4 < 5 or 6 and 2 < 7
成员运算:
in not in

int
'''
十进制:
1 23 100 10000
1.23 1.56 3.1415926

二进制:
01010101010101

八进制:
pass

十六进制:
0 1 2 3 4 5 6 7 8 9 A B C D E F
pass
16: 1 * 16 + 6 = 22
...........

'''

十进制与二进制的转化。

二进制 -----> 十进制

'''
0010 0111
1 * 2**0 + 1 * 2**1 + 1* 2**2 + 1 * 2**5 = 39

'''
十进制转化成二进制
'''
42 ------> ? 101010

'''

int
i = 1000
print(i.bit_length())
'''
该int 转化成的二进制的有效位数。
1 0000 0001
2 0000 0010
3 0000 0011
4 0000 0100
'''

bool
True False
int str bool
int str
int bool

str bool

str ---> bool 非空即True
s1 = 'fsdafa'
s1 = '太白'
s1 = ' '
s1 = ''
print(bool(s1))

bool ---> str 没有意义
print(str(True),type(str(True)))

字符串的索引与切片
s = 'python脱产班20期'
每个字符都有对应的号码。
通过索引(切片)出来的内容都是字符串类型。
1,通过索引(下标) 号牌 找到对应的元素。
s1 = s[0]
print(s1,type(s1))
s2 = s[2]
print(s2)
s3 = s[-1]
print(s3)
s4 = s[-2]
print(s4)
s = 'python脱产班20期'

按照切片找值
顾头不顾腚
s1 = s[:6] # 从0开始可以省略不写
print(s1)
s2 = s[6:9]
print(s2)
s3 = s[:] # 从头取到尾
print(s3)
s4 = s[6:]
print(s4)

切片+步长
s5 = s[:5:2]
print(s5)


反向取值必须加反向步长。
s6 = s[-1:-4:-1]
print(s6)

s7 = s[-1:-6:-2]
print(s7)

[起始索引: 结尾索引+1: 步长] # 切片取值


字符串的常用操作方法

大前提:对字符串的任何操作都是产生一个新的字符串,与原字符串没有关系

s = 'taiBai'

capitalize 首字母大写 **
s1 = s.capitalize()
print(s1)
print(s)

upper() lower() ***
s2 = s.upper()
print(s2)
举例:
code = 'AeRf'.upper()
username = input('请输入用户名:')
your_code = input('请输入验证码:').upper()
if your_code == 'AERF' or your_code == 'aerf' or .... :
if your_code == code:
if username =='alex':
pass
else:
print('验证码输入有误')

s = 'dklwfa'
print(s[100])
'''利用while循环 依次打印字符串的每个字符
len()
d s[0]
k s[1]
l
。。。
a s[count]

'''
print(s[0])
print(s[1])
print(s[5])

s = 'dkfdsafdasfdasfdaslsfa'
最后一个元素的索引值与总长度有关
index = 0
while index < len(s):
print(s[index])
index += 1

s = 'dkfdsafdasfdasfdaslsfa'
for 循环:有限循环
'''
for 变量 in iterable:
pass
'''
for i in s:
print(i)
if i == 's':
break

break
continue

for else: 用法与while else相同

猜你喜欢

转载自www.cnblogs.com/guoqiming/p/10538774.html