python运算符与基本数据类型

本文详细介绍了Python中的字符串操作,包括成员运算、比较运算、逻辑运算及其结果的布尔值特性。此外,还讨论了字符串的基本方法,如大小写转换、切片、索引等,并展示了如何生成随机验证码。同时,提到了Python中的数字类型和字符串的规范化格式。
摘要由CSDN通过智能技术生成


in not in 成员运算

结果是布尔值

#正 字符
#正负 子序列
name = "正负正"
if "正正" in name:
	print("ok")
else:
	print("Eroor")

比较

结果是布尔值
== > < >= <= != <>

and or not 逻辑

一个一个算 平时推荐使用括号
False and →结果False
True or →结果True

整体注释

选中然后 :ctrl+?

布尔值

真 假
在python中,真为True,假为False。
while True:
还可以通过in 或not in表达出真假
if 后面的判断语句本身就是True或False

not False

小结

比较、逻辑、成员运算 的结果是布尔值

基本数据类型

数字
a = 123
py3里都是int型
py2里范围分 int 和l ong int
字符串
列表
元组
字典
布尔值

数字

int

#字符串转换成整型
a = "123"
b = int(a)
#设置base
num = "b"
c = int(num,base=16)
#至少用几位二进制数字表达 
age = 5
d = age.bit_length()

字符串

test = "alex"
#首字母大写
v1 = test.capitallize()
#所有字符转小写
v2 = test.casefold()  #更牛逼
v3 = test.lower()       #熟知的英文
#设置宽容,并将内容居中def center(self,width,fillchar=None)
#fillchar 只支持填一个字符
v4 = test.center(20,"*")
#计算当前字符串中的子序列或字符有多少个  def count(self,sub,start=None,end=None)
test = "abcababd"
v5 = test.count("a")
#以什么为结尾或开始,返回布尔值
test="alex"
v6 = test.endswith("")
v7 = test。startswith("")
#寻找子序列,从前往后,返回第一个查找到的位置,返回-1时没找到。def find(self,sub,start=None,end=None)
test="alexalex"
test.find("ex")
>2
#找不到报错
test.index("8")
#序列化
format
test = ' i am {name}'
v8 = test.format(name='alex')
print(v8)
>i am alex
test = " i am {0},age{1}"
v9 = format('alex',19)
print(v9)
>i am alex,age19
test = ' i am {name}'
v10 = test.format_map({"name":"alex"})
print(v10)
>i am alex
#是否只包含字母和数字,返回布尔值
test = "abc890"
v11 = test.isalnum()
#6个一组,包含\t,就让\t按数字补齐
s = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"
s.expandtabs(20)
输出
username            email               password
laiying				ying@q.com			123
laiying				ying@q.com			123
#是否为字母
test = "abc"
test.isalpha()
True
#是否为数字
test = "②"
v1 = test.isdecimal()
v2 = test.isdigit()
False
True
s = "二"
v0 = s.isdecimal()
v1 = s.isdigit()
v2 = s.isnumeric()
print(v0,v1,v2)
>False False True
#字母,数字,下划线
a = "abc"
v = a.isidentifier()
print(v)
#是否存在不可直接显示的字符 \t \n
s1 = "123\n456"
s2 = "123\t456"
v1 = s1.isprintable()
v2 = s2.isprintable()
print(v1,v2)
False False
#是否全部都是空格
s = "  "
v = s.isspace()
print(v)
True
#是否为标题 首字母都大写 不能怕短中文
s = "Aa Bb Cc"
v =  s.istitle()
#转换成标题
s1 = " aa bb cc"
v1 = s.title()
print(v,v1)
True Aa Bb Cc
# join 拆解字符串元素,按照指定字符拼接  (重要的)
s = "你是风儿我是沙"
t = " "
v = t.join(s)
v1 = "_".join(s)
print(v)
print(v1)
你 是 风 儿 我 是 沙
你_是_风_儿_我_是_沙
#设置宽度,填充一个字符,默认空格
s =  "alex"
v0 = s.center(20,"中")
v1 = s.ljust(20,"左")
v2 = s.rjust(20,"右")
print(v0)
print(v1)
print(v2)
中中中中中中中中alex中中中中中中中中
alex左左左左左左左左左左左左左左左左
右右右右右右右右右右右右右右右右alex
#设置宽度,填充0
s = "alex"
v = s.zfill(20)
print(v)
0000000000000000alex
#是否小写,转换小写
test = "Alex"
v1 = test.islower()
v2 = test.lower()
print(v1,v2)
False alex
#是否大写,转换大写
test = "Alex"
v1 = test.isupper()
v2 = test.upper()
print(v1,v2)
False ALEX
去除左右指定子序列或字符,默认空白   ,优先最多匹配
test = "\n\t alex"
v = test.lstrip()
#rstrip 右
#strip  两边
print(v)
test = "xalex"
v = test.lstrip("99xa33")
print(v)
 maketrans  和translate 一起用  替换
test = "qwert"
test1 = "12345"
v = "qqqssswwwssseeesssrrrsssttt"
m = str.maketrans("qwert","12345")
new_v = v.translate(m)
print(new_v)
test = "123s456s789s000"
v = test.partition("s")  #永远分割成3份
print(v)
v1 = test.rpartition("s")
print(v1)
v3 = test.split("s") #自己拿不到s,可设置step
print(v3)
#正则表达式,相当于partition和split的合集
是否保留换行  ,默认False
test = "123\n456\n789"
v = test.splitlines()
print(v)
test = "back123456"
v0 = test.startswith("bac")
v1 = test.endswith("56")
print(v0,v1)
大小写转换
test = "alex"
v = test.swapcase()
print(v)
#索引,获取字符串中的某一个字符
test = "alex"
v = test[0]
print(v)
#切片
v1 = test[0:-1] #前币后开
print(v1)
#len()  python2中如果字符是中文,一个中文字符输出3位
test = "12345"
v = len(test)
print(v)
#对于列表,计算,分割了几个元素
#for i in str
test = "123123123"
#指定替代某个字符
v = test.replace("3","bb",2)
print(v)
12bb12bb123
#range创建连续数字,可以设置step
v0 = range(0,100,5)
print(v0)
v = range(100)
print(v)
for item in v0:
    print(item)
    

注意:索引 切片 for len 几乎所有数据类型都能使用 join有的可能会用到
字符串做+或修改都会重新再生成
字符串一旦创建就不可修改
在这里插入图片描述
因为内存是连续的空间

将文字对应的索引打印出来

#将文字对应的索引打印出来
test = input(">>>")
l = len(test)
r = range(0,l)
for item in r:
    print(item,test[item])
for item in range(0,len(test)):
    print(item,test[item])

随机验证码

#随机验证码
def check_code():
    import random
    checkcode = ''
    for i in range(4):
        current = random.randrange(0,4)
        if current != i:
            temp = chr(random.randint(65,90))
        else:
            temp = random.randint(0,9)
        checkcode += str(temp)
    return checkcode
code = check_code()
print(code)

5.True False
空字符串 ‘’ 0 → False
“ ” 和其他 → True
s = “”
bool(s)
6.只要能执行 for i in : ≈可迭代对象
str int bool 统称为类 创建出来的叫做对象
7.python2中range立即创建 python3中range for循环时才一个个创建
python2中的xrange=python3中range
for i in range(100,0,-1)
print(i)

split出来的是列表对象

value = "5+9"
v1 = value.split("+")
print(type(v1))
v2,v3 = value.split("+")
print(type(v2))
<class 'list'>
<class 'str'>

pycharm规范格式

code reformat code

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值