1、运算符
结果是值:
算数运算 a = 10 * 10
赋值运算 a = a + 1 a+=1
结果是布尔值:
比较运算 a = 1 > 5
逻辑运算 a = 1>6 or 1==1
成员运算 name = “b” in "abc"
user = "alex" pwd = "123" v = user == 'alex' and pwd == "123" or 1 == 2 and pwd == "99854" and 1 == 2 print(v)
补充:
有括号先计算括号内
执行顺序:
从前到后 结果
True or ==> True
True and ==> 继续走
False or ==> 继续走
False and ==> False
2、基本数据类型
数字 int , 所有功能,都放在int里
- int
将字符串转换为数字
a = "123" print(type(a),a) b = int(a) print(type(b),b)
num = "0011" v = int(num, base=16) print(v) 把这个字符串按照16进制转换成10进制,没有base把这个字符直接转化成10进制
- bit_lenght
# 当前数字的二进制,至少用n位表示
r = age.bit_length()
字符串 str
# test = "aLex" # 首字母大写 # v = test.capitalize() # print(v)
# 所有变小写,casefold更牛逼,很多未知的对相应变小写 # v1 = test.casefold() # print(v1) # v2 = test.lower() # print(v2)
# 去字符串中寻找,寻找子序列的出现次数,可以设置起始位置结束位置 # test = "aLexalexr" # v = test.count('ex') # print(v) # test = "aLexalexr" # v = test.count('ex',5,6) #可以设置起始位置结束位置 # print(v)
# test = "alex" # v = test.endswith('ex') # 以什么什么结尾 # v = test.startswith('ex') # 以什么什么开始 # print(v)
*** # 从开始往后找,找到第一个之后,获取其未知 # test = "alexalex" # 未找到 -1 # v = test.find('ex') # v = test.find('ex',b,8) 指定的位置 > 或 >= # print(v)
# index找不到,报错 忽略
# test = "alexalex"
# v = test.index('8')
# print(v)
# 格式化,将一个字符串中的占位符替换为指定的值 # test = 'i am {name}, age {a}' # print(test) # v = test.format(name='alex',a=19) # print(v) # test = 'i am {0}, age {1}' # print(test) # v = test.format('alex',19) # print(v)
# 格式化,传入的值 {"name": 'alex', "a": 19} # test = 'i am {name}, age {a}' # v1 = test.format(name='df',a=10) # v2 = test.format_map({"name": 'alex', "a": 19})
# 字符串中是否只包含 字母和数字 # test = "123" # v = test.isalnum() # print(v)
#是否是字母,汉字 #test="asdf" #v = test.isalpha() #print(v)
#当前输入是否是数字 test = "123②二" v1 = test.isdecimal() v2 = test.isdigit() #可以支持殊数字 v3 = test.isnumeric() #可以支持汉字的数字
print(v1,v2,v3)
#字母,数字,下划线 : 标识符 def class a = "_123" v = a.isidentifier() print(v)
#是否为小写 test = "Wsd" v = test.islower() print(v)
#是否存在不可显示的字符 #\t 制表符 #\n 换行符 test = "oiuas\tjdjs" v = test.isprintable() print(v)
#判断是否全部是空格 test = " " v = test.isspace() print(v)
#判断是否为标题 test = "Return True if S is a titlecased string and there is at least one" v1 = test.istitle() #判断是否为标题 print(v1) v2 = test.title() #变成标题 print(v2) v3 = v2.istitle() #再次判断 print(v3)
*** #将字符串中的每一个元素按照指定分隔符进行拼接 test = "你是风儿我是沙" print(test) v1 = " ".join(test) print(v1) v2 = "❤".join(test) print(v2)
# test = "12345678\t9" # v = test.expandtabs(6) #断距6,6个一组 # print(v,len(v)) #test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123" #v = test.expandtabs(20) #print(v)
# 设置宽度,并将内容居中 # 20 代指总长度 # * 空白未知填充,一个字符,可有可无 # v = test.center(20,"中") # print(v) #把内容放左边,右边填充 #test = "alex" #v = test.ljust(20,"*") #print(v) #把内容放在右边,左边填充 #test1 = "alex" #v1 = test1.rjust(20,"*") #print(v1)
#判断是否全部是大小写和转换为大小写 test = "Alex" v1 = test.islower() # 判断是否是小写 v2 = test.lower() #转换为小写 print(v1,v2) v3 = test.isupper() #判断是否是大写 v4 = test.upper() #转换为大写 print(v3,v4)
#test = " alex " #test.lstrip() #test.rstrip() #test.strip() #去除左右空白 #v = test.lstrip() #v = test.rstrip() #v = test.strip() #print(v) #test = "\nalex" #v = test.lstrip() #v = test.rstrip() #v = test.strip() #print(v) #去除\n \t #test = "xalex" #v = test.lstrip('xa') #v = test.rstrip('9aexa') #v = test.strip('xa') #print(v) #移除指定字符串 #有限最多匹配
#v = "abcdefghigklmn" #m = str.maketrans("abcde", "12345") #new_v = v.translate(m) #print(new_v) #替换
#test = "testasdsddfg" #v = test.partition('s') #只能分成3个元素 #print(v) #v1 = test.rpartition('s') #从右边分 #print(v1) #v = test.split('s',2) #可以分成指定个元素,但没有分隔符 #v = test.rsplit() #print(v)
***#分割,只能根据,True, false: 是否保留换行 #test = "asdfg\nfdjffjdfjkf\nfggjhfjgjgj" #v = test.splitlines(True) #print(v)
#判断以XXX开头,以XXX结尾 #test = "backend 1.1.1.1" #v = test.startswith('a') #print(v) #test.endswith('a')
#大小写转换 #test = "alex" #v = test.swapcase() #print(v)
#替换 test = "alexalexalex" #v = test.replace("ex","bbb") #print(v) v = test.replace("ex","bbb",2) print(v)
##############必须会的7个基本魔法################
jion split find upper lower replace
##############灰魔法############################
#索引,下标,获取字符串中的某一字符 #test = "alex" #v = test[3] #print(v)
#切片 #v = test[0:1] # >=0 , <1 #print(v)
#python3,len获取当前字符串中由几个字符组成 #v = len(test) #print(v) #li = [11,22,33,44,55,66,"asdf"] 按逗号计算数量 #v = len(li) #print(v) #一个字一个字的拿出来 test = "妹子有种冲我来" #index = 0 #while index < len(test): # v = test[index] # print(v) # index += 1 #print "======" #for循环 for i in test: print(i)
#帮助创建连续的数字,也可以设置步长来指定不连续的数字 #v = range(0,100) #v = range(0,100,5) #for item in v: # print(item)
#将文字对应的索引打印出来 #test = input(">>>") #print(test) #l = len(test) #print(l) #r = range(0,l) #for item in r: # print(item,test[item]) test = input(">>>") for item in range(0,len(test)): print(item, test[item])
###################1个深灰魔法#################
#字符串一旦创建,不可修改 #一旦修改或者拼接,都会造成重新生成字符串 #name = "xiaoming" #age = "18" #info = name + age #print(info)